cmd.php 6.3 KB

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