-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbdMVC.php
513 lines (509 loc) · 12.4 KB
/
dbdMVC.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<?php
/**
* dbdMVC.php :: dbdMVC Include File & Front Controller Class
*
* dbdMVC version 1.8.4
* Copyright (c) 2006-2009 by Don't Blink Design
* http://dbdmvc.com
*
* dbdMVC is a light weight model-view-controller framework.
*
* dbdMVC is released under the terms of the LGPL license
* http://www.gnu.org/copyleft/lesser.html
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package dbdMVC
* @version 1.8.4
* @author Don't Blink Design <info@dontblinkdesign.com>
* @copyright Copyright (c) 2006-2009 by Don't Blink Design
* @license http://www.gnu.org/copyleft/lesser.html
* @todo Create readme and update dbdMVC description
*/
/**
* Shorthand for DIRECTORY_SEPARATOR
*/
define("DBD_DS", DIRECTORY_SEPARATOR);
/**
* dbdMVC base directory
*/
if (!defined("DBD_MVC_DIR"))
define("DBD_MVC_DIR", dirname(__FILE__).DBD_DS);
/**
* Smarty package directory
*/
define("DBD_SMARTY_DIR", DBD_MVC_DIR."Smarty-2.6.18".DBD_DS."libs".DBD_DS);
/**
* Smarty add0n plug-in directory directory (used to avoid disrupting standard plug-in dir)
*/
define("DBD_SMARTY_PLUG_DIR", DBD_SMARTY_DIR.DBD_DS."plugins_dbd".DBD_DS);
/**
* xAjax package directory
*/
define("DBD_XAJAX_DIR", DBD_MVC_DIR."xajax_0.2.5".DBD_DS);
/**
* FirePHP core directory
*/
define("DBD_FIREPHP_DIR", DBD_MVC_DIR."FirePHPCore-0.3.1".DBD_DS."lib".DBD_DS);
/**
* dbdMVC plug-in directory
*/
define("DBD_PLUG_DIR", DBD_MVC_DIR."plugins".DBD_DS);
/**
* dbdMVC cache directory
*/
define("DBD_CACHE_DIR", DBD_APP_DIR."cache".DBD_DS);
/**
* Are we running in CLI mode?
*/
define("DBD_MVC_CLI", php_sapi_name() == "cli");
/**
* Turn on all debugging
*/
define("DBD_DEBUG_ALL", 255);
/**
* Turn on all debugging
*/
define("DBD_DEBUG_DB", 1);
define("DBD_DEBUG_HTML", 2);
define("DBD_DEBUG_JS", 4);
define("DBD_DEBUG_CSS", 8);
/**
* error_log function that handles complex types too
*/
if (!function_exists("dbdLog"))
{
require_once(DBD_FIREPHP_DIR."FirePHP.class.php");
function dbdLog($msg = "", $type = null, $dest = null)
{
$firephp = FirePHP::getInstance(true);
if (dbdMVC::PHPisExposed())
$firephp->log($msg, "dbdLog");
if (is_array($msg) || is_object($msg))
$msg = print_r($msg, 1);
if ($dest === null)
$dest = dbdMVC::getErrorLog();
if (file_exists($dest) && is_writeable($dest))
{
if ($type === null)
$type = 3;
$msg = date("[d-M-Y H:i:s] ").$msg."\n";
}
error_log($msg, $type, $dest);
}
}
/**
* After the dbdLoader is included, all further dbdMVC includes use dbdLoader::load() or __autoload()
*/
require_once("lib/dbdLoader.php");
/**
* Add all of the dbdMVC directories to dbdLoader, where there are also ini_set() to the php_include_path
*/
dbdLoader::addPath(DBD_MVC_DIR);
dbdLoader::addPath(DBD_MVC_DIR."lib");
dbdLoader::addPath(DBD_SMARTY_DIR);
dbdLoader::addPath(DBD_XAJAX_DIR);
dbdLoader::addPath(DBD_PLUG_DIR);
/**
* dbdMVC Front Controller Class
* @package dbdMVC
* @uses dbdDispatcher
* @uses dbdLoader
* @uses dbdRequest
* @uses dbdRouter
*/
class dbdMVC
{
/**
* Current version number
* <b>Note:</b> May not match file @version number
*/
const VERSION = "1.8.4";
/**
* #@+
* @access private
*/
/**
* Active instance of dbdMVC Singleton
* @staticvar object
*/
private static $instance = null;
/**
* Start time of dbdApp, from microtime()
* @staticvar float
*/
private static $start = null;
/**
* Allow phpinfo() via dbdInfo controller
* @staticvar boolean
*/
private static $expose_php_info = null;
/**
* Current debug mode.
* @var integer
*/
private $debug_mode = 0;
/**
* Current debug minify mode.
* 0 = no minify
* 1 = minify
* @var integer
*/
private $minify_mode = 0;
/**
* Name of dbdApp
* @var string
*/
private $app_name = null;
/**
* Path to dbdApp files (models, views, and controllers dirs)
* @var string
*/
private $app_dir = null;
/**
* Fallback/Default controller (if left null, the dbdDispatcher::FALLBACK_CONTROLLER will be used)
* @var string
*/
private $fallback_controller = null;
/**
* Error/Default controller (if left null, the dbdDispatcher::ERROR_CONTROLLER will be used)
* @var string
*/
private $error_controller = null;
/**
* Error log file (if left null, system default will be used)
* @var string
*/
private $error_log = null;
/**
* Instance of dbdRequest
* @var object
*/
private $request = null;
/**
* Instance of dbdRouter
* @var object
*/
private $router = null;
/**
* Instance of dbdDispatcher
* @var object
*/
private $dispatcher = null;
/**
* Constructer sets exception handler and the default app_name.
* <b>Note:</b> The cunstructer is private because this class can only
* be instantiated by dbdMVC::getInstance() to achieve a Singleton.
*/
private function __construct()
{
ob_start();
set_exception_handler(array("dbdMVC", "e"));
$this->app_name = "dbdMVC v".dbdMVC::VERSION;
}
/**
* Prepare for dispatch by setting up the dbdDispatcher
* and by adding the application specific paths to dbdLoader.
*/
private function init()
{
$this->request = new dbdRequest((DBD_MVC_CLI ? $this->genCLIURI() : null));
$this->router = new dbdRouter();
$this->dispatcher = new dbdDispatcher();
dbdLoader::addPath($this->app_dir);
foreach (dbdLoader::getControllerDirs() as $d)
{
if (substr($d, 0, 1) != DBD_DS)
$d = $this->app_dir.$d;
dbdLoader::addPath($d);
}
dbdLoader::addPath($this->app_dir.dbdLoader::getModelDir());
}
/**
* Dispatch dbdDispatcher.
*/
private function dispatch()
{
$this->dispatcher->dispatch();
}
/**
* Generate a URI from the Command Line Arguments.
* <b>Usage:</b> php index.php -c controller -a action --param=value
*/
private function genCLIURI()
{
$c = $a = null;
$p = array();
$argv = $_SERVER['argv'];
for ($i = 1; $i < $_SERVER['argc']; $i++)
{
if (strpos($argv[$i], '=') !== false && strpos($argv[$i], '--') !== false)
{
list($k, $v) = explode('=', $argv[$i], 2);
}
else
{
$k = $argv[$i];
$v = isset($argv[$i + 1]) ? $argv[++$i] : null;
}
switch ($k)
{
case '-c':
case '--controller':
$c = $v;
break;
case '-a':
case '--action':
$a = $v;
break;
default:
$p[ltrim($k, '-')] = urlencode($v);
break;
}
}
return dbdURI::create($c, $a, $p);
}
/**#@-*/
/**
* #@+
* dbdMVC interface methods
* @static
*/
/**
* Set application name property.
* @param string $app_name
*/
public static function setAppName($app_name)
{
self::getInstance()->app_name = $app_name;
}
/**
* Set fallback_controller property.
* @param string $fallback
*/
public static function setFallbackController($fallback)
{
self::getInstance()->fallback_controller = $fallback;
}
/**
* Set error_controller property.
* @param string $error
*/
public static function setErrorController($error)
{
self::getInstance()->error_controller = $error;
}
/**
* Set error_log property.
* @param string $error
*/
public static function setErrorLog($path)
{
self::getInstance()->error_log = $path;
}
/**
* Set application directory property.
* <b>Note:</b> Accepts paths with or without trailing slash.
* @param string $app_dir
*/
public static function setAppDir($app_dir)
{
if (substr($app_dir, -1) != DBD_DS)
$app_dir .= DBD_DS;
self::getInstance()->app_dir = $app_dir;
}
/**
* Set debug mode.
* @param integer $mode
*/
public static function setDebugMode($mode)
{
self::getInstance()->debug_mode = $mode;
}
/**
* Set debug minify mode.
* @param integer $mode
*/
public static function setDebugMinifyMode($mode)
{
self::getInstance()->minify_mode = $mode;
}
/**
* Get the active instance of dbdMVC or create one.
* @return object instance of dbdMVC
*/
public static function getInstance()
{
if (self::$instance === null)
self::$instance = new self();
return self::$instance;
}
/**
* Default ecpetion handler
* @param Exception $e
*/
public static function e(Exception $e)
{
dbdLog($e->__toString());
try
{
if (($c = self::getErrorController()) === null)
$c = dbdDispatcher::ERROR_CONTROLLER;
$uri = dbdURI::create($c, "default", array("code" => $e->getCode(), "msg" => urlencode($e->getMessage()), "trace" => urlencode($e->__toString())));
$that = self::getInstance();
$that->dispatcher = new dbdDispatcher(new dbdRouter(new dbdRequest($uri)));
$that->dispatch();
}
catch (dbdException $e2)
{
dbdLog($e2->__toString());
echo "<h1>".$e->getCode()." - ".$e->getMessage()."</h1>";
}
}
/**
* Run dbdMVC on the given dbdApp dir.
* This is the main method of dbdMVC.
* @param string $app_dir
* @param string $app_name
*/
public static function run($app_dir, $app_name = null, $fallback_controller = null, $error_controller = null, $error_log = null, $debug_mode = null)
{
self::$start = microtime(true);
$that = self::getInstance();
if ($app_name !== null)
self::setAppName($app_name);
if ($fallback_controller !== null)
self::setFallbackController($fallback_controller);
if ($error_controller !== null)
self::setErrorController($error_controller);
if ($error_log !== null)
self::setErrorLog($error_log);
if ($debug_mode !== null)
self::setDebugMode($debug_mode);
$that->setAppDir($app_dir);
$that->init();
$that->dispatch();
}
/**
* Get application name.
* @return string app_name
*/
public static function getAppName()
{
return self::getInstance()->app_name;
}
/**
* Get application directory.
* @return string app_dir
*/
public static function getAppDir()
{
return self::getInstance()->app_dir;
}
/**
* Get fallback controller name.
* @return string fallback_controller
*/
public static function getFallbackController()
{
return self::getInstance()->fallback_controller;
}
/**
* Get error controller name.
* @return string error_controller
*/
public static function getErrorController()
{
return self::getInstance()->error_controller;
}
/**
* Return the error_log file name.
* @return float
*/
public static function getErrorLog()
{
return self::getInstance()->error_log === null ? ini_get("error_log") : self::getInstance()->error_log;
}
/**
* Get the instance of dbdRequest.
* @return object dbdRequest
*/
public static function getRequest()
{
return self::getInstance()->request;
}
/**
* Get the instance of dbdRouter.
* @return object dbdRouter
*/
public static function getRouter()
{
return self::getInstance()->router;
}
/**
* Calculate execution time of the application and return.
* @return float
*/
public static function getExecutionTime()
{
return round(microtime(true) - self::$start, 4)." seconds";
}
/**
* Log execution time of the application to the error_log.
* @throws dbdException
*/
public static function logExecutionTime()
{
if (self::$start === null)
throw new dbdException("dbdMVC::run() must be called prior to calling dbdMVC::logExecutionTime()!");
error_log("execution time: ".self::getExecutionTime(), 0);
}
/**
* Check if debug mode is set.
* @return boolean
*/
public static function debugMode($mode)
{
return (self::getInstance()->debug_mode & $mode) === $mode ? true : false;
}
/**
* Check if debug minify mode is set.
* @return boolean
*/
public static function debugMinifyMode()
{
return self::getInstance()->minify_mode;
}
/**
* Allow phpinfo() display via dbdInfo.
* <b>WARNING:</b> Allowing system information to be view by the public
* can be a major security risk. This should be used with caution for development only!
* @see dbdInfo
*/
public static function exposePHPInfo()
{
self::$expose_php_info = true;
}
/**
* Check if phpinfo display is allowed.
* @return boolean
*/
public static function PHPisExposed()
{
return (self::$expose_php_info === true);
}
/**#@-*/
}
?>