PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/install.php

https://bitbucket.org/nanomites_webdev/heroframework
PHP | 285 lines | 198 code | 48 blank | 39 comment | 33 complexity | ad5195ad56dfc7f6f634033d62dfbcad MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-2.0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Install Controller
  4. *
  5. * Installs app by 1) generating config details and setting up the DB file and, 2) Creating the first admin account
  6. *
  7. * @copyright Electric Function, Inc.
  8. * @author Electric Function, Inc.
  9. * @package Hero Framework
  10. */
  11. class Install extends CI_Controller {
  12. function Install()
  13. {
  14. parent::__construct();
  15. define("_INSTALLER","1");
  16. // make sure we have a trailing slash
  17. if (substr($this->current_url(),-7,7) == 'install') {
  18. // we don't have a trailing slash
  19. header('Location: ' . $this->current_url() . '/');
  20. die();
  21. }
  22. // no one should access this if app is installed
  23. if (file_exists(APPPATH . 'config/installed.php')) {
  24. show_error($this->config->item('app_name') .' has already been installed. This file, /system/opengateway/controllers/install.php, can be deleted.');
  25. die();
  26. }
  27. }
  28. function index() {
  29. $this->load->helper('file');
  30. $this->load->helper('string');
  31. // check for submission
  32. if ($this->input->post('base_url') != '') {
  33. // we have a submission
  34. // validate MySQL info
  35. $valid_mysql = FALSE;
  36. if ($dbh = @mysql_connect($this->input->post('db_host'),$this->input->post('db_user'),$this->input->post('db_pass')))
  37. {
  38. if (@mysql_select_db($this->input->post('db_name'), $dbh))
  39. {
  40. $valid_mysql = TRUE;
  41. }
  42. }
  43. if ($valid_mysql == FALSE) {
  44. $error_mysql = TRUE;
  45. }
  46. $base_url = rtrim($this->input->post('base_url'),'/') . '/';
  47. $cron_key = $this->input->post('cron_key');
  48. $encryption_key = $this->input->post('encryption_key');
  49. if (empty($base_url) or empty($cron_key) or empty($encryption_key)) {
  50. $error_empty_site = TRUE;
  51. }
  52. if (!strstr($base_url,'http://')) {
  53. $error_base_url = TRUE;
  54. }
  55. // no errors? let's write to config files
  56. if (!isset($error_empty_site) and !isset($error_base_url) and !isset($error_mysql)) {
  57. // all good!
  58. // read in current config
  59. $config_file = read_file(APPPATH . 'config/config.php');
  60. // swap in variables
  61. $config_file = preg_replace('/\$config\[\'base_url\'\](.*?)\=(.*?)\"(.*?)\"/','$config[\'base_url\'] = "' . $base_url . '"',$config_file);
  62. $config_file = preg_replace('/\$config\[\'cron_key\'\](.*?)\=(.*?)\'(.*?)\'/','$config[\'cron_key\'] = \'' . $cron_key . '\'',$config_file);
  63. $config_file = preg_replace('/\$config\[\'encryption_key\'\](.*?)\=(.*?)\"(.*?)\"/','$config[\'encryption_key\'] = "' . $encryption_key . '"',$config_file);
  64. // write config file
  65. write_file(APPPATH . 'config/config.php',$config_file,'w');
  66. // create database file
  67. $database_file = read_file(APPPATH . 'config/database.format.php');
  68. $database_file = preg_replace('/\$db\[\'default\'\]\[\'hostname\'\](.*?)\=(.*?)\"(.*?)\"/','$db[\'default\'][\'hostname\'] = "' . $this->input->post('db_host') . '"',$database_file);
  69. $database_file = preg_replace('/\$db\[\'default\'\]\[\'username\'\](.*?)\=(.*?)\"(.*?)\"/','$db[\'default\'][\'username\'] = "' . $this->input->post('db_user') . '"',$database_file);
  70. $database_file = preg_replace('/\$db\[\'default\'\]\[\'password\'\](.*?)\=(.*?)\'(.*?)\'/','$db[\'default\'][\'password\'] = \'' . $this->input->post('db_pass') . '\'',$database_file);
  71. $database_file = preg_replace('/\$db\[\'default\'\]\[\'database\'\](.*?)\=(.*?)\"(.*?)\"/','$db[\'default\'][\'database\'] = "' . $this->input->post('db_name') . '"',$database_file);
  72. // write database file
  73. write_file(APPPATH . 'config/database.php',$database_file,'w');
  74. // import initial database structure
  75. // note - all update files will be run before the next step loads (because auto_updater will be invoked)
  76. $structure = read_file(APPPATH . 'updates/install.php');
  77. $structure = str_replace('<?php if (!defined(\'BASEPATH\')) exit(\'No direct script access allowed\'); ?>','',$structure);
  78. // break into newlines
  79. $structure = explode("\n",$structure);
  80. // run mysql queries
  81. $query = "";
  82. $querycount = 0;
  83. foreach ($structure as $sql_line)
  84. {
  85. if (trim($sql_line) != "" and substr($sql_line,0,2) != "--")
  86. {
  87. $query .= $sql_line;
  88. if (substr(trim($query), -1, 1) == ";")
  89. {
  90. // this query is finished, execute it
  91. if (@mysql_query($query, $dbh))
  92. {
  93. $query = "";
  94. $querycount++;
  95. }
  96. else {
  97. show_error('There was a critical error importing the initial database structure. Please contact support.<br /><br />Query:<br /><br />' . $query);
  98. die();
  99. }
  100. }
  101. }
  102. }
  103. // update settings
  104. mysql_query('UPDATE `settings` SET `setting_value`=\'' . $this->input->post('site_name') . '\' WHERE `setting_name`=\'site_name\' or `setting_name`=\'email_name\'');
  105. mysql_query('UPDATE `settings` SET `setting_value`=\'' . $this->input->post('site_email') . '\' WHERE `setting_name`=\'site_email\'');
  106. // send to administrator account setup
  107. if (strstr($this->current_url(),'/index')) {
  108. $forward_url = str_replace('/index','/admin',current_url());
  109. }
  110. else {
  111. $forward_url = rtrim($this->current_url(),'/') . '/admin';
  112. }
  113. // send to admin step
  114. header('Location: ' . $forward_url);
  115. die();
  116. }
  117. }
  118. // which folders/files should be writeable?
  119. $file_permissions = array(
  120. str_replace('system/','',BASEPATH) . 'writeable',
  121. APPPATH . 'config',
  122. APPPATH . 'config/config.php'
  123. );
  124. $file_permission_errors = array();
  125. foreach ($file_permissions as $file) {
  126. if (!is_writable($file)) {
  127. $file_permission_errors[] = array(
  128. 'file' => $file,
  129. 'folder' => (is_dir($file)) ? TRUE : FALSE
  130. );
  131. }
  132. }
  133. // get domain name
  134. $domain = ($this->input->post('base_url')) ? $this->input->post('base_url') : rtrim(str_replace(array('install','index'),'',$this->current_url()), '/') . '/';
  135. // default values
  136. $db_user = ($this->input->post('db_user')) ? $this->input->post('db_user') : '';
  137. $db_host = ($this->input->post('db_host')) ? $this->input->post('db_host') : 'localhost';
  138. $db_pass = ($this->input->post('db_pass')) ? $this->input->post('db_pass') : '';
  139. $db_name = ($this->input->post('db_name')) ? $this->input->post('db_name') : '';
  140. $site_name = ($this->input->post('site_name')) ? $this->input->post('site_name') : 'Your Website';
  141. // build email from $domain
  142. $email_domain = str_replace(array('http://','www.','/'),'',$domain);
  143. $site_email = ($this->input->post('site_email')) ? $this->input->post('site_email') : 'you@' . $email_domain;
  144. // generate random keys
  145. $cron_key = random_string('unique');
  146. $encryption_key = random_string('unique');
  147. $vars = array(
  148. 'file_permission_errors' => $file_permission_errors,
  149. 'domain' => $domain,
  150. 'cron_key' => $cron_key,
  151. 'encryption_key' => $encryption_key,
  152. 'site_name' => $site_name,
  153. 'site_email' => $site_email,
  154. 'error_mysql' => (isset($error_mysql) and !empty($error_mysql)) ? TRUE : FALSE,
  155. 'error_empty_site' => (isset($error_empty_site) and !empty($error_empty_site)) ? TRUE : FALSE,
  156. 'error_base_url' => (isset($error_base_url) and !empty($error_base_url)) ? TRUE : FALSE,
  157. 'db_user' => $db_user,
  158. 'db_host' => $db_host,
  159. 'db_name' => $db_name,
  160. 'db_pass' => $db_pass
  161. );
  162. $this->load->view(branded_view('install/configuration.php'), $vars);
  163. }
  164. function current_url() {
  165. $pageURL = 'http';
  166. if (isset($_SERVER["HTTPS"]) and $_SERVER["HTTPS"] == "on") {
  167. $pageURL .= "s";
  168. }
  169. $pageURL .= "://";
  170. if ($_SERVER["SERVER_PORT"] != "80") {
  171. $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  172. } else {
  173. $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  174. }
  175. return $pageURL;
  176. }
  177. function admin () {
  178. $this->load->library('session');
  179. $this->load->helper('url');
  180. if ($this->input->post('username')) {
  181. if ($this->input->post('password') != $this->input->post('password2')) {
  182. $error_password = 'Your passwords do not match.';
  183. }
  184. elseif (strlen($this->input->post('password')) < 6) {
  185. $error_password = 'Your password is less than 6 characters in length. It must be longer.';
  186. }
  187. if (!isset($error_password)) {
  188. // form submission
  189. $user_id = $this->user_model->new_user(
  190. $this->input->post('email'),
  191. $this->input->post('password'),
  192. $this->input->post('username'),
  193. $this->input->post('first_name'),
  194. $this->input->post('last_name'),
  195. FALSE,
  196. FALSE,
  197. TRUE);
  198. if (isset($user_id)) {
  199. // success!
  200. $this->session->set_userdata('username',$this->input->post('username'));
  201. $this->session->set_userdata('email',$this->input->post('email'));
  202. $this->session->set_userdata('password',$this->input->post('password'));
  203. header('Location: ' . site_url('install/complete'));
  204. die();
  205. }
  206. }
  207. }
  208. // default values
  209. $username = ($this->input->post('username')) ? $this->input->post('username') : 'admin';
  210. $email = ($this->input->post('email')) ? $this->input->post('email') : '';
  211. $first_name = ($this->input->post('first_name')) ? $this->input->post('first_name') : '';
  212. $last_name = ($this->input->post('last_name')) ? $this->input->post('last_name') : '';
  213. $gmt_offset = ($this->input->post('gmt_offset')) ? $this->input->post('gmt_offset') : 'UM5';
  214. $vars = array(
  215. 'username' => $username,
  216. 'email' => $email,
  217. 'first_name' => $first_name,
  218. 'last_name' => $last_name,
  219. 'gmt_offset' => $gmt_offset,
  220. 'error_password' => (isset($error_password)) ? $error_password : FALSE
  221. );
  222. $this->load->view(branded_view('install/admin.php'), $vars);
  223. }
  224. function complete () {
  225. $this->load->helper('file');
  226. // write the file that disables the installer - they can't even refresh this page now
  227. write_file(APPPATH . 'config/installed.php', '<?php /* App is installed */ ?>','w');
  228. $vars = array(
  229. 'username' => $this->session->userdata('username'),
  230. 'email' => $this->session->userdata('email'),
  231. 'password' => $this->session->userdata('password'),
  232. 'cron_key' => $this->config->item('cron_key'),
  233. 'cp_link' => site_url('admincp')
  234. );
  235. $this->load->view(branded_view('install/complete.php'), $vars);
  236. }
  237. }