cmd.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentInstall Trident Install
  7. * @{
  8. *
  9. /**
  10. * Install command line interface
  11. */
  12. class BxDolInstallCmd
  13. {
  14. protected $_sHeaderPath;
  15. protected $_aSiteConfig;
  16. protected $_isQuiet = false;
  17. protected $_aAdditionalModules = array();
  18. protected $_aReturnCodes = array(
  19. 'success' => array ('code' => 0, 'msg' => 'Success.'),
  20. 'already installed' => array ('code' => 0, 'msg' => 'Script is already installed. Can\'t perform install.'),
  21. 'requirements failed' => array ('code' => 2, 'msg' => 'Requirements aren\'t met.'),
  22. 'permissions failed' => array ('code' => 3, 'msg' => 'Folders and/or files permissions aren\'t correct.'),
  23. 'create config failed' => array ('code' => 4, 'msg' => 'Form data was not submitted.'),
  24. 'module failed' => array ('code' => 5, 'msg' => 'Additional module install failed. '),
  25. );
  26. public function __construct()
  27. {
  28. $aPathInfo = pathinfo(__FILE__);
  29. $this->_aSiteConfig = array (
  30. 'server_http_host' => 'localhost',
  31. 'server_php_self' => '/install/index.php',
  32. 'server_doc_root' => str_replace('/install', '/', $aPathInfo['dirname']),
  33. // form data below
  34. 'site_config' => true,
  35. 'db_name' => 'test',
  36. 'db_user' => 'root',
  37. 'db_password' => 'root',
  38. 'site_title' => 'UNA Test',
  39. 'site_email' => 'no-reply@example.com',
  40. 'admin_email' => 'admin@example.com',
  41. 'admin_username' => 'admin',
  42. 'admin_password' => 'unauna',
  43. 'language' => 'en',
  44. );
  45. $this->_sHeaderPath = $this->_aSiteConfig['server_doc_root'] . 'inc/header.inc.php';
  46. }
  47. public function main()
  48. {
  49. // set neccessary options
  50. $a = getopt('hqm:', $this->getOptions());
  51. if (isset($a['h']))
  52. $this->finish($this->_aReturnCodes['success']['code'], $this->getHelp());
  53. if (isset($a['q']))
  54. $this->_isQuiet = true;
  55. if (isset($a['m']))
  56. $this->_aAdditionalModules = explode(',', $a['m']);
  57. $this->_aSiteConfig = array_merge($this->_aSiteConfig, $a);
  58. // initialize environment
  59. $this->init();
  60. // peform install
  61. $this->checkRequirements();
  62. $this->checkPermissions();
  63. $this->createSiteConfig();
  64. $this->finish($this->_aReturnCodes['success']['code'], $this->_aReturnCodes['success']['msg']);
  65. }
  66. protected function getOptions()
  67. {
  68. $a = array ();
  69. foreach ($this->_aSiteConfig as $sKey => $sValue)
  70. if ('site_config' != $sKey)
  71. $a[] = "$sKey::";
  72. return $a;
  73. }
  74. protected function getHelp()
  75. {
  76. $s = "Usage: php cmd.php [options]\n";
  77. $s .= str_pad("\t -h", 35) . "Print this help\n";
  78. $s .= str_pad("\t -q", 35) . "Quiet\n";
  79. $s .= str_pad("\t -m <module1,module2,...,moduleN>", 35) . "Install additional modules, by module name (ex:bx_notes)\n";
  80. foreach ($this->_aSiteConfig as $sKey => $sVal)
  81. if ('site_config' != $sKey)
  82. $s .= str_pad("\t --{$sKey}=<value>", 35) . "Default value: {$sVal}\n";
  83. $s .= "\n";
  84. $s .= "Return codes:\n";
  85. foreach ($this->_aReturnCodes as $r)
  86. $s .= str_pad("\t {$r['code']}", 5) . "{$r['msg']}\n";
  87. return $s;
  88. }
  89. protected function finish($iCode, $sMsg)
  90. {
  91. if (!$this->_isQuiet)
  92. fwrite($iCode ? STDERR : STDOUT, $sMsg . "\n");
  93. exit($iCode);
  94. }
  95. protected function init()
  96. {
  97. // skip this test if script is already installed
  98. if (file_exists($this->_sHeaderPath))
  99. $this->finish($this->_aReturnCodes['already installed']['code'], $this->_aReturnCodes['already installed']['msg']);
  100. // include necessary files to perform install
  101. $_REQUEST['action'] = 'empty';
  102. $aPathInfo = pathinfo(__FILE__);
  103. require_once($aPathInfo['dirname'] . '/index.php');
  104. }
  105. public function checkRequirements()
  106. {
  107. $oAudit = new BxDolStudioToolsAudit();
  108. $aErrors = $oAudit->checkRequirements(BX_DOL_AUDIT_FAIL);
  109. if (!empty($aErrors))
  110. $this->finish($this->_aReturnCodes['requirements failed']['code'], $this->_aReturnCodes['requirements failed']['msg']);
  111. }
  112. public function checkPermissions()
  113. {
  114. $oAdmTools = new BxDolStudioTools();
  115. $bPermissionsOk = $oAdmTools->checkPermissions(false, false);
  116. if (!$bPermissionsOk)
  117. $this->finish($this->_aReturnCodes['permissions failed']['code'], $this->_aReturnCodes['permissions failed']['msg']);
  118. }
  119. public function createSiteConfig()
  120. {
  121. $oSiteConfig = new BxDolInstallSiteConfig($this->_aSiteConfig['server_http_host'], $this->_aSiteConfig['server_php_self'], $this->_aSiteConfig['server_doc_root'], false);
  122. $sFormData = $this->_aSiteConfig;
  123. unset($sFormData['server_http_host']);
  124. unset($sFormData['server_php_self']);
  125. unset($sFormData['server_doc_root']);
  126. $sErrorMessage = '';
  127. $mixedResult = $oSiteConfig->getFormHtml(array_merge($oSiteConfig->getAutoValues(), $sFormData), false, $sErrorMessage);
  128. if (true !== $mixedResult)
  129. $this->finish($this->_aReturnCodes['create config failed']['code'], $sErrorMessage ? $sErrorMessage : $this->_aReturnCodes['create config failed']['msg']);
  130. // install custom additional modules
  131. if (!empty($this->_aAdditionalModules)) {
  132. foreach ($this->_aAdditionalModules as $sModuleName) {
  133. $sErrorMessage = $oSiteConfig->processModuleByName($sModuleName);
  134. if ($sErrorMessage)
  135. $this->finish($this->_aReturnCodes['module failed']['code'], $this->_aReturnCodes['module failed']['msg'] . strip_tags($sErrorMessage));
  136. }
  137. }
  138. }
  139. }
  140. $o = new BxDolInstallCmd();
  141. $o->main();
  142. /** @} */