-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.php
517 lines (501 loc) · 15.4 KB
/
core.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
514
515
516
517
<?php
/*
* Simple Core 2.1.0
* Copyright(c) 2004-2008, Simple Site Solutions Pty. Ltd.
*
* http://www.s3core.com/SCPL
*/
//TODO: Entire framework does not work in PHP version 3.1.0 > WHY?!
error_reporting(E_ALL);
#OBJECT DEFINITIONS
@define('CORE','CORE');
#Globals
global ${CORE},$_OVERRIDE;
//Special Global Functions
function ifSetOr(&$theVar,$defaultTo=null)
{
if (!isset($theVar))
{
$theVar=$defaultTo;
}
return true;
}
function ifIsFileInclude($file='')
{
if ($return=is_file($file))
{
include($file);
}
return $return;
}
function ifIsFileIncludeOnce($file='')
{
if ($return=is_file($file))
{
include_once($file);
}
return $return;
}
function ifIsFileRequire($file='')
{
if ($return=is_file($file))
{
require($file);
}
return $return;
}
function ifIsFileRequireOnce($file='')
{
if ($return=is_file($file))
{
require_once($file);
}
return $return;
}
/**
* THE SIMPLE CORE ;)
*
* This is the heart of the framework. It handles the framework's
* flow and processes all core logic. It uses various handlers,
* also known as core classes, to deal with framework specific
* logic and processing.
*
* Although it uses it's various handlers, the core is responsible
* for the execution of such handlers and holds the logic to
* efficently get the job done.
*
* @final
* @author Timothy Chandler
* @version 2.3.0
* @copyright Simple Site Solutions 06/12/2007
*/
final class core
{
//Public Attributes
public $version ='2.3.2';
/**
* @var config - The core configuration (usually config.xml)
*/
public $config =false;
public $debug =false;
public $component =false;
public $application =false;
public $global =false;
public $corelogger =false;
public $coreError =false;
public $server =array();
public $defaultApplication =null;
public $my =null;
public $unitTesting =null;
//URI STUFF
public $URI ='';
public $nodes =array();
public $basePath ='';
public $fullPath ='';
public function initiate()
{
global $_OVERRIDE;
//Set core headers.
@header('X-Powered-By: PHP/'.@phpversion().' & Simple Core/'.$this->version);
@header('X-Simple-Core: Version '.$this->version);
// if (!(version_compare(PHP_VERSION,'5.2.11','>=') && version_compare(PHP_VERSION,'5.3','<')))
// {
// throw new Exception('Incompatible version of PHP detected. You are running version: '.PHP_VERSION.'. Please use PHP 5.2.11 or higher. PHP 5.3 is currently unsupported.');
// }
if (!$this->obtainServerEnvironment())
{
throw new Exception('Core was unable to obtain server environment.');
}
/* LOAD CONFIG
* Check if a config directive was passed in.
* If it wasn't, we'll look for it.
*/
include_once(dirname(__FILE__)._.'system'._.'core'._.'component'._.'config.php');
if (!isset($_OVERRIDE['config']))
{
if (@is_file(dirname(__FILE__)._.'config.xml'))
{
// $this->config=simplexml_load_file(dirname(__FILE__)._.'config.xml');
$this->config=new config(dirname(__FILE__)._.'config.xml');
}
else
{
throw new Exception('Failed to locate config file.',0);
}
}
else
{
if (@is_file($_OVERRIDE['config']))
{
// $this->config=simplexml_load_file($_OVERRIDE['config']);
$this->config=new config($_OVERRIDE['config']);
}
else
{
throw new Exception('Failed to locate config file in config override mode.',0);
}
}
if (!isset($this->config->defaultApplication))
{
throw new Exception('Config is either empty or missing!');
}
else
{
//Parse the configuration paths.
$this->parseConfigPaths();
//Setup the core's 'my' property.
$this->my=new stdClass;
$this->my->name ='core';
$this->my->dir =(string)$this->config->path->system;
$this->my->branchDir =$this->my->dir;
$this->my->includeDir =realpath($this->my->dir.'core')._;
$this->my->abstractDir =realpath($this->my->includeDir.'abstract')._;
$this->my->componentDir =realpath($this->my->includeDir.'component')._;
//Load the object container and overloader.
include_once($this->config->path->system.'objectContainer.php');
include_once($this->config->path->system.'overloader.php');
//Initiate object containers.
$this->component =new objectContainer($this,'component');
$this->application =new objectContainer($this,'application');
//Load all the core abstract classes.
foreach (new DirectoryIterator($this->my->abstractDir) as $iteration)
{
if ($iteration->isFile())
{
include_once($iteration->getPath()._.$iteration->getFilename());
}
}
//Load all the core component classes.
foreach (new DirectoryIterator($this->my->componentDir) as $iteration)
{
if ($iteration->isFile())
{
include_once($iteration->getPath()._.$iteration->getFilename());
}
}
//Handle loading the debugger.
if (isset($this->config->debug) && (bool)(int)$this->config->debug)
{
$this->debug=new core_debug;
error_reporting(E_ALL);
}
else
{
error_reporting(0);
}
//Everything else...
//Taint Mode
if ((bool)(int)$this->config->taintmode->active)
{
$this->global=new taint((int)$this->config->taintmode->level);
}
else
{
$this->global=new notaint;
}
//Logging
if ((bool)$this->config->logging['enabled'] && (bool)$this->config->logging->core)
{
$this->corelogger=new core_logger('core');
$this->corelogger->info('Logger Enabled');
}
else
{
$this->corelogger=new core_logger_dummy;
}
//Prepare the URI vars.
$this->prepareURI();
//DATABASE
if (isset($this->config->component->database->connection))
{
$this->corelogger->info('Database connection(s) found. Establishing connection(s).');
//Proceed with initiation.
if (!$this->component->database->setup($this->config->component->database->connection))
{
$this->exception('Database setup failed.',component_database::EXCEPTION_NAME);
}
// unset($config);
}
//SESSION
if (isset($this->config->component->session->active) && (bool)(int)$this->config->component->session->active)
{
$this->corelogger->info('Database sessions enabled. Establishing/resuming session.');
if (!isset($this->config->component->database->connection))
{
$this->exception('Unable to use session component. The session component depends on an'
.' active "core" database connection but there is no "core" connection defined in the config.','Session Handler Exception');
}
else
{
$this->component->session->start();
}
}
else
{
$this->corelogger->info('Database sessions NOT enabled. Using vanilla PHP sessions.');
session_start();
}
//PAGE
if (isset($this->config->component->page->captureBuffer) && (bool)(int)$this->config->component->page->captureBuffer)
{
$this->corelogger->info('Page output handling enabled. Taking over output buffering.');
if (!$this->component->page->startPageCapture())
{
$this->exception('Unable to start page capturing.',component_page::EXCEPTION_NAME);
}
}
//APPLICATION
if (!isset($this->config->defaultApplication))
{
$this->exception('Unable to load any applications. A default application was not specified.'
.' You must specify <defaultApplication></defaultApplication> in the core'
,' config.xml file.','Core Exception');
}
elseif (!is_dir($this->config->path->applications.$this->config->defaultApplication._))
{
$this->exception('Unable to load default application. The application doesn\'t exist.'
.' The default applicaiton is "'.$this->config->defaultApplication.'".','Core Exception');
}
elseif (!is_file($this->config->path->applications
.$this->config->defaultApplication._.$this->config->defaultApplication.'.php'))
{
$this->exception('Unable to load default application. The application core doesn\'t exist.'
.' The default applicaiton is "'.$this->config->defaultApplication.'".','Core Exception');
}
else
{
$this->corelogger->info('Default application found. Running application -> "'.$this->config->defaultApplication.'".');
$this->defaultApplication=$this->application->{$this->config->defaultApplication}->run();
}
}
}
public function applicationExists($application=null)
{
$return=false;
if (is_dir($this->config->path->applications.$application._)
&& is_file($this->config->path->applications
.$application._.$application.'.php'))
{
$return=true;
}
return $return;
}
public function exception($exceptionMessage=null,$exceptionCode=null)
{
$this->corelogger->info('Caught Exception. Details follow...');
$this->corelogger->error($exceptionMessage,'exception');
if ($this->debug)
{
new core_exception($this,$exceptionMessage);
}
exit();
}
public function shutdown()
{
if (isset($this->component->database))
{
$this->component->database->disconnect();
}
/** [PAGE OUTPUT] **/
if (isset($this->component->page))
{
//Stop the page captureing.
$this->component->page->stopPageCapture();
//If gzip compression is turned on in the config file, turn it on for the page handler.
#die(var_dump($this->config->general->gzip));
if ($this->config->component->page->gzip)$this->component->page->GZIPon();
//Output the caputred and compressed page.
print $this->component->page->outputPageCapture($this->config->component->page->gzipLevel);
}
/** [/PAGE OUTPUT] **/
if (!$this->unitTesting)
{
$this->corelogger->info('Tidy Core Shutdown.');
exit();
}
else
{
$this->corelogger->info('Core isn\'t fully shutting down because unit testing is in progress.');
}
}
private function prepareURI()
{
if (is_null($this->global->get('URI')))
{
//Catch the URI.
list($this->URI)=explode('?',$this->global->server('REQUEST_URI'),2);
$this->URI=preg_replace('@'.addslashes($this->config->path->publicroot).'@i','/',$this->URI);
$this->URI=str_replace('index.php','',$this->URI);
//Create nodes from the caught URI.
$this->nodes=explode('/',$this->URI);
if (!end($this->nodes))array_pop($this->nodes);
if (!reset($this->nodes))array_shift($this->nodes);
//If we have no nodes (root address), specify node 0 as empty.
if (!isset($this->nodes[0]))$this->nodes[0]='';
//Create the base path.
$this->basePath=$this->global->server('HTTP_HOST').$this->config->path->publicroot;
//Construct the full path from the base path.
$this->fullPath=$this->basePath.ltrim('/',$this->URI);
if (count($this->global->get()) && (!empty($this->nodes[0])))$this->fullPath.='?'.$this->global->server('QUERY_STRING');
}
else
{
//Catch the URI.
$this->URI=$this->global->get('URI');
//Create nodes from the caught URI.
$this->nodes=explode('/',$this->URI);
if (!end($this->nodes))array_pop($this->nodes);
if (!reset($this->nodes))array_shift($this->nodes);
//If we have no nodes (root address), specify node 0 as empty.
if (!isset($this->nodes[0]))$this->nodes[0]='';
//Create the base path.
$this->basePath=$this->global->server('HTTP_HOST').$this->config->path->publicroot.'?URI=';
//Construct the full path from the base path.
$this->fullPath=$this->basePath.$this->URI;
//Reconstruct the get vars onto the full path, filtering out the URI get var.
foreach ($this->global->get() as $key=>$val)
{
if ($key!='URI')$this->fullPath.="&$key=$val";
}
}
return true;
}
private function parseConfigPaths()
{
global $_OVERRIDE;
if (!isset($this->config->path->root)) $this->config->path->root=dirname(__FILE__)._;
if (!empty($this->config->path->publicroot))
{
$this->config->path->publicroot='/'.$this->config->path->publicroot.'/';
}
else
{
$this->config->path->publicroot='/';
}
if (!empty($this->config->path->publicrootcss))
{
$this->config->path->publicrootcss=$this->config->path->publicroot.$this->config->path->publicrootcss.'/';
}
if (!empty($this->config->path->publicrootjs))
{
$this->config->path->publicrootjs=$this->config->path->publicroot.$this->config->path->publicrootjs.'/';
}
if (!empty($this->config->path->publicrootimages))
{
$this->config->path->publicrootimages=$this->config->path->publicroot.$this->config->path->publicrootimages.'/';
}
if (isset($_OVERRIDE['publichtml']))
{
$this->config->path->publichtml= $_OVERRIDE['publichtml'];
}
else
{
$this->config->path->publichtml= $this->config->path->root.$this->config->path->publichtml._;
}
$this->config->path->system= $this->config->path->root.$this->config->path->system._;
$this->config->path->logs= $this->config->path->root.$this->config->path->logs._;
$this->config->path->data= $this->config->path->system.$this->config->path->data._;
$this->config->path->debug= $this->config->path->system.$this->config->path->debug._;
$this->config->path->core= $this->config->path->system.$this->config->path->core._;
$this->config->path->components= $this->config->path->system.$this->config->path->components._;
if (isset($_OVERRIDE['applicationsDir']))
{
$this->config->path->applications= realpath($_OVERRIDE['applicationsDir'])._;
}
else
{
$this->config->path->applications= realpath($this->config->path->system.$this->config->path->applications)._;
}
$fragments=explode(_,$this->config->path->applications);
if (!end($fragments))array_pop($fragments);
$this->config->path->publicapplications=$this->config->path->publicroot.end($fragments).'/';
return true;
}
private function obtainServerEnvironment()
{
$return=false;
if (isset($_SERVER['SERVER_SOFTWARE']))
{
if (@preg_match('@apache@i',$_SERVER['SERVER_SOFTWARE']))
{
$this->server['application']='apache';
}
else
{
$this->server['application']='unknown';
}
}
elseif (isset($_SERVER['CLIENTNAME']) && $_SERVER['CLIENTNAME']=='Console')
{
$this->server['application']='zendDebugger';
}
else if (isset($_SERVER['PHP_SELF']) && stristr($_SERVER['PHP_SELF'],'phpunit'))
{
$this->server['application']='phpunit';
$this->unitTesting =true;
}
if (stristr(PHP_OS,'win'))
{
$this->server['os']='windows';
}
else
{
$this->server['os']='unix';
}
$this->server['slash']=DIRECTORY_SEPARATOR;
if (!defined('_'))define('_',DIRECTORY_SEPARATOR);
if (!empty($this->server['application'])
&& !empty($this->server['os'])
&& !empty($this->server['slash']))
{
$return=true;
}
return $return;
}
// public function runApplicationWithBoundAddress($application=null,$address=array())
// {
// $return=false;
// if (!empty($application))
// {
// if (!isset($this->application->{$application}))
// {
// if (is_dir($this->config->path->applications.$application._)
// && (is_file($this->config->path->applications.$application._.$application.'.php')))
// {
// include_once($this->config->path->applications.$application._.$application.'.php');
// $className='application_'.$application;
// if (class_exists($className))
// {
// $return=$this->application->{$application}=new $className($this->core,$address);
// }
// else
// {
// $this->exception('Application "'.$application.'" has an invalid class name. Expecting "'.$className.'".','Core Object Handler');
// }
// }
// else
// {
// $this->exception('Unable to find application "'.$application.'".','Core Object Handler');
// }
// }
// else
// {
// $this->exception('');
// }
// }
// return $return;
// }
}
if (${CORE}=new core)
{
try
{
${CORE}->initiate();
}
catch (core_exception $e)
{
$e->outputError();
}
${CORE}->shutdown();
}
?>