[ Index ] |
|
Code source de Dotclear 2.0-beta6 |
1 <?php 2 # ***** BEGIN LICENSE BLOCK ***** 3 # This file is part of Clearbricks. 4 # Copyright (c) 2006 Olivier Meunier and contributors. All rights 5 # reserved. 6 # 7 # Clearbricks is free software; you can redistribute it and/or modify 8 # it under the terms of the GNU General Public License as published by 9 # the Free Software Foundation; either version 2 of the License, or 10 # (at your option) any later version. 11 # 12 # Clearbricks is distributed in the hope that it will be useful, 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # GNU General Public License for more details. 16 # 17 # You should have received a copy of the GNU General Public License 18 # along with Clearbricks; if not, write to the Free Software 19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 # 21 # ***** END LICENSE BLOCK ***** 22 23 class http 24 { 25 /** 26 @function getHost 27 28 Return current scheme, host and port. 29 */ 30 public static function getHost() 31 { 32 $server_name = explode(':',$_SERVER['HTTP_HOST']); 33 $server_name = $server_name[0]; 34 if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') 35 { 36 $scheme = 'https'; 37 $port = ($_SERVER['SERVER_PORT'] != '443') ? ':'.$_SERVER['SERVER_PORT'] : ''; 38 } 39 else 40 { 41 $scheme = 'http'; 42 $port = ($_SERVER['SERVER_PORT'] != '80') ? ':'.$_SERVER['SERVER_PORT'] : ''; 43 } 44 45 return $scheme.'://'.$server_name.$port; 46 } 47 48 /** 49 @function getSelfURI 50 51 Returns current URI with full hostname 52 */ 53 public static function getSelfURI() 54 { 55 return self::getHost().$_SERVER['REQUEST_URI']; 56 } 57 58 /** 59 @function redirect 60 61 Performs a conforming HTTP redirect for a relative URL. 62 63 @param page string Relative URL 64 */ 65 public static function redirect($page) 66 { 67 if (preg_match('%^http[s]?://%',$page)) 68 { 69 $redir = $page; 70 } 71 else 72 { 73 $host = self::getHost(); 74 $dir = dirname($_SERVER['PHP_SELF']); 75 76 if (substr($page,0,1) == '/') { 77 $redir = $host.$page; 78 } else { 79 if (substr($dir,-1) == '/') { 80 $dir = substr($dir,0,-1); 81 } 82 $redir = $host.$dir.'/'.$page; 83 } 84 } 85 86 # Close session if exists 87 if (session_id()) { 88 session_write_close(); 89 } 90 91 header('Location: '.$redir); 92 exit; 93 } 94 95 /** 96 @function concatURL 97 98 Appends a path to a given URL. If path begins with "/" it will replace the 99 original URL path. 100 101 @param url string URL 102 @param path string Path to append 103 @return string 104 */ 105 public static function concatURL($url,$path) 106 { 107 if (substr($path,0,1) != '/') { 108 return $url.$path; 109 } 110 111 return preg_replace('#^(.+?//.+?)/(.*)$#','$1'.$path,$url); 112 } 113 114 /** 115 @function realIP 116 117 Returns the real client IP (or tries to do its best) 118 Taken from http://uk.php.net/source.php?url=/include/ip-to-country.inc 119 120 @return string 121 */ 122 public static function realIP() 123 { 124 # No IP found (will be overwritten by for 125 # if any IP is found behind a firewall) 126 $ip = FALSE; 127 128 # If HTTP_CLIENT_IP is set, then give it priority 129 if (!empty($_SERVER['HTTP_CLIENT_IP'])) { 130 $ip = $_SERVER['HTTP_CLIENT_IP']; 131 } 132 133 # User is behind a proxy and check that we discard RFC1918 IP addresses 134 # if they are behind a proxy then only figure out which IP belongs to the 135 # user. Might not need any more hackin if there is a squid reverse proxy 136 # infront of apache. 137 if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { 138 139 # Put the IP's into an array which we shall work with shortly. 140 $ips = explode (', ', $_SERVER['HTTP_X_FORWARDED_FOR']); 141 if ($ip) { array_unshift($ips, $ip); $ip = FALSE; } 142 143 for ($i = 0; $i < count($ips); $i++) 144 { 145 # Skip RFC 1918 IP's 10.0.0.0/8, 172.16.0.0/12 and 146 # 192.168.0.0/16 -- jim kill me later with my regexp pattern 147 # below. 148 if (!preg_match('/^(10|172\.16|192\.168)\./',$ips[$i])) { 149 if (ip2long($ips[$i]) != false) { 150 $ip = $ips[$i]; 151 break; 152 } 153 } 154 } 155 } 156 157 # Return with the found IP or the remote address 158 if ($ip) { 159 return $ip; 160 } elseif (isset($_SERVER['REMOTE_ADDR'])) { 161 return $_SERVER['REMOTE_ADDR']; 162 } else { 163 return null; 164 } 165 } 166 167 /** 168 @function getAcceptLanguage 169 170 Returns a two letters language code take from HTTP_ACCEPT_LANGUAGE. 171 172 @return string 173 */ 174 public static function getAcceptLanguage() 175 { 176 $dlang = ''; 177 if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) 178 { 179 $acclang = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); 180 $L = explode(';', $acclang[0]); 181 $dlang = substr(trim($L[0]),0,2); 182 } 183 184 return $dlang; 185 } 186 187 /** 188 @function cache 189 190 Sends HTTP cache headers (304) according to a list of files and an optionnal 191 list of timestamps. 192 193 @param files array Files on which check mtime 194 @param mod_ts array List of timestamps 195 */ 196 public static function cache($files,$mod_ts=array()) 197 { 198 if (empty($files) || !is_array($files)) { 199 return; 200 } 201 202 array_walk($files,create_function('&$v','$v = filemtime($v);')); 203 204 $array_ts = array_merge($mod_ts,$files); 205 206 rsort($array_ts); 207 $ts = $array_ts[0]; 208 209 $since = NULL; 210 if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { 211 $since = $_SERVER['HTTP_IF_MODIFIED_SINCE']; 212 $since = preg_replace ('/^(.*)(Mon|Tue|Wed|Thu|Fri|Sat|Sun)(.*)(GMT)(.*)/', '$2$3 GMT', $since); 213 $since = strtotime($since); 214 } 215 216 # Common headers list 217 $headers[] = 'Last-Modified: '.gmdate('D, d M Y H:i:s',$ts).' GMT'; 218 $headers[] = 'Cache-Control: must-revalidate, max-age=0'; 219 $headers[] = 'Pragma:'; 220 221 if ($since >= $ts) 222 { 223 self::head(304,'Not Modified'); 224 foreach ($headers as $v) { 225 header($v); 226 } 227 exit; 228 } 229 else 230 { 231 header('Date: '.gmdate('D, d M Y H:i:s').' GMT'); 232 foreach ($headers as $v) { 233 header($v); 234 } 235 } 236 } 237 238 /** 239 @function etag 240 241 Sends HTTP cache headers (304) according to a list of etags in client request 242 243 @param p_content string Response page content 244 */ 245 public static function etag() 246 { 247 # We create an etag from all arguments 248 $args = func_get_args(); 249 if (empty($args)) { 250 return; 251 } 252 253 $etag = md5(implode('',$args)); 254 unset($args); 255 256 header('ETag: "'.$etag.'"'); 257 258 # Do we have a previously sent content? 259 if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) 260 { 261 foreach (explode(',',$_SERVER['HTTP_IF_NONE_MATCH']) as $i) 262 { 263 if (stripslashes(trim($i)) == $etag) { 264 self::head(304,'Not Modified'); 265 exit; 266 } 267 } 268 } 269 } 270 271 /** 272 @function head 273 274 Sends an HTTP code and message to client 275 276 @param code string HTTP code 277 @param msg string Message 278 */ 279 public static function head($code,$msg=null) 280 { 281 $status_mode = preg_match('/cgi/',php_sapi_name()); 282 283 if (!$msg) 284 { 285 $msg_codes = array( 286 100 => 'Continue', 287 101 => 'Switching Protocols', 288 200 => 'OK', 289 201 => 'Created', 290 202 => 'Accepted', 291 203 => 'Non-Authoritative Information', 292 204 => 'No Content', 293 205 => 'Reset Content', 294 206 => 'Partial Content', 295 300 => 'Multiple Choices', 296 301 => 'Moved Permanently', 297 302 => 'Found', 298 303 => 'See Other', 299 304 => 'Not Modified', 300 305 => 'Use Proxy', 301 307 => 'Temporary Redirect', 302 400 => 'Bad Request', 303 401 => 'Unauthorized', 304 402 => 'Payment Required', 305 403 => 'Forbidden', 306 404 => 'Not Found', 307 405 => 'Method Not Allowed', 308 406 => 'Not Acceptable', 309 407 => 'Proxy Authentication Required', 310 408 => 'Request Timeout', 311 409 => 'Conflict', 312 410 => 'Gone', 313 411 => 'Length Required', 314 412 => 'Precondition Failed', 315 413 => 'Request Entity Too Large', 316 414 => 'Request-URI Too Long', 317 415 => 'Unsupported Media Type', 318 416 => 'Requested Range Not Satisfiable', 319 417 => 'Expectation Failed', 320 500 => 'Internal Server Error', 321 501 => 'Not Implemented', 322 502 => 'Bad Gateway', 323 503 => 'Service Unavailable', 324 504 => 'Gateway Timeout', 325 505 => 'HTTP Version Not Supported' 326 ); 327 328 $msg = isset($msg_codes[$code]) ? $msg_codes[$code] : '-'; 329 } 330 331 if ($status_mode) { 332 header('Status: '.$code.' '.$msg); 333 } else { 334 if (version_compare(phpversion(),'4.3.0','>=')) { 335 header($msg, TRUE, $code); 336 } else { 337 header('HTTP/1.x '.$code.' '.$msg); 338 } 339 } 340 } 341 342 /** 343 @function trimRequest 344 345 Trims every value in GET, POST, REQUEST and COOKIE vars. 346 Removes magic quotes if magic_quote_gpc is on. 347 */ 348 public static function trimRequest() 349 { 350 if(!empty($_GET)) { 351 array_walk($_GET,array('self','trimRequestHandler')); 352 } 353 if(!empty($_POST)) { 354 array_walk($_POST,array('self','trimRequestHandler')); 355 } 356 if(!empty($_REQUEST)) { 357 array_walk($_REQUEST,array('self','trimRequestHandler')); 358 } 359 if(!empty($_COOKIE)) { 360 array_walk($_COOKIE,array('self','trimRequestHandler')); 361 } 362 } 363 364 private static function trimRequestHandler(&$v,$key) 365 { 366 $v = self::trimRequestInVar($v); 367 } 368 369 private static function trimRequestInVar($value) 370 { 371 if (is_array($value)) 372 { 373 $result = array(); 374 foreach ($value as $k => $v) 375 { 376 if (is_array($v)) { 377 $result[$k] = self::trimRequestInVar($v); 378 } else { 379 if (get_magic_quotes_gpc()) { 380 $v = stripslashes($v); 381 } 382 $result[$k] = trim($v); 383 } 384 } 385 return $result; 386 } 387 else 388 { 389 if (get_magic_quotes_gpc()) { 390 $value = stripslashes($value); 391 } 392 return trim($value); 393 } 394 } 395 396 /** 397 @function unsetGlobals 398 399 If register_globals is on, removes every GET, POST, COOKIE, REQUEST, SERVER, 400 ENV, FILES vars from GLOBALS. 401 */ 402 public static function unsetGlobals() 403 { 404 if (!ini_get('register_globals')) { 405 return; 406 } 407 408 if (isset($_REQUEST['GLOBALS'])) { 409 throw new Exception('GLOBALS overwrite attempt detected'); 410 } 411 412 # Variables that shouldn't be unset 413 $no_unset = array('GLOBALS','_GET','_POST','_COOKIE','_REQUEST', 414 '_SERVER','_ENV','_FILES'); 415 416 $input = array_merge($_GET,$_POST,$_COOKIE,$_SERVER,$_ENV,$_FILES, 417 (isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array())); 418 419 foreach ($input as $k => $v) { 420 if (!in_array($k,$no_unset) && isset($GLOBALS[$k]) ) { 421 $GLOBALS[$k] = NULL; 422 unset($GLOBALS[$k]); 423 } 424 } 425 } 426 } 427 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Feb 23 22:16:06 2007 | par Balluche grâce à PHPXref 0.7 |