PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/application/modules/install/install_.php

http://github.com/imagecms/ImageCMS
PHP | 363 lines | 286 code | 65 blank | 12 comment | 33 complexity | 0e54fa17609dc346f491cd53c1c86894 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Install extends MY_Controller {
  3. public $host = '';
  4. public $useSqlFile = 'sqlShopClean.sql'; // sqlShop.sql
  5. private $exts = FALSE;
  6. public function __construct()
  7. {
  8. error_reporting(0);
  9. parent::__construct();
  10. $this->host = 'http://'.str_replace('index.php', '',$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']).'index.php/';
  11. $this->load->helper('string');
  12. $this->host = reduce_multiples($this->host);
  13. $this->load->language('main', 'russian');
  14. }
  15. public function index()
  16. {
  17. $data = array(
  18. 'content' => $this->load->view('license', array('next_link' => $this->host.'install/step_1' ), TRUE),
  19. );
  20. $this->load->view('main', $data);
  21. }
  22. public function step_1()
  23. {
  24. $result = TRUE;
  25. // Check folders permissions
  26. $dir_array = array(
  27. './application/config/config.php' => 'ok',
  28. './system/cache' => 'ok',
  29. './captcha/' => 'ok',
  30. './system/cache/templates_c' => 'ok',
  31. './uploads/' => 'ok',
  32. './uploads/images' => 'ok',
  33. './uploads/files' => 'ok',
  34. './uploads/media' => 'ok',
  35. );
  36. foreach ($dir_array as $k => $v)
  37. {
  38. if ( is_really_writable($k) === TRUE )
  39. {
  40. $dir_array[$k] = 'ok';
  41. }
  42. else
  43. {
  44. $dir_array[$k] = 'err';
  45. $result = FALSE;
  46. }
  47. }
  48. // Check server params
  49. $allow_params = array(
  50. 'register_globals' => 'ok',
  51. 'safe_mode' => 'ok',
  52. );
  53. foreach($allow_params as $k => $v)
  54. {
  55. if ( ini_get($k) == 1 )
  56. {
  57. $allow_params[$k] = 'warning';
  58. }else{
  59. $allow_params[$k] = 'ok';
  60. }
  61. }
  62. // Check installed php exts.
  63. $exts = array(
  64. 'curl' => 'ok',
  65. 'json' => 'ok',
  66. 'mbstring' => 'ok',
  67. 'iconv' => 'ok',
  68. 'gd' => 'ok',
  69. 'zlib' => 'ok',
  70. );
  71. foreach($exts as $k => $v)
  72. {
  73. if ( $this->_get_ext($k) === FALSE )
  74. {
  75. $exts[$k] = 'warning';
  76. if ($k == 'json')
  77. {
  78. $exts[$k] = 'err';
  79. $result = FALSE;
  80. }
  81. if ($k == 'mbstring')
  82. {
  83. $exts[$k] = 'err';
  84. $result = FALSE;
  85. }
  86. if ($k == 'curl')
  87. {
  88. $exts[$k] = 'err';
  89. $result = FALSE;
  90. }
  91. }
  92. }
  93. $data = array(
  94. 'dirs' => $dir_array,
  95. 'need_params' => $need_params,
  96. 'allow_params' => $allow_params,
  97. 'exts' => $exts,
  98. 'next_link' => $this->_get_next_link($result, 1),
  99. );
  100. $this->_display( $this->load->view('step_1', $data, TRUE) );
  101. }
  102. private function _get_ext($name = '')
  103. {
  104. if ($this->exts === FALSE)
  105. {
  106. ob_start();
  107. phpinfo(INFO_MODULES);
  108. $this->exts = ob_get_contents();
  109. ob_end_clean();
  110. $this->exts = strip_tags($this->exts,'<h2><th><td>');
  111. }
  112. $result = preg_match("/<h2>.*$name.*<\/h2>/", $this->exts, $m);
  113. if (count($m) == 0)
  114. {
  115. return FALSE;
  116. }
  117. return TRUE;
  118. }
  119. public function step_2()
  120. {
  121. $this->load->library('Form_validation');
  122. $this->form_validation->set_error_delimiters('', '');
  123. $result = TRUE;
  124. $other_errors = '';
  125. if (count($_POST) > 0)
  126. {
  127. $this->form_validation->set_rules('site_title', '???????? ?????', 'required');
  128. $this->form_validation->set_rules('db_host', '????', 'required');
  129. $this->form_validation->set_rules('db_user', '??? ???????????? ??', 'required');
  130. //$this->form_validation->set_rules('db_pass', '?????? ??', 'required');
  131. $this->form_validation->set_rules('db_name', '??? ??', 'required');
  132. $this->form_validation->set_rules('admin_login', '????? ??????????????', 'required|min_length[4]');
  133. $this->form_validation->set_rules('admin_pass', '?????? ??????????????', 'required|min_length[5]');
  134. $this->form_validation->set_rules('admin_mail', '????? ?????????????', 'required|valid_email');
  135. if ($this->form_validation->run() == FALSE)
  136. {
  137. $result = FALSE;
  138. }
  139. else
  140. {
  141. // Test database conn.
  142. if( $this->test_db() == FALSE )
  143. {
  144. $other_errors .= '?????? ??????????? ? ???? ??????.<br/>';
  145. $result = FALSE;
  146. }
  147. }
  148. if ($result == TRUE)
  149. {
  150. $this->make_install();
  151. }
  152. }
  153. $data = array(
  154. 'next_link' => $this->_get_next_link($result, 2),
  155. 'other_errors' => $other_errors,
  156. 'host' => $this->host,
  157. 'sqlFileName' => $this->useSqlFile,
  158. );
  159. $this->_display( $this->load->view('step_2', $data, TRUE) );
  160. }
  161. private function make_install()
  162. {
  163. $this->load->helper('file');
  164. $this->load->helper('url');
  165. $db_server = $this->input->post('db_host');
  166. $db_user = $this->input->post('db_user');
  167. $db_pass = $this->input->post('db_pass');
  168. $db_name = $this->input->post('db_name');
  169. $link = mysql_connect($db_server, $db_user, $db_pass);
  170. $db_sel = mysql_select_db($db_name);
  171. // Drop all tables in DB
  172. $tables = array();
  173. $sql = "SHOW TABLES FROM $db_name";
  174. if($result = mysql_query($sql, $link))
  175. {
  176. while($row = mysql_fetch_row($result))
  177. {
  178. $tables[] = $row[0];
  179. }
  180. }
  181. if (count($tables) > 0)
  182. {
  183. foreach($tables as $t)
  184. {
  185. $sql = "DROP TABLE $db_name.$t";
  186. if(!mysql_query($sql, $link))
  187. {
  188. die ("MySQL error. Can\'t delete $db_name.$t");
  189. }
  190. }
  191. }
  192. // Insert sql data
  193. if($this->input->post('product_samples') == "on")
  194. {
  195. $this->useSqlFile = 'sqlShop.sql';
  196. }
  197. mysql_query('SET NAMES `utf8`;', $link);
  198. $sqlFileData = read_file(dirname(__FILE__).'/'.$this->useSqlFile);
  199. $queries = explode(";\n", $sqlFileData);
  200. foreach ($queries as $q)
  201. {
  202. $q = trim($q);
  203. if ($q != '')
  204. {
  205. mysql_query($q.';',$link);
  206. }
  207. }
  208. // Update site title
  209. mysql_query('UPDATE `settings` SET `site_title`=\''.mysql_real_escape_string($this->input->post('site_title')).'\' ', $link);
  210. // Create admin account
  211. $this->load->helper('cookie');
  212. delete_cookie('autologin');
  213. $this->load->library('DX_Auth');
  214. $admin_pass = crypt($this->dx_auth->_encode( $this->input->post('admin_pass') ));
  215. $admin_login = mysql_real_escape_string( $this->input->post('admin_login') );
  216. $admin_mail = mysql_real_escape_string( $this->input->post('admin_mail') );
  217. $admin_created = date('Y-m-d H:i:s', time());
  218. $sql = "INSERT INTO `users` (`id`, `role_id`, `username`, `password`, `email`, `banned`, `ban_reason`, `newpass`, `newpass_key`, `newpass_time`, `last_ip`, `last_login`, `created`, `modified`) VALUES
  219. (1, 2, '$admin_login', '$admin_pass', '$admin_mail', 0, NULL, NULL, NULL, NULL, '127.0.0.1', '0000-00-00 00:00:00', '$admin_created', '0000-00-00 00:00:00'); ";
  220. mysql_query($sql,$link);
  221. // Rewrite config file
  222. $this->write_config_file();
  223. //redirect('install/done','refresh');
  224. header("Location: ".$this->host."install/done");
  225. }
  226. public function done()
  227. {
  228. $this->_display( $this->load->view('done', '',TRUE) );
  229. }
  230. private function write_config_file()
  231. {
  232. $config_file = APPPATH.'config/config.php';
  233. $config_file_copy = APPPATH.'modules/install/config.php';
  234. $this->load->helper('file');
  235. $config = read_file($config_file_copy);
  236. $db_server = $this->input->post('db_host');
  237. $db_user = $this->input->post('db_user');
  238. $db_pass = $this->input->post('db_pass');
  239. $db_name = $this->input->post('db_name');
  240. $db_settings="\$db['default']['hostname'] = '$db_server';
  241. \$db['default']['username'] = '$db_user';
  242. \$db['default']['password'] = '$db_pass';
  243. \$db['default']['database'] = '$db_name';
  244. \$db['default']['dbdriver'] = 'mysql';
  245. \$db['default']['dbprefix'] = '';
  246. \$db['default']['pconnect'] = FALSE;
  247. \$db['default']['db_debug'] = TRUE;
  248. \$db['default']['cache_on'] = FALSE;
  249. \$db['default']['cachedir'] = '';
  250. \$db['default']['char_set'] = 'utf8';
  251. \$db['default']['dbcollat'] = 'utf8_general_ci';
  252. \$db['default']['swap_pre'] = '';
  253. \$db['default']['autoinit'] = TRUE;
  254. \$db['default']['stricton'] = FALSE;
  255. ";
  256. $config = str_replace('{DB_SETTINGS}', $db_settings, $config);
  257. if ( ! write_file($config_file, $config))
  258. {
  259. die('?????? ?????? ????? config.php');
  260. }
  261. }
  262. private function _get_next_link($result = FALSE, $step = 1)
  263. {
  264. if ($result === TRUE)
  265. {
  266. $next_link = $this->host.'install/step_'.($step + 1);
  267. }
  268. else
  269. {
  270. $next_link = $this->host.'install/step_'.$step;
  271. }
  272. return $next_link;
  273. }
  274. public function _display($content)
  275. {
  276. $data = array(
  277. 'content' => $content,
  278. );
  279. $this->load->view('main', $data);
  280. }
  281. private function test_db()
  282. {
  283. $result = TRUE;
  284. $db_server = $this->input->post('db_host');
  285. $db_user = $this->input->post('db_user');
  286. $db_pass = $this->input->post('db_pass');
  287. $db_name = $this->input->post('db_name');
  288. $link = mysql_connect($db_server, $db_user, $db_pass);
  289. $db_sel = mysql_select_db($db_name);
  290. if ($link == FALSE OR $db_sel == FALSE)
  291. {
  292. $result = FALSE;
  293. }
  294. mysql_close($link);
  295. return $result;
  296. }
  297. }
  298. /* End of file install.php */