summaryrefslogtreecommitdiff
path: root/protected
diff options
context:
space:
mode:
Diffstat (limited to 'protected')
-rwxr-xr-xprotected/Async.php71
-rwxr-xr-xprotected/Cache.php50
-rwxr-xr-xprotected/Settings.php31
-rwxr-xr-xprotected/Video.php95
-rwxr-xr-xprotected/autoload.php15
5 files changed, 262 insertions, 0 deletions
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 @@
+<?php
+
+
+class Async {
+
+ private $_method = 'error';
+
+ public function __construct($method,$password){
+
+ $realPassword = Settings::$DeveloperPassword;
+
+ if($password == $realPassword){
+ $this->_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 @@
+<?php
+/**
+ * Created by IntelliJ IDEA.
+ * User: root
+ * Date: 8/15/13
+ * Time: 11:17 AM
+ * To change this template use File | Settings | File Templates.
+ */
+
+class Cache {
+
+ public function isCached($id){
+ $cachePath = Settings::$CachePath;
+
+ $result = (file_exists($cachePath. DIRECTORY_SEPARATOR .$id)) ? true : false;
+
+ return $result;
+ }
+
+ public function urlToId($url){
+
+ $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $url);
+ //$clean = strtolower(trim($clean, '-'));
+ $clean = preg_replace("/[\/_|+ -]+/", '_', $clean);
+
+ return $clean;
+ }
+
+ public function CacheHttpVideo($url){
+
+ $id = $this->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 @@
+<?php
+/**
+ * Created by IntelliJ IDEA.
+ * User: root
+ * Date: 8/15/13
+ * Time: 11:17 AM
+ * To change this template use File | Settings | File Templates.
+ */
+
+
+class Settings {
+ static $BasePath = "";
+ static $CachePath = "";
+ static $YoutubeDlCommand = "youtube-dl ";
+ static $PythonCommand = "python ";
+ static $DeveloperPassword = "JHdfgJ346Kfgfh345fgjfns435346dkgnksdg469849JKJf";
+ static $Domain = "";
+ static $CacheDownloadMaxSpeed = false; //false for disable
+
+ public static function setConfigs(){
+ $baseDir = dirname(__FILE__) . "/../";
+
+ self::$BasePath = $baseDir;
+ self::$CachePath = $baseDir."cache/" ;
+
+ $_SERVER['REQUEST_SCHEME'] = (isset($_SERVER['REQUEST_SCHEME'])) ? $_SERVER['REQUEST_SCHEME'] : "http";
+
+ self::$Domain = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'];
+
+ }
+} \ No newline at end of file
diff --git a/protected/Video.php b/protected/Video.php
new file mode 100755
index 0000000..3149482
--- /dev/null
+++ b/protected/Video.php
@@ -0,0 +1,95 @@
+<?php
+/**
+ * Created by IntelliJ IDEA.
+ * User: root
+ * Date: 8/15/13
+ * Time: 11:17 AM
+ * To change this template use File | Settings | File Templates.
+ */
+
+class Video {
+
+ private $_cache = null;
+
+ public function __construct(){
+ $this->_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 @@
+<?php
+
+function logging($content){
+ $file = dirname(__FILE__).'/log.txt';
+ file_put_contents($file, $content, FILE_APPEND | LOCK_EX);
+}
+
+
+require_once "Async.php";
+require_once "Cache.php";
+require_once "Settings.php";
+require_once "Video.php";
+
+
+Settings::setConfigs(); \ No newline at end of file