forked from dionyziz/Life
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
76 lines (76 loc) · 2.26 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
include 'header.php';
$methods = array(
'create' => 1,
'view' => 0,
'listing' => 0,
'update' => 1,
'delete' => 1
);
if ( isset( $_GET[ 'resource' ] ) ) {
$resource = $_GET[ 'resource' ];
}
else {
$resource = '';
}
if ( isset( $_GET[ 'method' ] ) ) {
$method = $_GET[ 'method' ];
}
else {
$method = '';
}
if ( !isset( $methods[ $method ] ) ) {
$method = 'view';
}
switch ( $_SERVER[ 'REQUEST_METHOD' ] ) {
case 'POST':
$http_vars = $_POST;
break;
case 'GET':
$http_vars = $_GET;
break;
default:
$http_vars = array();
break;
}
if ( $methods[ $method ] == 1 && $_SERVER[ 'REQUEST_METHOD' ] != 'POST' ) {
$method = $method . 'View';
// die( 'Method "' . $method . '" which is a POST method called using HTTP GET. (Rejected)' );
}
$resource = basename( $resource );
$filename = 'controllers/' . $resource . '.php';
if ( !file_exists( $filename ) ) {
// die( 'File not found: ' . $filename );
$resource = 'session';
$method = 'createView';
$filename = 'controllers/' . $resource . '.php';
}
include $filename;
$controllername = ucfirst( $resource ) . 'Controller';
$methodname = $method;
$reflection = new ReflectionMethod( $controllername, $methodname );
$parameters = $reflection->getParameters();
$arguments = array();
foreach ( $parameters as $parameter ) {
if ( isset( $http_vars[ $parameter->name ] ) ) {
$arguments[] = $http_vars[ $parameter->name ];
}
else {
$arguments[] = null;
}
}
try {
call_user_func_array( array( $controllername, $methodname ), $arguments );
}
catch ( NotImplemented $e ) {
die( 'An attempt was made to call a non-implemented function: ' . $e->getFunctionName() );
}
catch ( RedirectException $e ) {
global $settings;
$url = $settings[ 'url' ] . $e->getURL();
header( 'Location: ' . $url );
}
catch ( Exception $e ) {
die( $controllername . '::' . $methodname . ' call rejected: ' . $e->getMessage() );
}
?>