-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
239 lines (198 loc) · 4.9 KB
/
index.php
File metadata and controls
239 lines (198 loc) · 4.9 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
//Air MVC framework
//Adrian Balcan
//start date: 19.07.2012
//last update: 15.07.2014
//version 13
//PHP version >= 5.2
define('DS', DIRECTORY_SEPARATOR);
define('ROOT',dirname(__FILE__));
define('APP_DIR', ROOT);
//Paths
define('APP_CONTROLLER', APP_DIR . DS . 'controllers');
define('APP_MODEL', APP_DIR . DS . 'models');
define('APP_VIEWS', APP_DIR . DS . 'views');
define('APP_CACHE', APP_DIR . DS . 'cache');
require_once('config.php');
//Autoload
function airAutoloader($className)
{
if (file_exists(APP_CONTROLLER . DS . strtolower($className) . '.php'))
require_once(APP_CONTROLLER . DS . strtolower($className) . '.php');
else if (file_exists(APP_MODEL . DS . strtolower($className) . '.php'))
require_once(APP_MODEL . DS . strtolower($className) . '.php');
else
throw new Exception('Class ' . $className . ' not found');
//trigger_error('Class ' . $className . ' not found', E_USER_ERROR);
}
spl_autoload_register('airAutoloader');
function display404(){
header('HTTP/1.0 404 Not Found');
echo '<h1>404 Not Found</h1>';
echo 'The page that you have requested could not be found.';
exit();
}
class loader{
public $resources = array();
//Load model
public function model($name){
$this->resources[$name] = new $name();
}
//Load controller
public function controller($name){
$this->resources[$name] = new $name();
}
//Load view
public function view($viewName, $viewParams = array(), $returnOutput = false){
if($returnOutput){
ob_start();
}
if(count($viewParams) > 0){
foreach($viewParams as $paramKey => $paramValue){
$$paramKey = $paramValue;
}
}
if(file_exists(APP_VIEWS . DS . $viewName . '.php')){
require(APP_VIEWS . DS . $viewName . '.php');
}else{
trigger_error($viewName . ' doesn\'t exists', E_USER_ERROR);
}
if($returnOutput){
$viewOutput = ob_get_clean();
ob_end_clean();
return $viewOutput;
}
}
public function __get($name){
if(isset($this->resources[$name])){
return $this->resources[$name];
}else{
trigger_error($name . ' doesn\'t exists', E_USER_ERROR);
}
}
}
//Main controller
class controller{
public $load;
public function __construct(){
$this->load = new loader();
header("Cache-Control: public");
header("Pragma: public");
}
public function __get($name){
if(isset($this->$name)){
return $this->$name;
}else{
return $this->load->$name;
}
}
}
//Main model
class model{
public $db;
public $load;
public function __construct(){
$this->load = new loader();
$this->db = new db();
}
public function __get($name){
if(isset($this->$name)){
return $this->$name;
}else{
return $this->load->$name;
}
}
}
class db{
public $conn;
public function __construct(){
$this->conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS);
mysqli_select_db($this->conn, DB_DATABASE);
}
public function query($str){
$res = mysqli_query($this->conn, $str) or trigger_error(mysql_error(), E_USER_ERROR);
return $res;
}
public function beginTransaction(){
mysqli_autocommit($this->conn,FALSE);
}
public function commit(){
mysqli_commit($this->conn);
}
public function getArray($query){
$res = $this->query($query);
$result = array();
$i = 0;
while($row = mysqli_fetch_assoc($res)){
$result[$i] = $row;
$i++;
}
return $result;
}
public function insertFromArray($table, $values){
$cols = array();
$vals = array();
foreach($values as $key => $val){
$cols[] = mysqli_real_escape_string($this->conn, $key);
$vals[] = mysqli_real_escape_string($this->conn, $val);
}
$colStr = '`' . implode('`, `', $cols) . '`';
$valStr = '"' . implode('", "', $vals) . '"';
$query = 'INSERT INTO `'.$table.'` (%s) VALUES (%s)';
$query = sprintf($query, $colStr, $valStr);
return $this->query($query);
}
}
//Router
//For CLI Call
if(!isset($_GET['url'])){
$_GET['url']=implode('/', array_slice($argv, 1));
}
if(!empty($_GET['url'])){
$routed = false;
foreach ($routes as $key => $route){
if(preg_match($route['url'], $_GET['url'], $matches)){
$inst = new $route['controller']();
//Added first group as controller param
if(isset($matches[1])){
$inst->$route['action']($matches[1]);
}else{
$inst->$route['action']();
}
$routed = true;
break;
}
}
$segment = explode('/', $_GET['url']);
if(!$routed){
try{
if(class_exists($segment[0]) && get_parent_class($segment[0]) == 'controller'){
$inst = new $segment[0]();
if(!empty($segment[1])){
if(method_exists($inst, $segment[1])){
if(!empty($segment[2])){
$inst->$segment[1]($segment[2]);
}else{
$inst->$segment[1]();
}
}else{
display404();
}
}else{
if(method_exists($inst, 'index')){
$inst->index();
}else{
display404();
}
}
}else{
display404();
}
}catch(Exception $e){
display404();
}
}
}else{
$inst = new $default['controller']();
$inst->$default['action']();
}