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(); + } +}