From 278d5f5133517904cb6ad480235b4850ab4f6549 Mon Sep 17 00:00:00 2001 From: Web Addicto Date: Wed, 8 Feb 2017 17:44:22 +0100 Subject: [PATCH 1/5] VimeoPlugin Supports playing of videos on Vimeo. --- src/VimeoPlugin.php | 181 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 src/VimeoPlugin.php diff --git a/src/VimeoPlugin.php b/src/VimeoPlugin.php new file mode 100644 index 0000000..7ec4a66 --- /dev/null +++ b/src/VimeoPlugin.php @@ -0,0 +1,181 @@ + JSON_Encode => Remove " + $url_proxied = str_replace("\"", "", json_encode(proxify_url($url, $this->base_url))); + + // Return data with proxified URLs + return str_replace($matches[1], $url_proxied, $matches[0]); + } + + private function json_parse($matches){ + + // Find and replace all JSON "url" and "src" fields + $matches[0] = preg_replace_callback('/"url":"(.+?)"/', array($this, 'json_src'), $matches[0]); + $matches[0] = preg_replace_callback('/"src":"(.*?)"/', array($this, 'json_src'), $matches[0]); + $matches[0] = preg_replace_callback('/"src_2x":"(.*?)"/', array($this, 'json_src'), $matches[0]); + $matches[0] = preg_replace_callback('/"src_4x":"(.*?)"/', array($this, 'json_src'), $matches[0]); + $matches[0] = preg_replace_callback('/"src_8x":"(.*?)"/', array($this, 'json_src'), $matches[0]); + $matches[0] = preg_replace_callback('/"thumbnail":"(.*?)"/', array($this, 'json_src'), $matches[0]); + $matches[0] = preg_replace_callback('/"thumbnail_2x":"(.*?)"/', array($this, 'json_src'), $matches[0]); + $matches[0] = preg_replace_callback('/"thumbnail_4x":"(.*?)"/', array($this, 'json_src'), $matches[0]); + $matches[0] = preg_replace_callback('/"thumbnail_8x":"(.*?)"/', array($this, 'json_src'), $matches[0]); + + //Set autoplay to false else it appends &autoplay=1 to proxified URLs + $matches[0] = str_replace(',"autoplay":true', ',"autoplay":false', $matches[0]); + $matches[0] = str_replace('"name":"autoplay","choice":"on",', '"name":"autoplay","choice":"off",', $matches[0]); + + // Return data + return $matches[0]; + } + + public function onCompleted(ProxyEvent $event){ + + // Set HTML content + $output = $event['response']->getContent(); + + // Set base_url needed for proxify_url() + $this->base_url = $event['request']->getUri(); + + // Replace JSON https:\/\/... URLs inside scripts + $output = preg_replace_callback('/]*?>([\s\S]*?)<\/script>/', array($this, 'json_parse'), $output); + + $url = ''; + + // Download URL that contains video info (method 1) + if(preg_match('/\/is', $output, $matches)) + { + $url = str_replace("&autoplay=1", "", trim($matches[1])); + } + + // Download URL that contains video info (method 2) + if(!$url) + { + if(preg_match('/\/is', $output, $matches)) + $url = str_replace("&autoplay=1", "", trim($matches[1])); + } + + // Download URL that contains video info (method 3) + if(!$url) + { + if(preg_match('/XMLHttpRequest;e\.open\("GET","(.+?)"/is', $output, $matches)) + $url = trim($matches[1]); + } + + // If we have the URL with video info + if($url) + { + // Make sure the URL is valid + if(filter_var($url, FILTER_VALIDATE_URL)) + { + // Use cURL to download the URL content + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_REFERER, $this->base_url); + curl_setopt($ch, CURLOPT_TIMEOUT, 5); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, False); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, False); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, False); + $result = curl_exec($ch); + curl_close($ch); + + // If we have URL content + if($result) + { + // Extract JSON data + if(preg_match('/\(function\(e\,a\)\{var t=(.+?)\;if\(\!t\.request\)/is', $result, $matches)) + { + $json = $matches[1]; + } + else + { + $json = $result; + } + + // If we have JSON data + if($json) + { + // Decode JSON data + $decoded = json_decode($json, true); + + // If we have video info + if($decoded['request']['files']['progressive']) + { + // Set our array for video URLs + $urls = array(); + + // Loop each array item and get video URLs + foreach($decoded['request']['files']['progressive'] as $video) + { + if($video['url']) + { + // Remove the "p" so we can ksort() the array + $urls[str_replace("p", "", $video['quality'])] = rawurldecode(stripslashes(trim($video['url']))); + } + } + + // If we have at least one URL + if(count($urls)>0) + { + // Sort array based on quality (lower to high) + ksort($urls); + + // First try to set preferred video quality URL + $video_url = $urls['540'] ? $urls['540'] : $urls['720']; + + // In case there is no URL of that quality, get the HD quality + if(!$video_url) $video_url = $urls['1080']; + + // In case there is no URL of that quality, get the lowest quality + if(!$video_url) $video_url = $urls['360']; + + // Get the first video URL (lower quality) and validate it + if(filter_var($video_url, FILTER_VALIDATE_URL)) + { + // Set video URL on HTML5 player + $player = vid_player($video_url, 973, 547, 'mp4'); + + // Replace original player container with our player + + // When on channels, i.e https://vimeo.com/channels/1341 + $output = Html::replace_inner(".channel_clip_container", $player, $output); + + // When on single video, i.e https://vimeo.com/202629825 + $output = Html::replace_inner(".app_banner_container", $player, $output); + } + } + } + } + } + } + } + + $event['response']->setContent($output); + } +} + +?> From 35fd32edaf76361645fbf7767a108df8fb8da6ef Mon Sep 17 00:00:00 2001 From: Web Addicto Date: Wed, 8 Feb 2017 17:51:13 +0100 Subject: [PATCH 2/5] Fixed spaces --- src/VimeoPlugin.php | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/src/VimeoPlugin.php b/src/VimeoPlugin.php index 7ec4a66..26b5a6f 100644 --- a/src/VimeoPlugin.php +++ b/src/VimeoPlugin.php @@ -76,7 +76,7 @@ public function onCompleted(ProxyEvent $event){ $url = str_replace("&autoplay=1", "", trim($matches[1])); } - // Download URL that contains video info (method 3) + // Download URL that contains video info (method 3) ***data is JSON*** if(!$url) { if(preg_match('/XMLHttpRequest;e\.open\("GET","(.+?)"/is', $output, $matches)) @@ -91,30 +91,23 @@ public function onCompleted(ProxyEvent $event){ { // Use cURL to download the URL content $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_HEADER, 0); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_REFERER, $this->base_url); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_REFERER, $this->base_url); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, False); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, False); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, False); - $result = curl_exec($ch); - curl_close($ch); + $result = curl_exec($ch); + curl_close($ch); - // If we have URL content + // If we have URL content if($result) { // Extract JSON data - if(preg_match('/\(function\(e\,a\)\{var t=(.+?)\;if\(\!t\.request\)/is', $result, $matches)) - { - $json = $matches[1]; - } - else - { - $json = $result; - } + $json = preg_match('/\(function\(e\,a\)\{var t=(.+?)\;if\(\!t\.request\)/is', $result, $matches) ? $matches[1] : $result; // If we have JSON data if($json) @@ -122,7 +115,7 @@ public function onCompleted(ProxyEvent $event){ // Decode JSON data $decoded = json_decode($json, true); - // If we have video info + // If we have video info if($decoded['request']['files']['progressive']) { // Set our array for video URLs @@ -153,16 +146,16 @@ public function onCompleted(ProxyEvent $event){ // In case there is no URL of that quality, get the lowest quality if(!$video_url) $video_url = $urls['360']; - // Get the first video URL (lower quality) and validate it + // Validate the video URL if(filter_var($video_url, FILTER_VALIDATE_URL)) { // Set video URL on HTML5 player - $player = vid_player($video_url, 973, 547, 'mp4'); + $player = vid_player($video_url, 973, 547, 'mp4'); // Replace original player container with our player // When on channels, i.e https://vimeo.com/channels/1341 - $output = Html::replace_inner(".channel_clip_container", $player, $output); + $output = Html::replace_inner(".channel_clip_container", $player, $output); // When on single video, i.e https://vimeo.com/202629825 $output = Html::replace_inner(".app_banner_container", $player, $output); From ad5c6befb9816774d3bb9ab34af8a3ba8bd91008 Mon Sep 17 00:00:00 2001 From: Web Addicto Date: Wed, 8 Feb 2017 18:50:14 +0100 Subject: [PATCH 3/5] Support searching for videos Show video thumbnail when searching for a video. --- src/VimeoPlugin.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/VimeoPlugin.php b/src/VimeoPlugin.php index 26b5a6f..d7b082e 100644 --- a/src/VimeoPlugin.php +++ b/src/VimeoPlugin.php @@ -22,6 +22,9 @@ private function json_src($matches){ // Do not proxify already proxified URLs if(stripos($url, app_url()) === 0) return $matches[0]; + // Remove ?autoplay=1 + $url = str_replace('?autoplay=1', '', $url); + // Proxify URL => JSON_Encode => Remove " $url_proxied = str_replace("\"", "", json_encode(proxify_url($url, $this->base_url))); @@ -41,6 +44,7 @@ private function json_parse($matches){ $matches[0] = preg_replace_callback('/"thumbnail_2x":"(.*?)"/', array($this, 'json_src'), $matches[0]); $matches[0] = preg_replace_callback('/"thumbnail_4x":"(.*?)"/', array($this, 'json_src'), $matches[0]); $matches[0] = preg_replace_callback('/"thumbnail_8x":"(.*?)"/', array($this, 'json_src'), $matches[0]); + $matches[0] = preg_replace_callback('/"link":"(.+?)"/', array($this, 'json_src'), $matches[0]); //Set autoplay to false else it appends &autoplay=1 to proxified URLs $matches[0] = str_replace(',"autoplay":true', ',"autoplay":false', $matches[0]); @@ -66,14 +70,14 @@ public function onCompleted(ProxyEvent $event){ // Download URL that contains video info (method 1) if(preg_match('/\/is', $output, $matches)) { - $url = str_replace("&autoplay=1", "", trim($matches[1])); + $url = str_replace('?autoplay=1', '', trim($matches[1])); } // Download URL that contains video info (method 2) if(!$url) { if(preg_match('/\/is', $output, $matches)) - $url = str_replace("&autoplay=1", "", trim($matches[1])); + $url = str_replace('?autoplay=1', '', trim($matches[1])); } // Download URL that contains video info (method 3) ***data is JSON*** @@ -94,6 +98,7 @@ public function onCompleted(ProxyEvent $event){ curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0'); curl_setopt($ch, CURLOPT_REFERER, $this->base_url); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); @@ -102,19 +107,19 @@ public function onCompleted(ProxyEvent $event){ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, False); $result = curl_exec($ch); curl_close($ch); - + // If we have URL content if($result) { // Extract JSON data $json = preg_match('/\(function\(e\,a\)\{var t=(.+?)\;if\(\!t\.request\)/is', $result, $matches) ? $matches[1] : $result; - + // If we have JSON data if($json) { // Decode JSON data $decoded = json_decode($json, true); - + // If we have video info if($decoded['request']['files']['progressive']) { @@ -145,7 +150,7 @@ public function onCompleted(ProxyEvent $event){ // In case there is no URL of that quality, get the lowest quality if(!$video_url) $video_url = $urls['360']; - + // Validate the video URL if(filter_var($video_url, FILTER_VALIDATE_URL)) { @@ -153,11 +158,6 @@ public function onCompleted(ProxyEvent $event){ $player = vid_player($video_url, 973, 547, 'mp4'); // Replace original player container with our player - - // When on channels, i.e https://vimeo.com/channels/1341 - $output = Html::replace_inner(".channel_clip_container", $player, $output); - - // When on single video, i.e https://vimeo.com/202629825 $output = Html::replace_inner(".app_banner_container", $player, $output); } } From 11a3913fbf1534d80992c15f60b0a64ac0568971 Mon Sep 17 00:00:00 2001 From: Web Addicto Date: Wed, 8 Feb 2017 20:17:55 +0100 Subject: [PATCH 4/5] More preg_replaces for thumbnails --- src/VimeoPlugin.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/VimeoPlugin.php b/src/VimeoPlugin.php index d7b082e..06bcc0c 100644 --- a/src/VimeoPlugin.php +++ b/src/VimeoPlugin.php @@ -45,6 +45,10 @@ private function json_parse($matches){ $matches[0] = preg_replace_callback('/"thumbnail_4x":"(.*?)"/', array($this, 'json_src'), $matches[0]); $matches[0] = preg_replace_callback('/"thumbnail_8x":"(.*?)"/', array($this, 'json_src'), $matches[0]); $matches[0] = preg_replace_callback('/"link":"(.+?)"/', array($this, 'json_src'), $matches[0]); + $matches[0] = preg_replace_callback('/"background_image_url":"(.+?)"/', array($this, 'json_src'), $matches[0]); + $matches[0] = preg_replace_callback('/"background_image_url_2x":"(.+?)"/', array($this, 'json_src'), $matches[0]); + $matches[0] = preg_replace_callback('/"portrait":"(.+?)"/', array($this, 'json_src'), $matches[0]); + $matches[0] = preg_replace_callback('/"portrait_bg":"(.+?)"/', array($this, 'json_src'), $matches[0]); //Set autoplay to false else it appends &autoplay=1 to proxified URLs $matches[0] = str_replace(',"autoplay":true', ',"autoplay":false', $matches[0]); From 9686ae642694fbc98d5b5152c09aeb6a4f456622 Mon Sep 17 00:00:00 2001 From: Web Addicto Date: Mon, 13 Feb 2017 17:03:34 +0100 Subject: [PATCH 5/5] Some minor improvements --- src/VimeoPlugin.php | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/VimeoPlugin.php b/src/VimeoPlugin.php index 06bcc0c..ff7d7f7 100644 --- a/src/VimeoPlugin.php +++ b/src/VimeoPlugin.php @@ -135,7 +135,7 @@ public function onCompleted(ProxyEvent $event){ { if($video['url']) { - // Remove the "p" so we can ksort() the array + // Remove the "p" so we get video quality as numbers (i.e 270, 360, etc) $urls[str_replace("p", "", $video['quality'])] = rawurldecode(stripslashes(trim($video['url']))); } } @@ -143,18 +143,15 @@ public function onCompleted(ProxyEvent $event){ // If we have at least one URL if(count($urls)>0) { - // Sort array based on quality (lower to high) - ksort($urls); - - // First try to set preferred video quality URL - $video_url = $urls['540'] ? $urls['540'] : $urls['720']; + // First try to set preferred (low) video quality URL + $video_url = $urls['270'] ? $urls['270'] : $urls['360']; - // In case there is no URL of that quality, get the HD quality + // In case there is no URL of that quality, try to get better quality + if(!$video_url) $video_url = $urls['540'] ? $urls['540'] : $urls['720']; + + // In case there is no URL of that quality, try to get HD quality if(!$video_url) $video_url = $urls['1080']; - // In case there is no URL of that quality, get the lowest quality - if(!$video_url) $video_url = $urls['360']; - // Validate the video URL if(filter_var($video_url, FILTER_VALIDATE_URL)) {