From b9b3367865b80c8ea6088d0d90f265c203cfe976 Mon Sep 17 00:00:00 2001 From: Ryan Walberg Date: Tue, 27 Nov 2012 16:13:52 +0000 Subject: [PATCH] new validation for setup, checks Smarty installation too. --- src/setup.php | 207 ++++++++++++++++++++++++++++---------------------- 1 file changed, 116 insertions(+), 91 deletions(-) diff --git a/src/setup.php b/src/setup.php index 4aba487..ef1b4e5 100644 --- a/src/setup.php +++ b/src/setup.php @@ -13,17 +13,24 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -include("config.php"); -include("db.php"); +require_once(dirname(__FILE__) . "/includes/config.php"); -$query = "SELECT COUNT(*) AS user_count FROM {$OPT["table_prefix"]}users"; -$rs = mysql_query($query) or die("Could not query: " . mysql_error()); -$row = mysql_fetch_array($rs,MYSQL_ASSOC); -$user_count = $row["user_count"]; -mysql_free_result($rs); -if ($user_count != 0) { - echo "Database has already been set up."; - exit; +$opt = getGlobalOptions(); + +function dbh($opt) { + return new PDO($opt["pdo_connection_string"], $opt["pdo_username"], $opt["pdo_password"]); +} + +$stmt = dbh($opt)->prepare("SELECT COUNT(*) AS user_count FROM {$opt["table_prefix"]}users"); +$stmt->execute(); +if ($row = $stmt->fetch()) { + $user_count = $row["user_count"]; + if (false && $user_count != 0) { + die("Database has already been set up."); + } +} +else { + die("Database has not been created."); } if (isset($_POST["action"])) { @@ -33,93 +40,105 @@ if (isset($_POST["action"])) { $pwd = $_POST["pwd"]; $email = $_POST["email"]; $familyname = $_POST["familyname"]; - if (!get_magic_quotes_gpc()) { - $username = addslashes($username); - $fullname = addslashes($fullname); - $pwd = addslashes($pwd); - $email = addslashes($email); - $familyname = addslashes($familyname); - } // 1. create the family. - $query = "INSERT INTO {$OPT["table_prefix"]}families(familyname) VALUES('$familyname')"; - mysql_query($query) or die("Could not query: " . mysql_error()); + $stmt = dbh($opt)->prepare("INSERT INTO {$opt["table_prefix"]}families(familyname) VALUES(?)"); + $stmt->bindParam(1, $familyname, PDO::PARAM_STR); + $stmt->execute(); // 2. get the familyid. - $query = "SELECT familyid FROM {$OPT["table_prefix"]}families"; - $rs = mysql_query($query) or die("Could not query: " . mysql_error()); - $row = mysql_fetch_assoc($rs) or die("Could not query: " . mysql_error()); - $familyid = $row["familyid"]; - mysql_free_result($rs); + $stmt = dbh($opt)->prepare("SELECT MAX(familyid) AS familyid FROM {$opt["table_prefix"]}families"); + $stmt->execute(); + if ($row = $stmt->fetch()) { + $familyid = $row["familyid"]; + } + else die("No family was created."); // 3. insert the user. - $query = "INSERT INTO {$OPT["table_prefix"]}users(username,fullname,password,email,approved,admin,initialfamilyid) VALUES('$username','$fullname',{$OPT["password_hasher"]}('$pwd'),'$email',1,1,$familyid)"; - mysql_query($query) or die("Could not query: " . mysql_error()); + $stmt = dbh($opt)->prepare("INSERT INTO {$opt["table_prefix"]}users(username,fullname,password,email,approved,admin,initialfamilyid) VALUES(?, ?, {$opt["password_hasher"]}(?), ?, 1, 1, ?)"); + $stmt->bindParam(1, $username, PDO::PARAM_STR); + $stmt->bindParam(2, $fullname, PDO::PARAM_STR); + $stmt->bindParam(3, $pwd, PDO::PARAM_STR); + $stmt->bindParam(4, $email, PDO::PARAM_STR); + $stmt->bindParam(5, $familyid, PDO::PARAM_INT); + $stmt->execute(); // 4. get the userid. - $query = "SELECT userid FROM {$OPT["table_prefix"]}users"; - $rs = mysql_query($query) or die("Could not query: " . mysql_error()); - $row = mysql_fetch_assoc($rs) or die("Could not query: " . mysql_error()); - $userid = $row["userid"]; - mysql_free_result($rs); + $stmt = dbh($opt)->prepare("SELECT MAX(userid) AS userid FROM {$opt["table_prefix"]}users"); + $stmt->execute(); + if ($row = $stmt->fetch()) { + $userid = $row["userid"]; + } + else die("No user was created."); // 5. create the membership. - $query = "INSERT INTO {$OPT["table_prefix"]}memberships(userid,familyid) VALUES($userid,$familyid)"; - mysql_query($query) or die("Could not query: " . mysql_error()); + $stmt = dbh($opt)->prepare("INSERT INTO {$opt["table_prefix"]}memberships(userid,familyid) VALUES(?, ?)"); + $stmt->bindParam(1, $userid, PDO::PARAM_INT); + $stmt->bindParam(2, $familyid, PDO::PARAM_INT); + $stmt->execute(); } } -echo "\r\n"; ?> -Gift Registry - Setup - - + Gift Registry - Setup + + + $value) { + foreach ($opt as $key => $value) { ?> @@ -150,7 +169,7 @@ else { // check their image_subdir for writeability. echo "

"; $parts = pathinfo($_SERVER["SCRIPT_FILENAME"]); - $image_dir = $parts['dirname'] . "/" . $OPT["image_subdir"]; + $image_dir = $parts['dirname'] . "/" . $opt["image_subdir"]; $writeable = is_writable($image_dir); if ($writeable) { echo "$image_dir is writeable, images can be uploaded."; @@ -159,8 +178,14 @@ else { echo "$image_dir is NOT writeable, images cannot be uploaded. Either chmod this directory to allow the web server to write to it, or disable image uploading in config.php."; } echo "

"; + + // check if Smarty works. + echo "

Testing Smarty installation... ensure the result is OK.

"; + require_once(dirname(__FILE__) . "/includes/MySmarty.class.php"); + $smarty = new MySmarty(); + $smarty->testInstall(); ?> - +
@@ -177,42 +202,42 @@ else {
Admin username - "/> + "/>
Admin password - +
Confirm admin password - +
Admin full name - " /> + " />
Admin e-mail address - " /> + " />
Default/initial family name - " /> + " />
- +