diff options
Diffstat (limited to 'web/lib')
-rw-r--r-- | web/lib/acctfuncs.inc.php | 661 | ||||
-rw-r--r-- | web/lib/aur.inc.php | 152 | ||||
-rw-r--r-- | web/lib/aurjson.class.php | 30 | ||||
-rw-r--r-- | web/lib/config.inc.php.proto | 32 | ||||
-rw-r--r-- | web/lib/pkgfuncs.inc.php | 310 | ||||
-rw-r--r-- | web/lib/stats.inc.php | 18 | ||||
-rw-r--r-- | web/lib/translator.inc.php | 101 |
7 files changed, 799 insertions, 505 deletions
diff --git a/web/lib/acctfuncs.inc.php b/web/lib/acctfuncs.inc.php index b7c3ee8..a41a4e7 100644 --- a/web/lib/acctfuncs.inc.php +++ b/web/lib/acctfuncs.inc.php @@ -8,10 +8,28 @@ function in_request($name) { return ""; } +# Format PGP key fingerprint +function html_format_pgp_fingerprint($fingerprint) { + if (strlen($fingerprint) != 40 || !ctype_xdigit($fingerprint)) { + return $fingerprint; + } + + return htmlspecialchars(substr($fingerprint, 0, 4) . " " . + substr($fingerprint, 4, 4) . " " . + substr($fingerprint, 8, 4) . " " . + substr($fingerprint, 12, 4) . " " . + substr($fingerprint, 16, 4) . " " . + substr($fingerprint, 20, 4) . " " . + substr($fingerprint, 24, 4) . " " . + substr($fingerprint, 28, 4) . " " . + substr($fingerprint, 32, 4) . " " . + substr($fingerprint, 36, 4) . " ", ENT_QUOTES); +} + # Display the standard Account form, pass in default values if any function display_account_form($UTYPE,$A,$U="",$T="",$S="", - $E="",$P="",$C="",$R="",$L="",$I="",$UID=0) { + $E="",$P="",$C="",$R="",$L="",$I="",$K="",$UID=0) { # UTYPE: what user type the form is being displayed for # A: what "form" name to use # U: value to display for username @@ -28,124 +46,7 @@ function display_account_form($UTYPE,$A,$U="",$T="",$S="", global $SUPPORTED_LANGS; - print "<form action='account.php' method='post'>\n"; - print "<fieldset>"; - print "<input type='hidden' name='Action' value='".$A."' />\n"; - if ($UID) { - print "<input type='hidden' name='ID' value='".$UID."' />\n"; - print "<input type='hidden' name='token' value='".htmlspecialchars($_COOKIE['AURSID'])."' />\n"; - } - print "</fieldset>"; - print "<table border='0' cellpadding='0' cellspacing='0' width='80%' style=\"margin:0 auto;\">\n"; - print "<tr><td colspan='2'> </td></tr>\n"; - - print "<tr>"; - print "<td align='left'>".__("Username").":</td>"; - print "<td align='left'><input type='text' size='30' maxlength='64'"; - print " name='U' value='".htmlspecialchars($U,ENT_QUOTES)."' /> (".__("required").")</td>"; - print "</tr>\n"; - - # Only TUs or Devs can promote/demote/suspend a user - if ($UTYPE == "Trusted User" || $UTYPE == "Developer") { - print "<tr>"; - print "<td align='left'>".__("Account Type").":</td>"; - print "<td align='left'><select name=T>\n"; - print "<option value='1'"; - $T == "User" ? print " selected>" : print ">"; - print __("Normal user")."\n"; - print "<option value='2'"; - $T == "Trusted User" ? print " selected>" : print ">"; - print __("Trusted user")."\n"; - - # Only developers can make another account a developer - if ($UTYPE == "Developer") { - print "<option value='3'"; - $T == "Developer" ? print " selected>" : print ">"; - print __("Developer")."\n"; - } - print "</select></td>"; - print "</tr>\n"; - - print "<tr>"; - print "<td align='left'>".__("Account Suspended").":</td>"; - print "<td align='left'><input type='checkbox' name='S'"; - if ($S) { - print " checked=\"checked\" />"; - } else { - print " />"; - } - print "</tr>\n"; - } - - print "<tr>"; - print "<td align='left'>".__("Email Address").":</td>"; - print "<td align='left'><input type='text' size='30' maxlength='64'"; - print " name='E' value='".htmlspecialchars($E,ENT_QUOTES)."' /> (".__("required").")</td>"; - print "</tr>\n"; - - print "<tr>"; - print "<td align='left'>".__("Password").":</td>"; - print "<td align='left'><input type='password' size='30' maxlength='32'"; - print " name='P' value='".$P."' />"; - if ($A != "UpdateAccount") { - print " (".__("required").")"; - } - print "</td></tr>\n"; - - print "<tr>"; - print "<td align='left'>".__("Re-type password").":</td>"; - print "<td align='left'><input type='password' size='30' maxlength='32'"; - print " name='C' value='".$C."' />"; - if ($A != "UpdateAccount") { - print " (".__("required").")"; - } - print "</td></tr>\n"; - - print "<tr>"; - print "<td align='left'>".__("Real Name").":</td>"; - print "<td align='left'><input type='text' size='30' maxlength='32'"; - print " name='R' value='".htmlspecialchars($R,ENT_QUOTES)."' /></td>"; - print "</tr>\n"; - - print "<tr>"; - print "<td align='left'>".__("IRC Nick").":</td>"; - print "<td align='left'><input type='text' size='30' maxlength='32'"; - print " name='I' value='".htmlspecialchars($I,ENT_QUOTES)."' /></td>"; - print "</tr>\n"; - - print "<tr>"; - print "<td align='left'>".__("Language").":</td>"; - print "<td align='left'><select name=L>\n"; - - reset($SUPPORTED_LANGS); - while (list($code, $lang) = each($SUPPORTED_LANGS)) { - if ($L == $code) { - print "<option value=".$code." selected> ".$lang."\n"; - } else { - print "<option value=".$code."> ".$lang."\n"; - } - } - print "</select></td>"; - print "</tr>\n"; - - print "<tr><td colspan='2'> </td></tr>\n"; - print "<tr>"; - print "<td> </td>"; - print "<td align='left'>"; - - if ($A == "UpdateAccount") { - print "<input type='submit' class='button'"; - print " value='".__("Update")."' /> "; - } else { - print "<input type='submit' class='button'"; - print " value='".__("Create")."' /> "; - } - print "<input type='reset' class='button' value='".__("Reset")."' />"; - print "</td>"; - print "</tr>\n"; - - print "</table>\n"; - print "</form>\n"; + include("account_edit_form.php"); return; } # function display_account_form() @@ -153,7 +54,7 @@ function display_account_form($UTYPE,$A,$U="",$T="",$S="", # process form input from a new/edit account form # function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", - $P="",$C="",$R="",$L="",$I="",$UID=0) { + $P="",$C="",$R="",$L="",$I="",$K="",$UID=0,$dbh=NULL) { # UTYPE: The user's account type # TYPE: either "edit" or "new" # A: what parent "form" name to use @@ -172,14 +73,17 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", # error check and process request for a new/modified account global $SUPPORTED_LANGS; + if (!$dbh) { + $dbh = db_connect(); + } + if(isset($_COOKIE['AURSID'])) { - $editor_user = uid_from_sid($_COOKIE['AURSID']); + $editor_user = uid_from_sid($_COOKIE['AURSID'], $dbh); } else { $editor_user = null; } - $dbh = db_connect(); $error = ""; if (empty($E) || empty($U)) { $error = __("Missing a required field."); @@ -197,7 +101,7 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", } } - if (!$error && !valid_username($U) && !user_is_privileged($editor_user)) + if (!$error && !valid_username($U) && !user_is_privileged($editor_user, $dbh)) $error = __("The username is invalid.") . "<ul>\n" ."<li>" . __("It must be between %s and %s characters long", USERNAME_MIN_LEN, USERNAME_MAX_LEN ) @@ -215,6 +119,11 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", if (!$error && !valid_email($E)) { $error = __("The email address is invalid."); } + + if (!$error && $K != '' && !valid_pgp_fingerprint($K)) { + $error = __("The PGP key fingerprint is invalid."); + } + if ($UTYPE == "Trusted User" && $T == 3) { $error = __("A Trusted User cannot assign Developer status."); } @@ -234,7 +143,7 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", if ($result) { $row = mysql_fetch_array($result); if ($row[0]) { - $error = __("The username, %h%s%h, is already in use.", + $error = __("The username, %s%s%s, is already in use.", "<b>", htmlspecialchars($U,ENT_QUOTES), "</b>"); } } @@ -252,7 +161,7 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", if ($result) { $row = mysql_fetch_array($result); if ($row[0]) { - $error = __("The address, %h%s%h, is already in use.", + $error = __("The address, %s%s%s, is already in use.", "<b>", htmlspecialchars($E,ENT_QUOTES), "</b>"); } } @@ -260,26 +169,26 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", if ($error) { print "<span class='error'>".$error."</span><br/>\n"; display_account_form($UTYPE, $A, $U, $T, $S, $E, "", "", - $R, $L, $I, $UID); + $R, $L, $I, $K, $UID); } else { if ($TYPE == "new") { # no errors, go ahead and create the unprivileged user $salt = generate_salt(); $P = salted_hash($P, $salt); $escaped = array_map('db_escape_string', - array($U, $E, $P, $salt, $R, $L, $I)); + array($U, $E, $P, $salt, $R, $L, $I, str_replace(" ", "", $K))); $q = "INSERT INTO Users (" . "AccountTypeID, Suspended, Username, Email, Passwd, Salt" . - ", RealName, LangPreference, IRCNick) " . + ", RealName, LangPreference, IRCNick, PGPKey) " . "VALUES (1, 0, '" . implode("', '", $escaped) . "')"; $result = db_query($q, $dbh); if (!$result) { - print __("Error trying to create account, %h%s%h: %s.", + print __("Error trying to create account, %s%s%s: %s.", "<b>", htmlspecialchars($U,ENT_QUOTES), "</b>", mysql_error($dbh)); } else { # account created/modified, tell them so. # - print __("The account, %h%s%h, has been successfully created.", + print __("The account, %s%s%s, has been successfully created.", "<b>", htmlspecialchars($U,ENT_QUOTES), "</b>"); print "<p>\n"; print __("Click on the Home link above to login."); @@ -308,13 +217,14 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", $q.= ", RealName = '".db_escape_string($R)."'"; $q.= ", LangPreference = '".db_escape_string($L)."'"; $q.= ", IRCNick = '".db_escape_string($I)."'"; + $q.= ", PGPKey = '".db_escape_string(str_replace(" ", "", $K))."'"; $q.= " WHERE ID = ".intval($UID); $result = db_query($q, $dbh); if (!$result) { - print __("Error trying to modify account, %h%s%h: %s.", + print __("Error trying to modify account, %s%s%s: %s.", "<b>", htmlspecialchars($U,ENT_QUOTES), "</b>", mysql_error($dbh)); } else { - print __("The account, %h%s%h, has been successfully modified.", + print __("The account, %s%s%s, has been successfully modified.", "<b>", htmlspecialchars($U,ENT_QUOTES), "</b>"); } } @@ -333,7 +243,7 @@ function search_accounts_form() { # search results page # function search_results_page($UTYPE,$O=0,$SB="",$U="",$T="", - $S="",$E="",$R="",$I="") { + $S="",$E="",$R="",$I="",$K="",$dbh=NULL) { # UTYPE: what account type the user belongs to # O: what row offset we're at # SB: how to sort the results @@ -388,6 +298,10 @@ function search_results_page($UTYPE,$O=0,$SB="",$U="",$T="", $q.= "AND IRCNick LIKE '%".db_escape_like($I)."%' "; $search_vars[] = "I"; } + if ($K) { + $q.= "AND PGPKey LIKE '%".db_escape_like(str_replace(" ", "", $K))."%' "; + $search_vars[] = "K"; + } switch ($SB) { case 't': $q.= "ORDER BY AccountTypeID, Username "; @@ -408,182 +322,18 @@ function search_results_page($UTYPE,$O=0,$SB="",$U="",$T="", $search_vars[] = "SB"; $q.= "LIMIT " . $HITS_PER_PAGE . " OFFSET " . $OFFSET; - $dbh = db_connect(); - - $result = db_query($q, $dbh); - if (!$result) { - print __("No results matched your search criteria."); - } else { - $num_rows = mysql_num_rows($result); - if ($num_rows) { - print "<table border='0' cellpadding='0'"; - print " cellspacing='0' width='90%'"; - print " style=\"margin:0 auto\">\n"; - print "<tr>"; - print "<td colspan='2'>"; - print "<table border='0' cellpadding='0'"; - print " cellspacing='0' width='100%'>\n"; - print "<tr>"; - print "<th class='header'>"; - print "<span class='f2'>".__("Username")."</span></th>"; - print "<th class='header'>"; - print "<span class='f2'>".__("Type")."</span></th>"; - print "<th class='header'>"; - print "<span class='f2'>".__("Status")."</span></th>"; - print "<th class='header'>"; - print "<span class='f2'>".__("Real Name")."</span></th>"; - print "<th class='header'>"; - print "<span class='f2'>".__("IRC Nick")."</span></th>"; - print "<th class='header'>"; - print "<span class='f2'>".__("Last Voted")."</span></th>"; - print "<th class='header'>"; - print "<span class='f2'>".__("Edit Account")."</span></th>"; - print "</tr>\n"; - $i = 0; - while ($row = mysql_fetch_assoc($result)) { - if ($i % 2) { - $c = "data1"; - } else { - $c = "data2"; - } - print "<tr>"; - print "<td class='".$c."'>"; - print "<span class='f5'><a href='packages.php?SeB=m&K=".$row["Username"]."'>".$row["Username"]."</a></span></td>"; - print "<td class='".$c."'>"; - print "<span class='f5'>".$row["AccountType"]; - print "</span></td>"; - print "<td class='".$c."'><span class='f5'>"; - if ($row["Suspended"]) { - print __("Suspended"); - } else { - print __("Active"); - } - print "</span></td>"; - print "<td class='".$c."'><span class='f5'>"; - $row["RealName"] ? print htmlspecialchars($row["RealName"],ENT_QUOTES) : print " "; - print "</span></td>"; - print "<td class='".$c."'><span class='f5'>"; - $row["IRCNick"] ? print htmlspecialchars($row["IRCNick"],ENT_QUOTES) : print " "; - print "</span></td>"; - print "<td class='".$c."'><span class='f5'>"; - $row["LastVoted"] - ? print date("Ymd", $row["LastVoted"]) - : print __("Never"); - print "</span></td>"; - print "<td class='".$c."'><span class='f5'>"; - if ($UTYPE == "Trusted User" && $row["AccountType"] == "Developer") { - # TUs can't edit devs - # - print " </span></td>"; - } else { - $edit_url = "account.php?Action=DisplayAccount&ID=".$row["ID"]; - print "<a href='".$edit_url . "'>"; - print "Edit</a></span></td>"; - } - print "</tr>\n"; - $i++; - } - print "</table>\n"; - print "</td></tr>\n"; - - print "<tr>"; - print "<td align='left'>"; - print "<form action='account.php' method='post'>\n"; - print "<fieldset>"; - print "<input type='hidden' name='Action' value='SearchAccounts' />\n"; - print "<input type='hidden' name='O'"; - print " value='".($OFFSET-$HITS_PER_PAGE)."' />\n"; - reset($search_vars); - while (list($k, $ind) = each($search_vars)) { - print "<input type='hidden' name='".$ind."'"; - print " value='".${$ind}."' />\n"; - } - print "<input type='submit' class='button'"; - print " value='<-- ".__("Less")."' />"; - print "</fieldset>"; - print "</form>\n"; - print "</td>"; - print "<td align='right'>"; - print "<form action='account.php' method='post'>\n"; - print "<fieldset>"; - print "<input type='hidden' name='Action' value='SearchAccounts' />\n"; - print "<input type='hidden' name='O'"; - print " value='".($OFFSET+$HITS_PER_PAGE)."' />\n"; - reset($search_vars); - while (list($k, $ind) = each($search_vars)) { - print "<input type='hidden' name='".$ind."'"; - print " value='".${$ind}."' />\n"; - } - print "<input type='submit' class='button'"; - print " value='".__("More")." -->' />"; - print "</fieldset>"; - print "</form>\n"; - print "</td>"; - print "</tr>\n"; - print "</table>\n"; - } else { - print "<p style=\"text-align:center;\">\n"; - print __("No more results to display."); - print "</p>\n"; - } + if (!$dbh) { + $dbh = db_connect(); } - return; -} -# Display non-editable account info -# -function display_account_info($U="", $T="", $E="", $R="", $I="") { - # U: value to display for username - # T: value to display for account type - # E: value to display for email address - # R: value to display for RealName - # I: value to display for IRC nick + $result = db_query($q, $dbh); + $num_rows = mysql_num_rows($result); - global $SUPPORTED_LANGS; + while ($row = mysql_fetch_assoc($result)) { + $userinfo[] = $row; + } - print "<table border='0' cellpadding='0' cellspacing='0' width='33%' style=\"margin:0 auto;\">\n"; - print " <tr>\n"; - print " <td colspan='2'> </td>\n"; - print " </tr>\n"; - - print " <tr>\n"; - print " <td align='left'>".__("Username").":</td>\n"; - print " <td align='left'>".$U."</td>\n"; - print " </tr>\n"; - - print " <tr>\n"; - print " <td align='left'>".__("Account Type").":</td>\n"; - print " <td align='left'>"; - if ($T == "User") { - print __("User"); - } elseif ($T == "Trusted User") { - print __("Trusted User"); - } elseif ($T == "Developer") { - print __("Developer"); - } - print " </td>\n"; - print " </tr>\n"; - - print " <tr>\n"; - print " <td align='left'>".__("Email Address").":</td>\n"; - print " <td align='left'><a href='mailto:".htmlspecialchars($E,ENT_QUOTES)."'>".htmlspecialchars($E,ENT_QUOTES)."</a></td>\n"; - print " </tr>\n"; - - print " <tr>\n"; - print " <td align='left'>".__("Real Name").":</td>\n"; - print " <td align='left'>".htmlspecialchars($R,ENT_QUOTES)."</td>\n"; - print " </tr>\n"; - - print " <tr>\n"; - print " <td align='left'>".__("IRC Nick").":</td>\n"; - print " <td align='left'>".htmlspecialchars($I,ENT_QUOTES)."</td>\n"; - print " </tr>\n"; - - print " <tr>\n"; - print " <td colspan='2'><a href='packages.php?K=".$U."&SeB=m'>".__("View this user's packages")."</a></td>\n"; - print " </tr>\n"; - - print "</table>\n"; + include("account_search_results.php"); return; } @@ -591,7 +341,7 @@ function display_account_info($U="", $T="", $E="", $R="", $I="") { * Returns SID (Session ID) and error (error message) in an array * SID of 0 means login failed. */ -function try_login() { +function try_login($dbh=NULL) { global $MAX_SESSIONS_PER_USER, $PERSISTENT_COOKIE_TIMEOUT; $login_error = ""; @@ -599,21 +349,22 @@ function try_login() { $userID = null; if ( isset($_REQUEST['user']) || isset($_REQUEST['passwd']) ) { + if (!$dbh) { + $dbh = db_connect(); + } + $userID = valid_user($_REQUEST['user'], $dbh); - $userID = valid_user($_REQUEST['user']); - - if ( user_suspended( $userID ) ) { + if ( user_suspended($userID, $dbh) ) { $login_error = "Account Suspended."; } elseif ( $userID && isset($_REQUEST['passwd']) - && valid_passwd($userID, $_REQUEST['passwd']) ) { + && valid_passwd($userID, $_REQUEST['passwd'], $dbh) ) { $logged_in = 0; $num_tries = 0; # Account looks good. Generate a SID and store it. - $dbh = db_connect(); while (!$logged_in && $num_tries < 5) { if ($MAX_SESSIONS_PER_USER) { # Delete all user sessions except the @@ -644,8 +395,11 @@ function try_login() { } if ($logged_in) { - # set our SID cookie + $q = "UPDATE Users SET LastLogin = UNIX_TIMESTAMP() "; + $q.= "WHERE ID = '$userID'"; + db_query($q, $dbh); + # set our SID cookie if (isset($_POST['remember_me']) && $_POST['remember_me'] == "on") { # Set cookies for 30 days. @@ -684,8 +438,7 @@ function try_login() { * Returns the username if it is valid * Returns nothing if it isn't valid */ -function valid_username( $user ) -{ +function valid_username($user) { if (!empty($user)) { #Is username at not too short or too long? @@ -711,11 +464,14 @@ function valid_username( $user ) * Checks if the username is valid and if it exists in the database * Returns the username ID or nothing */ -function valid_user( $user ) -{ +function valid_user($user, $dbh=NULL) { /* if ( $user = valid_username($user) ) { */ - if ( $user ) { + + if(!$dbh) { $dbh = db_connect(); + } + + if ( $user ) { $q = "SELECT ID FROM Users WHERE Username = '" . db_escape_string($user). "'"; @@ -729,8 +485,72 @@ function valid_user( $user ) return; } -function good_passwd( $passwd ) -{ +# Check for any open proposals about a user. Used to prevent multiple proposals. +function open_user_proposals($user, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = "SELECT * FROM TU_VoteInfo WHERE User = '" . db_escape_string($user) . "'"; + $q.= " AND End > UNIX_TIMESTAMP()"; + $result = db_query($q, $dbh); + if (mysql_num_rows($result)) { + return true; + } + else { + return false; + } +} + +# Creates a new trusted user proposal from entered agenda. +# Optionally takes proposal about specific user. Length of vote set by submitter. +function add_tu_proposal($agenda, $user, $votelength, $submitteruid, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = "INSERT INTO TU_VoteInfo (Agenda, User, Submitted, End, SubmitterID) VALUES "; + $q.= "('" . db_escape_string($agenda) . "', "; + $q.= "'" . db_escape_string($user) . "', "; + $q.= "UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + " . db_escape_string($votelength); + $q.= ", " . $submitteruid . ")"; + db_query($q, $dbh); + +} + +# Add a reset key for a specific user +function create_resetkey($resetkey, $uid, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = "UPDATE Users "; + $q.= "SET ResetKey = '" . $resetkey . "' "; + $q.= "WHERE ID = " . $uid; + db_query($q, $dbh); +} + +# Change a password and save the salt only if reset key and email are correct +function password_reset($hash, $salt, $resetkey, $email, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = "UPDATE Users "; + $q.= "SET Passwd = '$hash', "; + $q.= "Salt = '$salt', "; + $q.= "ResetKey = '' "; + $q.= "WHERE ResetKey != '' "; + $q.= "AND ResetKey = '".db_escape_string($resetkey)."' "; + $q.= "AND Email = '".db_escape_string($email)."'"; + $result = db_query($q, $dbh); + + if (!mysql_affected_rows($dbh)) { + $error = __('Invalid e-mail and reset key combination.'); + return $error; + } else { + header('Location: passreset.php?step=complete'); + exit(); + } +} + +function good_passwd($passwd) { if ( strlen($passwd) >= PASSWD_MIN_LEN ) { return true; } @@ -740,11 +560,11 @@ function good_passwd( $passwd ) /* Verifies that the password is correct for the userID specified. * Returns true or false */ -function valid_passwd( $userID, $passwd ) -{ - if ( strlen($passwd) > 0 ) { +function valid_passwd($userID, $passwd, $dbh=NULL) { + if (!$dbh) { $dbh = db_connect(); - + } + if ( strlen($passwd) > 0 ) { # get salt for this user $salt = get_salt($userID); if ($salt) { @@ -783,14 +603,23 @@ function valid_passwd( $userID, $passwd ) } /* + * Checks if the PGP key fingerprint is valid (must be 40 hexadecimal digits). + */ +function valid_pgp_fingerprint($fingerprint) { + $fingerprint = str_replace(" ", "", $fingerprint); + return (strlen($fingerprint) == 40 && ctype_xdigit($fingerprint)); +} + +/* * Is the user account suspended? */ -function user_suspended( $id ) -{ +function user_suspended($id, $dbh=NULL) { + if (!$dbh) { + $dbh = db_connect(); + } if (!$id) { return false; } - $dbh = db_connect(); $q = "SELECT Suspended FROM Users WHERE ID = " . $id; $result = db_query($q, $dbh); if ($result) { @@ -805,9 +634,10 @@ function user_suspended( $id ) /* * This should be expanded to return something */ -function user_delete( $id ) -{ - $dbh = db_connect(); +function user_delete($id, $dbh=NULL) { + if (!$dbh) { + $dbh = db_connect(); + } $q = "DELETE FROM Users WHERE ID = " . $id; db_query($q, $dbh); return; @@ -817,26 +647,38 @@ function user_delete( $id ) * A different way of determining a user's privileges * rather than account_from_sid() */ -function user_is_privileged( $id ) -{ - $dbh = db_connect(); +function user_is_privileged($id, $dbh=NULL) { + if (!$dbh) { + $dbh = db_connect(); + } $q = "SELECT AccountTypeID FROM Users WHERE ID = " . $id; $result = db_query($q, $dbh); if ($result) { $row = mysql_fetch_row($result); - if( $result[0] > 1) { - return $result[0]; + if($row[0] > 1) { + return $row[0]; } } return 0; } +# Remove session on logout +function delete_session_id($sid, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + + $q = "DELETE FROM Sessions WHERE SessionID = '"; + $q.= db_escape_string($sid) . "'"; + db_query($q, $dbh); +} + # Clear out old expired sessions. -function clear_expired_sessions($dbh = null) { +function clear_expired_sessions($dbh=NULL) { global $LOGIN_TIMEOUT; - if (empty($dbh)) { + if(!$dbh) { $dbh = db_connect(); } @@ -846,3 +688,150 @@ function clear_expired_sessions($dbh = null) { return; } +function account_details($uid, $username, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = "SELECT Users.*, AccountTypes.AccountType "; + $q.= "FROM Users, AccountTypes "; + $q.= "WHERE AccountTypes.ID = Users.AccountTypeID "; + if (!empty($uid)) { + $q.= "AND Users.ID = ".intval($uid); + } else { + $q.= "AND Users.Username = '".db_escape_string($username) . "'"; + } + $result = db_query($q, $dbh); + + if ($result) { + $row = mysql_fetch_assoc($result); + } + + return $row; +} + +function own_account_details($sid, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = "SELECT Users.*, AccountTypes.AccountType "; + $q.= "FROM Users, AccountTypes, Sessions "; + $q.= "WHERE AccountTypes.ID = Users.AccountTypeID "; + $q.= "AND Users.ID = Sessions.UsersID "; + $q.= "AND Sessions.SessionID = '"; + $q.= db_escape_string($sid)."'"; + $result = db_query($q, $dbh); + + if ($result) { + $row = mysql_fetch_assoc($result); + } + + return $row; +} + +function tu_voted($voteid, $uid, $dbh=NULL) { + if (!$dbh) { + $dbh = db_connect(); + } + + $q = "SELECT * FROM TU_Votes WHERE VoteID = " . intval($voteid) . " AND UserID = " . intval($uid); + $result = db_query($q, $dbh); + if (mysql_num_rows($result)) { + return true; + } + else { + return false; + } +} + +function current_proposal_list($order, $dbh=NULL) { + if (!$dbh) { + $dbh = db_connect(); + } + + $q = "SELECT * FROM TU_VoteInfo WHERE End > " . time() . " ORDER BY Submitted " . $order; + $result = db_query($q, $dbh); + + $details = array(); + while ($row = mysql_fetch_assoc($result)) { + $details[] = $row; + } + + return $details; +} + +function past_proposal_list($order, $lim, $dbh=NULL) { + if (!$dbh) { + $dbh = db_connect(); + } + + $q = "SELECT * FROM TU_VoteInfo WHERE End < " . time() . " ORDER BY Submitted " . $order . $lim; + $result = db_query($q, $dbh); + + $details = array(); + while ($row = mysql_fetch_assoc($result)) { + $details[] = $row; + } + + return $details; +} + +function proposal_count($dbh=NULL) { + if (!$dbh) { + $dbh = db_connect(); + } + + $q = "SELECT COUNT(*) FROM TU_VoteInfo"; + $result = db_query($q, $dbh); + $row = mysql_fetch_row($result); + + return $row[0]; +} + +function vote_details($voteid, $dbh=NULL) { + if (!$dbh) { + $dbh = db_connect(); + } + + $q = "SELECT * FROM TU_VoteInfo "; + $q.= "WHERE ID = " . intval($voteid); + + $result = db_query($q, $dbh); + $row = mysql_fetch_assoc($result); + + return $row; +} + +function voter_list($voteid, $dbh=NULL) { + if (!$dbh) { + $dbh = db_connect(); + } + + $whovoted = ''; + + $q = "SELECT tv.UserID,U.Username "; + $q.= "FROM TU_Votes tv, Users U "; + $q.= "WHERE tv.VoteID = " . intval($voteid); + $q.= " AND tv.UserID = U.ID "; + $q.= "ORDER BY Username"; + + $result = db_query($q, $dbh); + if ($result) { + while ($row = mysql_fetch_assoc($result)) { + $whovoted.= '<a href="account.php?Action=AccountInfo&ID='.$row['UserID'].'">'.$row['Username'].'</a> '; + } + } + return $whovoted; +} + +function cast_proposal_vote($voteid, $uid, $vote, $newtotal, $dbh=NULL) { + if (!$dbh) { + $dbh = db_connect(); + } + + $q = "UPDATE TU_VoteInfo SET " . $vote . " = " . ($newtotal) . " WHERE ID = " . $voteid; + db_query($q, $dbh); + + $q = "INSERT INTO TU_Votes (VoteID, UserID) VALUES (" . $voteid . ", " . $uid . ")"; + db_query($q, $dbh); + +} diff --git a/web/lib/aur.inc.php b/web/lib/aur.inc.php index 8b9f31e..1a6164e 100644 --- a/web/lib/aur.inc.php +++ b/web/lib/aur.inc.php @@ -52,9 +52,7 @@ function check_sid($dbh=NULL) { } elseif ($failed == 2) { # session id timeout was reached and they must login again. # - $q = "DELETE FROM Sessions WHERE SessionID = '"; - $q.= db_escape_string($_COOKIE["AURSID"]) . "'"; - db_query($q, $dbh); + delete_session_id($_COOKIE["AURSID"], $dbh); setcookie("AURSID", "", 1, "/", null, !empty($_SERVER['HTTPS']), true); unset($_COOKIE['AURSID']); @@ -90,14 +88,18 @@ function check_token() { # verify that an email address looks like it is legitimate # function valid_email($addy) { - return (filter_var($addy, FILTER_VALIDATE_EMAIL) !== false); -} + // check against RFC 3696 + if (filter_var($addy, FILTER_VALIDATE_EMAIL) === false) { + return false; + } -# a new seed value for mt_srand() -# -function make_seed() { - list($usec, $sec) = explode(' ', microtime()); - return (float) $sec + ((float) $usec * 10000); + // check dns for mx, a, aaaa records + list($local, $domain) = explode('@', $addy); + if (!(checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A') || checkdnsrr($domain, 'AAAA'))) { + return false; + } + + return true; } # generate a (hopefully) unique session id @@ -282,77 +284,11 @@ function db_query($query="", $db_handle="") { return $result; } -# set up the visitor's language -# -function set_lang($dbh=NULL) { - global $LANG; - global $SUPPORTED_LANGS; - global $PERSISTENT_COOKIE_TIMEOUT; - global $streamer, $l10n; - - $update_cookie = 0; - if (isset($_REQUEST['setlang'])) { - # visitor is requesting a language change - # - $LANG = $_REQUEST['setlang']; - $update_cookie = 1; - - } elseif (isset($_COOKIE['AURLANG'])) { - # If a cookie is set, use that - # - $LANG = $_COOKIE['AURLANG']; - - } elseif (isset($_COOKIE["AURSID"])) { - # No language but a session; use default lang preference - # - if(!$dbh) { - $dbh = db_connect(); - } - $q = "SELECT LangPreference FROM Users, Sessions "; - $q.= "WHERE Users.ID = Sessions.UsersID "; - $q.= "AND Sessions.SessionID = '"; - $q.= db_escape_string($_COOKIE["AURSID"])."'"; - $result = db_query($q, $dbh); - - if ($result) { - $row = mysql_fetch_array($result); - $LANG = $row[0]; - } - $update_cookie = 1; - } - - # Set $LANG to default if nothing is valid. - if (!array_key_exists($LANG, $SUPPORTED_LANGS)) { - $LANG = DEFAULT_LANG; - } - - if ($update_cookie) { - $cookie_time = time() + $PERSISTENT_COOKIE_TIMEOUT; - setcookie("AURLANG", $LANG, $cookie_time, "/"); - } - - $streamer = new FileReader('../locale/' . $LANG . - '/LC_MESSAGES/aur.mo'); - $l10n = new gettext_reader($streamer, true); - - return; -} - - # common header # function html_header($title="") { - global $_SERVER; - global $_COOKIE; - global $_POST; global $LANG; global $SUPPORTED_LANGS; - global $DISABLE_HTTP_LOGIN; - - if (!$DISABLE_HTTP_LOGIN || (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'])) { - $login = try_login(); - $login_error = $login['error']; - } $title = htmlspecialchars($title, ENT_QUOTES); @@ -441,8 +377,7 @@ function chmod_group($path) { # obtain the uid given a Users.Username # -function uid_from_username($username="", $dbh=NULL) -{ +function uid_from_username($username="", $dbh=NULL) { if (!$username) { return ""; } @@ -462,8 +397,7 @@ function uid_from_username($username="", $dbh=NULL) # obtain the uid given a Users.Email # -function uid_from_email($email="", $dbh=NULL) -{ +function uid_from_email($email="", $dbh=NULL) { if (!$email) { return ""; } @@ -483,8 +417,7 @@ function uid_from_email($email="", $dbh=NULL) # check user privileges # -function check_user_privileges() -{ +function check_user_privileges() { $type = account_from_sid($_COOKIE['AURSID']); return ($type == 'Trusted User' || $type == 'Developer'); } @@ -523,8 +456,7 @@ function mkurl($append) { return substr($out, 5); } -function get_salt($user_id, $dbh=NULL) -{ +function get_salt($user_id, $dbh=NULL) { if(!$dbh) { $dbh = db_connect(); } @@ -537,8 +469,7 @@ function get_salt($user_id, $dbh=NULL) return; } -function save_salt($user_id, $passwd, $dbh=NULL) -{ +function save_salt($user_id, $passwd, $dbh=NULL) { if(!$dbh) { $dbh = db_connect(); } @@ -549,21 +480,18 @@ function save_salt($user_id, $passwd, $dbh=NULL) return db_query($salting_q, $dbh); } -function generate_salt() -{ +function generate_salt() { return md5(uniqid(mt_rand(), true)); } -function salted_hash($passwd, $salt) -{ +function salted_hash($passwd, $salt) { if (strlen($salt) != 32) { trigger_error('Salt does not look like an md5 hash', E_USER_WARNING); } return md5($salt . $passwd); } -function parse_comment($comment) -{ +function parse_comment($comment) { $url_pattern = '/(\b(?:https?|ftp):\/\/[\w\/\#~:.?+=&%@!\-;,]+?' . '(?=[.:?\-;,]*(?:[^\w\/\#~:.?+=&%@!\-;,]|$)))/iS'; @@ -585,3 +513,43 @@ function parse_comment($comment) return $html; } + +function begin_atomic_commit($dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + db_query("BEGIN", $dbh); +} + +function end_atomic_commit($dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + db_query("COMMIT", $dbh); +} + +function last_insert_id($dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + return mysql_insert_id($dbh); +} + +function latest_pkgs($numpkgs, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + + $q = "SELECT * FROM Packages "; + $q.= "ORDER BY SubmittedTS DESC "; + $q.= "LIMIT " .intval($numpkgs); + $result = db_query($q, $dbh); + + if ($result) { + while ($row = mysql_fetch_assoc($result)) { + $packages[] = $row; + } + } + + return $packages; +} diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index 6c7725c..c1b079a 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -18,11 +18,14 @@ class AurJSON { 'search', 'info', 'multiinfo', 'msearch' ); private static $fields = array( - 'Packages.ID', 'Name', 'Version', 'CategoryID', - 'Description', 'URL', 'License', - 'NumVotes', '(OutOfDateTS IS NOT NULL) AS OutOfDate', + 'Packages.ID', 'Name', 'Version', 'CategoryID', 'Description', 'URL', + 'License', 'NumVotes', 'OutOfDateTS AS OutOfDate', 'SubmittedTS AS FirstSubmitted', 'ModifiedTS AS LastModified' ); + private static $numeric_fields = array( + 'ID', 'CategoryID', 'NumVotes', 'OutOfDate', 'FirstSubmitted', + 'LastModified' + ); /** * Handles post data, and routes the request. @@ -100,7 +103,7 @@ class AurJSON { private function json_error($msg) { // set content type header to app/json header('content-type: application/json'); - return $this->json_results('error', $msg); + return $this->json_results('error', 0, $msg); } /** @@ -109,8 +112,8 @@ class AurJSON { * @param $data The result data to return * @return mixed A json formatted result response. **/ - private function json_results($type, $data) { - return json_encode( array('type' => $type, 'results' => $data) ); + private function json_results($type, $count, $data) { + return json_encode( array('type' => $type, 'resultcount' => $count, 'results' => $data) ); } private function process_query($type, $where_condition) { @@ -121,12 +124,21 @@ class AurJSON { "WHERE ${where_condition}"; $result = db_query($query, $this->dbh); - if ( $result && (mysql_num_rows($result) > 0) ) { + $resultcount = mysql_num_rows($result); + if ( $result && $resultcount > 0 ) { $search_data = array(); while ( $row = mysql_fetch_assoc($result) ) { $name = $row['Name']; $row['URLPath'] = URL_DIR . substr($name, 0, 2) . "/" . $name . "/" . $name . ".tar.gz"; + /* Unfortunately, mysql_fetch_assoc() returns all fields as + * strings. We need to coerce numeric values into integers to + * provide proper data types in the JSON response. + */ + foreach (self::$numeric_fields as $field) { + $row[$field] = intval($row[$field]); + } + if ($type == 'info') { $search_data = $row; break; @@ -137,10 +149,10 @@ class AurJSON { } mysql_free_result($result); - return $this->json_results($type, $search_data); + return $this->json_results($type, $resultcount, $search_data); } else { - return $this->json_error('No results found'); + return $this->json_results($type, 0, array()); } } diff --git a/web/lib/config.inc.php.proto b/web/lib/config.inc.php.proto index 79f5e4c..1e2699e 100644 --- a/web/lib/config.inc.php.proto +++ b/web/lib/config.inc.php.proto @@ -8,7 +8,6 @@ define( "AUR_db_user", "aur" ); define( "AUR_db_pass", "aur" ); # Configuration of directories where things live -define( "UPLOAD_DIR", "/srv/aur/unsupported-temp/" ); define( "INCOMING_DIR", "/srv/aur/unsupported/" ); define( "URL_DIR", "/packages/" ); @@ -33,34 +32,6 @@ define("SQL_DEBUG", 0); # to '127.0.0.1:11211'. #define("MEMCACHE_SERVERS", '127.0.0.1:11211'); -# Languages we have translations for -$SUPPORTED_LANGS = array( - "ca" => "Català", - "cs" => "česky", - "da" => "Dansk", - "de" => "Deutsch", - "en" => "English", - "el" => "Ελληνικά", - "es" => "Español", - "fi" => "Finnish", - "fr" => "Français", - "he" => "עברית", - "hr" => "Hrvatski", - "hu" => "Magyar", - "it" => "Italiano", - "nb" => "Norsk", - "nl" => "Dutch", - "pl" => "Polski", - "pt_BR" => "Português (Brasil)", - "pt_PT" => "Português (Portugal)", - "ro" => "Română", - "ru" => "Русский", - "sr" => "Srpski", - "tr" => "Türkçe", - "uk" => "Українська", - "zh_CN" => "简体中文" -); - # Session limit per user $MAX_SESSIONS_PER_USER = 8; @@ -77,3 +48,6 @@ $MAX_FILESIZE_UNCOMPRESSED = 1024 * 1024 * 8; # Allow HTTPs logins only $DISABLE_HTTP_LOGIN = true; + +# Web URL used in email links and absolute redirects, no trailing slash +$AUR_LOCATION = "http://localhost"; diff --git a/web/lib/pkgfuncs.inc.php b/web/lib/pkgfuncs.inc.php index 88b18b8..0009b93 100644 --- a/web/lib/pkgfuncs.inc.php +++ b/web/lib/pkgfuncs.inc.php @@ -200,6 +200,56 @@ function package_comments($pkgid, $dbh=NULL) { return $comments; } +# Add a comment to a package page and send out appropriate notifications +# TODO: Move notification logic to separate function where it belongs +function add_package_comment($pkgid, $uid, $comment, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + + $q = 'INSERT INTO PackageComments '; + $q.= '(PackageID, UsersID, Comments, CommentTS) VALUES ('; + $q.= intval($pkgid) . ', ' . $uid . ', '; + $q.= "'" . db_escape_string($comment) . "', "; + $q.= 'UNIX_TIMESTAMP())'; + db_query($q, $dbh); + + # Send email notifications + $q = 'SELECT CommentNotify.*, Users.Email '; + $q.= 'FROM CommentNotify, Users '; + $q.= 'WHERE Users.ID = CommentNotify.UserID '; + $q.= 'AND CommentNotify.UserID != ' . $uid . ' '; + $q.= 'AND CommentNotify.PkgID = ' . intval($pkgid); + $result = db_query($q, $dbh); + $bcc = array(); + + if (mysql_num_rows($result)) { + while ($row = mysql_fetch_assoc($result)) { + array_push($bcc, $row['Email']); + } + + $q = 'SELECT Packages.* '; + $q.= 'FROM Packages '; + $q.= 'WHERE Packages.ID = ' . intval($pkgid); + $result = db_query($q, $dbh); + $row = mysql_fetch_assoc($result); + + # TODO: native language emails for users, based on their prefs + # Simply making these strings translatable won't work, users would be + # getting emails in the language that the user who posted the comment was in + $body = + 'from ' . $AUR_LOCATION . '/packages.php?ID=' + . $pkgid . "\n" + . username_from_sid($_COOKIE['AURSID'], $dbh) . " wrote:\n\n" + . $comment + . "\n\n---\nIf you no longer wish to receive notifications about this package, please go the the above package page and click the UnNotify button."; + $body = wordwrap($body, 70); + $bcc = implode(', ', $bcc); + $headers = "Bcc: $bcc\nReply-to: nobody@archlinux.org\nFrom: aur-notify@archlinux.org\nX-Mailer: AUR\n"; + @mail('undisclosed-recipients: ;', "AUR Comment for " . $row['Name'], $body, $headers); + } +} + # grab package sources # function package_sources($pkgid, $dbh=NULL) { @@ -269,20 +319,37 @@ function pkgnotify_from_sid($sid="", $dbh=NULL) { # get name of package based on pkgid # -function pkgname_from_id($pkgid, $dbh=NULL) { - $pkgid = intval($pkgid); - $name = ""; - if ($pkgid > 0) { +function pkgname_from_id($pkgids, $dbh=NULL) { + if (is_array($pkgids)) { + $pkgids = sanitize_ids($pkgids); + $names = array(); + if(!$dbh) { + $dbh = db_connect(); + } + $q = "SELECT Name FROM Packages WHERE ID IN (" . + implode(",", $pkgids) . ")"; + $result = db_query($q, $dbh); + if (mysql_num_rows($result) > 0) { + while ($row = mysql_fetch_assoc($result)) { + $names[] = $row['Name']; + } + } + return $names; + } + elseif ($pkgids > 0) { if(!$dbh) { $dbh = db_connect(); } - $q = "SELECT Name FROM Packages WHERE ID = " . $pkgid; + $q = "SELECT Name FROM Packages WHERE ID = " . $pkgids; $result = db_query($q, $dbh); if (mysql_num_rows($result) > 0) { $name = mysql_result($result, 0); } + return $name; + } + else { + return NULL; } - return $name; } # Check if a package name is blacklisted. @@ -301,6 +368,8 @@ function pkgname_is_blacklisted($name, $dbh=NULL) { # display package details # function package_details($id=0, $SID="", $dbh=NULL) { + global $AUR_LOCATION; + if(!$dbh) { $dbh = db_connect(); } @@ -312,12 +381,12 @@ function package_details($id=0, $SID="", $dbh=NULL) { $results = db_query($q, $dbh); if (!$results) { - print __("Error retrieving package details.") . "<br />\n"; + print "<p>" . __("Error retrieving package details.") . "</p>\n"; } else { $row = mysql_fetch_assoc($results); if (empty($row)) { - print __("Package details could not be found.") . "<br />\n"; + print "<p>" . __("Package details could not be found.") . "</p>\n"; } else { @@ -326,6 +395,10 @@ function package_details($id=0, $SID="", $dbh=NULL) { # Actions Bar if ($SID) { include('actions_form.php'); + if (isset($_REQUEST['comment']) && check_token()) { + $uid = uid_from_sid($SID, $dbh); + add_package_comment($id, $uid, $_REQUEST['comment'], $dbh); + } include('pkg_comment_form.php'); } @@ -563,8 +636,8 @@ function pkg_search_page($SID="", $dbh=NULL) { $templ_pages = array(); if ($current > 1) { - $templ_pages[__('First')] = 0; - $templ_pages[__('Previous')] = ($current - 2) * $per_page; + $templ_pages['« ' . __('First')] = 0; + $templ_pages['‹ ' . __('Previous')] = ($current - 2) * $per_page; } if ($current - 5 > 1) @@ -578,11 +651,16 @@ function pkg_search_page($SID="", $dbh=NULL) { $templ_pages["... "] = false; if ($current < $pages) { - $templ_pages[__('Next')] = $current * $per_page; - $templ_pages[__('Last')] = ($pages - 1) * $per_page; + $templ_pages[__('Next') . ' ›'] = $current * $per_page; + $templ_pages[__('Last') . ' »'] = ($pages - 1) * $per_page; } include('pkg_search_form.php'); + + while ($row = mysql_fetch_assoc($result)) { + $searchresults[] = $row; + } + include('pkg_search_results.php'); return; @@ -618,6 +696,8 @@ function sanitize_ids($ids) { * @return string Translated success or error messages */ function pkg_flag ($atype, $ids, $action=true, $dbh=NULL) { + global $AUR_LOCATION; + if (!$atype) { if ($action) { return __("You must be logged in before you can flag packages."); @@ -664,7 +744,7 @@ function pkg_flag ($atype, $ids, $action=true, $dbh=NULL) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { # construct email - $body = "Your package " . $row['Name'] . " has been flagged out of date by " . $f_name . " [1]. You may view your package at:\nhttps://aur.archlinux.org/packages.php?ID=" . $row['ID'] . "\n\n[1] - https://aur.archlinux.org/account.php?Action=AccountInfo&ID=" . $f_uid; + $body = "Your package " . $row['Name'] . " has been flagged out of date by " . $f_name . " [1]. You may view your package at:\n" . $AUR_LOCATION . "/packages.php?ID=" . $row['ID'] . "\n\n[1] - " . $AUR_LOCATION . "/account.php?Action=AccountInfo&ID=" . $f_uid; $body = wordwrap($body, 70); $headers = "Reply-to: nobody@archlinux.org\nFrom:aur-notify@archlinux.org\nX-Mailer: PHP\nX-MimeOLE: Produced By AUR\n"; @mail($row['Email'], "AUR Out-of-date Notification for ".$row['Name'], $body, $headers); @@ -708,6 +788,44 @@ function pkg_delete ($atype, $ids, $mergepkgid, $dbh=NULL) { } if ($mergepkgid) { + $mergepkgname = pkgname_from_id($mergepkgid, $dbh); + } + + # Send email notifications + foreach ($ids as $pkgid) { + $q = 'SELECT CommentNotify.*, Users.Email '; + $q.= 'FROM CommentNotify, Users '; + $q.= 'WHERE Users.ID = CommentNotify.UserID '; + $q.= 'AND CommentNotify.UserID != ' . uid_from_sid($_COOKIE['AURSID']) . ' '; + $q.= 'AND CommentNotify.PkgID = ' . $pkgid; + $result = db_query($q, $dbh); + $bcc = array(); + + while ($row = mysql_fetch_assoc($result)) { + array_push($bcc, $row['Email']); + } + if (!empty($bcc)) { + $pkgname = pkgname_from_id($pkgid); + + # TODO: native language emails for users, based on their prefs + # Simply making these strings translatable won't work, users would be + # getting emails in the language that the user who posted the comment was in + $body = ""; + if ($mergepkgid) { + $body .= username_from_sid($_COOKIE['AURSID']) . " merged \"".$pkgname."\" into \"$mergepkgname\".\n\n"; + $body .= "You will no longer receive notifications about this package, please go to https://aur.archlinux.org/packages.php?ID=".$mergepkgid." and click the Notify button if you wish to recieve them again."; + } else { + $body .= username_from_sid($_COOKIE['AURSID']) . " deleted \"".$pkgname."\".\n\n"; + $body .= "You will no longer receive notifications about this package."; + } + $body = wordwrap($body, 70); + $bcc = implode(', ', $bcc); + $headers = "Bcc: $bcc\nReply-to: nobody@archlinux.org\nFrom: aur-notify@archlinux.org\nX-Mailer: AUR\n"; + @mail('undisclosed-recipients: ;', "AUR Package deleted: " . $pkgname, $body, $headers); + } + } + + if ($mergepkgid) { /* Merge comments */ $q = "UPDATE PackageComments "; $q.= "SET PackageID = " . intval($mergepkgid) . " "; @@ -889,6 +1007,71 @@ function pkg_vote ($atype, $ids, $action=true, $dbh=NULL) { } } +# Get all usernames and ids for a specifc package id +function getvotes($pkgid, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + + $pkgid = db_escape_string($pkgid); + + $q = "SELECT UsersID,Username FROM PackageVotes "; + $q.= "LEFT JOIN Users on (UsersID = ID) "; + $q.= "WHERE PackageID = ". $pkgid . " "; + $q.= "ORDER BY Username"; + $result = db_query($q, $dbh); + + if (!$result) { + return; + } + + while ($row = mysql_fetch_assoc($result)) { + $votes[] = $row; + } + + return $votes; +} + +# Determine if a user has already voted for a specific package +function user_voted($uid, $pkgid, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + + $uid = db_escape_string($uid); + $pkgid = db_escape_string($pkgid); + + $q = "SELECT * FROM PackageVotes WHERE UsersID = ". $uid; + $q.= " AND PackageID = ".$pkgid; + $result = db_query($q, $dbh); + if (mysql_num_rows($result)) { + return true; + } + else { + return false; + } +} + +# Determine if a user wants notifications for a specific package +function user_notify($uid, $pkgid, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + + $uid = db_escape_string($uid); + $pkgid = db_escape_string($pkgid); + + $q = "SELECT * FROM CommentNotify WHERE UserID = ". $uid; + $q.= " AND PkgID = ".$pkgid; + $result = db_query($q, $dbh); + if (mysql_num_rows($result)) { + return true; + } + else { + return false; + } +} + /** * Toggle notification of packages * @@ -1046,8 +1229,8 @@ function pkg_change_category($atype, $dbh=NULL) { } $uid = uid_from_sid($_COOKIE["AURSID"], $dbh); - if ($uid == $pkg["MaintainerUID"] or - ($atype == "Developer" or $atype == "Trusted User")) { + if ($uid == $pkg["MaintainerUID"] || + ($atype == "Developer" || $atype == "Trusted User")) { $q = "UPDATE Packages "; $q.= "SET CategoryID = ".intval($category_id)." "; $q.= "WHERE ID = ".intval($pid); @@ -1057,3 +1240,100 @@ function pkg_change_category($atype, $dbh=NULL) { return __("You are not allowed to change this package category."); } } + +function pkgdetails_by_pkgname($pkgname, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = "SELECT * FROM Packages WHERE Name = '" . db_escape_string($pkgname) . "'"; + $result = db_query($q, $dbh); + if ($result) { + $pdata = mysql_fetch_assoc($result); + } + return $pdata; +} + +function new_pkgdetails($pkgname, $license, $pkgver, $category_id, $pkgdesc, $pkgurl, $uid, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = sprintf("INSERT INTO Packages (Name, License, Version, CategoryID, Description, URL, SubmittedTS, ModifiedTS, SubmitterUID, MaintainerUID) VALUES ('%s', '%s', '%s', %d, '%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), %d, %d)", + db_escape_string($pkgname), + db_escape_string($license), + db_escape_string($pkgver), + $category_id, + db_escape_string($pkgdesc), + db_escape_string($pkgurl), + $uid, + $uid); + + db_query($q, $dbh); +} + +function update_pkgdetails($pkgname, $license, $pkgver, $pkgdesc, $pkgurl, $uid, $pkgid, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + # This is an overwrite of an existing package + $q = sprintf("UPDATE Packages SET ModifiedTS = UNIX_TIMESTAMP(), Name = '%s', Version = '%s', License = '%s', Description = '%s', URL = '%s', OutOfDateTS = NULL, MaintainerUID = %d WHERE ID = %d", + db_escape_string($pkgname), + db_escape_string($pkgver), + db_escape_string($license), + db_escape_string($pkgdesc), + db_escape_string($pkgurl), + $uid, + $pkgid); + + db_query($q, $dbh); +} + +function add_pkg_dep($pkgid, $depname, $depcondition, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = sprintf("INSERT INTO PackageDepends (PackageID, DepName, DepCondition) VALUES (%d, '%s', '%s')", + $pkgid, + db_escape_string($depname), + db_escape_string($depcondition)); + + db_query($q, $dbh); +} + +function add_pkg_src($pkgid, $pkgsrc, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = "INSERT INTO PackageSources (PackageID, Source) VALUES ("; + $q .= $pkgid . ", '" . db_escape_string($pkgsrc) . "')"; + + db_query($q, $dbh); +} + +function update_pkg_category($pkgid, $category_id, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = sprintf( "UPDATE Packages SET CategoryID = %d WHERE ID = %d", + $category_id, + $pkgid); + + db_query($q, $dbh); +} + +function remove_pkg_deps($pkgid, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = "DELETE FROM PackageDepends WHERE PackageID = " . $pkgid; + + db_query($q, $dbh); +} + +function remove_pkg_sources($pkgid, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = "DELETE FROM PackageSources WHERE PackageID = " . $pkgid; + + db_query($q, $dbh); +} diff --git a/web/lib/stats.inc.php b/web/lib/stats.inc.php index 8f0f770..2828dc9 100644 --- a/web/lib/stats.inc.php +++ b/web/lib/stats.inc.php @@ -2,8 +2,7 @@ include_once('aur.inc.php'); -function updates_table($dbh) -{ +function updates_table($dbh) { $key = 'recent_updates'; if(!($newest_packages = get_cache_value($key))) { $q = 'SELECT * FROM Packages ORDER BY ModifiedTS DESC LIMIT 10'; @@ -18,18 +17,16 @@ function updates_table($dbh) include('stats/updates_table.php'); } -function user_table($user, $dbh) -{ - $escuser = db_escape_string($user); - $base_q = "SELECT count(*) FROM Packages,Users WHERE Packages.MaintainerUID = Users.ID AND Users.Username='" . $escuser . "'"; +function user_table($userid, $dbh) { + $base_q = "SELECT count(*) FROM Packages WHERE Packages.MaintainerUID = " . $userid; $maintainer_unsupported_count = db_cache_value($base_q, $dbh, - 'user_unsupported_count:' . $escuser); + 'user_unsupported_count:' . $userid); - $q = "SELECT count(*) FROM Packages,Users WHERE Packages.OutOfDateTS IS NOT NULL AND Packages.MaintainerUID = Users.ID AND Users.Username='" . $escuser . "'"; + $q = "SELECT count(*) FROM Packages WHERE Packages.OutOfDateTS IS NOT NULL AND Packages.MaintainerUID = " . $userid; $flagged_outdated = db_cache_value($q, $dbh, - 'user_flagged_outdated:' . $escuser); + 'user_flagged_outdated:' . $userid); # If the user is a TU calculate the number of the packages $atype = account_from_sid($_COOKIE["AURSID"]); @@ -37,8 +34,7 @@ function user_table($user, $dbh) include('stats/user_table.php'); } -function general_stats_table($dbh) -{ +function general_stats_table($dbh) { # AUR statistics $q = "SELECT count(*) FROM Packages"; $unsupported_count = db_cache_value($q, $dbh, 'unsupported_count'); diff --git a/web/lib/translator.inc.php b/web/lib/translator.inc.php index 44c87bd..f269b93 100644 --- a/web/lib/translator.inc.php +++ b/web/lib/translator.inc.php @@ -5,12 +5,11 @@ set_include_path(get_include_path() . PATH_SEPARATOR . '../lib' . PATH_SEPARATOR # usage: # use the __() function for returning translated strings of -# text. The string can contain escape codes %h for HTML -# and %s for regular text. +# text. The string can contain escape codes "%s". # # examples: # print __("%s has %s apples.", "Bill", "5"); -# print __("This is a %hmajor%h problem!", "<b>", "</b>"); +# print __("This is a %smajor%s problem!", "<b>", "</b>"); include_once('config.inc.php'); include_once('gettext.php'); @@ -18,6 +17,34 @@ include_once('streams.php'); global $streamer, $l10n; +# Languages we have translations for +$SUPPORTED_LANGS = array( + "ca" => "Català", + "cs" => "česky", + "da" => "Dansk", + "de" => "Deutsch", + "en" => "English", + "el" => "Ελληνικά", + "es" => "Español", + "fi" => "Finnish", + "fr" => "Français", + "he" => "עברית", + "hr" => "Hrvatski", + "hu" => "Magyar", + "it" => "Italiano", + "nb" => "Norsk", + "nl" => "Dutch", + "pl" => "Polski", + "pt_BR" => "Português (Brasil)", + "pt_PT" => "Português (Portugal)", + "ro" => "Română", + "ru" => "Русский", + "sr" => "Srpski", + "tr" => "Türkçe", + "uk" => "Українська", + "zh_CN" => "简体中文" +); + function __() { global $LANG; global $l10n; @@ -26,25 +53,73 @@ function __() { $args = func_get_args(); # First argument is always string to be translated - $tag = $args[0]; + $tag = array_shift($args); # Translate using gettext_reader initialized before. $translated = $l10n->translate($tag); $translated = htmlspecialchars($translated, ENT_QUOTES); - $num_args = sizeof($args); - # Subsequent arguments are strings to be formatted - # - # TODO: make this more robust. - # '%%' should translate to a literal '%' + if (count($args) > 0) { + $translated = vsprintf($translated, $args); + } + + return $translated; +} + +# set up the visitor's language +# +function set_lang($dbh=NULL) { + global $LANG; + global $SUPPORTED_LANGS; + global $PERSISTENT_COOKIE_TIMEOUT; + global $streamer, $l10n; + + $update_cookie = 0; + if (isset($_REQUEST['setlang'])) { + # visitor is requesting a language change + # + $LANG = $_REQUEST['setlang']; + $update_cookie = 1; - if ( $num_args > 1 ) { - for ($i = 1; $i < $num_args; $i++) { - $translated = preg_replace("/\%[sh]/", $args[$i], $translated, 1); + } elseif (isset($_COOKIE['AURLANG'])) { + # If a cookie is set, use that + # + $LANG = $_COOKIE['AURLANG']; + + } elseif (isset($_COOKIE["AURSID"])) { + # No language but a session; use default lang preference + # + if(!$dbh) { + $dbh = db_connect(); + } + $q = "SELECT LangPreference FROM Users, Sessions "; + $q.= "WHERE Users.ID = Sessions.UsersID "; + $q.= "AND Sessions.SessionID = '"; + $q.= mysql_real_escape_string($_COOKIE["AURSID"])."'"; + $result = db_query($q, $dbh); + + if ($result) { + $row = mysql_fetch_array($result); + $LANG = $row[0]; } + $update_cookie = 1; } - return $translated; + # Set $LANG to default if nothing is valid. + if (!array_key_exists($LANG, $SUPPORTED_LANGS)) { + $LANG = DEFAULT_LANG; + } + + if ($update_cookie) { + $cookie_time = time() + $PERSISTENT_COOKIE_TIMEOUT; + setcookie("AURLANG", $LANG, $cookie_time, "/"); + } + + $streamer = new FileReader('../locale/' . $LANG . + '/LC_MESSAGES/aur.mo'); + $l10n = new gettext_reader($streamer, true); + + return; } |