14
14
* See the License for the specific language governing permissions and
15
15
* limitations under the License.
16
16
*/
17
+
17
18
namespace Docnet ;
18
19
19
20
use \Docnet \JAPI \Exceptions \Routing as RoutingException ;
20
21
use \Docnet \JAPI \Exceptions \Auth as AuthException ;
21
22
use \Docnet \JAPI \Exceptions \Maintenance as MaintenanceException ;
23
+ use \Docnet \JAPI \Exceptions \AccessDenied as AccessDeniedException ;
22
24
23
25
/**
24
26
* Front controller for our JSON APIs
@@ -35,35 +37,35 @@ class JAPI
35
37
/**
36
38
* @var JAPI\Config
37
39
*/
38
- private static $ obj_config = NULL ;
40
+ private static $ obj_config = null ;
39
41
40
42
/**
41
43
* @var JAPI\Router
42
44
*/
43
- private static $ obj_router = NULL ;
45
+ private static $ obj_router = null ;
44
46
45
47
/**
46
48
* @var JAPI\Logger
47
49
*/
48
- private $ obj_logger = NULL ;
50
+ private $ obj_logger = null ;
49
51
50
52
/**
51
53
* @var null|float
52
54
*/
53
- private static $ flt_startup = NULL ;
55
+ private static $ flt_startup = null ;
54
56
55
57
/**
56
58
* When creating a new JAPI, hook up the shutdown function and set Config
57
59
*
58
60
* @param null|JAPI\Config $obj_config
59
61
*/
60
- public function __construct ($ obj_config = NULL )
62
+ public function __construct ($ obj_config = null )
61
63
{
62
64
register_shutdown_function (array ($ this , 'timeToDie ' ));
63
- if ( NULL !== $ obj_config ) {
65
+ if ( null !== $ obj_config ) {
64
66
self ::$ obj_config = $ obj_config ;
65
67
}
66
- self ::$ flt_startup = (isset ($ _SERVER ['REQUEST_TIME_FLOAT ' ]) ? $ _SERVER ['REQUEST_TIME_FLOAT ' ] : microtime (TRUE ));
68
+ self ::$ flt_startup = (isset ($ _SERVER ['REQUEST_TIME_FLOAT ' ]) ? $ _SERVER ['REQUEST_TIME_FLOAT ' ] : microtime (true ));
67
69
}
68
70
69
71
/**
@@ -85,6 +87,9 @@ public function run()
85
87
} catch (AuthException $ obj_ex ) {
86
88
$ this ->jsonError ($ obj_ex , 401 );
87
89
90
+ } catch (AccessDeniedException $ obj_ex ) {
91
+ $ this ->jsonError ($ obj_ex , 403 );
92
+
88
93
} catch (\Exception $ obj_ex ) {
89
94
$ this ->jsonError ($ obj_ex );
90
95
}
@@ -107,26 +112,30 @@ public function timeToDie()
107
112
* Whatever went wrong, let 'em have it in JSON
108
113
*
109
114
* One day...
115
+ *
110
116
* @see http://www.php.net/manual/en/function.http-response-code.php
111
117
*
112
118
* @param string|\Exception $mix_message
113
119
* @param int $int_code
114
120
*/
115
- protected function jsonError ($ mix_message = NULL , $ int_code = 500 )
121
+ protected function jsonError ($ mix_message = null , $ int_code = 500 )
116
122
{
117
123
switch ($ int_code ) {
118
124
case 401 :
119
- header ($ _SERVER ["SERVER_PROTOCOL " ] . " 401 Unauthorized " , TRUE , 401 );
125
+ header ($ _SERVER ["SERVER_PROTOCOL " ] . " 401 Unauthorized " , true , 401 );
126
+ break ;
127
+ case 403 :
128
+ header ($ _SERVER ["SERVER_PROTOCOL " ] . " 403 Forbidden " , true , 401 );
120
129
break ;
121
130
case 404 :
122
- header ($ _SERVER ["SERVER_PROTOCOL " ] . " 404 Not Found " , TRUE , 404 );
131
+ header ($ _SERVER ["SERVER_PROTOCOL " ] . " 404 Not Found " , true , 404 );
123
132
break ;
124
133
case 503 :
125
- header ($ _SERVER ["SERVER_PROTOCOL " ] . " 503 Service Unavailable " , TRUE , 503 );
134
+ header ($ _SERVER ["SERVER_PROTOCOL " ] . " 503 Service Unavailable " , true , 503 );
126
135
break ;
127
136
case 500 :
128
137
default :
129
- header ($ _SERVER ["SERVER_PROTOCOL " ] . " 500 Internal Server Error " , TRUE , 500 );
138
+ header ($ _SERVER ["SERVER_PROTOCOL " ] . " 500 Internal Server Error " , true , 500 );
130
139
}
131
140
if ($ mix_message instanceof \Exception) {
132
141
$ str_log = get_class ($ mix_message ) . ': ' . $ mix_message ->getMessage ();
@@ -137,10 +146,12 @@ protected function jsonError($mix_message = NULL, $int_code = 500)
137
146
$ str_log = $ str_message = 'Unknown error ' ;
138
147
}
139
148
header ('Content-type: application/json ' );
140
- echo json_encode (array (
141
- 'response ' => (int )$ int_code ,
142
- 'msg ' => $ str_message
143
- ));
149
+ echo json_encode (
150
+ array (
151
+ 'response ' => (int )$ int_code ,
152
+ 'msg ' => $ str_message ,
153
+ )
154
+ );
144
155
$ this ->log (LOG_ERR , "[JAPI exiting with {$ int_code }] " . $ str_log );
145
156
exit ();
146
157
}
@@ -152,7 +163,7 @@ protected function jsonError($mix_message = NULL, $int_code = 500)
152
163
*/
153
164
public static function getRouter ()
154
165
{
155
- if (NULL === self ::$ obj_router ) {
166
+ if (null === self ::$ obj_router ) {
156
167
self ::$ obj_router = new JAPI \Router ();
157
168
}
158
169
return self ::$ obj_router ;
@@ -175,7 +186,7 @@ public function setRouter(JAPI\Interfaces\Router $obj_router)
175
186
*/
176
187
public static function getConfig ()
177
188
{
178
- if ( NULL === self ::$ obj_config ) {
189
+ if ( null === self ::$ obj_config ) {
179
190
self ::$ obj_config = new JAPI \Config ();
180
191
}
181
192
return self ::$ obj_config ;
@@ -189,7 +200,7 @@ public static function getConfig()
189
200
*/
190
201
public static function getDuration ($ int_dp = 4 )
191
202
{
192
- return round (microtime (TRUE ) - self ::$ flt_startup , $ int_dp );
203
+ return round (microtime (true ) - self ::$ flt_startup , $ int_dp );
193
204
}
194
205
195
206
/**
@@ -200,7 +211,7 @@ public static function getDuration($int_dp = 4)
200
211
*/
201
212
protected function log ($ int_level , $ str_message )
202
213
{
203
- if ( NULL === $ this ->obj_logger ) {
214
+ if ( null === $ this ->obj_logger ) {
204
215
$ this ->obj_logger = new JAPI \Logger ();
205
216
}
206
217
$ this ->obj_logger ->log ($ int_level , $ str_message );
0 commit comments