cmd.php 6.0 KB

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