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.)
*/
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;
}
}

View File

@ -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);

View File

@ -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

View File

@ -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":
/**

View File

@ -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;

View File

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

View File

@ -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);
}
}
}

View File

@ -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);
}
}

View File

@ -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(

View File

@ -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"];

View File

@ -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');

View File

@ -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);

View File

@ -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);
}
}
}

View File

@ -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")) {

View File

@ -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);

View File

@ -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.");
}

View File

@ -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.");
}

View File

@ -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";

View File

@ -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);