Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.94 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Installer extends MX_Controller
  4. {
  5. private $_database = array();
  6. /**
  7. *
  8. * Constructor - Initializes and references CI
  9. *
  10. */
  11.  
  12. public function __construct()
  13. {
  14. parent::__construct();
  15.  
  16. $this->load->config('trinity');
  17. }
  18.  
  19. /**
  20. *
  21. * Displays the default page of the installer.
  22. *
  23. * @access private
  24. * @return void
  25. *
  26. */
  27.  
  28. public function index()
  29. {
  30. $this->load->view('installer');
  31. }
  32.  
  33. public function step_1()
  34. {
  35. $this->load->view('step_1');
  36. }
  37.  
  38. public function step_1_confirmation()
  39. {
  40. $hostname = $this->input->post('hostname');
  41. $username = $this->input->post('username');
  42. $password = $this->input->post('password');
  43. $database = $this->input->post('database');
  44. $dbdriver = $this->input->post('dbdriver');
  45. $this->BuildDB($hostname, $username, $password, $database, $dbdriver, 'default');
  46. $this->BuildCFG('default');
  47. redirect('installer/step_2');
  48. }
  49.  
  50. public function step_2()
  51. {
  52. $this->load->view('step_2');
  53. }
  54.  
  55. public function step_2_confirmation()
  56. {
  57. $hostname = $this->input->post('hostname');
  58. $username = $this->input->post('username');
  59. $password = $this->input->post('password');
  60. $database = $this->input->post('database');
  61. $dbdriver = $this->input->post('dbdriver');
  62. $this->BuildDB($hostname, $username, $password, $database, $dbdriver, 'auth');
  63. $this->BuildCFG('auth');
  64. $this->BuildTAuthCFG();
  65. redirect('installer/step_3_1');
  66. }
  67.  
  68. public function step_3_1()
  69. {
  70. $this->load->view('step_3_1');
  71. }
  72.  
  73. public function step_3_2()
  74. {
  75. $this->load->view('step_3_2');
  76. $post = $this->input->post('realms');
  77. $data = array(
  78. 'realms' => $post
  79. );
  80. $this->session->set_userdata($data);
  81. }
  82.  
  83. public function step_3_confirmation()
  84. {
  85. $this->BuildCharactersCFG($this->session->userdata('realms'));
  86. show_error('Congratulations, you are almost done installing your new site. All you have to do now is delete the installer folder located in path/applications/modules/installer . If you don\'t do this your site\'s security is compromised!');
  87. }
  88.  
  89. /**
  90. *
  91. * Build the database.
  92. *
  93. * @access private
  94. * @param string
  95. * @return void
  96. *
  97. */
  98.  
  99. private function BuildDB($hostname, $username, $password, $database, $dbdriver, $type)
  100. {
  101. switch($type)
  102. {
  103. case 'default':
  104. // This array will be written to the config file!
  105. $db = array(
  106. 'hostname' => $hostname,
  107. 'username' => $username,
  108. 'password' => $password,
  109. 'database' => $database,
  110. 'dbdriver' => $dbdriver,
  111. 'dbprefix' => '',
  112. 'pconnect' => 'FALSE',
  113. 'db_debug' => 'FALSE',
  114. 'cache_on' => 'FALSE',
  115. 'cachedir' => '',
  116. 'char_set' => 'utf8',
  117. 'dbcollat' => 'utf8_general_ci',
  118. 'swap_pre' => '',
  119. 'autoinit' => 'TRUE',
  120. 'stricton' => 'FALSE'
  121. );
  122.  
  123. // We are supposed to use it later, right?
  124. $this->_database['default'] = $db;
  125. // Database connection array
  126. $dbconn = array(
  127. 'hostname' => $hostname,
  128. 'username' => $username,
  129. 'password' => $password,
  130. 'database' => $database,
  131. 'dbdriver' => $dbdriver,
  132. 'dbprefix' => '',
  133. 'pconnect' => FALSE,
  134. 'db_debug' => FALSE,
  135. 'cache_on' => FALSE,
  136. 'cachedir' => '',
  137. 'char_set' => 'utf8',
  138. 'dbcollat' => 'utf8_general_ci',
  139. 'swap_pre' => '',
  140. 'autoinit' => TRUE,
  141. 'stricton' => FALSE
  142. );
  143.  
  144. // Blank password
  145. if($db['password'] == FALSE)
  146. {
  147. $db['password'] = '';
  148. }
  149.  
  150. // Test connection
  151. if(!($connection = @mysql_connect($db['hostname'], $db['username'], $db['password'])))
  152. {
  153. @mysql_close($connection);
  154. show_error('Cannot connect to the MySQL server. Please try again.');
  155. }
  156. else
  157. {
  158. // Close connection
  159. @mysql_close($connection);
  160. // Connect to the database
  161. $this->load->database($dbconn);
  162.  
  163. // Create the database if it doesn't exists
  164. $this->load->dbutil();
  165. if(!$this->dbutil->database_exists($db['database']))
  166. {
  167. $this->load->dbforge();
  168. $this->dbforge->create_database($db['database']);
  169. // Insert the necessary data inside the database.
  170. $path = BASEPATH.'sql';
  171. $query = file_get_contents($path.'/database.sql');
  172. $result = explode(';', $query);
  173. foreach($result as $v)
  174. {
  175. $this->db->query($v);
  176. }
  177. }
  178. }
  179. break;
  180.  
  181. case 'auth':
  182. // This array will be written to the config file!
  183. $db = array(
  184. 'hostname' => $hostname,
  185. 'username' => $username,
  186. 'password' => $password,
  187. 'database' => $database,
  188. 'dbdriver' => $dbdriver,
  189. 'dbprefix' => '',
  190. 'pconnect' => 'FALSE',
  191. 'db_debug' => 'FALSE',
  192. 'cache_on' => 'FALSE',
  193. 'cachedir' => '',
  194. 'char_set' => 'utf8',
  195. 'dbcollat' => 'utf8_general_ci',
  196. 'swap_pre' => '',
  197. 'autoinit' => 'TRUE',
  198. 'stricton' => 'FALSE'
  199. );
  200. $this->_database['auth'] = $db;
  201.  
  202. // Test connection
  203. if(!($connection = @mysql_connect($db['hostname'], $db['username'], $db['password'])))
  204. {
  205. @mysql_close($connection);
  206. show_error('Cannot connect to the MySQL server. Please try again.');
  207. }
  208. break;
  209. }
  210. }
  211.  
  212. /**
  213. *
  214. * Build the default config.
  215. *
  216. * @access private
  217. * @param string
  218. * @return void
  219. *
  220. */
  221.  
  222. private function BuildCFG($type)
  223. {
  224. $db = $this->_database[$type];
  225. $config = fopen(APPPATH.'config/database.php', 'a+');
  226. $content = '';
  227. foreach($db as $k => $v)
  228. {
  229. if(!in_array($v, array('TRUE', 'FALSE')))
  230. {
  231. $content .= '$db[\''.$type.'\'][\''.$k.'\'] = \''.$v.'\';';
  232. }
  233. else
  234. {
  235. $content .= '$db[\''.$type.'\'][\''.$k.'\'] = '.$v.';';
  236. }
  237. $content .= PHP_EOL;
  238. }
  239.  
  240. fwrite($config, $content);
  241. fclose($config);
  242. }
  243.  
  244. /**
  245. *
  246. * Inserts auth database inside Trinity.php config
  247. *
  248. * @access private
  249. * @return void
  250. *
  251. */
  252.  
  253. private function BuildTAuthCFG()
  254. {
  255. $file = fopen(APPPATH.'config/trinity.php', 'a+');
  256. $text = '$config[\'DB_auth\'] = \''.$this->_database['auth']['database'].'\';';
  257. fwrite($file, $text);
  258. fclose($file);
  259. }
  260. /**
  261. *
  262. * Build the characters config.
  263. *
  264. * @access private
  265. * @return void
  266. *
  267. */
  268.  
  269. private function BuildCharactersCFG($realms)
  270. {
  271. $post = $this->input->post();
  272. $content = '';
  273. for($i = 1; $i <= $realms; $i++)
  274. {
  275. $db[$post['database_'.$i]] = array(
  276. 'hostname' => $post['hostname_'.$i],
  277. 'username' => $post['username_'.$i],
  278. 'password' => $post['password_'.$i],
  279. 'database' => $post['database_'.$i],
  280. 'dbdriver' => $post['dbdriver_'.$i],
  281. 'dbprefix' => '',
  282. 'pconnect' => 'FALSE',
  283. 'db_debug' => 'FALSE',
  284. 'cache_on' => 'FALSE',
  285. 'cachedir' => '',
  286. 'char_set' => 'utf8',
  287. 'dbcollat' => 'utf8_general_ci',
  288. 'swap_pre' => '',
  289. 'autoinit' => 'TRUE',
  290. 'stricton' => 'FALSE'
  291. );
  292.  
  293. $dbchar[$i] = array(
  294. 'DB_char' => $post['database_'.$i]
  295. );
  296. }
  297. foreach($db as $k => $v)
  298. {
  299. foreach($v as $key => $value)
  300. {
  301. if(!in_array($value, array('TRUE', 'FALSE')))
  302. {
  303. $content .= '$db[\''.$k.'\'][\''.$key.'\'] = \''.$value.'\';';
  304. }
  305. else
  306. {
  307. $content .= '$db[\''.$k.'\'][\''.$key.'\'] = '.$value.';';
  308. }
  309. $content .= PHP_EOL;
  310. }
  311. }
  312. $config = fopen(APPPATH.'config/database.php', 'a+');
  313. fwrite($config, $content);
  314. fclose($config);
  315. $txt = '';
  316. foreach($dbchar as $index => $val)
  317. {
  318. foreach($val as $ind => $k)
  319. {
  320. $txt .= PHP_EOL;
  321. $txt .= '$config[\'DB_chars\']['.$index.'][\'DB_char\'] = \''.$k.'\';';
  322. }
  323. }
  324.  
  325. $trinityconf = fopen(APPPATH.'config/trinity.php', 'a+');
  326. fwrite($trinityconf, $txt);
  327. $realmlist = '';
  328. $realmlist .= PHP_EOL;
  329. $realmlist .= '$config[\'realmlist\'] = \''.$post['realmlist'].'\';';
  330. fwrite($trinityconf, $realmlist);
  331. fclose($trinityconf);
  332. }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement