cmd.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 $_aReturnCodes = array(
  18. 'success' => array ('code' => 0, 'msg' => 'Success.'),
  19. 'already installed' => array ('code' => 0, 'msg' => 'Script is already installed. Can\'t perform install.'),
  20. 'requirements failed' => array ('code' => 2, 'msg' => 'Requirements aren\'t met.'),
  21. 'permissions failed' => array ('code' => 3, 'msg' => 'Folders and/or files permissions aren\'t correct.'),
  22. 'create config failed' => array ('code' => 4, 'msg' => 'Form data was not submitted.'),
  23. );
  24. public function __construct()
  25. {
  26. $aPathInfo = pathinfo(__FILE__);
  27. $this->_aSiteConfig = array (
  28. 'server_http_host' => 'localhost',
  29. 'server_php_self' => '/install/index.php',
  30. 'server_doc_root' => str_replace('/install', '/', $aPathInfo['dirname']),
  31. // form data below
  32. 'site_config' => true,
  33. 'db_name' => 'test',
  34. 'db_user' => 'root',
  35. 'db_password' => 'root',
  36. 'site_title' => 'Dolphin Test',
  37. 'site_desc' => 'Dolphin Test',
  38. 'site_email' => 'no-reply@example.com',
  39. 'admin_email' => 'admin@example.com',
  40. 'admin_username' => 'admin',
  41. 'admin_password' => 'dolphin',
  42. 'language' => 'en',
  43. );
  44. $this->_sHeaderPath = $this->_aSiteConfig['server_doc_root'] . 'inc/header.inc.php';
  45. }
  46. public function main()
  47. {
  48. // set neccessary options
  49. $a = getopt('hq', $this->getOptions());
  50. if (isset($a['h']))
  51. $this->finish($this->_aReturnCodes['success']['code'], $this->getHelp());
  52. if (isset($a['q']))
  53. $this->_isQuiet = true;
  54. $this->_aSiteConfig = array_merge($this->_aSiteConfig, $a);
  55. // initialize environment
  56. $this->init();
  57. // peform install
  58. $this->checkRequirements();
  59. $this->checkPermissions();
  60. $this->createSiteConfig();
  61. }
  62. protected function getOptions()
  63. {
  64. $a = array ();
  65. foreach ($this->_aSiteConfig as $sKey => $sValue)
  66. if ('site_config' != $sKey)
  67. $a[] = "$sKey::";
  68. return $a;
  69. }
  70. protected function getHelp()
  71. {
  72. $s = '';
  73. $s .= "Usage: php cmd.php [options]\n";
  74. $s .= str_pad("\t -h", 35) . "Print this help\n";
  75. $s .= str_pad("\t -q", 35) . "Quiet\n";
  76. foreach ($this->_aSiteConfig as $sKey => $sVal)
  77. if ('site_config' != $sKey)
  78. $s .= str_pad("\t --{$sKey}=<value>", 35) . "Default value: {$sVal}\n";
  79. return $s;
  80. }
  81. protected function finish($iCode, $sMsg)
  82. {
  83. if (!$this->_isQuiet)
  84. fwrite($iCode ? STDERR : STDOUT, $sMsg . "\n");
  85. exit($iCode);
  86. }
  87. protected function init()
  88. {
  89. // skip this test if script is already installed
  90. if (file_exists($this->_sHeaderPath))
  91. $this->finish($this->_aReturnCodes['already installed']['code'], $this->_aReturnCodes['already installed']['msg']);
  92. // include necessary files to perform install
  93. $_REQUEST['action'] = 'empty';
  94. $aPathInfo = pathinfo(__FILE__);
  95. require_once($aPathInfo['dirname'] . '/index.php');
  96. }
  97. public function checkRequirements()
  98. {
  99. $oAudit = new BxDolStudioToolsAudit();
  100. $aErrors = $oAudit->checkRequirements(BX_DOL_AUDIT_FAIL);
  101. if (!empty($aErrors))
  102. $this->finish($this->_aReturnCodes['requirements failed']['code'], $this->_aReturnCodes['requirements failed']['msg']);
  103. }
  104. public function checkPermissions()
  105. {
  106. $oAdmTools = new BxDolStudioTools();
  107. $bPermissionsOk = $oAdmTools->checkPermissions(false, false);
  108. if (!$bPermissionsOk)
  109. $this->finish($this->_aReturnCodes['permissions failed']['code'], $this->_aReturnCodes['permissions failed']['msg']);
  110. }
  111. public function createSiteConfig()
  112. {
  113. $oSiteConfig = new BxDolInstallSiteConfig($this->_aSiteConfig['server_http_host'], $this->_aSiteConfig['server_php_self'], $this->_aSiteConfig['server_doc_root'], false);
  114. $sFormData = $this->_aSiteConfig;
  115. unset($sFormData['server_http_host']);
  116. unset($sFormData['server_php_self']);
  117. unset($sFormData['server_doc_root']);
  118. $sErrorMessage = '';
  119. $mixedResult = $oSiteConfig->getFormHtml(array_merge($oSiteConfig->getAutoValues(), $sFormData), false, $sErrorMessage);
  120. if (true !== $mixedResult)
  121. $this->finish($this->_aReturnCodes['create config failed']['code'], $sErrorMessage ? $sErrorMessage : $this->_aReturnCodes['create config failed']['msg']);
  122. }
  123. }
  124. $o = new BxDolInstallCmd();
  125. $o->main();
  126. /** @} */