diff --git a/core/event.php b/core/event.php index 24699291..b000e6eb 100644 --- a/core/event.php +++ b/core/event.php @@ -106,13 +106,28 @@ class PageRequestEvent extends Event /** * Get the n th argument of the page request (if it exists.) */ - public function get_arg(int $n): ?string + public function get_arg(int $n): string { $offset = $this->part_count + $n; if ($offset >= 0 && $offset < $this->arg_count) { return $this->args[$offset]; } else { - return null; + throw new SCoreException("Requested an invalid argument #$n"); + } + } + + public function try_page_num(int $n): int { + if($this->count_args() > $n) { + $i = $this->get_arg($n); + if (!is_numeric($i) || $i <= 0) { + return int_escape($i); + } + else { + return 1; + } + } + else { + return 1; } } diff --git a/ext/alias_editor/main.php b/ext/alias_editor/main.php index ad50a464..ab0f76bd 100644 --- a/ext/alias_editor/main.php +++ b/ext/alias_editor/main.php @@ -49,13 +49,18 @@ class AliasEditor extends Extension } } } elseif ($event->get_arg(0) == "list") { - $page_number = $event->get_arg(1); - if (is_null($page_number) || !is_numeric($page_number)) { + if($event->count_args() == 2) { + $page_number = $event->get_arg(1); + if (!is_numeric($page_number)) { + $page_number = 0; + } elseif ($page_number <= 0) { + $page_number = 0; + } else { + $page_number--; + } + } + else { $page_number = 0; - } elseif ($page_number <= 0) { - $page_number = 0; - } else { - $page_number--; } $alias_per_page = $config->get_int('alias_items_per_page', 30); diff --git a/ext/arrowkey_navigation/main.php b/ext/arrowkey_navigation/main.php index 6464f592..c7a890df 100644 --- a/ext/arrowkey_navigation/main.php +++ b/ext/arrowkey_navigation/main.php @@ -54,21 +54,23 @@ class ArrowkeyNavigation extends Extension // get the amount of images per page $images_per_page = $config->get_int(IndexConfig::IMAGES); - // if there are no tags, use default - if (is_null($event->get_arg(1))) { - $prefix = ""; - $page_number = int_escape($event->get_arg(0)); - $total_pages = ceil($database->get_one( - "SELECT COUNT(*) FROM images" - ) / $images_per_page); - } else { // if there are tags, use pages with tags + if($event->count_args() > 1) { + // if there are tags, use pages with tags $prefix = url_escape($event->get_arg(0)) . "/"; - $page_number = int_escape($event->get_arg(1)); + $page_number = $event->try_page_num(1); $total_pages = ceil($database->get_one( "SELECT count FROM tags WHERE tag=:tag", ["tag"=>$event->get_arg(0)] ) / $images_per_page); } + else { + // if there are no tags, use default + $prefix = ""; + $page_number = $event->try_page_num(0); + $total_pages = ceil($database->get_one( + "SELECT COUNT(*) FROM images" + ) / $images_per_page); + } // creates previous & next values // When previous first page, go to last page diff --git a/ext/blotter/main.php b/ext/blotter/main.php index b1d90edc..9f11ede1 100644 --- a/ext/blotter/main.php +++ b/ext/blotter/main.php @@ -71,7 +71,7 @@ class Blotter extends Extension public function onPageRequest(PageRequestEvent $event) { global $page, $database, $user; - if ($event->page_matches("blotter")) { + if ($event->page_matches("blotter") && $event->count_args() > 0) { switch ($event->get_arg(0)) { case "editor": /** diff --git a/ext/comment/main.php b/ext/comment/main.php index c19938ea..d929f642 100644 --- a/ext/comment/main.php +++ b/ext/comment/main.php @@ -243,14 +243,14 @@ class CommentList extends Extension private function onPageRequest_list(PageRequestEvent $event) { - $page_num = int_escape($event->get_arg(1)); + $page_num = $event->try_page_num(1); $this->build_page($page_num); } private function onPageRequest_beta_search(PageRequestEvent $event) { $search = $event->get_arg(1); - $page_num = int_escape($event->get_arg(2)); + $page_num = $event->try_page_num(2); $duser = User::by_name($search); $i_comment_count = Comment::count_comments_by_user($duser); $com_per_page = 50; diff --git a/ext/cron_uploader/main.php b/ext/cron_uploader/main.php index 49f4f171..06d6a974 100644 --- a/ext/cron_uploader/main.php +++ b/ext/cron_uploader/main.php @@ -36,9 +36,8 @@ class CronUploader extends Extension global $user; if ($event->page_matches("cron_upload")) { - $key = $event->get_arg(0); - if (!empty($key)) { - $this->process_upload($key); // Start upload + if ($event->count_args() == 1) { + $this->process_upload($event->get_arg(0)); // Start upload } elseif ($user->can(Permissions::CRON_ADMIN)) { $this->display_documentation(); } diff --git a/ext/danbooru_api/main.php b/ext/danbooru_api/main.php index dfbce69a..a8ccffab 100644 --- a/ext/danbooru_api/main.php +++ b/ext/danbooru_api/main.php @@ -4,37 +4,31 @@ class DanbooruApi extends Extension { public function onPageRequest(PageRequestEvent $event) { - if ($event->page_matches("api") && ($event->get_arg(0) == 'danbooru')) { - $this->api_danbooru($event); - } - } + if ($event->page_matches("api/danbooru")) { + global $page; + $page->set_mode(PageMode::DATA); - // Danbooru API - private function api_danbooru(PageRequestEvent $event) - { - global $page; - $page->set_mode(PageMode::DATA); + if ($event->page_matches("api/danbooru/add_post") || $event->page_matches("api/danbooru/post/create.xml")) { + // No XML data is returned from this function + $page->set_type("text/plain"); + $this->api_add_post(); + } elseif ($event->page_matches("api/danbooru/find_posts") || $event->page_matches("api/danbooru/post/index.xml")) { + $page->set_type("application/xml"); + $page->set_data($this->api_find_posts()); + } elseif ($event->page_matches("api/danbooru/find_tags")) { + $page->set_type("application/xml"); + $page->set_data($this->api_find_tags()); + } - if (($event->get_arg(1) == 'add_post') || (($event->get_arg(1) == 'post') && ($event->get_arg(2) == 'create.xml'))) { - // No XML data is returned from this function - $page->set_type("text/plain"); - $this->api_add_post(); - } elseif (($event->get_arg(1) == 'find_posts') || (($event->get_arg(1) == 'post') && ($event->get_arg(2) == 'index.xml'))) { - $page->set_type("application/xml"); - $page->set_data($this->api_find_posts()); - } elseif ($event->get_arg(1) == 'find_tags') { - $page->set_type("application/xml"); - $page->set_data($this->api_find_tags()); - } - - // Hackery for danbooruup 0.3.2 providing the wrong view url. This simply redirects to the proper - // Shimmie view page - // Example: danbooruup says the url is http://shimmie/api/danbooru/post/show/123 - // This redirects that to http://shimmie/post/view/123 - elseif (($event->get_arg(1) == 'post') && ($event->get_arg(2) == 'show')) { - $fixedlocation = make_link("post/view/" . $event->get_arg(3)); - $page->set_mode(PageMode::REDIRECT); - $page->set_redirect($fixedlocation); + // Hackery for danbooruup 0.3.2 providing the wrong view url. This simply redirects to the proper + // Shimmie view page + // Example: danbooruup says the url is http://shimmie/api/danbooru/post/show/123 + // This redirects that to http://shimmie/post/view/123 + elseif ($event->page_matches("api/danbooru/post/show")) { + $fixedlocation = make_link("post/view/" . $event->get_arg(0)); + $page->set_mode(PageMode::REDIRECT); + $page->set_redirect($fixedlocation); + } } } diff --git a/ext/ext_manager/main.php b/ext/ext_manager/main.php index 9e057b02..ddb9e63a 100644 --- a/ext/ext_manager/main.php +++ b/ext/ext_manager/main.php @@ -38,7 +38,7 @@ class ExtManager extends Extension global $page, $user; if ($event->page_matches("ext_manager")) { if ($user->can(Permissions::MANAGE_EXTENSION_LIST)) { - if ($event->get_arg(0) == "set" && $user->check_auth_token()) { + if ($event->count_args() == 1 && $event->get_arg(0) == "set" && $user->check_auth_token()) { if (is_writable("data/config")) { $this->set_things($_POST); log_warning("ext_manager", "Active extensions changed", "Active extensions changed"); @@ -60,11 +60,14 @@ class ExtManager extends Extension } if ($event->page_matches("ext_doc")) { - $ext = $event->get_arg(0); - if (file_exists("ext/$ext/info.php")) { - $info = ExtensionInfo::get_by_key($ext); - $this->theme->display_doc($page, $info); - } else { + if($event->count_args() == 1) { + $ext = $event->get_arg(0); + if (file_exists("ext/$ext/info.php")) { + $info = ExtensionInfo::get_by_key($ext); + $this->theme->display_doc($page, $info); + } + } + else { $this->theme->display_table($page, $this->get_extensions(false), false); } } diff --git a/ext/forum/main.php b/ext/forum/main.php index 25f233ba..5c5e76e8 100644 --- a/ext/forum/main.php +++ b/ext/forum/main.php @@ -245,18 +245,23 @@ class Forum extends Extension private function show_last_threads(Page $page, PageRequestEvent $event, $showAdminOptions = false) { global $config, $database; - $pageNumber = $event->get_arg(1); $threadsPerPage = $config->get_int('forumThreadsPerPage', 15); $totalPages = ceil($database->get_one("SELECT COUNT(*) FROM forum_threads") / $threadsPerPage); - if (is_null($pageNumber) || !is_numeric($pageNumber)) { + if($event->count_args() >= 2) { + $pageNumber = $event->get_arg(1); + if (!is_numeric($pageNumber)) { + $pageNumber = 0; + } elseif ($pageNumber <= 0) { + $pageNumber = 0; + } elseif ($pageNumber >= $totalPages) { + $pageNumber = $totalPages - 1; + } else { + $pageNumber--; + } + } + else { $pageNumber = 0; - } elseif ($pageNumber <= 0) { - $pageNumber = 0; - } elseif ($pageNumber >= $totalPages) { - $pageNumber = $totalPages - 1; - } else { - $pageNumber--; } $threads = $database->get_all( @@ -278,19 +283,24 @@ class Forum extends Extension { global $config, $database; $threadID = $event->get_arg(1); - $pageNumber = $event->get_arg(2); $postsPerPage = $config->get_int('forumPostsPerPage', 15); $totalPages = ceil($database->get_one("SELECT COUNT(*) FROM forum_posts WHERE thread_id = ?", [$threadID]) / $postsPerPage); $threadTitle = $this->get_thread_title($threadID); - if (is_null($pageNumber) || !is_numeric($pageNumber)) { + if($event->count_args() >= 3) { + $pageNumber = $event->get_arg(2); + if (!is_numeric($pageNumber)) { + $pageNumber = 0; + } elseif ($pageNumber <= 0) { + $pageNumber = 0; + } elseif ($pageNumber >= $totalPages) { + $pageNumber = $totalPages - 1; + } else { + $pageNumber--; + } + } + else { $pageNumber = 0; - } elseif ($pageNumber <= 0) { - $pageNumber = 0; - } elseif ($pageNumber >= $totalPages) { - $pageNumber = $totalPages - 1; - } else { - $pageNumber--; } $posts = $database->get_all( diff --git a/ext/log_db/main.php b/ext/log_db/main.php index 9105ae15..a9c35eaf 100644 --- a/ext/log_db/main.php +++ b/ext/log_db/main.php @@ -47,10 +47,7 @@ class LogDatabase extends Extension if ($user->can(Permissions::VIEW_EVENTLOG)) { $wheres = []; $args = []; - $page_num = int_escape($event->get_arg(0)); - if ($page_num <= 0) { - $page_num = 1; - } + $page_num = $event->try_page_num(0); if (!empty($_GET["time-start"])) { $wheres[] = "date_sent > :time_start"; $args["time_start"] = $_GET["time-start"]; diff --git a/ext/notes/main.php b/ext/notes/main.php index 11cbdcca..1e9f05f5 100644 --- a/ext/notes/main.php +++ b/ext/notes/main.php @@ -384,12 +384,7 @@ class Notes extends Extension { global $database, $config; - $pageNumber = $event->get_arg(1); - if (is_null($pageNumber) || !is_numeric($pageNumber) || $pageNumber <= 0) { - $pageNumber = 0; - } else { - $pageNumber--; - } + $pageNumber = $event->try_page_num(1) - 1; $notesPerPage = $config->get_int('notesNotesPerPage'); @@ -419,12 +414,7 @@ class Notes extends Extension { global $config, $database; - $pageNumber = $event->get_arg(1); - if (is_null($pageNumber) || !is_numeric($pageNumber) || $pageNumber <= 0) { - $pageNumber = 0; - } else { - $pageNumber--; - } + $pageNumber = $event->try_page_num(1) - 1; $requestsPerPage = $config->get_int('notesRequestsPerPage'); @@ -478,12 +468,7 @@ class Notes extends Extension { global $config, $database; - $pageNumber = $event->get_arg(1); - if (is_null($pageNumber) || !is_numeric($pageNumber) || $pageNumber <= 0) { - $pageNumber = 0; - } else { - $pageNumber--; - } + $pageNumber = $event->try_page_num(1) - 1; $historiesPerPage = $config->get_int('notesHistoriesPerPage'); @@ -511,13 +496,7 @@ class Notes extends Extension global $config, $database; $noteID = $event->get_arg(1); - - $pageNumber = $event->get_arg(2); - if (is_null($pageNumber) || !is_numeric($pageNumber) || $pageNumber <= 0) { - $pageNumber = 0; - } else { - $pageNumber--; - } + $pageNumber = $event->try_page_num(2); $historiesPerPage = $config->get_int('notesHistoriesPerPage'); diff --git a/ext/pools/main.php b/ext/pools/main.php index 6846006e..46137433 100644 --- a/ext/pools/main.php +++ b/ext/pools/main.php @@ -173,7 +173,7 @@ class Pools extends Extension // What action are we trying to perform? switch ($event->get_arg(0)) { case "list": //index - $this->list_pools($page, int_escape($event->get_arg(1))); + $this->list_pools($page, $event->try_page_num(1)); break; case "new": // Show form for new pools @@ -779,14 +779,7 @@ class Pools extends Extension { global $config, $user, $database; - $pageNumber = int_escape($event->get_arg(2)); - if (is_null($pageNumber) || !is_numeric($pageNumber)) { - $pageNumber = 0; - } elseif ($pageNumber <= 0) { - $pageNumber = 0; - } else { - $pageNumber--; - } + $pageNumber = $event->try_page_num(2) - 1; $poolID = int_escape($poolID); $pool = $this->get_pool($poolID); diff --git a/ext/setup/main.php b/ext/setup/main.php index 12e4ea6b..8fdd764c 100644 --- a/ext/setup/main.php +++ b/ext/setup/main.php @@ -288,7 +288,11 @@ class Setup extends Extension if (!$user->can(Permissions::CHANGE_SETTING)) { $this->theme->display_permission_denied(); } else { - if ($event->get_arg(0) == "save" && $user->check_auth_token()) { + if ($event->count_args() == 0) { + $panel = new SetupPanel(); + send_event(new SetupBuildingEvent($panel)); + $this->theme->display_page($page, $panel); + } elseif ($event->get_arg(0) == "save" && $user->check_auth_token()) { send_event(new ConfigSaveEvent($config)); $config->save(); flash_message("Config saved"); @@ -297,10 +301,6 @@ class Setup extends Extension $page->set_redirect(make_link("setup")); } elseif ($event->get_arg(0) == "advanced") { $this->theme->display_advanced($page, $config->values); - } else { - $panel = new SetupPanel(); - send_event(new SetupBuildingEvent($panel)); - $this->theme->display_page($page, $panel); } } } diff --git a/ext/shimmie_api/main.php b/ext/shimmie_api/main.php index a3bcf703..6cddf2ed 100644 --- a/ext/shimmie_api/main.php +++ b/ext/shimmie_api/main.php @@ -39,10 +39,15 @@ class ShimmieApi extends Extension $page->set_type("text/plain"); if ($event->page_matches("api/shimmie/get_tags")) { - $tag = $event->get_arg(0); - if (empty($tag) && isset($_GET['tag'])) { + if($event->count_args() > 0) { + $tag = $event->get_arg(0); + } + elseif (isset($_GET['tag'])) { $tag = $_GET['tag']; } + else { + $tag = null; + } $res = $this->api_get_tags($tag); $page->set_data(json_encode($res)); } elseif ($event->page_matches("api/shimmie/get_image")) { diff --git a/ext/transcode/main.php b/ext/transcode/main.php index 71e40469..69a034d0 100644 --- a/ext/transcode/main.php +++ b/ext/transcode/main.php @@ -128,12 +128,13 @@ class TranscodeImage extends Extension global $page, $user; if ($event->page_matches("transcode") && $user->can(Permissions::EDIT_FILES)) { - $image_id = int_escape($event->get_arg(0)); - if (empty($image_id)) { - $image_id = isset($_POST['image_id']) ? int_escape($_POST['image_id']) : null; + if($event->count_args() >= 1) { + $image_id = int_escape($event->get_arg(0)); } - // Try to get the image ID - if (empty($image_id)) { + elseif(isset($_POST['image_id'])) { + $image_id = int_escape($_POST['image_id']); + } + else { throw new ImageTranscodeException("Can not resize Image: No valid Image ID given."); } $image_obj = Image::by_id($image_id); diff --git a/ext/trash/main.php b/ext/trash/main.php index a58b900d..c2d43b23 100644 --- a/ext/trash/main.php +++ b/ext/trash/main.php @@ -19,11 +19,13 @@ class Trash extends Extension if ($event->page_matches("trash_restore") && $user->can(Permissions::VIEW_TRASH)) { // Try to get the image ID - $image_id = int_escape($event->get_arg(0)); - if (empty($image_id)) { - $image_id = isset($_POST['image_id']) ? $_POST['image_id'] : null; + if($event->count_args() >= 1) { + $image_id = int_escape($event->get_arg(0)); } - if (empty($image_id)) { + elseif (isset($_POST['image_id'])) { + $image_id = $_POST['image_id']; + } + else { throw new SCoreException("Can not restore image: No valid Image ID given."); } diff --git a/ext/upload/main.php b/ext/upload/main.php index 9bc90995..87a31ab9 100644 --- a/ext/upload/main.php +++ b/ext/upload/main.php @@ -187,11 +187,13 @@ class Upload extends Extension throw new UploadException("Can not replace Image: disk nearly full"); } // Try to get the image ID - $image_id = int_escape($event->get_arg(0)); - if (empty($image_id)) { - $image_id = isset($_POST['image_id']) ? $_POST['image_id'] : null; + if($event->count_args() >= 1) { + $image_id = int_escape($event->get_arg(0)); } - if (empty($image_id)) { + elseif(isset($_POST['image_id'])) { + $image_id = $_POST['image_id']; + } + else { throw new UploadException("Can not replace Image: No valid Image ID given."); } diff --git a/ext/user/main.php b/ext/user/main.php index 7905c16f..acdb9bac 100644 --- a/ext/user/main.php +++ b/ext/user/main.php @@ -53,10 +53,7 @@ class UserPage extends Extension } elseif ($event->get_arg(0) == "list") { $limit = 50; - $page_num = int_escape($event->get_arg(1)); - if ($page_num <= 0) { - $page_num = 1; - } + $page_num = $event->try_page_num(1); $offset = ($page_num-1) * $limit; $q = "WHERE 1=1"; diff --git a/ext/wiki/main.php b/ext/wiki/main.php index 03efb951..14c6eb7f 100644 --- a/ext/wiki/main.php +++ b/ext/wiki/main.php @@ -103,7 +103,7 @@ class Wiki extends Extension { global $page, $user; if ($event->page_matches("wiki")) { - if (is_null($event->get_arg(0)) || strlen(trim($event->get_arg(0))) === 0) { + if ($event->count_args() == 0 || strlen(trim($event->get_arg(0))) === 0) { $title = "Index"; } else { $title = $event->get_arg(0);