Commit cf2ab50b authored by canyonknight's avatar canyonknight Committed by Lukas Fleischer
Browse files

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: default avatarcanyonknight <canyonknight@gmail.com>
Signed-off-by: default avatarLukas Fleischer <archlinux@cryptocrack.de>
parent 4235d240
......@@ -10,9 +10,7 @@ include_once("acctfuncs.inc.php"); # access AUR common functions
# sending any HTML output.
#
if (isset($_COOKIE["AURSID"])) {
if (!isset($dbh)) {
$dbh = DB::connect();
}
$dbh = DB::connect();
delete_session_id($_COOKIE["AURSID"]);
# setting expiration to 1 means '1 second after midnight January 1, 1970'
setcookie("AURSID", "", 1, "/", null, !empty($_SERVER['HTTPS']), true);
......
......@@ -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
global $SUPPORTED_LANGS;
if (!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
if(isset($_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();
if (!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT Users.*, AccountTypes.AccountType ";
$q.= "FROM Users, AccountTypes ";
......@@ -364,9 +360,7 @@ function search_results_page($UTYPE,$O=0,$SB="",$U="",$T="",
$search_vars[] = "SB";
$q.= "LIMIT " . $HITS_PER_PAGE . " OFFSET " . $OFFSET;
if (!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$result = $dbh->query($q);
......@@ -394,9 +388,7 @@ function try_login() {
$userID = null;
if ( isset($_REQUEST['user']) || isset($_REQUEST['passwd']) ) {
if (!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$userID = valid_user($_REQUEST['user']);
if ( user_suspended($userID) ) {
......@@ -517,9 +509,7 @@ function valid_username($user) {
function valid_user($user) {
/* if ( $user = valid_username($user) ) { */
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
if ( $user ) {
$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
*/
function open_user_proposals($user) {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT * FROM TU_VoteInfo WHERE User = " . $dbh->quote($user) . " ";
$q.= "AND End > UNIX_TIMESTAMP()";
$result = $dbh->query($q);
......@@ -568,9 +556,7 @@ function open_user_proposals($user) {
* @return void
*/
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.= "(" . $dbh->quote($agenda) . ", " . $dbh->quote($user) . ", ";
......@@ -588,9 +574,7 @@ function add_tu_proposal($agenda, $user, $votelength, $submitteruid) {
* @return void
*/
function create_resetkey($resetkey, $uid) {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "UPDATE Users ";
$q.= "SET ResetKey = '" . $resetkey . "' ";
$q.= "WHERE ID = " . $uid;
......@@ -608,9 +592,7 @@ function create_resetkey($resetkey, $uid) {
* @return string|void Redirect page if successful, otherwise return error message
*/
function password_reset($hash, $salt, $resetkey, $email) {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "UPDATE Users ";
$q.= "SET Passwd = '$hash', ";
$q.= "Salt = '$salt', ";
......@@ -652,9 +634,7 @@ function good_passwd($passwd) {
* @return bool True if password was correct and properly salted, otherwise false
*/
function valid_passwd($userID, $passwd) {
if (!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
if ( strlen($passwd) > 0 ) {
# get salt for this user
$salt = get_salt($userID);
......@@ -713,9 +693,7 @@ function valid_pgp_fingerprint($fingerprint) {
* @return bool True if the user is suspended, otherwise false
*/
function user_suspended($id) {
if (!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
if (!$id) {
return false;
}
......@@ -738,9 +716,7 @@ function user_suspended($id) {
* @return void
*/
function user_delete($id) {
if (!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "DELETE FROM Users WHERE ID = " . $id;
$dbh->query($q);
return;
......@@ -754,9 +730,7 @@ function user_delete($id) {
* @return int|string Return 0 if un-privileged, "2" if Trusted User, "3" if Developer
*/
function user_is_privileged($id) {
if (!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT AccountTypeID FROM Users WHERE ID = " . $id;
$result = $dbh->query($q);
if ($result) {
......@@ -777,9 +751,7 @@ function user_is_privileged($id) {
* @return void
*/
function delete_session_id($sid) {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "DELETE FROM Sessions WHERE SessionID = " . $dbh->quote($sid);
$dbh->query($q);
......@@ -793,9 +765,7 @@ function delete_session_id($sid) {
* @return void
*/
function delete_user_sessions($uid) {
if (!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "DELETE FROM Sessions WHERE UsersID = " . intval($uid);
$dbh->exec($q);
......@@ -811,9 +781,7 @@ function delete_user_sessions($uid) {
function clear_expired_sessions() {
global $LOGIN_TIMEOUT;
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "DELETE FROM Sessions WHERE LastUpdateTS < (UNIX_TIMESTAMP() - $LOGIN_TIMEOUT)";
$dbh->query($q);
......@@ -830,9 +798,7 @@ function clear_expired_sessions() {
* @return array Account details for the specified user
*/
function account_details($uid, $username) {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT Users.*, AccountTypes.AccountType ";
$q.= "FROM Users, AccountTypes ";
$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
*/
function tu_voted($voteid, $uid) {
if (!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT COUNT(*) FROM TU_Votes ";
$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
*/
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;
$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
*/
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;
$result = $dbh->query($q);
......@@ -927,10 +887,7 @@ function past_proposal_list($order, $lim) {
* @return string The total number of Trusted User proposals
*/
function proposal_count() {
if (!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT COUNT(*) FROM TU_VoteInfo";
$result = $dbh->query($q);
$row = $result->fetch(PDO::FETCH_NUM);
......@@ -946,9 +903,7 @@ function proposal_count() {
* @return array All stored details for a specific vote
*/
function vote_details($voteid) {
if (!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT * FROM TU_VoteInfo ";
$q.= "WHERE ID = " . intval($voteid);
......@@ -967,9 +922,7 @@ function vote_details($voteid) {
* @return array All users who voted for a specific proposal
*/
function voter_list($voteid) {
if (!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$whovoted = array();
......@@ -999,9 +952,7 @@ function voter_list($voteid) {
* @return void
*/
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;
$result = $dbh->exec($q);
......
......@@ -37,9 +37,7 @@ function check_sid() {
$failed = 0;
# 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.= "WHERE SessionID = " . $dbh->quote($_COOKIE["AURSID"]);
$result = $dbh->query($q);
......@@ -143,9 +141,7 @@ function username_from_id($id="") {
if (!$id) {
return "";
}
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT Username FROM Users WHERE ID = " . $dbh->quote($id);
$result = $dbh->query($q);
if (!$result) {
......@@ -167,9 +163,7 @@ function username_from_sid($sid="") {
if (!$sid) {
return "";
}
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT Username ";
$q.= "FROM Users, Sessions ";
$q.= "WHERE Users.ID = Sessions.UsersID ";
......@@ -194,9 +188,7 @@ function email_from_sid($sid="") {
if (!$sid) {
return "";
}
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT Email ";
$q.= "FROM Users, Sessions ";
$q.= "WHERE Users.ID = Sessions.UsersID ";
......@@ -221,9 +213,7 @@ function account_from_sid($sid="") {
if (!$sid) {
return "";
}
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT AccountType ";
$q.= "FROM Users, AccountTypes, Sessions ";
$q.= "WHERE Users.ID = Sessions.UsersID ";
......@@ -249,9 +239,7 @@ function uid_from_sid($sid="") {
if (!$sid) {
return "";
}
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT Users.ID ";
$q.= "FROM Users, Sessions ";
$q.= "WHERE Users.ID = Sessions.UsersID ";
......@@ -306,9 +294,7 @@ function html_footer($ver="") {
*/
function can_submit_pkg($name="", $sid="") {
if (!$name || !$sid) {return 0;}
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT MaintainerUID ";
$q.= "FROM Packages WHERE Name = " . $dbh->quote($name);
$result = $dbh->query($q);
......@@ -364,9 +350,7 @@ function uid_from_username($username="") {
if (!$username) {
return "";
}
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT ID FROM Users WHERE Username = " . $dbh->quote($username);
$result = $dbh->query($q);
if (!$result) {
......@@ -388,9 +372,7 @@ function uid_from_email($email="") {
if (!$email) {
return "";
}
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT ID FROM Users WHERE Email = " . $dbh->quote($email);
$result = $dbh->query($q);
if (!$result) {
......@@ -453,9 +435,7 @@ function mkurl($append) {
* @return string|void Return the salt for the requested user, otherwise void
*/
function get_salt($user_id) {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT Salt FROM Users WHERE ID = " . $user_id;
$result = $dbh->query($q);
if ($result) {
......@@ -472,9 +452,7 @@ function get_salt($user_id) {
* @param string $passwd The password of the user logging in
*/
function save_salt($user_id, $passwd) {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$salt = generate_salt();
$hash = salted_hash($passwd, $salt);
$q = "UPDATE Users SET Salt = " . $dbh->quote($salt) . ", ";
......@@ -540,9 +518,7 @@ function parse_comment($comment) {
* Wrapper for beginning a database transaction
*/
function begin_atomic_commit() {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$dbh->beginTransaction();
}
......@@ -550,9 +526,7 @@ function begin_atomic_commit() {
* Wrapper for committing a database transaction
*/
function end_atomic_commit() {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$dbh->commit();
}
......@@ -563,9 +537,7 @@ function end_atomic_commit() {
* @return string The ID of the last inserted row
*/
function last_insert_id() {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
return $dbh->lastInsertId();
}
......@@ -577,9 +549,7 @@ function last_insert_id() {
* @return array $packages Package info for the specified number of recent packages
*/
function latest_pkgs($numpkgs) {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT * FROM Packages ";
$q.= "ORDER BY SubmittedTS DESC ";
......
......@@ -18,9 +18,7 @@ function canDeleteComment($comment_id=0, $atype="", $uid=0) {
# A TU/Dev can delete any comment
return TRUE;
}
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT COUNT(ID) AS CNT ";
$q.= "FROM PackageComments ";
$q.= "WHERE ID = " . intval($comment_id);
......@@ -87,9 +85,7 @@ function canSubmitBlacklisted($atype = "") {
*/
function pkgCategories() {
$cats = array();
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT * FROM PackageCategories WHERE ID != 1 ";
$q.= "ORDER BY Category ASC";
$result = $dbh->query($q);
......@@ -110,9 +106,7 @@ function pkgCategories() {
*/
function pkgid_from_name($name="") {
if (!$name) {return NULL;}
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT ID FROM Packages ";
$q.= "WHERE Name = " . $dbh->quote($name);
$result = $dbh->query($q);
......@@ -134,9 +128,7 @@ function package_dependencies($pkgid) {
$deps = array();
$pkgid = intval($pkgid);
if ($pkgid > 0) {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT pd.DepName, pd.DepCondition, p.ID FROM PackageDepends pd ";
$q.= "LEFT JOIN Packages p ON pd.DepName = p.Name ";
$q.= "WHERE pd.PackageID = ". $pkgid . " ";
......@@ -162,9 +154,7 @@ function package_dependencies($pkgid) {
function package_required($name="") {
$deps = array();
if ($name != "") {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT DISTINCT p.Name, PackageID FROM PackageDepends pd ";
$q.= "JOIN Packages p ON pd.PackageID = p.ID ";
$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
*/
function package_comments_count($pkgid) {
if (!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$pkgid = intval($pkgid);
if ($pkgid > 0) {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT COUNT(*) FROM PackageComments ";
$q.= "WHERE PackageID = " . $pkgid;
$q.= " AND DelUsersID IS NULL";
......@@ -220,9 +206,7 @@ function package_comments($pkgid) {
$comments = array();
$pkgid = intval($pkgid);
if ($pkgid > 0) {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT PackageComments.ID, UserName, UsersID, Comments, CommentTS ";
$q.= "FROM PackageComments, Users ";
$q.= "WHERE PackageComments.UsersID = Users.ID";
......@@ -260,9 +244,7 @@ function package_comments($pkgid) {
function add_package_comment($pkgid, $uid, $comment) {
global $AUR_LOCATION;
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "INSERT INTO PackageComments ";
$q.= "(PackageID, UsersID, Comments, CommentTS) VALUES (";
......@@ -317,9 +299,7 @@ function package_sources($pkgid) {
$sources = array();
$pkgid = intval($pkgid);
if ($pkgid > 0) {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT Source FROM PackageSources ";
$q.= "WHERE PackageID = " . $pkgid;
$q.= " ORDER BY Source";
......@@ -344,9 +324,7 @@ function package_sources($pkgid) {
function pkgvotes_from_sid($sid="") {
$pkgs = array();
if (!$sid) {return $pkgs;}
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT PackageID ";
$q.= "FROM PackageVotes, Users, Sessions ";
$q.= "WHERE Users.ID = Sessions.UsersID ";
......@@ -372,9 +350,7 @@ function pkgname_from_id($pkgids) {
if (is_array($pkgids)) {
$pkgids = sanitize_ids($pkgids);
$names = array();
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT Name FROM Packages WHERE ID IN (";
$q.= implode(",", $pkgids) . ")";
$result = $dbh->query($q);
......@@ -386,9 +362,7 @@ function pkgname_from_id($pkgids) {
return $names;
}
elseif ($pkgids > 0) {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT Name FROM Packages WHERE ID = " . $pkgids;
$result = $dbh->query($q);
if ($result) {
......@@ -409,9 +383,7 @@ function pkgname_from_id($pkgids) {
* @return bool True if the name is blacklisted, otherwise false
*/
function pkgname_is_blacklisted($name) {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT COUNT(*) FROM PackageBlacklist ";
$q.= "WHERE Name = " . $dbh->quote($name);
$result = $dbh->query($q);
......@@ -428,9 +400,7 @@ function pkgname_is_blacklisted($name) {
* @return array The package's details OR error message
**/
function get_package_details($id=0) {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "SELECT Packages.*,Category ";
$q.= "FROM Packages,PackageCategories ";
......@@ -468,9 +438,7 @@ function display_package_details($id=0, $row, $SID="") {
global $AUR_LOCATION;
global $USE_VIRTUAL_URLS;
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
if (isset($row['error'])) {
print "<p>" . $row['error'] . "</p>\n";
......@@ -542,9 +510,7 @@ function display_package_details($id=0, $row, $SID="") {
* do_UnNotify - Disable notification
*/
function pkg_search_page($SID="") {
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
// get commonly used variables...
// TODO: REDUCE DB HITS.
......@@ -800,9 +766,7 @@ function pkg_flag($atype, $ids) {
return __("You did not select any packages to flag.");
}
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "UPDATE Packages SET";
$q.= " OutOfDateTS = UNIX_TIMESTAMP()";
......@@ -854,9 +818,7 @@ function pkg_unflag($atype, $ids) {
return __("You did not select any packages to unflag.");
}
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
$q = "UPDATE Packages SET ";
$q.= "OutOfDateTS = NULL ";
......@@ -897,9 +859,7 @@ function pkg_delete ($atype, $ids, $mergepkgid) {
return __("You did not select any packages to delete.");
}
if(!$dbh) {
$dbh = DB::connect();
}
$dbh = DB::connect();
if ($mergepkgid) {
$mergepkgname = pkgname_from_id($mergepkgid);
......@@ -999,9 +959,7 @@ function pkg_adopt ($atype, $ids, $action=true) {
}
}
if(!$dbh) {
$dbh = DB::connect();