http_header.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/1/30
  6. * Time: 下午3:46
  7. */
  8. class http_header
  9. {
  10. const COOKIE_EXPIRES = "; expires=";
  11. const COOKIE_MAX_AGE = "; Max-Age=";
  12. const COOKIE_DOMAIN = "; domain=";
  13. const COOKIE_PATH = "; path=";
  14. const COOKIE_SECURE = "; secure";
  15. const COOKIE_HTTPONLY = "; HttpOnly";
  16. private static $stHeader = NULL;
  17. private $mHeader = NULL;
  18. private function __construct()
  19. {
  20. }
  21. public function start()
  22. {
  23. $this->mHeader = new SplDoublyLinkedList();
  24. }
  25. public function end()
  26. {
  27. $sheader = '';
  28. foreach($this->mHeader as $val)
  29. {
  30. $sheader .= $val . "\r\n";
  31. }
  32. $sheader .= "\r\n";
  33. return $sheader;
  34. }
  35. static public function instance()
  36. {
  37. if(self::$stHeader == NULL) {
  38. self::$stHeader = new http_header();
  39. }
  40. return self::$stHeader;
  41. }
  42. public function setcookie($name, $value = null, $expire = null, $path = null, $domain = null, $secure = null,$url_encode = true, $httponly = null)
  43. {
  44. if(empty($name)) {
  45. return false;
  46. } else if(strpbrk($name, "=,; \t\r\n\013\014") != false) {
  47. Log::record('Cookie names cannot contain any of the following \'=,; \\t\\r\\n\\013\\014\'');
  48. return false;
  49. }
  50. if(empty($value)) {
  51. $cookie = sprintf("Set-Cookie: %s=deleted; expires=%s; Max-Age=0", $name, gmdate("D, d-M-Y H:i:s T",1));
  52. }
  53. else
  54. {
  55. if(!$url_encode && strpbrk($value,"=,; \t\r\n\013\014") != false) {
  56. Log::record('Cookie valuse cannot contain any of the following \'=,; \\t\\r\\n\\013\\014\'');
  57. return false;
  58. }
  59. if($url_encode) {
  60. $cookie = sprintf("Set-Cookie: %s=%s", $name, !empty($value) ? urlencode($value) : "");
  61. } else {
  62. $cookie = sprintf("Set-Cookie: %s=%s", $name, !empty($value) ? $value : "");
  63. }
  64. if($expire > 0) {
  65. $tmp = self::COOKIE_EXPIRES . gmdate("D, d-M-Y H:i:s T",$expire);
  66. $cookie .= $tmp;
  67. }
  68. $tsdelta = sprintf("%d", $expire - time());
  69. $cookie .= self::COOKIE_MAX_AGE . $tsdelta;
  70. }
  71. if(!empty($path)) {
  72. $cookie .= self::COOKIE_PATH . $path;
  73. }
  74. if(!empty($domain)) {
  75. $cookie .= self::COOKIE_DOMAIN . $domain;
  76. }
  77. if(!empty($secure)) {
  78. $cookie .= self::COOKIE_SECURE . $secure;
  79. }
  80. if(!empty($httponly)) {
  81. $cookie .= self::COOKIE_HTTPONLY . $httponly;
  82. }
  83. $this->mHeader->push($cookie);
  84. return true;
  85. }
  86. private function replace($string)
  87. {
  88. $reg = '/^([^:]*): (.*)/i';
  89. $string = trim($string);
  90. if(preg_match($reg,$string,$m)) {
  91. $sname =strtolower($m[1]);
  92. }
  93. $index = 0;
  94. for($this->mHeader->rewind();$this->mHeader->valid();$this->mHeader->next())
  95. {
  96. $val = $this->mHeader->current();
  97. if(preg_match($reg,$val,$m))
  98. {
  99. $hname = strtolower($m[1]);
  100. if(strcmp($sname,$hname) == 0) {
  101. $this->mHeader->offsetUnset($index);
  102. break;
  103. }
  104. $index++;
  105. }
  106. }
  107. $this->mHeader->push($string);
  108. }
  109. public function header ($string, $replace = true, $http_response_code = null)
  110. {
  111. if(empty($string)) return;
  112. $datas = str_split($string);
  113. foreach($datas as $val) {
  114. if($val == '\r' || $val == '\n') {
  115. Log::record("Header may not contain more than a single header, new line detected",Log::ERR);
  116. return;
  117. }
  118. }
  119. if($replace) {
  120. $this->replace($string);
  121. } else {
  122. $this->mHeader->push($string);
  123. }
  124. }
  125. }
  126. function init_cookie($cookie)
  127. {
  128. $regxp = '/([^=]+=[^;]*)[;]?/i';
  129. $val = preg_match_all($regxp,$cookie,$match);
  130. foreach($_COOKIE as $key => $val) {
  131. unset($_COOKIE[$key]);
  132. }
  133. if($val == 2)
  134. {
  135. foreach($match[1] as $val)
  136. {
  137. $kv = preg_split('/=/',$val);
  138. if(!empty($kv))
  139. {
  140. $k = trim($kv[0]);
  141. $v = trim($kv[1]);
  142. if(!empty($k)) {
  143. $_COOKIE[$k] = $v;
  144. }
  145. }
  146. }
  147. }
  148. }
  149. function fcgi_setcookie($name, $value = null, $expire = null, $path = null, $domain = null, $secure = null, $httponly = null)
  150. {
  151. return http_header::instance()->setcookie($name,$value,$expire,$path,$domain,$secure,true,$httponly);
  152. }
  153. function fcgi_setrawcookie($name, $value = null, $expire = null, $path = null, $domain = null, $secure = null, $httponly = null)
  154. {
  155. return http_header::instance()->setcookie($name,$value,$expire,$path,$domain,$secure,false,$httponly);
  156. }
  157. function fcgi_header($string, $replace = true, $http_response_code = null)
  158. {
  159. http_header::instance()->header($string,$replace,$http_response_code);
  160. }
  161. function fcgi_header_remove($name)
  162. {
  163. }