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.

256 lines
7.2KB

  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. if ( ! is_array($languages) ) {
  65. $languages = [$languages];
  66. }
  67. $f3->set('default_lang', array_shift($languages));
  68. $getLang = null !== $f3->get('GET.lang') ? strtolower($f3->get('GET.lang')) : false;
  69. if (isset($getLang) && in_array($getLang, $languages)) {
  70. $f3->set('LANG', $getLang);
  71. } else {
  72. $f3->set('LANG', $f3->get('default_lang'));
  73. }
  74. // set content dir
  75. if(array_key_exists($f3->get('LANG'), $f3->get('content'))) {
  76. $content_dir=$f3->get('content.'.$f3->get('LANG'));
  77. } else {
  78. $content_dir=$f3->get('content.'.$f3->get('default_lang'));
  79. }
  80. $f3->set('CONTENT', $content_dir);
  81. $f3->set('CONTENT_BASE', array_shift(explode("/",$f3->get('CONTENT'))).'/');
  82. $f3->set('UI', implode(';', array(
  83. ROOT.$f3->get('templatepath'),
  84. ROOT.'app/views/',
  85. ROOT.$f3->get('CONTENT_BASE') // content folders can contain .html templates
  86. )));
  87. function menu_recursion($m,$root="") {
  88. $f3 = \Base::instance();
  89. //$url = explode("/", $f3->get('url'));
  90. $out=[];
  91. foreach ($m as $key=>$value) {
  92. $folder = $key == "index" ? "" : "/".$key;
  93. $path = $root.$folder;
  94. if (is_array($value)) {
  95. # submenu
  96. $submenu=menu_recursion($value,$path);
  97. $out[$path]=$submenu[$path];
  98. unset($submenu[$path]);
  99. $out[$path]['submenu']=$submenu;
  100. } else {
  101. $class="";
  102. $ex = explode(":=", $value);
  103. if (count($ex) > 1) {
  104. # external link
  105. $href = trim($ex[1]);
  106. if(!strncmp($href,"?",1)) {
  107. // if 'external link' starts with '?'
  108. // probably it is language switch
  109. foreach (explode("&",substr($href,1)) as $option) {
  110. if(!strncmp($option, "lang=", 5) !== FALSE) {
  111. $value = substr( $option, 5);
  112. $class .= ($value == $f3->get('LANG')) ? ' active' : ' away';
  113. }
  114. }
  115. }
  116. } else {
  117. # internal link
  118. $href = $f3->get('SITE_URL').$path;
  119. $href .= count($f3->get('GET')) ? "?".query_string_from_array($f3->get('GET')):"";
  120. $class .= in_array($path,$f3->get('url')) ? ' active' : ' away';
  121. }
  122. $out[$path]['name']=$ex[0];
  123. $out[$path]['href']=$href;
  124. $out[$path]['class']=$class;
  125. }
  126. }
  127. return $out;
  128. }
  129. function read_menu($menu_name) {
  130. $f3 = \Base::instance();
  131. if(array_key_exists($f3->get('LANG'), $f3->get('nav.'.$menu_name))) {
  132. $menu=$f3->get('nav.'.$menu_name.'.'.$f3->get('LANG'));
  133. } else {
  134. $menu=$f3->get('nav.'.$menu_name.'.'.$f3->get('default_lang'));
  135. }
  136. if (is_array($menu)) {
  137. $menu=menu_recursion($menu);
  138. }
  139. $f3->set('navi.'.$menu_name, $menu);
  140. }
  141. // this is needed so the menu can compare the current page with a link to it
  142. // and use this information to determine the active state
  143. $tmp_url = substr($_SERVER['REQUEST_URI'],1);
  144. $url=substr($tmp_url,0,(strpos($tmp_url,'?') === false) ? 999 : strpos($tmp_url,'?'));
  145. $url_ex=explode('/',$url);
  146. $url=array();
  147. for ($i=1;$i<=count($url_ex);$i++) {
  148. $url[]="/".implode("/",array_slice($url_ex,0,$i));
  149. }
  150. $f3->set('url', $url);
  151. $f3->set('is_home', in_array($url[0],["/","/home"]));
  152. $f3->set('level', 1); // this is for templates, to be abl to count nesting
  153. if (is_array($f3->get('nav'))) {
  154. foreach ($f3->get('nav') as $k=>$v) {
  155. read_menu($k);
  156. }
  157. }
  158. ///////////////////////////////////
  159. // development utility functions //
  160. ///////////////////////////////////
  161. ##############################################################################
  162. // this need to go away from here -----------------------------------------
  163. // just to be more tidy etc.
  164. function is_image($path) {
  165. $ex = explode('.',$path);
  166. $ext = array_pop($ex);
  167. return in_array($ext,array( 'jpg', 'jpeg', 'png' ));
  168. }
  169. function pic_cache($in1,$inwidth=360){
  170. $f3 = \Base::instance();
  171. if(is_image($in1)) {
  172. $info = pathinfo($in1);
  173. $fn = basename($in1,'.'.$info['extension']);
  174. $width=$inwidth;
  175. $name = md5($in1.$width);
  176. $name = sprintf("%s%s.png",$fn,$name);
  177. $out ='rsc/img_display/s'.$name;
  178. if(!file_exists($in1)) { $in1='rsc/img/default.png'; }
  179. if(!file_exists($out)) {
  180. $img1 = new Image($in1);
  181. $img1->resize($width);
  182. $f3->write($out,$img1->dump('png',9));
  183. }
  184. } elseif(!strncmp("locallink:",$in1, strlen("locallink:"))) {
  185. $key = substr($in1, strlen("locallink:"));
  186. // localize internal links
  187. if($f3->get('LANG') != 'DE') {
  188. $key .= "?lang=".$f3->get('LANG');
  189. }
  190. $out = $key;
  191. } else {
  192. $out = $in1;
  193. }
  194. return $out;
  195. }
  196. //------------------------------------------------------------------------
  197. ###############################################################################
  198. // HTML preloading of images
  199. // this could also find some better place
  200. $f3->mset(array(
  201. 'cached_images' => array(\Controller\Page::check_folder_for_backgroundimage($f3->get('CONTENT_BASE'))
  202. )
  203. ));
  204. debug("before run fatfree");
  205. $bbb = new \Controller\Cart();
  206. $bbb->index();
  207. $f3->run();
  208. if ($f3->get("GET.admin")) {
  209. $admin = new \Controller\Admin;
  210. $f3->set('backend',$admin->index());
  211. } else {
  212. $f3->set('backend',false);
  213. }
  214. echo \Template::instance()->render($f3->get('template'));