diff --git a/core/imageboard/image.php b/core/imageboard/image.php index f9dfbefc..af5b7c60 100644 --- a/core/imageboard/image.php +++ b/core/imageboard/image.php @@ -734,7 +734,7 @@ class Image $database->scoreql_to_sql( "INSERT INTO image_tags(image_id, tag_id) VALUES(:id, (SELECT id FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)))" - ), + ), ["id"=>$this->id, "tag"=>$tag] ); } else { diff --git a/core/user.php b/core/user.php index 91329542..4b746a78 100644 --- a/core/user.php +++ b/core/user.php @@ -103,6 +103,16 @@ class User return is_null($row) ? null : new User($row); } + public static function name_to_id(string $name): int + { + $u = User::by_name($name); + if (is_null($u)) { + throw SCoreException("Can't find any user named " . html_escape($name)); + } else { + return $u->id; + } + } + public static function by_name_and_pass(string $name, string $pass): ?User { $my_user = User::by_name($name); diff --git a/ext/comment/main.php b/ext/comment/main.php index e0d31a03..2b954331 100644 --- a/ext/comment/main.php +++ b/ext/comment/main.php @@ -347,13 +347,7 @@ class CommentList extends Extension $comments = $matches[2]; $event->add_querylet(new Querylet("images.id IN (SELECT DISTINCT image_id FROM comments GROUP BY image_id HAVING count(image_id) $cmp $comments)")); } elseif (preg_match("/^commented_by[=|:](.*)$/i", $event->term, $matches)) { - $my_user = User::by_name($matches[1]); - if (!is_null($my_user)) { - $user_id = $my_user->id; - } else { - $user_id = -1; - } - + $user_id = User::name_to_id($matches[1]); $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM comments WHERE owner_id = $user_id)")); } elseif (preg_match("/^commented_by_userno[=|:]([0-9]+)$/i", $event->term, $matches)) { $user_id = int_escape($matches[1]); diff --git a/ext/favorites/main.php b/ext/favorites/main.php index 3772f8c8..0c54382b 100644 --- a/ext/favorites/main.php +++ b/ext/favorites/main.php @@ -120,13 +120,7 @@ class Favorites extends Extension $favorites = $matches[2]; $event->add_querylet(new Querylet("images.id IN (SELECT id FROM images WHERE favorites $cmp $favorites)")); } elseif (preg_match("/^favorited_by[=|:](.*)$/i", $event->term, $matches)) { - $my_user = User::by_name($matches[1]); - if (!is_null($my_user)) { - $user_id = $my_user->id; - } else { - $user_id = -1; - } - + $user_id = User::name_to_id($matches[1]); $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM user_favorites WHERE user_id = $user_id)")); } elseif (preg_match("/^favorited_by_userno[=|:](\d+)$/i", $event->term, $matches)) { $user_id = int_escape($matches[1]); diff --git a/ext/notes/main.php b/ext/notes/main.php index a69cb6ea..702603bb 100644 --- a/ext/notes/main.php +++ b/ext/notes/main.php @@ -194,13 +194,7 @@ class Notes extends Extension $notes = $matches[2]; $event->add_querylet(new Querylet("images.id IN (SELECT id FROM images WHERE notes $cmp $notes)")); } elseif (preg_match("/^notes_by[=|:](.*)$/i", $event->term, $matches)) { - $my_user = User::by_name($matches[1]); - if (!is_null($my_user)) { - $user_id = $my_user->id; - } else { - $user_id = -1; - } - + $user_id = User::name_to_id($matches[1]); $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE user_id = $user_id)")); } elseif (preg_match("/^(notes_by_userno|notes_by_user_id)[=|:](\d+)$/i", $event->term, $matches)) { $user_id = int_escape($matches[2]); diff --git a/ext/user/main.php b/ext/user/main.php index bfcc0423..9ac332e9 100644 --- a/ext/user/main.php +++ b/ext/user/main.php @@ -296,12 +296,7 @@ class UserPage extends Extension $matches = []; if (preg_match("/^(?:poster|user)[=|:](.*)$/i", $event->term, $matches)) { - $duser = User::by_name($matches[1]); - if (!is_null($duser)) { - $user_id = $duser->id; - } else { - $user_id = -1; - } + $user_id = User::name_to_id($matches[1]); $event->add_querylet(new Querylet("images.owner_id = $user_id")); } elseif (preg_match("/^(?:poster|user)_id[=|:]([0-9]+)$/i", $event->term, $matches)) { $user_id = int_escape($matches[1]);