-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccApp.php
1074 lines (1021 loc) · 35.6 KB
/
ccApp.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* File: ccApp.php
*
* The ccApp class represents the application. It is a singleton.
*
* @todo Look into using AutoLoad package (by the Doctrine and Symfony folks)
* @todo Add session handling?
* @todo Need a way to set "debug" setting that will cascade thru components.
* @todo Move error handling ccError class and refer through ccApp
* @todo Add on init-log-file(), to output info only when created.
* @todo Add setLogFile(), setAppDir(js)
* @todo Consider a separate "site" object (to allow site specific configuration),
* currently part of the "app" object.
*
* @todo Consider moving most of this code to index.php? No. Keep most of this
* out of public/hacking view
* Things I need to do soon:
* @todo Add example of DB/model component (Doctrine? RedBean?)
* @todo Add internal "redirection" support
* @todo Allow site paths to auto-generate paths.
* @todo Debugging/tracing component (work in progress: ccTrace
* @todo Move error handling ccError class and refer through ccApp?
*
* Things I dunno how to do:
* @todo Need for session support?
* @todo Page caching
* @todo ob_start() support
* @todo Create structure of simple front-end event mapping to support here.
* @todo CSS and JS compression/minimization support for production mode.
* @todo Single ccApp.setDebug() setting that will cascade thru components.
* @todo Logging support.
* @todo Reconsider DevMode handling (rename to AppMode).
* @todo Need a way to set "debug" setting that will cascade thru components.
* @todo Look into using AutoLoad package (by the Doctrine and Symfony folks)?
* @todo MODE_PRODUCITON should prevent revealing errors (hide path info)
*/
/*
* @See http://php.net/manual/en/reserved.variables.php#Hcom55068
* 2010-10-22 404 uses exceptions
* - Extend classpath interpretation to enter specific class specs.
* 2013-08-28 Renamed get/setMainPage() get/setPage()
* - Removed setFrameworkPath()
* - setFrameworkPath() is now static
* - Fix getRootUrl()'s duplicate '/'
* - Redefined operational mode settings (ccApp::MODE_*)
* 2013-09-02 Renamed get/setSitePath() to get/setAppPath()
* - Renamed createSiteDir() to createAppDir() and protected it.
* - Added createWorkingDir()
* 2013-09-12 Remove getPage()
* 2017-11-07 If no ccRequest is passed to dispatch(), it is created.
*/
//******************************************************************************\
namespace {
// [BEGIN] Portability settings
// @see http://www.php.net/manual/en/function.phpversion.php
// @see http://www.php.net/manual/en/reserved.constants.php#reserved.constants.core
if (!defined('PHP_VERSION_ID'))
{
$_version = explode('.', PHP_VERSION);
define('PHP_VERSION_ID', ($_version[0] * 10000 + $_version[1] * 100 + $_version[2]));
}
if (PHP_VERSION_ID < 50400) {
if (PHP_VERSION_ID < 50300)
{
if (PHP_VERSION_ID < 50207)
{
if (PHP_VERSION_ID < 50200)
define('E_RECOVERABLE_ERROR',4096);
define('PHP_MAJOR_VERSION', $_version[0]);
define('PHP_MINOR_VERSION', $_version[1]);
$_version = explode('-', $_version[2]);
define('PHP_EXTRA_VERSION', $_version[0]);
define('PHP_RELEASE_VERSION', isset($_version[1]) ? $_version[1] : '');
}
define('E_DEPRECATED', 8092);
define('E_USER_DEPRECATED', 16384);
define('__DIR__', dirname(__FILE__));
}
define('PHP_SESSION_DISABLED',0);
define('PHP_SESSION_NONE',1);
define('PHP_SESSION_ACTIVE',2);
/**
* Return $status of session. Built-in available in 5.4
* @return int enum of $status
* @see php.net
*/
function session_status()
{
return \session_id() === '' ? PHP_SESSION_NONE : PHP_SESSION_ACTIVE;
}
}
unset($_version); // Not needed any longer
// [END] Portability settings
} // namespace
namespace //! ccPhp
{
// include 'ccPhp.inc';
//!use ccPhp\core\ccTrace;
//!use ccPhp\core\ccRequest;
//!use ccPhp\core\ccPageInterface;
// include('ccTrace.php'); // @todo Remove
//******************************************************************************
/**
* Framework class representing the "application". You can derive this class, but
* you are not allowed to instantiate it directly, use ccApp::createApp().
*
* @package ccPhp
* @todo Consider consolidating createApp() with getApp()
* @todo Support instance specific error and exception handlers
* @todo Consider that flags can be user defined, with some pre-defined meanings.
*/
class ccApp
implements \Serializable
{ /** Debugging output */
const MODE_DEBUG = 1;
/** PHP info msgs */
const MODE_INFO = 6;
/** PHP warnings */
const MODE_WARN = 2;
/** PHP errors */
const MODE_ERR = 4;
/** Show tracebacks */
const MODE_TRACEBACK= 8;
/** Reveal paths */
const MODE_REVEAL = 16;
/** Use minimized resources (scripts, CSS, etc.) */
const MODE_MINIMIZE = 32;
/** Enable profile */
const MODE_PROFILE = 64;
/** Enable caching where it can */
const MODE_CACHE = 128;
/** @var ccApp Reference to singleton self */
protected static $_me=NULL;
// Configuration array
// protected $config=Array();
/** @var string URL path offset to base of this app;
* path from domain root for the site.
*/
protected $UrlOffset=NULL;
/** @var int Operation mode bitmask. See MODE_* constants */
protected $devMode = CCAPP_DEVELOPMENT;
/** @var boolean Central place to hold debug status */
// protected $bDebug = FALSE;
/** @var string Path to site specific files. */
protected $sitepath=NULL;
/** @var string Path to working directory (for cache, etc) */
protected $temppath='';
/** @var ccPageInterface Main page object for app */
protected $page=NULL;
/** @var string|ccPageInterface class that renders 404 pages. */
protected $error404 = NULL;
// The following are rel to sitepath:
/** @var array List of site paths to search for classes */
protected $classpath=array();
/** @var SplClassLoader reference */
// protected $classLoader=NULL;
/** @var ccRequest Remember current request */
protected $current_request;
/**
* Save $_me as a singularity and (hack).
*/
protected function __construct() // As a singleton: block public allocation
{
self::$_me=$this;
$callstack = (PHP_VERSION_ID < 50400) ? debug_backtrace(0) : debug_backtrace(0,2);
// Hack: look up the call-stack to pull 1st arg from createApp()
foreach ($callstack as $caller)
if ($caller['function'] == 'createApp')
{
$this->sitepath = $caller['args'][0];
if (substr($this->sitepath, -1) != DIRECTORY_SEPARATOR)
$this->sitepath .= DIRECTORY_SEPARATOR;
break;
}
} // __construct()
/**
* Search for class definition from framework folders.
* If there is an instance of the app, call its autoload first where
* site specific searches will take precedence.
*
* This method is appropriate to call this from __autoload() or
* register via spl_autoload_register()
* @param string $className Name of class to load.
*/
public static function _autoload($className)
{
// echo __METHOD__.'#'.__LINE__."($className) ".__NAMESPACE__.' '.__CLASS__. " <br>";
if (self::$_me && method_exists(self::$_me,'autoload'))
{
self::$_me->autoload($className); // Using spl_autoload_register()?
}
// if (strpos($className, 'ccTrace') > 0) {
// echo '<pre>';
// var_dump(debug_backtrace());
// echo '</pre>';
// }
// Check instance specific autoload
if (!class_exists($className)) // Check framework directories
{
if ( __NAMESPACE__ != ''
&& __NAMESPACE__ == substr($className, 0, strlen(__NAMESPACE__)))
{
$className = explode('\\', $className);
// // If __NAMESPACE__ in effect and namespace is not part of name
// // or no NS specified, return (not request a ccFramework class)
// if ( $className[0] != __NAMESPACE__ || count($className) < 2)
// return;
$className = end($className); // Use only classname of this f/w
}
// $classFilename = str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
$classFilename = $className.'.php';
if (file_exists(self::getFrameworkPath() . 'classes' . DIRECTORY_SEPARATOR . $classFilename))
include(self::getFrameworkPath() . 'classes' . DIRECTORY_SEPARATOR . $classFilename);
elseif (file_exists(self::getFrameworkPath() . $classFilename))
include(self::getFrameworkPath() . $classFilename);
}
} // _autoload()
/**
* Add a path to the list of site-specific paths to search when
* loading site-specific classes.
* @param string $path Is the path to be included in the search
* or, if $classname is specified, then the full fliepath. If the first
* char is not '/' (or '\', as appropriate) then the site dir is
* assumed.
* @param string $classname is an optional class name that, when sought, will
* load the specified file specified by $path. If prefixed with a '\',
* then it represents a namespace. In this case, $path represents a
* source directory for files of that namespace.
* @example
* define('DS',DIRECTORY_SEPARATOR);
* $app->addClassPath('classes') // Search app's directory
* ->addClassPath('..'.DS.'smarty'.DS.'Smarty.php','Smarty')
* ->addClassPath('..'.DS.'RedBeanPHP'.DS.'rb.php','R')
* ->addClassPath('..'.DS.'Facebook'.DS.'facebook.php','Facebook');
* @todo Allow array of directories to be passed in.
* @todo Test for path's existence.
*/
function addClassPath($path,$classname=NULL)
{
if (!$path)
$path = $this->sitepath;
elseif ($path[0] != DIRECTORY_SEPARATOR)
$path = $this->sitepath.$path;
if ($classname)
$this->classpath[$classname] = $path;
else
{
if (substr($path,-1) != DIRECTORY_SEPARATOR)
$path .= DIRECTORY_SEPARATOR;
$this->classpath[] = $path;
}
return $this;
} // addClassPath()
/**
* Convenience method to prefix or suffix the PHP search path.
* @param string $path Path component to add to search path.
* @param bool $prefix Prefix the search path with the new path. Default:
* false, append new path to the end of the search path.
* @see set_include_path()
* @todo If not absolute path, prefix with site path.
* @todo Automatically convert '/' or '\' to the correct DIRECTORY_SEPARATOR
* for the current OS.
*/
public function addPhpPath($path,$prefix=FALSE)
{
set_include_path(
$prefix
? $path . PATH_SEPARATOR . get_include_path()
: get_include_path() . PATH_SEPARATOR . $path
);
return $this;
} // addPhpPath()
/**
* Instance specific autoload() searches site specific paths.
* @param string $className Name of class to load.
*/
public function autoload($className)
{
$classFilename = str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
$classFilename = str_replace('\\', DIRECTORY_SEPARATOR, $className).'.php';
// global $bb,$eb, $bi,$ei, $btt,$ett, $rarr,$ldquo,$rdquo,$hellip,$nbsp,$nl;
// ccTrace::s_out( '#'.__LINE__.' '.ccTrace::getCaller(0,dirname(self::getApp()->getAppPath())).': '.$className." $rarr ".$classFilename.$nl);
// ccTrace::s_out( '#'.__LINE__.' '.ccTrace::getCaller(3).': '.$className." $rarr ".$classFilename.$nl);
// echo '#'.__LINE__." $className $rarr $classFilename".$nl;
// self::tr(' '.$this->sitepath.$classFilename);
// ccTrace::tr(' '.$this->sitepath.$classFilename);
// Check app paths, first
if ($this->sitepath && file_exists($this->sitepath . $classFilename))
{
include($this->sitepath . $classFilename);
return;
}
else foreach ($this->classpath as $class => $path)
{
// ccTrace::s_out( $nbsp.$nbsp.'*'.$class.'*'.$className.'*'.(($class === $className)).'*'.$path.$nl);
// self::tr( '*'.$class.'*'.$className.'*'.(($class === $className)).'*'.$path);
if ($class === $className) // If specific class mapping
{ // then load associated file
// if (!file_exists($path))
// throw new Exception($path . " does not exist in ".getcwd());
if (require($path))
return;
}
// If class-association registered w/ccApp is a namespace, check whether
// class to load has a namespace; if namespace names match, then use
// registered path as source.
if ( $class[0] === '\\' && strpos($className,'\\') !== FALSE
&& substr($class,1).'\\' === substr($className,0,strlen($class)) )
{
// echo '#'.__LINE__." class=$class path=$path$nl";
$namespaceClassName = substr($className,strlen($class)).'.php';
// echo '#'.__LINE__.' '.$className." $rarr ".$path.$namespaceClassName.$nl;
if (include($path . $namespaceClassName))
return;
}
if (file_exists($path . $classFilename))
// elseif (@include($path . $classFilename))
{ // Else if assumed name exists...
// echo '#'.__LINE__." Exists=$path$classFilename$nl";
include($path . $classFilename);
return;
}
}
// echo '#'.__LINE__.' '.$className.$rarr.$classFilename.$nl;
// @include($classFilename); // Finally, check include path.
// if (class_exists($className)) // Check framework directories
// echo '#'.__LINE__.$nbsp.$nbsp.$className.$rarr.$classFilename.' LOADED!!!'.$nl;
// IF WE GET HERE, WE COULD NOT RESOLVE THE CLASS
} // autoload()
/**
* Create singleton instance of the app.
*
* @param string $appPath Absolute path to the app's code.
* @param string $className By default instance of ccApp is created, but
* the name of a derived class can be instantiated.
* @return ccApp App object.
* @todo Consider consolidating into getApp()
* @todo Consider allowing instance of ccApp to be passed.
* @todo Consider blocking this from creating a 2nd instance.
* @todo Load cached version, if available.
* @todo Allow parameters passe to constructor.
*/
public static function createApp($appPath, $className=NULL)
{
// $sessActive = (session_status() == PHP_SESSION_ACTIVE);
// if (!$sessActive) // If session support not running
// session_start(); // turn on to presist browser info
//
// if ( isset($_SESSION['ccApp']) ) // If already cached, return info
// {
// self::$_me = unserialize($_SESSION['ccApp']);
// if ( !$sessActive ) // Session wasn't running
// session_commit(); // So turn back off
// return self::$_me; // Return serialized object
// }
//
if (substr($appPath,-1) != DIRECTORY_SEPARATOR) // Ensure path-spec
$appPath .= DIRECTORY_SEPARATOR; // suffixed w/'/'
chdir($appPath); // Set cd to "known" place
$className ? new $className : new self;
// $_SESSION['ccApp'] = serialize(self::$_me);
// if ( !$sessActive ) // Session wasn't running
// session_commit(); // So turn back off
return self::$_me;
} // createApp()
/**
* Create site-specific directory, if it doesn't exist.
*
* @param string $dir Directory name (relative to site-path), if not an
* absolute path.
*
* @return string Semi-normalized path name (suffixed with '/' prefixed w/
* site-path.
* @todo Accept array of directory names (to move common code here)
*/
public function createAppDir($dir)
{
if ( substr($dir, -1) != DIRECTORY_SEPARATOR ) // Ensure suffixed w/'/'
$dir .= DIRECTORY_SEPARATOR;
if ( $dir[0] != DIRECTORY_SEPARATOR ) // Not absolute path?
$dir = $this->sitepath . $dir; // Prefix with site's path
if (!is_dir($dir)) // Path does not exist?
mkdir($dir,0744,TRUE); // Create path
return $dir; // Return modified path
} // createAppDir()
/**
* Create a directory under the app's working directory.
* @param string $dir The name of a directory to be created under the working dir
* @return string The path to the directory created.
*/
public function createWorkingDir($dir)
{
if ( substr($dir, -1) != DIRECTORY_SEPARATOR ) // Ensure suffixed w/'/'
$dir .= DIRECTORY_SEPARATOR;
if ( $dir[0] != DIRECTORY_SEPARATOR ) // Not absolute path?
$dir = $this->sitepath.$this->temppath.$dir;// Prefix with site's working path
if (!is_dir($dir)) // Path does not exist?
mkdir($dir,0744,TRUE); // Create path
return $dir; // Return modified path
} // createWorkingDir()
/**
* This method is called to render pages for the web site. It invokes the
* "page" (which is usually a dispatcher or controller) to render
* content. ccPageInterface::render() returning false implies no content was
* rendered then 404, Page Not Found. handling is invoked.
* @param ccRequest $request Current HTTP/ccPhp request
* @throws ccHttpStatusException If false, since this is the end of the line.
*/
public function dispatch(ccRequest $request=NULL)
{
($request === NULL) && $request = new ccRequest();
try
{
$this->current_request = &$request;
if (!$this->page->render($request))
{
// ccTrace::tr(getallheaders());
throw new ccHttpStatusException(404);
}
}
catch (ccHttpStatusException $e)
{
switch ($e->getCode())
{
case 300: case 301: case 302: case 303:
case 305: case 306: case 307:
header($_SERVER['SERVER_PROTOCOL'].' '.$e->getCode().' '.$e->getMessage(), TRUE, $e->getCode());
$this->redirect($e->getLocation(), $e->getCode(), $e->getMessage());
break;
case 304:
header($_SERVER['SERVER_PROTOCOL'].' '.$e->getCode().' '.$e->getMessage(), TRUE, $e->getCode());
break;
case 404: $this->show404($request);
break;
default: // No other stati supported right now.
// http_response_code($e->getCode());
if (!headers_sent())
header($_SERVER['SERVER_PROTOCOL'].' '.$e->getCode().' '.$e->getMessage(), TRUE, $e->getCode());
throw $e;
}
}
} // dispatch()
// /**
// * A place to get/set settings that should be accessible across the application
// * @param string $name Name of value to return.
// *
// * @see set()
// * @todo Option to automatically save in session, dependent on dev mode.
// */
// public function get($name, $default=NULL)
// {
// if (!isset($this->config[$name])) // If not set,
// $this->set($name, $default); // set to default
// return $this->config[$name]; // and return value.
// } // get()
// /**
// * A place to get/set settings that should be accessible across the application
// * @param string $name Name of value to set (names starting with an underscore
// * are reserved for "internal use" and should be avoided).
// * @param mixed $value Value associated with $name.
// *
// * @see get()
// * @todo Option to automatically save in session, dependent on dev mode.
// */
// public function set($name, $value)
// {
// $this->config[$name] = $value;
// return $this;
// } // set()
/**
* Handle 404 (page not found errors).
* @param ccPageInterface|string $error404page The object or classname that
* would render a 404 page.
* @see show404() on404()
* @todo Add support for string name of class.
*/
function set404Page(ccPageInterface $error404page)
{
$this->error404 = $error404page;
return $this;
} // set404handler()
/**
* Get current app object singleton.
* @return object App singleton instance
*/
public static function getApp()
{
return self::$_me;
} // getApp()
/**
* Default cookie setting to URL Offset. If the path is not specified or
* not an absolute path, then the base is assumed to be the URL offset to
* the site.
*
* @param string $name Cookie name
* @param string $value Cookie value
* @param integer $expire Expiration
* @param string $path URI sub-path
* @param string $domain Domain
* @param boolean $secure https only?
* @param boolean $httponly http only?
*
* @see http://php.net/manual/en/function.setcookie.php
* @see getUrlOffset()
*/
function setCookie(
$name,
$value=NULL,
$expire = 0,
$path=NULL,
$domain=NULL,
$secure=false,
$httponly=false )
{
if ($path === NULL)
$path = $this->getUrlOffset();
elseif ($path[0] != '/')
$path = $this->getUrlOffset() . $path;
if ($expire === 0 && ($value === NULL || $value == ''))
$expire = time()-3600; // Delete cookie
setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
} // setCookie()
// /**
// * Set debug setting
// */
// public function getDebug()
// {
// return $this->bDebug;
// }
// public function setDebug($bDebug=TRUE)
// {
// $this->bDebug = $bDebug;
// return $this;
// }
/**
* Get/set app's disposition mask.
*/
public function getDevMode()
{
return $this->devMode;
}
/**
* Set "how" the app should behave based on the $mode bit-mask.
* @param integer $mode [description]
* MODE_DEBUG Debugging output
* MODE_INFO PHP info msgs
* MODE_WARN PHP warnings
* MODE_ERR PHP errors
* MODE_TRACEBACK Show tracebacks
* MODE_REVEAL Reveal paths
* MODE_MINIMIZE Use minimized resources (scripts, CSS, etc.)
* MODE_PROFILE Enable profile
* MODE_CACHE Enable caching where it can
*/
public function setDevMode($mode)
{
$this->devMode = $mode;
return $this;
}
/**
* Set the error handler; a convenience method for stylistic consistency.
* @param callback $function The name of the callback function or array,
* when the callback is a class or object method.
* The callback function should look like:
* handler ( int $errno , string $errstr [,
* string $errfile [, int $errline [, array $errcontext ]]] )
* @see http://www.php.net/manual/en/function.set-error-handler.php
* @todo Probably don't need this if errors are chained to exceptions.
*/
function setErrorHandler($function)
{
set_error_handler($function);
return $this;
} // setErrorHandler()
/**
* Set the exception handler; a convenience method for stylistic consistency.
* @param callback $function The name of the callback function or array,
* when the callback is a class or object method.
* The callback function should look like:
* handler ( Exception $e )
* @see http://www.php.net/manual/en/function.set-error-handler.php
*/
function setExceptionHandler($function)
{
set_error_handler($function);
return $this;
} // setErrorHandler()
/**
* Set server path to site's files (not the URL)
* @param string $path Full, absolute path (e.g., dirname(__FILE__) of
* ccApp.php)
*/
public static function getFrameworkPath()
{
return dirname(__FILE__).DIRECTORY_SEPARATOR;
} // getFrameworkPath()
// /**
// * @return object app's main page (e.g., dispatcher or controller)
// */
// public function getPage()
// {
// return $this->page;
// } // getPage()
/**
* Set the primary page handler for the app. This is usually a controller
* that dispatches to other handlers (internally or externally; i.e., other
* ccPageInterface objects).
* @param ccPageInterface $page [description]
*/
public function setPage(ccPageInterface $page)
{
$this->page = $page;
return $this;
} // setPage()
/**
* Current request (set via dispatch())
*/
function getRequest()
{
return $this->current_request;
} // getRequest()
/**
* Get the part of the URL which points to the root of this app, i.e., the
* start of where this app resides.
* @return string The URI
* @see getUrlOffset()
* @todo Handle case where URL does not have a scheme
* @todo Move to ccRequest
*/
function getRootUrl()
{
$path = isset($_SERVER['REDIRECT_SCRIPT_URI'])
? $_SERVER['REDIRECT_SCRIPT_URI']
: $_SERVER['SCRIPT_URI'];
$p = strpos($path,'//'); // Offset past the protocol scheme spec
if ($p === FALSE) // No protocol scheme.
{ // Don't know what to do here... bad input.
}
else // Set $path to the part past the domain:port part
{ // suffixed with a '/'
$p = strpos($path,'/',$p+2); // First path separator past the scheme
if ($p === FALSE) // No '/': this app is at the root.
$path .= '/'; // Ensure it ends with a '/'
else
$path = substr($path,0,$p+1); // Ignore path after the domain portion.
}
return $path . substr($this->getUrlOffset(),1);
} // getRootUrl()
/**
* Get the server path to site's files (not the URL)
* @param string $path Full, absolute path (e.g., dirname(__FILE__)
* of caller)
*/
public function getAppPath()
{
return $this->sitepath;
} // getAppPath()
// /**
// * Get/set server path to site's files (not the URL). This method also sets
// * this path as the current directory so that all subsequent relative
// * paths are from a normalized location.
// * @param string $path Full, absolute path (e.g., dirname(__FILE__)
// * of caller)
// */
// public function setAppPath($path)
// {
// if (substr($path,-1) != DIRECTORY_SEPARATOR) // Ensure path-spec
// $path .= DIRECTORY_SEPARATOR; // suffixed w/'/'
//
// $this->sitepath = $path; // Save path
// chdir($path); // Set cd to "known" place
// return $this;
// } // setAppPath()
/**
* The path part of the URL starting from '/' up to the path where this
* app's "root" starts; e.g., "/" or "/index.php/".
*
* @see getRootUrl()
* @todo Consider moving to ccRequest?
*/
public function getUrlOffset()
{
if (!$this->UrlOffset) // If not set,
$this->initUrlOffset(); // Ensure init'd
// echo __METHOD__.'#'.__LINE__.' "'.$this->UrlOffset.'"<br/>';
return $this->UrlOffset;
} // getUrlOffset()
/**
* This returns the part of the url that is the "root" of this app.
* This value is inferred from env settings, so shouldn't be public.
*/
private function initUrlOffset()
{
$UrlOffset = dirname($_SERVER['SCRIPT_NAME']);
if ($UrlOffset != '/')
$UrlOffset .= '/';
$this->UrlOffset = $UrlOffset;
} // initUrlOffset()
/**
* Set the application's working directory relative to the app's code. It
* is created, if necessary.
* @param string $dir Directory name, relative to app's path (unless an
* absolute path-spec).
* @see createAppDir();
*/
public function setWorkingDir($dir)
{
if ( $dir != '' && substr($dir, -1) != DIRECTORY_SEPARATOR ) // Ensure suffixed w/'/'
$dir .= DIRECTORY_SEPARATOR;
$this->createAppDir($dir);
$this->temppath = $dir;
return $this;
} // setWorkingDir()
/**
* Get the full path to the working directory.
* @return string Absolute path of working directory
*/
public function getWorkingPath()
{
return ($this->temppath[0] == DIRECTORY_SEPARATOR)
? $this->temppath
: $this->sitepath.$this->temppath;
}
/**
* Default 404 handler.
* @param ccRequest $request The current request
*/
protected function on404(ccRequest $request)
{
if (!headers_sent())
{
http_response_code(404);
// header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', TRUE, 404);
header('Content-type: text/html');
}
?>
<html><body><hr/>
<?php print $request->getUrl() ?>
<h1>404 Not Found</h1>
This is not the page you are looking for.<hr/>
<?php
if ($this->getDevMode() & self::MODE_TRACEBACK)
{
$trace = debug_backtrace(); // Get whole stack list
array_shift($trace); // Ignore this function
ccTrace::showTrace($trace); // Display stack.
}
?>
</body></html>
<?php
// exit();
} // on404()
/**
* Php error handler directs output to destinations determined by ccTrace. This also
* will output to stdout based on the app's devMode setting.
*
* @param integer $errno Error number
* @param string $errstr Error text
* @param string $errfile Filename containing error
* @param integer $errline Line number of error occurance in $errfile
* @param array $errcontext Symbol table state when error occurred
* (Deprecated as of v7.2.0)
*
* @todo Consider throwing exception (caveat, flow of control does not continue)
* @todo Add distinction between dev and production modes of output.
* @todo Consider moving to separate Trace class
* @todo Display error to page based on display_errors setting
*/
static function onError($errno, $errstr, $errfile, $errline, $errcontext=NULL)
{
if (error_reporting() & $errno)
{
$errortype = Array(
E_ERROR => 'Error', // 1
E_PARSE => 'Parsing Error', // 4
// E_CORE_ERROR => 'Core Error', // 16
// E_CORE_WARNING => 'Core Warning', // 32
E_COMPILE_ERROR => 'Compile Error', // 64
// E_COMPILE_WARNING => 'Compile Warning', // 128
E_WARNING => 'Warning', // 2
E_NOTICE => 'Notice', // 8
E_USER_ERROR => 'User Error', // 256
E_USER_WARNING => 'User Warning', // 512
E_USER_NOTICE => 'User Notice', // 1024
E_STRICT => 'Strict' // 2048
);
if ( PHP_VERSION_ID >= 50200 )
{
$errortype[E_RECOVERABLE_ERROR] = 'Recoverable Error';// 4096
if ( PHP_VERSION_ID >= 50300 )
{
$errortype[E_DEPRECATED] = 'Deprecated'; // 8092
$errortype[E_USER_DEPRECATED] = 'User Deprecated'; // 16384
}
}
if (!isset($errortype[$errno]))
$errortype[$errno] = "Error($errno)";
global $bred,$ered,$bb,$eb, $bi,$ei, $btt,$ett, $rarr,$ldquo,$rdquo,$hellip,$nbsp,$nl;
error_log("$errortype[$errno]: $errstr in $errfile#$errline",0);
$msg = "$bb$bred$errortype[$errno]$ered: $errstr$eb$nl"
// . " in $errfile#$errline";
. " in ".ccTrace::fmtPath($errfile,$errline);
$self = self::getApp();
if ($self) // In case this is invoked before constructor
switch ($errno)
{
case E_COMPILE_ERROR:
case E_ERROR:
case E_PARSE:
case E_USER_ERROR:
if ( ! ($self->devMode & self::MODE_ERR) ) break;
$errno = E_ERROR; // Normalize for next step
case E_WARNING:
case E_USER_WARNING:
case E_RECOVERABLE_ERROR:
if ( ! ( $errno == E_ERROR || $self->devMode & self::MODE_WARN ) ) break;
$errno = E_WARNING; // Normalize for next step
case E_NOTICE:
case E_USER_NOTICE:
case E_DEPRECATED:
case E_USER_DEPRECATED:
if ( ! ( $errno == E_WARNING || $self->devMode & self::MODE_INFO ) ) break;
echo "$msg<br/>";
break;
default:
echo "errno($errno): $msg<br/>";
break;
}
else
echo "errno($errno): $msg<br/>";
// self::log($msg);
$trace = debug_backtrace(); // Get whole stack list
array_shift($trace); // Ignore this function
ccTrace::showTrace($trace); // Display stack.
return TRUE;
}
else
return FALSE; // chain to normal error handler
} // onError()
/**
* Default exception handler. Works in conjunction with ccTrace to produce
* formatted output to the right destination.
*
* @param Exception $exception Exception object to report
*
* @todo Add distinction between dev and production modes of output.
* @todo See php.net on tips for proper handling of this handler.
* @todo Consider moving to separate Trace class
*/
static function onException($exception)
{
try
{
global $bred,$ered,$bb,$eb, $bi,$ei, $btt,$ett, $rarr,$ldquo,$rdquo,$hellip,$nbsp,$nl;
$msg = $bb.get_class($exception).'('.$exception->getCode().'):'.$eb.' "'.$exception->getMessage().'" in '.ccTrace::fmtPath($exception->getFile()).'#'.$exception->getLine();
print $msg.$nl;
self::log($msg);
ccTrace::showTrace($exception->getTrace());
// die();
}
catch (Exception $e)
{
self::log(get_class($e)." thrown within the exception handler. Message: ".$e->getMessage()." on line ".$e->getLine());
// die();
}
} // onException()
/**
* Redirect to a different URL.
* @param string $url Send rediret to browser
* @param integer $status HTTP status code #
* @param string $message Status code text message
*
* @todo Forward qstring, post variables, and cookies.
* @todo Allow "internal" redirect that does not return to the client.
* @todo Consider using ccHttpStatusException
*/
function redirect($url,$status = 302,$message='Redirect')
{
if (!headers_sent())
{
header($_SERVER['SERVER_PROTOCOL'].' '.$status.' '.$message, TRUE, $status);
header('Location: '.$url);
echo "Redirecting to {$url} via header…";
}
else
{
echo <<<EOD
Redirecting to {$url} via scripting…
<script>window.top.location.href="$url"</script>
EOD;
}
exit();
} // redirect()
/**
* For whatever reason, display 404 page response.
* @param ccRequest $request Current HTTP/ccPhp request
*/
protected function show404(ccRequest $request)
{
ccTrace::log($request->getUrl());
if ($this->error404) // If 404 page defined
{
if (is_string($this->error404))
$this->error404 = new $this->error404;
if (($this->getDevMode() & CCAPP_DEVELOPMENT)
&& !($this->error404 instanceof ccPageInterface))
{
trigger_error(get_class($this->error404).' does not implement ccPageInterface', E_WARNING);
}
call_user_func(array($this->error404,'render'), $request);
}
else // No app specific page
$this->on404($request); // Perform local 404 rendering
} // show404()
// static function out($string)
// {
// if (!(ccApp::$_me->devMode & CCAPPE_DEVELOPMENT))
// return;
// //error_log($string,3,'/home/wrlee/htd.log');
// echo $string;
// }
/**
* Output to log file.
* options: HTML, log, stderr, stdout, formatted, timestamp
*/
static function log()
{
return call_user_func_array(array('ccTrace','log'),func_get_args());
} // tr()
/**
* Output debug output.
* options: HTML, log, stderr, stdout, formatted, timestamp
*/
static function tr()
{
return call_user_func_array(array('ccTrace','tr'),func_get_args());
} // tr()
/**
* serializable implementation to save instance.
* @return [type] Serialized object
*/
public function serialize ( )
{
return serialize($this);
}
/**
* serializable implementation to restore object.
* @param [type] $serialized Serialized object.
*/
public function unserialize ( $serialized )
{