From d3da989d3e2d2cb03cdc4bd2e23783f716ef5035 Mon Sep 17 00:00:00 2001 From: Eudyptula Date: Tue, 4 Aug 2009 17:58:55 -0400 Subject: Widespread cleanup - moved $conf, $death into $S; $S is given as arg to init_x() and body_x(); $request removed in favor of $_REQUEST; query() replaces $S[pdo]->query(); etc. --- shared/classes/0sql_row_obj.php | 2 -- shared/classes/1conf_build_common.php | 4 ++-- shared/classes/build.php | 16 ++++++++-------- shared/classes/configuration.php | 3 ++- shared/classes/gentoo_package.php | 2 +- shared/classes/gentoo_profile.php | 16 ++++++++-------- shared/classes/registrationtoken.php | 2 +- shared/classes/session.php | 10 +++++----- shared/classes/task.php | 2 +- shared/classes/validate.php | 4 ++-- shared/functions/debug.php | 4 ++-- shared/functions/get_timezones.php | 8 ++++---- shared/functions/load_config.php | 2 +- shared/functions/query.php | 8 ++++++++ shared/functions/url.php | 12 ++++++------ shared/functions/xhtmlemail.php | 6 +++--- shared/include/dbinit.php | 5 +---- shared/include/includes.php | 1 + 18 files changed, 56 insertions(+), 51 deletions(-) create mode 100644 shared/functions/query.php (limited to 'shared') diff --git a/shared/classes/0sql_row_obj.php b/shared/classes/0sql_row_obj.php index 239dae3..4efacbd 100644 --- a/shared/classes/0sql_row_obj.php +++ b/shared/classes/0sql_row_obj.php @@ -285,8 +285,6 @@ abstract class sql_row_obj { // If the name of this class changes, it must be up if (!array_key_exists($name, $this->db_values) || $this->values[$name] !== $this->db_values[$name]) { $sql_val=$col->sql_value($this->values[$name]); if (is_array($sql_val)) { - //echo $name."\n"; - print_r($sql_val); throw new Exception('Failed to write '.get_class($this).' object to table `'.$this->table.'` because column `'.$name.'` ('.$sql_val['description'].') '.$sql_val['reason'].' (value: '.$sql_val['value'].')'); } if (++$i > 1) { diff --git a/shared/classes/1conf_build_common.php b/shared/classes/1conf_build_common.php index f5721a4..af20eba 100644 --- a/shared/classes/1conf_build_common.php +++ b/shared/classes/1conf_build_common.php @@ -26,7 +26,7 @@ abstract class conf_build_common extends sql_row_obj { global $S; if (!isset($this->opt_cache)) { $this->opt_cache=array(); - $r=$S['pdo']->query('SELECT * FROM `'.$this->info['optstable'].'` WHERE `'.$this->info['self'].'`="'.$this->id.'"'); + $r=query('SELECT * FROM `'.$this->info['optstable'].'` WHERE `'.$this->info['self'].'`="'.$this->id.'"'); while ($opt=$r->fetch(PDO::FETCH_ASSOC)) { $this->opt_cache[$opt['name']]=new $this->info['optsclass']($opt); } @@ -59,7 +59,7 @@ abstract class conf_build_common extends sql_row_obj { while (true) { $id=randstring(6); debug("Trying id=$id..."); - $r=$S['pdo']->query('SELECT `id` FROM `'.$this->table.'` WHERE `id`="'.$id.'"'); + $r=query('SELECT `id` FROM `'.$this->table.'` WHERE `id`="'.$id.'"'); if ($r->rowCount() == 0) { break; } diff --git a/shared/classes/build.php b/shared/classes/build.php index db12801..7230673 100644 --- a/shared/classes/build.php +++ b/shared/classes/build.php @@ -60,8 +60,8 @@ class sql_build extends conf_build_common { $html='
'.(isset($this->name) && strlen($this->name)?htmlentities($this->name):'Unnamed Build').' '; $links=array(); if ($this->status == -128) { - $total=$S['pdo']->query('SELECT COUNT(*) FROM `builds` WHERE `status`=-128')->fetch(PDO::FETCH_COLUMN); - $num=$S['pdo']->query('SELECT COUNT(*) FROM `builds` WHERE `status`=-128 AND `ctime` <= '.$this->ctime)->fetch(PDO::FETCH_COLUMN); + $total=query('SELECT COUNT(*) FROM `builds` WHERE `status`=-128')->fetch(PDO::FETCH_COLUMN); + $num=query('SELECT COUNT(*) FROM `builds` WHERE `status`=-128 AND `ctime` <= '.$this->ctime)->fetch(PDO::FETCH_COLUMN); $html.="[Queued ($num/$total)]"; } elseif ($this->status == -127) { $html.='[uploading]'; @@ -74,7 +74,7 @@ class sql_build extends conf_build_common { $links['Build Log']="build/$this->id"; } } elseif ($this->status == 0) { - $r=$S['pdo']->query('SELECT COUNT(*) as `count`, MAX(`time`) as `time` FROM `downloads` WHERE `build`="'.$this->id.'"')->fetch(PDO::FETCH_ASSOC); + $r=query('SELECT COUNT(*) as `count`, MAX(`time`) as `time` FROM `downloads` WHERE `build`="'.$this->id.'"')->fetch(PDO::FETCH_ASSOC); $d=($perms && $r['count']?'id/history").'">':'').$r['count'].' download'.($r['count'] != 1?'s':'').($r['count']?($perms?'':'').'
(last at '.date($format, $r['time']).')':''); $html.=''.$d.'[successful]'; $links['Download image']="build/$this->id/download"; @@ -125,15 +125,15 @@ class sql_build extends conf_build_common { global $S; static $cache; if (!isset($cache)) - $cache=$S['pdo']->query('SELECT COUNT(`order`) FROM `tasks` WHERE `start` IS NULL AND `build`="'.$this->id.'"')->fetch(PDO::FETCH_COLUMN); + $cache=query('SELECT COUNT(`order`) FROM `tasks` WHERE `start` IS NULL AND `build`="'.$this->id.'"')->fetch(PDO::FETCH_COLUMN); return $cache; } public function delete() { global $S; - $S['pdo']->query('DELETE FROM `buildlogs` WHERE `build`="'.$this->id.'"'); - $S['pdo']->query('DELETE FROM `tasks` WHERE `build`="'.$this->id.'"'); - $S['pdo']->query('DELETE FROM `buildopts` WHERE `build`="'.$this->id.'"'); - $S['pdo']->query('DELETE FROM `downloads` WHERE `build`="'.$this->id.'"'); + query('DELETE FROM `buildlogs` WHERE `build`="'.$this->id.'"'); + query('DELETE FROM `tasks` WHERE `build`="'.$this->id.'"'); + query('DELETE FROM `buildopts` WHERE `build`="'.$this->id.'"'); + query('DELETE FROM `downloads` WHERE `build`="'.$this->id.'"'); foreach (glob(COMPLETED."/build-.$this->id.*") as $file) unlink($file); parent::delete(); diff --git a/shared/classes/configuration.php b/shared/classes/configuration.php index c6d5766..c7795f4 100644 --- a/shared/classes/configuration.php +++ b/shared/classes/configuration.php @@ -48,6 +48,7 @@ class sql_configuration extends conf_build_common { } } $build=new sql_build(); + $build->visibility='public'; $build->init(); $build->name=$name; $build->module=$this->module; @@ -65,7 +66,7 @@ class sql_configuration extends conf_build_common { // Returns an array of the IDs of all the builds that report this configuration as their source public function get_builds() { global $S; - $r=$S['pdo']->query('SELECT `build` FROM `buildopts` WHERE `name`="configuration" AND `value`="'.$this->id.'"'); + $r=query('SELECT `build` FROM `buildopts` WHERE `name`="configuration" AND `value`="'.$this->id.'"'); if ($r->rowCount()) { $builds=array(); while ($b=$r->fetch(PDO::FETCH_COLUMN)) { diff --git a/shared/classes/gentoo_package.php b/shared/classes/gentoo_package.php index 2c06a26..01d6da4 100644 --- a/shared/classes/gentoo_package.php +++ b/shared/classes/gentoo_package.php @@ -88,7 +88,7 @@ class sql_gentoo_package extends sql_row_obj { } if ($name != '*') $c[]='`name`="'.$name.'"'; $c=implode(' AND ', $c); - $r=$S['pdo']->query('SELECT * FROM `gentoo_packages` WHERE '.$c); + $r=query('SELECT * FROM `gentoo_packages` WHERE '.$c); while ($pkg=$r->fetch(PDO::FETCH_ASSOC)) { $pkg=new sql_gentoo_package($pkg); if (!$pkg->is_masked()) diff --git a/shared/classes/gentoo_profile.php b/shared/classes/gentoo_profile.php index 7184aad..d7726f8 100644 --- a/shared/classes/gentoo_profile.php +++ b/shared/classes/gentoo_profile.php @@ -55,8 +55,8 @@ class sql_gentoo_profile extends sql_row_obj { } // Reads the data from the Packages file in $this->pkgdir public function read_Packages($update_pkgs=false, $verbose=false) { - global $conf; - if (!is_readable($file="{$conf['pkgdir_root']}/$this->pkgdir/Packages")) { + global $S; + if (!is_readable($file="{$S['conf']['pkgdir_root']}/$this->pkgdir/Packages")) { throw_exception("Packages file doesn't exist for pkgdir $this->pkgdir"); } $file=fopen($file, 'r'); @@ -104,7 +104,7 @@ class sql_gentoo_profile extends sql_row_obj { $u=$d=$t=0; if ($update_pkgs) { global $S; - $r=$S['pdo']->query('SELECT * FROM `gentoo_packages` WHERE `profile`='.$this->id); + $r=query('SELECT * FROM `gentoo_packages` WHERE `profile`='.$this->id); while ($pkg=$r->fetch(PDO::FETCH_ASSOC)) { $pkg=new sql_gentoo_package($pkg); if (isset($p[$pkg->bcat][$pkg->lcat][$pkg->name][$pkg->version])) { @@ -152,7 +152,7 @@ class sql_gentoo_profile extends sql_row_obj { $name=array_shift($pkgs); $obj=new sql_gentoo_pkgset(); if ($update) { - $r=$S['pdo']->query('SELECT * FROM `gentoo_pkgsets` WHERE `profile`='.$this->id.' AND `name`="'.$name.'" LIMIT 1'); + $r=query('SELECT * FROM `gentoo_pkgsets` WHERE `profile`='.$this->id.' AND `name`="'.$name.'" LIMIT 1'); if ($r->rowCount()) $obj->from_array($r->fetch(PDO::FETCH_ASSOC), true); } @@ -169,13 +169,13 @@ class sql_gentoo_profile extends sql_row_obj { } } if ($update) - $S['pdo']->query('DELETE FROM `gentoo_pkgsets` WHERE `profile`='.$this->id.($exists?' AND `id` NOT IN ('.implode(',', $exists).')':'')); + query('DELETE FROM `gentoo_pkgsets` WHERE `profile`='.$this->id.($exists?' AND `id` NOT IN ('.implode(',', $exists).')':'')); } public function read_stage3($update=false) { global $S; if ($update) { - $S['pdo']->query('DELETE FROM `gentoo_basepkgs` WHERE `profile`='.$this->id); - $S['pdo']->query('DELETE FROM `gentoo_baseinit` WHERE `profile`='.$this->id); + query('DELETE FROM `gentoo_basepkgs` WHERE `profile`='.$this->id); + query('DELETE FROM `gentoo_baseinit` WHERE `profile`='.$this->id); } $file=realpath(CACHE.'/stage3/'.$this->stage3); if (!is_readable($file)) return false; @@ -198,7 +198,7 @@ class sql_gentoo_profile extends sql_row_obj { public function &get_packages($omit_masked=false, $trim=null) { global $S; $skip_masked=!in_array('masked', $trim); - $r=$S['pdo']->query('SELECT * FROM `gentoo_packages` WHERE `profile`='.$this->id); + $r=query('SELECT * FROM `gentoo_packages` WHERE `profile`='.$this->id); $p=array(); while ($pkg=$r->fetch(PDO::FETCH_ASSOC)) { $pkg=new sql_gentoo_package($pkg); diff --git a/shared/classes/registrationtoken.php b/shared/classes/registrationtoken.php index 9f331ae..93fe4e6 100644 --- a/shared/classes/registrationtoken.php +++ b/shared/classes/registrationtoken.php @@ -33,7 +33,7 @@ class sql_registrationtoken extends sql_row_obj { $id=null; do { $id=randstring(30); - if ($S['pdo']->query('SELECT COUNT(*) FROM `registrationtokens` WHERE `id`=\''.$id.'\'')->fetch(PDO::FETCH_COLUMN)) + if (query('SELECT COUNT(*) FROM `registrationtokens` WHERE `id`=\''.$id.'\'')->fetch(PDO::FETCH_COLUMN)) $id=null; } while ($id==null); return new sql_registrationtoken($id, null, null, null); diff --git a/shared/classes/session.php b/shared/classes/session.php index e422b21..1233a71 100644 --- a/shared/classes/session.php +++ b/shared/classes/session.php @@ -33,18 +33,18 @@ class sql_session extends sql_row_obj { ); // Creates a new session for the user at $S['user'] with a unique id, sends a cookie to the user and returns true for success, false for failure static function create() { - global $S, $conf; + global $S; $id=null; while (!$id) { $id=randstring(30); - $r=$S['pdo']->query('SELECT * FROM `sessions` WHERE `id`="'.$id.'"'); + $r=query('SELECT * FROM `sessions` WHERE `id`="'.$id.'"'); if ($r->rowCount()) { $id=null; } } - $S['session']=new sql_session($id, $S['user']->id, time(), $conf['sessionlength']); - debug('setcookie', $conf['cookiename'].'='.$id); - if (setcookie($conf['cookiename'], $S['session']->id, time()+$conf['sessionlength'], $S['cookie_dir'], '', false, true)) { + $S['session']=new sql_session($id, $S['user']->id, time(), $S['conf']['sessionlength']); + debug('setcookie', $S['conf']['cookiename'].'='.$id); + if (setcookie($S['conf']['cookiename'], $S['session']->id, time()+$S['conf']['sessionlength'], $S['cookie_dir'], '', false, true)) { $S['session']->write(); return true; } else { diff --git a/shared/classes/task.php b/shared/classes/task.php index cdcd15c..d763107 100644 --- a/shared/classes/task.php +++ b/shared/classes/task.php @@ -73,7 +73,7 @@ class sql_task extends sql_row_obj { $html.='[running] Running for '.display_time(time()-$this->start).''; } } else { - $num=$S['pdo']->query('SELECT COUNT(*) FROM `tasks` WHERE `builds`="'.$this->build.'" AND `start` IS NULL AND `order` <= '.$this->order)->fetch(PDO::FETCH_ASSOC); + $num=query('SELECT COUNT(*) FROM `tasks` WHERE `builds`="'.$this->build.'" AND `start` IS NULL AND `order` <= '.$this->order)->fetch(PDO::FETCH_ASSOC); $html.="[queued $num/".$build->queued_tasks()."]"; } $html.='
'; diff --git a/shared/classes/validate.php b/shared/classes/validate.php index c409db4..6c6f479 100644 --- a/shared/classes/validate.php +++ b/shared/classes/validate.php @@ -37,8 +37,8 @@ class Validate { } } // Checking DNS to make sure the domain is real is total overkill - global $conf; - if ($conf['check_email_dns'] && $isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) { + global $S; + if ($S['conf']['check_email_dns'] && $isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) { // domain not found in DNS $isValid = false; } diff --git a/shared/functions/debug.php b/shared/functions/debug.php index 4231e21..8a054f9 100644 --- a/shared/functions/debug.php +++ b/shared/functions/debug.php @@ -1,7 +1,7 @@ diff --git a/shared/functions/url.php b/shared/functions/url.php index f2ff5ba..0234b2b 100644 --- a/shared/functions/url.php +++ b/shared/functions/url.php @@ -1,23 +1,23 @@ true)); + $S['pdo']=new PDO('mysql:dbname='.$S['conf']['sqldb'].';host='.$S['conf']['sqlhost'], $S['conf']['sqluser'], $S['conf']['sqlpass'], array(PDO::ATTR_PERSISTENT => true)); } catch (Exception $e) { die(print_error('Database connection failure.', $e->getMessage())); } -unset($pdoclass); sql_row_obj::set_pdo_obj($S['pdo']); ?> diff --git a/shared/include/includes.php b/shared/include/includes.php index bfd358d..8a75b58 100644 --- a/shared/include/includes.php +++ b/shared/include/includes.php @@ -1,4 +1,5 @@