From bf0b1ec0476347184981ce905cfb5aa550f37a93 Mon Sep 17 00:00:00 2001 From: flashwave Date: Sat, 30 Jan 2016 14:25:18 +0100 Subject: [PATCH] r20160130.1 --- libraries/Controllers/Forum.php | 62 +++++++++ libraries/Controllers/Meta.php | 153 +++++++++++++++++++++++ libraries/Router.php | 7 +- libraries/Urls.php | 6 +- public/faq.php | 28 ----- public/group.php | 22 ---- public/index.php | 73 +---------- public/news.php | 32 ----- public/router.php | 19 --- public/search.php | 27 ---- routes.php | 63 ++++++++++ sakura.php | 1 + templates/yuuno/elements/indexPanel.twig | 2 +- 13 files changed, 290 insertions(+), 205 deletions(-) create mode 100644 libraries/Controllers/Forum.php delete mode 100644 public/faq.php delete mode 100644 public/group.php delete mode 100644 public/news.php delete mode 100644 public/router.php delete mode 100644 public/search.php diff --git a/libraries/Controllers/Forum.php b/libraries/Controllers/Forum.php new file mode 100644 index 0000000..e3b0636 --- /dev/null +++ b/libraries/Controllers/Forum.php @@ -0,0 +1,62 @@ + (new ForumData\Forum()), + 'stats' => [ + 'userCount' => Database::count('users', ['password_algo' => ['nologin', '!='], 'rank_main' => ['1', '!=']])[0], + 'newestUser' => User::construct(Users::getNewestUserId()), + 'lastRegData' => date_diff( + date_create(date('Y-m-d', User::construct(Users::getNewestUserId())->registered)), + date_create(date('Y-m-d')) + )->format('%a'), + 'topicCount' => Database::count('topics')[0], + 'postCount' => Database::count('posts')[0], + 'onlineUsers' => Users::checkAllOnline(), + ], + ] + ); + + // Set parse variables + $template->setVariables($renderData); + + // Return the compiled page + return $template->render('forum/index'); + } + + // View a forum + public static function forum($id = null) + { + } +} diff --git a/libraries/Controllers/Meta.php b/libraries/Controllers/Meta.php index e7dea77..3a62432 100644 --- a/libraries/Controllers/Meta.php +++ b/libraries/Controllers/Meta.php @@ -5,7 +5,13 @@ namespace Sakura\Controllers; +use Sakura\Config; +use Sakura\Database; +use Sakura\News; use Sakura\Template; +use Sakura\User; +use Sakura\Users; +use Sakura\Utils; /** * Class Meta @@ -13,4 +19,151 @@ use Sakura\Template; */ class Meta { + // Site index + public static function index() + { + // Get the global renderData + global $renderData; + + // Initialise templating engine + $template = new Template(); + + // Merge index specific stuff with the global render data + $renderData = array_merge( + $renderData, + [ + 'news' => new News(Config::get('site_news_category')), + 'newsCount' => Config::get('front_page_news_posts'), + 'stats' => [ + 'userCount' => Database::count('users', ['password_algo' => ['nologin', '!='], 'rank_main' => ['1', '!=']])[0], + 'newestUser' => User::construct(Users::getNewestUserId()), + 'lastRegDate' => date_diff( + date_create(date('Y-m-d', User::construct(Users::getNewestUserId())->registered)), + date_create(date('Y-m-d')) + )->format('%a'), + 'topicCount' => Database::count('topics')[0], + 'postCount' => Database::count('posts')[0], + 'onlineUsers' => Users::checkAllOnline(), + ], + ] + ); + + // Set parse variables + $template->setVariables($renderData); + + // Return the compiled page + return $template->render('main/index'); + } + + // News + public static function news() + { + // Get the global renderData + global $renderData; + + // Get arguments + $args = func_get_args(); + $category = isset($args[0]) && !is_numeric($args[0]) ? $args[0] : Config::get('site_news_category'); + $post = isset($args[1]) && is_numeric($args[1]) ? $args[1] : ( + isset($args[0]) && is_numeric($args[0]) ? $args[0] : 0 + ); + + // Create news object + $news = new News($category); + + // Merge the data for this page with the global + $renderData = array_merge($renderData, [ + 'news' => $news, + 'postsPerPage' => Config::get('news_posts_per_page'), + 'viewPost' => $post != 0, + 'postExists' => $news->postExists($post), + ]); + + // Initialise templating engine + $template = new Template(); + + // Set parse variables + $template->setVariables($renderData); + + // Print page contents + return $template->render('main/news'); + } + + // FAQ + public static function faq() + { + // Get the global renderData + global $renderData; + + // Add page specific things + $renderData['page'] = [ + 'title' => 'Frequently Asked Questions', + 'questions' => Utils::getFaqData(), + ]; + + // Initialise templating engine + $template = new Template(); + + // Set parse variables + $template->setVariables($renderData); + + // Print page contents + echo $template->render('main/faq'); + + } + + // Info pages + public static function infoPage($id = null) + { + // Get the global renderData + global $renderData; + + // Initialise templating engine + $template = new Template(); + + // Set default variables + $renderData['page'] = [ + 'content' => '

