/
Page.php
2557 lines (2369 loc) · 98.1 KB
/
Page.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
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* @author Black Cat Development
* @copyright 2013, Black Cat Development
* @link http://blackcat-cms.org
* @license http://www.gnu.org/licenses/gpl.html
* @category CAT_Core
* @package CAT_Core
*
*/
if (!class_exists('CAT_Helper_Page'))
{
if (!class_exists('CAT_Object', false))
{
@include dirname(__FILE__) . '/../Object.php';
}
class CAT_Helper_Page extends CAT_Object
{
protected $_config = array(
'loglevel' => 8,
'forbidden_l0' => array( // configurables will be added later
'account',
'framework',
'include',
'install',
'languages',
'modules',
'search',
'temp',
'templates',
'index.php'
),
'forbidden_filenames_l0' => array(
'index.php',
'config.php',
'upgrade-script.php'
),
);
private static $instance;
private static $space = ' '; // space before header items
private static $pages = array();
private static $pages_by_visibility = array();
private static $pages_by_parent = array();
private static $pages_by_id = array();
private static $pages_sections = array();
private static $pages_editable = 0;
// header components
private static $css = array();
private static $meta = array();
private static $js = array();
private static $jquery = array();
private static $jquery_core = false;
private static $jquery_ui_core = false;
// scan dirs
private static $css_search_path = array();
private static $js_search_path = array();
// footer components
private static $script = array();
private static $f_jquery = array();
private static $f_js = array();
/**
* the constructor loads the available pages from the DB and stores it
* in internal arrays
*
* @access private
* @return void
**/
public static function getInstance($skip_init=false)
{
if (!self::$instance)
{
self::$instance = new self();
if(!$skip_init) self::init();
}
return self::$instance;
}
public function __call($method, $args)
{
if ( ! isset($this) || ! is_object($this) )
return false;
if ( method_exists( $this, $method ) )
return call_user_func_array(array($this, $method), $args);
}
/**
*
* @access public
* @return
**/
public static function reset()
{
self::init(1);
} // end function reset()
/**
* initialize; fills the internal pages array
*
* @access private
* @param boolean $force - always reload
* @return void
**/
private static function init($force=false)
{
global $page_id;
if(CAT_Registry::exists('CAT_HELPER_PAGE_INITIALIZED') && !$force)
return;
if(!self::$instance) self::getInstance(true);
// add configurable dirs to forbidden array (level 0)
foreach( array(PAGES_DIRECTORY,MEDIA_DIRECTORY,CAT_BACKEND_FOLDER) as $dir )
{
$dir = preg_replace('~^/~','',$dir);
if(!in_array($dir,self::$instance->_config['forbidden_l0']))
array_push(self::$instance->_config['forbidden_l0'],$dir);
}
// fill pages array
if(count(self::$pages)==0 || $force)
{
$now = time();
$result = self::$instance->db()->query(sprintf(
'SELECT * FROM %spages ORDER BY `level` ASC, `position` ASC',
CAT_TABLE_PREFIX
));
if( $result && $result->numRows()>0 )
{
$children_count = array();
$direct_parent = 0;
while ( false !== ( $row = $result->fetchRow(MYSQL_ASSOC) ) )
{
$row['children'] = 0;
$row['is_parent'] = false;
$row['has_children'] = false; // same as is_parent!
$row['is_editable'] = false;
$row['is_in_trail'] = false;
$row['is_direct_parent'] = false;
$row['is_current'] = false;
$row['is_open'] = isset( $_COOKIE[ session_name() . 'pageid_'.$row['page_id']] ) ? true : false; // for page tree
$row['href'] = CAT_URL . PAGES_DIRECTORY . $row['link'] . PAGE_EXTENSION;
// mark editable pages by checking user perms and page
// visibility
// --------------------- NOT READY YET! ----------------------------------------
if ( CAT_Users::ami_group_member($row['admin_groups']) )
{
if ( CAT_Registry::get('PAGE_TRASH') !== 'true' || $row['visibility'] !== 'deleted' )
{
$row['is_editable'] = true;
self::$pages_editable++;
}
}
// --------------------- NOT READY YET! ----------------------------------------
// mark current page
if (isset($page_id) && $row['page_id'] == $page_id )
{
$row['is_current'] = true;
$direct_parent = $row['parent'];
}
// count children; this lets us mark pages that have
// children later
if(!isset($children_count[$row['parent']]))
$children_count[$row['parent']] = 1;
else
$children_count[$row['parent']]++;
// add any other settings
$set = self::$instance->db()->query(sprintf(
'SELECT * FROM %spages_settings WHERE page_id=%d',
CAT_TABLE_PREFIX, $row['page_id']
));
if( $set && $set->numRows()>0 )
{
while ( false !== ( $set_row = $set->fetchRow(MYSQL_ASSOC) ) )
{
if(!isset($row['settings']))
$row['settings'] = array();
if(!isset($row['settings'][$set_row['set_type']]))
$row[$set_row['set_type']] = array();
if(!isset($row['settings'][$set_row['set_type']][$set_row['set_name']]))
$row[$set_row['set_type']][$set_row['set_name']] = array();
$row['settings'][$set_row['set_type']][$set_row['set_name']][] = $set_row['set_value'];
}
}
self::$pages[] = $row;
end(self::$pages);
self::$pages_by_id[$row['page_id']] = key(self::$pages);
reset(self::$pages);
} // end while()
// mark pages that have children
foreach(self::$pages as $i => $page)
{
if(isset($children_count[$page['page_id']]))
{
self::$pages[$i]['children'] = $children_count[$page['page_id']];
self::$pages[$i]['is_parent'] = true;
self::$pages[$i]['has_children'] = true;
}
// mark pages in current trail
#if(isset($page_id) && in_array($page['page_id'],$trail))
# self::$pages[$i]['is_in_trail'] = true;
if($direct_parent && $page['page_id'] == $direct_parent)
self::$pages[$i]['is_direct_parent'] = true;
}
// resolve the trail
$trail = array();
if(isset($page_id) && isset(self::$pages_by_id[$page_id]))
{
// mark parents
$trail = explode(",", '0,'.self::$pages[self::$pages_by_id[$page_id]]['page_trail']);
array_pop($trail); // remove the current page
}
// add 'virtual' page -1
if(!isset(self::$pages_by_id['-1']))
{
self::$pages_by_id['-1'] = 0;
}
} // end if($result)
}
CAT_Registry::register('CAT_HELPER_PAGE_INITIALIZED',true);
} // end function init()
/**
*
* @access public
* @return
**/
public static function addCSS($url,$for='frontend',$media='screen')
{
CAT_Helper_Page::$css[] = array(
'media' => $media,
'file' => $url
);
} // end function addCSS()
/**
* creates a new page
*
* @access public
* @param array $options
* @return mixed - new page ID or false on error
**/
public static function addPage($options)
{
$self = self::getInstance();
// get mandatory fields
$res = $self->db()->query(sprintf(
'DESCRIBE `%spages`',CAT_TABLE_PREFIX
));
$mandatory = array();
while(false!==($row=$res->fetchRow(MYSQL_ASSOC)))
if($row['Null']=='NO'&&$row['Key']!='PRI')
$mandatory[$row['Field']] = $row['Type'];
// fill options
$sql = 'INSERT INTO `%spages` SET ';
foreach($options as $key => $value)
{
$sql .= '`'.$key.'` = \''.$value.'\', ';
if(array_key_exists($key,$mandatory))
unset($mandatory[$key]);
}
// all mandatory fields filled?
if(count($mandatory))
foreach($mandatory as $key=>$type)
$sql .= '`'.$key.'`='
. (
preg_match('~^int~i',$type)
? '0, '
: '\'\', '
);
$sql = preg_replace('~,\s*$~','',$sql);
$self->db()->query(sprintf($sql,CAT_TABLE_PREFIX));
// reload pages list
if(!$self->db()->is_error()) self::init(1);
return
$self->db()->is_error()
? false
: $self->db()->get_one("SELECT LAST_INSERT_ID()");
} // end function addPage()
/**
* update page options
*
* @access public
* @param integer $page_id
* @param array $options
* @return boolean
**/
public static function updatePage($page_id,$options)
{
if(!self::$instance) self::getInstance();
$sql = 'UPDATE `%spages` SET ';
foreach($options as $key => $value)
{
if(is_array($value))
$value = implode(',',$value);
$sql .= '`'.$key.'` = \''.$value.'\', ';
}
$sql = preg_replace('~,\s*$~','',$sql);
$sql .= ' WHERE page_id=%d';
self::$instance->db()->query(sprintf($sql,CAT_TABLE_PREFIX,$page_id));
// reload pages list
if(!self::$instance->db()->is_error()) self::init(1);
return
( self::$instance->db()->is_error() === true )
? false
: true;
} // end function updatePage()
/**
*
* @access public
* @return
**/
public static function updatePageSettings($page_id,$options)
{
if(!self::$instance) self::getInstance();
foreach($options as $key => $value)
{
if(is_array($value))
$value = implode(',',$value);
self::$instance->db()->query(sprintf(
'REPLACE INTO `%spages_settings` VALUES( %d, "%s", "%s", "%s" )',
CAT_TABLE_PREFIX,$page_id,'internal',$key,$value
));
}
} // end function updatePageSettings()
/**
* delete page; uses _trashPages() if trash is enabled, _deletePage()
* otherwise
*
* @access public
* @param integer $page_id
* @param boolean $use_trash
* @return boolean
**/
public static function deletePage($page_id,$use_trash=false)
{
if($use_trash)
{
// Update the page visibility to 'deleted'
self::getInstance()->db()->query(sprintf(
"UPDATE `%spages` SET `visibility` = 'deleted' WHERE `page_id` = %d LIMIT 1",
CAT_TABLE_PREFIX, $page_id
));
return self::_trashPages($page_id);
}
else
{
// remove sub pages
$sub_pages = self::getSubPages($page_id);
$sub_pages = array_reverse($sub_pages);
$errors = array();
foreach($sub_pages as $sub_page_id)
{
$err = self::_deletePage( $sub_page_id );
$errors = array_merge($errors,$err);
}
// remove the page itself
$err = self::_deletePage($page_id);
$errors = array_merge($errors,$err);
if(count($errors)) return false;
return true;
}
} // end function deletePage()
/**
*
*
**/
public static function createAccessFile($filename, $page_id)
{
// check if $filename is a full path (may be 'link' db value)
if(!preg_match('~^'.sanitize_path(CAT_PATH.PAGES_DIRECTORY).'~i',$filename))
$filename = sanitize_path(CAT_PATH.PAGES_DIRECTORY.'/'.dirname($filename).'/'.self::getFilename(basename($filename)).PAGE_EXTENSION);
$pages_path = CAT_Helper_Directory::sanitizePath(CAT_PATH.PAGES_DIRECTORY);
$rel_pages_dir = str_replace($pages_path, '', CAT_Helper_Directory::sanitizePath(dirname($filename)));
$rel_filename = str_replace($pages_path, '', CAT_Helper_Directory::sanitizePath($filename));
// prevent system directories and files from being overwritten (level 0)
$denied = false;
if (PAGES_DIRECTORY == '')
{
$forbidden_dirs = self::$instance->_config['forbidden_l0'];
$forbidden_files = self::$instance->_config['forbidden_filenames_l0'];
$search = explode('/', $rel_filename);
$denied = in_array($search[1], $forbidden_dirs);
$denied = in_array($search[1], $forbidden_files);
}
if ((true === is_writable($pages_path)) && (false == $denied))
{
// First make sure parent folder exists
$parent_folders = explode('/', $rel_pages_dir);
$parents = '';
foreach ($parent_folders as $parent_folder)
{
if ($parent_folder != '/' && $parent_folder != '')
{
$parents .= '/' . $parent_folder;
if (!file_exists($pages_path . $parents))
{
// create dir; also creates index.php (last param = true)
CAT_Helper_Directory::createDirectory($pages_path . $parents, OCTAL_DIR_MODE, true);
CAT_Helper_Directory::setPerms($pages_path . $parents);
}
}
}
$step_back = str_repeat('../', substr_count($rel_pages_dir, '/') + (PAGES_DIRECTORY == "" ? 0 : 1));
$content = '<?php' . "\n";
$content .= "/**\n *\tThis file is autogenerated by BlackCat CMS Version " . CAT_VERSION . "\n";
$content .= " *\tDo not modify this file!\n */\n";
$content .= "\t" . '$page_id = ' . $page_id . ';' . "\n";
$content .= "\t" . 'require_once \'' . $step_back . 'index.php\';' . "\n";
$content .= '?>';
/**
* write the file
*/
$fp = fopen($filename, 'w');
if ($fp)
{
fwrite($fp, $content, strlen($content));
fclose($fp);
/**
* Chmod the file
*/
CAT_Helper_Directory::getInstance()->setPerms($filename);
}
else
{
CAT_Backend::getInstance()->print_error('Error creating access file in the pages directory, cannot open file');
return false;
}
return true;
}
else
{
CAT_Backend::getInstance()->print_error('Error creating access file in the pages directory, path not writable or forbidden file / directory name');
return false;
}
} // end function createAccessFile()
/**
*
* @access public
* @return
**/
public static function deleteAccessFile($page_id) {
// Unlink the access file and directory
$directory = CAT_PATH . PAGES_DIRECTORY . self::properties($page_id,'link');
$filename = $directory . PAGE_EXTENSION;
$directory .= '/';
if (file_exists($filename))
{
if (!is_writable(CAT_PATH . PAGES_DIRECTORY . '/'))
{
$self = self::getInstance(true);
$errors[] = $self->lang()->translate('Cannot delete access file!');
}
else
{
unlink($filename);
if (
is_dir($directory)
&& (rtrim($directory, '/') != CAT_PATH . PAGES_DIRECTORY)
) {
CAT_Helper_Directory::removeDirectory($directory);
}
}
}
} // end function deleteAccessFile()
/**
*
* @access public
* @return
**/
public static function deleteLanguageLink($page_id,$lang)
{
if(!self::$instance) self::getInstance(true);
self::$instance->db()->query(sprintf(
'DELETE FROM `%spage_langs` WHERE link_page_id = %d AND lang = "%s"',
CAT_TABLE_PREFIX, $page_id, $lang
));
} // end function deleteLanguageLink()
/**
* checks if a page exists; checks access file and database entry
*
* @access public
* @return
**/
public static function exists($link)
{
// check database
if(!self::$instance) self::getInstance(true);
$get_same_page = self::$instance->db()->query(sprintf(
"SELECT page_id FROM `%spages` WHERE link='%s'",
CAT_TABLE_PREFIX,$link
));
if ($get_same_page->numRows() > 0)
return true;
// check access file
if(
file_exists(CAT_PATH.PAGES_DIRECTORY.$link.PAGE_EXTENSION)
|| file_exists(CAT_PATH.PAGES_DIRECTORY.$link.'/')
) {
return true;
}
} // end function exists()
/**
* prints the backend footers
*
* @access public
* @return string
**/
public static function getBackendFooters()
{
// -----------------------------------------------------------------
// ----- backend theme -----
// -----------------------------------------------------------------
$tpl = CAT_Registry::get('DEFAULT_THEME');
$file = sanitize_path(CAT_PATH.'/templates/'.$tpl.'/footers.inc.php');
if (file_exists($file))
{
self::getInstance()->log()->logDebug(sprintf('adding footer items for backend theme [%s]', $tpl));
self::_load_footers_inc($file, 'backend', 'templates/'.$tpl);
} // end loading theme
// -----------------------------------------------------------------
// ----- admin tool -----
// -----------------------------------------------------------------
$tool = CAT_Helper_Validate::get('_REQUEST','tool','string');
if ($tool)
{
$path = sanitize_path(CAT_PATH.'/modules/'.$tool.'/tool.php');
self::getInstance()->log()->logDebug(sprintf('handle admin tool [%s] - path [%s]', $tool, $path));
if (file_exists($path))
{
$file = sanitize_path(CAT_PATH . '/modules/' . $tool . '/footers.inc.php');
if (file_exists($file))
{
self::getInstance()->log()->logDebug(sprintf('adding footer items for admin tool [%s]', $_REQUEST['tool']));
self::_load_footers_inc($file, 'backend', 'templates/' . $tpl);
}
}
}
// -----------------------------------------------------------------
// ----- scan for js files -----
// -----------------------------------------------------------------
if (count(self::$js_search_path))
{
$val = CAT_Helper_Validate::getInstance();
foreach (self::$js_search_path as $directory)
{
$file = sanitize_path($directory . '/backend_body.js');
if (file_exists(CAT_PATH . '/' . $file))
{
self::$f_js[] = '<script type="text/javascript" src="' . $val->sanitize_url(CAT_URL . $file) . '"></script>' . "\n";
}
}
}
return self::getJQuery('footer') . self::getJavaScripts('footer');
} // end function getBackendFooters()
/**
*
*
*
*
**/
public static function getBackendHeaders($section)
{
// -----------------------------------------------------------------
// ----- backend theme -----
// -----------------------------------------------------------------
$file = sanitize_path(CAT_PATH . '/templates/' . DEFAULT_THEME . '/headers.inc.php');
if (file_exists($file))
{
self::$instance->log()->logDebug(sprintf('adding items for backend theme [%s]', DEFAULT_THEME));
self::_load_headers_inc($file, 'backend', 'templates/' . DEFAULT_THEME, $section);
} // end loading theme
// -----------------------------------------------------------------
// ----- admin tool -----
// -----------------------------------------------------------------
if (isset($_REQUEST['tool']))
{
$path = sanitize_path(CAT_PATH . '/modules/' . $_REQUEST['tool'] . '/tool.php');
self::$instance->log()->logDebug(sprintf('handle admin tool [%s] - path [%s]', $_REQUEST['tool'], $path));
if (file_exists($path))
{
array_push(CAT_Helper_Page::$css_search_path, '/modules/' . $_REQUEST['tool'], '/modules/' . $_REQUEST['tool'] . '/css');
array_push(CAT_Helper_Page::$js_search_path, '/modules/' . $_REQUEST['tool'], '/modules/' . $_REQUEST['tool'] . '/js');
$file = sanitize_path(CAT_PATH . '/modules/' . $_REQUEST['tool'] . '/headers.inc.php');
if (file_exists($file))
{
self::$instance->log()->logDebug(sprintf('adding items for admin tool [%s]', $_REQUEST['tool']));
self::_load_headers_inc($file, 'backend', 'modules/' . $_REQUEST['tool'], $section);
}
}
}
// -----------------------------------------------------------------
// ----- edit page -----
// -----------------------------------------------------------------
else
{
self::$instance->log()->logDebug('Loading sections');
self::_load_sections('backend');
}
// -----------------------------------------------------------------
// ----- scan for css files -----
// -----------------------------------------------------------------
self::_load_css('backend');
// -----------------------------------------------------------------
// ----- scan for js files -----
// -----------------------------------------------------------------
self::_load_js('backend');
// return the results
return self::getCSS('backend') . self::getJQuery('header') . self::getJavaScripts('header');
} // end function getBackendHeaders()
/**
* returns the items of static array $css as HTML link markups
*
* @access public
* @return HTML
**/
public static function getCSS($for='frontend')
{
$output = NULL;
if (count(CAT_Helper_Page::$css))
{
// check for template variants
$key = 'DEFAULT_TEMPLATE_VARIANT';
$subkey = 'DEFAULT_TEMPLATE';
$file = 'template';
if ( $for == 'backend' )
{
$key = 'DEFAULT_THEME_VARIANT';
$subkey = 'DEFAULT_THEME';
$file = 'theme';
}
$path = CAT_Helper_Directory::sanitizePath(CAT_PATH.'/templates/'.CAT_Registry::get($subkey).'/css/'.CAT_Registry::get($key));
if(CAT_Registry::get($key) != '' && file_exists($path) && file_exists($path.'/'.$file.'.css') )
{
array_push(CAT_Helper_Page::$css, array('file'=>'templates/'.CAT_Registry::get($subkey).'/css/'.CAT_Registry::get($key).'/'.$file.'.css'));
}
$val = CAT_Helper_Validate::getInstance();
$seen = array();
foreach (CAT_Helper_Page::$css as $item)
{
if ( ! isset($seen[$item['file']]) )
{
// make sure we have an URI (CAT_URL included)
if(!preg_match('~^http(s)?://~i',$item['file']))
{
if ( ! preg_match( '~^/~', $item['file'] ) )
$item['file'] = '/' . $item['file'];
$file = (preg_match('#' . CAT_URL . '#i', $item['file']) ? $item['file'] : CAT_URL . '/' . $item['file']);
$file = $val->sanitize_url($file);
}
else
{
$file = $item['file'];
}
$output .= '<link rel="stylesheet" type="text/css" href="' . $file . '" media="' . (isset($item['media']) ? $item['media'] : 'all') . '" />' . "\n";
}
$seen[$item['file']] = 1;
}
}
return $output;
} // end function getCSS()
/**
* determine default page
*
* @access public
* @return void
**/
public static function getDefaultPage()
{
if ( ! count(self::$pages) )
self::init();
// for all pages with level 0...
$root = array();
$now = time();
$ordered = CAT_Helper_Array::getInstance()->ArraySort ( self::$pages, 'position' );
foreach( $ordered as $page )
{
if (
$page['level'] == 0
&& $page['visibility'] == 'public'
&& self::isActive($page['page_id'])
) {
if(!PAGE_LANGUAGES || $page['language'] == LANGUAGE)
{
return $page['page_id'];
}
}
}
} // end function getDefaultPage()
/**
* returns the number of editable pages
*
* @access public
* @return integer
**/
public static function getEditable()
{
if(!count(self::$pages)) self::init();
return self::$pages_editable;
} // end function getEditable()
/**
* convert page title to a valid filename
*
* @access public
* @param string $string - page title
* @return string
**/
public static function getFilename($string)
{
require_once CAT_PATH . '/framework/functions-utf8.php';
$string = entities_to_7bit($string);
// Now remove all bad characters
$bad = array('\'', '"', '`', '!', '@', '#', '$', '%', '^', '&', '*', '=', '+', '|', '/', '\\', ';', ':', ',', '?');
$string = str_replace($bad, '', $string);
// replace multiple dots in filename to single dot and (multiple) dots at the end of the filename to nothing
$string = preg_replace(array('/\.+/', '/\.+$/'), array('.', ''), $string);
// Now replace spaces with page spcacer
$string = trim($string);
$string = preg_replace('/(\s)+/', PAGE_SPACER, $string);
// Now convert to lower-case
$string = strtolower($string);
// If there are any weird language characters, this will protect us against possible problems they could cause
$string = str_replace(array('%2F', '%'), array('/', ''), urlencode($string));
// Finally, return the cleaned string
return $string;
} // end function getFilename()
/**
* calls appropriate function for analyzing and printing page footers
*
* @access public
* @param string $for - 'backend'/'frontend'
* @return mixed
**/
public static function getFooters($for)
{
// what for?
if (!$for || $for == '' || ($for != 'frontend' && $for != 'backend'))
$for = 'frontend';
if ($for == 'backend')
return self::getBackendFooters();
else
return self::getFrontendFooters();
} // end function getFooters()
/**
* prints the frontend footers
*
* @access public
* @return string
**/
public static function getFrontendFooters()
{
// -----------------------------------------------------------------
// ----- frontend theme -----
// -----------------------------------------------------------------
$tpl = CAT_Registry::get('TEMPLATE');
$file = sanitize_path(CAT_PATH.'/templates/'.$tpl.'/footers.inc.php');
if (file_exists($file))
{
self::$instance->log()->logDebug(sprintf('adding footer items for frontend template [%s]', $tpl));
self::_load_footers_inc($file, 'frontend', 'templates/' . $tpl);
} // end loading theme
// -----------------------------------------------------------------
// ----- scan for js files -----
// -----------------------------------------------------------------
if (count(CAT_Helper_Page::$js_search_path))
{
$val = CAT_Helper_Validate::getInstance();
foreach (CAT_Helper_Page::$js_search_path as $directory)
{
$file = sanitize_path($directory . '/frontend_body.js');
if (file_exists(CAT_PATH . '/' . $file))
{
CAT_Helper_Page::$f_js[] = CAT_Helper_Page::$space . '<script type="text/javascript" src="' . $val->sanitize_url(CAT_URL . $file) . '"></script>' . "\n";
}
}
}
return self::getJQuery('footer') . self::getJavaScripts('footer');
} // end function getFrontendFooters()
/**
*
*
*
*
**/
public static function getFrontendHeaders()
{
global $page_id;
// -----------------------------------------------------------------
// ----- frontend theme -----
// -----------------------------------------------------------------
$tpl = CAT_Registry::get('TEMPLATE');
$file = sanitize_path(CAT_PATH.'/templates/'.$tpl.'/headers.inc.php');
self::$instance->log()->logDebug(sprintf('searching for file [%s]', $file));
if (file_exists($file))
{
self::$instance->log()->logDebug(sprintf('adding items for frontend template [%s]', $tpl));
self::_load_headers_inc($file, 'frontend', 'templates/'.$tpl);
}
else
{
self::$instance->log()->logDebug('no headers.inc.php');
}
// add template path to CSS search path (frontend only)
array_push(
CAT_Helper_Page::$css_search_path,
'/templates/'.$tpl,
'/templates/'.$tpl.'/css',
// for skinnables
'/templates/'.$tpl.'/templates/default',
'/templates/'.$tpl.'/templates/default/css',
// page
CAT_Registry::get('PAGES_DIRECTORY').'/css/',
// search
'/modules/'.CAT_Registry::get('SEARCH_LIBRARY').'/templates/custom/',
'/modules/'.CAT_Registry::get('SEARCH_LIBRARY').'/templates/default/'
);
// Javascript search path
array_push(
CAT_Helper_Page::$js_search_path,
'/templates/'.$tpl,
'/templates/'.$tpl.'/js',
// for skinnables
'/templates/'.$tpl.'/templates/default',
'/templates/'.$tpl.'/templates/default/js',
// page
CAT_Registry::get('PAGES_DIRECTORY').'/js/'
);
// -----------------------------------------------------------------
// ----- sections (modules) -----
// -----------------------------------------------------------------
self::_load_sections('frontend');
// -----------------------------------------------------------------
// ----- scan for css files -----
// -----------------------------------------------------------------
self::_load_css('frontend');
// -----------------------------------------------------------------
// ----- scan for js files -----
// -----------------------------------------------------------------
self::_load_js('frontend');
// called from backend?
if(CAT_Helper_Validate::get('_REQUEST','preview') && CAT_Users::is_authenticated())
{
$file = CAT_PATH.'/templates/'.DEFAULT_THEME.'/css/visibility.css';
if(file_exists($file))
{
CAT_Helper_Page::$css[] = array(
'media' => 'screen,projection',
'file' => '/templates/'.DEFAULT_THEME.'/css/visibility.css'
);
}
$file = CAT_PATH.'/templates/'.DEFAULT_THEME.'/js/visibility.js';
if(file_exists($file))
{
global $page_id;
CAT_Helper_Page::$js[] = '<script type="text/javascript">'."\n"
. ' var visibility = \''.self::getInstance(1)->lang()->translate(self::properties($page_id,'visibility')).'\';'."\n"
. ' var visibility_text = \''.self::getInstance(1)->lang()->translate('Visibility of this page').'\';'."\n"
. ' var visibility_title = \''.self::getInstance(1)->lang()->translate('Black Cat CMS Page Preview').'\';'."\n"
. '</script>' . "\n"
. '<script type="text/javascript" src="' . CAT_Helper_Validate::sanitize_url(CAT_URL.'/templates/'.DEFAULT_THEME.'/js/visibility.js') . '"></script>' . "\n"
;
}
}
// get droplets
$droplets_config = CAT_Helper_Droplet::getDropletsForHeader($page_id);
// return the results
return self::getMeta($droplets_config)
. self::getCSS('frontend')
. ($droplets_config['css'] ? "<!-- dropletsExtension -->\n".$droplets_config['css']."\n<!-- /dropletsExtension -->\n" : NULL)
. self::getJQuery('header')
. self::getJavaScripts('header')
. ($droplets_config['js'] ? "<!-- dropletsExtension -->\n".$droplets_config['js']."\n<!-- /dropletsExtension -->\n" : NULL)
;
} // end function getFrontendHeaders()
/**
* calls appropriate function for analyzing and printing page headers
*
* @access public
* @param string $for - 'backend'/'frontend'
* @param string $section - backend section name to load JS for
* @return mixed
*
**/
public static function getHeaders($for = NULL, $section = false)
{
global $page_id;
// don't do this twice
if (defined('CAT_HEADERS_SENT'))
{
self::$instance->log()->logDebug('headers already sent, returning');
return;
}
// what for?
if (!$for || $for == '' || ($for != 'frontend' && $for != 'backend'))
{
$for = 'frontend';
}
self::$instance->log()->logDebug('creating headers for ['.$for.'], page id: ['.$page_id.']');
// add default
$default = "
var WB_URL = '" . CAT_URL . "',
LEPTON_URL = '" . CAT_URL . "',
CAT_URL = '" . CAT_URL . "';
";
// backend only
if($for == 'backend')
{
$default .= "
var THEME_URL = '" . CAT_THEME_URL . "',
CAT_THEME_URL = '" . CAT_THEME_URL . "',
ADMIN_URL = '" . CAT_ADMIN_URL . "',