-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.php
438 lines (381 loc) · 13.1 KB
/
update.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
<?php
/*
LightBlog, a PHP/SQLite blogging platform
Copyright (C) 2008-2016 The LightBlog Team.
update.php
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
define('INLB', true);
define('INDEVMODE', true);
// Gets directory URL
function baseurl()
{
$site_url = explode('/', $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
unset($site_url[count($site_url)-1]);
$site_url = implode('/', $site_url);
return 'http://'.$site_url.'/';
}
// Operates the side menu selectors
// First parameter specifies page
// Second parameter defines if we're changing the page or just reading it
function menuClass($item, $op = 0)
{
static $cur_item = 1;
if($op == 1)
{
$cur_item = (int)$item;
}
else
{
if($cur_item == $item)
{
echo "selected";
}
if($cur_item > $item)
{
echo "done";
}
if($cur_item < $item)
{
echo "notdone";
}
}
}
function remove_inserts($sql)
{
// Since none of the text have semicolons we can do this safely.
$commands = array();
foreach(explode(';', $sql) as $command)
{
$command = trim($command);
// Like the function name implies, no INSERTs. Well, except for a couple
// tables.
if(strtoupper(substr($command, 0, 6)) == 'INSERT' && strpos($command, 'INSERT INTO \'roles\'') === false && strpos($command, 'INSERT INTO \'role_permissions\'') === false)
{
continue;
}
$commands[] = $command;
}
return implode(';'. "\r\n", $commands);
}
function create_temp_db($sql)
{
require(dirname(__FILE__). '/Sources/StringFunctions.php');
// We need a random name for the database.
$filename = randomString(mt_rand(32, 64)). '.db';
$ndbh = new SQLiteDatabase($filename);
// Execute the queries.
$executed = $ndbh->queryExec($sql, $error_message);
return array($ndbh, $filename, ($executed ? null : $error_message));
}
function sanitize_row($row)
{
foreach($row as $key => $value)
{
if((string)$value == (string)(int)$value)
{
$value = (int)$value;
}
elseif((string)$value == (string)(float)$value)
{
$value = (float)$value;
}
else
{
$value = sqlite_escape_string($value);
}
$row[$key] = $value;
}
return $row;
}
function generate_query($table_name, $data)
{
return 'INSERT INTO \''. $table_name. '\' VALUES(\''. implode('\', \'', sanitize_row($data)). '\')';
}
function copy_data($db, $ndb)
{
// First off, for the categories.
$request = $db->query('
SELECT
id, shortname, fullname, info
FROM categories');
while($row = $request->fetch(SQLITE_ASSOC))
{
$ndb->query(generate_query('categories', array($row['id'], $row['shortname'], $row['fullname'], $row['info'])));
}
// Then all the settings.
$request = $db->query('
SELECT
variable, value
FROM core');
while($row = $request->fetch(SQLITE_ASSOC))
{
$ndb->query(generate_query('settings', array($row['variable'], $row['value'])));
}
// Comments, we want those!
$request = $db->query('
SELECT
id, published, pid, name, email, website, date, text, spam
FROM comments');
$comment_count = array();
while($row = $request->fetch(SQLITE_ASSOC))
{
$ndb->query(generate_query('comments', array($row['id'], $row['pid'], 'comment', $row['published'], 0, $row['name'], $row['email'], $row['website'], '', $row['date'], $row['text'], $row['spam'])));
if($row['published'] == 1)
{
$comment_count[$row['pid']] = (isset($comment_count[$row['pid']]) ? $comment_count[$row['pid']] + 1 : 1);
}
}
// We will do users next, we need their names to do a couple things right
// in just a bit.
$request = $db->query('
SELECT
id, username, password, email, displayname, role, ip, salt
FROM users');
$id_map = array();
while($row = $request->fetch(SQLITE_ASSOC))
{
$id_map[strtolower($row['username'])] = (int)$row['id'];
$ndb->query(generate_query('users', array($row['id'], $row['username'], $row['password'], $row['email'], $row['displayname'], $row['role'], $row['ip'], $row['salt'], 1, time())));
}
// Then their pages...
$request = $db->query('
SELECT
id, title, page, date, author, published
FROM pages');
while($row = $request->fetch(SQLITE_ASSOC))
{
$ndb->query(generate_query('pages', array($row['id'], $row['title'], generate_shortname($row['id'], $row['title']), $row['date'], $row['published'], $row['author'], isset($id_map[strtolower($row['author'])]) ? $id_map[strtolower($row['author'])] : 0, $row['page'])));
}
// Finally, their posts. The most important, if I do say so myself :P.
$request = $db->query('
SELECT
id, title, post, date, author, published, category, comments
FROM posts');
while($row = $request->fetch(SQLITE_ASSOC))
{
$ndb->query(generate_query('posts', array($row['id'], $row['title'], generate_shortname($row['id'], $row['title']), $row['date'], $row['published'], $row['author'], isset($id_map[strtolower($row['author'])]) ? $id_map[strtolower($row['author'])] : 0, $row['post'], $row['category'], $row['comments'], $row['comments'], isset($comment_count[$row['id']]) ? $comment_count[$row['id']] : 0)));
$ndb->query(generate_query('post_categories', array($row['id'], $row['category'])));
}
}
function generate_shortname($id, $name)
{
$char_map = 'abcdefghijklmnopqrstuvwxyz0123456789';
$name_length = strlen($name);
$name = strtolower($name);
$shortname = '';
$prev_char = null;
for($index = 0; $index < $name_length; $index++)
{
$char = substr($name, $index, 1);
// Is this an allowed character?
if(strpos($char_map, $char) === false)
{
// No repeated -.
if($prev_char !== null && $prev_char != '-')
{
$prev_char = '-';
$shortname .= '-';
}
}
else
{
$prev_char = $char;
$shortname .= $char;
}
}
return ((int)$id). '-'. trim($shortname, '-');
}
function update()
{
require('config.php');
@set_time_limit(3600);
@ini_set('memory_limit', '64M');
$dbh = new SQLiteDatabase( DBH );
// We actually use the install.sql file this time...
if(!is_readable('install.sql') && !chmod('install.sql', 0644))
{
return 'Failed to open install.sql. Please chmod it to 644 and try again.';
}
// Open, read, and close SQL file
if(is_readable('install.sql'))
{
$sqlh = fopen('install.sql', 'r');
$sql = fread($sqlh, filesize('install.sql'));
fclose($sqlh);
// Now to do some work...
// Such as removing any INSERT's, as they aren't needed.
$sql = remove_inserts($sql);
// Now create a temporary database with the new SQL.
list($ndbh, $filename, $error_message) = create_temp_db($sql);
if($error_message !== null)
{
return 'Failed to write to the temporary database because: '.$error_message.'.';
}
// Alright, almost there. Now we need to copy the data over from the old
// database to the new one.
copy_data($dbh, $ndbh);
// Okay, close the two databases and then rename it all.
unset($dbh, $ndbh);
$backup_name = dirname(DBH). '/backup-'. basename(DBH);
rename(DBH, $backup_name);
rename($filename, DBH);
return null;
}
return 'An unknown error occurred.';
}
// Will process after Step 1
if(isset($_POST['update']))
{
$return = update();
// Check for errors
if(!$return == null)
{
$error = $return;
$page = null;
}
else
{
// Set new updater page
$page = "finish";
menuClass(2, 1);
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>LightBlog Updater</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<style type="text/css">
body {
background: #EDEDED;
font-family: "Trebuchet MS", "Verdana", sans;
}
#wrapper {
width: 870px;
min-height: 540px;
margin: 20px auto;
}
#header {
height: 35px;
background: #2B5EA1;
color: #fff;
}
#header h3 {
font-size: 1.2em;
padding: 5px 0 0 8px;
}
#sidebar {
background: #D6DFEB;
width: 239px;
float: left;
min-height: 505px;
border-left: 1px dotted #5E9BEB;
border-right: 1px dotted #5E9BEB;
border-bottom: 1px dotted #5E9BEB;
}
#sidebar ul {
list-style-type: circle;
font-size: 0.9em;
margin-top: 25px;
}
#sidebar ul li {
margin-bottom: 3px;
}
#sidebar ul li.selected {
color: #000;
}
#sidebar ul li.done {
color: #9BB1CF;
}
#sidebar ul li.notdone {
color: #748CAB;
}
#content {
width: 608px;
min-height: 495px;
float: left;
background: #fff;
border-bottom: 1px dotted #5E9BEB;
border-right: 1px dotted #5E9BEB;
padding: 5px 10px;
}
#content h2 {
color: #78B1EB;
margin: 10px 0 5px 10px;
}
#content p {
margin: 15px 0 0 25px;
font-size: .98em;
line-height: 1.5em;
}
#content span#error {
margin: 15px 0 0 25px;
color: #9C0606;
font-size: .9em;
}
form, button {
margin: 20px 0 0 25px;
}
label {
font-size: .9em;
font-weight: bold;
}
input[type="submit"], button {
padding: 3px 10px 3px 10px;
}
div.clear {
clear: both;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="header">
<h3>LightBlog Updater</h3>
</div>
<div id="sidebar">
<ul>
<li class="<?php menuClass(1); ?>">Step 1: Update</li>
<li class="<?php menuClass(2); ?>">Step 2: Finish</li>
</ul>
<div class="clear"></div>
</div>
<div id="content">
<?php if(!isset($page) || $page == null): $disable = null; $page = null; ?>
<h2>Updating your database</h2>
<p>This updater <strong>only</strong> works for LightBlog sites being upgraded from version<br />
0.9.3 to 0.9.4. You will need to update your LightBlog site's database <br />
before you can continue to use your site. Click the Update button below to <br />
start the process. Please note that if you have a lot of content on your site<br />
it will take more time, so please be patient. DO NOT click the back button<br />
or close your browser before the update is complete.</p>
<p><strong>Note:</strong> This process can take a few minutes.</p>
<form action="<?php echo basename($_SERVER['SCRIPT_FILENAME']); ?>" method="post">
<div>
<input type="submit" name="update" value="Update Database" onclick="setTimeout('this.disabled=true', 250);" />
</div>
</form>
<br /><span id="error"><?php if(!isset($error)){$error=null;}echo $error; ?></span>
<?php endif; if($page == 'finish'): ?>
<h2>You're done!</h2>
<p>Click the Continue button to go on to your updated blog! :)</p>
<p>A backup copy of your previous database was kept and may be deleted (containing "backup-" in the name) if everything was properly copied.</p>
<button onclick="window.location='<?php echo baseurl() ?>'">Continue</button>
<?php endif; ?>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
</body>
</html>