Unable to load the requested info page.

Check the URL and try again.

', + ]; + + // Set page id + $id = strtolower($id); + + // Get info page data from the database + if ($ipData = Utils::loadInfoPage($id)) { + // Assign new proper variable + $renderData['page'] = [ + 'id' => $id, + 'title' => $ipData['page_title'], + 'content' => $ipData['page_content'], + ]; + } + + // Set parse variables + $template->setVariables($renderData); + + // Return the compiled page + return $template->render('main/infopage'); + } + + // Search + public static function search() + { + // Get the global renderData + global $renderData; + + // Add page specific things + $renderData['page'] = [ + 'title' => 'Search', + ]; + + // Initialise templating engine + $template = new Template(); + + // Set parse variables + $template->setVariables($renderData); + + // Print page contents + echo $template->render('main/search'); + } } diff --git a/libraries/Router.php b/libraries/Router.php index c40fd70..689b7a6 100644 --- a/libraries/Router.php +++ b/libraries/Router.php @@ -17,7 +17,7 @@ class Router // Router container protected static $router = null; - // Base path + // Base path (unused for now) protected static $basePath = null; // Dispatcher @@ -40,9 +40,10 @@ class Router // Check if the method exists if (in_array($name = strtoupper($name), self::$methods)) { $path = isset($args[2]) && $args !== null ? [$args[0], $args[2]] : $args[0]; + $handler = is_callable($args[1]) || is_array($args[1]) ? $args[1] : explode('@', $args[1]); $filter = isset($args[3]) ? $args[3] : []; - self::$router->addRoute($name, $path, $args[1], $filter); + self::$router->addRoute($name, $path, $handler, $filter); } } @@ -65,7 +66,7 @@ class Router // Parse the url private static function parseUrl($url) { - return parse_url(str_replace(self::$basePath, '', $url), PHP_URL_PATH); + return parse_url($url, PHP_URL_PATH); } // Handle requests diff --git a/libraries/Urls.php b/libraries/Urls.php index 72a6cc0..2d617a0 100644 --- a/libraries/Urls.php +++ b/libraries/Urls.php @@ -16,7 +16,7 @@ class Urls // General site sections 'SITE_HOME' => [ - '/', + '/index.php', '/', ], 'SITE_NEWS' => [ @@ -32,7 +32,7 @@ class Urls '/news/%s', ], 'SITE_NEWS_CAT_POST' => [ - '/news.php?cat=$s&id=%u', + '/news.php?cat=%s&id=%u', '/news/%s/%u', ], 'SITE_SEARCH' => [ @@ -292,7 +292,7 @@ class Urls } // Check if mod_rewrite is enabled - $rewrite = ($rewrite === null ? Config::get('url_rewrite') : $rewrite) ? 1 : 0; + $rewrite = 0; //($rewrite === null ? Config::get('url_rewrite') : $rewrite) ? 1 : 0; // Format urls $formatted = vsprintf($this->urls[$lid][$rewrite], $args); diff --git a/public/faq.php b/public/faq.php deleted file mode 100644 index b097c93..0000000 --- a/public/faq.php +++ /dev/null @@ -1,28 +0,0 @@ - 'Frequently Asked Questions', - 'questions' => Utils::getFaqData(), -]; - -// Initialise templating engine -$template = new Template(); - -// Change templating engine -$template->setTemplate($templateName); - -// Set parse variables -$template->setVariables($renderData); - -// Print page contents -echo $template->render('main/faq'); diff --git a/public/group.php b/public/group.php deleted file mode 100644 index 8e11dc2..0000000 --- a/public/group.php +++ /dev/null @@ -1,22 +0,0 @@ -setTemplate($templateName); - -// Set parse variables -$template->setVariables($renderData); - -// Print page contents -echo $template->render('group/index'); diff --git a/public/index.php b/public/index.php index 8f69048..06f02b0 100644 --- a/public/index.php +++ b/public/index.php @@ -1,6 +1,6 @@ setTemplate($templateName); - -// Info pages -if (isset($_GET['p'])) { - // Set default variables - $renderData['page'] = [ - 'content' => '

