forked from shish/shimmie2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.php
481 lines (432 loc) · 14.3 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<?php
/**
* Shimmie Installer
*
* @package Shimmie
* @copyright Copyright (c) 2007-2015, Shish et al.
* @author Shish <webmaster at shishnet.org>, jgen <jeffgenovy at gmail.com>
* @link http://code.shishnet.org/shimmie2/
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
* Initialise the database, check that folder
* permissions are set properly.
*
* This file should be independant of the database
* and other such things that aren't ready yet
*/
// TODO: Rewrite the entire installer and make it more readable.
ob_start();
date_default_timezone_set('UTC');
?>
<!DOCTYPE html>
<html>
<head>
<title>Shimmie Installation</title>
<link rel="shortcut icon" href="favicon.ico" />
<link rel='stylesheet' href='lib/shimmie.css' type='text/css'>
<script type="text/javascript" src="lib/vendor/js/jquery-1.12.3.min.js"></script>
</head>
<body>
<?php if(false) { ?>
<div id="installer">
<h1>Install Error</h1>
<p>Shimmie needs to be run via a web server with PHP support -- you
appear to be either opening the file from your hard disk, or your
web server is mis-configured and doesn't know how to handle PHP files.</p>
<p>If you've installed a web server on your desktop PC, you probably
want to visit <a href="http://localhost/">the local web server</a>.<br/><br/>
</p>
</div>
<div style="display: none;">
<PLAINTEXT>
<?php }
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_BAIL, 1);
/*
* Compute the path to the folder containing "install.php" and
* store it as the 'Shimmie Root' folder for later on.
*
* Example:
* __SHIMMIE_ROOT__ = '/var/www/shimmie2/'
*
*/
define('__SHIMMIE_ROOT__', trim(remove_trailing_slash(dirname(__FILE__))) . '/');
// Pull in necessary files
require_once __SHIMMIE_ROOT__."core/util.inc.php";
require_once __SHIMMIE_ROOT__."core/exceptions.class.php";
require_once __SHIMMIE_ROOT__."core/database.class.php";
if(is_readable("data/config/shimmie.conf.php")) die("Shimmie is already installed.");
do_install();
// utilities {{{
// TODO: Can some of these be pushed into "core/util.inc.php" ?
/**
* Strips off any kind of slash at the end so as to normalise the path.
* @param string $path Path to normalise.
* @return string Path without trailing slash.
*/
function remove_trailing_slash($path) {
if ((substr($path, -1) === '/') || (substr($path, -1) === '\\')) {
return substr($path, 0, -1);
} else {
return $path;
}
}
function check_gd_version() {
$gdversion = 0;
if (function_exists('gd_info')){
$gd_info = gd_info();
if (substr_count($gd_info['GD Version'], '2.')) {
$gdversion = 2;
} else if (substr_count($gd_info['GD Version'], '1.')) {
$gdversion = 1;
}
}
return $gdversion;
}
function check_im_version() {
if(!ini_get('safe_mode')) {
$convert_check = exec("convert");
}
return (empty($convert_check) ? 0 : 1);
}
function eok($name, $value) {
echo "<br>$name ... ";
if($value) {
echo "<span color='green'>ok</span>\n";
}
else {
echo "<span color='red'>failed</span>\n";
}
}
// }}}
function do_install() { // {{{
if(file_exists("data/config/auto_install.conf.php")) {
require_once "data/config/auto_install.conf.php";
}
else if(@$_POST["database_type"] == "sqlite" && isset($_POST["database_name"])) {
define('DATABASE_DSN', "sqlite:{$_POST["database_name"]}");
}
else if(isset($_POST['database_type']) && isset($_POST['database_host']) && isset($_POST['database_user']) && isset($_POST['database_name'])) {
define('DATABASE_DSN', "{$_POST['database_type']}:user={$_POST['database_user']};password={$_POST['database_password']};host={$_POST['database_host']};dbname={$_POST['database_name']}");
}
else {
ask_questions();
return;
}
if(isset($_POST['recommended_extensions'])) {
define('EXTRA_EXTS', 'favorites,rating,numeric_score,relationships,tag_categories,tag_history,source_history');
}
define("DATABASE_KA", true);
install_process();
} // }}}
function ask_questions() { // {{{
$warnings = array();
$errors = array();
if(check_gd_version() == 0 && check_im_version() == 0) {
$errors[] = "
No thumbnailers cound be found - install the imagemagick
tools (or the PHP-GD library, of imagemagick is unavailable).
";
}
else if(check_im_version() == 0) {
$warnings[] = "
The 'convert' command (from the imagemagick package)
could not be found - PHP-GD can be used instead, but
the size of thumbnails will be limited.
";
}
$drivers = PDO::getAvailableDrivers();
if(
!in_array("mysql", $drivers) &&
!in_array("pgsql", $drivers) &&
!in_array("sqlite", $drivers)
) {
$errors[] = "
No database connection library could be found; shimmie needs
PDO with either Postgres, MySQL, or SQLite drivers
";
}
$db_m = in_array("mysql", $drivers) ? '<option value="mysql">MySQL</option>' : "";
$db_p = in_array("pgsql", $drivers) ? '<option value="pgsql">PostgreSQL</option>' : "";
$db_s = in_array("sqlite", $drivers) ? '<option value="sqlite">SQLite</option>' : "";
$warn_msg = $warnings ? "<h3>Warnings</h3>".implode("\n<br>", $warnings) : "";
$err_msg = $errors ? "<h3>Errors</h3>".implode("\n<br>", $errors) : "";
print <<<EOD
<div id="installer">
<h1>Shimmie Installer</h1>
$warn_msg
$err_msg
<h3>Database Install</h3>
<form action="install.php" method="POST">
<center>
<table class='form'>
<tr>
<th>Type:</th>
<td><select name="database_type" id="database_type" onchange="update_qs();">
$db_m
$db_p
$db_s
</select></td>
</tr>
<tr class="dbconf mysql pgsql">
<th>Host:</th>
<td><input type="text" name="database_host" size="40" value="localhost"></td>
</tr>
<tr class="dbconf mysql pgsql">
<th>Username:</th>
<td><input type="text" name="database_user" size="40"></td>
</tr>
<tr class="dbconf mysql pgsql">
<th>Password:</th>
<td><input type="password" name="database_password" size="40"></td>
</tr>
<tr class="dbconf mysql pgsql sqlite">
<th>DB Name:</th>
<td><input type="text" name="database_name" size="40" value="shimmie"></td>
</tr>
<tr>
<th>Recommended Extensions:</th>
<td><input type="checkbox" name="recommended_extensions" size="40" checked></td>
</tr>
<tr><td colspan="2"><input type="submit" value="Go!"></td></tr>
</table>
</center>
<script>
$(function() {
update_qs();
});
function update_qs() {
$(".dbconf").hide();
var seldb = $("#database_type").val() || "none";
$("."+seldb).show();
}
</script>
</form>
<h3>Help</h3>
<p class="dbconf mysql pgsql">
Please make sure the database you have chosen exists and is empty.<br>
The username provided must have access to create tables within the database.
</p>
<p class="dbconf sqlite">
For SQLite the database name will be a filename on disk, relative to
where shimmie was installed.
</p>
<p class="dbconf none">
Drivers can generally be downloaded with your OS package manager;
for Debian / Ubuntu you want php5-pgsql, php5-mysql, or php5-sqlite.
</p>
</div>
EOD;
} // }}}
/**
* This is where the install really takes place.
*/
function install_process() { // {{{
build_dirs();
create_tables();
insert_defaults();
write_config();
} // }}}
function create_tables() { // {{{
try {
$db = new Database();
if ( $db->count_tables() > 0 ) {
print <<<EOD
<div id="installer">
<h1>Shimmie Installer</h1>
<h3>Warning: The Database schema is not empty!</h3>
<p>Please ensure that the database you are installing Shimmie with is empty before continuing.</p>
<p>Once you have emptied the database of any tables, please hit 'refresh' to continue.</p>
<br/><br/>
</div>
EOD;
exit(2);
}
$db->create_table("aliases", "
oldtag VARCHAR(128) NOT NULL,
newtag VARCHAR(128) NOT NULL,
PRIMARY KEY (oldtag)
");
$db->execute("CREATE INDEX aliases_newtag_idx ON aliases(newtag)", array());
$db->create_table("config", "
name VARCHAR(128) NOT NULL,
value TEXT,
PRIMARY KEY (name)
");
$db->create_table("users", "
id SCORE_AIPK,
name VARCHAR(32) UNIQUE NOT NULL,
pass VARCHAR(250),
joindate SCORE_DATETIME NOT NULL DEFAULT SCORE_NOW,
class VARCHAR(32) NOT NULL DEFAULT 'user',
email VARCHAR(128)
");
$db->execute("CREATE INDEX users_name_idx ON users(name)", array());
$db->create_table("images", "
id SCORE_AIPK,
owner_id INTEGER NOT NULL,
owner_ip SCORE_INET NOT NULL,
filename VARCHAR(255) NOT NULL,
filesize INTEGER NOT NULL,
hash CHAR(32) UNIQUE NOT NULL,
ext CHAR(4) NOT NULL,
source VARCHAR(255),
width INTEGER NOT NULL,
height INTEGER NOT NULL,
posted SCORE_DATETIME NOT NULL DEFAULT SCORE_NOW,
locked SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N,
FOREIGN KEY (owner_id) REFERENCES users(id) ON DELETE RESTRICT
");
$db->execute("CREATE INDEX images_owner_id_idx ON images(owner_id)", array());
$db->execute("CREATE INDEX images_width_idx ON images(width)", array());
$db->execute("CREATE INDEX images_height_idx ON images(height)", array());
$db->execute("CREATE INDEX images_hash_idx ON images(hash)", array());
$db->create_table("tags", "
id SCORE_AIPK,
tag VARCHAR(64) UNIQUE NOT NULL,
count INTEGER NOT NULL DEFAULT 0
");
$db->execute("CREATE INDEX tags_tag_idx ON tags(tag)", array());
$db->create_table("image_tags", "
image_id INTEGER NOT NULL,
tag_id INTEGER NOT NULL,
UNIQUE(image_id, tag_id),
FOREIGN KEY (image_id) REFERENCES images(id) ON DELETE CASCADE,
FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE
");
$db->execute("CREATE INDEX images_tags_image_id_idx ON image_tags(image_id)", array());
$db->execute("CREATE INDEX images_tags_tag_id_idx ON image_tags(tag_id)", array());
$db->execute("INSERT INTO config(name, value) VALUES('db_version', 11)");
$db->commit();
}
catch(PDOException $e) {
print <<<EOD
<div id="installer">
<h1>Shimmie Installer</h1>
<h3>Database Error:</h3>
<p>An error occured while trying to create the database tables necessary for Shimmie.</p>
<p>Please check and ensure that the database configuration options are all correct.</p>
<p>{$e->getMessage()}</p>
</div>
EOD;
exit(3);
}
catch (Exception $e) {
print <<<EOD
<div id="installer">
<h1>Shimmie Installer</h1>
<h3>Unknown Error:</h3>
<p>An unknown error occured while trying to create the database tables necessary for Shimmie.</p>
<p>Please check the server log files for more information.</p>
<p>{$e->getMessage()}</p>
</div>
EOD;
exit(4);
}
} // }}}
function insert_defaults() { // {{{
try {
$db = new Database();
$db->execute("INSERT INTO users(name, pass, joindate, class) VALUES(:name, :pass, now(), :class)", Array("name" => 'Anonymous', "pass" => null, "class" => 'anonymous'));
$db->execute("INSERT INTO config(name, value) VALUES(:name, :value)", Array("name" => 'anon_id', "value" => $db->get_last_insert_id('users_id_seq')));
if(check_im_version() > 0) {
$db->execute("INSERT INTO config(name, value) VALUES(:name, :value)", Array("name" => 'thumb_engine', "value" => 'convert'));
}
$db->commit();
}
catch(PDOException $e)
{
print <<<EOD
<div id="installer">
<h1>Shimmie Installer</h1>
<h3>Database Error:</h3>
<p>An error occured while trying to insert data into the database.</p>
<p>Please check and ensure that the database configuration options are all correct.</p>
<p>{$e->getMessage()}</p>
</div>
EOD;
exit(5);
}
catch (Exception $e)
{
print <<<EOD
<div id="installer">
<h1>Shimmie Installer</h1>
<h3>Unknown Error:</h3>
<p>An unknown error occured while trying to insert data into the database.</p>
<p>Please check the server log files for more information.</p>
<p>{$e->getMessage()}</p>
</div>
EOD;
exit(6);
}
} // }}}
function build_dirs() { // {{{
// *try* and make default dirs. Ignore any errors --
// if something is amiss, we'll tell the user later
if(!file_exists("images")) @mkdir("images");
if(!file_exists("thumbs")) @mkdir("thumbs");
if(!file_exists("data") ) @mkdir("data");
if(!is_writable("images")) @chmod("images", 0755);
if(!is_writable("thumbs")) @chmod("thumbs", 0755);
if(!is_writable("data") ) @chmod("data", 0755);
// Clear file status cache before checking again.
clearstatcache();
if(
!file_exists("images") || !file_exists("thumbs") || !file_exists("data") ||
!is_writable("images") || !is_writable("thumbs") || !is_writable("data")
) {
print "
<div id='installer'>
<h1>Shimmie Installer</h1>
<h3>Directory Permissions Error:</h3>
<p>Shimmie needs to make three folders in it's directory, '<i>images</i>', '<i>thumbs</i>', and '<i>data</i>', and they need to be writable by the PHP user.</p>
<p>If you see this error, if probably means the folders are owned by you, and they need to be writable by the web server.</p>
<p>PHP reports that it is currently running as user: ".$_ENV["USER"]." (". $_SERVER["USER"] .")</p>
<p>Once you have created these folders and / or changed the ownership of the shimmie folder, hit 'refresh' to continue.</p>
<br/><br/>
</div>
";
exit(7);
}
} // }}}
function write_config() { // {{{
$file_content = ''.
'<?php'."\n".
'define(\'DATABASE_DSN\', \''.DATABASE_DSN.'\');'."\n".
(defined('EXTRA_EXTS') ? 'define(\'EXTRA_EXTS\', \''.EXTRA_EXTS.'\');'."\n" : '').
'?>';
if(!file_exists("data/config")) {
mkdir("data/config", 0755, true);
}
if(file_put_contents("data/config/shimmie.conf.php", $file_content, LOCK_EX)) {
header("Location: index.php");
print <<<EOD
<div id="installer">
<h1>Shimmie Installer</h1>
<h3>Things are OK \o/</h3>
<p>If you aren't redirected, <a href="index.php">click here to Continue</a>.
</div>
EOD;
}
else {
$h_file_content = htmlentities($file_content);
print <<<EOD
<div id="installer">
<h1>Shimmie Installer</h1>
<h3>File Permissions Error:</h3>
The web server isn't allowed to write to the config file; please copy
the text below, save it as 'data/config/shimmie.conf.php', and upload it into the shimmie
folder manually. Make sure that when you save it, there is no whitespace
before the "<?php" or after the "?>"
<p><textarea cols="80" rows="2">$h_file_content</textarea>
<p>Once done, <a href="index.php">click here to Continue</a>.
<br/><br/>
</div>
EOD;
}
echo "\n";
} // }}}
?>
</body>
</html>