Untitled

 avatar
unknown
php
4 years ago
11 kB
4
Indexable
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Install extends CI_Controller {

    public function __construct()
	{
        parent::__construct();

        $this->load->helper('form');
        $this->lang->load('auth');
        $this->load->library(array('session', 'form_validation'));
        $this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));
        
        if ($this->router->default_controller != 'install') {
            redirect(site_url('auth/login'), 'refresh');
        }
	}

	public function index()
	{
        $this->session->sess_destroy();
        $this->data['title'] = 'Welcome';
        $this->load->view('install/index', $this->data);
    }

    public function step($step)
    {        
        switch($step) {
            case '1':
                $this->data['checks'] = $this->check_requirements();
                $this->data['title'] = 'Requirements';
                $this->load->view('install/step_one', $this->data);
                break;
            case '2':

                $checks = $this->check_requirements();

                if (!$checks['success']) {
                    redirect(site_url('install/step/1'), 'refresh');
                }

                if (isset($_POST) && !empty($_POST)) {

                    $dbauth = array(
                        'hostname' => $this->input->post('hostname'),
                        'database' => $this->input->post('database'),
                        'username' => $this->input->post('username'),
                        'password' => $this->input->post('password'),
                    );
                    $this->session->set_userdata('dbauth', $dbauth);
                    $connection = $this->check_database_connection();

                    if ($connection == 'failed') {
                        $this->session->set_flashdata('message', '<div class="alert alert-default alert-dismissible fade show">Error establishing a database connection<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>');
                    } else if ($connection == 'dbnotfound') {
                        $this->session->set_flashdata('message', '<div class="alert alert-default alert-dismissible fade show">Database not found<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>');
                    } else {
                        redirect(site_url('install/migrate'), 'refresh');
                    }
                }

                $this->data['title'] = 'Database Configuration';
                $this->data['message'] = $this->session->flashdata('message');
                $this->data['message'] = $this->session->flashdata('message');
                $this->data['dbauth'] = $this->session->userdata('dbauth');
                $this->data['database'] = $this->session->userdata('database_setup');
                $this->load->view('install/step_two', $this->data);                
                break;
            case '3':
                if ($this->session->userdata('database_setup') !== 1) {
                    redirect(site_url('install/step/3'), 'refresh');
                }

                if (isset($_POST) && !empty($_POST)) {
                    $this->session->set_userdata('license_key', $this->input->post('license_key'));
                    $this->session->set_userdata('license', 'extended');
                    redirect(site_url('install/step/4'), 'refresh');
                }

                $this->data['license'] = $this->session->userdata('license');
                $this->data['license_key'] = $this->session->userdata('license_key');
                $this->data['title'] = 'Software License';
                $this->data['message'] = $this->session->flashdata('message');
                $this->load->view('install/step_three', $this->data);
                break;
            case '4':

                if ($this->session->userdata('license') !== 'regular' && $this->session->userdata('license') !== 'extended') {
                    redirect(site_url('install/step/3'), 'refresh');
                }

                $this->load->database();
                $this->load->library('ion_auth');

                $this->form_validation->set_rules('firstname', $this->lang->line('create_user_validation_fname_label'), 'trim|required');
                $this->form_validation->set_rules('lastname', $this->lang->line('create_user_validation_lname_label'), 'trim');
                $this->form_validation->set_rules('email', $this->lang->line('create_user_validation_email_label'), 'trim|required|valid_email');
                $this->form_validation->set_rules('password', $this->lang->line('create_user_validation_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[passwordconfirm]');
                $this->form_validation->set_rules('passwordconfirm', $this->lang->line('create_user_validation_password_confirm_label'), 'required');

                if (isset($_POST) && !empty($_POST)) {
                    $account = array(
                        'firstname' => $this->input->post('firstname'),
                        'lastname' => $this->input->post('lastname'),
                        'email' => strtolower($this->input->post('email')),
                        'password' => $this->input->post('password'),
                        'passwordconfirm' => $this->input->post('passwordconfirm'),
                    );

                    $this->session->set_userdata('account', $account);
                }

                if ($this->form_validation->run() === TRUE && $this->ion_auth->register($account['email'], $account['password'], $account['email'], array('first_name' => $account['firstname'], 'last_name' => $account['lastname']), array('1'))) {
                    $this->session->set_userdata('account_setup', 1);
                    redirect(site_url('install/step/5'), 'refresh');
                }

                $this->data['title'] = 'User Account';
                $this->data['account'] = $this->session->userdata('account');
                $this->data['account_setup'] = $this->session->userdata('account_setup');
                $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
                $this->load->view('install/step_four', $this->data);
                break;
            case '5':

                if ($this->session->userdata('account_setup') !== 1) {
                    redirect(site_url('install/step/4'), 'refresh');
                }

                $this->change_routes();
                $this->load->database();
                $this->settings->set('license_key', $this->session->userdata('license_key'));

                $this->data['title'] = 'Installation Complete';
                $this->data['account'] = $this->session->userdata('account');
                $this->load->view('install/step_five', $this->data);
                break;
        }
    }

    public function migrate()
    {
        $this->load->database();
        $this->load->library('migration');
        $this->migration->latest();
        $this->session->set_userdata('database_setup', 1);
        redirect(site_url('install/step/3'), 'refresh');
    }

    public function check_requirements()
    {
        $checks = array();
        $checks['config'] = is_writable(FCPATH . '/application/config');
        $checks['cache'] = is_writable(FCPATH . '/application/cache');
        $checks['uploads'] = is_writable(FCPATH . '/assets/uploads');
        $checks['curl'] = function_exists('curl_version');
        $checks['file_get_contents'] = ini_get('allow_url_fopen');
        $checks['success'] = $checks['config'] && $checks['cache'] && $checks['uploads'] && $checks['curl'] && $checks['file_get_contents'];
        return $checks;
    }

    public function check_database_connection() {

        $dbauth = $this->session->userdata('dbauth');
        if (!$dbauth) { return 'failed'; }
        $connection = mysqli_connect($dbauth['hostname'], $dbauth['username'], $dbauth['password']);

        if (!$connection) {
            mysqli_close($connection);
            return 'failed';
        }

        $db_selected = mysqli_select_db($connection, $dbauth['database']);

        if (!$db_selected) {
            mysqli_close($connection);
            return "dbnotfound";
        }

        mysqli_close($connection);

        $config = file_get_contents(FCPATH . '/application/config/database.php');
        $config = str_replace('%DBHOST%',	$dbauth['hostname'],	$config);
        $config = str_replace('%DBUSER%',	$dbauth['username'],	$config);
        $config = str_replace('%DBPASS%',	$dbauth['password'],	$config);
        $config = str_replace('%DBNAME%',	$dbauth['database'],	$config);
        file_put_contents(FCPATH . '/application/config/database.php', $config);

        $this->check_already_installed();
        return 'success';
    }

    public function change_routes()
    {
        // Change base url
        $data = file(FCPATH . '/application/config/config.php');
		function replace_a_line($data) {
			if (stristr($data, '$config[\'base_url\']')) {
				return '$config[\'base_url\'] = \'' . site_url() . '\';' . "\n";
			}
			return $data;
		}
		$data = array_map('replace_a_line',$data);
        file_put_contents(FCPATH . '/application/config/config.php', implode('', $data));
        
        // Change default route
        $config = file_get_contents(FCPATH . '/application/config/routes.php');
        $config = str_replace('install', 'home', $config);
        file_put_contents(FCPATH . '/application/config/routes.php', $config);
    }

    public function check_already_installed()
    {
        $this->load->database();
        $this->load->library('migration');
        $migration = $this->db->get('migrations')->row();
        
        if ($migration->version == 5) {
            $this->session->set_userdata('database_setup', 1);
            $this->session->set_userdata('account_setup', 1);
            $license = $this->db->where('name', 'license_key')->get('settings')->row();
            $this->session->set_userdata('license_key', $license->value);
            $this->session->set_userdata('license', 'extended');
            redirect(site_url('install/step/5'), 'refresh');
        }
    }
}
Editor is loading...