forked from GroundApps/ShoppingList_Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
INSTALL.php
301 lines (270 loc) · 11.4 KB
/
INSTALL.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
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<title>Install ShoppingList</title>
</head>
<body>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<?php
if (version_compare(phpversion(), '5.3.3', '>=') AND version_compare(phpversion(), '5.5', '<'))
require_once('func.inc.php');
//Start on submit form
if(isset($_POST['createConfig'])) {
function generateRandomPWD($length = 25) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function get_qrcode($_apikey) {
include('lib/phpqrcode-git/phpqrcode.php');
$url = "http" . (($_SERVER['SERVER_PORT'] == 443) ? "s://" : "://") . $_SERVER['HTTP_HOST'];
$uri = substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/') + 1);
$api_url = $url . $uri . "api.php";
$ssl = (($_SERVER['SERVER_PORT'] == 443) ? "true" : "false");
$qr_code = "{ \"url\": \"$api_url\", \"apikey\": \"$_apikey\", \"ssl\": $ssl}";
ob_start();
QRCode::png($qr_code);
$image_string = base64_encode(ob_get_clean());
header('Content-Type: text/html');
return $image_string;
}
//set vars
//hash api key with bcrypt
$apikey = password_hash($_POST['apikey'], PASSWORD_BCRYPT);
$dbtype = $_POST['type'];
$dbhost = $_POST['hostname'];
$dbname = (isset($_POST['database']) ? $_POST['database'] : "");
$dbuser = $_POST['dbuser'];
$dbpassword = $_POST['dbpassword'];
$createDBUser = (isset($_POST['createDBUser']) ? $_POST['createDBUser'] : "");
$dbrandom_pwd = generateRandomPWD();
$filename = 'shoppinglist.sqlite';
//set up .htaccess rules for sqlite database if sqlite is used
$htaccess = "Options ALL -Indexes\nDirectoryIndex api.php";
if($dbtype === 'SQLite') {
$htaccess .= "\n<files ".$filename.">\norder allow,deny\ndeny from all\n</files>\n";
}
file_put_contents('.htaccess', $htaccess);
//create config file value
if($createDBUser == "true") {
$config_access = '
<?php
$authKey = \''.$apikey.'\';
$dataBase = "'.$dbtype.'";
//only for SQLite
$SQLiteConfig = array(
\'file\' => "'.$filename.'",
);
//only for MySQL
$MySQLConfig = array(
\'host\' => "'.$dbhost.'",
\'db\' => "shopping",
\'user\' => "ShoppingListUser",
\'password\' => "'.$dbrandom_pwd.'",
);
?>';
//mysql dump for root access
$dbdump_access = "CREATE USER 'ShoppingListUser'@'".$dbhost."' IDENTIFIED BY '".$dbrandom_pwd."';";
$dbdump_access2 = "CREATE DATABASE shopping;";
$dbdump_access3 = "GRANT ALL PRIVILEGES ON shopping.ShoppingList TO 'ShoppingListUser'@'".$dbhost."';";
} else {
$config = '
<?php
$authKey = \''.$apikey.'\';
$dataBase = "'.$dbtype.'";
//only for SQLite
$SQLiteConfig = array(
\'file\' => "'.$filename.'",
);
//only for MySQL
$MySQLConfig = array(
\'host\' => "'.$dbhost.'",
\'db\' => "'.$dbname.'",
\'user\' => "'.$dbuser.'",
\'password\' => "'.$dbpassword.'",
);
?>';
}
//mysql dump
$dbdump = "
CREATE TABLE IF NOT EXISTS ShoppingList (
item VARCHAR(255),
count VARCHAR(255),
RID int(11) NOT NULL auto_increment,
primary KEY (RID))
ENGINE=InnoDB DEFAULT COLLATE=utf8_general_ci;";
//try to open/create config.php file
//success: write config value
//error: message and get out config.php value
if($handler = fopen('config.php', 'w')) {
if($createDBUser == "true")
fwrite($handler, $config_access);
else
fwrite($handler, $config);
fclose($handler);
} else {
echo <<<EOCONFIG
<h2>Config.php</h2>
<div class="alert alert-danger" role="alert">It was not possible to create the config.php file. Please copy the code and paste it in the file.</div>
<div class="form-group">
<label for="msg_config">config.php</label>
<textarea class="form-control" rows="15" min-height="300px" id="msg_config">
EOCONFIG;
if($createDBUser == "true")
echo $config_access;
else
echo $config;
echo '</textarea>';
}
//when DB Type MySQL create table
if($dbtype == "MySQL") {
if($createDBUser == "true")
$handler = new mysqli($dbhost, $dbuser, $dbpassword);
else
$handler = new mysqli($dbhost, $dbuser, $dbpassword, $dbname);
//check if connection successful
if ($handler->connect_error)
die('<div class="alert alert-danger" role="alert">No Connection to your Database. Please correct your Informations!</div>');
//prepare query
if($createDBUser == "true") {
$stmt1 = $handler->prepare($dbdump_access);
$stmt2 = $handler->prepare($dbdump_access2);
$stmt3 = $handler->prepare($dbdump_access3);
}
else
$stmt = $handler->prepare($dbdump);
//execute query and check if successful
if($createDBUser == "true") {
if($stmt1->execute() && $stmt2->execute() && $stmt3->execute()) {
$mysql_error = false;
$handler = new mysqli($dbhost, $dbuser, $dbpassword, 'shopping');
$stmt4 = $handler->prepare($dbdump);
if($stmt4->execute())
$mysql_error = false;
else
$mysql_error = true;
}
else {
$mysql_error = true;
}
//close connection
$stmt1->close();
$stmt2->close();
$stmt3->close();
$stmt4->close();
} else {
if($stmt->execute())
$mysql_error = false;
else
$mysql_error = true;
//close connection
$stmt->close();
}
if (!$mysql_error) {
echo '<div class="alert alert-success" role="alert">All done! Please delete the INSTALL.php file!</div>';
echo '<img src="data:image/png;base64,' . get_qrcode($_POST['apikey']) . '"/><br />
<p>Open your app and scan code for automatically configuration</p>';
} else {
echo '<div class="alert alert-danger" role="alert">
There was an Error in the MySQL Statment!
</div>';
}
} else {
echo '<div class="alert alert-success" role="alert">All done! Please delete the INSTALL.php file!</div>';
echo '<img src="data:image/png;base64,' . get_qrcode($_POST['apikey']) . '"/><br />
<p>Open your app and scan code for automatically configuration</p>';
}
} else {
?>
<script>
jQuery(document).ready(function() {
jQuery('#type').change(function(){
if(jQuery('#type').val() == 'SQLite') {
jQuery('#MySQL').hide();
} else {
jQuery('#MySQL').show();
}
});
jQuery('#createDBUser').change(function() {
if(this.checked) {
$('#createDBMessage').show();
$('#dbname').hide();
}
else {
$('#createDBMessage').hide();
$('#dbname').show();
}
});
});
</script>
<h2>Install ShoppingList Database</h2>
<h2><small>API Key</small></h2>
<form class="form-inline" action="INSTALL.php" method="post">
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-key"></i></div>
<input type="password" class="form-control" name="apikey" placeholder="API Key" size="35">
</div>
</div>
<h2><small>Database Setup</small></h2>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-database"></i></div>
<select class="form-control" id="type" name="type">
<option value="MySQL" selected>MySQL</option>
<option value="SQLite">SQLite</option>
</select>
</div>
</div><br style="font-size:5px"/>
<div id="MySQL">
<h3><small><input type="checkbox" name="createDBUser" id="createDBUser" value="true"/> Do you want to create a user and database for that App? </small></h3>
<div class="alert alert-info" role="alert" id="createDBMessage" style="display:none;">Please fill the form with your root MySQL data. After this installation this data will delete and the app will take the new created user for the operations.</div>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-globe"></i></div>
<input type="text" class="form-control" name="hostname" placeholder="Host" size="35">
</div>
</div>
<br /><br style="font-size:5px"/>
<div class="form-group" id="dbname">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-database"></i></div>
<input type="text" class="form-control" name="database" placeholder="Database Name" size="35">
</div>
<br /><br style="font-size:5px"/>
</div>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-user"></i></div>
<input type="text" class="form-control" name="dbuser" placeholder="Database User" size="35">
</div>
</div>
<br /><br style="font-size:5px"/>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-key"></i></div>
<input type="password" class="form-control" name="dbpassword" placeholder="Database Password" size="35">
</div>
</div>
</div>
<br /><br style="font-size:5px"/>
<small>Click Create to write the necessary configuration to config.php and in case of MySQL to create a table for the list storage in the given database.</small><br ><br >
<button type="submit" class="btn btn-primary" name="createConfig"> Create </button></div>
</form>
</div>
</div>
<?php
}
?>
</body>
</html>