Installer dev, everything is working now, so far as I can tell
authorDan Fuhry <dan@enanocms.org>
Wed, 21 Aug 2013 23:11:58 -0400
changeset 1379 5cbd678df965
parent 1378 e58294b867c1
child 1380 0e400f946644
Installer dev, everything is working now, so far as I can tell
includes/functions.php
install/includes/common.php
install/includes/stages/database_mysql.php
install/includes/stages/database_post.php
install/includes/stages/install.php
install/includes/stages/sysreqs.php
--- a/includes/functions.php	Wed Aug 21 22:50:34 2013 -0400
+++ b/includes/functions.php	Wed Aug 21 23:11:58 2013 -0400
@@ -1004,6 +1004,8 @@
 {
 	global $db, $session, $paths, $template, $plugins; // Common objects
 	
+	require_once(ENANO_ROOT . '/includes/template.php');
+	
 	if ( !defined('scriptPath') )
 		require( ENANO_ROOT . '/config.php' );
 
--- a/install/includes/common.php	Wed Aug 21 22:50:34 2013 -0400
+++ b/install/includes/common.php	Wed Aug 21 23:11:58 2013 -0400
@@ -111,6 +111,7 @@
 require_once(ENANO_ROOT . '/includes/constants.php');
 require_once(ENANO_ROOT . '/includes/rijndael.php');
 require_once(ENANO_ROOT . '/includes/hmac.php');
+require_once(ENANO_ROOT . '/includes/dbal.php');
 
 // If we have at least PHP 5, load json2
 if ( version_compare(PHP_VERSION, '5.0.0', '>=') )
--- a/install/includes/stages/database_mysql.php	Wed Aug 21 22:50:34 2013 -0400
+++ b/install/includes/stages/database_mysql.php	Wed Aug 21 23:11:58 2013 -0400
@@ -16,6 +16,11 @@
 if ( !defined('IN_ENANO_INSTALL') )
 	die();
 
+function pdo_escape($pdo, $str)
+{
+	return substr($pdo->quote($str), 1, -1);
+}
+
 if ( isset($_POST['_cont']) )
 {
 	$allow_go = true;
@@ -84,39 +89,20 @@
 	
 	$dbhost = ( preg_match('/^:/', $info['db_host']) ) ? $info['db_host'] : "{$info['db_host']}:{$info['db_port']}";
 	
-	// Try to connect as the normal user
-	$test = @mysql_connect($dbhost, $info['db_user'], $info['db_pass']);
-	if ( !$test )
+	if ( have_pdo('mysql') )
 	{
-		$return['creating_user'] = true;
-		$return['last_error'] = mysql_error();
-		if ( strstr( $return['last_error'], 'Lost connection' ) || strstr( $return['last_error'], 'Unknown MySQL server host' ) )
-		{
-			$return['host_good'] = false;
-		}
-		// Doing that failed. If we have root credentials, test those
-		if ( !empty($info['db_root_user']) && !empty($info['db_root_pass']) )
+		// Try to connect as the normal user
+		try
 		{
-			// Log in with root rights and if that works, tell 'em we'll reset the password or create
-			// the account if it doesn't exist already. This is done with GRANT ALL PRIVILEGES ON enano_db.*
-			// etc etc, a little hackish but known to work with MySQL >= 4.1.
-			$test_root = @mysql_connect($dbhost, $info['db_root_user'], $info['db_root_pass']);
-			if ( $test_root )
+			$test = new PDO("mysql:host=$dbhost;charset=UTF8", $info['db_user'], $info['db_pass']);
+			
+			// We're connected; do we have permission to use the database?
+			$have_database = false;
+			$q = $test->query('USE `' . pdo_escape($test, $info['db_name']) . '`;');
+			if ( $q )
 			{
-				// We logged in with root rights, assume that we have appropriate permissions.
-				// If not, well, the installation will fail. Tough on the user, but creating
-				// test databases/users is too risky.
-				
-				// Does the database exist?
-				$q = @mysql_query('USE `' . mysql_real_escape_string($info['db_name']) . '`;', $test_root);
-				if ( !$q )
-				{
-					// Nope, we'll have to create it
-					$return['creating_db'] = true;
-					$return['last_error'] = mysql_error();
-				}
-				
-				$version = mysql_get_server_info($test_root);
+				// Permissions are good and we're all connected. Perform version check...
+				$version = $test->getAttribute(PDO::ATTR_SERVER_VERSION);
 				$return['version'] = array(
 					'version' => $version,
 					'good' => version_compare($version, '4.0.17', '>=')
@@ -126,43 +112,123 @@
 			}
 			else
 			{
-				// Well that helped. Root credentials are bad.
+				$return['last_error'] = mysql_error();
 				$return['creating_db'] = true;
-				$return['root_fail'] = true;
+				
+				// We don't have permission to use the database or it doesn't exist.
+				// See if we have a root login to work with, if not then fail
+				if ( !empty($info['db_root_user']) && !empty($info['db_root_pass']) )
+				{
+					// Log in with root rights and if that works, tell 'em we'll create the database.
+					try
+					{
+						$test_root = new PDO("mysql:host=$dbhost;charset=UTF8", $info['db_root_user'], $info['db_root_pass']);
+						
+						// We logged in with root rights, assume that we have appropriate permissions.
+						// If not, well, the installation will fail. Tough on the user, but creating
+						// test databases/users is too risky.
+						
+						// See if the database already exists
+						$dbname = pdo_escape($test_root, $info['db_name']);
+						$q = $test_root->query("SHOW DATABASES LIKE '$dbname';");
+						if ( $q )
+						{
+							if ( $q->rows() > 0 )
+							{
+								$return['creating_db'] = false;
+								$return['creating_db_grant'] = true;
+							}
+						}
+						
+						$version = $test->getAttribute(PDO::ATTR_SERVER_VERSION);
+						$return['version'] = array(
+							'version' => $version,
+							'good' => version_compare($version, '4.0.17', '>=')
+						);
+						
+						$return['can_install'] = ( $return['version']['good'] ) ? true : false;
+					}
+					catch ( PDOException $e )
+					{
+						// Well that helped. Root credentials are bad.
+						$return['creating_db'] = true;
+						$return['root_fail'] = true;
+					}
+				}
+				// No root credentials, fail out
 			}
 		}
-		else
+		catch ( PDOException $e )
 		{
-			// No root credentials, fail out
-			$return['root_fail'] = true;
+			$return['creating_user'] = true;
+			$return['last_error'] = mysql_error();
+			if ( strstr( $return['last_error'], 'Lost connection' ) || strstr( $return['last_error'], 'Unknown MySQL server host' ) )
+			{
+				$return['host_good'] = false;
+			}
+			// Doing that failed. If we have root credentials, test those
+			if ( !empty($info['db_root_user']) && !empty($info['db_root_pass']) )
+			{
+				// Log in with root rights and if that works, tell 'em we'll reset the password or create
+				// the account if it doesn't exist already. This is done with GRANT ALL PRIVILEGES ON enano_db.*
+				// etc etc, a little hackish but known to work with MySQL >= 4.1.
+				try
+				{
+					$test_root = new PDO("mysql:host=$dbhost;charset=UTF8", $info['db_root_user'], $info['db_root_pass']);
+					
+					// We logged in with root rights, assume that we have appropriate permissions.
+					// If not, well, the installation will fail. Tough on the user, but creating
+					// test databases/users is too risky.
+					
+					// Does the database exist?
+					$q = $test_root->query('USE `' . mysql_real_escape_string($info['db_name']) . '`;');
+					if ( !$q )
+					{
+						// Nope, we'll have to create it
+						$return['creating_db'] = true;
+						$return['last_error'] = mysql_error();
+					}
+					
+					$version = $test_root->getAttribute(PDO::ATTR_SERVER_VERSION);
+					$return['version'] = array(
+						'version' => $version,
+						'good' => version_compare($version, '4.0.17', '>=')
+					);
+					
+					$return['can_install'] = ( $return['version']['good'] ) ? true : false;
+				}
+				catch ( PDOException $e )
+				{
+					// Well that helped. Root credentials are bad.
+					$return['creating_db'] = true;
+					$return['root_fail'] = true;
+				}
+			}
+			else
+			{
+				// No root credentials, fail out
+				$return['root_fail'] = true;
+			}
 		}
 	}
 	else
 	{
-		// We're connected; do we have permission to use the database?
-		$have_database = false;
-		$q = @mysql_query('USE `' . mysql_real_escape_string($info['db_name']) . '`;', $test);
-		if ( $q )
+		// Try to connect as the normal user
+		$test = @mysql_connect($dbhost, $info['db_user'], $info['db_pass']);
+		if ( !$test )
 		{
-			// Permissions are good and we're all connected. Perform version check...
-			$version = mysql_get_server_info($test);
-			$return['version'] = array(
-				'version' => $version,
-				'good' => version_compare($version, '4.0.17', '>=')
-			);
-			
-			$return['can_install'] = ( $return['version']['good'] ) ? true : false;
-		}
-		else
-		{
+			$return['creating_user'] = true;
 			$return['last_error'] = mysql_error();
-			$return['creating_db'] = true;
-			
-			// We don't have permission to use the database or it doesn't exist.
-			// See if we have a root login to work with, if not then fail
+			if ( strstr( $return['last_error'], 'Lost connection' ) || strstr( $return['last_error'], 'Unknown MySQL server host' ) )
+			{
+				$return['host_good'] = false;
+			}
+			// Doing that failed. If we have root credentials, test those
 			if ( !empty($info['db_root_user']) && !empty($info['db_root_pass']) )
 			{
-				// Log in with root rights and if that works, tell 'em we'll create the database.
+				// Log in with root rights and if that works, tell 'em we'll reset the password or create
+				// the account if it doesn't exist already. This is done with GRANT ALL PRIVILEGES ON enano_db.*
+				// etc etc, a little hackish but known to work with MySQL >= 4.1.
 				$test_root = @mysql_connect($dbhost, $info['db_root_user'], $info['db_root_pass']);
 				if ( $test_root )
 				{
@@ -170,20 +236,16 @@
 					// If not, well, the installation will fail. Tough on the user, but creating
 					// test databases/users is too risky.
 					
-					// See if the database already exists
-					$dbname = mysql_real_escape_string($info['db_name']);
-					$q = @mysql_query("SHOW DATABASES LIKE '$dbname';", $test_root);
-					if ( $q )
+					// Does the database exist?
+					$q = @mysql_query('USE `' . mysql_real_escape_string($info['db_name']) . '`;', $test_root);
+					if ( !$q )
 					{
-						if ( mysql_num_rows($q) > 0 )
-						{
-							$return['creating_db'] = false;
-							$return['creating_db_grant'] = true;
-						}
-						@mysql_free_result($q);
+						// Nope, we'll have to create it
+						$return['creating_db'] = true;
+						$return['last_error'] = mysql_error();
 					}
 					
-					$version = mysql_get_server_info($test);
+					$version = mysql_get_server_info($test_root);
 					$return['version'] = array(
 						'version' => $version,
 						'good' => version_compare($version, '4.0.17', '>=')
@@ -198,7 +260,75 @@
 					$return['root_fail'] = true;
 				}
 			}
-			// No root credentials, fail out
+			else
+			{
+				// No root credentials, fail out
+				$return['root_fail'] = true;
+			}
+		}
+		else
+		{
+			// We're connected; do we have permission to use the database?
+			$have_database = false;
+			$q = @mysql_query('USE `' . mysql_real_escape_string($info['db_name']) . '`;', $test);
+			if ( $q )
+			{
+				// Permissions are good and we're all connected. Perform version check...
+				$version = mysql_get_server_info($test);
+				$return['version'] = array(
+					'version' => $version,
+					'good' => version_compare($version, '4.0.17', '>=')
+				);
+				
+				$return['can_install'] = ( $return['version']['good'] ) ? true : false;
+			}
+			else
+			{
+				$return['last_error'] = mysql_error();
+				$return['creating_db'] = true;
+				
+				// We don't have permission to use the database or it doesn't exist.
+				// See if we have a root login to work with, if not then fail
+				if ( !empty($info['db_root_user']) && !empty($info['db_root_pass']) )
+				{
+					// Log in with root rights and if that works, tell 'em we'll create the database.
+					$test_root = @mysql_connect($dbhost, $info['db_root_user'], $info['db_root_pass']);
+					if ( $test_root )
+					{
+						// We logged in with root rights, assume that we have appropriate permissions.
+						// If not, well, the installation will fail. Tough on the user, but creating
+						// test databases/users is too risky.
+						
+						// See if the database already exists
+						$dbname = mysql_real_escape_string($info['db_name']);
+						$q = @mysql_query("SHOW DATABASES LIKE '$dbname';", $test_root);
+						if ( $q )
+						{
+							if ( mysql_num_rows($q) > 0 )
+							{
+								$return['creating_db'] = false;
+								$return['creating_db_grant'] = true;
+							}
+							@mysql_free_result($q);
+						}
+						
+						$version = mysql_get_server_info($test);
+						$return['version'] = array(
+							'version' => $version,
+							'good' => version_compare($version, '4.0.17', '>=')
+						);
+						
+						$return['can_install'] = ( $return['version']['good'] ) ? true : false;
+					}
+					else
+					{
+						// Well that helped. Root credentials are bad.
+						$return['creating_db'] = true;
+						$return['root_fail'] = true;
+					}
+				}
+				// No root credentials, fail out
+			}
 		}
 	}
 	
--- a/install/includes/stages/database_post.php	Wed Aug 21 22:50:34 2013 -0400
+++ b/install/includes/stages/database_post.php	Wed Aug 21 23:11:58 2013 -0400
@@ -17,9 +17,9 @@
 	die();
 
 // Start up the DBAL
-require( ENANO_ROOT . '/includes/dbal.php' );
 require( ENANO_ROOT . '/includes/sql_parse.php' );
-$dbal = new $driver();
+$driver_cls = have_pdo($driver) ? "{$driver}_pdo" : $driver;
+$dbal = new $driver_cls();
 $db_host =& $_POST['db_host'];
 $db_port =& $_POST['db_port'];
 $db_user =& $_POST['db_user'];
--- a/install/includes/stages/install.php	Wed Aug 21 22:50:34 2013 -0400
+++ b/install/includes/stages/install.php	Wed Aug 21 23:11:58 2013 -0400
@@ -18,7 +18,6 @@
 
 require ( ENANO_ROOT . '/install/includes/libenanoinstall.php' );
 require ( ENANO_ROOT . '/includes/sql_parse.php' );
-require ( ENANO_ROOT . '/includes/dbal.php' );
 require ( ENANO_ROOT . '/config.new.php' );
 
 if ( !in_array($dbdriver, $supported_drivers) )
--- a/install/includes/stages/sysreqs.php	Wed Aug 21 22:50:34 2013 -0400
+++ b/install/includes/stages/sysreqs.php	Wed Aug 21 23:11:58 2013 -0400
@@ -81,12 +81,12 @@
 }
 
 // Test: MySQL
-$req_mysql = function_exists('mysql_connect');
+$req_mysql = function_exists('mysql_connect') || have_pdo('mysql');
 if ( $req_mysql )
 	$have_dbms = true;
 
 // Test: PostgreSQL
-$req_pgsql = function_exists('pg_connect');
+$req_pgsql = function_exists('pg_connect') || have_pdo('postgresql');
 if ( $req_pgsql )
 	$have_dbms = true;