mostly filebased Content Presentation System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
7.0KB

  1. <?php
  2. function debug($message) {
  3. $f3 = \Base::instance();
  4. if ($f3->get('DEBUG')) {
  5. printf("<hr>%s:<br>Memory Usage: %s <br>", $message, memory_get_usage());
  6. }
  7. }
  8. function urlsafe_b64encode($string) {
  9. $data = base64_encode($string);
  10. $data = str_replace(array('+','/','='),array('-','_','.'),$data);
  11. return $data;
  12. }
  13. function urlsafe_b64decode($string) {
  14. $data = str_replace(array('-','_','.'),array('+','/','='),$string);
  15. $mod4 = strlen($data) % 4;
  16. if ($mod4) {
  17. $data .= substr('====', $mod4);
  18. }
  19. return base64_decode($data);
  20. }
  21. /////////////////////////////
  22. // configure installation: //
  23. /////////////////////////////
  24. define("ROOT", "./");
  25. require_once( ROOT.'lib/autoload.php' );
  26. error_reporting(0);
  27. ini_set("log_errors", 1);
  28. ini_set("error_log", "php-error.log");
  29. /** @var \Base $f3 */
  30. $f3 = \Base::instance();
  31. $f3->set('DEBUG', 0);
  32. $f3->set('CACHE', FALSE);
  33. $f3->set('AUTOLOAD', ROOT.'app/');
  34. if (!is_dir($f3->get('TEMP'))) {
  35. mkdir($f3->get('TEMP'));
  36. }
  37. function query_string_from_array($in){
  38. $out = [];
  39. foreach ($in as $key=>$value) {
  40. $out[] = sprintf("%s=%s", $key, $value);
  41. }
  42. return implode("&", $out);
  43. }
  44. /////////////////////////
  45. // main configuration: //
  46. /////////////////////////
  47. debug("before read config");
  48. $f3->config( ROOT.'routes.cfg' );
  49. $f3->config( ROOT.'main.cfg' );
  50. if(!setlocale(LC_TIME, 'de_DE.UTF-8')) {
  51. //echo "locale not set";
  52. }
  53. ///////////////////////////////////////////////
  54. // configuration based on configuration file //
  55. ///////////////////////////////////////////////
  56. // rerouting
  57. if(is_array($f3->get('reroutes'))) {
  58. foreach ($f3->get('reroutes') as $source=>$dest) {
  59. $f3->redirect('GET '.$source, $dest);
  60. }
  61. }
  62. // set language
  63. $languages = $f3->get('languages');
  64. $f3->set('default_lang', array_shift($languages));
  65. if (in_array(strtolower($f3->get('GET.lang')), $languages)) {
  66. $f3->set('LANG', strtolower($f3->get('GET.lang')));
  67. } else {
  68. $f3->set('LANG', $f3->get('default_lang'));
  69. }
  70. // set content dir
  71. if(array_key_exists($f3->get('LANG'), $f3->get('content'))) {
  72. $content_dir=$f3->get('content.'.$f3->get('LANG'));
  73. } else {
  74. $content_dir=$f3->get('content.'.$f3->get('default_lang'));
  75. }
  76. $f3->set('CONTENT', $content_dir);
  77. $f3->set('CONTENT_BASE', array_shift(explode("/",$f3->get('CONTENT'))).'/');
  78. $f3->set('UI', implode(';', array(
  79. ROOT.'app/views/',
  80. ROOT.$f3->get('CONTENT_BASE') // content folders can contain .html templates
  81. )));
  82. function menu_recursion($m,$root="") {
  83. $f3 = \Base::instance();
  84. //$url = explode("/", $f3->get('url'));
  85. $out=[];
  86. foreach ($m as $key=>$value) {
  87. $folder = $key == "index" ? "" : "/".$key;
  88. $path = $root.$folder;
  89. if (is_array($value)) {
  90. # submenu
  91. $submenu=menu_recursion($value,$path);
  92. $out[$path]=$submenu[$path];
  93. unset($submenu[$path]);
  94. $out[$path]['submenu']=$submenu;
  95. } else {
  96. $class="";
  97. $ex = explode(":=", $value);
  98. if (count($ex) > 1) {
  99. # external link
  100. $href = trim($ex[1]);
  101. if(!strncmp($href,"?",1)) {
  102. // if 'external link' starts with '?'
  103. // probably it is language switch
  104. foreach (explode("&",substr($href,1)) as $option) {
  105. if(!strncmp($option, "lang=", 5) !== FALSE) {
  106. $value = substr( $option, 5);
  107. $class .= ($value == $f3->get('LANG')) ? ' active' : ' away';
  108. }
  109. }
  110. }
  111. } else {
  112. # internal link
  113. $href = $f3->get('SITE_URL').$path;
  114. $href .= count($f3->get('GET')) ? "?".query_string_from_array($f3->get('GET')):"";
  115. $class .= in_array($path,$f3->get('url')) ? ' active' : ' away';
  116. }
  117. $out[$path]['name']=$ex[0];
  118. $out[$path]['href']=$href;
  119. $out[$path]['class']=$class;
  120. }
  121. }
  122. return $out;
  123. }
  124. function read_menu($menu_name) {
  125. $f3 = \Base::instance();
  126. if(array_key_exists($f3->get('LANG'), $f3->get('nav.'.$menu_name))) {
  127. $menu=$f3->get('nav.'.$menu_name.'.'.$f3->get('LANG'));
  128. } else {
  129. $menu=$f3->get('nav.'.$menu_name.'.'.$f3->get('default_lang'));
  130. }
  131. if (is_array($menu)) {
  132. $menu=menu_recursion($menu);
  133. }
  134. $f3->set('navi.'.$menu_name, $menu);
  135. }
  136. // this is needed so the menu can compare the current page with a link to it
  137. // and use this information to determine the active state
  138. $tmp_url = substr($_SERVER['REQUEST_URI'],1);
  139. $url=substr($tmp_url,0,(strpos($tmp_url,'?') === false) ? 999 : strpos($tmp_url,'?'));
  140. $url_ex=explode('/',$url);
  141. $url=array();
  142. for ($i=1;$i<=count($url_ex);$i++) {
  143. $url[]="/".implode("/",array_slice($url_ex,0,$i));
  144. }
  145. $f3->set('url', $url);
  146. $f3->set('is_home', in_array($url[0],["/","/home"]));
  147. $f3->set('level', 1); // this is for templates, to be abl to count nesting
  148. if (is_array($f3->get('nav'))) {
  149. foreach ($f3->get('nav') as $k=>$v) {
  150. read_menu($k);
  151. }
  152. }
  153. ///////////////////////////////////
  154. // development utility functions //
  155. ///////////////////////////////////
  156. ##############################################################################
  157. // this need to go away from here -----------------------------------------
  158. // just to be more tidy etc.
  159. function is_image($path) {
  160. $ex = explode('.',$path);
  161. $ext = array_pop($ex);
  162. return in_array($ext,array( 'jpg', 'jpeg', 'png' ));
  163. }
  164. function pic_cache($in1,$inwidth=360){
  165. $f3 = \Base::instance();
  166. if(is_image($in1)) {
  167. $info = pathinfo($in1);
  168. $fn = basename($in1,'.'.$info['extension']);
  169. $width=$inwidth;
  170. $name = md5($in1.$width);
  171. $name = sprintf("%s%s.png",$fn,$name);
  172. $out ='rsc/img_display/s'.$name;
  173. if(!file_exists($in1)) { $in1='rsc/img/default.png'; }
  174. if(!file_exists($out)) {
  175. $img1 = new Image($in1);
  176. $img1->resize($width);
  177. $f3->write($out,$img1->dump('png',9));
  178. }
  179. } elseif(!strncmp("locallink:",$in1, strlen("locallink:"))) {
  180. $key = substr($in1, strlen("locallink:"));
  181. // localize internal links
  182. if($f3->get('LANG') != 'DE') {
  183. $key .= "?lang=".$f3->get('LANG');
  184. }
  185. $out = $key;
  186. } else {
  187. $out = $in1;
  188. }
  189. return $out;
  190. }
  191. //------------------------------------------------------------------------
  192. ###############################################################################
  193. if ($f3->get("GET.admin")) {
  194. $admin = new \Controller\Admin;
  195. $f3->set('backend',$admin->index());
  196. } else {
  197. $f3->set('backend',false);
  198. }
  199. // HTML preloading of images
  200. // this could also find some better place
  201. $f3->mset(array(
  202. 'cached_images' => array(\Controller\Page::check_folder_for_backgroundimage($f3->get('CONTENT_BASE'))
  203. )
  204. ));
  205. debug("before run fatfree");
  206. $f3->run();
  207. echo \Template::instance()->render($f3->get('template'));