(a little test for later merges)
[phpbb.git] / phpBB / install / convertors / functions_phpbb20.php
blobb80c7673e3c99fc9bbf40d0728cbe03443a9a6d9
1 <?php
2 /**
4 * @package install
5 * @version $Id$
6 * @copyright (c) 2006 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 if (!defined('IN_PHPBB'))
13 exit;
16 /**
17 * Helper functions for phpBB 2.0.x to phpBB 3.0.x conversion
20 /**
21 * Set forum flags - only prune old polls by default
23 function phpbb_forum_flags()
25 // Set forum flags
26 $forum_flags = 0;
28 // FORUM_FLAG_LINK_TRACK
29 $forum_flags += 0;
31 // FORUM_FLAG_PRUNE_POLL
32 $forum_flags += FORUM_FLAG_PRUNE_POLL;
34 // FORUM_FLAG_PRUNE_ANNOUNCE
35 $forum_flags += 0;
37 // FORUM_FLAG_PRUNE_STICKY
38 $forum_flags += 0;
40 // FORUM_FLAG_ACTIVE_TOPICS
41 $forum_flags += 0;
43 // FORUM_FLAG_POST_REVIEW
44 $forum_flags += FORUM_FLAG_POST_REVIEW;
46 return $forum_flags;
49 /**
50 * Insert/Convert forums
52 function phpbb_insert_forums()
54 global $db, $src_db, $same_db, $convert, $user, $config;
56 $db->sql_query($convert->truncate_statement . FORUMS_TABLE);
58 // Determine the highest id used within the old forums table (we add the categories after the forum ids)
59 $sql = 'SELECT MAX(forum_id) AS max_forum_id
60 FROM ' . $convert->src_table_prefix . 'forums';
61 $result = $src_db->sql_query($sql);
62 $max_forum_id = (int) $src_db->sql_fetchfield('max_forum_id');
63 $src_db->sql_freeresult($result);
65 $max_forum_id++;
67 // pruning disabled globally?
68 $sql = "SELECT config_value
69 FROM {$convert->src_table_prefix}config
70 WHERE config_name = 'prune_enable'";
71 $result = $src_db->sql_query($sql);
72 $prune_enabled = (int) $src_db->sql_fetchfield('config_value');
73 $src_db->sql_freeresult($result);
76 // Insert categories
77 $sql = 'SELECT cat_id, cat_title
78 FROM ' . $convert->src_table_prefix . 'categories
79 ORDER BY cat_order';
81 if ($convert->mysql_convert && $same_db)
83 $src_db->sql_query("SET NAMES 'binary'");
86 $result = $src_db->sql_query($sql);
88 if ($convert->mysql_convert && $same_db)
90 $src_db->sql_query("SET NAMES 'utf8'");
93 switch ($db->sql_layer)
95 case 'mssql':
96 case 'mssql_odbc':
97 $db->sql_query('SET IDENTITY_INSERT ' . FORUMS_TABLE . ' ON');
98 break;
101 $cats_added = array();
102 while ($row = $src_db->sql_fetchrow($result))
104 $sql_ary = array(
105 'forum_id' => (int) $max_forum_id,
106 'forum_name' => ($row['cat_title']) ? htmlspecialchars(phpbb_set_default_encoding($row['cat_title']), ENT_COMPAT, 'UTF-8') : $user->lang['CATEGORY'],
107 'parent_id' => 0,
108 'forum_parents' => '',
109 'forum_desc' => '',
110 'forum_type' => FORUM_CAT,
111 'forum_status' => ITEM_UNLOCKED,
112 'forum_rules' => '',
115 $sql = 'SELECT MAX(right_id) AS right_id
116 FROM ' . FORUMS_TABLE;
117 $_result = $db->sql_query($sql);
118 $cat_row = $db->sql_fetchrow($_result);
119 $db->sql_freeresult($_result);
121 $sql_ary['left_id'] = (int) ($cat_row['right_id'] + 1);
122 $sql_ary['right_id'] = (int) ($cat_row['right_id'] + 2);
124 $sql = 'INSERT INTO ' . FORUMS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
125 $db->sql_query($sql);
127 $cats_added[$row['cat_id']] = $max_forum_id;
128 $max_forum_id++;
130 $src_db->sql_freeresult($result);
132 // There may be installations having forums with non-existant category ids.
133 // We try to catch them and add them to an "unknown" category instead of leaving them out.
134 $sql = 'SELECT cat_id
135 FROM ' . $convert->src_table_prefix . 'forums
136 GROUP BY cat_id';
137 $result = $src_db->sql_query($sql);
139 $unknown_cat_id = false;
140 while ($row = $src_db->sql_fetchrow($result))
142 // Catch those categories not been added before
143 if (!isset($cats_added[$row['cat_id']]))
145 $unknown_cat_id = true;
148 $src_db->sql_freeresult($result);
150 // Is there at least one category not known?
151 if ($unknown_cat_id === true)
153 $unknown_cat_id = 'ghost';
155 $sql_ary = array(
156 'forum_id' => (int) $max_forum_id,
157 'forum_name' => (string) $user->lang['CATEGORY'],
158 'parent_id' => 0,
159 'forum_parents' => '',
160 'forum_desc' => '',
161 'forum_type' => FORUM_CAT,
162 'forum_status' => ITEM_UNLOCKED,
163 'forum_rules' => '',
166 $sql = 'SELECT MAX(right_id) AS right_id
167 FROM ' . FORUMS_TABLE;
168 $_result = $db->sql_query($sql);
169 $cat_row = $db->sql_fetchrow($_result);
170 $db->sql_freeresult($_result);
172 $sql_ary['left_id'] = (int) ($cat_row['right_id'] + 1);
173 $sql_ary['right_id'] = (int) ($cat_row['right_id'] + 2);
175 $sql = 'INSERT INTO ' . FORUMS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
176 $db->sql_query($sql);
178 $cats_added[$unknown_cat_id] = $max_forum_id;
179 $max_forum_id++;
182 // Now insert the forums
183 $sql = 'SELECT f.forum_id, f.forum_name, f.cat_id, f.forum_desc, f.forum_status, f.prune_enable, f.prune_next, fp.prune_days, fp.prune_freq FROM ' . $convert->src_table_prefix . 'forums f
184 LEFT JOIN ' . $convert->src_table_prefix . 'forum_prune fp ON f.forum_id = fp.forum_id
185 GROUP BY f.forum_id, f.forum_name, f.cat_id, f.forum_desc, f.forum_status, f.prune_enable, f.prune_next, f.forum_order, fp.prune_days, fp.prune_freq
186 ORDER BY f.cat_id, f.forum_order';
188 if ($convert->mysql_convert && $same_db)
190 $src_db->sql_query("SET NAMES 'binary'");
193 $result = $src_db->sql_query($sql);
195 if ($convert->mysql_convert && $same_db)
197 $src_db->sql_query("SET NAMES 'utf8'");
200 while ($row = $src_db->sql_fetchrow($result))
202 // Some might have forums here with an id not being "possible"...
203 // To be somewhat friendly we "change" the category id for those to a previously created ghost category
204 if (!isset($cats_added[$row['cat_id']]) && $unknown_cat_id !== false)
206 $row['cat_id'] = $unknown_cat_id;
209 if (!isset($cats_added[$row['cat_id']]))
211 continue;
214 // Define the new forums sql ary
215 $sql_ary = array(
216 'forum_id' => (int) $row['forum_id'],
217 'forum_name' => htmlspecialchars(phpbb_set_default_encoding($row['forum_name']), ENT_COMPAT, 'UTF-8'),
218 'parent_id' => (int) $cats_added[$row['cat_id']],
219 'forum_parents' => '',
220 'forum_desc' => htmlspecialchars(phpbb_set_default_encoding($row['forum_desc']), ENT_COMPAT, 'UTF-8'),
221 'forum_type' => FORUM_POST,
222 'forum_status' => is_item_locked($row['forum_status']),
223 'enable_prune' => ($prune_enabled) ? (int)$row['prune_enable'] : 0,
224 'prune_next' => (int) null_to_zero($row['prune_next']),
225 'prune_days' => (int) null_to_zero($row['prune_days']),
226 'prune_viewed' => 0,
227 'prune_freq' => (int) null_to_zero($row['prune_freq']),
229 'forum_flags' => phpbb_forum_flags(),
230 'forum_options' => 0,
232 // Default values
233 'forum_desc_bitfield' => '',
234 'forum_desc_options' => 7,
235 'forum_desc_uid' => '',
236 'forum_link' => '',
237 'forum_password' => '',
238 'forum_style' => 0,
239 'forum_image' => '',
240 'forum_rules' => '',
241 'forum_rules_link' => '',
242 'forum_rules_bitfield' => '',
243 'forum_rules_options' => 7,
244 'forum_rules_uid' => '',
245 'forum_topics_per_page' => 0,
246 'forum_posts' => 0,
247 'forum_topics' => 0,
248 'forum_topics_real' => 0,
249 'forum_last_post_id' => 0,
250 'forum_last_poster_id' => 0,
251 'forum_last_post_subject' => '',
252 'forum_last_post_time' => 0,
253 'forum_last_poster_name' => '',
254 'forum_last_poster_colour' => '',
255 'display_on_index' => 1,
256 'enable_indexing' => 1,
257 'enable_icons' => 0,
260 // Now add the forums with proper left/right ids
261 $sql = 'SELECT left_id, right_id
262 FROM ' . FORUMS_TABLE . '
263 WHERE forum_id = ' . $cats_added[$row['cat_id']];
264 $_result = $db->sql_query($sql);
265 $cat_row = $db->sql_fetchrow($_result);
266 $db->sql_freeresult($_result);
268 $sql = 'UPDATE ' . FORUMS_TABLE . '
269 SET left_id = left_id + 2, right_id = right_id + 2
270 WHERE left_id > ' . $cat_row['right_id'];
271 $db->sql_query($sql);
273 $sql = 'UPDATE ' . FORUMS_TABLE . '
274 SET right_id = right_id + 2
275 WHERE ' . $cat_row['left_id'] . ' BETWEEN left_id AND right_id';
276 $db->sql_query($sql);
278 $sql_ary['left_id'] = (int) $cat_row['right_id'];
279 $sql_ary['right_id'] = (int) ($cat_row['right_id'] + 1);
281 $sql = 'INSERT INTO ' . FORUMS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
282 $db->sql_query($sql);
284 $src_db->sql_freeresult($result);
286 switch ($db->sql_layer)
288 case 'postgres':
289 $db->sql_query("SELECT SETVAL('" . FORUMS_TABLE . "_seq',(select case when max(forum_id)>0 then max(forum_id)+1 else 1 end from " . FORUMS_TABLE . '));');
290 break;
292 case 'mssql':
293 case 'mssql_odbc':
294 $db->sql_query('SET IDENTITY_INSERT ' . FORUMS_TABLE . ' OFF');
295 break;
297 case 'oracle':
298 $result = $db->sql_query('SELECT MAX(forum_id) as max_id FROM ' . FORUMS_TABLE);
299 $row = $db->sql_fetchrow($result);
300 $db->sql_freeresult($result);
302 $largest_id = (int) $row['max_id'];
304 if ($largest_id)
306 $db->sql_query('DROP SEQUENCE ' . FORUMS_TABLE . '_seq');
307 $db->sql_query('CREATE SEQUENCE ' . FORUMS_TABLE . '_seq START WITH ' . ($largest_id + 1));
309 break;
314 * Function for recoding text with the default language
316 * @param string $text text to recode to utf8
317 * @param bool $grab_user_lang if set to true the function tries to use $convert_row['user_lang'] (and falls back to $convert_row['poster_id']) instead of the boards default language
319 function phpbb_set_encoding($text, $grab_user_lang = true)
321 global $lang_enc_array, $convert_row;
322 global $convert, $phpEx;
324 /*static $lang_enc_array = array(
325 'korean' => 'euc-kr',
326 'serbian' => 'windows-1250',
327 'polish' => 'iso-8859-2',
328 'kurdish' => 'windows-1254',
329 'slovak' => 'Windows-1250',
330 'russian' => 'windows-1251',
331 'estonian' => 'iso-8859-4',
332 'chinese_simplified' => 'gb2312',
333 'macedonian' => 'windows-1251',
334 'azerbaijani' => 'UTF-8',
335 'romanian' => 'iso-8859-2',
336 'romanian_diacritice' => 'iso-8859-2',
337 'lithuanian' => 'windows-1257',
338 'turkish' => 'iso-8859-9',
339 'ukrainian' => 'windows-1251',
340 'japanese' => 'shift_jis',
341 'hungarian' => 'ISO-8859-2',
342 'romanian_no_diacritics' => 'iso-8859-2',
343 'mongolian' => 'UTF-8',
344 'slovenian' => 'windows-1250',
345 'bosnian' => 'windows-1250',
346 'czech' => 'Windows-1250',
347 'farsi' => 'Windows-1256',
348 'croatian' => 'windows-1250',
349 'greek' => 'iso-8859-7',
350 'russian_tu' => 'windows-1251',
351 'sakha' => 'UTF-8',
352 'serbian_cyrillic' => 'windows-1251',
353 'bulgarian' => 'windows-1251',
354 'chinese_traditional_taiwan' => 'big5',
355 'chinese_traditional' => 'big5',
356 'arabic' => 'windows-1256',
357 'hebrew' => 'WINDOWS-1255',
358 'thai' => 'windows-874',
359 //'chinese_traditional_taiwan' => 'utf-8' // custom modified, we may have to do an include :-(
360 );*/
362 if (empty($lang_enc_array))
364 $lang_enc_array = array();
367 $get_lang = trim(get_config_value('default_lang'));
369 // Do we need the users language encoding?
370 if ($grab_user_lang && !empty($convert_row))
372 if (!empty($convert_row['user_lang']))
374 $get_lang = trim($convert_row['user_lang']);
376 else if (!empty($convert_row['poster_id']))
378 global $src_db, $same_db;
380 if ($convert->mysql_convert && $same_db)
382 $src_db->sql_query("SET NAMES 'binary'");
385 $sql = 'SELECT user_lang
386 FROM ' . $convert->src_table_prefix . 'users
387 WHERE user_id = ' . (int) $convert_row['poster_id'];
388 $result = $src_db->sql_query($sql);
389 $get_lang = (string) $src_db->sql_fetchfield('user_lang');
390 $src_db->sql_freeresult($result);
392 if ($convert->mysql_convert && $same_db)
394 $src_db->sql_query("SET NAMES 'utf8'");
397 $get_lang = (!trim($get_lang)) ? trim(get_config_value('default_lang')) : trim($get_lang);
401 if (!isset($lang_enc_array[$get_lang]))
403 $filename = $convert->options['forum_path'] . '/language/lang_' . $get_lang . '/lang_main.' . $phpEx;
405 if (!file_exists($filename))
407 $get_lang = trim(get_config_value('default_lang'));
410 if (!isset($lang_enc_array[$get_lang]))
412 include($convert->options['forum_path'] . '/language/lang_' . $get_lang . '/lang_main.' . $phpEx);
413 $lang_enc_array[$get_lang] = $lang['ENCODING'];
414 unset($lang);
418 $encoding = $lang_enc_array[$get_lang];
420 return utf8_recode($text, $lang_enc_array[$get_lang]);
424 * Same as phpbb_set_encoding, but forcing boards default language
426 function phpbb_set_default_encoding($text)
428 return phpbb_set_encoding($text, false);
432 * Convert Birthday from Birthday MOD to phpBB Format
434 function phpbb_get_birthday($birthday = '')
436 if (defined('MOD_BIRTHDAY_TERRA'))
438 $birthday = (string) $birthday;
440 // stored as month, day, year
441 if (!$birthday)
443 return ' 0- 0- 0';
446 // We use the original mod code to retrieve the birthday (not ideal)
447 preg_match('/(..)(..)(....)/', sprintf('%08d', $birthday), $birthday_parts);
449 $month = $birthday_parts[1];
450 $day = $birthday_parts[2];
451 $year = $birthday_parts[3];
453 return sprintf('%2d-%2d-%4d', $day, $month, $year);
455 else
457 $birthday = (int) $birthday;
459 if (!$birthday || $birthday == 999999 || ((version_compare(PHP_VERSION, '5.1.0') < 0) && $birthday < 0))
461 return ' 0- 0- 0';
464 // The birthday mod from niels is using this code to transform to day/month/year
465 return sprintf('%2d-%2d-%4d', gmdate('j', $birthday * 86400 + 1), gmdate('n', $birthday * 86400 + 1), gmdate('Y', $birthday * 86400 + 1));
470 * Return correct user id value
471 * Everyone's id will be one higher to allow the guest/anonymous user to have a positive id as well
473 function phpbb_user_id($user_id)
475 global $config;
477 // Increment user id if the old forum is having a user with the id 1
478 if (!isset($config['increment_user_id']))
480 global $src_db, $same_db, $convert;
482 if ($convert->mysql_convert && $same_db)
484 $src_db->sql_query("SET NAMES 'binary'");
487 // Now let us set a temporary config variable for user id incrementing
488 $sql = "SELECT user_id
489 FROM {$convert->src_table_prefix}users
490 WHERE user_id = 1";
491 $result = $src_db->sql_query($sql);
492 $id = (int) $src_db->sql_fetchfield('user_id');
493 $src_db->sql_freeresult($result);
495 // Try to get the maximum user id possible...
496 $sql = "SELECT MAX(user_id) AS max_user_id
497 FROM {$convert->src_table_prefix}users";
498 $result = $src_db->sql_query($sql);
499 $max_id = (int) $src_db->sql_fetchfield('max_user_id');
500 $src_db->sql_freeresult($result);
502 if ($convert->mysql_convert && $same_db)
504 $src_db->sql_query("SET NAMES 'utf8'");
507 // If there is a user id 1, we need to increment user ids. :/
508 if ($id === 1)
510 set_config('increment_user_id', ($max_id + 1), true);
511 $config['increment_user_id'] = $max_id + 1;
513 else
515 set_config('increment_user_id', 0, true);
516 $config['increment_user_id'] = 0;
520 // If the old user id is -1 in 2.0.x it is the anonymous user...
521 if ($user_id == -1)
523 return ANONYMOUS;
526 if (!empty($config['increment_user_id']) && $user_id == 1)
528 return $config['increment_user_id'];
531 // A user id of 0 can happen, for example within the ban table if no user is banned...
532 // Within the posts and topics table this can be "dangerous" but is the fault of the user
533 // having mods installed (a poster id of 0 is not possible in 2.0.x).
534 // Therefore, we return the user id "as is".
536 return (int) $user_id;
539 /* Copy additional table fields from old forum to new forum if user wants this (for Mod compatibility for example)
540 function phpbb_copy_table_fields()
546 * Convert authentication
547 * user, group and forum table has to be filled in order to work
549 function phpbb_convert_authentication($mode)
551 global $db, $src_db, $same_db, $convert, $user, $config, $cache;
553 if ($mode == 'start')
555 $db->sql_query($convert->truncate_statement . ACL_USERS_TABLE);
556 $db->sql_query($convert->truncate_statement . ACL_GROUPS_TABLE);
558 // What we will do is handling all 2.0.x admins as founder to replicate what is common in 2.0.x.
559 // After conversion the main admin need to make sure he is removing permissions and the founder status if wanted.
562 // Grab user ids of users with user_level of ADMIN
563 $sql = "SELECT user_id
564 FROM {$convert->src_table_prefix}users
565 WHERE user_level = 1
566 ORDER BY user_regdate ASC";
567 $result = $src_db->sql_query($sql);
569 while ($row = $src_db->sql_fetchrow($result))
571 $user_id = (int) phpbb_user_id($row['user_id']);
572 // Set founder admin...
573 $sql = 'UPDATE ' . USERS_TABLE . '
574 SET user_type = ' . USER_FOUNDER . "
575 WHERE user_id = $user_id";
576 $db->sql_query($sql);
578 $src_db->sql_freeresult($result);
580 $sql = 'SELECT group_id
581 FROM ' . GROUPS_TABLE . "
582 WHERE group_name = '" . $db->sql_escape('BOTS') . "'";
583 $result = $db->sql_query($sql);
584 $bot_group_id = (int) $db->sql_fetchfield('group_id');
585 $db->sql_freeresult($result);
588 // Grab forum auth information
589 $sql = "SELECT *
590 FROM {$convert->src_table_prefix}forums";
591 $result = $src_db->sql_query($sql);
593 $forum_access = array();
594 while ($row = $src_db->sql_fetchrow($result))
596 $forum_access[$row['forum_id']] = $row;
598 $src_db->sql_freeresult($result);
600 if ($convert->mysql_convert && $same_db)
602 $src_db->sql_query("SET NAMES 'binary'");
604 // Grab user auth information from 2.0.x board
605 $sql = "SELECT ug.user_id, aa.*
606 FROM {$convert->src_table_prefix}auth_access aa, {$convert->src_table_prefix}user_group ug, {$convert->src_table_prefix}groups g, {$convert->src_table_prefix}forums f
607 WHERE g.group_id = aa.group_id
608 AND g.group_single_user = 1
609 AND ug.group_id = g.group_id
610 AND f.forum_id = aa.forum_id";
611 $result = $src_db->sql_query($sql);
613 $user_access = array();
614 while ($row = $src_db->sql_fetchrow($result))
616 $user_access[$row['forum_id']][] = $row;
618 $src_db->sql_freeresult($result);
620 // Grab group auth information
621 $sql = "SELECT g.group_id, aa.*
622 FROM {$convert->src_table_prefix}auth_access aa, {$convert->src_table_prefix}groups g
623 WHERE g.group_id = aa.group_id
624 AND g.group_single_user <> 1";
625 $result = $src_db->sql_query($sql);
627 $group_access = array();
628 while ($row = $src_db->sql_fetchrow($result))
630 $group_access[$row['forum_id']][] = $row;
632 $src_db->sql_freeresult($result);
634 if ($convert->mysql_convert && $same_db)
636 $src_db->sql_query("SET NAMES 'utf8'");
639 // Add Forum Access List
640 $auth_map = array(
641 'auth_view' => array('f_', 'f_list'),
642 'auth_read' => array('f_read', 'f_search'),
643 'auth_post' => array('f_post', 'f_bbcode', 'f_smilies', 'f_img', 'f_sigs', 'f_postcount', 'f_report', 'f_subscribe', 'f_print', 'f_email'),
644 'auth_reply' => 'f_reply',
645 'auth_edit' => 'f_edit',
646 'auth_delete' => 'f_delete',
647 'auth_pollcreate' => 'f_poll',
648 'auth_vote' => 'f_vote',
649 'auth_announce' => 'f_announce',
650 'auth_sticky' => 'f_sticky',
651 'auth_attachments' => array('f_attach', 'f_download'),
652 'auth_download' => 'f_download',
655 // Define the ACL constants used in 2.0 to make the code slightly more readable
656 define('AUTH_ALL', 0);
657 define('AUTH_REG', 1);
658 define('AUTH_ACL', 2);
659 define('AUTH_MOD', 3);
660 define('AUTH_ADMIN', 5);
662 // A mapping of the simple permissions used by 2.0
663 $simple_auth_ary = array(
664 'public' => array(
665 'auth_view' => AUTH_ALL,
666 'auth_read' => AUTH_ALL,
667 'auth_post' => AUTH_ALL,
668 'auth_reply' => AUTH_ALL,
669 'auth_edit' => AUTH_REG,
670 'auth_delete' => AUTH_REG,
671 'auth_sticky' => AUTH_MOD,
672 'auth_announce' => AUTH_MOD,
673 'auth_vote' => AUTH_REG,
674 'auth_pollcreate' => AUTH_REG,
676 'registered' => array(
677 'auth_view' => AUTH_ALL,
678 'auth_read' => AUTH_ALL,
679 'auth_post' => AUTH_REG,
680 'auth_reply' => AUTH_REG,
681 'auth_edit' => AUTH_REG,
682 'auth_delete' => AUTH_REG,
683 'auth_sticky' => AUTH_MOD,
684 'auth_announce' => AUTH_MOD,
685 'auth_vote' => AUTH_REG,
686 'auth_pollcreate' => AUTH_REG,
688 'registered_hidden' => array(
689 'auth_view' => AUTH_REG,
690 'auth_read' => AUTH_REG,
691 'auth_post' => AUTH_REG,
692 'auth_reply' => AUTH_REG,
693 'auth_edit' => AUTH_REG,
694 'auth_delete' => AUTH_REG,
695 'auth_sticky' => AUTH_MOD,
696 'auth_announce' => AUTH_MOD,
697 'auth_vote' => AUTH_REG,
698 'auth_pollcreate' => AUTH_REG,
700 'private' => array(
701 'auth_view' => AUTH_ALL,
702 'auth_read' => AUTH_ACL,
703 'auth_post' => AUTH_ACL,
704 'auth_reply' => AUTH_ACL,
705 'auth_edit' => AUTH_ACL,
706 'auth_delete' => AUTH_ACL,
707 'auth_sticky' => AUTH_ACL,
708 'auth_announce' => AUTH_MOD,
709 'auth_vote' => AUTH_ACL,
710 'auth_pollcreate' => AUTH_ACL,
712 'private_hidden' => array(
713 'auth_view' => AUTH_ACL,
714 'auth_read' => AUTH_ACL,
715 'auth_post' => AUTH_ACL,
716 'auth_reply' => AUTH_ACL,
717 'auth_edit' => AUTH_ACL,
718 'auth_delete' => AUTH_ACL,
719 'auth_sticky' => AUTH_ACL,
720 'auth_announce' => AUTH_MOD,
721 'auth_vote' => AUTH_ACL,
722 'auth_pollcreate' => AUTH_ACL,
724 'moderator' => array(
725 'auth_view' => AUTH_ALL,
726 'auth_read' => AUTH_MOD,
727 'auth_post' => AUTH_MOD,
728 'auth_reply' => AUTH_MOD,
729 'auth_edit' => AUTH_MOD,
730 'auth_delete' => AUTH_MOD,
731 'auth_sticky' => AUTH_MOD,
732 'auth_announce' => AUTH_MOD,
733 'auth_vote' => AUTH_MOD,
734 'auth_pollcreate' => AUTH_MOD,
736 'moderator_hidden' => array(
737 'auth_view' => AUTH_MOD,
738 'auth_read' => AUTH_MOD,
739 'auth_post' => AUTH_MOD,
740 'auth_reply' => AUTH_MOD,
741 'auth_edit' => AUTH_MOD,
742 'auth_delete' => AUTH_MOD,
743 'auth_sticky' => AUTH_MOD,
744 'auth_announce' => AUTH_MOD,
745 'auth_vote' => AUTH_MOD,
746 'auth_pollcreate' => AUTH_MOD,
750 if ($mode == 'start')
752 user_group_auth('guests', 'SELECT user_id, {GUESTS} FROM ' . USERS_TABLE . ' WHERE user_id = ' . ANONYMOUS, false);
753 user_group_auth('registered', 'SELECT user_id, {REGISTERED} FROM ' . USERS_TABLE . ' WHERE user_id <> ' . ANONYMOUS . " AND group_id <> $bot_group_id", false);
755 // Selecting from old table
756 if (!empty($config['increment_user_id']))
758 $auth_sql = 'SELECT user_id, {ADMINISTRATORS} FROM ' . $convert->src_table_prefix . 'users WHERE user_level = 1 AND user_id <> 1';
759 user_group_auth('administrators', $auth_sql, true);
761 $auth_sql = 'SELECT ' . $config['increment_user_id'] . ' as user_id, {ADMINISTRATORS} FROM ' . $convert->src_table_prefix . 'users WHERE user_level = 1 AND user_id = 1';
762 user_group_auth('administrators', $auth_sql, true);
764 else
766 $auth_sql = 'SELECT user_id, {ADMINISTRATORS} FROM ' . $convert->src_table_prefix . 'users WHERE user_level = 1';
767 user_group_auth('administrators', $auth_sql, true);
770 if (!empty($config['increment_user_id']))
772 $auth_sql = 'SELECT user_id, {GLOBAL_MODERATORS} FROM ' . $convert->src_table_prefix . 'users WHERE user_level = 1 AND user_id <> 1';
773 user_group_auth('global_moderators', $auth_sql, true);
775 $auth_sql = 'SELECT ' . $config['increment_user_id'] . ' as user_id, {GLOBAL_MODERATORS} FROM ' . $convert->src_table_prefix . 'users WHERE user_level = 1 AND user_id = 1';
776 user_group_auth('global_moderators', $auth_sql, true);
778 else
780 $auth_sql = 'SELECT user_id, {GLOBAL_MODERATORS} FROM ' . $convert->src_table_prefix . 'users WHERE user_level = 1';
781 user_group_auth('global_moderators', $auth_sql, true);
784 else if ($mode == 'first')
786 // Go through all 2.0.x forums
787 foreach ($forum_access as $forum)
789 $new_forum_id = (int) $forum['forum_id'];
791 // Administrators have full access to all forums whatever happens
792 mass_auth('group_role', $new_forum_id, 'administrators', 'FORUM_FULL');
794 $matched_type = '';
795 foreach ($simple_auth_ary as $key => $auth_levels)
797 $matched = 1;
798 foreach ($auth_levels as $k => $level)
800 if ($forum[$k] != $auth_levels[$k])
802 $matched = 0;
806 if ($matched)
808 $matched_type = $key;
809 break;
813 switch ($matched_type)
815 case 'public':
816 mass_auth('group_role', $new_forum_id, 'guests', 'FORUM_LIMITED');
817 mass_auth('group_role', $new_forum_id, 'registered', 'FORUM_LIMITED_POLLS');
818 mass_auth('group_role', $new_forum_id, 'bots', 'FORUM_BOT');
819 break;
821 case 'registered':
822 mass_auth('group_role', $new_forum_id, 'guests', 'FORUM_READONLY');
823 mass_auth('group_role', $new_forum_id, 'bots', 'FORUM_BOT');
825 // no break;
827 case 'registered_hidden':
828 mass_auth('group_role', $new_forum_id, 'registered', 'FORUM_POLLS');
829 break;
831 case 'private':
832 case 'private_hidden':
833 case 'moderator':
834 case 'moderator_hidden':
835 default:
836 // The permissions don't match a simple set, so we're going to have to map them directly
838 // No post approval for all, in 2.0.x this feature does not exist
839 mass_auth('group', $new_forum_id, 'guests', 'f_noapprove', ACL_YES);
840 mass_auth('group', $new_forum_id, 'registered', 'f_noapprove', ACL_YES);
842 // Go through authentication map
843 foreach ($auth_map as $old_auth_key => $new_acl)
845 // If old authentication key does not exist we continue
846 // This is helpful for mods adding additional authentication fields, we need to add them to the auth_map array
847 if (!isset($forum[$old_auth_key]))
849 continue;
852 // Now set the new ACL correctly
853 switch ($forum[$old_auth_key])
855 // AUTH_ALL
856 case AUTH_ALL:
857 mass_auth('group', $new_forum_id, 'guests', $new_acl, ACL_YES);
858 mass_auth('group', $new_forum_id, 'bots', $new_acl, ACL_YES);
859 mass_auth('group', $new_forum_id, 'registered', $new_acl, ACL_YES);
860 break;
862 // AUTH_REG
863 case AUTH_REG:
864 mass_auth('group', $new_forum_id, 'registered', $new_acl, ACL_YES);
865 break;
867 // AUTH_ACL
868 case AUTH_ACL:
869 // Go through the old group access list for this forum
870 if (isset($group_access[$forum['forum_id']]))
872 foreach ($group_access[$forum['forum_id']] as $index => $access)
874 // We only check for ACL_YES equivalence entry
875 if (isset($access[$old_auth_key]) && $access[$old_auth_key] == 1)
877 mass_auth('group', $new_forum_id, (int) $access['group_id'], $new_acl, ACL_YES);
882 if (isset($user_access[$forum['forum_id']]))
884 foreach ($user_access[$forum['forum_id']] as $index => $access)
886 // We only check for ACL_YES equivalence entry
887 if (isset($access[$old_auth_key]) && $access[$old_auth_key] == 1)
889 mass_auth('user', $new_forum_id, (int) phpbb_user_id($access['user_id']), $new_acl, ACL_YES);
893 break;
895 // AUTH_MOD
896 case AUTH_MOD:
897 if (isset($group_access[$forum['forum_id']]))
899 foreach ($group_access[$forum['forum_id']] as $index => $access)
901 // We only check for ACL_YES equivalence entry
902 if (isset($access[$old_auth_key]) && $access[$old_auth_key] == 1)
904 mass_auth('group', $new_forum_id, (int) $access['group_id'], $new_acl, ACL_YES);
909 if (isset($user_access[$forum['forum_id']]))
911 foreach ($user_access[$forum['forum_id']] as $index => $access)
913 // We only check for ACL_YES equivalence entry
914 if (isset($access[$old_auth_key]) && $access[$old_auth_key] == 1)
916 mass_auth('user', $new_forum_id, (int) phpbb_user_id($access['user_id']), $new_acl, ACL_YES);
920 break;
923 break;
927 else if ($mode == 'second')
929 // Assign permission roles and other default permissions
931 // guests having u_download and u_search ability
932 $db->sql_query('INSERT INTO ' . ACL_GROUPS_TABLE . ' (group_id, forum_id, auth_option_id, auth_role_id, auth_setting) SELECT ' . get_group_id('guests') . ', 0, auth_option_id, 0, 1 FROM ' . ACL_OPTIONS_TABLE . " WHERE auth_option IN ('u_', 'u_download', 'u_search')");
934 // administrators/global mods having full user features
935 mass_auth('group_role', 0, 'administrators', 'USER_FULL');
936 mass_auth('group_role', 0, 'global_moderators', 'USER_FULL');
938 // By default all converted administrators are given full access
939 mass_auth('group_role', 0, 'administrators', 'ADMIN_FULL');
941 // All registered users are assigned the standard user role
942 mass_auth('group_role', 0, 'registered', 'USER_STANDARD');
943 mass_auth('group_role', 0, 'registered_coppa', 'USER_STANDARD');
945 // Instead of administrators being global moderators we give the MOD_FULL role to global mods (admins already assigned to this group)
946 mass_auth('group_role', 0, 'global_moderators', 'MOD_FULL');
948 // And now those who have had their avatar rights removed get assigned a more restrictive role
949 $sql = 'SELECT user_id FROM ' . $convert->src_table_prefix . 'users
950 WHERE user_allowavatar = 0
951 AND user_id > 0';
952 $result = $src_db->sql_query($sql);
954 while ($row = $src_db->sql_fetchrow($result))
956 mass_auth('user_role', 0, (int) phpbb_user_id($row['user_id']), 'USER_NOAVATAR');
958 $src_db->sql_freeresult($result);
960 // And the same for those who have had their PM rights removed
961 $sql = 'SELECT user_id FROM ' . $convert->src_table_prefix . 'users
962 WHERE user_allow_pm = 0
963 AND user_id > 0';
964 $result = $src_db->sql_query($sql);
966 while ($row = $src_db->sql_fetchrow($result))
968 mass_auth('user_role', 0, (int) phpbb_user_id($row['user_id']), 'USER_NOPM');
970 $src_db->sql_freeresult($result);
972 else if ($mode == 'third')
974 // And now the moderators
975 // We make sure that they have at least standard access to the forums they moderate in addition to the moderating permissions
977 $mod_post_map = array(
978 'auth_announce' => 'f_announce',
979 'auth_sticky' => 'f_sticky'
982 foreach ($user_access as $forum_id => $access_map)
984 $forum_id = (int) $forum_id;
986 foreach ($access_map as $access)
988 if (isset($access['auth_mod']) && $access['auth_mod'] == 1)
990 mass_auth('user_role', $forum_id, (int) phpbb_user_id($access['user_id']), 'MOD_STANDARD');
991 mass_auth('user_role', $forum_id, (int) phpbb_user_id($access['user_id']), 'FORUM_STANDARD');
992 foreach ($mod_post_map as $old => $new)
994 if (isset($forum_access[$forum_id]) && isset($forum_access[$forum_id][$old]) && $forum_access[$forum_id][$old] == AUTH_MOD)
996 mass_auth('user', $forum_id, (int) phpbb_user_id($access['user_id']), $new, ACL_YES);
1003 foreach ($group_access as $forum_id => $access_map)
1005 $forum_id = (int) $forum_id;
1007 foreach ($access_map as $access)
1009 if (isset($access['auth_mod']) && $access['auth_mod'] == 1)
1011 mass_auth('group_role', $forum_id, (int) $access['group_id'], 'MOD_STANDARD');
1012 mass_auth('group_role', $forum_id, (int) $access['group_id'], 'FORUM_STANDARD');
1013 foreach ($mod_post_map as $old => $new)
1015 if (isset($forum_access[$forum_id]) && isset($forum_access[$forum_id][$old]) && $forum_access[$forum_id][$old] == AUTH_MOD)
1017 mass_auth('group', $forum_id, (int) $access['group_id'], $new, ACL_YES);
1024 // We grant everyone readonly access to the categories to ensure that the forums are visible
1025 $sql = 'SELECT forum_id, forum_name, parent_id, left_id, right_id
1026 FROM ' . FORUMS_TABLE . '
1027 ORDER BY left_id ASC';
1028 $result = $db->sql_query($sql);
1030 $parent_forums = $forums = array();
1031 while ($row = $db->sql_fetchrow($result))
1033 if ($row['parent_id'] == 0)
1035 mass_auth('group_role', $row['forum_id'], 'administrators', 'FORUM_FULL');
1036 mass_auth('group_role', $row['forum_id'], 'global_moderators', 'FORUM_FULL');
1037 $parent_forums[] = $row;
1039 else
1041 $forums[] = $row;
1044 $db->sql_freeresult($result);
1046 global $auth;
1048 // Let us see which groups have access to these forums...
1049 foreach ($parent_forums as $row)
1051 // Get the children
1052 $branch = $forum_ids = array();
1054 foreach ($forums as $key => $_row)
1056 if ($_row['left_id'] > $row['left_id'] && $_row['left_id'] < $row['right_id'])
1058 $branch[] = $_row;
1059 $forum_ids[] = $_row['forum_id'];
1060 continue;
1064 if (sizeof($forum_ids))
1066 // Now make sure the user is able to read these forums
1067 $hold_ary = $auth->acl_group_raw_data(false, 'f_list', $forum_ids);
1069 if (empty($hold_ary))
1071 continue;
1074 foreach ($hold_ary as $g_id => $f_id_ary)
1076 $set_group = false;
1078 foreach ($f_id_ary as $f_id => $auth_ary)
1080 foreach ($auth_ary as $auth_option => $setting)
1082 if ($setting == ACL_YES)
1084 $set_group = true;
1085 break 2;
1090 if ($set_group)
1092 mass_auth('group', $row['forum_id'], $g_id, 'f_list', ACL_YES);
1101 * Set primary group.
1102 * Really simple and only based on user_level (remaining groups will be assigned later)
1104 function phpbb_set_primary_group($user_level)
1106 global $convert_row;
1108 if ($user_level == 1)
1110 return get_group_id('administrators');
1112 /* else if ($user_level == 2)
1114 return get_group_id('global_moderators');
1116 else if ($user_level == 0 && $convert_row['user_active'])*/
1117 else if ($convert_row['user_active'])
1119 return get_group_id('registered');
1122 return 0;
1126 * Convert the group name, making sure to avoid conflicts with 3.0 special groups
1128 function phpbb_convert_group_name($group_name)
1130 $default_groups = array(
1131 'GUESTS',
1132 'REGISTERED',
1133 'REGISTERED_COPPA',
1134 'GLOBAL_MODERATORS',
1135 'ADMINISTRATORS',
1136 'BOTS',
1139 if (in_array(strtoupper($group_name), $default_groups))
1141 return 'phpBB2 - ' . $group_name;
1144 return phpbb_set_default_encoding($group_name);
1148 * Convert the group type constants
1150 function phpbb_convert_group_type($group_type)
1152 switch ($group_type)
1154 case 0:
1155 return GROUP_OPEN;
1156 break;
1158 case 1:
1159 return GROUP_CLOSED;
1160 break;
1162 case 2:
1163 return GROUP_HIDDEN;
1164 break;
1167 // Never return GROUP_SPECIAL here, because only phpBB3's default groups are allowed to have this type set.
1168 return GROUP_HIDDEN;
1172 * Convert the topic type constants
1174 function phpbb_convert_topic_type($topic_type)
1176 switch ($topic_type)
1178 case 0:
1179 return POST_NORMAL;
1180 break;
1182 case 1:
1183 return POST_STICKY;
1184 break;
1186 case 2:
1187 return POST_ANNOUNCE;
1188 break;
1190 case 3:
1191 return POST_GLOBAL;
1192 break;
1195 return POST_NORMAL;
1198 function phpbb_replace_size($matches)
1200 return '[size=' . min(200, ceil(100.0 * (((double) $matches[1])/12.0))) . ':' . $matches[2] . ']';
1204 * Reparse the message stripping out the bbcode_uid values and adding new ones and setting the bitfield
1205 * @todo What do we want to do about HTML in messages - currently it gets converted to the entities, but there may be some objections to this
1207 function phpbb_prepare_message($message)
1209 global $phpbb_root_path, $phpEx, $db, $convert, $user, $config, $cache, $convert_row, $message_parser;
1211 if (!$message)
1213 $convert->row['mp_bbcode_bitfield'] = $convert_row['mp_bbcode_bitfield'] = 0;
1214 return '';
1217 // Decode phpBB 2.0.x Message
1218 if (isset($convert->row['old_bbcode_uid']) && $convert->row['old_bbcode_uid'] != '')
1220 // Adjust size...
1221 if (strpos($message, '[size=') !== false)
1223 $message = preg_replace_callback('/\[size=(\d*):(' . $convert->row['old_bbcode_uid'] . ')\]/', 'phpbb_replace_size', $message);
1226 $message = preg_replace('/\:(([a-z0-9]:)?)' . $convert->row['old_bbcode_uid'] . '/s', '', $message);
1229 if (strpos($message, '[quote=') !== false)
1231 $message = preg_replace('/\[quote="(.*?)"\]/s', '[quote=&quot;\1&quot;]', $message);
1232 $message = preg_replace('/\[quote=\\\"(.*?)\\\"\]/s', '[quote=&quot;\1&quot;]', $message);
1234 // let's hope that this solves more problems than it causes. Deal with escaped quotes.
1235 $message = str_replace('\"', '&quot;', $message);
1236 $message = str_replace('\&quot;', '&quot;', $message);
1239 // Already the new user id ;)
1240 $user_id = $convert->row['poster_id'];
1242 $message = str_replace('<', '&lt;', $message);
1243 $message = str_replace('>', '&gt;', $message);
1244 $message = str_replace('<br />', "\n", $message);
1246 // make the post UTF-8
1247 $message = phpbb_set_encoding($message);
1249 $message_parser->warn_msg = array(); // Reset the errors from the previous message
1250 $message_parser->bbcode_uid = make_uid($convert->row['post_time']);
1251 $message_parser->message = $message;
1252 unset($message);
1254 // Make sure options are set.
1255 // $enable_html = (!isset($row['enable_html'])) ? false : $row['enable_html'];
1256 $enable_bbcode = (!isset($convert->row['enable_bbcode'])) ? true : $convert->row['enable_bbcode'];
1257 $enable_smilies = (!isset($convert->row['enable_smilies'])) ? true : $convert->row['enable_smilies'];
1258 $enable_magic_url = (!isset($convert->row['enable_magic_url'])) ? true : $convert->row['enable_magic_url'];
1260 // parse($allow_bbcode, $allow_magic_url, $allow_smilies, $allow_img_bbcode = true, $allow_flash_bbcode = true, $allow_quote_bbcode = true, $allow_url_bbcode = true, $update_this_message = true, $mode = 'post')
1261 $message_parser->parse($enable_bbcode, $enable_magic_url, $enable_smilies);
1263 if (sizeof($message_parser->warn_msg))
1265 $msg_id = isset($convert->row['post_id']) ? $convert->row['post_id'] : $convert->row['privmsgs_id'];
1266 $convert->p_master->error('<span style="color:red">' . $user->lang['POST_ID'] . ': ' . $msg_id . ' ' . $user->lang['CONV_ERROR_MESSAGE_PARSER'] . ': <br /><br />' . implode('<br />', $message_parser->warn_msg), __LINE__, __FILE__, true);
1269 $convert->row['mp_bbcode_bitfield'] = $convert_row['mp_bbcode_bitfield'] = $message_parser->bbcode_bitfield;
1271 $message = $message_parser->message;
1272 unset($message_parser->message);
1274 return $message;
1278 * Return the bitfield calculated by the previous function
1280 function get_bbcode_bitfield()
1282 global $convert_row;
1284 return $convert_row['mp_bbcode_bitfield'];
1288 * Determine the last user to edit a post
1289 * In practice we only tracked edits by the original poster in 2.0.x so this will only be set if they had edited their own post
1291 function phpbb_post_edit_user()
1293 global $convert_row, $config;
1295 if (isset($convert_row['post_edit_count']))
1297 return phpbb_user_id($convert_row['poster_id']);
1300 return 0;
1304 * Obtain the path to uploaded files on the 2.0.x forum
1305 * This is only used if the Attachment MOD was installed
1307 function phpbb_get_files_dir()
1309 if (!defined('MOD_ATTACHMENT'))
1311 return;
1314 global $src_db, $same_db, $convert, $user, $config, $cache;
1316 if ($convert->mysql_convert && $same_db)
1318 $src_db->sql_query("SET NAMES 'binary'");
1320 $sql = 'SELECT config_value AS upload_dir
1321 FROM ' . $convert->src_table_prefix . "attachments_config
1322 WHERE config_name = 'upload_dir'";
1323 $result = $src_db->sql_query($sql);
1324 $upload_path = $src_db->sql_fetchfield('upload_dir');
1325 $src_db->sql_freeresult($result);
1327 $sql = 'SELECT config_value AS ftp_upload
1328 FROM ' . $convert->src_table_prefix . "attachments_config
1329 WHERE config_name = 'allow_ftp_upload'";
1330 $result = $src_db->sql_query($sql);
1331 $ftp_upload = (int) $src_db->sql_fetchfield('ftp_upload');
1332 $src_db->sql_freeresult($result);
1334 if ($convert->mysql_convert && $same_db)
1336 $src_db->sql_query("SET NAMES 'utf8'");
1339 if ($ftp_upload)
1341 $convert->p_master->error($user->lang['CONV_ERROR_ATTACH_FTP_DIR'], __LINE__, __FILE__);
1344 return $upload_path;
1348 * Copy thumbnails of uploaded images from the 2.0.x forum
1349 * This is only used if the Attachment MOD was installed
1351 function phpbb_copy_thumbnails()
1353 global $db, $convert, $user, $config, $cache, $phpbb_root_path;
1355 $src_path = $convert->options['forum_path'] . '/' . phpbb_get_files_dir() . '/thumbs/';
1357 if ($handle = @opendir($src_path))
1359 while ($entry = readdir($handle))
1361 if ($entry[0] == '.')
1363 continue;
1366 if (is_dir($src_path . $entry))
1368 continue;
1370 else
1372 copy_file($src_path . $entry, $config['upload_path'] . '/' . preg_replace('/^t_/', 'thumb_', $entry));
1373 @unlink($phpbb_root_path . $config['upload_path'] . '/thumbs/' . $entry);
1376 closedir($handle);
1381 * Convert the attachment category constants
1382 * This is only used if the Attachment MOD was installed
1384 function phpbb_attachment_category($cat_id)
1386 switch ($cat_id)
1388 case 1:
1389 return ATTACHMENT_CATEGORY_IMAGE;
1390 break;
1392 case 2:
1393 return ATTACHMENT_CATEGORY_WM;
1394 break;
1396 case 3:
1397 return ATTACHMENT_CATEGORY_FLASH;
1398 break;
1401 return ATTACHMENT_CATEGORY_NONE;
1405 * Obtain list of forums in which different attachment categories can be used
1407 function phpbb_attachment_forum_perms($forum_permissions)
1409 if (empty($forum_permissions))
1411 return '';
1414 // Decode forum permissions
1415 $forum_ids = array();
1417 $one_char_encoding = '#';
1418 $two_char_encoding = '.';
1420 $auth_len = 1;
1421 for ($pos = 0; $pos < strlen($forum_permissions); $pos += $auth_len)
1423 $forum_auth = substr($forum_permissions, $pos, 1);
1424 if ($forum_auth == $one_char_encoding)
1426 $auth_len = 1;
1427 continue;
1429 else if ($forum_auth == $two_char_encoding)
1431 $auth_len = 2;
1432 $pos--;
1433 continue;
1436 $forum_auth = substr($forum_permissions, $pos, $auth_len);
1437 $forum_id = base64_unpack($forum_auth);
1439 $forum_ids[] = (int) $forum_id;
1442 if (sizeof($forum_ids))
1444 return attachment_forum_perms($forum_ids);
1447 return '';
1451 * Convert the avatar type constants
1453 function phpbb_avatar_type($type)
1455 switch ($type)
1457 case 1:
1458 return AVATAR_UPLOAD;
1459 break;
1461 case 2:
1462 return AVATAR_REMOTE;
1463 break;
1465 case 3:
1466 return AVATAR_GALLERY;
1467 break;
1470 return 0;
1475 * Just undos the replacing of '<' and '>'
1477 function phpbb_smilie_html_decode($code)
1479 $code = str_replace('&lt;', '<', $code);
1480 return str_replace('&gt;', '>', $code);
1484 * Transfer avatars, copying the image if it was uploaded
1486 function phpbb_import_avatar($user_avatar)
1488 global $convert_row;
1490 if (!$convert_row['user_avatar_type'])
1492 return '';
1494 else if ($convert_row['user_avatar_type'] == 1)
1496 // Uploaded avatar
1497 return import_avatar($user_avatar, false, $convert_row['user_id']);
1499 else if ($convert_row['user_avatar_type'] == 2)
1501 // Remote avatar
1502 return $user_avatar;
1504 else if ($convert_row['user_avatar_type'] == 3)
1506 // Gallery avatar
1507 return $user_avatar;
1510 return '';
1515 * Find out about the avatar's dimensions
1517 function phpbb_get_avatar_height($user_avatar)
1519 global $convert_row;
1521 if (empty($convert_row['user_avatar_type']))
1523 return 0;
1525 return get_avatar_height($user_avatar, 'phpbb_avatar_type', $convert_row['user_avatar_type']);
1530 * Find out about the avatar's dimensions
1532 function phpbb_get_avatar_width($user_avatar)
1534 global $convert_row;
1536 if (empty($convert_row['user_avatar_type']))
1538 return 0;
1541 return get_avatar_width($user_avatar, 'phpbb_avatar_type', $convert_row['user_avatar_type']);
1546 * Calculate the correct to_address field for private messages
1548 function phpbb_privmsgs_to_userid($to_userid)
1550 global $config;
1552 return 'u_' . phpbb_user_id($to_userid);
1556 * Calculate whether a private message was unread using the bitfield
1558 function phpbb_unread_pm($pm_type)
1560 return ($pm_type == 5) ? 1 : 0;
1564 * Calculate whether a private message was new using the bitfield
1566 function phpbb_new_pm($pm_type)
1568 return ($pm_type == 1) ? 1 : 0;
1572 * Obtain the folder_id for the custom folder created to replace the savebox from 2.0.x (used to store saved private messages)
1574 function phpbb_get_savebox_id($user_id)
1576 global $db;
1578 $user_id = phpbb_user_id($user_id);
1580 // Only one custom folder, check only one
1581 $sql = 'SELECT folder_id
1582 FROM ' . PRIVMSGS_FOLDER_TABLE . '
1583 WHERE user_id = ' . $user_id;
1584 $result = $db->sql_query_limit($sql, 1);
1585 $folder_id = (int) $db->sql_fetchfield('folder_id');
1586 $db->sql_freeresult($result);
1588 return $folder_id;
1592 * Transfer attachment specific configuration options
1593 * These were not stored in the main config table on 2.0.x
1594 * This is only used if the Attachment MOD was installed
1596 function phpbb_import_attach_config()
1598 global $db, $src_db, $same_db, $convert, $config;
1600 if ($convert->mysql_convert && $same_db)
1602 $src_db->sql_query("SET NAMES 'binary'");
1605 $sql = 'SELECT *
1606 FROM ' . $convert->src_table_prefix . 'attachments_config';
1607 $result = $src_db->sql_query($sql);
1609 if ($convert->mysql_convert && $same_db)
1611 $src_db->sql_query("SET NAMES 'utf8'");
1614 $attach_config = array();
1615 while ($row = $src_db->sql_fetchrow($result))
1617 $attach_config[$row['config_name']] = $row['config_value'];
1619 $src_db->sql_freeresult($result);
1621 set_config('allow_attachments', 1);
1623 // old attachment mod? Must be very old if this entry do not exist...
1624 if (!empty($attach_config['display_order']))
1626 set_config('display_order', $attach_config['display_order']);
1628 set_config('max_filesize', $attach_config['max_filesize']);
1629 set_config('max_filesize_pm', $attach_config['max_filesize_pm']);
1630 set_config('attachment_quota', $attach_config['attachment_quota']);
1631 set_config('max_attachments', $attach_config['max_attachments']);
1632 set_config('max_attachments_pm', $attach_config['max_attachments_pm']);
1633 set_config('allow_pm_attach', $attach_config['allow_pm_attach']);
1635 set_config('img_display_inlined', $attach_config['img_display_inlined']);
1636 set_config('img_max_width', $attach_config['img_max_width']);
1637 set_config('img_max_height', $attach_config['img_max_height']);
1638 set_config('img_link_width', $attach_config['img_link_width']);
1639 set_config('img_link_height', $attach_config['img_link_height']);
1640 set_config('img_create_thumbnail', $attach_config['img_create_thumbnail']);
1641 set_config('img_max_thumb_width', 400);
1642 set_config('img_min_thumb_filesize', $attach_config['img_min_thumb_filesize']);
1643 set_config('img_imagick', $attach_config['img_imagick']);
1647 * Calculate the date a user became inactive
1649 function phpbb_inactive_time()
1651 global $convert_row;
1653 if ($convert_row['user_active'])
1655 return 0;
1658 if ($convert_row['user_lastvisit'])
1660 return $convert_row['user_lastvisit'];
1663 return $convert_row['user_regdate'];
1667 * Calculate the reason a user became inactive
1668 * We can't actually tell the difference between a manual deactivation and one for profile changes
1669 * from the data available to assume the latter
1671 function phpbb_inactive_reason()
1673 global $convert_row;
1675 if ($convert_row['user_active'])
1677 return 0;
1680 if ($convert_row['user_lastvisit'])
1682 return INACTIVE_PROFILE;
1685 return INACTIVE_REGISTER;
1689 * Adjust 2.0.x disallowed names to 3.0.x format
1691 function phpbb_disallowed_username($username)
1693 // Replace * with %
1694 $username = phpbb_set_default_encoding(str_replace('*', '%', $username));
1695 return utf8_htmlspecialchars($username);
1699 * Checks whether there are any usernames on the old board that would map to the same
1700 * username_clean on phpBB3. Prints out a list if any exist and exits.
1702 function phpbb_create_userconv_table()
1704 global $db, $src_db, $convert, $table_prefix, $user, $lang;
1706 $map_dbms = '';
1707 switch ($db->sql_layer)
1709 case 'mysql':
1710 $map_dbms = 'mysql_40';
1711 break;
1713 case 'mysql4':
1714 if (version_compare($db->sql_server_info(true), '4.1.3', '>='))
1716 $map_dbms = 'mysql_41';
1718 else
1720 $map_dbms = 'mysql_40';
1722 break;
1724 case 'mysqli':
1725 $map_dbms = 'mysql_41';
1726 break;
1728 case 'mssql':
1729 case 'mssql_odbc':
1730 $map_dbms = 'mssql';
1731 break;
1733 default:
1734 $map_dbms = $db->sql_layer;
1735 break;
1738 // create a temporary table in which we store the clean usernames
1739 $drop_sql = 'DROP TABLE ' . USERCONV_TABLE;
1740 switch ($map_dbms)
1742 case 'firebird':
1743 $create_sql = 'CREATE TABLE ' . USERCONV_TABLE . ' (
1744 user_id INTEGER NOT NULL,
1745 username_clean VARCHAR(255) CHARACTER SET UTF8 DEFAULT \'\' NOT NULL COLLATE UNICODE
1747 break;
1749 case 'mssql':
1750 $create_sql = 'CREATE TABLE [' . USERCONV_TABLE . '] (
1751 [user_id] [int] NOT NULL ,
1752 [username_clean] [varchar] (255) DEFAULT (\'\') NOT NULL
1754 break;
1756 case 'mysql_40':
1757 $create_sql = 'CREATE TABLE ' . USERCONV_TABLE . ' (
1758 user_id mediumint(8) NOT NULL,
1759 username_clean blob NOT NULL
1761 break;
1763 case 'mysql_41':
1764 $create_sql = 'CREATE TABLE ' . USERCONV_TABLE . ' (
1765 user_id mediumint(8) NOT NULL,
1766 username_clean varchar(255) DEFAULT \'\' NOT NULL
1767 ) CHARACTER SET `utf8` COLLATE `utf8_bin`';
1768 break;
1770 case 'oracle':
1771 $create_sql = 'CREATE TABLE ' . USERCONV_TABLE . ' (
1772 user_id number(8) NOT NULL,
1773 username_clean varchar2(255) DEFAULT \'\'
1775 break;
1777 case 'postgres':
1778 $create_sql = 'CREATE TABLE ' . USERCONV_TABLE . ' (
1779 user_id INT4 DEFAULT \'0\',
1780 username_clean varchar_ci DEFAULT \'\' NOT NULL
1782 break;
1784 case 'sqlite':
1785 $create_sql = 'CREATE TABLE ' . USERCONV_TABLE . ' (
1786 user_id INTEGER NOT NULL DEFAULT \'0\',
1787 username_clean varchar(255) NOT NULL DEFAULT \'\'
1789 break;
1792 $db->sql_return_on_error(true);
1793 $db->sql_query($drop_sql);
1794 $db->sql_return_on_error(false);
1795 $db->sql_query($create_sql);
1798 function phpbb_check_username_collisions()
1800 global $db, $src_db, $convert, $table_prefix, $user, $lang;
1802 // now find the clean version of the usernames that collide
1803 $sql = 'SELECT username_clean
1804 FROM ' . USERCONV_TABLE .'
1805 GROUP BY username_clean
1806 HAVING COUNT(user_id) > 1';
1807 $result = $db->sql_query($sql);
1809 $colliding_names = array();
1810 while ($row = $db->sql_fetchrow($result))
1812 $colliding_names[] = $row['username_clean'];
1814 $db->sql_freeresult($result);
1816 // there was at least one collision, the admin will have to solve it before conversion can continue
1817 if (sizeof($colliding_names))
1819 $sql = 'SELECT user_id, username_clean
1820 FROM ' . USERCONV_TABLE . '
1821 WHERE ' . $db->sql_in_set('username_clean', $colliding_names);
1822 $result = $db->sql_query($sql);
1823 unset($colliding_names);
1825 $colliding_user_ids = array();
1826 while ($row = $db->sql_fetchrow($result))
1828 $colliding_user_ids[(int) $row['user_id']] = $row['username_clean'];
1830 $db->sql_freeresult($result);
1832 $sql = 'SELECT username, user_id, user_posts
1833 FROM ' . $convert->src_table_prefix . 'users
1834 WHERE ' . $src_db->sql_in_set('user_id', array_keys($colliding_user_ids));
1835 $result = $src_db->sql_query($sql);
1837 $colliding_users = array();
1838 while ($row = $src_db->sql_fetchrow($result))
1840 $row['user_id'] = (int) $row['user_id'];
1841 if (isset($colliding_user_ids[$row['user_id']]))
1843 $colliding_users[$colliding_user_ids[$row['user_id']]][] = $row;
1846 $src_db->sql_freeresult($result);
1847 unset($colliding_user_ids);
1849 $list = '';
1850 foreach ($colliding_users as $username_clean => $users)
1852 $list .= sprintf($user->lang['COLLIDING_CLEAN_USERNAME'], $username_clean) . "<br />\n";
1853 foreach ($users as $i => $row)
1855 $list .= sprintf($user->lang['COLLIDING_USER'], $row['user_id'], phpbb_set_default_encoding($row['username']), $row['user_posts']) . "<br />\n";
1859 $lang['INST_ERR_FATAL'] = $user->lang['CONV_ERR_FATAL'];
1860 $convert->p_master->error('<span style="color:red">' . $user->lang['COLLIDING_USERNAMES_FOUND'] . '</span></b><br /><br />' . $list . '<b>', __LINE__, __FILE__);
1863 $drop_sql = 'DROP TABLE ' . USERCONV_TABLE;
1864 $db->sql_query($drop_sql);