CVS update: /cowiki/htdocs/setup/class/task/
[email protected] 6 May 2005 02:14:11 -0000
| Newsgroups | gmane.comp.php.cowiki.cvs |
|---|---|
| Message-ID | <[email protected]> |
User: dgorski Date: 2005/05/05 19:14:11 Modified: cowiki/htdocs/setup/class/task/class.TaskDocumentStorageConnector.php Log: Simplified File Changes: Directory: /cowiki/htdocs/setup/class/task/ =========================================== File [changed]: class.TaskDocumentStorageConnector.php Url: http://cowiki.tigris.org/source/browse/cowiki/htdocs/setup/class/task/class.TaskDocumentStorageConnector.php?r1=1.2&r2=1.3 Delta lines: +116 -324 ----------------------- --- class.TaskDocumentStorageConnector.php 27 Apr 2005 04:36:44 -0000 1.2 +++ class.TaskDocumentStorageConnector.php 6 May 2005 02:14:09 -0000 1.3 @@ -2,7 +2,7 @@ /** * - * $Id: class.TaskDocumentStorageConnector.php,v 1.2 2005/04/27 04:36:44 dgorski Exp $ + * $Id: class.TaskDocumentStorageConnector.php,v 1.3 2005/05/06 02:14:09 dgorski Exp $ * * This file is part of coWiki. coWiki is free software under the terms of * the GNU General Public License (GPL). Read the LICENSE file. If you did @@ -17,7 +17,7 @@ * @author Daniel T. Gorski, <[email protected]> * @copyright (C) Daniel T. Gorski, {@link http://www.develnet.org} * @license http://www.gnu.org/licenses/gpl.html - * @version $Revision: 1.2 $ + * @version $Revision: 1.3 $ * */ @@ -35,6 +35,7 @@ const MYSQL_REQUIRED_VERSION = '4.0.0'; const MYSQLI_REQUIRED_VERSION = '4.1.0'; const POSTGRES_REQUIRED_VERSION = '7.4.5'; + const SQLITE_REQUIRED_VERSION = '3.2.0'; private $Conf = null; @@ -261,335 +262,123 @@ echo $this->Tpl->parse(SETUP_PATH.'/tpl/window.default.tpl'); } - // -------------------------------------------------------------------- + // ==================================================================== + // Basic connection check private function checkDBConnection() { - switch ($this->State->getDocStorageScheme()) { - case 'mysql': - $this->checkDBConnection_MySQL(); - break; - - case 'mysqli': - $this->checkDBConnection_MySQLi(); - break; + try { + $sDSN = $this->State->mustCreateNewDocumentDatabase() + ? $this->State->getDocumentStorageRootDSN() + : $this->State->getDocumentStorageDSN(); - case 'mysql+innodb': - $this->checkDBConnection_MySQL_InnoDB(); - break; - - case 'mysqli+innodb': - $this->checkDBConnection_MySQLi_InnoDB(); - break; + $Storage = StorageFactory::getInstance() + ->createDocumentStorage($sDSN); - default: - $this->addError('Unsupported storage container.'); - break; - } - } - - // ==================================================================== + // Do we have basically the capability to access this database? + } catch (StorageApiException $sae) { + return $this->addError( + 'Your PHP installation basically does not support + access to the database container you have chosen.' + ); - // Basic connection check for MySQL - private function checkDBConnection_MySQL() { + } catch (StorageConnectionFailureException $scfe) { + return $this->addError($scfe->getExceptionMessage()); - // Do we have basically access to this database? - if (!function_exists('mysql_connect')) { - $sStr = 'Your PHP installation basically does not support - simple access to MySQL. The MySQL extension is missing.'; - - if ($this->State->isWindows()) { - $sStr .= ' Activate the <tt>php_mysql.dll</tt> entry in - your <tt>php.ini</tt>'; - } else { - $sStr .= ' Reconfigure and recompile PHP with the MySQL - extension and link the correct client libraries.'; + } catch (StorageDatabaseSelectionException $sdse) { + if (!$this->State->mustCreateNewDocumentDatabase()) { + return $this->addError($sdse->getExceptionMessage()); } - - return $this->addError($sStr); } // ---------------------------------------------------------------- - $bHasValidDBAdmin = false; + // Remove alpha chars from version string and get storage name + $sVer = preg_replace('#[^0-9.]#', '', $Storage->getStorageVersion()); + $sName = $Storage->getStorageName(); - // If we have been given the administrator credentials, try to - // connect to the database as root. If this works, a few of - // following checks can be skipped. - if ($this->State->mustCreateNewDocumentDatabase()) { - $sUser = $this->State->getDocStorageAdminUser(); - $sPass = $this->State->getDocStorageAdminPassword(); - - $bHasValidDBAdmin = true; - - } else { - $sUser = $this->State->getDocStorageConnectorUser(); - $sPass = $this->State->getDocStorageConnectorPassword(); - } - - $sHost = $this->State->getDocHost(); - $sPort = $this->State->getDocHostPort(); - if ($sPort != '') { $sHost = $sHost . ':' . $sPort; } + // Required version dummy + $sReqVersion = 'unknown'; - // ---------------------------------------------------------------- - - // Try to connect to database with given root or connector user - // credentials - if (!($rLink = @mysql_connect($sHost, $sUser, $sPass))) { - - // Try to check the cases where a user selected to use an - // existing database, but this database does not yet exist. - switch (mysql_errno()) { - case 1045: - $this->addError( - 'Are you sure that this database exists? If it does, - the given connector user is not set up correctly.' - ); + switch (strtolower($sName)) { + case 'mysql': + $sReqVersion = self::MYSQL_REQUIRED_VERSION; break; - default: - $this->addError( - 'Got error #'.mysql_errno().' from database. - We don\'t know what it is. Please report it.' - ); + case 'mysqli': + $sReqVersion = self::MYSQLI_REQUIRED_VERSION; break; - } - - return $this->addError('MySQL connection error: '.mysql_error()); - } - // Try to select the given database. - $sDatabase = $this->State->getDocDatabaseName(); - - if ($this->State->mustCreateNewDocumentDatabase()) { - if (@mysql_select_db($sDatabase, $rLink)) { - return $this->addError( - 'Database with this name already exists, but you - have chosen to create a new one. Go back and change - your settings or use an other database name.' - ); - } - } else { - if (!@mysql_select_db($sDatabase, $rLink)) { - return $this->addError( - 'MySQL database selection error: '.mysql_error().'. - You have chosen to use an existing database, but it - isn\'t there.' - ); - } - } - - // ---------------------------------------------------------------- + case 'postgres': + $sReqVersion = self::POSTGRES_REQUIRED_VERSION; + break; - // Check version of database - $sQuery = 'SELECT VERSION() AS version'; - $rResult = @mysql_query($sQuery); - $aData = @mysql_fetch_array($rResult, MYSQL_ASSOC); + case 'sqlite': + $sReqVersion = self::SQLITE_REQUIRED_VERSION; + break; - if (!isset($aData['version'])) { - return $this->addError( - 'Could not determine the version of the database. Something - appears to be broken.' - ); + default: + return $this->addError('Unknown storage API.'); } - // Remove alpha chars from version string - $sVar = preg_replace('#[^0-9.]#', '', $aData['version']); - // Abuse PHPs version_compare() function - if (version_compare($sVar, self::MYSQL_REQUIRED_VERSION) == -1) { + if (version_compare($sVer, $sReqVersion) == -1) { return $this->addError( - COWIKI_FULL_IDENT . ' requires at least MySQL version ' - . self::MYSQL_REQUIRED_VERSION . ' to work properly. Your - MySQL version is ' . $aData['version'] . '. Please update - your MySQL server to the latest available stable version.' + COWIKI_FULL_IDENT . ' requires at least '.$sName.' version ' + . $sReqVersion . ' to work properly. Your '.$sName.' version' + .' is ' . $sVer . '. Please update your '.$sName.' server' + .' to the latest available stable version.' ); } // ---------------------------------------------------------------- - if (!$bHasValidDBAdmin) { - - // Determine required connector user privileges. MySQL - // unfortunately returns only a string that we have to scan for - // the right parts. - $sQuery = 'SHOW GRANTS FOR '.$sUser.'@'.$sHost; - $rResult = @mysql_query($sQuery); - - $sStr = ''; - while ($aData = @mysql_fetch_row($rResult)) { - $sStr .= $aData[0] . "\n"; - } - - // Check for GRANT ALL PRIVILEGES. This is if user has all - // privs. This is a bad setting as the _connector_ user has all - // administrative access to the database, but that is not our - // problem. - if (preg_match('#GRANT ALL PRIV#', $sStr)) { - return; - } - - // Check for following privileges that are required by the - // connector user: - $aRequired = array( - 'SELECT', 'INSERT', 'UPDATE', 'DELETE', - 'CREATE', 'DROP', 'INDEX', 'ALTER' - ); - $aLack = array(); - - foreach ($aRequired as $sVal) { - if (!preg_match('#'.preg_quote($sVal).'#', $sStr)) { - $aLack[] = $sVal; - } - } - - if (sizeof($aLack)) { - return $this->addError( - 'The given user \''.$sUser.'\' lacks of following - privileges for the MySQL database table - \''.$sDatabase.'\': ' . join(', ', $aLack) . '.' - ); - } - } - - return true; - } - - // ==================================================================== - - // Basic connection check for MySQLi ('MySQL improved' interface) - private function checkDBConnection_MySQLi() { - - // Do we have basically access to this database? - if (!function_exists('mysqli_connect')) { - $sStr = 'Your PHP installation basically does not support - access to MySQL via the MySQLi interface. The MySQLi - extension is missing.'; - - if ($this->State->isWindows()) { - $sStr .= ' Activate the <tt>php_mysqli.dll</tt> entry in - your <tt>php.ini</tt>'; - } else { - $sStr .= ' Reconfigure and recompile PHP with the MySQLi - extension and link the correct client libraries.'; - } - - return $this->addError($sStr); - } - - // ---------------------------------------------------------------- - - $bHasValidDBAdmin = false; - - // If we have been given the administrator credentials, try to - // connect to the database as root. If this works, a few of - // following checks can be skipped. + // If we have to create a new database connector user, do not care + // about the existing one. if ($this->State->mustCreateNewDocumentDatabase()) { - $sUser = $this->State->getDocStorageAdminUser(); - $sPass = $this->State->getDocStorageAdminPassword(); - - $bHasValidDBAdmin = true; - - } else { - $sUser = $this->State->getDocStorageConnectorUser(); - $sPass = $this->State->getDocStorageConnectorPassword(); + return true; } - $sHost = $this->State->getDocHost(); - $sPort = $this->State->getDocHostPort(); - if ($sPort != '') { $sHost = $sHost . ':' . $sPort; } - // ---------------------------------------------------------------- - // Try to connect to database with given root or connector user - // credentials - if (!($rLink = @mysqli_connect($sHost, $sUser, $sPass))) { - - // Try to check the cases where a user selected to use an - // existing database, but this database does not yet exist. - switch (mysqli_connect_errno()) { - case 1045: - $this->addError( - 'Are you sure that this database exists? If it does, - the given connector user is not set up correctly.' - ); + // Check the user privileges/permissions + switch (strtolower($sName)) { + case 'mysql': + case 'mysqli': + return $this->checkMySqlUserPrivileges($Storage); break; - default: - $this->addError( - 'Got error #'.mysqli_connect_errno().' from database. - We don\'t know what it is. Please report it.' - ); + case 'postgres': + return $this->checkPostgresUserPrivileges($Storage); break; - } - - return $this->addError( - 'MySQLi connection error: '.mysqli_connect_error() - ); - } - // Try to select the given database. - $sDatabase = $this->State->getDocDatabaseName(); - - if ($this->State->mustCreateNewDocumentDatabase()) { - if (@mysqli_select_db($rLink, $sDatabase)) { - return $this->addError( - 'Database with this name already exists, but you - have chosen to create a new one. Go back and change - your settings or use an other database name.' - ); - } - } else { - if (!@mysqli_select_db($rLink, $sDatabase)) { - return $this->addError( - 'MySQLi database selection error: '.mysqli_error($rLink).'. - You have chosen to use an existing database, but it - isn\'t there.' - ); - } - } - - // ---------------------------------------------------------------- - - // Check version of database - $sQuery = 'SELECT VERSION() AS version'; - $rResult = @mysqli_query($rLink, $sQuery); - $aData = @mysqli_fetch_array($rResult); + case 'sqlite': + return $this->checkSqliteUserPrivileges($Storage); + break; - if (!isset($aData['version'])) { - return $this->addError( - 'Could not determine the version of the database. Something - appears to be broken.' - ); + default: + return $this->addError('Unknown storage API.'); } - // Remove alpha chars from version string - $sVar = preg_replace('#[^0-9.]#', '', $aData['version']); - - // Abuse PHPs version_compare() function - if (version_compare($sVar, self::MYSQLI_REQUIRED_VERSION) == -1) { - return $this->addError( - COWIKI_FULL_IDENT . ' - using the MySQLi interface - - requires at least MySQL version ' - . self::MYSQLI_REQUIRED_VERSION . ' to work properly. Your - MySQL version is ' . $aData['version'] . '. Please update - your MySQL server to the latest available stable version, - that provides the MySQLi interface.' - ); + return true; } - // ---------------------------------------------------------------- + // -------------------------------------------------------------------- - if (!$bHasValidDBAdmin) { + private function checkMySqlUserPrivileges($Storage) { // Determine required connector user privileges. MySQL // unfortunately returns only a string that we have to scan for // the right parts. - $sQuery = 'SHOW GRANTS FOR '.$sUser.'@'.$sHost; - $rResult = @mysqli_query($rLink, $sQuery); + $sQuery = 'SHOW GRANTS FOR ' + . $Storage->getUserName() + . '@' + . $Storage->getHostString(); + + $rResult = $Storage->query($sQuery); $sStr = ''; - while ($aData = @mysqli_fetch_row($rResult)) { + while ($aData = $Storage->fetchRow($rResult)) { $sStr .= $aData[0] . "\n"; } @@ -617,26 +406,29 @@ if (sizeof($aLack)) { return $this->addError( - 'The given user \''.$sUser.'\' lacks of following - privileges for the MySQL database - table \''.$sDatabase.'\': ' . join(', ', $aLack) . '.' + 'The given user \''.$Storage->getUserName().'\' lacks of + following privileges for the '.$Storage->getStorageName().' + database \''.$Storage->getDatabase().'\': ' + . join(', ', $aLack) . '.' ); } - } - return true; } - // ==================================================================== + // -------------------------------------------------------------------- - private function checkDBConnection_MySQL_InnoDB() { - // FIX: Check for InnoDB capability - return $this->checkDBConnection_MySQL(); + private function checkPostgresUserPrivileges($Storage) { + return $this->addError( + 'User privilege check not implemented for Postgres. Halt.' + ); } - private function checkDBConnection_MySQLi_InnoDB() { - // FIX: Check for InnoDB capability - return $this->checkDBConnection_MySQLi(); + // -------------------------------------------------------------------- + + private function checkSqliteUserPrivileges($Storage) { + return $this->addError( + 'User privilege check not implemented for SQLite. Halt.' + ); } } // of class