aboutsummaryrefslogtreecommitdiff
path: root/admin/Library/Call.php
blob: d80f1d43437acc277b1ea4afb78ef74a919d95f4 (plain) (blame)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php

// All EasyGiant code is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
// See COPYRIGHT.txt and LICENSE.txt.

if (!defined('EG')) die('Direct access not allowed!');


/* SANITIZE SUPERGLOBAL ARRAYS */
function sanitizeSuperGlobal()
{
	$_GET = stripslashesDeep($_GET);

	$_POST   = stripslashesDeep($_POST);

	$_COOKIE = stripslashesDeep($_COOKIE);

	$_SERVER = stripslashesDeep($_SERVER);
}



function checkPostLength()
{
	if (MAX_POST_LENGTH !== 0)
	{
		foreach ($_POST as $key => $value)
		{
			if (strlen($value) > MAX_POST_LENGTH) die('the length of some of the $_POST values is too large');
		}
	}
}

function checkRequestUriLength()
{
	if (MAX_REQUEST_URI_LENGTH !== 0)
	{
		if (strlen($_SERVER['REQUEST_URI']) > MAX_REQUEST_URI_LENGTH) die('the length of the REQUEST_URI is too large');
	}
}

function checkRegisterGlobals()
{
    if (ini_get('register_globals')) die('register globals is on: easyGiant works only with register globals off');
}

function callHook()
{

	if (MOD_REWRITE_MODULE === true)
	{
		$url = isset($_GET['url']) ? $_GET['url'] : DEFAULT_CONTROLLER . '/' . DEFAULT_ACTION;
	}
	else
	{
		$url = (strcmp(getQueryString(),"") !== 0) ? getQueryString() : DEFAULT_CONTROLLER . '/' . DEFAULT_ACTION;
	}
	
	//rewrite the URL
// 	if (Route::$rewrite === 'yes')
// 	{
// 		$url = rewrite($url);
// 	}

// 	echo $url;
	
	$urlArray = array();
	$urlArray = explode("/",$url);

	$controller = DEFAULT_CONTROLLER;
	$action = DEFAULT_ACTION;
	
	if (isset($urlArray[0]))
	{
		$controller = (strcmp($urlArray[0],'') !== 0) ? strtolower(trim($urlArray[0])) : DEFAULT_CONTROLLER;
	}

	array_shift($urlArray);

	if (isset($urlArray[0]))
	{
		$action = (strcmp($urlArray[0],'') !== 0) ? strtolower(trim($urlArray[0])) : DEFAULT_ACTION;
	}

	//set ERROR_CONTROLLER and ERROR_ACTION
	$errorController = ERROR_CONTROLLER !== false ? ERROR_CONTROLLER : DEFAULT_CONTROLLER;
	$errorAction = ERROR_ACTION !== false ? ERROR_ACTION : DEFAULT_ACTION;

	/*
	VERIFY THE ACTION NAME
	*/
	if (method_exists('Controller', $action) or !ctype_alnum($action) or (strcmp($action,'') === 0))
	{
		$controller = $errorController;
		$action = $errorAction;
		$urlArray = array();
	}

	/*
	VERIFY THE CONTROLLER NAME
	*/
	if (!ctype_alnum($controller) or (strcmp($controller,'') === 0))
	{
		$controller = $errorController;
		$action = $errorAction;
		$urlArray = array();
	}

	//check that the controller class belongs to the application/controllers folder
	//otherwise set the controller to the default controller
	if (!file_exists(ROOT.DS.APPLICATION_PATH.DS.'Controllers'.DS.ucwords($controller).'Controller.php'))
	{
		$controller = $errorController;
		$action = $errorAction;
		$urlArray = array();
	}

	//set the controller class to DEFAULT_CONTROLLER if it doesn't exists
	if (!class_exists(ucwords($controller).'Controller'))
	{
		$controller = $errorController;
		$action = $errorAction;
		$urlArray = array();
	}

	//set the action to DEFAULT_ACTION if it doesn't exists
	if (!method_exists(ucwords($controller).'Controller', $action))
	{
		$controller = $errorController;
		$action = $errorAction;
		$urlArray = array();
	}

	/*
		CHECK COUPLES CONTROLLER,ACTION
	*/
	if (!in_array('all',Route::$allowed))
	{
		$couple = "$controller,$action";
		if (!in_array($couple,Route::$allowed))
		{
			$controller = $errorController;
			$action = $errorAction;
			$urlArray = array();
		}
	}
	
	array_shift($urlArray);
	$queryString = $urlArray;
	//set the name of the application
	$application = $controller;
	$controller = ucwords($controller);
	$model = $controller;
	$controller .= 'Controller';
	$model .= 'Model';

	//include the file containing the set of actions to carry out before the initialization of the controller class
	Hooks::load(ROOT . DS . APPLICATION_PATH . DS . 'Hooks' . DS . 'BeforeInitialization.php');

	if (class_exists($controller))
	{
		$dispatch = new $controller($model,$application,$queryString);
		
		//pass the action to the controller object
		$dispatch->action = $action;
		$dispatch->currPage = $dispatch->baseUrl.'/'.$dispatch->controller.'/'.$dispatch->action;
		
		//require the file containing the set of actions to carry out after the initialization of the controller class
		Hooks::load(ROOT . DS . APPLICATION_PATH . DS . 'Hooks' . DS . 'AfterInitialization.php');

		$templateFlag= true;

		if (method_exists($controller, $action))
		{
			//pass the action to the theme object
			$dispatch->theme->action = $action;
			$dispatch->theme->currPage = $dispatch->baseUrl.'/'.$dispatch->controller.'/'.$dispatch->action;
			
			call_user_func_array(array($dispatch,$action),$queryString);
		}
		else
		{
			$templateFlag= false;
		}

		if ($templateFlag)
		{
			$dispatch->theme->render();
		}

	}
	else
	{
		echo "<h2>the '$controller' controller is not present!</h2>";
	}

}


