From 09f498695dc586b52e272de4467067502844fdec Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Sun, 1 Jan 2023 19:48:12 +0100 Subject: [PATCH] Router: split off from Dispatcher --- models/Dispatcher.php | 69 +------------------------------------- models/Router.php | 77 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 68 deletions(-) create mode 100644 models/Router.php diff --git a/models/Dispatcher.php b/models/Dispatcher.php index e755499..d5688ab 100644 --- a/models/Dispatcher.php +++ b/models/Dispatcher.php @@ -8,79 +8,12 @@ class Dispatcher { - public static function route() - { - $possibleActions = [ - 'addalbum' => 'EditAlbum', - 'albums' => 'ViewPhotoAlbums', - 'editalbum' => 'EditAlbum', - 'editasset' => 'EditAsset', - 'edittag' => 'EditTag', - 'edituser' => 'EditUser', - 'login' => 'Login', - 'logout' => 'Logout', - 'managealbums' => 'ManageAlbums', - 'manageassets' => 'ManageAssets', - 'manageerrors' => 'ManageErrors', - 'managetags' => 'ManageTags', - 'manageusers' => 'ManageUsers', - 'people' => 'ViewPeople', - 'resetpassword' => 'ResetPassword', - 'suggest' => 'ProvideAutoSuggest', - 'timeline' => 'ViewTimeline', - 'uploadmedia' => 'UploadMedia', - 'download' => 'Download', - ]; - - // Work around PHP's FPM not always providing PATH_INFO. - if (empty($_SERVER['PATH_INFO']) && isset($_SERVER['REQUEST_URI'])) - { - if (strpos($_SERVER['REQUEST_URI'], '?') === false) - $_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI']; - else - $_SERVER['PATH_INFO'] = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '?')); - } - - // Just showing the album index? - if (empty($_SERVER['PATH_INFO']) || $_SERVER['PATH_INFO'] == '/') - { - return new ViewPhotoAlbum(); - } - // Asynchronously generating thumbnails? - elseif (preg_match('~^/thumbnail/(?\d+)/(?\d+)x(?\d+)(?:_(?c(t|b|s|)))?/?~', $_SERVER['PATH_INFO'], $path)) - { - $_GET = array_merge($_GET, $path); - return new GenerateThumbnail(); - } - // Look for particular actions... - elseif (preg_match('~^/(?[a-z]+)(?:/page/(?\d+))?/?~', $_SERVER['PATH_INFO'], $path) && isset($possibleActions[$path['action']])) - { - $_GET = array_merge($_GET, $path); - return new $possibleActions[$path['action']](); - } - // An album, person, or any other tag? - elseif (preg_match('~^/(?.+?)(?:/page/(?\d+))?/?$~', $_SERVER['PATH_INFO'], $path) && Tag::matchSlug($path['tag'])) - { - $_GET = array_merge($_GET, $path); - return new ViewPhotoAlbum(); - } - // A photo for sure, then, right? - elseif (preg_match('~^/(?.+?)/?$~', $_SERVER['PATH_INFO'], $path)) - { - $_GET = array_merge($_GET, $path); - return new ViewPhoto(); - } - // No idea, then? - else - throw new NotFoundException(); - } - public static function dispatch() { // Let's try to find our bearings! try { - $page = self::route(); + $page = Router::route(); $page->showContent(); } // Something wasn't found? diff --git a/models/Router.php b/models/Router.php new file mode 100644 index 0000000..ba54a61 --- /dev/null +++ b/models/Router.php @@ -0,0 +1,77 @@ + 'EditAlbum', + 'albums' => 'ViewPhotoAlbums', + 'editalbum' => 'EditAlbum', + 'editasset' => 'EditAsset', + 'edittag' => 'EditTag', + 'edituser' => 'EditUser', + 'login' => 'Login', + 'logout' => 'Logout', + 'managealbums' => 'ManageAlbums', + 'manageassets' => 'ManageAssets', + 'manageerrors' => 'ManageErrors', + 'managetags' => 'ManageTags', + 'manageusers' => 'ManageUsers', + 'people' => 'ViewPeople', + 'resetpassword' => 'ResetPassword', + 'suggest' => 'ProvideAutoSuggest', + 'timeline' => 'ViewTimeline', + 'uploadmedia' => 'UploadMedia', + 'download' => 'Download', + ]; + + // Work around PHP's FPM not always providing PATH_INFO. + if (empty($_SERVER['PATH_INFO']) && isset($_SERVER['REQUEST_URI'])) + { + if (strpos($_SERVER['REQUEST_URI'], '?') === false) + $_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI']; + else + $_SERVER['PATH_INFO'] = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '?')); + } + + // Just showing the album index? + if (empty($_SERVER['PATH_INFO']) || $_SERVER['PATH_INFO'] == '/') + { + return new ViewPhotoAlbum(); + } + // Asynchronously generating thumbnails? + elseif (preg_match('~^/thumbnail/(?\d+)/(?\d+)x(?\d+)(?:_(?c(t|b|s|)))?/?~', $_SERVER['PATH_INFO'], $path)) + { + $_GET = array_merge($_GET, $path); + return new GenerateThumbnail(); + } + // Look for particular actions... + elseif (preg_match('~^/(?[a-z]+)(?:/page/(?\d+))?/?~', $_SERVER['PATH_INFO'], $path) && isset($possibleActions[$path['action']])) + { + $_GET = array_merge($_GET, $path); + return new $possibleActions[$path['action']](); + } + // An album, person, or any other tag? + elseif (preg_match('~^/(?.+?)(?:/page/(?\d+))?/?$~', $_SERVER['PATH_INFO'], $path) && Tag::matchSlug($path['tag'])) + { + $_GET = array_merge($_GET, $path); + return new ViewPhotoAlbum(); + } + // A photo for sure, then, right? + elseif (preg_match('~^/(?.+?)/?$~', $_SERVER['PATH_INFO'], $path)) + { + $_GET = array_merge($_GET, $path); + return new ViewPhoto(); + } + // No idea, then? + else + throw new NotFoundException(); + } +}