123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 16/1/30
- * Time: 下午3:46
- */
- class http_header
- {
- const COOKIE_EXPIRES = "; expires=";
- const COOKIE_MAX_AGE = "; Max-Age=";
- const COOKIE_DOMAIN = "; domain=";
- const COOKIE_PATH = "; path=";
- const COOKIE_SECURE = "; secure";
- const COOKIE_HTTPONLY = "; HttpOnly";
- private static $stHeader = NULL;
- private $mHeader = NULL;
- private function __construct()
- {
- }
- public function start()
- {
- $this->mHeader = new SplDoublyLinkedList();
- }
- public function end()
- {
- $sheader = '';
- foreach($this->mHeader as $val)
- {
- $sheader .= $val . "\r\n";
- }
- $sheader .= "\r\n";
- return $sheader;
- }
- static public function instance()
- {
- if(self::$stHeader == NULL) {
- self::$stHeader = new http_header();
- }
- return self::$stHeader;
- }
- public function setcookie($name, $value = null, $expire = null, $path = null, $domain = null, $secure = null,$url_encode = true, $httponly = null)
- {
- if(empty($name)) {
- return false;
- } else if(strpbrk($name, "=,; \t\r\n\013\014") != false) {
- Log::record('Cookie names cannot contain any of the following \'=,; \\t\\r\\n\\013\\014\'');
- return false;
- }
- if(empty($value)) {
- $cookie = sprintf("Set-Cookie: %s=deleted; expires=%s; Max-Age=0", $name, gmdate("D, d-M-Y H:i:s T",1));
- }
- else
- {
- if(!$url_encode && strpbrk($value,"=,; \t\r\n\013\014") != false) {
- Log::record('Cookie valuse cannot contain any of the following \'=,; \\t\\r\\n\\013\\014\'');
- return false;
- }
- if($url_encode) {
- $cookie = sprintf("Set-Cookie: %s=%s", $name, !empty($value) ? urlencode($value) : "");
- } else {
- $cookie = sprintf("Set-Cookie: %s=%s", $name, !empty($value) ? $value : "");
- }
- if($expire > 0) {
- $tmp = self::COOKIE_EXPIRES . gmdate("D, d-M-Y H:i:s T",$expire);
- $cookie .= $tmp;
- }
- $tsdelta = sprintf("%d", $expire - time());
- $cookie .= self::COOKIE_MAX_AGE . $tsdelta;
- }
- if(!empty($path)) {
- $cookie .= self::COOKIE_PATH . $path;
- }
- if(!empty($domain)) {
- $cookie .= self::COOKIE_DOMAIN . $domain;
- }
- if(!empty($secure)) {
- $cookie .= self::COOKIE_SECURE . $secure;
- }
- if(!empty($httponly)) {
- $cookie .= self::COOKIE_HTTPONLY . $httponly;
- }
- $this->mHeader->push($cookie);
- return true;
- }
- private function replace($string)
- {
- $reg = '/^([^:]*): (.*)/i';
- $string = trim($string);
- if(preg_match($reg,$string,$m)) {
- $sname =strtolower($m[1]);
- }
- $index = 0;
- for($this->mHeader->rewind();$this->mHeader->valid();$this->mHeader->next())
- {
- $val = $this->mHeader->current();
- if(preg_match($reg,$val,$m))
- {
- $hname = strtolower($m[1]);
- if(strcmp($sname,$hname) == 0) {
- $this->mHeader->offsetUnset($index);
- break;
- }
- $index++;
- }
- }
- $this->mHeader->push($string);
- }
- public function header ($string, $replace = true, $http_response_code = null)
- {
- if(empty($string)) return;
- $datas = str_split($string);
- foreach($datas as $val) {
- if($val == '\r' || $val == '\n') {
- Log::record("Header may not contain more than a single header, new line detected",Log::ERR);
- return;
- }
- }
- if($replace) {
- $this->replace($string);
- } else {
- $this->mHeader->push($string);
- }
- }
- }
- function init_cookie($cookie)
- {
- $regxp = '/([^=]+=[^;]*)[;]?/i';
- $val = preg_match_all($regxp,$cookie,$match);
- foreach($_COOKIE as $key => $val) {
- unset($_COOKIE[$key]);
- }
- if($val == 2)
- {
- foreach($match[1] as $val)
- {
- $kv = preg_split('/=/',$val);
- if(!empty($kv))
- {
- $k = trim($kv[0]);
- $v = trim($kv[1]);
- if(!empty($k)) {
- $_COOKIE[$k] = $v;
- }
- }
- }
- }
- }
- function fcgi_setcookie($name, $value = null, $expire = null, $path = null, $domain = null, $secure = null, $httponly = null)
- {
- return http_header::instance()->setcookie($name,$value,$expire,$path,$domain,$secure,true,$httponly);
- }
- function fcgi_setrawcookie($name, $value = null, $expire = null, $path = null, $domain = null, $secure = null, $httponly = null)
- {
- return http_header::instance()->setcookie($name,$value,$expire,$path,$domain,$secure,false,$httponly);
- }
- function fcgi_header($string, $replace = true, $http_response_code = null)
- {
- http_header::instance()->header($string,$replace,$http_response_code);
- }
- function fcgi_header_remove($name)
- {
- }
|