// //rewrite the URL
// function rewrite($url)
// {
// 	foreach (Route::$map as $key => $address)
// 	{
// 		if (preg_match('/^'.$key.'/',$url))
// 		{
// 			return preg_replace('/^'.$key.'/',$address,$url);
// 		}
// 	}
// 	return ERROR_CONTROLLER.'/'.ERROR_ACTION;
// }

function getQueryString()
{

	if (strstr($_SERVER['REQUEST_URI'],'index.php/'))
	{
		return Params::$mbStringLoaded === true ? mb_substr(mb_strstr($_SERVER['REQUEST_URI'],'index.php/'),10) : substr(strstr($_SERVER['REQUEST_URI'],'index.php/'),10);
	}

	return '';
}

function __autoload($className)
{

	$backupName = $className;

	if (strstr($className,'_'))
	{
		$parts = explode('_',$className);
		$className = implode(DS,$parts);
	}

	if (file_exists(ROOT . DS . 'Library' . DS . $className . '.php'))
	{
		require_once(ROOT . DS . 'Library' . DS . $className . '.php'); 
	}
	else if (file_exists(ROOT . DS . APPLICATION_PATH . DS . 'Controllers' . DS . $backupName . '.php'))
	{
		require_once(ROOT . DS . APPLICATION_PATH . DS . 'Controllers' . DS . $backupName . '.php');
	}
	else if (file_exists(ROOT . DS . APPLICATION_PATH . DS . 'Models' . DS . $backupName . '.php'))
	{
		require_once(ROOT . DS . APPLICATION_PATH . DS . 'Models' . DS . $backupName . '.php');
	}
	else if (file_exists(ROOT . DS . APPLICATION_PATH . DS . 'Modules' . DS . $backupName . '.php'))
	{
		require_once(ROOT . DS . APPLICATION_PATH . DS . 'Modules' . DS . $backupName . '.php');
	}
	else if (file_exists(ROOT . DS . APPLICATION_PATH . DS . 'Strings' . DS . $className . '.php'))
	{
		require_once(ROOT . DS . APPLICATION_PATH . DS . 'Strings' . DS . $className . '.php');
	}
	
}

try {

	//check the length of the $_POST values
	checkPostLength();
	
	//check the length of the REQUEST_URI
	checkRequestUriLength();
	
	//connect to the database
	Factory_Db::getInstance(DATABASE_TYPE,array(HOST,USER,PWD,DB));
	
	//set htmlentities charset
	switch (DEFAULT_CHARSET)
	{
		case 'SJIS':
			Params::$htmlentititiesCharset = 'Shift_JIS';
			break;
	}

	$allowedCharsets = array('UTF-8','ISO-8859-1','EUC-JP','SJIS');
	if (!in_array(DEFAULT_CHARSET,$allowedCharsets)) die('charset not-allowed');

	//check if the mbstring extension is loaded
	if (extension_loaded('mbstring'))
	{
		//set the internal encoding
		mb_internal_encoding(DEFAULT_CHARSET);
		Params::$mbStringLoaded = true;
	}
	
	//load the files defined inside Config/Autoload.php
	foreach (Autoload::$files as $file)
	{
		$ext = strtolower(end(explode('.', $file)));
		$path = ROOT . DS . APPLICATION_PATH . DS . 'Include' . DS . $file;
		if (file_exists($path) and $ext === 'php')
		{
			require_once($path);
		}
	}

	//include the file containing the set of actions to carry out before the check of the super global array
	Hooks::load(ROOT . DS . APPLICATION_PATH . DS . 'Hooks' . DS . 'BeforeChecks.php');

	//sanitize super global arrays
	sanitizeSuperGlobal();

	//report errors
	ErrorReporting();

	//verify that register globals is not active
	checkRegisterGlobals();

	//call the main hook
	callHook();

	//disconnect to the database
	Factory_Db::disconnect(DATABASE_TYPE);

} catch (Exception $e) {

	echo '<div class="alert">Message: '.$e->getMessage().'</div>';

}