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

/installer/controllers/installer.php

http://github.com/pyrocms/pyrocms
PHP | 474 lines | 257 code | 66 blank | 151 comment | 14 complexity | 24b8e0f50a4f808dc39a148f2a04fa54 MD5 | raw file
Possible License(s): CC0-1.0, MIT
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * @author Yorick Peterse - PyroCMS development team
  4. * @package PyroCMS
  5. * @subpackage Installer
  6. * @category Application
  7. * @since v0.9.6.2
  8. *
  9. * Installer controller.
  10. */
  11. class Installer extends CI_Controller
  12. {
  13. /**
  14. * Array of languages supported by the installer
  15. */
  16. private $languages = array ('arabic', 'brazilian', 'english', 'dutch', 'french', 'german', 'polish', 'chinese_traditional', 'slovenian', 'spanish', 'russian', 'greek', 'lithuanian','danish','vietnamese', 'indonesian');
  17. /**
  18. * Array containing the directories that need to be writeable
  19. *
  20. * @access private
  21. * @var array
  22. */
  23. private $writeable_directories = array(
  24. 'system/cms/cache',
  25. 'system/cms/config',
  26. 'addons',
  27. 'uploads'
  28. );
  29. /**
  30. * Array containing the files that need to be writeable
  31. *
  32. * @access private
  33. * @var array
  34. */
  35. private $writeable_files = array(
  36. 'system/cms/config/config.php'
  37. );
  38. /**
  39. * Constructor method
  40. *
  41. * @access public
  42. * @return void
  43. */
  44. public function __construct()
  45. {
  46. parent::__construct();
  47. // Load the config file that contains a list of supported servers
  48. $this->load->config('servers');
  49. // Sets the language
  50. $this->_set_language();
  51. // Load form validation
  52. $this->load->library('form_validation');
  53. }
  54. /**
  55. * Index method
  56. *
  57. * @access public
  58. * @return void
  59. */
  60. public function index()
  61. {
  62. // The index function doesn't do that much itself, it only displays a view file with 3 buttons : Install, Upgrade and Maintenance.
  63. $data['page_output'] = $this->parser->parse('main', $this->lang->language, TRUE);
  64. // Load the view file
  65. $this->load->view('global',$data);
  66. }
  67. /**
  68. * Pre installation
  69. *
  70. * @access public
  71. * @return void
  72. */
  73. public function step_1()
  74. {
  75. // Save this junk for later
  76. $this->session->set_userdata(array(
  77. 'hostname' => $this->input->post('hostname'),
  78. 'username' => $this->input->post('username'),
  79. 'password' => $this->input->post('password'),
  80. 'port' => $this->input->post('port'),
  81. 'http_server' => $this->input->post('http_server')
  82. ));
  83. // Set rules
  84. $this->form_validation->set_rules(array(
  85. array(
  86. 'field' => 'hostname',
  87. 'label' => 'lang:server',
  88. 'rules' => 'trim|required|callback_test_db_connection'
  89. ),
  90. array(
  91. 'field' => 'username',
  92. 'label' => 'lang:username',
  93. 'rules' => 'trim|required'
  94. ),
  95. array(
  96. 'field' => 'password',
  97. 'label' => 'lang:password',
  98. 'rules' => 'trim'
  99. ),
  100. array(
  101. 'field' => 'port',
  102. 'label' => 'lang:portnr',
  103. 'rules' => 'trim|required'
  104. ),
  105. array(
  106. 'field' => 'http_server',
  107. 'label' => 'lang:server_settings',
  108. 'rules' => 'trim|required'
  109. )
  110. ));
  111. // If the form validation passed
  112. if ( $this->form_validation->run() )
  113. {
  114. // Set the flashdata message
  115. $this->session->set_flashdata('message', lang('db_success') );
  116. $this->session->set_flashdata('message_type', 'success');
  117. // Redirect to the second step
  118. $this->session->set_userdata('step_1_passed', TRUE);
  119. redirect('installer/step_2');
  120. }
  121. // Get supported servers
  122. $supported_servers = $this->config->item('supported_servers');
  123. $data->server_options = array();
  124. foreach($supported_servers as $key => $server)
  125. {
  126. $data->server_options[$key] = $server['name'];
  127. }
  128. // Get the port from the session or set it to the default value when it isn't specified
  129. $data->port = $this->session->userdata('port') ? $this->session->userdata('port') : 3306;
  130. // Load language labels
  131. $data = array_merge((array) $data,$this->lang->language);
  132. // Load the view file
  133. $this->load->view('global', array(
  134. 'page_output' => $this->parser->parse('step_1', $data, TRUE)
  135. ));
  136. }
  137. /**
  138. *Function to validate the database name
  139. *
  140. * @access public
  141. * @return bool
  142. */
  143. public function validate_mysql_db_name($db_name)
  144. {
  145. $this->form_validation->set_message('validate_mysql_db_name', lang('invalid_db_name'));
  146. return $this->installer_lib->validate_mysql_db_name($db_name);
  147. }
  148. /**
  149. * Function to test the DB connection (used for the form validation)
  150. *
  151. * @access public
  152. * @return bool
  153. */
  154. public function test_db_connection()
  155. {
  156. if ( ! $this->installer_lib->test_db_connection())
  157. {
  158. $this->form_validation->set_message('test_db_connection', lang('db_failure') . mysql_error());
  159. return false;
  160. }
  161. return true;
  162. }
  163. /**
  164. * First actual installation step
  165. *
  166. * @access public
  167. * @return void
  168. */
  169. public function step_2()
  170. {
  171. // Did the user enter the DB settings ?
  172. if ( ! $this->session->userdata('step_1_passed'))
  173. {
  174. // Set the flashdata message
  175. $this->session->set_flashdata('message', lang('step1_failure'));
  176. $this->session->set_flashdata('message_type','failure');
  177. // Redirect
  178. redirect('');
  179. }
  180. // Check the PHP version
  181. $data->php_min_version = '5.2';
  182. $data->php_acceptable = $this->installer_lib->php_acceptable($data->php_min_version);
  183. $data->php_version = $this->installer_lib->php_version;
  184. // Check the MySQL data
  185. $data->mysql->server_version_acceptable = $this->installer_lib->mysql_acceptable('server');
  186. $data->mysql->client_version_acceptable = $this->installer_lib->mysql_acceptable('client');
  187. $data->mysql->server_version = $this->installer_lib->mysql_server_version;
  188. $data->mysql->client_version = $this->installer_lib->mysql_client_version;
  189. // Check the GD data
  190. $data->gd_acceptable = $this->installer_lib->gd_acceptable();
  191. $data->gd_version = $this->installer_lib->gd_version;
  192. // Check to see if Zlib is enabled
  193. $data->zlib_enabled = $this->installer_lib->zlib_enabled();
  194. // Check to see if Curl is enabled
  195. $data->curl_enabled = $this->installer_lib->curl_enabled();
  196. // Get the server
  197. $selected_server = $this->session->userdata('http_server');
  198. $supported_servers = $this->config->item('supported_servers');
  199. $data->http_server->supported = $this->installer_lib->verify_http_server($this->session->userdata('http_server'));
  200. $data->http_server->name = @$supported_servers[$selected_server]['name'];
  201. // Check the final results
  202. $data->step_passed = $this->installer_lib->check_server($data);
  203. $this->session->set_userdata('step_2_passed', $data->step_passed);
  204. // Load the view files
  205. $final_data['page_output'] = $this->load->view('step_2', $data, TRUE);
  206. $this->load->view('global',$final_data);
  207. }
  208. /**
  209. * Another step, yay!
  210. *
  211. * @access public
  212. * @return void
  213. */
  214. public function step_3()
  215. {
  216. if ( ! $this->session->userdata('step_1_passed') OR !$this->session->userdata('step_2_passed'))
  217. {
  218. // Redirect the user back to step 1
  219. redirect('installer/step_2');
  220. }
  221. // Load the file helper
  222. $this->load->helper('file');
  223. // Get the write permissions for the folders
  224. foreach($this->writeable_directories as $dir)
  225. {
  226. @chmod('../'.$dir, 0777);
  227. $permissions['directories'][$dir] = is_really_writable('../' . $dir);
  228. }
  229. foreach($this->writeable_files as $file)
  230. {
  231. @chmod('../'.$file, 0666);
  232. $permissions['files'][$file] = is_really_writable('../' . $file);
  233. }
  234. // If all permissions are TRUE, go ahead
  235. $data->step_passed = !in_array(FALSE, $permissions['directories']) && !in_array(FALSE, $permissions['files']);
  236. $this->session->set_userdata('step_3_passed', $data->step_passed);
  237. // View variables
  238. $data->permissions = $permissions;
  239. // Load the language labels
  240. $data = (object) array_merge((array) $data,$this->lang->language);
  241. // Load the view file
  242. $final_data['page_output'] = $this->parser->parse('step_3', $data, TRUE);
  243. $this->load->view('global', $final_data);
  244. }
  245. /**
  246. * Another step, damn thee steps, damn thee!
  247. *
  248. * @access public
  249. * @return void
  250. */
  251. public function step_4()
  252. {
  253. if ( ! $this->session->userdata('step_1_passed') OR ! $this->session->userdata('step_2_passed') OR ! $this->session->userdata('step_3_passed'))
  254. {
  255. // Redirect the user back to step 2
  256. redirect('installer/step_2');
  257. }
  258. // Set rules
  259. $this->form_validation->set_rules(array(
  260. array(
  261. 'field' => 'database',
  262. 'label' => 'lang:database',
  263. 'rules' => 'trim|required|callback_validate_mysql_db_name'
  264. ),
  265. array(
  266. 'field' => 'site_ref',
  267. 'label' => 'lang:site_ref',
  268. 'rules' => 'trim|required|alpha_dash'
  269. ),
  270. array(
  271. 'field' => 'user_name',
  272. 'label' => 'lang:username',
  273. 'rules' => 'trim|required'
  274. ),
  275. array(
  276. 'field' => 'user_firstname',
  277. 'label' => 'lang:first_name',
  278. 'rules' => 'trim|required'
  279. ),
  280. array(
  281. 'field' => 'user_lastname',
  282. 'label' => 'lang:last_name',
  283. 'rules' => 'trim|required'
  284. ),
  285. array(
  286. 'field' => 'user_email',
  287. 'label' => 'lang:email',
  288. 'rules' => 'trim|required|valid_email'
  289. ),
  290. array(
  291. 'field' => 'user_password',
  292. 'label' => 'lang:password',
  293. 'rules' => 'trim|min_length[6]|max_length[20]|required'
  294. ),
  295. ));
  296. // If the form validation failed (or did not run)
  297. if ($this->form_validation->run() == FALSE)
  298. {
  299. $final_data['page_output'] = $this->parser->parse('step_4', $this->lang->language, TRUE);
  300. $this->load->view('global', $final_data);
  301. }
  302. // If the form validation passed
  303. else
  304. {
  305. // Let's try to install the system
  306. $install = $this->installer_lib->install($_POST);
  307. // Did the install fail?
  308. if ($install['status'] === FALSE)
  309. {
  310. // Let's tell them why the install failed
  311. $this->session->set_flashdata('message', $this->lang->line('error_'.$install['code']) . $install['message']);
  312. $final_data['page_output'] = $this->parser->parse('step_4', $this->lang->language, TRUE);
  313. $this->load->view('global', $final_data);
  314. }
  315. // Success!
  316. $this->session->set_flashdata('message', lang('success'));
  317. $this->session->set_flashdata('message_type', 'success');
  318. // Store the default username and password in the session data
  319. $this->session->set_userdata('user', array(
  320. 'user_email' => $this->input->post('user_email'),
  321. 'user_password' => $this->input->post('user_password'),
  322. 'user_firstname'=> $this->input->post('user_firstname'),
  323. 'user_lastname' => $this->input->post('user_lastname')
  324. ));
  325. //define the default user email to be used in the settings module install
  326. define('DEFAULT_EMAIL', $this->input->post('user_email'));
  327. // Import the modules
  328. $this->load->library('module_import');
  329. $this->module_import->import_all();
  330. redirect('installer/complete');
  331. }
  332. }
  333. /**
  334. * We're done, thank god for that
  335. *
  336. * @access public
  337. * @return void
  338. */
  339. public function complete()
  340. {
  341. // check if we came from step4
  342. if ( ! $this->session->userdata('user'))
  343. {
  344. redirect(site_url());
  345. }
  346. $server_name = $this->session->userdata('http_server');
  347. $supported_servers = $this->config->item('supported_servers');
  348. // Load our user's settings
  349. $data = $this->session->userdata('user');
  350. // Load the language labels
  351. $data = array_merge((array) $data, $this->lang->language);
  352. // Create the admin link
  353. $data['website_url'] = 'http://'.$this->input->server('HTTP_HOST').preg_replace('/installer\/index.php$/', '', $this->input->server('SCRIPT_NAME'));
  354. $data['control_panel_url'] = $data['website_url'] . ($supported_servers[$server_name]['rewrite_support'] === TRUE ? 'admin' : 'index.php/admin');
  355. // Let's remove our session since it contains data we don't want anyone to see
  356. $this->session->sess_destroy();
  357. // Load the view files
  358. $data['page_output'] = $this->parser->parse('complete',$data, TRUE);
  359. $this->load->view('global',$data);
  360. }
  361. /**
  362. * Changes the active language
  363. *
  364. * @access public
  365. * @author jeroenvdgulik
  366. * @since 0.9.8.1
  367. * @param string $language
  368. * @return void
  369. */
  370. public function change($language)
  371. {
  372. if (in_array($language, $this->languages))
  373. {
  374. $this->session->set_userdata('language', $language);
  375. }
  376. redirect('installer');
  377. }
  378. /**
  379. * Sets the language and loads the corresponding language files
  380. *
  381. * @access private
  382. * @author jeroenvdgulik
  383. * @since 0.9.8.1
  384. * @return void
  385. */
  386. private function _set_language()
  387. {
  388. // let's check if the language is supported
  389. if (in_array($this->session->userdata('language'), $this->languages))
  390. {
  391. // if so we set it
  392. $this->config->set_item('language', $this->session->userdata('language'));
  393. }
  394. // let's load the language file belonging to the page i.e. method
  395. $lang_file = $this->config->item('language') . '/' . $this->router->fetch_method() . '_lang';
  396. if (is_file(realpath(dirname(__FILE__) . '/../language/' . $lang_file . EXT)))
  397. {
  398. $this->lang->load($this->router->fetch_method());
  399. }
  400. // also we load some generic language labels
  401. $this->lang->load('global');
  402. // set the supported languages to be saved in Settings for emails and .etc
  403. // modules > settings > details.php uses this
  404. require_once(dirname(FCPATH).'/system/cms/config/language.php');
  405. define('DEFAULT_LANG', $config['default_language']);
  406. }
  407. }
  408. /* End of file installer.php */