1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
/*
* @author : Creativeitem
* date : 07 october, 2018
* Academy
* http://codecanyon.net/user/Creativeitem
* http://ictlife.vn
*/
ini_set('max_execution_time', 0);
ini_set('memory_limit','2048M');
class Install extends CI_Controller {
public function index() {
if ($this->router->default_controller == 'install') {
redirect(site_url('install/step0'), 'refresh');
}
redirect(site_url('login'), 'refresh');
}
function step0() {
if ($this->router->default_controller != 'install') {
redirect(site_url('login'), 'refresh');
}
$page_data['page_name'] = 'step0';
$this->load->view('install/index', $page_data);
}
function step1() {
if ($this->router->default_controller != 'install') {
redirect(site_url('login'), 'refresh');
}
$page_data['page_name'] = 'step1';
$this->load->view('install/index', $page_data);
}
function step2($param1 = '', $param2 = '') {
if ($this->router->default_controller != 'install') {
redirect(site_url('login'), 'refresh');
}
if ($param1 == 'error') {
$page_data['error'] = 'Purchase Code Verification Failed';
}
$page_data['page_name'] = 'step2';
$this->load->view('install/index', $page_data);
}
function validate_purchase_code() {
$purchase_code = $this->input->post('purchase_code');
$validation_response = TRUE;
if ($validation_response == true) {
// keeping the purchase code in users session
session_start();
$_SESSION['purchase_code'] = $purchase_code;
$_SESSION['purchase_code_verified'] = 1;
//move to step 3
redirect(site_url('install/step3'), 'refresh');
} else {
//remain on step 2 and show error
session_start();
$_SESSION['purchase_code_verified'] = 0;
redirect(site_url('install/step2/error'), 'refresh');
}
}
function step3($param1 = '', $param2 = '') {
if ($this->router->default_controller != 'install') {
redirect(site_url('login'), 'refresh');
}
$this->check_purchase_code_verification();
if ($param1 == 'error_con_fail') {
$page_data['error_con_fail'] = 'Error establishing a database conenction using your provided information. Please
recheck hostname, username, password and try again with correct information';
}
if ($param1 == 'error_nodb') {
$page_data['error_con_fail'] = 'The database you are trying to use for the application does not exist. Please create
the database first';
}
if ($param1 == 'configure_database') {
$hostname = $this->input->post('hostname');
$username = $this->input->post('username');
$password = $this->input->post('password');
$dbname = $this->input->post('dbname');
// check db connection using the above credentials
$db_connection = $this->check_database_connection($hostname, $username, $password, $dbname);
if ($db_connection == 'failed') {
redirect(site_url('install/step3/error_con_fail'), 'refresh');
} else if ($db_connection == 'db_not_exist') {
redirect(site_url('install/step3/error_nodb'), 'refresh');
} else {
// proceed to step 4
session_start();
$_SESSION['hostname'] = $hostname;
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
$_SESSION['dbname'] = $dbname;
redirect(site_url('install/step4'), 'refresh');
}
}
$page_data['page_name'] = 'step3';
$this->load->view('install/index', $page_data);
}
function check_purchase_code_verification() {
if ($_SERVER['SERVER_NAME'] == 'localhost' || $_SERVER['SERVER_NAME'] == '127.0.0.1') {
//return 'running_locally';
} else {
session_start();
if (!isset($_SESSION['purchase_code_verified']))
redirect(site_url('install/step2'), 'refresh');
else if ($_SESSION['purchase_code_verified'] == 0)
redirect(site_url('install/step2'), 'refresh');
}
}
function check_database_connection($hostname, $username, $password, $dbname) {
$link = mysqli_connect($hostname, $username, $password, $dbname);
if (!$link) {
mysqli_close($link);
return 'failed';
}
$db_selected = mysqli_select_db($link, $dbname);
if (!$db_selected) {
mysqli_close($link);
return "db_not_exist";
}
mysqli_close($link);
return 'success';
}
function step4($param1 = '') {
if ($this->router->default_controller != 'install') {
redirect(site_url('login'), 'refresh');
}
if ($param1 == 'confirm_install') {
// write database.php
$this->configure_database();
// run sql
$this->run_blank_sql();
// redirect to admin creation page
redirect(site_url('install/finalizing_setup'), 'refresh');
}
$page_data['page_name'] = 'step4';
$this->load->view('install/index', $page_data);
}
function configure_database() {
// write database.php
$data_db = file_get_contents('./application/config/database.php');
session_start();
$data_db = str_replace('db_name', $_SESSION['dbname'], $data_db);
$data_db = str_replace('db_user', $_SESSION['username'], $data_db);
$data_db = str_replace('db_pass', $_SESSION['password'], $data_db);
$data_db = str_replace('db_host', $_SESSION['hostname'], $data_db);
file_put_contents('./application/config/database.php', $data_db);
}
function run_blank_sql() {
$this->load->database();
// Set line to collect lines that wrap
$templine = '';
// Read in entire file
$lines = file('./uploads/install.sql');
// Loop through each line
foreach ($lines as $line) {
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
continue;
// Add this line to the current templine we are creating
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query so can process this templine
if (substr(trim($line), -1, 1) == ';') {
// Perform the query
$this->db->query($templine);
// Reset temp variable to empty
$templine = '';
}
}
}
function finalizing_setup($param1 = '', $param2 = '') {
if ($this->router->default_controller != 'install') {
redirect(site_url('login'), 'refresh');
}
if ($param1 == 'setup_admin') {
$admin_data['first_name'] = html_escape($this->input->post('first_name'));
$admin_data['last_name'] = html_escape($this->input->post('last_name'));
$admin_data['email'] = html_escape($this->input->post('email'));
$admin_data['password'] = sha1($this->input->post('password'));
$social_links = array(
'facebook' => "",
'twitter' => "",
'linkedin' => ""
);
$admin_data['social_links'] = json_encode($social_links);
$admin_data['role_id'] = 1;
$admin_data['status'] = 1;
$this->load->database();
$this->db->insert('users', $admin_data);
$data['value'] = $this->input->post('system_name');
$this->db->where('key', 'system_name');
$this->db->update('settings', $data);
redirect(site_url('install/success'), 'refresh');
}
$page_data['page_name'] = 'finalizing_setup';
$this->load->view('install/index', $page_data);
}
function success($param1 = '') {
if ($this->router->default_controller != 'install') {
redirect(site_url('login'), 'refresh');
}
if ($param1 == 'login') {
$this->configure_routes();
redirect(site_url('login'), 'refresh');
}
$this->load->database();
$admin_email = $this->db->get_where('users', array('id' => 1))->row()->email;
session_start();
if (isset($_SESSION['purchase_code'])) {
$data['value'] = $_SESSION['purchase_code'];
$this->db->where('key', 'purchase_code');
$this->db->update('settings', $data);
}
session_destroy();
$page_data['admin_email'] = $admin_email;
$page_data['page_name'] = 'success';
$this->load->view('install/index', $page_data);
}
function configure_routes() {
// write routes.php
$data_routes = file_get_contents('./application/config/routes.php');
$data_routes = str_replace('install', 'home', $data_routes);
file_put_contents('./application/config/routes.php', $data_routes);
}
}