[aur-dev] [PATCH 1/3] Do not return "None" in user functions
Return null instead of the string "None" in username_from_id(), uid_from_email() and uid_from_username(). Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de> --- web/html/passreset.php | 2 +- web/lib/acctfuncs.inc.php | 2 +- web/lib/aur.inc.php | 44 ++++++++++++++++++++----------------- web/template/pkg_details.php | 18 +++++++-------- web/template/pkgbase_details.php | 18 +++++++-------- web/template/tu_details.php | 2 +- web/template/tu_last_votes_list.php | 4 ++-- 7 files changed, 47 insertions(+), 43 deletions(-) diff --git a/web/html/passreset.php b/web/html/passreset.php index 9541021..9d8e1ae 100644 --- a/web/html/passreset.php +++ b/web/html/passreset.php @@ -25,7 +25,7 @@ if (isset($_GET['resetkey'], $_POST['email'], $_POST['password'], $_POST['confir $error = __('Missing a required field.'); } elseif ($password != $confirm) { $error = __('Password fields do not match.'); - } elseif ($uid == NULL || $uid == 'None') { + } elseif ($uid == null) { $error = __('Invalid e-mail.'); } diff --git a/web/lib/acctfuncs.inc.php b/web/lib/acctfuncs.inc.php index 51ffec6..a996561 100644 --- a/web/lib/acctfuncs.inc.php +++ b/web/lib/acctfuncs.inc.php @@ -642,7 +642,7 @@ function send_resetkey($email, $subject, $body) { global $AUR_LOCATION; $uid = uid_from_email($email); - if ($uid != NULL && $uid != 'None') { + if ($uid != null) { /* * We (ab)use new_sid() to get a random 32 characters long * string. diff --git a/web/lib/aur.inc.php b/web/lib/aur.inc.php index 99f5ae4..3368696 100644 --- a/web/lib/aur.inc.php +++ b/web/lib/aur.inc.php @@ -135,20 +135,19 @@ function new_sid() { * * @param string $id User's ID * - * @return string Username if it exists, otherwise "None" + * @return string Username if it exists, otherwise null */ -function username_from_id($id="") { - if (!$id) { - return ""; - } +function username_from_id($id) { + $id = intval($id); + $dbh = DB::connect(); $q = "SELECT Username FROM Users WHERE ID = " . $dbh->quote($id); $result = $dbh->query($q); if (!$result) { - return "None"; + return null; } - $row = $result->fetch(PDO::FETCH_NUM); + $row = $result->fetch(PDO::FETCH_NUM); return $row[0]; } @@ -178,6 +177,17 @@ function username_from_sid($sid="") { } /** + * Format a user name for inclusion in HTML data + * + * @param string $username The user name to format + * + * @return void + */ +function html_format_username($username) { + return $username ? htmlspecialchars($username) : __("None"); +} + +/** * Determine the user's e-mail address in the database using a session ID * * @param string $sid User's session ID @@ -363,20 +373,17 @@ function rm_tree($dirname) { * * @param string $username The username of an account * - * @return string Return user ID if exists for username, otherwise "None" + * @return string Return user ID if exists for username, otherwise null */ -function uid_from_username($username="") { - if (!$username) { - return ""; - } +function uid_from_username($username) { $dbh = DB::connect(); $q = "SELECT ID FROM Users WHERE Username = " . $dbh->quote($username); $result = $dbh->query($q); if (!$result) { - return "None"; + return null; } - $row = $result->fetch(PDO::FETCH_NUM); + $row = $result->fetch(PDO::FETCH_NUM); return $row[0]; } @@ -387,18 +394,15 @@ function uid_from_username($username="") { * * @return string The user's ID */ -function uid_from_email($email="") { - if (!$email) { - return ""; - } +function uid_from_email($email) { $dbh = DB::connect(); $q = "SELECT ID FROM Users WHERE Email = " . $dbh->quote($email); $result = $dbh->query($q); if (!$result) { - return "None"; + return null; } - $row = $result->fetch(PDO::FETCH_NUM); + $row = $result->fetch(PDO::FETCH_NUM); return $row[0]; } diff --git a/web/template/pkg_details.php b/web/template/pkg_details.php index c813e35..6326d4e 100644 --- a/web/template/pkg_details.php +++ b/web/template/pkg_details.php @@ -261,12 +261,12 @@ if ($row["SubmitterUID"]): if ($SID): if (!$USE_VIRTUAL_URLS): ?> - <td><a href="<?= get_uri('/account/'); ?>?Action=AccountInfo&ID=<?= htmlspecialchars($row['SubmitterUID'], ENT_QUOTES) ?>" title="<?= __('View account information for')?> <?= htmlspecialchars($submitter) ?>"><?= htmlspecialchars($submitter) ?></a></td> + <td><a href="<?= get_uri('/account/'); ?>?Action=AccountInfo&ID=<?= htmlspecialchars($row['SubmitterUID'], ENT_QUOTES) ?>" title="<?= __('View account information for')?> <?= html_format_username($submitter) ?>"><?= html_format_username($submitter) ?></a></td> <?php else: ?> - <td><a href="<?= get_uri('/account/') . htmlspecialchars($submitter, ENT_QUOTES) ?>" title="<?= __('View account information for %s', htmlspecialchars($submitter)) ?>"><?= htmlspecialchars($submitter) ?></a></td> + <td><a href="<?= get_uri('/account/') . html_format_username($submitter) ?>" title="<?= __('View account information for %s', html_format_username($submitter)) ?>"><?= html_format_username($submitter) ?></a></td> <?php endif; ?> <?php else: ?> - <td><?= htmlspecialchars($submitter) ?></td> + <td><?= html_format_username($submitter) ?></td> <?php endif; ?> <?php else: ?> <td><?= __('None') ?></td> @@ -279,12 +279,12 @@ if ($row["MaintainerUID"]): if ($SID): if (!$USE_VIRTUAL_URLS): ?> - <td><a href="<?= get_uri('/account/'); ?>?Action=AccountInfo&ID=<?= htmlspecialchars($row['MaintainerUID'], ENT_QUOTES) ?>" title="<?= __('View account information for')?> <?= htmlspecialchars($maintainer) ?>"><?= htmlspecialchars($maintainer) ?></a></td> + <td><a href="<?= get_uri('/account/'); ?>?Action=AccountInfo&ID=<?= htmlspecialchars($row['MaintainerUID'], ENT_QUOTES) ?>" title="<?= __('View account information for')?> <?= html_format_username($maintainer) ?>"><?= html_format_username($maintainer) ?></a></td> <?php else: ?> - <td><a href="<?= get_uri('/account/') . htmlspecialchars($maintainer, ENT_QUOTES) ?>" title="<?= __('View account information for %s', htmlspecialchars($maintainer)) ?>"><?= htmlspecialchars($maintainer) ?></a></td> + <td><a href="<?= get_uri('/account/') . html_format_username($maintainer) ?>" title="<?= __('View account information for %s', html_format_username($maintainer)) ?>"><?= html_format_username($maintainer) ?></a></td> <?php endif; ?> <?php else: ?> - <td><?= htmlspecialchars($maintainer) ?></td> + <td><?= html_format_username($maintainer) ?></td> <?php endif; ?> <?php else: ?> <td><?= __('None') ?></td> @@ -297,12 +297,12 @@ if ($row["PackagerUID"]): if ($SID): if (!$USE_VIRTUAL_URLS): ?> - <td><a href="<?= get_uri('/account/'); ?>?Action=AccountInfo&ID=<?= htmlspecialchars($row['PackagerUID'], ENT_QUOTES) ?>" title="<?= __('View account information for')?> <?= htmlspecialchars($packager) ?>"><?= htmlspecialchars($packager) ?></a></td> + <td><a href="<?= get_uri('/account/'); ?>?Action=AccountInfo&ID=<?= htmlspecialchars($row['PackagerUID'], ENT_QUOTES) ?>" title="<?= __('View account information for')?> <?= html_format_username($packager) ?>"><?= html_format_username($packager) ?></a></td> <?php else: ?> - <td><a href="<?= get_uri('/account/') . htmlspecialchars($packager, ENT_QUOTES) ?>" title="<?= __('View account information for %s', htmlspecialchars($packager)) ?>"><?= htmlspecialchars($packager) ?></a></td> + <td><a href="<?= get_uri('/account/') . html_format_username($packager) ?>" title="<?= __('View account information for %s', html_format_username($packager)) ?>"><?= html_format_username($packager) ?></a></td> <?php endif; ?> <?php else: ?> - <td><?= htmlspecialchars($packager) ?></td> + <td><?= html_format_username($packager) ?></td> <?php endif; ?> <?php else: ?> <td><?= __('None') ?></td> diff --git a/web/template/pkgbase_details.php b/web/template/pkgbase_details.php index da9a962..6c617bf 100644 --- a/web/template/pkgbase_details.php +++ b/web/template/pkgbase_details.php @@ -144,12 +144,12 @@ if ($row["SubmitterUID"]): if ($SID): if (!$USE_VIRTUAL_URLS): ?> - <td><a href="<?= get_uri('/account/'); ?>?Action=AccountInfo&ID=<?= htmlspecialchars($row['SubmitterUID'], ENT_QUOTES) ?>" title="<?= __('View account information for')?> <?= htmlspecialchars($submitter) ?>"><?= htmlspecialchars($submitter) ?></a></td> + <td><a href="<?= get_uri('/account/'); ?>?Action=AccountInfo&ID=<?= htmlspecialchars($row['SubmitterUID'], ENT_QUOTES) ?>" title="<?= __('View account information for')?> <?= html_format_username($submitter) ?>"><?= html_format_username($submitter) ?></a></td> <?php else: ?> - <td><a href="<?= get_uri('/account/') . htmlspecialchars($submitter, ENT_QUOTES) ?>" title="<?= __('View account information for %s', htmlspecialchars($submitter)) ?>"><?= htmlspecialchars($submitter) ?></a></td> + <td><a href="<?= get_uri('/account/') . html_format_username($submitter, ENT_QUOTES) ?>" title="<?= __('View account information for %s', html_format_username($submitter)) ?>"><?= html_format_username($submitter) ?></a></td> <?php endif; ?> <?php else: ?> - <td><?= htmlspecialchars($submitter) ?></td> + <td><?= html_format_username($submitter) ?></td> <?php endif; ?> <?php else: ?> <td><?= __('None') ?></td> @@ -162,12 +162,12 @@ if ($row["MaintainerUID"]): if ($SID): if (!$USE_VIRTUAL_URLS): ?> - <td><a href="<?= get_uri('/account/'); ?>?Action=AccountInfo&ID=<?= htmlspecialchars($row['MaintainerUID'], ENT_QUOTES) ?>" title="<?= __('View account information for')?> <?= htmlspecialchars($maintainer) ?>"><?= htmlspecialchars($maintainer) ?></a></td> + <td><a href="<?= get_uri('/account/'); ?>?Action=AccountInfo&ID=<?= htmlspecialchars($row['MaintainerUID'], ENT_QUOTES) ?>" title="<?= __('View account information for')?> <?= html_format_username($maintainer) ?>"><?= html_format_username($maintainer) ?></a></td> <?php else: ?> - <td><a href="<?= get_uri('/account/') . htmlspecialchars($maintainer, ENT_QUOTES) ?>" title="<?= __('View account information for %s', htmlspecialchars($maintainer)) ?>"><?= htmlspecialchars($maintainer) ?></a></td> + <td><a href="<?= get_uri('/account/') . html_format_username($maintainer) ?>" title="<?= __('View account information for %s', html_format_username($maintainer)) ?>"><?= html_format_username($maintainer) ?></a></td> <?php endif; ?> <?php else: ?> - <td><?= htmlspecialchars($maintainer) ?></td> + <td><?= html_format_username($maintainer) ?></td> <?php endif; ?> <?php else: ?> <td><?= __('None') ?></td> @@ -180,12 +180,12 @@ if ($row["PackagerUID"]): if ($SID): if (!$USE_VIRTUAL_URLS): ?> - <td><a href="<?= get_uri('/account/'); ?>?Action=AccountInfo&ID=<?= htmlspecialchars($row['PackagerUID'], ENT_QUOTES) ?>" title="<?= __('View account information for')?> <?= htmlspecialchars($packager) ?>"><?= htmlspecialchars($packager) ?></a></td> + <td><a href="<?= get_uri('/account/'); ?>?Action=AccountInfo&ID=<?= htmlspecialchars($row['PackagerUID'], ENT_QUOTES) ?>" title="<?= __('View account information for')?> <?= html_format_username($packager) ?>"><?= html_format_username($packager) ?></a></td> <?php else: ?> - <td><a href="<?= get_uri('/account/') . htmlspecialchars($packager, ENT_QUOTES) ?>" title="<?= __('View account information for %s', htmlspecialchars($packager)) ?>"><?= htmlspecialchars($packager) ?></a></td> + <td><a href="<?= get_uri('/account/') . html_format_username($packager) ?>" title="<?= __('View account information for %s', html_format_username($packager)) ?>"><?= html_format_username($packager) ?></a></td> <?php endif; ?> <?php else: ?> - <td><?= htmlspecialchars($packager) ?></td> + <td><?= html_format_username($packager) ?></td> <?php endif; ?> <?php else: ?> <td><?= __('None') ?></td> diff --git a/web/template/tu_details.php b/web/template/tu_details.php index fca1815..38f6c0d 100644 --- a/web/template/tu_details.php +++ b/web/template/tu_details.php @@ -39,7 +39,7 @@ if ($yes > $active_tus / 2) { <?php endif; ?> </strong> <br /> - <?= __("Submitted: %s by %s", gmdate("Y-m-d H:i", $row['Submitted']), username_from_id($row['SubmitterID'])) ?> + <?= __("Submitted: %s by %s", gmdate("Y-m-d H:i", $row['Submitted']), html_format_username(username_from_id($row['SubmitterID']))) ?> <br /> <?= __("End") ?>: <strong><?= gmdate("Y-m-d H:i", $row['End']) ?></strong> diff --git a/web/template/tu_last_votes_list.php b/web/template/tu_last_votes_list.php index 090ce8d..e897a6a 100644 --- a/web/template/tu_last_votes_list.php +++ b/web/template/tu_last_votes_list.php @@ -22,9 +22,9 @@ <tr class="<?= $c ?>"> <td> <?php if (!$USE_VIRTUAL_URLS): ?> - <a href="<?= get_uri('/account/'); ?>?Action=AccountInfo&ID=<?= htmlspecialchars($row['UserID'], ENT_QUOTES) ?>" title="<?= __('View account information for')?> <?= htmlspecialchars($username) ?>"><?= htmlspecialchars($username) ?></a></td> + <a href="<?= get_uri('/account/'); ?>?Action=AccountInfo&ID=<?= htmlspecialchars($row['UserID'], ENT_QUOTES) ?>" title="<?= __('View account information for')?> <?= html_format_username($username) ?>"><?= html_format_username($username) ?></a></td> <?php else: ?> - <a href="<?= get_uri('/account/') . htmlspecialchars($username, ENT_QUOTES) ?>" title="<?= __('View account information for %s', htmlspecialchars($username)) ?>"><?= htmlspecialchars($username) ?></a> + <a href="<?= get_uri('/account/') . html_format_username($username) ?>" title="<?= __('View account information for %s', html_format_username($username)) ?>"><?= html_format_username($username) ?></a> <?php endif; ?> </td> <td> -- 2.0.0
Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de> --- web/lib/acctfuncs.inc.php | 521 +++++++++++++++++++++++----------------------- 1 file changed, 264 insertions(+), 257 deletions(-) diff --git a/web/lib/acctfuncs.inc.php b/web/lib/acctfuncs.inc.php index a996561..f1776c0 100644 --- a/web/lib/acctfuncs.inc.php +++ b/web/lib/acctfuncs.inc.php @@ -184,110 +184,115 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", "<strong>", htmlspecialchars($E,ENT_QUOTES), "</strong>"); } } + if ($error) { print "<ul class='errorlist'><li>".$error."</li></ul>\n"; display_account_form($UTYPE, $A, $U, $T, $S, $E, "", "", $R, $L, $I, $K, $J, $UID); + return; + } + + if ($TYPE == "new") { + /* Create an unprivileged user. */ + $salt = generate_salt(); + if (empty($P)) { + $send_resetkey = true; + $email = $E; + } else { + $send_resetkey = false; + $P = salted_hash($P, $salt); + } + $U = $dbh->quote($U); + $E = $dbh->quote($E); + $P = $dbh->quote($P); + $salt = $dbh->quote($salt); + $R = $dbh->quote($R); + $L = $dbh->quote($L); + $I = $dbh->quote($I); + $K = $dbh->quote(str_replace(" ", "", $K)); + $q = "INSERT INTO Users (AccountTypeID, Suspended, "; + $q.= "InactivityTS, Username, Email, Passwd, Salt, "; + $q.= "RealName, LangPreference, IRCNick, PGPKey) "; + $q.= "VALUES (1, 0, 0, $U, $E, $P, $salt, $R, $L, "; + $q.= "$I, $K)"; + $result = $dbh->exec($q); + if (!$result) { + print __("Error trying to create account, %s%s%s.", + "<strong>", htmlspecialchars($U,ENT_QUOTES), "</strong>"); + return; + } + + print __("The account, %s%s%s, has been successfully created.", + "<strong>", htmlspecialchars($U,ENT_QUOTES), "</strong>"); + print "<p>\n"; + + if (!$send_resetkey) { + print __("Click on the Login link above to use your account."); + print "</p>\n"; + return; + } + + $subject = 'Welcome to the Arch User Repository'; + $body = __('Welcome to %s! In order ' . + 'to set an initial password ' . + 'for your new account, ' . + 'please click the link ' . + 'below. If the link does ' . + 'not work try copying and ' . + 'pasting it into your ' . + 'browser.', + $AUR_LOCATION); + send_resetkey($email, $subject, $body); + + print __("A password reset key has been sent to your e-mail address."); + print "</p>\n"; } else { - if ($TYPE == "new") { - /* Create an unprivileged user. */ - $salt = generate_salt(); - if (empty($P)) { - $send_resetkey = true; - $email = $E; - } else { - $send_resetkey = false; - $P = salted_hash($P, $salt); - } - $U = $dbh->quote($U); - $E = $dbh->quote($E); - $P = $dbh->quote($P); - $salt = $dbh->quote($salt); - $R = $dbh->quote($R); - $L = $dbh->quote($L); - $I = $dbh->quote($I); - $K = $dbh->quote(str_replace(" ", "", $K)); - $q = "INSERT INTO Users (AccountTypeID, Suspended, "; - $q.= "InactivityTS, Username, Email, Passwd, Salt, "; - $q.= "RealName, LangPreference, IRCNick, PGPKey) "; - $q.= "VALUES (1, 0, 0, $U, $E, $P, $salt, $R, $L, "; - $q.= "$I, $K)"; - $result = $dbh->exec($q); - if (!$result) { - print __("Error trying to create account, %s%s%s.", - "<strong>", htmlspecialchars($U,ENT_QUOTES), "</strong>"); - } else { - print __("The account, %s%s%s, has been successfully created.", - "<strong>", htmlspecialchars($U,ENT_QUOTES), "</strong>"); - print "<p>\n"; - if ($send_resetkey) { - $subject = 'Welcome to the Arch User Repository'; - $body = __('Welcome to %s! In order ' . - 'to set an initial password ' . - 'for your new account, ' . - 'please click the link ' . - 'below. If the link does ' . - 'not work try copying and ' . - 'pasting it into your ' . - 'browser.', - $AUR_LOCATION); - send_resetkey($email, $subject, $body); - print __("A password reset key has been sent to your e-mail address."); - } else { - print __("Click on the Login link above to use your account."); - } - print "</p>\n"; - } + /* Modify an existing account. */ + $q = "SELECT InactivityTS FROM Users WHERE "; + $q.= "ID = " . intval($UID); + $result = $dbh->query($q); + $row = $result->fetch(PDO::FETCH_NUM); + if ($row[0] && $J) { + $inactivity_ts = $row[0]; + } elseif ($J) { + $inactivity_ts = time(); + } else { + $inactivity_ts = 0; + } + $q = "UPDATE Users SET "; + $q.= "Username = " . $dbh->quote($U); + if ($T) { + $q.= ", AccountTypeID = ".intval($T); + } + if ($S) { + /* Ensure suspended users can't keep an active session */ + delete_user_sessions($UID); + $q.= ", Suspended = 1"; } else { - /* Modify an existing account. */ - $q = "SELECT InactivityTS FROM Users WHERE "; - $q.= "ID = " . intval($UID); - $result = $dbh->query($q); - $row = $result->fetch(PDO::FETCH_NUM); - if ($row[0] && $J) { - $inactivity_ts = $row[0]; - } elseif ($J) { - $inactivity_ts = time(); - } else { - $inactivity_ts = 0; - } - - $q = "UPDATE Users SET "; - $q.= "Username = " . $dbh->quote($U); - if ($T) { - $q.= ", AccountTypeID = ".intval($T); - } - if ($S) { - /* Ensure suspended users can't keep an active session */ - delete_user_sessions($UID); - $q.= ", Suspended = 1"; - } else { - $q.= ", Suspended = 0"; - } - $q.= ", Email = " . $dbh->quote($E); - if ($P) { - $salt = generate_salt(); - $hash = salted_hash($P, $salt); - $q .= ", Passwd = '$hash', Salt = '$salt'"; - } - $q.= ", RealName = " . $dbh->quote($R); - $q.= ", LangPreference = " . $dbh->quote($L); - $q.= ", IRCNick = " . $dbh->quote($I); - $q.= ", PGPKey = " . $dbh->quote(str_replace(" ", "", $K)); - $q.= ", InactivityTS = " . $inactivity_ts; - $q.= " WHERE ID = ".intval($UID); - $result = $dbh->exec($q); - if (!$result) { - print __("No changes were made to the account, %s%s%s.", - "<strong>", htmlspecialchars($U,ENT_QUOTES), "</strong>"); - } else { - print __("The account, %s%s%s, has been successfully modified.", - "<strong>", htmlspecialchars($U,ENT_QUOTES), "</strong>"); - } + $q.= ", Suspended = 0"; + } + $q.= ", Email = " . $dbh->quote($E); + if ($P) { + $salt = generate_salt(); + $hash = salted_hash($P, $salt); + $q .= ", Passwd = '$hash', Salt = '$salt'"; + } + $q.= ", RealName = " . $dbh->quote($R); + $q.= ", LangPreference = " . $dbh->quote($L); + $q.= ", IRCNick = " . $dbh->quote($I); + $q.= ", PGPKey = " . $dbh->quote(str_replace(" ", "", $K)); + $q.= ", InactivityTS = " . $inactivity_ts; + $q.= " WHERE ID = ".intval($UID); + $result = $dbh->exec($q); + if (!$result) { + print __("No changes were made to the account, %s%s%s.", + "<strong>", htmlspecialchars($U,ENT_QUOTES), "</strong>"); + } else { + print __("The account, %s%s%s, has been successfully modified.", + "<strong>", htmlspecialchars($U,ENT_QUOTES), "</strong>"); } } - return; } /** @@ -408,100 +413,100 @@ function try_login() { $new_sid = ""; $userID = null; - if ( isset($_REQUEST['user']) || isset($_REQUEST['passwd']) ) { - if (is_ipbanned()) { - $login_error = __('The login form is currently disabled ' . - 'for your IP address, probably due ' . - 'to sustained spam attacks. Sorry for the ' . - 'inconvenience.'); - return array('SID' => '', 'error' => $login_error); - } - $dbh = DB::connect(); - $userID = valid_user($_REQUEST['user']); + if (!isset($_REQUEST['user']) && !isset($_REQUEST['passwd'])) { + return array('SID' => '', 'error' => null); + } + + if (is_ipbanned()) { + $login_error = __('The login form is currently disabled ' . + 'for your IP address, probably due ' . + 'to sustained spam attacks. Sorry for the ' . + 'inconvenience.'); + return array('SID' => '', 'error' => $login_error); + } + + $dbh = DB::connect(); + $userID = valid_user($_REQUEST['user']); + + if (user_suspended($userID)) { + $login_error = __('Account suspended'); + return array('SID' => '', 'error' => $login_error); + } elseif (passwd_is_empty($userID)) { + $login_error = __('Your password has been reset. ' . + 'If you just created a new account, please ' . + 'use the link from the confirmation email ' . + 'to set an initial password. Otherwise, ' . + 'please request a reset key on the %s' . + 'Password Reset%s page.', '<a href="' . + htmlspecialchars(get_uri('/passreset')) . '">', + '</a>'); + return array('SID' => '', 'error' => $login_error); + } elseif (!valid_passwd($userID, $_REQUEST['passwd'])) { + $login_error = __("Bad username or password."); + return array('SID' => '', 'error' => $login_error); + } - if ( user_suspended($userID) ) { - $login_error = __('Account suspended'); + $logged_in = 0; + $num_tries = 0; + + /* Generate a session ID and store it. */ + while (!$logged_in && $num_tries < 5) { + if ($MAX_SESSIONS_PER_USER) { + /* + * Delete all user sessions except the + * last ($MAX_SESSIONS_PER_USER - 1). + */ + $q = "DELETE s.* FROM Sessions s "; + $q.= "LEFT JOIN (SELECT SessionID FROM Sessions "; + $q.= "WHERE UsersId = " . $userID . " "; + $q.= "ORDER BY LastUpdateTS DESC "; + $q.= "LIMIT " . ($MAX_SESSIONS_PER_USER - 1) . ") q "; + $q.= "ON s.SessionID = q.SessionID "; + $q.= "WHERE s.UsersId = " . $userID . " "; + $q.= "AND q.SessionID IS NULL;"; + $dbh->query($q); } - elseif ( $userID && isset($_REQUEST['passwd']) - && valid_passwd($userID, $_REQUEST['passwd']) ) { - - $logged_in = 0; - $num_tries = 0; - - /* Generate a session ID and store it. */ - while (!$logged_in && $num_tries < 5) { - if ($MAX_SESSIONS_PER_USER) { - /* - * Delete all user sessions except the - * last ($MAX_SESSIONS_PER_USER - 1). - */ - $q = "DELETE s.* FROM Sessions s "; - $q.= "LEFT JOIN (SELECT SessionID FROM Sessions "; - $q.= "WHERE UsersId = " . $userID . " "; - $q.= "ORDER BY LastUpdateTS DESC "; - $q.= "LIMIT " . ($MAX_SESSIONS_PER_USER - 1) . ") q "; - $q.= "ON s.SessionID = q.SessionID "; - $q.= "WHERE s.UsersId = " . $userID . " "; - $q.= "AND q.SessionID IS NULL;"; - $dbh->query($q); - } - - $new_sid = new_sid(); - $q = "INSERT INTO Sessions (UsersID, SessionID, LastUpdateTS)" - ." VALUES (" . $userID . ", '" . $new_sid . "', UNIX_TIMESTAMP())"; - $result = $dbh->exec($q); - - /* Query will fail if $new_sid is not unique. */ - if ($result) { - $logged_in = 1; - break; - } - - $num_tries++; - } - - if ($logged_in) { - $q = "UPDATE Users SET LastLogin = UNIX_TIMESTAMP(), "; - $q.= "LastLoginIPAddress = " . $dbh->quote(ip2long($_SERVER['REMOTE_ADDR'])) . " "; - $q.= "WHERE ID = '$userID'"; - $dbh->exec($q); - - /* Set the SID cookie. */ - if (isset($_POST['remember_me']) && - $_POST['remember_me'] == "on") { - /* Set cookies for 30 days. */ - $cookie_time = time() + $PERSISTENT_COOKIE_TIMEOUT; - - /* Set session for 30 days. */ - $q = "UPDATE Sessions SET LastUpdateTS = $cookie_time "; - $q.= "WHERE SessionID = '$new_sid'"; - $dbh->exec($q); - } - else - $cookie_time = 0; - - setcookie("AURSID", $new_sid, $cookie_time, "/", null, !empty($_SERVER['HTTPS']), true); - header("Location: " . get_uri('/')); - $login_error = ""; - - } - else { - $login_error = __('An error occurred trying to generate a user session.'); - } - } elseif (passwd_is_empty($userID)) { - $login_error = __('Your password has been reset. ' . - 'If you just created a new account, please ' . - 'use the link from the confirmation email ' . - 'to set an initial password. Otherwise, ' . - 'please request a reset key on the %s' . - 'Password Reset%s page.', '<a href="' . - htmlspecialchars(get_uri('/passreset')) . '">', - '</a>'); - } else { - $login_error = __("Bad username or password."); + + $new_sid = new_sid(); + $q = "INSERT INTO Sessions (UsersID, SessionID, LastUpdateTS)" + ." VALUES (" . $userID . ", '" . $new_sid . "', UNIX_TIMESTAMP())"; + $result = $dbh->exec($q); + + /* Query will fail if $new_sid is not unique. */ + if ($result) { + $logged_in = 1; + break; } + + $num_tries++; + } + + if (!$logged_in) { + $login_error = __('An error occurred trying to generate a user session.'); + return array('SID' => $new_sid, 'error' => $login_error); } - return array('SID' => $new_sid, 'error' => $login_error); + + $q = "UPDATE Users SET LastLogin = UNIX_TIMESTAMP(), "; + $q.= "LastLoginIPAddress = " . $dbh->quote(ip2long($_SERVER['REMOTE_ADDR'])) . " "; + $q.= "WHERE ID = '$userID'"; + $dbh->exec($q); + + /* Set the SID cookie. */ + if (isset($_POST['remember_me']) && $_POST['remember_me'] == "on") { + /* Set cookies for 30 days. */ + $cookie_time = time() + $PERSISTENT_COOKIE_TIMEOUT; + + /* Set session for 30 days. */ + $q = "UPDATE Sessions SET LastUpdateTS = $cookie_time "; + $q.= "WHERE SessionID = '$new_sid'"; + $dbh->exec($q); + } else { + $cookie_time = 0; + } + + setcookie("AURSID", $new_sid, $cookie_time, "/", null, !empty($_SERVER['HTTPS']), true); + header("Location: " . get_uri('/')); + $login_error = ""; } /** @@ -515,11 +520,7 @@ function is_ipbanned() { $q = "SELECT * FROM Bans WHERE IPAddress = " . $dbh->quote(ip2long($_SERVER['REMOTE_ADDR'])); $result = $dbh->query($q); - if ($result->fetchColumn()) { - return true; - } else { - return false; - } + return ($result->fetchColumn() ? true : false); } /** @@ -553,17 +554,21 @@ function valid_username($user) { * @return string|void Return user ID if in database, otherwise void */ function valid_user($user) { - if ($user) { - $dbh = DB::connect(); + if (!$user) { + return false; + } - $q = "SELECT ID FROM Users WHERE "; - $q.= "Username = " . $dbh->quote($user); - $result = $dbh->query($q); - if ($result) { - $row = $result->fetch(PDO::FETCH_NUM); - return $row[0]; - } + $dbh = DB::connect(); + + $q = "SELECT ID FROM Users WHERE "; + $q.= "Username = " . $dbh->quote($user); + $result = $dbh->query($q); + if (!$result) { + return null; } + + $row = $result->fetch(PDO::FETCH_NUM); + return $row[0]; } /** @@ -578,12 +583,8 @@ function open_user_proposals($user) { $q = "SELECT * FROM TU_VoteInfo WHERE User = " . $dbh->quote($user) . " "; $q.= "AND End > UNIX_TIMESTAMP()"; $result = $dbh->query($q); - if ($result->fetchColumn()) { - return true; - } - else { - return false; - } + + return ($result->fetchColumn() ? true : false); } /** @@ -642,26 +643,26 @@ function send_resetkey($email, $subject, $body) { global $AUR_LOCATION; $uid = uid_from_email($email); - if ($uid != null) { - /* - * We (ab)use new_sid() to get a random 32 characters long - * string. - */ - $resetkey = new_sid(); - create_resetkey($resetkey, $uid); - /* Send e-mail with confirmation link. */ - $body = wordwrap($body, 70); - $body .= "\n\n". - "{$AUR_LOCATION}/" . get_uri('/passreset/') . "?". - "resetkey={$resetkey}"; - $headers = "MIME-Version: 1.0\r\n" . - "Content-type: text/plain; charset=UTF-8\r\n" . - "Reply-to: noreply@aur.archlinux.org\r\n" . - "From: notify@aur.achlinux.org\r\n" . - "X-Mailer: PHP\r\n" . - "X-MimeOLE: Produced By AUR"; - @mail($email, $subject, $body, $headers); + if ($uid == null) { + return; } + + /* We (ab)use new_sid() to get a random 32 characters long string. */ + $resetkey = new_sid(); + create_resetkey($resetkey, $uid); + + /* Send e-mail with confirmation link. */ + $body = wordwrap($body, 70); + $body .= "\n\n". + "{$AUR_LOCATION}/" . get_uri('/passreset/') . "?". + "resetkey={$resetkey}"; + $headers = "MIME-Version: 1.0\r\n" . + "Content-type: text/plain; charset=UTF-8\r\n" . + "Reply-to: noreply@aur.archlinux.org\r\n" . + "From: notify@aur.achlinux.org\r\n" . + "X-Mailer: PHP\r\n" . + "X-MimeOLE: Produced By AUR"; + @mail($email, $subject, $body, $headers); } /** @@ -718,41 +719,47 @@ function good_passwd($passwd) { */ function valid_passwd($userID, $passwd) { $dbh = DB::connect(); - if ( strlen($passwd) > 0 ) { - /* Get salt for this user. */ - $salt = get_salt($userID); - if ($salt) { - $q = "SELECT ID FROM Users "; - $q.= "WHERE ID = " . $userID . " "; - $q.= "AND Passwd = " . $dbh->quote(salted_hash($passwd, $salt)); - $result = $dbh->query($q); - if ($result) { - $row = $result->fetch(PDO::FETCH_NUM); - if ($row[0]) { - return true; - } - } - } else { - /* Check password without using salt. */ - $q = "SELECT ID FROM Users "; - $q.= "WHERE ID = " . $userID . " "; - $q.= "AND Passwd = " . $dbh->quote(md5($passwd)); - $result = $dbh->query($q); - if ($result) { - $row = $result->fetch(PDO::FETCH_NUM); - if ($row[0]) { - /* Password correct, but salt it first! */ - if (!save_salt($userID, $passwd)) { - trigger_error("Unable to salt user's password;" . - " ID " . $userID, E_USER_WARNING); - return false; - } - return true; - } - } + if ($passwd == "") { + return false; + } + + /* Get salt for this user. */ + $salt = get_salt($userID); + if ($salt) { + $q = "SELECT ID FROM Users "; + $q.= "WHERE ID = " . $userID . " "; + $q.= "AND Passwd = " . $dbh->quote(salted_hash($passwd, $salt)); + $result = $dbh->query($q); + if (!$result) { + return false; } + + $row = $result->fetch(PDO::FETCH_NUM); + return ($row[0] > 0); + } else { + /* Check password without using salt. */ + $q = "SELECT ID FROM Users "; + $q.= "WHERE ID = " . $userID . " "; + $q.= "AND Passwd = " . $dbh->quote(md5($passwd)); + $result = $dbh->query($q); + if (!$result) { + return false; + } + + $row = $result->fetch(PDO::FETCH_NUM); + if (!$row[0]) { + return false; + } + + /* Password correct, but salt it first! */ + if (!save_salt($userID, $passwd)) { + trigger_error("Unable to salt user's password;" . + " ID " . $userID, E_USER_WARNING); + return false; + } + + return true; } - return false; } /** -- 2.0.0
Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de> --- web/lib/acctfuncs.inc.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/web/lib/acctfuncs.inc.php b/web/lib/acctfuncs.inc.php index f1776c0..06d4311 100644 --- a/web/lib/acctfuncs.inc.php +++ b/web/lib/acctfuncs.inc.php @@ -119,14 +119,15 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", $error = __("Missing User ID"); } - if (!$error && !valid_username($U) && !user_is_privileged($editor_user)) - $error = __("The username is invalid.") . "<ul>\n" + if (!$error && !valid_username($U) && !user_is_privileged($editor_user)) { + $error = __("The username is invalid.") . "<ul>\n" ."<li>" . __("It must be between %s and %s characters long", USERNAME_MIN_LEN, USERNAME_MAX_LEN ) . "</li>" . "<li>" . __("Start and end with a letter or number") . "</li>" . "<li>" . __("Can contain only one period, underscore or hyphen.") . "</li>\n</ul>"; + } if (!$error && $P && $C && ($P != $C)) { $error = __("Password fields do not match."); -- 2.0.0
participants (1)
-
Lukas Fleischer