Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Step_3 extends Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->helper('file');
$this->load->helper('form');
$this->load->helper('array');
$this->load->library('form_validation');
$this->lang->load('english', 'english');
}
public function index() {
if(($this->input->server('REQUEST_METHOD') == 'POST') && $this->validate()) {
$data['db_hostname'] = $this->input->post('db_hostname');
$data['db_username'] = $this->input->post('db_username');
$data['db_password'] = $this->input->post('db_password');
$data['db_database'] = $this->input->post('db_database');
$data['db_driver'] = $this->input->post('db_driver');
$data['db_prefix'] = $this->input->post('db_prefix');
$data = $this->load->view('template/configuration/database', $data, true);
write_file(dirname(FCPATH) . '/admin/application/config/database.php', $data, 'r+');
write_file(dirname(FCPATH) . '/catalog/config/database.php', $data, 'r+');
write_file(FCPATH . '/application/config/database.php', $data, 'r+');
//$this->load->library('migration');
//$this->migration->current();
redirect('step_4');
}
$data['heading_step_3'] = $this->lang->line('heading_step_3');
$data['heading_step_3_small'] = $this->lang->line('heading_step_3_small');
$data['text_license'] = $this->lang->line('text_license');
$data['text_installation'] = $this->lang->line('text_installation');
$data['text_configuration'] = $this->lang->line('text_configuration');
$data['text_finished'] = $this->lang->line('text_finished');
$data['text_db_connection'] = $this->lang->line('text_db_connection');
$data['text_db_administration'] = $this->lang->line('text_db_administration');
$data['text_mysqli'] = $this->lang->line('text_mysqli');
$data['entry_db_driver'] = $this->lang->line('entry_db_driver');
$data['entry_db_hostname'] = $this->lang->line('entry_db_hostname');
$data['entry_db_username'] = $this->lang->line('entry_db_username');
$data['entry_db_password'] = $this->lang->line('entry_db_password');
$data['entry_db_database'] = $this->lang->line('entry_db_database');
$data['entry_db_prefix'] = $this->lang->line('entry_db_prefix');
$data['entry_username'] = $this->lang->line('entry_username');
$data['entry_password'] = $this->lang->line('entry_password');
$data['entry_email'] = $this->lang->line('entry_email');
$data['button_continue'] = $this->lang->line('button_continue');
$data['button_back'] = $this->lang->line('button_back');
if (!empty($this->error['db_hostname'])) {
$data['error_db_hostname'] = $this->error['db_hostname'];
} else {
$data['error_db_hostname'] = '';
}
if (!empty($this->error['db_username'])) {
$data['error_db_username'] = $this->error['db_username'];
} else {
$data['error_db_username'] = '';
}
if (!empty($this->error['db_database'])) {
$data['error_db_database'] = $this->error['db_database'];
} else {
$data['error_db_database'] = '';
}
if (!empty($this->error['db_prefix'])) {
$data['error_db_prefix'] = $this->error['db_prefix'];
} else {
$data['error_db_prefix'] = '';
}
$data['action'] = site_url("step_3");
if (!empty($this->input->post('db_driver'))) {
$data['db_driver'] = $this->input->post('db_driver');
} else {
$data['db_driver'] = '';
}
if (!empty($this->input->post('db_hostname'))) {
$data['db_hostname'] = $this->input->post('db_hostname');
} else {
$data['db_hostname'] = 'localhost';
}
if (!empty($this->input->post('db_username'))) {
$data['db_username'] = $this->input->post('db_username');
} else {
$data['db_username'] = '';
}
if (!empty($this->input->post('db_password'))) {
$data['db_password'] = $this->input->post('db_password');
} else {
$data['db_password'] = '';
}
if (!empty($this->input->post('db_database'))) {
$data['db_database'] = $this->input->post('db_database');
} else {
$data['db_database'] = '';
}
if (!empty($this->input->post('db_prefix'))) {
$data['db_prefix'] = $this->input->post('db_prefix');
} else {
$data['db_prefix'] = '';
}
$data['back'] = site_url('step_2');
$this->load->view('template/step_3', $data);
}
private function validate() {
if (!$this->input->post('db_hostname')) {
$this->error['db_hostname'] = 'Hostname required!';
}
if (!$this->input->post('db_username')) {
$this->error['db_username'] = 'Username required!';
}
if (!$this->input->post('db_database')) {
$this->error['db_database'] = 'Database Name required!';
}
if ($this->input->post('db_prefix') && preg_match('/[^a-z0-9_]/', $this->input->post('db_prefix'))) {
$this->error['db_prefix'] = 'DB Prefix can only contain lowercase characters in the a-z range, 0-9 and "_"!';
}
if ($this->input->post('db_driver') == 'mysqli') {
$mysql = @new mysqli($this->input->post('db_hostname'), $this->input->post('db_username'), $this->input->post('db_password'), $this->input->post('db_database'));
if ($mysql->connect_error) {
$this->error['warning'] = 'Error: Could not connect to the database please make sure the database server, username and password is correct!';
} else {
$mysql->close();
}
}
}
}
Code:
if (array_key_exists('db_hostname', $this->error)) {
$data['error_db_hostname'] = $this->error['db_hostname'];
} else {
$data['error_db_hostname'] = '';
}
if (array_key_exists('db_username', $this->error)) {
$data['error_db_username'] = $this->error['db_username'];
} else {
$data['error_db_username'] = '';
}
if ("" == trim($this->input->post('db_hostname'))) {
$data['db_hostname'] = $this->input->post('db_hostname');
} else {
$data['db_hostname'] = 'localhost';
}
if ("" == trim($this->input->post('db_username'))) {
$data['db_username'] = $this->input->post('db_username');
} else {
$data['db_username'] = '';
}