00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009 class Common {
00010
00011
00012
00013
00014
00015 public static function is_cgi() {
00016 return ( substr(php_sapi_name(), 0, 3) == 'cgi' );
00017 }
00018
00019
00020
00021
00022 public static function send_status_code($iStatusCode) {
00023 if (headers_sent()) {
00024 return;
00025 }
00026 $arr = array(
00027 100 => 'Continue',
00028 101 => 'Switching Protocols',
00029 200 => 'OK',
00030 201 => 'Created',
00031 202 => 'Accepted',
00032 203 => 'Non-Authoritative Information',
00033 204 => 'No Content',
00034 205 => 'Reset Content',
00035 206 => 'Partial Content',
00036 300 => 'Multiple Choices',
00037 301 => 'Moved Permanently',
00038 302 => 'Found',
00039 303 => 'See Other',
00040 304 => 'Not Modified',
00041 305 => 'Use Proxy',
00042 306 => '[Unused]',
00043 307 => 'Temporary Redirect',
00044 400 => 'Bad Request',
00045 401 => 'Unauthorized',
00046 402 => 'Payment Required',
00047 403 => 'Forbidden',
00048 404 => 'Not Found',
00049 405 => 'Method Not Allowed',
00050 406 => 'Not Acceptable',
00051 407 => 'Proxy Authentication Required',
00052 408 => 'Request Timeout',
00053 409 => 'Conflict',
00054 410 => 'Gone',
00055 411 => 'Length Required',
00056 412 => 'Precondition Failed',
00057 413 => 'Request Entity Too Large',
00058 414 => 'Request-URI Too Long',
00059 415 => 'Unsupported Media Type',
00060 416 => 'Requested Range Not Satisfiable',
00061 417 => 'Expectation Failed',
00062 500 => 'Internal Server Error',
00063 501 => 'Not Implemented',
00064 502 => 'Bad Gateway',
00065 503 => 'Service Unavailable',
00066 504 => 'Gateway Timeout',
00067 505 => 'HTTP Version Not Supported'
00068 );
00069 if (isset($arr[$iStatusCode])) {
00070 $text = $iStatusCode . ' ' . $arr[$iStatusCode];
00071 if ( self::is_cgi() ) {
00072 header('Status: ' . $text);
00073 }
00074 else {
00075 header('HTTP/1.x ' . $text);
00076 }
00077 }
00078 if ($iStatusCode >= 500 && $iStatusCode < 600) {
00079 header('Retry-After: 120');
00080 }
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 public static function header($name, $value = false, $override = true) {
00093 GyroHeaders::set($name, $value, $override);
00094 }
00095
00096
00097
00098
00099
00100
00101 public static function header_remove($name) {
00102 GyroHeaders::remove($name);
00103 }
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 public static function split_header($header) {
00114 return GyroHeaders::split($header);
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 public static function is_header_sent($name) {
00127 return GyroHeaders::is_set($name);
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 public static function get_headers() {
00140 return GyroHeaders::headers();
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 public static function header_restore($arr_headers) {
00153 GyroHeaders::restore($arr_headers);
00154 }
00155
00156
00157
00158
00159 public static function preprocess_input() {
00160
00161 if ( function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc() ) {
00162
00163 $_REQUEST = self::transcribe($_REQUEST);
00164 $_GET = self::transcribe($_GET);
00165 $_POST = self::transcribe($_POST);
00166 $_COOKIE = self::transcribe($_COOKIE);
00167 }
00168 }
00169
00170
00171 private static function transcribe($aList, $aIsTopLevel = true) {
00172 $gpcList = array();
00173 foreach ($aList as $key => $value) {
00174 if (is_array($value)) {
00175 $decodedKey = (!$aIsTopLevel) ? stripslashes($key) : $key;
00176 $decodedValue = self::transcribe($value, false);
00177 } else {
00178 $decodedKey = stripslashes($key);
00179 $decodedValue = stripslashes($value);
00180 }
00181 $gpcList[$decodedKey] = $decodedValue;
00182 }
00183 return $gpcList;
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193 public static function constant($name, $default = false) {
00194 return (defined($name)) ? constant($name) : $default;
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204 public static function flag_is_set($value, $flag) {
00205 if ($flag == 0) {
00206 return ($value == 0);
00207 }
00208 return (($value & $flag) == $flag);
00209 }
00210
00211
00212
00213
00214
00215
00216 public static function check_not_modified($date) {
00217 if ($date) {
00218
00219 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
00220
00221 $modifiedSince = explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
00222
00223
00224 $modifiedSince = strtotime($modifiedSince[0]);
00225 }
00226 else {
00227
00228 $modifiedSince = 0;
00229 }
00230
00231
00232 if ($date <= $modifiedSince) {
00233
00234
00235 Common::send_status_code(304);
00236 exit;
00237 }
00238 }
00239
00240 return false;
00241 }
00242
00243
00244
00245
00246
00247 public static function check_if_none_match($etag) {
00248
00249 $match_tag = Arr::get_item($_SERVER, 'HTTP_IF_NONE_MATCH', '');
00250 if ($match_tag && $match_tag == $etag) {
00251
00252
00253 Common::send_status_code(304);
00254 exit;
00255 }
00256
00257 return false;
00258 }
00259
00260 public static function is_google() {
00261 if (isset($_SERVER["HTTP_USER_AGENT"]))
00262 return (strpos($_SERVER["HTTP_USER_AGENT"], "Googlebot") !== false);
00263 else
00264 return false;
00265 }
00266
00267
00268
00269
00270
00271
00272 public static function create_token($salt = false) {
00273 return sha1(uniqid($salt ? $salt : mt_rand(), true));
00274 }
00275
00276
00277
00278
00279
00280
00281 public static function get_temp_dir() {
00282 $ret = '';
00283 if (function_exists('sys_get_temp_dir')) {
00284 $ret = sys_get_temp_dir();
00285 }
00286
00287 if (empty($ret)) {
00288 foreach(array('TMP','TEMP','TMPDIR') as $env) {
00289 $ret = getenc($env);
00290 if ($ret) {
00291 break;
00292 }
00293 }
00294 }
00295
00296 if (empty($ret)) {
00297 $ret = Config::get_value(Config::TEMP_DIR);
00298 }
00299
00300 if ($ret) {
00301 $ret = rtrim(realpath($ret), '/') . '/';
00302 }
00303
00304 return $ret;
00305 }
00306
00307 public static function create_temp_file($content) {
00308 $ret = false;
00309 $file = tempnam(self::get_temp_dir(), 'gyro');
00310 if ($file !== false) {
00311 if (file_put_contents($file, $content) !== false) {;
00312 $ret = $file;
00313 }
00314 }
00315 return $ret;
00316 }
00317 }