Unable to load the requested info page.

Check the URL and try again.

', - ]; - - // Set page id - $pageId = isset($_GET['p']) ? strtolower($_GET['p']) : ''; - - // Get info page data from the database - if ($ipData = Utils::loadInfoPage($pageId)) { - // Assign new proper variable - $renderData['page'] = [ - 'id' => $pageId, - 'title' => $ipData['page_title'], - 'content' => $ipData['page_content'], - ]; - } - - // Set parse variables - $template->setVariables($renderData); - - // Print page contents - echo $template->render('main/infopage'); - exit; -} - -// Are we in forum mode? -$forumMode = isset($_GET['forum']) ? ($_GET['forum'] == true) : false; - -$renderData['news'] = ($forumMode ? null : (new News(Config::get('site_news_category')))); - -$renderData['newsCount'] = Config::get('front_page_news_posts'); - -$renderData['forum'] = ($forumMode ? (new Forum\Forum()) : null); - -$renderData['latestPosts'] = Database::fetch('posts', true, null, ['post_id', true], [3]); - -$renderData['stats'] = [ - 'userCount' => Database::count('users', ['password_algo' => ['nologin', '!='], 'rank_main' => ['1', '!=']])[0], - 'newestUser' => ($_INDEX_NEWEST_USER = User::construct(Users::getNewestUserId())), - 'lastRegDate' => ($_INDEX_LAST_REGDATE = date_diff( - date_create( - date( - 'Y-m-d', - $_INDEX_NEWEST_USER->registered - ) - ), - date_create( - date('Y-m-d') - ) - )->format('%a')) . ' day' . ($_INDEX_LAST_REGDATE == 1 ? '' : 's'), - 'topicCount' => Database::count('topics')[0], - 'postCount' => Database::count('posts')[0], - 'onlineUsers' => Users::checkAllOnline(), -]; - -// Set parse variables -$template->setVariables($renderData); - -// Print page contents -echo $template->render(($forumMode ? 'forum' : 'main') . '/index'); +// Handle requests +echo Router::handle($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']); diff --git a/public/news.php b/public/news.php deleted file mode 100644 index bb917d8..0000000 --- a/public/news.php +++ /dev/null @@ -1,32 +0,0 @@ - $news, - 'postsPerPage' => Config::get('news_posts_per_page'), - 'viewPost' => isset($_GET['id']), - 'postExists' => $news->postExists(isset($_GET['id']) ? $_GET['id'] : 0), -]); - -// Initialise templating engine -$template = new Template(); - -// Change templating engine -$template->setTemplate($templateName); - -// Set parse variables -$template->setVariables($renderData); - -// Print page contents -echo $template->render('main/news'); diff --git a/public/router.php b/public/router.php deleted file mode 100644 index 7378a0c..0000000 --- a/public/router.php +++ /dev/null @@ -1,19 +0,0 @@ - 'Search', -]; - -// Initialise templating engine -$template = new Template(); - -// Change templating engine -$template->setTemplate($templateName); - -// Set parse variables -$template->setVariables($renderData); - -// Print page contents -echo $template->render('main/search'); diff --git a/routes.php b/routes.php index a77ecf7..66ac099 100644 --- a/routes.php +++ b/routes.php @@ -5,3 +5,66 @@ // Define namespace namespace Sakura; + +// Meta pages +Router::get('/', 'Sakura\Controllers\Meta@index', 'main.index'); +Router::get('/faq', 'Sakura\Controllers\Meta@faq', 'main.faq'); +Router::get('/search', 'Sakura\Controllers\Meta@search', 'main.search'); +Router::get('/p/{id}', 'Sakura\Controllers\Meta@infoPage', 'main.infopage'); + +// News +Router::get('/news', 'Sakura\Controllers\Meta@news', 'news.index'); +Router::get('/news/{category}', 'Sakura\Controllers\Meta@news', 'news.category'); +Router::get('/news/{category}/{id}', 'Sakura\Controllers\Meta@news', 'news.post'); + +// Forum +Router::get('/forum', 'Sakura\Controllers\Forum@index', 'forum.index'); + +// Redirections +Router::any('/index.php', function () { + // Info pages + if (isset($_REQUEST['p'])) { + header('Location: /p/' . $_REQUEST['p']); + return; + } + + // Forum index + if (isset($_REQUEST['forum']) && $_REQUEST['forum']) { + header('Location: /forum'); + return; + } + + // Site index + header('Location: /'); +}); + +Router::any('/news.php', function () { + // Category + post + if (isset($_REQUEST['cat']) && isset($_REQUEST['id'])) { + header('Location: /news/' . $_REQUEST['cat'] . '/'. $_REQUEST['id']); + return; + } + + // Category + if (isset($_REQUEST['cat'])) { + header('Location: /news/' . $_REQUEST['cat']); + return; + } + + // Post in the main category + if (isset($_REQUEST['id'])) { + header('Location: /news/' . $_REQUEST['id']); + return; + } + + // All posts in main category + header('Location: /news'); +}); + +Router::any('/faq.php', function () { + header('Location: /faq'); +}); + +Router::any('/search.php', function () { + header('Location: /search'); +}); diff --git a/sakura.php b/sakura.php index 5e24a5a..f683f29 100644 --- a/sakura.php +++ b/sakura.php @@ -61,6 +61,7 @@ require_once ROOT . 'libraries/Users.php'; require_once ROOT . 'libraries/Utils.php'; require_once ROOT . 'libraries/Whois.php'; require_once ROOT . 'libraries/Console/Application.php'; +require_once ROOT . 'libraries/Controllers/Forum.php'; require_once ROOT . 'libraries/Controllers/Meta.php'; require_once ROOT . 'libraries/Forum/Forum.php'; require_once ROOT . 'libraries/Forum/Post.php'; diff --git a/templates/yuuno/elements/indexPanel.twig b/templates/yuuno/elements/indexPanel.twig index e073efa..6cba99d 100644 --- a/templates/yuuno/elements/indexPanel.twig +++ b/templates/yuuno/elements/indexPanel.twig @@ -26,7 +26,7 @@
Stats
We have {{ stats.userCount }} user{% if stats.userCount != 1 %}s{% endif %}, {{ stats.newestUser.username }} is the newest user, - it has been {{ stats.lastRegDate }} since the last user registered and the forum has {{ stats.topicCount }} thread{% if stats.topicCount != 1 %}s{% endif %} and {{ stats.postCount }} post{% if stats.postCount != 1 %}s{% endif %}. + it has been {{ stats.lastRegDate }} day{{ stats.lastRegDate == 1 ? '' : 's' }} since the last user registered and the forum has {{ stats.topicCount }} thread{% if stats.topicCount != 1 %}s{% endif %} and {{ stats.postCount }} post{% if stats.postCount != 1 %}s{% endif %}.
Online Users
{% if stats.onlineUsers %} All active users in the past {{ sakura.onlineTimeout / 60 }} minute{% if sakura.onlineTimeout != 60 %}s{% endif %}