AlanGomes

Código

Mar 15th, 2015
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.94 KB | None | 0 0
  1. <?php
  2. require_once 'db.php';
  3.  
  4. function url_exist($url)
  5. {
  6.     $c=curl_init();
  7.     curl_setopt($c,CURLOPT_URL,$url);
  8.     curl_setopt($c,CURLOPT_HEADER,1);
  9.     curl_setopt($c,CURLOPT_NOBODY,1);
  10.     curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
  11.     if(!curl_exec($c)){
  12.         return false;
  13.     }else{
  14.         return true;
  15.     }
  16. }
  17.  
  18. function checarUrl($url) {
  19.    $parseUrl = parse_url(trim($url));
  20.    $dominio = $parseUrl['host'];
  21.    return $dominio == "www.frhub.ga" || $dominio == "frhub.ga";
  22. }
  23.  
  24. function shorteURLFromID ($integer)
  25. {
  26.     $base = '0123456789abcdefghijklmnopqrstuvwxyz';
  27.     $length = strlen($base);
  28.     while($integer > $length - 5)
  29.     {
  30.         $out = $base[fmod($integer, $length)] . $out;
  31.         $integer = floor( $integer / $length );
  32.     }
  33.     return $base[$integer] . $out;
  34. }
  35.  
  36. if(isset($_POST['url']))
  37. {
  38.     $url = trim(strip_tags($_POST['url']));
  39.     $url = str_replace(array("http://", "http://www.", "www."), array('', '', ''), $url);
  40.     if(strlen($url) < 3) die("Coloque uma URL");
  41.     if(url_exist($url))
  42.     {
  43.         //check if exists
  44.         $rs = mysql_query("SELECT string FROM links WHERE destination = '".mysql_real_escape_string($url)."'");
  45.         if(@mysql_num_rows($rs)) {
  46.             $row=@mysql_fetch_object($rs);
  47.             $string = $row->string;
  48.             print "http://".$_SERVER['SERVER_NAME']."/".$string;
  49.         }else{
  50.             if (checarUrl($_POST['url'])) {
  51.                 die("Coloque uma URL válida!");
  52.                 exit;
  53.             }
  54.             $rs = mysql_query("INSERT INTO links (destination, added, ip) VALUES
  55.             ('".mysql_real_escape_string($url)."', '".time()."', '".ip2long($_SERVER['REMOTE_ADDR'])."')");
  56.             if($rs) {
  57.                 $string = shorteURLFromID(mysql_insert_id());
  58.                 $rs = mysql_query("UPDATE links SET string = '$string' WHERE linkID = '".mysql_insert_id()."'");
  59.                 print "http://".$_SERVER['SERVER_NAME']."/".$string;
  60.             }else{
  61.                 print "Não foi possivel encurtar este link.".mysql_error();
  62.             }
  63.         }
  64.        
  65.     }else{
  66.         print "URL ínvalida!";
  67.     }
  68. }else{
  69.     print 'URL não recebida<br/>';
  70. }
Advertisement
Add Comment
Please, Sign In to add comment