-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.php
More file actions
152 lines (112 loc) · 3.51 KB
/
module.php
File metadata and controls
152 lines (112 loc) · 3.51 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
<?php
require_once($MODULES_ROOT."/phpModules/class.module.php");
require_once("phpMlib/phpAccessController/parser.class.php");
if(!isset($MODULES_ROOT)) $MODULES_ROOT = "";
class phpAccessControllerModule extends Module{
public $URL_DIR;
private $HTACCESS = false;
private $URL_PARSER = null;
public $ASSET_DIRS;
private $REGISTRY = array();
private $REGISTRY_ROOT = null;
function __construct($json){
parent::__construct($json);
$this->ASSET_DIRS = $json->ASSET_DIRS;
$this->REGISTRY_ROOT = $json->REGISTRY;
$this->HTACCESS = $json->HTACCESS == "true";
}
public function Load(){
__APPEND_LOG("CONTROLL: ".$this->LOADED);
if($this->LOADED) return 1;
parent::Load();
if($this->HTACCESS) $this->prepHtaccess();
$this->prepBuiltinFunctions();
$files = scandir($this->REGISTRY_ROOT);
foreach($files as $f){
if($f != "." && $f != ".."){
require_once($this->REGISTRY_ROOT.$f);
$register($this);
}
}
$this->LOADED = true;
}
// Registers a url and its handler to the registry
// Registry looks like:
// [ID: (REX, HANDLER)]
function register_handler($regex, $handler, $ID=null){
$pair = array("REX" => "/".$regex."/", "HANDLER"=>$handler);
if($ID)
$this->REGISTRY[$ID] = $pair;
else
array_push($this->REGISTRY, $pair);
$ID = count($this->REGISTRY);
__APPEND_LOG("RegisteredURL: ".$pair["REX"]);
}
function get_dir($handle){
return $this->ASSET_DIRS->$handle;
}
// Appends the predefined page handlers for the module
private function prepBuiltinFunctions(){
require("built_in_handlers.php");
$register($this);
}
private function prepHtaccess(){
if(file_exists(".htaccess")){
__APPEND_LOG(".httacess file found");
}else{
__APPEND_LOG("no .httacess file found... creating...");
$F = fopen(".htaccess", "w");
fwrite($F, "RewriteEngine on\n");
fwrite($F, "RewriteRule ^(.*)$ index.php?dir=$1 [QSA]\n");
fclose($F);
}
}
/*
parse url
exacutes the function connected in the registry
*/
public function create($create, $args=array()){
switch($create){
case "remote_render":
__APPEND_LOG("attempting to parse request: ".$args['url']);
$request = $this->URL_PARSER->PARESE_URL($args['url']);
$request['REQUEST']['TYPE'] = "REMOTE";
$this->PAGE_HANDLERS[$request['TARGET']]($request);
break;
case "parse":
__APPEND_LOG("attempting to parse url: ".$args['url']);
$request = $this->parse($args['url']);
$request['HANDLER']->handle($request);
}
}
// The new parser, not that new really
private function parse($URL){
__APPEND_LOG("Parsing URL: ".$URL);
foreach($this->REGISTRY as $k=>$v){
__APPEND_LOG($URL."->".$v['REX']);
if(preg_match_all($v["REX"], $URL, $matches, PREG_SET_ORDER, 0)){
__APPEND_LOG(print_r($matches, true));
$e = $v;
@$e["REQUEST"] = array("URL"=>$URL, "REFERER"=>$_SERVER['HTTP_REFERER'], "TYPE"=>"DIRECT");
$e["MATCHES"] = $matches;
return $e;
}
}
@$referer = $_SERVER['HTTP_REFERER'];
$cont = $this->REGISTRY["404"];
$cont["REQUEST"] = array("URL"=>$URL, "REFERER"=>$referer, "TYPE"=>"DIRECT");
return $cont;
}
function reverse($label, $request){
$this->PAGE_HANDLERS[$label]($request);
}
function __destruct(){
$store = memory_get_usage();
$this->PAGE_HANDLERS = null;
unset($this->PAGE_HANDLERS);
$this->URL_PARSER = null;
unset($this->URL_PARSER);
__APPEND_LOG($this->MODULEName." unloaded, memory freed: ".((memory_get_usage()-$store)/1024)."KB");
}
}
?>