Th3-822

Download Link Shortener

Jun 24th, 2015
1,514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.36 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.     Download-Link Shortener
  5.     Info and install instructions at the forum: http://rapidleech.com/forum/viewtopic.php?f=18&t=653
  6. */
  7.  
  8. header('X-Robots-Tag: noindex, nofollow');
  9. require_once('rl_init.php');
  10. require_once(CLASS_DIR . 'http.php');
  11.  
  12. // Only cURL is supported here.
  13. if (!extension_loaded('curl') || !function_exists('curl_init') || !function_exists('curl_exec')) html_error('cURL isn\'t enabled or cURL\'s functions are disabled');
  14.  
  15. class LinkConverter {
  16.     private $link, $count, $https = false;
  17.     protected $debug = false;
  18.     public function __construct($link) {
  19.         $this->count = 0;
  20.         $link = $this->ValidateURL($link);
  21.         if (!$link) html_error('Invalid Link');
  22.         $this->link = $link;
  23.         //$this->debug = true; // Uncomment to enable debug info.
  24.     }
  25.  
  26.     public function MassConvert($methods) {
  27.         foreach(array_filter(explode(' ', $methods)) as $method) {
  28.             if ($this->count > 3) return;
  29.             $this->Convert($method);
  30.         }
  31.     }
  32.  
  33.     public function Convert($method, $opts = null) {
  34.         if ($this->count > 3) return false;
  35.         $func = array($this, "lnk_$method");
  36.         try {
  37.             if (!is_callable($func)) throw new Exception(__CLASS__."::lnk_$method is not callable.");
  38.             if (!($convertedLink = call_user_func($func, $opts))) return false;
  39.             $this->link = $convertedLink;
  40.         } catch (Exception $e) {
  41.             $GLOBALS['throwRLErrors'] = false; // Disable throw on error in case of a cURL error.
  42.             if ($this->debug) {
  43.                 textarea($e); // Debug
  44.                 //textarea(var_export($e, true)); // Debug | Extra info
  45.             }
  46.             return false;
  47.         }
  48.         $this->count++;
  49.     }
  50.  
  51.     public function GetLink() {
  52.         return $this->link;
  53.     }
  54.  
  55.     protected function ValidateURL($link) {
  56.         if (empty($link)) return false;
  57.         $link = filter_var($link, FILTER_SANITIZE_URL);
  58.         if (filter_var($link, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED | FILTER_FLAG_PATH_REQUIRED) && in_array(strtolower(parse_url($link, PHP_URL_SCHEME)), array('http', 'https', 'ftp'))) return $link;
  59.         else return false;
  60.     }
  61.  
  62.     protected function Request($link, $cookie = 0, $post = 0, $referer = 0, $auth = 0, $opts = 0) {
  63.         if (!empty($post) && is_array($post)) $post = http_build_query($post, '', '&');
  64.         $timeouts = array();
  65.         $timeouts[CURLOPT_FAILONERROR] = (!$this->debug); // Fail on errors
  66.         $timeouts[CURLOPT_CONNECTTIMEOUT] = 10; // Connection Timeout
  67.         $timeouts[CURLOPT_TIMEOUT] = 10; // Page Timeout
  68.         if (is_array($opts)) $opts = array_merge($opts, $timeouts);
  69.         else $opts = $timeouts;
  70.  
  71.         $GLOBALS['throwRLErrors'] = true; // Throw errors.
  72.         $page = cURL($link, $cookie, $post, $referer, $auth, $opts);
  73.         $GLOBALS['throwRLErrors'] = false; // Disable throw on error.
  74.  
  75.         // Debug
  76.         if ($this->debug) {
  77.             $debug = "Request: $link";
  78.             if (!empty($referer)) $debug .= "\nReferer: " . str_replace(array("\r", "\n"), array('\r', '\n'), $referer);
  79.             if (!empty($_GET['useproxy']) && $_GET['useproxy'] == 'on' && !empty($_GET['proxy'])) $debug .= "\nProxy Enabled\nProxy: " . $_GET['proxy'];
  80.             if (!empty($cookie)) $debug .= "\nCookie: ".(is_array($cookie) ? substr(print_r($cookie, true), 0, -1) : trim($cookie));
  81.             if ($auth) $debug .= "\nAuth: ".base64_decode($auth);
  82.             if ($post != '0') $debug .= "\nPOST: ".(is_array($post) ? substr(print_r($post, true), 0, -1) : $post);
  83.             textarea("$debug\n-----\n".preg_replace('@([^\r])\n@', '$1\n'."\n", str_replace("\r\n", '\r\n'."\r\n", substr($page, 0, strpos($page, "\r\n\r\n")+4))).(strpos($page, chr(0)) === false ? substr($page, strpos($page, "\r\n\r\n")+4) : '[Binary Content?]'),200,15);
  84.         }
  85.  
  86.         return $page;
  87.     }
  88.  
  89.     protected function GetBody($page) {
  90.         return (($pos = strpos($page, "\r\n\r\n")) > 0) ? substr($page, $pos + 4) : $page;
  91.     }
  92.  
  93.     protected function json2array($content) {
  94.         if (!function_exists('json_decode')) html_error('Error: Please enable JSON in php.');
  95.         if (empty($content)) return NULL;
  96.         $content = ltrim($content);
  97.         if (($pos = strpos($content, "\r\n\r\n")) > 0) $content = trim(substr($content, $pos + 4));
  98.         $cb_pos = strpos($content, '{');
  99.         $sb_pos = strpos($content, '[');
  100.         if ($cb_pos === false && $sb_pos === false) throw new Exception('Json start braces not found.');
  101.         $sb = ($cb_pos === false || $sb_pos < $cb_pos) ? true : false;
  102.         $content = substr($content, strpos($content, ($sb ? '[' : '{')));$content = substr($content, 0, strrpos($content, ($sb ? ']' : '}')) + 1);
  103.         if (empty($content)) throw new Exception('No json content.');
  104.         $rply = json_decode($content, true);
  105.         if ($rply === NULL) throw new Exception('Error reading json.');
  106.         return $rply;
  107.     }
  108.  
  109.     protected function chkHttpsSupport() {
  110.         if ($this->https) return true;
  111.         $cV = curl_version();
  112.         return $this->https = in_array('https', $cV['protocols'], true);
  113.     }
  114.  
  115.     // Add Methods Here: (prefixed with 'lnk_')
  116.     protected function lnk_adfly($opts = null) {
  117.         $options = array( // https://adf.ly/publisher/tools#tools-api
  118.             'key' => '1d7528f24a09dbd0339860c6a1cf9a22', /* Your ApiKey */
  119.             'uid' => '10221009', /* Your UserId */
  120.             'advert_type' => 'int', /* 'int' or 'banner' */
  121.             'domain' => 'adf.ly' /* 'adf.ly', 'j.gs', 'q.gs' or 'your custom domain' */
  122.         );
  123.         if (!is_null($opts) && is_array($opts)) $options = array_merge($options, $opts);
  124.         if (empty($options['key']) || empty($options['uid'])) return false;
  125.         $options['url'] = $this->link;
  126.         $body = $this->GetBody($this->Request('http://api.adf.ly/api.php?'.http_build_query($options, '', '&')));
  127.         return $this->ValidateURL($body);
  128.     }
  129.  
  130.     protected function lnk_bcvc($opts = null) {
  131.         $options = array( // http://bc.vc/tools.php?api
  132.             'key' => '1a708a7090a08c94e36b63cbf1de97cc', /* Your ApiKey */
  133.             'uid' => '109189' /* Your UserId */
  134.         );
  135.         if (!is_null($opts) && is_array($opts)) $options = array_merge($options, $opts);
  136.         if (empty($options['key']) || empty($options['uid'])) return false;
  137.         $options['url'] = $this->link;
  138.  
  139.         $x = 0;
  140.         do {
  141.             $body = $this->GetBody($this->Request('http://bc.vc/api.php?'.http_build_query($options, '', '&')));
  142.             $x++;
  143.         } while (!empty($body) && $x < 3 && stripos($body, 'Unable to connect to database') !== false);
  144.         return $this->ValidateURL($body);
  145.     }
  146.  
  147.     protected function lnk_linkbucks($opts = null) {
  148.         if (!function_exists('json_encode')) html_error('Error: Please enable JSON in php.');
  149.         if (!$this->chkHttpsSupport()) html_error('Your cURL doesn\'t support https connections.');
  150.         $options = array( // https://www.linkbucks.com/Profile
  151.             'user' => 'Th3822', /* Your UserName */
  152.             'apiPassword' => '23c02a93e403480b', /* Your ApiKey (Secret) */
  153.             'adType' => 2, /* 2 = Ads ($) | 5 = No Ads (No $) */
  154.             'contentType' => 1, /* 1: Clean | 2: Adult Content */
  155.             'domain' => 'linkbucks.com' /* Alias Domain, full list is at https://www.linkbucks.com/CreateLinks/Single/ */
  156.         );
  157.         if (!is_null($opts) && is_array($opts)) $options = array_merge($options, $opts);
  158.         if (empty($options['user']) || empty($options['apiPassword'])) return false;
  159.         $options['originalLink'] = $this->link;
  160.  
  161.         $json = $this->json2array($this->Request('https://www.linkbucks.com/api/createLink/single', 0, json_encode($options))); // Alternate domain that supports API: www.linkbucksmedia.com
  162.         return (!empty($json['link']) ? $this->ValidateURL($json['link']) : false);
  163.     }
  164.  
  165.     protected function lnk_shortest($opts = null) {
  166.         if (!$this->chkHttpsSupport()) html_error('Your cURL doesn\'t support https connections.');
  167.         $options = array( // https://shorte.st/es/tools/api
  168.             'key' => '567b51cd25df5567eb48709142876afd' /* Your ApiKey */
  169.         );
  170.         if (!is_null($opts) && is_array($opts)) $options = array_merge($options, $opts);
  171.         if (empty($options['key'])) return false;
  172.  
  173.         $json = $this->json2array($this->Request('https://api.shorte.st/v1/data/url', 0, array('urlToShorten' => $this->link), "\r\nX-HTTP-Method-Override: PUT\r\nPublic-Api-Token: " . $options['key']));
  174.         return (!empty($json['shortenedUrl']) ? str_ireplace('http://', 'https://', $this->ValidateURL($json['shortenedUrl'])) : false);
  175.     }
  176.  
  177.     // Evilest shortener, annoys users with captchas.
  178.     protected function lnk_ouoio($opts = null) {
  179.         $options = array( // http://ouo.io/manage/tools/quick-link
  180.             'key' => 'IsYbNk1X' /* Your ApiKey */
  181.         );
  182.         if (!is_null($opts) && is_array($opts)) $options = array_merge($options, $opts);
  183.         if (empty($options['key'])) return false;
  184.  
  185.         $body = $this->GetBody($this->Request("http://ouo.io/api/{$options['key']}?s=" . urlencode($this->link)));
  186.         return $this->ValidateURL($body);
  187.     }
  188.  
  189.     protected function lnk_ppw($opts = null) {
  190.         $options = array( // http://p.pw/publisher/tools.php
  191.             'user' => '382635' /* Your UserId */
  192.         );
  193.         if (!is_null($opts) && is_array($opts)) $options = array_merge($options, $opts);
  194.         if (empty($options['user'])) return false;
  195.         $options['url'] = $this->link;
  196.  
  197.         $json = $this->json2array($this->Request('http://p.pw/API/write/get?' . http_build_query($options, '', '&')));
  198.         return ((!empty($json['success']) && !empty($json['data']['url'])) ? $this->ValidateURL($json['data']['url']) : false);
  199.     }
  200. }
  201.  
  202. if (empty($_GET['path']) && empty($_GET['sha'])) {
  203.     // Redirect to index.php when called without query.
  204.     $Path = strtr(dirname($_SERVER['SCRIPT_NAME']), '\\', '/');
  205.     if (empty($Path) || $Path == '.' || $Path == '/') $Path = '';
  206.     else $Path = (substr($Path, 0, 1) == '/' ? $Path : "/$Path");
  207.     $Path .= "/index.php";
  208.     header("Location: $Path");
  209.     exit;
  210. } elseif (empty($_GET['path']) || empty($_GET['sha'])) html_error('Invalid download-link.');
  211. $path = decrypt(base64_decode($_GET['path']));
  212. if (sha1($path) != $_GET['sha']) html_error('Corrupted download-link.');
  213.  
  214. $path = strtr((in_array(substr($path, 0, 1), array('/', '\\')) ? $path : "/$path"), '\\', '/'); // Add start / to path and replace any backslash with a slash
  215. $link = 'http://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']) . $path;
  216. $shortener = new LinkConverter($link);
  217. // Call Converters Here
  218.  
  219. //$shortener->MassConvert('adfly bcvc linkbucks shortest ouoio ppw');
  220. //$shortener->Convert('ppw');
  221.  
  222. // Get Final Converted Link
  223. $newLink = $shortener->GetLink();
  224. if (headers_sent($filename, $line)) {
  225.     // Show Error Message and Link to Continue.
  226.     html_error('Headers Sent by: '.htmlspecialchars($filename).' @ Line #'.$line.'<br /><a href="'.htmlspecialchars($newLink).'">Click Here to Continue</a>');
  227. } else {
  228.     // Do Redirect
  229.     header("Location: $newLink", true, 301);
  230.     exit;
  231. }
  232.  
  233. // Written by Th3-822
  234. // Last Updated: 10/2/2017
  235. // Added p.pw support
Add Comment
Please, Sign In to add comment