DocumentManagementController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Validator;
  6. use Illuminate\Support\Facades\Hash;
  7. use Illuminate\Support\Carbon;
  8. class DocumentManagementController extends Controller{
  9. private $responseController;
  10. private $encryptionController;
  11. private $functionsController;
  12. public function __construct(){
  13. $this->responseController = new ResponseController();
  14. $this->encryptionController = new EncryptionController();
  15. $this->functionsController = new FunctionsController();
  16. }
  17. public function downloadFile($token, $idUser, $line){
  18. DB::enableQueryLog();
  19. $tokenInfo = DB::table('S002V01TTODE')->where([
  20. ['TODE_NULI', '=', $line],
  21. ['TODE_TOKE', '=', $token]
  22. ])->first();
  23. if(is_null($tokenInfo)){
  24. return $this->responseController->makeResponse(true, 'El token de descarga no existe.', [], 404);
  25. }else if($tokenInfo->TODE_ESTA != 'Activo'){
  26. return $this->responseController->makeResponse(true, 'El token de descarga ya fue utilizado.', [], 401);
  27. }
  28. $token = $this->encryptionController->shortDec($token);
  29. if(!$token){
  30. return $this->responseController->makeResponse(true, 'El token de descarga no está encriptado correctamente.', [], 400);
  31. }
  32. $tokenArr = explode("|", $token);
  33. if(count($tokenArr) != 3){
  34. return $this->responseController->makeResponse(true, 'Estructura de token inválida.', [], 401);
  35. }else if(intval($tokenArr[1]) != $tokenInfo->TODE_LLAL){
  36. return $this->responseController->makeResponse(true, 'Token inválido.', [], 401);
  37. }else if($tokenArr[2] != 'syp'){
  38. return $this->responseController->makeResponse(true, 'Token inválido.', [], 401);
  39. }
  40. $idUser = $this->encryptionController->shortDec($idUser);
  41. if(!$idUser){
  42. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  43. }
  44. $usr = DB::table('S002V01TUSUA')->where([
  45. ['USUA_IDUS', '=', $idUser],
  46. ['USUA_NULI', '=', $line]
  47. ])->first();
  48. if(is_null($usr)){
  49. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  50. }
  51. $id = $tokenArr[0];
  52. $codiArr = explode("=", $id);
  53. $prefArr = explode("-", $codiArr[0]);
  54. $suffArr = explode(".", $codiArr[2]);
  55. $nuli = intval($prefArr[0]);
  56. $como = $prefArr[1];
  57. $cldo = $prefArr[2];
  58. $fecr = $prefArr[3];
  59. $nuse = intval($prefArr[4]);
  60. $nuve = $codiArr[1];
  61. $noar = $suffArr[0];
  62. $exte = $suffArr[1];
  63. $nowStr = Carbon::now('America/Mexico_city')->toDateTimeString();
  64. DB::table('S002V01TTODE')->where([
  65. ['TODE_IDTO', '=', $tokenInfo->TODE_IDTO],
  66. ['TODE_NULI', '=', $line]
  67. ])->update([
  68. 'TODE_ESTA' => 'Usado',
  69. 'TODE_USDE' => $idUser,
  70. 'TODE_FEDE' => $nowStr
  71. ]);
  72. $file = DB::table('S002V01TAFAL')->where([
  73. ['AFAL_NULI', '=', $nuli],
  74. ['AFAL_COMO', '=', $como],
  75. ['AFAL_CLDO', '=', $cldo],
  76. ['AFAL_FECR', '=', $fecr],
  77. ['AFAL_NUSE', '=', $nuse],
  78. ['AFAL_NUVE', '=', $nuve],
  79. ['AFAL_NOAR', '=', $noar],
  80. ['AFAL_EXTE', '=', $exte],
  81. ])->first();
  82. if(is_null($usr)){
  83. return $this->responseController->makeResponse(true, 'El archivo solicitado no existe.', [], 404);
  84. }
  85. return response()->download($file->AFAL_UBIC);
  86. }
  87. public function getDownloadToken($idFile, $idUser, $line){
  88. DB::enableQueryLog();
  89. $idFile = $this->encryptionController->shortDec($idFile);
  90. if(!$idFile){
  91. return $this->responseController->makeResponse(true, 'El ID del archivo requerido no está encriptado correctamente.', [], 400);
  92. }
  93. $idUser = $this->encryptionController->shortDec($idUser);
  94. if(!$idUser){
  95. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  96. }
  97. $usr = DB::table('S002V01TUSUA')->where([
  98. ['USUA_IDUS', '=', $idUser],
  99. ['USUA_NULI', '=', $line]
  100. ])->first();
  101. if(is_null($usr)){
  102. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  103. }
  104. $codiArr = explode("=", $idFile);
  105. $prefArr = explode("-", $codiArr[0]);
  106. $suffArr = explode(".", $codiArr[2]);
  107. $nuli = intval($prefArr[0]);
  108. $como = $prefArr[1];
  109. $cldo = $prefArr[2];
  110. $fecr = $prefArr[3];
  111. $nuse = intval($prefArr[4]);
  112. $nuve = $codiArr[1];
  113. $noar = $suffArr[0];
  114. $exte = $suffArr[1];
  115. $file = DB::table('S002V01TAFAL')->where([
  116. ['AFAL_NULI', '=', $nuli],
  117. ['AFAL_COMO', '=', $como],
  118. ['AFAL_CLDO', '=', $cldo],
  119. ['AFAL_FECR', '=', $fecr],
  120. ['AFAL_NUSE', '=', $nuse],
  121. ['AFAL_NUVE', '=', $nuve],
  122. ['AFAL_NOAR', '=', $noar],
  123. ['AFAL_EXTE', '=', $exte],
  124. ])->first();
  125. if(is_null($file)){
  126. return $this->responseController->makeResponse(true, 'El archivo solicitado no existe.', [], 404);
  127. }
  128. $rand = rand(0, 99999);
  129. $tokenArr = [$idFile, $rand, "syp"];
  130. $tokenStr = join('|', $tokenArr);
  131. $token = $this->encryptionController->shortEnc($tokenStr);
  132. $token = str_replace("+", "=P=", $token);
  133. $token = str_replace("/", "=S=", $token);
  134. $nowStr = Carbon::now('America/Mexico_city')->toDateTimeString();
  135. DB::table('S002V01TTODE')->insert([
  136. 'TODE_NULI' => $line,
  137. 'TODE_TOKE' => $token,
  138. 'TODE_CODO' => $idFile,
  139. 'TODE_LLAL' => $rand,
  140. 'TODE_USRE' => $idUser,
  141. 'TODE_FERE' => $nowStr
  142. ]);
  143. return $this->responseController->makeResponse(false, 'EXITO', ['TOKEN' => $token]);
  144. }
  145. public function getFileInfo($id, $idUser, $line){
  146. DB::enableQueryLog();
  147. $id = $this->encryptionController->shortDec($id);
  148. if(!$id){
  149. return $this->responseController->makeResponse(true, 'El ID del archivo requerido no está encriptado correctamente.', [], 400);
  150. }
  151. $idUser = $this->encryptionController->shortDec($idUser);
  152. if(!$idUser){
  153. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  154. }
  155. $usr = DB::table('S002V01TUSUA')->where([
  156. ['USUA_IDUS', '=', $idUser],
  157. ['USUA_NULI', '=', $line]
  158. ])->first();
  159. if(is_null($usr)){
  160. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  161. }
  162. $idArr = explode("=", $id);
  163. $codiArr = explode("-", $idArr[0]);
  164. $nuli = $line;
  165. $como = $codiArr[1];
  166. $cldo = $codiArr[2];
  167. $fecr = $codiArr[3];
  168. $nuse = intval($codiArr[4]);
  169. $nuve = intval($idArr[1]);
  170. $fileInfo = DB::table('S002V01TAFAL')->select([
  171. 'AFAL_NOAR AS NOMBREARCHIVO',
  172. 'AFAL_EXTE AS EXTENSION',
  173. 'AFAL_TAMA AS PESO',
  174. 'AFAL_USAC AS ACCESO',
  175. 'AFAL_ESTA AS ESTADO'
  176. ])->where([
  177. ['AFAL_NULI', '=', $nuli],
  178. ['AFAL_COMO', '=', $como],
  179. ['AFAL_CLDO', '=', $cldo],
  180. ['AFAL_FECR', '=', $fecr],
  181. ['AFAL_NUSE', '=', $nuse],
  182. ['AFAL_NUVE', '=', $nuve],
  183. ])->first();
  184. if(is_null($fileInfo)){
  185. return $this->responseController->makeResponse(true, 'El archivo solicitado no está registrado.', [], 404);
  186. }else if($fileInfo->ESTADO != 'Activo'){
  187. return $this->responseController->makeResponse(true, 'El archivo solicitado no está disponible.', [], 404);
  188. }
  189. return $this->responseController->makeResponse(false, 'EXITO', $fileInfo);
  190. }
  191. }