Avoid shadowing global variable

When we aren't referencing the current `global $user`, we should give it
a different name to avoid confusion
This commit is contained in:
Shish 2019-11-11 16:24:13 +00:00
parent bde49c4f5e
commit 247cfcbd77
8 changed files with 31 additions and 31 deletions

View File

@ -105,15 +105,15 @@ class User
public static function by_name_and_pass(string $name, string $pass): ?User public static function by_name_and_pass(string $name, string $pass): ?User
{ {
$user = User::by_name($name); $my_user = User::by_name($name);
if ($user) { if ($my_user) {
if ($user->passhash == md5(strtolower($name) . $pass)) { if ($my_user->passhash == md5(strtolower($name) . $pass)) {
log_info("core-user", "Migrating from md5 to bcrypt for ".html_escape($name)); log_info("core-user", "Migrating from md5 to bcrypt for ".html_escape($name));
$user->set_password($pass); $my_user->set_password($pass);
} }
if (password_verify($pass, $user->passhash)) { if (password_verify($pass, $my_user->passhash)) {
log_info("core-user", "Logged in as ".html_escape($name)." ({$user->class->name})"); log_info("core-user", "Logged in as ".html_escape($name)." ({$my_user->class->name})");
return $user; return $my_user;
} else { } else {
log_warning("core-user", "Failed to log in as ".html_escape($name)." (Invalid password)"); log_warning("core-user", "Failed to log in as ".html_escape($name)." (Invalid password)");
} }

View File

@ -570,19 +570,19 @@ function _decaret(string $str): string
function _get_user(): User function _get_user(): User
{ {
global $config, $page; global $config, $page;
$user = null; $my_user = null;
if ($page->get_cookie("user") && $page->get_cookie("session")) { if ($page->get_cookie("user") && $page->get_cookie("session")) {
$tmp_user = User::by_session($page->get_cookie("user"), $page->get_cookie("session")); $tmp_user = User::by_session($page->get_cookie("user"), $page->get_cookie("session"));
if (!is_null($tmp_user)) { if (!is_null($tmp_user)) {
$user = $tmp_user; $my_user = $tmp_user;
} }
} }
if (is_null($user)) { if (is_null($my_user)) {
$user = User::by_id($config->get_int("anon_id", 0)); $my_user = User::by_id($config->get_int("anon_id", 0));
} }
assert(!is_null($user)); assert(!is_null($my_user));
return $user; return $my_user;
} }
function _get_query(): string function _get_query(): string

View File

@ -347,9 +347,9 @@ class CommentList extends Extension
$comments = $matches[2]; $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)")); $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)) { } elseif (preg_match("/^commented_by[=|:](.*)$/i", $event->term, $matches)) {
$user = User::by_name($matches[1]); $my_user = User::by_name($matches[1]);
if (!is_null($user)) { if (!is_null($my_user)) {
$user_id = $user->id; $user_id = $my_user->id;
} else { } else {
$user_id = -1; $user_id = -1;
} }

View File

@ -269,13 +269,13 @@ class CronUploader extends Extension
if (empty($user_id)) { if (empty($user_id)) {
throw new SCoreException("Cron upload user not set"); throw new SCoreException("Cron upload user not set");
} }
$user = User::by_id($user_id); $my_user = User::by_id($user_id);
if ($user == null) { if ($my_user == null) {
throw new SCoreException("No user found for cron upload user $user_id"); throw new SCoreException("No user found for cron upload user $user_id");
} }
send_event(new UserLoginEvent($user)); send_event(new UserLoginEvent($my_user));
$this->log_message(SCORE_LOG_INFO, "Logged in as user {$user->name}"); $this->log_message(SCORE_LOG_INFO, "Logged in as user {$my_user->name}");
$lockfile = fopen($this->get_lock_file(), "w"); $lockfile = fopen($this->get_lock_file(), "w");
if (!flock($lockfile, LOCK_EX | LOCK_NB)) { if (!flock($lockfile, LOCK_EX | LOCK_NB)) {

View File

@ -120,9 +120,9 @@ class Favorites extends Extension
$favorites = $matches[2]; $favorites = $matches[2];
$event->add_querylet(new Querylet("images.id IN (SELECT id FROM images WHERE favorites $cmp $favorites)")); $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)) { } elseif (preg_match("/^favorited_by[=|:](.*)$/i", $event->term, $matches)) {
$user = User::by_name($matches[1]); $my_user = User::by_name($matches[1]);
if (!is_null($user)) { if (!is_null($my_user)) {
$user_id = $user->id; $user_id = $my_user->id;
} else { } else {
$user_id = -1; $user_id = -1;
} }

View File

@ -112,7 +112,7 @@ class ForumTheme extends Themelet
$message = stripslashes($message); $message = stripslashes($message);
$user = "<a href='".make_link("user/".$post["user_name"]."")."'>".$post["user_name"]."</a>"; $userLink = "<a href='".make_link("user/".$post["user_name"]."")."'>".$post["user_name"]."</a>";
$poster = User::by_name($post["user_name"]); $poster = User::by_name($post["user_name"]);
$gravatar = $poster->get_avatar_html(); $gravatar = $poster->get_avatar_html();
@ -140,7 +140,7 @@ class ForumTheme extends Themelet
<td class='forumSupmessage'><div class=deleteLink>".$delete_link."</div></td> <td class='forumSupmessage'><div class=deleteLink>".$delete_link."</div></td>
</tr> </tr>
<tr class='posBody'> <tr class='posBody'>
<td class='forumUser'>".$user."<br>".$rank."<br>".$gravatar."<br></td> <td class='forumUser'>".$userLink."<br>".$rank."<br>".$gravatar."<br></td>
<td class='forumMessage'> <td class='forumMessage'>
<div class=postDate><small>".autodate($post['date'])."</small></div> <div class=postDate><small>".autodate($post['date'])."</small></div>
<div class=postNumber> #".$post_number."</div> <div class=postNumber> #".$post_number."</div>

View File

@ -194,9 +194,9 @@ class Notes extends Extension
$notes = $matches[2]; $notes = $matches[2];
$event->add_querylet(new Querylet("images.id IN (SELECT id FROM images WHERE notes $cmp $notes)")); $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)) { } elseif (preg_match("/^notes_by[=|:](.*)$/i", $event->term, $matches)) {
$user = User::by_name($matches[1]); $my_user = User::by_name($matches[1]);
if (!is_null($user)) { if (!is_null($my_user)) {
$user_id = $user->id; $user_id = $my_user->id;
} else { } else {
$user_id = -1; $user_id = -1;
} }

View File

@ -390,10 +390,10 @@ class UserPage extends Extension
private function page_recover(string $username) private function page_recover(string $username)
{ {
$user = User::by_name($username); $my_user = User::by_name($username);
if (is_null($user)) { if (is_null($my_user)) {
$this->theme->display_error(404, "Error", "There's no user with that name"); $this->theme->display_error(404, "Error", "There's no user with that name");
} elseif (is_null($user->email)) { } elseif (is_null($my_user->email)) {
$this->theme->display_error(400, "Error", "That user has no registered email address"); $this->theme->display_error(400, "Error", "That user has no registered email address");
} else { } else {
// send email // send email