-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.php
135 lines (113 loc) · 3.92 KB
/
app.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
abstract class Plugin {
private $app = null;
public function delegate(&$app) { $this->app = &$app; }
}
class Application {
private $currentPage;
private $errorPage;
private $httpVerb;
private $httpParams;
private $errStack;
// Application Arguments ///////////////////////////////////////////
// Get and set session value by key
public function get($key) { return $_SESSION[$key]; }
public function set($key, $value) { $_SESSION[$key] = $value; }
// No parameter => HTTP verb
// True => complete parameters
// Key => parameter[Key]
public function arg($key = null) {
if ($key == null) {
return $this->httpVerb;
} else {
if ($key == true) {
return $this->httpParams;
} else if (is_array($this->httpParameters) && array_key_exists($key, $this->httpParams)) {
return $this->httpParams[$key];
} else {
return null;
}
}
}
// Constructor /////////////////////////////////////////////////////
function __construct($landingPage = 'index.php', $errorPage = 'showErrors.php') {
$this->currentPage = end(explode('/', $_SERVER['PHP_SELF']));
$this->errorPage = $errorPage;
// Start session
session_start();
// Restart session if we're on the landing page
if ($this->currentPage == $landingPage) {
session_destroy();
session_start();
}
// Depickle any error data
if (isset($_SESSION['errStack'])) {
$this->errStack = unserialize($_SESSION['errStack']);
}
// Get HTTP verb and query data
$this->httpVerb = $_SERVER['REQUEST_METHOD'];
switch ($this->httpVerb) {
case 'GET':
$this->httpParams = $_GET;
break;
case 'POST':
$this->httpParams = $_POST;
break;
default:
parse_str(file_get_contents('php://input'), $this->httpParams);
break;
}
}
// Plugin Interface ////////////////////////////////////////////////
private $plugins = array();
public function registerPlugin($name, &$p) {
if ($p instanceof Plugin && !array_key_exists($name, $this->plugins)) {
$this->plugins[$name] = &$p;
$this->plugins[$name]->delegate($this);
} else {
if (array_key_exists($name, $this->plugins)) {
$this->log('\''.$name.'\' plugin has already been registered.');
} else {
$this->log('\''.$name.'\' is not a valid plugin.');
}
}
}
public function plugin($name) {
if (array_key_exists($name, $this->plugins)) {
return $this->plugins[$name];
}
}
// Error Logging ///////////////////////////////////////////////////
// Append to error stack, if we're not on the error page
public function log($desc, $fatal = true) {
if ($this->currentPage != $this->errorPage) {
$this->errStack[] = array('timestamp' => floor(microtime(true) * 1000),
'description' => $desc,
'fatal' => $fatal);
}
}
// Output error stack, if applicable
public function trace() {
if (is_array($this->errStack)) {
return array_reverse($this->errStack);
} else {
return false;
}
}
// Redirect to error page if there were any fatal errors
// This needs to be called at the end of the script header (i.e.,
// before output is generated, which is why it can't be in the class
// destructor).
public function complain() {
$wasFatal = false;
if ($this->currentPage != $this->errorPage && is_array($this->errStack)) {
foreach ($this->errStack as $e) $wasFatal = $wasFatal || $e['fatal'];
}
if ($wasFatal) {
$this->set('errStack', serialize($this->errStack));
@header('Location: '.$this->errorPage);
exit;
}
}
}
?>