Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.49 KB | None | 0 0
  1. <?php
  2.  
  3.   function _arruma_arquivo_legenda_hash($nomearquivo, $path, $debug = false) {
  4.     $caminho = $path;
  5.  
  6.     $arquivo_legendas = "/LTV/Legendas.tv.txt";
  7.     $arquivo_legendasurl = "/LTV/Legendas.tv.url";
  8.     $arquivo_comentarios = "/LTV/comentario.txt";
  9.  
  10.     $ext      = strtolower(substr($nomearquivo, -3));
  11.     $temp_dir = strtolower(substr($nomearquivo, 0, -4));//remove o .zip ou .rar
  12.      
  13.     //cria pasta temporaria
  14.     {
  15.       //mkdir teste_leg
  16.       $cmd  = 'mkdir ' . $caminho . $temp_dir;
  17.      
  18.       // echo $cmd;
  19.       // exit;
  20.  
  21.       $result = false;
  22.       @exec($cmd,$result);
  23.       if($debug) {
  24.         debug($cmd);
  25.         debug($result);
  26.       }
  27.     }
  28.     //extrai .rar ou .zip para a pasta temporaria
  29.     {
  30.       switch($ext) {
  31.  
  32.         case 'zip':
  33.          
  34.           // $cmd  = 'cd ' . $caminho . ';';
  35.           // $cmd .= 'unzip ' . $nomearquivo  . ' -d ./' . $temp_dir;
  36.  
  37.           $cmd = '/usr/bin/unzip ' . $caminho . $nomearquivo  . ' -d ' . $caminho . $temp_dir;
  38.          
  39.           // echo $cmd;
  40.           // exit;
  41.  
  42.           $result = false;
  43.           @exec($cmd,$result);
  44.           if($debug) {
  45.             debug($cmd);
  46.             debug($result);
  47.           }
  48.  
  49.           break;
  50.          
  51.         case 'rar':
  52.           //rar e -y teste_leg.rar ./teste_leg
  53.           // $cmd  = 'cd ' . $caminho . ';';
  54.           // $cmd .= 'rar x -y ' . $nomearquivo  . ' ./' . $temp_dir;
  55.  
  56.           $cmd = '/usr/bin/rar x -y ' . $caminho . $nomearquivo  . ' ' . $caminho . $temp_dir;
  57.  
  58.           // echo $cmd;
  59.           // exit;
  60.  
  61.           $result = false;
  62.           @exec($cmd,$result);
  63.           if($debug) {
  64.             debug($cmd);
  65.             debug($result);
  66.           }
  67.          
  68.           break;
  69.       }
  70.     }
  71.     //cria novo .rar somente com arquivos .txt ou .srt ou .sub ou .ass
  72.     {
  73.  
  74.       $cmd  = 'cd ' . $caminho.$temp_dir . ';';
  75.       $cmd .= 'find . -name "*.txt" -o -name "*.srt" -o -name "*.sub" -o -name "*.ass" | /usr/bin/rar a ' . $temp_dir . '.rar @;';
  76.       //inclui arquivo legendas.tv.url e comentario
  77.       $cmd .= '/usr/bin/rar a -ep ' . $temp_dir . '.rar ' . $arquivo_legendasurl . ' -c -z' . $arquivo_comentarios;
  78.  
  79.       // echo $cmd;
  80.       // exit;
  81.  
  82.       $result = false;
  83.       @exec($cmd,$result);
  84.       if($debug) {
  85.         debug($cmd);
  86.         debug($result);
  87.       }
  88.     }
  89.     if($debug) {
  90.       // exit;
  91.     }
  92.     //remove arquivo original
  93.     {
  94.       $cmd = '/bin/rm -rf ' . $caminho.$nomearquivo;
  95.       $result = false;
  96.       @exec($cmd,$result);
  97.       if($debug) {
  98.         debug($cmd);
  99.         debug($result);
  100.       }
  101.     }
  102.     //sobrescreve rar orginal com novo rar
  103.     {
  104.       // $cmd  = 'cd ' . $caminho . ';';
  105.       // $cmd .= 'rm -rf ' . $temp_dir . '.'.$ext.';';
  106.       // $cmd .= 'mv ' . $temp_dir.DS.$temp_dir . '.rar .';
  107.  
  108.       $cmd = '/bin/mv ' . $caminho.$temp_dir . '/'.$temp_dir.'.rar ' . $caminho;
  109.  
  110.       $result = false;
  111.       @exec($cmd,$result);
  112.       if($debug) {
  113.         debug($cmd);
  114.         debug($result);
  115.       }
  116.     }
  117.     //excluir pasta temporaria
  118.     {
  119.       // $cmd = 'rm -rf ' . $caminho . $temp_dir . ';';
  120.       $cmd = '/bin/rm -rf ' . $caminho . $temp_dir . ';';
  121.       $result = false;
  122.       @exec($cmd,$result);
  123.       if($debug) {
  124.         debug($cmd);
  125.         debug($result);
  126.       }
  127.     }
  128.  
  129.     if($debug) {
  130.       exit;
  131.     }
  132.  
  133.     return $temp_dir . '.rar';
  134.    
  135.   }
  136.  
  137.   function _uploadFileHash($arquivo, $caminho = null, $prefixo)
  138.   {
  139.  
  140.     ini_set("memory_limit","200M");
  141.     set_time_limit(0);
  142.  
  143.     switch ($arquivo['error'])
  144.     {
  145.       case UPLOAD_ERR_INI_SIZE:
  146.         die('Erro: Arquivo muito grande');
  147.       case UPLOAD_ERR_FORM_SIZE:
  148.         die('Erro: Arquivo muito grande');
  149.       case UPLOAD_ERR_PARTIAL:
  150.         die('Erro ao enviar arquivo, tente novamente(UPLOAD_ERR_PARTIAL)');
  151.       case UPLOAD_ERR_NO_FILE:
  152.         die('Erro: Nenhum arquivo enviado');
  153.       case UPLOAD_ERR_NO_TMP_DIR:
  154.         die('Erro ao enviar arquivo, tente novamente(UPLOAD_ERR_NO_TMP_DIR)');
  155.       case UPLOAD_ERR_CANT_WRITE:
  156.         die('Erro ao enviar arquivo, tente novamente(UPLOAD_ERR_CANT_WRITE)');
  157.       case UPLOAD_ERR_EXTENSION:
  158.         die('Erro: Tipo de arquivo inv&aacute;lido.');
  159.     }
  160.  
  161.     if($arquivo["name"] != "")
  162.     {
  163.  
  164.       $timestamp = date("YmdHisu");
  165.       $filenamePieces = explode(".",$arquivo["name"]);
  166.       $extension = $filenamePieces[count($filenamePieces)-1];
  167.  
  168.       // $filename = $this->prefixo_arquivo.$timestamp.".".$extension;
  169.       // When the file is opened and cleaned, it always become a rar
  170.       $filename = $prefixo.$timestamp.".rar";
  171.  
  172.       $filenameMd5 = md5($filename);
  173.  
  174.       $fstTwo = substr($filenameMd5, 0, 2);
  175.       $sndTwo = substr($filenameMd5, 2, 2);
  176.  
  177.       $newPath = "/LTV/files/$fstTwo/$sndTwo/";
  178.  
  179.       while (file_exists($newPath.$filename)) {
  180.         $timestamp = date("YmdHisu");
  181.         $filenamePieces = explode(".",$arquivo["name"]);
  182.         $extension = $filenamePieces[count($filenamePieces)-1];
  183.  
  184.         $filename = $prefixo.$timestamp.".rar";
  185.  
  186.         $filenameMd5 = md5($filename);
  187.  
  188.         $fstTwo = substr($filenameMd5, 0, 2);
  189.         $sndTwo = substr($filenameMd5, 2, 2);
  190.  
  191.         $newPath = "/LTV/files/$fstTwo/$sndTwo/";
  192.       }
  193.  
  194.       if (file_exists($arquivo['tmp_name'])) {
  195.         if (!is_dir($newPath)) {
  196.           if (!mkdir($newPath, 0775, true)) {
  197.             return '';
  198.           }
  199.         }
  200.  
  201.         $moveError = rename($arquivo['tmp_name'], $newPath.$filename);
  202.         if ($moveError) {
  203.           $obj = array();
  204.           $obj["path"] = $newPath;
  205.           $obj["filename"] = $filename;
  206.           return $obj;
  207.         } else {
  208.           return '';
  209.         }
  210.       } else {
  211.         return '';
  212.       }
  213.     }
  214.     else
  215.     {
  216.       return "";
  217.     }
  218.   }
  219.  
  220.  
  221.   function main($json) {
  222.     $data = json_decode($json);
  223.  
  224.     var_dump($data);
  225.  
  226.     $tempFile = $data->file;
  227.     $uid = $data->uid;
  228.     $prefixo = $data->prefixo;
  229.  
  230.     // $tempFile = $data["file"];
  231.     // $uid = $data["uid"];
  232.     // $prefixo = $data["prefixo"];
  233.  
  234.     $errormessage = "";
  235.     $erro = false;
  236.  
  237.     $returnObj = array();
  238.  
  239.     //Tamanho do arquivo
  240.     if($tempFile->size >= 2048000) { //2048000 = 2MB
  241.     // if($tempFile["size"] >= 2048000) { //2048000 = 2MB
  242.       $errormessage .= 'O arquivo da legenda deve ter no máximo 2MB.<br />';
  243.       $erro = true;
  244.     } else {
  245.       //extensao zip ou rar
  246.       $ext = strtoupper(substr($tempFile->name, -3));
  247.       // $ext = strtoupper(substr($tempFile["name"], -3));
  248.       if ($ext == 'RAR' || $ext == 'ZIP') {
  249.         $pathAndFilename = _uploadFileHash(json_decode(json_encode($tempFile), true), "", $prefixo);
  250.  
  251.         $newPath = false;
  252.         $newFilename = false;
  253.         if (is_array($pathAndFilename)) {
  254.           $newPath = $pathAndFilename["path"];
  255.           $newFilename = $pathAndFilename["filename"];
  256.         }
  257.  
  258.         if (!$newFilename || $newFilename == '') {
  259.           $errormessage .= 'Erro ao salvar arquivo, por favor tente novamente.<br />';
  260.           $erro = true;
  261.         } else {
  262.           $newFilename = _arruma_arquivo_legenda_hash($newFilename, $newPath);
  263.           $returnObj["dsc_url_legenda"] = $newFilename;
  264.           $returnObj["dsc_file_path"] = base64_encode($newPath);
  265.           $returnObj["uid"] = $uid;
  266.         }
  267.       } else {
  268.  
  269.         $errormessage .= 'O arquivo da legenda deve estar em formato RAR ou ZIP.<br />';
  270.         $erro = true;
  271.       }
  272.     }
  273.  
  274.     if ($erro) {
  275.       echo date("Y-m-d H:i:s")." Problemas - {$json}\n";
  276.       file_put_contents("/LTV/async-worker.log", date("Y-m-d H:i:s")." Problemas - {$json}\n", FILE_APPEND);
  277.     } else {
  278.       // echo json_encode($returnObj)."\n";
  279.  
  280.       $notificationURL = "http://legendas.tv/background/update_path_and_filename/{$returnObj['uid']}/{$returnObj['dsc_url_legenda']}/{$returnObj['dsc_file_path']}";
  281.       echo $notificationURL."\n";
  282.  
  283.       $ch = curl_init();
  284.       curl_setopt($ch, CURLOPT_URL, $notificationURL);
  285.       curl_setopt($ch, CURLOPT_HEADER, 0);
  286.       curl_exec($ch);
  287.       curl_close($ch);
  288.     }
  289.   }
  290.  
  291. $worker = new GearmanWorker();
  292. $worker->addServer('roberts.legendas.tv');
  293.  
  294. $worker->addFunction("enviar_legenda", function(GearmanJob $job) {
  295.   file_put_contents("/LTV/async-worker.log", date("Y-m-d H:i:s")." Processando novo upload - {$job->workload()}\n", FILE_APPEND);
  296.   if (strlen($job->workload()) > 0) main($job->workload());
  297.   else echo date("Y-m-d H:i:s")." - Nenhum json informado.";
  298. });
  299.  
  300. $worker->work();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement