test return value from db_query before assuming it is valid

make the sql query form consistent in usage by cleaning up instances
where db_query's result was not inspected before attempting to fetch row
data from the handle

Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de>
This commit is contained in:
elij 2011-05-11 16:17:12 -07:00 committed by Lukas Fleischer
parent d38f3460e5
commit 0898f1447a
7 changed files with 135 additions and 68 deletions

View file

@ -21,14 +21,26 @@ if ($atype == "Trusted User" OR $atype == "Developer") {
if (!empty($_POST['user'])) { if (!empty($_POST['user'])) {
$qcheck = "SELECT * FROM Users WHERE Username = '" . mysql_real_escape_string($_POST['user']) . "'"; $qcheck = "SELECT * FROM Users WHERE Username = '" . mysql_real_escape_string($_POST['user']) . "'";
$check = mysql_num_rows(db_query($qcheck, $dbh)); $result = db_query($qcheck, $dbh);
if ($result) {
$check = mysql_num_rows($result);
}
else {
$check = 0;
}
if ($check == 0) { if ($check == 0) {
$error.= __("Username does not exist."); $error.= __("Username does not exist.");
} else { } else {
$qcheck = "SELECT * FROM TU_VoteInfo WHERE User = '" . mysql_real_escape_string($_POST['user']) . "'"; $qcheck = "SELECT * FROM TU_VoteInfo WHERE User = '" . mysql_real_escape_string($_POST['user']) . "'";
$qcheck.= " AND End > UNIX_TIMESTAMP()"; $qcheck.= " AND End > UNIX_TIMESTAMP()";
$check = mysql_num_rows(db_query($qcheck, $dbh)); $result = db_query($qcheck, $dbh);
if ($result) {
$check = mysql_num_rows($result);
}
else {
$check = 0;
}
if ($check != 0) { if ($check != 0) {
$error.= __("%s already has proposal running for them.", htmlentities($_POST['user'])); $error.= __("%s already has proposal running for them.", htmlentities($_POST['user']));

View file

@ -36,7 +36,13 @@ if ($atype == "Trusted User" OR $atype == "Developer") {
$qvoted = "SELECT * FROM TU_Votes WHERE "; $qvoted = "SELECT * FROM TU_Votes WHERE ";
$qvoted.= "VoteID = " . $row['ID'] . " AND "; $qvoted.= "VoteID = " . $row['ID'] . " AND ";
$qvoted.= "UserID = " . uid_from_sid($_COOKIE["AURSID"]); $qvoted.= "UserID = " . uid_from_sid($_COOKIE["AURSID"]);
$hasvoted = mysql_num_rows(db_query($qvoted, $dbh)); $result = db_query($qvoted, $dbh);
if ($result) {
$hasvoted = mysql_num_rows($result);
}
else {
$hasvoted = 0;
}
# List voters of a proposal. # List voters of a proposal.
$qwhoVoted = "SELECT tv.UserID,U.Username $qwhoVoted = "SELECT tv.UserID,U.Username
@ -85,12 +91,17 @@ if ($atype == "Trusted User" OR $atype == "Developer") {
$canvote = 0; $canvote = 0;
$errorvote = __("You've already voted for this proposal."); $errorvote = __("You've already voted for this proposal.");
# Update if they voted # Update if they voted
$hasvoted = mysql_num_rows(db_query($qvoted, $dbh)); $result = db_query($qvoted, $dbh);
if ($result) {
$hasvoted = mysql_num_rows($result);
}
$results = db_query($q, $dbh); $results = db_query($q, $dbh);
if ($results) {
$row = mysql_fetch_assoc($results); $row = mysql_fetch_assoc($results);
} }
} }
}
include("tu_details.php"); include("tu_details.php");
} }
} else { } else {

View file

@ -718,11 +718,11 @@ function valid_user( $user )
$q = "SELECT ID FROM Users WHERE Username = '" $q = "SELECT ID FROM Users WHERE Username = '"
. mysql_real_escape_string($user). "'"; . mysql_real_escape_string($user). "'";
$result = mysql_fetch_row(db_query($q, $dbh)); $result = db_query($q, $dbh);
# Is the username in the database? # Is the username in the database?
if ($result[0]) { if ($result) {
return $result[0]; $row = mysql_fetch_row($result);
return $row[0];
} }
} }
return; return;
@ -751,28 +751,33 @@ function valid_passwd( $userID, $passwd )
$passwd_q = "SELECT ID FROM Users" . $passwd_q = "SELECT ID FROM Users" .
" WHERE ID = " . $userID . " AND Passwd = '" . " WHERE ID = " . $userID . " AND Passwd = '" .
salted_hash($passwd, $salt) . "'"; salted_hash($passwd, $salt) . "'";
$passwd_result = mysql_fetch_row(db_query($passwd_q, $dbh)); $result = db_query($passwd_q, $dbh);
if ($result) {
$passwd_result = mysql_fetch_row($result);
if ($passwd_result[0]) { if ($passwd_result[0]) {
return true; return true;
} }
}
} else { } else {
# check without salt # check without salt
$nosalt_q = "SELECT ID FROM Users". $nosalt_q = "SELECT ID FROM Users".
" WHERE ID = " . $userID . " WHERE ID = " . $userID .
" AND Passwd = '" . md5($passwd) . "'"; " AND Passwd = '" . md5($passwd) . "'";
$nosalt_result = mysql_fetch_row(db_query($nosalt_q, $dbh)); $result = db_query($nosalt_q, $dbh);
if ($nosalt_result[0]) { if ($result) {
$nosalt_row = mysql_fetch_row($result);
if ($nosalt_row[0]) {
# password correct, but salt it first # password correct, but salt it first
if (!save_salt($userID, $passwd)) { if (!save_salt($userID, $passwd)) {
trigger_error("Unable to salt user's password;" . trigger_error("Unable to salt user's password;" .
" ID " . $userID, E_USER_WARNING); " ID " . $userID, E_USER_WARNING);
return false; return false;
} }
return true; return true;
} }
} }
} }
}
return false; return false;
} }
@ -783,10 +788,13 @@ function user_suspended( $id )
{ {
$dbh = db_connect(); $dbh = db_connect();
$q = "SELECT Suspended FROM Users WHERE ID = " . $id; $q = "SELECT Suspended FROM Users WHERE ID = " . $id;
$result = mysql_fetch_row(db_query($q, $dbh)); $result = db_query($q, $dbh);
if ($result) {
$row = mysql_fetch_row($result);
if ($result[0] == 1 ) { if ($result[0] == 1 ) {
return true; return true;
} }
}
return false; return false;
} }
@ -797,7 +805,7 @@ function user_delete( $id )
{ {
$dbh = db_connect(); $dbh = db_connect();
$q = "DELETE FROM Users WHERE ID = " . $id; $q = "DELETE FROM Users WHERE ID = " . $id;
$result = mysql_fetch_row(db_query($q, $dbh)); db_query($q, $dbh);
return; return;
} }
@ -809,10 +817,13 @@ function user_is_privileged( $id )
{ {
$dbh = db_connect(); $dbh = db_connect();
$q = "SELECT AccountTypeID FROM Users WHERE ID = " . $id; $q = "SELECT AccountTypeID FROM Users WHERE ID = " . $id;
$result = mysql_fetch_row(db_query($q, $dbh)); $result = db_query($q, $dbh);
if ($result) {
$row = mysql_fetch_row($result);
if( $result[0] > 1) { if( $result[0] > 1) {
return $result[0]; return $result[0];
} }
}
return 0; return 0;
} }

