[aur-dev] [PATCH 0/5] Database connection overhaul
Hello all, I recently noticed in my SQL logs that multiple database connections will be started over the course of a page loading. Past commits have relied on passing the handle as an argument to avoid that. The problem is many functions don't get a handle passed to them and there is a large amount of boilerplate code. This patch series will basically fully implement the same functionality as passing DB handles as arguments but with a lot less code and not mucking up function args. Due to the fact that some of these patches are quite large they may get eaten by the list, so as always they will be available on my "working" branch. canyonknight (5): Add database wrapper class and new connection method Remove unnecessary database connection parameter from all functions Remove documentation references to database parameter Remove checks before calling connection method Remove unneeded database connection calls web/html/account.php | 1 - web/html/home.php | 8 +- web/html/logout.php | 7 +- web/html/pkgsubmit.php | 23 ++-- web/lib/DB.class.php | 28 +++++ web/lib/acctfuncs.inc.php | 179 +++++++++-------------------- web/lib/aur.inc.php | 131 ++++++--------------- web/lib/aurjson.class.php | 2 +- web/lib/cachefuncs.inc.php | 3 +- web/lib/pkgfuncs.inc.php | 276 +++++++++++++++------------------------------ web/lib/stats.inc.php | 33 +++--- web/lib/translator.inc.php | 6 +- 12 files changed, 239 insertions(+), 458 deletions(-) create mode 100644 web/lib/DB.class.php -- 1.8.1.2
Uses the Singleton pattern to ensure all queries use the same database connection that is released upon script completion. All database connections should now be called with DB::connect() and not db_connect(). Signed-off-by: canyonknight <canyonknight@gmail.com> --- web/html/account.php | 2 +- web/html/home.php | 2 +- web/html/logout.php | 2 +- web/html/pkgsubmit.php | 2 +- web/lib/DB.class.php | 28 ++++++++++++++++++ web/lib/acctfuncs.inc.php | 48 +++++++++++++++---------------- web/lib/aur.inc.php | 49 +++++++++++-------------------- web/lib/aurjson.class.php | 2 +- web/lib/pkgfuncs.inc.php | 72 +++++++++++++++++++++++----------------------- web/lib/translator.inc.php | 2 +- 10 files changed, 110 insertions(+), 99 deletions(-) create mode 100644 web/lib/DB.class.php diff --git a/web/html/account.php b/web/html/account.php index 2133734..7cd0263 100644 --- a/web/html/account.php +++ b/web/html/account.php @@ -20,7 +20,7 @@ $action = in_request("Action"); if (isset($_COOKIE["AURSID"])) { # visitor is logged in # - $dbh = db_connect(); + $dbh = DB::connect(); $atype = account_from_sid($_COOKIE["AURSID"]); if ($action == "SearchAccounts") { diff --git a/web/html/home.php b/web/html/home.php index 0b51d55..a10ebf0 100644 --- a/web/html/home.php +++ b/web/html/home.php @@ -10,7 +10,7 @@ include_once('stats.inc.php'); html_header( __("Home") ); -$dbh = db_connect(); +$dbh = DB::connect(); ?> diff --git a/web/html/logout.php b/web/html/logout.php index 3d059e7..2d8bebc 100644 --- a/web/html/logout.php +++ b/web/html/logout.php @@ -11,7 +11,7 @@ include_once("acctfuncs.inc.php"); # access AUR common functions # if (isset($_COOKIE["AURSID"])) { if (!isset($dbh)) { - $dbh = db_connect(); + $dbh = DB::connect(); } delete_session_id($_COOKIE["AURSID"], $dbh); # setting expiration to 1 means '1 second after midnight January 1, 1970' diff --git a/web/html/pkgsubmit.php b/web/html/pkgsubmit.php index 5dd58af..78fceac 100644 --- a/web/html/pkgsubmit.php +++ b/web/html/pkgsubmit.php @@ -356,7 +356,7 @@ if ($uid): # Update the backend database if (!$error) { - $dbh = db_connect(); + $dbh = DB::connect(); begin_atomic_commit($dbh); $pdata = pkgdetails_by_pkgname($new_pkgbuild['pkgname'], $dbh); diff --git a/web/lib/DB.class.php b/web/lib/DB.class.php new file mode 100644 index 0000000..0975989 --- /dev/null +++ b/web/lib/DB.class.php @@ -0,0 +1,28 @@ +<?php + +class DB { + + /** + * A database object + */ + private static $dbh = null; + + /** + * Return an already existing database object or newly instantiated object + * + * @return \PDO A database connection using PDO + */ + public static function connect() { + if (self::$dbh === null) { + try { + self::$dbh = new PDO(AUR_db_DSN_prefix . ":" . AUR_db_host + . ";dbname=" . AUR_db_name, AUR_db_user, AUR_db_pass); + self::$dbh->exec("SET NAMES 'utf8' COLLATE 'utf8_general_ci';"); + } catch (PDOException $e) { + die('Error - Could not connect to AUR database'); + } + } + + return self::$dbh; + } +} diff --git a/web/lib/acctfuncs.inc.php b/web/lib/acctfuncs.inc.php index 3759c63..1e25f62 100644 --- a/web/lib/acctfuncs.inc.php +++ b/web/lib/acctfuncs.inc.php @@ -95,7 +95,7 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", global $SUPPORTED_LANGS; if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } if(isset($_COOKIE['AURSID'])) { @@ -301,7 +301,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 "; @@ -367,7 +367,7 @@ function search_results_page($UTYPE,$O=0,$SB="",$U="",$T="", $q.= "LIMIT " . $HITS_PER_PAGE . " OFFSET " . $OFFSET; if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $result = $dbh->query($q); @@ -398,7 +398,7 @@ function try_login($dbh=NULL) { if ( isset($_REQUEST['user']) || isset($_REQUEST['passwd']) ) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $userID = valid_user($_REQUEST['user'], $dbh); @@ -522,7 +522,7 @@ function valid_user($user, $dbh=NULL) { /* if ( $user = valid_username($user) ) { */ if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } if ( $user ) { @@ -549,7 +549,7 @@ function valid_user($user, $dbh=NULL) { */ function open_user_proposals($user, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT * FROM TU_VoteInfo WHERE User = " . $dbh->quote($user) . " "; $q.= "AND End > UNIX_TIMESTAMP()"; @@ -575,7 +575,7 @@ function open_user_proposals($user, $dbh=NULL) { */ function add_tu_proposal($agenda, $user, $votelength, $submitteruid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "INSERT INTO TU_VoteInfo (Agenda, User, Submitted, End, SubmitterID) VALUES "; @@ -596,7 +596,7 @@ function add_tu_proposal($agenda, $user, $votelength, $submitteruid, $dbh=NULL) */ function create_resetkey($resetkey, $uid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "UPDATE Users "; $q.= "SET ResetKey = '" . $resetkey . "' "; @@ -617,7 +617,7 @@ function create_resetkey($resetkey, $uid, $dbh=NULL) { */ function password_reset($hash, $salt, $resetkey, $email, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "UPDATE Users "; $q.= "SET Passwd = '$hash', "; @@ -662,7 +662,7 @@ function good_passwd($passwd) { */ function valid_passwd($userID, $passwd, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } if ( strlen($passwd) > 0 ) { # get salt for this user @@ -724,7 +724,7 @@ function valid_pgp_fingerprint($fingerprint) { */ function user_suspended($id, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } if (!$id) { return false; @@ -750,7 +750,7 @@ function user_suspended($id, $dbh=NULL) { */ function user_delete($id, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "DELETE FROM Users WHERE ID = " . $id; $dbh->query($q); @@ -767,7 +767,7 @@ function user_delete($id, $dbh=NULL) { */ function user_is_privileged($id, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT AccountTypeID FROM Users WHERE ID = " . $id; $result = $dbh->query($q); @@ -791,7 +791,7 @@ function user_is_privileged($id, $dbh=NULL) { */ function delete_session_id($sid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "DELETE FROM Sessions WHERE SessionID = " . $dbh->quote($sid); @@ -808,7 +808,7 @@ function delete_session_id($sid, $dbh=NULL) { */ function delete_user_sessions($uid, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "DELETE FROM Sessions WHERE UsersID = " . intval($uid); @@ -827,7 +827,7 @@ function clear_expired_sessions($dbh=NULL) { global $LOGIN_TIMEOUT; if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "DELETE FROM Sessions WHERE LastUpdateTS < (UNIX_TIMESTAMP() - $LOGIN_TIMEOUT)"; @@ -847,7 +847,7 @@ function clear_expired_sessions($dbh=NULL) { */ function account_details($uid, $username, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT Users.*, AccountTypes.AccountType "; $q.= "FROM Users, AccountTypes "; @@ -877,7 +877,7 @@ function account_details($uid, $username, $dbh=NULL) { */ function tu_voted($voteid, $uid, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT COUNT(*) FROM TU_Votes "; @@ -901,7 +901,7 @@ function tu_voted($voteid, $uid, $dbh=NULL) { */ function current_proposal_list($order, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT * FROM TU_VoteInfo WHERE End > " . time() . " ORDER BY Submitted " . $order; @@ -926,7 +926,7 @@ function current_proposal_list($order, $dbh=NULL) { */ function past_proposal_list($order, $lim, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT * FROM TU_VoteInfo WHERE End < " . time() . " ORDER BY Submitted " . $order . $lim; @@ -949,7 +949,7 @@ function past_proposal_list($order, $lim, $dbh=NULL) { */ function proposal_count($dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT COUNT(*) FROM TU_VoteInfo"; @@ -969,7 +969,7 @@ function proposal_count($dbh=NULL) { */ function vote_details($voteid, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT * FROM TU_VoteInfo "; @@ -991,7 +991,7 @@ function vote_details($voteid, $dbh=NULL) { */ function voter_list($voteid, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $whovoted = array(); @@ -1024,7 +1024,7 @@ function voter_list($voteid, $dbh=NULL) { */ function cast_proposal_vote($voteid, $uid, $vote, $newtotal, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "UPDATE TU_VoteInfo SET " . $vote . " = (" . $newtotal . ") WHERE ID = " . $voteid; diff --git a/web/lib/aur.inc.php b/web/lib/aur.inc.php index d8c5cb4..9a84ec7 100644 --- a/web/lib/aur.inc.php +++ b/web/lib/aur.inc.php @@ -11,6 +11,7 @@ include_once('translator.inc.php'); set_lang(); include_once("config.inc.php"); +include_once("DB.class.php"); include_once("routing.inc.php"); include_once("version.inc.php"); include_once("acctfuncs.inc.php"); @@ -38,7 +39,7 @@ function check_sid($dbh=NULL) { # 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"]); @@ -145,7 +146,7 @@ function username_from_id($id="", $dbh=NULL) { return ""; } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT Username FROM Users WHERE ID = " . $dbh->quote($id); $result = $dbh->query($q); @@ -170,7 +171,7 @@ function username_from_sid($sid="", $dbh=NULL) { return ""; } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT Username "; $q.= "FROM Users, Sessions "; @@ -198,7 +199,7 @@ function email_from_sid($sid="", $dbh=NULL) { return ""; } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT Email "; $q.= "FROM Users, Sessions "; @@ -226,7 +227,7 @@ function account_from_sid($sid="", $dbh=NULL) { return ""; } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT AccountType "; $q.= "FROM Users, AccountTypes, Sessions "; @@ -255,7 +256,7 @@ function uid_from_sid($sid="", $dbh=NULL) { return ""; } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT Users.ID "; $q.= "FROM Users, Sessions "; @@ -271,24 +272,6 @@ function uid_from_sid($sid="", $dbh=NULL) { } /** - * Establish a connection with a database using PDO - * - * @return \PDO A database connection - */ -function db_connect() { - try { - $dbh = new PDO(AUR_db_DSN_prefix . ":" . AUR_db_host . ";dbname=" . AUR_db_name, AUR_db_user, AUR_db_pass); - } - catch (PDOException $e) { - echo "Error - Could not connect to AUR database: " . $e->getMessage(); - } - - $dbh->exec("SET NAMES 'utf8' COLLATE 'utf8_general_ci';"); - - return $dbh; -} - -/** * Common AUR header displayed on all pages * * @global string $LANG Language selected by the visitor @@ -331,7 +314,7 @@ function html_footer($ver="") { function can_submit_pkg($name="", $sid="", $dbh=NULL) { if (!$name || !$sid) {return 0;} if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT MaintainerUID "; $q.= "FROM Packages WHERE Name = " . $dbh->quote($name); @@ -390,7 +373,7 @@ function uid_from_username($username="", $dbh=NULL) { return ""; } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT ID FROM Users WHERE Username = " . $dbh->quote($username); $result = $dbh->query($q); @@ -415,7 +398,7 @@ function uid_from_email($email="", $dbh=NULL) { return ""; } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT ID FROM Users WHERE Email = " . $dbh->quote($email); $result = $dbh->query($q); @@ -481,7 +464,7 @@ function mkurl($append) { */ function get_salt($user_id, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT Salt FROM Users WHERE ID = " . $user_id; $result = $dbh->query($q); @@ -501,7 +484,7 @@ function get_salt($user_id, $dbh=NULL) { */ function save_salt($user_id, $passwd, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $salt = generate_salt(); $hash = salted_hash($passwd, $salt); @@ -571,7 +554,7 @@ function parse_comment($comment) { */ function begin_atomic_commit($dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $dbh->beginTransaction(); } @@ -583,7 +566,7 @@ function begin_atomic_commit($dbh=NULL) { */ function end_atomic_commit($dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $dbh->commit(); } @@ -598,7 +581,7 @@ function end_atomic_commit($dbh=NULL) { */ function last_insert_id($dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } return $dbh->lastInsertId(); } @@ -613,7 +596,7 @@ function last_insert_id($dbh=NULL) { */ function latest_pkgs($numpkgs, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT * FROM Packages "; diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index 616b783..b905275 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -52,7 +52,7 @@ class AurJSON { // do the routing if ( in_array($http_data['type'], self::$exposed_methods) ) { // set up db connection. - $this->dbh = db_connect(); + $this->dbh = DB::connect(); // ugh. this works. I hate you php. $json = call_user_func(array(&$this, $http_data['type']), diff --git a/web/lib/pkgfuncs.inc.php b/web/lib/pkgfuncs.inc.php index 568ca3d..b29acb3 100644 --- a/web/lib/pkgfuncs.inc.php +++ b/web/lib/pkgfuncs.inc.php @@ -20,7 +20,7 @@ function canDeleteComment($comment_id=0, $atype="", $uid=0, $dbh=NULL) { return TRUE; } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT COUNT(ID) AS CNT "; $q.= "FROM PackageComments "; @@ -89,7 +89,7 @@ function canSubmitBlacklisted($atype = "") { function pkgCategories($dbh=NULL) { $cats = array(); if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT * FROM PackageCategories WHERE ID != 1 "; $q.= "ORDER BY Category ASC"; @@ -113,7 +113,7 @@ function pkgCategories($dbh=NULL) { function pkgid_from_name($name="", $dbh=NULL) { if (!$name) {return NULL;} if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT ID FROM Packages "; $q.= "WHERE Name = " . $dbh->quote($name); @@ -138,7 +138,7 @@ function package_dependencies($pkgid, $dbh=NULL) { $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 "; @@ -167,7 +167,7 @@ function package_required($name="", $dbh=NULL) { $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 "; @@ -192,13 +192,13 @@ function package_required($name="", $dbh=NULL) { */ function package_comments_count($pkgid, $dbh=NULL) { 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; @@ -227,7 +227,7 @@ function package_comments($pkgid, $dbh=NULL) { $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 "; @@ -268,7 +268,7 @@ function add_package_comment($pkgid, $uid, $comment, $dbh=NULL) { global $AUR_LOCATION; if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "INSERT INTO PackageComments "; @@ -326,7 +326,7 @@ function package_sources($pkgid, $dbh=NULL) { $pkgid = intval($pkgid); if ($pkgid > 0) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT Source FROM PackageSources "; $q.= "WHERE PackageID = " . $pkgid; @@ -354,7 +354,7 @@ function pkgvotes_from_sid($sid="", $dbh=NULL) { $pkgs = array(); if (!$sid) {return $pkgs;} if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT PackageID "; $q.= "FROM PackageVotes, Users, Sessions "; @@ -383,7 +383,7 @@ function pkgname_from_id($pkgids, $dbh=NULL) { $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) . ")"; @@ -397,7 +397,7 @@ function pkgname_from_id($pkgids, $dbh=NULL) { } elseif ($pkgids > 0) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT Name FROM Packages WHERE ID = " . $pkgids; $result = $dbh->query($q); @@ -421,7 +421,7 @@ function pkgname_from_id($pkgids, $dbh=NULL) { */ function pkgname_is_blacklisted($name, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT COUNT(*) FROM PackageBlacklist "; $q.= "WHERE Name = " . $dbh->quote($name); @@ -441,7 +441,7 @@ function pkgname_is_blacklisted($name, $dbh=NULL) { **/ function get_package_details($id=0, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT Packages.*,Category "; @@ -482,7 +482,7 @@ function display_package_details($id=0, $row, $SID="", $dbh=NULL) { global $USE_VIRTUAL_URLS; if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } if (isset($row['error'])) { @@ -556,7 +556,7 @@ function display_package_details($id=0, $row, $SID="", $dbh=NULL) { */ function pkg_search_page($SID="", $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } // get commonly used variables... @@ -814,7 +814,7 @@ function pkg_flag($atype, $ids, $dbh=NULL) { } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "UPDATE Packages SET"; @@ -868,7 +868,7 @@ function pkg_unflag($atype, $ids, $dbh=NULL) { } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "UPDATE Packages SET "; @@ -911,7 +911,7 @@ function pkg_delete ($atype, $ids, $mergepkgid, $dbh=NULL) { } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } if ($mergepkgid) { @@ -1013,7 +1013,7 @@ function pkg_adopt ($atype, $ids, $action=true, $dbh=NULL) { } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $field = "MaintainerUID"; @@ -1073,7 +1073,7 @@ function pkg_vote ($atype, $ids, $action=true, $dbh=NULL) { } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $my_votes = pkgvotes_from_sid($_COOKIE["AURSID"], $dbh); $uid = uid_from_sid($_COOKIE["AURSID"], $dbh); @@ -1143,7 +1143,7 @@ function pkg_vote ($atype, $ids, $action=true, $dbh=NULL) { */ function getvotes($pkgid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT UsersID,Username FROM PackageVotes "; @@ -1175,7 +1175,7 @@ function getvotes($pkgid, $dbh=NULL) { */ function user_voted($uid, $pkgid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT * FROM PackageVotes WHERE UsersID = ". $dbh->quote($uid); @@ -1201,7 +1201,7 @@ function user_voted($uid, $pkgid, $dbh=NULL) { */ function user_notify($uid, $pkgid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT * FROM CommentNotify WHERE UserID = " . $dbh->quote($uid); @@ -1236,7 +1236,7 @@ function pkg_notify ($atype, $ids, $action=true, $dbh=NULL) { } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $uid = uid_from_sid($_COOKIE["AURSID"], $dbh); @@ -1315,7 +1315,7 @@ function pkg_delete_comment($atype, $dbh=NULL) { } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $uid = uid_from_sid($_COOKIE["AURSID"], $dbh); if (canDeleteComment($comment_id, $atype, $uid, $dbh)) { @@ -1349,7 +1349,7 @@ function pkg_change_category($pid, $atype, $dbh=NULL) { } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $catArray = pkgCategories($dbh); if (!array_key_exists($category_id, $catArray)) { @@ -1391,7 +1391,7 @@ function pkg_change_category($pid, $atype, $dbh=NULL) { */ function pkgdetails_by_pkgname($pkgname, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT * FROM Packages WHERE Name = " . $dbh->quote($pkgname); $result = $dbh->query($q); @@ -1417,7 +1417,7 @@ function pkgdetails_by_pkgname($pkgname, $dbh=NULL) { */ function new_pkgdetails($pkgname, $license, $pkgver, $category_id, $pkgdesc, $pkgurl, $uid, $dbh=NULL) { 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)", $dbh->quote($pkgname), @@ -1448,7 +1448,7 @@ function new_pkgdetails($pkgname, $license, $pkgver, $category_id, $pkgdesc, $pk */ function update_pkgdetails($pkgname, $license, $pkgver, $pkgdesc, $pkgurl, $uid, $pkgid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $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", @@ -1475,7 +1475,7 @@ function update_pkgdetails($pkgname, $license, $pkgver, $pkgdesc, $pkgurl, $uid, */ function add_pkg_dep($pkgid, $depname, $depcondition, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = sprintf("INSERT INTO PackageDepends (PackageID, DepName, DepCondition) VALUES (%d, %s, %s)", $pkgid, @@ -1496,7 +1496,7 @@ function add_pkg_dep($pkgid, $depname, $depcondition, $dbh=NULL) { */ function add_pkg_src($pkgid, $pkgsrc, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "INSERT INTO PackageSources (PackageID, Source) VALUES ("; $q .= $pkgid . ", " . $dbh->quote($pkgsrc) . ")"; @@ -1515,7 +1515,7 @@ function add_pkg_src($pkgid, $pkgsrc, $dbh=NULL) { */ function update_pkg_category($pkgid, $category_id, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = sprintf( "UPDATE Packages SET CategoryID = %d WHERE ID = %d", $category_id, @@ -1534,7 +1534,7 @@ function update_pkg_category($pkgid, $category_id, $dbh=NULL) { */ function remove_pkg_deps($pkgid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "DELETE FROM PackageDepends WHERE PackageID = " . $pkgid; @@ -1551,7 +1551,7 @@ function remove_pkg_deps($pkgid, $dbh=NULL) { */ function remove_pkg_sources($pkgid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "DELETE FROM PackageSources WHERE PackageID = " . $pkgid; diff --git a/web/lib/translator.inc.php b/web/lib/translator.inc.php index 1477b17..ec8cb58 100644 --- a/web/lib/translator.inc.php +++ b/web/lib/translator.inc.php @@ -91,7 +91,7 @@ function set_lang($dbh=NULL) { # No language but a session; use default lang preference # if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT LangPreference FROM Users, Sessions "; $q.= "WHERE Users.ID = Sessions.UsersID "; -- 1.8.1.2
On Sun, Feb 3, 2013 at 11:26 AM, canyonknight <canyonknight@gmail.com> wrote:
Uses the Singleton pattern to ensure all queries use the same database connection that is released upon script completion.
All database connections should now be called with DB::connect() and not db_connect().
Signed-off-by: canyonknight <canyonknight@gmail.com> --- web/html/account.php | 2 +- web/html/home.php | 2 +- web/html/logout.php | 2 +- web/html/pkgsubmit.php | 2 +- web/lib/DB.class.php | 28 ++++++++++++++++++ web/lib/acctfuncs.inc.php | 48 +++++++++++++++---------------- web/lib/aur.inc.php | 49 +++++++++++-------------------- web/lib/aurjson.class.php | 2 +- web/lib/pkgfuncs.inc.php | 72 +++++++++++++++++++++++----------------------- web/lib/translator.inc.php | 2 +- 10 files changed, 110 insertions(+), 99 deletions(-) create mode 100644 web/lib/DB.class.php
diff --git a/web/html/account.php b/web/html/account.php index 2133734..7cd0263 100644 --- a/web/html/account.php +++ b/web/html/account.php @@ -20,7 +20,7 @@ $action = in_request("Action"); if (isset($_COOKIE["AURSID"])) { # visitor is logged in # - $dbh = db_connect(); + $dbh = DB::connect(); $atype = account_from_sid($_COOKIE["AURSID"]);
if ($action == "SearchAccounts") { diff --git a/web/html/home.php b/web/html/home.php index 0b51d55..a10ebf0 100644 --- a/web/html/home.php +++ b/web/html/home.php @@ -10,7 +10,7 @@ include_once('stats.inc.php');
html_header( __("Home") );
-$dbh = db_connect(); +$dbh = DB::connect();
?>
diff --git a/web/html/logout.php b/web/html/logout.php index 3d059e7..2d8bebc 100644 --- a/web/html/logout.php +++ b/web/html/logout.php @@ -11,7 +11,7 @@ include_once("acctfuncs.inc.php"); # access AUR common functions # if (isset($_COOKIE["AURSID"])) { if (!isset($dbh)) { - $dbh = db_connect(); + $dbh = DB::connect(); } delete_session_id($_COOKIE["AURSID"], $dbh); # setting expiration to 1 means '1 second after midnight January 1, 1970' diff --git a/web/html/pkgsubmit.php b/web/html/pkgsubmit.php index 5dd58af..78fceac 100644 --- a/web/html/pkgsubmit.php +++ b/web/html/pkgsubmit.php @@ -356,7 +356,7 @@ if ($uid):
# Update the backend database if (!$error) { - $dbh = db_connect(); + $dbh = DB::connect(); begin_atomic_commit($dbh);
$pdata = pkgdetails_by_pkgname($new_pkgbuild['pkgname'], $dbh); diff --git a/web/lib/DB.class.php b/web/lib/DB.class.php new file mode 100644 index 0000000..0975989 --- /dev/null +++ b/web/lib/DB.class.php @@ -0,0 +1,28 @@ +<?php + +class DB { + + /** + * A database object + */ + private static $dbh = null; + + /** + * Return an already existing database object or newly instantiated object + * + * @return \PDO A database connection using PDO + */ + public static function connect() { + if (self::$dbh === null) { + try { + self::$dbh = new PDO(AUR_db_DSN_prefix . ":" . AUR_db_host + . ";dbname=" . AUR_db_name, AUR_db_user, AUR_db_pass); + self::$dbh->exec("SET NAMES 'utf8' COLLATE 'utf8_general_ci';"); + } catch (PDOException $e) { + die('Error - Could not connect to AUR database'); + } + } + + return self::$dbh; + } +} diff --git a/web/lib/acctfuncs.inc.php b/web/lib/acctfuncs.inc.php index 3759c63..1e25f62 100644 --- a/web/lib/acctfuncs.inc.php +++ b/web/lib/acctfuncs.inc.php @@ -95,7 +95,7 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", global $SUPPORTED_LANGS;
if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
if(isset($_COOKIE['AURSID'])) { @@ -301,7 +301,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 "; @@ -367,7 +367,7 @@ function search_results_page($UTYPE,$O=0,$SB="",$U="",$T="", $q.= "LIMIT " . $HITS_PER_PAGE . " OFFSET " . $OFFSET;
if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$result = $dbh->query($q); @@ -398,7 +398,7 @@ function try_login($dbh=NULL) {
if ( isset($_REQUEST['user']) || isset($_REQUEST['passwd']) ) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $userID = valid_user($_REQUEST['user'], $dbh);
@@ -522,7 +522,7 @@ function valid_user($user, $dbh=NULL) { /* if ( $user = valid_username($user) ) { */
if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
if ( $user ) { @@ -549,7 +549,7 @@ function valid_user($user, $dbh=NULL) { */ function open_user_proposals($user, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT * FROM TU_VoteInfo WHERE User = " . $dbh->quote($user) . " "; $q.= "AND End > UNIX_TIMESTAMP()"; @@ -575,7 +575,7 @@ function open_user_proposals($user, $dbh=NULL) { */ function add_tu_proposal($agenda, $user, $votelength, $submitteruid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "INSERT INTO TU_VoteInfo (Agenda, User, Submitted, End, SubmitterID) VALUES "; @@ -596,7 +596,7 @@ function add_tu_proposal($agenda, $user, $votelength, $submitteruid, $dbh=NULL) */ function create_resetkey($resetkey, $uid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "UPDATE Users "; $q.= "SET ResetKey = '" . $resetkey . "' "; @@ -617,7 +617,7 @@ function create_resetkey($resetkey, $uid, $dbh=NULL) { */ function password_reset($hash, $salt, $resetkey, $email, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "UPDATE Users "; $q.= "SET Passwd = '$hash', "; @@ -662,7 +662,7 @@ function good_passwd($passwd) { */ function valid_passwd($userID, $passwd, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } if ( strlen($passwd) > 0 ) { # get salt for this user @@ -724,7 +724,7 @@ function valid_pgp_fingerprint($fingerprint) { */ function user_suspended($id, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } if (!$id) { return false; @@ -750,7 +750,7 @@ function user_suspended($id, $dbh=NULL) { */ function user_delete($id, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "DELETE FROM Users WHERE ID = " . $id; $dbh->query($q); @@ -767,7 +767,7 @@ function user_delete($id, $dbh=NULL) { */ function user_is_privileged($id, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT AccountTypeID FROM Users WHERE ID = " . $id; $result = $dbh->query($q); @@ -791,7 +791,7 @@ function user_is_privileged($id, $dbh=NULL) { */ function delete_session_id($sid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "DELETE FROM Sessions WHERE SessionID = " . $dbh->quote($sid); @@ -808,7 +808,7 @@ function delete_session_id($sid, $dbh=NULL) { */ function delete_user_sessions($uid, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "DELETE FROM Sessions WHERE UsersID = " . intval($uid); @@ -827,7 +827,7 @@ function clear_expired_sessions($dbh=NULL) { global $LOGIN_TIMEOUT;
if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "DELETE FROM Sessions WHERE LastUpdateTS < (UNIX_TIMESTAMP() - $LOGIN_TIMEOUT)"; @@ -847,7 +847,7 @@ function clear_expired_sessions($dbh=NULL) { */ function account_details($uid, $username, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT Users.*, AccountTypes.AccountType "; $q.= "FROM Users, AccountTypes "; @@ -877,7 +877,7 @@ function account_details($uid, $username, $dbh=NULL) { */ function tu_voted($voteid, $uid, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "SELECT COUNT(*) FROM TU_Votes "; @@ -901,7 +901,7 @@ function tu_voted($voteid, $uid, $dbh=NULL) { */ function current_proposal_list($order, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "SELECT * FROM TU_VoteInfo WHERE End > " . time() . " ORDER BY Submitted " . $order; @@ -926,7 +926,7 @@ function current_proposal_list($order, $dbh=NULL) { */ function past_proposal_list($order, $lim, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "SELECT * FROM TU_VoteInfo WHERE End < " . time() . " ORDER BY Submitted " . $order . $lim; @@ -949,7 +949,7 @@ function past_proposal_list($order, $lim, $dbh=NULL) { */ function proposal_count($dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "SELECT COUNT(*) FROM TU_VoteInfo"; @@ -969,7 +969,7 @@ function proposal_count($dbh=NULL) { */ function vote_details($voteid, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "SELECT * FROM TU_VoteInfo "; @@ -991,7 +991,7 @@ function vote_details($voteid, $dbh=NULL) { */ function voter_list($voteid, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$whovoted = array(); @@ -1024,7 +1024,7 @@ function voter_list($voteid, $dbh=NULL) { */ function cast_proposal_vote($voteid, $uid, $vote, $newtotal, $dbh=NULL) { if (!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "UPDATE TU_VoteInfo SET " . $vote . " = (" . $newtotal . ") WHERE ID = " . $voteid; diff --git a/web/lib/aur.inc.php b/web/lib/aur.inc.php index d8c5cb4..9a84ec7 100644 --- a/web/lib/aur.inc.php +++ b/web/lib/aur.inc.php @@ -11,6 +11,7 @@ include_once('translator.inc.php'); set_lang();
I noticed a problem when the translator tries to change a logged-in user's language by connecting to the database. Added an include to the translator on my working branch to fix this.
include_once("config.inc.php"); +include_once("DB.class.php"); include_once("routing.inc.php"); include_once("version.inc.php"); include_once("acctfuncs.inc.php"); @@ -38,7 +39,7 @@ function check_sid($dbh=NULL) { # 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"]); @@ -145,7 +146,7 @@ function username_from_id($id="", $dbh=NULL) { return ""; } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT Username FROM Users WHERE ID = " . $dbh->quote($id); $result = $dbh->query($q); @@ -170,7 +171,7 @@ function username_from_sid($sid="", $dbh=NULL) { return ""; } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT Username "; $q.= "FROM Users, Sessions "; @@ -198,7 +199,7 @@ function email_from_sid($sid="", $dbh=NULL) { return ""; } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT Email "; $q.= "FROM Users, Sessions "; @@ -226,7 +227,7 @@ function account_from_sid($sid="", $dbh=NULL) { return ""; } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT AccountType "; $q.= "FROM Users, AccountTypes, Sessions "; @@ -255,7 +256,7 @@ function uid_from_sid($sid="", $dbh=NULL) { return ""; } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT Users.ID "; $q.= "FROM Users, Sessions "; @@ -271,24 +272,6 @@ function uid_from_sid($sid="", $dbh=NULL) { }
/** - * Establish a connection with a database using PDO - * - * @return \PDO A database connection - */ -function db_connect() { - try { - $dbh = new PDO(AUR_db_DSN_prefix . ":" . AUR_db_host . ";dbname=" . AUR_db_name, AUR_db_user, AUR_db_pass); - } - catch (PDOException $e) { - echo "Error - Could not connect to AUR database: " . $e->getMessage(); - } - - $dbh->exec("SET NAMES 'utf8' COLLATE 'utf8_general_ci';"); - - return $dbh; -} - -/** * Common AUR header displayed on all pages * * @global string $LANG Language selected by the visitor @@ -331,7 +314,7 @@ function html_footer($ver="") { function can_submit_pkg($name="", $sid="", $dbh=NULL) { if (!$name || !$sid) {return 0;} if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT MaintainerUID "; $q.= "FROM Packages WHERE Name = " . $dbh->quote($name); @@ -390,7 +373,7 @@ function uid_from_username($username="", $dbh=NULL) { return ""; } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT ID FROM Users WHERE Username = " . $dbh->quote($username); $result = $dbh->query($q); @@ -415,7 +398,7 @@ function uid_from_email($email="", $dbh=NULL) { return ""; } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT ID FROM Users WHERE Email = " . $dbh->quote($email); $result = $dbh->query($q); @@ -481,7 +464,7 @@ function mkurl($append) { */ function get_salt($user_id, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT Salt FROM Users WHERE ID = " . $user_id; $result = $dbh->query($q); @@ -501,7 +484,7 @@ function get_salt($user_id, $dbh=NULL) { */ function save_salt($user_id, $passwd, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $salt = generate_salt(); $hash = salted_hash($passwd, $salt); @@ -571,7 +554,7 @@ function parse_comment($comment) { */ function begin_atomic_commit($dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $dbh->beginTransaction(); } @@ -583,7 +566,7 @@ function begin_atomic_commit($dbh=NULL) { */ function end_atomic_commit($dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $dbh->commit(); } @@ -598,7 +581,7 @@ function end_atomic_commit($dbh=NULL) { */ function last_insert_id($dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } return $dbh->lastInsertId(); } @@ -613,7 +596,7 @@ function last_insert_id($dbh=NULL) { */ function latest_pkgs($numpkgs, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "SELECT * FROM Packages "; diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index 616b783..b905275 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -52,7 +52,7 @@ class AurJSON { // do the routing if ( in_array($http_data['type'], self::$exposed_methods) ) { // set up db connection. - $this->dbh = db_connect(); + $this->dbh = DB::connect();
// ugh. this works. I hate you php. $json = call_user_func(array(&$this, $http_data['type']), diff --git a/web/lib/pkgfuncs.inc.php b/web/lib/pkgfuncs.inc.php index 568ca3d..b29acb3 100644 --- a/web/lib/pkgfuncs.inc.php +++ b/web/lib/pkgfuncs.inc.php @@ -20,7 +20,7 @@ function canDeleteComment($comment_id=0, $atype="", $uid=0, $dbh=NULL) { return TRUE; } if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT COUNT(ID) AS CNT "; $q.= "FROM PackageComments "; @@ -89,7 +89,7 @@ function canSubmitBlacklisted($atype = "") { function pkgCategories($dbh=NULL) { $cats = array(); if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT * FROM PackageCategories WHERE ID != 1 "; $q.= "ORDER BY Category ASC"; @@ -113,7 +113,7 @@ function pkgCategories($dbh=NULL) { function pkgid_from_name($name="", $dbh=NULL) { if (!$name) {return NULL;} if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT ID FROM Packages "; $q.= "WHERE Name = " . $dbh->quote($name); @@ -138,7 +138,7 @@ function package_dependencies($pkgid, $dbh=NULL) { $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 "; @@ -167,7 +167,7 @@ function package_required($name="", $dbh=NULL) { $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 "; @@ -192,13 +192,13 @@ function package_required($name="", $dbh=NULL) { */ function package_comments_count($pkgid, $dbh=NULL) { 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; @@ -227,7 +227,7 @@ function package_comments($pkgid, $dbh=NULL) { $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 "; @@ -268,7 +268,7 @@ function add_package_comment($pkgid, $uid, $comment, $dbh=NULL) { global $AUR_LOCATION;
if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "INSERT INTO PackageComments "; @@ -326,7 +326,7 @@ function package_sources($pkgid, $dbh=NULL) { $pkgid = intval($pkgid); if ($pkgid > 0) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT Source FROM PackageSources "; $q.= "WHERE PackageID = " . $pkgid; @@ -354,7 +354,7 @@ function pkgvotes_from_sid($sid="", $dbh=NULL) { $pkgs = array(); if (!$sid) {return $pkgs;} if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT PackageID "; $q.= "FROM PackageVotes, Users, Sessions "; @@ -383,7 +383,7 @@ function pkgname_from_id($pkgids, $dbh=NULL) { $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) . ")"; @@ -397,7 +397,7 @@ function pkgname_from_id($pkgids, $dbh=NULL) { } elseif ($pkgids > 0) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT Name FROM Packages WHERE ID = " . $pkgids; $result = $dbh->query($q); @@ -421,7 +421,7 @@ function pkgname_from_id($pkgids, $dbh=NULL) { */ function pkgname_is_blacklisted($name, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT COUNT(*) FROM PackageBlacklist "; $q.= "WHERE Name = " . $dbh->quote($name); @@ -441,7 +441,7 @@ function pkgname_is_blacklisted($name, $dbh=NULL) { **/ function get_package_details($id=0, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "SELECT Packages.*,Category "; @@ -482,7 +482,7 @@ function display_package_details($id=0, $row, $SID="", $dbh=NULL) { global $USE_VIRTUAL_URLS;
if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
if (isset($row['error'])) { @@ -556,7 +556,7 @@ function display_package_details($id=0, $row, $SID="", $dbh=NULL) { */ function pkg_search_page($SID="", $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
// get commonly used variables... @@ -814,7 +814,7 @@ function pkg_flag($atype, $ids, $dbh=NULL) { }
if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "UPDATE Packages SET"; @@ -868,7 +868,7 @@ function pkg_unflag($atype, $ids, $dbh=NULL) { }
if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "UPDATE Packages SET "; @@ -911,7 +911,7 @@ function pkg_delete ($atype, $ids, $mergepkgid, $dbh=NULL) { }
if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
if ($mergepkgid) { @@ -1013,7 +1013,7 @@ function pkg_adopt ($atype, $ids, $action=true, $dbh=NULL) { }
if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$field = "MaintainerUID"; @@ -1073,7 +1073,7 @@ function pkg_vote ($atype, $ids, $action=true, $dbh=NULL) { }
if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $my_votes = pkgvotes_from_sid($_COOKIE["AURSID"], $dbh); $uid = uid_from_sid($_COOKIE["AURSID"], $dbh); @@ -1143,7 +1143,7 @@ function pkg_vote ($atype, $ids, $action=true, $dbh=NULL) { */ function getvotes($pkgid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "SELECT UsersID,Username FROM PackageVotes "; @@ -1175,7 +1175,7 @@ function getvotes($pkgid, $dbh=NULL) { */ function user_voted($uid, $pkgid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "SELECT * FROM PackageVotes WHERE UsersID = ". $dbh->quote($uid); @@ -1201,7 +1201,7 @@ function user_voted($uid, $pkgid, $dbh=NULL) { */ function user_notify($uid, $pkgid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); }
$q = "SELECT * FROM CommentNotify WHERE UserID = " . $dbh->quote($uid); @@ -1236,7 +1236,7 @@ function pkg_notify ($atype, $ids, $action=true, $dbh=NULL) { }
if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $uid = uid_from_sid($_COOKIE["AURSID"], $dbh);
@@ -1315,7 +1315,7 @@ function pkg_delete_comment($atype, $dbh=NULL) { }
if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $uid = uid_from_sid($_COOKIE["AURSID"], $dbh); if (canDeleteComment($comment_id, $atype, $uid, $dbh)) { @@ -1349,7 +1349,7 @@ function pkg_change_category($pid, $atype, $dbh=NULL) { }
if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $catArray = pkgCategories($dbh); if (!array_key_exists($category_id, $catArray)) { @@ -1391,7 +1391,7 @@ function pkg_change_category($pid, $atype, $dbh=NULL) { */ function pkgdetails_by_pkgname($pkgname, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT * FROM Packages WHERE Name = " . $dbh->quote($pkgname); $result = $dbh->query($q); @@ -1417,7 +1417,7 @@ function pkgdetails_by_pkgname($pkgname, $dbh=NULL) { */ function new_pkgdetails($pkgname, $license, $pkgver, $category_id, $pkgdesc, $pkgurl, $uid, $dbh=NULL) { 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)", $dbh->quote($pkgname), @@ -1448,7 +1448,7 @@ function new_pkgdetails($pkgname, $license, $pkgver, $category_id, $pkgdesc, $pk */ function update_pkgdetails($pkgname, $license, $pkgver, $pkgdesc, $pkgurl, $uid, $pkgid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $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", @@ -1475,7 +1475,7 @@ function update_pkgdetails($pkgname, $license, $pkgver, $pkgdesc, $pkgurl, $uid, */ function add_pkg_dep($pkgid, $depname, $depcondition, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = sprintf("INSERT INTO PackageDepends (PackageID, DepName, DepCondition) VALUES (%d, %s, %s)", $pkgid, @@ -1496,7 +1496,7 @@ function add_pkg_dep($pkgid, $depname, $depcondition, $dbh=NULL) { */ function add_pkg_src($pkgid, $pkgsrc, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "INSERT INTO PackageSources (PackageID, Source) VALUES ("; $q .= $pkgid . ", " . $dbh->quote($pkgsrc) . ")"; @@ -1515,7 +1515,7 @@ function add_pkg_src($pkgid, $pkgsrc, $dbh=NULL) { */ function update_pkg_category($pkgid, $category_id, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = sprintf( "UPDATE Packages SET CategoryID = %d WHERE ID = %d", $category_id, @@ -1534,7 +1534,7 @@ function update_pkg_category($pkgid, $category_id, $dbh=NULL) { */ function remove_pkg_deps($pkgid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "DELETE FROM PackageDepends WHERE PackageID = " . $pkgid;
@@ -1551,7 +1551,7 @@ function remove_pkg_deps($pkgid, $dbh=NULL) { */ function remove_pkg_sources($pkgid, $dbh=NULL) { if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "DELETE FROM PackageSources WHERE PackageID = " . $pkgid;
diff --git a/web/lib/translator.inc.php b/web/lib/translator.inc.php index 1477b17..ec8cb58 100644 --- a/web/lib/translator.inc.php +++ b/web/lib/translator.inc.php @@ -91,7 +91,7 @@ function set_lang($dbh=NULL) { # No language but a session; use default lang preference # if(!$dbh) { - $dbh = db_connect(); + $dbh = DB::connect(); } $q = "SELECT LangPreference FROM Users, Sessions "; $q.= "WHERE Users.ID = Sessions.UsersID "; -- 1.8.1.2
All functions now have a database connection method that will use the same database connection. This imitates the functionality of passing a database connection as an argument and makes it redundant. Signed-off-by: canyonknight <canyonknight@gmail.com> --- web/html/home.php | 6 +-- web/html/logout.php | 4 +- web/html/pkgsubmit.php | 22 ++++----- web/lib/acctfuncs.inc.php | 58 ++++++++++++------------ web/lib/aur.inc.php | 34 +++++++------- web/lib/cachefuncs.inc.php | 3 +- web/lib/pkgfuncs.inc.php | 108 ++++++++++++++++++++++----------------------- web/lib/stats.inc.php | 28 ++++++------ web/lib/translator.inc.php | 2 +- 9 files changed, 133 insertions(+), 132 deletions(-) diff --git a/web/html/home.php b/web/html/home.php index a10ebf0..8fccc7f 100644 --- a/web/html/home.php +++ b/web/html/home.php @@ -80,7 +80,7 @@ $dbh = DB::connect(); <td class="pkg-name"> <?php $userid = uid_from_sid($_COOKIE["AURSID"]); - user_table($userid, $dbh); + user_table($userid); ?> </td> </tr> @@ -100,10 +100,10 @@ $dbh = DB::connect(); </form> </div> <div id="pkg-updates" class="widget box"> - <?php updates_table($dbh); ?> + <?php updates_table(); ?> </div> <div id="pkg-stats" class="widget box"> - <?php general_stats_table($dbh); ?> + <?php general_stats_table(); ?> </div> </div> diff --git a/web/html/logout.php b/web/html/logout.php index 2d8bebc..6c98290 100644 --- a/web/html/logout.php +++ b/web/html/logout.php @@ -13,11 +13,11 @@ if (isset($_COOKIE["AURSID"])) { if (!isset($dbh)) { $dbh = DB::connect(); } - delete_session_id($_COOKIE["AURSID"], $dbh); + 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); unset($_COOKIE['AURSID']); - clear_expired_sessions($dbh); + clear_expired_sessions(); } header('Location: /'); diff --git a/web/html/pkgsubmit.php b/web/html/pkgsubmit.php index 78fceac..12203c4 100644 --- a/web/html/pkgsubmit.php +++ b/web/html/pkgsubmit.php @@ -357,9 +357,9 @@ if ($uid): # Update the backend database if (!$error) { $dbh = DB::connect(); - begin_atomic_commit($dbh); + begin_atomic_commit(); - $pdata = pkgdetails_by_pkgname($new_pkgbuild['pkgname'], $dbh); + $pdata = pkgdetails_by_pkgname($new_pkgbuild['pkgname']); # Check the category to use, "1" meaning "none" (or "keep category" for # existing packages). @@ -380,8 +380,8 @@ if ($uid): $packageID = $pdata["ID"]; # Flush out old data that will be replaced with new data - remove_pkg_deps($packageID, $dbh); - remove_pkg_sources($packageID, $dbh); + remove_pkg_deps($packageID); + remove_pkg_sources($packageID); # If a new category was chosen, change it to that if ($category_id > 1) { @@ -389,11 +389,11 @@ if ($uid): } # Update package data - update_pkgdetails($new_pkgbuild['pkgname'], $new_pkgbuild['license'], $pkg_version, $new_pkgbuild['pkgdesc'], $new_pkgbuild['url'], $uid, $packageID, $dbh); + update_pkgdetails($new_pkgbuild['pkgname'], $new_pkgbuild['license'], $pkg_version, $new_pkgbuild['pkgdesc'], $new_pkgbuild['url'], $uid, $packageID); } else { # This is a brand new package - new_pkgdetails($new_pkgbuild['pkgname'], $new_pkgbuild['license'], $pkg_version, $category_id, $new_pkgbuild['pkgdesc'], $new_pkgbuild['url'], $uid, $dbh); - $packageID = last_insert_id($dbh); + new_pkgdetails($new_pkgbuild['pkgname'], $new_pkgbuild['license'], $pkg_version, $category_id, $new_pkgbuild['pkgdesc'], $new_pkgbuild['url'], $uid); + $packageID = last_insert_id(); } @@ -410,7 +410,7 @@ if ($uid): else if ($deppkgname == "#") { break; } - add_pkg_dep($packageID, $deppkgname, $depcondition, $dbh); + add_pkg_dep($packageID, $deppkgname, $depcondition); } } @@ -418,18 +418,18 @@ if ($uid): if (!empty($new_pkgbuild['source'])) { $sources = explode(" ", $new_pkgbuild['source']); foreach ($sources as $src) { - add_pkg_src($packageID, $src, $dbh); + add_pkg_src($packageID, $src); } } # If we just created this package, or it was an orphan and we # auto-adopted, add submitting user to the notification list. if (!$pdata || $pdata["MaintainerUID"] === NULL) { - pkg_notify(account_from_sid($_COOKIE["AURSID"], $dbh), array($packageID), true, $dbh); + pkg_notify(account_from_sid($_COOKIE["AURSID"]), array($packageID), true); } # Entire package creation process is atomic - end_atomic_commit($dbh); + end_atomic_commit(); header('Location: ' . get_pkg_uri($pkg_name)); } diff --git a/web/lib/acctfuncs.inc.php b/web/lib/acctfuncs.inc.php index 1e25f62..fe13b49 100644 --- a/web/lib/acctfuncs.inc.php +++ b/web/lib/acctfuncs.inc.php @@ -89,7 +89,7 @@ function display_account_form($UTYPE,$A,$U="",$T="",$S="", * @return string|void Return void if successful, otherwise return error */ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", - $P="",$C="",$R="",$L="",$I="",$K="",$UID=0,$dbh=NULL) { + $P="",$C="",$R="",$L="",$I="",$K="",$UID=0) { # error check and process request for a new/modified account global $SUPPORTED_LANGS; @@ -99,7 +99,7 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", } if(isset($_COOKIE['AURSID'])) { - $editor_user = uid_from_sid($_COOKIE['AURSID'], $dbh); + $editor_user = uid_from_sid($_COOKIE['AURSID']); } else { $editor_user = null; @@ -122,7 +122,7 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", } } - if (!$error && !valid_username($U) && !user_is_privileged($editor_user, $dbh)) + if (!$error && !valid_username($U) && !user_is_privileged($editor_user)) $error = __("The username is invalid.") . "<ul>\n" ."<li>" . __("It must be between %s and %s characters long", USERNAME_MIN_LEN, USERNAME_MAX_LEN ) @@ -230,7 +230,7 @@ function process_account_form($UTYPE,$TYPE,$A,$U="",$T="",$S="",$E="", } if ($S) { /* Ensure suspended users can't keep an active session */ - delete_user_sessions($UID, $dbh); + delete_user_sessions($UID); $q.= ", Suspended = 1"; } else { $q.= ", Suspended = 0"; @@ -287,7 +287,7 @@ function search_accounts_form() { * @return void */ function search_results_page($UTYPE,$O=0,$SB="",$U="",$T="", - $S="",$E="",$R="",$I="",$K="",$dbh=NULL) { + $S="",$E="",$R="",$I="",$K="") { $HITS_PER_PAGE = 50; if ($O) { @@ -389,7 +389,7 @@ function search_results_page($UTYPE,$O=0,$SB="",$U="",$T="", * * @return array Session ID for user, error message if applicable */ -function try_login($dbh=NULL) { +function try_login() { global $MAX_SESSIONS_PER_USER, $PERSISTENT_COOKIE_TIMEOUT; $login_error = ""; @@ -400,13 +400,13 @@ function try_login($dbh=NULL) { if (!$dbh) { $dbh = DB::connect(); } - $userID = valid_user($_REQUEST['user'], $dbh); + $userID = valid_user($_REQUEST['user']); - if ( user_suspended($userID, $dbh) ) { + if ( user_suspended($userID) ) { $login_error = "Account Suspended."; } elseif ( $userID && isset($_REQUEST['passwd']) - && valid_passwd($userID, $_REQUEST['passwd'], $dbh) ) { + && valid_passwd($userID, $_REQUEST['passwd']) ) { $logged_in = 0; $num_tries = 0; @@ -518,7 +518,7 @@ function valid_username($user) { * * @return string|void Return user ID if in database, otherwise void */ -function valid_user($user, $dbh=NULL) { +function valid_user($user) { /* if ( $user = valid_username($user) ) { */ if(!$dbh) { @@ -547,7 +547,7 @@ function valid_user($user, $dbh=NULL) { * * @return bool True if there is an open proposal about the user, otherwise false */ -function open_user_proposals($user, $dbh=NULL) { +function open_user_proposals($user) { if(!$dbh) { $dbh = DB::connect(); } @@ -573,7 +573,7 @@ function open_user_proposals($user, $dbh=NULL) { * * @return void */ -function add_tu_proposal($agenda, $user, $votelength, $submitteruid, $dbh=NULL) { +function add_tu_proposal($agenda, $user, $votelength, $submitteruid) { if(!$dbh) { $dbh = DB::connect(); } @@ -594,7 +594,7 @@ function add_tu_proposal($agenda, $user, $votelength, $submitteruid, $dbh=NULL) * * @return void */ -function create_resetkey($resetkey, $uid, $dbh=NULL) { +function create_resetkey($resetkey, $uid) { if(!$dbh) { $dbh = DB::connect(); } @@ -615,7 +615,7 @@ function create_resetkey($resetkey, $uid, $dbh=NULL) { * * @return string|void Redirect page if successful, otherwise return error message */ -function password_reset($hash, $salt, $resetkey, $email, $dbh=NULL) { +function password_reset($hash, $salt, $resetkey, $email) { if(!$dbh) { $dbh = DB::connect(); } @@ -660,7 +660,7 @@ function good_passwd($passwd) { * * @return bool True if password was correct and properly salted, otherwise false */ -function valid_passwd($userID, $passwd, $dbh=NULL) { +function valid_passwd($userID, $passwd) { if (!$dbh) { $dbh = DB::connect(); } @@ -722,7 +722,7 @@ function valid_pgp_fingerprint($fingerprint) { * * @return bool True if the user is suspended, otherwise false */ -function user_suspended($id, $dbh=NULL) { +function user_suspended($id) { if (!$dbh) { $dbh = DB::connect(); } @@ -748,7 +748,7 @@ function user_suspended($id, $dbh=NULL) { * * @return void */ -function user_delete($id, $dbh=NULL) { +function user_delete($id) { if (!$dbh) { $dbh = DB::connect(); } @@ -765,7 +765,7 @@ function user_delete($id, $dbh=NULL) { * * @return int|string Return 0 if un-privileged, "2" if Trusted User, "3" if Developer */ -function user_is_privileged($id, $dbh=NULL) { +function user_is_privileged($id) { if (!$dbh) { $dbh = DB::connect(); } @@ -789,7 +789,7 @@ function user_is_privileged($id, $dbh=NULL) { * * @return void */ -function delete_session_id($sid, $dbh=NULL) { +function delete_session_id($sid) { if(!$dbh) { $dbh = DB::connect(); } @@ -806,7 +806,7 @@ function delete_session_id($sid, $dbh=NULL) { * * @return void */ -function delete_user_sessions($uid, $dbh=NULL) { +function delete_user_sessions($uid) { if (!$dbh) { $dbh = DB::connect(); } @@ -823,7 +823,7 @@ function delete_user_sessions($uid, $dbh=NULL) { * * @return void */ -function clear_expired_sessions($dbh=NULL) { +function clear_expired_sessions() { global $LOGIN_TIMEOUT; if(!$dbh) { @@ -845,7 +845,7 @@ function clear_expired_sessions($dbh=NULL) { * * @return array Account details for the specified user */ -function account_details($uid, $username, $dbh=NULL) { +function account_details($uid, $username) { if(!$dbh) { $dbh = DB::connect(); } @@ -875,7 +875,7 @@ function account_details($uid, $username, $dbh=NULL) { * * @return bool True if the user has already voted, otherwise false */ -function tu_voted($voteid, $uid, $dbh=NULL) { +function tu_voted($voteid, $uid) { if (!$dbh) { $dbh = DB::connect(); } @@ -899,7 +899,7 @@ function tu_voted($voteid, $uid, $dbh=NULL) { * * @return array The details for all current Trusted User proposals */ -function current_proposal_list($order, $dbh=NULL) { +function current_proposal_list($order) { if (!$dbh) { $dbh = DB::connect(); } @@ -924,7 +924,7 @@ function current_proposal_list($order, $dbh=NULL) { * * @return array The details for the subset of past Trusted User proposals */ -function past_proposal_list($order, $lim, $dbh=NULL) { +function past_proposal_list($order, $lim) { if (!$dbh) { $dbh = DB::connect(); } @@ -947,7 +947,7 @@ function past_proposal_list($order, $lim, $dbh=NULL) { * * @return string The total number of Trusted User proposals */ -function proposal_count($dbh=NULL) { +function proposal_count() { if (!$dbh) { $dbh = DB::connect(); } @@ -967,7 +967,7 @@ function proposal_count($dbh=NULL) { * * @return array All stored details for a specific vote */ -function vote_details($voteid, $dbh=NULL) { +function vote_details($voteid) { if (!$dbh) { $dbh = DB::connect(); } @@ -989,7 +989,7 @@ function vote_details($voteid, $dbh=NULL) { * * @return array All users who voted for a specific proposal */ -function voter_list($voteid, $dbh=NULL) { +function voter_list($voteid) { if (!$dbh) { $dbh = DB::connect(); } @@ -1022,7 +1022,7 @@ function voter_list($voteid, $dbh=NULL) { * * @return void */ -function cast_proposal_vote($voteid, $uid, $vote, $newtotal, $dbh=NULL) { +function cast_proposal_vote($voteid, $uid, $vote, $newtotal) { if (!$dbh) { $dbh = DB::connect(); } diff --git a/web/lib/aur.inc.php b/web/lib/aur.inc.php index 9a84ec7..77a84e4 100644 --- a/web/lib/aur.inc.php +++ b/web/lib/aur.inc.php @@ -30,7 +30,7 @@ include_once("cachefuncs.inc.php"); * * @return void */ -function check_sid($dbh=NULL) { +function check_sid() { global $_COOKIE; global $LOGIN_TIMEOUT; @@ -66,7 +66,7 @@ function check_sid($dbh=NULL) { } elseif ($failed == 2) { # session id timeout was reached and they must login again. # - delete_session_id($_COOKIE["AURSID"], $dbh); + delete_session_id($_COOKIE["AURSID"]); setcookie("AURSID", "", 1, "/", null, !empty($_SERVER['HTTPS']), true); unset($_COOKIE['AURSID']); @@ -141,7 +141,7 @@ function new_sid() { * * @return string Username if it exists, otherwise "None" */ -function username_from_id($id="", $dbh=NULL) { +function username_from_id($id="") { if (!$id) { return ""; } @@ -166,7 +166,7 @@ function username_from_id($id="", $dbh=NULL) { * * @return string Username of the visitor */ -function username_from_sid($sid="", $dbh=NULL) { +function username_from_sid($sid="") { if (!$sid) { return ""; } @@ -194,7 +194,7 @@ function username_from_sid($sid="", $dbh=NULL) { * * @return string User's e-mail address as given during registration */ -function email_from_sid($sid="", $dbh=NULL) { +function email_from_sid($sid="") { if (!$sid) { return ""; } @@ -222,7 +222,7 @@ function email_from_sid($sid="", $dbh=NULL) { * * @return string Account type of user ("User", "Trusted User", or "Developer") */ -function account_from_sid($sid="", $dbh=NULL) { +function account_from_sid($sid="") { if (!$sid) { return ""; } @@ -251,7 +251,7 @@ function account_from_sid($sid="", $dbh=NULL) { * * @return string|int The user's name, 0 on query failure */ -function uid_from_sid($sid="", $dbh=NULL) { +function uid_from_sid($sid="") { if (!$sid) { return ""; } @@ -311,7 +311,7 @@ function html_footer($ver="") { * * @return int 0 if the user can't submit, 1 if the user can submit */ -function can_submit_pkg($name="", $sid="", $dbh=NULL) { +function can_submit_pkg($name="", $sid="") { if (!$name || !$sid) {return 0;} if(!$dbh) { $dbh = DB::connect(); @@ -324,7 +324,7 @@ function can_submit_pkg($name="", $sid="", $dbh=NULL) { if (!$row[0]) { return 1; } - $my_uid = uid_from_sid($sid, $dbh); + $my_uid = uid_from_sid($sid); if ($row[0] === NULL || $row[0] == $my_uid) { return 1; @@ -368,7 +368,7 @@ function rm_tree($dirname) { * * @return string Return user ID if exists for username, otherwise "None" */ -function uid_from_username($username="", $dbh=NULL) { +function uid_from_username($username="") { if (!$username) { return ""; } @@ -393,7 +393,7 @@ function uid_from_username($username="", $dbh=NULL) { * * @return string The user's ID */ -function uid_from_email($email="", $dbh=NULL) { +function uid_from_email($email="") { if (!$email) { return ""; } @@ -462,7 +462,7 @@ function mkurl($append) { * * @return string|void Return the salt for the requested user, otherwise void */ -function get_salt($user_id, $dbh=NULL) { +function get_salt($user_id) { if(!$dbh) { $dbh = DB::connect(); } @@ -482,7 +482,7 @@ function get_salt($user_id, $dbh=NULL) { * @param string $passwd The password of the user logging in * @param \PDO $dbh Already established database connection */ -function save_salt($user_id, $passwd, $dbh=NULL) { +function save_salt($user_id, $passwd) { if(!$dbh) { $dbh = DB::connect(); } @@ -552,7 +552,7 @@ function parse_comment($comment) { * * @param \PDO $dbh Already established database connection */ -function begin_atomic_commit($dbh=NULL) { +function begin_atomic_commit() { if(!$dbh) { $dbh = DB::connect(); } @@ -564,7 +564,7 @@ function begin_atomic_commit($dbh=NULL) { * * @param \PDO $dbh Already established database connection */ -function end_atomic_commit($dbh=NULL) { +function end_atomic_commit() { if(!$dbh) { $dbh = DB::connect(); } @@ -579,7 +579,7 @@ function end_atomic_commit($dbh=NULL) { * * @return string The ID of the last inserted row */ -function last_insert_id($dbh=NULL) { +function last_insert_id() { if(!$dbh) { $dbh = DB::connect(); } @@ -594,7 +594,7 @@ function last_insert_id($dbh=NULL) { * * @return array $packages Package info for the specified number of recent packages */ -function latest_pkgs($numpkgs, $dbh=NULL) { +function latest_pkgs($numpkgs) { if(!$dbh) { $dbh = DB::connect(); } diff --git a/web/lib/cachefuncs.inc.php b/web/lib/cachefuncs.inc.php index f109bcf..d558be4 100644 --- a/web/lib/cachefuncs.inc.php +++ b/web/lib/cachefuncs.inc.php @@ -67,7 +67,8 @@ function get_cache_value($key, &$status=false) { # Run a simple db query, retrieving and/or caching the value if APC is # available for use. Accepts an optional TTL value (defaults to 600 seconds). -function db_cache_value($dbq, $dbh, $key, $ttl=600) { +function db_cache_value($dbq, $key, $ttl=600) { + $dbh = DB::connect(); $status = false; $value = get_cache_value($key, $status); if (!$status) { diff --git a/web/lib/pkgfuncs.inc.php b/web/lib/pkgfuncs.inc.php index b29acb3..21fc37c 100644 --- a/web/lib/pkgfuncs.inc.php +++ b/web/lib/pkgfuncs.inc.php @@ -14,7 +14,7 @@ include_once("config.inc.php"); * * @return bool True if the user can delete the comment, otherwise false */ -function canDeleteComment($comment_id=0, $atype="", $uid=0, $dbh=NULL) { +function canDeleteComment($comment_id=0, $atype="", $uid=0) { if ($atype == "Trusted User" || $atype == "Developer") { # A TU/Dev can delete any comment return TRUE; @@ -86,7 +86,7 @@ function canSubmitBlacklisted($atype = "") { * * @return array All package categories */ -function pkgCategories($dbh=NULL) { +function pkgCategories() { $cats = array(); if(!$dbh) { $dbh = DB::connect(); @@ -110,7 +110,7 @@ function pkgCategories($dbh=NULL) { * * @return string|void Package name if it already exists */ -function pkgid_from_name($name="", $dbh=NULL) { +function pkgid_from_name($name="") { if (!$name) {return NULL;} if(!$dbh) { $dbh = DB::connect(); @@ -133,7 +133,7 @@ function pkgid_from_name($name="", $dbh=NULL) { * * @return array All package dependencies for the package */ -function package_dependencies($pkgid, $dbh=NULL) { +function package_dependencies($pkgid) { $deps = array(); $pkgid = intval($pkgid); if ($pkgid > 0) { @@ -163,7 +163,7 @@ function package_dependencies($pkgid, $dbh=NULL) { * * @return array All packages that depend on the specified package name */ -function package_required($name="", $dbh=NULL) { +function package_required($name="") { $deps = array(); if ($name != "") { if(!$dbh) { @@ -190,7 +190,7 @@ function package_required($name="", $dbh=NULL) { * * @return string The number of comments left for a specific package */ -function package_comments_count($pkgid, $dbh=NULL) { +function package_comments_count($pkgid) { if (!$dbh) { $dbh = DB::connect(); } @@ -222,7 +222,7 @@ function package_comments_count($pkgid, $dbh=NULL) { * * @return array All package comment information for a specific package */ -function package_comments($pkgid, $dbh=NULL) { +function package_comments($pkgid) { $comments = array(); $pkgid = intval($pkgid); if ($pkgid > 0) { @@ -264,7 +264,7 @@ function package_comments($pkgid, $dbh=NULL) { * * @return void */ -function add_package_comment($pkgid, $uid, $comment, $dbh=NULL) { +function add_package_comment($pkgid, $uid, $comment) { global $AUR_LOCATION; if(!$dbh) { @@ -303,7 +303,7 @@ function add_package_comment($pkgid, $uid, $comment, $dbh=NULL) { # getting emails in the language that the user who posted the comment was in $body = 'from ' . $AUR_LOCATION . get_pkg_uri($row['Name']) . "\n" - . username_from_sid($_COOKIE['AURSID'], $dbh) . " wrote:\n\n" + . username_from_sid($_COOKIE['AURSID']) . " 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); @@ -321,7 +321,7 @@ function add_package_comment($pkgid, $uid, $comment, $dbh=NULL) { * * @return array All sources associated with a specific package */ -function package_sources($pkgid, $dbh=NULL) { +function package_sources($pkgid) { $sources = array(); $pkgid = intval($pkgid); if ($pkgid > 0) { @@ -350,7 +350,7 @@ function package_sources($pkgid, $dbh=NULL) { * * @return array All packages the visitor has voted for */ -function pkgvotes_from_sid($sid="", $dbh=NULL) { +function pkgvotes_from_sid($sid="") { $pkgs = array(); if (!$sid) {return $pkgs;} if(!$dbh) { @@ -378,7 +378,7 @@ function pkgvotes_from_sid($sid="", $dbh=NULL) { * * @return array|string All names if multiple package IDs, otherwise package name */ -function pkgname_from_id($pkgids, $dbh=NULL) { +function pkgname_from_id($pkgids) { if (is_array($pkgids)) { $pkgids = sanitize_ids($pkgids); $names = array(); @@ -419,7 +419,7 @@ function pkgname_from_id($pkgids, $dbh=NULL) { * * @return bool True if the name is blacklisted, otherwise false */ -function pkgname_is_blacklisted($name, $dbh=NULL) { +function pkgname_is_blacklisted($name) { if(!$dbh) { $dbh = DB::connect(); } @@ -439,7 +439,7 @@ function pkgname_is_blacklisted($name, $dbh=NULL) { * * @return array The package's details OR error message **/ -function get_package_details($id=0, $dbh=NULL) { +function get_package_details($id=0) { if(!$dbh) { $dbh = DB::connect(); } @@ -477,7 +477,7 @@ function get_package_details($id=0, $dbh=NULL) { * * @return void */ -function display_package_details($id=0, $row, $SID="", $dbh=NULL) { +function display_package_details($id=0, $row, $SID="") { global $AUR_LOCATION; global $USE_VIRTUAL_URLS; @@ -495,14 +495,14 @@ function display_package_details($id=0, $row, $SID="", $dbh=NULL) { 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); + $uid = uid_from_sid($SID); + add_package_comment($id, $uid, $_REQUEST['comment']); } include('pkg_comment_form.php'); } # Print Comments - $comments = package_comments($id, $dbh); + $comments = package_comments($id); if (!empty($comments)) { include('pkg_comments.php'); } @@ -554,7 +554,7 @@ function display_package_details($id=0, $row, $SID="", $dbh=NULL) { * do_Notify - Enable notification * do_UnNotify - Disable notification */ -function pkg_search_page($SID="", $dbh=NULL) { +function pkg_search_page($SID="") { if(!$dbh) { $dbh = DB::connect(); } @@ -563,7 +563,7 @@ function pkg_search_page($SID="", $dbh=NULL) { // TODO: REDUCE DB HITS. // grab info for user if they're logged in if ($SID) - $myuid = uid_from_sid($SID, $dbh); + $myuid = uid_from_sid($SID); // get a list of package categories $cats = pkgCategories($dbh); //meow @@ -633,7 +633,7 @@ function pkg_search_page($SID="", $dbh=NULL) { } # Search by submitter elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "s") { - $q_where .= "AND SubmitterUID = ".uid_from_username($_GET['K'], $dbh)." "; + $q_where .= "AND SubmitterUID = ".uid_from_username($_GET['K'])." "; } # Search by name elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "n") { @@ -801,7 +801,7 @@ function sanitize_ids($ids) { * * @return string Translated success or error messages */ -function pkg_flag($atype, $ids, $dbh=NULL) { +function pkg_flag($atype, $ids) { global $AUR_LOCATION; if (!$atype) { @@ -826,9 +826,9 @@ function pkg_flag($atype, $ids, $dbh=NULL) { if ($affected_pkgs > 0) { # Notify of flagging by email - $f_name = username_from_sid($_COOKIE['AURSID'], $dbh); - $f_email = email_from_sid($_COOKIE['AURSID'], $dbh); - $f_uid = uid_from_sid($_COOKIE['AURSID'], $dbh); + $f_name = username_from_sid($_COOKIE['AURSID']); + $f_email = email_from_sid($_COOKIE['AURSID']); + $f_uid = uid_from_sid($_COOKIE['AURSID']); $q = "SELECT Packages.Name, Users.Email, Packages.ID "; $q.= "FROM Packages, Users "; $q.= "WHERE Packages.ID IN (" . implode(",", $ids) .") "; @@ -857,7 +857,7 @@ function pkg_flag($atype, $ids, $dbh=NULL) { * * @return string Translated success or error messages */ -function pkg_unflag($atype, $ids, $dbh=NULL) { +function pkg_unflag($atype, $ids) { if (!$atype) { return __("You must be logged in before you can unflag packages."); } @@ -876,7 +876,7 @@ function pkg_unflag($atype, $ids, $dbh=NULL) { $q.= "WHERE ID IN (" . implode(",", $ids) . ") "; if ($atype != "Trusted User" && $atype != "Developer") { - $q.= "AND MaintainerUID = " . uid_from_sid($_COOKIE["AURSID"], $dbh); + $q.= "AND MaintainerUID = " . uid_from_sid($_COOKIE["AURSID"]); } $result = $dbh->exec($q); @@ -895,7 +895,7 @@ function pkg_unflag($atype, $ids, $dbh=NULL) { * * @return string Translated error or success message */ -function pkg_delete ($atype, $ids, $mergepkgid, $dbh=NULL) { +function pkg_delete ($atype, $ids, $mergepkgid) { if (!$atype) { return __("You must be logged in before you can delete packages."); } @@ -915,7 +915,7 @@ function pkg_delete ($atype, $ids, $mergepkgid, $dbh=NULL) { } if ($mergepkgid) { - $mergepkgname = pkgname_from_id($mergepkgid, $dbh); + $mergepkgname = pkgname_from_id($mergepkgid); } # Send email notifications @@ -994,7 +994,7 @@ function pkg_delete ($atype, $ids, $mergepkgid, $dbh=NULL) { * * @return string Translated error or success message */ -function pkg_adopt ($atype, $ids, $action=true, $dbh=NULL) { +function pkg_adopt ($atype, $ids, $action=true) { if (!$atype) { if ($action) { return __("You must be logged in before you can adopt packages."); @@ -1020,7 +1020,7 @@ function pkg_adopt ($atype, $ids, $action=true, $dbh=NULL) { $q = "UPDATE Packages "; if ($action) { - $user = uid_from_sid($_COOKIE["AURSID"], $dbh); + $user = uid_from_sid($_COOKIE["AURSID"]); } else { $user = 'NULL'; } @@ -1032,13 +1032,13 @@ function pkg_adopt ($atype, $ids, $action=true, $dbh=NULL) { # Regular users may only adopt orphan packages from unsupported $q.= "AND $field IS NULL "; } else if ($atype == "User") { - $q.= "AND $field = " . uid_from_sid($_COOKIE["AURSID"], $dbh); + $q.= "AND $field = " . uid_from_sid($_COOKIE["AURSID"]); } $dbh->exec($q); if ($action) { - pkg_notify(account_from_sid($_COOKIE["AURSID"], $dbh), $ids, $dbh); + pkg_notify(account_from_sid($_COOKIE["AURSID"]), $ids); return __("The selected packages have been adopted."); } else { return __("The selected packages have been disowned."); @@ -1054,7 +1054,7 @@ function pkg_adopt ($atype, $ids, $action=true, $dbh=NULL) { * * @return string Translated error or success message */ -function pkg_vote ($atype, $ids, $action=true, $dbh=NULL) { +function pkg_vote ($atype, $ids, $action=true) { if (!$atype) { if ($action) { return __("You must be logged in before you can vote for packages."); @@ -1075,8 +1075,8 @@ function pkg_vote ($atype, $ids, $action=true, $dbh=NULL) { if(!$dbh) { $dbh = DB::connect(); } - $my_votes = pkgvotes_from_sid($_COOKIE["AURSID"], $dbh); - $uid = uid_from_sid($_COOKIE["AURSID"], $dbh); + $my_votes = pkgvotes_from_sid($_COOKIE["AURSID"]); + $uid = uid_from_sid($_COOKIE["AURSID"]); $first = 1; foreach ($ids as $pid) { @@ -1141,7 +1141,7 @@ function pkg_vote ($atype, $ids, $action=true, $dbh=NULL) { * * @return array User IDs and usernames that voted for a specific package */ -function getvotes($pkgid, $dbh=NULL) { +function getvotes($pkgid) { if(!$dbh) { $dbh = DB::connect(); } @@ -1173,7 +1173,7 @@ function getvotes($pkgid, $dbh=NULL) { * * @return bool True if the user has already voted, otherwise false */ -function user_voted($uid, $pkgid, $dbh=NULL) { +function user_voted($uid, $pkgid) { if(!$dbh) { $dbh = DB::connect(); } @@ -1199,7 +1199,7 @@ function user_voted($uid, $pkgid, $dbh=NULL) { * * @return bool True if the user wants notifications, otherwise false */ -function user_notify($uid, $pkgid, $dbh=NULL) { +function user_notify($uid, $pkgid) { if(!$dbh) { $dbh = DB::connect(); } @@ -1224,7 +1224,7 @@ function user_notify($uid, $pkgid, $dbh=NULL) { * * @return string Translated error or success message */ -function pkg_notify ($atype, $ids, $action=true, $dbh=NULL) { +function pkg_notify ($atype, $ids, $action=true) { if (!$atype) { # return __("You must be logged in before you can get notifications on comments."); return; @@ -1238,7 +1238,7 @@ function pkg_notify ($atype, $ids, $action=true, $dbh=NULL) { if(!$dbh) { $dbh = DB::connect(); } - $uid = uid_from_sid($_COOKIE["AURSID"], $dbh); + $uid = uid_from_sid($_COOKIE["AURSID"]); $output = ""; @@ -1302,7 +1302,7 @@ function pkg_notify ($atype, $ids, $action=true, $dbh=NULL) { * * @return string Translated error or success message */ -function pkg_delete_comment($atype, $dbh=NULL) { +function pkg_delete_comment($atype) { if (!$atype) { return __("You must be logged in before you can edit package information."); } @@ -1317,8 +1317,8 @@ function pkg_delete_comment($atype, $dbh=NULL) { if(!$dbh) { $dbh = DB::connect(); } - $uid = uid_from_sid($_COOKIE["AURSID"], $dbh); - if (canDeleteComment($comment_id, $atype, $uid, $dbh)) { + $uid = uid_from_sid($_COOKIE["AURSID"]); + if (canDeleteComment($comment_id, $atype, $uid)) { $q = "UPDATE PackageComments "; $q.= "SET DelUsersID = ".$uid." "; $q.= "WHERE ID = ".intval($comment_id); @@ -1336,7 +1336,7 @@ function pkg_delete_comment($atype, $dbh=NULL) { * * @return string Translated error or success message */ -function pkg_change_category($pid, $atype, $dbh=NULL) { +function pkg_change_category($pid, $atype) { if (!$atype) { return __("You must be logged in before you can edit package information."); } @@ -1368,7 +1368,7 @@ function pkg_change_category($pid, $atype, $dbh=NULL) { return __("You are not allowed to change this package category."); } - $uid = uid_from_sid($_COOKIE["AURSID"], $dbh); + $uid = uid_from_sid($_COOKIE["AURSID"]); if ($uid == $row["MaintainerUID"] || ($atype == "Developer" || $atype == "Trusted User")) { $q = "UPDATE Packages "; @@ -1389,7 +1389,7 @@ function pkg_change_category($pid, $atype, $dbh=NULL) { * * @return array All package details for a specific package */ -function pkgdetails_by_pkgname($pkgname, $dbh=NULL) { +function pkgdetails_by_pkgname($pkgname) { if(!$dbh) { $dbh = DB::connect(); } @@ -1415,7 +1415,7 @@ function pkgdetails_by_pkgname($pkgname, $dbh=NULL) { * * @return void */ -function new_pkgdetails($pkgname, $license, $pkgver, $category_id, $pkgdesc, $pkgurl, $uid, $dbh=NULL) { +function new_pkgdetails($pkgname, $license, $pkgver, $category_id, $pkgdesc, $pkgurl, $uid) { if(!$dbh) { $dbh = DB::connect(); } @@ -1446,7 +1446,7 @@ function new_pkgdetails($pkgname, $license, $pkgver, $category_id, $pkgdesc, $pk * * @return void */ -function update_pkgdetails($pkgname, $license, $pkgver, $pkgdesc, $pkgurl, $uid, $pkgid, $dbh=NULL) { +function update_pkgdetails($pkgname, $license, $pkgver, $pkgdesc, $pkgurl, $uid, $pkgid) { if(!$dbh) { $dbh = DB::connect(); } @@ -1473,7 +1473,7 @@ function update_pkgdetails($pkgname, $license, $pkgver, $pkgdesc, $pkgurl, $uid, * * @return void */ -function add_pkg_dep($pkgid, $depname, $depcondition, $dbh=NULL) { +function add_pkg_dep($pkgid, $depname, $depcondition) { if(!$dbh) { $dbh = DB::connect(); } @@ -1494,7 +1494,7 @@ function add_pkg_dep($pkgid, $depname, $depcondition, $dbh=NULL) { * * @return void */ -function add_pkg_src($pkgid, $pkgsrc, $dbh=NULL) { +function add_pkg_src($pkgid, $pkgsrc) { if(!$dbh) { $dbh = DB::connect(); } @@ -1513,7 +1513,7 @@ function add_pkg_src($pkgid, $pkgsrc, $dbh=NULL) { * * @return void */ -function update_pkg_category($pkgid, $category_id, $dbh=NULL) { +function update_pkg_category($pkgid, $category_id) { if(!$dbh) { $dbh = DB::connect(); } @@ -1532,7 +1532,7 @@ function update_pkg_category($pkgid, $category_id, $dbh=NULL) { * * @return void */ -function remove_pkg_deps($pkgid, $dbh=NULL) { +function remove_pkg_deps($pkgid) { if(!$dbh) { $dbh = DB::connect(); } @@ -1549,7 +1549,7 @@ function remove_pkg_deps($pkgid, $dbh=NULL) { * * @return void */ -function remove_pkg_sources($pkgid, $dbh=NULL) { +function remove_pkg_sources($pkgid) { if(!$dbh) { $dbh = DB::connect(); } diff --git a/web/lib/stats.inc.php b/web/lib/stats.inc.php index cfae794..3b7cb8f 100644 --- a/web/lib/stats.inc.php +++ b/web/lib/stats.inc.php @@ -9,7 +9,8 @@ include_once('aur.inc.php'); * * @return void */ -function updates_table($dbh) { +function updates_table() { + $dbh = DB::connect(); $key = 'recent_updates'; if(!($newest_packages = get_cache_value($key))) { $q = 'SELECT * FROM Packages ORDER BY ModifiedTS DESC LIMIT 10'; @@ -32,16 +33,15 @@ function updates_table($dbh) { * * @return void */ -function user_table($userid, $dbh) { +function user_table($userid) { $base_q = "SELECT count(*) FROM Packages WHERE Packages.MaintainerUID = " . $userid; - $maintainer_unsupported_count = db_cache_value($base_q, $dbh, + $maintainer_unsupported_count = db_cache_value($base_q, 'user_unsupported_count:' . $userid); $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:' . $userid); + $flagged_outdated = db_cache_value($q, 'user_flagged_outdated:' . $userid); include('stats/user_table.php'); } @@ -53,34 +53,34 @@ function user_table($userid, $dbh) { * * @return void */ -function general_stats_table($dbh) { +function general_stats_table() { # AUR statistics $q = "SELECT count(*) FROM Packages"; - $unsupported_count = db_cache_value($q, $dbh, 'unsupported_count'); + $unsupported_count = db_cache_value($q, 'unsupported_count'); $q = "SELECT count(*) FROM Packages WHERE MaintainerUID IS NULL"; - $orphan_count = db_cache_value($q, $dbh, 'orphan_count'); + $orphan_count = db_cache_value($q, 'orphan_count'); $q = "SELECT count(*) FROM Users"; - $user_count = db_cache_value($q, $dbh, 'user_count'); + $user_count = db_cache_value($q, 'user_count'); $q = "SELECT count(*) FROM Users,AccountTypes WHERE Users.AccountTypeID = AccountTypes.ID AND AccountTypes.AccountType = 'Trusted User'"; - $tu_count = db_cache_value($q, $dbh, 'tu_count'); + $tu_count = db_cache_value($q, 'tu_count'); $targstamp = intval(strtotime("-7 days")); $yearstamp = intval(strtotime("-1 year")); $q = "SELECT count(*) FROM Packages WHERE Packages.ModifiedTS >= $targstamp AND Packages.ModifiedTS = Packages.SubmittedTS"; - $add_count = db_cache_value($q, $dbh, 'add_count'); + $add_count = db_cache_value($q, 'add_count'); $q = "SELECT count(*) FROM Packages WHERE Packages.ModifiedTS >= $targstamp AND Packages.ModifiedTS != Packages.SubmittedTS"; - $update_count = db_cache_value($q, $dbh, 'update_count'); + $update_count = db_cache_value($q, 'update_count'); $q = "SELECT count(*) FROM Packages WHERE Packages.ModifiedTS >= $yearstamp AND Packages.ModifiedTS != Packages.SubmittedTS"; - $update_year_count = db_cache_value($q, $dbh, 'update_year_count'); + $update_year_count = db_cache_value($q, 'update_year_count'); $q = "SELECT count(*) FROM Packages WHERE Packages.ModifiedTS = Packages.SubmittedTS"; - $never_update_count = db_cache_value($q, $dbh, 'never_update_count'); + $never_update_count = db_cache_value($q, 'never_update_count'); include('stats/general_stats_table.php'); } diff --git a/web/lib/translator.inc.php b/web/lib/translator.inc.php index ec8cb58..df798d1 100644 --- a/web/lib/translator.inc.php +++ b/web/lib/translator.inc.php @@ -69,7 +69,7 @@ function __() { # set up the visitor's language # -function set_lang($dbh=NULL) { +function set_lang() { global $LANG; global $SUPPORTED_LANGS; global $PERSISTENT_COOKIE_TIMEOUT; -- 1.8.1.2
--- web/lib/acctfuncs.inc.php | 24 ------------------------ web/lib/aur.inc.php | 18 ------------------ web/lib/pkgfuncs.inc.php | 24 ------------------------ web/lib/stats.inc.php | 5 ----- 4 files changed, 71 deletions(-) diff --git a/web/lib/acctfuncs.inc.php b/web/lib/acctfuncs.inc.php index fe13b49..e982849 100644 --- a/web/lib/acctfuncs.inc.php +++ b/web/lib/acctfuncs.inc.php @@ -84,7 +84,6 @@ function display_account_form($UTYPE,$A,$U="",$T="",$S="", * @param string $I The IRC nickname of the user * @param string $K The PGP fingerprint of the user * @param string $UID The user ID of the modified account - * @param \PDO $dbh An already established database connection * * @return string|void Return void if successful, otherwise return error */ @@ -282,7 +281,6 @@ function search_accounts_form() { * @param string $R The real name search criteria * @param string $I The IRC nickname search criteria * @param string $K The PGP key fingerprint search criteria - * @param \PDO $dbh An already established database connection * * @return void */ @@ -385,7 +383,6 @@ function search_results_page($UTYPE,$O=0,$SB="",$U="",$T="", * * @global int $MAX_SESSIONS_PER_USER Maximum sessions a single user may have open * @global int $PERSISTENT_COOKIE_TIMEOUT Time until cookie expires - * @param \PDO $dbh An already established database connection * * @return array Session ID for user, error message if applicable */ @@ -514,7 +511,6 @@ function valid_username($user) { * Determine if a username exists in the database * * @param string $user Username to check in the database - * @param \PDO $dbh An already established database connection * * @return string|void Return user ID if in database, otherwise void */ @@ -543,7 +539,6 @@ function valid_user($user) { * Determine if a user already has a proposal open about themselves * * @param string $user Username to checkout for open proposal - * @param \PDO $dbh An already established database connection * * @return bool True if there is an open proposal about the user, otherwise false */ @@ -569,7 +564,6 @@ function open_user_proposals($user) { * @param string $user The use the vote is about * @param int $votelength The length of time for the vote to last * @param string $submitteruid The user ID of the individual who submitted the proposal - * @param \PDO $dbh An already established database connection * * @return void */ @@ -590,7 +584,6 @@ function add_tu_proposal($agenda, $user, $votelength, $submitteruid) { * * @param string $resetkey A password reset key to be stored in database * @param string $uid The user ID to store the reset key for - * @param \PDO $dbh An already established database connection * * @return void */ @@ -611,7 +604,6 @@ function create_resetkey($resetkey, $uid) { * @param string $salt New salt for the user's password * @param string $resetkey Code e-mailed to a user to reset a password * @param string $email E-mail address of the user resetting their password - * @param \PDO $dbh An already established database connection * * @return string|void Redirect page if successful, otherwise return error message */ @@ -656,7 +648,6 @@ function good_passwd($passwd) { * * @param string $userID The user ID to check the password against * @param string $passwd The password the visitor sent - * @param \PDO $dbh An already established database connection * * @return bool True if password was correct and properly salted, otherwise false */ @@ -718,7 +709,6 @@ function valid_pgp_fingerprint($fingerprint) { * Determine if the user account has been suspended * * @param string $id The ID of user to check if suspended - * @param \PDO $dbh An already established database connection * * @return bool True if the user is suspended, otherwise false */ @@ -744,7 +734,6 @@ function user_suspended($id) { * Delete a specified user account from the database * * @param int $id The user ID of the account to be deleted - * @param \PDO $dbh An already established database connection * * @return void */ @@ -761,7 +750,6 @@ function user_delete($id) { * Determine if a user is either a Trusted User or Developer * * @param string $id The ID of the user to check if privileged - * @param \PDO $dbh An already established database connection * * @return int|string Return 0 if un-privileged, "2" if Trusted User, "3" if Developer */ @@ -785,7 +773,6 @@ function user_is_privileged($id) { * Remove the session from the database on logout * * @param string $sid User's session ID - * @param \PDO $dbh An already established database connection * * @return void */ @@ -802,7 +789,6 @@ function delete_session_id($sid) { * Remove all sessions belonging to a particular user * * @param int $uid ID of user to remove all sessions for - * @param \PDO $dbh An already established database connection * * @return void */ @@ -819,7 +805,6 @@ function delete_user_sessions($uid) { * Remove sessions from the database that have exceed the timeout * * @global int $LOGIN_TIMEOUT Time until session expires - * @param \PDO $dbh An already established database connection * * @return void */ @@ -841,7 +826,6 @@ function clear_expired_sessions() { * * @param string $uid The User ID of account to get information for * @param string $username The username of the account to get for - * @param \PDO $dbh An already established database connection * * @return array Account details for the specified user */ @@ -871,7 +855,6 @@ function account_details($uid, $username) { * * @param string $voteid The ID of the Trusted User proposal * @param string $uid The ID to check if the user already voted - * @param \PDO $dbh An already established database connection * * @return bool True if the user has already voted, otherwise false */ @@ -895,7 +878,6 @@ function tu_voted($voteid, $uid) { * Get all current Trusted User proposals from the database * * @param string $order Ascending or descending order for the proposal listing - * @param \PDO $dbh An already established database connection * * @return array The details for all current Trusted User proposals */ @@ -920,7 +902,6 @@ function current_proposal_list($order) { * * @param string $order Ascending or descending order for the proposal listing * @param string $lim The number of proposals to list with the offset - * @param \PDO $dbh An already established database connection * * @return array The details for the subset of past Trusted User proposals */ @@ -943,8 +924,6 @@ function past_proposal_list($order, $lim) { /** * Determine the total number of Trusted User proposals * - * @param \PDO $dbh An already established database connection - * * @return string The total number of Trusted User proposals */ function proposal_count() { @@ -963,7 +942,6 @@ function proposal_count() { * Get all details related to a specific vote from the database * * @param string $voteid The ID of the Trusted User proposal - * @param \PDO $dbh An already established database connection * * @return array All stored details for a specific vote */ @@ -985,7 +963,6 @@ function vote_details($voteid) { * Get an alphabetical list of users who voted for a proposal with HTML links * * @param string $voteid The ID of the Trusted User proposal - * @param \PDO $dbh An already established database connection * * @return array All users who voted for a specific proposal */ @@ -1018,7 +995,6 @@ function voter_list($voteid) { * @param string $uid The user ID of the individual voting * @param string $vote Vote position, either "Yes", "No", or "Abstain" * @param int $newtotal The total number of votes after the user has voted - * @param \PDO $dbh An already established database connection * * @return void */ diff --git a/web/lib/aur.inc.php b/web/lib/aur.inc.php index 77a84e4..3182dc9 100644 --- a/web/lib/aur.inc.php +++ b/web/lib/aur.inc.php @@ -26,7 +26,6 @@ include_once("cachefuncs.inc.php"); * * @global array $_COOKIE User cookie values * @global string $LOGIN_TIMEOUT Time until session times out - * @param \PDO $dbh Already established database connection * * @return void */ @@ -137,7 +136,6 @@ function new_sid() { * Determine the user's username in the database using a user ID * * @param string $id User's ID - * @param \PDO $dbh Already established database connection * * @return string Username if it exists, otherwise "None" */ @@ -162,7 +160,6 @@ function username_from_id($id="") { * Determine the user's username in the database using a session ID * * @param string $sid User's session ID - * @param \PDO $dbh Already established database connection * * @return string Username of the visitor */ @@ -190,7 +187,6 @@ function username_from_sid($sid="") { * Determine the user's e-mail address in the database using a session ID * * @param string $sid User's session ID - * @param \PDO $dbh Already established database connection * * @return string User's e-mail address as given during registration */ @@ -218,7 +214,6 @@ function email_from_sid($sid="") { * Determine the user's account type in the database using a session ID * * @param string $sid User's session ID - * @param \PDO $dbh Already established database connection * * @return string Account type of user ("User", "Trusted User", or "Developer") */ @@ -247,7 +242,6 @@ function account_from_sid($sid="") { * Determine the user's ID in the database using a session ID * * @param string $sid User's session ID - * @param \PDO $dbh Already established database connection * * @return string|int The user's name, 0 on query failure */ @@ -307,7 +301,6 @@ function html_footer($ver="") { * * @param string $name Name of the package to be submitted * @param string $sid User's session ID - * @param \PDO $dbh Already established database connection * * @return int 0 if the user can't submit, 1 if the user can submit */ @@ -364,7 +357,6 @@ function rm_tree($dirname) { * Determine the user's ID in the database using a username * * @param string $username The username of an account - * @param \PDO $dbh Already established database connection * * @return string Return user ID if exists for username, otherwise "None" */ @@ -389,7 +381,6 @@ function uid_from_username($username="") { * Determine the user's ID in the database using an e-mail address * * @param string $email An e-mail address in foo@example.com format - * @param \PDO $dbh Already established database connection * * @return string The user's ID */ @@ -458,7 +449,6 @@ function mkurl($append) { * Determine a user's salt from the database * * @param string $user_id The user ID of the user trying to log in - * @param \PDO $dbh Already established database connection * * @return string|void Return the salt for the requested user, otherwise void */ @@ -480,7 +470,6 @@ function get_salt($user_id) { * * @param string $user_id The user ID of the user who is salting their password * @param string $passwd The password of the user logging in - * @param \PDO $dbh Already established database connection */ function save_salt($user_id, $passwd) { if(!$dbh) { @@ -549,8 +538,6 @@ function parse_comment($comment) { /** * Wrapper for beginning a database transaction - * - * @param \PDO $dbh Already established database connection */ function begin_atomic_commit() { if(!$dbh) { @@ -561,8 +548,6 @@ function begin_atomic_commit() { /** * Wrapper for committing a database transaction - * - * @param \PDO $dbh Already established database connection */ function end_atomic_commit() { if(!$dbh) { @@ -575,8 +560,6 @@ function end_atomic_commit() { * * Determine the row ID for the most recently insterted row * - * @param \PDO $dbh Already established database connection - * * @return string The ID of the last inserted row */ function last_insert_id() { @@ -590,7 +573,6 @@ function last_insert_id() { * Determine package information for latest package * * @param int $numpkgs Number of packages to get information on - * @param \PDO $dbh Already established database connection * * @return array $packages Package info for the specified number of recent packages */ diff --git a/web/lib/pkgfuncs.inc.php b/web/lib/pkgfuncs.inc.php index 21fc37c..acaa218 100644 --- a/web/lib/pkgfuncs.inc.php +++ b/web/lib/pkgfuncs.inc.php @@ -10,7 +10,6 @@ include_once("config.inc.php"); * @param string $comment_id The comment ID in the database * @param string $atype The account type of the user trying to delete a comment * @param string|int $uid The user ID of the individual trying to delete a comment - * @param \PDO $dbh An already established database connection * * @return bool True if the user can delete the comment, otherwise false */ @@ -106,7 +105,6 @@ function pkgCategories() { * Check to see if the package name already exists in the database * * @param string $name The package name to check - * @param \PDO $dbh An already established database connection * * @return string|void Package name if it already exists */ @@ -129,7 +127,6 @@ function pkgid_from_name($name="") { * Get package dependencies for a specific package * * @param int $pkgid The package to get dependencies for - * @param \PDO $dbh An already established database connection * * @return array All package dependencies for the package */ @@ -159,7 +156,6 @@ function package_dependencies($pkgid) { * Determine packages that depend on a package * * @param string $name The package name for the dependency search - * @param \PDO $dbh An already established database connection * * @return array All packages that depend on the specified package name */ @@ -186,7 +182,6 @@ function package_required($name="") { * Get the number of non-deleted comments for a specific package * * @param string $pkgid The package ID to get comment count for - * @param \PDO $dbh An already established database connection * * @return string The number of comments left for a specific package */ @@ -218,7 +213,6 @@ function package_comments_count($pkgid) { * Get all package comment information for a specific package * * @param int $pkgid The package ID to get comments for - * @param \PDO $dbh An already established database connection * * @return array All package comment information for a specific package */ @@ -260,7 +254,6 @@ function package_comments($pkgid) { * @param string $pkgid The package ID to add the comment on * @param string $uid The user ID of the individual who left the comment * @param string $comment The comment left on a package page - * @param \PDO $dbh An already established database connection * * @return void */ @@ -317,7 +310,6 @@ function add_package_comment($pkgid, $uid, $comment) { * Get all package sources for a specific package * * @param string $pkgid The package ID to get the sources for - * @param \PDO $dbh An already established database connection * * @return array All sources associated with a specific package */ @@ -346,7 +338,6 @@ function package_sources($pkgid) { * Get a list of all packages a logged-in user has voted for * * @param string $sid The session ID of the visitor - * @param \PDO $dbh An already established database connection * * @return array All packages the visitor has voted for */ @@ -374,7 +365,6 @@ function pkgvotes_from_sid($sid="") { * Determine package names from package IDs * * @param string|array $pkgids The package IDs to get names for - * @param \PDO $dbh An already established database connection * * @return array|string All names if multiple package IDs, otherwise package name */ @@ -415,7 +405,6 @@ function pkgname_from_id($pkgids) { * Determine if a package name is on the database blacklist * * @param string $name The package name to check - * @param \PDO $dbh An already established database connection * * @return bool True if the name is blacklisted, otherwise false */ @@ -435,7 +424,6 @@ function pkgname_is_blacklisted($name) { * Get the package details * * @param string $id The package ID to get description for - * @param \PDO $dbh An already established database connection * * @return array The package's details OR error message **/ @@ -473,7 +461,6 @@ function get_package_details($id=0) { * @param string $id The package ID to get details page for * @param array $row Package details retrieved by get_package_details * @param string $SID The session ID of the visitor - * @param \PDO $dbh An already established database connection * * @return void */ @@ -1137,7 +1124,6 @@ function pkg_vote ($atype, $ids, $action=true) { * Get all usernames and IDs that voted for a specific package * * @param string $pkgid The package ID to get all votes for - * @param \PDO $dbh An already established database connection * * @return array User IDs and usernames that voted for a specific package */ @@ -1169,7 +1155,6 @@ function getvotes($pkgid) { * * @param string $uid The user ID to check for an existing vote * @param string $pkgid The package ID to check for an existing vote - * @param \PDO $dbh An already established database connection * * @return bool True if the user has already voted, otherwise false */ @@ -1195,7 +1180,6 @@ function user_voted($uid, $pkgid) { * * @param string $uid User ID to check in the database * @param string $pkgid Package ID to check notifications for - * @param \PDO $dbh An already established database connection * * @return bool True if the user wants notifications, otherwise false */ @@ -1385,7 +1369,6 @@ function pkg_change_category($pid, $atype) { * Get all package information in the database for a specific package * * @param string $pkgname The name of the package to get details for - * @param \PDO $dbh An already established database connection * * @return array All package details for a specific package */ @@ -1411,7 +1394,6 @@ function pkgdetails_by_pkgname($pkgname) { * @param string $pkgdesc Description of the new package * @param string $pkgurl Upstream URL for the new package * @param int $uid User ID of the package uploader - * @param \PDO $dbh An already established database connection * * @return void */ @@ -1442,7 +1424,6 @@ function new_pkgdetails($pkgname, $license, $pkgver, $category_id, $pkgdesc, $pk * @param string $pkgurl The upstream URL for the package * @param int $uid The user ID of the updater * @param int $pkgid The package ID of the updated package - * @param \PDO $dbh An already established database connection * * @return void */ @@ -1469,7 +1450,6 @@ function update_pkgdetails($pkgname, $license, $pkgver, $pkgdesc, $pkgurl, $uid, * @param int $pkgid The package ID to add the dependency for * @param string $depname The name of the dependency to add * @param string $depcondition The type of dependency for the package - * @param \PDO $dbh An already established database connection * * @return void */ @@ -1490,7 +1470,6 @@ function add_pkg_dep($pkgid, $depname, $depcondition) { * * @param int $pkgid The package ID to add the source for * @param string $pkgsrc The package source to add to the database - * @param \PDO $dbh An already established database connection * * @return void */ @@ -1509,7 +1488,6 @@ function add_pkg_src($pkgid, $pkgsrc) { * * @param int $pkgid The package ID to change the category for * @param int $category_id The new category ID for the package - * @param \PDO $dbh An already established database connection * * @return void */ @@ -1528,7 +1506,6 @@ function update_pkg_category($pkgid, $category_id) { * Remove package dependencies from a specific package * * @param string $pkgid The package ID to remove package dependencies from - * @param \PDO $dbh An already established database connection * * @return void */ @@ -1545,7 +1522,6 @@ function remove_pkg_deps($pkgid) { * Remove package sources from a specific package * * @param string $pkgid The package ID to remove package sources from - * @param \PDO $dbh An already established database connection * * @return void */ diff --git a/web/lib/stats.inc.php b/web/lib/stats.inc.php index 3b7cb8f..e0e0b02 100644 --- a/web/lib/stats.inc.php +++ b/web/lib/stats.inc.php @@ -5,8 +5,6 @@ include_once('aur.inc.php'); /** * Display the most recent 10 packages * - * @param \PDO $dbh An already established database connection - * * @return void */ function updates_table() { @@ -29,7 +27,6 @@ function updates_table() { * Display a user's statistics table * * @param string $userid The user ID of the person to get package statistics for - * @param \PDO $dbh An already established database connection * * @return void */ @@ -49,8 +46,6 @@ function user_table($userid) { /** * Display the general package statistics table * - * @param \PDO $dbh An already established database connection - * * @return void */ function general_stats_table() { -- 1.8.1.2
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> --- web/html/logout.php | 4 +- web/lib/acctfuncs.inc.php | 97 ++++++++---------------------- web/lib/aur.inc.php | 60 +++++-------------- web/lib/pkgfuncs.inc.php | 144 ++++++++++++--------------------------------- web/lib/translator.inc.php | 4 +- 5 files changed, 77 insertions(+), 232 deletions(-) diff --git a/web/html/logout.php b/web/html/logout.php index 6c98290..3958c25 100644 --- a/web/html/logout.php +++ b/web/html/logout.php @@ -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); diff --git a/web/lib/acctfuncs.inc.php b/web/lib/acctfuncs.inc.php index e982849..9c0998a 100644 --- a/web/lib/acctfuncs.inc.php +++ b/web/lib/acctfuncs.inc.php @@ -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); diff --git a/web/lib/aur.inc.php b/web/lib/aur.inc.php index 3182dc9..b3a800c 100644 --- a/web/lib/aur.inc.php +++ b/web/lib/aur.inc.php @@ -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 "; diff --git a/web/lib/pkgfuncs.inc.php b/web/lib/pkgfuncs.inc.php index acaa218..5ad53b2 100644 --- a/web/lib/pkgfuncs.inc.php +++ b/web/lib/pkgfuncs.inc.php @@ -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(); - } + $dbh = DB::connect(); $field = "MaintainerUID"; $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"]); $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 */ function getvotes($pkgid) { - if(!$dbh) { - $dbh = DB::connect(); - } + $dbh = DB::connect(); $q = "SELECT UsersID,Username FROM PackageVotes "; $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 */ function user_voted($uid, $pkgid) { - if(!$dbh) { - $dbh = DB::connect(); - } + $dbh = DB::connect(); $q = "SELECT * FROM PackageVotes WHERE UsersID = ". $dbh->quote($uid); $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 */ function user_notify($uid, $pkgid) { - if(!$dbh) { - $dbh = DB::connect(); - } + $dbh = DB::connect(); $q = "SELECT * FROM CommentNotify WHERE UserID = " . $dbh->quote($uid); $q.= " AND PkgID = " . $dbh->quote($pkgid); @@ -1219,9 +1169,7 @@ function pkg_notify ($atype, $ids, $action=true) { return __("Couldn't add to notification list."); } - if(!$dbh) { - $dbh = DB::connect(); - } + $dbh = DB::connect(); $uid = uid_from_sid($_COOKIE["AURSID"]); $output = ""; @@ -1298,9 +1246,7 @@ function pkg_delete_comment($atype) { return __("Missing comment ID."); } - if(!$dbh) { - $dbh = DB::connect(); - } + $dbh = DB::connect(); $uid = uid_from_sid($_COOKIE["AURSID"]); if (canDeleteComment($comment_id, $atype, $uid)) { $q = "UPDATE PackageComments "; @@ -1332,9 +1278,7 @@ function pkg_change_category($pid, $atype) { return __("Missing category ID."); } - if(!$dbh) { - $dbh = DB::connect(); - } + $dbh = DB::connect(); $catArray = pkgCategories($dbh); if (!array_key_exists($category_id, $catArray)) { return __("Invalid category ID."); @@ -1373,9 +1317,7 @@ function pkg_change_category($pid, $atype) { * @return array All package details for a specific package */ function pkgdetails_by_pkgname($pkgname) { - if(!$dbh) { - $dbh = DB::connect(); - } + $dbh = DB::connect(); $q = "SELECT * FROM Packages WHERE Name = " . $dbh->quote($pkgname); $result = $dbh->query($q); if ($result) { @@ -1398,9 +1340,7 @@ function pkgdetails_by_pkgname($pkgname) { * @return void */ 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)", $dbh->quote($pkgname), $dbh->quote($license), @@ -1428,9 +1368,7 @@ function new_pkgdetails($pkgname, $license, $pkgver, $category_id, $pkgdesc, $pk * @return void */ 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 $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), @@ -1454,9 +1392,7 @@ function update_pkgdetails($pkgname, $license, $pkgver, $pkgdesc, $pkgurl, $uid, * @return void */ 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)", $pkgid, $dbh->quote($depname), @@ -1474,9 +1410,7 @@ function add_pkg_dep($pkgid, $depname, $depcondition) { * @return void */ function add_pkg_src($pkgid, $pkgsrc) { - if(!$dbh) { - $dbh = DB::connect(); - } + $dbh = DB::connect(); $q = "INSERT INTO PackageSources (PackageID, Source) VALUES ("; $q .= $pkgid . ", " . $dbh->quote($pkgsrc) . ")"; @@ -1492,9 +1426,7 @@ function add_pkg_src($pkgid, $pkgsrc) { * @return void */ 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", $category_id, $pkgid); @@ -1510,9 +1442,7 @@ function update_pkg_category($pkgid, $category_id) { * @return void */ function remove_pkg_deps($pkgid) { - if(!$dbh) { - $dbh = DB::connect(); - } + $dbh = DB::connect(); $q = "DELETE FROM PackageDepends WHERE PackageID = " . $pkgid; $dbh->exec($q); @@ -1526,9 +1456,7 @@ function remove_pkg_deps($pkgid) { * @return void */ function remove_pkg_sources($pkgid) { - if(!$dbh) { - $dbh = DB::connect(); - } + $dbh = DB::connect(); $q = "DELETE FROM PackageSources WHERE PackageID = " . $pkgid; $dbh->exec($q); diff --git a/web/lib/translator.inc.php b/web/lib/translator.inc.php index df798d1..b539315 100644 --- a/web/lib/translator.inc.php +++ b/web/lib/translator.inc.php @@ -90,9 +90,7 @@ function set_lang() { } elseif (isset($_COOKIE["AURSID"])) { # No language but a session; use default lang preference # - if(!$dbh) { - $dbh = DB::connect(); - } + $dbh = DB::connect(); $q = "SELECT LangPreference FROM Users, Sessions "; $q.= "WHERE Users.ID = Sessions.UsersID "; $q.= "AND Sessions.SessionID = '"; -- 1.8.1.2
Since all database related functions will establish a connection when needed, there is no need to pre-emptively try and establish a database connection. Signed-off-by: canyonknight <canyonknight@gmail.com> --- web/html/account.php | 1 - web/html/home.php | 2 -- web/html/logout.php | 1 - web/html/pkgsubmit.php | 1 - 4 files changed, 5 deletions(-) diff --git a/web/html/account.php b/web/html/account.php index 7cd0263..c367914 100644 --- a/web/html/account.php +++ b/web/html/account.php @@ -20,7 +20,6 @@ $action = in_request("Action"); if (isset($_COOKIE["AURSID"])) { # visitor is logged in # - $dbh = DB::connect(); $atype = account_from_sid($_COOKIE["AURSID"]); if ($action == "SearchAccounts") { diff --git a/web/html/home.php b/web/html/home.php index 8fccc7f..e4439d9 100644 --- a/web/html/home.php +++ b/web/html/home.php @@ -10,8 +10,6 @@ include_once('stats.inc.php'); html_header( __("Home") ); -$dbh = DB::connect(); - ?> <div id="content-left-wrapper"> diff --git a/web/html/logout.php b/web/html/logout.php index 3958c25..5e8e8f4 100644 --- a/web/html/logout.php +++ b/web/html/logout.php @@ -10,7 +10,6 @@ include_once("acctfuncs.inc.php"); # access AUR common functions # sending any HTML output. # if (isset($_COOKIE["AURSID"])) { - $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); diff --git a/web/html/pkgsubmit.php b/web/html/pkgsubmit.php index 12203c4..fefb31e 100644 --- a/web/html/pkgsubmit.php +++ b/web/html/pkgsubmit.php @@ -356,7 +356,6 @@ if ($uid): # Update the backend database if (!$error) { - $dbh = DB::connect(); begin_atomic_commit(); $pdata = pkgdetails_by_pkgname($new_pkgbuild['pkgname']); -- 1.8.1.2
On Sun, Feb 3, 2013 at 10:26 AM, canyonknight <canyonknight@gmail.com> wrote:
Hello all,
I recently noticed in my SQL logs that multiple database connections will be started over the course of a page loading. Past commits have relied on passing the handle as an argument to avoid that.
The problem is many functions don't get a handle passed to them and there is a large amount of boilerplate code. This patch series will basically fully implement the same functionality as passing DB handles as arguments but with a lot less code and not mucking up function args.
Due to the fact that some of these patches are quite large they may get eaten by the list, so as always they will be available on my "working" branch.
canyonknight (5): Add database wrapper class and new connection method Remove unnecessary database connection parameter from all functions Remove documentation references to database parameter Remove checks before calling connection method Remove unneeded database connection calls
web/html/account.php | 1 - web/html/home.php | 8 +- web/html/logout.php | 7 +- web/html/pkgsubmit.php | 23 ++-- web/lib/DB.class.php | 28 +++++ web/lib/acctfuncs.inc.php | 179 +++++++++-------------------- web/lib/aur.inc.php | 131 ++++++--------------- web/lib/aurjson.class.php | 2 +- web/lib/cachefuncs.inc.php | 3 +- web/lib/pkgfuncs.inc.php | 276 +++++++++++++++------------------------------ web/lib/stats.inc.php | 33 +++--- web/lib/translator.inc.php | 6 +- 12 files changed, 239 insertions(+), 458 deletions(-) create mode 100644 web/lib/DB.class.php
-- 1.8.1.2
Without reviewing every single detail, I clearly left the original work half-complete. This seems like a much more complete and full solution so I give it a +1, although I did not review every single change in the multiple patches posted here. -Dan
participants (2)
-
canyonknight
-
Dan McGee