-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.php
1565 lines (1439 loc) · 47.5 KB
/
index.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
/**
* Faster -- A Minimalist PHP MVC Framework
*
* This is a single file MVC framework, combining bootstrap, framework, and front controller
* into one file.
*
* @package Faster-Framework-API
* @author Volo, LLC
* @link http://volosites.com/
* @version 1.0370
*/
// SPEED UP PHP BY TURNING OFF UNNECESSARY ASP TAG PARSING
ini_set('asp_tags','0');
// FIX MAGIC QUOTES IF TURNED ON
if (get_magic_quotes_gpc()) {
$process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
while (list($key, $val) = each($process)) {
foreach ($val as $k => $v) {
unset($process[$key][$k]);
if (is_array($v)) {
$process[$key][stripslashes($k)] = $v;
$process[] = &$process[$key][stripslashes($k)];
} else {
$process[$key][stripslashes($k)] = stripslashes($v);
}
}
}
unset($process);
}
// FIXES A PROBLEM WHERE fgets() AND file() MAY NOT READ LINEWRAPS ON MAC OR WINDOWS FILES PROPERLY.
// BSD UNIX AND UNIX AND LINUX ALL USE \n FOR LINE WRAPS.
// MACS, EVEN THOUGH BASED ON BSD UNIX, USES \r FOR LINE WRAPS IN MOST GUI-BASED APPS.
// WINDOWS ABOUT 99% OF THE TIME USES \r\n FOR LINE WRAPS IN FILES.
ini_set('auto_detect_line_endings','1');
// TURN ON SHORT OPEN TAGS
ini_set('short_open_tag','On');
// SET SESSION TIMEOUT
ini_set('session.cookie_lifetime','3600'); //an hour
ini_set('session.gc_maxlifetime','3600');
// SOMETIMES IN VERY RARE CASES WE MAY ARRIVE HERE BY REDIRECT_QUERY STRING.
// IT MESSES UP OUR $_GET AND WE HAVE TO REPAIR IT.
if (isset($_SERVER['REDIRECT_QUERY_STRING'])) {
$_GET = array();
$_SERVER['QUERY_STRING'] = $_SERVER['REDIRECT_QUERY_STRING'];
parse_str(preg_replace('/&(\w+)(&|$)/', '&$1=$2', strtr($_SERVER['QUERY_STRING'], ';', '&')), $_GET);
}
// boot our framework
$mvc = new Faster();
$mvc->core = new Faster_Core();
$mvc->view = new Faster_View();
$mvc->request = new Faster_Request();
$mvc->model = new Faster_Model();
$mvc->data = new Faster_Data();
// Remember, Faster_Core does not inherit from Faster. This is because we want to restrict $this only to
// variables ($this->VARIABLE) and core things like baseurl(), etc.
$mvc->view->core = $mvc->core;
$mvc->view->_setRequest($mvc->request);
// inherits from Faster and therefore gets full $this objects for the framework
$mvc->request->core = $mvc->core;
$mvc->request->view = $mvc->view;
$mvc->request->request = $mvc->request;
$mvc->request->model = $mvc->model;
$mvc->request->data = $mvc->data;
// inherits from Faster and therefore gets full $this objects for the framework
$mvc->model->core = $mvc->core;
$mvc->model->view = $mvc->view;
$mvc->model->request = $mvc->request;
$mvc->model->model = $mvc->model;
$mvc->model->data = $mvc->data;
// does not inherit from Faster because it only needs the core object for pathing reasons
$mvc->data->_setCore($mvc->core);
// set our page timer
$mvc->core->_setPageLoadStart();
// let our request object figure out the incoming URL
$sTest = @ $argv[1];
$bCLI = (!empty($sTest));
if ($bCLI) {
$sPath = $sTest;
$sPath = str_replace('--PATH','',$sPath);
$sPath = str_replace('--path','',$sPath);
$sPath = str_replace('--','',$sPath);
$sPath = str_replace('.php','',$sPath);
$sPath = str_replace('.PHP','',$sPath);
$sPath = str_replace('=','',$sPath);
$sPath = str_replace('"','',$sPath);
$sPath = str_replace("'",'',$sPath);
$sPath = str_replace('\\','/',$sPath);
$sPath = str_replace(' ','',$sPath);
$mvc->request->_setGroup($mvc->request->polishGroup($sPath));
$mvc->request->_setAction($mvc->request->polishAction($sPath));
} else {
$mvc->request->_setGroup($mvc->request->getGroup());
$mvc->request->_setAction($mvc->request->getAction());
}
// SET OUR TIMEZONE STUFF
try {
$sTimeZone = @ $mvc->core->config['TIMEZONE'];
$sTimeZone = (empty($sTimeZone)) ? 'GMT' : $sTimeZone;
if (function_exists('date_default_timezone_set')) {
date_default_timezone_set($sTimeZone);
}
else {
putenv('TZ=' .$sTimeZone);
}
ini_set('date.timezone', $sTimeZone);
} catch(Exception $e) {}
// display errors or log them instead
$sDisplayErrors = @ $mvc->core->config['DISPLAY_ERRORS'];
$sDisplayErrors = (empty($sDisplayErrors)) ? 'Off' : $sDisplayErrors;
ini_set('display_errors',$sDisplayErrors);
// set our error reporting
$sErrReporting = @ $mvc->core->config['ERROR_REPORTING'];
$sErrReporting = (empty($sErrReporting)) ? E_ALL : $sErrReporting;
error_reporting($sErrReporting);
// define our FRAMEWORK_LOADED constant, used by files to keep out prying eyes
/**
* Our constant to identify that the framework has been loaded, and therefore avoid issues with
* prying eyes directly on application folder scripts.
*
*/
define('FRAMEWORK_LOADED',TRUE);
$sBase = $mvc->core->base();
/**
* Our constant to provide a quick way to identify a header path.
*
*/
define('HEADER',$sBase . '/app/_views/HEADER.php');
/**
* Our constant to provide a quick way to identify a footer path.
*
*/
define('FOOTER',$sBase . '/app/_views/FOOTER.php');
// handle our front controller task
$bStopWhenRouted = TRUE;
$mvc->request->dispatchRoute('',$bStopWhenRouted);
/**
* Faster Class
*
* Serves up the entire framework off the $this variable, as in $this->core, $this->request,
* $this->model, and $this->data.
*
* @package Faster-Framework-API
*/
class Faster {
/**
* Gets mapped to Faster_Core
*/
public $core;
/**
* Gets mapped to Faster_Request; synonym for controller for faster typing
*/
public $request;
/**
* Gets mapped to Faster_Model
*/
public $model;
/**
* Gets mapped to Faster_View
*/
public $view;
/**
* Gets mapped to Faster_Data
*/
public $data;
} // end class Faster
/**
* This class handles our controller details and loads our page controllers with the framework
* objects hanging off of the $this object.
*
* @package Faster-Framework-API
*/
class Faster_Request extends Faster {
/**
* Private variable storing our group, parsed from the url.
*/
private $_request_group;
/**
* Private variable storing our action, parsed from the url.
*/
private $_request_action;
/**
* Private variable storing our interpreted params past the group and action parts of the url.
*/
private $_params;
/**
* Set the Group. Note that this is public only for when the framework is loaded, but isn't
* meant to be part of the API.
*
* @ignore
* @param string $sGroup Passes in a group portion of the URL when framework is loaded
*/
public function _setGroup($sGroup) {
$this->_request_group = $sGroup;
}
/**
* Set the Action. Note that this is public only for when the framework is loaded, but isn't
* meant to be part of the API.
*
* @ignore
* @param string $sAction Passes in an action portion of the URL when framework is loaded
*/
public function _setAction($sAction) {
$this->_request_action = $sAction;
}
/**
* Redirect the user's browser to another location. If 404 location, then show a 404 condition.
*
* @param string $sPath Where to redirect the user.
* @param bool $bTemp Defaults to 1; designates whether this is a permanent or temporary redirection.
*/
public function redirectRoute($sPath, $bTemp = 1) {
if ($sPath == 404) {
trigger_error('404');
$this->dispatchRoute(404);
exit;
}
$sPath = trim($sPath);
if (($bTemp == 1) and ($sPath != '')) {
header('HTTP/1.1 302 Moved Temporarily');
header("Location: $sPath");
exit;
}
if ($sPath != '') {
header('HTTP/1.1 301 Moved Permanently');
header("Location: $sPath");
exit;
}
}
/**
* Identify whether someone posted a form to the site.
*
* @return bool Whether someone posetd a form to the site via $_POST or $_FILES
*/
public function isPosted() {
if ((empty($_POST)) and (empty($_FILES))) {
return FALSE;
}
return TRUE;
}
/**
* Returns a session variable.
*
* @param string $sVar The session variable key
* @return string The session variable value
*/
public function getSessionVar($sVar) {
@ session_set_cookie_params(0, '/');
@ session_start();
$sResult = @ $_SESSION[session_id() . '_' . strtoupper($sVar)];
return $sResult;
}
/**
* Sets a session variable.
*
* @param string $sVar The session variable key to set
* @param string $sVal The session variable value to set
*/
public function setSessionVar($sVar, $sVal) {
@ session_set_cookie_params(0, '/');
@ session_start();
$_SESSION[session_id() . '_' . strtoupper($sVar)] = $sVal;
}
/**
* Returns the IP address of this user
*
* @return string IP Address
*/
public function getIP() {
$sTest = '';
if (isset($_SERVER['HTTP_X_FORWARD_FOR'])) {
$sTest = $_SERVER['HTTP_X_FORWARD_FOR'];
}
if ($sTest) {
$s = $sTest;
} else {
$s = $_SERVER['REMOTE_ADDR'];
}
$s = strip_tags($s);
if (function_exists('filter_var')) {
$s = filter_var($s,FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
}
return $s;
}
/**
* Returns the User Agent string of this user
*
* @return string User Agent string
*/
public function getUserAgent() {
$s = $_SERVER['HTTP_USER_AGENT'];
$s = strip_tags($s);
if (function_exists('filter_var')) {
$s = filter_var($s,FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
}
return $s;
}
/**
* Returns the referring page to the current web page.
*
* @return string The referring page
*/
public function getReferrer() {
$s = @ $_SERVER['HTTP_REFERER'];
$s = strip_tags($s);
if (function_exists('filter_var')) {
$s = filter_var($s,FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
}
return $s;
}
/**
* Another version of getReferrer, in case misspelled
*
* @return string The referring page
*/
public function getReferer() {
return $this->getReferrer();
}
/**
* Returns the actual base file path of the site
*
* @return string Base file path
*/
public function getPath(){
$sPath = str_replace('index.php','',$_SERVER['SCRIPT_NAME']);
$s = @ $_SERVER['REQUEST_URI'];
$sTest = substr($s, 0, strlen($sPath));
if ($sTest == $sPath) {
$s = substr($s, strlen($sPath));
}
if (empty($s)) {
$s = dirname(__FILE__);
}
return $s;
}
/**
* Converts the Group (aka "controller") portion of the URL from dashes into the form that is
* used by this framework in its ProperCase format.
*
* @param string $s The path. This is parsed from getPath() unless a URL is passed to it.
* @return string Group portion of the URL
*/
public function polishGroup($s) {
$s = ltrim($s, '/');
$s = rtrim($s, '/');
$s = str_replace('-',' ',$s);
$s = ucwords(strtolower($s));
$s = str_replace(' ','',$s);
$s = strrev($s);
$s = basename($s);
$s = strrev($s);
return $s;
}
/**
* Converts the Action portion of the URL from dashes into the form that is
* used by this framework in its ProperCase format.
*
* @param string $s The path. This is parsed from getPath() unless a URL is passed to it.
* @return string Action portion of the URL
*/
public function polishAction($s){
$s = ltrim($s, '/');
$s = rtrim($s, '/');
$s = str_replace('-',' ',$s);
$s = ucwords(strtolower($s));
$s = str_replace(' ','',$s);
if (strpos(' ' . $s,'/') === FALSE) {
$s = (empty($s)) ? 'Default' : $s;
return $s;
}
$asParts = explode('/',$s);
$s = @ $asParts[1];
$s = ucfirst($s);
$s = (empty($s)) ? 'Default' : $s;
if (substr($s,-4) == '.php') {
$s = str_replace('.php','',$s);
}
return $s;
}
/**
* Returns a guess of a group from the URL, which comes right after the base URL.
* It is not entirely accurate and cannot be trusted yet because the value may be a parameter,
* instead. Therefore, this routine is used by another class method, which is why it is marked
* private and not meant for the outside world.
*
* @return string Guessed group parameter of the URL
*/
private function _getGuessGroup(){
$s = $this->getPath();
return $this->polishGroup($s);
}
/**
* Returns a guess of the action part of the URL, which comes right after the detected group
* parameter. It is not entirel yaccurate and cannot be trusted yet because the value may be
* a parameter, instead. Therefore, this routine is used by another class method, which is why
* it is marked private and not meant for the outside world.
*
* @return string Guessed action parameter of the URL
*/
private function _getGuessAction(){
$s = $this->getPath();
return $this->polishAction($s);
}
/**
* Returns the actual group paramter of the URL, which comes right after the base part of the
* URL. Has been checked to see if this location exists with the controllers.
*
* @return string Actual group parameter of the URL.
*/
public function getGroup(){
if (isset($this->_request_group)) {
return $this->_request_group;
}
$sGGroup = $this->_getGuessGroup();
$sGAction = $this->_getGuessAction();
$F = $this->core->base();
$sTestPath1 = $F . '/app/_controllers/' . $sGGroup . '/c' . $sGAction . '.php';
$sTestPath2 = $F . '/app/_controllers/' . $sGGroup . '/cDefault.php';
if ((file_exists($sTestPath1)) or (file_exists($sTestPath2))) {
$this->_request_group = $sGGroup;
return $sGGroup;
}
$this->_request_group = 'Default';
return 'Default';
}
/**
* Returns the actual action paramter of the URL, which comes right after the group part of the
* URL. Has been checked to see if this location exists with the controllers.
*
* @return string Actual action parameter of the URL.
*/
public function getAction(){
if (isset($this->_request_action)) {
return $this->_request_action;
}
$sGGroup = $this->_getGuessGroup();
$sGAction = $this->_getGuessAction();
$F = $this->core->base();
$sTestPath1 = $F . '/app/_controllers/' . $sGGroup . '/c' . $sGAction . '.php';
$sTestPath2 = $F . '/app/_controllers/' . $sGGroup . '/cDefault.php';
if ((file_exists($sTestPath1)) or (file_exists($sTestPath2))) {
if (file_exists($sTestPath1)) {
$this->_request_action = $sGAction;
return $sGAction;
} else {
$this->_request_action = 'Default';
return 'Default';
}
}
$this->_request_action = 'Default';
return 'Default';
}
public function getParam($nIndex) {
$asParams = $this->getParams();
$sVal = @ $asParams[$nIndex];
return $sVal;
}
/**
* Returns the parameters of the URL that come after the group and action. Has been checked
* to prove that these parameters are not part of the controller path.
*
* @return array An Array of string parameters of the URL, if present, beyond the action parameter of the URL.
*/
public function getParams(){
if (isset($this->_params)) {
return $this->_params;
}
global $argv;
$sTest = @ $argv[1];
$bCLI = (!empty($sTest));
if ($bCLI) {
$asParams = $argv;
array_shift($asParams);
array_shift($asParams);
foreach($asParams as $sKey => $sVal) {
if (substr($sVal, 0, 2) == '--') {
$sVal = preg_replace('/^--/','',$sVal);
$asParams[$sKey] = $sVal;
}
if ((substr($sVal, 0, 1) == '"') and (substr($sVal, -1, 1) == '"')) {
$sVal = ltrim($sVal, '"');
$sVal = rtrim($sVal, '"');
$asParams[$sKey] = $sVal;
} else if ((substr($sVal, 0, 1) == "'") and (substr($sVal, -1, 1) == "'")) {
$sVal = ltrim($sVal, "'");
$sVal = rtrim($sVal, "'");
$asParams[$sKey] = $sVal;
}
}
$this->_params = $asParams;
return $asParams;
}
$F = $this->core->base();
$sGroup = $this->getGroup();
$sAction = $this->getAction();
$s = $this->getPath();
if (strpos(' ' . $s, '?') > 0) {
$nPos = strpos($s, '?');
$s = substr($s, 0, $nPos);
}
$s = ltrim($s, '/');
$s = rtrim($s, '/');
$asParts = explode('/', $s);
$sPossibleGroup = @ $asParts[0];
$sPossibleAction = @ $asParts[1];
$sPossibleGroup = str_replace('-',' ',$sPossibleGroup);
$sPossibleGroup = ucwords(strtolower($sPossibleGroup));
$sPossibleGroup = str_replace(' ','',$sPossibleGroup);
$sPossibleGroup = ucfirst($sPossibleGroup);
$sPossibleAction = str_replace('-',' ',$sPossibleAction);
$sPossibleAction = ucwords(strtolower($sPossibleAction));
$sPossibleAction = str_replace(' ','',$sPossibleAction);
$sPossisPossibleActionbleGroup = ucfirst($sPossibleAction);
if (file_exists($F . '/app/_controllers/' . $sPossibleGroup)) {
array_shift($asParts);
}
if (file_exists($F . '/app/_controllers/' . $sPossibleGroup . '/c' . $sPossibleAction . '.php')) {
array_shift($asParts);
}
if ($sAction == 'Default') {
$sScript = @ $_SERVER['SCRIPT_NAME'];
$sRequest = @ $_SERVER['REQUEST_URI'];
$sScript = str_replace('/index.php','',$sScript);
$sRequest = str_replace($sScript,'',$sRequest);
$sRequest .= '?';
$asParts = explode('?',$sRequest);
$sRequest = $asParts[0];
$sRequest = ltrim($sRequest, '/');
$sRequest = rtrim($sRequest,'/');
if (strlen($sRequest) == 0) {
return array();
}
$asParts = explode('/',$sRequest);
if ($sGroup != 'Default') {
array_shift($asParts);
}
}
$this->_params = $asParts;
return $asParts;
}
/**
* Returns the $_GET -- added to keep the framework consistent.
*
* @return array The $_GET parameters
*/
public function getVars(){
foreach($_GET as $sKey => $sVal){
$sVal = urldecode($sVal);
$_GET[$sKey] = trim($sVal);
}
return $_GET;
}
/**
* Returns the $_POST -- added to keep the framework consistent.
*
* @return array The $_POST parameters
*/
public function getPostedVars(){
foreach($_POST as $sKey => $sVal){
$_POST[$sKey] = trim($sVal);
}
return $_POST;
}
/**
* Returns the $_SERVER -- added to keep the framework consistent.
*
* @return array The $_SERVER parameters
*/
public function getServerVars(){
return $_SERVER;
}
/**
* Returns the $_GET by key param
*
* @param string $sKey The key
* @param boolean $bStripTags Whether to apply strip_tags(). Defaults to TRUE.
* @return string The value
*/
public function getVar($sKey, $bStripTags = TRUE){
$sVal = @ $_GET[$sKey];
if ($bStripTags) {
$sVal = strip_tags($sVal);
}
$sVal = urldecode($sVal);
$sVal = trim($sVal);
return $sVal;
}
/**
* Returns the $_POST by key param
*
* @param string $sKey The key
* @param boolean $bStripTags Whether to apply strip_tags(). Defaults to TRUE.
* @return string The value
*/
public function getPostedVar($sKey, $bStripTags = TRUE){
$sVal = @ $_POST[$sKey];
if ($bStripTags) {
$sVal = strip_tags($sVal);
}
$sVal = trim($sVal);
return $sVal;
}
/**
* Returns the $_SERVER by key param
*
* @param string $sKey The key
* @return string The value
*/
public function getServerVar($sKey){
$sVal = @ $_SERVER[$sKey];
return $sVal;
}
/**
* Block the Default page controller Default/cDefault.php from being able to process
* ending URL parameters, and send them to 404 pages instead.
*
* Drop a call to this in app/_controllers/Default/cDefault.php at the top and it will
* give you 404 pages. This gives you better debugging if you're actually wanting 404.
*
*/
public function blockDefaultURLParams(){
$sScript = @ $_SERVER['SCRIPT_NAME'];
$sRequest = @ $_SERVER['REQUEST_URI'];
$sScript = str_replace('/index.php','',$sScript);
$sRequest = str_replace($sScript,'',$sRequest);
if (strpos(' ' . $sRequest,'?')>0) {
$asParts = explode('?',$sRequest);
$sRequest = $asParts[0];
}
if ($sRequest != '/') {
$this->request->redirectRoute(404);
}
}
/**
* Looks at the incoming URL, or the $sWhichController variable, and routes the site workflow
* to that controller. Can also be used for 404 handling. As well, can also be used to dispatch
* a route but not stop (exit(0)) when done. An example of possible URLs and their routing are:
*
* <code>
* http://example.com/ = app/_controllers/Default/cDefault.php
* http://example.com/my-blog-post = app/_controllers/Default/cDefault.php
* http://example.com/articles/my-blog-post = app/_controllers/Articles/cDefault.php
* http://example.com/membership-system/get-user = app/_controllers/MembershipSystem/cGetUser.php
* http://example.com/members/get-user/400 = app/_controllers/Members/cGetUser.php
* http://example.com/members/get-user/400/500 = app/_controllers/Members/cGetUser.php
* </code>
*
* Therefore, the typical URL is either in the format:
*
* <code>
* http://example.com/
* http://example.com/{PARAMETER 1..n}
* http://example.com/{GROUP}/{PARAMETER 1...n}
* http://example.com/{GROUP}/{ACTION}
* http://example.com/{GROUP}/{ACTION}/{PARAMETER 1...n}
* </code>
*
* The Default controller group is a folder called Default.
* The Default controller action is a file called cDefault.php.
* The actual controller file begins with a 'c' prefix because it helps us in tabbed text editors
* so as not to confuse these with models or views.
* All dashes in the group and action slots cause the group or action to be ProperCased with
* dashes removed, but this is not applied to the parameters that follow that.
* It is possible to have a group like Articles, but then map to cDefault.php for action in order to
* absorb the parameters that may follow.
* When nothing comes after the base of the site, like http://example.com/, the site defaults to
* using the controller path app/_controllers/Default/cDefault.php
*
* Note underscores in variables in this function are so that the controller doesn't inherit
* variables. (I mean, it does, but it would have to use underscore vars.) This is actually a
* failsafe in case this function gets edited. We will do unset() on all variables possible.
*
* Note also an alternative action path. Instead of cDefault.php, you can name it the same as the
* group path. So, if you have /about for the URL, then you could use About/cAbout.php for the
* controller path. This helps when working with multiple files at once in a text editor, where
* you won't confuse all the cDefault.php files together.
*
* @param string $_sWhichController By default, the front controller leaves this empty and therefore
* parses the URL for that value. However, this can be overridden. Pass a value of 404 here and the
* framework will try to send 404 headers and try to load either a 404.php or 404.html file, if found
* or merely stop with the 404 headers.
* @param string $_bStopWhenRouted By default, the front controller stops code execution after the
* routing, which means it runs through the controller code and then stops. However, this can be
* overridden.
*/
public function dispatchRoute($_sWhichController = '', $_bStopWhenRouted = TRUE){
$_F = $this->core->base();
if ($_sWhichController == 404) {
if (!headers_sent()) {
header('HTTP/1.1 404 Not Found');
header('Status: 404 Not Found');
}
if (file_exists($_F . '/404.php')) {
unset($_sWhichController);
unset($_bStopWhenRouted);
require_once($_F . '/404.php');
} else {
@ include($_F . '/404.html');
}
die();
}
if (empty($_sWhichController)) {
$_sGroup = $this->getGroup();
$_sAction = $this->getAction();
if (!file_exists($_F . '/app/_controllers')) {
trigger_error('Your folder layout is missing a app/_controllers folder', E_USER_ERROR);
}
if (!file_exists($_F . '/app/_controllers/Default')) {
trigger_error('Your folder layout is missing a app/_controllers/Default controller folder', E_USER_ERROR);
}
if (!file_exists($_F . '/app/_controllers/Default/cDefault.php')) {
trigger_error('Your folder layout is missing a app/_controllers/Default/cDefault.php controller file', E_USER_ERROR);
}
$_sPath = $_F . '/app/_controllers/' . $_sGroup . '/c' . $_sAction . '.php';
if (!file_exists($_sPath)) {
$_sPath = $_F . '/app/_controllers/' . $_sGroup . '/c' . $_sGroup . '.php';
if (!file_exists($_sPath)) {
trigger_error('Your folder layout is missing a "' . $_sPath . '" controller file', E_USER_ERROR);
}
}
unset($_sWhichController);
unset($_F);
unset($_sGroup);
unset($_sAction);
require_once($_sPath);
} else {
// syntax on $_sWhichController would be like 'Test/MeOut'
$_s = $_sWhichController;
unset($_sWhichController);
$_s = str_replace('.php','',$_s);
$_s = ltrim($_s, '/');
$_s = rtrim($_s, '/');
$_s .= '.x';
$_sBase = basename($_s);
$_s = str_replace('/' . $_sBase,'/c' . $_sBase,$_s);
$_s = str_replace('.x','',$_s);
require_once($_F . '/app/_controllers/' . $_s . '.php');
}
if ($_bStopWhenRouted) {
exit(0);
}
}
/**
* Returns an unencrypted cookie value.
*
* @param string $sLabel The key of that cookie value
* @return string The value of that cookie by key
*/
public function readCookie($sLabel) {
$sCookiePrefix = @ $this->core->config['COOKIE_PREFIX'] . '_';
$sLabel = $sCookiePrefix . strtolower($sLabel);
if (isset($_COOKIE[$sLabel])) {
return $_COOKIE[$sLabel];
} else {
return '';
}
}
/**
* Returns an unencrypted cookie value that was encrypted.
*
* @param string $sLabel The key of that cookie value
* @return string The value of that cookie by key
*/
public function readEncryptedCookie($sLabel) {
$sValue = $this->readCookie($sLabel);
$sValue = $this->_decryptData($sValue);
return $sValue;
}
/**
* Writes a cookie value. This is a session cookie, not persistent.
*
* @param string $sLabel The key of that cookie value
* @param string $sValue The value of that cookie by key
*/
public function writeCookie($sLabel, $sValue) {
$sCookiePrefix = @ $this->core->config['COOKIE_PREFIX'] . '_';
$sPath = '/';
if ($sValue == '') {
$this->deleteCookie($sLabel);
} else {
setcookie(strtolower($sCookiePrefix . $sLabel), $sValue, 0, $sPath);
}
}
/**
* Writes a cookie value. This is a persistent, not session, cookie. The persistence is for
* 365 days.
*
* @param string $sLabel The key of that cookie value
* @param string $sValue The value of that cookie by key
* @param integer $nDays The number of days to keep the cookie. Defaults to 365 days.
*/
public function writePersistentCookie($sLabel, $sValue,$nDays = 365) {
$sCookiePrefix = @ $this->core->config['COOKIE_PREFIX'] . '_';
$sPath = '/';
if (!headers_sent()) {
header ('Cache-control: private'); // IE 6 Fix.
}
setcookie(strtolower($sCookiePrefix . $sLabel), $sValue, time()+60*60*24*$nDays, $sPath);
}
/**
* Encrypts and writes an encrypted cookie value. This is a session cookie, not persistent.
*
* @param string $sLabel The key of that cookie value
* @param string $sValue The value of that cookie by key
*/
public function writeEncryptedCookie($sLabel, $sValue) {
$sValue = $this->_encryptData($sValue);
$this->writeCookie($sLabel, $sValue);
}
/**
* Encrypts and writes an encrypted cookie value. This is a persistent cookie.
*
* @param string $sLabel The key of that cookie value
* @param string $sValue The value of that cookie by key
* @param integer $nDays The number of days to keep the cookie. Defaults to 365 days.
*/
public function writeEncryptedPersistentCookie($sLabel, $sValue,$nDays = 365) {
$sValue = $this->_encryptData($sValue);
$this->writePersistentCookie($sLabel, $sValue,$nDays);
}
/**
* Given a key, this will delete a cookie.
*
* @param string $sLabel the key of that cookie
*/
public function deleteCookie($sLabel) {
$sCookiePrefix = @ $this->core->config['COOKIE_PREFIX'] . '_';
$sPath = '/';
$sLabel = strtolower($sLabel);
setcookie ($sCookiePrefix . $sLabel, ' ', 0, $sPath);
setcookie ($sCookiePrefix . $sLabel, '', time() - 3600, $sPath);
}
/**
* This private class method decrypts our data.
*
* @param string $sData String of encrypted data.
* @return string Unencrypted data.
*/
private function _decryptData($sData){
$sCookiePrefix = @ $this->core->config['COOKIE_PREFIX'];
$sPrivateKey = md5($sCookiePrefix);
if (trim($sData) == '') {
return '';
}
// REVERSE THE PRIVATE KEY
$sPrivateKey = strrev($sPrivateKey);
// URL DECODE THE DATA
$sData = urldecode($sData);
// BASE64 DECODE THE DATA
$sData = base64_decode($sData);
if (function_exists('mcrypt_encrypt')) {
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$sData = mcrypt_decrypt(MCRYPT_RIJNDAEL_256,$sPrivateKey,$sData,
MCRYPT_MODE_ECB, $iv);
} else {
// XOR EACH CHAR BY EACH CHAR IN A PRIVATE KEY (PRIVATE KEY REPEATED
// OVER AND OVER AGAIN)
$j=0; $nLen = strlen($sData);
for($i = 0; $i < $nLen; $i++){
$sData[$i] = chr(ord($sData[$i]) ^ ord($sPrivateKey[$j]));
$j++;
$j = ($j >= strlen($sPrivateKey)) ? 0 : $j;
}
// REVERSE OUR DATA
$sData = strrev($sData);
}
// RETURN OUR DATA
return trim($sData);
}
/**
* This private class method encrypts our data.
*
* @param string $sData String of unencrypted data.
* @return string Encrypted data.
*/
private function _encryptData($sData){
$sCookiePrefix = @ $this->core->config['COOKIE_PREFIX'];
$sPrivateKey = md5($sCookiePrefix);
if (trim($sData) == '') {
return '';
}
// REVERSE THE PRIVATE KEY
$sPrivateKey = strrev($sPrivateKey);
if (function_exists('mcrypt_encrypt')) {
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$sData = mcrypt_encrypt(MCRYPT_RIJNDAEL_256,$sPrivateKey,$sData,
MCRYPT_MODE_ECB,$iv);
} else {
// REVERSE OUR DATA
$sData = strrev($sData);
// XOR EACH CHAR BY EACH CHAR IN A PRIVATE KEY (PRIVATE KEY REPEATED
// OVER AND OVER AGAIN)
$j=0; $nLen = strlen($sData);
for($i = 0; $i < $nLen; $i++){
$sData[$i] = chr(ord($sData[$i]) ^ ord($sPrivateKey[$j]));
$j++;
$j = ($j >= strlen($sPrivateKey)) ? 0 : $j;
}
}
// BASE64 OUR DATA
$sData = trim(base64_encode($sData));
// FIX THE = PROBLEM IN THAT RESULT
$sData = str_replace('=','',$sData);
// URL ENCODE THE DATA
$sData = urlencode($sData);
// RETURN THE DATA
return $sData;
}
} // end class Faster_Request
/**
* A class for loading model objects and enabling them with a powerful $this object, just like our
* controllers have.
*
* @package Faster-Framework-API
*/