/install/controller/install/step_3.php

https://gitlab.com/shapcy/opencart · PHP · 303 lines · 243 code · 60 blank · 0 comment · 58 complexity · e21c3886fb53c0a04e2d2aca1943df26 MD5 · raw file

  1. <?php
  2. class ControllerInstallStep3 extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->language->load('install/step_3');
  6. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
  7. $this->load->model('install/install');
  8. $this->model_install_install->database($this->request->post);
  9. $output = '<?php' . "\n";
  10. $output .= '// HTTP' . "\n";
  11. $output .= 'define(\'HTTP_SERVER\', \'' . HTTP_OPENCART . '\');' . "\n\n";
  12. $output .= '// HTTPS' . "\n";
  13. $output .= 'define(\'HTTPS_SERVER\', \'' . HTTP_OPENCART . '\');' . "\n\n";
  14. $output .= '// DIR' . "\n";
  15. $output .= 'define(\'DIR_APPLICATION\', \'' . DIR_OPENCART . 'catalog/\');' . "\n";
  16. $output .= 'define(\'DIR_SYSTEM\', \'' . DIR_OPENCART . 'system/\');' . "\n";
  17. $output .= 'define(\'DIR_IMAGE\', \'' . DIR_OPENCART . 'image/\');' . "\n";
  18. $output .= 'define(\'DIR_LANGUAGE\', \'' . DIR_OPENCART . 'catalog/language/\');' . "\n";
  19. $output .= 'define(\'DIR_TEMPLATE\', \'' . DIR_OPENCART . 'catalog/view/theme/\');' . "\n";
  20. $output .= 'define(\'DIR_CONFIG\', \'' . DIR_OPENCART . 'system/config/\');' . "\n";
  21. $output .= 'define(\'DIR_CACHE\', \'' . DIR_OPENCART . 'system/storage/cache/\');' . "\n";
  22. $output .= 'define(\'DIR_DOWNLOAD\', \'' . DIR_OPENCART . 'system/storage/download/\');' . "\n";
  23. $output .= 'define(\'DIR_LOGS\', \'' . DIR_OPENCART . 'system/storage/logs/\');' . "\n";
  24. $output .= 'define(\'DIR_MODIFICATION\', \'' . DIR_OPENCART . 'system/storage/modification/\');' . "\n";
  25. $output .= 'define(\'DIR_UPLOAD\', \'' . DIR_OPENCART . 'system/storage/upload/\');' . "\n\n";
  26. $output .= '// DB' . "\n";
  27. $output .= 'define(\'DB_DRIVER\', \'' . addslashes($this->request->post['db_driver']) . '\');' . "\n";
  28. $output .= 'define(\'DB_HOSTNAME\', \'' . addslashes($this->request->post['db_hostname']) . '\');' . "\n";
  29. $output .= 'define(\'DB_USERNAME\', \'' . addslashes($this->request->post['db_username']) . '\');' . "\n";
  30. $output .= 'define(\'DB_PASSWORD\', \'' . addslashes(html_entity_decode($this->request->post['db_password'], ENT_QUOTES, 'UTF-8')) . '\');' . "\n";
  31. $output .= 'define(\'DB_DATABASE\', \'' . addslashes($this->request->post['db_database']) . '\');' . "\n";
  32. $output .= 'define(\'DB_PORT\', \'' . addslashes($this->request->post['db_port']) . '\');' . "\n";
  33. $output .= 'define(\'DB_PREFIX\', \'' . addslashes($this->request->post['db_prefix']) . '\');' . "\n";
  34. $file = fopen(DIR_OPENCART . 'config.php', 'w');
  35. fwrite($file, $output);
  36. fclose($file);
  37. $output = '<?php' . "\n";
  38. $output .= '// HTTP' . "\n";
  39. $output .= 'define(\'HTTP_SERVER\', \'' . HTTP_OPENCART . 'admin/\');' . "\n";
  40. $output .= 'define(\'HTTP_CATALOG\', \'' . HTTP_OPENCART . '\');' . "\n\n";
  41. $output .= '// HTTPS' . "\n";
  42. $output .= 'define(\'HTTPS_SERVER\', \'' . HTTP_OPENCART . 'admin/\');' . "\n";
  43. $output .= 'define(\'HTTPS_CATALOG\', \'' . HTTP_OPENCART . '\');' . "\n\n";
  44. $output .= '// DIR' . "\n";
  45. $output .= 'define(\'DIR_APPLICATION\', \'' . DIR_OPENCART . 'admin/\');' . "\n";
  46. $output .= 'define(\'DIR_SYSTEM\', \'' . DIR_OPENCART . 'system/\');' . "\n";
  47. $output .= 'define(\'DIR_IMAGE\', \'' . DIR_OPENCART . 'image/\');' . "\n";
  48. $output .= 'define(\'DIR_LANGUAGE\', \'' . DIR_OPENCART . 'admin/language/\');' . "\n";
  49. $output .= 'define(\'DIR_TEMPLATE\', \'' . DIR_OPENCART . 'admin/view/template/\');' . "\n";
  50. $output .= 'define(\'DIR_CONFIG\', \'' . DIR_OPENCART . 'system/config/\');' . "\n";
  51. $output .= 'define(\'DIR_CACHE\', \'' . DIR_OPENCART . 'system/storage/cache/\');' . "\n";
  52. $output .= 'define(\'DIR_DOWNLOAD\', \'' . DIR_OPENCART . 'system/storage/download/\');' . "\n";
  53. $output .= 'define(\'DIR_LOGS\', \'' . DIR_OPENCART . 'system/storage/logs/\');' . "\n";
  54. $output .= 'define(\'DIR_MODIFICATION\', \'' . DIR_OPENCART . 'system/storage/modification/\');' . "\n";
  55. $output .= 'define(\'DIR_UPLOAD\', \'' . DIR_OPENCART . 'system/storage/upload/\');' . "\n";
  56. $output .= 'define(\'DIR_CATALOG\', \'' . DIR_OPENCART . 'catalog/\');' . "\n\n";
  57. $output .= '// DB' . "\n";
  58. $output .= 'define(\'DB_DRIVER\', \'' . addslashes($this->request->post['db_driver']) . '\');' . "\n";
  59. $output .= 'define(\'DB_HOSTNAME\', \'' . addslashes($this->request->post['db_hostname']) . '\');' . "\n";
  60. $output .= 'define(\'DB_USERNAME\', \'' . addslashes($this->request->post['db_username']) . '\');' . "\n";
  61. $output .= 'define(\'DB_PASSWORD\', \'' . addslashes(html_entity_decode($this->request->post['db_password'], ENT_QUOTES, 'UTF-8')) . '\');' . "\n";
  62. $output .= 'define(\'DB_DATABASE\', \'' . addslashes($this->request->post['db_database']) . '\');' . "\n";
  63. $output .= 'define(\'DB_PORT\', \'' . addslashes($this->request->post['db_port']) . '\');' . "\n";
  64. $output .= 'define(\'DB_PREFIX\', \'' . addslashes($this->request->post['db_prefix']) . '\');' . "\n";
  65. $file = fopen(DIR_OPENCART . 'admin/config.php', 'w');
  66. fwrite($file, $output);
  67. fclose($file);
  68. $this->response->redirect($this->url->link('install/step_4'));
  69. }
  70. $this->document->setTitle($this->language->get('heading_title'));
  71. $data['heading_title'] = $this->language->get('heading_title');
  72. $data['text_step_3'] = $this->language->get('text_step_3');
  73. $data['text_db_connection'] = $this->language->get('text_db_connection');
  74. $data['text_db_administration'] = $this->language->get('text_db_administration');
  75. $data['text_mysqli'] = $this->language->get('text_mysqli');
  76. $data['text_mpdo'] = $this->language->get('text_mpdo');
  77. $data['text_pgsql'] = $this->language->get('text_pgsql');
  78. $data['entry_db_driver'] = $this->language->get('entry_db_driver');
  79. $data['entry_db_hostname'] = $this->language->get('entry_db_hostname');
  80. $data['entry_db_username'] = $this->language->get('entry_db_username');
  81. $data['entry_db_password'] = $this->language->get('entry_db_password');
  82. $data['entry_db_database'] = $this->language->get('entry_db_database');
  83. $data['entry_db_port'] = $this->language->get('entry_db_port');
  84. $data['entry_db_prefix'] = $this->language->get('entry_db_prefix');
  85. $data['entry_username'] = $this->language->get('entry_username');
  86. $data['entry_password'] = $this->language->get('entry_password');
  87. $data['entry_email'] = $this->language->get('entry_email');
  88. $data['button_continue'] = $this->language->get('button_continue');
  89. $data['button_back'] = $this->language->get('button_back');
  90. if (isset($this->error['warning'])) {
  91. $data['error_warning'] = $this->error['warning'];
  92. } else {
  93. $data['error_warning'] = '';
  94. }
  95. if (isset($this->error['db_hostname'])) {
  96. $data['error_db_hostname'] = $this->error['db_hostname'];
  97. } else {
  98. $data['error_db_hostname'] = '';
  99. }
  100. if (isset($this->error['db_username'])) {
  101. $data['error_db_username'] = $this->error['db_username'];
  102. } else {
  103. $data['error_db_username'] = '';
  104. }
  105. if (isset($this->error['db_database'])) {
  106. $data['error_db_database'] = $this->error['db_database'];
  107. } else {
  108. $data['error_db_database'] = '';
  109. }
  110. if (isset($this->error['db_port'])) {
  111. $data['error_db_port'] = $this->error['db_port'];
  112. } else {
  113. $data['error_db_port'] = '';
  114. }
  115. if (isset($this->error['db_prefix'])) {
  116. $data['error_db_prefix'] = $this->error['db_prefix'];
  117. } else {
  118. $data['error_db_prefix'] = '';
  119. }
  120. if (isset($this->error['username'])) {
  121. $data['error_username'] = $this->error['username'];
  122. } else {
  123. $data['error_username'] = '';
  124. }
  125. if (isset($this->error['password'])) {
  126. $data['error_password'] = $this->error['password'];
  127. } else {
  128. $data['error_password'] = '';
  129. }
  130. if (isset($this->error['email'])) {
  131. $data['error_email'] = $this->error['email'];
  132. } else {
  133. $data['error_email'] = '';
  134. }
  135. $data['action'] = $this->url->link('install/step_3');
  136. if (isset($this->request->post['db_driver'])) {
  137. $data['db_driver'] = $this->request->post['db_driver'];
  138. } else {
  139. $data['db_driver'] = '';
  140. }
  141. if (isset($this->request->post['db_hostname'])) {
  142. $data['db_hostname'] = $this->request->post['db_hostname'];
  143. } else {
  144. $data['db_hostname'] = 'localhost';
  145. }
  146. if (isset($this->request->post['db_username'])) {
  147. $data['db_username'] = $this->request->post['db_username'];
  148. } else {
  149. $data['db_username'] = 'root';
  150. }
  151. if (isset($this->request->post['db_password'])) {
  152. $data['db_password'] = $this->request->post['db_password'];
  153. } else {
  154. $data['db_password'] = '';
  155. }
  156. if (isset($this->request->post['db_database'])) {
  157. $data['db_database'] = $this->request->post['db_database'];
  158. } else {
  159. $data['db_database'] = '';
  160. }
  161. if (isset($this->request->post['db_port'])) {
  162. $data['db_port'] = $this->request->post['db_port'];
  163. } else {
  164. $data['db_port'] = 3306;
  165. }
  166. if (isset($this->request->post['db_prefix'])) {
  167. $data['db_prefix'] = $this->request->post['db_prefix'];
  168. } else {
  169. $data['db_prefix'] = 'oc_';
  170. }
  171. if (isset($this->request->post['username'])) {
  172. $data['username'] = $this->request->post['username'];
  173. } else {
  174. $data['username'] = 'admin';
  175. }
  176. if (isset($this->request->post['password'])) {
  177. $data['password'] = $this->request->post['password'];
  178. } else {
  179. $data['password'] = '';
  180. }
  181. if (isset($this->request->post['email'])) {
  182. $data['email'] = $this->request->post['email'];
  183. } else {
  184. $data['email'] = '';
  185. }
  186. $data['mysqli'] = extension_loaded('mysqli');
  187. $data['mysql'] = extension_loaded('mysql');
  188. $data['pdo'] = extension_loaded('pdo');
  189. $data['pgsql'] = extension_loaded('pgsql');
  190. $data['back'] = $this->url->link('install/step_2');
  191. $data['footer'] = $this->load->controller('common/footer');
  192. $data['header'] = $this->load->controller('common/header');
  193. $data['column_left'] = $this->load->controller('common/column_left');
  194. $this->response->setOutput($this->load->view('install/step_3', $data));
  195. }
  196. private function validate() {
  197. if (!$this->request->post['db_hostname']) {
  198. $this->error['db_hostname'] = $this->language->get('error_db_hostname');
  199. }
  200. if (!$this->request->post['db_username']) {
  201. $this->error['db_username'] = $this->language->get('error_db_username');
  202. }
  203. if (!$this->request->post['db_database']) {
  204. $this->error['db_database'] = $this->language->get('error_db_database');
  205. }
  206. if (!$this->request->post['db_port']) {
  207. $this->error['db_port'] = $this->language->get('error_db_port');
  208. }
  209. if ($this->request->post['db_prefix'] && preg_match('/[^a-z0-9_]/', $this->request->post['db_prefix'])) {
  210. $this->error['db_prefix'] = $this->language->get('error_db_prefix');
  211. }
  212. if ($this->request->post['db_driver'] == 'mysqli') {
  213. $mysql = @new MySQLi($this->request->post['db_hostname'], $this->request->post['db_username'], html_entity_decode($this->request->post['db_password'], ENT_QUOTES, 'UTF-8'), $this->request->post['db_database'], $this->request->post['db_port']);
  214. if ($mysql->connect_error) {
  215. $this->error['warning'] = $mysql->connect_error;
  216. } else {
  217. $mysql->close();
  218. }
  219. } elseif ($this->request->post['db_driver'] == 'mpdo') {
  220. try {
  221. new \DB\mPDO($this->request->post['db_hostname'], $this->request->post['db_username'], $this->request->post['db_password'], $this->request->post['db_database'], $this->request->post['db_port']);
  222. } catch(Exception $e) {
  223. $this->error['warning'] = $e->getMessage();
  224. }
  225. }
  226. if (!$this->request->post['username']) {
  227. $this->error['username'] = $this->language->get('error_username');
  228. }
  229. if (!$this->request->post['password']) {
  230. $this->error['password'] = $this->language->get('error_password');
  231. }
  232. if ((utf8_strlen($this->request->post['email']) > 96) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
  233. $this->error['email'] = $this->language->get('error_email');
  234. }
  235. if (!is_writable(DIR_OPENCART . 'config.php')) {
  236. $this->error['warning'] = $this->language->get('error_config') . DIR_OPENCART . 'config.php!';
  237. }
  238. if (!is_writable(DIR_OPENCART . 'admin/config.php')) {
  239. $this->error['warning'] = $this->language->get('error_config') . DIR_OPENCART . 'admin/config.php!';
  240. }
  241. return !$this->error;
  242. }
  243. }