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
{
$user = User::by_name($name);
if ($user) {
if ($user->passhash == md5(strtolower($name) . $pass)) {
$my_user = User::by_name($name);
if ($my_user) {
if ($my_user->passhash == md5(strtolower($name) . $pass)) {
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)) {
log_info("core-user", "Logged in as ".html_escape($name)." ({$user->class->name})");
return $user;
if (password_verify($pass, $my_user->passhash)) {
log_info("core-user", "Logged in as ".html_escape($name)." ({$my_user->class->name})");
return $my_user;
} else {
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
{
global $config, $page;
$user = null;
$my_user = null;
if ($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)) {
$user = $tmp_user;
$my_user = $tmp_user;
}
}
if (is_null($user)) {
$user = User::by_id($config->get_int("anon_id", 0));
if (is_null($my_user)) {
$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

View file

@ -347,9 +347,9 @@ 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)) {
$user = User::by_name($matches[1]);
if (!is_null($user)) {
$user_id = $user->id;
$my_user = User::by_name($matches[1]);
if (!is_null($my_user)) {
$user_id = $my_user->id;
} else {
$user_id = -1;
}

View file

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

View file

@ -120,9 +120,9 @@ 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)) {
$user = User::by_name($matches[1]);
if (!is_null($user)) {
$user_id = $user->id;
$my_user = User::by_name($matches[1]);
if (!is_null($my_user)) {
$user_id = $my_user->id;
} else {
$user_id = -1;
}

View file

@ -112,7 +112,7 @@ class ForumTheme extends Themelet
$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"]);
$gravatar = $poster->get_avatar_html();
@ -140,7 +140,7 @@ class ForumTheme extends Themelet
<td class='forumSupmessage'><div class=deleteLink>".$delete_link."</div></td>
</tr>
<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'>
<div class=postDate><small>".autodate($post['date'])."</small></div>
<div class=postNumber> #".$post_number."</div>

View file

@ -194,9 +194,9 @@ 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)) {
$user = User::by_name($matches[1]);
if (!is_null($user)) {
$user_id = $user->id;
$my_user = User::by_name($matches[1]);
if (!is_null($my_user)) {
$user_id = $my_user->id;
} else {
$user_id = -1;
}

View file

@ -390,10 +390,10 @@ class UserPage extends Extension
private function page_recover(string $username)
{
$user = User::by_name($username);
if (is_null($user)) {
$my_user = User::by_name($username);
if (is_null($my_user)) {
$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");
} else {
// send email