PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/application/modules/setup/controllers/setup.php

https://bitbucket.org/fusioninvoice_it/fusioninvoice
PHP | 360 lines | 267 code | 75 blank | 18 comment | 23 complexity | 1a392d686cbcb11447d66b4f7d63ca4f MD5 | raw file
  1. <?php
  2. if (!defined('BASEPATH'))
  3. exit('No direct script access allowed');
  4. /*
  5. * FusionInvoice
  6. *
  7. * A free and open source web based invoicing system
  8. *
  9. * @package FusionInvoice
  10. * @author Jesse Terry
  11. * @copyright Copyright (c) 2012 - 2013 FusionInvoice, LLC
  12. * @license http://www.fusioninvoice.com/license.txt
  13. * @link http://www.fusioninvoice.com
  14. *
  15. */
  16. class Setup extends MX_Controller {
  17. public $errors = 0;
  18. public function __construct()
  19. {
  20. parent::__construct();
  21. $this->load->library('session');
  22. $this->load->helper('file');
  23. $this->load->helper('directory');
  24. $this->load->helper('url');
  25. $this->load->helper('language');
  26. $this->load->model('mdl_setup');
  27. $this->load->module('layout');
  28. if (!$this->session->userdata('fi_lang'))
  29. {
  30. $this->session->set_userdata('fi_lang', 'italian'); //---it---
  31. }
  32. $this->lang->load('fi', $this->session->userdata('fi_lang'));
  33. //---it---inizio
  34. if ($this->session->userdata('fi_lang') == 'italian')
  35. $this->lang->load('custom', $this->session->userdata('fi_lang'));
  36. //---it---fine
  37. }
  38. public function index()
  39. {
  40. redirect('setup/language');
  41. }
  42. public function language()
  43. {
  44. if ($this->input->post('btn_continue'))
  45. {
  46. $this->session->set_userdata('fi_lang', $this->input->post('fi_lang'));
  47. $this->session->set_userdata('install_step', 'prerequisites');
  48. redirect('setup/prerequisites');
  49. }
  50. $this->session->unset_userdata('install_step');
  51. $this->session->unset_userdata('is_upgrade');
  52. $this->load->helper('directory');
  53. $languages = directory_map(APPPATH . '/language', TRUE);
  54. sort($languages);
  55. $this->layout->set('languages', $languages);
  56. $this->layout->buffer('content', 'setup/language');
  57. $this->layout->render('base');
  58. }
  59. public function prerequisites()
  60. {
  61. if ($this->session->userdata('install_step') <> 'prerequisites')
  62. {
  63. redirect('setup/language');
  64. }
  65. if ($this->input->post('btn_continue'))
  66. {
  67. $this->session->set_userdata('install_step', 'configure_database');
  68. redirect('setup/configure_database');
  69. }
  70. $this->layout->set(
  71. array(
  72. 'basics' => $this->check_basics(),
  73. 'writables' => $this->check_writables(),
  74. 'errors' => $this->errors
  75. )
  76. );
  77. $this->layout->buffer('content', 'setup/prerequisites');
  78. $this->layout->render('base');
  79. }
  80. public function configure_database()
  81. {
  82. if ($this->session->userdata('install_step') <> 'configure_database')
  83. {
  84. redirect('setup/prerequisites');
  85. }
  86. if ($this->input->post('btn_continue'))
  87. {
  88. $this->load_ci_database();
  89. // This might be an upgrade - check if it is
  90. if (!$this->db->table_exists('fi_versions'))
  91. {
  92. // This appears to be an install
  93. $this->session->set_userdata('install_step', 'install_tables');
  94. redirect('setup/install_tables');
  95. }
  96. else
  97. {
  98. // This appears to be an upgrade
  99. $this->session->set_userdata('is_upgrade', TRUE);
  100. $this->session->set_userdata('install_step', 'upgrade_tables');
  101. redirect('setup/upgrade_tables');
  102. }
  103. }
  104. if ($this->input->post('db_hostname'))
  105. {
  106. $this->write_database_config($this->input->post('db_hostname'), $this->input->post('db_username'), $this->input->post('db_password'), $this->input->post('db_database'));
  107. }
  108. $this->layout->set('database', $this->check_database());
  109. $this->layout->set('errors', $this->errors);
  110. $this->layout->buffer('content', 'setup/configure_database');
  111. $this->layout->render('base');
  112. }
  113. public function install_tables()
  114. {
  115. if ($this->session->userdata('install_step') <> 'install_tables')
  116. {
  117. redirect('setup/prerequisites');
  118. }
  119. if ($this->input->post('btn_continue'))
  120. {
  121. $this->session->set_userdata('install_step', 'upgrade_tables');
  122. redirect('setup/upgrade_tables');
  123. }
  124. $this->load_ci_database();
  125. $this->layout->set(
  126. array(
  127. 'success' => $this->mdl_setup->install_tables(),
  128. 'errors' => $this->mdl_setup->errors
  129. )
  130. );
  131. $this->layout->buffer('content', 'setup/install_tables');
  132. $this->layout->render('base');
  133. }
  134. public function upgrade_tables()
  135. {
  136. if ($this->session->userdata('install_step') <> 'upgrade_tables')
  137. {
  138. redirect('setup/prerequisites');
  139. }
  140. if ($this->input->post('btn_continue'))
  141. {
  142. if (!$this->session->userdata('is_upgrade'))
  143. {
  144. $this->session->set_userdata('install_step', 'create_user');
  145. redirect('setup/create_user');
  146. }
  147. else
  148. {
  149. $this->session->set_userdata('install_step', 'complete');
  150. redirect('setup/complete');
  151. }
  152. }
  153. $this->load_ci_database();
  154. $this->layout->set(
  155. array(
  156. 'success' => $this->mdl_setup->upgrade_tables(),
  157. 'errors' => $this->mdl_setup->errors
  158. )
  159. );
  160. $this->layout->buffer('content', 'setup/upgrade_tables');
  161. $this->layout->render('base');
  162. }
  163. public function create_user()
  164. {
  165. if ($this->session->userdata('install_step') <> 'create_user')
  166. {
  167. redirect('setup/prerequisites');
  168. }
  169. $this->load_ci_database();
  170. $this->load->model('users/mdl_users');
  171. if ($this->mdl_users->run_validation())
  172. {
  173. $db_array = $this->mdl_users->db_array();
  174. $db_array['user_type'] = 1;
  175. $this->mdl_users->save(NULL, $db_array);
  176. $this->session->set_userdata('install_step', 'complete');
  177. redirect('setup/complete');
  178. }
  179. $this->layout->buffer('content', 'setup/create_user');
  180. $this->layout->render('base');
  181. }
  182. public function complete()
  183. {
  184. if ($this->session->userdata('install_step') <> 'complete')
  185. {
  186. redirect('setup/prerequisites');
  187. }
  188. $this->layout->buffer('content', 'setup/complete');
  189. $this->layout->render('base');
  190. }
  191. private function check_writables()
  192. {
  193. $checks = array();
  194. $writables = array(
  195. './uploads',
  196. './uploads/temp',
  197. './' . APPPATH . 'config/database.php',
  198. './' . APPPATH . 'helpers/mpdf/tmp'
  199. );
  200. foreach ($writables as $writable)
  201. {
  202. if (!is_writable($writable))
  203. {
  204. $checks[] = array(
  205. 'message' => $writable . ' ' . lang('is_not_writable'),
  206. 'success' => 0
  207. );
  208. $this->errors += 1;
  209. }
  210. else
  211. {
  212. $checks[] = array(
  213. 'message' => $writable . ' ' . lang('is_writable'),
  214. 'success' => 1
  215. );
  216. }
  217. }
  218. return $checks;
  219. }
  220. private function check_database()
  221. {
  222. $this->load->library('lib_mysql');
  223. require(APPPATH . '/config/database.php');
  224. $db = $db['default'];
  225. $can_connect = $this->lib_mysql->connect($db['hostname'], $db['username'], $db['password']);
  226. if (!$can_connect)
  227. {
  228. $this->errors += 1;
  229. return array(
  230. 'message' => lang('cannot_connect_database_server'),
  231. 'success' => 0
  232. );
  233. }
  234. $can_select_db = $this->lib_mysql->select_db($db['database']);
  235. if (!$can_select_db)
  236. {
  237. $this->errors += 1;
  238. return array(
  239. 'message' => lang('cannot_select_specified_database'),
  240. 'success' => 0
  241. );
  242. }
  243. return array(
  244. 'message' => lang('database_properly_configured'),
  245. 'success' => 1
  246. );
  247. }
  248. private function check_basics()
  249. {
  250. $checks = array();
  251. $php_required = '5.3';
  252. $php_installed = PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION;
  253. if ($php_installed < $php_required)
  254. {
  255. $this->errors += 1;
  256. $checks[] = array(
  257. 'message' => sprintf(lang('php_version_fail'), $php_installed, $php_required),
  258. 'success' => 0
  259. );
  260. }
  261. else
  262. {
  263. $checks[] = array(
  264. 'message' => lang('php_version_success'),
  265. 'success' => 1
  266. );
  267. }
  268. return $checks;
  269. }
  270. private function write_database_config($hostname, $username, $password, $database)
  271. {
  272. $db_file = read_file(APPPATH . 'config/database_empty.php');
  273. $db_file = str_replace('$db[\'default\'][\'hostname\'] = \'\'', '$db[\'default\'][\'hostname\'] = \'' . $hostname . '\'', $db_file);
  274. $db_file = str_replace('$db[\'default\'][\'username\'] = \'\'', '$db[\'default\'][\'username\'] = \'' . $username . '\'', $db_file);
  275. $db_file = str_replace('$db[\'default\'][\'password\'] = \'\'', '$db[\'default\'][\'password\'] = \'' . $password . '\'', $db_file);
  276. $db_file = str_replace('$db[\'default\'][\'database\'] = \'\'', '$db[\'default\'][\'database\'] = \'' . $database . '\'', $db_file);
  277. write_file(APPPATH . 'config/database.php', $db_file);
  278. }
  279. private function load_ci_database()
  280. {
  281. $this->load->database();
  282. // $this->db->db_debug = 0;
  283. }
  284. }
  285. ?>