From a736da57f084017ac17e06cd9db19b1fd166e1a6 Mon Sep 17 00:00:00 2001 From: yo mama Date: Sat, 4 Apr 2015 01:11:47 -0700 Subject: first --- protected/Async.php | 71 +++++++++++++++++++++++++++++++++++++ protected/Cache.php | 50 ++++++++++++++++++++++++++ protected/Settings.php | 31 ++++++++++++++++ protected/Video.php | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++ protected/autoload.php | 15 ++++++++ 5 files changed, 262 insertions(+) create mode 100755 protected/Async.php create mode 100755 protected/Cache.php create mode 100755 protected/Settings.php create mode 100755 protected/Video.php create mode 100755 protected/autoload.php (limited to 'protected') diff --git a/protected/Async.php b/protected/Async.php new file mode 100755 index 0000000..0f677e8 --- /dev/null +++ b/protected/Async.php @@ -0,0 +1,71 @@ +_method = $method; + } + } + + public function Run(){ + $method = $this->_method; + + $this->$method(); + } + + private function error(){ + + } + + private function cacheVideo(){ + + $url = (isset($_POST['url'])) ? $_POST['url'] : null; + + $cache = new Cache(); + + $cache->CacheHttpVideo($url); + + } + + + public static function createAsyncRequest($uri, $params){ + + $type='POST'; + + foreach ($params as $key => &$val) { + if (is_array($val)) $val = implode(',', $val); + $post_params[] = $key.'='.urlencode($val); + } + + $post_string = implode('&', $post_params); + + $domain = Settings::$Domain; + $parts=parse_url($domain . $uri); + + $fp = fsockopen($parts['host'], + isset($parts['port'])?$parts['port']:80, + $errno, $errstr, 2); + + // Data goes in the path for a GET request + if('GET' == $type) $parts['path'] .= '?'.$post_string; + + $out = "$type ".$parts['path']." HTTP/1.1\r\n"; + $out.= "Host: ".$parts['host']."\r\n"; + $out.= "Content-Type: application/x-www-form-urlencoded\r\n"; + $out.= "Content-Length: ".strlen($post_string)."\r\n"; + $out.= "Connection: Close\r\n\r\n"; + // Data goes in the request body for a POST request + if ('POST' == $type && isset($post_string)) $out.= $post_string; + + fwrite($fp, $out); + fclose($fp); + + } +} diff --git a/protected/Cache.php b/protected/Cache.php new file mode 100755 index 0000000..9a2d46e --- /dev/null +++ b/protected/Cache.php @@ -0,0 +1,50 @@ +urlToId($url); + + if(file_exists(Settings::$CachePath . $id . ".part")){ + return false; + } + + $speed = Settings::$CacheDownloadMaxSpeed; + $r = ""; + if($speed){ + $r = " -r $speed "; + } + + $command = Settings::$YoutubeDlCommand . " $r -o '" . Settings::$CachePath . "$id" . "' $url"; + + $output = exec($command); + echo "$output"; + + } + +} \ No newline at end of file diff --git a/protected/Settings.php b/protected/Settings.php new file mode 100755 index 0000000..e568421 --- /dev/null +++ b/protected/Settings.php @@ -0,0 +1,31 @@ +_cache = new Cache(); + } + + public function playVideo($url){ + $id = $this->_cache->urlToId($url); + + if($this->_cache->isCached($id)){ + $filePath = Settings::$CachePath . DIRECTORY_SEPARATOR .$id; + $this->playFileVideo($filePath); + }else{ + + Async::createAsyncRequest("/async.php", array( + "password" => Settings::$DeveloperPassword, + "url" => $url, + "method" => "cacheVideo" + )); + + $this->playHttpVideo($url); + } + } + + public function playFileVideo($filePath){ + + $path = $filePath; + + $size=filesize($path); + + $fm=@fopen($path,'rb'); + if(!$fm) { + // You can also redirect here + header ("HTTP/1.0 404 Not Found"); + die(); + } + + $begin=0; + $end=$size; + + if(isset($_SERVER['HTTP_RANGE'])) { + if(preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) { + $begin=intval($matches[0]); + if(!empty($matches[1])) { + $end=intval($matches[1]); + } + } + } + + if($begin>0||$end<$size) + header('HTTP/1.0 206 Partial Content'); + else + header('HTTP/1.0 200 OK'); + + header("Content-Type: video/mp4"); + header('Accept-Ranges: bytes'); + header('Content-Length:'.($end-$begin)); + header("Content-Disposition: inline;"); + header("Content-Range: bytes $begin-$end/$size"); + header("Content-Transfer-Encoding: binary\n"); + header('Connection: close'); + + $cur=$begin; + fseek($fm,$begin,0); + + while(!feof($fm)&&$cur<$end&&(connection_status()==0)) + { print fread($fm,min(1024*16,$end-$cur)); + $cur+=1024*16; + usleep(1000); + } + die(); + + } + + public function playHttpVideo($url){ + + $command = Settings::$PythonCommand . " " . Settings::$BasePath."protected" ." play_online_video.cgi"; + + header("Location: /play_video_online.cgi?url=$url"); + + } + + +} \ No newline at end of file diff --git a/protected/autoload.php b/protected/autoload.php new file mode 100755 index 0000000..39efbae --- /dev/null +++ b/protected/autoload.php @@ -0,0 +1,15 @@ +