diff --git a/setup/index.php b/setup/index.php index b301d5c9cc..ce83b1c7d9 100644 --- a/setup/index.php +++ b/setup/index.php @@ -13,15 +13,15 @@ define(MYSQL_MIN_VERSION, '5.0.0'); $sOperation = Utils::ReadParam('operation', 'step1'); $oP = new setup_web_page('iTop configuration wizard'); -$oP->no_cache(); /** * Helper function to check if the current version of PHP * is compatible with the application * @return boolean true if this is Ok, false otherwise */ -function CheckPHPVersion(nice_web_page $oP) +function CheckPHPVersion(setup_web_page $oP) { + $oP->log('Info - CheckPHPVersion'); if (version_compare(phpversion(), PHP_MIN_VERSION, '>=')) { $oP->ok("The current PHP Version (".phpversion().") is greater than the minimum required version (".PHP_MIN_VERSION.")"); @@ -31,13 +31,30 @@ function CheckPHPVersion(nice_web_page $oP) $oP->error("Error: The current PHP Version (".phpversion().") is lower than the minimum required version (".PHP_MIN_VERSION.")"); return false; } - if (extension_loaded('mysql')) + $aMandatoryExtensions = array('mysql', 'iconv', 'simplexml'); + asort($aMandatoryExtensions); // Sort the list to look clean ! + $aExtensionsOk = array(); + $aMissingExtensions = array(); + $aMissingExtensionsLinks = array(); + foreach($aMandatoryExtensions as $sExtension) { - $oP->ok("The required extension 'mysql' is present."); + if (extension_loaded($sExtension)) + { + $aExtensionsOk[] = $sExtension; + } + else + { + $aMissingExtensions[] = $sExtension; + $aMissingExtensionsLinks[] = "$sExtension"; + } } - else + if (count($aExtensionsOk) > 0) { - $oP->error("Error: missing required extension 'mysql'."); + $oP->ok("Required PHP extension(s): ".implode(', ', $aExtensionsOk)."."); + } + if (count($aMissingExtensions) > 0) + { + $oP->error("Missing PHP extension(s): ".implode(', ', $aMissingExtensionsLinks)."."); return false; } return true; @@ -48,9 +65,10 @@ function CheckPHPVersion(nice_web_page $oP) * the existing databases * @return Array The list of databases found in the server */ -function CheckServerConnection(nice_web_page $oP, $sDBServer, $sDBUser, $sDBPwd) +function CheckServerConnection(setup_web_page $oP, $sDBServer, $sDBUser, $sDBPwd) { $aResult = array(); + $oP->log('Info - CheckServerConnection'); try { $oDBSource = new CMDBSource; @@ -66,7 +84,15 @@ function CheckServerConnection(nice_web_page $oP, $sDBServer, $sDBUser, $sDBPwd) $oP->error("Error: Current MySQL version is ($sDBVersion), minimum required version (".MYSQL_MIN_VERSION.")"); return false; } - $aResult = $oDBSource->ListDB(); + try + { + $aResult = $oDBSource->ListDB(); + } + catch(Exception $e) + { + $oP->warning("Warning: unable to enumerate the current databases."); + $aResult = true; // Not an array to differentiate with an empty array + } } catch(Exception $e) { @@ -76,14 +102,16 @@ function CheckServerConnection(nice_web_page $oP, $sDBServer, $sDBUser, $sDBPwd) } return $aResult; } - + /** - * Helper function to create the database structure - * @return boolean true on success, false otherwise - */ -function CreateDatabaseStructure(nice_web_page $oP, Config $oConfig, $sDBName, $sDBPrefix) + * Helper function to initialize the ORM and load the data model + * from the given file + * @param $sConfigFileName string The name of the configuration file to load + * @param $bAllowMissingDatabase boolean Whether or not to allow loading a data model with no corresponding DB + * @return none + */ +function InitDataModel(setup_web_page $oP, $sConfigFileName, $bAllowMissingDatabase = true) { - $oP->info("Creating the structure in '$sDBName' (prefix = '$sDBPrefix')."); require_once('../core/coreexception.class.inc.php'); require_once('../core/attributedef.class.inc.php'); require_once('../core/filterdef.class.inc.php'); @@ -96,7 +124,18 @@ function CreateDatabaseStructure(nice_web_page $oP, Config $oConfig, $sDBName, $ require_once('../core/dbobjectsearch.class.php'); require_once('../core/dbobjectset.class.php'); require_once('../core/userrights.class.inc.php'); - MetaModel::Startup(TMP_CONFIG_FILE, true); // allow missing DB + $oP->log("Info - MetaModel::Startup from file '$sConfigFileName' (AllowMissingDB = $bAllowMissingDatabase)"); + MetaModel::Startup($sConfigFileName, $bAllowMissingDatabase); +} +/** + * Helper function to create the database structure + * @return boolean true on success, false otherwise + */ +function CreateDatabaseStructure(setup_web_page $oP, Config $oConfig, $sDBName, $sDBPrefix) +{ + InitDataModel($oP, TMP_CONFIG_FILE, true); // Allow the DB to NOT exist since we're about to create it ! + $oP->log('Info - CreateDatabaseStructure'); + $oP->info("Creating the structure in '$sDBName' (prefix = '$sDBPrefix')."); //MetaModel::CheckDefinitions(); if (!MetaModel::DBExists()) { @@ -106,6 +145,8 @@ function CreateDatabaseStructure(nice_web_page $oP, Config $oConfig, $sDBName, $ else { $oP->error("Error: database '$sDBName' (prefix = '$sDBPrefix') already exists."); + $oP->p("Tables with conflicting names already exist in the database. + Try selecting another database instance or specifiy a prefix to prevent conflicting table names."); return false; } return true; @@ -115,21 +156,10 @@ function CreateDatabaseStructure(nice_web_page $oP, Config $oConfig, $sDBName, $ * Helper function to create and administrator account for iTop * @return boolean true on success, false otherwise */ -function CreateAdminAccount(nice_web_page $oP, Config $oConfig, $sAdminUser, $sAdminPwd) +function CreateAdminAccount(setup_web_page $oP, Config $oConfig, $sAdminUser, $sAdminPwd) { - require_once('../core/coreexception.class.inc.php'); - require_once('../core/attributedef.class.inc.php'); - require_once('../core/filterdef.class.inc.php'); - require_once('../core/stimulus.class.inc.php'); - require_once('../core/MyHelpers.class.inc.php'); - require_once('../core/expression.class.inc.php'); - require_once('../core/cmdbsource.class.inc.php'); - require_once('../core/sqlquery.class.inc.php'); - require_once('../core/dbobject.class.php'); - require_once('../core/dbobjectsearch.class.php'); - require_once('../core/dbobjectset.class.php'); - require_once('../core/userrights.class.inc.php'); - MetaModel::Startup(TMP_CONFIG_FILE, true); // allow missing DB + $oP->log('Info - CreateAdminAccount'); + InitDataModel($oP, TMP_CONFIG_FILE, true); // allow missing DB if (UserRights::CreateAdministrator($sAdminUser, $sAdminPwd)) { $oP->ok("Administrator account '$sAdminUser' created."); @@ -143,35 +173,82 @@ function CreateAdminAccount(nice_web_page $oP, Config $oConfig, $sAdminUser, $sA } /** - * Helper function to load some sample data into the database + * Helper function to load the standard menus into the database */ -function LoadSampleData(nice_web_page $oP) +function LoadStandardMenus(setup_web_page $oP) { - // TO BE IMPLEMENTED + $oP->log('Info - LoadStandardMenus'); + + $oXml = simplexml_load_file('menus.xml'); + $aReplicas = array(); + foreach($oXml as $oXmlMenu) + { + $iPreviousId = (integer)$oXmlMenu['id']; // Mandatory to cast + $iParentId = (integer)$oXmlMenu->parent_id; // Mandatory to cast + // echo "
PreviousId = $iPreviousId; parent_id: $iParentId
\n"; + $oMenuNode = MetaModel::NewObject('menuNode'); + $oMenuNode->Set('name', $oXmlMenu->name); + $oMenuNode->Set('label', $oXmlMenu->label); + $oMenuNode->Set('hyperlink', $oXmlMenu->hyperlink); + $oMenuNode->Set('template', $oXmlMenu->template); + $oMenuNode->Set('rank', $oXmlMenu->rank); + $oMenuNode->DBInsert(); + $iDstId = $oMenuNode->GetKey(); + $aReplicas[$iPreviousId] = array('dstObj' => $oMenuNode, 'parentId' => $iParentId); + } + + foreach($aReplicas as $iKey => $aReplica) + { + $iSrcParentId = $aReplica['parentId']; + if ($iSrcParentId != 0) + { + if (isset($aReplicas[$iSrcParentId])) + { + $oParentMenu = $aReplicas[$iSrcParentId]['dstObj']; + $oMenu = $aReplica['dstObj']; + $oMenu->Set('parent_id', $oParentMenu->GetKey()); + $oMenu->DBUpdate(); + } + } + } + + $oP->ok("Standard menus have been created successfully."); + return true; +} + +/** + * Helper function to load sample data into the database + */ +function LoadSampleData(setup_web_page $oP) +{ + $oP->log('Info - LoadSampleData'); + $oP->ok("Sample data loaded into the database."); return true; } - + + /** * Display the form for the first step of the configuration wizard * which consists in the database server selection */ -function DisplayStep1(nice_web_page $oP) +function DisplayStep1(setup_web_page $oP) { $sNextOperation = 'step2'; $oP->add("