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) { }