PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/shop/installation-2013-05-08/controller/step_3.php

https://bitbucket.org/jojoluzifer/gold-light-project
PHP | 256 lines | 206 code | 50 blank | 0 comment | 55 complexity | deeb20700480002a92caabe6a64d81a3 MD5 | raw file
  1. <?php
  2. class ControllerStep3 extends Controller {
  3. private $error = array();
  4. public function index() {
  5. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
  6. $this->load->model('install');
  7. $this->model_install->mysql($this->request->post);
  8. $output = '<?php' . "\n";
  9. $output .= '// HTTP' . "\n";
  10. $output .= 'define(\'HTTP_SERVER\', \'' . HTTP_OPENCART . '\');' . "\n\n";
  11. $output .= '// HTTPS' . "\n";
  12. $output .= 'define(\'HTTPS_SERVER\', \'' . HTTP_OPENCART . '\');' . "\n\n";
  13. $output .= '// DIR' . "\n";
  14. $output .= 'define(\'DIR_APPLICATION\', \'' . DIR_OPENCART . 'catalog/\');' . "\n";
  15. $output .= 'define(\'DIR_SYSTEM\', \'' . DIR_OPENCART. 'system/\');' . "\n";
  16. $output .= 'define(\'DIR_DATABASE\', \'' . DIR_OPENCART . 'system/database/\');' . "\n";
  17. $output .= 'define(\'DIR_LANGUAGE\', \'' . DIR_OPENCART . 'catalog/language/\');' . "\n";
  18. $output .= 'define(\'DIR_TEMPLATE\', \'' . DIR_OPENCART . 'catalog/view/theme/\');' . "\n";
  19. $output .= 'define(\'DIR_CONFIG\', \'' . DIR_OPENCART . 'system/config/\');' . "\n";
  20. $output .= 'define(\'DIR_IMAGE\', \'' . DIR_OPENCART . 'image/\');' . "\n";
  21. $output .= 'define(\'DIR_CACHE\', \'' . DIR_OPENCART . 'system/cache/\');' . "\n";
  22. $output .= 'define(\'DIR_DOWNLOAD\', \'' . DIR_OPENCART . 'download/\');' . "\n";
  23. $output .= 'define(\'DIR_LOGS\', \'' . DIR_OPENCART . 'system/logs/\');' . "\n\n";
  24. $output .= '// DB' . "\n";
  25. $output .= 'define(\'DB_DRIVER\', \'' . addslashes($this->request->post['db_driver']) . '\');' . "\n";
  26. $output .= 'define(\'DB_HOSTNAME\', \'' . addslashes($this->request->post['db_host']) . '\');' . "\n";
  27. $output .= 'define(\'DB_USERNAME\', \'' . addslashes($this->request->post['db_user']) . '\');' . "\n";
  28. $output .= 'define(\'DB_PASSWORD\', \'' . addslashes($this->request->post['db_password']) . '\');' . "\n";
  29. $output .= 'define(\'DB_DATABASE\', \'' . addslashes($this->request->post['db_name']) . '\');' . "\n";
  30. $output .= 'define(\'DB_PREFIX\', \'' . addslashes($this->request->post['db_prefix']) . '\');' . "\n";
  31. $output .= '?>';
  32. $file = fopen(DIR_OPENCART . 'config.php', 'w');
  33. fwrite($file, $output);
  34. fclose($file);
  35. $output = '<?php' . "\n";
  36. $output .= '// HTTP' . "\n";
  37. $output .= 'define(\'HTTP_SERVER\', \'' . HTTP_OPENCART . 'admin/\');' . "\n";
  38. $output .= 'define(\'HTTP_CATALOG\', \'' . HTTP_OPENCART . '\');' . "\n\n";
  39. $output .= '// HTTPS' . "\n";
  40. $output .= 'define(\'HTTPS_SERVER\', \'' . HTTP_OPENCART . 'admin/\');' . "\n";
  41. $output .= 'define(\'HTTPS_CATALOG\', \'' . HTTP_OPENCART . '\');' . "\n\n";
  42. $output .= '// DIR' . "\n";
  43. $output .= 'define(\'DIR_APPLICATION\', \'' . DIR_OPENCART . 'admin/\');' . "\n";
  44. $output .= 'define(\'DIR_SYSTEM\', \'' . DIR_OPENCART . 'system/\');' . "\n";
  45. $output .= 'define(\'DIR_DATABASE\', \'' . DIR_OPENCART . 'system/database/\');' . "\n";
  46. $output .= 'define(\'DIR_LANGUAGE\', \'' . DIR_OPENCART . 'admin/language/\');' . "\n";
  47. $output .= 'define(\'DIR_TEMPLATE\', \'' . DIR_OPENCART . 'admin/view/template/\');' . "\n";
  48. $output .= 'define(\'DIR_CONFIG\', \'' . DIR_OPENCART . 'system/config/\');' . "\n";
  49. $output .= 'define(\'DIR_IMAGE\', \'' . DIR_OPENCART . 'image/\');' . "\n";
  50. $output .= 'define(\'DIR_CACHE\', \'' . DIR_OPENCART . 'system/cache/\');' . "\n";
  51. $output .= 'define(\'DIR_DOWNLOAD\', \'' . DIR_OPENCART . 'download/\');' . "\n";
  52. $output .= 'define(\'DIR_LOGS\', \'' . DIR_OPENCART . 'system/logs/\');' . "\n";
  53. $output .= 'define(\'DIR_CATALOG\', \'' . DIR_OPENCART . 'catalog/\');' . "\n\n";
  54. $output .= '// DB' . "\n";
  55. $output .= 'define(\'DB_DRIVER\', \'' . addslashes($this->request->post['db_driver']) . '\');' . "\n";
  56. $output .= 'define(\'DB_HOSTNAME\', \'' . addslashes($this->request->post['db_host']) . '\');' . "\n";
  57. $output .= 'define(\'DB_USERNAME\', \'' . addslashes($this->request->post['db_user']) . '\');' . "\n";
  58. $output .= 'define(\'DB_PASSWORD\', \'' . addslashes($this->request->post['db_password']) . '\');' . "\n";
  59. $output .= 'define(\'DB_DATABASE\', \'' . addslashes($this->request->post['db_name']) . '\');' . "\n";
  60. $output .= 'define(\'DB_PREFIX\', \'' . addslashes($this->request->post['db_prefix']) . '\');' . "\n";
  61. $output .= '?>';
  62. $file = fopen(DIR_OPENCART . 'admin/config.php', 'w');
  63. fwrite($file, $output);
  64. fclose($file);
  65. $this->redirect($this->url->link('step_4'));
  66. }
  67. if (isset($this->error['warning'])) {
  68. $this->data['error_warning'] = $this->error['warning'];
  69. } else {
  70. $this->data['error_warning'] = '';
  71. }
  72. if (isset($this->error['db_host'])) {
  73. $this->data['error_db_host'] = $this->error['db_host'];
  74. } else {
  75. $this->data['error_db_host'] = '';
  76. }
  77. if (isset($this->error['db_user'])) {
  78. $this->data['error_db_user'] = $this->error['db_user'];
  79. } else {
  80. $this->data['error_db_user'] = '';
  81. }
  82. if (isset($this->error['db_name'])) {
  83. $this->data['error_db_name'] = $this->error['db_name'];
  84. } else {
  85. $this->data['error_db_name'] = '';
  86. }
  87. if (isset($this->error['db_prefix'])) {
  88. $this->data['error_db_prefix'] = $this->error['db_prefix'];
  89. } else {
  90. $this->data['error_db_prefix'] = '';
  91. }
  92. if (isset($this->error['username'])) {
  93. $this->data['error_username'] = $this->error['username'];
  94. } else {
  95. $this->data['error_username'] = '';
  96. }
  97. if (isset($this->error['password'])) {
  98. $this->data['error_password'] = $this->error['password'];
  99. } else {
  100. $this->data['error_password'] = '';
  101. }
  102. if (isset($this->error['email'])) {
  103. $this->data['error_email'] = $this->error['email'];
  104. } else {
  105. $this->data['error_email'] = '';
  106. }
  107. $this->data['action'] = $this->url->link('step_3');
  108. if (isset($this->request->post['db_driver'])) {
  109. $this->data['db_driver'] = $this->request->post['db_driver'];
  110. } else {
  111. $this->data['db_driver'] = 'mysql';
  112. }
  113. if (isset($this->request->post['db_host'])) {
  114. $this->data['db_host'] = $this->request->post['db_host'];
  115. } else {
  116. $this->data['db_host'] = 'localhost';
  117. }
  118. if (isset($this->request->post['db_user'])) {
  119. $this->data['db_user'] = html_entity_decode($this->request->post['db_user']);
  120. } else {
  121. $this->data['db_user'] = '';
  122. }
  123. if (isset($this->request->post['db_password'])) {
  124. $this->data['db_password'] = html_entity_decode($this->request->post['db_password']);
  125. } else {
  126. $this->data['db_password'] = '';
  127. }
  128. if (isset($this->request->post['db_name'])) {
  129. $this->data['db_name'] = html_entity_decode($this->request->post['db_name']);
  130. } else {
  131. $this->data['db_name'] = '';
  132. }
  133. if (isset($this->request->post['db_prefix'])) {
  134. $this->data['db_prefix'] = html_entity_decode($this->request->post['db_prefix']);
  135. } else {
  136. $this->data['db_prefix'] = 'oc_';
  137. }
  138. if (isset($this->request->post['username'])) {
  139. $this->data['username'] = $this->request->post['username'];
  140. } else {
  141. $this->data['username'] = 'admin';
  142. }
  143. if (isset($this->request->post['password'])) {
  144. $this->data['password'] = $this->request->post['password'];
  145. } else {
  146. $this->data['password'] = '';
  147. }
  148. if (isset($this->request->post['email'])) {
  149. $this->data['email'] = $this->request->post['email'];
  150. } else {
  151. $this->data['email'] = '';
  152. }
  153. $this->data['back'] = $this->url->link('step_2');
  154. $this->template = 'step_3.tpl';
  155. $this->children = array(
  156. 'header',
  157. 'footer'
  158. );
  159. $this->response->setOutput($this->render());
  160. }
  161. private function validate() {
  162. if (!$this->request->post['db_host']) {
  163. $this->error['db_host'] = 'Host required!';
  164. }
  165. if (!$this->request->post['db_user']) {
  166. $this->error['db_user'] = 'User required!';
  167. }
  168. if (!$this->request->post['db_name']) {
  169. $this->error['db_name'] = 'Database Name required!';
  170. }
  171. if ($this->request->post['db_prefix'] && preg_match('/[^a-z0-9_]/', $this->request->post['db_prefix'])) {
  172. $this->error['db_prefix'] = 'DB Prefix can only contain lowercase characters in the a-z range, 0-9 and "_"!';
  173. }
  174. if ($this->request->post['db_driver'] == 'mysql') {
  175. if (!$connection = @mysql_connect($this->request->post['db_host'], $this->request->post['db_user'], $this->request->post['db_password'])) {
  176. $this->error['warning'] = 'Error: Could not connect to the database please make sure the database server, username and password is correct!';
  177. } else {
  178. if (!@mysql_select_db($this->request->post['db_name'], $connection)) {
  179. $this->error['warning'] = 'Error: Database does not exist!';
  180. }
  181. mysql_close($connection);
  182. }
  183. }
  184. if (!$this->request->post['username']) {
  185. $this->error['username'] = 'Username required!';
  186. }
  187. if (!$this->request->post['password']) {
  188. $this->error['password'] = 'Password required!';
  189. }
  190. if ((utf8_strlen($this->request->post['email']) > 96) || !preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email'])) {
  191. $this->error['email'] = 'Invalid E-Mail!';
  192. }
  193. if (!is_writable(DIR_OPENCART . 'config.php')) {
  194. $this->error['warning'] = 'Error: Could not write to config.php please check you have set the correct permissions on: ' . DIR_OPENCART . 'config.php!';
  195. }
  196. if (!is_writable(DIR_OPENCART . 'admin/config.php')) {
  197. $this->error['warning'] = 'Error: Could not write to config.php please check you have set the correct permissions on: ' . DIR_OPENCART . 'admin/config.php!';
  198. }
  199. if (!$this->error) {
  200. return true;
  201. } else {
  202. return false;
  203. }
  204. }
  205. }
  206. ?>