User management for the world wide web

Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

dbsetup.php

00001 <HTML>
00002 <HEAD><TITLE>GenieGate setup</TITLE></HEAD>
00003 <BODY>
00004 <P>
00005 This is the database setup procedure, it will install the tables
00006 and data required for proper operation.
00007 </P>
00008 <P>
00009 Your <I>geniegate.ini</I> file must have the required [database] section
00010 completed before running this script. 
00011 </P>
00012 <P>
00013 If you forgot your root password, the only way to retrieve it is 
00014 by searching mysql's <I>ua_users</I> table. 
00015 </P>
00016 <P>
00017 Your root username is <I>root</I>, the default password is <I>geniegate</I>
00018 this should be changed as soon as possible.
00019 </P>
00020 <P>
00021 If all went well, you can access your <A HREF="index.php">Admin control panel</A>
00022 at this time.
00023 </P>
00024 <?php
00025 
00026 //http://pong.tunestar.net/sandbox/GenieGate/web/admin/index.php?DO_LOOKUP_UID=1&UID=root
00027 
00034 class GenieGate_SetupDruid {
00035 
00036   var $CMAP = array( ua_users => create_ua_users,
00037              ua_members => create_ua_members,
00038              ua_psect => create_ua_psect,
00039              ua_prnam => create_ua_prnam,
00040              ua_prop  => create_ua_prop,
00041              ua_group => create_ua_group
00042              );
00043 
00044   // Properties that the user could (in theory) alter.
00045   var $PUB_PROP = array(
00046             tel  => "Telephone Number",
00047             postalCode => "Postal code",
00048             state => "State/Province",
00049             country => "Country",
00050             fname  => "First Name",
00051             lname  => "Last Name",
00052             fax  => "FAX number",
00053             city => "City",
00054             street => "Street",
00055             pob => "P.O. Box number",
00056             dob   => "Date of birth",
00057             homePage => "Home Page URL",
00058             title  => "Job Title/Position",
00059             organization => "Company or organization",
00060             wphone => "Work Phone"              
00061             );
00062 
00063   // Properties for admin.
00064   var $PRIV_PROP = array(comments => "Comments");
00065 
00066   var $ERR = array();
00067 
00068   function GenieGate_SetupDruid() {}
00069 
00070   function run($conf){ 
00071     $dbc = $conf[database];
00072     $dbh = mysql_connect($dbc[HOST],$dbc[USER],$dbc[PASS]);
00073     if(! $dbh){
00074       die(mysql_errno() . " " .  mysql_error());        
00075     }    
00076     if(! mysql_select_db($dbc[DATABASE],$dbh)){
00077       die(mysql_errno() . " " .  mysql_error());
00078     }
00079     $this->check($conf,$dbh);
00080   }
00081 
00082   function check($conf,$dbh){
00083     $tables = array();
00084     $inf = $conf[database];
00085     $dbname = $inf[DATABASE];
00086     echo "<H1>Using database: [$dbname]</H1>";
00087     $ctr = $this->check_tables($dbname,$dbh);
00088     echo "<P>";
00089     $this->install_prop($dbh);
00090     echo "<P>";
00091     echo "<H2>Checking user account for root</H2>";
00092     $this->check_admin($dbh);
00093   }
00094 
00095   // Add an error.
00096   function error($message){ array_push($this->ERR,$message); }
00097 
00098   function check_admin($dbh){
00099       $stats = $this->checkRoot($dbh);
00100       echo "<UL>";
00101       // Setup admin group.
00102       if(! $stats[ua_group]){
00103           echo "<LI>Creating admin group ";
00104           $sql = "INSERT INTO ua_group (gid,name,signup) VALUES('admin','User can access control panel, use wisely!','N')";
00105           mysql_query($sql,$dbh) or die(mysql_error());
00106       }else{
00107           // Doesn't hurt to make sure this is always set to 'N'.
00108           echo "<LI>Making admin group unavailable to user signup. ";
00109           mysql_query("UPDATE ua_group SET signup='N' WHERE gid='admin'") or die(mysql_error());
00110       }
00111       // setup root user.
00112       if(! $stats[ua_users]){
00113 ?>
00114 <P>
00115 <H2>Creating root user (password: [geniegate] remember to change this!)</H2>
00116 <STRONG>It is extremely important to write this
00117 password down.</STRONG> You cannot simply re-run this script to
00118 obtain the root password. (this is for security reasons) 
00119 </P>
00120 <P>
00121 It should also be noted that your root password needs to be changed as
00122 soon as possible. You can do this in the admin panel, simply type 'root'
00123 in the search box provided on the main screen. Until it is changed 
00124 other people will be able to access your system.
00125 </P>
00126 
00127 <?php
00128           $sql = "INSERT INTO ua_users (id,email,uid,created,confirm,name,password) ";
00129           $sql .= "VALUES('1','','root',CURRENT_DATE,'Y','Root User (do not delete)','geniegate')";
00130           mysql_query($sql,$dbh) or die(mysql_error());
00131       }else{
00132           echo "<LI>User root already exists, not changing";
00133       }
00134       // Make root a member of admin.
00135       if(! $stats[ua_members]){
00136           echo "<LI>Making root a member of admin";
00137           $sql = "INSERT INTO ua_members (gid,uid) VALUES('admin','root')";
00138           mysql_query($sql,$dbh) or die(mysql_error());      
00139       }else{
00140           echo "<LI>Root is already a member of admin, not changing";
00141       }
00142       echo "</UL>";
00143   }
00144   
00145   // Check to see if the root user is setup OK. (exists, group admin exists, and root is a member.
00146   function checkRoot($dbh){
00147       $stat = array();
00148       $sql = "SELECT uid FROM ua_users WHERE uid='root'";
00149       $rs = mysql_query($sql,$dbh);
00150       if($rs){
00151           $row = mysql_fetch_row($rs);
00152           if($row[0] == 'root'){
00153               $stat[ua_users] = TRUE;
00154           }
00155       }
00156       $sql = "SELECT gid FROM ua_group WHERE gid='admin'";
00157       $rs = mysql_query($sql,$dbh);
00158       if($rs){
00159           $row = mysql_fetch_row($rs);
00160           if($row[0] == 'admin'){
00161               $stat[ua_group] = TRUE;
00162           }
00163       }
00164       $sql = "SELECT uid,gid FROM ua_members WHERE uid='root' AND gid='admin'";
00165       $rs = mysql_query($sql,$dbh);
00166       if($rs){
00167           $row = mysql_fetch_row($rs);
00168           if($row[0] == 'root'){
00169               $stat[ua_members] = $row[1] == 'admin';
00170           }
00171       }
00172       return($stat);
00173   }
00174 
00179   function check_tables($dbname,$dbh){    
00180     $res = mysql_list_tables($dbname,$dbh);    
00181     $ctr = 0;
00182     while($row = mysql_fetch_row($res)){
00183       $tn = $row[0];
00184       $tables[$tn] = TRUE;
00185     }
00186     echo "<DL><DT>Checking Tables</DT><DD>\n";
00187     foreach($this->CMAP as $tbl => $meth){
00188       if(! $tables[$tbl]){
00189     echo "<LI>Create: $tbl .. ";
00190     if($this->$meth($dbh)){
00191       echo "Success!\n";
00192       ++$ctr;
00193     }else{
00194       $er = mysql_error();
00195       $this->error("Couldn't create $tbl " . $er);
00196       echo "<B>Failure:</B> " . htmlentities($er) . "\n";
00197     }
00198       }else{
00199     echo "<LI>Table [$tbl] exists, not creating.\n";
00200       }
00201     }
00202     echo "</DD></DL>\n";
00203     return($ctr);
00204   }
00205   function create_ua_group($dbh){
00206     $sql = "CREATE TABLE ua_group (
00207                    gid CHAR(12) UNIQUE NOT NULL,
00208                    name   VARCHAR(80),
00209                    signup ENUM('Y','N'),
00210                    PRIMARY KEY(gid) 
00211                    )";
00212     return(mysql_query($sql,$dbh));
00213   }
00214   function create_ua_users($dbh){
00215     $sql = "CREATE TABLE ua_users (
00216                   id    MEDIUMINT,
00217                   uid   CHAR(12) UNIQUE NOT NULL,
00218                   created DATE NOT NULL,
00219                   confirm ENUM('U','Y') DEFAULT 'U',
00220                   email   VARCHAR(60),
00221                   name    VARCHAR(80),  
00222                   password VARCHAR(24),
00223                   INDEX(email,id),
00224                   PRIMARY KEY(uid)
00225                   )";
00226     return(mysql_query($sql,$dbh));
00227   }
00228   function create_ua_members($dbh) {
00229     $sql = "CREATE TABLE ua_members (
00230                  gid CHAR(12),
00231                  uid CHAR(12)
00232                  )";
00233     return(mysql_query($sql,$dbh));
00234   }
00235   function create_ua_psect($dbh) {
00236     $sql =  "CREATE TABLE ua_psect (
00237          sid MEDIUMINT AUTO_INCREMENT,
00238          name VARCHAR(80),
00239          skey VARCHAR(80) UNIQUE NOT NULL,
00240          PRIMARY KEY(sid))";
00241     return(mysql_query($sql,$dbh));
00242   }
00243   function create_ua_prnam($dbh){
00244     $sql = "CREATE TABLE ua_prnam (
00245           prid MEDIUMINT AUTO_INCREMENT,
00246           sid  MEDIUMINT NOT NULL,
00247           pkey VARCHAR(80),
00248           diz VARCHAR(255),
00249           PRIMARY KEY(prid))";
00250     return(mysql_query($sql,$dbh));
00251   }
00252   function create_ua_prop($dbh){
00253     $sql = "CREATE TABLE ua_prop (
00254            prid     MEDIUMINT NOT NULL,
00255            uid  CHAR(12) NOT NULL,  
00256            val     VARCHAR(255),
00257            INDEX(prid,uid)
00258               )";
00259     return(mysql_query($sql,$dbh));
00260   }
00261 
00262   // Defines the properties and sections.
00263   function install_prop($dbh){
00264     require_once("GenieGate/Properties.php");
00265     $pm = new GenieGate_Properties($dbh);       
00266     $this->defineSection($pm,"genie.form.Public","Properties users are allowed to set");
00267     $this->defineSection($pm,"genie.form.Private","Internal properties, users cannot view/alter.");
00268 
00269     echo "<TABLE COLS=\"2\" WIDTH=\"80%\" ALIGN=\"CENTER\" >";    
00270     echo "<TR><TH COLSPAN=\"2\">Properties that users are allowed to set</TH></TR>";
00271     echo "<TR><TH>Property</TH><TH>Name</TH></TR>";
00272     foreach($this->PUB_PROP as $pkey => $name){
00273       echo "<TR><TD WIDTH=\"15%\">[$pkey]</TD><TD>$name</TD></TR>";
00274       $pm->createProperty("genie.form.Public",$pkey,$name);
00275     }
00276     echo "<P></TABLE>";
00277     echo "<TABLE COLS=\"2\" WIDTH=\"80%\" ALIGN=\"CENTER\" >";    
00278     echo "<TR><TH COLSPAN=\"2\">Properties that users are not allowed to set</TH></TR>";
00279     echo "<TR><TH>Property</TH><TH>Name</TH></TR>";
00280     foreach($this->PRIV_PROP as $pkey => $name){
00281       echo "<TR><TD WIDTH=\"15%\">[$pkey]</TD><TD>$name</TD></TR>";
00282       $pm->createProperty("genie.form.Private",$pkey,$name);
00283     }
00284     echo "</TABLE>";
00285   }
00286   // Define a section, if not exist.
00287   function defineSection($pm,$skey,$sname){   
00288     if($pm->getSectionId($skey)){
00289       return;
00290     }
00291     $pm->createSection($skey,$sname);
00292   }
00293 
00294 }
00295 
00296 ?>
00297 </BODY>
00298 </HTML>

DoxyGen Documentation generated by DoxyGen