pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

kohana private pastebin - collaborative debugging tool What's a private pastebin?


Posted by Shadowhand on Thu 31 Jan 15:44 (modification of post by view diff)
report abuse | download | new post

  1. <?php
  2. /**
  3.  * is_file_url() check if a URL gives a valid response code
  4.  *
  5.  * @param   STRING  $url            - The URL to check (needs scheme and host, must be http only)
  6.  * @param   BOOL    $allowRedirects - If a 3xx code is returned and a location header is given, should we follow it?
  7.  * @param   INT     $maxNumRedirs   - How many redirects should we follow befroe bailing out?
  8.  * @param   INT     $redirNum       - Do NOT Set this in your calls to the function--used to track recursive calls and avoid endless recursion
  9.  * @return  BOOL                    - Whether or not the URL returned a valid response code
  10.  */
  11. function is_file_url($url,$allowRedirects=true,$maxNumRedirs=5,$redirNum=0)
  12. {
  13.   $returnValue = false;
  14.   if($redirNum<=$maxNumRedirs)
  15.   {
  16.     $check = httpRequestStatus($url);
  17.     if($check[0]==200)
  18.     {
  19.       $returnValue = true;
  20.     }
  21.     elseif($allowRedirects===true && substr($check[0],0,1)=='3' && !empty($check[1]))
  22.     {
  23.       $returnValue = is_file_url($check[1],$allowRedirects,$maxNumRedirs,($redirNum+1));
  24.     }
  25.   }
  26.   return $returnValue;
  27. }
  28.  
  29. function httpRequestStatus($url){
  30.   $returnValue = array('0','');
  31.   $urlParts = parse_url($url);
  32.   if($urlParts['scheme']=='http' &&
  33.      (gethostbyname($urlParts['host'])!=$urlParts['host'] ||
  34.       preg_match("/^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/",$urlParts['host'])
  35.      )
  36.     )
  37.   {
  38.     $urlParts['path'] = !empty($urlParts['path']) ? $urlParts['path'] : '/';
  39.     $httpREQ = "HEAD {$urlParts['path']} HTTP/1.0\r\nHost: {$urlParts['host']}\r\n\r\n";
  40.           $httpSocket = @fsockopen($urlParts['host'], 80, $errno, $errstr, 5);
  41.     if(is_resource($httpSocket)){
  42.       $returnFromServer = '';
  43.       fputs($httpSocket, $httpREQ);
  44.       while(!feof($httpSocket)){
  45.         $returnFromServer .= fgets($httpSocket,128);
  46.         if(ereg("\r\n\r\n",$returnFromServer)){
  47.                                   break;
  48.         }
  49.       }
  50.       fclose($httpSocket);
  51.       if(!empty($returnFromServer)){
  52.         if(preg_match('/\d{3}/',$returnFromServer,$statusCode)){
  53.           $statusCode = $statusCode[0];
  54.           $locationField = (
  55.                             substr($statusCode,0,1)=='3' &&
  56.                             preg_match('/Location: (.*?)\r\n/i',$returnFromServer,$parsedLocationField) &&
  57.                             isset($parsedLocationField[1])
  58.                            )
  59.                          ? $parsedLocationField[1]
  60.                          : '';
  61.           $returnValue = array($statusCode,$locationField);
  62.         }
  63.       }
  64.     }
  65.         }
  66.         return $returnValue;
  67. }
  68.  
  69. //Examples:
  70. //Valid URL, is_file_url() returns true
  71. $url = 'http://www.jaaulde.com/';
  72. echo $url.' is '.(is_file_url($url)?'good':'bad')."\n";
  73. //Invalid URL, is_file_url() returns false
  74. $url = 'http://www.jaaulde.com/feuhfiuhufih';
  75. echo $url.' is '.(is_file_url($url)?'good':'bad')."\n";
  76. //Valid URL, that has redirection, when allowing redirection is_file_url() returns true
  77. $url = 'http://www.jaaulde.com/test_bed/cookieLib';
  78. echo $url.' is '.(is_file_url($url)?'good':'bad')."\n";
  79. //Valid URL, that has redirection, when NOT allowing redirection is_file_url() returns false
  80. $url = 'http://www.jaaulde.com/test_bed/cookieLib';
  81. echo $url.' is '.(is_file_url($url,false)?'good':'bad')."\n";
  82.  
  83. //Sample output when run from command line:
  84. /*
  85.  
  86. Jims-MacBook-Pro:~ jim$ php isfileurl.php
  87. http://www.jaaulde.com/ is good
  88. http://www.jaaulde.com/feuhfiuhufih is bad
  89. http://www.jaaulde.com/test_bed/cookieLib is good
  90. http://www.jaaulde.com/test_bed/cookieLib is bad
  91. Jims-MacBook-Pro:~ jim$
  92.  
  93. */
  94. ?>

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post