Initial Kohana install
[speedfreak] / Server / system / helpers / remote.php
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3  * Remote url/file helper.
4  *
5  * $Id: remote.php 3769 2008-12-15 00:48:56Z zombor $
6  *
7  * @package    Core
8  * @author     Kohana Team
9  * @copyright  (c) 2007-2008 Kohana Team
10  * @license    http://kohanaphp.com/license.html
11  */
12 class remote_Core {
13
14         public static function status($url)
15         {
16                 if ( ! valid::url($url, 'http'))
17                         return FALSE;
18
19                 // Get the hostname and path
20                 $url = parse_url($url);
21
22                 if (empty($url['path']))
23                 {
24                         // Request the root document
25                         $url['path'] = '/';
26                 }
27
28                 // Open a remote connection
29                 $remote = fsockopen($url['host'], 80, $errno, $errstr, 5);
30
31                 if ( ! is_resource($remote))
32                         return FALSE;
33
34                 // Set CRLF
35                 $CRLF = "\r\n";
36
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);
42
43                 // Send one more CRLF to terminate the headers
44                 fwrite($remote, $CRLF);
45
46                 while ( ! feof($remote))
47                 {
48                         // Get the line
49                         $line = trim(fgets($remote, 512));
50
51                         if ($line !== '' AND preg_match('#^HTTP/1\.[01] (\d{3})#', $line, $matches))
52                         {
53                                 // Response code found
54                                 $response = (int) $matches[1];
55
56                                 break;
57                         }
58                 }
59
60                 // Close the connection
61                 fclose($remote);
62
63                 return isset($response) ? $response : FALSE;
64         }
65
66 } // End remote