From e33c245d910e55b8cab407a03e669470509a705d Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Fri, 10 Mar 2017 21:46:12 +0100 Subject: Several changes have been made in this commit, which together with the previous commits result in version 1.1: + The rules for the Apache and nginx configuration have been changed and redirects now all requests to the index.php. + A router class has been added which now handles all requests that arrives at the application on the index.php. + Short-hand functions "PAGE", "POST" and "USER" for use in templates added to get specific item data by ID. + More language variables have been added to the core language. --- core/application.php | 2 +- core/configuration-example.php | 52 +++++++++++++++++----------------- core/functions.php | 42 +++++++++++++++++++++++++-- core/language/de.php | 7 +++++ core/language/en.php | 7 +++++ core/namespace/Router.php | 64 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 144 insertions(+), 30 deletions(-) create mode 100644 core/namespace/Router.php (limited to 'core') diff --git a/core/application.php b/core/application.php index 58dd728..11f5b83 100644 --- a/core/application.php +++ b/core/application.php @@ -3,7 +3,7 @@ # Application initialization [Thomas Lange ] # #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# # # -# This file is included by each file from the system or admin directory. # +# This file brings the application up! # # # #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# diff --git a/core/configuration-example.php b/core/configuration-example.php index 1416621..d56ce84 100644 --- a/core/configuration-example.php +++ b/core/configuration-example.php @@ -23,7 +23,7 @@ Application::set('BLOGMETA.MAIL', 'mail@example.org'); Application::set('BLOGMETA.LANG', 'en'); #=============================================================================== -# Settings for database connection +# Database configuration #=============================================================================== Application::set('DATABASE.HOSTNAME', 'localhost'); Application::set('DATABASE.BASENAME', 'blog'); @@ -31,33 +31,47 @@ Application::set('DATABASE.USERNAME', ''); Application::set('DATABASE.PASSWORD', ''); #=============================================================================== -# Backend configuration +# Template configuration #=============================================================================== -Application::set('ADMIN.TEMPLATE', 'admin'); -Application::set('ADMIN.LANGUAGE', Application::get('CORE.LANGUAGE')); +Application::set('TEMPLATE.NAME', 'standard'); +Application::set('TEMPLATE.LANG', Application::get('CORE.LANGUAGE')); #=============================================================================== -# Settings for template configuration +# Backend configuration #=============================================================================== -Application::set('TEMPLATE.NAME', 'standard'); -Application::set('TEMPLATE.LANG', Application::get('CORE.LANGUAGE')); +Application::set('ADMIN.TEMPLATE', 'admin'); +Application::set('ADMIN.LANGUAGE', Application::get('CORE.LANGUAGE')); #=============================================================================== -# Protocol, hostname and path for this installation +# Protocol, hostname and base directory for this installation #=============================================================================== Application::set('PATHINFO.PROT', $_SERVER['REQUEST_SCHEME']); Application::set('PATHINFO.HOST', $_SERVER['HTTP_HOST']); Application::set('PATHINFO.BASE', ''); #=============================================================================== -# Enable or disable the use of slug URLs for item permalinks +# Item base directories +#=============================================================================== +Application::set('PAGE.DIRECTORY', 'page'); +Application::set('POST.DIRECTORY', 'post'); +Application::set('USER.DIRECTORY', 'user'); + +#=============================================================================== +# Use slug URLs for item permalinks #=============================================================================== Application::set('PAGE.SLUG_URLS', TRUE); Application::set('POST.SLUG_URLS', TRUE); Application::set('USER.SLUG_URLS', TRUE); #=============================================================================== -# Number of items to display on feed and overview sites +# Parse emoticons in items content +#=============================================================================== +Application::set('PAGE.EMOTICONS', TRUE); +Application::set('POST.EMOTICONS', TRUE); +Application::set('USER.EMOTICONS', TRUE); + +#=============================================================================== +# Number of items to show on feed and item overview #=============================================================================== Application::set('PAGE.LIST_SIZE', 10); Application::set('POST.LIST_SIZE', 10); @@ -66,28 +80,14 @@ Application::set('PAGE.FEED_SIZE', 25); Application::set('POST.FEED_SIZE', 25); #=============================================================================== -# Settings for item URL generation (you have to change the .htaccess as well!) -#=============================================================================== -Application::set('PAGE.DIRECTORY', 'page'); -Application::set('POST.DIRECTORY', 'post'); -Application::set('USER.DIRECTORY', 'user'); - -#=============================================================================== -# Enable or disable the use of emoticons for item content -#=============================================================================== -Application::set('PAGE.EMOTICONS', TRUE); -Application::set('POST.EMOTICONS', TRUE); -Application::set('USER.EMOTICONS', TRUE); - -#=============================================================================== -# Number of characters to display in the items description +# Number of characters to show in the items description #=============================================================================== Application::set('PAGE.DESCRIPTION_SIZE', 200); Application::set('POST.DESCRIPTION_SIZE', 200); Application::set('USER.DESCRIPTION_SIZE', 200); #=============================================================================== -# "ORDER BY" clause for item sorting on feed and overview sites +# "ORDER BY" clause for item sorting on feed and item overview #=============================================================================== Application::set('PAGE.LIST_SORT', 'time_insert DESC'); Application::set('POST.LIST_SORT', 'time_insert DESC'); diff --git a/core/functions.php b/core/functions.php index 726e96f..471c14f 100644 --- a/core/functions.php +++ b/core/functions.php @@ -308,9 +308,9 @@ function excerpt($string, $length = 500, $replace = ' […]') { return $string; } -#==================================================================================================== +#=============================================================================== # Generate a valid slug URL part from a string -#==================================================================================================== +#=============================================================================== function makeSlugURL($string) { $string = strtolower($string); $string = str_replace(['ä', 'ö', 'ü', 'ß'], ['ae', 'oe', 'ue', 'ss'], $string); @@ -319,4 +319,40 @@ function makeSlugURL($string) { return trim($string, '-'); } -?> + +#=============================================================================== +# Function to get data from specific page in templates +#=============================================================================== +function PAGE($id) { + try { + $Page = Page\Factory::build($id); + return generatePageItemData($Page); + } catch(Page\Exception $Exception) { + return NULL; + } +} + +#=============================================================================== +# Function to get data from specific post in templates +#=============================================================================== +function POST($id) { + try { + $Post = Post\Factory::build($id); + return generatePostItemData($Post); + } catch(Post\Exception $Exception) { + return NULL; + } +} + +#=============================================================================== +# Function to get data from specific user in templates +#=============================================================================== +function USER($id) { + try { + $User = User\Factory::build($id); + return generateUserItemData($User); + } catch(User\Exception $Exception) { + return NULL; + } +} +?> \ No newline at end of file diff --git a/core/language/de.php b/core/language/de.php index dc6e924..c310719 100644 --- a/core/language/de.php +++ b/core/language/de.php @@ -191,4 +191,11 @@ $LANGUAGE['title_user_overview'] = "{$LANGUAGE['user_overview']} [%d]"; #=============================================================================== $LANGUAGE['title_search_request'] = 'Volltextsuche'; $LANGUAGE['title_search_results'] = 'Ergebnisse für "%s"'; + +#=============================================================================== +# Feed names +#=============================================================================== +$LANGUAGE['feed_name_items'] = '%s [alle Inhalte]'; +$LANGUAGE['feed_name_pages'] = '%s [nur Seiten]'; +$LANGUAGE['feed_name_posts'] = '%s [nur Beiträge]'; ?> \ No newline at end of file diff --git a/core/language/en.php b/core/language/en.php index 3e48191..55c0ce3 100644 --- a/core/language/en.php +++ b/core/language/en.php @@ -191,4 +191,11 @@ $LANGUAGE['title_user_overview'] = "{$LANGUAGE['user_overview']} [%d]"; #=============================================================================== $LANGUAGE['title_search_request'] = 'Fulltext search'; $LANGUAGE['title_search_results'] = 'Results for "%s"'; + +#=============================================================================== +# Feed names +#=============================================================================== +$LANGUAGE['feed_name_items'] = '%s [all content]'; +$LANGUAGE['feed_name_pages'] = '%s [only pages]'; +$LANGUAGE['feed_name_posts'] = '%s [only posts]'; ?> \ No newline at end of file diff --git a/core/namespace/Router.php b/core/namespace/Router.php new file mode 100644 index 0000000..22784c2 --- /dev/null +++ b/core/namespace/Router.php @@ -0,0 +1,64 @@ + 'route', + 'pattern' => $pattern, + 'callback' => $callback + ]; + } + + #=============================================================================== + # Add redirect + #=============================================================================== + public static function addRedirect($pattern, $location, $code = 302) { + $pattern = Application::get('PATHINFO.BASE').$pattern; + + return self::$routes[] = [ + 'type' => 'redirect', + 'code' => $code, + 'pattern' => $pattern, + 'location' => $location + ]; + } + + #=============================================================================== + # Execute routing + #=============================================================================== + public static function execute($path) { + $path = ltrim($path, '/'); + $route_found = FALSE; + + foreach(self::$routes as $route) { + if($route['type'] === 'redirect') { + $location = preg_replace("#^{$route['pattern']}$#", $route['location'], $path, -1, $count); + + if($count) { + HTTP::redirect($location, $route['code']); + } + } + + else { + if(preg_match("#^{$route['pattern']}$#", $path, $matches)) { + # Remove the first element from matches which contains the whole string. + array_shift($matches); + + $route_found = TRUE; + call_user_func_array($route['callback'], $matches); + } + } + } + + if($route_found === FALSE) { + require_once 'system/404.php'; + } + } +} +?> \ No newline at end of file -- cgit v1.2.3