Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
 
4
/**
5
 * HTTP::Header
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * @category    HTTP
10
 * @package     HTTP_Header
11
 * @author      Wolfram Kriesing <wk@visionp.de>
12
 * @author      Davey Shafik <davey@php.net>
13
 * @author      Michael Wallner <mike@php.net>
14
 * @copyright   2003-2005 The Authors
15
 * @license     BSD, revised
16
 * @version     CVS: $Id: Header.php 304418 2010-10-15 13:18:02Z clockwerx $
17
 * @link        http://pear.php.net/package/HTTP_Header
18
 */
19
 
20
/**
21
 * Requires HTTP
22
 */
23
require_once 'HTTP.php';
24
 
25
/**#@+
26
 * Information Codes
27
 */
28
define('HTTP_HEADER_STATUS_100', '100 Continue');
29
define('HTTP_HEADER_STATUS_101', '101 Switching Protocols');
30
define('HTTP_HEADER_STATUS_102', '102 Processing');
31
define('HTTP_HEADER_STATUS_INFORMATIONAL',1);
32
/**#@-*/
33
 
34
/**#+
35
 * Success Codes
36
 */
37
define('HTTP_HEADER_STATUS_200', '200 OK');
38
define('HTTP_HEADER_STATUS_201', '201 Created');
39
define('HTTP_HEADER_STATUS_202', '202 Accepted');
40
define('HTTP_HEADER_STATUS_203', '203 Non-Authoritative Information');
41
define('HTTP_HEADER_STATUS_204', '204 No Content');
42
define('HTTP_HEADER_STATUS_205', '205 Reset Content');
43
define('HTTP_HEADER_STATUS_206', '206 Partial Content');
44
define('HTTP_HEADER_STATUS_207', '207 Multi-Status');
45
define('HTTP_HEADER_STATUS_SUCCESSFUL',2);
46
/**#@-*/
47
 
48
/**#@+
49
 * Redirection Codes
50
 */
51
define('HTTP_HEADER_STATUS_300', '300 Multiple Choices');
52
define('HTTP_HEADER_STATUS_301', '301 Moved Permanently');
53
define('HTTP_HEADER_STATUS_302', '302 Found');
54
define('HTTP_HEADER_STATUS_303', '303 See Other');
55
define('HTTP_HEADER_STATUS_304', '304 Not Modified');
56
define('HTTP_HEADER_STATUS_305', '305 Use Proxy');
57
define('HTTP_HEADER_STATUS_306', '306 (Unused)');
58
define('HTTP_HEADER_STATUS_307', '307 Temporary Redirect');
59
define('HTTP_HEADER_STATUS_REDIRECT',3);
60
/**#@-*/
61
 
62
/**#@+
63
 * Error Codes
64
 */
65
define('HTTP_HEADER_STATUS_400', '400 Bad Request');
66
define('HTTP_HEADER_STATUS_401', '401 Unauthorized');
67
define('HTTP_HEADER_STATUS_402', '402 Payment Granted');
68
define('HTTP_HEADER_STATUS_403', '403 Forbidden');
69
define('HTTP_HEADER_STATUS_404', '404 File Not Found');
70
define('HTTP_HEADER_STATUS_405', '405 Method Not Allowed');
71
define('HTTP_HEADER_STATUS_406', '406 Not Acceptable');
72
define('HTTP_HEADER_STATUS_407', '407 Proxy Authentication Required');
73
define('HTTP_HEADER_STATUS_408', '408 Request Time-out');
74
define('HTTP_HEADER_STATUS_409', '409 Conflict');
75
define('HTTP_HEADER_STATUS_410', '410 Gone');
76
define('HTTP_HEADER_STATUS_411', '411 Length Required');
77
define('HTTP_HEADER_STATUS_412', '412 Precondition Failed');
78
define('HTTP_HEADER_STATUS_413', '413 Request Entity Too Large');
79
define('HTTP_HEADER_STATUS_414', '414 Request-URI Too Large');
80
define('HTTP_HEADER_STATUS_415', '415 Unsupported Media Type');
81
define('HTTP_HEADER_STATUS_416', '416 Requested range not satisfiable');
82
define('HTTP_HEADER_STATUS_417', '417 Expectation Failed');
83
define('HTTP_HEADER_STATUS_422', '422 Unprocessable Entity');
84
define('HTTP_HEADER_STATUS_423', '423 Locked');
85
define('HTTP_HEADER_STATUS_424', '424 Failed Dependency');
86
define('HTTP_HEADER_STATUS_CLIENT_ERROR',4);
87
/**#@-*/
88
 
89
/**#@+
90
 * Server Errors
91
 */
92
define('HTTP_HEADER_STATUS_500', '500 Internal Server Error');
93
define('HTTP_HEADER_STATUS_501', '501 Not Implemented');
94
define('HTTP_HEADER_STATUS_502', '502 Bad Gateway');
95
define('HTTP_HEADER_STATUS_503', '503 Service Unavailable');
96
define('HTTP_HEADER_STATUS_504', '504 Gateway Time-out');
97
define('HTTP_HEADER_STATUS_505', '505 HTTP Version not supported');
98
define('HTTP_HEADER_STATUS_507', '507 Insufficient Storage');
99
define('HTTP_HEADER_STATUS_SERVER_ERROR',5);
100
/**#@-*/
101
 
102
/**
103
 * HTTP_Header
104
 *
105
 * @package     HTTP_Header
106
 * @category    HTTP
107
 * @access      public
108
 * @version     $Revision: 304418 $
109
 */
110
class HTTP_Header extends HTTP
111
{
112
    /**
113
     * Default Headers
114
     *
115
     * The values that are set as default, are the same as PHP sends by default.
116
     *
117
     * @var     array
118
     * @access  private
119
     */
120
    var $_headers = array(
121
        'content-type'  =>  'text/html',
122
        'pragma'        =>  'no-cache',
123
        'cache-control' =>  'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
124
    );
125
 
126
    /**
127
     * HTTP version
128
     *
129
     * @var     string
130
     * @access  private
131
     */
132
    var $_httpVersion = '1.0';
133
 
134
    /**
135
     * @var     bool
136
     * @access  public
137
     */
138
    var $prettify = false;
139
 
140
    /**
141
     * Constructor
142
     *
143
     * Sets HTTP version.
144
     *
145
     * @access  public
146
     * @return  object  HTTP_Header
147
     */
148
    function HTTP_Header()
149
    {
150
        if (isset($_SERVER['SERVER_PROTOCOL'])) {
151
            $this->setHttpVersion(substr($_SERVER['SERVER_PROTOCOL'], -3));
152
        }
153
    }
154
 
155
    /**
156
     * Set HTTP version
157
     *
158
     * @access  public
159
     * @return  bool    Returns true on success or false if version doesn't
160
     *                  match 1.0 or 1.1 (note: 1 will result in 1.0)
161
     * @param   mixed   $version HTTP version, either 1.0 or 1.1
162
     */
163
    function setHttpVersion($version)
164
    {
165
        $version = round((float) $version, 1);
166
        if ($version < 1.0 || $version > 1.1) {
167
            return false;
168
        }
169
        $this->_httpVersion = sprintf('%0.1f', $version);
170
        return true;
171
    }
172
 
173
    /**
174
     * Get HTTP version
175
     *
176
     * @access  public
177
     * @return  string
178
     */
179
    function getHttpVersion()
180
    {
181
        return $this->_httpVersion;
182
    }
183
 
184
    /**
185
     * Set Header
186
     *
187
     * The default value for the Last-Modified header will be current
188
     * date and atime if $value is omitted.
189
     *
190
     * @access  public
191
     * @return  bool    Returns true on success or false if $key was empty or
192
     *                  $value was not of an scalar type.
193
     * @param   string  $key The name of the header.
194
     * @param   string  $value The value of the header. (NULL to unset header)
195
     */
196
    function setHeader($key, $value = null)
197
    {
198
        if (empty($key) || (isset($value) && !is_scalar($value))) {
199
            return false;
200
        }
201
 
202
        $key = strToLower($key);
203
        if ($key == 'last-modified') {
204
            if (!isset($value)) {
205
                $value = HTTP::Date(time());
206
            } elseif (is_numeric($value)) {
207
                $value = HTTP::Date($value);
208
            }
209
        }
210
 
211
        if (isset($value)) {
212
            $this->_headers[$key] = $value;
213
        } else {
214
            unset($this->_headers[$key]);
215
        }
216
 
217
        return true;
218
    }
219
 
220
    /**
221
     * Get Header
222
     *
223
     * If $key is omitted, all stored headers will be returned.
224
     *
225
     * @access  public
226
     * @return  mixed   Returns string value of the requested header,
227
     *                  array values of all headers or false if header $key
228
     *                  is not set.
229
     * @param   string  $key    The name of the header to fetch.
230
     */
231
    function getHeader($key = null)
232
    {
233
        if (!isset($key)) {
234
            return $this->_headers;
235
        }
236
 
237
        $key = strToLower($key);
238
 
239
        if (!isset($this->_headers[$key])) {
240
            return false;
241
        }
242
 
243
        return $this->_headers[$key];
244
    }
245
 
246
    /**
247
     * Send Headers
248
     *
249
     * Send out the header that you set via setHeader().
250
     *
251
     * @access  public
252
     * @return  bool    Returns true on success or false if headers are already
253
     *                  sent.
254
     * @param   array   $keys Headers to (not) send, see $include.
255
     * @param   array   $include If true only $keys matching headers will be
256
     *                  sent, if false only header not matching $keys will be
257
     *                  sent.
258
     */
259
    function sendHeaders($keys = array(), $include = true)
260
    {
261
        if (headers_sent()) {
262
            return false;
263
        }
264
 
265
        if (count($keys)) {
266
            array_change_key_case($keys, CASE_LOWER);
267
            foreach ($this->_headers as $key => $value) {
268
                if ($include ? in_array($key, $keys) : !in_array($key, $keys)) {
269
                    header(($this->prettify ? uctitle($key) : $key) .': '. $value);
270
                }
271
            }
272
        } else {
273
            foreach ($this->_headers as $header => $value) {
274
                header(($this->prettify ? uctitle($header) : $header) .': '. $value);
275
            }
276
        }
277
        return true;
278
    }
279
 
280
    /**
281
     * Send Satus Code
282
     *
283
     * Send out the given HTTP-Status code. Use this for example when you
284
     * want to tell the client this page is cached, then you would call
285
     * sendStatusCode(304).
286
     *
287
     * @see HTTP_Header_Cache::exitIfCached()
288
     *
289
     * @access  public
290
     * @return  bool    Returns true on success or false if headers are already
291
     *                  sent.
292
     * @param   int     $code The status code to send, i.e. 404, 304, 200, etc.
293
     */
294
    function sendStatusCode($code)
295
    {
296
        if (headers_sent()) {
297
            return false;
298
        }
299
 
300
        if ($code == (int) $code && defined('HTTP_HEADER_STATUS_'. $code)) {
301
            $code = constant('HTTP_HEADER_STATUS_'. $code);
302
        }
303
 
304
        if (strncasecmp(PHP_SAPI, 'cgi', 3)) {
305
            header('HTTP/'. $this->_httpVersion .' '. $code);
306
        } else {
307
            header('Status: '. $code);
308
        }
309
        return true;
310
    }
311
 
312
    /**
313
     * Date to Timestamp
314
     *
315
     * Converts dates like
316
     *      Mon, 31 Mar 2003 15:26:34 GMT
317
     *      Tue, 15 Nov 1994 12:45:26 GMT
318
     * into a timestamp, strtotime() didn't do it in older versions.
319
     *
320
     * @deprecated      Use PHPs strtotime() instead.
321
     * @access  public
322
     * @return  mixed   Returns int unix timestamp or false if the date doesn't
323
     *                  seem to be a valid GMT date.
324
     * @param   string  $date The GMT date.
325
     */
326
    function dateToTimestamp($date)
327
    {
328
        static $months = array(
329
            null => 0, 'Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4,
330
            'May' => 5, 'Jun' => 6, 'Jul' => 7, 'Aug' => 8, 'Sep' => 9,
331
            'Oct' => 10, 'Nov' => 11, 'Dec' => 12
332
        );
333
 
334
        if (-1 < $timestamp = strToTime($date)) {
335
            return $timestamp;
336
        }
337
 
338
        if (!preg_match('~[^,]*,\s(\d+)\s(\w+)\s(\d+)\s(\d+):(\d+):(\d+).*~',
339
            $date, $m)) {
340
            return false;
341
        }
342
 
343
        // [0] => Mon, 31 Mar 2003 15:42:55 GMT
344
        // [1] => 31 [2] => Mar [3] => 2003 [4] => 15 [5] => 42 [6] => 55
345
        return mktime($m[4], $m[5], $m[6], $months[$m[2]], $m[1], $m[3]);
346
    }
347
 
348
    /**
349
     * Redirect
350
     *
351
     * This function redirects the client. This is done by issuing a Location
352
     * header and exiting.  Additionally to HTTP::redirect() you can also add
353
     * parameters to the url.
354
     *
355
     * If you dont need parameters to be added, simply use HTTP::redirect()
356
     * otherwise use HTTP_Header::redirect().
357
     *
358
     * @see     HTTP::redirect()
359
     * @author  Wolfram Kriesing <wk@visionp.de>
360
     * @access  public
361
     * @return  void
362
     * @param   string  $url The URL to redirect to, if none is given it
363
     *                  redirects to the current page.
364
     * @param   array   $param Array of query string parameters to add; usually
365
     *                  a set of key => value pairs; if an array entry consists
366
     *                  only of an value it is used as key and the respective
367
     *                  value is fetched from $GLOBALS[$value]
368
     * @param   bool    $session Whether the session name/id should be added
369
     */
370
    function redirect($url = null, $param = array(), $session = false)
371
    {
372
        if (!isset($url)) {
373
            $url = $_SERVER['PHP_SELF'];
374
        }
375
 
376
        $qs = array();
377
 
378
        if ($session) {
379
            $qs[] = session_name() .'='. session_id();
380
        }
381
 
382
        if (is_array($param) && count($param)) {
383
            if (count($param)) {
384
                foreach ($param as $key => $val) {
385
                    if (is_string($key)) {
386
                        $qs[] = urlencode($key) .'='. urlencode($val);
387
                    } else {
388
                        $qs[] = urlencode($val) .'='. urlencode(@$GLOBALS[$val]);
389
                    }
390
                }
391
            }
392
        }
393
 
394
        if ($qstr = implode('&', $qs)) {
395
            $purl = parse_url($url);
396
            $url .= (isset($purl['query']) ? '&' : '?') . $qstr;
397
        }
398
 
399
        parent::redirect($url);
400
    }
401
 
402
    /**#@+
403
     * @author  Davey Shafik <davey@php.net>
404
     * @param   int $http_code HTTP Code to check
405
     * @access  public
406
     */
407
 
408
    /**
409
     * Return HTTP Status Code Type
410
     *
411
     * @return int|false
412
     */
413
    function getStatusType($http_code)
414
    {
415
        if(is_int($http_code) && defined('HTTP_HEADER_STATUS_' .$http_code) || defined($http_code)) {
416
            $type = substr($http_code,0,1);
417
            switch ($type) {
418
                case HTTP_HEADER_STATUS_INFORMATIONAL:
419
                case HTTP_HEADER_STATUS_SUCCESSFUL:
420
                case HTTP_HEADER_STATUS_REDIRECT:
421
                case HTTP_HEADER_STATUS_CLIENT_ERROR:
422
                case HTTP_HEADER_STATUS_SERVER_ERROR:
423
                    return $type;
424
                    break;
425
                default:
426
                    return false;
427
                    break;
428
            }
429
        } else {
430
            return false;
431
        }
432
    }
433
 
434
    /**
435
     * Return Status Code Message
436
     *
437
     * @return string|false
438
     */
439
    function getStatusText($http_code)
440
    {
441
        if ($this->getStatusType($http_code)) {
442
            if (is_int($http_code) && defined('HTTP_HEADER_STATUS_' .$http_code)) {
443
                return substr(constant('HTTP_HEADER_STATUS_' .$http_code),4);
444
            } else {
445
                return substr($http_code,4);
446
            }
447
        } else {
448
            return false;
449
        }
450
    }
451
 
452
    /**
453
     * Checks if HTTP Status code is Information (1xx)
454
     *
455
     * @return boolean
456
     */
457
    function isInformational($http_code)
458
    {
459
        if ($status_type = $this->getStatusType($http_code)) {
460
            return $status_type{0} == HTTP_HEADER_STATUS_INFORMATIONAL;
461
        } else {
462
            return false;
463
        }
464
    }
465
 
466
    /**
467
     * Checks if HTTP Status code is Successful (2xx)
468
     *
469
     * @return boolean
470
     */
471
    function isSuccessful($http_code)
472
    {
473
        if ($status_type = $this->getStatusType($http_code)) {
474
            return $status_type{0} == HTTP_HEADER_STATUS_SUCCESSFUL;
475
        } else {
476
            return false;
477
        }
478
    }
479
 
480
    /**
481
     * Checks if HTTP Status code is a Redirect (3xx)
482
     *
483
     * @return boolean
484
     */
485
    function isRedirect($http_code)
486
    {
487
        if ($status_type = $this->getStatusType($http_code)) {
488
            return $status_type{0} == HTTP_HEADER_STATUS_REDIRECT;
489
        } else {
490
            return false;
491
        }
492
    }
493
 
494
    /**
495
     * Checks if HTTP Status code is a Client Error (4xx)
496
     *
497
     * @return boolean
498
     */
499
    function isClientError($http_code)
500
    {
501
        if ($status_type = $this->getStatusType($http_code)) {
502
            return $status_type{0} == HTTP_HEADER_STATUS_CLIENT_ERROR;
503
        } else {
504
            return false;
505
        }
506
    }
507
 
508
    /**
509
     * Checks if HTTP Status code is Server Error (5xx)
510
     *
511
     * @return boolean
512
     */
513
    function isServerError($http_code)
514
    {
515
        if ($status_type = $this->getStatusType($http_code)) {
516
            return $status_type{0} == HTTP_HEADER_STATUS_SERVER_ERROR;
517
        } else {
518
            return false;
519
        }
520
    }
521
 
522
    /**
523
     * Checks if HTTP Status code is Server OR Client Error (4xx or 5xx)
524
     *
525
     * @return boolean
526
     */
527
    function isError($http_code)
528
    {
529
        if ($status_type = $this->getStatusType($http_code)) {
530
            return (($status_type == HTTP_HEADER_STATUS_CLIENT_ERROR) || ($status_type == HTTP_HEADER_STATUS_SERVER_ERROR)) ? true : false;
531
        } else {
532
            return false;
533
        }
534
    }
535
    /**#@-*/
536
}
537
?>