Have get_arg never return null

90% of places assume it will never return null, and they will break in
weird ways if it does return null
This commit is contained in:
Shish 2019-11-04 00:40:10 +00:00
parent fc7da5114f
commit d17e207984
19 changed files with 141 additions and 137 deletions

View File

@ -106,13 +106,28 @@ class PageRequestEvent extends Event
/** /**
* Get the n th argument of the page request (if it exists.) * 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; $offset = $this->part_count + $n;
if ($offset >= 0 && $offset < $this->arg_count) { if ($offset >= 0 && $offset < $this->arg_count) {
return $this->args[$offset]; return $this->args[$offset];
} else { } 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;
} }
} }

View File

@ -49,14 +49,19 @@ class AliasEditor extends Extension
} }
} }
} elseif ($event->get_arg(0) == "list") { } elseif ($event->get_arg(0) == "list") {
if($event->count_args() == 2) {
$page_number = $event->get_arg(1); $page_number = $event->get_arg(1);
if (is_null($page_number) || !is_numeric($page_number)) { if (!is_numeric($page_number)) {
$page_number = 0; $page_number = 0;
} elseif ($page_number <= 0) { } elseif ($page_number <= 0) {
$page_number = 0; $page_number = 0;
} else { } else {
$page_number--; $page_number--;
} }
}
else {
$page_number = 0;
}
$alias_per_page = $config->get_int('alias_items_per_page', 30); $alias_per_page = $config->get_int('alias_items_per_page', 30);

View File

@ -54,21 +54,23 @@ class ArrowkeyNavigation extends Extension
// get the amount of images per page // get the amount of images per page
$images_per_page = $config->get_int(IndexConfig::IMAGES); $images_per_page = $config->get_int(IndexConfig::IMAGES);
// if there are no tags, use default if($event->count_args() > 1) {
if (is_null($event->get_arg(1))) { // if there are tags, use pages with tags
$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
$prefix = url_escape($event->get_arg(0)) . "/"; $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( $total_pages = ceil($database->get_one(
"SELECT count FROM tags WHERE tag=:tag", "SELECT count FROM tags WHERE tag=:tag",
["tag"=>$event->get_arg(0)] ["tag"=>$event->get_arg(0)]
) / $images_per_page); ) / $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 // creates previous & next values
// When previous first page, go to last page // When previous first page, go to last page

View File

@ -71,7 +71,7 @@ class Blotter extends Extension
public function onPageRequest(PageRequestEvent $event) public function onPageRequest(PageRequestEvent $event)
{ {
global $page, $database, $user; global $page, $database, $user;
if ($event->page_matches("blotter")) { if ($event->page_matches("blotter") && $event->count_args() > 0) {
switch ($event->get_arg(0)) { switch ($event->get_arg(0)) {
case "editor": case "editor":
/** /**

View File

@ -243,14 +243,14 @@ class CommentList extends Extension
private function onPageRequest_list(PageRequestEvent $event) 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); $this->build_page($page_num);
} }
private function onPageRequest_beta_search(PageRequestEvent $event) private function onPageRequest_beta_search(PageRequestEvent $event)
{ {
$search = $event->get_arg(1); $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); $duser = User::by_name($search);
$i_comment_count = Comment::count_comments_by_user($duser); $i_comment_count = Comment::count_comments_by_user($duser);
$com_per_page = 50; $com_per_page = 50;

View File

@ -36,9 +36,8 @@ class CronUploader extends Extension
global $user; global $user;
if ($event->page_matches("cron_upload")) { if ($event->page_matches("cron_upload")) {
$key = $event->get_arg(0); if ($event->count_args() == 1) {
if (!empty($key)) { $this->process_upload($event->get_arg(0)); // Start upload
$this->process_upload($key); // Start upload
} elseif ($user->can(Permissions::CRON_ADMIN)) { } elseif ($user->can(Permissions::CRON_ADMIN)) {
$this->display_documentation(); $this->display_documentation();
} }

View File

@ -4,25 +4,18 @@ class DanbooruApi extends Extension
{ {
public function onPageRequest(PageRequestEvent $event) public function onPageRequest(PageRequestEvent $event)
{ {
if ($event->page_matches("api") && ($event->get_arg(0) == 'danbooru')) { if ($event->page_matches("api/danbooru")) {
$this->api_danbooru($event);
}
}
// Danbooru API
private function api_danbooru(PageRequestEvent $event)
{
global $page; global $page;
$page->set_mode(PageMode::DATA); $page->set_mode(PageMode::DATA);
if (($event->get_arg(1) == 'add_post') || (($event->get_arg(1) == 'post') && ($event->get_arg(2) == 'create.xml'))) { if ($event->page_matches("api/danbooru/add_post") || $event->page_matches("api/danbooru/post/create.xml")) {
// No XML data is returned from this function // No XML data is returned from this function
$page->set_type("text/plain"); $page->set_type("text/plain");
$this->api_add_post(); $this->api_add_post();
} elseif (($event->get_arg(1) == 'find_posts') || (($event->get_arg(1) == 'post') && ($event->get_arg(2) == 'index.xml'))) { } elseif ($event->page_matches("api/danbooru/find_posts") || $event->page_matches("api/danbooru/post/index.xml")) {
$page->set_type("application/xml"); $page->set_type("application/xml");
$page->set_data($this->api_find_posts()); $page->set_data($this->api_find_posts());
} elseif ($event->get_arg(1) == 'find_tags') { } elseif ($event->page_matches("api/danbooru/find_tags")) {
$page->set_type("application/xml"); $page->set_type("application/xml");
$page->set_data($this->api_find_tags()); $page->set_data($this->api_find_tags());
} }
@ -31,12 +24,13 @@ class DanbooruApi extends Extension
// Shimmie view page // Shimmie view page
// Example: danbooruup says the url is http://shimmie/api/danbooru/post/show/123 // Example: danbooruup says the url is http://shimmie/api/danbooru/post/show/123
// This redirects that to http://shimmie/post/view/123 // This redirects that to http://shimmie/post/view/123
elseif (($event->get_arg(1) == 'post') && ($event->get_arg(2) == 'show')) { elseif ($event->page_matches("api/danbooru/post/show")) {
$fixedlocation = make_link("post/view/" . $event->get_arg(3)); $fixedlocation = make_link("post/view/" . $event->get_arg(0));
$page->set_mode(PageMode::REDIRECT); $page->set_mode(PageMode::REDIRECT);
$page->set_redirect($fixedlocation); $page->set_redirect($fixedlocation);
} }
} }
}
/** /**
* Turns out I use this a couple times so let's make it a utility function * Turns out I use this a couple times so let's make it a utility function

View File

@ -38,7 +38,7 @@ class ExtManager extends Extension
global $page, $user; global $page, $user;
if ($event->page_matches("ext_manager")) { if ($event->page_matches("ext_manager")) {
if ($user->can(Permissions::MANAGE_EXTENSION_LIST)) { 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")) { if (is_writable("data/config")) {
$this->set_things($_POST); $this->set_things($_POST);
log_warning("ext_manager", "Active extensions changed", "Active extensions changed"); log_warning("ext_manager", "Active extensions changed", "Active extensions changed");
@ -60,11 +60,14 @@ class ExtManager extends Extension
} }
if ($event->page_matches("ext_doc")) { if ($event->page_matches("ext_doc")) {
if($event->count_args() == 1) {
$ext = $event->get_arg(0); $ext = $event->get_arg(0);
if (file_exists("ext/$ext/info.php")) { if (file_exists("ext/$ext/info.php")) {
$info = ExtensionInfo::get_by_key($ext); $info = ExtensionInfo::get_by_key($ext);
$this->theme->display_doc($page, $info); $this->theme->display_doc($page, $info);
} else { }
}
else {
$this->theme->display_table($page, $this->get_extensions(false), false); $this->theme->display_table($page, $this->get_extensions(false), false);
} }
} }

View File

@ -245,11 +245,12 @@ class Forum extends Extension
private function show_last_threads(Page $page, PageRequestEvent $event, $showAdminOptions = false) private function show_last_threads(Page $page, PageRequestEvent $event, $showAdminOptions = false)
{ {
global $config, $database; global $config, $database;
$pageNumber = $event->get_arg(1);
$threadsPerPage = $config->get_int('forumThreadsPerPage', 15); $threadsPerPage = $config->get_int('forumThreadsPerPage', 15);
$totalPages = ceil($database->get_one("SELECT COUNT(*) FROM forum_threads") / $threadsPerPage); $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; $pageNumber = 0;
} elseif ($pageNumber <= 0) { } elseif ($pageNumber <= 0) {
$pageNumber = 0; $pageNumber = 0;
@ -258,6 +259,10 @@ class Forum extends Extension
} else { } else {
$pageNumber--; $pageNumber--;
} }
}
else {
$pageNumber = 0;
}
$threads = $database->get_all( $threads = $database->get_all(
"SELECT f.id, f.sticky, f.title, f.date, f.uptodate, u.name AS user_name, u.email AS user_email, u.class AS user_class, sum(1) - 1 AS response_count ". "SELECT f.id, f.sticky, f.title, f.date, f.uptodate, u.name AS user_name, u.email AS user_email, u.class AS user_class, sum(1) - 1 AS response_count ".
@ -278,12 +283,13 @@ class Forum extends Extension
{ {
global $config, $database; global $config, $database;
$threadID = $event->get_arg(1); $threadID = $event->get_arg(1);
$pageNumber = $event->get_arg(2);
$postsPerPage = $config->get_int('forumPostsPerPage', 15); $postsPerPage = $config->get_int('forumPostsPerPage', 15);
$totalPages = ceil($database->get_one("SELECT COUNT(*) FROM forum_posts WHERE thread_id = ?", [$threadID]) / $postsPerPage); $totalPages = ceil($database->get_one("SELECT COUNT(*) FROM forum_posts WHERE thread_id = ?", [$threadID]) / $postsPerPage);
$threadTitle = $this->get_thread_title($threadID); $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; $pageNumber = 0;
} elseif ($pageNumber <= 0) { } elseif ($pageNumber <= 0) {
$pageNumber = 0; $pageNumber = 0;
@ -292,6 +298,10 @@ class Forum extends Extension
} else { } else {
$pageNumber--; $pageNumber--;
} }
}
else {
$pageNumber = 0;
}
$posts = $database->get_all( $posts = $database->get_all(
"SELECT p.id, p.date, p.message, u.name as user_name, u.email AS user_email, u.class AS user_class ". "SELECT p.id, p.date, p.message, u.name as user_name, u.email AS user_email, u.class AS user_class ".

View File

@ -47,10 +47,7 @@ class LogDatabase extends Extension
if ($user->can(Permissions::VIEW_EVENTLOG)) { if ($user->can(Permissions::VIEW_EVENTLOG)) {
$wheres = []; $wheres = [];
$args = []; $args = [];
$page_num = int_escape($event->get_arg(0)); $page_num = $event->try_page_num(0);
if ($page_num <= 0) {
$page_num = 1;
}
if (!empty($_GET["time-start"])) { if (!empty($_GET["time-start"])) {
$wheres[] = "date_sent > :time_start"; $wheres[] = "date_sent > :time_start";
$args["time_start"] = $_GET["time-start"]; $args["time_start"] = $_GET["time-start"];

View File

@ -384,12 +384,7 @@ class Notes extends Extension
{ {
global $database, $config; global $database, $config;
$pageNumber = $event->get_arg(1); $pageNumber = $event->try_page_num(1) - 1;
if (is_null($pageNumber) || !is_numeric($pageNumber) || $pageNumber <= 0) {
$pageNumber = 0;
} else {
$pageNumber--;
}
$notesPerPage = $config->get_int('notesNotesPerPage'); $notesPerPage = $config->get_int('notesNotesPerPage');
@ -419,12 +414,7 @@ class Notes extends Extension
{ {
global $config, $database; global $config, $database;
$pageNumber = $event->get_arg(1); $pageNumber = $event->try_page_num(1) - 1;
if (is_null($pageNumber) || !is_numeric($pageNumber) || $pageNumber <= 0) {
$pageNumber = 0;
} else {
$pageNumber--;
}
$requestsPerPage = $config->get_int('notesRequestsPerPage'); $requestsPerPage = $config->get_int('notesRequestsPerPage');
@ -478,12 +468,7 @@ class Notes extends Extension
{ {
global $config, $database; global $config, $database;
$pageNumber = $event->get_arg(1); $pageNumber = $event->try_page_num(1) - 1;
if (is_null($pageNumber) || !is_numeric($pageNumber) || $pageNumber <= 0) {
$pageNumber = 0;
} else {
$pageNumber--;
}
$historiesPerPage = $config->get_int('notesHistoriesPerPage'); $historiesPerPage = $config->get_int('notesHistoriesPerPage');
@ -511,13 +496,7 @@ class Notes extends Extension
global $config, $database; global $config, $database;
$noteID = $event->get_arg(1); $noteID = $event->get_arg(1);
$pageNumber = $event->try_page_num(2);
$pageNumber = $event->get_arg(2);
if (is_null($pageNumber) || !is_numeric($pageNumber) || $pageNumber <= 0) {
$pageNumber = 0;
} else {
$pageNumber--;
}
$historiesPerPage = $config->get_int('notesHistoriesPerPage'); $historiesPerPage = $config->get_int('notesHistoriesPerPage');

View File

@ -173,7 +173,7 @@ class Pools extends Extension
// What action are we trying to perform? // What action are we trying to perform?
switch ($event->get_arg(0)) { switch ($event->get_arg(0)) {
case "list": //index case "list": //index
$this->list_pools($page, int_escape($event->get_arg(1))); $this->list_pools($page, $event->try_page_num(1));
break; break;
case "new": // Show form for new pools case "new": // Show form for new pools
@ -779,14 +779,7 @@ class Pools extends Extension
{ {
global $config, $user, $database; global $config, $user, $database;
$pageNumber = int_escape($event->get_arg(2)); $pageNumber = $event->try_page_num(2) - 1;
if (is_null($pageNumber) || !is_numeric($pageNumber)) {
$pageNumber = 0;
} elseif ($pageNumber <= 0) {
$pageNumber = 0;
} else {
$pageNumber--;
}
$poolID = int_escape($poolID); $poolID = int_escape($poolID);
$pool = $this->get_pool($poolID); $pool = $this->get_pool($poolID);

View File

@ -288,7 +288,11 @@ class Setup extends Extension
if (!$user->can(Permissions::CHANGE_SETTING)) { if (!$user->can(Permissions::CHANGE_SETTING)) {
$this->theme->display_permission_denied(); $this->theme->display_permission_denied();
} else { } 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)); send_event(new ConfigSaveEvent($config));
$config->save(); $config->save();
flash_message("Config saved"); flash_message("Config saved");
@ -297,10 +301,6 @@ class Setup extends Extension
$page->set_redirect(make_link("setup")); $page->set_redirect(make_link("setup"));
} elseif ($event->get_arg(0) == "advanced") { } elseif ($event->get_arg(0) == "advanced") {
$this->theme->display_advanced($page, $config->values); $this->theme->display_advanced($page, $config->values);
} else {
$panel = new SetupPanel();
send_event(new SetupBuildingEvent($panel));
$this->theme->display_page($page, $panel);
} }
} }
} }

View File

@ -39,10 +39,15 @@ class ShimmieApi extends Extension
$page->set_type("text/plain"); $page->set_type("text/plain");
if ($event->page_matches("api/shimmie/get_tags")) { if ($event->page_matches("api/shimmie/get_tags")) {
if($event->count_args() > 0) {
$tag = $event->get_arg(0); $tag = $event->get_arg(0);
if (empty($tag) && isset($_GET['tag'])) { }
elseif (isset($_GET['tag'])) {
$tag = $_GET['tag']; $tag = $_GET['tag'];
} }
else {
$tag = null;
}
$res = $this->api_get_tags($tag); $res = $this->api_get_tags($tag);
$page->set_data(json_encode($res)); $page->set_data(json_encode($res));
} elseif ($event->page_matches("api/shimmie/get_image")) { } elseif ($event->page_matches("api/shimmie/get_image")) {

View File

@ -128,12 +128,13 @@ class TranscodeImage extends Extension
global $page, $user; global $page, $user;
if ($event->page_matches("transcode") && $user->can(Permissions::EDIT_FILES)) { if ($event->page_matches("transcode") && $user->can(Permissions::EDIT_FILES)) {
if($event->count_args() >= 1) {
$image_id = int_escape($event->get_arg(0)); $image_id = int_escape($event->get_arg(0));
if (empty($image_id)) {
$image_id = isset($_POST['image_id']) ? int_escape($_POST['image_id']) : null;
} }
// Try to get the image ID elseif(isset($_POST['image_id'])) {
if (empty($image_id)) { $image_id = int_escape($_POST['image_id']);
}
else {
throw new ImageTranscodeException("Can not resize Image: No valid Image ID given."); throw new ImageTranscodeException("Can not resize Image: No valid Image ID given.");
} }
$image_obj = Image::by_id($image_id); $image_obj = Image::by_id($image_id);

View File

@ -19,11 +19,13 @@ class Trash extends Extension
if ($event->page_matches("trash_restore") && $user->can(Permissions::VIEW_TRASH)) { if ($event->page_matches("trash_restore") && $user->can(Permissions::VIEW_TRASH)) {
// Try to get the image ID // Try to get the image ID
if($event->count_args() >= 1) {
$image_id = int_escape($event->get_arg(0)); $image_id = int_escape($event->get_arg(0));
if (empty($image_id)) {
$image_id = isset($_POST['image_id']) ? $_POST['image_id'] : null;
} }
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."); throw new SCoreException("Can not restore image: No valid Image ID given.");
} }

View File

@ -187,11 +187,13 @@ class Upload extends Extension
throw new UploadException("Can not replace Image: disk nearly full"); throw new UploadException("Can not replace Image: disk nearly full");
} }
// Try to get the image ID // Try to get the image ID
if($event->count_args() >= 1) {
$image_id = int_escape($event->get_arg(0)); $image_id = int_escape($event->get_arg(0));
if (empty($image_id)) {
$image_id = isset($_POST['image_id']) ? $_POST['image_id'] : null;
} }
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."); throw new UploadException("Can not replace Image: No valid Image ID given.");
} }

View File

@ -53,10 +53,7 @@ class UserPage extends Extension
} elseif ($event->get_arg(0) == "list") { } elseif ($event->get_arg(0) == "list") {
$limit = 50; $limit = 50;
$page_num = int_escape($event->get_arg(1)); $page_num = $event->try_page_num(1);
if ($page_num <= 0) {
$page_num = 1;
}
$offset = ($page_num-1) * $limit; $offset = ($page_num-1) * $limit;
$q = "WHERE 1=1"; $q = "WHERE 1=1";

View File

@ -103,7 +103,7 @@ class Wiki extends Extension
{ {
global $page, $user; global $page, $user;
if ($event->page_matches("wiki")) { 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"; $title = "Index";
} else { } else {
$title = $event->get_arg(0); $title = $event->get_arg(0);