A Qcodo based CMS/ecommerce framework
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.

171 lines
8.0 KiB

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