Avail feature updated
[ninja.git] / system / helpers / remote.php
blob4e219c7f8e6b0861f0c7e2f649bf4477692b5649
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Remote url/file helper.
5 * $Id: remote.php 3917 2009-01-21 03:06:22Z zombor $
7 * @package Core
8 * @author Kohana Team
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 class remote {
14 public static function status($url)
16 if ( ! valid::url($url, 'http'))
17 return FALSE;
19 // Get the hostname and path
20 $url = parse_url($url);
22 if (empty($url['path']))
24 // Request the root document
25 $url['path'] = '/';
28 // Open a remote connection
29 $remote = fsockopen($url['host'], 80, $errno, $errstr, 5);
31 if ( ! is_resource($remote))
32 return FALSE;
34 // Set CRLF
35 $CRLF = "\r\n";
37 // Send request
38 fwrite($remote, 'HEAD '.$url['path'].' HTTP/1.0'.$CRLF);
39 fwrite($remote, 'Host: '.$url['host'].$CRLF);
40 fwrite($remote, 'Connection: close'.$CRLF);
41 fwrite($remote, 'User-Agent: Kohana Framework (+http://kohanaphp.com/)'.$CRLF);
43 // Send one more CRLF to terminate the headers
44 fwrite($remote, $CRLF);
46 while ( ! feof($remote))
48 // Get the line
49 $line = trim(fgets($remote, 512));
51 if ($line !== '' AND preg_match('#^HTTP/1\.[01] (\d{3})#', $line, $matches))
53 // Response code found
54 $response = (int) $matches[1];
56 break;
60 // Close the connection
61 fclose($remote);
63 return isset($response) ? $response : FALSE;
66 } // End remote