public function download($id)
 {
     $anexo = Anexo::findFirst($id);
     $response = new Response();
     if ($anexo) {
         $response->setStatusCode(StatusCodes::OK);
         $response->setFileToSend($anexo->getCaminho(), $anexo->getOriginal());
     } else {
         $response->setStatusCode(StatusCodes::NAO_ENCONTRADO);
     }
     return $response;
 }
示例#2
0
 /**
  * Выполняет все плагины по цепочке
  * 
  * @param \Phalcon\Http\Request $request
  * @return string
  */
 public function process(Request $request)
 {
     $response = new Response();
     $pluginResult = new PluginResult();
     foreach ($this->_arPlugins as $plugin) {
         $pluginResult = $plugin->process($request, $pluginResult);
         if ($pluginResult->terminate) {
             break;
         }
     }
     if ($pluginResult->error) {
         $response->setStatusCode($pluginResult->errorCode, $pluginResult->errorMessage);
         $response->setContent($pluginResult->errorMessage);
         return $response;
     } else {
         $response->setStatusCode(200, "OK");
     }
     $callback = $request->getQuery('callback');
     $result = '';
     if ($callback) {
         $response->setHeader('Content-Type', 'application/javascript');
         $result .= $callback . '(';
     } else {
         $response->setHeader('Content-Type', 'application/json');
     }
     $result .= json_encode(array('searchContext' => $pluginResult->searchContext, 'result' => $pluginResult->result));
     if ($callback) {
         $result .= ');';
     }
     if ($pluginResult->fileToSend) {
         $response->setFileToSend($pluginResult->fileToSend, 'data.txt');
     } else {
         $response->setContent($result);
     }
     return $response;
 }
示例#3
0
 public function setFileToSend($filePath, $attachmentName = null, $attachment = true)
 {
     return parent::setFileToSend($filePath, $attachmentName, $attachment);
 }
 public function download($url, $destination = "", $file_name = "")
 {
     // check URL and get token if necessary
     preg_match("/(.*)periscope\\.tv\\/w\\/(.*)/", trim($url), $output_array);
     if (isset($output_array[2])) {
         $periscope_token = $output_array[2];
     } else {
         preg_match("/(.*)watchonperiscope\\.com\\/broadcast\\/(.*)/", trim($url), $output_array);
         if (isset($output_array[2])) {
             try {
                 $watchonperiscope_response = $this->_guzzle->get("https://watchonperiscope.com/api/accessChannel?broadcast_id=" . $output_array[2])->getBody();
             } catch (\GuzzleHttp\Exception\ServerException $e) {
                 throw new \Exception("URL error: Invalid watchonperiscope.com URL", 2);
             }
             $watchonperiscope_json = json_decode($watchonperiscope_response, true);
             if (!isset($watchonperiscope_json["error"])) {
                 preg_match("/(.*)periscope\\.tv\\/w\\/(.*)/", $watchonperiscope_json["share_url"], $output_array);
                 $periscope_token = $output_array[2];
             } else {
                 throw new \Exception("URL error: Invalid watchonperiscope.com URL", 2);
             }
         } else {
             throw new \Exception("URL error: Unsupported URL", 1);
         }
     }
     // construct filename and destination
     if ($file_name == "") {
         try {
             $periscope_details_response = $this->_guzzle->get("https://api.periscope.tv/api/v2/getBroadcastPublic?broadcast_id=" . $periscope_token)->getBody();
         } catch (\GuzzleHttp\Exception\ClientException $e) {
             throw new \Exception("Periscope error: Invalid token", 3);
         }
         $periscope_details_json = json_decode($periscope_details_response, true);
         $periscope_user = $periscope_details_json["user"]["username"];
         $periscope_start_time = $periscope_details_json["broadcast"]["start"];
         $date = substr($periscope_start_time, 0, 10);
         $hours = substr($periscope_start_time, 11, 2);
         $mins = substr($periscope_start_time, 14, 2);
         $file_name = $periscope_user . "_" . $date . "_" . $hours . "_" . $mins . ".ts";
     } else {
         $file_name = rtrim($file_name, ".ts") . ".ts";
     }
     if ($destination == "") {
         $destination = __DIR__ . "/";
     } else {
         $destination = rtrim($destination, "/") . "/";
     }
     // set up cookies
     try {
         $periscope_cookies_response = $this->_guzzle->get("https://api.periscope.tv/api/v2/getAccessPublic?broadcast_id=" . $periscope_token)->getBody();
     } catch (\GuzzleHttp\Exception\ClientException $e) {
         throw new \Exception("Periscope error: Invalid token", 3);
     }
     $periscope_cookies_json = json_decode($periscope_cookies_response, true);
     $replay_url = $periscope_cookies_json["replay_url"];
     $base_url = str_replace("/playlist.m3u8", "", $replay_url);
     $cookies = array();
     foreach ($periscope_cookies_json["cookies"] as $cookie) {
         $cookies[$cookie["Name"]] = $cookie["Value"];
     }
     $cookie_jar = new \GuzzleHttp\Cookie\CookieJar();
     $periscope_cookies = $cookie_jar::fromArray($cookies, "replay.periscope.tv");
     // download playlist and all chunks
     $periscope_playlist_response = $this->_guzzle->get($replay_url, ["cookies" => $periscope_cookies])->getBody()->getContents();
     preg_match_all("/chunk_(.*)\\.ts/", $periscope_playlist_response, $chunk_array);
     $tmp_folder = $destination . "/" . bin2hex(openssl_random_pseudo_bytes(16)) . "/";
     shell_exec("mkdir " . $tmp_folder);
     $path = $destination . $file_name;
     if (!file_exists($path)) {
         shell_exec("cat " . $tmp_folder . $chunk_array[0][0] . " >> " . $path);
     }
     $response = new Response();
     $filetype = filetype($path);
     $filesize = filesize($path);
     while (ob_get_level()) {
         ob_end_clean();
     }
     $response->setHeader("Content-Description", 'File Transfer');
     $response->setHeader("Cache-Control", 'must-revalidate, post-check=0, pre-check=0');
     $response->setHeader("Content-Disposition", 'attachment; filename=' . $file_name);
     $response->setHeader("Content-Type", $filetype);
     $response->setHeader("Content-Length", $filesize);
     $response->setHeader("Content-Transfer-Encoding", 'binary');
     $response->setHeader("Expires", '0');
     $response->setHeader("Pragma", 'public');
     $response->setFileToSend($path, null, false);
     $response->send();
     foreach ($chunk_array[0] as $chunk) {
         $chunk_response = $this->_guzzle->get($base_url . "/" . $chunk, ["cookies" => $periscope_cookies])->getBody()->getContents();
         //, 'stream' => true
         //               $chunk_size = $chunk_response->getHeader('content-length');
         //               $body = $chunk_response->getBody();
         //
         //                while ( ! $body->eof()) {
         //                    echo $body->read($chunk_size[0]);
         //                }
         file_put_contents($tmp_folder . $chunk, $chunk_response);
         $chunk_size = filesize($tmp_folder . $chunk);
         if (file_exists($path)) {
             shell_exec("cat " . $path . ' ' . $tmp_folder . $chunk . " >> " . $path);
         }
         $fh = fopen($tmp_folder . $chunk, "rb");
         echo fread($fh, $chunk_size);
         while (ob_get_level()) {
             ob_end_clean();
         }
         flush();
         fclose($fh);
     }
     // clean up
     shell_exec("rm -rf " . $tmp_folder);
     exit;
     return $destination . $file_name;
 }