forked from sproctor/php-calendar
/
install.php
416 lines (341 loc) · 11 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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
/*
* Copyright 2014 Sean Proctor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
Run this file to install the calendar
it needs very much work
*/
$phpc_root_path = dirname(__FILE__);
$phpc_includes_path = "$phpc_root_path/includes";
$phpc_config_file = "$phpc_root_path/config.php";
define('IN_PHPC', true);
if(!function_exists("mysqli_connect"))
soft_error("You must have the mysqli extension for PHP installed to use this calendar.");
require_once("$phpc_includes_path/dbversion.php");
require_once("$phpc_includes_path/calendar.php");
?><!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="static/phpc.css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<title>PHP Calendar Installation</title>
</head>
<body>
<h1>PHP Calendar</h1>
<?php
$must_upgrade = false;
if(file_exists($phpc_config_file)) {
include_once($phpc_config_file);
if(defined("SQL_HOST")) {
$dbh = connect_db(SQL_HOST, SQL_USER, SQL_PASSWD, SQL_DATABASE);
$query = "SELECT *\n"
."FROM `" . SQL_PREFIX . "calendars`\n";
$sth = $dbh->query($query);
$have_calendar = $sth && $sth->fetch_assoc();
$existing_version = 0;
$query = "SELECT `value`\n"
."FROM `" . SQL_PREFIX . "config`\n"
."WHERE `name`='version'";
$sth = $dbh->query($query);
if($sth) {
$result = $sth->fetch_assoc();
if(!empty($result['value']))
$existing_version = $result['value'];
}
if($have_calendar) {
$must_upgrade = true;
if($existing_version > PHPC_DB_VERSION) {
echo "<p>DB version is newer than the upgrader.</p>";
exit;
} elseif($existing_version == PHPC_DB_VERSION) {
echo '<p>The calendar has already been installed. <a href="index.php">Installed calendar</a></p>';
echo '<p>If you want to install again, manually delete config.php</p>';
exit;
}
}
}
}
echo '<p>Welcome to the PHP Calendar installation process.</p>
<form method="post" action="install.php">
';
foreach($_POST as $key => $value) {
echo "<input name=\"$key\" value=\"$value\" type=\"hidden\">\n";
}
$drop_tables = isset($_POST["drop_tables"]) && $_POST["drop_tables"] == "yes";
if(!isset($_POST['my_hostname'])
&& !isset($_POST['my_username'])
&& !isset($_POST['my_passwd'])
&& !isset($_POST['my_prefix'])
&& !isset($_POST['my_database'])) {
get_server_setup();
} elseif((isset($_POST['create_user']) || isset($_POST['create_db']))
&& !isset($_POST['done_user_db'])) {
add_sql_user_db();
} elseif(!isset($_POST['base'])) {
install_base();
} elseif(!isset($_POST['admin_user'])
&& !isset($_POST['admin_pass'])) {
get_admin();
} else {
add_calendar();
}
function check_config()
{
global $phpc_config_file;
if(is_writable($phpc_config_file))
return true;
// Check if we can create the file
if($file = @fopen($phpc_config_file, 'a')) {
fclose($file);
return true;
}
return false;
}
function report_config()
{
echo '<p>Your configuration file could not be written to. This file '
.'probably does not yet exist. If that is the case, youneed to '
.'create it. You need to make sure this script can write to '
.'it. We suggest logging in with a shell and typing:</p>
<p><pre>
touch config.php
chmod 666 config.php
</pre></p>
<p>or if you only have ftp access, upload a blank file named '
.'config.php to your php-calendar directory then use the chmod '
.'command to change the permissions of config.php to 666.</p>
<input type="submit" value="Retry"/>';
}
function get_server_setup()
{
if(!check_config())
return report_config();
echo '
<h3>Step 1: Database</h3>
<table class="display">
<tr>
<td>SQL Server Hostname:</td>
<td><input type="text" name="my_hostname" value="localhost"></td>
</tr>
<tr>
<td>SQL Database name:</td>
<td><input type="text" name="my_database" value="calendar"></td>
</tr>
<tr>
<td>SQL Table prefix:</td>
<td><input type="text" name="my_prefix" value="phpc_"></td>
</tr>
<tr>
<td>SQL Username:</td>
<td><input type="text" name="my_username" value="calendar"></td>
</tr>
<tr>
<td>SQL Password:</td>
<td><input type="password" name="my_passwd"></td>
</tr>
<tr>
<td colspan="2">
<input type="checkbox" name="create_db" value="yes"/>
Create the database (don\'t check this if it already exists)
</td>
</tr>
<tr>
<td colspan="2">
<input type="checkbox" name="drop_tables" value="yes">
Drop tables before creating them
</td>
</tr>
<tr><td colspan="2">
<span style="font-weight:bold;">Optional: user creation on database</span>
</td></tr>
<tr><td colspan="2">
If the credentials supplied above are new, you have to be the database administrator.
</td></tr>
<tr><td colspan="2">
<input type="checkbox" name="create_user" value="yes">
Check this if you want to do it and provide admin user and password.
</td></tr>
<tr>
<td>SQL Admin name:</td>
<td><input type="text" name="my_adminname"></td>
</tr>
<tr>
<td>SQL Admin Password:</td>
<td><input type="password" name="my_adminpasswd"></td>
</tr>
<tr>
<td colspan="2">
<input name="action" type="submit" value="Install">
</td>
</tr>
</table>';
}
function add_sql_user_db()
{
$my_hostname = $_POST['my_hostname'];
$my_username = $_POST['my_username'];
$my_passwd = $_POST['my_passwd'];
$my_prefix = $_POST['my_prefix'];
$my_database = $_POST['my_database'];
$my_adminname = $_POST['my_adminname'];
$my_adminpasswd = $_POST['my_adminpasswd'];
$create_user = isset($_POST['create_user'])
&& $_POST['create_user'] == 'yes';
$create_db = isset($_POST['create_db']) && $_POST['create_db'] == 'yes';
// Make the database connection.
if($create_user) {
$dbh = connect_db($my_hostname, $my_adminname, $my_adminpasswd);
} else {
$dbh = connect_db($my_hostname, $my_username, $my_passwd);
}
$string = "<h3>Step 2: Database Setup</h3>";
if($create_db) {
$query = "CREATE DATABASE $my_database";
$dbh->query($query)
or install_db_error($dbh, 'error creating db', $query);
$string .= "<p>Successfully created database</p>";
}
if($create_user) {
$query = "GRANT ALL ON accounts.* TO $my_username@$my_hostname identified by '$my_passwd'";
$dbh->query($query)
or install_db_error($dbh, 'Could not grant:', $query);
$query = "GRANT ALL ON $my_database.*\n"
."TO $my_username IDENTIFIED BY '$my_passwd'";
$dbh->query($query)
or install_db_error($dbh, 'Could not grant:', $query);
$query = "FLUSH PRIVILEGES";
$dbh->query($query)
or install_db_error($dbh, "Could not flush privileges", $query);
$string .= "<p>Successfully added user</p>";
}
echo "$string\n"
."<div><input type=\"submit\" name=\"done_user_db\" value=\"continue\"/>"
."</div>\n";
}
function create_config($sql_hostname, $sql_username, $sql_passwd, $sql_database,
$sql_prefix, $sql_type)
{
return "<?php\n"
."define('SQL_HOST', '$sql_hostname');\n"
."define('SQL_USER', '$sql_username');\n"
."define('SQL_PASSWD', '$sql_passwd');\n"
."define('SQL_DATABASE', '$sql_database');\n"
."define('SQL_PREFIX', '$sql_prefix');\n"
."define('SQL_TYPE', '$sql_type');\n"
."?".">\n"; // Break this up to fix syntax HL in vim
}
function install_base()
{
global $phpc_config_file;
$sql_type = "mysqli";
$my_hostname = $_POST['my_hostname'];
$my_username = $_POST['my_username'];
$my_passwd = $_POST['my_passwd'];
$my_prefix = $_POST['my_prefix'];
$my_database = $_POST['my_database'];
$fp = fopen($phpc_config_file, 'w')
or soft_error('Couldn\'t open config file.');
fwrite($fp, create_config($my_hostname, $my_username, $my_passwd,
$my_database, $my_prefix, $sql_type))
or soft_error("Could not write to file");
fclose($fp);
// Make the database connection.
include($phpc_config_file);
$dbh = connect_db(SQL_HOST, SQL_USER, SQL_PASSWD, SQL_DATABASE);
create_tables($dbh);
$query = "REPLACE INTO `" . SQL_PREFIX . "config`\n"
."(`name`, `value`)\n"
."VALUES ('version', '".PHPC_DB_VERSION."')";
$dbh->query($query)
or install_db_error($dbh, 'Error creating version row.', $query);
echo "<p>Config file created at \"". realpath($phpc_config_file) ."\"</p>"
."<p>Calendars database created</p>\n"
."<div><input type=\"submit\" name=\"base\" value=\"continue\">"
."</div>\n";
}
function create_tables($dbh)
{
global $drop_tables, $phpc_includes_path;
require_once("$phpc_includes_path/schema.php");
foreach(phpc_table_schemas() as $table) {
$table->create($dbh, $drop_tables);
}
}
function get_admin()
{
echo '<h3>Step 4: Administration account</h3>';
echo "<table>\n"
."<tr><td colspan=\"2\">Now you must create the calendar administrative "
."account.</td></tr>\n"
."<tr><td>\n"
."Admin name:\n"
."</td><td>\n"
."<input type=\"text\" name=\"admin_user\" />\n"
."</td></tr><tr><td>\n"
."Admin password:"
."</td><td>\n"
."<input type=\"password\" name=\"admin_pass\" />\n"
."</td></tr><tr><td colspan=\"2\">"
."<input type=\"submit\" value=\"Create Admin Account\" />\n"
."</td></tr></table>\n";
}
function add_calendar()
{
global $phpc_config_file;
require_once($phpc_config_file);
$calendar_title = 'PHP-Calendar';
// Make the database connection.
$dbh = connect_db(SQL_HOST, SQL_USER, SQL_PASSWD, SQL_DATABASE);
$query = "INSERT INTO ".SQL_PREFIX."calendars\n"
."(`cid`) VALUE (1)";
$dbh->query($query)
or install_db_error($dbh, 'Error reading options', $query);
$cid = $dbh->insert_id;
echo "<h3>Final Step</h3>\n";
echo "<p>Saved default configuration</p>\n";
$passwd = md5($_POST['admin_pass']);
$query = "INSERT INTO `" . SQL_PREFIX . "users`\n"
."(`username`, `password`, `admin`) VALUES\n"
."('$_POST[admin_user]', '$passwd', 1)";
$dbh->query($query)
or install_db_error($dbh, 'Error adding admin.', $query);
echo "<p>Admin account created.</p>";
echo "<p>Now you should delete install.php file from root directory (for security reasons).</p>";
echo "<p>You should also change the permissions on config.php so only your webserver can read it.</p>";
echo "<p><a href=\"index.php\">View calendar</a></p>";
}
echo '</form></body></html>';
// called when there is an error involving the DB
function install_db_error($dbh, $str, $query = "")
{
$string = "$str<br />" . $dbh->error;
if($query != "")
$string .= "<br />SQL query: $query";
soft_error($string);
}
function connect_db($hostname, $username, $passwd, $database = false)
{
$dbh = new mysqli($hostname, $username, $passwd);
if(mysqli_connect_errno()) {
soft_error("Database connect failed (" . mysqli_connect_errno()
. "): " . mysqli_connect_error());
}
if($database)
$dbh->select_db($database);
$dbh->query("SET NAMES 'utf8'");
return $dbh;
}
?>