-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
93 lines (77 loc) · 3.2 KB
/
index.php
File metadata and controls
93 lines (77 loc) · 3.2 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
session_start();
# Set constant ROOT to this directory
defined('ROOT') or define('ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);
# Set constant PUBLIC
defined('PUBLIC_DIR') or define('PUBLIC_DIR', ROOT . 'public' . DIRECTORY_SEPARATOR);
# Set constant MODELS
defined('MODELS') or define('MODELS', ROOT . 'app' . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR);
# Set constant VIEWS
defined('VIEWS') or define('VIEWS', ROOT . 'app' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR);
# Set constant CONTROLLERS
defined('CONTROLLERS') or define('CONTROLLERS', ROOT . 'app' . DIRECTORY_SEPARATOR . 'controllers' . DIRECTORY_SEPARATOR);
require 'Router.php';
$router = new Router(CONTROLLERS);
# Matches /
$router->addGet('', function() { echo 'Homepage!'; });
# /test gives 2 routes
$router->addGet('test', function() { echo 'Test, first definition!<br>'; });
$router->addGet('test', function() { echo 'Test, second definition!<br>'; });
# Matches /admin/... before
$router->addBefore('GET', '/admin/(.*)', function() use ($router) {
if (!empty($_SESSION['user_level']) && $_SESSION['user_level'] >= 2) {
# Logged in as admin
} else {
header('Location: ' . Router::buildURL('login'));
exit();
}
});
# /admin/deleteUser/1673 --> Delete user 1673!
$router->addGet('admin/deleteUser/(\d+)', function($id) { echo "Delete user $id!"; });
# /admin/renameUser/7/Swen=Test --> Rename user 7 from Test to Swen!
$router->add('GET', 'admin/renameUser/(\d+)/(\w+)=(\w+)', function($id, $to, $from) { echo "Rename user $id from $from to $to!"; });
# Matches /login | /logout, calls CONTROLLERS.user->login() or logout()
$router->add('GET', '/login', 'user@login');
$router->addGet('logout', 'user@logout');
$router->add('POST', '/loggedin', 'user@loggedin');
# Guess
$router->add404(function() { exit('404!'); });
# REST Test
$router->add('POST', 'api/showAllUsers', function() {
# Fake authentification
$auth = !empty($_SERVER['PHP_AUTH_USER']) && !empty($_SERVER['PHP_AUTH_PW']);
if ($auth) {
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
# Set up response
$users = [];
for ($i = 1; $i <= 10; $i++) {
$users[] = ['id' => $i, 'name' => 'Name' . $i];
}
exit(json_encode($users));
} else {
header('HTTP/1.0 403 Forbidden');
exit();
}
});
# Send all data back to the client
$router->add('POST', 'api/sendback', function() {
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
# Get posted data
$data = json_decode(file_get_contents('php://input'), true);
exit(json_encode(
array('response' => [
'send_data' => $data,
'auth' => [
'user' => !empty($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : 'unknown',
'pw' => !empty($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : 'unknown'
]
])
));
});
# Start the router
$numroutes = $router->start();
if ($numroutes > 1) {
echo "Number of routes: $numroutes";
}