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->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, 'default');
- $this->BuildCFG('default');
- redirect('installer/step_2');
- }
- public function step_2()
- {
- $this->load->view('step_2');
- }
- public function step_2_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, 'auth');
- $this->BuildCFG('auth');
- $this->BuildTAuthCFG();
- redirect('installer/step_3_1');
- }
- public function step_3_1()
- {
- $this->load->view('step_3_1');
- }
- public function step_3_2()
- {
- $this->load->view('step_3_2');
- $post = $this->input->post('realms');
- $data = array(
- 'realms' => $post
- );
- $this->session->set_userdata($data);
- }
- public function step_3_confirmation()
- {
- $this->BuildCharactersCFG($this->session->userdata('realms'));
- 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!');
- }
- /**
- *
- * Build the database.
- *
- * @access private
- * @param string
- * @return void
- *
- */
- private function BuildDB($hostname, $username, $password, $database, $dbdriver, $type)
- {
- switch($type)
- {
- case 'default':
- // 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);
- }
- }
- }
- break;
- case 'auth':
- // 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'
- );
- $this->_database['auth'] = $db;
- // 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.');
- }
- break;
- }
- }
- /**
- *
- * Build the default config.
- *
- * @access private
- * @param string
- * @return void
- *
- */
- private function BuildCFG($type)
- {
- $db = $this->_database[$type];
- $config = fopen(APPPATH.'config/database.php', 'a+');
- $content = '';
- foreach($db as $k => $v)
- {
- if(!in_array($v, array('TRUE', 'FALSE')))
- {
- $content .= '$db[\''.$type.'\'][\''.$k.'\'] = \''.$v.'\';';
- }
- else
- {
- $content .= '$db[\''.$type.'\'][\''.$k.'\'] = '.$v.';';
- }
- $content .= PHP_EOL;
- }
- fwrite($config, $content);
- fclose($config);
- }
- /**
- *
- * Inserts auth database inside Trinity.php config
- *
- * @access private
- * @return void
- *
- */
- private function BuildTAuthCFG()
- {
- $file = fopen(APPPATH.'config/trinity.php', 'a+');
- $text = '$config[\'DB_auth\'] = \''.$this->_database['auth']['database'].'\';';
- fwrite($file, $text);
- fclose($file);
- }
- /**
- *
- * Build the characters config.
- *
- * @access private
- * @return void
- *
- */
- private function BuildCharactersCFG($realms)
- {
- $post = $this->input->post();
- $content = '';
- for($i = 1; $i <= $realms; $i++)
- {
- $db[$post['database_'.$i]] = array(
- 'hostname' => $post['hostname_'.$i],
- 'username' => $post['username_'.$i],
- 'password' => $post['password_'.$i],
- 'database' => $post['database_'.$i],
- 'dbdriver' => $post['dbdriver_'.$i],
- 'dbprefix' => '',
- 'pconnect' => 'FALSE',
- 'db_debug' => 'FALSE',
- 'cache_on' => 'FALSE',
- 'cachedir' => '',
- 'char_set' => 'utf8',
- 'dbcollat' => 'utf8_general_ci',
- 'swap_pre' => '',
- 'autoinit' => 'TRUE',
- 'stricton' => 'FALSE'
- );
- $dbchar[$i] = array(
- 'DB_char' => $post['database_'.$i]
- );
- }
- foreach($db as $k => $v)
- {
- foreach($v as $key => $value)
- {
- if(!in_array($value, array('TRUE', 'FALSE')))
- {
- $content .= '$db[\''.$k.'\'][\''.$key.'\'] = \''.$value.'\';';
- }
- else
- {
- $content .= '$db[\''.$k.'\'][\''.$key.'\'] = '.$value.';';
- }
- $content .= PHP_EOL;
- }
- }
- $config = fopen(APPPATH.'config/database.php', 'a+');
- fwrite($config, $content);
- fclose($config);
- $txt = '';
- foreach($dbchar as $index => $val)
- {
- foreach($val as $ind => $k)
- {
- $txt .= PHP_EOL;
- $txt .= '$config[\'DB_chars\']['.$index.'][\'DB_char\'] = \''.$k.'\';';
- }
- }
- $trinityconf = fopen(APPPATH.'config/trinity.php', 'a+');
- fwrite($trinityconf, $txt);
- $realmlist = '';
- $realmlist .= PHP_EOL;
- $realmlist .= '$config[\'realmlist\'] = \''.$post['realmlist'].'\';';
- fwrite($trinityconf, $realmlist);
- fclose($trinityconf);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement