mostly filebased Content Presentation System
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

255 行
8.7KB

  1. <?php
  2. namespace Controller;
  3. class Page {
  4. public static $keyword = "page"; // variable in URL (query string)
  5. function index(\Base $f3, $params) {
  6. /////////////////////////////
  7. // what page are we watching?
  8. $page = $params[self::$keyword];
  9. $f3->set('page', $page);
  10. $f3->set('bodyClass',"page-$page");
  11. switch ($page) {
  12. case 'home':
  13. default:
  14. $f3->mset(array(
  15. 'template'=>$f3->get('template') ? :'tpl/index.html',
  16. 'templateContent'=>'maincontent.html'
  17. ));
  18. self::folder2();
  19. $footer_content = $f3->get('footerContent') ? : false;
  20. if ($footer_content) {
  21. $footer = new \Modules\FilesInFolders(
  22. $f3->get('CONTENT')."footer/",
  23. array('content'=>array(
  24. 'secondary'=>'secondary',
  25. 'zzz'=>'hidden',
  26. 'unpublish'=>'hidden'
  27. ))
  28. );
  29. $footer->prepare_files();
  30. $footer->fill_content();
  31. $f3->set('footer_content', implode("\n", $footer->content['default']));
  32. } else {
  33. $f3->set('footer_content', "");
  34. }
  35. break;
  36. }
  37. }
  38. ////////////////
  39. // interfaces //
  40. ////////////////
  41. function home(\Base $f3) {
  42. self::index($f3, array('page'=>'home'));
  43. }
  44. function secondLevel(\Base $f3,$params) {
  45. self::index($f3, array('page'=>$params['level1']."/".$params['level2']));
  46. }
  47. function thirdLevel(\Base $f3,$params) {
  48. self::index($f3, array('page'=>$params['level1']."/".$params['level2']."/".$params['level3']));
  49. }
  50. function fourthLevel(\Base $f3,$params) {
  51. self::index($f3, array('page'=>$params['level1']."/".$params['level2']."/".$params['level3']."/".$params['level4']));
  52. }
  53. //////////////////
  54. // main program //
  55. //////////////////
  56. function folder2() {
  57. $f3 = \Base::instance();
  58. $page = $f3->get('page');
  59. $f3->set('current_page_folder', $f3->get('CONTENT').$page."/");
  60. $folder = new \Modules\FilesInFolders(
  61. $f3->get('current_page_folder'),
  62. array(
  63. 'content'=>array(
  64. 'secondary'=>'secondary',
  65. 'zzz'=>'hidden',
  66. 'unpublish'=>'hidden'
  67. ),
  68. 'keyfiles'=>array(
  69. 'logo'=>array(
  70. 'until'=>$f3->get("CONTENT"),
  71. 'type'=>'pic'
  72. ),
  73. 'banner'=>array(
  74. 'until'=>$f3->get("CONTENT"),
  75. 'type'=>'pic'
  76. ),
  77. 'background'=>array(
  78. 'until'=>$f3->get("CONTENT"),
  79. 'type'=>'pic'
  80. ),
  81. 'colors'=>array(
  82. 'until'=>$f3->get("CONTENT"),
  83. 'type'=>'txt'
  84. )
  85. )
  86. )
  87. );
  88. $folder->prepare_files();
  89. if ( null !== $f3->get('customVars')) {
  90. foreach($f3->get('customVars') as $key=>$value) {
  91. $f3->set($key,$value);
  92. $this->register_key($key,$folder);
  93. }
  94. }
  95. $folder->fill_content();
  96. foreach($f3->get('siteColors') as $k=>$v){
  97. $f3->set($k,
  98. array_key_exists($k,$folder->config)
  99. ? $folder->config[$k]
  100. : $v
  101. );
  102. }
  103. $f3->set('hasBanner',
  104. array_key_exists('includeBanner',$folder->config)
  105. ? $folder->config['includeBanner']
  106. : $f3->get('templateVars.includeBanner')
  107. );
  108. if ($f3->get('hasBanner')) {
  109. //$banner = new \Modules\CachedImage($folder->extras['banner']);
  110. $f3->set('banner',$folder->extras['banner']);//$banner->get_src(2000));
  111. }
  112. if (isset($folder->extras['logo'])) {
  113. $f3->set('logo', $folder->extras['logo']);
  114. }
  115. if ($folder->extras['background']
  116. && empty($folder->config['supressBackground'])) {
  117. //$background = new \Modules\CachedImage($folder->extras['background']);
  118. //$f3->set('backgroundImage',$background->get_src(3000));
  119. $background = $folder->extras['background'];
  120. $f3->set('backgroundImage',$background);
  121. }
  122. // $f3->set('background',$background);
  123. $f3->set('content', implode("\n", $folder->content['default']));
  124. $f3->set('secondary_content', implode("\n", $folder->content['secondary']));
  125. }
  126. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  127. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  128. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  129. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  130. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  131. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  132. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  133. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  134. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  135. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  136. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  137. ////////////////
  138. // recursions //
  139. ////////////////
  140. function search_up($key,$paths,$ext) {
  141. $return = "";
  142. if(count($paths) == 2) {
  143. $current = $paths[0];
  144. $last_try = $paths[1];
  145. $ls=scandir($current);
  146. foreach($ls as $f) {
  147. if(!strncmp($f,'.',1)) continue; // ignore hidden files
  148. $ex=explode(".", $f);
  149. if(in_array(strtolower(end($ex)),$ext)) {
  150. if($ex[0]==$key) {
  151. $return = $current.$f;
  152. break;
  153. }
  154. }
  155. }
  156. }
  157. if ($return) {
  158. return $return;
  159. } elseif($current == $last_try) {
  160. return false;
  161. } else {
  162. $p = explode('/',$current);
  163. array_pop($p);
  164. array_pop($p);
  165. return self::search_up($key,array(implode("/",$p)."/",$last_try),$ext);
  166. }
  167. }
  168. ///////////////////////
  169. // Utility functions //
  170. ///////////////////////
  171. function register_key($name, &$ff) {
  172. $f3 = \Base::instance();
  173. if (array_key_exists($name,$ff->config)) {
  174. $f3->set($name,$ff->config[$name]);
  175. }
  176. }
  177. function linkify($string) {
  178. $pattern = "/\s@(\w+)[=]([\w,]+)\s/";
  179. $count = 0;
  180. $new = preg_replace_callback
  181. ($pattern,
  182. function($m){
  183. $f3 = \Base::instance();
  184. return $f3->get('SITE_URL')
  185. .$f3->alias($m[1],self::$keyword."=".$m[2])
  186. ;},
  187. $string);
  188. return $new;
  189. }
  190. function get_config_from_content($string) {
  191. $f3 = \Base::instance();
  192. $f = 0;
  193. $pattern = "/#\+(\w+):\s?(.*)/";
  194. $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
  195. foreach($matches[0] as $match) {
  196. $string = str_replace($match,"",$string);
  197. }
  198. foreach($matches[1] as $key => $match) {
  199. $f3->set('content_config_'.$match,$matches[2][$key]);
  200. }
  201. return $string;
  202. }
  203. public static function check_folder_for_backgroundimage($folder){
  204. $EXT=array(
  205. 'pic'=>array( 'jpg', 'jpeg', 'png' ),
  206. 'tpl'=>array( 'html', 'htm', 'tpl' ),
  207. 'txt'=>array( 'txt', 'text', 'md' )
  208. );
  209. $ls = scandir($folder);
  210. $background = false;
  211. foreach ($ls as $key=>$f) {
  212. if(!strncmp($f,'.',1)) continue;
  213. $ex=explode(".", $f);
  214. $ext=array_pop($ex);
  215. if(in_array(strtolower($ext), $EXT['pic'])) {
  216. if($ex[0]=='background') {
  217. $background=pic_cache($folder.$f,836);
  218. break;
  219. }
  220. }
  221. }
  222. return $background;
  223. }
  224. }