[ Index ] |
|
Code source de b2evolution 2.1.0-beta |
1 <?php 2 /** 3 * 4 * 5 * This file is part of the b2evolution/evocms project - {@link http://b2evolution.net/}. 6 * See also {@link http://sourceforge.net/projects/evocms/}. 7 * 8 * @copyright (c)2003-2007 by Francois PLANQUE - {@link http://fplanque.net/}. 9 * Parts of this file are copyright (c)2006 by Daniel HAHLER - {@link http://daniel.hahler.de/}. 10 * 11 * @license http://b2evolution.net/about/license.html GNU General Public License (GPL) 12 * 13 * {@internal Open Source relicensing agreement: 14 * Daniel HAHLER grants Francois PLANQUE the right to license 15 * Daniel HAHLER's contributions to this file and the b2evolution project 16 * under any OSI approved OSS license (http://www.opensource.org/licenses/). 17 * }} 18 * 19 * @package evocore 20 * 21 * @author blueyed: Daniel HAHLER 22 * @author Danny Ferguson 23 * 24 * @version $Id: _url.funcs.php,v 1.2 2007/07/03 22:16:15 blueyed Exp $ 25 */ 26 if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' ); 27 28 29 /** 30 * Fetch remote page 31 * 32 * Attempt to retrieve a remote page, first with cURL, then fsockopen, then fopen. 33 * 34 * @todo dh> Should we try remaining methods, if the previous one(s) failed? 35 * @param string URL 36 * @param array Info (by reference) 37 * 'error': holds error message, if any 38 * 'status': HTTP status (e.g. 200 or 404) 39 * 'used_method': Used method ("curl", "fopen", "fsockopen" or null if no method 40 * is available) 41 * @return string|false The remote page as a string; false in case of error 42 */ 43 function fetch_remote_page( $url, & $info ) 44 { 45 $info = array( 46 'error' => '', 47 'status' => NULL ); 48 49 // CURL: 50 if( extension_loaded('curl') ) 51 { 52 $info['used_method'] = 'curl'; 53 54 $ch = curl_init(); 55 curl_setopt($ch, CURLOPT_URL, $url); 56 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 57 if( ! empty($params['method']) && $params['method'] == 'HEAD' ) 58 { 59 curl_setopt($ch, CURLOPT_NOBODY, true); 60 } 61 $r = curl_exec($ch); 62 $info['status'] = curl_getinfo($ch, CURLINFO_HTTP_CODE); 63 $info['error'] = curl_error($ch); 64 if( curl_errno($ch) ) 65 { 66 $info['error'] .= '(#'.curl_errno($ch).')'; 67 } 68 curl_close($ch); 69 70 return $r; 71 } 72 73 74 // FSOCKOPEN: 75 if( function_exists('fsockopen') ) // may have been disabled 76 { 77 $info['used_method'] = 'fsockopen'; 78 $url_parsed = parse_url($url); 79 if( empty($url_parsed['scheme']) ) { 80 $url_parsed = parse_url('http://'.$url); 81 } 82 83 $host = $url_parsed['host']; 84 $port = ( empty($url_parsed['port']) ? 80 : $url_parsed['port'] ); 85 $path = empty($url_parsed['path']) ? '/' : $url_parsed['path']; 86 if( ! empty($url_parsed['query']) ) 87 { 88 $path .= '?'.$url_parsed['query']; 89 } 90 91 $out = "GET $path HTTP/1.0\r\n"; 92 $out .= "Host: $host:$port\r\n"; 93 $out .= "Connection: Close\r\n\r\n"; 94 95 $fp = @fsockopen($host, $port, $errno, $errstr, 30); 96 if( ! $fp ) 97 { 98 $info['error'] = $errstr.' (#'.$errno.')'; 99 return false; 100 } 101 102 // Set timeout for data: 103 if( function_exists('stream_set_timeout') ) 104 stream_set_timeout( $fp, 20 ); // PHP 4.3.0 105 else 106 socket_set_timeout( $fp, 20 ); // PHP 4 107 108 // Send request: 109 fwrite($fp, $out); 110 111 // Read response: 112 $r = ''; 113 // First line: 114 $s = fgets($fp, 4096); 115 if( ! preg_match( '~^HTTP/\d+\.\d+ (\d+)~', $s, $match ) ) 116 { 117 $info['error'] = 'Invalid response.'; 118 $r = false; 119 } 120 else 121 { 122 $info['status'] = $match[1]; 123 124 $foundBody = false; 125 while( ! feof($fp) ) 126 { 127 $s = fgets($fp, 4096); 128 if( $s == "\r\n" ) 129 { 130 $foundBody = true; 131 continue; 132 } 133 if( $foundBody ) 134 { 135 $r .= $s; 136 } 137 } 138 } 139 fclose($fp); 140 return $r; 141 } 142 143 144 // URL FOPEN: 145 if( ini_get('allow_url_fopen') && function_exists('stream_get_meta_data') /* PHP 4.3, may also be disabled!? */ ) 146 { 147 $info['used_method'] = 'fopen'; 148 149 $fp = @fopen($url, 'r'); 150 if( $fp ) 151 { // this will be false e.g. for "404", but it's not trivial to get the status error for this, so we retry with fsockopen further down 152 // headers: 153 $meta = stream_get_meta_data($fp); 154 if( ! $meta || ! preg_match( '~^HTTP/\d+\.\d+ (\d+)~', $meta['wrapper_data'][0], $match ) ) 155 { 156 $info['error'] = 'Invalid response.'; 157 $r = false; 158 } 159 else 160 { 161 $info['status'] = $match[1]; 162 $r = ''; 163 while( $buf = fread($fp, 4096) ) 164 { //read the complete file (binary safe) 165 $r .= $buf; 166 } 167 } 168 fclose($fp); 169 170 return $r; 171 } 172 } 173 174 175 // All failed: 176 $info['used_method'] = null; 177 $info['error'] = 'No method available to access URL!'; 178 return false; 179 } 180 181 182 /** 183 * Get $url with the same protocol (http/https) as $other_url. 184 * 185 * @param string URL 186 * @param string other URL (defaults to {@link $ReqHost}) 187 * @return string 188 */ 189 function url_same_protocol( $url, $other_url = NULL ) 190 { 191 if( is_null($other_url) ) 192 { 193 global $ReqHost; 194 195 $other_url = $ReqHost; 196 } 197 198 // change protocol of $url to same of admin ('https' <=> 'http') 199 if( substr( $url, 0, 7 ) == 'http://' ) 200 { 201 if( substr( $other_url, 0, 8 ) == 'https://' ) 202 { 203 $url = 'https://'.substr( $url, 7 ); 204 } 205 } 206 elseif( substr( $url, 0, 8 ) == 'https://' ) 207 { 208 if( substr( $other_url, 0, 7 ) == 'http://' ) 209 { 210 $url = 'http://'.substr( $url, 8 ); 211 } 212 } 213 214 return $url; 215 } 216 217 218 /* {{{ Revision log: 219 * $Log: _url.funcs.php,v $ 220 * Revision 1.2 2007/07/03 22:16:15 blueyed 221 * Solved todo: changed order to "cURL, fsockopen, fopen" for fetch_remote_page 222 * 223 * Revision 1.1 2007/06/25 10:58:54 fplanque 224 * MODULES (refactored MVC) 225 * 226 * Revision 1.10 2007/04/26 00:11:08 fplanque 227 * (c) 2007 228 * 229 * Revision 1.9 2007/03/12 22:39:42 blueyed 230 * Fixed just introduced E_PARSE 231 * 232 * Revision 1.8 2007/03/12 22:12:58 blueyed 233 * fetch_remote_page(): handle case when fsockopen is disabled 234 * 235 * Revision 1.7 2007/03/08 22:55:21 blueyed 236 * fetch_remote_page: Added "used_method" to $info and errno to "curl" method. 237 * 238 * Revision 1.6 2006/12/22 00:25:15 blueyed 239 * Do not send URL fragment in GET 240 * 241 * Revision 1.5 2006/12/21 21:50:52 fplanque 242 * doc 243 * 244 * Revision 1.4 2006/12/19 21:35:26 blueyed 245 * fetch_remote_page(): if url_fopen fails, use fsockopen-fallback (for errstr and status). Plus minor fixes. 246 * 247 * Revision 1.3 2006/12/01 17:31:38 blueyed 248 * Fixed url_fopen method for fetch_remote_page 249 * 250 * Revision 1.2 2006/11/29 20:48:46 blueyed 251 * Moved url_rel_to_same_host() from _misc.funcs.php to _url.funcs.php 252 * 253 * Revision 1.1 2006/11/25 23:00:39 blueyed 254 * Added file for URL handling. Includes fetch_remote_page() 255 * 256 * }}} 257 */ 258 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Thu Nov 29 23:58:50 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |