cmd.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaInstall UNA 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_host' => 'localhost',
  36. 'db_port' => '',
  37. 'db_sock' => '',
  38. 'db_name' => 'test',
  39. 'db_user' => 'root',
  40. 'db_password' => 'root',
  41. 'site_title' => 'UNA Test',
  42. 'site_email' => 'no-reply@example.com',
  43. 'admin_email' => 'admin@example.com',
  44. 'admin_username' => 'admin',
  45. 'admin_password' => 'unauna',
  46. 'language' => 'en',
  47. 'oauth_key' => '',
  48. 'oauth_secret' => '',
  49. );
  50. $this->_sHeaderPath = $this->_aSiteConfig['server_doc_root'] . 'inc/header.inc.php';
  51. }
  52. public function main()
  53. {
  54. // set neccessary options
  55. $a = getopt('hqm:', $this->getOptions());
  56. if (isset($a['h']))
  57. $this->finish($this->_aReturnCodes['success']['code'], $this->getHelp());
  58. if (isset($a['q']))
  59. $this->_isQuiet = true;
  60. if (isset($a['m']))
  61. $this->_aAdditionalModules = explode(',', $a['m']);
  62. $this->_aSiteConfig = array_merge($this->_aSiteConfig, $a);
  63. // initialize environment
  64. $this->init();
  65. // peform install
  66. $this->checkRequirements();
  67. $this->checkPermissions();
  68. $this->createSiteConfig();
  69. $this->hashSystemFiles();
  70. $this->finish($this->_aReturnCodes['success']['code'], $this->_aReturnCodes['success']['msg']);
  71. }
  72. protected function getOptions()
  73. {
  74. $a = array ();
  75. foreach ($this->_aSiteConfig as $sKey => $sValue)
  76. if ('site_config' != $sKey)
  77. $a[] = "$sKey::";
  78. return $a;
  79. }
  80. protected function getHelp()
  81. {
  82. $s = "Usage: php cmd.php [options]\n";
  83. $s .= str_pad("\t -h", 35) . "Print this help\n";
  84. $s .= str_pad("\t -q", 35) . "Quiet\n";
  85. $s .= str_pad("\t -m <module1,module2,...,moduleN>", 35) . "Install additional modules, by module name (ex:bx_notes)\n";
  86. foreach ($this->_aSiteConfig as $sKey => $sVal)
  87. if ('site_config' != $sKey)
  88. $s .= str_pad("\t --{$sKey}=<value>", 35) . "Default value: {$sVal}\n";
  89. $s .= "\n";
  90. $s .= "Return codes:\n";
  91. foreach ($this->_aReturnCodes as $r)
  92. $s .= str_pad("\t {$r['code']}", 5) . "{$r['msg']}\n";
  93. return $s;
  94. }
  95. protected function finish($iCode, $sMsg)
  96. {
  97. if (!$this->_isQuiet)
  98. fwrite($iCode ? STDERR : STDOUT, $sMsg . "\n");
  99. exit($iCode);
  100. }
  101. protected function init()
  102. {
  103. // skip this test if script is already installed
  104. if (file_exists($this->_sHeaderPath))
  105. $this->finish($this->_aReturnCodes['already installed']['code'], $this->_aReturnCodes['already installed']['msg']);
  106. // include necessary files to perform install
  107. $_REQUEST['action'] = 'empty';
  108. $aPathInfo = pathinfo(__FILE__);
  109. require_once($aPathInfo['dirname'] . '/index.php');
  110. }
  111. public function checkRequirements()
  112. {
  113. $oAudit = new BxDolStudioToolsAudit();
  114. $aErrors = $oAudit->checkRequirements(BX_DOL_AUDIT_FAIL);
  115. if (!empty($aErrors))
  116. $this->finish($this->_aReturnCodes['requirements failed']['code'], $this->_aReturnCodes['requirements failed']['msg']);
  117. }
  118. public function checkPermissions()
  119. {
  120. $oAdmTools = new BxDolStudioTools();
  121. $bPermissionsOk = $oAdmTools->checkPermissions(false, false);
  122. if (!$bPermissionsOk)
  123. $this->finish($this->_aReturnCodes['permissions failed']['code'], $this->_aReturnCodes['permissions failed']['msg']);
  124. }
  125. public function createSiteConfig()
  126. {
  127. $oSiteConfig = new BxDolInstallSiteConfig($this->_aSiteConfig['server_http_host'], $this->_aSiteConfig['server_php_self'], $this->_aSiteConfig['server_doc_root'], false);
  128. $sFormData = $this->_aSiteConfig;
  129. unset($sFormData['server_http_host']);
  130. unset($sFormData['server_php_self']);
  131. unset($sFormData['server_doc_root']);
  132. $sErrorMessage = '';
  133. $mixedResult = $oSiteConfig->getFormHtml(array_merge($oSiteConfig->getAutoValues(), $sFormData), false, $sErrorMessage);
  134. if (true !== $mixedResult)
  135. $this->finish($this->_aReturnCodes['create config failed']['code'], $sErrorMessage ? $sErrorMessage : $this->_aReturnCodes['create config failed']['msg']);
  136. // install custom additional modules
  137. if (!empty($this->_aAdditionalModules)) {
  138. foreach ($this->_aAdditionalModules as $sModuleName) {
  139. $sErrorMessage = $oSiteConfig->processModuleByName($sModuleName);
  140. if ($sErrorMessage)
  141. $this->finish($this->_aReturnCodes['module failed']['code'], $this->_aReturnCodes['module failed']['msg'] . strip_tags($sErrorMessage));
  142. }
  143. }
  144. }
  145. public function hashSystemFiles()
  146. {
  147. require_once(BX_INSTALL_PATH_HEADER);
  148. $oHasher = bx_instance('BxDolInstallerHasher');
  149. $oHasher->hashSystemFiles();
  150. }
  151. }
  152. $o = new BxDolInstallCmd();
  153. $o->main();
  154. /** @} */