/
install.php
276 lines (230 loc) · 8.21 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
<?php
/**
* Suomen Frisbeeliitto Kisakone
* Copyright 2009-2010 Kisakone projektiryhmä
*
* This file performs the basic installation if Kisakone.
*
* --
*
* This file is part of Kisakone.
* Kisakone is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Kisakone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Kisakone. If not, see <http://www.gnu.org/licenses/>.
* */
// Some version of PHP 5 complain about lack of time zone data, so if we can
// we'll set it now
if (is_callable('date_default_timezone_set')) {
date_default_timezone_set("Europe/Helsinki");
}
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Kisakone Installation</title>
</head>
<body>
<?php
stripSlashesIfNecessary();
if (file_exists('config.php')) {
include('config.php');
}
if (@$settings['DB_ADDRESS']) {
echo "Installation appears to have been completed already. You can not
re-enter this page without removing the configuration file.";
return;
}
if (count($_POST)) {
if (DoInstall()) {
echo "Done.";
return;
}
}
form();
function form() {
?>
<form method="POST">
<h1>Database connection settings</h1>
<table>
<tr><td>Hostname</td><td><input type="text" name="db_host"></td></tr>
<tr><td>Username</td><td><input type="text" name="db_user"></td></tr>
<tr><td>Password</td><td><input type="password" name="db_pass"></td></tr>
<tr><td>Database name</td><td><input type="text" name="db_db"></td></tr>
<tr><td>Table prefix</td><td><input type="text" name="db_prefix"></td></tr>
</table>
<h1>Create database</h1>
<p>Database will be created if not found with the defined prefix.</p>
<table>
<tr><td colspan=2"><input type="checkbox" name="db_extralogin" /> Use different login information for creating the database</td></tr>
<tr><td>Username</td><td><input type="text" name="db_admin_user"></td></tr>
<tr><td>Password</td><td><input type="password" name="db_admin_pass"></td></tr>
</table>
<h1>Initial Admin Account</h1>
<table>
<tr><td>Username</td><td><input type="text" name="ad_user"></td></tr>
<tr><td>Password</td><td><input type="password" name="ad_pass"></td></tr>
<tr><td>First Name</td><td><input type="text" name="ad_firstname"></td></tr>
<tr><td>Last Name</td><td><input type="text" name="ad_lastname"></td></tr>
<tr><td>E-mail</td><td><input type="text" name="ad_email"></td></tr>
</table>
<h1>Miscellaneous settings</h1>
<p><input type="checkbox" name="rewrite"> Use mod_rewrite</p>
<p>If the web server is not apache with mod_rewrite enabled (or another compatible option), and you've enabled this setting
all the internal links will fail. If you can and do use this setting, it does allow the system to use prettier urls.
</p>
<p>
<input type="submit" value="Save">
</p>
</form>
<?php
}
?>
</body>
</html>
<?php
function DoInstall() {
echo '<table>';
test("Database connection");
$conn = mysql_connect($_POST['db_host'], $_POST['db_user'], $_POST['db_pass']);
if (!$conn) {
return failed();
}
success();
test('Accessing database');
if (!mysql_select_db($_POST['db_db'])) return failed();
success();
if (@$_POST['db_extralogin']) {
test("Database Admin connection");
mysql_close($conn);
$conn = null;
$conn = mysql_connect($_POST['db_host'], $_POST['db_admin_user'], $_POST['db_admin_pass'], true);
if (!$conn) {
return failed();
}
success();
test('Accessing database');
if (!mysql_select_db($_POST['db_db'])) return failed();
success();
}
if (@$_POST['ad_user']) {
test('Admin username');
if (!preg_match('/[\w\d_]+/', @$_POST['ad_user'])) {
return failed();
}
success();
}
test('Config.php writable');
$fp = @fopen('config.php', 'a');
if (!$fp) return failed();
success();
fclose($fp);
echo '</table><p>Starting installation</p>';
$prefix = @$_POST['db_prefix'];
$q = "SELECT 1 FROM `{$prefix}RoundResult`";
if (@mysql_query($q)) {
echo "Database appears to be present, won't create it.<br />";
} else {
if (!InstallDB()) return;
}
if (@$_POST['ad_user']) {
if (!CreateAdmin()) return;
} else {
echo "Not creating an admin user, not asked to.<br>";
}
SaveSettings();
return true;
}
function stripSlashesIfNecessary() {
if (!get_magic_quotes_gpc()) return;
// Luckily php doesn't mind having the array values changed while
// in the foreach loops.
foreach ($_POST as $k => $v) $_POST[$k] = stripslashes($v);
foreach ($_GET as $k => $v) $_GET[$k] = stripslashes($v);
foreach ($_REQUEST as $k => $v) $_REQUEST[$k] = stripslashes($v);
foreach ($_COOKIE as $k => $v) $_COOKIE[$k] = stripslashes($v);
}
function test($n) {
printf("<tr><td>Testing: %s</td>", $n);
}
function failed() {
echo '<td style="color: red">Failed!</td></tr></table>';
return false;
}
function success() {
echo '<td style="color: green">Success!</td></tr>';
}
function InstallDB() {
$source = file_get_contents('_install_db.sql');
$queries = explode(';', $source);
$prefix = @$_POST['db_prefix'];
foreach ($queries as $query) {
if (!trim($query)) continue;
if (trim($query) == 'SHOW WARNINGS') continue;
$query = str_replace(':', $prefix, $query);
if (!mysql_query($query)) {
echo $query, "<br>";
echo mysql_error();
return false;
}
}
return true;
}
function CreateAdmin() {
$prefix = @$_POST['db_prefix'];
$query = sprintf("INSERT INTO %sUser (Username, Password, UserEmail, Role, UserFirstname, UserLastname )
VALUES ('%s', '%s', '%s', 'admin', '%s', '%s')",
$prefix,
mysql_real_escape_string($_POST['ad_user']),
mysql_real_escape_string(md5($_POST['ad_pass'])),
mysql_real_escape_string($_POST['ad_email']),
mysql_real_escape_string($_POST['ad_firstname']),
mysql_real_escape_string($_POST['ad_lastname']));
if (mysql_query($query)) {
return true;
}
echo mysql_error();
return false;
}
function SaveSettings() {
$now = date('Y-m-d H:i:s');
$dbaddr= str_replace('"', "\\\"", @$_POST['db_host']);
$dbuser = str_replace('"', "\\\"", @$_POST['db_user']);
$prefix = str_replace('"', "\\\"", @$_POST['db_prefix']);
$dbpass= str_replace('"', "\\\"", @$_POST['db_pass']);
$dbuser = str_replace('"', "\\\"", @$_POST['db_user']);
$db = str_replace('"', "\\\"", @$_POST['db_db']);
$rewrite = @$_POST['rewrite'] ? 'true' : 'false';
$q = "?";
$settingsFile =
<<<EOF
<{$q}php
/** Kisakone configuration settings
* Generated automatically on $now
* The file itself is in the public domain */
global \$settings;
\$settings['DB_ADDRESS'] = '$dbaddr';
\$settings['DB_DB'] = "$db";
\$settings['DB_USERNAME'] = "$dbuser";
\$settings['DB_PASSWORD'] = "$dbpass";
\$settings['DB_PREFIX'] = "$prefix";
\$settings['USE_MOD_REWRITE'] = $rewrite;
include_once('config_email.php');
$q>
EOF;
$fp = fopen('config.php', 'w');
if (!$fp) {
return false;
}
fwrite($fp, $settingsFile);
fclose($fp);
return true;
}
?>