Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Installer extends MX_Controller
- {
- private $_database = array();
- /**
- *
- * Constructor - Initializes and references CI
- *
- */
- public function __construct()
- {
- parent::__construct();
- $this->load->config('trinity');
- }
- /**
- *
- * Displays the default page of the installer.
- *
- * @access private
- * @return void
- *
- */
- public function index()
- {
- $this->load->view('installer');
- }
- public function step_1()
- {
- $this->load->library('form_validation');
- $this->load->view('step_1');
- }
- public function step_1_confirmation()
- {
- $hostname = $this->input->post('hostname');
- $username = $this->input->post('username');
- $password = $this->input->post('password');
- $database = $this->input->post('database');
- $dbdriver = $this->input->post('dbdriver');
- $this->BuildDB($hostname, $username, $password, $database, $dbdriver);
- $this->BuildCFG();
- #redirect('step_2');
- }
- /**
- *
- * Build the database.
- *
- * @access private
- * @param string
- * @return void
- *
- */
- private function BuildDB($hostname, $username, $password, $database, $dbdriver)
- {
- // This array will be written to the config file!
- $db = array(
- 'hostname' => $hostname,
- 'username' => $username,
- 'password' => $password,
- 'database' => $database,
- 'dbdriver' => $dbdriver,
- 'dbprefix' => '\'\'',
- 'pconnect' => 'FALSE',
- 'db_debug' => 'FALSE',
- 'cache_on' => 'FALSE',
- 'cachedir' => '\'\'',
- 'char_set' => 'utf8',
- 'dbcollat' => 'utf8_general_ci',
- 'swap_pre' => '',
- 'autoinit' => 'TRUE',
- 'stricton' => 'FALSE'
- );
- // We are supposed to use it later, right?
- $this->_database['default'] = $db;
- // Database connection array
- $dbconn = array(
- 'hostname' => $hostname,
- 'username' => $username,
- 'password' => $password,
- 'database' => $database,
- 'dbdriver' => $dbdriver,
- 'dbprefix' => '',
- 'pconnect' => FALSE,
- 'db_debug' => FALSE,
- 'cache_on' => FALSE,
- 'cachedir' => '',
- 'char_set' => 'utf8',
- 'dbcollat' => 'utf8_general_ci',
- 'swap_pre' => '',
- 'autoinit' => TRUE,
- 'stricton' => FALSE
- );
- // Blank password
- if($db['password'] == FALSE)
- {
- $db['password'] = '';
- }
- // Test connection
- if(!($connection = @mysql_connect($db['hostname'], $db['username'], $db['password'])))
- {
- @mysql_close($connection);
- show_error('Cannot connect to the MySQL server. Please try again.');
- }
- else
- {
- // Close connection
- @mysql_close($connection);
- // Connect to the database
- $this->load->database($dbconn);
- // Create the database if it doesn't exists
- $this->load->dbutil();
- if(!$this->dbutil->database_exists($db['database']))
- {
- $this->load->dbforge();
- $this->dbforge->create_database($db['database']);
- }
- // Insert the necessary data inside the database.
- $path = BASEPATH.'sql';
- $query = file_get_contents($path.'/database.sql');
- $result = explode(';', $query);
- foreach($result as $v)
- {
- $this->db->query($v);
- }
- }
- }
- /**
- *
- * Build the config.
- *
- * @access private
- * @return void
- *
- */
- private function BuildCFG()
- {
- $db = $this->_database['default'];
- $config = fopen(APPPATH.'config/database.php', 'w');
- foreach($db as $k => $v)
- {
- fwrite($config, '$config[\'default\'][\''.$k.'\'] = \''.$v.'\';');
- }
- $content = file_get_contents(APPPATH.'config/database.php');
- str_replace("'TRUE'", 'TRUE', $content);
- str_replace("'FALSE\'", 'FALSE', $content);
- fwrite($config, $content);
- fclose($config);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement