Remove checks before calling connection method

Large amount of boilerplate code that checks if a database
connection exists is useless now that the new connection method
automatically does the same check.

Signed-off-by: canyonknight <canyonknight@gmail.com>
Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de>
This commit is contained in:
canyonknight 2013-02-03 16:26:31 +00:00 committed by Lukas Fleischer
parent 4235d24039
commit cf2ab50b82
4 changed files with 76 additions and 229 deletions

View file

@ -10,9 +10,7 @@ include_once("acctfuncs.inc.php"); # access AUR common functions
# sending any HTML output. # sending any HTML output.
# #
if (isset($_COOKIE["AURSID"])) { if (isset($_COOKIE["AURSID"])) {
if (!isset($dbh)) { $dbh = DB::connect();
$dbh = DB::connect();
}
delete_session_id($_COOKIE["AURSID"]); delete_session_id($_COOKIE["AURSID"]);
# setting expiration to 1 means '1 second after midnight January 1, 1970' # setting expiration to 1 means '1 second after midnight January 1, 1970'
setcookie("AURSID", "", 1, "/", null, !empty($_SERVER['HTTPS']), true); setcookie("AURSID", "", 1, "/", null, !empty($_SERVER['HTTPS']), true);

View file

@ -93,9 +93,7 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="",
# error check and process request for a new/modified account # error check and process request for a new/modified account
global $SUPPORTED_LANGS; global $SUPPORTED_LANGS;
if (!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
if(isset($_COOKIE['AURSID'])) { if(isset($_COOKIE['AURSID'])) {
$editor_user = uid_from_sid($_COOKIE['AURSID']); $editor_user = uid_from_sid($_COOKIE['AURSID']);
@ -298,9 +296,7 @@ function search_results_page($UTYPE,$O=0,$SB="",$U="",$T="",
} }
$search_vars = array(); $search_vars = array();
if (!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT Users.*, AccountTypes.AccountType "; $q = "SELECT Users.*, AccountTypes.AccountType ";
$q.= "FROM Users, AccountTypes "; $q.= "FROM Users, AccountTypes ";
@ -364,9 +360,7 @@ function search_results_page($UTYPE,$O=0,$SB="",$U="",$T="",
$search_vars[] = "SB"; $search_vars[] = "SB";
$q.= "LIMIT " . $HITS_PER_PAGE . " OFFSET " . $OFFSET; $q.= "LIMIT " . $HITS_PER_PAGE . " OFFSET " . $OFFSET;
if (!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$result = $dbh->query($q); $result = $dbh->query($q);
@ -394,9 +388,7 @@ function try_login() {
$userID = null; $userID = null;
if ( isset($_REQUEST['user']) || isset($_REQUEST['passwd']) ) { if ( isset($_REQUEST['user']) || isset($_REQUEST['passwd']) ) {
if (!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$userID = valid_user($_REQUEST['user']); $userID = valid_user($_REQUEST['user']);
if ( user_suspended($userID) ) { if ( user_suspended($userID) ) {
@ -517,9 +509,7 @@ function valid_username($user) {
function valid_user($user) { function valid_user($user) {
/* if ( $user = valid_username($user) ) { */ /* if ( $user = valid_username($user) ) { */
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
if ( $user ) { if ( $user ) {
$q = "SELECT ID FROM Users "; $q = "SELECT ID FROM Users ";
@ -543,9 +533,7 @@ function valid_user($user) {
* @return bool True if there is an open proposal about the user, otherwise false * @return bool True if there is an open proposal about the user, otherwise false
*/ */
function open_user_proposals($user) { function open_user_proposals($user) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT * FROM TU_VoteInfo WHERE User = " . $dbh->quote($user) . " "; $q = "SELECT * FROM TU_VoteInfo WHERE User = " . $dbh->quote($user) . " ";
$q.= "AND End > UNIX_TIMESTAMP()"; $q.= "AND End > UNIX_TIMESTAMP()";
$result = $dbh->query($q); $result = $dbh->query($q);
@ -568,9 +556,7 @@ function open_user_proposals($user) {
* @return void * @return void
*/ */
function add_tu_proposal($agenda, $user, $votelength, $submitteruid) { function add_tu_proposal($agenda, $user, $votelength, $submitteruid) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "INSERT INTO TU_VoteInfo (Agenda, User, Submitted, End, SubmitterID) VALUES "; $q = "INSERT INTO TU_VoteInfo (Agenda, User, Submitted, End, SubmitterID) VALUES ";
$q.= "(" . $dbh->quote($agenda) . ", " . $dbh->quote($user) . ", "; $q.= "(" . $dbh->quote($agenda) . ", " . $dbh->quote($user) . ", ";
@ -588,9 +574,7 @@ function add_tu_proposal($agenda, $user, $votelength, $submitteruid) {
* @return void * @return void
*/ */
function create_resetkey($resetkey, $uid) { function create_resetkey($resetkey, $uid) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "UPDATE Users "; $q = "UPDATE Users ";
$q.= "SET ResetKey = '" . $resetkey . "' "; $q.= "SET ResetKey = '" . $resetkey . "' ";
$q.= "WHERE ID = " . $uid; $q.= "WHERE ID = " . $uid;
@ -608,9 +592,7 @@ function create_resetkey($resetkey, $uid) {
* @return string|void Redirect page if successful, otherwise return error message * @return string|void Redirect page if successful, otherwise return error message
*/ */
function password_reset($hash, $salt, $resetkey, $email) { function password_reset($hash, $salt, $resetkey, $email) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "UPDATE Users "; $q = "UPDATE Users ";
$q.= "SET Passwd = '$hash', "; $q.= "SET Passwd = '$hash', ";
$q.= "Salt = '$salt', "; $q.= "Salt = '$salt', ";
@ -652,9 +634,7 @@ function good_passwd($passwd) {
* @return bool True if password was correct and properly salted, otherwise false * @return bool True if password was correct and properly salted, otherwise false
*/ */
function valid_passwd($userID, $passwd) { function valid_passwd($userID, $passwd) {
if (!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
if ( strlen($passwd) > 0 ) { if ( strlen($passwd) > 0 ) {
# get salt for this user # get salt for this user
$salt = get_salt($userID); $salt = get_salt($userID);
@ -713,9 +693,7 @@ function valid_pgp_fingerprint($fingerprint) {
* @return bool True if the user is suspended, otherwise false * @return bool True if the user is suspended, otherwise false
*/ */
function user_suspended($id) { function user_suspended($id) {
if (!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
if (!$id) { if (!$id) {
return false; return false;
} }
@ -738,9 +716,7 @@ function user_suspended($id) {
* @return void * @return void
*/ */
function user_delete($id) { function user_delete($id) {
if (!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "DELETE FROM Users WHERE ID = " . $id; $q = "DELETE FROM Users WHERE ID = " . $id;
$dbh->query($q); $dbh->query($q);
return; return;
@ -754,9 +730,7 @@ function user_delete($id) {
* @return int|string Return 0 if un-privileged, "2" if Trusted User, "3" if Developer * @return int|string Return 0 if un-privileged, "2" if Trusted User, "3" if Developer
*/ */
function user_is_privileged($id) { function user_is_privileged($id) {
if (!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT AccountTypeID FROM Users WHERE ID = " . $id; $q = "SELECT AccountTypeID FROM Users WHERE ID = " . $id;
$result = $dbh->query($q); $result = $dbh->query($q);
if ($result) { if ($result) {
@ -777,9 +751,7 @@ function user_is_privileged($id) {
* @return void * @return void
*/ */
function delete_session_id($sid) { function delete_session_id($sid) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "DELETE FROM Sessions WHERE SessionID = " . $dbh->quote($sid); $q = "DELETE FROM Sessions WHERE SessionID = " . $dbh->quote($sid);
$dbh->query($q); $dbh->query($q);
@ -793,9 +765,7 @@ function delete_session_id($sid) {
* @return void * @return void
*/ */
function delete_user_sessions($uid) { function delete_user_sessions($uid) {
if (!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "DELETE FROM Sessions WHERE UsersID = " . intval($uid); $q = "DELETE FROM Sessions WHERE UsersID = " . intval($uid);
$dbh->exec($q); $dbh->exec($q);
@ -811,9 +781,7 @@ function delete_user_sessions($uid) {
function clear_expired_sessions() { function clear_expired_sessions() {
global $LOGIN_TIMEOUT; global $LOGIN_TIMEOUT;
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "DELETE FROM Sessions WHERE LastUpdateTS < (UNIX_TIMESTAMP() - $LOGIN_TIMEOUT)"; $q = "DELETE FROM Sessions WHERE LastUpdateTS < (UNIX_TIMESTAMP() - $LOGIN_TIMEOUT)";
$dbh->query($q); $dbh->query($q);
@ -830,9 +798,7 @@ function clear_expired_sessions() {
* @return array Account details for the specified user * @return array Account details for the specified user
*/ */
function account_details($uid, $username) { function account_details($uid, $username) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT Users.*, AccountTypes.AccountType "; $q = "SELECT Users.*, AccountTypes.AccountType ";
$q.= "FROM Users, AccountTypes "; $q.= "FROM Users, AccountTypes ";
$q.= "WHERE AccountTypes.ID = Users.AccountTypeID "; $q.= "WHERE AccountTypes.ID = Users.AccountTypeID ";
@ -859,9 +825,7 @@ function account_details($uid, $username) {
* @return bool True if the user has already voted, otherwise false * @return bool True if the user has already voted, otherwise false
*/ */
function tu_voted($voteid, $uid) { function tu_voted($voteid, $uid) {
if (!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT COUNT(*) FROM TU_Votes "; $q = "SELECT COUNT(*) FROM TU_Votes ";
$q.= "WHERE VoteID = " . intval($voteid) . " AND UserID = " . intval($uid); $q.= "WHERE VoteID = " . intval($voteid) . " AND UserID = " . intval($uid);
@ -882,9 +846,7 @@ function tu_voted($voteid, $uid) {
* @return array The details for all current Trusted User proposals * @return array The details for all current Trusted User proposals
*/ */
function current_proposal_list($order) { function current_proposal_list($order) {
if (!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT * FROM TU_VoteInfo WHERE End > " . time() . " ORDER BY Submitted " . $order; $q = "SELECT * FROM TU_VoteInfo WHERE End > " . time() . " ORDER BY Submitted " . $order;
$result = $dbh->query($q); $result = $dbh->query($q);
@ -906,9 +868,7 @@ function current_proposal_list($order) {
* @return array The details for the subset of past Trusted User proposals * @return array The details for the subset of past Trusted User proposals
*/ */
function past_proposal_list($order, $lim) { function past_proposal_list($order, $lim) {
if (!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT * FROM TU_VoteInfo WHERE End < " . time() . " ORDER BY Submitted " . $order . $lim; $q = "SELECT * FROM TU_VoteInfo WHERE End < " . time() . " ORDER BY Submitted " . $order . $lim;
$result = $dbh->query($q); $result = $dbh->query($q);
@ -927,10 +887,7 @@ function past_proposal_list($order, $lim) {
* @return string The total number of Trusted User proposals * @return string The total number of Trusted User proposals
*/ */
function proposal_count() { function proposal_count() {
if (!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT COUNT(*) FROM TU_VoteInfo"; $q = "SELECT COUNT(*) FROM TU_VoteInfo";
$result = $dbh->query($q); $result = $dbh->query($q);
$row = $result->fetch(PDO::FETCH_NUM); $row = $result->fetch(PDO::FETCH_NUM);
@ -946,9 +903,7 @@ function proposal_count() {
* @return array All stored details for a specific vote * @return array All stored details for a specific vote
*/ */
function vote_details($voteid) { function vote_details($voteid) {
if (!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT * FROM TU_VoteInfo "; $q = "SELECT * FROM TU_VoteInfo ";
$q.= "WHERE ID = " . intval($voteid); $q.= "WHERE ID = " . intval($voteid);
@ -967,9 +922,7 @@ function vote_details($voteid) {
* @return array All users who voted for a specific proposal * @return array All users who voted for a specific proposal
*/ */
function voter_list($voteid) { function voter_list($voteid) {
if (!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$whovoted = array(); $whovoted = array();
@ -999,9 +952,7 @@ function voter_list($voteid) {
* @return void * @return void
*/ */
function cast_proposal_vote($voteid, $uid, $vote, $newtotal) { function cast_proposal_vote($voteid, $uid, $vote, $newtotal) {
if (!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "UPDATE TU_VoteInfo SET " . $vote . " = (" . $newtotal . ") WHERE ID = " . $voteid; $q = "UPDATE TU_VoteInfo SET " . $vote . " = (" . $newtotal . ") WHERE ID = " . $voteid;
$result = $dbh->exec($q); $result = $dbh->exec($q);

View file

@ -37,9 +37,7 @@ function check_sid() {
$failed = 0; $failed = 0;
# the visitor is logged in, try and update the session # the visitor is logged in, try and update the session
# #
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT LastUpdateTS, UNIX_TIMESTAMP() FROM Sessions "; $q = "SELECT LastUpdateTS, UNIX_TIMESTAMP() FROM Sessions ";
$q.= "WHERE SessionID = " . $dbh->quote($_COOKIE["AURSID"]); $q.= "WHERE SessionID = " . $dbh->quote($_COOKIE["AURSID"]);
$result = $dbh->query($q); $result = $dbh->query($q);
@ -143,9 +141,7 @@ function username_from_id($id="") {
if (!$id) { if (!$id) {
return ""; return "";
} }
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT Username FROM Users WHERE ID = " . $dbh->quote($id); $q = "SELECT Username FROM Users WHERE ID = " . $dbh->quote($id);
$result = $dbh->query($q); $result = $dbh->query($q);
if (!$result) { if (!$result) {
@ -167,9 +163,7 @@ function username_from_sid($sid="") {
if (!$sid) { if (!$sid) {
return ""; return "";
} }
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT Username "; $q = "SELECT Username ";
$q.= "FROM Users, Sessions "; $q.= "FROM Users, Sessions ";
$q.= "WHERE Users.ID = Sessions.UsersID "; $q.= "WHERE Users.ID = Sessions.UsersID ";
@ -194,9 +188,7 @@ function email_from_sid($sid="") {
if (!$sid) { if (!$sid) {
return ""; return "";
} }
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT Email "; $q = "SELECT Email ";
$q.= "FROM Users, Sessions "; $q.= "FROM Users, Sessions ";
$q.= "WHERE Users.ID = Sessions.UsersID "; $q.= "WHERE Users.ID = Sessions.UsersID ";
@ -221,9 +213,7 @@ function account_from_sid($sid="") {
if (!$sid) { if (!$sid) {
return ""; return "";
} }
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT AccountType "; $q = "SELECT AccountType ";
$q.= "FROM Users, AccountTypes, Sessions "; $q.= "FROM Users, AccountTypes, Sessions ";
$q.= "WHERE Users.ID = Sessions.UsersID "; $q.= "WHERE Users.ID = Sessions.UsersID ";
@ -249,9 +239,7 @@ function uid_from_sid($sid="") {
if (!$sid) { if (!$sid) {
return ""; return "";
} }
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT Users.ID "; $q = "SELECT Users.ID ";
$q.= "FROM Users, Sessions "; $q.= "FROM Users, Sessions ";
$q.= "WHERE Users.ID = Sessions.UsersID "; $q.= "WHERE Users.ID = Sessions.UsersID ";
@ -306,9 +294,7 @@ function html_footer($ver="") {
*/ */
function can_submit_pkg($name="", $sid="") { function can_submit_pkg($name="", $sid="") {
if (!$name || !$sid) {return 0;} if (!$name || !$sid) {return 0;}
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT MaintainerUID "; $q = "SELECT MaintainerUID ";
$q.= "FROM Packages WHERE Name = " . $dbh->quote($name); $q.= "FROM Packages WHERE Name = " . $dbh->quote($name);
$result = $dbh->query($q); $result = $dbh->query($q);
@ -364,9 +350,7 @@ function uid_from_username($username="") {
if (!$username) { if (!$username) {
return ""; return "";
} }
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT ID FROM Users WHERE Username = " . $dbh->quote($username); $q = "SELECT ID FROM Users WHERE Username = " . $dbh->quote($username);
$result = $dbh->query($q); $result = $dbh->query($q);
if (!$result) { if (!$result) {
@ -388,9 +372,7 @@ function uid_from_email($email="") {
if (!$email) { if (!$email) {
return ""; return "";
} }
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT ID FROM Users WHERE Email = " . $dbh->quote($email); $q = "SELECT ID FROM Users WHERE Email = " . $dbh->quote($email);
$result = $dbh->query($q); $result = $dbh->query($q);
if (!$result) { if (!$result) {
@ -453,9 +435,7 @@ function mkurl($append) {
* @return string|void Return the salt for the requested user, otherwise void * @return string|void Return the salt for the requested user, otherwise void
*/ */
function get_salt($user_id) { function get_salt($user_id) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT Salt FROM Users WHERE ID = " . $user_id; $q = "SELECT Salt FROM Users WHERE ID = " . $user_id;
$result = $dbh->query($q); $result = $dbh->query($q);
if ($result) { if ($result) {
@ -472,9 +452,7 @@ function get_salt($user_id) {
* @param string $passwd The password of the user logging in * @param string $passwd The password of the user logging in
*/ */
function save_salt($user_id, $passwd) { function save_salt($user_id, $passwd) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$salt = generate_salt(); $salt = generate_salt();
$hash = salted_hash($passwd, $salt); $hash = salted_hash($passwd, $salt);
$q = "UPDATE Users SET Salt = " . $dbh->quote($salt) . ", "; $q = "UPDATE Users SET Salt = " . $dbh->quote($salt) . ", ";
@ -540,9 +518,7 @@ function parse_comment($comment) {
* Wrapper for beginning a database transaction * Wrapper for beginning a database transaction
*/ */
function begin_atomic_commit() { function begin_atomic_commit() {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$dbh->beginTransaction(); $dbh->beginTransaction();
} }
@ -550,9 +526,7 @@ function begin_atomic_commit() {
* Wrapper for committing a database transaction * Wrapper for committing a database transaction
*/ */
function end_atomic_commit() { function end_atomic_commit() {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$dbh->commit(); $dbh->commit();
} }
@ -563,9 +537,7 @@ function end_atomic_commit() {
* @return string The ID of the last inserted row * @return string The ID of the last inserted row
*/ */
function last_insert_id() { function last_insert_id() {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
return $dbh->lastInsertId(); return $dbh->lastInsertId();
} }
@ -577,9 +549,7 @@ function last_insert_id() {
* @return array $packages Package info for the specified number of recent packages * @return array $packages Package info for the specified number of recent packages
*/ */
function latest_pkgs($numpkgs) { function latest_pkgs($numpkgs) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT * FROM Packages "; $q = "SELECT * FROM Packages ";
$q.= "ORDER BY SubmittedTS DESC "; $q.= "ORDER BY SubmittedTS DESC ";

View file

@ -18,9 +18,7 @@ function canDeleteComment($comment_id=0, $atype="", $uid=0) {
# A TU/Dev can delete any comment # A TU/Dev can delete any comment
return TRUE; return TRUE;
} }
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT COUNT(ID) AS CNT "; $q = "SELECT COUNT(ID) AS CNT ";
$q.= "FROM PackageComments "; $q.= "FROM PackageComments ";
$q.= "WHERE ID = " . intval($comment_id); $q.= "WHERE ID = " . intval($comment_id);
@ -87,9 +85,7 @@ function canSubmitBlacklisted($atype = "") {
*/ */
function pkgCategories() { function pkgCategories() {
$cats = array(); $cats = array();
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT * FROM PackageCategories WHERE ID != 1 "; $q = "SELECT * FROM PackageCategories WHERE ID != 1 ";
$q.= "ORDER BY Category ASC"; $q.= "ORDER BY Category ASC";
$result = $dbh->query($q); $result = $dbh->query($q);
@ -110,9 +106,7 @@ function pkgCategories() {
*/ */
function pkgid_from_name($name="") { function pkgid_from_name($name="") {
if (!$name) {return NULL;} if (!$name) {return NULL;}
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT ID FROM Packages "; $q = "SELECT ID FROM Packages ";
$q.= "WHERE Name = " . $dbh->quote($name); $q.= "WHERE Name = " . $dbh->quote($name);
$result = $dbh->query($q); $result = $dbh->query($q);
@ -134,9 +128,7 @@ function package_dependencies($pkgid) {
$deps = array(); $deps = array();
$pkgid = intval($pkgid); $pkgid = intval($pkgid);
if ($pkgid > 0) { if ($pkgid > 0) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT pd.DepName, pd.DepCondition, p.ID FROM PackageDepends pd "; $q = "SELECT pd.DepName, pd.DepCondition, p.ID FROM PackageDepends pd ";
$q.= "LEFT JOIN Packages p ON pd.DepName = p.Name "; $q.= "LEFT JOIN Packages p ON pd.DepName = p.Name ";
$q.= "WHERE pd.PackageID = ". $pkgid . " "; $q.= "WHERE pd.PackageID = ". $pkgid . " ";
@ -162,9 +154,7 @@ function package_dependencies($pkgid) {
function package_required($name="") { function package_required($name="") {
$deps = array(); $deps = array();
if ($name != "") { if ($name != "") {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT DISTINCT p.Name, PackageID FROM PackageDepends pd "; $q = "SELECT DISTINCT p.Name, PackageID FROM PackageDepends pd ";
$q.= "JOIN Packages p ON pd.PackageID = p.ID "; $q.= "JOIN Packages p ON pd.PackageID = p.ID ";
$q.= "WHERE DepName = " . $dbh->quote($name) . " "; $q.= "WHERE DepName = " . $dbh->quote($name) . " ";
@ -186,15 +176,11 @@ function package_required($name="") {
* @return string The number of comments left for a specific package * @return string The number of comments left for a specific package
*/ */
function package_comments_count($pkgid) { function package_comments_count($pkgid) {
if (!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$pkgid = intval($pkgid); $pkgid = intval($pkgid);
if ($pkgid > 0) { if ($pkgid > 0) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT COUNT(*) FROM PackageComments "; $q = "SELECT COUNT(*) FROM PackageComments ";
$q.= "WHERE PackageID = " . $pkgid; $q.= "WHERE PackageID = " . $pkgid;
$q.= " AND DelUsersID IS NULL"; $q.= " AND DelUsersID IS NULL";
@ -220,9 +206,7 @@ function package_comments($pkgid) {
$comments = array(); $comments = array();
$pkgid = intval($pkgid); $pkgid = intval($pkgid);
if ($pkgid > 0) { if ($pkgid > 0) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT PackageComments.ID, UserName, UsersID, Comments, CommentTS "; $q = "SELECT PackageComments.ID, UserName, UsersID, Comments, CommentTS ";
$q.= "FROM PackageComments, Users "; $q.= "FROM PackageComments, Users ";
$q.= "WHERE PackageComments.UsersID = Users.ID"; $q.= "WHERE PackageComments.UsersID = Users.ID";
@ -260,9 +244,7 @@ function package_comments($pkgid) {
function add_package_comment($pkgid, $uid, $comment) { function add_package_comment($pkgid, $uid, $comment) {
global $AUR_LOCATION; global $AUR_LOCATION;
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "INSERT INTO PackageComments "; $q = "INSERT INTO PackageComments ";
$q.= "(PackageID, UsersID, Comments, CommentTS) VALUES ("; $q.= "(PackageID, UsersID, Comments, CommentTS) VALUES (";
@ -317,9 +299,7 @@ function package_sources($pkgid) {
$sources = array(); $sources = array();
$pkgid = intval($pkgid); $pkgid = intval($pkgid);
if ($pkgid > 0) { if ($pkgid > 0) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT Source FROM PackageSources "; $q = "SELECT Source FROM PackageSources ";
$q.= "WHERE PackageID = " . $pkgid; $q.= "WHERE PackageID = " . $pkgid;
$q.= " ORDER BY Source"; $q.= " ORDER BY Source";
@ -344,9 +324,7 @@ function package_sources($pkgid) {
function pkgvotes_from_sid($sid="") { function pkgvotes_from_sid($sid="") {
$pkgs = array(); $pkgs = array();
if (!$sid) {return $pkgs;} if (!$sid) {return $pkgs;}
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT PackageID "; $q = "SELECT PackageID ";
$q.= "FROM PackageVotes, Users, Sessions "; $q.= "FROM PackageVotes, Users, Sessions ";
$q.= "WHERE Users.ID = Sessions.UsersID "; $q.= "WHERE Users.ID = Sessions.UsersID ";
@ -372,9 +350,7 @@ function pkgname_from_id($pkgids) {
if (is_array($pkgids)) { if (is_array($pkgids)) {
$pkgids = sanitize_ids($pkgids); $pkgids = sanitize_ids($pkgids);
$names = array(); $names = array();
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT Name FROM Packages WHERE ID IN ("; $q = "SELECT Name FROM Packages WHERE ID IN (";
$q.= implode(",", $pkgids) . ")"; $q.= implode(",", $pkgids) . ")";
$result = $dbh->query($q); $result = $dbh->query($q);
@ -386,9 +362,7 @@ function pkgname_from_id($pkgids) {
return $names; return $names;
} }
elseif ($pkgids > 0) { elseif ($pkgids > 0) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT Name FROM Packages WHERE ID = " . $pkgids; $q = "SELECT Name FROM Packages WHERE ID = " . $pkgids;
$result = $dbh->query($q); $result = $dbh->query($q);
if ($result) { if ($result) {
@ -409,9 +383,7 @@ function pkgname_from_id($pkgids) {
* @return bool True if the name is blacklisted, otherwise false * @return bool True if the name is blacklisted, otherwise false
*/ */
function pkgname_is_blacklisted($name) { function pkgname_is_blacklisted($name) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT COUNT(*) FROM PackageBlacklist "; $q = "SELECT COUNT(*) FROM PackageBlacklist ";
$q.= "WHERE Name = " . $dbh->quote($name); $q.= "WHERE Name = " . $dbh->quote($name);
$result = $dbh->query($q); $result = $dbh->query($q);
@ -428,9 +400,7 @@ function pkgname_is_blacklisted($name) {
* @return array The package's details OR error message * @return array The package's details OR error message
**/ **/
function get_package_details($id=0) { function get_package_details($id=0) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT Packages.*,Category "; $q = "SELECT Packages.*,Category ";
$q.= "FROM Packages,PackageCategories "; $q.= "FROM Packages,PackageCategories ";
@ -468,9 +438,7 @@ function display_package_details($id=0, $row, $SID="") {
global $AUR_LOCATION; global $AUR_LOCATION;
global $USE_VIRTUAL_URLS; global $USE_VIRTUAL_URLS;
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
if (isset($row['error'])) { if (isset($row['error'])) {
print "<p>" . $row['error'] . "</p>\n"; print "<p>" . $row['error'] . "</p>\n";
@ -542,9 +510,7 @@ function display_package_details($id=0, $row, $SID="") {
* do_UnNotify - Disable notification * do_UnNotify - Disable notification
*/ */
function pkg_search_page($SID="") { function pkg_search_page($SID="") {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
// get commonly used variables... // get commonly used variables...
// TODO: REDUCE DB HITS. // TODO: REDUCE DB HITS.
@ -800,9 +766,7 @@ function pkg_flag($atype, $ids) {
return __("You did not select any packages to flag."); return __("You did not select any packages to flag.");
} }
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "UPDATE Packages SET"; $q = "UPDATE Packages SET";
$q.= " OutOfDateTS = UNIX_TIMESTAMP()"; $q.= " OutOfDateTS = UNIX_TIMESTAMP()";
@ -854,9 +818,7 @@ function pkg_unflag($atype, $ids) {
return __("You did not select any packages to unflag."); return __("You did not select any packages to unflag.");
} }
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "UPDATE Packages SET "; $q = "UPDATE Packages SET ";
$q.= "OutOfDateTS = NULL "; $q.= "OutOfDateTS = NULL ";
@ -897,9 +859,7 @@ function pkg_delete ($atype, $ids, $mergepkgid) {
return __("You did not select any packages to delete."); return __("You did not select any packages to delete.");
} }
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
if ($mergepkgid) { if ($mergepkgid) {
$mergepkgname = pkgname_from_id($mergepkgid); $mergepkgname = pkgname_from_id($mergepkgid);
@ -999,9 +959,7 @@ function pkg_adopt ($atype, $ids, $action=true) {
} }
} }
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$field = "MaintainerUID"; $field = "MaintainerUID";
$q = "UPDATE Packages "; $q = "UPDATE Packages ";
@ -1059,9 +1017,7 @@ function pkg_vote ($atype, $ids, $action=true) {
} }
} }
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$my_votes = pkgvotes_from_sid($_COOKIE["AURSID"]); $my_votes = pkgvotes_from_sid($_COOKIE["AURSID"]);
$uid = uid_from_sid($_COOKIE["AURSID"]); $uid = uid_from_sid($_COOKIE["AURSID"]);
@ -1128,9 +1084,7 @@ function pkg_vote ($atype, $ids, $action=true) {
* @return array User IDs and usernames that voted for a specific package * @return array User IDs and usernames that voted for a specific package
*/ */
function getvotes($pkgid) { function getvotes($pkgid) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT UsersID,Username FROM PackageVotes "; $q = "SELECT UsersID,Username FROM PackageVotes ";
$q.= "LEFT JOIN Users on (UsersID = ID) "; $q.= "LEFT JOIN Users on (UsersID = ID) ";
@ -1159,9 +1113,7 @@ function getvotes($pkgid) {
* @return bool True if the user has already voted, otherwise false * @return bool True if the user has already voted, otherwise false
*/ */
function user_voted($uid, $pkgid) { function user_voted($uid, $pkgid) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT * FROM PackageVotes WHERE UsersID = ". $dbh->quote($uid); $q = "SELECT * FROM PackageVotes WHERE UsersID = ". $dbh->quote($uid);
$q.= " AND PackageID = " . $dbh->quote($pkgid); $q.= " AND PackageID = " . $dbh->quote($pkgid);
@ -1184,9 +1136,7 @@ function user_voted($uid, $pkgid) {
* @return bool True if the user wants notifications, otherwise false * @return bool True if the user wants notifications, otherwise false
*/ */
function user_notify($uid, $pkgid) { function user_notify($uid, $pkgid) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT * FROM CommentNotify WHERE UserID = " . $dbh->quote($uid); $q = "SELECT * FROM CommentNotify WHERE UserID = " . $dbh->quote($uid);
$q.= " AND PkgID = " . $dbh->quote($pkgid); $q.= " AND PkgID = " . $dbh->quote($pkgid);
@ -1219,9 +1169,7 @@ function pkg_notify ($atype, $ids, $action=true) {
return __("Couldn't add to notification list."); return __("Couldn't add to notification list.");
} }
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$uid = uid_from_sid($_COOKIE["AURSID"]); $uid = uid_from_sid($_COOKIE["AURSID"]);
$output = ""; $output = "";
@ -1298,9 +1246,7 @@ function pkg_delete_comment($atype) {
return __("Missing comment ID."); return __("Missing comment ID.");
} }
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$uid = uid_from_sid($_COOKIE["AURSID"]); $uid = uid_from_sid($_COOKIE["AURSID"]);
if (canDeleteComment($comment_id, $atype, $uid)) { if (canDeleteComment($comment_id, $atype, $uid)) {
$q = "UPDATE PackageComments "; $q = "UPDATE PackageComments ";
@ -1332,9 +1278,7 @@ function pkg_change_category($pid, $atype) {
return __("Missing category ID."); return __("Missing category ID.");
} }
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$catArray = pkgCategories($dbh); $catArray = pkgCategories($dbh);
if (!array_key_exists($category_id, $catArray)) { if (!array_key_exists($category_id, $catArray)) {
return __("Invalid category ID."); return __("Invalid category ID.");
@ -1373,9 +1317,7 @@ function pkg_change_category($pid, $atype) {
* @return array All package details for a specific package * @return array All package details for a specific package
*/ */
function pkgdetails_by_pkgname($pkgname) { function pkgdetails_by_pkgname($pkgname) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "SELECT * FROM Packages WHERE Name = " . $dbh->quote($pkgname); $q = "SELECT * FROM Packages WHERE Name = " . $dbh->quote($pkgname);
$result = $dbh->query($q); $result = $dbh->query($q);
if ($result) { if ($result) {
@ -1398,9 +1340,7 @@ function pkgdetails_by_pkgname($pkgname) {
* @return void * @return void
*/ */
function new_pkgdetails($pkgname, $license, $pkgver, $category_id, $pkgdesc, $pkgurl, $uid) { function new_pkgdetails($pkgname, $license, $pkgver, $category_id, $pkgdesc, $pkgurl, $uid) {
if(!$dbh) { $dbh = DB::connect();
$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)", $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)",
$dbh->quote($pkgname), $dbh->quote($pkgname),
$dbh->quote($license), $dbh->quote($license),
@ -1428,9 +1368,7 @@ function new_pkgdetails($pkgname, $license, $pkgver, $category_id, $pkgdesc, $pk
* @return void * @return void
*/ */
function update_pkgdetails($pkgname, $license, $pkgver, $pkgdesc, $pkgurl, $uid, $pkgid) { function update_pkgdetails($pkgname, $license, $pkgver, $pkgdesc, $pkgurl, $uid, $pkgid) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
# This is an overwrite of an existing package # 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", $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",
$dbh->quote($pkgname), $dbh->quote($pkgname),
@ -1454,9 +1392,7 @@ function update_pkgdetails($pkgname, $license, $pkgver, $pkgdesc, $pkgurl, $uid,
* @return void * @return void
*/ */
function add_pkg_dep($pkgid, $depname, $depcondition) { function add_pkg_dep($pkgid, $depname, $depcondition) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = sprintf("INSERT INTO PackageDepends (PackageID, DepName, DepCondition) VALUES (%d, %s, %s)", $q = sprintf("INSERT INTO PackageDepends (PackageID, DepName, DepCondition) VALUES (%d, %s, %s)",
$pkgid, $pkgid,
$dbh->quote($depname), $dbh->quote($depname),
@ -1474,9 +1410,7 @@ function add_pkg_dep($pkgid, $depname, $depcondition) {
* @return void * @return void
*/ */
function add_pkg_src($pkgid, $pkgsrc) { function add_pkg_src($pkgid, $pkgsrc) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "INSERT INTO PackageSources (PackageID, Source) VALUES ("; $q = "INSERT INTO PackageSources (PackageID, Source) VALUES (";
$q .= $pkgid . ", " . $dbh->quote($pkgsrc) . ")"; $q .= $pkgid . ", " . $dbh->quote($pkgsrc) . ")";
@ -1492,9 +1426,7 @@ function add_pkg_src($pkgid, $pkgsrc) {
* @return void * @return void
*/ */
function update_pkg_category($pkgid, $category_id) { function update_pkg_category($pkgid, $category_id) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = sprintf( "UPDATE Packages SET CategoryID = %d WHERE ID = %d", $q = sprintf( "UPDATE Packages SET CategoryID = %d WHERE ID = %d",
$category_id, $category_id,
$pkgid); $pkgid);
@ -1510,9 +1442,7 @@ function update_pkg_category($pkgid, $category_id) {
* @return void * @return void
*/ */
function remove_pkg_deps($pkgid) { function remove_pkg_deps($pkgid) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "DELETE FROM PackageDepends WHERE PackageID = " . $pkgid; $q = "DELETE FROM PackageDepends WHERE PackageID = " . $pkgid;
$dbh->exec($q); $dbh->exec($q);
@ -1526,9 +1456,7 @@ function remove_pkg_deps($pkgid) {
* @return void * @return void
*/ */
function remove_pkg_sources($pkgid) { function remove_pkg_sources($pkgid) {
if(!$dbh) { $dbh = DB::connect();
$dbh = DB::connect();
}
$q = "DELETE FROM PackageSources WHERE PackageID = " . $pkgid; $q = "DELETE FROM PackageSources WHERE PackageID = " . $pkgid;
$dbh->exec($q); $dbh->exec($q);