A QCodo powered CMS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
6.6 KiB

  1. <?php
  2. /**
  3. * This is the installation system for Quinta CMS.
  4. */
  5. // Include to load Quinta and Qcodo configuration ..
  6. require('../includes/quinta/Quinta.class.php');
  7. class InstallationForm extends QForm {
  8. protected $strQCodoConfigFile;
  9. protected $strQuintaConfigFile;
  10. protected $txtDatabaseAdapter;
  11. protected $txtDatabaseServer;
  12. protected $txtDatabaseName;
  13. protected $txtDatabaseUser;
  14. protected $txtDatabasePassword;
  15. protected $txtDatabaseAdminUser;
  16. protected $txtDatabaseAdminPassword;
  17. protected $chkCreateDatabase;
  18. protected $chkInstallExampleData;
  19. protected $blnFinished = false;
  20. protected $lblMessage;
  21. protected $btnSaveConfig;
  22. protected $strErrors;
  23. protected function Form_Create() {
  24. $this->strQCodoConfigFile = __QCODO_ROOT__ . '/includes/configuration.inc.php';
  25. $this->strQuintaConfigFile = __QUINTA_CORE__ . '/quinta_config.php';
  26. $this->txtDatabaseAdapter = new QTextBox($this);
  27. $this->txtDatabaseAdapter->Text = 'MySqli5';
  28. $this->txtDatabaseAdapter->Name = Quinta::Translate('Database Adapter (Required)') . ':';
  29. $this->txtDatabaseServer = new QTextBox($this);
  30. $this->txtDatabaseServer->Text = 'localhost';
  31. $this->txtDatabaseServer->Name = Quinta::Translate('Database Server (Required)') . ':';
  32. $this->txtDatabaseName = new QTextBox($this);
  33. $this->txtDatabaseName->Text = 'quintacmstest';
  34. $this->txtDatabaseName->Name = Quinta::Translate('Database (Required)') . ':';
  35. $this->txtDatabaseUser = new QTextBox($this);
  36. $this->txtDatabaseUser->Text = 'quintadbutest';
  37. $this->txtDatabaseUser->Name = Quinta::Translate('Database User Name (Required)') . ':';
  38. $this->txtDatabasePassword = new QTextBox($this);
  39. $this->txtDatabasePassword->Text = 'quintadbptest';
  40. $this->txtDatabasePassword->Name = Quinta::Translate('Database Password (Required)') . ':';
  41. $this->txtDatabaseAdminPassword = new QTextBox($this);
  42. $this->txtDatabaseAdminPassword->Text = '';
  43. $this->txtDatabaseAdminPassword->Name = Quinta::Translate('Database Admin Password') . ':';
  44. $this->txtDatabaseAdminUser = new QTextBox($this);
  45. $this->txtDatabaseAdminUser->Text = 'root';
  46. $this->txtDatabaseAdminUser->Name = Quinta::Translate('Database Admin Username') . ':';
  47. $this->lblMessage = new QLabel($this);
  48. $this->lblMessage->Text = '';
  49. $this->lblMessage->HtmlEntities = false;
  50. $this->chkInstallExampleData = new QCheckBox($this);
  51. $this->chkInstallExampleData->Name = Quinta::Translate('Install Example Data? (Recommended for new users.) ') . ':';
  52. $this->chkInstallExampleData->Checked = true;
  53. $this->chkInstallExampleData->Width = '1em';
  54. $this->chkCreateDatabase = new QCheckBox($this);
  55. $this->chkCreateDatabase->Name = Quinta::Translate('Create Database ? (Requires Admin username and password) ') . ':';
  56. $this->chkCreateDatabase->Width = '1em';
  57. $this->btnSaveConfig = new QButton($this);
  58. $this->btnSaveConfig->Text = 'Continue';
  59. $this->btnSaveConfig->AddAction(new QClickEvent(), new QServerAction('btnSaveConfig_Click'));
  60. }
  61. protected function btnSaveConfig_Click($strFormId, $strControlId, $strParameter) {
  62. if ($this->blnFinished)
  63. Quinta::Redirect(__QUINTA_SUBDIRECTORY__ . '/index.php/Home');
  64. $strOutFile = $this->strQCodoConfigFile;
  65. if ($this->chkCreateDatabase->Checked)
  66. if (!$this->createDatabase())
  67. die($this->strErrors);
  68. if (!$this->installDatabase())
  69. die($this->strErrors);
  70. $this->writeConfigFile($strOutFile);
  71. $this->lblMessage->Text =
  72. '<h3>Configuration saved!</h3> <p style=" color:red; font-weight:bold;">'
  73. . '<strong>Security Warning:</strong> You must change the permissions on '
  74. . $strOutFile . ' <i>Immediately!!</i></p>'
  75. . '<p>On Linux/Unix use this command: <br /><pre><code> chmod go-w '
  76. . $strOutFile . '</code></pre></p><p style=" color:red; font-weight:bold;">'
  77. . '<strong>Security Warning:</strong> You must also remove the install.php links '
  78. . ' <i>Immediately!!</i></p>'
  79. . '<p>On Linux/Unix use this command: <br /><pre><code> rm '
  80. . __QUINTA_WWWROOT__ . '/install* </code></pre></p>';
  81. $this->blnFinished = true;
  82. }
  83. protected function installDatabase() {
  84. if ($this->chkInstallExampleData->Checked)
  85. $strSqlFile = __QUINTA_DBDIR__ . '/quinta-with-data.sql';
  86. else
  87. $strSqlFile = __QUINTA_DBDIR__ . '/quinta.sql';
  88. $strCommand = 'mysql -u ' . $this->txtDatabaseUser->Text . ' -h ' . $this->txtDatabaseServer->Text;
  89. if($this->txtDatabasePassword->Text )
  90. $strCommand .= ' -p' . $this->txtDatabasePassword->Text;
  91. $strCommand .= ' ' . $this->txtDatabaseName->Text . ' < ' . $strSqlFile;
  92. $aryErrors = array();
  93. $intRetval = 0;
  94. $blnFork = exec($strCommand, $aryErrors, $intRetval);
  95. //! TODO: catch errors - even if mysql fails exec is not catching the errors .. dunno why.
  96. if (false === $blnFork || 0 != $intRetval) {
  97. $this->strErrors .= 'Failed to install data - command: ' . $strCommand;
  98. foreach ($aryErrors as $strError)
  99. $this->strErrors .= $strError;
  100. return false;
  101. }
  102. return true;
  103. }
  104. protected function createDatabase() {
  105. $strCommand = 'mysql -u ' . $this->txtDatabaseAdminUser->Text . ' -h ' . $this->txtDatabaseServer->Text;
  106. if($this->txtDatabaseAdminPassword->Text )
  107. $strCommand .= ' -p' . $this->txtDatabaseAdminPassword->Text;
  108. $strCommand .= " -e ' CREATE DATABASE " . $this->txtDatabaseName->Text . ';'
  109. . ' GRANT ALL PRIVILEGES ON ' . $this->txtDatabaseName->Text . '.* TO '
  110. //!TODO - make smarter, this will work only on a local database ..
  111. . $this->txtDatabaseUser->Text . '@localhost IDENTIFIED BY "'
  112. . $this->txtDatabasePassword->Text . '" ;\' ';
  113. $aryErrors = array();
  114. $intRetval = 0;
  115. $blnFork = exec($strCommand, $aryErrors, $intRetval);
  116. if (false === $blnFork || 0 != $intRetval) {
  117. $this->strErrors .= 'Failed to create database - command: ' . $strCommand;
  118. foreach ($aryErrors as $strError)
  119. $this->strErrors .= $strError;
  120. return false;
  121. }
  122. return true;
  123. }
  124. protected function writeConfigFile($strOutFile) {
  125. $strInFile = $this->strQCodoConfigFile . '-quinta-dist';
  126. $aryValues = array(
  127. "/'adapter' => '.*'/" => "'adapter' => '" . $this->txtDatabaseAdapter->Text . "'",
  128. "/'server' => '.*'/" => "'server' => '" . $this->txtDatabaseServer->Text . "'",
  129. "/'database' => '.*'/" => "'database' => '" . $this->txtDatabaseName->Text . "'",
  130. "/'username' => '.*'/" => "'username' => '" . $this->txtDatabaseUser->Text . "'",
  131. "/'password' => '.*'/" => "'password' => '" . $this->txtDatabasePassword->Text . "'",
  132. );
  133. $aryFile = file($strInFile);
  134. foreach ($aryValues as $strName => $strValue)
  135. $aryFile = preg_replace($strName, $strValue, $aryFile);
  136. $fp = fopen($strOutFile, 'w');
  137. foreach ($aryFile as $strLine)
  138. fwrite($fp, $strLine);
  139. fclose($fp);
  140. }
  141. }
  142. InstallationForm::Run('InstallationForm');
  143. ?>