View file

@ -491,8 +491,12 @@ function get_salt($user_id)
{ {
$dbh = db_connect(); $dbh = db_connect();
$salt_q = "SELECT Salt FROM Users WHERE ID = " . $user_id; $salt_q = "SELECT Salt FROM Users WHERE ID = " . $user_id;
$salt_result = mysql_fetch_row(db_query($salt_q, $dbh)); $result = db_query($salt_q, $dbh);
return $salt_result[0]; if ($result) {
$salt_row = mysql_fetch_row($result);
return $salt_row[0];
}
return;
} }
function save_salt($user_id, $passwd) function save_salt($user_id, $passwd)

View file

@ -507,7 +507,13 @@ function pkg_search_page($SID="") {
$q_total = "SELECT COUNT(*) " . $q_from . $q_where; $q_total = "SELECT COUNT(*) " . $q_from . $q_where;
$result = db_query($q, $dbh); $result = db_query($q, $dbh);
$total = mysql_result(db_query($q_total, $dbh), 0); $result_t = db_query($q_total, $dbh);
if ($result_t) {
$total = mysql_result($result_t, 0);
}
else {
$total = 0;
}
if ($result && $total > 0) { if ($result && $total > 0) {
if (isset($_GET["SO"]) && $_GET["SO"] == "d"){ if (isset($_GET["SO"]) && $_GET["SO"] == "d"){
@ -859,7 +865,13 @@ function pkg_notify ($atype, $ids, $action = True) {
# format in which it's sent requires this. # format in which it's sent requires this.
foreach ($ids as $pid) { foreach ($ids as $pid) {
$q = "SELECT Name FROM Packages WHERE ID = $pid"; $q = "SELECT Name FROM Packages WHERE ID = $pid";
$pkgname = mysql_result(db_query($q, $dbh), 0); $result = db_query($q, $dbh);
if ($result) {
$pkgname = mysql_result($result , 0);
}
else {
$pkgname = '';
}
if ($first) if ($first)
$first = False; $first = False;
@ -872,7 +884,8 @@ function pkg_notify ($atype, $ids, $action = True) {
$q .= " AND PkgID = $pid"; $q .= " AND PkgID = $pid";
# Notification already added. Don't add again. # Notification already added. Don't add again.
if (!mysql_num_rows(db_query($q, $dbh))) { $result = db_query($q, $dbh);
if (!mysql_num_rows($result)) {
$q = "INSERT INTO CommentNotify (PkgID, UserID) VALUES ($pid, $uid)"; $q = "INSERT INTO CommentNotify (PkgID, UserID) VALUES ($pid, $uid)";
db_query($q, $dbh); db_query($q, $dbh);
} }
@ -967,8 +980,12 @@ function pkg_change_category($atype) {
$q.= "FROM Packages "; $q.= "FROM Packages ";
$q.= "WHERE Packages.ID = ".$pid; $q.= "WHERE Packages.ID = ".$pid;
$result = db_query($q, $dbh); $result = db_query($q, $dbh);
echo mysql_error(); if ($result) {
$pkg = mysql_fetch_assoc($result); $pkg = mysql_fetch_assoc($result);
}
else {
return __("You are not allowed to change this package category.");
}
$uid = uid_from_sid($_COOKIE["AURSID"]); $uid = uid_from_sid($_COOKIE["AURSID"]);
if ($uid == $pkg["MaintainerUID"] or if ($uid == $pkg["MaintainerUID"] or

View file

@ -8,38 +8,44 @@
# #
$q = "SELECT * FROM PackageVotes WHERE UsersID = ". $uid; $q = "SELECT * FROM PackageVotes WHERE UsersID = ". $uid;
$q.= " AND PackageID = ".$row["ID"]; $q.= " AND PackageID = ".$row["ID"];
if (!mysql_num_rows(db_query($q, $dbh))) { $result = db_query($q, $dbh);
if ($result) {
if (!mysql_num_rows($result)) {
echo " <input type='submit' class='button' name='do_Vote'"; echo " <input type='submit' class='button' name='do_Vote'";
echo " value='".__("Vote")."' /> "; echo " value='".__("Vote")."' /> ";
} else { } else {
echo "<input type='submit' class='button' name='do_UnVote'"; echo "<input type='submit' class='button' name='do_UnVote'";
echo " value='".__("UnVote")."' /> "; echo " value='".__("UnVote")."' /> ";
} }
}
# Comment Notify Button # Comment Notify Button
# #
$q = "SELECT * FROM CommentNotify WHERE UserID = ". $uid; $q = "SELECT * FROM CommentNotify WHERE UserID = ". $uid;
$q.= " AND PkgID = ".$row["ID"]; $q.= " AND PkgID = ".$row["ID"];
if (!mysql_num_rows(db_query($q, $dbh))) { $result = db_query($q, $dbh);
if ($result) {
if (!mysql_num_rows($result)) {
echo "<input type='submit' class='button' name='do_Notify'"; echo "<input type='submit' class='button' name='do_Notify'";
echo " value='".__("Notify")."' title='".__("New Comment Notification")."' /> "; echo " value='".__("Notify")."' title='".__("New Comment Notification")."' /> ";
} else { } else {
echo "<input type='submit' class='button' name='do_UnNotify'"; echo "<input type='submit' class='button' name='do_UnNotify'";
echo " value='".__("UnNotify")."' title='".__("No New Comment Notification")."' /> "; echo " value='".__("UnNotify")."' title='".__("No New Comment Notification")."' /> ";
} }
}
if ($row["OutOfDateTS"] === NULL) { if ($row["OutOfDateTS"] === NULL) {
echo "<input type='submit' class='button' name='do_Flag'"; echo "<input type='submit' class='button' name='do_Flag'";
echo " value='".__("Flag Out-of-date")."' />\n"; echo " value='".__("Flag Out-of-date")."' />\n";
} else { } else {
echo "<input type='submit' class='button' name='do_UnFlag'"; echo "<input type='submit' class='button' name='do_UnFlag'";
echo " value='".__("UnFlag Out-of-date")."' />\n"; echo " value='".__("UnFlag Out-of-date")."' />\n";
} }
if ($row["MaintainerUID"] === NULL) { if ($row["MaintainerUID"] === NULL) {
echo "<input type='submit' class='button' name='do_Adopt'"; echo "<input type='submit' class='button' name='do_Adopt'";
echo " value='".__("Adopt Packages")."' />\n"; echo " value='".__("Adopt Packages")."' />\n";
} else if ($uid == $row["MaintainerUID"] || } else if ($uid == $row["MaintainerUID"] ||
$atype == "Trusted User" || $atype == "Developer") { $atype == "Trusted User" || $atype == "Developer") {
echo "<input type='submit' class='button' name='do_Disown'"; echo "<input type='submit' class='button' name='do_Disown'";
echo " value='".__("Disown Packages")."' />\n"; echo " value='".__("Disown Packages")."' />\n";

View file

@ -40,7 +40,13 @@
<td class='<?php print $c ?>'> <td class='<?php print $c ?>'>
<?php <?php
$q = "SELECT * FROM TU_Votes WHERE VoteID = " . $row['ID'] . " AND UserID = " . uid_from_sid($_COOKIE["AURSID"]); $q = "SELECT * FROM TU_Votes WHERE VoteID = " . $row['ID'] . " AND UserID = " . uid_from_sid($_COOKIE["AURSID"]);
$hasvoted = mysql_num_rows(db_query($q, $dbh)); $result_tulist = db_query($q, $dbh);
if ($result_tulist) {
$hasvoted = mysql_num_rows($result_tulist);
}
else {
$hasvoted = 0;
}
?> ?>
<span class='f5'><span class='blue'> <span class='f5'><span class='blue'>
<?php if ($hasvoted == 0) { ?> <?php if ($hasvoted == 0) { ?>