final move of files
[web.mtrack] / Auth / Yadis / HTTPFetcher.php
1 <?php
2
3 /**
4  * This module contains the HTTP fetcher interface
5  *
6  * PHP versions 4 and 5
7  *
8  * LICENSE: See the COPYING file included in this distribution.
9  *
10  * @package OpenID
11  * @author JanRain, Inc. <openid@janrain.com>
12  * @copyright 2005-2008 Janrain, Inc.
13  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
14  */
15
16 /**
17  * Require logging functionality
18  */
19 require_once "Auth/OpenID.php";
20
21 define('Auth_OpenID_FETCHER_MAX_RESPONSE_KB', 1024);
22 define('Auth_OpenID_USER_AGENT', 
23        'php-openid/'.Auth_OpenID_VERSION.' (php/'.phpversion().')');
24
25 class Auth_Yadis_HTTPResponse {
26     function Auth_Yadis_HTTPResponse($final_url = null, $status = null,
27                                          $headers = null, $body = null)
28     {
29         $this->final_url = $final_url;
30         $this->status = $status;
31         $this->headers = $headers;
32         $this->body = $body;
33     }
34 }
35
36 /**
37  * This class is the interface for HTTP fetchers the Yadis library
38  * uses.  This interface is only important if you need to write a new
39  * fetcher for some reason.
40  *
41  * @access private
42  * @package OpenID
43  */
44 class Auth_Yadis_HTTPFetcher {
45
46     var $timeout = 20; // timeout in seconds.
47
48     /**
49      * Return whether a URL can be fetched.  Returns false if the URL
50      * scheme is not allowed or is not supported by this fetcher
51      * implementation; returns true otherwise.
52      *
53      * @return bool
54      */
55     function canFetchURL($url)
56     {
57         if ($this->isHTTPS($url) && !$this->supportsSSL()) {
58             Auth_OpenID::log("HTTPS URL unsupported fetching %s",
59                              $url);
60             return false;
61         }
62
63         if (!$this->allowedURL($url)) {
64             Auth_OpenID::log("URL fetching not allowed for '%s'",
65                              $url);
66             return false;
67         }
68
69         return true;
70     }
71
72     /**
73      * Return whether a URL should be allowed. Override this method to
74      * conform to your local policy.
75      *
76      * By default, will attempt to fetch any http or https URL.
77      */
78     function allowedURL($url)
79     {
80         return $this->URLHasAllowedScheme($url);
81     }
82
83     /**
84      * Does this fetcher implementation (and runtime) support fetching
85      * HTTPS URLs?  May inspect the runtime environment.
86      *
87      * @return bool $support True if this fetcher supports HTTPS
88      * fetching; false if not.
89      */
90     function supportsSSL()
91     {
92         trigger_error("not implemented", E_USER_ERROR);
93     }
94
95     /**
96      * Is this an https URL?
97      *
98      * @access private
99      */
100     function isHTTPS($url)
101     {
102         return (bool)preg_match('/^https:\/\//i', $url);
103     }
104
105     /**
106      * Is this an http or https URL?
107      *
108      * @access private
109      */
110     function URLHasAllowedScheme($url)
111     {
112         return (bool)preg_match('/^https?:\/\//i', $url);
113     }
114
115     /**
116      * @access private
117      */
118     function _findRedirect($headers)
119     {
120         foreach ($headers as $line) {
121             if (strpos(strtolower($line), "location: ") === 0) {
122                 $parts = explode(" ", $line, 2);
123                 return $parts[1];
124             }
125         }
126         return null;
127     }
128
129     /**
130      * Fetches the specified URL using optional extra headers and
131      * returns the server's response.
132      *
133      * @param string $url The URL to be fetched.
134      * @param array $extra_headers An array of header strings
135      * (e.g. "Accept: text/html").
136      * @return mixed $result An array of ($code, $url, $headers,
137      * $body) if the URL could be fetched; null if the URL does not
138      * pass the URLHasAllowedScheme check or if the server's response
139      * is malformed.
140      */
141     function get($url, $headers = null)
142     {
143         trigger_error("not implemented", E_USER_ERROR);
144     }
145 }
146
147 ?>