00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010 class GyroHeaders {
00011
00012
00013
00014
00015
00016
00017
00018 private static $headers = array();
00019
00020
00021
00022
00023
00024
00025
00026
00027 public static function set($name, $value = false, $override = true) {
00028 if ($value === false) {
00029 $tmp = self::split($name);
00030 $name = array_shift($tmp);
00031 $value = array_shift($tmp);
00032 }
00033 if ($override || !self::is_set($name)) {
00034 self::$headers[$name] = $value;
00035 }
00036 }
00037
00038
00039
00040
00041 public static function remove($name) {
00042 self::$headers[$name] = '';
00043 }
00044
00045
00046
00047
00048
00049
00050
00051 public static function is_set($name) {
00052 $ret = false;
00053
00054 $name = strtolower($name);
00055 foreach(self::$headers as $hdr => $value) {
00056 $hdr = strtolower($hdr);
00057 if ($hdr == $name) {
00058 $ret = true;
00059 break;
00060 }
00061 }
00062 return $ret;
00063 }
00064
00065
00066
00067
00068
00069
00070
00071 public static function headers() {
00072 $ret = array();
00073 foreach(self::$headers as $name => $value) {
00074 if ($value) {
00075 $value = $name . ': ' . $value;
00076 }
00077 $ret[strtolower($name)] = $value;
00078 }
00079 return $ret;
00080 }
00081
00082
00083
00084
00085
00086
00087
00088 public static function values() {
00089 $ret = array();
00090 foreach(self::$headers as $name => $value) {
00091 $ret[strtolower($name)] = $value;
00092 }
00093 return $ret;
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 public static function restore($arr_headers) {
00106 $current = self::headers();
00107
00108 foreach(array_diff(array_keys($current), array_keys($arr_headers)) as $name) {
00109 self::remove($name);
00110 }
00111 foreach($arr_headers as $name => $value) {
00112 self::set($value, false, true);
00113 }
00114 }
00115
00116
00117
00118
00119
00120
00121 public static function sync() {
00122 foreach(headers_list() as $header) {
00123 self::set($header, false, false);
00124 }
00125 }
00126
00127
00128
00129
00130
00131
00132
00133 public static function send() {
00134 $file = '';
00135 $line = 0;
00136 if (headers_sent($file, $line)) {
00137 throw new Exception('GyroHeaders: Headers already sent in file "' . $file . '" on line ' . $line);
00138 }
00139 $has_remove = function_exists('header_remove');
00140 foreach(self::$headers as $name => $value) {
00141 if ($value) {
00142 header($name . ': ' . $value);
00143 }
00144 else if ($has_remove) {
00145
00146 header_remove($name);
00147 }
00148 }
00149 }
00150
00151
00152
00153
00154
00155
00156 public static function split($header) {
00157 $ret = explode(':', $header, 2);
00158 if (count($ret) == 1) {
00159 $ret = explode(' ', $header, 2);
00160 }
00161 if (count($ret) == 1) {
00162 $ret[] = '';
00163 }
00164 array_walk($ret, 'trim');
00165 return $ret;
00166 }
00167 }