setuppage.class.inc.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Web page used for displaying the login form
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once(APPROOT."/application/nicewebpage.class.inc.php");
  25. define('INSTALL_LOG_FILE', APPROOT.'/setup.log');
  26. define ('MODULE_ACTION_OPTIONAL', 1);
  27. define ('MODULE_ACTION_MANDATORY', 2);
  28. define ('MODULE_ACTION_IMPOSSIBLE', 3);
  29. date_default_timezone_set('Europe/Paris');
  30. class SetupWebPage extends NiceWebPage
  31. {
  32. public function __construct($sTitle)
  33. {
  34. parent::__construct($sTitle);
  35. $this->add_linked_script("../js/jquery.blockUI.js");
  36. $this->add_linked_script("./setup.js");
  37. $this->add_style("
  38. body {
  39. background-color: #eee;
  40. margin: 0;
  41. padding: 0;
  42. font-size: 10pt;
  43. overflow-y: auto;
  44. }
  45. #header {
  46. width: 600px;
  47. margin-left: auto;
  48. margin-right: auto;
  49. margin-top: 50px;
  50. padding: 20px;
  51. background: #f6f6f1;
  52. height: 54px;
  53. border-top: 1px solid #000;
  54. border-left: 1px solid #000;
  55. border-right: 1px solid #000;
  56. }
  57. #header img {
  58. border: 0;
  59. vertical-align: middle;
  60. margin-right: 20px;
  61. }
  62. #header h1 {
  63. vertical-align: middle;
  64. height: 54px;
  65. noline-height: 54px;
  66. margin: 0;
  67. }
  68. #setup {
  69. width: 600px;
  70. margin-left: auto;
  71. margin-right: auto;
  72. padding: 20px;
  73. background-color: #fff;
  74. border-left: 1px solid #000;
  75. border-right: 1px solid #000;
  76. border-bottom: 1px solid #000;
  77. }
  78. .center {
  79. text-align: center;
  80. }
  81. h1 {
  82. color: #1C94C4;
  83. font-size: 16pt;
  84. }
  85. h2 {
  86. color: #000;
  87. font-size: 14pt;
  88. }
  89. h3 {
  90. color: #1C94C4;
  91. font-size: 12pt;
  92. font-weight: bold;
  93. }
  94. .next {
  95. width: 100%;
  96. text-align: right;
  97. }
  98. .v-spacer {
  99. padding-top: 1em;
  100. }
  101. button {
  102. margin-top: 1em;
  103. padding-left: 1em;
  104. padding-right: 1em;
  105. }
  106. p.info {
  107. padding-left: 50px;
  108. background: url(../images/info-mid.png) no-repeat left -5px;
  109. min-height: 48px;
  110. }
  111. p.ok {
  112. padding-left: 50px;
  113. background: url(../images/clean-mid.png) no-repeat left -8px;
  114. min-height: 48px;
  115. }
  116. p.warning {
  117. padding-left: 50px;
  118. background: url(../images/messagebox_warning-mid.png) no-repeat left -5px;
  119. min-height: 48px;
  120. }
  121. p.error {
  122. padding-left: 50px;
  123. background: url(../images/stop-mid.png) no-repeat left -5px;
  124. min-height: 48px;
  125. }
  126. td.label {
  127. text-align: left;
  128. }
  129. label.read-only {
  130. color: #666;
  131. cursor: text;
  132. }
  133. td.input {
  134. text-align: left;
  135. }
  136. table.formTable {
  137. border: 0;
  138. cellpadding: 2px;
  139. cellspacing: 0;
  140. }
  141. .wizlabel, .wizinput {
  142. color: #000;
  143. font-size: 10pt;
  144. }
  145. .wizhelp {
  146. color: #333;
  147. font-size: 8pt;
  148. }
  149. #progress {
  150. border:1px solid #000000;
  151. width: 180px;
  152. height: 20px;
  153. line-height: 20px;
  154. text-align: center;
  155. margin: 5px;
  156. }
  157. ");
  158. }
  159. public function info($sText)
  160. {
  161. $this->add("<p class=\"info\">$sText</p>\n");
  162. $this->log_info($sText);
  163. }
  164. public function ok($sText)
  165. {
  166. $this->add("<p class=\"ok\">$sText</p>\n");
  167. $this->log_ok($sText);
  168. }
  169. public function warning($sText)
  170. {
  171. $this->add("<p class=\"warning\">$sText</p>\n");
  172. $this->log_warning($sText);
  173. }
  174. public function error($sText)
  175. {
  176. $this->add("<p class=\"error\">$sText</p>\n");
  177. $this->log_error($sText);
  178. }
  179. public function form($aData)
  180. {
  181. $this->add("<table class=\"formTable\">\n");
  182. foreach($aData as $aRow)
  183. {
  184. $this->add("<tr>\n");
  185. if (isset($aRow['label']) && isset($aRow['input']) && isset($aRow['help']))
  186. {
  187. $this->add("<td class=\"wizlabel\">{$aRow['label']}</td>\n");
  188. $this->add("<td class=\"wizinput\">{$aRow['input']}</td>\n");
  189. $this->add("<td class=\"wizhelp\">{$aRow['help']}</td>\n");
  190. }
  191. else if (isset($aRow['label']) && isset($aRow['help']))
  192. {
  193. $this->add("<td colspan=\"2\" class=\"wizlabel\">{$aRow['label']}</td>\n");
  194. $this->add("<td class=\"wizhelp\">{$aRow['help']}</td>\n");
  195. }
  196. else if (isset($aRow['label']) && isset($aRow['input']))
  197. {
  198. $this->add("<td class=\"wizlabel\">{$aRow['label']}</td>\n");
  199. $this->add("<td colspan=\"2\" class=\"wizinput\">{$aRow['input']}</td>\n");
  200. }
  201. else if (isset($aRow['label']))
  202. {
  203. $this->add("<td colspan=\"3\" class=\"wizlabel\">{$aRow['label']}</td>\n");
  204. }
  205. $this->add("</tr>\n");
  206. }
  207. $this->add("</table>\n");
  208. }
  209. public function output()
  210. {
  211. $this->s_content = "<div id=\"header\"><h1><a href=\"http://www.combodo.com/itop\" target=\"_blank\"><img title=\"iTop by Combodo\" src=\"../images/itop-logo.png\"></a>&nbsp;{$this->s_title}</h1>\n</div><div id=\"setup\">{$this->s_content}\n</div>\n";
  212. return parent::output();
  213. }
  214. public static function log_error($sText)
  215. {
  216. self::log("Error - ".$sText);
  217. }
  218. public static function log_warning($sText)
  219. {
  220. self::log("Warning - ".$sText);
  221. }
  222. public static function log_info($sText)
  223. {
  224. self::log("Info - ".$sText);
  225. }
  226. public static function log_ok($sText)
  227. {
  228. self::log("Ok - ".$sText);
  229. }
  230. public static function log($sText)
  231. {
  232. $hLogFile = @fopen(INSTALL_LOG_FILE, 'a');
  233. if ($hLogFile !== false)
  234. {
  235. $sDate = date('Y-m-d H:i:s');
  236. fwrite($hLogFile, "$sDate - $sText\n");
  237. fclose($hLogFile);
  238. }
  239. }
  240. static $m_aModuleArgs = array(
  241. 'label' => 'One line description shown during the interactive setup',
  242. 'dependencies' => 'array of module ids',
  243. 'mandatory' => 'boolean',
  244. 'visible' => 'boolean',
  245. 'datamodel' => 'array of data model files',
  246. 'dictionary' => 'array of dictionary files',
  247. 'data.struct' => 'array of structural data files',
  248. 'data.sample' => 'array of sample data files',
  249. 'doc.manual_setup' => 'url',
  250. 'doc.more_information' => 'url',
  251. );
  252. static $m_aModules = array();
  253. // All the entries below are list of file paths relative to the module directory
  254. static $m_aFilesList = array('datamodel', 'webservice', 'dictionary', 'data.struct', 'data.sample');
  255. static $m_sModulePath = null;
  256. public static function SetModulePath($sModulePath)
  257. {
  258. self::$m_sModulePath = $sModulePath;
  259. }
  260. public static function AddModule($sFilePath, $sId, $aArgs)
  261. {
  262. if (!array_key_exists('itop_version', $aArgs))
  263. {
  264. // Assume 1.0.2
  265. $aArgs['itop_version'] = '1.0.2';
  266. }
  267. foreach (self::$m_aModuleArgs as $sArgName => $sArgDesc)
  268. {
  269. if (!array_key_exists($sArgName, $aArgs))
  270. {
  271. throw new Exception("Module '$sId': missing argument '$sArgName'");
  272. }
  273. }
  274. self::$m_aModules[$sId] = $aArgs;
  275. foreach(self::$m_aFilesList as $sAttribute)
  276. {
  277. if (isset(self::$m_aModules[$sId][$sAttribute]))
  278. {
  279. // All the items below are list of files, that are relative to the current file
  280. // being loaded, let's update their path to store path relative to the application directory
  281. foreach(self::$m_aModules[$sId][$sAttribute] as $idx => $sRelativePath)
  282. {
  283. self::$m_aModules[$sId][$sAttribute][$idx] = self::$m_sModulePath.'/'.$sRelativePath;
  284. }
  285. }
  286. }
  287. }
  288. public static function GetModules($oP = null)
  289. {
  290. // Order the modules to take into account their inter-dependencies
  291. $aDependencies = array();
  292. foreach(self::$m_aModules as $sId => $aModule)
  293. {
  294. $aDependencies[$sId] = $aModule['dependencies'];
  295. }
  296. $aOrderedModules = array();
  297. $iLoopCount = 1;
  298. while(($iLoopCount < count(self::$m_aModules)) && (count($aDependencies) > 0) )
  299. {
  300. foreach($aDependencies as $sId => $aRemainingDeps)
  301. {
  302. $bDependenciesSolved = true;
  303. foreach($aRemainingDeps as $sDepId)
  304. {
  305. if (!in_array($sDepId, $aOrderedModules))
  306. {
  307. $bDependenciesSolved = false;
  308. }
  309. }
  310. if ($bDependenciesSolved)
  311. {
  312. $aOrderedModules[] = $sId;
  313. unset($aDependencies[$sId]);
  314. }
  315. }
  316. $iLoopCount++;
  317. }
  318. if (count($aDependencies) >0)
  319. {
  320. $sHtml = "<ul><b>Warning: the following modules have unmet dependencies, and have been ignored:</b>\n";
  321. foreach($aDependencies as $sId => $aDeps)
  322. {
  323. $aModule = self::$m_aModules[$sId];
  324. $sHtml.= "<li>{$aModule['label']} (id: $sId), depends on: ".implode(', ', $aDeps)."</li>";
  325. }
  326. $sHtml .= "</ul>\n";
  327. if (is_object($oP))
  328. {
  329. $oP->warning($sHtml);
  330. }
  331. else
  332. {
  333. self::log_warning($sHtml);
  334. }
  335. }
  336. // Return the ordered list, so that the dependencies are met...
  337. $aResult = array();
  338. foreach($aOrderedModules as $sId)
  339. {
  340. $aResult[$sId] = self::$m_aModules[$sId];
  341. }
  342. return $aResult;
  343. }
  344. } // End of class
  345. /**
  346. * Helper function to initialize the ORM and load the data model
  347. * from the given file
  348. * @param $sConfigFileName string The name of the configuration file to load
  349. * @param $bModelOnly boolean Whether or not to allow loading a data model with no corresponding DB
  350. * @return none
  351. */
  352. function InitDataModel($sConfigFileName, $bModelOnly = true, $bUseCache = false)
  353. {
  354. require_once(APPROOT.'/core/log.class.inc.php');
  355. require_once(APPROOT.'/core/kpi.class.inc.php');
  356. require_once(APPROOT.'/core/coreexception.class.inc.php');
  357. require_once(APPROOT.'/core/dict.class.inc.php');
  358. require_once(APPROOT.'/core/attributedef.class.inc.php');
  359. require_once(APPROOT.'/core/filterdef.class.inc.php');
  360. require_once(APPROOT.'/core/stimulus.class.inc.php');
  361. require_once(APPROOT.'/core/MyHelpers.class.inc.php');
  362. require_once(APPROOT.'/core/expression.class.inc.php');
  363. require_once(APPROOT.'/core/cmdbsource.class.inc.php');
  364. require_once(APPROOT.'/core/sqlquery.class.inc.php');
  365. require_once(APPROOT.'/core/dbobject.class.php');
  366. require_once(APPROOT.'/core/dbobjectsearch.class.php');
  367. require_once(APPROOT.'/core/dbobjectset.class.php');
  368. require_once(APPROOT.'/application/cmdbabstract.class.inc.php');
  369. require_once(APPROOT.'/core/userrights.class.inc.php');
  370. require_once(APPROOT.'/setup/moduleinstallation.class.inc.php');
  371. SetupWebPage::log_info("MetaModel::Startup from file '$sConfigFileName' (ModelOnly = $bModelOnly)");
  372. if ($bUseCache)
  373. {
  374. // Reset the cache for the first use !
  375. $oConfig = new Config($sConfigFileName, false);
  376. MetaModel::ResetCache($oConfig);
  377. }
  378. MetaModel::Startup($sConfigFileName, $bModelOnly, $bUseCache);
  379. }
  380. /**
  381. * Search (on the disk) for all defined iTop modules, load them and returns the list (as an array)
  382. * of the possible iTop modules to install
  383. * @param none
  384. * @return Hash A big array moduleID => ModuleData
  385. */
  386. function GetAvailableModules($oP = null)
  387. {
  388. clearstatcache();
  389. ListModuleFiles('modules');
  390. return SetupWebPage::GetModules($oP);
  391. }
  392. /**
  393. * Analyzes the current installation and the possibilities
  394. *
  395. * @param $oP SetupWebPage For accessing the list of loaded modules
  396. * @param $sDBServer string Name/IP of the DB server
  397. * @param $sDBUser username for the DB server connection
  398. * @param $sDBPwd password for the DB server connection
  399. * @param $sDBName Name of the database instance
  400. * @param $sDBPrefix Prefix for the iTop tables in the DB instance
  401. * @return hash Array with the following format:
  402. * array =>
  403. * 'iTop' => array(
  404. * 'version_db' => ... (could be empty in case of a fresh install)
  405. * 'version_code => ...
  406. * )
  407. * <module_name> => array(
  408. * 'version_db' => ...
  409. * 'version_code' => ...
  410. * 'install' => array(
  411. * 'flag' => SETUP_NEVER | SETUP_OPTIONAL | SETUP_MANDATORY
  412. * 'message' => ...
  413. * )
  414. * 'uninstall' => array(
  415. * 'flag' => SETUP_NEVER | SETUP_OPTIONAL | SETUP_MANDATORY
  416. * 'message' => ...
  417. * )
  418. * 'label' => ...
  419. * 'dependencies' => array(<module1>, <module2>, ...)
  420. * 'visible' => true | false
  421. * )
  422. * )
  423. */
  424. function AnalyzeInstallation($oConfig)
  425. {
  426. $aRes = array(
  427. 'iTop' => array(
  428. 'version_db' => '',
  429. 'version_code' => ITOP_VERSION.'.'.ITOP_REVISION,
  430. )
  431. );
  432. $aModules = GetAvailableModules();
  433. foreach($aModules as $sModuleId => $aModuleInfo)
  434. {
  435. list($sModuleName, $sModuleVersion) = GetModuleName($sModuleId);
  436. $sModuleAppVersion = $aModuleInfo['itop_version'];
  437. $aModuleInfo['version_db'] = '';
  438. $aModuleInfo['version_code'] = $sModuleVersion;
  439. if (!in_array($sModuleAppVersion, array('1.0.0', '1.0.1', '1.0.2')))
  440. {
  441. // This module is NOT compatible with the current version
  442. $aModuleInfo['install'] = array(
  443. 'flag' => MODULE_ACTION_IMPOSSIBLE,
  444. 'message' => 'the module is not compatible with the current version of the application'
  445. );
  446. }
  447. elseif ($aModuleInfo['mandatory'])
  448. {
  449. $aModuleInfo['install'] = array(
  450. 'flag' => MODULE_ACTION_MANDATORY,
  451. 'message' => 'the module is part of the application'
  452. );
  453. }
  454. else
  455. {
  456. $aModuleInfo['install'] = array(
  457. 'flag' => MODULE_ACTION_OPTIONAL,
  458. 'message' => ''
  459. );
  460. }
  461. $aRes[$sModuleName] = $aModuleInfo;
  462. }
  463. try
  464. {
  465. CMDBSource::Init($oConfig->GetDBHost(), $oConfig->GetDBUser(), $oConfig->GetDBPwd(), $oConfig->GetDBName());
  466. $aSelectInstall = CMDBSource::QueryToArray("SELECT * FROM ".$oConfig->GetDBSubname()."priv_module_install");
  467. }
  468. catch (MySQLException $e)
  469. {
  470. // No database or eroneous information
  471. $aSelectInstall = array();
  472. }
  473. // Build the list of installed module (get the latest installation)
  474. //
  475. $aInstallByModule = array(); // array of <module> => array ('installed' => timestamp, 'version' => <version>)
  476. foreach ($aSelectInstall as $aInstall)
  477. {
  478. //$aInstall['comment']; // unsused
  479. //$aInstall['parent_id']; // unsused
  480. $iInstalled = strtotime($aInstall['installed']);
  481. $sModuleName = $aInstall['name'];
  482. $sModuleVersion = $aInstall['version'];
  483. if ($sModuleName == 'itop')
  484. {
  485. $aRes['iTop']['version_db'] = $sModuleVersion;
  486. continue;
  487. }
  488. if (array_key_exists($sModuleName, $aInstallByModule))
  489. {
  490. if ($iInstalled < $aInstallByModule[$sModuleName]['installed'])
  491. {
  492. continue;
  493. }
  494. }
  495. $aInstallByModule[$sModuleName]['installed'] = $iInstalled;
  496. $aInstallByModule[$sModuleName]['version'] = $sModuleVersion;
  497. }
  498. // Adjust the list of proposed modules
  499. //
  500. foreach ($aInstallByModule as $sModuleName => $aModuleDB)
  501. {
  502. if (!array_key_exists($sModuleName, $aRes))
  503. {
  504. // A module was installed, it is not proposed in the new build... skip
  505. continue;
  506. }
  507. $aRes[$sModuleName]['version_db'] = $aModuleDB['version'];
  508. if ($aRes[$sModuleName]['install']['flag'] == MODULE_ACTION_MANDATORY)
  509. {
  510. $aRes[$sModuleName]['uninstall'] = array(
  511. 'flag' => MODULE_ACTION_IMPOSSIBLE,
  512. 'message' => 'the module is part of the application'
  513. );
  514. }
  515. else
  516. {
  517. $aRes[$sModuleName]['uninstall'] = array(
  518. 'flag' => MODULE_ACTION_OPTIONAL,
  519. 'message' => ''
  520. );
  521. }
  522. }
  523. return $aRes;
  524. }
  525. /**
  526. * Helper function to interpret the name of a module
  527. * @param $sModuleId string Identifier of the module, in the form 'name/version'
  528. * @return array(name, version)
  529. */
  530. function GetModuleName($sModuleId)
  531. {
  532. if (preg_match('!^(.*)/(.*)$!', $sModuleId, $aMatches))
  533. {
  534. $sName = $aMatches[1];
  535. $sVersion = $aMatches[2];
  536. }
  537. else
  538. {
  539. $sName = $sModuleId;
  540. $sVersion = "";
  541. }
  542. return array($sName, $sVersion);
  543. }
  544. /**
  545. * Helper function to create the database structure
  546. * @return boolean true on success, false otherwise
  547. */
  548. function CreateDatabaseStructure(Config $oConfig, $aSelectedModules, $sMode)
  549. {
  550. if (strlen($oConfig->GetDBSubname()) > 0)
  551. {
  552. SetupWebPage::log_info("Creating the structure in '".$oConfig->GetDBName()."' (table names prefixed by '".$oConfig->GetDBSubname()."').");
  553. }
  554. else
  555. {
  556. SetupWebPage::log_info("Creating the structure in '".$oConfig->GetDBSubname()."'.");
  557. }
  558. //MetaModel::CheckDefinitions();
  559. if ($sMode == 'install')
  560. {
  561. if (!MetaModel::DBExists(/* bMustBeComplete */ false))
  562. {
  563. MetaModel::DBCreate();
  564. SetupWebPage::log_ok("Database structure successfully created.");
  565. }
  566. else
  567. {
  568. if (strlen($oConfig->GetDBSubname()) > 0)
  569. {
  570. throw new Exception("Error: found iTop tables into the database '".$oConfig->GetDBName()."' (prefix: '".$oConfig->GetDBSubname()."'). Please, try selecting another database instance or specify another prefix to prevent conflicting table names.");
  571. }
  572. else
  573. {
  574. throw new Exception("Error: found iTop tables into the database '".$oConfig->GetDBName()."'. Please, try selecting another database instance or specify a prefix to prevent conflicting table names.");
  575. }
  576. }
  577. }
  578. else
  579. {
  580. if (MetaModel::DBExists(/* bMustBeComplete */ false))
  581. {
  582. MetaModel::DBCreate();
  583. SetupWebPage::log_ok("Database structure successfully created.");
  584. }
  585. else
  586. {
  587. if (strlen($oConfig->GetDBSubname()) > 0)
  588. {
  589. throw new Exception("Error: No previous instance of iTop found into the database '".$oConfig->GetDBName()."' (prefix: '".$oConfig->GetDBSubname()."'). Please, try selecting another database instance.");
  590. }
  591. else
  592. {
  593. throw new Exception("Error: No previous instance of iTop found into the database '".$oConfig->GetDBName()."'. Please, try selecting another database instance.");
  594. }
  595. }
  596. }
  597. return true;
  598. }
  599. function RecordInstallation(Config $oConfig, $aSelectedModules)
  600. {
  601. // Record main installation
  602. $oInstallRec = new ModuleInstallation();
  603. $oInstallRec->Set('name', 'itop');
  604. $oInstallRec->Set('version', ITOP_VERSION.'.'.ITOP_REVISION);
  605. $oInstallRec->Set('comment', "Done by the setup program\nBuilt on ".ITOP_BUILD_DATE);
  606. $oInstallRec->Set('parent_id', 0); // root module
  607. $iMainItopRecord = $oInstallRec->DBInsertNoReload();
  608. // Record installed modules
  609. //
  610. $aAvailableModules = AnalyzeInstallation($oConfig);
  611. foreach($aSelectedModules as $sModuleId)
  612. {
  613. $aModuleData = $aAvailableModules[$sModuleId];
  614. $sName = $sModuleId;
  615. $sVersion = $aModuleData['version_code'];
  616. $aComments = array();
  617. $aComments[] = 'Done by the setup program';
  618. if ($aModuleData['mandatory'])
  619. {
  620. $aComments[] = 'Mandatory';
  621. }
  622. else
  623. {
  624. $aComments[] = 'Optional';
  625. }
  626. if ($aModuleData['visible'])
  627. {
  628. $aComments[] = 'Visible (during the setup)';
  629. }
  630. else
  631. {
  632. $aComments[] = 'Hidden (selected automatically)';
  633. }
  634. foreach ($aModuleData['dependencies'] as $sDependOn)
  635. {
  636. $aComments[] = "Depends on module: $sDependOn";
  637. }
  638. $sComment = implode("\n", $aComments);
  639. $oInstallRec = new ModuleInstallation();
  640. $oInstallRec->Set('name', $sName);
  641. $oInstallRec->Set('version', $sVersion);
  642. $oInstallRec->Set('comment', $sComment);
  643. $oInstallRec->Set('parent_id', $iMainItopRecord);
  644. $oInstallRec->DBInsertNoReload();
  645. }
  646. // Database is created, installation has been tracked into it
  647. return true;
  648. }
  649. function ListModuleFiles($sRelDir)
  650. {
  651. $sDirectory = APPROOT.'/'.$sRelDir;
  652. //echo "<p>$sDirectory</p>\n";
  653. if ($hDir = opendir($sDirectory))
  654. {
  655. // This is the correct way to loop over the directory. (according to the documentation)
  656. while (($sFile = readdir($hDir)) !== false)
  657. {
  658. $aMatches = array();
  659. if (is_dir($sDirectory.'/'.$sFile))
  660. {
  661. if (($sFile != '.') && ($sFile != '..') && ($sFile != '.svn'))
  662. {
  663. ListModuleFiles($sRelDir.'/'.$sFile);
  664. }
  665. }
  666. else if (preg_match('/^module\.(.*).php$/i', $sFile, $aMatches))
  667. {
  668. SetupWebPage::SetModulePath($sRelDir);
  669. try
  670. {
  671. //echo "<p>Loading: $sDirectory/$sFile...</p>\n";
  672. require_once($sDirectory.'/'.$sFile);
  673. //echo "<p>Done.</p>\n";
  674. }
  675. catch(Exception $e)
  676. {
  677. // Continue...
  678. }
  679. }
  680. }
  681. closedir($hDir);
  682. }
  683. else
  684. {
  685. throw new Exception("Data directory (".$sDirectory.") not found or not readable.");
  686. }
  687. }
  688. ?>