-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.php
More file actions
362 lines (353 loc) · 14 KB
/
install.php
File metadata and controls
362 lines (353 loc) · 14 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
define('INSTALLER', true);
define('VERSION', 1);
error_reporting(0);
include_once(dirname(__FILE__).'/inc/functions.php');
include_once(dirname(__FILE__).'/inc/icdb.php');
$url_base = '//'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
$filename = basename(__FILE__);
if (($pos = strpos($url_base, $filename)) !== false) $url_base = substr($url_base, 0, $pos);
$url_base = rtrim($url_base, '/').'/';
$actions = array('start', 'connect-db', 'save-config', 'create-admin');
$wpdb = null;
$db_ready = false;
$admin_created = false;
$tables_created = false;
if (file_exists(dirname(__FILE__).'/inc/config.php')) {
include_once(dirname(__FILE__).'/inc/config.php');
try {
$wpdb = new ICDB(DB_HOST, DB_HOST_PORT, DB_NAME, DB_USER, DB_PASSWORD, DB_TABLE_PREFIX);
$db_ready = true;
sync_database();
$tables_created = true;
$tmp = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."users WHERE role = 'admin' AND deleted != '1' AND status = 'active' LIMIT 0, 1", ARRAY_A);
if (!empty($tmp)) $admin_created = true;
} catch (Exception $e) {
if (!$tables_created) {
echo fatal_error_html('Database connection error. Check MySQL credentials and database user privileges. Remove file <strong>/inc/config.php</strong> to start installation from scratch.');
exit;
}
}
}
if ($db_ready && $tables_created && $admin_created) {
if (array_key_exists('action', $_POST)) {
$step = 5;
} else {
header('Location: '.$url_base);
exit;
}
} else if (!array_key_exists('action', $_POST) || !in_array($_POST['action'], $actions)) {
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="robots" content="noindex,nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-style-type" content="text/css" />
<title><?php echo esc_html__('Install', 'fb'); ?></title>
<link href="//fonts.googleapis.com/css?family=Open+Sans:100,100italic,300,300italic,400,400italic,600,600italic,700,700italic,800,800italic&subset=cyrillic,cyrillic-ext,greek,greek-ext,latin,latin-ext,vietnamese" rel="stylesheet" type="text/css">
<link href="<?php echo $url_base; ?>css/fontawesome.css" rel="stylesheet" type="text/css" />
<link href="<?php echo $url_base; ?>css/login.css" rel="stylesheet" type="text/css" />
<script src="<?php echo $options["url"]; ?>js/jquery.min.js"></script>
<script src="<?php echo $url_base; ?>js/install.js"></script>
<script>var ajax_url = "<?php echo $url_base; ?>install.php";</script>
</head>
<body>
<div class="wrapper">
<div class="wrapper-left-column" style="background-image: url('images/default-pattern.png')"></div>
<div class="wrapper-right-column">
<div class="form-wrapper" id="installation-form">
<div class="form">
<div class="form-row">
<h1><?php echo esc_html__('Script Setup', 'fb'); ?></h1>
</div>
<div class="form-row">
<p><?php echo esc_html__("Hi, I am a Wizard. I gonna help you to setup this script. You just need perform several simple steps. Let's start?", 'fb'); ?></p>
</div>
<div class="form-row">
<input type="hidden" name="action" value="start">
<a class="button" href="#" onclick="return submit_form(this);">
<span><?php echo esc_html__('Continue', 'fb'); ?></span>
<i class="fas fa-angle-right"></i>
</a>
</div>
</div>
</div>
</div>
</div>
<div id="global-message"></div>
</body>
</html>
<?php
exit;
} else {
switch ($_POST['action']) {
case 'start':
if ($admin_created) $step = 5;
else if ($tables_created) $step = 4;
else $step = 2;
break;
case 'connect-db':
if ($admin_created) $step = 5;
else if ($tables_created) $step = 4;
else {
$host = trim(stripslashes($_POST['hostname']));
$port = trim(stripslashes($_POST['port']));
$username = trim(stripslashes($_POST['username']));
$password = trim(stripslashes($_POST['password']));
$database = trim(stripslashes($_POST['database']));
$prefix = trim(stripslashes($_POST['prefix']));
$errors = array();
if (empty($host) || !is_hostname($host)) $errors[] = esc_html__('Inavlid MySQL Hostname.', 'fb');
if (!empty($port) && $port != preg_replace('/[^0-9]/', '', $port)) $errors[] = esc_html__('Port value must be a number.', 'fb');
if (empty($username)) $errors[] = esc_html__('Username can not be empty.', 'fb');
else if (strpos($username, "'") !== false) $errors[] = esc_html__('Username can not contain single quote symbol.', 'fb');
if (empty($database)) $errors[] = esc_html__('Invalid Database name.', 'fb');
else if (strpos($database, "'") !== false) $errors[] = esc_html__('Database can not contain single quote symbol.', 'fb');
if (strpos($password, "'") !== false) $errors[] = esc_html__('Password can not contain single quote symbol.', 'fb');
if (!preg_match('/^[a-zA-Z]+[a-zA-Z_]+$/', $prefix)) $errors[] = esc_html__('Table Prefix must contain letters and/or underscore symbol.', 'fb');
if (!empty($errors)) {
$return_object = array();
$return_object['status'] = 'ERROR';
$return_object['message'] = implode('<br />', $errors);
echo json_encode($return_object);
exit;
}
try {
$wpdb = new ICDB($host, $port, $database, $username, $password, $prefix);
} catch (Exception $e) {
$return_object = array();
$return_object['status'] = 'ERROR';
$return_object['message'] = esc_html__('Can not connect to MySQL database using provided credentials.', 'fb');
echo json_encode($return_object);
exit;
}
try {
sync_database();
} catch (Exception $e) {
$return_object = array();
$return_object['status'] = 'ERROR';
$return_object['message'] = sprintf(esc_html__('Can not create database tables. Make sure that user %s has sufficient privileges to manipulate database.', 'fb'), '<strong>'.esc_html($username).'</strong>');
echo json_encode($return_object);
exit;
}
$config_content = "<?php
define('DB_HOST', '".$host."');
define('DB_HOST_PORT', '".$port."');
define('DB_USER', '".$username."');
define('DB_PASSWORD', '".$password."');
define('DB_NAME', '".$database."');
define('DB_TABLE_PREFIX', '".$prefix."');
?>";
$result = file_put_contents(dirname(__FILE__).DIRECTORY_SEPARATOR.'inc'.DIRECTORY_SEPARATOR.'config.php', $config_content);
if ($result !== false) {
$login = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."options WHERE options_key = 'login'");
$password = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."options WHERE options_key = 'password'");
if (!empty($login) && !empty($password)) $step = 5;
else $step = 4;
} else $step = 3;
}
break;
case 'save-config':
if ($admin_created) $step = 5;
else if ($tables_created) $step = 4;
else {
$return_object = array();
$return_object['status'] = 'ERROR';
$return_object['message'] = esc_html__('Hm. Seems config.php still does not contain correct database credentials. Please update it properly.', 'fb');
echo json_encode($return_object);
exit;
}
break;
case 'create-admin':
if ($admin_created) $step = 5;
else if (!$tables_created || !$db_ready) {
$return_object = array();
$return_object['status'] = 'ERROR';
$return_object['message'] = esc_html__('Something went wrong. We still can not connect to database. Please try setup procedure again. Just refresh the page.', 'fb');
echo json_encode($return_object);
exit;
} else {
$email = strtolower(trim(stripslashes($_POST['email'])));
$password = trim(stripslashes($_POST['password']));
$errors = array();
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,19})$/i", $email) || empty($email)) $errors[] = esc_html__('This is not a valid email address.', 'fb');
else if (strlen($email) > 127) $errors[] = esc_html__('Email address is too long.', 'fb');
if (strlen($password) < 6) $errors[] = esc_html__('Password length must be at least 6 characters.', 'fb');
if (!empty($errors)) {
$return_object = array();
$return_object['status'] = 'ERROR';
$return_object['message'] = implode('<br />', $errors);
echo json_encode($return_object);
exit;
}
try {
$login = create_login($email);
$wpdb->query("INSERT INTO ".$wpdb->prefix."users (
uuid,
login,
password,
email,
name,
role,
status,
timezone,
options,
email_confirmed,
email_confirmation_uid,
password_reset_uid,
deleted,
created
) VALUES (
'".esc_sql(uuid_v4())."',
'".esc_sql($login)."',
'".esc_sql(password_hash($password, PASSWORD_DEFAULT))."',
'".esc_sql($email)."',
'".esc_sql(esc_html__('Administrator', 'fb'))."',
'admin',
'active',
'UTC',
'',
'1',
'".esc_sql(uuid_v4())."',
'".esc_sql(uuid_v4())."',
'0',
'".time()."'
)");
save_option('url', $url_base);
$step = 5;
} catch (Exception $e) {
$return_object = array();
$return_object['status'] = 'ERROR';
$return_object['message'] = sprintf(esc_html__('Can not insert record into table. Make sure that user %s has sufficient privileges to manipulate database.', 'fb'), '<strong>'.esc_html(UAP_DB_USERNAME).'</strong>');
echo json_encode($return_object);
exit;
}
}
break;
default:
echo esc_html__('We do not have to be here. Never.', 'fb');
exit;
}
}
$return_object = array();
$return_object['status'] = 'OK';
$return_object['html'] = esc_html__('We do not have to see this message. Never.', 'fb');
if ($step == 2) {
$return_object['html'] = '
<div class="form">
<div class="form-row">
<h1>'.esc_html__('Setup Database', 'fb').'</h1>
</div>
<div class="form-row">
<div class="input-box">
<input type="text" name="hostname" value="localhost" placeholder="localhost" />
<label>'.esc_html__('Enter MySQL server hostname. Usually it is a localhost, but we recommend to clarify this parameter from your hosting provider.', 'fb').'</label>
</div>
</div>
<div class="form-row">
<div class="input-box">
<input type="text" name="port" value="" placeholder="3306" />
<label>'.esc_html__('Enter MySQL server port. Leave it empty if you do not know the port or it is standard 3306.', 'fb').'</label>
</div>
</div>
<div class="form-row">
<div class="input-box">
<input type="text" name="username" value="" placeholder="Username" />
<label>'.esc_html__('Enter MySQL server username. Find it in your hosting control panel.', 'fb').'</label>
</div>
</div>
<div class="form-row">
<div class="input-box">
<input type="text" name="password" value="" placeholder="Password" />
<label>'.esc_html__('Enter password for MySQL server user. Find it in your hosting control panel.', 'fb').'</label>
</div>
</div>
<div class="form-row">
<div class="input-box">
<input type="text" name="database" value="" placeholder="Database" />
<label>'.esc_html__('Enter MySQL database name. Find it in your hosting control panel.', 'fb').'</label>
</div>
</div>
<div class="form-row">
<div class="input-box">
<input type="text" name="prefix" value="fb_" placeholder="Table Prefix" />
<label>'.esc_html__('Enter prefix for MySQL tables. If you plan to have several installations of this script, use unique prefix for each installation.', 'fb').'</label>
</div>
</div>
<div class="form-row">
<input type="hidden" name="action" value="connect-db">
<a class="button" href="#" onclick="return submit_form(this);">
<span>'.esc_html__('Continue', 'fb').'</span>
<i class="fas fa-angle-right"></i>
</a>
</div>
</div>';
} else if ($step == 3) {
$return_object['html'] = '
<div class="form">
<div class="form-row">
<h1>'.esc_html__('Save Config File', 'fb').'</h1>
</div>
<div class="form-row">
<p>'.sprintf(esc_html__('Unfortunately, we could not save database credentials into config.php (due to file permissions). You have to do it manually. Please edit file %s and overwrite its content with the following code.', 'fb'), '<br /><strong>'.dirname(__FILE__).DIRECTORY_SEPARATOR.'inc'.DIRECTORY_SEPARATOR.'config.php'.'</strong><br />').'</p>
<div class="input-box">
<textarea readonly="readonly" onclick="this.focus();this.select();">'.esc_html($config_content).'</textarea>
</div>
</div>
<div class="form-row">
<input type="hidden" name="action" value="save-config">
<a class="button" href="#" onclick="return submit_form(this);">
<span>'.esc_html__('Continue', 'fb').'</span>
<i class="fas fa-angle-right"></i>
</a>
</div>
</div>';
} else if ($step == 4) {
$return_object['html'] = '
<div class="form">
<div class="form-row">
<h1>'.esc_html__('Create Admin Account', 'fb').'</h1>
</div>
<div class="form-row">
<div class="input-box">
<input type="text" name="email" value="" placeholder="admin@website.com" />
<label>'.esc_html__('Email address is your login to enter account.', 'fb').'</label>
</div>
</div>
<div class="form-row">
<div class="input-box">
<input type="text" name="password" value="" placeholder="Password" />
<label>'.esc_html__('Use this password to enter your account.', 'fb').'</label>
</div>
</div>
<div class="form-row">
<input type="hidden" name="action" value="create-admin">
<a class="button" href="#" onclick="return submit_form(this);">
<span>'.esc_html__('Continue', 'fb').'</span>
<i class="fas fa-angle-right"></i>
</a>
</div>
</div>';
} else if ($step == 5) {
$return_object['html'] = '
<div class="form">
<div class="form-row">
<h1>'.esc_html__('Finished', 'fb').'</h1>
</div>
<div class="form-row">
<p>'.esc_html__('Congratulation! Installation successfully completed. Now you can enter your account using created login/password and work there. Good luck!', 'fb').'</p>
</div>
<div class="form-row">
<input type="hidden" name="action" value="save-config">
<a class="button" href="'.$url_base.'">
<span>'.esc_html__('Finish', 'fb').'</span>
<i class="fas fa-angle-right"></i>
</a>
</div>
</div>';
}
echo json_encode($return_object);
exit;
?>