forked from reasoncms/reasoncms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.php
1885 lines (1771 loc) · 55.5 KB
/
default.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
/**
* the default minisite_template object
* @author dave hendler
* @author brendon stanton
* @author matt ryan
* @author ben cochran
* @author nathan white
* @package reason
* @subpackage minisite_templates
*/
/**
* Register the template with Reason
*
* This is important for any template you create.
*
* Because templates are referred to by filename alone, Reason needs a way to identify
* the name of the class to instantiate. The way it does this is by looking to see if there
* is an index in the $GLOBALS[ '_minisite_template_class_names' ] array that
* matches the name of the file.
*/
$GLOBALS[ '_minisite_template_class_names' ][ basename( __FILE__) ] = 'MinisiteTemplate';
/**
* Include a bunch of Reason libraries
*/
reason_include_once( 'function_libraries/images.php' );
reason_include_once( 'function_libraries/file_finders.php' );
reason_include_once( 'content_listers/tree.php3' );
reason_include_once( 'minisite_templates/nav_classes/default.php' );
reason_include_once( 'classes/head_items.php' );
reason_include_once( 'classes/page_access.php' );
reason_include_once( 'classes/crumbs.php' );
reason_include_once( 'classes/api/factory.php' );
reason_include_once( 'classes/object_cache.php' );
reason_include_once( 'classes/canonicalizer.php' );
reason_include_once( 'classes/title_tag_parser.php' );
include_once( CARL_UTIL_INC . 'dev/timer.php' );
/**
* The default (and base) Reason minisite template class
*
* This class handles building Reason pages. It is responsible for two main things:
*
* 1. Reading the page type of the current reason page, instantiating, and initializing
* the modules that are part of the page
*
* 2. Producing and assembling the HTML of the page. Note that modules typically handle their own
* markup generation, so the template is only responsible for the markup *outside* the modules'
* output. Altering the output of modules can be done in some cases by extending the module,
* or in more advanced cases by using a view/markup_generation/templating system implemented by
* the module itself. This can vary from module-to-module.
*
* To create a new Reason template, you can extend this class and overload methods. As the inner
* workings of this class are not 100% guaranteed to remain exactly as-is, it is a good idea to
* limit modifications to the run() method and the other methods that produce (X)HTML markup.
*
* Note that Templates *should not* obligate particular css. Coding css directly into the template
* will make it a lot less flexible, as it means that you will be creating a new template for every
* single little css change. It is much better practice to think of the template as the markup and
* the theme (set up in the Reason database as a template + css) as where the markup and the style
* meet. This will enable you to use a single template for any number of similar themes.
*
* @todo Complete documenting this class
*
* @todo Make a clearer distinction between the logic and presentation -- greater mvc design
* and/or use of Smarty or other templating system for the markup, perhaps
*
* @todo Tighten up distinction between public and private methods and attributes
*
* @todo Work to stop passing a reference to the template to the modules. This is a bad design, as
* it tightly couples the modules to the template (meaning that they cannot be intantiated
* or run outside the context of a minisite template).
*
* @todo Allow multiple modules to be placed in a single section
*
* @todo Eliminate the silly table-based/non-table-based logic... it currently means that an ideal
* extension modifies both branches of the logic, which would be kind of crazy.
*/
class MinisiteTemplate
{
/**
* The id of the current site
* @var integer
* @todo clean up modules so that this can be private
*/
var $site_id;
/**
* An entity object representing the current site
* @var object (entity)
* @todo clean up modules so that this can be private
*/
var $site_info;
/**
* The id of the current page
* @var integer
* @todo clean up modules so that this can be private
*/
var $page_id;
/**
* An entity object representing the current page
* @var object (entity)
* @todo clean up modules so that this can be private
*/
/**
* Whether or not the page shown by the template is public - populated in _handle_access_auth_check.
*
* @var boolean
*/
protected $page_is_public;
var $page_info;
/**
* The title of the current page
*
* NOTE: this generally just contains the name of the page.
* The full title as used in the <title> tag is produced by the method get_title().
*
* @var string
* @access private
*/
var $title;
/**
* A minisite navigation class, which contains a tree of all pages in the site
* and can be asked for links, etc.
* @var object (supports minisiteNavigation API)
* @todo clean up modules so that this can be a private variable
*/
var $pages;
/**
* The theme entity that the template should use to get css, etc.
* @var object (entity)
* @access private
*/
var $theme;
/**
* The name of the navigation class that the module should instantiate
*
* To use a different navigation class than the default, include the class in the file that
* defines your template, and then overload the value of this variable.
*
* @access private
* @var string
*/
var $nav_class = 'MinisiteNavigation';
/**
* An array of breadcrumbs that can be displayed in the template.
*
* The last breadcrumb will be used in the <title> attribute.
*
* Do not address this array directly to set crumbs; use the method add_crumb() instead.
*
* @deprecated Use the _get_crumbs_object() instead
* @access private
* @var array
*/
var $additional_crumbs = array();
/**
* The breadcrumbs object
*
* It's best practice to access this via the _get_crumbs_object() method,
* as this object is created lazily and may not exist here yet.
*
* @var object
* @access private
*/
var $_crumbs;
/**
* @deprecated
* @access private
*/
var $last_modified;
/**
* Is there a current user logged in?
* @todo deprecate me
* @var boolean
*/
var $logged_in = false;
/**
* An array that maps section names to module names
*
* Array keys are section names, array values are module names
*
* @var array
* @access private
*/
var $section_to_module = array();
/**
* An array of module objects
*
* Array keys are section names, array values are module objects
* @access private
*/
var $_modules = array();
/**
* Head items object
*
* This object represents all the html elements that should be placed in the head of the page.
* You can use it to add head items (like css, js, meta, etc.) to the page.
*
* A reference to this object is passed to all modules, enabling them to add any required head
* items to the page.
*
* Note that head items added after the head items have already been output in the run phase
* will not be included -- they must be added during the initialization phase of the template
* or module.
*
* Don't worry about duplicate items; the head_items object scrubs duplicates.
*
* @var object
* @todo clean up modules so that this can be a private variable
*/
var $head_items;
/**
* This is a boolean that sends the default module into two different modes --
* table-based or non-table-based
*
* This is a really bad design necessitated by our coupling of logic and presentation --
* we wanted to have logic shared by both table-based and non-table-based layouts.
*
* You probably should not rely on this variable being present in extensions to the class.
*
* @deprecated
* @var boolean
* @access private
*/
var $use_tables = false;
/**
* An array of major page sections
*
* Keys are section names and values are class methods
*
* This is another not-so-well-thought-out design that probably shouldn't be in the default template.
* It was an attempt at generalization that does not allow enough customization -- what if I want a
* particular html element between these two sections, or this section needs an additional wrapper
* div?
*
* You should probably not rely on this variable in your extensions.
*
* @var array
* @access private
*/
var $sections = array('content'=>'show_main_content','related'=>'show_sidebar','navigation'=>'show_navbar');
/**
* What elements should be used for each section?
*
* Example: array('content'=>'section','related'=>'aside');
*
* The template use divs by default.
* @var array
*/
protected $section_elements = array();
/**
* What ARIA roles should each of the sections have?
*
* Example: array('content'=>'main','related'=>'complementary');
*
* @var array
*/
protected $section_roles = array();
/**
* The doctype that the template should use
* @var string
* @access private
*/
var $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
/**
* Should the template cache the navigation object?
*
* The default template has the option to store the navigation object in a cache.
* This can help speed things up (reducing queries and php processing) for sites that are
* large and have high traffic.
*
* This defaults to true as of Reason 4.2 to improve performance. The minisite_page content
* manager has code which clears the navigation_cache when pages are modified, so there isn't a great
* reason to not use this by default.
*
* @var boolean
* @access private
*/
var $use_navigation_cache = true;
/**
* EXPERIMENTAL: page mode
*
* This class variable stores the mode of the page
*
* The template can be put into non-standard modes like "documentation," in which the template
* asks the modules for their documentation rather than asking them for their output, or
* "samples" in which the module can produce sample output.
*
* This is still an experimental feature of the Reaspon template system.
*
* @var string possible values: 'default','documentation','samples'
* @access private
*/
var $mode = 'default';
/**
* Have the current site's parent site been requested from the db?
*
* Until they have, the value of this is false. Afterwards, it is true.
*
* @var boolean
* @acces private
*/
var $queried_for_parent_sites = false;
/**
* The parent sites of the current site
*
* Do not attempt to access this directly, as it will not necessarily be populated.
* Use the method get_parent_sites() instead.
*
* @access private
* @var array
*/
var $parent_sites = array();
/**
* Should the template add the basic Reason modules.css and modules_mod.css?
*
* These css files include basic styling for many common modules. If you want to style
* modules 100% from scratch, set this variable to false.
*
* @access private
* @var boolean
*/
var $include_modules_css = true;
/**
* The page type
*
* Don't access this variable directly -- use $this->get_page_type(), which will set it up
* if it has not already.
*
* @var object
*/
protected $page_type;
/**
* Set up the template
*
* @var integer $site_id
* @var integer $page_id
* @todo page_id should not have a default value -- this makes it seem like you could initialize
* the template without providing a page_id, but that would result in a 404.
*/
function initialize( $site_id, $page_id = '' ) // {{{
{
$this->sess =& get_reason_session();
if( $this->sess->exists() )
{
// if a session exists and the server supports https, pop over to the secure
// site so we have access to the secure session information
force_secure_if_available();
if(!$this->sess->has_started())
$this->sess->start();
}
$this->site_id = $site_id;
$this->page_id = $page_id;
$this->site_info = new entity( $site_id );
$this->page_info = new entity( $page_id );
$this->head_items = new HeadItems();
// make sure that the page exists or that the page's state is Live
// if not, redirect to the 404
if( !$this->page_info->get_values() OR $this->page_info->get_value( 'state' ) != 'Live' )
{
//trigger_error( 'page does not exist', WARNING );
$this->display_404_page();
die();
}
if ($this->use_navigation_cache)
{
$cache = new ReasonObjectCache($this->site_id . '_navigation_cache', 3600); // lifetime of 1 hour
$page_object_cache =& $cache->fetch();
if ($page_object_cache && is_array($page_object_cache) && isset($page_object_cache[$this->nav_class]))
{
$this->pages = $page_object_cache[$this->nav_class];
}
elseif ($page_object_cache && is_object($page_object_cache)) // old format
{
// lets use our cache and also update it
$this->pages = $page_object_cache;
$new_page_object_cache[$this->nav_class] = $this->pages;
$cache->set($new_page_object_cache); // replace with our array keyed cache
}
}
// lets check the persistent cache
if (empty($this->pages) || !isset($this->pages->values[$this->page_info->id()]))
{
// lets setup $this->pages and place in the persistent cache
$this->pages = new $this->nav_class;
// small kludge - just give the tree view access to the site info. used in the show_item function to show the root node of the navigation
$this->pages->site_info =& $this->site_info;
$this->pages->order_by = 'sortable.sort_order';
$this->pages->init( $this->site_id, id_of('minisite_page') );
if ($this->use_navigation_cache)
{
$page_object_cache[$this->nav_class] = $this->pages;
$cache->set($page_object_cache);
}
}
else // if pages came from cache refresh the request variables and set site_info and order_by
{
$this->pages->grab_request();
$this->pages->site_info =& $this->site_info;
$this->pages->order_by = 'sortable.sort_order'; // in case it was changed in the request
}
$this->_handle_access_auth_check();
$this->textonly = '';
if( $this->pages->values )
{
if( !$this->page_id )
$this->page_id = $this->pages->root_node();
$this->pages->cur_page_id = $this->page_id;
$this->pages->force_open( $this->page_id );
$this->cur_page = new entity($this->page_id);
$this->title = $this->cur_page->get_value('name');
$this->get_css_files();
$this->get_meta_information();
if( $this->sess->exists() )
{
if (USE_JS_LOGOUT_TIMER)
{
$this->head_items->add_stylesheet(REASON_HTTP_BASE_PATH.'css/timer.css');
$this->head_items->add_javascript(JQUERY_URL, true);
$this->head_items->add_javascript(WEB_JAVASCRIPT_PATH . 'timer/timer.js');
}
// we know that someone is logged in if the session exists
$this->logged_in = true;
}
// hook for any actions to take prior to loading modules
$this->pre_load_modules();
// load the modules
$this->load_modules();
}
else
{
trigger_error('Page requested not able to be displayed... no pages on site');
$this->_display_403_page();
die();
}
} // }}}
function _handle_access_auth_check()
{
$auth_username = reason_check_authentication();
$rpa = new reasonPageAccess();
$rpa->set_page_tree($this->pages);
$has_access = $rpa->has_access($auth_username, $this->page_id);
if(!$has_access)
{
if(!empty($auth_username))
{
$this->_display_403_page();
die();
}
else
{
header('Location: '.REASON_LOGIN_URL.'?dest_page='.urlencode(get_current_url()));
die();
}
}
else
{
$this->page_is_public = (empty($auth_username)) ? true : $rpa->has_access(false, $this->page_id);
}
}
function _display_403_page()
{
http_response_code(403);
if(file_exists(WEB_PATH.ERROR_403_PATH) && is_readable(WEB_PATH.ERROR_403_PATH))
{
include(WEB_PATH.ERROR_403_PATH);
}
else
{
trigger_error('The file at ERROR_403_PATH ('.ERROR_403_PATH.') is not able to be included');
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>403: Forbidden</title></head><body><h1>403: Forbidden</h1><p>You do not have access to this page.</p></body></html>';
}
}
function _display_404_page()
{
http_response_code(404);
if(file_exists(WEB_PATH.ERROR_404_PATH) && is_readable(WEB_PATH.ERROR_404_PATH))
{
include(WEB_PATH.ERROR_404_PATH);
}
else
{
trigger_error('The file at ERROR_404_PATH ('.ERROR_404_PATH.') is not able to be included');
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>404: Forbidden</title></head><body><h1>404: Not Found</h1><p>This page was not found.</p></body></html>';
}
}
// hook
function pre_load_modules()
{
}
function set_theme( $t ) //{{{
{
$this->theme = $t;
} // }}}
function get_css_files()
{
$css_files = array();
// get css assoc with template
$es = new entity_selector();
$es->description = 'Get CSS associated with template';
$es->add_type( id_of('css') );
$es->add_right_relationship( $this->template_id, relationship_id_of('minisite_template_to_external_css') );
$es->set_order( 'sortable.sort_order' );
$css_files += $es->run_one();
// Get css assoc with theme
$es = new entity_selector();
$es->description = 'Get CSS associated with theme';
$es->add_type( id_of('css') );
$es->add_right_relationship( $this->theme->id(), relationship_id_of('theme_to_external_css_url') );
$es->set_order( 'sortable.sort_order' );
$css_files += $es->run_one();
if($this->include_modules_css)
{
$this->head_items->add_stylesheet(REASON_HTTP_BASE_PATH.'css/modules.css');
}
if( $css_files )
{
foreach( $css_files AS $css )
{
if($css->get_value( 'css_relative_to_reason_http_base' ) == 'true')
{
$url = REASON_HTTP_BASE_PATH.$css->get_value( 'url' );
}
else
{
$url = $css->get_value( 'url' );
}
$media = $css->has_value('css_media') ? $css->get_value('css_media') : '';
$this->head_items->add_stylesheet( $url, $media );
}
}
if($customizer = $this->get_theme_customizer())
{
$customizer->modify_head_items($this->head_items);
}
}
/**
* Add structured extra head items (stored on the page entity as json) to the page
*/
function add_extra_head_content_structured()
{
if($this->page_info->has_value('extra_head_content_structured') && $this->page_info->get_value('extra_head_content_structured') && ($data = json_decode($this->page_info->get_value('extra_head_content_structured'))))
{
foreach($data as $item)
{
if(empty($item->url))
continue;
switch($item->type)
{
case 'js':
$this->head_items->add_javascript( $item->url );
break;
case 'css':
$this->head_items->add_stylesheet( $item->url );
break;
default:
trigger_error('Unrecognized head item type ('.$item->type.')');
}
}
}
}
/**
* Get the theme customizer class
*/
function get_theme_customizer()
{
if(!isset($this->theme_customizer))
{
if($this->theme->get_value('theme_customizer') && $this->site_info->get_value('theme_customization'))
{
$this->theme_customizer = reason_get_theme_customizer($this->site_info, $this->theme);
if(empty($this->theme_customizer))
trigger_error('Theme customizer "'.$this->theme->get_value('theme_customizer').'" not found or not registered properly. No customizations applied.');
}
else
$this->theme_customizer = false;
}
return $this->theme_customizer;
}
function get_meta_information()
{
// add the charset information
$this->head_items->add_head_item('meta',array('http-equiv'=>'Content-Type','content'=>'text/html; charset=UTF-8' ) );
if($favicon_path = $this->_get_favicon_path() )
{
$this->head_items->add_head_item('link',array('rel'=>'shortcut icon','href'=>$favicon_path, ) );
}
// array of meta tags to search for in the page entity
// key: entity field
// value: meta tag to use
$meta_tags = array(
'description' => 'description',
'author' => 'author'
);
if ( REASON_SHOW_META_KEYWORDS )
$meta_tags['keywords'] = 'keywords';
// load meta elements from current page
$tags_added = array();
foreach( $meta_tags as $entity_field => $meta_name )
{
if( $this->cur_page->get_value( $entity_field ) )
{
$content = reason_htmlspecialchars( $this->cur_page->get_value( $entity_field ) );
$this->head_items->add_head_item('meta',array('name'=>$meta_name,'content'=>$content) );
$tags_added[] = $meta_name;
}
}
if ( !in_array('keywords',$tags_added)
&& $this->pages->root_node() == $this->page_id
&& REASON_SHOW_META_KEYWORDS )
{
$content = reason_htmlspecialchars( $this->site_info->get_value( 'keywords' ) );
$this->head_items->add_head_item('meta',array('name'=>'keywords','content'=>$content) );
}
if (!empty( $_REQUEST['no_search'] )
|| $this->site_info->get_value('site_state') != 'Live'
|| ( defined('THIS_IS_A_DEVELOPMENT_REASON_INSTANCE') && THIS_IS_A_DEVELOPMENT_REASON_INSTANCE )
|| !$this->cur_page->get_value('indexable'))
{
$this->head_items->add_head_item('meta',array('name'=>'robots','content'=>'none' ) );
}
}
function _get_favicon_path()
{
if(defined('REASON_DEFAULT_FAVICON_PATH') && REASON_DEFAULT_FAVICON_PATH )
{
return REASON_DEFAULT_FAVICON_PATH;
}
return NULL;
}
function run() // {{{
{
$this->start_page();
$this->show_body();
$this->end_page();
} // }}}
/**
* Run by generate_page.php if module_api is defined in the request.
*
* What if nothing has content?
*/
function run_api()
{
if (!empty($this->section_to_module))
{
foreach ($this->section_to_module as $section => $module)
{
$module =& $this->_get_module( $section );
$module->run_api();
}
}
else // LETS DO A 404 with text/html
{
$api = new CarlUtilAPI('html');
$api->run();
exit();
}
}
/**
* @return mixed string requested_api name or false
*/
function get_requested_api()
{
return (!empty($this->requested_api)) ? $this->requested_api : false;
}
/**
* @return mixed string requested_module_identified name or false
*/
function get_requested_identifier()
{
return (!empty($this->requested_identifier)) ? $this->requested_identifier : false;
}
function change_module( $page_type, $section, $new_module ) // {{{
// allows runtime modification of module to use for a given
// type-section pair.
{
if( $page_type == $this->cur_page->get_value( 'custom_page' ) )
$this->section_to_module[ $section ] = $new_module;
} // }}}
function change_module_global( $section, $new_module ) // {{{
// allows runtime modification of module regardless of page type
// useful for changing the navigation section globally.
{
$this->section_to_module[ $section ] = $new_module;
} // }}}
function alter_modules() // {{{
{
trigger_error('alter_modules() is deprecated. Please use alter_page_type() instead');
} // }}}
/**
* @deprecated
*/
function alter_page_type($page_type)
{
return $page_type;
}
/**
* If the instantiated module extends alter_page_type - do our legacy work and throw an error.
*
* @todo We use reflection here and should make sure performance is okay... it might be possible without reflection.
* @todo make sure equivalency check of page_type_oldformat_altered and page_type_oldformat is correct.
* @todo update the export function to properly handle _meta content
*/
final protected function _legacy_alter_page_type($page_type, $page_type_name)
{
// if my instance has parents
if ($parents = class_parents($this))
{
$r = new ReflectionClass(get_class($this));
$alter_page_type_method = $r->getMethod('alter_page_type');
if ($alter_page_type_method->class != array_pop($parents))
{
// we need to call alter_page_type with the old style array and trigger a warning
//trigger_error('The template object ' . $alter_page_type_method->class . ' extends alter_page_type, which is deprecated. Use alter_reason_page_type instead.');
$page_type_oldformat = $page_type->export("reasonPTArray_var");
$page_type_meta = $page_type->meta();
$page_type_oldformat_altered = $this->alter_page_type($page_type_oldformat);
if ($page_type_oldformat_altered != $page_type_oldformat)
{
$rpt =& get_reason_page_types();
$page_type = $rpt->get_page_type($page_type_name, $page_type_oldformat_altered);
// we are setting the meta information again because the export doesn't handle it properly.
// this should be removed once that is fixed.
foreach($page_type_meta as $k=>$v)
$page_type->meta($k, $v);
};
}
}
return $page_type;
}
/**
* Hook in load modules which allows modification of the page_type object.
*
* This is often a bad idea because a page type ought to work consistently across templates.
*
* @param object reference to the page type object
*/
function alter_reason_page_type($page_type)
{
}
function additional_args( &$args ) // {{{
//if a module needs additional args
{
} // }}}
function get_page_type()
{
if(!isset($this->page_type))
{
reason_include_once( 'classes/page_types.php');
$requested_page_type_name = ($this->cur_page->get_value('custom_page') !== FALSE) ? $this->cur_page->get_value('custom_page') : null;
// get the fully composed page type - make sure to support legacy alter_page_type operations
$rpt =& get_reason_page_types();
$page_type = ($requested_page_type = $rpt->get_page_type($requested_page_type_name)) ? $requested_page_type : $rpt->get_page_type();
$page_type = $this->_legacy_alter_page_type($page_type, $requested_page_type_name);
$this->alter_reason_page_type($page_type);
$this->page_type = $page_type;
}
return $this->page_type;
}
function load_modules() // {{{
{
$page_type = $this->get_page_type();
if (extension_loaded('newrelic')) {
newrelic_name_transaction($page_type->get_name());
}
// if an api was requested lets identify the region to run
if ($requested_api = $this->get_requested_api())
{
$module_api = ReasonAPIFactory::get_requested_api($page_type, $requested_api, $this->get_requested_identifier());
if ($module_api) $this->section_to_module[$module_api['module_region']] = $module_api['module_name'];
else $this->section_to_module = null;
}
else
{
foreach ($page_type->get_region_names() as $region)
{
$region_info = $page_type->get_region($region);
$module_name = $region_info['module_name'];
$module_filename = $region_info['module_filename'];
if ($module_filename && reason_file_exists($module_filename)) reason_include_once( $module_filename );
$this->section_to_module[$region] = $module_name;
}
}
if (!empty($this->section_to_module))
{
$canonicalizer = new reasonCanonicalizer();
foreach( $this->section_to_module AS $region => $module_name )
{
if( !empty( $module_name ) )
{
$region_info = $page_type->get_region($region);
$params = ($region_info['module_params'] != null) ? $region_info['module_params'] : array();
$module_class = (!empty($GLOBALS[ '_module_class_names' ][ $module_name ])) ? $GLOBALS[ '_module_class_names' ][ $module_name ] : '';
if( !empty( $module_class ) )
{
$this->_modules[ $region ] = new $module_class;
$args = array();
// set up a reference instead of a copy
// dh - I really want to get rid of this. For now, it stays. However, I'm adding a number
// of other parameters that a module will take by default so that we can rely on some important
// data coming in. 9/15/04
$args[ 'parent' ] =& $this; // pass this object to the module
$args[ 'page_id' ] = $this->page_id;
$args[ 'site_id' ] = $this->site_id;
$args[ 'cur_page' ] = $this->cur_page;
// we set the module identifier as a hash of the section - should be unique
$args[ 'identifier' ] = ReasonAPIFactory::get_identifier_for_module($page_type, $region);
//$args[ 'nav_pages' ] =& $this->pages;
$args[ 'textonly' ] = '';
$args[ 'api' ] = (!empty($module_api)) ? $module_api['api'] : false;
$args[ 'page_is_public' ] = $this->page_is_public;
// this is used by a few templates to add some arguments. leaving it in for backwards
// compatibility. i believe that any usage of this can be done with page type parameteres now.
$this->additional_args( $args );
// localizes the args array inside the module class. this is basically another layer of backwards
// compatibility with old modules.
$this->_modules[ $region ]->prep_args( $args );
// Pass a reference to the pages object into the module (so the module doesn't have to use the
// deprecated reference to the template)
$this->_modules[ $region ]->set_page_nav( $this->pages );
// Pass a reference to the head items object into the module (so the module doesn't have to use the
// deprecated reference to the template)
$this->_modules[ $region ]->set_head_items( $this->head_items );
// Pass a reference to the head items object into the module (so the module doesn't have to use the
// deprecated reference to the template)
$breadcrumbs_obj =& $this->_get_crumbs_object();
$this->_modules[ $region ]->set_crumbs( $breadcrumbs_obj );
// send and check parameters gathered above from the page_types
$this->_modules[ $region ]->handle_params( $params );
// hook to run code before grabbing and sanitizing the _REQUEST. this is important for something
// that might not know what variables will be coming through until a Disco class or some such thing
// has been loaded.
$this->_modules[ $region ]->pre_request_cleanup_init();
// Set the module request array based on the cleanup rules.
$this->_modules[ $region ]->request = $this->clean_external_vars($this->_modules[$region]->get_cleanup_rules());
// init takes $args as a backwards compatibility feature. otherwise, everything should be handled
// in prep_args
if ($this->should_benchmark()) $this->benchmark_start('init module ' . $module_name);
$this->_modules[ $region ]->init( $args );
if ($this->should_benchmark()) $this->benchmark_stop('init module ' . $module_name);
$canonicalizer->register($this->_modules[ $region ]);
}
else
{
trigger_error( 'Badly formatted module ('.$module_name.') - $module_class not set ' );
}
}
}
if($canonical_url = $canonicalizer->get_canonical_url())
{
$this->head_items->add_head_item('link',array('rel'=>'canonical','href'=>$canonical_url ), '');
}
}
} // }}}
function & _get_module( $sec ) // {{{
{
if( !empty( $this->_modules[ $sec ] ) && is_object( $this->_modules[ $sec ] ) )
{
return $this->_modules[ $sec ];
}
$false = false;
return $false;
} // }}}
function clean_vars( &$vars, $rules ) // {{{
// Returns an array which takes the values of the keys in Vars of
// the keys set in Settings, and runs the cleaning function
// specified in the value of settings
{
return carl_clean_vars( $vars, $rules );
} // }}}
function clean_external_vars($rules)
// Cleanup rules can include a 'method'
// parameter which indicates where the value should come from -- options are get, post, and
// nothing/anything else, which means the $_REQUEST array.
{
$request = $cleanup_params = array();
$prepped_request = conditional_stripslashes($_REQUEST);
$prepped_post = conditional_stripslashes($_POST);
$prepped_get = conditional_stripslashes($_GET);
foreach ($rules as $param => $rule)
{
if (isset($rule['method']))
{
switch ($rule['method'])
{
case 'get':
case 'GET':
$cleanup_params['prepped_get'][$param] = $rule;
break;
case 'post':
case 'POST':
$cleanup_params['prepped_post'][$param] = $rule;
break;
default:
$cleanup_params['prepped_request'][$param] = $rule;
break;
}
} else {
$cleanup_params['prepped_request'][$param] = $rule;
}
}
foreach ($cleanup_params as $source => $rules)
{
$cleaned = $this->clean_vars( $$source, $rules );
$request = array_merge($request, $cleaned);
}
return $request;
} // }}}
function run_section( $sec ) // {{{
{
$module =& $this->_get_module( $sec );
$module_name = $this->section_to_module[$sec];
if( $module )
{
echo "\n\n";
if($this->in_documentation_mode())
{
$this->run_documentation($sec);
}
else
{
if ($this->should_benchmark()) $this->benchmark_start('run module ' . $module_name);
$module->run();
if ($this->should_benchmark()) $this->benchmark_stop('run module ' . $module_name);
}
echo "\n\n";