1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace ICanBoogie;
13
14 15 16 17
18 class Exception extends \Exception
19 {
20 protected $code;
21 public $title = 'Exception';
22
23 public function __construct($message, array $params=[], $code=500, $previous=null)
24 {
25 static $codes = [
26
27 400 => 'Bad Request',
28 401 => 'Unauthorized',
29 402 => 'Payment Required',
30 403 => 'Forbidden',
31 404 => 'Not Found',
32 405 => 'Method Not Allowed',
33 406 => 'Not Acceptable',
34 407 => 'Proxy Authentication Required',
35 408 => 'Request Timeout',
36 409 => 'Conflict',
37 410 => 'Gone',
38 411 => 'Length Required',
39 412 => 'Precondition Failed',
40 413 => 'Request Entity Too Large',
41 414 => 'Request-URI Too Long',
42 415 => 'Unsupported Media Type',
43 416 => 'Requested Range Not Satisfiable',
44 417 => 'Expectation Failed',
45
46 500 => 'Internal Server Error',
47 501 => 'Not Implemented',
48 502 => 'Bad Gateway',
49 503 => 'Service Unavailable',
50 504 => 'Gateway Timeout',
51 505 => 'HTTP Version Not Supported'
52
53 ];
54
55 $this->code = $code;
56
57 if (is_array($code))
58 {
59 $this->code = key($code);
60 $this->title = array_shift($code);
61 }
62 else if (isset($codes[$code]))
63 {
64 $this->title = $codes[$code];
65 }
66
67
68
69
70
71 $message = \ICanBoogie\format($message, $params);
72
73 parent::__construct($message, $code, $previous);
74 }
75
76 77 78 79 80 81 82 83 84
85 public function __get($property)
86 {
87 switch ($property)
88 {
89 case 'message': return $this->getMessage();
90 case 'code': return $this->code;
91 }
92
93 throw new PropertyNotReadable([ $property, $this ]);
94 }
95
96 public function __toString()
97 {
98 if ($this->code && !headers_sent())
99 {
100 header('HTTP/1.0 ' . $this->code . ' ' . $this->title);
101 }
102
103 $message = Debug::format_alert($this);
104
105 Debug::report($message);
106
107 return $message;
108 }
109
110 public function getTitle()
111 {
112 return $this->code . ' ' . $this->title;
113 }
114
115 116 117
118 public function ()
119 {
120 header("HTTP/1.0 $this->code $this->title");
121 }
122 }
123
124 125 126 127 128 129
130 class SecurityException extends \Exception
131 {
132
133 }
134
135 136 137
138 class AuthenticationRequired extends SecurityException
139 {
140 public function __construct($message="The requested URL requires authentication.", $code=401, \Exception $previous=null)
141 {
142 parent::__construct($message, $code, $previous);
143 }
144 }
145
146 147 148 149 150 151
152 class AlreadyAuthenticated extends SecurityException
153 {
154 public function __construct($message="The user is already authenticated", $code=401, \Exception $previous=null)
155 {
156 parent::__construct($message, $code, $previous);
157 }
158 }
159
160 161 162
163 class PermissionRequired extends SecurityException
164 {
165 public function __construct($message="You don't have the required permission.", $code=401, \Exception $previous=null)
166 {
167 parent::__construct($message, $code, $previous);
168 }
169 }