Advertisement
SalimiII

Youtube Download Plugins

Nov 14th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.14 KB | None | 0 0
  1. <?php
  2.  
  3. if (!defined('RAPIDLEECH')) {
  4. require_once ('index.html');
  5. exit();
  6. }
  7.  
  8. class youtube_com extends DownloadClass {
  9. private $page, $cookie, $fmts, $fmturlmaps;
  10. public function Download($link) {
  11. $this->cookie = isset($_REQUEST['yt_QS']) && !empty($_POST['cookie']) ? StrToCookies(decrypt(urldecode($_POST['cookie']))) : array();
  12. $this->page = $this->GetPage($link, $this->cookie);
  13. if (preg_match('@Location: ((https?://(?:[^/\r\n]+\.)?youtube\.com)?/watch\?[^\r\n]+)@i', $this->page, $redir)) {
  14. $link = (empty($redir[2])) ? 'http://www.youtube.com'.$redir[1] : $redir[1];
  15. $this->page = $this->GetPage($link, $this->cookie);
  16. }
  17.  
  18. if (preg_match('@^HTTP/1\.[01] 403 Forbidden@i', $this->page)) {
  19. $this->CheckErrors();
  20. html_error('403 Forbidden');
  21. } else if (preg_match('@^HTTP/1\.[01] 404 Not Found@i', $this->page)) {
  22. $this->CheckErrors();
  23. html_error('404 Page Not Found');
  24. } else $this->CheckErrors();
  25.  
  26. if (isset($_REQUEST['step']) || preg_match('@Location: https?://(www\.)?youtube\.com/das_captcha@i', $this->page)) {
  27. $this->captcha($link);
  28. }
  29. $Mesg = lang(300);
  30. if (preg_match('#Location: https?://(www\.)?youtube\.com/verify_age#i', $this->page)) {
  31. $Mesg .= '<br /><br />Verify_age page found:<br />This video may contain content that is inappropriate for some users<br /><br />Logging in to Youtube...<br />Direct Link option may not work.';
  32. $this->changeMesg($Mesg);
  33. $this->verify_age($link);
  34. }
  35. if (preg_match('#Location: https?://(www\.)?youtube\.com/verify_controversy#i', $this->page)) {
  36. $Mesg .= '<br /><br />Verify_controversy page found:<br />The following content has been identified by the YouTube community as being potentially offensive or inappropriate. Viewer discretion is advised.';
  37. $this->changeMesg($Mesg);
  38. $this->verify_controversy($link);
  39. }
  40.  
  41. if (!preg_match('#fmt_stream_map=(.+?)(&|(\\\u0026))#', $this->page, $fmt_url_map)) html_error('Video link not found.');
  42. $fmt_url_maps = explode(',', urldecode($fmt_url_map[1]));
  43.  
  44. $this->fmts = array(38,37,22,45,35,44,34,43,18,5,17);
  45. $yt_fmt = empty($_REQUEST['yt_fmt']) ? '' : $_REQUEST['yt_fmt'];
  46. $this->fmturlmaps = $this->GetVideosArr($fmt_url_maps);
  47.  
  48. if (empty($yt_fmt) && !isset($_GET["audl"])) return $this->QSelector($link);
  49. elseif (isset($_REQUEST['ytube_mp4']) && $_REQUEST['ytube_mp4'] == 'on' && !empty($yt_fmt)) {
  50. //look for and download the highest quality we can find?
  51. if ($yt_fmt == 'highest') {
  52. foreach ($this->fmts as $fmt) {
  53. if (array_key_exists($fmt, $this->fmturlmaps)) {
  54. $furl = $this->fmturlmaps[$fmt];
  55. break;
  56. }
  57. }
  58. } else { //get the format the user specified (making sure it actually exists)
  59. if (!$furl = $this->fmturlmaps[$yt_fmt]) html_error ('Specified video format not found');
  60. $fmt = $yt_fmt;
  61. }
  62. } else { //just get the one Youtube plays by default (in some cases it could also be the highest quality format)
  63. $fmt = key($this->fmturlmaps);
  64. $furl = $this->fmturlmaps[$fmt];
  65. }
  66.  
  67. $ext = '.flv';
  68. $fmtexts = array('.3gp' => array(17), '.mp4' => array(18,22,37,38), '.webm' => array(43,44,45));
  69. foreach ($fmtexts as $k => $v) {
  70. if (!is_array($v)) $v = array($v);
  71. if (in_array($fmt, $v)) {
  72. $ext = $k;
  73. break;
  74. }
  75. }
  76.  
  77. $_s = '[\r\n\t\s]';
  78. if (!preg_match("#<title>$_s*(.*)$_s+-$_s+YouTube$_s*</title>#Us", $this->page, $title) && !preg_match("#<title>$_s*YouTube\s+-\s+(.*)$_s*</title>#Us", $this->page, $title)) html_error('No video title found! Download halted.');
  79. if (!preg_match('/video_id=(.+?)(\\\|"|&|(\\\u0026))/', $this->page, $video_id)) html_error('Video id not found.');
  80. $FileName = str_replace(str_split('\\/:*?"<>|'), '_', html_entity_decode(trim($title[1]), ENT_QUOTES)) . "-[YT-$fmt][{$video_id[1]}]$ext";
  81.  
  82. if (isset($_REQUEST['ytdirect']) && $_REQUEST['ytdirect'] == 'on') {
  83. echo "<br /><br /><h4><a style='color:yellow' href='" . urldecode($furl) . "'>Click here or copy the link to your download manager to download</a></h4>";
  84. echo "<input name='dlurl' style='width: 1000px; border: 1px solid #55AAFF; background-color: #FFFFFF; padding:3px' value='" . urldecode($furl) . "' onclick='javascript:this.select();' readonly></input>";
  85. } else {
  86. $this->RedirectDownload ($furl, $FileName, $this->cookie, 0, 0, $FileName);
  87. }
  88. }
  89.  
  90. private function captcha($link) {
  91. $url = 'http://www.youtube.com/das_captcha?next=' . urlencode($link);
  92. if (isset($_REQUEST['step']) && $_REQUEST['step'] == '1') {
  93. if (empty($_POST['recaptcha_response_field'])) html_error('You didn\'t enter the image verification code.');
  94. $post = array('recaptcha_challenge_field' => $_POST['recaptcha_challenge_field'], 'recaptcha_response_field' => $_POST['recaptcha_response_field']);
  95. $post['next'] = $_POST['next'];
  96. $post['action_recaptcha_verify'] = $_POST['action_recaptcha_verify'];
  97. $post['submit'] = $_POST['_submit'];
  98. $post['session_token'] = $_POST['session_token'];
  99. $cookie = urldecode($_POST['cookie']);
  100.  
  101. $page = $this->GetPage($url, $cookie, $post, $url);
  102. is_present($page, 'The verification code was invalid', 'The verification code was invalid or has timed out, please try again.');
  103. is_present($page, "\r\n\r\nAuthorization Error.", 'Error sending captcha.');
  104. is_notpresent($page, 'Set-Cookie: goojf=', 'Cannot get captcha cookie.');
  105.  
  106. $this->cookie = GetCookiesArr($page);
  107. $this->page = $this->GetPage($link, $this->cookie);
  108. } else {
  109. $page = $this->GetPage($url);
  110. if (!preg_match('@//(?:[^/]+\.)?(?:(?:google\.com/recaptcha/api)|(?:recaptcha\.net))/(?:(?:challenge)|(?:noscript))\?k=([\w|\-]+)@i', $page, $pid)) html_error('Error: reCAPTCHA not found.');
  111.  
  112. $data = $this->DefaultParamArr($link, GetCookies($page));
  113. $data['next'] = urlencode(html_entity_decode(cut_str($page, 'name="next" value="', '"')));
  114. $data['action_recaptcha_verify'] = urlencode(cut_str($page, 'name="action_recaptcha_verify" value="', '"'));
  115. $data['_submit'] = urlencode(cut_str($page, 'type="submit" name="submit" value="', '"'));
  116. $data['session_token'] = urlencode(cut_str($page, "'XSRF_TOKEN': '", "'"));
  117. if (isset($_REQUEST['ytube_mp4'])) $data['ytube_mp4'] = $_REQUEST['ytube_mp4'];
  118. if (isset($_REQUEST['ytdirect'])) $data['ytdirect'] = $_REQUEST['ytdirect'];
  119. if (isset($_REQUEST['yt_fmt'])) $data['yt_fmt'] = $_REQUEST['yt_fmt'];
  120. $data['step'] = 1;
  121.  
  122. $this->Show_reCaptcha($pid[1], $data);
  123. }
  124. }
  125.  
  126. private function Show_reCaptcha($pid, $inputs) {
  127. global $PHP_SELF;
  128. if (!is_array($inputs)) html_error('Error parsing captcha data.');
  129.  
  130. // Themes: 'red', 'white', 'blackglass', 'clean'
  131. echo "<script language='JavaScript'>var RecaptchaOptions={theme:'red', lang:'en'};</script>\n";
  132.  
  133. echo "\n<center><form name='dl' action='$PHP_SELF' method='post'><br />\n";
  134. foreach ($inputs as $name => $input) echo "<input type='hidden' name='$name' id='$name' value='$input' />\n";
  135. echo "<script type='text/javascript' src='http://www.google.com/recaptcha/api/challenge?k=$pid'></script>";
  136. echo "<noscript><iframe src='http://www.google.com/recaptcha/api/noscript?k=$pid' height='300' width='500' frameborder='0'></iframe><br />";
  137. echo "<textarea name='recaptcha_challenge_field' rows='3' cols='40'></textarea><input type='hidden' name='recaptcha_response_field' value='manual_challenge' /></noscript><br />";
  138. echo "<input type='submit' name='submit' onclick='javascript:return checkc();' value='Download File' />\n";
  139. echo "<script type='text/javascript'>/*<![CDATA[*/\nfunction checkc(){\nvar capt=document.getElementById('recaptcha_response_field');\nif (capt.value == '') { window.alert('You didn\'t enter the image verification code.'); return false; }\nelse { return true; }\n}\n/*]]>*/</script>\n";
  140. echo "</form></center>\n</body>\n</html>";
  141. exit;
  142. }
  143.  
  144. private function login($link) {
  145. global $premium_acc;
  146. if (!empty($this->cookie['SID'])) return;
  147.  
  148. if (!empty($_REQUEST['premium_user']) && !empty($_REQUEST['premium_pass'])) {
  149. $user = $_REQUEST['premium_user'];
  150. $pass = $_REQUEST['premium_pass'];
  151. } else {
  152. $user = $premium_acc['youtube_com']['user'];
  153. $pass = $premium_acc['youtube_com']['pass'];
  154. }
  155. if (empty($user) || empty($pass)) html_error('Login Failed: Login Empty.', 0);
  156.  
  157. $post = array();
  158. $post['Email'] = urlencode($user);
  159. $post['Passwd'] = urlencode($pass);
  160. $post['service'] = 'youtube';
  161.  
  162. $page = $this->GetPage('https://www.google.com/accounts/ClientLogin', 0, $post, 'https://www.google.com/accounts/ClientLogin');
  163. is_present($page, 'Error=BadAuthentication', 'Login Failed: The login/password entered are incorrect.');
  164. is_present($page, 'Error=NotVerified', 'Login Failed: The account has not been verified.');
  165. is_present($page, 'Error=TermsNotAgreed', 'Login Failed: The account has not agreed to terms.');
  166. is_present($page, 'Error=CaptchaRequired', 'Login Failed: Need CAPTCHA. (Not supported yet)... Or check you login and try again.');
  167. is_present($page, 'Error=Unknown', 'Login Failed.');
  168. is_present($page, 'Error=AccountDeleted', 'Login Failed: The user account has been deleted.');
  169. is_present($page, 'Error=AccountDisabled', 'Login Failed: The user account has been disabled.');
  170. is_present($page, 'Error=ServiceDisabled', 'Login Failed: The user\'s access to the specified service has been disabled.');
  171. is_present($page, 'Error=ServiceUnavailable', 'Login Failed: Service is not available; try again later.');
  172.  
  173. if (!preg_match('@SID=([^\r|\n]+)@i', $page, $sid)) html_error('Login Failed: SessionID token not found.', 0);
  174.  
  175. $this->cookie['SID'] = $sid[1];
  176. $this->page = $this->GetPage($link, $this->cookie);
  177. $this->cookie = GetCookiesArr($this->page, $this->cookie);
  178. }
  179.  
  180. private function verify_age($link) {
  181. $this->login($link);
  182.  
  183. if (!preg_match('@Location: https?://(www\.)?youtube\.com/verify_age@i', $this->page)) return;
  184.  
  185. $url = 'http://www.youtube.com/verify_age?next_url=' . urlencode($link);
  186. $page = $this->GetPage($url, $this->cookie);
  187.  
  188. $post = array();
  189. $post['next_url'] = urlencode($link);
  190. $post['set_racy'] = 'true';
  191. $post['session_token'] = urlencode(cut_str($page, "'XSRF_TOKEN': '", "'"));
  192.  
  193. $urlc = 'http://www.youtube.com/verify_age?action_confirm=true';
  194. $page = $this->GetPage($urlc, $this->cookie, $post, $url);
  195. $this->page = $this->GetPage("$link&has_verified=1", $this->cookie, 0, $urlc);
  196. }
  197.  
  198. private function verify_controversy($link) {
  199. $url = 'http://www.youtube.com/verify_controversy?next_url=' . urlencode($link);
  200. $page = $this->GetPage($url, $this->cookie);
  201.  
  202. $post = array();
  203. $post['next_url'] = urlencode($link);
  204. // $post['ignorecont'] = 'on';
  205. $post['session_token'] = urlencode(cut_str($page, "'XSRF_TOKEN': '", "'"));
  206.  
  207. $urlc = 'http://www.youtube.com/verify_controversy?action_confirm=1';
  208. $page = $this->GetPage($urlc, $this->cookie, $post, $url);
  209. $this->page = $this->GetPage("$link&skipcontrinter=1", $this->cookie, 0, $urlc);
  210. }
  211.  
  212. private function GetVideosArr($fmtmaps) {
  213. $fmturls = array();
  214. foreach ($fmtmaps as $fmtlist) {
  215. $arr1 = explode('&', $fmtlist);
  216. $fmtlist = $arr3 = array();
  217. foreach ($arr1 as $key => $val) {
  218. $arr2 = explode('=', $val);
  219. foreach ($arr2 as $key2 => $val2) {
  220. $arr3[] = $val2;
  221. }
  222. }
  223. for ($i = 0; $i <= count($arr3); $i += 2) {
  224. if (array_key_exists($i, $arr3)) {
  225. if ($arr3[$i] != "") {
  226. $fmtlist[trim($arr3[$i])] = $arr3[$i+1];
  227. }
  228. }
  229. }
  230. $fmtlist = array_map('urldecode', $fmtlist);
  231. $fmturls[$fmtlist['itag']] = $fmtlist['url'];
  232. if (stripos($fmtlist['url'], '&signature=') === false) $fmturls[$fmtlist['itag']] .= '&signature='.$fmtlist['sig'];
  233. }
  234. return $fmturls;
  235. }
  236.  
  237. private function CheckErrors() {
  238. $page = cut_str($this->page, 'class="yt-alert-message">', '</');
  239. if (empty($page)) $page = $this->page;
  240. $servw = (isset($_GET['useproxy']) && !empty($_GET['proxy'])) ? 'proxy' : 'server';
  241. $errors = array();
  242. $errors[] = array('find' => 'The video you have requested is not available.'); // Empty or no 'error' key will show 'find'.
  243. $errors[] = array('find' => 'This video has been removed by the user.');
  244. $errors[] = array('find' => 'The uploader has not made this video available in your country.', 'error' => "The uploader has not made this video available in this $servw's country.");
  245. $errors[] = array('find' => ', who has blocked it in your country on copyright grounds.', 'error' => "This video has content with copyright and it's blocked in this $servw's country.");
  246. $errors[] = array('find' => 'This video is no longer available because the uploader has closed their YouTube account.');
  247. $errors[] = array('find' => 'This video is no longer available because the YouTube account associated with this video has been terminated.');
  248. foreach ($errors as $err) {
  249. if (empty($err['find'])) continue;
  250. is_present($page, $err['find'], (!empty($err['error']) ? $err['error'] : ''));
  251. }
  252. }
  253.  
  254. private function QSelector($link) {
  255. global $PHP_SELF;
  256. $fmtlangs = array(38 => 377, 37 => 228, 22 => 227, 45 => 225, 35 => 223, 44 => 389, 34 => 222, 43 => 224, 18 => 226, 5 => 221, 17 => 220);
  257. echo "\n<br /><br /><h3 style='text-align: center;'>".lang(216).".</h4>";
  258. echo "\n<center><form name='dl' action='$PHP_SELF' method='post'>\n";
  259. echo "<input type='hidden' name='yt_QS' value='on' />\n";
  260. echo '<input type="checkbox" name="ytdirect" /><small>&nbsp;'.lang(217).'</small><br />';
  261. echo "<select name='yt_fmt' id='vbb_qs'>\n";
  262. foreach ($this->fmturlmaps as $fmt => $url) {
  263. if (in_array($fmt, $this->fmts)) echo '<option '.($fmt == 18 ? "selected='selected' " : '')."value='$fmt'>".lang($fmtlangs[$fmt]).(!empty($sizes[$fmt]) ? ' ('.$sizes[$fmt].')' : '')."</option>\n";
  264. }
  265. echo "</select>\n";
  266. if (count($this->cookie) > 0) $this->cookie = encrypt(CookiesToStr($this->cookie));
  267. $data = $this->DefaultParamArr($link, $this->cookie);
  268. $data['ytube_mp4'] = 'on';
  269. foreach ($data as $n => $v) echo("<input type='hidden' name='$n' id='$n' value='$v' />\n");
  270.  
  271. echo "<input type='submit' name='submit' value='".lang(209)."' />\n";
  272. echo "</form></center>\n</body>\n</html>";
  273. exit;
  274. }
  275. }
  276.  
  277. //re-written by szal based on original plugin by eqbal
  278. //updated 07 June 2010
  279. // [28-03-2011] Fixed (!$video_id) regex. - Th3-822
  280. // [29-03-2011] Added support for captcha. - Th3-822
  281. // [02-04-2011] Fixed redirect error. [26-04-2011] Added error msgs. - Th3-822
  282. // [04-8-2011] Fixed for recent changes in fmt_stream_map content & some edits maded for work fine. (Redirect is needed yet) - Th3-822
  283. // [12-8-2011] Added support for videos that need login for verify age & Changed fmt order by quality & Fixed regexps for fileext. - Th3-822
  284. // [13-8-2011] Some fixes & removed not working code & fixed verify_age function. - Th3-822
  285. // [17-9-2011] Added function for skip 'verify_controversy' on youtube && Fixed cookies after captcha && Little changes. - Th3-822
  286. // [26-1-2012] Fixed regexp for get title, added a quality selector (if the one in template is removed) and some changes in the code. - Th3-822
  287. // [17-5-2012] Fixed captcha (Now uses reCaptcha). - Th3-822
  288. // [14-9-2012] Fixed Download links & small changes. - Th3-822
  289. // [07-10-2012] Fixed for redirect at link. - Th3-822
  290.  
  291. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement