-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.php
398 lines (374 loc) · 14.6 KB
/
setup.php
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
declare(strict_types=1);
$finish = false;
if (isset($_POST) && !empty($_POST)){
if (!isset($_POST['finish']) && isset($_POST['user'])){
/*testing connection to DB*/
$my = @mysqli_connect($_POST['host'],$_POST['user'],$_POST['pass'],$_POST['db']);
header('Content-Type: application/json');
if(!$my){
echo json_encode(array('status'=>'error','msg'=>utf8_encode(mysqli_connect_error())));
exit;
}
mysqli_close($my);
echo json_encode(array('status'=>'success'));
exit;
}elseif(isset($_POST['finish'])){
$config = file_get_contents('config-example.php');
$config = str_replace("define('DB_HOSTNAME','localhost');", "define('DB_HOSTNAME','".addslashes($_POST['host'])."');", $config);
$config = str_replace("define('DB_USERNAME','root');", "define('DB_USERNAME','".addslashes($_POST['user'])."');", $config);
$config = str_replace("define('DB_PASSWORD','');", "define('DB_PASSWORD','".addslashes($_POST['pass'])."');", $config);
$config = str_replace("define('DB_DATABASE','trackr');", "define('DB_DATABASE','".addslashes($_POST['db'])."');", $config);
$_POST['prefix'] = preg_replace('/(?![a-z_])/m', '', strtolower($_POST['prefix']));
$config = str_replace("define('DB_PREFIX','');", "define('DB_PREFIX','".addslashes($_POST['prefix'])."');", $config);
mysqli_report(MYSQLI_REPORT_ALL ^ MYSQLI_REPORT_STRICT);
$my = mysqli_connect($_POST['host'],$_POST['user'],$_POST['pass'],$_POST['db']);
$sql = file_get_contents('database.sql');
$sql = str_replace("CREATE TABLE IF NOT EXISTS `", "CREATE TABLE IF NOT EXISTS `".$_POST['prefix'], $sql);
$sql = str_replace("REFERENCES `", "REFERENCES `".$_POST['prefix'], $sql);
$sql = str_replace("INDEX `", "INDEX `".$_POST['prefix'], $sql);
$sql = str_replace("CONSTRAINT `", "CONSTRAINT `".$_POST['prefix'], $sql);
$sql = str_replace("TRUNCATE `", "TRUNCATE `".$_POST['prefix'], $sql);
$sql = str_replace("DROP TABLE IF EXISTS `", "DROP TABLE IF EXISTS `".$_POST['prefix'], $sql);
$sql = explode(';',$sql);
foreach($sql as $q){
if (empty(trim($q))) continue;
mysqli_query($my,$q);
}
mysqli_close($my);
$my = mysqli_connect($_POST['host'],$_POST['user'],$_POST['pass'],$_POST['db']);
$stmt = mysqli_prepare($my, "INSERT INTO `".$_POST['prefix']."admin`(`username`,`password`) VALUES(?,?)");
if ($stmt) {
$_POST['adm_pwd'] = password_hash($_POST['adm_pwd'],PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "ss", $_POST['adm_user'],$_POST['adm_pwd']);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}
mysqli_close($my);
$actions = array();
foreach($_POST['action'] as $key=>$action){
if (!isset($actions[$action])) $actions[$action] = array();
$selector = $_POST['selector'][$key];
$value = $_POST['value'][$key];
$actions[$action][$selector] = $value;
}
$config = preg_replace('/\$actions = array\(\X*?;/m', '$actions = '.var_export($actions,TRUE).";", $config);
file_put_contents('config.php', $config);
$finish = true;
}
}elseif (file_exists('config.php')){
echo 'Config file already exists. Exiting installation script.';
exit;
}
?><!DOCTYPE html>
<html>
<head>
<style type="text/css">
.footer{
margin-top:30px;
}
code{
word-break: break-word;
}
.footer > div{
background:#F4F4F4;
padding:10px;
}
body h4{
margin-bottom: 30px;
margin-top:30px;
}
html .bright{
border-right: 1px solid silver;
background-color: #F4F4F4;
display: block;
position: absolute;
height: 100%;
}
html .rb{
float: right;
padding-bottom: 57px;
}
.tline{
border:1px solid silver;
}
.tline .row{
position: relative;
}
body h1{
text-align: center;
margin-bottom: 30px;
display: block;
}
li.selected{
color:#09A9C6;
font-weight: bold;
}
li{
padding-top:10px;
padding-bottom: 10px;
}
span.ok{
color:#31BA27;
}
span.notok{
color:#D81F1F;
font-weight: bold;
}
.bar{
position: absolute;
bottom:0px;
right: 0px;
padding-top:10px;
border-top:1px solid silver;
width: 100%;
padding-right: 10px;
text-align: right;
padding-bottom: 10px;
}
.page{display: none;}
.form-control{
margin-bottom: 12px;
}
/* xs sm */
@media (max-width: 991px) {
html .bright{border:none;position: relative;height: auto;}
.bright li{display:inline-block;width:auto;border-right: 1px solid silver;padding-left:10px;padding-right: 20px;}
.bright ul{padding:0px;}
.tline .row{}
html .rb{
padding-bottom: 100px;
float: none;
}
}
</style>
<title>CSSTrackr Setup</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<h1><img src="csstrackr-logo.svg" style="height: 53px;vertical-align: middle;"> CSSTrackr Setup Script</h1>
<?php if(!$finish){?>
<div class="container tline">
<div class="row">
<div class="col-md-3 bright">
<ul>
<li class="selected" data-for="#first">1. Requirements</li>
<li data-for="#second">2. Database connection</li>
<li data-for="#third">3. User actions</li>
</ul>
</div>
<div class="col-md-9 rb">
<form method="POST" id="form">
<input type="hidden" name="finish">
<div id="first" class="page" style="display: block;">
<h4>Welcome to CSSTrackr Setup Script. Below you will find a pre-scan of our needed functions.</h4>
<?php
$errors = array();
if (!file_exists('vendor/lucaasleaal/mysqlucas/mysqlucas.php')){
$errors['composer'] = 'You need to install my dependencies with composer. <a target="_blank" href="https://github.com/lucaasleaal/csstrackr">Check the repo</a> for instructions.';
}
if (!file_exists('config-example.php')){
$errors['configexample'] = 'The config-example.php file is missing!';
}
if (!defined('PHP_VERSION')){
if (function_exists('phpversion')){
define('PHP_VERSION',phpversion());
}else{
define('PHP_VERSION','3.0.0');
}
}
if (version_compare(PHP_VERSION, '7.0.0', '<')) {
$errors['php_version'] = 'Your PHP version is '.PHP_VERSION.' but it should be at least 7.0.0';
}
if (!function_exists('mysqli_query')){
$errors['mysqli'] = 'You don\' have mysqli extension enabled. You need to enable <strong>mysqli</strong>.';
}
if (!fopen('config.php','w') || !is_writable('config.php')){
$errors['write'] = 'This script has no permission to edit the config.php file. You need to grant it, please.';
}
if (file_exists('config.php')){
unlink('config.php');
}
?>
<ol>
<li>Config example ... <?php echo isset($errors['configexample'])?'<span class="notok">'.$errors['configexample'].'</span>':'<span class="ok">OK</span>'?></li>
<li>Composer setup ... <?php echo isset($errors['composer'])?'<span class="notok">'.$errors['composer'].'</span>':'<span class="ok">OK</span>'?></li>
<li>PHP 7.0+ ... <?php echo isset($errors['php_version'])?'<span class="notok">'.$errors['php_version'].'</span>':'<span class="ok">OK</span>'?></li>
<li>mysqli extension ... <?php echo isset($errors['mysqli'])?'<span class="notok">'.$errors['mysqli'].'</span>':'<span class="ok">OK</span>'?></li>
<li>Write permissions ... <?php echo isset($errors['write'])?'<span class="notok">'.$errors['write'].'</span>':'<span class="ok">OK</span>'?></li>
</ol>
<div class="bar">
<?php if(empty($errors)){?><button class="btn btn-success" onclick="step('#second')" type="button">Next</button><?php }?>
</div>
</div>
<div id="second" class="page">
<h4>Now that your server is ready, show me your database.</h4>
<fieldset class="form-horizontal">
<!-- Text input-->
<div class="form-group">
<label class="col-md-2 control-label" for="host">Host</label>
<div class="col-md-2">
<input id="host" name="host" value="localhost" type="text" placeholder="ex: localhost" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-2 control-label" for="user">User</label>
<div class="col-md-4">
<input id="user" value="root" name="user" type="text" placeholder="user from database" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-2 control-label" for="pass">Password</label>
<div class="col-md-4">
<input id="pass" name="pass" type="text" placeholder="*it will be visible as you type*" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-2 control-label" for="db">Database</label>
<div class="col-md-4">
<input id="db" name="db" value="csstrackr" type="text" placeholder="database name" class="form-control input-md">
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="db">Database prefix</label>
<div class="col-md-4">
<input id="prefix" name="prefix" value="trk_" type="text" placeholder="tables prefix" class="form-control input-md">
</div>
</div>
</fieldset>
<div class="clearfix"></div>
<div class="bar">
<?php if(empty($errors)){?><button class="btn btn-default" onclick="step('#first');" type="button" id="btn-test">Back</button><?php }?>
<?php if(empty($errors)){?><button class="btn btn-success" onclick="checkdb();" type="button" id="btn-test">Test database</button><?php }?>
</div>
</div>
<div id="third" class="page">
<h4>Everything working! Now tell me which elements you want to track.</h4>
<fieldset class="form-horizontal">
<div class="form-group">
<div class="col-sm-3">
<select name="action[]" class="form-control">
<option value='click'>Click</option>
<option value='hover'>Hover</option>
<option value='hoverhold'>Hover-hold</option>
<option value='check'>Check</option>
</select>
</div>
<div class="col-sm-3">
<input type="text" required name="selector[]" placeholder="selector" class="form-control"/>
</div>
<div class="col-xs-9 col-sm-3">
<input type="text" required name="value[]" placeholder="description" class="form-control"/>
</div>
<div class="col-xs-3 col-sm-3"><button type="button" onclick="if($('.remove').length>1){$(this).closest('.form-group').remove();}" class="remove btn btn-danger">x</button></div>
</div>
</fieldset>
<button type="button" id="more" onclick="addmore()" class="btn btn-success">+</button>
<br>
<br>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<input type="text" required name="adm_user" placeholder="admin username" class="form-control" maxlength="30" pattern="^[a-z]*$" title="Only lowercase letters" />
</div>
<div class="col-sm-12">
<input type="text" required name="adm_pwd" placeholder="*visible* admin password" class="form-control" maxlength="30" />
</div>
</div>
</div>
<div class="bar">
<?php if(empty($errors)){?><button class="btn btn-default" onclick="step('#second');" type="button" id="btn-test">Back</button><?php }?>
<?php if(empty($errors)){?><button class="btn btn-success" type="submit" id="btn-submit">Save</button><?php }?>
</div>
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript">
var form = document.getElementById("form");
form.noValidate = true; // turn off default validation
form.onsubmit = function(e) {
e.preventDefault(); // preventing default behaviour
this.reportValidity(); // run native validation manually
// runs default behaviour (submitting) in case of validation success
if (this.checkValidity()) return form.submit();
$('.bar:visible .btn-success[type="button"]').click();
}
function addmore(){
var html = "<div class='form-group'>"+$('#third fieldset .form-group:first-child').html()+"</div>";
$('#third fieldset').append(html);
}
function step($to){
$('.page').hide();
$($to).show();
$('[data-for]').removeClass('selected');
$('[data-for="'+$to+'"]').addClass('selected');
}
function blockinput(){
$('body').css('opacity','0.5').css('pointer-events','none');
}
function unblockinput(){
$('body').css('opacity','').css('pointer-events','');
}
function checkdb(){
var btn = $('#btn-test');
var text = btn.html();
btn.text('loading...');
blockinput();
$.ajax({
url: 'setup.php',
type: 'POST',
data: $('#host,#user,#pass,#db').serialize(),
})
.done(function(data) {
if (data.status=='error'){
alert(data.msg);
return;
}else{
step('#third');
}
})
.fail(function(data) {
alert('It was not possible to even test. Check your internet connection.');
alert(data);
})
.always(function(){
unblockinput();
btn.html(text);
});
}
</script>
<?php }else{?>
<div class="container">
<div class="col-xs-12">
<h4>Done!</h4>
<p>Now you just need to put the css file in all pages you want to track users.</p>
<p>Put the code below before the <?php echo htmlentities("</head>")?> tag:</p>
<?php
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$css_link = str_replace('setup.php','csstrackr.css.php',$actual_link);
$admin_link = str_replace('setup.php','admin/',$actual_link);
?>
<code>
<?php echo htmlentities('<link rel="stylesheet" href="'.$css_link.'" type="text/css" media="all">')?>
</code>
<br>
<br>
<p>After that, you can access the <a href="<?= $admin_link?>" target="_blank">CSSTrackr panel (/admin/)</a></p>
</div>
</div>
<?php }?>
<div class="container footer">
<div class="row"><div class="col-xs-12">
<div class="pull-left"><a href="https://github.com/lucaasleaal/csstrackr" target="_blank">Fork on Github</a></div>
<div class="pull-right">This project is licenced by MIT License.</div>
</div></div>
</div>
</body>
</html>