ok, let's recode other text fields too...
[phpbb.git] / phpBB / install / convertors / functions_phpbb20.php
blob62ce8b774a06157b8954e694754ea90cd665be77
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 /**
12 * Helper functions for phpBB 2.0.x to phpBB 3.0.x conversion
15 /**
16 * Set forum flags - only prune old polls by default
18 function phpbb_forum_flags()
20 // Set forum flags
21 $forum_flags = 0;
23 // FORUM_FLAG_LINK_TRACK
24 $forum_flags += 0;
26 // FORUM_FLAG_PRUNE_POLL
27 $forum_flags += FORUM_FLAG_PRUNE_POLL;
29 // FORUM_FLAG_PRUNE_ANNOUNCE
30 $forum_flags += 0;
32 // FORUM_FLAG_PRUNE_STICKY
33 $forum_flags += 0;
35 // FORUM_FLAG_ACTIVE_TOPICS
36 $forum_flags += 0;
38 // FORUM_FLAG_POST_REVIEW
39 $forum_flags += FORUM_FLAG_POST_REVIEW;
41 return $forum_flags;
44 /**
45 * Insert/Convert forums
47 function phpbb_insert_forums()
49 global $db, $convert, $user, $config;
51 $db->sql_query($convert->truncate_statement . FORUMS_TABLE);
53 // Determine the highest id used within the old forums table (we add the categories after the forum ids)
54 $sql = 'SELECT MAX(forum_id) AS max_forum_id
55 FROM ' . $convert->src_table_prefix . 'forums';
56 $result = $db->sql_query($sql);
57 $max_forum_id = (int) $db->sql_fetchfield('max_forum_id');
58 $db->sql_freeresult($result);
60 $max_forum_id++;
62 // Insert categories
63 $sql = 'SELECT cat_id, cat_title
64 FROM ' . $convert->src_table_prefix . 'categories
65 ORDER BY cat_order';
66 $result = $db->sql_query($sql);
68 $cats_added = array();
69 while ($row = $db->sql_fetchrow($result))
71 $sql_ary = array(
72 'forum_id' => $max_forum_id,
73 'forum_name' => ($row['cat_title']) ? htmlspecialchars(phpbb_set_encoding($row['cat_title'], false), ENT_COMPAT, 'UTF-8') : $user->lang['CATEGORY'],
74 'parent_id' => 0,
75 'forum_parents' => '',
76 'forum_desc' => '',
77 'forum_type' => FORUM_CAT,
78 'forum_status' => ITEM_UNLOCKED,
79 'forum_rules' => '',
82 $sql = 'SELECT MAX(right_id) AS right_id
83 FROM ' . FORUMS_TABLE;
84 $_result = $db->sql_query($sql);
85 $cat_row = $db->sql_fetchrow($_result);
86 $db->sql_freeresult($_result);
88 $sql_ary['left_id'] = $cat_row['right_id'] + 1;
89 $sql_ary['right_id'] = $cat_row['right_id'] + 2;
91 $sql = 'INSERT INTO ' . FORUMS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
92 $db->sql_query($sql);
94 $cats_added[$row['cat_id']] = $max_forum_id;
95 $max_forum_id++;
97 $db->sql_freeresult($result);
99 // There may be installations having forums with non-existant category ids.
100 // We try to catch them and add them to an "unknown" category instead of leaving them out.
101 $sql = 'SELECT cat_id
102 FROM ' . $convert->src_table_prefix . 'forums
103 GROUP BY cat_id';
104 $result = $db->sql_query($sql);
106 $unknown_cat_id = false;
107 while ($row = $db->sql_fetchrow($result))
109 // Catch those categories not been added before
110 if (!isset($cats_added[$row['cat_id']]))
112 $unknown_cat_id = true;
115 $db->sql_freeresult($result);
117 // Is there at least one category not known?
118 if ($unknown_cat_id === true)
120 $unknown_cat_id = 'ghost';
122 $sql_ary = array(
123 'forum_id' => $max_forum_id,
124 'forum_name' => $user->lang['CATEGORY'],
125 'parent_id' => 0,
126 'forum_parents' => '',
127 'forum_desc' => '',
128 'forum_type' => FORUM_CAT,
129 'forum_status' => ITEM_UNLOCKED,
130 'forum_rules' => '',
133 $sql = 'SELECT MAX(right_id) AS right_id
134 FROM ' . FORUMS_TABLE;
135 $_result = $db->sql_query($sql);
136 $cat_row = $db->sql_fetchrow($_result);
137 $db->sql_freeresult($_result);
139 $sql_ary['left_id'] = $cat_row['right_id'] + 1;
140 $sql_ary['right_id'] = $cat_row['right_id'] + 2;
142 $sql = 'INSERT INTO ' . FORUMS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
143 $db->sql_query($sql);
145 $cats_added[$unknown_cat_id] = $max_forum_id;
146 $max_forum_id++;
149 // Now insert the forums
150 $sql = 'SELECT f.*, fp.prune_days, fp.prune_freq FROM ' . $convert->src_table_prefix . 'forums f
151 LEFT JOIN ' . $convert->src_table_prefix . 'forum_prune fp ON f.forum_id = fp.forum_id
152 GROUP BY f.forum_id
153 ORDER BY f.cat_id, f.forum_order';
154 $result = $db->sql_query($sql);
156 while ($row = $db->sql_fetchrow($result))
158 // Some might have forums here with an id not being "possible"...
159 // To be somewhat friendly we "change" the category id for those to a previously created ghost category
160 if (!isset($cats_added[$row['cat_id']]) && $unknown_cat_id !== false)
162 $row['cat_id'] = $unknown_cat_id;
165 if (!isset($cats_added[$row['cat_id']]))
167 continue;
170 // Define the new forums sql ary
171 $sql_ary = array(
172 'forum_id' => (int) $row['forum_id'],
173 'forum_name' => htmlspecialchars(phpbb_set_encoding($row['forum_name'], false), ENT_COMPAT, 'UTF-8'),
174 'parent_id' => $cats_added[$row['cat_id']],
175 'forum_parents' => '',
176 'forum_desc' => htmlspecialchars(phpbb_set_encoding($row['forum_desc'], false), ENT_COMPAT, 'UTF-8'),
177 'forum_type' => FORUM_POST,
178 'forum_status' => is_item_locked($row['forum_status']),
179 'enable_prune' => $row['prune_enable'],
180 'prune_next' => null_to_zero($row['prune_next']),
181 'prune_days' => null_to_zero($row['prune_days']),
182 'prune_viewed' => 0,
183 'prune_freq' => null_to_zero($row['prune_freq']),
185 'forum_flags' => phpbb_forum_flags(),
187 // Default values
188 'forum_desc_bitfield' => '',
189 'forum_desc_options' => 7,
190 'forum_desc_uid' => '',
191 'forum_link' => '',
192 'forum_password' => '',
193 'forum_style' => 0,
194 'forum_image' => '',
195 'forum_rules' => '',
196 'forum_rules_link' => '',
197 'forum_rules_bitfield' => '',
198 'forum_rules_options' => 7,
199 'forum_rules_uid' => '',
200 'forum_topics_per_page' => 0,
201 'forum_posts' => 0,
202 'forum_topics' => 0,
203 'forum_topics_real' => 0,
204 'forum_last_post_id' => 0,
205 'forum_last_poster_id' => 0,
206 'forum_last_post_subject' => '',
207 'forum_last_post_time' => 0,
208 'forum_last_poster_name' => '',
209 'forum_last_poster_colour' => '',
210 'display_on_index' => 1,
211 'enable_indexing' => 1,
212 'enable_icons' => 0,
215 // Now add the forums with proper left/right ids
216 $sql = 'SELECT left_id, right_id
217 FROM ' . FORUMS_TABLE . '
218 WHERE forum_id = ' . $cats_added[$row['cat_id']];
219 $_result = $db->sql_query($sql);
220 $cat_row = $db->sql_fetchrow($_result);
221 $db->sql_freeresult($_result);
223 $sql = 'UPDATE ' . FORUMS_TABLE . '
224 SET left_id = left_id + 2, right_id = right_id + 2
225 WHERE left_id > ' . $cat_row['right_id'];
226 $db->sql_query($sql);
228 $sql = 'UPDATE ' . FORUMS_TABLE . '
229 SET right_id = right_id + 2
230 WHERE ' . $cat_row['left_id'] . ' BETWEEN left_id AND right_id';
231 $db->sql_query($sql);
233 $sql_ary['left_id'] = $cat_row['right_id'];
234 $sql_ary['right_id'] = $cat_row['right_id'] + 1;
236 $sql = 'INSERT INTO ' . FORUMS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
237 $db->sql_query($sql);
239 $db->sql_freeresult($result);
243 * Function for recoding text with the default language
245 * @param string $text text to recode to utf8
246 * @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
248 function phpbb_set_encoding($text, $grab_user_lang = true)
250 global $lang_enc_array, $convert_row;
251 global $convert, $phpEx;
253 /*static $lang_enc_array = array(
254 'korean' => 'euc-kr',
255 'serbian' => 'windows-1250',
256 'polish' => 'iso-8859-2',
257 'kurdish' => 'windows-1254',
258 'slovak' => 'Windows-1250',
259 'russian' => 'windows-1251',
260 'estonian' => 'iso-8859-4',
261 'chinese_simplified' => 'gb2312',
262 'macedonian' => 'windows-1251',
263 'azerbaijani' => 'UTF-8',
264 'romanian' => 'iso-8859-2',
265 'romanian_diacritice' => 'iso-8859-2',
266 'lithuanian' => 'windows-1257',
267 'turkish' => 'iso-8859-9',
268 'ukrainian' => 'windows-1251',
269 'japanese' => 'shift_jis',
270 'hungarian' => 'ISO-8859-2',
271 'romanian_no_diacritics' => 'iso-8859-2',
272 'mongolian' => 'UTF-8',
273 'slovenian' => 'windows-1250',
274 'bosnian' => 'windows-1250',
275 'czech' => 'Windows-1250',
276 'farsi' => 'Windows-1256',
277 'croatian' => 'windows-1250',
278 'greek' => 'iso-8859-7',
279 'russian_tu' => 'windows-1251',
280 'sakha' => 'UTF-8',
281 'serbian_cyrillic' => 'windows-1251',
282 'bulgarian' => 'windows-1251',
283 'chinese_traditional_taiwan' => 'big5',
284 'chinese_traditional' => 'big5',
285 'arabic' => 'windows-1256',
286 'hebrew' => 'WINDOWS-1255',
287 'thai' => 'windows-874',
288 //'chinese_traditional_taiwan' => 'utf-8' // custom modified, we may have to do an include :-(
289 );*/
291 if (empty($lang_enc_array))
293 $lang_enc_array = array();
296 $get_lang = trim(get_config_value('default_lang'));
298 // Do we need the users language encoding?
299 if ($grab_user_lang && !empty($convert_row))
301 if (!empty($convert_row['user_lang']))
303 $get_lang = trim($convert_row['user_lang']);
305 else if (!empty($convert_row['poster_id']))
307 global $db;
309 $sql = 'SELECT user_lang
310 FROM ' . $convert->src_table_prefix . 'users
311 WHERE user_id = ' . (int) $convert_row['poster_id'];
312 $result = $db->sql_query($sql);
313 $get_lang = (string) $db->sql_fetchfield('user_lang');
314 $db->sql_freeresult($result);
316 $get_lang = (!trim($get_lang)) ? trim(get_config_value('default_lang')) : trim($get_lang);
320 if (!isset($lang_enc_array[$get_lang]))
322 $filename = $convert->convertor_status['forum_path'] . '/language/lang_' . $get_lang . '/lang_main.' . $phpEx;
324 if (!file_exists($filename))
326 $get_lang = trim(get_config_value('default_lang'));
329 if (!isset($lang_enc_array[$get_lang]))
331 include($convert->convertor_status['forum_path'] . '/language/lang_' . $get_lang . '/lang_main.' . $phpEx);
332 $lang_enc_array[$get_lang] = $lang['ENCODING'];
333 unset($lang);
337 $encoding = $lang_enc_array[$get_lang];
339 return utf8_recode($text, $lang_enc_array[$get_lang]);
343 * Same as phpbb_set_encoding, but forcing boards default language
345 function phpbb_set_default_encoding($text)
347 return phpbb_set_encoding($text, false);
351 * Convert Birthday from Birthday MOD to phpBB Format
353 function phpbb_get_birthday($birthday = '')
355 $birthday = (int) $birthday;
357 if (defined('MOD_BIRTHDAY_TERRA'))
359 // stored as month, day, year
360 if (!$birthday)
362 return ' 0- 0- 0';
365 $birthday = (string) $birthday;
367 $month = substr($birthday, 0, 2);
368 $day = substr($birthday, 2, 2);
369 $year = substr($birthday, -4);
371 return sprintf('%02d-%02d-%04d', $day, $month, $year);
373 else
375 if (!$birthday || $birthday == 999999 || $birthday < 0)
377 return ' 0- 0- 0';
380 // The birthday mod from niels is using this code to transform to day/month/year
381 return gmdate('d-m-Y', $birthday * 86400 + 1);
386 * Return correct user id value
387 * Everyone's id will be one higher to allow the guest/anonymous user to have a positive id as well
389 function phpbb_user_id($user_id)
391 if (!$user_id)
393 return 0;
396 if ($user_id == -1)
398 return ANONYMOUS;
401 global $config;
403 // Increment user id if the old forum is having a user with the id 1
404 if (!isset($config['increment_user_id']))
406 global $db, $convert;
408 // Now let us set a temporary config variable for user id incrementing
409 $sql = "SELECT user_id
410 FROM {$convert->src_table_prefix}users
411 WHERE user_id = 1";
412 $result = $db->sql_query($sql);
413 $id = (int) $db->sql_fetchfield('user_id');
414 $db->sql_freeresult($result);
416 // If there is a user id 1, we need to increment user ids. :/
417 if ($id === 1)
419 set_config('increment_user_id', 1, true);
420 $config['increment_user_id'] = 1;
422 else
424 set_config('increment_user_id', 0, true);
425 $config['increment_user_id'] = 0;
429 if (!empty($config['increment_user_id']))
431 $user_id++;
434 return $user_id;
437 /* Copy additional table fields from old forum to new forum if user wants this (for Mod compatibility for example)
438 function phpbb_copy_table_fields()
444 * Convert authentication
445 * user, group and forum table has to be filled in order to work
447 function phpbb_convert_authentication($mode)
449 global $db, $convert, $user, $config, $cache;
451 if ($mode == 'start')
453 $db->sql_query($convert->truncate_statement . ACL_USERS_TABLE);
454 $db->sql_query($convert->truncate_statement . ACL_GROUPS_TABLE);
456 // Grab user id of first user with user_level of ADMIN
457 $sql = "SELECT user_id
458 FROM {$convert->src_table_prefix}users
459 WHERE user_level = 1
460 ORDER BY user_regdate ASC";
461 $result = $db->sql_query_limit($sql, 1);
462 $row = $db->sql_fetchrow($result);
463 $db->sql_freeresult($result);
465 $founder_id = phpbb_user_id($row['user_id']);
467 // Set a founder admin ... we'll assume it's the first user with admin level access
468 $sql = 'UPDATE ' . USERS_TABLE . '
469 SET user_type = ' . USER_FOUNDER . "
470 WHERE user_id = $founder_id";
471 $db->sql_query($sql);
474 // Grab forum auth information
475 $sql = "SELECT *
476 FROM {$convert->src_table_prefix}forums";
477 $result = $db->sql_query($sql);
479 $forum_access = array();
480 while ($row = $db->sql_fetchrow($result))
482 $forum_access[] = $row;
484 $db->sql_freeresult($result);
486 // Grab user auth information from 2.0.x board
487 $sql = "SELECT ug.user_id, aa.*
488 FROM {$convert->src_table_prefix}auth_access aa, {$convert->src_table_prefix}user_group ug, {$convert->src_table_prefix}groups g
489 WHERE g.group_id = aa.group_id
490 AND g.group_single_user = 1
491 AND ug.group_id = g.group_id";
492 $result = $db->sql_query($sql);
494 $user_access = array();
495 while ($row = $db->sql_fetchrow($result))
497 $user_access[$row['forum_id']][] = $row;
499 $db->sql_freeresult($result);
501 // Grab group auth information
502 $sql = "SELECT g.group_id, aa.*
503 FROM {$convert->src_table_prefix}auth_access aa, {$convert->src_table_prefix}groups g
504 WHERE g.group_id = aa.group_id
505 AND g.group_single_user <> 1";
506 $result = $db->sql_query($sql);
508 $group_access = array();
509 while ($row = $db->sql_fetchrow($result))
511 $group_access[$row['forum_id']][] = $row;
513 $db->sql_freeresult($result);
515 // Add Forum Access List
516 $auth_map = array(
517 'auth_view' => array('f_', 'f_list'),
518 'auth_read' => 'f_read',
519 'auth_post' => array('f_post', 'f_bbcode', 'f_smilies', 'f_img', 'f_sigs', 'f_search', 'f_postcount'),
520 'auth_reply' => 'f_reply',
521 'auth_edit' => 'f_edit',
522 'auth_delete' => 'f_delete',
523 'auth_pollcreate' => 'f_poll',
524 'auth_vote' => 'f_vote',
525 'auth_announce' => 'f_announce',
526 'auth_sticky' => 'f_sticky',
527 'auth_attachments' => 'f_attach',
528 'auth_download' => 'f_download',
531 // Define the ACL constants used in 2.0 to make the code slightly more readable
532 define('AUTH_ALL', 0);
533 define('AUTH_REG', 1);
534 define('AUTH_ACL', 2);
535 define('AUTH_MOD', 3);
536 define('AUTH_ADMIN', 5);
538 // A mapping of the simple permissions used by 2.0
539 $simple_auth_ary = array(
540 'public' => array(
541 'auth_view' => AUTH_ALL,
542 'auth_read' => AUTH_ALL,
543 'auth_post' => AUTH_ALL,
544 'auth_reply' => AUTH_ALL,
545 'auth_edit' => AUTH_REG,
546 'auth_delete' => AUTH_REG,
547 'auth_sticky' => AUTH_MOD,
548 'auth_announce' => AUTH_MOD,
549 'auth_vote' => AUTH_REG,
550 'auth_pollcreate' => AUTH_REG,
552 'registered' => array(
553 'auth_view' => AUTH_ALL,
554 'auth_read' => AUTH_ALL,
555 'auth_post' => AUTH_REG,
556 'auth_reply' => AUTH_REG,
557 'auth_edit' => AUTH_REG,
558 'auth_delete' => AUTH_REG,
559 'auth_sticky' => AUTH_MOD,
560 'auth_announce' => AUTH_MOD,
561 'auth_vote' => AUTH_REG,
562 'auth_pollcreate' => AUTH_REG,
564 'registered_hidden' => array(
565 'auth_view' => AUTH_REG,
566 'auth_read' => AUTH_REG,
567 'auth_post' => AUTH_REG,
568 'auth_reply' => AUTH_REG,
569 'auth_edit' => AUTH_REG,
570 'auth_delete' => AUTH_REG,
571 'auth_sticky' => AUTH_MOD,
572 'auth_announce' => AUTH_MOD,
573 'auth_vote' => AUTH_REG,
574 'auth_pollcreate' => AUTH_REG,
576 'private' => array(
577 'auth_view' => AUTH_ALL,
578 'auth_read' => AUTH_ACL,
579 'auth_post' => AUTH_ACL,
580 'auth_reply' => AUTH_ACL,
581 'auth_edit' => AUTH_ACL,
582 'auth_delete' => AUTH_ACL,
583 'auth_sticky' => AUTH_ACL,
584 'auth_announce' => AUTH_MOD,
585 'auth_vote' => AUTH_ACL,
586 'auth_pollcreate' => AUTH_ACL,
588 'private_hidden' => array(
589 'auth_view' => AUTH_ACL,
590 'auth_read' => AUTH_ACL,
591 'auth_post' => AUTH_ACL,
592 'auth_reply' => AUTH_ACL,
593 'auth_edit' => AUTH_ACL,
594 'auth_delete' => AUTH_ACL,
595 'auth_sticky' => AUTH_ACL,
596 'auth_announce' => AUTH_MOD,
597 'auth_vote' => AUTH_ACL,
598 'auth_pollcreate' => AUTH_ACL,
600 'moderator' => array(
601 'auth_view' => AUTH_ALL,
602 'auth_read' => AUTH_MOD,
603 'auth_post' => AUTH_MOD,
604 'auth_reply' => AUTH_MOD,
605 'auth_edit' => AUTH_MOD,
606 'auth_delete' => AUTH_MOD,
607 'auth_sticky' => AUTH_MOD,
608 'auth_announce' => AUTH_MOD,
609 'auth_vote' => AUTH_MOD,
610 'auth_pollcreate' => AUTH_MOD,
612 'moderator_hidden' => array(
613 'auth_view' => AUTH_MOD,
614 'auth_read' => AUTH_MOD,
615 'auth_post' => AUTH_MOD,
616 'auth_reply' => AUTH_MOD,
617 'auth_edit' => AUTH_MOD,
618 'auth_delete' => AUTH_MOD,
619 'auth_sticky' => AUTH_MOD,
620 'auth_announce' => AUTH_MOD,
621 'auth_vote' => AUTH_MOD,
622 'auth_pollcreate' => AUTH_MOD,
626 if ($mode == 'start')
628 user_group_auth('guests', 'SELECT user_id, {GUESTS} FROM ' . USERS_TABLE . ' WHERE user_id = ' . ANONYMOUS);
629 user_group_auth('registered', 'SELECT user_id, {REGISTERED} FROM ' . USERS_TABLE . ' WHERE user_id <> ' . ANONYMOUS);
631 // Selecting from old table
632 $auth_sql = 'SELECT ';
633 $auth_sql .= (!empty($config['increment_user_id'])) ? 'user_id + 1 as user_id' : 'user_id';
634 $auth_sql .= ', {ADMINISTRATORS} FROM ' . $convert->src_table_prefix . 'users WHERE user_level = 1';
636 user_group_auth('administrators', $auth_sql);
638 else if ($mode == 'first')
640 // Go through all 2.0.x forums (we saved those ids for reference)
641 foreach ($forum_access as $forum)
643 $new_forum_id = (int) $forum['forum_id'];
645 // Administrators have full access to all forums whatever happens
646 mass_auth('group_role', $new_forum_id, 'administrators', 'FORUM_FULL');
648 $matched_type = '';
649 foreach ($simple_auth_ary as $key => $auth_levels)
651 $matched = 1;
652 foreach ($auth_levels as $k => $level)
654 if ($forum[$k] != $auth_levels[$k])
656 $matched = 0;
660 if ($matched)
662 $matched_type = $key;
663 break;
667 switch ($matched_type)
669 case 'public':
670 mass_auth('group_role', $new_forum_id, 'guests', 'FORUM_LIMITED');
671 mass_auth('group_role', $new_forum_id, 'registered', 'FORUM_LIMITED_POLLS');
672 mass_auth('group_role', $new_forum_id, 'bots', 'FORUM_BOT');
673 break;
675 case 'registered':
676 mass_auth('group_role', $new_forum_id, 'guests', 'FORUM_READONLY');
677 mass_auth('group_role', $new_forum_id, 'bots', 'FORUM_BOT');
679 // no break;
681 case 'registered_hidden':
682 mass_auth('group_role', $new_forum_id, 'registered', 'FORUM_LIMITED_POLLS');
683 break;
685 case 'private':
686 case 'private_hidden':
687 case 'moderator':
688 case 'moderator_hidden':
689 default:
690 // The permissions don't match a simple set, so we're going to have to map them directly
692 // No post approval for all, in 2.0.x this feature does not exist
693 mass_auth('group', $new_forum_id, 'guests', 'f_noapprove', ACL_YES);
694 mass_auth('group', $new_forum_id, 'registered', 'f_noapprove', ACL_YES);
696 // Go through authentication map
697 foreach ($auth_map as $old_auth_key => $new_acl)
699 // If old authentication key does not exist we continue
700 // This is helpful for mods adding additional authentication fields, we need to add them to the auth_map array
701 if (!isset($forum[$old_auth_key]))
703 continue;
706 // Now set the new ACL correctly
707 switch ($forum[$old_auth_key])
709 // AUTH_ALL
710 case AUTH_ALL:
711 mass_auth('group', $new_forum_id, 'guests', $new_acl, ACL_YES);
712 mass_auth('group', $new_forum_id, 'registered', $new_acl, ACL_YES);
713 break;
715 // AUTH_REG
716 case AUTH_REG:
717 mass_auth('group', $new_forum_id, 'registered', $new_acl, ACL_YES);
718 break;
720 // AUTH_ACL
721 case AUTH_ACL:
722 // Go through the old group access list for this forum
723 if (isset($group_access[$forum['forum_id']]))
725 foreach ($group_access[$forum['forum_id']] as $index => $access)
727 // We only check for ACL_YES equivalence entry
728 if (isset($access[$old_auth_key]) && $access[$old_auth_key] == 1)
730 mass_auth('group', $new_forum_id, $access['group_id'], $new_acl, ACL_YES);
735 if (isset($user_access[$forum['forum_id']]))
737 foreach ($user_access[$forum['forum_id']] as $index => $access)
739 // We only check for ACL_YES equivalence entry
740 if (isset($access[$old_auth_key]) && $access[$old_auth_key] == 1)
742 mass_auth('user', $new_forum_id, phpbb_user_id($access['user_id']), $new_acl, ACL_YES);
746 break;
748 // AUTH_MOD
749 case AUTH_MOD:
750 if (isset($group_access[$forum['forum_id']]))
752 foreach ($group_access[$forum['forum_id']] as $index => $access)
754 // We only check for ACL_YES equivalence entry
755 if (isset($access[$old_auth_key]) && $access[$old_auth_key] == 1)
757 mass_auth('group', $new_forum_id, $access['group_id'], $new_acl, ACL_YES);
762 if (isset($user_access[$forum['forum_id']]))
764 foreach ($user_access[$forum['forum_id']] as $index => $access)
766 // We only check for ACL_YES equivalence entry
767 if (isset($access[$old_auth_key]) && $access[$old_auth_key] == 1)
769 mass_auth('user', $new_forum_id, phpbb_user_id($access['user_id']), $new_acl, ACL_YES);
773 break;
776 break;
780 else if ($mode == 'second')
782 // Assign permission roles
784 // By default all converted administrators are given full access
785 // @todo Review the implications of this - we might want to remove access to clear logs, view phpinfo, etc
786 mass_auth('group_role', 0, 'administrators', 'ADMIN_FULL');
787 mass_auth('group_role', 0, 'administrators', 'MOD_FULL');
788 mass_auth('group_role', 0, 'administrators', 'USER_FULL');
790 // All registered users are assigned the standard user role
791 mass_auth('group_role', 0, 'registered', 'USER_STANDARD');
793 // And now those who have had their avatar rights removed get assigned a more restrictive role
794 $sql = 'SELECT user_id FROM ' . $convert->src_table_prefix . 'users
795 WHERE user_allowavatar = 0
796 AND user_id > 0';
797 $result = $db->sql_query($sql);
799 while ($row = $db->sql_fetchrow($result))
801 mass_auth('user_role', 0, phpbb_user_id($row['user_id']), 'USER_NOAVATAR');
803 $db->sql_freeresult($result);
805 // And the same for those who have had their PM rights removed
806 $sql = 'SELECT user_id FROM ' . $convert->src_table_prefix . 'users
807 WHERE user_allow_pm = 0
808 AND user_id > 0';
809 $result = $db->sql_query($sql);
811 while ($row = $db->sql_fetchrow($result))
813 mass_auth('user_role', 0, phpbb_user_id($row['user_id']), 'USER_NOPM');
815 $db->sql_freeresult($result);
817 else if ($mode == 'third')
819 // And now the moderators
820 // We make sure that they have at least standard access to the forums they moderate in addition to the moderating permissions
821 foreach ($user_access as $forum_id => $access_map)
823 $forum_id = (int) $forum_id;
825 foreach ($access_map as $access)
827 if (isset($access['auth_mod']) && $access['auth_mod'] == 1)
829 mass_auth('user_role', $forum_id, phpbb_user_id($access['user_id']), 'MOD_STANDARD');
830 mass_auth('user_role', $forum_id, phpbb_user_id($access['user_id']), 'FORUM_STANDARD');
835 foreach ($group_access as $forum_id => $access_map)
837 $forum_id = (int) $forum_id;
839 foreach ($access_map as $access)
841 if (isset($access['auth_mod']) && $access['auth_mod'] == 1)
843 mass_auth('group_role', $forum_id, $access['group_id'], 'MOD_STANDARD');
844 mass_auth('group_role', $forum_id, $access['group_id'], 'FORUM_STANDARD');
849 // We grant everyone readonly access to the categories to ensure that the forums are visible
850 $sql = 'SELECT forum_id, forum_name, parent_id, left_id, right_id
851 FROM ' . FORUMS_TABLE . '
852 ORDER BY left_id ASC';
853 $result = $db->sql_query($sql);
855 $parent_forums = $forums = array();
856 while ($row = $db->sql_fetchrow($result))
858 if ($row['parent_id'] == 0)
860 mass_auth('group_role', $row['forum_id'], 'administrators', 'FORUM_FULL');
861 $parent_forums[] = $row;
863 else
865 $forums[] = $row;
868 $db->sql_freeresult($result);
870 global $auth;
872 // Let us see if guests/registered users have access to these forums...
873 foreach ($parent_forums as $row)
875 // Get the childs...
876 $branch = $forum_ids = array();
878 foreach ($forums as $key => $_row)
880 if ($_row['left_id'] > $row['left_id'] && $_row['left_id'] < $row['right_id'])
882 $branch[] = $_row;
883 $forum_ids[] = $_row['forum_id'];
884 continue;
888 if (sizeof($forum_ids))
890 // Now make sure the user is able to read these forums
891 $hold_ary = $auth->acl_group_raw_data(get_group_id('guests'), 'f_list', $forum_ids);
893 if (!empty($hold_ary))
895 mass_auth('group', $row['forum_id'], 'guests', 'f_list', ACL_YES);
896 mass_auth('group', $row['forum_id'], 'registered', 'f_list', ACL_YES);
904 * Set primary group.
905 * Really simple and only based on user_level (remaining groups will be assigned later)
907 function phpbb_set_primary_group($user_level)
909 global $convert_row;
911 if ($user_level == 1)
913 return get_group_id('administrators');
915 /* else if ($user_level == 2)
917 return get_group_id('global_moderators');
919 else if ($user_level == 0 && $convert_row['user_active'])*/
920 else if ($convert_row['user_active'])
922 return get_group_id('registered');
925 return 0;
929 * Convert the group name, making sure to avoid conflicts with 3.0 special groups
931 function phpbb_convert_group_name($group_name)
933 $default_groups = array(
934 'GUESTS',
935 'REGISTERED',
936 'REGISTERED_COPPA',
937 'GLOBAL_MODERATORS',
938 'ADMINISTRATORS',
939 'BOTS',
942 if (in_array(strtoupper($group_name), $default_groups))
944 return 'phpBB2 - ' . $group_name;
947 return phpbb_set_encoding($group_name, false);
951 * Convert the group type constants
953 function phpbb_convert_group_type($group_type)
955 switch ($group_type)
957 case 0:
958 return GROUP_OPEN;
959 break;
961 case 1:
962 return GROUP_CLOSED;
963 break;
965 case 2:
966 return GROUP_HIDDEN;
967 break;
970 return GROUP_SPECIAL;
974 * Convert the topic type constants
976 function phpbb_convert_topic_type($topic_type)
978 switch ($topic_type)
980 case 0:
981 return POST_NORMAL;
982 break;
984 case 1:
985 return POST_STICKY;
986 break;
988 case 2:
989 return POST_ANNOUNCE;
990 break;
992 case 3:
993 return POST_GLOBAL;
994 break;
997 return POST_NORMAL;
1001 * Reparse the message stripping out the bbcode_uid values and adding new ones and setting the bitfield
1002 * @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
1004 function phpbb_prepare_message($message)
1006 global $phpbb_root_path, $phpEx, $db, $convert, $user, $config, $cache, $convert_row, $message_parser;
1008 if (!$message)
1010 $convert->row['mp_bbcode_bitfield'] = $convert_row['mp_bbcode_bitfield'] = 0;
1011 return '';
1014 // Decode phpBB 2.0.x Message
1015 if (isset($convert->row['old_bbcode_uid']) && $convert->row['old_bbcode_uid'] != '')
1017 $message = preg_replace('/\:(([a-z0-9]:)?)' . $convert->row['old_bbcode_uid'] . '/s', '', $message);
1020 if (strpos($message, '[quote=') !== false)
1022 $message = preg_replace('/\[quote="(.*?)"\]/s', '[quote=&quot;\1&quot;]', $message);
1025 $user_id = $convert->row['poster_id'];
1027 $message = str_replace('<', '&lt;', $message);
1028 $message = str_replace('>', '&gt;', $message);
1029 $message = str_replace('<br />', "\n", $message);
1031 // make the post UTF-8
1032 $message = phpbb_set_encoding($message);
1034 $message_parser->warn_msg = array(); // Reset the errors from the previous message
1035 $message_parser->bbcode_uid = make_uid($convert->row['post_time']);
1036 $message_parser->message = $message;
1037 unset($message);
1039 // Make sure options are set.
1040 // $enable_html = (!isset($row['enable_html'])) ? false : $row['enable_html'];
1041 $enable_bbcode = (!isset($convert->row['enable_bbcode'])) ? true : $convert->row['enable_bbcode'];
1042 $enable_smilies = (!isset($convert->row['enable_smilies'])) ? true : $convert->row['enable_smilies'];
1043 $enable_magic_url = (!isset($convert->row['enable_magic_url'])) ? true : $convert->row['enable_magic_url'];
1045 // 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')
1046 $message_parser->parse($enable_bbcode, $enable_magic_url, $enable_smilies);
1048 if (sizeof($message_parser->warn_msg))
1050 $msg_id = isset($convert->row['post_id']) ? $convert->row['post_id'] : $convert->row['privmsgs_id'];
1051 $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);
1054 $convert->row['mp_bbcode_bitfield'] = $convert_row['mp_bbcode_bitfield'] = $message_parser->bbcode_bitfield;
1056 $message = $message_parser->message;
1057 unset($message_parser->message);
1059 return $message;
1063 * Return the bitfield calculated by the previous function
1065 function get_bbcode_bitfield()
1067 global $convert_row;
1069 return $convert_row['mp_bbcode_bitfield'];
1073 * Determine the last user to edit a post
1074 * 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
1076 function phpbb_post_edit_user()
1078 global $convert_row, $config;
1080 if (isset($convert_row['post_edit_count']))
1082 return phpbb_user_id($convert_row['poster_id']);
1085 return 0;
1089 * Obtain the path to uploaded files on the 2.0.x forum
1090 * This is only used if the Attachment MOD was installed
1092 function phpbb_get_files_dir()
1094 if (!defined('MOD_ATTACHMENT'))
1096 return;
1099 global $db, $convert, $user, $config, $cache;
1101 $sql = 'SELECT config_value AS upload_dir
1102 FROM ' . $convert->src_table_prefix . "attachments_config
1103 WHERE config_name = 'upload_dir'";
1104 $result = $db->sql_query($sql);
1105 $upload_path = $db->sql_fetchfield('upload_dir');
1106 $db->sql_freeresult($result);
1108 $sql = 'SELECT config_value AS ftp_upload
1109 FROM ' . $convert->src_table_prefix . "attachments_config
1110 WHERE config_name = 'allow_ftp_upload'";
1111 $result = $db->sql_query($sql);
1112 $ftp_upload = (int) $db->sql_fetchfield('ftp_upload');
1113 $db->sql_freeresult($result);
1115 if ($ftp_upload)
1117 $convert->p_master->error($user->lang['CONV_ERROR_ATTACH_FTP_DIR'], __LINE__, __FILE__);
1120 return $upload_path;
1124 * Copy thumbnails of uploaded images from the 2.0.x forum
1125 * This is only used if the Attachment MOD was installed
1127 function phpbb_copy_thumbnails()
1129 global $db, $convert, $user, $config, $cache, $phpbb_root_path;
1131 $src_path = $convert->convertor_status['forum_path'] . '/' . phpbb_get_files_dir() . '/thumbs/';
1133 if ($handle = @opendir($src_path))
1135 while ($entry = readdir($handle))
1137 if ($entry[0] == '.')
1139 continue;
1142 if (is_dir($src_path . $entry))
1144 continue;
1146 else
1148 copy_file($src_path . $entry, $config['upload_path'] . '/' . preg_replace('/^t_/', 'thumb_', $entry));
1149 @unlink($phpbb_root_path . $config['upload_path'] . '/thumbs/' . $entry);
1152 closedir($handle);
1157 * Convert the attachment category constants
1158 * This is only used if the Attachment MOD was installed
1160 function phpbb_attachment_category($cat_id)
1162 switch ($cat_id)
1164 case 1:
1165 return ATTACHMENT_CATEGORY_IMAGE;
1166 break;
1168 case 2:
1169 return ATTACHMENT_CATEGORY_WM;
1170 break;
1172 case 3:
1173 return ATTACHMENT_CATEGORY_FLASH;
1174 break;
1177 return ATTACHMENT_CATEGORY_NONE;
1181 * Obtain list of forums in which different attachment categories can be used
1183 function phpbb_attachment_forum_perms($forum_permissions)
1185 if (empty($forum_permissions))
1187 return '';
1190 // Decode forum permissions
1191 $forum_ids = array();
1193 $one_char_encoding = '#';
1194 $two_char_encoding = '.';
1196 $auth_len = 1;
1197 for ($pos = 0; $pos < strlen($forum_permissions); $pos += $auth_len)
1199 $forum_auth = substr($forum_permissions, $pos, 1);
1200 if ($forum_auth == $one_char_encoding)
1202 $auth_len = 1;
1203 continue;
1205 else if ($forum_auth == $two_char_encoding)
1207 $auth_len = 2;
1208 $pos--;
1209 continue;
1212 $forum_auth = substr($forum_permissions, $pos, $auth_len);
1213 $forum_id = base64_unpack($forum_auth);
1215 $forum_ids[] = (int) $forum_id;
1218 if (sizeof($forum_ids))
1220 return attachment_forum_perms($forum_ids);
1223 return '';
1227 * Convert the avatar type constants
1229 function phpbb_avatar_type($type)
1231 switch ($type)
1233 case 1:
1234 return AVATAR_UPLOAD;
1235 break;
1237 case 2:
1238 return AVATAR_REMOTE;
1239 break;
1241 case 3:
1242 return AVATAR_GALLERY;
1243 break;
1246 return 0;
1250 * Transfer avatars, copying the image if it was uploaded
1252 function phpbb_import_avatar($user_avatar)
1254 global $convert_row;
1256 if (!$convert_row['user_avatar_type'])
1258 return '';
1260 else if ($convert_row['user_avatar_type'] == 1)
1262 // Uploaded avatar
1263 return import_avatar($user_avatar, '');
1265 else if ($convert_row['user_avatar_type'] == 2)
1267 // Remote avatar
1268 return $user_avatar;
1270 else if ($convert_row['user_avatar_type'] == 3)
1272 // Gallery avatar
1273 return $user_avatar;
1276 return '';
1280 * Calculate the correct to_address field for private messages
1282 function phpbb_privmsgs_to_userid($to_userid)
1284 global $config;
1286 return 'u_' . phpbb_user_id($to_userid);
1290 * Calculate whether a private message was unread using the bitfield
1292 function phpbb_unread_pm($pm_type)
1294 return ($pm_type == 5) ? 1 : 0;
1298 * Calculate whether a private message was new using the bitfield
1300 function phpbb_new_pm($pm_type)
1302 return ($pm_type == 1) ? 1 : 0;
1306 * Obtain the folder_id for the custom folder created to replace the savebox from 2.0.x (used to store saved private messages)
1308 function phpbb_get_savebox_id($user_id)
1310 global $db;
1312 $user_id = phpbb_user_id($user_id);
1314 // Only one custom folder, check only one
1315 $sql = 'SELECT folder_id
1316 FROM ' . PRIVMSGS_FOLDER_TABLE . '
1317 WHERE user_id = ' . $user_id;
1318 $result = $db->sql_query_limit($sql, 1);
1319 $folder_id = (int) $db->sql_fetchfield('folder_id');
1320 $db->sql_freeresult($result);
1322 return $folder_id;
1326 * Transfer attachment specific configuration options
1327 * These were not stored in the main config table on 2.0.x
1328 * This is only used if the Attachment MOD was installed
1330 function phpbb_import_attach_config()
1332 global $db, $convert, $config;
1334 $sql = 'SELECT *
1335 FROM ' . $convert->src_table_prefix . 'attachments_config';
1336 $result = $db->sql_query($sql);
1338 $attach_config = array();
1339 while ($row = $db->sql_fetchrow($result))
1341 $attach_config[$row['config_name']] = $row['config_value'];
1343 $db->sql_freeresult($result);
1345 set_config('allow_attachments', 1);
1347 // old attachment mod? Must be very old if this entry do not exist...
1348 if (!empty($attach_config['display_order']))
1350 set_config('display_order', $attach_config['display_order']);
1352 set_config('max_filesize', $attach_config['max_filesize']);
1353 set_config('max_filesize_pm', $attach_config['max_filesize_pm']);
1354 set_config('attachment_quota', $attach_config['attachment_quota']);
1355 set_config('max_attachments', $attach_config['max_attachments']);
1356 set_config('max_attachments_pm', $attach_config['max_attachments_pm']);
1357 set_config('allow_pm_attach', $attach_config['allow_pm_attach']);
1359 set_config('img_display_inlined', $attach_config['img_display_inlined']);
1360 set_config('img_max_width', $attach_config['img_max_width']);
1361 set_config('img_max_height', $attach_config['img_max_height']);
1362 set_config('img_link_width', $attach_config['img_link_width']);
1363 set_config('img_link_height', $attach_config['img_link_height']);
1364 set_config('img_create_thumbnail', $attach_config['img_create_thumbnail']);
1365 set_config('img_max_thumb_width', 400);
1366 set_config('img_min_thumb_filesize', $attach_config['img_min_thumb_filesize']);
1367 set_config('img_imagick', $attach_config['img_imagick']);
1371 * Calculate the date a user became inactive
1373 function phpbb_inactive_time()
1375 global $convert_row;
1377 if ($convert_row['user_active'])
1379 return 0;
1382 if ($convert_row['user_lastvisit'])
1384 return $convert_row['user_lastvisit'];
1387 return $convert_row['user_regdate'];
1391 * Calculate the reason a user became inactive
1392 * We can't actually tell the difference between a manual deactivation and one for profile changes
1393 * from the data available to assume the latter
1395 function phpbb_inactive_reason()
1397 global $convert_row;
1399 if ($convert_row['user_active'])
1401 return 0;
1404 if ($convert_row['user_lastvisit'])
1406 return INACTIVE_PROFILE;
1409 return INACTIVE_REGISTER;