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 3.70 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. *
  9. * Constructor - Initializes and references CI
  10. *
  11. */
  12.  
  13. public function __construct()
  14. {
  15. parent::__construct();
  16.  
  17. $this->load->config('trinity');
  18. }
  19.  
  20. /**
  21. *
  22. * Displays the default page of the installer.
  23. *
  24. * @access private
  25. * @return void
  26. *
  27. */
  28.  
  29. public function index()
  30. {
  31. $this->load->view('installer');
  32. }
  33.  
  34. public function step_1()
  35. {
  36. $this->load->library('form_validation');
  37. $this->load->view('step_1');
  38. }
  39.  
  40. public function step_1_confirmation()
  41. {
  42. $hostname = $this->input->post('hostname');
  43. $username = $this->input->post('username');
  44. $password = $this->input->post('password');
  45. $database = $this->input->post('database');
  46. $dbdriver = $this->input->post('dbdriver');
  47. $this->BuildDB($hostname, $username, $password, $database, $dbdriver);
  48. $this->BuildCFG();
  49. #redirect('step_2');
  50. }
  51.  
  52. /**
  53. *
  54. * Build the database.
  55. *
  56. * @access private
  57. * @param string
  58. * @return void
  59. *
  60. */
  61.  
  62. private function BuildDB($hostname, $username, $password, $database, $dbdriver)
  63. {
  64. // This array will be written to the config file!
  65. $db = array(
  66. 'hostname' => $hostname,
  67. 'username' => $username,
  68. 'password' => $password,
  69. 'database' => $database,
  70. 'dbdriver' => $dbdriver,
  71. 'dbprefix' => '\'\'',
  72. 'pconnect' => 'FALSE',
  73. 'db_debug' => 'FALSE',
  74. 'cache_on' => 'FALSE',
  75. 'cachedir' => '\'\'',
  76. 'char_set' => 'utf8',
  77. 'dbcollat' => 'utf8_general_ci',
  78. 'swap_pre' => '',
  79. 'autoinit' => 'TRUE',
  80. 'stricton' => 'FALSE'
  81. );
  82.  
  83. // We are supposed to use it later, right?
  84. $this->_database['default'] = $db;
  85.  
  86. // Database connection array
  87. $dbconn = array(
  88. 'hostname' => $hostname,
  89. 'username' => $username,
  90. 'password' => $password,
  91. 'database' => $database,
  92. 'dbdriver' => $dbdriver,
  93. 'dbprefix' => '',
  94. 'pconnect' => FALSE,
  95. 'db_debug' => FALSE,
  96. 'cache_on' => FALSE,
  97. 'cachedir' => '',
  98. 'char_set' => 'utf8',
  99. 'dbcollat' => 'utf8_general_ci',
  100. 'swap_pre' => '',
  101. 'autoinit' => TRUE,
  102. 'stricton' => FALSE
  103. );
  104.  
  105. // Blank password
  106. if($db['password'] == FALSE)
  107. {
  108. $db['password'] = '';
  109. }
  110.  
  111. // Test connection
  112. if(!($connection = @mysql_connect($db['hostname'], $db['username'], $db['password'])))
  113. {
  114. @mysql_close($connection);
  115. show_error('Cannot connect to the MySQL server. Please try again.');
  116. }
  117. else
  118. {
  119. // Close connection
  120. @mysql_close($connection);
  121. // Connect to the database
  122. $this->load->database($dbconn);
  123.  
  124. // Create the database if it doesn't exists
  125. $this->load->dbutil();
  126. if(!$this->dbutil->database_exists($db['database']))
  127. {
  128. $this->load->dbforge();
  129. $this->dbforge->create_database($db['database']);
  130. }
  131.  
  132. // Insert the necessary data inside the database.
  133. $path = BASEPATH.'sql';
  134. $query = file_get_contents($path.'/database.sql');
  135. $result = explode(';', $query);
  136. foreach($result as $v)
  137. {
  138. $this->db->query($v);
  139. }
  140. }
  141. }
  142.  
  143. /**
  144. *
  145. * Build the config.
  146. *
  147. * @access private
  148. * @return void
  149. *
  150. */
  151.  
  152. private function BuildCFG()
  153. {
  154. $db = $this->_database['default'];
  155.  
  156. $config = fopen(APPPATH.'config/database.php', 'w');
  157.  
  158. $content = '';
  159.  
  160. foreach($db as $k => $v)
  161. {
  162. if( ! in_array($v, array('TRUE', 'FALSE')))
  163. {
  164. $content .= '$config[\'default\'][\''.$k.'\'] = \''.$v.'\';';
  165. }
  166.  
  167. else
  168. {
  169. $content .= '$config[\'default\'][\''.$k.'\'] = '.$v.';';
  170. }
  171.  
  172. $content .= PHP_EOL;
  173. }
  174.  
  175. fwrite($config, $content);
  176. fclose($config);
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement