]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/servererroraction.php
ErrorAction to autodiscoverable file.
[quix0rs-gnu-social.git] / lib / servererroraction.php
1 <?php
2
3 /**
4  * Server error action.
5  *
6  * PHP version 5
7  *
8  * @category Action
9  * @package  StatusNet
10  * @author   Evan Prodromou <evan@status.net>
11  * @author   Zach Copley <zach@status.net>
12  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
13  * @link     http://status.net/
14  *
15  * StatusNet - the distributed open-source microblogging tool
16  * Copyright (C) 2008, 2009, StatusNet, Inc.
17  *
18  * This program is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU Affero General Public License as published by
20  * the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU Affero General Public License for more details.
27  *
28  * You should have received a copy of the GNU Affero General Public License
29  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30  */
31
32 if (!defined('GNUSOCIAL')) { exit(1); }
33
34 /**
35  * Class for displaying HTTP server errors
36  *
37  * Note: The older util.php class simply printed a string, but the spec
38  * says that 500 errors should be treated similarly to 400 errors, and
39  * it's easier to give an HTML response.  Maybe we can customize these
40  * to display some funny animal cartoons.  If not, we can probably role
41  * these classes up into a single class.
42  *
43  * See: http://tools.ietf.org/html/rfc2616#section-10
44  *
45  * @category Action
46  * @package  StatusNet
47  * @author   Zach Copley <zach@status.net>
48  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
49  * @link     http://status.net/
50  */
51
52 class ServerErrorAction extends ErrorAction
53 {
54     static $status = array(500 => 'Internal Server Error',
55                            501 => 'Not Implemented',
56                            502 => 'Bad Gateway',
57                            503 => 'Service Unavailable',
58                            504 => 'Gateway Timeout',
59                            505 => 'HTTP Version Not Supported');
60
61     function __construct($message='Error', $code=500, $ex=null)
62     {
63         parent::__construct($message, $code);
64
65         $this->default = 500;
66
67         // Server errors must be logged.
68         $log = "ServerErrorAction: $code $message";
69         if ($ex) {
70             $log .= "\n" . $ex->getTraceAsString();
71         }
72         common_log(LOG_ERR, $log);
73     }
74
75     // XXX: Should these error actions even be invokable via URI?
76
77     protected function handle()
78     {
79         parent::handle();
80
81         $this->code = $this->trimmed('code');
82
83         if (!$this->code || $code < 500 || $code > 599) {
84             $this->code = $this->default;
85         }
86
87         $this->message = $this->trimmed('message');
88
89         if (!$this->message) {
90             $this->message = "Server Error $this->code";
91         }
92
93         $this->showPage();
94     }
95
96     /**
97      *  To specify additional HTTP headers for the action
98      *
99      *  @return void
100      */
101     function extraHeaders()
102     {
103         $status_string = @self::$status[$this->code];
104         header('HTTP/1.1 '.$this->code.' '.$status_string);
105     }
106
107     /**
108      * Page title.
109      *
110      * @return page title
111      */
112
113     function title()
114     {
115         return @self::$status[$this->code];
116     }
117
118 }