install/includes/stages/database_mysql.php
changeset 1379 5cbd678df965
parent 1227 bdac73ed481e
equal deleted inserted replaced
1378:e58294b867c1 1379:5cbd678df965
    13  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    13  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    14  */
    14  */
    15 
    15 
    16 if ( !defined('IN_ENANO_INSTALL') )
    16 if ( !defined('IN_ENANO_INSTALL') )
    17 	die();
    17 	die();
       
    18 
       
    19 function pdo_escape($pdo, $str)
       
    20 {
       
    21 	return substr($pdo->quote($str), 1, -1);
       
    22 }
    18 
    23 
    19 if ( isset($_POST['_cont']) )
    24 if ( isset($_POST['_cont']) )
    20 {
    25 {
    21 	$allow_go = true;
    26 	$allow_go = true;
    22 	// Do we have everything? If so, continue with installation.
    27 	// Do we have everything? If so, continue with installation.
    82 	if ( $info['db_host'] == 'localhost' && !empty($info['db_port']) && $info['db_port'] != 3306 )
    87 	if ( $info['db_host'] == 'localhost' && !empty($info['db_port']) && $info['db_port'] != 3306 )
    83 		$info['db_host'] = '127.0.0.1';
    88 		$info['db_host'] = '127.0.0.1';
    84 	
    89 	
    85 	$dbhost = ( preg_match('/^:/', $info['db_host']) ) ? $info['db_host'] : "{$info['db_host']}:{$info['db_port']}";
    90 	$dbhost = ( preg_match('/^:/', $info['db_host']) ) ? $info['db_host'] : "{$info['db_host']}:{$info['db_port']}";
    86 	
    91 	
    87 	// Try to connect as the normal user
    92 	if ( have_pdo('mysql') )
    88 	$test = @mysql_connect($dbhost, $info['db_user'], $info['db_pass']);
    93 	{
    89 	if ( !$test )
    94 		// Try to connect as the normal user
    90 	{
    95 		try
    91 		$return['creating_user'] = true;
    96 		{
    92 		$return['last_error'] = mysql_error();
    97 			$test = new PDO("mysql:host=$dbhost;charset=UTF8", $info['db_user'], $info['db_pass']);
    93 		if ( strstr( $return['last_error'], 'Lost connection' ) || strstr( $return['last_error'], 'Unknown MySQL server host' ) )
    98 			
    94 		{
    99 			// We're connected; do we have permission to use the database?
    95 			$return['host_good'] = false;
   100 			$have_database = false;
    96 		}
   101 			$q = $test->query('USE `' . pdo_escape($test, $info['db_name']) . '`;');
    97 		// Doing that failed. If we have root credentials, test those
   102 			if ( $q )
    98 		if ( !empty($info['db_root_user']) && !empty($info['db_root_pass']) )
   103 			{
    99 		{
   104 				// Permissions are good and we're all connected. Perform version check...
   100 			// Log in with root rights and if that works, tell 'em we'll reset the password or create
   105 				$version = $test->getAttribute(PDO::ATTR_SERVER_VERSION);
   101 			// the account if it doesn't exist already. This is done with GRANT ALL PRIVILEGES ON enano_db.*
       
   102 			// etc etc, a little hackish but known to work with MySQL >= 4.1.
       
   103 			$test_root = @mysql_connect($dbhost, $info['db_root_user'], $info['db_root_pass']);
       
   104 			if ( $test_root )
       
   105 			{
       
   106 				// We logged in with root rights, assume that we have appropriate permissions.
       
   107 				// If not, well, the installation will fail. Tough on the user, but creating
       
   108 				// test databases/users is too risky.
       
   109 				
       
   110 				// Does the database exist?
       
   111 				$q = @mysql_query('USE `' . mysql_real_escape_string($info['db_name']) . '`;', $test_root);
       
   112 				if ( !$q )
       
   113 				{
       
   114 					// Nope, we'll have to create it
       
   115 					$return['creating_db'] = true;
       
   116 					$return['last_error'] = mysql_error();
       
   117 				}
       
   118 				
       
   119 				$version = mysql_get_server_info($test_root);
       
   120 				$return['version'] = array(
   106 				$return['version'] = array(
   121 					'version' => $version,
   107 					'version' => $version,
   122 					'good' => version_compare($version, '4.0.17', '>=')
   108 					'good' => version_compare($version, '4.0.17', '>=')
   123 				);
   109 				);
   124 				
   110 				
   125 				$return['can_install'] = ( $return['version']['good'] ) ? true : false;
   111 				$return['can_install'] = ( $return['version']['good'] ) ? true : false;
   126 			}
   112 			}
   127 			else
   113 			else
   128 			{
   114 			{
   129 				// Well that helped. Root credentials are bad.
   115 				$return['last_error'] = mysql_error();
   130 				$return['creating_db'] = true;
   116 				$return['creating_db'] = true;
       
   117 				
       
   118 				// We don't have permission to use the database or it doesn't exist.
       
   119 				// See if we have a root login to work with, if not then fail
       
   120 				if ( !empty($info['db_root_user']) && !empty($info['db_root_pass']) )
       
   121 				{
       
   122 					// Log in with root rights and if that works, tell 'em we'll create the database.
       
   123 					try
       
   124 					{
       
   125 						$test_root = new PDO("mysql:host=$dbhost;charset=UTF8", $info['db_root_user'], $info['db_root_pass']);
       
   126 						
       
   127 						// We logged in with root rights, assume that we have appropriate permissions.
       
   128 						// If not, well, the installation will fail. Tough on the user, but creating
       
   129 						// test databases/users is too risky.
       
   130 						
       
   131 						// See if the database already exists
       
   132 						$dbname = pdo_escape($test_root, $info['db_name']);
       
   133 						$q = $test_root->query("SHOW DATABASES LIKE '$dbname';");
       
   134 						if ( $q )
       
   135 						{
       
   136 							if ( $q->rows() > 0 )
       
   137 							{
       
   138 								$return['creating_db'] = false;
       
   139 								$return['creating_db_grant'] = true;
       
   140 							}
       
   141 						}
       
   142 						
       
   143 						$version = $test->getAttribute(PDO::ATTR_SERVER_VERSION);
       
   144 						$return['version'] = array(
       
   145 							'version' => $version,
       
   146 							'good' => version_compare($version, '4.0.17', '>=')
       
   147 						);
       
   148 						
       
   149 						$return['can_install'] = ( $return['version']['good'] ) ? true : false;
       
   150 					}
       
   151 					catch ( PDOException $e )
       
   152 					{
       
   153 						// Well that helped. Root credentials are bad.
       
   154 						$return['creating_db'] = true;
       
   155 						$return['root_fail'] = true;
       
   156 					}
       
   157 				}
       
   158 				// No root credentials, fail out
       
   159 			}
       
   160 		}
       
   161 		catch ( PDOException $e )
       
   162 		{
       
   163 			$return['creating_user'] = true;
       
   164 			$return['last_error'] = mysql_error();
       
   165 			if ( strstr( $return['last_error'], 'Lost connection' ) || strstr( $return['last_error'], 'Unknown MySQL server host' ) )
       
   166 			{
       
   167 				$return['host_good'] = false;
       
   168 			}
       
   169 			// Doing that failed. If we have root credentials, test those
       
   170 			if ( !empty($info['db_root_user']) && !empty($info['db_root_pass']) )
       
   171 			{
       
   172 				// Log in with root rights and if that works, tell 'em we'll reset the password or create
       
   173 				// the account if it doesn't exist already. This is done with GRANT ALL PRIVILEGES ON enano_db.*
       
   174 				// etc etc, a little hackish but known to work with MySQL >= 4.1.
       
   175 				try
       
   176 				{
       
   177 					$test_root = new PDO("mysql:host=$dbhost;charset=UTF8", $info['db_root_user'], $info['db_root_pass']);
       
   178 					
       
   179 					// We logged in with root rights, assume that we have appropriate permissions.
       
   180 					// If not, well, the installation will fail. Tough on the user, but creating
       
   181 					// test databases/users is too risky.
       
   182 					
       
   183 					// Does the database exist?
       
   184 					$q = $test_root->query('USE `' . mysql_real_escape_string($info['db_name']) . '`;');
       
   185 					if ( !$q )
       
   186 					{
       
   187 						// Nope, we'll have to create it
       
   188 						$return['creating_db'] = true;
       
   189 						$return['last_error'] = mysql_error();
       
   190 					}
       
   191 					
       
   192 					$version = $test_root->getAttribute(PDO::ATTR_SERVER_VERSION);
       
   193 					$return['version'] = array(
       
   194 						'version' => $version,
       
   195 						'good' => version_compare($version, '4.0.17', '>=')
       
   196 					);
       
   197 					
       
   198 					$return['can_install'] = ( $return['version']['good'] ) ? true : false;
       
   199 				}
       
   200 				catch ( PDOException $e )
       
   201 				{
       
   202 					// Well that helped. Root credentials are bad.
       
   203 					$return['creating_db'] = true;
       
   204 					$return['root_fail'] = true;
       
   205 				}
       
   206 			}
       
   207 			else
       
   208 			{
       
   209 				// No root credentials, fail out
   131 				$return['root_fail'] = true;
   210 				$return['root_fail'] = true;
   132 			}
   211 			}
   133 		}
   212 		}
   134 		else
       
   135 		{
       
   136 			// No root credentials, fail out
       
   137 			$return['root_fail'] = true;
       
   138 		}
       
   139 	}
   213 	}
   140 	else
   214 	else
   141 	{
   215 	{
   142 		// We're connected; do we have permission to use the database?
   216 		// Try to connect as the normal user
   143 		$have_database = false;
   217 		$test = @mysql_connect($dbhost, $info['db_user'], $info['db_pass']);
   144 		$q = @mysql_query('USE `' . mysql_real_escape_string($info['db_name']) . '`;', $test);
   218 		if ( !$test )
   145 		if ( $q )
   219 		{
   146 		{
   220 			$return['creating_user'] = true;
   147 			// Permissions are good and we're all connected. Perform version check...
       
   148 			$version = mysql_get_server_info($test);
       
   149 			$return['version'] = array(
       
   150 				'version' => $version,
       
   151 				'good' => version_compare($version, '4.0.17', '>=')
       
   152 			);
       
   153 			
       
   154 			$return['can_install'] = ( $return['version']['good'] ) ? true : false;
       
   155 		}
       
   156 		else
       
   157 		{
       
   158 			$return['last_error'] = mysql_error();
   221 			$return['last_error'] = mysql_error();
   159 			$return['creating_db'] = true;
   222 			if ( strstr( $return['last_error'], 'Lost connection' ) || strstr( $return['last_error'], 'Unknown MySQL server host' ) )
   160 			
   223 			{
   161 			// We don't have permission to use the database or it doesn't exist.
   224 				$return['host_good'] = false;
   162 			// See if we have a root login to work with, if not then fail
   225 			}
       
   226 			// Doing that failed. If we have root credentials, test those
   163 			if ( !empty($info['db_root_user']) && !empty($info['db_root_pass']) )
   227 			if ( !empty($info['db_root_user']) && !empty($info['db_root_pass']) )
   164 			{
   228 			{
   165 				// Log in with root rights and if that works, tell 'em we'll create the database.
   229 				// Log in with root rights and if that works, tell 'em we'll reset the password or create
       
   230 				// the account if it doesn't exist already. This is done with GRANT ALL PRIVILEGES ON enano_db.*
       
   231 				// etc etc, a little hackish but known to work with MySQL >= 4.1.
   166 				$test_root = @mysql_connect($dbhost, $info['db_root_user'], $info['db_root_pass']);
   232 				$test_root = @mysql_connect($dbhost, $info['db_root_user'], $info['db_root_pass']);
   167 				if ( $test_root )
   233 				if ( $test_root )
   168 				{
   234 				{
   169 					// We logged in with root rights, assume that we have appropriate permissions.
   235 					// We logged in with root rights, assume that we have appropriate permissions.
   170 					// If not, well, the installation will fail. Tough on the user, but creating
   236 					// If not, well, the installation will fail. Tough on the user, but creating
   171 					// test databases/users is too risky.
   237 					// test databases/users is too risky.
   172 					
   238 					
   173 					// See if the database already exists
   239 					// Does the database exist?
   174 					$dbname = mysql_real_escape_string($info['db_name']);
   240 					$q = @mysql_query('USE `' . mysql_real_escape_string($info['db_name']) . '`;', $test_root);
   175 					$q = @mysql_query("SHOW DATABASES LIKE '$dbname';", $test_root);
   241 					if ( !$q )
   176 					if ( $q )
   242 					{
   177 					{
   243 						// Nope, we'll have to create it
   178 						if ( mysql_num_rows($q) > 0 )
   244 						$return['creating_db'] = true;
   179 						{
   245 						$return['last_error'] = mysql_error();
   180 							$return['creating_db'] = false;
       
   181 							$return['creating_db_grant'] = true;
       
   182 						}
       
   183 						@mysql_free_result($q);
       
   184 					}
   246 					}
   185 					
   247 					
   186 					$version = mysql_get_server_info($test);
   248 					$version = mysql_get_server_info($test_root);
   187 					$return['version'] = array(
   249 					$return['version'] = array(
   188 						'version' => $version,
   250 						'version' => $version,
   189 						'good' => version_compare($version, '4.0.17', '>=')
   251 						'good' => version_compare($version, '4.0.17', '>=')
   190 					);
   252 					);
   191 					
   253 					
   196 					// Well that helped. Root credentials are bad.
   258 					// Well that helped. Root credentials are bad.
   197 					$return['creating_db'] = true;
   259 					$return['creating_db'] = true;
   198 					$return['root_fail'] = true;
   260 					$return['root_fail'] = true;
   199 				}
   261 				}
   200 			}
   262 			}
   201 			// No root credentials, fail out
   263 			else
       
   264 			{
       
   265 				// No root credentials, fail out
       
   266 				$return['root_fail'] = true;
       
   267 			}
       
   268 		}
       
   269 		else
       
   270 		{
       
   271 			// We're connected; do we have permission to use the database?
       
   272 			$have_database = false;
       
   273 			$q = @mysql_query('USE `' . mysql_real_escape_string($info['db_name']) . '`;', $test);
       
   274 			if ( $q )
       
   275 			{
       
   276 				// Permissions are good and we're all connected. Perform version check...
       
   277 				$version = mysql_get_server_info($test);
       
   278 				$return['version'] = array(
       
   279 					'version' => $version,
       
   280 					'good' => version_compare($version, '4.0.17', '>=')
       
   281 				);
       
   282 				
       
   283 				$return['can_install'] = ( $return['version']['good'] ) ? true : false;
       
   284 			}
       
   285 			else
       
   286 			{
       
   287 				$return['last_error'] = mysql_error();
       
   288 				$return['creating_db'] = true;
       
   289 				
       
   290 				// We don't have permission to use the database or it doesn't exist.
       
   291 				// See if we have a root login to work with, if not then fail
       
   292 				if ( !empty($info['db_root_user']) && !empty($info['db_root_pass']) )
       
   293 				{
       
   294 					// Log in with root rights and if that works, tell 'em we'll create the database.
       
   295 					$test_root = @mysql_connect($dbhost, $info['db_root_user'], $info['db_root_pass']);
       
   296 					if ( $test_root )
       
   297 					{
       
   298 						// We logged in with root rights, assume that we have appropriate permissions.
       
   299 						// If not, well, the installation will fail. Tough on the user, but creating
       
   300 						// test databases/users is too risky.
       
   301 						
       
   302 						// See if the database already exists
       
   303 						$dbname = mysql_real_escape_string($info['db_name']);
       
   304 						$q = @mysql_query("SHOW DATABASES LIKE '$dbname';", $test_root);
       
   305 						if ( $q )
       
   306 						{
       
   307 							if ( mysql_num_rows($q) > 0 )
       
   308 							{
       
   309 								$return['creating_db'] = false;
       
   310 								$return['creating_db_grant'] = true;
       
   311 							}
       
   312 							@mysql_free_result($q);
       
   313 						}
       
   314 						
       
   315 						$version = mysql_get_server_info($test);
       
   316 						$return['version'] = array(
       
   317 							'version' => $version,
       
   318 							'good' => version_compare($version, '4.0.17', '>=')
       
   319 						);
       
   320 						
       
   321 						$return['can_install'] = ( $return['version']['good'] ) ? true : false;
       
   322 					}
       
   323 					else
       
   324 					{
       
   325 						// Well that helped. Root credentials are bad.
       
   326 						$return['creating_db'] = true;
       
   327 						$return['root_fail'] = true;
       
   328 					}
       
   329 				}
       
   330 				// No root credentials, fail out
       
   331 			}
   202 		}
   332 		}
   203 	}
   333 	}
   204 	
   334 	
   205 	if ( isset($test) && @is_resource($test) )
   335 	if ( isset($test) && @is_resource($test) )
   206 		@mysql_close($test);
   336 		@mysql_close($test);