IAL32

Link Shorten Finder by IAL32(PHP Page)

Sep 6th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.51 KB | None | 0 0
  1. <?php
  2.     $URI = redirected_url();
  3.     if ($URI != '') {
  4.         echo '<a href="' . $URI . '" target="_blank">' . $URI . '</a>';
  5.         echo '<script type="text/javascript">window.location = "' . $URI . '"</script>';
  6.     }
  7.     function redirected_url() {
  8.         $UrlShortener = array('http://0rz.tw', 'http://2.gp', 'http://2tu.us', 'http://qr.net', 'http://7.ly',
  9.                             'http://bit.ly', 'http://tinyurl.com', 'http://is.gd');
  10.         if ($_GET["url"] != "") {
  11.             $url = $_GET["url"];
  12.             if (preg_match("/http:\/\/adf\.ly\/.{5}/", $url)) { // controllo se la url contiene adf.ly, metodo piuttosto brutto, ma per ora va bene
  13.                 $html = file_get_contents($url); // Richiamo il source della pagina
  14.                 if (preg_match('(\/go/.{32}/.{64})' , $html , $matches)) // Espressione regolare, evidenzio: /go/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(32 valori HEX)/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(64 valori hex/vari)
  15.                 {
  16.                     return 'http://adf.ly' . $matches[0];
  17.                     echo '<a href="http://adf.ly' . $matches[0] . '" target="_blank">http://adf.ly' . $matches[0] . '</a>'; // scrivo la url ottenuta, mettendoci anche un piccolo link :)
  18.                 }
  19.             } elseif (preg_match("/(http:\/\/.{8}\.linkbucks\.com\/)/", $url, $m)) { //stessa cosa di adf.ly, ma con linkbucks
  20.                 $html = file_get_contents($url); // Richiamo il source della pagina
  21.                 if (preg_match("/(?<=TargetUrl = ').+?(?=')/" , $html, $matches)) //evidenzio la url, che si trova tra: "TargetUrl = '" e "'"
  22.                 {
  23.                     return $matches[0];
  24.                     echo '<a href="' . $matches[0] . '" target="_blank">' .$matches[0] . '</a>'; // scrivo la url ottenuta, mettendoci anche un piccolo link :)
  25.                 }
  26.             } elseif (preg_match('/http:\/\/cli\.gs\/.{5}/', $url, $m)) {
  27.                 $html = file_get_contents($url);
  28.                 if (preg_match("/(?<=<title>Preview\sPage\s\W\s)(.*?)(?=<\/title>)/", $html, $matches))
  29.                 {
  30.                     return $matches[0];
  31.                     echo '<a href="' . $matches[0] . '" target="_blank">' .$matches[0] . '</a>';
  32.                 }
  33.             } else { // se l'url non è una di quelle listate, allora scrivo
  34.                 echo 'Non è stata inserita una url valida. <br />';
  35.                 echo 'Inserire una url accorciata(shorten url) di uno dei seguenti siti: <br />';
  36.                 echo 'http://adf.ly/XXXXX <br />';
  37.                 echo 'XXXXXXXX.linkbucks.com <br />';
  38.                 echo '----- Url Shorten target url finder made by <strong>IAL32</strong> -----';
  39.             }
  40.         } else { // se non è stata inserita alcuna url dopo ?url= allora scrivo
  41.             echo 'Non è stata inserita alcuna url da analizzare. <br />';
  42.             echo 'Inserire la url di destinazione dopo "<strong>?url=</strong>" <br />';
  43.             echo '----- Url Shorten target url finder made by <strong>IAL32</strong> -----';
  44.         }
  45.     }
  46.     /* get_redirect_url()
  47.     * Gets the address that the provided URL redirects to,
  48.     * or FALSE if there's no redirect.
  49.     *
  50.     * @param string $url
  51.     * @return string
  52.     */
  53.     function get_redirect_url($url){
  54.         $redirect_url = null;
  55.         $url_parts = @parse_url($url);
  56.         if (!$url_parts) return false;
  57.         if (!isset($url_parts['host'])) return false; //can't process relative URLs
  58.         if (!isset($url_parts['path'])) $url_parts['path'] = '/';
  59.         $sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30);
  60.         if (!$sock) return false;
  61.         $request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1\r\n";
  62.         $request .= 'Host: ' . $url_parts['host'] . "\r\n";
  63.         $request .= "Connection: Close\r\n\r\n";
  64.         fwrite($sock, $request);
  65.         $response = '';
  66.         while(!feof($sock)) $response .= fread($sock, 8192);
  67.         fclose($sock);
  68.         if (preg_match('/^Location: (.+?)$/m', $response, $matches)){
  69.             if ( substr($matches[1], 0, 1) == "/" )
  70.                 return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]);
  71.             else
  72.                 return trim($matches[1]);
  73.    
  74.         } else {
  75.             return false;
  76.         }
  77.     }
  78.     /* get_all_redirects()
  79.     * Follows and collects all redirects, in order, for the given URL.
  80.     *
  81.     * @param string $url
  82.     * @return array
  83.     */
  84.     function get_all_redirects($url){
  85.         $redirects = array();
  86.         while ($newurl = get_redirect_url($url)){
  87.             if (in_array($newurl, $redirects)){
  88.                 break;
  89.             }
  90.             $redirects[] = $newurl;
  91.             $url = $newurl;
  92.         }
  93.         return $redirects;
  94.     }
  95.     /* get_final_url()
  96.     * Gets the address that the URL ultimately leads to.
  97.     * Returns $url itself if it isn't a redirect.
  98.     *
  99.     * @param string $url
  100.     * @return string
  101.     */
  102.     function get_final_url($url){
  103.         $redirects = get_all_redirects($url);
  104.         if (count($redirects)>0){
  105.             return array_pop($redirects);
  106.         } else {
  107.             return $url;
  108.         }
  109.     }
  110. ?>
Add Comment
Please, Sign In to add comment