forked from punbb/punbb
/
db_update.php
2240 lines (1842 loc) · 69.4 KB
/
db_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
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Database updating script.
*
* Updates the database to the latest version.
*
* @copyright (C) 2008-2012 PunBB, partially based on code (C) 2008-2009 FluxBB.org
* @license http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
* @package PunBB
*/
define('UPDATE_TO', '1.4.4');
define('UPDATE_TO_DB_REVISION', 5);
// The number of items to process per pageview (lower this if the update script times out during UTF-8 conversion)
define('PER_PAGE', 300);
define('MIN_MYSQL_VERSION', '4.1.2');
// Make sure we are running at least PHP 5.3.0
if (!function_exists('version_compare') || version_compare(PHP_VERSION, '5.3.0', '<'))
exit('You are running PHP version '.PHP_VERSION.'. '.UPDATE_TO.' requires at least PHP 5.3.0 to run properly. You must upgrade your PHP installation before you can continue.');
define('FORUM_ROOT', '../');
require FORUM_ROOT.'include/constants.php';
// Attempt to load the configuration file config.php
if (file_exists(FORUM_ROOT.'config.php'))
include FORUM_ROOT.'config.php';
if (defined('PUN'))
define('FORUM', 1);
// If FORUM isn't defined, config.php is missing or corrupt or we are outside the root directory
if (!defined('FORUM'))
exit('Cannot find config.php, are you sure it exists?');
// Enable debug mode
if (!defined('FORUM_DEBUG'))
define('FORUM_DEBUG', 1);
// Turn on full PHP error reporting
error_reporting(E_ALL);
// Turn off magic_quotes_runtime
if (version_compare(PHP_VERSION, '7.4.0', '<') && get_magic_quotes_runtime())
@ini_set('magic_quotes_runtime', false);
// Turn off PHP time limit
@set_time_limit(0);
// If a cookie name is not specified in config.php, we use the default (forum_cookie)
if (empty($cookie_name))
$cookie_name = 'forum_cookie';
// If the cache directory is not specified, we use the default setting
if (!defined('FORUM_CACHE_DIR'))
define('FORUM_CACHE_DIR', FORUM_ROOT.'cache/');
// Load the functions script
require FORUM_ROOT.'include/functions.php';
// Load UTF-8 functions
require FORUM_ROOT.'include/utf8/utf8.php';
require FORUM_ROOT.'include/utf8/ucwords.php';
require FORUM_ROOT.'include/utf8/trim.php';
// Strip out "bad" UTF-8 characters
forum_remove_bad_characters();
// If the request_uri is invalid try fix it
if (!defined('FORUM_IGNORE_REQUEST_URI'))
forum_fix_request_uri();
// Instruct DB abstraction layer that we don't want it to "SET NAMES". If we need to, we'll do it ourselves below.
define('FORUM_NO_SET_NAMES', 1);
// Load DB abstraction layer and try to connect
require FORUM_ROOT.'include/dblayer/common_db.php';
// Start a transaction
$forum_db->start_transaction();
// Check current version
$query = array(
'SELECT' => 'conf_value',
'FROM' => 'config',
'WHERE' => 'conf_name = \'o_cur_version\''
);
$result = $forum_db->query_build($query);
$cur_version = $forum_db->result($result);
if (version_compare($cur_version, '1.2', '<'))
error('Version mismatch. The database \''.$db_name.'\' doesn\'t seem to be running a PunBB database schema supported by this update script.', __FILE__, __LINE__);
// If we've already done charset conversion in a previous update, we have to do SET NAMES
$forum_db->set_names(version_compare($cur_version, '1.3', '>=') ? 'utf8' : 'latin1');
// If MySQL, make sure it's at least 4.1.2
if (in_array($db_type, array('mysql', 'mysqli', 'mysql_innodb', 'mysqli_innodb')))
{
$mysql_info = $forum_db->get_version();
if (version_compare($mysql_info['version'], MIN_MYSQL_VERSION, '<'))
error('You are running MySQL version '.$mysql_version.'. PunBB '.UPDATE_TO.' requires at least MySQL '.MIN_MYSQL_VERSION.' to run properly. You must upgrade your MySQL installation before you can continue.');
}
// Get the forum config
$query = array(
'SELECT' => '*',
'FROM' => 'config'
);
$result = $forum_db->query_build($query);
while ($cur_config_item = $forum_db->fetch_row($result))
$forum_config[$cur_config_item[0]] = $cur_config_item[1];
// Check the database revision and the current version
if (isset($forum_config['o_database_revision']) && $forum_config['o_database_revision'] >= UPDATE_TO_DB_REVISION && version_compare($forum_config['o_cur_version'], UPDATE_TO, '>='))
error('Your database is already as up-to-date as this script can make it.');
// If $base_url isn't set, use o_base_url from config
if (!isset($base_url))
$base_url = $forum_config['o_base_url'];
// There's no $forum_user, but we need the style element
// We default to Oxygen if the default style is invalid (a 1.2 to 1.3 upgrade most likely)
if (file_exists(FORUM_ROOT.'style/'.$forum_config['o_default_style'].'/'.$forum_config['o_default_style'].'.php'))
$forum_user['style'] = $forum_config['o_default_style'];
else
{
$forum_user['style'] = 'Oxygen';
$query = array(
'UPDATE' => 'config',
'SET' => 'conf_value = \'Oxygen\'',
'WHERE' => 'conf_name = \'o_default_style\''
);
$forum_db->query_build($query) or error(__FILE__, __LINE__);
}
// Make sure the default language exists
// We default to English if the default language is invalid (a 1.2 to 1.3 upgrade most likely)
if (!file_exists(FORUM_ROOT.'lang/'.$forum_config['o_default_lang'].'/common.php'))
{
$query = array(
'UPDATE' => 'config',
'SET' => 'conf_value = \'English\'',
'WHERE' => 'conf_name = \'o_default_lang\''
);
$forum_db->query_build($query) or error(__FILE__, __LINE__);
}
//
// Determines whether $str is UTF-8 encoded or not
//
function seems_utf8($str)
{
$str_len = strlen($str);
for ($i = 0; $i < $str_len; ++$i)
{
if (ord($str[$i]) < 0x80) continue; # 0bbbbbbb
else if ((ord($str[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
else if ((ord($str[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
else if ((ord($str[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb
else if ((ord($str[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb
else if ((ord($str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
else return false; # Does not match any model
for ($j = 0; $j < $n; ++$j) # n bytes matching 10bbbbbb follow ?
{
if ((++$i == strlen($str)) || ((ord($str[$i]) & 0xC0) != 0x80))
return false;
}
}
return true;
}
//
// Translates the number from an HTML numeric entity into an UTF-8 character
//
function dcr2utf8($src)
{
$dest = '';
if ($src < 0)
return false;
else if ($src <= 0x007f)
$dest .= chr($src);
else if ($src <= 0x07ff)
{
$dest .= chr(0xc0 | ($src >> 6));
$dest .= chr(0x80 | ($src & 0x003f));
}
else if ($src == 0xFEFF)
{
// nop -- zap the BOM
}
else if ($src >= 0xD800 && $src <= 0xDFFF)
{
// found a surrogate
return false;
}
else if ($src <= 0xffff)
{
$dest .= chr(0xe0 | ($src >> 12));
$dest .= chr(0x80 | (($src >> 6) & 0x003f));
$dest .= chr(0x80 | ($src & 0x003f));
}
else if ($src <= 0x10ffff)
{
$dest .= chr(0xf0 | ($src >> 18));
$dest .= chr(0x80 | (($src >> 12) & 0x3f));
$dest .= chr(0x80 | (($src >> 6) & 0x3f));
$dest .= chr(0x80 | ($src & 0x3f));
}
else
{
// out of range
return false;
}
return $dest;
}
//
// Attemts to convert $str from $old_charset to UTF-8. Also converts HTML entities (including numeric entities) to UTF-8 characters.
//
function convert_to_utf8(&$str, $old_charset)
{
if ($str == '')
return false;
$save = $str;
// Replace literal entities (for non-UTF-8 compliant html_entity_encode)
if (version_compare(PHP_VERSION, '5.0.0', '<') && $old_charset == 'ISO-8859-1' || $old_charset == 'ISO-8859-15')
$str = html_entity_decode($str, ENT_QUOTES, $old_charset);
if (!seems_utf8($str))
{
if ($old_charset == 'ISO-8859-1')
$str = utf8_encode($str);
else if (function_exists('iconv'))
$str = iconv($old_charset, 'UTF-8', $str);
else if (function_exists('mb_convert_encoding'))
$str = mb_convert_encoding($str, 'UTF-8', $old_charset);
}
// Replace literal entities (for UTF-8 compliant html_entity_encode)
if (version_compare(PHP_VERSION, '5.0.0', '>='))
$str = html_entity_decode($str, ENT_QUOTES, 'UTF-8');
// Replace numeric entities
$str = preg_replace_callback('/&#([0-9]+);/', 'utf8_callback_1', $str);
$str = preg_replace_callback('/&#x([a-f0-9]+);/i', 'utf8_callback_2', $str);
return ($save != $str);
}
function utf8_callback_1($matches)
{
return dcr2utf8($matches[1]);
}
function utf8_callback_2($matches)
{
return dcr2utf8(hexdec($matches[1]));
}
//
// Tries to determine whether post data in the database is UTF-8 encoded or not
//
function db_seems_utf8()
{
global $db_type, $forum_db;
$seems_utf8 = true;
$query = array(
'SELECT' => 'MIN(id), MAX(id), COUNT(id)',
'FROM' => 'posts'
);
$result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
list($min_id, $max_id, $count_id) = $forum_db->fetch_row($result);
if ($count_id == 0)
return false;
// Get a random soup of data and check if it appears to be UTF-8
for ($i = 0; $i < 100; ++$i)
{
$id = ($i == 0) ? $min_id : (($i == 1) ? $max_id : rand($min_id, $max_id));
$query = array(
'SELECT' => 'p.message, p.poster, t.subject, f.forum_name',
'FROM' => 'posts AS p',
'JOINS' => array(
array(
'INNER JOIN' => 'topics AS t',
'ON' => 't.id = p.topic_id'
),
array(
'INNER JOIN' => 'forums AS f',
'ON' => 'f.id = t.forum_id'
)
),
'WHERE' => 'p.id >= '.$id,
'LIMIT' => '1'
);
$result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
$random_row = $forum_db->fetch_row($result);
if (!seems_utf8($random_row[0].$random_row[1].$random_row[2].$random_row[3]))
{
$seems_utf8 = false;
break;
}
}
return $seems_utf8;
}
//
// Safely converts text type columns into utf8 (MySQL only)
// Function based on update_convert_table_utf8() from the Drupal project (http://drupal.org/)
//
function convert_table_utf8($table)
{
global $forum_db;
$types = array(
'char' => 'binary',
'varchar' => 'varbinary',
'tinytext' => 'tinyblob',
'mediumtext' => 'mediumblob',
'text' => 'blob',
'longtext' => 'longblob'
);
// Set table default charset to utf8
$forum_db->query('ALTER TABLE `'.$table.'` CHARACTER SET utf8') or error(__FILE__, __LINE__);
// Find out which columns need converting and build SQL statements
$result = $forum_db->query('SHOW FULL COLUMNS FROM `'.$table.'`') or error(__FILE__, __LINE__);
while ($cur_column = $forum_db->fetch_assoc($result))
{
list($type) = explode('(', $cur_column['Type']);
if (isset($types[$type]) && strpos($cur_column['Collation'], 'utf8') === false)
{
$allow_null = ($cur_column['Null'] == 'YES');
$forum_db->alter_field($table, $cur_column['Field'], preg_replace('/'.$type.'/i', $types[$type], $cur_column['Type']), $allow_null, $cur_column['Default']);
$forum_db->alter_field($table, $cur_column['Field'], $cur_column['Type'].' CHARACTER SET utf8', $allow_null, $cur_column['Default']);
}
}
}
// Move avatars to DB
function convert_avatars()
{
global $forum_config, $forum_db;
$avatar_dir = FORUM_ROOT.'img/avatars/';
if (!is_dir($avatar_dir))
{
return false;
}
if ($handle = opendir($avatar_dir))
{
while (false !== ($avatar = readdir($handle)))
{
$avatar_file = $avatar_dir.$avatar;
if (!is_file($avatar_file))
{
continue;
}
//echo $avatar_file;
$avatar = basename($avatar_file);
if (preg_match('/^(\d+)\.(png|gif|jpg)/', $avatar, $matches))
{
$user_id = intval($matches[1], 10);
$avatar_ext = $matches[2];
$avatar_type = FORUM_AVATAR_NONE;
if ($avatar_ext == 'png')
{
$avatar_type = FORUM_AVATAR_PNG;
}
else if ($avatar_ext == 'gif')
{
$avatar_type = FORUM_AVATAR_GIF;
}
else if ($avatar_ext == 'jpg')
{
$avatar_type = FORUM_AVATAR_JPG;
}
// Check user and avatar type
if ($user_id < 2 || $avatar_type == FORUM_AVATAR_NONE)
{
continue;
}
// Now check the width/height
list($width, $height, $type,) = @/**/getimagesize($avatar_file);
if (empty($width) || empty($height) || $width > $forum_config['o_avatars_width'] || $height > $forum_config['o_avatars_height'])
{
@/**/unlink($avatar_file);
}
else
{
// Save to DB
$query = array(
'UPDATE' => 'users',
'SET' => 'avatar=\''.$avatar_type.'\', avatar_height=\''.$height.'\', avatar_width=\''.$width.'\'',
'WHERE' => 'id='.$user_id
);
$forum_db->query_build($query) or error(__FILE__, __LINE__);
}
}
}
closedir($handle);
}
}
header('Content-type: text/html; charset=utf-8');
// Empty all output buffers and stop buffering
while (@ob_end_clean());
$stage = isset($_GET['stage']) ? $_GET['stage'] : '';
$old_charset = isset($_GET['req_old_charset']) ? str_replace('ISO8859', 'ISO-8859', strtoupper($_GET['req_old_charset'])) : 'ISO-8859-1';
$start_at = isset($_GET['start_at']) ? intval($_GET['start_at']) : 0;
$query_str = '';
switch ($stage)
{
// Show form
case '':
$db_seems_utf8 = db_seems_utf8();
?>
<!DOCTYPE html>
<!--[if lt IE 7 ]> <html class="oldie ie6" lang="en" dir="ltr"> <![endif]-->
<!--[if IE 7 ]> <html class="oldie ie7" lang="en" dir="ltr"> <![endif]-->
<!--[if IE 8 ]> <html class="oldie ie8" lang="en" dir="ltr"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en" dir="ltr"> <!--<![endif]-->
<head>
<meta charset="utf-8" />
<title>PunBB Database Update</title>
<link rel="stylesheet" type="text/css" href="<?php echo $base_url ?>/style/Oxygen/Oxygen.min.css" />
<script type="text/javascript" src="<?php echo $base_url ?>/include/js/min/punbb.common.min.js"></script>
</head>
<body>
<div id="brd-update" class="brd-page">
<div id="brd-wrap" class="brd">
<div id="brd-head" class="gen-content">
<p id="brd-title"><strong>PunBB Database Update</strong></p>
<p id="brd-desc">Update database tables of current installation</p>
</div>
<div id="brd-main" class="main basic">
<div class="main-head">
<h1 class="hn"><span>PunBB Database Update: Perform update of database tables</span></h1>
</div>
<div class="main-content frm">
<div class="ct-box info-box">
<ul class="spaced">
<li class="warn"><span><strong>WARNING!</strong> This script will update your PunBB forum database. The update procedure might take anything from a few seconds to a few minutes (or in extreme cases, hours) depending on the speed of the server, the size of the forum database and the number of changes required.</span></li>
<li><span>Do not forget to make a backup of the database before continuing.</span></li>
<li><span>Did you read the update instructions in the documentation? If not, start there.</span></li>
<?php
if (strpos($cur_version, '1.2') === 0 && (!$db_seems_utf8 || isset($_GET['force'])))
{
if (!function_exists('iconv') && !function_exists('mb_convert_encoding'))
{
?>
<li class="important"><strong>IMPORTANT!</strong> PunBB has detected that this PHP environment does not have support for the encoding mechanisms required to do UTF-8 conversion from character sets other than ISO-8859-1. What this means is that if the current character set is not ISO-8859-1, PunBB won't be able to convert your forum database to UTF-8 and you will have to do it manually. Instructions for doing manual charset conversion can be found in the update instructions.</span></li>
<?php
}
}
$current_url = get_current_url();
if (strpos($cur_version, '1.2') === 0 && $db_seems_utf8 && !isset($_GET['force']))
{
?>
<li class="important"><span><strong>IMPORTANT!</strong> Based on a random selection of 100 posts, topic subjects, usernames and forum names from the database, it appears as if text in the database is currently UTF-8 encoded. This is a good thing. Based on this, the update process will not attempt to do charset conversion. If you have reason to believe that the charset conversion is required nonetheless, you can <a href="<?php echo $current_url.((substr_count($current_url, '?') == 1) ? '&' : '?').'force=1' ?>">force the conversion to run</a>.</span></li>
<?php
}
?>
</ul>
</div>
<form class="frm-form" method="get" accept-charset="utf-8" action="<?php echo $current_url ?>">
<div class="hidden">
<input type="hidden" name="stage" value="start" />
</div>
<?php
if (strpos($cur_version, '1.2') === 0 && (!$db_seems_utf8 || isset($_GET['force'])))
{
?>
<div class="ct-box info-box">
<p class="important"><strong>Enable conversion:</strong> When enabled this update script will, after it has made the required structural changes to the database, convert all text in the database from the current character set to UTF-8. This conversion is required if you're upgrading from PunBB 1.2 and you are not currently using an UTF-8 language pack.</p>
<p class="important"><strong>Current character set:</strong> If the primary language in your forum is English, you can leave this at the default value. However, if your forum is non-English, you should enter the character set of the primary language pack used in the forum.</p>
</div>
<div id="req-msg" class="req-warn ct-box error-box">
<p class="important"><strong>Important!</strong> All fields labelled <em>(Required)</em> must be completed before submitting this form.</p>
</div>
<fieldset class="frm-group group1">
<legend class="group-legend"><span>Charset conversion</span></legend>
<div class="sf-set set1">
<div class="sf-box checkbox">
<span class="fld-input"><input type="checkbox" id="fld1" name="convert_charset" value="1" checked="checked" /></span>
<label for="fld1"><span>Enable conversion:</span> Perform database charset conversion.</label>
</div>
</div>
<div class="sf-set set2">
<div class="sf-box text required">
<label for="fld2"><span>Current character set: <em>(Required)</em></span> <small>Accept default for English forums otherwise the character set of the primary langauge pack.</small></label><br />
<span class="fld-input"><input type="text" id="fld2" name="req_old_charset" size="12" maxlength="20" value="ISO-8859-1" /></span>
</div>
</div>
</fieldset>
<?php
}
?>
<div class="frm-buttons">
<span class="submit primary"><input type="submit" name="start" value="Start update" /></span>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
<?php
break;
// Start by updating the database structure
case 'start':
// Put back dropped search tables
if (!$forum_db->table_exists('search_cache') && in_array($db_type, array('mysql', 'mysqli', 'mysql_innodb', 'mysqli_innodb')))
{
$schema = array(
'FIELDS' => array(
'id' => array(
'datatype' => 'INT(10) UNSIGNED',
'allow_null' => false,
'default' => '0'
),
'ident' => array(
'datatype' => 'VARCHAR(200)',
'allow_null' => false,
'default' => '\'\''
),
'search_data' => array(
'datatype' => 'TEXT',
'allow_null' => true
)
),
'PRIMARY KEY' => array('id'),
'INDEXES' => array(
'ident_idx' => array('ident(8)')
)
);
$forum_db->create_table('search_cache', $schema);
$schema = array(
'FIELDS' => array(
'post_id' => array(
'datatype' => 'INT(10) UNSIGNED',
'allow_null' => false,
'default' => '0'
),
'word_id' => array(
'datatype' => 'INT(10) UNSIGNED',
'allow_null' => false,
'default' => '0'
),
'subject_match' => array(
'datatype' => 'TINYINT(1)',
'allow_null' => false,
'default' => '0'
)
),
'INDEXES' => array(
'word_id_idx' => array('word_id'),
'post_id_idx' => array('post_id')
)
);
$forum_db->create_table('search_matches', $schema);
$schema = array(
'FIELDS' => array(
'id' => array(
'datatype' => 'SERIAL',
'allow_null' => false
),
'word' => array(
'datatype' => 'VARCHAR(20)',
'allow_null' => false,
'default' => '\'\'',
'collation' => 'bin'
)
),
'PRIMARY KEY' => array('word'),
'INDEXES' => array(
'id_idx' => array('id')
)
);
$forum_db->create_table('search_words', $schema);
}
// Add the extensions table if it doesn't already exist
if (!$forum_db->table_exists('extensions'))
{
$schema = array(
'FIELDS' => array(
'id' => array(
'datatype' => 'VARCHAR(150)',
'allow_null' => false,
'default' => '\'\''
),
'title' => array(
'datatype' => 'VARCHAR(255)',
'allow_null' => false,
'default' => '\'\''
),
'version' => array(
'datatype' => 'VARCHAR(25)',
'allow_null' => false,
'default' => '\'\''
),
'description' => array(
'datatype' => 'TEXT',
'allow_null' => true
),
'author' => array(
'datatype' => 'VARCHAR(50)',
'allow_null' => false,
'default' => '\'\''
),
'uninstall' => array(
'datatype' => 'TEXT',
'allow_null' => true
),
'uninstall_note' => array(
'datatype' => 'TEXT',
'allow_null' => true
),
'disabled' => array(
'datatype' => 'TINYINT(1)',
'allow_null' => false,
'default' => '0'
),
'dependencies' => array(
'datatype' => 'VARCHAR(255)',
'allow_null' => false,
'default' => '\'\''
)
),
'PRIMARY KEY' => array('id')
);
$forum_db->create_table('extensions', $schema);
}
// Make sure the collation on "word" in the search_words table is utf8_bin
if (in_array($db_type, array('mysql', 'mysqli', 'mysql_innodb', 'mysqli_innodb')))
{
$result = $forum_db->query('SHOW FULL COLUMNS FROM '.$forum_db->prefix.'search_words') or error(__FILE__, __LINE__);
while ($cur_column = $forum_db->fetch_assoc($result))
{
if ($cur_column['Field'] === 'word')
{
if ($cur_column['Collation'] !== 'utf8_bin')
$forum_db->alter_field('search_words', 'word', 'VARCHAR(20) CHARACTER SET utf8 COLLATE utf8_bin', false, '');
break;
}
}
}
// Add uninstall_note field to extensions
$forum_db->add_field('extensions', 'uninstall_note', 'TEXT', true, null, 'uninstall');
// Drop uninstall_notes (plural) field
$forum_db->drop_field('extensions', 'uninstall_notes');
// Add disabled field to extensions
$forum_db->add_field('extensions', 'disabled', 'TINYINT(1)', false, 0, 'uninstall_note');
// Add dependencies field to extensions
$forum_db->add_field('extensions', 'dependencies', 'VARCHAR(255)', false, '', 'disabled');
// Add the extension_hooks table
if (!$forum_db->table_exists('extension_hooks'))
{
$schema = array(
'FIELDS' => array(
'id' => array(
'datatype' => 'VARCHAR(150)',
'allow_null' => false,
'default' => '\'\''
),
'extension_id' => array(
'datatype' => 'VARCHAR(50)',
'allow_null' => false,
'default' => '\'\''
),
'code' => array(
'datatype' => 'TEXT',
'allow_null' => true
),
'installed' => array(
'datatype' => 'INT(10) UNSIGNED',
'allow_null' => false,
'default' => '0'
),
'priority' => array(
'datatype' => 'TINYINT(1) UNSIGNED',
'allow_null' => false,
'default' => '5'
)
),
'PRIMARY KEY' => array('id', 'extension_id')
);
$forum_db->create_table('extension_hooks', $schema);
}
// Add priority field to extension_hooks
$forum_db->add_field('extension_hooks', 'priority', 'TINYINT(1)', false, 5, 'installed');
// Extend id field in extension_hooks to 150
$forum_db->alter_field('extension_hooks', 'id', 'VARCHAR(150)', false, '');
// Add the subscriptions forum table if it doesn't already exist
if (!$forum_db->table_exists('forum_subscriptions'))
{
$schema = array(
'FIELDS' => array(
'user_id' => array(
'datatype' => 'INT(10) UNSIGNED',
'allow_null' => false,
'default' => '0'
),
'forum_id' => array(
'datatype' => 'INT(10) UNSIGNED',
'allow_null' => false,
'default' => '0'
)
),
'PRIMARY KEY' => array('user_id', 'forum_id')
);
$forum_db->create_table('forum_subscriptions', $schema);
}
// Make all e-mail fields VARCHAR(80)
$forum_db->alter_field('bans', 'email', 'VARCHAR(80)', true);
$forum_db->alter_field('posts', 'poster_email', 'VARCHAR(80)', true);
$forum_db->alter_field('users', 'email', 'VARCHAR(80)', false, '');
$forum_db->alter_field('users', 'jabber', 'VARCHAR(80)', true);
$forum_db->alter_field('users', 'msn', 'VARCHAR(80)', true);
$forum_db->alter_field('users', 'activate_string', 'VARCHAR(80)', true);
// Add avatars field
$forum_db->add_field('users', 'avatar', 'TINYINT(3) UNSIGNED', false, 0);
$forum_db->add_field('users', 'avatar_width', 'TINYINT(3) UNSIGNED', false, 0, 'avatar');
$forum_db->add_field('users', 'avatar_height', 'TINYINT(3) UNSIGNED', false, 0, 'avatar_width');
// Add new profile fileds
$forum_db->add_field('users', 'facebook', 'VARCHAR(100)', true, null, 'url');
$forum_db->add_field('users', 'twitter', 'VARCHAR(100)', true, null, 'facebook');
$forum_db->add_field('users', 'linkedin', 'VARCHAR(100)', true, null, 'twitter');
$forum_db->add_field('users', 'skype', 'VARCHAR(100)', true, null, 'linkedin');
// Add avatars to DB
convert_avatars();
// Remove NOT NULL from TEXT fields for consistency. See http://dev.punbb.org/changeset/596
$forum_db->alter_field('posts', 'message', 'TEXT', true);
$forum_db->alter_field('reports', 'message', 'TEXT', true);
// Drop fulltext indexes (should only apply to SVN installs)
if (in_array($db_type, array('mysql', 'mysqli', 'mysql_innodb', 'mysqli_innodb')))
{
$forum_db->drop_index('topics', 'subject_idx');
$forum_db->drop_index('posts', 'message_idx');
}
// Make all IP fields VARCHAR(39) to support IPv6
$forum_db->alter_field('posts', 'poster_ip', 'VARCHAR(39)', true);
$forum_db->alter_field('users', 'registration_ip', 'VARCHAR(39)', false, '0.0.0.0');
// Add the DST option to the users table
$forum_db->add_field('users', 'dst', 'TINYINT(1)', false, 0, 'timezone');
// Add the salt field to the users table
$forum_db->add_field('users', 'salt', 'VARCHAR(12)', true, null, 'password');
// Add the access_keys field to the users table
$forum_db->add_field('users', 'access_keys', 'TINYINT(1)', false, 0, 'show_sig');
// Add the CSRF token field to the online table
$forum_db->add_field('online', 'csrf_token', 'VARCHAR(40)', false, '', null);
// Add the prev_url field to the online table
$forum_db->add_field('online', 'prev_url', 'VARCHAR(255)', true, null, null);
// Add the last_post field to the online table
$forum_db->add_field('online', 'last_post', 'INT(10) UNSIGNED', true, null, null);
// Add the last_search field to the online table
$forum_db->add_field('online', 'last_search', 'INT(10) UNSIGNED', true, null, null);
// Drop use_avatar column from users table
$forum_db->drop_field('users', 'use_avatar');
// Drop save_pass column from users table
$forum_db->drop_field('users', 'save_pass');
// Drop g_edit_subjects_interval column from groups table
$forum_db->drop_field('groups', 'g_edit_subjects_interval');
$new_config = array();
// Add quote depth option
if (!array_key_exists('o_quote_depth', $forum_config))
$new_config[] = '\'o_quote_depth\', \'3\'';
// Add database revision number
if (!array_key_exists('o_database_revision', $forum_config))
$new_config[] = '\'o_database_revision\', \'0\'';
// Add default email setting option
if (!array_key_exists('o_default_email_setting', $forum_config))
$new_config[] = '\'o_default_email_setting\', \'1\'';
// Make sure we have o_additional_navlinks (was added in 1.2.1)
if (!array_key_exists('o_additional_navlinks', $forum_config))
$new_config[] = '\'o_additional_navlinks\', \'\'';
// Insert new config options o_sef
if (!array_key_exists('o_sef', $forum_config))
$new_config[] = '\'o_sef\', \'Default\'';
// Insert new config option o_topic_views
if (!array_key_exists('o_topic_views', $forum_config))
$new_config[] = '\'o_topic_views\', \'1\'';
// Insert new config option o_signatures
if (!array_key_exists('o_signatures', $forum_config))
$new_config[] = '\'o_signatures\', \'1\'';
// Insert new config option o_smtp_ssl
if (!array_key_exists('o_smtp_ssl', $forum_config))
$new_config[] = '\'o_smtp_ssl\', \'0\'';
// Insert new config option o_check_for_updates
if (!array_key_exists('o_check_for_updates', $forum_config))
{
$check_for_updates = (function_exists('curl_init') || function_exists('fsockopen') || in_array(strtolower(@ini_get('allow_url_fopen')), array('on', 'true', '1'))) ? 1 : 0;
$new_config[] = '\'o_check_for_updates\', \''.$check_for_updates.'\'';
}
// Insert new config option o_check_for_version
if (!array_key_exists('o_check_for_versions', $forum_config))
{
$o_check_for_versions = array_key_exists('o_check_for_updates', $forum_config) ? $forum_config['o_check_for_updates'] : $check_for_updates;
$new_config[] = '\'o_check_for_versions\', \''.$o_check_for_versions.'\'';
}
// Insert new config option o_announcement_heading
if (!array_key_exists('o_announcement_heading', $forum_config))
$new_config[] = '\'o_announcement_heading\', \'\'';
// Insert new config option o_default_dst
if (!array_key_exists('o_default_dst', $forum_config))
$new_config[] = '\'o_default_dst\', \'0\'';
// Insert new config option o_show_moderators
if (!array_key_exists('o_show_moderators', $forum_config))
$new_config[] = '\'o_show_moderators\', \'0\'';
// Insert new config option o_show_moderators
if (!array_key_exists('o_mask_passwords', $forum_config))
$new_config[] = '\'o_mask_passwords\', \'1\'';
if (!empty($new_config))
{
$query = array(
'INSERT' => 'conf_name, conf_value',
'INTO' => 'config',
'VALUES' => $new_config
);
$forum_db->query_build($query) or error(__FILE__, __LINE__);
}
unset($new_config);
// Server timezone is now simply the default timezone
if (!array_key_exists('o_default_timezone', $forum_config))
{
$query = array(
'UPDATE' => 'config',
'SET' => 'conf_name = \'o_default_timezone\'',
'WHERE' => 'conf_name = \'o_server_timezone\''
);
$forum_db->query_build($query) or error(__FILE__, __LINE__);
}
// Increase visit timeout to 30 minutes (only if it hasn't been changed from the default)
if ($forum_config['o_timeout_visit'] == '600')
{
$query = array(
'UPDATE' => 'config',
'SET' => 'conf_value = \'1800\'',
'WHERE' => 'conf_name = \'o_timeout_visit\''
);
$forum_db->query_build($query) or error(__FILE__, __LINE__);
}
// Update redirect timeout
if (version_compare($cur_version, '1.4', '<') && $forum_config['o_redirect_delay'] == '1')
{
$query = array(
'UPDATE' => 'config',
'SET' => 'conf_value = \'0\'',
'WHERE' => 'conf_name = \'o_redirect_delay\''
);
$forum_db->query_build($query) or error(__FILE__, __LINE__);
}
// Remove obsolete g_post_polls permission from groups table
$forum_db->drop_field('groups', 'g_post_polls');
// Make room for multiple moderator groups
if (!$forum_db->field_exists('groups', 'g_moderator'))
{
// Add g_moderator column to groups table
$forum_db->add_field('groups', 'g_moderator', 'TINYINT(1)', false, 0, 'g_user_title');
// Give the moderator group moderator privileges
$query = array(
'UPDATE' => 'groups',