From 79d0a253da7c8b167624889df7a61083d6203484 Mon Sep 17 00:00:00 2001 From: Eudyptula Date: Fri, 14 Aug 2009 11:12:45 -0400 Subject: Create sql_row_with_flags class, make user, build, configuration subclasses of it, replace visibility in builds, configurations with flags column; fix init script for correct path --- frontend/pages/builds/download.php | 2 +- frontend/pages/builds/history.php | 2 +- frontend/pages/builds/log.php | 2 +- frontend/pages/configurations/wizard.php | 1 - init_script | 2 +- shared/classes/1conf_build_common.php | 112 ------------------------------- shared/classes/1sql_row_with_flags.php | 14 ++++ shared/classes/2conf_build_common.php | 112 +++++++++++++++++++++++++++++++ shared/classes/build.php | 8 +-- shared/classes/configuration.php | 8 +-- shared/classes/gentoo_profile.php | 2 +- shared/classes/user.php | 5 +- 12 files changed, 140 insertions(+), 130 deletions(-) delete mode 100644 shared/classes/1conf_build_common.php create mode 100644 shared/classes/1sql_row_with_flags.php create mode 100644 shared/classes/2conf_build_common.php diff --git a/frontend/pages/builds/download.php b/frontend/pages/builds/download.php index f472046..ec957a1 100644 --- a/frontend/pages/builds/download.php +++ b/frontend/pages/builds/download.php @@ -13,7 +13,7 @@ function init_builds_download(&$S) { return '404'; } $build=new sql_build($r->fetch(PDO::FETCH_ASSOC)); - if ($build->visibility == 'private' && !owner_or_admin($build->owner)) { + if ($build->has_flag('p') && !owner_or_admin($build->owner)) { debug('builds_download', 'Permission denied'); return '404'; } diff --git a/frontend/pages/builds/history.php b/frontend/pages/builds/history.php index c9789e3..9e965f3 100644 --- a/frontend/pages/builds/history.php +++ b/frontend/pages/builds/history.php @@ -7,7 +7,7 @@ function init_builds_history(&$S) { $r=query('SELECT * FROM `builds` WHERE `id`="'.$_REQUEST['build'].'"'); if (!$r->rowCount()) return '404'; $S['builds_history']['build']=new sql_build($r->fetch(PDO::FETCH_ASSOC)); - if ($S['builds_history']['build']->visibility == 'private' && !owner_or_admin($S['builds_history']['build']->id)) { + if ($S['builds_history']['build']->has_flag('p') && !owner_or_admin($S['builds_history']['build']->id)) { return '404'; } return array('title' => 'Download History'); diff --git a/frontend/pages/builds/log.php b/frontend/pages/builds/log.php index 078b816..31ee5c7 100644 --- a/frontend/pages/builds/log.php +++ b/frontend/pages/builds/log.php @@ -6,7 +6,7 @@ function init_builds_log(&$S) { $r=query('SELECT * FROM `builds` WHERE `id`="'.$_REQUEST['build'].'"'); if ($r->rowCount()) { $S['builds_log']=new sql_build($r->fetch(PDO::FETCH_ASSOC)); - if ($S['builds_log']->visibility == 'private' && !owner_or_admin($S['builds_log']->owner)) return '404'; + if ($S['builds_log']->has_flag('p') && !owner_or_admin($S['builds_log']->owner)) return '404'; } else return '404'; if (isset($_REQUEST['task']) && is_numeric($_REQUEST['task'])) diff --git a/frontend/pages/configurations/wizard.php b/frontend/pages/configurations/wizard.php index 33b9949..6dc6a8e 100644 --- a/frontend/pages/configurations/wizard.php +++ b/frontend/pages/configurations/wizard.php @@ -36,7 +36,6 @@ function init_configurations_wizard(&$S) { $S['wizard']['configuration']=new sql_configuration(); $configuration=&$S['wizard']['configuration']; $configuration->name=$_REQUEST['name']; - $configuration->visibility='public'; $mod=isset($_REQUEST['mod']) && isset($S['conf']['modules'][$_REQUEST['mod']])?$S['conf']['modules'][$_REQUEST['mod']]:$S['conf']['modules'][0]; $configuration->module=$mod; $configuration->init(); diff --git a/init_script b/init_script index 8069ef2..18f14dc 100755 --- a/init_script +++ b/init_script @@ -1,6 +1,6 @@ #!/sbin/runscript -INGENUE_PATH="/home/eitan/soc/git" +INGENUE_PATH="/usr/share/ingenue" depend() { need mysql ntpd diff --git a/shared/classes/1conf_build_common.php b/shared/classes/1conf_build_common.php deleted file mode 100644 index 27c2198..0000000 --- a/shared/classes/1conf_build_common.php +++ /dev/null @@ -1,112 +0,0 @@ -info)) { - return; - } - $array=array( - 'sql_configuration' => array( - 'optsclass' => 'sql_configopt', - 'optstable' => 'configopts', - 'self' => 'configuration', - ), - 'sql_build' => array( - 'optsclass' => 'sql_buildopt', - 'optstable' => 'buildopts', - 'self' => 'build' - ) - ); - $this->info=$array[get_class($this)]; - } - // Fetches all available configopts pertaining to this configuration in a nice array (cached) - private $opt_cache; - public function &get_opts($get_objs=false) { - $this->set_vars(); - global $S; - if (!isset($this->opt_cache)) { - $this->opt_cache=array(); - $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); - } - } - if ($get_objs) { - return $this->opt_cache; - } else { - $opts=array(); - foreach ($this->opt_cache as $opt) { - $opts[$opt->name]=$opt->value; - } - } - return $opts; - } - // Returns the value (or object if $get_obj is true) of the option given by $name, or false if unset - public function get_opt($name, $get_obj=false) { - $opts=$this->get_opts($get_obj); - return array_key_exists($name, $opts)?$opts[$name]:false; - } - // Returns true if the given option is set and equals the given value, false otherwise - public function opt_is($name, $val) { - return ($this->get_opt($name) == $val); - } - // Generates a unique id and sets status to 1, writes self to db and returns id - public function init() { - global $S; - $this->owner=$S['user']->id; - if ($this->table == 'configurations') - $this->status=1; - else - $this->status='queued'; - $fails=0; - while (true) { - $id=randstring(6); - debug("Trying id=$id..."); - $r=query('SELECT `id` FROM `'.$this->table.'` WHERE `id`="'.$id.'"'); - if ($r->rowCount() == 0) { - break; - } - if (++$fails == 10) { - throw_exception('Failed 10 times to find a unique id in table `'.$this->table.'`... this shouldn\'t happen.'); - } - } - $this->id=$id; - $this->write(); - return $this->id; - } - public function summary() { - $opts=$this->get_opts(); - $r=array(); - foreach($opts as $name => $val) { - $name=htmlentities($name); - $val=htmlentities($val); - $r[]="$name$val"; - } - if ($r) { - return '
'.implode('
', $r).'
'; - } else { - return 'No options set'; - } - } - // Sets a particular config/buildopt, using either UPDATE or INSERT - public function set_opt($name, $val) { - $this->set_vars(); - $opts=$this->get_opts(true); - if (isset($opts[$name])) { - $opts[$name]->value=$val; - $opts[$name]->write(); - } else { - $this->opt_cache[$name]=new $this->info['optsclass']($this->id, $name, $val); - $this->opt_cache[$name]->write(); - } - } - // Deletes the given option if it exists - public function delete_opt($name) { - $opts=$this->get_opts(true); - if (isset($opts[$name])) { - $opts[$name]->delete(); - } - unset($opts[$name]); - } -} -?> diff --git a/shared/classes/1sql_row_with_flags.php b/shared/classes/1sql_row_with_flags.php new file mode 100644 index 0000000..43638a6 --- /dev/null +++ b/shared/classes/1sql_row_with_flags.php @@ -0,0 +1,14 @@ +flags, $flag) !== false); + } + public function set_flag($flag) { + if (!$this->has_flag($flag)) + $this->flags.=$flag; + } + public function unset_flag($flag) { + $this->flags=str_replace($flag, '', $this->flags); + } +} +?> diff --git a/shared/classes/2conf_build_common.php b/shared/classes/2conf_build_common.php new file mode 100644 index 0000000..acd9c0c --- /dev/null +++ b/shared/classes/2conf_build_common.php @@ -0,0 +1,112 @@ +info)) { + return; + } + $array=array( + 'sql_configuration' => array( + 'optsclass' => 'sql_configopt', + 'optstable' => 'configopts', + 'self' => 'configuration', + ), + 'sql_build' => array( + 'optsclass' => 'sql_buildopt', + 'optstable' => 'buildopts', + 'self' => 'build' + ) + ); + $this->info=$array[get_class($this)]; + } + // Fetches all available configopts pertaining to this configuration in a nice array (cached) + private $opt_cache; + public function &get_opts($get_objs=false) { + $this->set_vars(); + global $S; + if (!isset($this->opt_cache)) { + $this->opt_cache=array(); + $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); + } + } + if ($get_objs) { + return $this->opt_cache; + } else { + $opts=array(); + foreach ($this->opt_cache as $opt) { + $opts[$opt->name]=$opt->value; + } + } + return $opts; + } + // Returns the value (or object if $get_obj is true) of the option given by $name, or false if unset + public function get_opt($name, $get_obj=false) { + $opts=$this->get_opts($get_obj); + return array_key_exists($name, $opts)?$opts[$name]:false; + } + // Returns true if the given option is set and equals the given value, false otherwise + public function opt_is($name, $val) { + return ($this->get_opt($name) == $val); + } + // Generates a unique id and sets status to 1, writes self to db and returns id + public function init() { + global $S; + $this->owner=$S['user']->id; + if ($this->table == 'configurations') + $this->status=1; + else + $this->status='queued'; + $fails=0; + while (true) { + $id=randstring(6); + debug("Trying id=$id..."); + $r=query('SELECT `id` FROM `'.$this->table.'` WHERE `id`="'.$id.'"'); + if ($r->rowCount() == 0) { + break; + } + if (++$fails == 10) { + throw_exception('Failed 10 times to find a unique id in table `'.$this->table.'`... this shouldn\'t happen.'); + } + } + $this->id=$id; + $this->write(); + return $this->id; + } + public function summary() { + $opts=$this->get_opts(); + $r=array(); + foreach($opts as $name => $val) { + $name=htmlentities($name); + $val=htmlentities($val); + $r[]="$name$val"; + } + if ($r) { + return '
'.implode('
', $r).'
'; + } else { + return 'No options set'; + } + } + // Sets a particular config/buildopt, using either UPDATE or INSERT + public function set_opt($name, $val) { + $this->set_vars(); + $opts=$this->get_opts(true); + if (isset($opts[$name])) { + $opts[$name]->value=$val; + $opts[$name]->write(); + } else { + $this->opt_cache[$name]=new $this->info['optsclass']($this->id, $name, $val); + $this->opt_cache[$name]->write(); + } + } + // Deletes the given option if it exists + public function delete_opt($name) { + $opts=$this->get_opts(true); + if (isset($opts[$name])) { + $opts[$name]->delete(); + } + unset($opts[$name]); + } +} +?> diff --git a/shared/classes/build.php b/shared/classes/build.php index 45d8f07..6ba6798 100644 --- a/shared/classes/build.php +++ b/shared/classes/build.php @@ -25,9 +25,9 @@ class sql_build extends conf_build_common { 'not_null' => true, 'default' => '' ), - 'visibility' => array ( - 'type' => 'ENUM', - 'length' => '\'public\',\'private\'', + 'flags' => array ( + 'type' => 'VARCHAR', + 'length' => 255, 'not_null' => true ), 'backend' => array ( @@ -75,7 +75,7 @@ class sql_build extends conf_build_common { public function display() { global $S; $format='D j M Y G:i:s T'; - $perms=$this->visibility == 'public' || owner_or_admin($this->id); + $perms=!$this->has_flag('p') || owner_or_admin($this->id); $html='
'.(isset($this->name) && strlen($this->name)?htmlentities($this->name):'Unnamed Build').' '; if ($this->failed == 'true') $html.='[failed] '; diff --git a/shared/classes/configuration.php b/shared/classes/configuration.php index 8780b9d..4d7ac60 100644 --- a/shared/classes/configuration.php +++ b/shared/classes/configuration.php @@ -25,9 +25,9 @@ class sql_configuration extends conf_build_common { 'not_null' => true, 'default' => '' ), - 'visibility' => array ( - 'type' => 'ENUM', - 'length' => '\'public\',\'private\'', + 'flags' => array ( + 'type' => 'VARCHAR', + 'length' => 255, 'not_null' => true ), 'status' => array ( @@ -48,10 +48,10 @@ class sql_configuration extends conf_build_common { } } $build=new sql_build(); - $build->visibility='public'; $build->init(); $build->name=$name; $build->module=$this->module; + $build->flags=$this->flags; $opts=$this->get_opts(); $opts['configuration']=$this->id; foreach ($opts as $name => $val) { diff --git a/shared/classes/gentoo_profile.php b/shared/classes/gentoo_profile.php index d41497b..046de26 100644 --- a/shared/classes/gentoo_profile.php +++ b/shared/classes/gentoo_profile.php @@ -1,5 +1,5 @@ array ( 'type' => 'TINYINT', diff --git a/shared/classes/user.php b/shared/classes/user.php index eef0977..127397c 100644 --- a/shared/classes/user.php +++ b/shared/classes/user.php @@ -1,5 +1,5 @@ array ( 'type' => 'INT', @@ -35,8 +35,5 @@ class sql_user extends sql_row_obj { ) ); - public function has_flag($flag) { - return (strpos($this->flags, $flag) !== false); - } } ?> -- cgit v1.2.3-65-gdbad