-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
123 lines (113 loc) · 2.85 KB
/
index.php
File metadata and controls
123 lines (113 loc) · 2.85 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
<?php
require_once("config.php");
require_once($config["cmslib"]."router.php");
require_once($config["cmslib"]."request.php");
/*echo "<pre>";
print_r(Request::getInstance()->getval(""));
echo "</pre>";
exit;*/
$r = new Router();
//static content
$r->addRoute("GET","/favicon.ico",function() {//args[0] constain whole match
$req=Request::getInstance();
$f="";
if (file_exists($f)) {
header("Content-Type: ".make_content_type($f));
readfile($f);
}
else {
header($req->getval("srv.SERVER_PROTOCOL")." 404 Not Found", true, 404);
}
});
$r->addRoute("GET","/.*(css|js)",function() {
$req=Request::getInstance();
$f=".".$req->getval("uri");
if (file_exists($f)) {
header("Content-Type: ".make_content_type($f));
readfile($f);
}
else {
header($req->getval("srv.SERVER_PROTOCOL")." 404 Not Found", true, 404);
}
});
$r->addRoute("GET","(/icony/.*)",function() {
$req=Request::getInstance();
$f="..".$req->getval("uri");
if (file_exists($f)) {
header("Content-Type: ".make_content_type($f));
readfile($f);
}
else {
header($req->getval("srv.SERVER_PROTOCOL")." 404 Not Found", true, 404);
}
});
$r->addRoute("GET","(/res/.*)",function() {
$req=Request::getInstance();
$f=".".$req->getval("uri");
if (file_exists($f)) {
header("Content-Type: ".make_content_type($f));
readfile($f);
}
else {
header($req->getval("srv.SERVER_PROTOCOL")." 404 Not Found", true, 404);
}
});
$r->addRoute("GET","/ajax\\.js",function() {
$req=Request::getInstance();
$f="..".$req->getval("uri");
if (file_exists($f)) {
header("Content-Type: ".make_content_type($f));
readfile($f);
}
else {
header($req->getval("srv.SERVER_PROTOCOL")." 404 Not Found", true, 404);
}
});
//valid php scripts
$r->addRoute("","/api/(\\w+).*",function() {
global $config;
$req=Request::getInstance();
$args = func_get_args();
$func = strtolower($args[1]);
require_once("api/bridge.php");
$args = $req->getval("req");
$func = "api_".$func;
$func($args);
$c = ob_get_contents();
ob_end_clean();
if ($c) $req->addval("error",$c);
$r = $req->getval("state");
$c = $req->getval("error");
if ($c) $r->error=$c;
logstr("echo:".json_encode($r));
echo json_encode($r);
});
$r->addRoute("GET","/.*",function() {
global $config;
initdb();
$t = new TemplateEngine();
$t->load("bridge.tpl");
});
$r->addRoute("","",function() {
$req=Request::getInstance();
header($req->getval("srv.SERVER_PROTOCOL")." 404 Not Found", true, 404);
});
$r->route(Request::getInstance()->getval("method"), Request::getInstance()->getval("uri"));
function initdb() {
$db = DB::connectDefault();
$reqtabs=array(
"tables"=>"name varchar(255),expireOn date,state text",
);
$tabs=$db->tables();
if ($tabs===false) {
logstr($db->errmsg());
return false;
}
while (list($t,$v)=each($reqtabs)) {
if (in_array($t,$tabs)) continue;
logstr("create table $t $v");
$r=$db->tabcreate($t,$v);
}
$db->close();
}
?>