|
- <?php
-
- function debug($message) {
- $f3 = \Base::instance();
- if ($f3->get('DEBUG')) {
- printf("<hr>%s:<br>Memory Usage: %s <br>", $message, memory_get_usage());
- }
- }
-
- /////////////////////////////
- // configure installation: //
- /////////////////////////////
- define("ROOT", "./");
- require_once( ROOT.'lib/autoload.php' );
- error_reporting(0);
- ini_set("log_errors", 1);
- ini_set("error_log", "php-error.log");
-
- /** @var \Base $f3 */
- $f3 = \Base::instance();
- $f3->set('DEBUG', 0);
- $f3->set('CACHE', FALSE);
- $f3->set('AUTOLOAD', ROOT.'app/');
- $f3->set('UI', implode(';', array(
- ROOT.'app/views/',
- ROOT.'content/' // content folders can contain .html templates
- )));
-
- if (!is_dir($f3->get('TEMP'))) {
- mkdir($f3->get('TEMP'));
- }
-
- function query_string_from_array($in){
- $out = [];
- foreach ($in as $key=>$value) {
- $out[] = sprintf("%s=%s", $key, $value);
- }
- return implode("&", $out);
- }
-
- /////////////////////////
- // main configuration: //
- /////////////////////////
- debug("before read config");
- $f3->config( ROOT.'routes.cfg' );
- $f3->config( ROOT.'main.cfg' );
- if(!setlocale(LC_TIME, 'de_DE.UTF-8')) {
-
- //echo "locale not set";
- }
-
- ///////////////////////////////////////////////
- // configuration based on configuration file //
- ///////////////////////////////////////////////
- // rerouting
- if(is_array($f3->get('reroutes'))) {
- foreach ($f3->get('reroutes') as $source=>$dest) {
- $f3->redirect('GET '.$source, $dest);
- }
- }
-
- // set language
- $languages = $f3->get('languages');
- $f3->set('default_lang', array_shift($languages));
- if (in_array(strtolower($f3->get('GET.lang')), $languages)) {
- $f3->set('LANG', strtolower($f3->get('GET.lang')));
- } else {
- $f3->set('LANG', $f3->get('default_lang'));
- }
- // set content dir
- if(array_key_exists($f3->get('LANG'), $f3->get('content'))) {
- $content_dir=$f3->get('content.'.$f3->get('LANG'));
- } else {
- $content_dir=$f3->get('content.'.$f3->get('default_lang'));
- }
- $f3->set('CONTENT', $content_dir);
-
-
- function menu_recursion($m,$root="") {
- $f3 = \Base::instance();
- //$url = explode("/", $f3->get('url'));
- $out=[];
- foreach ($m as $key=>$value) {
- $folder = $key == "index" ? "" : "/".$key;
- $path = $root.$folder;
-
- if (is_array($value)) {
- # submenu
- $submenu=menu_recursion($value,$path);
- $out[$path]=$submenu[$path];
- unset($submenu[$path]);
- $out[$path]['submenu']=$submenu;
- } else {
- $class="";
- $ex = explode(":=", $value);
- if (count($ex) > 1) {
- # external link
- $href = trim($ex[1]);
-
- if(!strncmp($href,"?",1)) {
- // if 'external link' starts with '?'
- // probably it is language switch
- foreach (explode("&",substr($href,1)) as $option) {
- if(!strncmp($option, "lang=", 5) !== FALSE) {
- $value = substr( $option, 5);
- $class .= ($value == $f3->get('LANG')) ? ' active' : ' away';
- }
- }
- }
-
- } else {
- # internal link
- $href = $f3->get('SITE_URL').$path;
- $href .= count($f3->get('GET')) ? "?".query_string_from_array($f3->get('GET')):"";
- $class .= in_array($path,$f3->get('url')) ? ' active' : ' away';
- }
- $out[$path]['name']=$ex[0];
- $out[$path]['href']=$href;
- $out[$path]['class']=$class;
- }
- }
- return $out;
- }
- function read_menu($menu_name) {
- $f3 = \Base::instance();
-
- if(array_key_exists($f3->get('LANG'), $f3->get('nav.'.$menu_name))) {
- $menu=$f3->get('nav.'.$menu_name.'.'.$f3->get('LANG'));
- } else {
- $menu=$f3->get('nav.'.$menu_name.'.'.$f3->get('default_lang'));
- }
-
- if (is_array($menu)) {
- $menu=menu_recursion($menu);
- }
-
- $f3->set('navi.'.$menu_name, $menu);
- }
-
- // this is needed so the menu can compare the current page with a link to it
- // and use this information to determine the active state
- $tmp_url = substr($_SERVER['REQUEST_URI'],1);
- $url=substr($tmp_url,0,(strpos($tmp_url,'?') === false) ? 999 : strpos($tmp_url,'?'));
- $url_ex=explode('/',$url);
- $url=array();
- for ($i=1;$i<=count($url_ex);$i++) {
- $url[]="/".implode("/",array_slice($url_ex,0,$i));
- }
- $f3->set('url', $url);
- $f3->set('is_home', in_array($url[0],["/","/home"]));
- $f3->set('level', 1); // this is for templates, to be abl to count nesting
-
- if (is_array($f3->get('nav'))) {
- foreach ($f3->get('nav') as $k=>$v) {
- read_menu($k);
- }
- }
-
-
-
-
- ///////////////////////////////////
- // development utility functions //
- ///////////////////////////////////
-
-
-
- ##############################################################################
- // this need to go away from here -----------------------------------------
- // just to be more tidy etc.
- function is_image($path) {
- $ex = explode('.',$path);
- $ext = array_pop($ex);
- return in_array($ext,array( 'jpg', 'jpeg', 'png' ));
- }
- function pic_cache($in1,$inwidth=360){
- $f3 = \Base::instance();
- if(is_image($in1)) {
- $info = pathinfo($in1);
- $fn = basename($in1,'.'.$info['extension']);
- $width=$inwidth;
- $name = md5($in1.$width);
-
- $name = sprintf("%s%s.png",$fn,$name);
-
- $out ='rsc/img_display/s'.$name;
-
- if(!file_exists($in1)) { $in1='rsc/img/default.png'; }
-
- if(!file_exists($out)) {
- $img1 = new Image($in1);
- $img1->resize($width);
- $f3->write($out,$img1->dump('png',9));
- }
- } elseif(!strncmp("locallink:",$in1, strlen("locallink:"))) {
- $key = substr($in1, strlen("locallink:"));
- // localize internal links
- if($f3->get('LANG') != 'DE') {
- $key .= "?lang=".$f3->get('LANG');
- }
- $out = $key;
- } else {
- $out = $in1;
- }
- return $out;
- }
- //------------------------------------------------------------------------
- ###############################################################################
-
- if ($f3->get("GET.admin")) {
- $admin = new \Controller\Admin;
- $f3->set('backend',$admin->index());
- } else {
- $f3->set('backend',false);
- }
-
- // HTML preloading of images
- // this could also find some better place
- $f3->mset(array(
- 'cached_images' => array(\Controller\Page::check_folder_for_backgroundimage('content/'))
- ));
-
-
- debug("before run fatfree");
- $f3->run();
- echo \Template::instance()->render($f3->get('template'));
|