/
Router.php
1007 lines (817 loc) · 25.6 KB
/
Router.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
/**
* @package Aspen_Framework
* @subpackage System
* @author Michael Botsko
* @copyright 2009 Trellis Development, LLC
* @since 1.0
*/
/**
* Shortcut to return an instance of our original app
* @return object
*/
function &router(){
return app()->router;
}
/**
* Manages urls and relation to our application
* @package Aspen_Framework
*/
class Router {
/**
* @var array $_loaded_languages Remembers which langs have already been loaded
* @access private
*/
protected $_loaded_languages = array();
/**
* @var array $_original_map Holds the original mapped data
* @access private
*/
protected $_original_map = array();
/**
* @var string $_selected_module Lists the currently selected module
* @access private
*/
protected $_selected_module = false;
/**
* @var string $_selected_method Lists the currently selected method
* @access private
*/
protected $_selected_method = false;
/**
* @var array $_selected_arguments An array of arguments to pass to a function
* @access private
*/
protected $_selected_arguments = array();
/**
* @var array $map Holds our array of mapped URL routes
* @access private
*/
protected $map = array('module'=>false,'method'=>false,'bits'=>false);
/**
* Constructor
* @return Router
* @access private
*/
public function __construct(){
if(config()->get('enable_authentication_support')){
// determine user's authentication status
user()->determineUserAuthentication();
}
// map the url elements and then identify the module/method to load
$this->mapRequest();
$this->loadRequestedPagePath();
}
/**
* Maps URI elements to an internal array - either GET or clean urls
* @access private
*/
protected function mapRequest(){
$bits = array();
// if mod_rewrite enabled, and request doesn't look like a non-rewrite request
if(config()->get('enable_mod_rewrite') && strpos(app()->server->getQueryString('REQUEST_URI'), '.php?') === false){
// we force the entire url as replacement because
// an interface app name may be the same as a module
// and we don't want to remove both, just one
$replace = array($this->appUrl() . (LS != '' ? '/'.LS : '' ));
if($this->getPath() != '/'){
// try also to replace with app path minus the LS. If someone is masking the LS
// then it may not be seen in the uri.
$replace[] = $this->appUrl();
$replace[] = $this->getPath();
}
$uri = $to_map = str_replace($replace, '', $this->domainUrl() . app()->server->getQueryString('REQUEST_URI'));
$uri = explode('/', $uri);
// If route mapping fails, then we need to parse the url for the default config
if(!$this->applyRouteMap($to_map)){
$this->map['module'] = isset($uri[1]) ? $this->stripQuery($uri[1]) : false;
$this->map['method'] = isset($uri[2]) ? $this->stripQuery($uri[2]) : false;
}
// loop additional bits to pass to our arguments
if(!$this->map['bits']){
for($i = 3; $i < count($uri); $i++){
$bits[] = $this->stripQuery($uri[$i]);
}
$this->map['bits'] = $bits;
}
} else {
$this->map['module'] = get()->getElemId('module');
$this->map['method'] = get()->getElemId('method');
// loop additional bits to pass to our arguments
$get = app()->params->getRawSource('get');
if(is_array($get)){
foreach($get as $key => $value){
if($key != 'module' && $key != 'method'){
$bits[$key] = $this->stripQuery(get()->getRaw($key));
}
}
$this->map['bits'] = $bits;
}
}
// we need to remove any left over query string from malformed mod_rewrite query
$this->map['method'] = $this->stripQuery($this->map['method']);
// If method provided in place of module, use it with default module
if(empty($this->map['method'])){
$modules = array_keys(app()->getModuleRegistry());
if(!in_array(ucwords($this->map['module']), $modules)){
$this->map['method'] = $this->map['module'];
$this->map['module'] = config()->get('default_module');
}
}
// append interface to module
if(!empty($this->map['module'])){
$this->map['module'] .= (LOADING_SECTION != '' ? '_'.LOADING_SECTION : '');
}
$this->_original_map = $this->map;
}
/**
* Removes any remaining query string from manual URI processing.
* @param string $str
* @return string
*/
protected function stripQuery($str){
return preg_replace('/\?(.*)/', '', $str);
}
/**
* Applies custom mapping routes to URLs. If a match is found, default is not applied.
* @param string $path
* @access private
* @return boolean
*/
protected function applyRouteMap($path){
$path = $this->stripQuery($path);
$routes = config()->get('routes');
$map = false;
$matched_keys = array();
$matches = array();
if(!empty($path) && is_array($routes)){
foreach($routes as $route => $map_to){
if(isset($map_to['regex']) && $map_to['regex']){
preg_match_all($route, $path, $matches);
if(is_array($matches) && isset($matches[0]) && !empty($matches[0])){
// Check for any variable matches to append
for($a = 1; $a <= (count($matches)-1); $a++){
// replace any placeholders in the map_to array
foreach($map_to as $key => $ph){
if($ph === '$'.$a){
$matched_keys[] = $a;
if(count($matches[$a]) == 1){
$map_to[$key] = $matches[$a][0];
} else {
// we have an array of matches
// @todo improve support for arrays: bug 1476
if(isset($matches[$a][0])){
$map_to[$key] = $matches[$a][0];
}
}
}
}
// append any remaining matches as bits
for($a = 1; $a <= (count($matches)-1); $a++){
if(!in_array($a, $matched_keys)){
if(!empty($matches[$a][0])){
// @todo improve support for arrays: bug 1476
$map_to['bits'][] = $matches[$a][0];
}
}
}
}
$map = $map_to;
break;
}
} else {
if($path === $route){
$map = $routes[$path];
}
}
app()->log->section('Routes Mapping');
app()->log->write($matches);
}
// proper map was found, set the information and return
if(is_array($map)){
$this->map['module'] = isset($map['module']) ? $map['module'] : false;
$this->map['method'] = isset($map['method']) ? $map['method'] : false;
$this->map['bits'] = isset($map['bits']) ? $map['bits'] : false;
return true;
}
}
return false;
}
/**
* First checks for what path has been requested. Then, it
* checks for approval for that request (permissions, install, etc).
* @access private
*/
protected function loadRequestedPagePath(){
// requested
$req_module = $this->identifyRequestedModuleForLoad();
$req_method = $this->identifyRequestedMethodForLoad();
$acc_module = false;
$acc_method = false;
// Check if anonymous access is allowed
if(!user()->allowAnonymous($req_module, $req_method, LS)){
$acc_module = $this->identifyAcceptedModuleForLoad();
$acc_method = $this->identifyAcceptedMethodForLoad();
}
// Override if access to requested path not allowed
$acc_module = $acc_module ?: $req_module;
$acc_method = $acc_method ?: $req_method;
$this->_selected_module = ucfirst($acc_module);
$this->_selected_method = $acc_method;
}
/**
* Identifies the module that has been requested, but not yet approved
* @return string
* @access private
*/
protected function identifyRequestedModuleForLoad(){
return $this->map['module'] ?: user()->getUserDefaultModule();
}
/**
* Identifies the method that has been requested, but not yet approved
* @return string
* @access private
*/
protected function identifyRequestedMethodForLoad(){
return $this->map['method'] ?: config()->get('default_method');
}
/**
* Identifies which module needs to be loaded
* @return string
* @access public
* @todo clean this up now that loadRequestedPagePath exists
*/
protected function identifyAcceptedModuleForLoad(){
if(strtolower(get_class(app())) == "app"){
if(app()->isInstalled()){
// do a quick check to see if the user is logged in or not
// we need to create our own auth check, as the user module is not loaded at this point
if(user()->isLoggedIn()){
$default = $this->map['module'] ? $this->map['module'] : false;
} else {
$default = 'Users' . (LOADING_SECTION ? '_' . LOADING_SECTION : false);
}
} else {
$default = config()->get('default_module_no_config');
}
} else {
$default = get_class(app());
}
return $default;
}
/**
* Identifies which method needs to be loaded
* @param string $default
* @return string
* @access private
* @todo clean this up now that loadRequestedPagePath exists
*/
protected function identifyAcceptedMethodForLoad(){
$default = 'view';
// do a basic login check as user module is not loaded at this point
if(user()->isLoggedIn()){
$default = $this->map['method'];
$default = $default ? $default : config()->get('default_method');
}
elseif($this->map['method'] == 'authenticate' || $this->map['method'] == 'forgot'){
$default = $this->map['method'];
} else {
if(app()->isInstalled()){
$default = 'login';
}
}
return $default;
}
/**
* Returns the currently selected module
* @return string
* @access public
*/
public function module(){
return $this->_selected_module;
}
/**
* Returns the currently selected method
* @return string
* @access public
*/
public function method(){
return $this->_selected_method;
}
/**
* Returns all current method arguments
* @return string
* @access private
*/
public function getSelectedArguments(){
return $this->_arg;
}
/**
* Determines the parent of the current module, mainly for navigation purposes.
* @return string
* @access public
*/
public function getParentModule(){
$module = $this->module();
if(method_exists(app()->{$this->module()}, 'whosYourDaddy')){
$daddy = app()->{$this->module()}->whosYourDaddy();
$module = empty($daddy) ? $module : $daddy;
}
return $module;
}
/**
* Loads the lanuage file for the current interface
* @access private
*/
public function loadInterfaceLanguage($interface = false){
$languages = array();
$lang_setting = config()->get('language');
// load the interface language library
$path = $interface ? APPLICATION_PATH.DS.strtolower($interface) : INTERFACE_PATH;
$lang_path = $path . DS . 'language' . DS . $lang_setting . '.php';
app()->log->write('Seeking interface language library ' . $lang_path);
if(file_exists($lang_path)){
include($lang_path);
app()->log->write('Including interface language library ' . $lang_path);
if(isset($lang[LS])){
$languages = array_merge($languages, $lang[LS]);
}
if(isset($lang['*'])){
$languages = array_merge($languages, $lang['*']);
}
}
template()->loadLanguageTerms($languages);
}
/**
* Loads the lanuage file for the current module
* @access private
*/
public function loadModuleLanguage($module = false, $interface = false){
$languages = array();
$lang_setting = config()->get('language');
// load the module-specific language library
if(!in_array($module.'_'.$interface, $this->_loaded_languages)){
$module = $this->cleanModule($module);
$module_lang_path = $this->getModulePath($module, $interface).DS. 'language' . DS . $lang_setting.'.php';
app()->log->write('Seeking module language library ' . $module_lang_path);
if(file_exists($module_lang_path)){
include($module_lang_path);
app()->log->write('Including module language library ' . $module_lang_path);
if(isset($lang[LS])){
$languages = array_merge($languages, $lang[LS]);
}
if(isset($lang['*'])){
$languages = array_merge($languages, $lang['*']);
}
}
$this->_loaded_languages[] = $module.'_'.$interface;
template()->loadLanguageTerms($languages);
}
}
/**
* Loads all language files for user-created libraries.
* @access private
*/
public function loadLibraryLanguages(){
$langs = array();
$libs = app()->getLoadedLibraries();
foreach($libs as $lib){
if(isset($lib['module']) && is_object($lib['module'])){
$langs[(string)$lib['module']->classname] = (string)$lib['module']->classname;
}
}
if(!empty($langs)){
foreach($langs as $module){
$this->loadModuleLanguage($module);
}
}
return $langs;
}
/**
* Calls the module/method with arguments from the url
* @access public
*/
public function loadFromUrl(){
// If user is logged in, but does not have access to this interface app
if(user()->isLoggedIn() &&
$this->method() != 'login' && $this->method() != 'authenticate'
){
if(!user()->userHasInterfaceAccess()){
user()->logout();
$this->redirect('users/login');
}
}
// load the interface language file
if(config()->get('enable_languages')){
$this->loadInterfaceLanguage();
}
if($this->method() && user()->userHasAccess()){
// load the module language file
if(config()->get('enable_languages')){
$this->loadLibraryLanguages();
$this->loadModuleLanguage();
}
// set the function arguments for the method
$i = 1;
foreach($this->map['bits'] as $bit){
$this->_selected_arguments[$i] = $bit;
$i++;
}
app()->log->write('Looking for Module: ' . $this->module() . '->' . $this->method());
if(isset(app()->{$this->module()})){
if(method_exists(app()->{$this->module()}, $this->method())){
app()->log->write('Running Module: ' . $this->module() . '->' . $this->method());
// Call the actual class method for our current page, and pass all arguments to it
call_user_func_array(array(app()->{$this->module()}, $this->method()), $this->_selected_arguments);
} else {
app()->log->write('Method doesn\'t exist in module. ' . $this->module() . '->' . $this->method());
$this->header_code(404);
template()->setLayout('404');
template()->display();
exit;
}
} else {
app()->log->write('Module not loaded or doesn\'t exist. ' . $this->module());
$this->header_code(404);
template()->setLayout('404');
template()->display();
exit;
}
} else { // not authorized
app()->log->write('User not authorized. ' . $this->module());
$this->header_code(403);
template()->setLayout('403');
template()->display();
exit;
}
}
/**
* Check for an argument index
* @param integer $index
* @return mixed
* @access private
*/
public function arg($index){
return isset($this->_selected_arguments[$index]) ? $this->_selected_arguments[$index] : false;
}
/**
* Return an array of current arguments
* @return array
* @access public
*/
public function getMappedArguments(){
return $this->map['bits'];
}
/**
* Returns a full URL path for the current page, query string and all
* @return string
* @access public
*/
public function fullUrl(){
$url = $this->domainUrl();
$url .= app()->server->getQueryString('REQUEST_URI');
$url = strip_tags(urldecode($url));
return $url;
}
/**
* Returns the original map data, usually useful if there's
* an alias url
* @return array
*/
public function getOriginalMap(){
return $this->_original_map;
}
/**
* Returns the absolute url to the aspen installation path.
* @access public
* @return string
*/
public function appUrl(){
$url = $this->domainUrl();
$url .= config()->get('application_url') ? config()->get('application_url') : $this->getPath();
return $url;
}
/**
* Returns the current port number
* @return integer
* @access public
*/
public function port(){
return app()->server->getInt('SERVER_PORT');
}
/**
* Returns the protocol / domain name only
* @access public
* @return string
*/
public function domainUrl(){
$url = $this->port() == 443 ? 'https://' : 'http://';
$url .= app()->server->getServerName('SERVER_NAME');
if($this->port() != 80 && $this->port() != 443){
$url .= ':'.$this->port();
}
return $url;
}
/**
* Attempts to return the post-domain name path to our application (minus interface)
* @return string
* @access public
*/
public function getPath(){
$adjusted_app_path = str_replace('\\', '/', APPLICATION_PATH);
$adjusted_doc_root = str_replace('\\', '/', app()->server->getPath('DOCUMENT_ROOT'));
$doc_strlen = strlen($adjusted_doc_root);
if(substr($adjusted_doc_root, $doc_strlen - 1, $doc_strlen) == '/'){
$adjusted_doc_root = substr($adjusted_doc_root, 0, $doc_strlen - 1);
}
// compare the first characters of both path vars,
// if they don't match then we can't trust as true path
$ar = substr($adjusted_app_path, 0, 7);
$dr = substr($adjusted_doc_root, 0, 7);
// attempt to determine path replacing doc root with domain
if($adjusted_doc_root && $ar == $dr){
$uri = str_replace($adjusted_doc_root, '', $adjusted_app_path);
} else {
// if no mod_rewrite, we need to handle the paths appropriately
if(config()->get('enable_mod_rewrite') && strpos(app()->server->getQueryString('REQUEST_URI'), '.php?') === false){
$redirected = stripslashes(get()->getQueryString('redirected'));
$interface = LS ? LS : '';
$replace = array();
if(!empty($interface)){
$replace[] = '/'.$interface;
}
if(!empty($redirected)){
$replace[] = '/'.$redirected;
}
$uri = str_replace($replace, '', urldecode(app()->server->getQueryString('REQUEST_URI')));
$uri = $this->stripQuery($uri);
} else {
$no_qs_url = str_replace('?' . app()->server->getQueryString('QUERY_STRING'), '', app()->server->getQueryString('REQUEST_URI'));
$interface = LS ? LS : '';
$uri = str_replace(array("index.php", '/'.$interface), "", $no_qs_url);
}
}
$uri = $uri == '/' ? false : $uri;
return $uri;
}
/**
* Returns an absolute url to the current interface application.
* @param string $interface
* @return string
* @access public
*/
public function interfaceUrl($interface = false){
$interface = ($interface !== false ? $interface : LS);
$interface = ($interface == "app") ? "" : $interface;
return $this->appUrl() . (empty($interface) ? '' : '/' . $interface);
}
/**
* Returns the url to the file upoads directory
* @return string
* @access public
*/
public function uploadsUrl(){
$browser_url = config()->get('upload_browser_path');
if(!$browser_url){
$browser_url = str_replace(APPLICATION_PATH, $this->appUrl(), config()->get('upload_server_path'));
}
return $browser_url;
}
/**
* Returns the static content path if set in config, otherwise just interfaceUrl
* @return string
* @access public
*/
public function staticUrl($interface = false){
if(config()->get('static_content_url')){
return config()->get('static_content_url');
} else {
$interface = $interface !== false ? $interface : LS;
if(is_array(config()->get('interface_global_folder_replace'))){
$replace = config()->get('interface_global_folder_replace');
if(array_key_exists($interface, $replace)){
$interface = $replace[$interface];
}
}
return $this->interfaceUrl($interface);
}
}
/**
* Returns an absolute URL to the module folder
* @param string $module_name
* @return string
* @access public
*/
public function moduleUrl($module_name = false){
$module = $this->cleanModule($module_name);
$registry = app()->moduleRegistry(false, $module);
return isset($registry->folder) ? $this->appUrl() . '/modules/' . $registry->folder : false;
}
/**
* Encodes a string for use in the url
* @param string $var
* @access public
* @return string
*/
public function encodeForRewriteUrl($var, $lc = false){
$var = str_replace("?", "-question-", $var);
$var = str_replace("/", "-slash-", $var);
$var = str_replace("&", "-and-", $var);
$var = str_replace(" ", "_", $var);
$var = urlencode($var);
$var = ($lc ? strtolower($var) : $var);
return $var;
}
/**
* Decodes a string for use in the url
* @param string $var
* @access public
* @return string
*/
public function decodeForRewriteUrl($var){
$var = urldecode($var);
$var = str_replace("-question-", "?", $var);
$var = str_replace("-slash-", "/", $var);
$var = str_replace("-and-", "&", $var);
$var = str_replace("_", " ", $var);
return $var;
}
/**
* Returns the server path to our module
* @return string
* @access public
*/
public function getModulePath($module = false, $interface = false){
$module = $this->cleanModule($module);
$registry = app()->moduleRegistry(false, $module, $interface);
if(isset($registry->folder)){
return MODULES_PATH . DS . $registry->folder;
}
return false;
}
/**
* Returns the module name without the interface in case it was provided
* that way.
*
* However, do not do so if the interface is empty, because then just the
* undercore would be removed.
*
* @param string $module
* @param string $interface
* @return string
*/
public function cleanModule($module = false, $interface = false){
$module = $module ? $module : $this->module();
$interface = $interface ? $interface : LOADING_SECTION;
return empty($interface) ? $module : str_replace('_'.ucwords($interface), '', $module);
}
/**
* Answers whether or not the user is in a specific location
* @param string $module
* @param string $method
* @return boolean
* @access public
*/
public function here($path, $type = 'method'){
$here = false;
$u = new Url();
$r = $u->parseNamespacePath($path, $type);
$r['module'] = $r['module'] . ($r['interface'] ? '_' . $r['interface'] : false);
$selected_module = $this->module();
if(isset(app()->{$this->module()})){
if(method_exists(app()->{$this->module()}, 'whosYourDaddy')){
$daddy = app()->{$this->module()}->whosYourDaddy();
$selected_module = empty($daddy) ? $selected_module : $daddy;
}
}
$replacements = config()->get('interface_global_folder_replace');
foreach($replacements as $key => $replacement){
$selected_module = str_replace("_".$replacement, $key, strtolower($selected_module));
}
if($r['module'] && strtolower($selected_module) == strtolower($r['module'])){
if($r['method']){
if(is_array($r['method'])){
$here = in_array($this->method(), $r['method']);
} else {
$here = $this->method() == $r['method'];
}
} else {
$here = true;
}
}
return $here;
}
/**
* Sets the referring page for future reference
* @access public
*/
public function setReturnToReferrer(){
if(strpos($this->method(), 'ajax') === false){
if(app()->server->getUri('HTTP_REFERER')){
$_SESSION['referring_page'] = app()->server->getUri('HTTP_REFERER');
app()->refreshCage('session');
}
}
}
/**
* Returns the user to the referring page when called
* @access public
*/
public function returnToReferrer(){
$location = session()->getUri('referring_page', Url::path('view'));
if(!empty($location)){
$this->redirectToUrl($location);
}
}
/**
* Redirects user to an inner-application module/method address.
* @param string $path
* @param array $bits
* @access public
*/
public function redirect($path = false, $bits = false){
$this->redirectToUrl( Url::path($path, $bits)->getUrl(), false, true);
}
/**
* Redirects a user to any complete/absolute URL. Optionally, you may also
* provide status codes for an HTTP response as well as an exit, which discontinues
* executing following php code.
*
* @param string $url
* @param int $status
* @param boolean $exit
*/
public function redirectToUrl($url = false, $status = false, $exit = true){
$tmp_ar = array('url'=>$url,'status'=>$status);
$redirect = Peregrine::sanitize( $tmp_ar );
if($redirect->isUri('url')){
header("Location: ".$redirect->getUri('url'));
$status = $redirect->getDigits('status');
$this->header_code($status);
if($exit){
exit;
}
} else {
error()->raise(1, 'URL for redirect appears to be an invalid resource: '. $url, __FILE__, __LINE__);
}
}
/**
* Delivers a header code to the browser
* @param integer $status
*/
public function header_code($status = false){
$codes = array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Time-out',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Large',
415 => 'Unsupported Media Type',
416 => 'Requested range not satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Time-out'
);
if ($status && isset($codes[$status])) {
$header = sprintf("HTTP/1.1 %s %s", $status, $codes[$status]);
header($header);
}
}
/**
* Is the request an ajax-based request?