/install-dev/controllers/http/database.php

https://gitlab.com/jslee1/PrestaShop · PHP · 175 lines · 109 code · 17 blank · 49 comment · 8 complexity · 2f61c9ad9df4713b0c446fe40b6aeea7 MD5 · raw file

  1. <?php
  2. /**
  3. * 2007-2015 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. /**
  27. * Step 3 : configure database
  28. */
  29. class InstallControllerHttpDatabase extends InstallControllerHttp
  30. {
  31. /**
  32. * @var InstallModelDatabase
  33. */
  34. public $model_database;
  35. public function init()
  36. {
  37. require_once _PS_INSTALL_MODELS_PATH_.'database.php';
  38. $this->model_database = new InstallModelDatabase();
  39. }
  40. /**
  41. * @see InstallAbstractModel::processNextStep()
  42. */
  43. public function processNextStep()
  44. {
  45. // Save database config
  46. $this->session->database_server = trim(Tools::getValue('dbServer'));
  47. $this->session->database_name = trim(Tools::getValue('dbName'));
  48. $this->session->database_login = trim(Tools::getValue('dbLogin'));
  49. $this->session->database_password = trim(Tools::getValue('dbPassword'));
  50. $this->session->database_prefix = trim(Tools::getValue('db_prefix'));
  51. $this->session->database_clear = Tools::getValue('database_clear');
  52. $this->session->rewrite_engine = Tools::getValue('rewrite_engine');
  53. }
  54. /**
  55. * Database configuration must be valid to validate this step
  56. *
  57. * @see InstallAbstractModel::validate()
  58. */
  59. public function validate()
  60. {
  61. $this->errors = $this->model_database->testDatabaseSettings(
  62. $this->session->database_server,
  63. $this->session->database_name,
  64. $this->session->database_login,
  65. $this->session->database_password,
  66. $this->session->database_prefix,
  67. // We do not want to validate table prefix if we are already in install process
  68. ($this->session->step == 'process') ? true : $this->session->database_clear
  69. );
  70. if (count($this->errors)) {
  71. return false;
  72. }
  73. if (!isset($this->session->database_engine)) {
  74. $this->session->database_engine = $this->model_database->getBestEngine($this->session->database_server, $this->session->database_name, $this->session->database_login, $this->session->database_password);
  75. }
  76. return true;
  77. }
  78. public function process()
  79. {
  80. if (Tools::getValue('checkDb')) {
  81. $this->processCheckDb();
  82. } elseif (Tools::getValue('createDb')) {
  83. $this->processCreateDb();
  84. }
  85. }
  86. /**
  87. * Check if a connection to database is possible with these data
  88. */
  89. public function processCheckDb()
  90. {
  91. $server = Tools::getValue('dbServer');
  92. $database = Tools::getValue('dbName');
  93. $login = Tools::getValue('dbLogin');
  94. $password = Tools::getValue('dbPassword');
  95. $prefix = Tools::getValue('db_prefix');
  96. $clear = Tools::getValue('clear');
  97. $errors = $this->model_database->testDatabaseSettings($server, $database, $login, $password, $prefix, $clear);
  98. $this->ajaxJsonAnswer(
  99. (count($errors)) ? false : true,
  100. (count($errors)) ? implode('<br />', $errors) : $this->l('Database is connected')
  101. );
  102. }
  103. /**
  104. * Attempt to create the database
  105. */
  106. public function processCreateDb()
  107. {
  108. $server = Tools::getValue('dbServer');
  109. $database = Tools::getValue('dbName');
  110. $login = Tools::getValue('dbLogin');
  111. $password = Tools::getValue('dbPassword');
  112. $success = $this->model_database->createDatabase($server, $database, $login, $password);
  113. $this->ajaxJsonAnswer(
  114. $success,
  115. $success ? $this->l('Database is created') : $this->l('Cannot create the database automatically')
  116. );
  117. }
  118. /**
  119. * @see InstallAbstractModel::display()
  120. */
  121. public function display()
  122. {
  123. if (!$this->session->database_server) {
  124. if (file_exists(_PS_ROOT_DIR_.'/config/settings.inc.php')) {
  125. @include_once _PS_ROOT_DIR_.'/config/settings.inc.php';
  126. $this->database_server = _DB_SERVER_;
  127. $this->database_name = _DB_NAME_;
  128. $this->database_login = _DB_USER_;
  129. $this->database_password = _DB_PASSWD_;
  130. $this->database_engine = _MYSQL_ENGINE_;
  131. $this->database_prefix = _DB_PREFIX_;
  132. } else {
  133. $this->database_server = 'localhost';
  134. $this->database_name = 'prestashop';
  135. $this->database_login = 'root';
  136. $this->database_password = '';
  137. $this->database_engine = 'InnoDB';
  138. $this->database_prefix = 'ps_';
  139. }
  140. $this->database_clear = true;
  141. $this->use_smtp = false;
  142. $this->smtp_encryption = 'off';
  143. $this->smtp_port = 25;
  144. } else {
  145. $this->database_server = $this->session->database_server;
  146. $this->database_name = $this->session->database_name;
  147. $this->database_login = $this->session->database_login;
  148. $this->database_password = $this->session->database_password;
  149. $this->database_engine = $this->session->database_engine;
  150. $this->database_prefix = $this->session->database_prefix;
  151. $this->database_clear = $this->session->database_clear;
  152. $this->use_smtp = $this->session->use_smtp;
  153. $this->smtp_encryption = $this->session->smtp_encryption;
  154. $this->smtp_port = $this->session->smtp_port;
  155. }
  156. $this->displayTemplate('database');
  157. }
  158. }