-
Notifications
You must be signed in to change notification settings - Fork 0
/
configurator.php
260 lines (240 loc) · 9.87 KB
/
configurator.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
<?php
// Use default autoload implementation
require 'vendor/autoload.php';
$filename = 'config/config.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$code = Configurator::loadCode($filename);
$config = Configurator::parseConfig($code);
$config = Configurator::mergePost($config, $_POST);
echo "<pre>";
$success = false;
if (Configurator::testConfig($config)) {
$code = Configurator::generateCode($config);
Configurator::writeCode($filename, $code);
echo "\nConfig written\n";
$success = true;
} else {
echo "\nConfig not written (invalid)\n";
}
echo "</pre>";
if ($success) {
echo '<input type="button" value="OK" onClick="window.location.href=window.location.href">';
} else {
echo '<input type="button" value="Back" onClick="history.go(-1)">';
}
die();
}
$code = Configurator::loadCode($filename);
$config = Configurator::parseConfig($code);
echo Configurator::generateForm($config);
class Configurator
{
public static function loadCode($filename)
{
if (!file_exists($filename)) {
$filename .= '.template';
}
if (!file_exists($filename)) {
throw new \Exception("Could not read: $filename");
}
return file_get_contents($filename);
}
public static function writeCode($filename, $code)
{
file_put_contents($filename, $code);
if (!file_exists($filename)) {
throw new \Exception("Could not write: $filename");
}
}
public static function mergePost($config, $post)
{
$store = function ($class, $name, $value) use (&$config) {
foreach ($config[$class] as $i => $v) {
if ($name == $v['name']) {
if (gettype($v['value']) == 'boolean') {
$config[$class][$i]['value'] = (bool) $value;
} else if (gettype($v['value']) == 'integer') {
$config[$class][$i]['value'] = (int) $value;
} else if (gettype($v['value']) == 'double') {
$config[$class][$i]['value'] = (float) $value;
} else {
$config[$class][$i]['value'] = $value;
}
}
}
};
foreach ($config as $class => $variables) {
foreach ($variables as $v) {
$name = $v['name'];
if (isset($post[$class][$name])) {
$store($class, $name, $post[$class][$name]);
}
}
}
return $config;
}
public static function generateForm($config)
{
$str = "<h1>Configurator</h1>\n";
$str .= "<form method=\"POST\">\n";
foreach ($config as $class => $variables) {
$str .= "<fieldset>\n";
$str .= "<legend>$class</legend>\n";
foreach ($variables as $v) {
$name = $v['name'];
$comment = $v['comment'];
if ($comment) {
$comment = "($comment)";
}
$input = $class . '[' . $v['name'] . ']';
$str .= "$name $comment<br/>\n";
$value = htmlentities($v['value']);
if (gettype($v['value']) == 'boolean') {
$str .= '<input type="radio" name="' . $input . '" value="1"' . ($value ? ' checked="checked"' : '') . '> true';
$str .= '<input type="radio" name="' . $input . '" value="0"' . ((!$value) ? ' checked="checked"' : '') . '> false <br/>';
} else {
$str .= '<input type="text" name="' . $input . '" value="' . $value . '"><br/>';
}
}
$str .= "</fieldset>\n";
}
$str .= "<br/>\n";
$str .= '<input type="submit" value="Test and Save"><br/>';
$str .= "</form>\n";
return $str;
}
public static function parseConfig($code)
{
$config = [];
$lines = preg_split("/\r?\n/", $code);
foreach ($lines as $line) {
if (preg_match('/^\s*MintyPHP\\\([a-z]+)::\$([a-z]+)\s*=(.*);\s*(\/\/(.*))?$/i', $line, $matches)) {
$class = $matches[1];
if (!isset($config[$class])) {
$config[$class] = [];
}
$name = $matches[2];
$value = trim($matches[3]);
if (is_numeric($value) && strpos($value, '.') !== false) {
$value = (float) $value;
} else if (is_numeric($value)) {
$value = (int) $value;
} else if (in_array($value, array('true', 'false'))) {
$value = $value == 'true' ?: false;
} else {
$value = trim($value, '\'"');
}
if (isset($matches[5])) {
$comment = trim($matches[5]);
} else {
$comment = false;
}
$config[$class][] = compact('name', 'value', 'comment');
}
}
return $config;
}
public static function generateCode($config)
{
$export = function ($v) {
if (gettype($v['value']) == 'boolean') {
return $v['value'] ? 'true' : 'false';
} else {
return var_export($v['value'], true);
}
};
$code = "<?php\n\n";
foreach ($config as $class => $variables) {
$nameChars = 0;
foreach ($variables as $v) {
$nameChars = max($nameChars, strlen($v['name']));
}
foreach ($variables as $v) {
$name = sprintf("%-${nameChars}s", $v['name']);
$value = sprintf("%s", $export($v));
$code .= "MintyPHP\\$class::\$$name = $value;";
if ($v['comment']) {
$code .= " // $v[comment]";
}
$code .= "\n";
}
$code .= "\n";
}
return $code;
}
public static function testConfig(&$config)
{
$parameters = array();
foreach ($config as $class => &$variables) {
foreach ($variables as &$v) {
$parameters[$class . '_' . $v['name']] = &$v['value'];
}
}
mysqli_report(MYSQLI_REPORT_ERROR);
$mysqli = new mysqli($parameters['DB_host'], $parameters['DB_username'], $parameters['DB_password']);
if ($mysqli->connect_error) {
echo "ERROR: MySQL connect: ($mysqli->connect_errno) $mysqli->connect_error\n";
return false;
}
echo "INFO: MySQL connected\n";
$sql = "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$parameters[DB_database]';";
if (!$result = $mysqli->query($sql)) {
echo "ERROR: MySQL database check: $mysqli->error\n";
return false;
} elseif ($result->num_rows) {
echo "INFO: MySQL database exists\n";
} else {
if ($parameters['DB_username'] != 'root') {
echo "ERROR: MySQL database not found: $parameters[DB_database]\n";
return false;
}
$sql = "CREATE DATABASE `$parameters[DB_database]` COLLATE 'utf8_bin';";
if (!$result = $mysqli->query($sql)) {
echo "ERROR: MySQL database create: $mysqli->error\n";
return false;
}
echo "INFO: MySQL database created\n";
$host = $parameters['DB_host'] == 'localhost' ? 'localhost' : '%';
$pass = base64_encode(sha1(rand() . time(true) . $parameters['DB_database'], true));
$sql = "CREATE USER '$parameters[DB_database]'@'$host' IDENTIFIED BY '$pass';";
if (!$result = $mysqli->query($sql)) {
echo "ERROR: MySQL user create: $mysqli->error\n";
return false;
}
echo "INFO: MySQL user created\n";
$sql = "GRANT ALL PRIVILEGES ON `$parameters[DB_database]`.* TO '$parameters[DB_database]'@'$host';";
if (!$result = $mysqli->query($sql)) {
echo "ERROR: MySQL grant user: $mysqli->error\n";
return false;
}
echo "INFO: MySQL user granted\n";
$parameters['DB_username'] = $parameters['DB_database'];
$parameters['DB_password'] = $pass;
}
$sql = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$parameters[DB_database]' AND TABLE_NAME = 'users';";
if (!$result = $mysqli->query($sql)) {
echo "ERROR: MySQL users table check: $mysqli->error\n";
return false;
} elseif (!$result->num_rows) {
$sql = "CREATE TABLE `$parameters[DB_database]`.`users` (";
$sql .= "`id` int(11) NOT NULL AUTO_INCREMENT,";
$sql .= "`username` varchar(255) COLLATE utf8_bin NOT NULL,";
$sql .= "`password` varchar(255) COLLATE utf8_bin NOT NULL,";
$sql .= "`created` datetime NOT NULL,";
$sql .= "PRIMARY KEY (`id`),";
$sql .= "UNIQUE KEY `username` (`username`)";
$sql .= ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;";
if (!$mysqli->query($sql)) {
echo "ERROR: MySQL create users table: $mysqli->error\n";
return false;
}
echo "INFO: MySQL users table created\n";
} else {
echo "INFO: MySQL users table exists\n";
}
if ($mysqli->close()) {
echo "INFO: MySQL disconnected\n";
}
return true;
}
}