6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
20 * Recalculate Nested Sets
22 * @param int $new_id first left_id (should start with 1)
23 * @param string $pkey primary key-column (containing the id for the parent_id of the children)
24 * @param string $table constant or fullname of the table
25 * @param int $parent_id parent_id of the current set (default = 0)
26 * @param array $where contains strings to compare closer on the where statement (additional)
30 function recalc_nested_sets(&$new_id, $pkey, $table, $parent_id = 0, $where = array())
36 WHERE parent_id = ' . (int) $parent_id .
37 ((!empty($where)) ? ' AND ' . implode(' AND ', $where) : '') . '
38 ORDER BY left_id ASC';
39 $result = $db->sql_query($sql);
40 while ($row = $db->sql_fetchrow($result))
42 // First we update the left_id for this module
43 if ($row['left_id'] != $new_id)
45 $db->sql_query('UPDATE ' . $table . ' SET ' . $db->sql_build_array('UPDATE', array('left_id' => $new_id)) . " WHERE $pkey = {$row[$pkey]}");
49 // Then we go through any children and update their left/right id's
50 recalc_nested_sets($new_id, $pkey, $table, $row[$pkey], $where);
52 // Then we come back and update the right_id for this module
53 if ($row['right_id'] != $new_id)
55 $db->sql_query('UPDATE ' . $table . ' SET ' . $db->sql_build_array('UPDATE', array('right_id' => $new_id)) . " WHERE $pkey = {$row[$pkey]}");
59 $db->sql_freeresult($result);
63 * Simple version of jumpbox, just lists authed forums
65 function make_forum_select($select_id = false, $ignore_id = false, $ignore_acl = false, $ignore_nonpost = false, $ignore_emptycat = true, $only_acl_post = false, $return_array = false)
67 global $db, $user, $auth;
69 // This query is identical to the jumpbox one
70 $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, forum_flags, forum_options, left_id, right_id
71 FROM ' . FORUMS_TABLE . '
72 ORDER BY left_id ASC';
73 $result = $db->sql_query($sql, 600);
76 $padding_store = array('0' => '');
78 $forum_list = ($return_array) ? array() : '';
80 // Sometimes it could happen that forums will be displayed here not be displayed within the index page
81 // This is the result of forums not displayed at index, having list permissions and a parent of a forum with no permissions.
82 // If this happens, the padding could be "broken"
84 while ($row = $db->sql_fetchrow($result))
86 if ($row['left_id'] < $right)
88 $padding .= ' ';
89 $padding_store[$row['parent_id']] = $padding;
91 else if ($row['left_id'] > $right + 1)
93 $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
96 $right = $row['right_id'];
99 if (!$ignore_acl && $auth->acl_gets(array('f_list', 'a_forum', 'a_forumadd', 'a_forumdel'), $row['forum_id']))
101 if ($only_acl_post && !$auth->acl_get('f_post', $row['forum_id']) || (!$auth->acl_get('m_approve', $row['forum_id']) && !$auth->acl_get('f_noapprove', $row['forum_id'])))
106 else if (!$ignore_acl)
112 ((is_array($ignore_id) && in_array($row['forum_id'], $ignore_id)) || $row['forum_id'] == $ignore_id)
114 // Non-postable forum with no subforums, don't display
115 ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']) && $ignore_emptycat)
117 ($row['forum_type'] != FORUM_POST && $ignore_nonpost)
125 // Include some more information...
126 $selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? true : false) : (($row['forum_id'] == $select_id) ? true : false);
127 $forum_list[$row['forum_id']] = array_merge(array('padding' => $padding, 'selected' => ($selected && !$disabled), 'disabled' => $disabled), $row);
131 $selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? ' selected="selected"' : '') : (($row['forum_id'] == $select_id) ? ' selected="selected"' : '');
132 $forum_list .= '<option value="' . $row['forum_id'] . '"' . (($disabled) ? ' disabled="disabled" class="disabled-option"' : $selected) . '>' . $padding . $row['forum_name'] . '</option>';
135 $db->sql_freeresult($result);
136 unset($padding_store);
142 * Generate size select options
144 function size_select_options($size_compare)
148 $size_types_text = array($user->lang['BYTES'], $user->lang['KIB'], $user->lang['MIB']);
149 $size_types = array('b', 'kb', 'mb');
151 $s_size_options = '';
153 for ($i = 0, $size = sizeof($size_types_text); $i < $size; $i++)
155 $selected = ($size_compare == $size_types[$i]) ? ' selected="selected"' : '';
156 $s_size_options .= '<option value="' . $size_types[$i] . '"' . $selected . '>' . $size_types_text[$i] . '</option>';
159 return $s_size_options;
163 * Generate list of groups (option fields without select)
165 * @param int $group_id The default group id to mark as selected
166 * @param array $exclude_ids The group ids to exclude from the list, false (default) if you whish to exclude no id
167 * @param int $manage_founder If set to false (default) all groups are returned, if 0 only those groups returned not being managed by founders only, if 1 only those groups returned managed by founders only.
169 * @return string The list of options.
171 function group_select_options($group_id, $exclude_ids = false, $manage_founder = false)
173 global $db, $user, $config;
175 $exclude_sql = ($exclude_ids !== false && sizeof($exclude_ids)) ? 'WHERE ' . $db->sql_in_set('group_id', array_map('intval', $exclude_ids), true) : '';
176 $sql_and = (!$config['coppa_enable']) ? (($exclude_sql) ? ' AND ' : ' WHERE ') . "group_name <> 'REGISTERED_COPPA'" : '';
177 $sql_founder = ($manage_founder !== false) ? (($exclude_sql || $sql_and) ? ' AND ' : ' WHERE ') . 'group_founder_manage = ' . (int) $manage_founder : '';
179 $sql = 'SELECT group_id, group_name, group_type
180 FROM ' . GROUPS_TABLE . "
184 ORDER BY group_type DESC, group_name ASC";
185 $result = $db->sql_query($sql);
187 $s_group_options = '';
188 while ($row = $db->sql_fetchrow($result))
190 $selected = ($row['group_id'] == $group_id) ? ' selected="selected"' : '';
191 $s_group_options .= '<option' . (($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '') . ' value="' . $row['group_id'] . '"' . $selected . '>' . (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']) . '</option>';
193 $db->sql_freeresult($result);
195 return $s_group_options;
199 * Obtain authed forums list
201 function get_forum_list($acl_list = 'f_list', $id_only = true, $postable_only = false, $no_cache = false)
206 if (!isset($forum_rows))
208 // This query is identical to the jumpbox one
209 $expire_time = ($no_cache) ? 0 : 600;
211 $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
212 FROM ' . FORUMS_TABLE . '
213 ORDER BY left_id ASC';
214 $result = $db->sql_query($sql, $expire_time);
216 $forum_rows = array();
218 $right = $padding = 0;
219 $padding_store = array('0' => 0);
221 while ($row = $db->sql_fetchrow($result))
223 if ($row['left_id'] < $right)
226 $padding_store[$row['parent_id']] = $padding;
228 else if ($row['left_id'] > $right + 1)
230 // Ok, if the $padding_store for this parent is empty there is something wrong. For now we will skip over it.
231 // @todo digging deep to find out "how" this can happen.
232 $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : $padding;
235 $right = $row['right_id'];
236 $row['padding'] = $padding;
238 $forum_rows[] = $row;
240 $db->sql_freeresult($result);
241 unset($padding_store);
245 foreach ($forum_rows as $row)
247 if ($postable_only && $row['forum_type'] != FORUM_POST)
252 if ($acl_list == '' || ($acl_list != '' && $auth->acl_gets($acl_list, $row['forum_id'])))
254 $rowset[] = ($id_only) ? (int) $row['forum_id'] : $row;
264 function get_forum_branch($forum_id, $type = 'all', $order = 'descending', $include_forum = true)
271 $condition = 'f1.left_id BETWEEN f2.left_id AND f2.right_id';
275 $condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id';
279 $condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id OR f1.left_id BETWEEN f2.left_id AND f2.right_id';
286 FROM ' . FORUMS_TABLE . ' f1
287 LEFT JOIN ' . FORUMS_TABLE . " f2 ON ($condition)
288 WHERE f1.forum_id = $forum_id
289 ORDER BY f2.left_id " . (($order == 'descending') ? 'ASC' : 'DESC');
290 $result = $db->sql_query($sql);
292 while ($row = $db->sql_fetchrow($result))
294 if (!$include_forum && $row['forum_id'] == $forum_id)
301 $db->sql_freeresult($result);
307 * Copies permissions from one forum to others
309 * @param int $src_forum_id The source forum we want to copy permissions from
310 * @param array $dest_forum_ids The destination forum(s) we want to copy to
311 * @param bool $clear_dest_perms True if destination permissions should be deleted
312 * @param bool $add_log True if log entry should be added
314 * @return bool False on error
318 function copy_forum_permissions($src_forum_id, $dest_forum_ids, $clear_dest_perms = true, $add_log = true)
322 // Only one forum id specified
323 if (!is_array($dest_forum_ids))
325 $dest_forum_ids = array($dest_forum_ids);
328 // Make sure forum ids are integers
329 $src_forum_id = (int) $src_forum_id;
330 $dest_forum_ids = array_map('intval', $dest_forum_ids);
332 // No source forum or no destination forums specified
333 if (empty($src_forum_id) || empty($dest_forum_ids))
338 // Check if source forum exists
339 $sql = 'SELECT forum_name
340 FROM ' . FORUMS_TABLE . '
341 WHERE forum_id = ' . $src_forum_id;
342 $result = $db->sql_query($sql);
343 $src_forum_name = $db->sql_fetchfield('forum_name');
344 $db->sql_freeresult($result);
346 // Source forum doesn't exist
347 if (empty($src_forum_name))
352 // Check if destination forums exists
353 $sql = 'SELECT forum_id, forum_name
354 FROM ' . FORUMS_TABLE . '
355 WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
356 $result = $db->sql_query($sql);
358 $dest_forum_ids = $dest_forum_names = array();
359 while ($row = $db->sql_fetchrow($result))
361 $dest_forum_ids[] = (int) $row['forum_id'];
362 $dest_forum_names[] = $row['forum_name'];
364 $db->sql_freeresult($result);
366 // No destination forum exists
367 if (empty($dest_forum_ids))
372 // From the mysql documentation:
373 // Prior to MySQL 4.0.14, the target table of the INSERT statement cannot appear
374 // in the FROM clause of the SELECT part of the query. This limitation is lifted in 4.0.14.
375 // Due to this we stay on the safe side if we do the insertion "the manual way"
377 // Rowsets we're going to insert
378 $users_sql_ary = $groups_sql_ary = array();
380 // Query acl users table for source forum data
381 $sql = 'SELECT user_id, auth_option_id, auth_role_id, auth_setting
382 FROM ' . ACL_USERS_TABLE . '
383 WHERE forum_id = ' . $src_forum_id;
384 $result = $db->sql_query($sql);
386 while ($row = $db->sql_fetchrow($result))
389 'user_id' => (int) $row['user_id'],
390 'auth_option_id' => (int) $row['auth_option_id'],
391 'auth_role_id' => (int) $row['auth_role_id'],
392 'auth_setting' => (int) $row['auth_setting'],
395 foreach ($dest_forum_ids as $dest_forum_id)
397 $users_sql_ary[] = $row + array('forum_id' => $dest_forum_id);
400 $db->sql_freeresult($result);
402 // Query acl groups table for source forum data
403 $sql = 'SELECT group_id, auth_option_id, auth_role_id, auth_setting
404 FROM ' . ACL_GROUPS_TABLE . '
405 WHERE forum_id = ' . $src_forum_id;
406 $result = $db->sql_query($sql);
408 while ($row = $db->sql_fetchrow($result))
411 'group_id' => (int) $row['group_id'],
412 'auth_option_id' => (int) $row['auth_option_id'],
413 'auth_role_id' => (int) $row['auth_role_id'],
414 'auth_setting' => (int) $row['auth_setting'],
417 foreach ($dest_forum_ids as $dest_forum_id)
419 $groups_sql_ary[] = $row + array('forum_id' => $dest_forum_id);
422 $db->sql_freeresult($result);
424 $db->sql_transaction('begin');
426 // Clear current permissions of destination forums
427 if ($clear_dest_perms)
429 $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
430 WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
431 $db->sql_query($sql);
433 $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
434 WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
435 $db->sql_query($sql);
438 $db->sql_multi_insert(ACL_USERS_TABLE, $users_sql_ary);
439 $db->sql_multi_insert(ACL_GROUPS_TABLE, $groups_sql_ary);
443 add_log('admin', 'LOG_FORUM_COPIED_PERMISSIONS', $src_forum_name, implode(', ', $dest_forum_names));
446 $db->sql_transaction('commit');
452 * Get physical file listing
454 function filelist($rootdir, $dir = '', $type = 'gif|jpg|jpeg|png')
456 $matches = array($dir => array());
458 // Remove initial / if present
459 $rootdir = (substr($rootdir, 0, 1) == '/') ? substr($rootdir, 1) : $rootdir;
460 // Add closing / if not present
461 $rootdir = ($rootdir && substr($rootdir, -1) != '/') ? $rootdir . '/' : $rootdir;
463 // Remove initial / if present
464 $dir = (substr($dir, 0, 1) == '/') ? substr($dir, 1) : $dir;
465 // Add closing / if not present
466 $dir = ($dir && substr($dir, -1) != '/') ? $dir . '/' : $dir;
468 if (!is_dir($rootdir . $dir))
473 $dh = @opendir($rootdir . $dir);
480 while (($fname = readdir($dh)) !== false)
482 if (is_file("$rootdir$dir$fname"))
484 if (filesize("$rootdir$dir$fname") && preg_match('#\.' . $type . '$#i', $fname))
486 $matches[$dir][] = $fname;
489 else if ($fname[0] != '.' && is_dir("$rootdir$dir$fname"))
491 $matches += filelist($rootdir, "$dir$fname", $type);
502 function move_topics($topic_ids, $forum_id, $auto_sync = true)
506 if (empty($topic_ids))
511 $forum_ids = array($forum_id);
513 if (!is_array($topic_ids))
515 $topic_ids = array($topic_ids);
518 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
519 WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids) . '
520 AND forum_id = ' . $forum_id;
521 $db->sql_query($sql);
525 $sql = 'SELECT DISTINCT forum_id
526 FROM ' . TOPICS_TABLE . '
527 WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
528 $result = $db->sql_query($sql);
530 while ($row = $db->sql_fetchrow($result))
532 $forum_ids[] = $row['forum_id'];
534 $db->sql_freeresult($result);
537 $table_ary = array(TOPICS_TABLE, POSTS_TABLE, LOG_TABLE, DRAFTS_TABLE, TOPICS_TRACK_TABLE);
538 foreach ($table_ary as $table)
540 $sql = "UPDATE $table
541 SET forum_id = $forum_id
542 WHERE " . $db->sql_in_set('topic_id', $topic_ids);
543 $db->sql_query($sql);
549 sync('forum', 'forum_id', $forum_ids, true, true);
557 function move_posts($post_ids, $topic_id, $auto_sync = true)
561 if (!is_array($post_ids))
563 $post_ids = array($post_ids);
566 $forum_ids = array();
567 $topic_ids = array($topic_id);
569 $sql = 'SELECT DISTINCT topic_id, forum_id
570 FROM ' . POSTS_TABLE . '
571 WHERE ' . $db->sql_in_set('post_id', $post_ids);
572 $result = $db->sql_query($sql);
574 while ($row = $db->sql_fetchrow($result))
576 $forum_ids[] = (int) $row['forum_id'];
577 $topic_ids[] = (int) $row['topic_id'];
579 $db->sql_freeresult($result);
581 $sql = 'SELECT forum_id
582 FROM ' . TOPICS_TABLE . '
583 WHERE topic_id = ' . $topic_id;
584 $result = $db->sql_query($sql);
585 $forum_row = $db->sql_fetchrow($result);
586 $db->sql_freeresult($result);
590 trigger_error('NO_TOPIC');
593 $sql = 'UPDATE ' . POSTS_TABLE . '
594 SET forum_id = ' . (int) $forum_row['forum_id'] . ", topic_id = $topic_id
595 WHERE " . $db->sql_in_set('post_id', $post_ids);
596 $db->sql_query($sql);
598 $sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
599 SET topic_id = $topic_id, in_message = 0
600 WHERE " . $db->sql_in_set('post_msg_id', $post_ids);
601 $db->sql_query($sql);
605 $forum_ids[] = (int) $forum_row['forum_id'];
607 sync('topic_reported', 'topic_id', $topic_ids);
608 sync('topic_attachment', 'topic_id', $topic_ids);
609 sync('topic', 'topic_id', $topic_ids, true);
610 sync('forum', 'forum_id', $forum_ids, true, true);
613 // Update posted information
614 update_posted_info($topic_ids);
620 function delete_topics($where_type, $where_ids, $auto_sync = true, $post_count_sync = true, $call_delete_posts = true)
624 $approved_topics = 0;
625 $forum_ids = $topic_ids = array();
627 if ($where_type === 'range')
629 $where_clause = $where_ids;
633 $where_ids = (is_array($where_ids)) ? array_unique($where_ids) : array($where_ids);
635 if (!sizeof($where_ids))
637 return array('topics' => 0, 'posts' => 0);
640 $where_clause = $db->sql_in_set($where_type, $where_ids);
643 // Making sure that delete_posts does not call delete_topics again...
645 'posts' => ($call_delete_posts) ? delete_posts($where_type, $where_ids, false, true, $post_count_sync, false) : 0,
648 $sql = 'SELECT topic_id, forum_id, topic_approved, topic_moved_id
649 FROM ' . TOPICS_TABLE . '
650 WHERE ' . $where_clause;
651 $result = $db->sql_query($sql);
653 while ($row = $db->sql_fetchrow($result))
655 $forum_ids[] = $row['forum_id'];
656 $topic_ids[] = $row['topic_id'];
658 if ($row['topic_approved'] && !$row['topic_moved_id'])
663 $db->sql_freeresult($result);
665 $return['topics'] = sizeof($topic_ids);
667 if (!sizeof($topic_ids))
672 $db->sql_transaction('begin');
674 $table_ary = array(BOOKMARKS_TABLE, TOPICS_TRACK_TABLE, TOPICS_POSTED_TABLE, POLL_VOTES_TABLE, POLL_OPTIONS_TABLE, TOPICS_WATCH_TABLE, TOPICS_TABLE);
676 foreach ($table_ary as $table)
678 $sql = "DELETE FROM $table
679 WHERE " . $db->sql_in_set('topic_id', $topic_ids);
680 $db->sql_query($sql);
684 $moved_topic_ids = array();
686 // update the other forums
687 $sql = 'SELECT topic_id, forum_id
688 FROM ' . TOPICS_TABLE . '
689 WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids);
690 $result = $db->sql_query($sql);
692 while ($row = $db->sql_fetchrow($result))
694 $forum_ids[] = $row['forum_id'];
695 $moved_topic_ids[] = $row['topic_id'];
697 $db->sql_freeresult($result);
699 if (sizeof($moved_topic_ids))
701 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
702 WHERE ' . $db->sql_in_set('topic_id', $moved_topic_ids);
703 $db->sql_query($sql);
706 $db->sql_transaction('commit');
710 sync('forum', 'forum_id', array_unique($forum_ids), true, true);
711 sync('topic_reported', $where_type, $where_ids);
714 if ($approved_topics)
716 set_config_count('num_topics', $approved_topics * (-1), true);
725 function delete_posts($where_type, $where_ids, $auto_sync = true, $posted_sync = true, $post_count_sync = true, $call_delete_topics = true)
727 global $db, $config, $phpbb_root_path, $phpEx;
729 if ($where_type === 'range')
731 $where_clause = $where_ids;
735 if (is_array($where_ids))
737 $where_ids = array_unique($where_ids);
741 $where_ids = array($where_ids);
744 if (!sizeof($where_ids))
749 $where_ids = array_map('intval', $where_ids);
751 /* Possible code for splitting post deletion
752 if (sizeof($where_ids) >= 1001)
754 // Split into chunks of 1000
755 $chunks = array_chunk($where_ids, 1000);
757 foreach ($chunks as $_where_ids)
759 delete_posts($where_type, $_where_ids, $auto_sync, $posted_sync, $post_count_sync, $call_delete_topics);
765 $where_clause = $db->sql_in_set($where_type, $where_ids);
769 $post_ids = $topic_ids = $forum_ids = $post_counts = $remove_topics = array();
771 $sql = 'SELECT post_id, poster_id, post_approved, post_postcount, topic_id, forum_id
772 FROM ' . POSTS_TABLE . '
773 WHERE ' . $where_clause;
774 $result = $db->sql_query($sql);
776 while ($row = $db->sql_fetchrow($result))
778 $post_ids[] = (int) $row['post_id'];
779 $poster_ids[] = (int) $row['poster_id'];
780 $topic_ids[] = (int) $row['topic_id'];
781 $forum_ids[] = (int) $row['forum_id'];
783 if ($row['post_postcount'] && $post_count_sync && $row['post_approved'])
785 $post_counts[$row['poster_id']] = (!empty($post_counts[$row['poster_id']])) ? $post_counts[$row['poster_id']] + 1 : 1;
788 if ($row['post_approved'])
793 $db->sql_freeresult($result);
795 if (!sizeof($post_ids))
800 $db->sql_transaction('begin');
802 $table_ary = array(POSTS_TABLE, REPORTS_TABLE);
804 foreach ($table_ary as $table)
806 $sql = "DELETE FROM $table
807 WHERE " . $db->sql_in_set('post_id', $post_ids);
808 $db->sql_query($sql);
812 // Adjust users post counts
813 if (sizeof($post_counts) && $post_count_sync)
815 foreach ($post_counts as $poster_id => $substract)
817 $sql = 'UPDATE ' . USERS_TABLE . '
819 WHERE user_id = ' . $poster_id . '
820 AND user_posts < ' . $substract;
821 $db->sql_query($sql);
823 $sql = 'UPDATE ' . USERS_TABLE . '
824 SET user_posts = user_posts - ' . $substract . '
825 WHERE user_id = ' . $poster_id . '
826 AND user_posts >= ' . $substract;
827 $db->sql_query($sql);
831 // Remove topics now having no posts?
832 if (sizeof($topic_ids))
834 $sql = 'SELECT topic_id
835 FROM ' . POSTS_TABLE . '
836 WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
838 $result = $db->sql_query($sql);
840 while ($row = $db->sql_fetchrow($result))
842 $remove_topics[] = $row['topic_id'];
844 $db->sql_freeresult($result);
846 // Actually, those not within remove_topics should be removed. ;)
847 $remove_topics = array_diff($topic_ids, $remove_topics);
850 // Remove the message from the search index
851 $search_type = basename($config['search_type']);
853 if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
855 trigger_error('NO_SUCH_SEARCH_MODULE');
858 include_once("{$phpbb_root_path}includes/search/$search_type.$phpEx");
861 $search = new $search_type($error);
865 trigger_error($error);
868 $search->index_remove($post_ids, $poster_ids, $forum_ids);
870 delete_attachments('post', $post_ids, false);
872 $db->sql_transaction('commit');
874 // Resync topics_posted table
877 update_posted_info($topic_ids);
882 sync('topic_reported', 'topic_id', $topic_ids);
883 sync('topic', 'topic_id', $topic_ids, true);
884 sync('forum', 'forum_id', $forum_ids, true, true);
889 set_config_count('num_posts', $approved_posts * (-1), true);
892 // We actually remove topics now to not be inconsistent (the delete_topics function calls this function too)
893 if (sizeof($remove_topics) && $call_delete_topics)
895 delete_topics('topic_id', $remove_topics, $auto_sync, $post_count_sync, false);
898 return sizeof($post_ids);
904 * @param string $mode can be: post|message|topic|attach|user
905 * @param mixed $ids can be: post_ids, message_ids, topic_ids, attach_ids, user_ids
906 * @param bool $resync set this to false if you are deleting posts or topics
908 function delete_attachments($mode, $ids, $resync = true)
912 // 0 is as bad as an empty array
920 $ids = array_unique($ids);
921 $ids = array_map('intval', $ids);
925 $ids = array((int) $ids);
934 $sql_id = 'post_msg_id';
935 $sql_where = ' AND in_message = ' . ($mode == 'message' ? 1 : 0);
939 $sql_id = 'topic_id';
943 $sql_id = 'poster_id';
948 $sql_id = 'attach_id';
953 $post_ids = $message_ids = $topic_ids = $physical = array();
955 // Collect post and topic ids for later use if we need to touch remaining entries (if resync is enabled)
956 $sql = 'SELECT post_msg_id, topic_id, in_message, physical_filename, thumbnail, filesize, is_orphan
957 FROM ' . ATTACHMENTS_TABLE . '
958 WHERE ' . $db->sql_in_set($sql_id, $ids);
962 $result = $db->sql_query($sql);
964 while ($row = $db->sql_fetchrow($result))
966 // We only need to store post/message/topic ids if resync is enabled and the file is not orphaned
967 if ($resync && !$row['is_orphan'])
969 if (!$row['in_message'])
971 $post_ids[] = $row['post_msg_id'];
972 $topic_ids[] = $row['topic_id'];
976 $message_ids[] = $row['post_msg_id'];
980 $physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize'], 'is_orphan' => $row['is_orphan']);
982 $db->sql_freeresult($result);
984 // Delete attachments
985 $sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . '
986 WHERE ' . $db->sql_in_set($sql_id, $ids);
990 $db->sql_query($sql);
991 $num_deleted = $db->sql_affectedrows();
998 // Delete attachments from filesystem
999 $space_removed = $files_removed = 0;
1000 foreach ($physical as $file_ary)
1002 if (phpbb_unlink($file_ary['filename'], 'file', true) && !$file_ary['is_orphan'])
1004 // Only non-orphaned files count to the file size
1005 $space_removed += $file_ary['filesize'];
1009 if ($file_ary['thumbnail'])
1011 phpbb_unlink($file_ary['filename'], 'thumbnail', true);
1015 if ($space_removed || $files_removed)
1017 set_config_count('upload_dir_size', $space_removed * (-1), true);
1018 set_config_count('num_files', $files_removed * (-1), true);
1021 // If we do not resync, we do not need to adjust any message, post, topic or user entries
1024 return $num_deleted;
1027 // No more use for the original ids
1030 // Now, we need to resync posts, messages, topics. We go through every one of them
1031 $post_ids = array_unique($post_ids);
1032 $message_ids = array_unique($message_ids);
1033 $topic_ids = array_unique($topic_ids);
1035 // Update post indicators for posts now no longer having attachments
1036 if (sizeof($post_ids))
1038 // Just check which posts are still having an assigned attachment not orphaned by querying the attachments table
1039 $sql = 'SELECT post_msg_id
1040 FROM ' . ATTACHMENTS_TABLE . '
1041 WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
1044 $result = $db->sql_query($sql);
1046 $remaining_ids = array();
1047 while ($row = $db->sql_fetchrow($result))
1049 $remaining_ids[] = $row['post_msg_id'];
1051 $db->sql_freeresult($result);
1053 // Now only unset those ids remaining
1054 $post_ids = array_diff($post_ids, $remaining_ids);
1056 if (sizeof($post_ids))
1058 $sql = 'UPDATE ' . POSTS_TABLE . '
1059 SET post_attachment = 0
1060 WHERE ' . $db->sql_in_set('post_id', $post_ids);
1061 $db->sql_query($sql);
1065 // Update message table if messages are affected
1066 if (sizeof($message_ids))
1068 // Just check which messages are still having an assigned attachment not orphaned by querying the attachments table
1069 $sql = 'SELECT post_msg_id
1070 FROM ' . ATTACHMENTS_TABLE . '
1071 WHERE ' . $db->sql_in_set('post_msg_id', $message_ids) . '
1074 $result = $db->sql_query($sql);
1076 $remaining_ids = array();
1077 while ($row = $db->sql_fetchrow($result))
1079 $remaining_ids[] = $row['post_msg_id'];
1081 $db->sql_freeresult($result);
1083 // Now only unset those ids remaining
1084 $message_ids = array_diff($message_ids, $remaining_ids);
1086 if (sizeof($message_ids))
1088 $sql = 'UPDATE ' . PRIVMSGS_TABLE . '
1089 SET message_attachment = 0
1090 WHERE ' . $db->sql_in_set('msg_id', $message_ids);
1091 $db->sql_query($sql);
1095 // Now update the topics. This is a bit trickier, because there could be posts still having attachments within the topic
1096 if (sizeof($topic_ids))
1098 // Just check which topics are still having an assigned attachment not orphaned by querying the attachments table (much less entries expected)
1099 $sql = 'SELECT topic_id
1100 FROM ' . ATTACHMENTS_TABLE . '
1101 WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
1103 $result = $db->sql_query($sql);
1105 $remaining_ids = array();
1106 while ($row = $db->sql_fetchrow($result))
1108 $remaining_ids[] = $row['topic_id'];
1110 $db->sql_freeresult($result);
1112 // Now only unset those ids remaining
1113 $topic_ids = array_diff($topic_ids, $remaining_ids);
1115 if (sizeof($topic_ids))
1117 $sql = 'UPDATE ' . TOPICS_TABLE . '
1118 SET topic_attachment = 0
1119 WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1120 $db->sql_query($sql);
1124 return $num_deleted;
1128 * Deletes shadow topics pointing to a specified forum.
1130 * @param int $forum_id The forum id
1131 * @param string $sql_more Additional WHERE statement, e.g. t.topic_time < (time() - 1234)
1132 * @param bool $auto_sync Will call sync() if this is true
1134 * @return array Array with affected forums
1138 function delete_topic_shadows($forum_id, $sql_more = '', $auto_sync = true)
1148 // Set of affected forums we have to resync
1149 $sync_forum_ids = array();
1151 // Amount of topics we select and delete at once.
1156 $sql = 'SELECT t2.forum_id, t2.topic_id
1157 FROM ' . TOPICS_TABLE . ' t2, ' . TOPICS_TABLE . ' t
1158 WHERE t2.topic_moved_id = t.topic_id
1159 AND t.forum_id = ' . (int) $forum_id . '
1160 ' . (($sql_more) ? 'AND ' . $sql_more : '');
1161 $result = $db->sql_query_limit($sql, $batch_size);
1163 $topic_ids = array();
1164 while ($row = $db->sql_fetchrow($result))
1166 $topic_ids[] = (int) $row['topic_id'];
1168 $sync_forum_ids[(int) $row['forum_id']] = (int) $row['forum_id'];
1170 $db->sql_freeresult($result);
1172 if (!empty($topic_ids))
1174 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1175 WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1176 $db->sql_query($sql);
1179 while (sizeof($topic_ids) == $batch_size);
1183 sync('forum', 'forum_id', $sync_forum_ids, true, true);
1186 return $sync_forum_ids;
1190 * Update/Sync posted information for topics
1192 function update_posted_info(&$topic_ids)
1194 global $db, $config;
1196 if (empty($topic_ids) || !$config['load_db_track'])
1201 // First of all, let us remove any posted information for these topics
1202 $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
1203 WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1204 $db->sql_query($sql);
1206 // Now, let us collect the user/topic combos for rebuilding the information
1207 $sql = 'SELECT poster_id, topic_id
1208 FROM ' . POSTS_TABLE . '
1209 WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
1210 AND poster_id <> ' . ANONYMOUS . '
1211 GROUP BY poster_id, topic_id';
1212 $result = $db->sql_query($sql);
1215 while ($row = $db->sql_fetchrow($result))
1217 // Add as key to make them unique (grouping by) and circumvent empty keys on array_unique
1218 $posted[$row['poster_id']][] = $row['topic_id'];
1220 $db->sql_freeresult($result);
1222 // Now add the information...
1224 foreach ($posted as $user_id => $topic_row)
1226 foreach ($topic_row as $topic_id)
1229 'user_id' => (int) $user_id,
1230 'topic_id' => (int) $topic_id,
1231 'topic_posted' => 1,
1237 $db->sql_multi_insert(TOPICS_POSTED_TABLE, $sql_ary);
1241 * Delete attached file
1243 function phpbb_unlink($filename, $mode = 'file', $entry_removed = false)
1245 global $db, $phpbb_root_path, $config;
1247 // Because of copying topics or modifications a physical filename could be assigned more than once. If so, do not remove the file itself.
1248 $sql = 'SELECT COUNT(attach_id) AS num_entries
1249 FROM ' . ATTACHMENTS_TABLE . "
1250 WHERE physical_filename = '" . $db->sql_escape(utf8_basename($filename)) . "'";
1251 $result = $db->sql_query($sql);
1252 $num_entries = (int) $db->sql_fetchfield('num_entries');
1253 $db->sql_freeresult($result);
1255 // Do not remove file if at least one additional entry with the same name exist.
1256 if (($entry_removed && $num_entries > 0) || (!$entry_removed && $num_entries > 1))
1261 $filename = ($mode == 'thumbnail') ? 'thumb_' . utf8_basename($filename) : utf8_basename($filename);
1262 return @unlink($phpbb_root_path . $config['upload_path'] . '/' . $filename);
1266 * All-encompasing sync function
1270 * sync('topic', 'topic_id', 123); // resync topic #123
1271 * sync('topic', 'forum_id', array(2, 3)); // resync topics from forum #2 and #3
1272 * sync('topic'); // resync all topics
1273 * sync('topic', 'range', 'topic_id BETWEEN 1 AND 60'); // resync a range of topics/forums (only available for 'topic' and 'forum' modes)
1277 * - forum Resync complete forum
1278 * - topic Resync topics
1279 * - topic_moved Removes topic shadows that would be in the same forum as the topic they link to
1280 * - topic_approved Resyncs the topic_approved flag according to the status of the first post
1281 * - post_reported Resyncs the post_reported flag, relying on actual reports
1282 * - topic_reported Resyncs the topic_reported flag, relying on post_reported flags
1283 * - post_attachement Same as post_reported, but with attachment flags
1284 * - topic_attachement Same as topic_reported, but with attachment flags
1286 function sync($mode, $where_type = '', $where_ids = '', $resync_parents = false, $sync_extra = false)
1290 if (is_array($where_ids))
1292 $where_ids = array_unique($where_ids);
1293 $where_ids = array_map('intval', $where_ids);
1295 else if ($where_type != 'range')
1297 $where_ids = ($where_ids) ? array((int) $where_ids) : array();
1300 if ($mode == 'forum' || $mode == 'topic' || $mode == 'topic_approved' || $mode == 'topic_reported' || $mode == 'post_reported')
1305 $where_sql_and = 'WHERE';
1307 else if ($where_type == 'range')
1309 // Only check a range of topics/forums. For instance: 'topic_id BETWEEN 1 AND 60'
1310 $where_sql = 'WHERE (' . $mode[0] . ".$where_ids)";
1311 $where_sql_and = $where_sql . "\n\tAND";
1315 // Do not sync the "global forum"
1316 $where_ids = array_diff($where_ids, array(0));
1318 if (!sizeof($where_ids))
1320 // Empty array with IDs. This means that we don't have any work to do. Just return.
1324 // Limit the topics/forums we are syncing, use specific topic/forum IDs.
1325 // $where_type contains the field for the where clause (forum_id, topic_id)
1326 $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
1327 $where_sql_and = $where_sql . "\n\tAND";
1332 if (!sizeof($where_ids))
1337 // $where_type contains the field for the where clause (forum_id, topic_id)
1338 $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
1339 $where_sql_and = $where_sql . "\n\tAND";
1345 $db->sql_transaction('begin');
1346 switch ($db->sql_layer)
1350 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1351 USING ' . TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
1352 WHERE t1.topic_moved_id = t2.topic_id
1353 AND t1.forum_id = t2.forum_id";
1354 $db->sql_query($sql);
1358 $sql = 'SELECT t1.topic_id
1359 FROM ' .TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
1360 WHERE t1.topic_moved_id = t2.topic_id
1361 AND t1.forum_id = t2.forum_id";
1362 $result = $db->sql_query($sql);
1364 $topic_id_ary = array();
1365 while ($row = $db->sql_fetchrow($result))
1367 $topic_id_ary[] = $row['topic_id'];
1369 $db->sql_freeresult($result);
1371 if (!sizeof($topic_id_ary))
1376 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1377 WHERE ' . $db->sql_in_set('topic_id', $topic_id_ary);
1378 $db->sql_query($sql);
1383 $db->sql_transaction('commit');
1386 case 'topic_approved':
1388 $db->sql_transaction('begin');
1389 switch ($db->sql_layer)
1393 $sql = 'UPDATE ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
1394 SET t.topic_approved = p.post_approved
1395 $where_sql_and t.topic_first_post_id = p.post_id";
1396 $db->sql_query($sql);
1400 $sql = 'SELECT t.topic_id, p.post_approved
1401 FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
1402 $where_sql_and p.post_id = t.topic_first_post_id
1403 AND p.post_approved <> t.topic_approved";
1404 $result = $db->sql_query($sql);
1406 $topic_ids = array();
1407 while ($row = $db->sql_fetchrow($result))
1409 $topic_ids[] = $row['topic_id'];
1411 $db->sql_freeresult($result);
1413 if (!sizeof($topic_ids))
1418 $sql = 'UPDATE ' . TOPICS_TABLE . '
1419 SET topic_approved = 1 - topic_approved
1420 WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1421 $db->sql_query($sql);
1425 $db->sql_transaction('commit');
1428 case 'post_reported':
1429 $post_ids = $post_reported = array();
1431 $db->sql_transaction('begin');
1433 $sql = 'SELECT p.post_id, p.post_reported
1434 FROM ' . POSTS_TABLE . " p
1436 GROUP BY p.post_id, p.post_reported";
1437 $result = $db->sql_query($sql);
1439 while ($row = $db->sql_fetchrow($result))
1441 $post_ids[$row['post_id']] = $row['post_id'];
1442 if ($row['post_reported'])
1444 $post_reported[$row['post_id']] = 1;
1447 $db->sql_freeresult($result);
1449 $sql = 'SELECT DISTINCT(post_id)
1450 FROM ' . REPORTS_TABLE . '
1451 WHERE ' . $db->sql_in_set('post_id', $post_ids) . '
1452 AND report_closed = 0';
1453 $result = $db->sql_query($sql);
1455 $post_ids = array();
1456 while ($row = $db->sql_fetchrow($result))
1458 if (!isset($post_reported[$row['post_id']]))
1460 $post_ids[] = $row['post_id'];
1464 unset($post_reported[$row['post_id']]);
1467 $db->sql_freeresult($result);
1469 // $post_reported should be empty by now, if it's not it contains
1470 // posts that are falsely flagged as reported
1471 foreach ($post_reported as $post_id => $void)
1473 $post_ids[] = $post_id;
1476 if (sizeof($post_ids))
1478 $sql = 'UPDATE ' . POSTS_TABLE . '
1479 SET post_reported = 1 - post_reported
1480 WHERE ' . $db->sql_in_set('post_id', $post_ids);
1481 $db->sql_query($sql);
1484 $db->sql_transaction('commit');
1487 case 'topic_reported':
1490 sync('post_reported', $where_type, $where_ids);
1493 $topic_ids = $topic_reported = array();
1495 $db->sql_transaction('begin');
1497 $sql = 'SELECT DISTINCT(t.topic_id)
1498 FROM ' . POSTS_TABLE . " t
1499 $where_sql_and t.post_reported = 1";
1500 $result = $db->sql_query($sql);
1502 while ($row = $db->sql_fetchrow($result))
1504 $topic_reported[$row['topic_id']] = 1;
1506 $db->sql_freeresult($result);
1508 $sql = 'SELECT t.topic_id, t.topic_reported
1509 FROM ' . TOPICS_TABLE . " t
1511 $result = $db->sql_query($sql);
1513 while ($row = $db->sql_fetchrow($result))
1515 if ($row['topic_reported'] ^ isset($topic_reported[$row['topic_id']]))
1517 $topic_ids[] = $row['topic_id'];
1520 $db->sql_freeresult($result);
1522 if (sizeof($topic_ids))
1524 $sql = 'UPDATE ' . TOPICS_TABLE . '
1525 SET topic_reported = 1 - topic_reported
1526 WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1527 $db->sql_query($sql);
1530 $db->sql_transaction('commit');
1533 case 'post_attachment':
1534 $post_ids = $post_attachment = array();
1536 $db->sql_transaction('begin');
1538 $sql = 'SELECT p.post_id, p.post_attachment
1539 FROM ' . POSTS_TABLE . " p
1541 GROUP BY p.post_id, p.post_attachment";
1542 $result = $db->sql_query($sql);
1544 while ($row = $db->sql_fetchrow($result))
1546 $post_ids[$row['post_id']] = $row['post_id'];
1547 if ($row['post_attachment'])
1549 $post_attachment[$row['post_id']] = 1;
1552 $db->sql_freeresult($result);
1554 $sql = 'SELECT DISTINCT(post_msg_id)
1555 FROM ' . ATTACHMENTS_TABLE . '
1556 WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
1557 AND in_message = 0';
1558 $result = $db->sql_query($sql);
1560 $post_ids = array();
1561 while ($row = $db->sql_fetchrow($result))
1563 if (!isset($post_attachment[$row['post_msg_id']]))
1565 $post_ids[] = $row['post_msg_id'];
1569 unset($post_attachment[$row['post_msg_id']]);
1572 $db->sql_freeresult($result);
1574 // $post_attachment should be empty by now, if it's not it contains
1575 // posts that are falsely flagged as having attachments
1576 foreach ($post_attachment as $post_id => $void)
1578 $post_ids[] = $post_id;
1581 if (sizeof($post_ids))
1583 $sql = 'UPDATE ' . POSTS_TABLE . '
1584 SET post_attachment = 1 - post_attachment
1585 WHERE ' . $db->sql_in_set('post_id', $post_ids);
1586 $db->sql_query($sql);
1589 $db->sql_transaction('commit');
1592 case 'topic_attachment':
1595 sync('post_attachment', $where_type, $where_ids);
1598 $topic_ids = $topic_attachment = array();
1600 $db->sql_transaction('begin');
1602 $sql = 'SELECT DISTINCT(t.topic_id)
1603 FROM ' . POSTS_TABLE . " t
1604 $where_sql_and t.post_attachment = 1";
1605 $result = $db->sql_query($sql);
1607 while ($row = $db->sql_fetchrow($result))
1609 $topic_attachment[$row['topic_id']] = 1;
1611 $db->sql_freeresult($result);
1613 $sql = 'SELECT t.topic_id, t.topic_attachment
1614 FROM ' . TOPICS_TABLE . " t
1616 $result = $db->sql_query($sql);
1618 while ($row = $db->sql_fetchrow($result))
1620 if ($row['topic_attachment'] ^ isset($topic_attachment[$row['topic_id']]))
1622 $topic_ids[] = $row['topic_id'];
1625 $db->sql_freeresult($result);
1627 if (sizeof($topic_ids))
1629 $sql = 'UPDATE ' . TOPICS_TABLE . '
1630 SET topic_attachment = 1 - topic_attachment
1631 WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1632 $db->sql_query($sql);
1635 $db->sql_transaction('commit');
1641 $db->sql_transaction('begin');
1643 // 1: Get the list of all forums
1645 FROM ' . FORUMS_TABLE . " f
1647 $result = $db->sql_query($sql);
1649 $forum_data = $forum_ids = $post_ids = $last_post_id = $post_info = array();
1650 while ($row = $db->sql_fetchrow($result))
1652 if ($row['forum_type'] == FORUM_LINK)
1657 $forum_id = (int) $row['forum_id'];
1658 $forum_ids[$forum_id] = $forum_id;
1660 $forum_data[$forum_id] = $row;
1663 $forum_data[$forum_id]['posts'] = 0;
1664 $forum_data[$forum_id]['topics'] = 0;
1665 $forum_data[$forum_id]['topics_real'] = 0;
1667 $forum_data[$forum_id]['last_post_id'] = 0;
1668 $forum_data[$forum_id]['last_post_subject'] = '';
1669 $forum_data[$forum_id]['last_post_time'] = 0;
1670 $forum_data[$forum_id]['last_poster_id'] = 0;
1671 $forum_data[$forum_id]['last_poster_name'] = '';
1672 $forum_data[$forum_id]['last_poster_colour'] = '';
1674 $db->sql_freeresult($result);
1676 if (!sizeof($forum_ids))
1681 $forum_ids = array_values($forum_ids);
1683 // 2: Get topic counts for each forum (optional)
1686 $sql = 'SELECT forum_id, topic_approved, COUNT(topic_id) AS forum_topics
1687 FROM ' . TOPICS_TABLE . '
1688 WHERE ' . $db->sql_in_set('forum_id', $forum_ids) . '
1689 GROUP BY forum_id, topic_approved';
1690 $result = $db->sql_query($sql);
1692 while ($row = $db->sql_fetchrow($result))
1694 $forum_id = (int) $row['forum_id'];
1695 $forum_data[$forum_id]['topics_real'] += $row['forum_topics'];
1697 if ($row['topic_approved'])
1699 $forum_data[$forum_id]['topics'] = $row['forum_topics'];
1702 $db->sql_freeresult($result);
1705 // 3: Get post count for each forum (optional)
1708 if (sizeof($forum_ids) == 1)
1710 $sql = 'SELECT SUM(t.topic_replies + 1) AS forum_posts
1711 FROM ' . TOPICS_TABLE . ' t
1712 WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
1713 AND t.topic_approved = 1
1714 AND t.topic_status <> ' . ITEM_MOVED;
1718 $sql = 'SELECT t.forum_id, SUM(t.topic_replies + 1) AS forum_posts
1719 FROM ' . TOPICS_TABLE . ' t
1720 WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
1721 AND t.topic_approved = 1
1722 AND t.topic_status <> ' . ITEM_MOVED . '
1723 GROUP BY t.forum_id';
1726 $result = $db->sql_query($sql);
1728 while ($row = $db->sql_fetchrow($result))
1730 $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
1732 $forum_data[$forum_id]['posts'] = (int) $row['forum_posts'];
1734 $db->sql_freeresult($result);
1737 // 4: Get last_post_id for each forum
1738 if (sizeof($forum_ids) == 1)
1740 $sql = 'SELECT MAX(t.topic_last_post_id) as last_post_id
1741 FROM ' . TOPICS_TABLE . ' t
1742 WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
1743 AND t.topic_approved = 1';
1747 $sql = 'SELECT t.forum_id, MAX(t.topic_last_post_id) as last_post_id
1748 FROM ' . TOPICS_TABLE . ' t
1749 WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
1750 AND t.topic_approved = 1
1751 GROUP BY t.forum_id';
1754 $result = $db->sql_query($sql);
1756 while ($row = $db->sql_fetchrow($result))
1758 $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
1760 $forum_data[$forum_id]['last_post_id'] = (int) $row['last_post_id'];
1762 $post_ids[] = $row['last_post_id'];
1764 $db->sql_freeresult($result);
1766 // 5: Retrieve last_post infos
1767 if (sizeof($post_ids))
1769 $sql = 'SELECT p.post_id, p.poster_id, p.post_subject, p.post_time, p.post_username, u.username, u.user_colour
1770 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1771 WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
1772 AND p.poster_id = u.user_id';
1773 $result = $db->sql_query($sql);
1775 while ($row = $db->sql_fetchrow($result))
1777 $post_info[$row['post_id']] = $row;
1779 $db->sql_freeresult($result);
1781 foreach ($forum_data as $forum_id => $data)
1783 if ($data['last_post_id'])
1785 if (isset($post_info[$data['last_post_id']]))
1787 $forum_data[$forum_id]['last_post_subject'] = $post_info[$data['last_post_id']]['post_subject'];
1788 $forum_data[$forum_id]['last_post_time'] = $post_info[$data['last_post_id']]['post_time'];
1789 $forum_data[$forum_id]['last_poster_id'] = $post_info[$data['last_post_id']]['poster_id'];
1790 $forum_data[$forum_id]['last_poster_name'] = ($post_info[$data['last_post_id']]['poster_id'] != ANONYMOUS) ? $post_info[$data['last_post_id']]['username'] : $post_info[$data['last_post_id']]['post_username'];
1791 $forum_data[$forum_id]['last_poster_colour'] = $post_info[$data['last_post_id']]['user_colour'];
1795 // For some reason we did not find the post in the db
1796 $forum_data[$forum_id]['last_post_id'] = 0;
1797 $forum_data[$forum_id]['last_post_subject'] = '';
1798 $forum_data[$forum_id]['last_post_time'] = 0;
1799 $forum_data[$forum_id]['last_poster_id'] = 0;
1800 $forum_data[$forum_id]['last_poster_name'] = '';
1801 $forum_data[$forum_id]['last_poster_colour'] = '';
1808 // 6: Now do that thing
1809 $fieldnames = array('last_post_id', 'last_post_subject', 'last_post_time', 'last_poster_id', 'last_poster_name', 'last_poster_colour');
1813 array_push($fieldnames, 'posts', 'topics', 'topics_real');
1816 foreach ($forum_data as $forum_id => $row)
1820 foreach ($fieldnames as $fieldname)
1822 if ($row['forum_' . $fieldname] != $row[$fieldname])
1824 if (preg_match('#(name|colour|subject)$#', $fieldname))
1826 $sql_ary['forum_' . $fieldname] = (string) $row[$fieldname];
1830 $sql_ary['forum_' . $fieldname] = (int) $row[$fieldname];
1835 if (sizeof($sql_ary))
1837 $sql = 'UPDATE ' . FORUMS_TABLE . '
1838 SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
1839 WHERE forum_id = ' . $forum_id;
1840 $db->sql_query($sql);
1844 $db->sql_transaction('commit');
1848 $topic_data = $post_ids = $approved_unapproved_ids = $resync_forums = $delete_topics = $delete_posts = $moved_topics = array();
1850 $db->sql_transaction('begin');
1852 $sql = 'SELECT t.topic_id, t.forum_id, t.topic_moved_id, t.topic_approved, ' . (($sync_extra) ? 't.topic_attachment, t.topic_reported, ' : '') . 't.topic_poster, t.topic_time, t.topic_replies, t.topic_replies_real, t.topic_first_post_id, t.topic_first_poster_name, t.topic_first_poster_colour, t.topic_last_post_id, t.topic_last_post_subject, t.topic_last_poster_id, t.topic_last_poster_name, t.topic_last_poster_colour, t.topic_last_post_time
1853 FROM ' . TOPICS_TABLE . " t
1855 $result = $db->sql_query($sql);
1857 while ($row = $db->sql_fetchrow($result))
1859 if ($row['topic_moved_id'])
1861 $moved_topics[] = $row['topic_id'];
1865 $topic_id = (int) $row['topic_id'];
1866 $topic_data[$topic_id] = $row;
1867 $topic_data[$topic_id]['replies_real'] = -1;
1868 $topic_data[$topic_id]['replies'] = 0;
1869 $topic_data[$topic_id]['first_post_id'] = 0;
1870 $topic_data[$topic_id]['last_post_id'] = 0;
1871 unset($topic_data[$topic_id]['topic_id']);
1873 // This array holds all topic_ids
1874 $delete_topics[$topic_id] = '';
1878 $topic_data[$topic_id]['reported'] = 0;
1879 $topic_data[$topic_id]['attachment'] = 0;
1882 $db->sql_freeresult($result);
1884 // Use "t" as table alias because of the $where_sql clause
1885 // NOTE: 't.post_approved' in the GROUP BY is causing a major slowdown.
1886 $sql = 'SELECT t.topic_id, t.post_approved, COUNT(t.post_id) AS total_posts, MIN(t.post_id) AS first_post_id, MAX(t.post_id) AS last_post_id
1887 FROM ' . POSTS_TABLE . " t
1889 GROUP BY t.topic_id, t.post_approved";
1890 $result = $db->sql_query($sql);
1892 while ($row = $db->sql_fetchrow($result))
1894 $topic_id = (int) $row['topic_id'];
1896 $row['first_post_id'] = (int) $row['first_post_id'];
1897 $row['last_post_id'] = (int) $row['last_post_id'];
1899 if (!isset($topic_data[$topic_id]))
1901 // Hey, these posts come from a topic that does not exist
1902 $delete_posts[$topic_id] = '';
1906 // Unset the corresponding entry in $delete_topics
1907 // When we'll be done, only topics with no posts will remain
1908 unset($delete_topics[$topic_id]);
1910 $topic_data[$topic_id]['replies_real'] += $row['total_posts'];
1911 $topic_data[$topic_id]['first_post_id'] = (!$topic_data[$topic_id]['first_post_id']) ? $row['first_post_id'] : min($topic_data[$topic_id]['first_post_id'], $row['first_post_id']);
1913 if ($row['post_approved'] || !$topic_data[$topic_id]['last_post_id'])
1915 $topic_data[$topic_id]['replies'] = $row['total_posts'] - 1;
1916 $topic_data[$topic_id]['last_post_id'] = $row['last_post_id'];
1920 $db->sql_freeresult($result);
1922 foreach ($topic_data as $topic_id => $row)
1924 $post_ids[] = $row['first_post_id'];
1925 if ($row['first_post_id'] != $row['last_post_id'])
1927 $post_ids[] = $row['last_post_id'];
1931 // Now we delete empty topics and orphan posts
1932 if (sizeof($delete_posts))
1934 delete_posts('topic_id', array_keys($delete_posts), false);
1935 unset($delete_posts);
1938 if (!sizeof($topic_data))
1940 // If we get there, topic ids were invalid or topics did not contain any posts
1941 delete_topics($where_type, $where_ids, true);
1945 if (sizeof($delete_topics))
1947 $delete_topic_ids = array();
1948 foreach ($delete_topics as $topic_id => $void)
1950 unset($topic_data[$topic_id]);
1951 $delete_topic_ids[] = $topic_id;
1954 delete_topics('topic_id', $delete_topic_ids, false);
1955 unset($delete_topics, $delete_topic_ids);
1958 $sql = 'SELECT p.post_id, p.topic_id, p.post_approved, p.poster_id, p.post_subject, p.post_username, p.post_time, u.username, u.user_colour
1959 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1960 WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
1961 AND u.user_id = p.poster_id';
1962 $result = $db->sql_query($sql);
1964 $post_ids = array();
1965 while ($row = $db->sql_fetchrow($result))
1967 $topic_id = intval($row['topic_id']);
1969 if ($row['post_id'] == $topic_data[$topic_id]['first_post_id'])
1971 if ($topic_data[$topic_id]['topic_approved'] != $row['post_approved'])
1973 $approved_unapproved_ids[] = $topic_id;
1975 $topic_data[$topic_id]['time'] = $row['post_time'];
1976 $topic_data[$topic_id]['poster'] = $row['poster_id'];
1977 $topic_data[$topic_id]['first_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
1978 $topic_data[$topic_id]['first_poster_colour'] = $row['user_colour'];
1981 if ($row['post_id'] == $topic_data[$topic_id]['last_post_id'])
1983 $topic_data[$topic_id]['last_poster_id'] = $row['poster_id'];
1984 $topic_data[$topic_id]['last_post_subject'] = $row['post_subject'];
1985 $topic_data[$topic_id]['last_post_time'] = $row['post_time'];
1986 $topic_data[$topic_id]['last_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
1987 $topic_data[$topic_id]['last_poster_colour'] = $row['user_colour'];
1990 $db->sql_freeresult($result);
1992 // Make sure shadow topics do link to existing topics
1993 if (sizeof($moved_topics))
1995 $delete_topics = array();
1997 $sql = 'SELECT t1.topic_id, t1.topic_moved_id
1998 FROM ' . TOPICS_TABLE . ' t1
1999 LEFT JOIN ' . TOPICS_TABLE . ' t2 ON (t2.topic_id = t1.topic_moved_id)
2000 WHERE ' . $db->sql_in_set('t1.topic_id', $moved_topics) . '
2001 AND t2.topic_id IS NULL';
2002 $result = $db->sql_query($sql);
2004 while ($row = $db->sql_fetchrow($result))
2006 $delete_topics[] = $row['topic_id'];
2008 $db->sql_freeresult($result);
2010 if (sizeof($delete_topics))
2012 delete_topics('topic_id', $delete_topics, false);
2014 unset($delete_topics);
2016 // Make sure shadow topics having no last post data being updated (this only rarely happens...)
2017 $sql = 'SELECT topic_id, topic_moved_id, topic_last_post_id, topic_first_post_id
2018 FROM ' . TOPICS_TABLE . '
2019 WHERE ' . $db->sql_in_set('topic_id', $moved_topics) . '
2020 AND topic_last_post_time = 0';
2021 $result = $db->sql_query($sql);
2023 $shadow_topic_data = $post_ids = array();
2024 while ($row = $db->sql_fetchrow($result))
2026 $shadow_topic_data[$row['topic_moved_id']] = $row;
2027 $post_ids[] = $row['topic_last_post_id'];
2028 $post_ids[] = $row['topic_first_post_id'];
2030 $db->sql_freeresult($result);
2032 $sync_shadow_topics = array();
2033 if (sizeof($post_ids))
2035 $sql = 'SELECT p.post_id, p.topic_id, p.post_approved, p.poster_id, p.post_subject, p.post_username, p.post_time, u.username, u.user_colour
2036 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
2037 WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
2038 AND u.user_id = p.poster_id';
2039 $result = $db->sql_query($sql);
2041 $post_ids = array();
2042 while ($row = $db->sql_fetchrow($result))
2044 $topic_id = (int) $row['topic_id'];
2046 // Ok, there should be a shadow topic. If there isn't, then there's something wrong with the db.
2047 // However, there's not much we can do about it.
2048 if (!empty($shadow_topic_data[$topic_id]))
2050 if ($row['post_id'] == $shadow_topic_data[$topic_id]['topic_first_post_id'])
2052 $orig_topic_id = $shadow_topic_data[$topic_id]['topic_id'];
2054 if (!isset($sync_shadow_topics[$orig_topic_id]))
2056 $sync_shadow_topics[$orig_topic_id] = array();
2059 $sync_shadow_topics[$orig_topic_id]['topic_time'] = $row['post_time'];
2060 $sync_shadow_topics[$orig_topic_id]['topic_poster'] = $row['poster_id'];
2061 $sync_shadow_topics[$orig_topic_id]['topic_first_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
2062 $sync_shadow_topics[$orig_topic_id]['topic_first_poster_colour'] = $row['user_colour'];
2065 if ($row['post_id'] == $shadow_topic_data[$topic_id]['topic_last_post_id'])
2067 $orig_topic_id = $shadow_topic_data[$topic_id]['topic_id'];
2069 if (!isset($sync_shadow_topics[$orig_topic_id]))
2071 $sync_shadow_topics[$orig_topic_id] = array();
2074 $sync_shadow_topics[$orig_topic_id]['topic_last_poster_id'] = $row['poster_id'];
2075 $sync_shadow_topics[$orig_topic_id]['topic_last_post_subject'] = $row['post_subject'];
2076 $sync_shadow_topics[$orig_topic_id]['topic_last_post_time'] = $row['post_time'];
2077 $sync_shadow_topics[$orig_topic_id]['topic_last_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
2078 $sync_shadow_topics[$orig_topic_id]['topic_last_poster_colour'] = $row['user_colour'];
2082 $db->sql_freeresult($result);
2084 $shadow_topic_data = array();
2086 // Update the information we collected
2087 if (sizeof($sync_shadow_topics))
2089 foreach ($sync_shadow_topics as $sync_topic_id => $sql_ary)
2091 $sql = 'UPDATE ' . TOPICS_TABLE . '
2092 SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
2093 WHERE topic_id = ' . $sync_topic_id;
2094 $db->sql_query($sql);
2099 unset($sync_shadow_topics, $shadow_topic_data);
2102 // approved becomes unapproved, and vice-versa
2103 if (sizeof($approved_unapproved_ids))
2105 $sql = 'UPDATE ' . TOPICS_TABLE . '
2106 SET topic_approved = 1 - topic_approved
2107 WHERE ' . $db->sql_in_set('topic_id', $approved_unapproved_ids);
2108 $db->sql_query($sql);
2110 unset($approved_unapproved_ids);
2112 // These are fields that will be synchronised
2113 $fieldnames = array('time', 'replies', 'replies_real', 'poster', 'first_post_id', 'first_poster_name', 'first_poster_colour', 'last_post_id', 'last_post_subject', 'last_post_time', 'last_poster_id', 'last_poster_name', 'last_poster_colour');
2117 // This routine assumes that post_reported values are correct
2118 // if they are not, use sync('post_reported') first
2119 $sql = 'SELECT t.topic_id, p.post_id
2120 FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
2121 $where_sql_and p.topic_id = t.topic_id
2122 AND p.post_reported = 1
2123 GROUP BY t.topic_id, p.post_id";
2124 $result = $db->sql_query($sql);
2126 $fieldnames[] = 'reported';
2127 while ($row = $db->sql_fetchrow($result))
2129 $topic_data[intval($row['topic_id'])]['reported'] = 1;
2131 $db->sql_freeresult($result);
2133 // This routine assumes that post_attachment values are correct
2134 // if they are not, use sync('post_attachment') first
2135 $sql = 'SELECT t.topic_id, p.post_id
2136 FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
2137 $where_sql_and p.topic_id = t.topic_id
2138 AND p.post_attachment = 1
2139 GROUP BY t.topic_id, p.post_id";
2140 $result = $db->sql_query($sql);
2142 $fieldnames[] = 'attachment';
2143 while ($row = $db->sql_fetchrow($result))
2145 $topic_data[intval($row['topic_id'])]['attachment'] = 1;
2147 $db->sql_freeresult($result);
2150 foreach ($topic_data as $topic_id => $row)
2154 foreach ($fieldnames as $fieldname)
2156 if (isset($row[$fieldname]) && isset($row['topic_' . $fieldname]) && $row['topic_' . $fieldname] != $row[$fieldname])
2158 $sql_ary['topic_' . $fieldname] = $row[$fieldname];
2162 if (sizeof($sql_ary))
2164 $sql = 'UPDATE ' . TOPICS_TABLE . '
2165 SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
2166 WHERE topic_id = ' . $topic_id;
2167 $db->sql_query($sql);
2169 $resync_forums[$row['forum_id']] = $row['forum_id'];
2174 $db->sql_transaction('commit');
2176 // if some topics have been resync'ed then resync parent forums
2177 // except when we're only syncing a range, we don't want to sync forums during
2178 // batch processing.
2179 if ($resync_parents && sizeof($resync_forums) && $where_type != 'range')
2181 sync('forum', 'forum_id', array_values($resync_forums), true, true);
2192 function prune($forum_id, $prune_mode, $prune_date, $prune_flags = 0, $auto_sync = true)
2196 if (!is_array($forum_id))
2198 $forum_id = array($forum_id);
2201 if (!sizeof($forum_id))
2208 if (!($prune_flags & FORUM_FLAG_PRUNE_ANNOUNCE))
2210 $sql_and .= ' AND topic_type <> ' . POST_ANNOUNCE;
2213 if (!($prune_flags & FORUM_FLAG_PRUNE_STICKY))
2215 $sql_and .= ' AND topic_type <> ' . POST_STICKY;
2218 if ($prune_mode == 'posted')
2220 $sql_and .= " AND topic_last_post_time < $prune_date";
2223 if ($prune_mode == 'viewed')
2225 $sql_and .= " AND topic_last_view_time < $prune_date";
2228 $sql = 'SELECT topic_id
2229 FROM ' . TOPICS_TABLE . '
2230 WHERE ' . $db->sql_in_set('forum_id', $forum_id) . "
2233 $result = $db->sql_query($sql);
2235 $topic_list = array();
2236 while ($row = $db->sql_fetchrow($result))
2238 $topic_list[] = $row['topic_id'];
2240 $db->sql_freeresult($result);
2242 if ($prune_flags & FORUM_FLAG_PRUNE_POLL)
2244 $sql = 'SELECT topic_id
2245 FROM ' . TOPICS_TABLE . '
2246 WHERE ' . $db->sql_in_set('forum_id', $forum_id) . "
2248 AND poll_last_vote < $prune_date
2250 $result = $db->sql_query($sql);
2252 while ($row = $db->sql_fetchrow($result))
2254 $topic_list[] = $row['topic_id'];
2256 $db->sql_freeresult($result);
2258 $topic_list = array_unique($topic_list);
2261 return delete_topics('topic_id', $topic_list, $auto_sync, false);
2265 * Function auto_prune(), this function now relies on passed vars
2267 function auto_prune($forum_id, $prune_mode, $prune_flags, $prune_days, $prune_freq)
2271 $sql = 'SELECT forum_name
2272 FROM ' . FORUMS_TABLE . "
2273 WHERE forum_id = $forum_id";
2274 $result = $db->sql_query($sql, 3600);
2275 $row = $db->sql_fetchrow($result);
2276 $db->sql_freeresult($result);
2280 $prune_date = time() - ($prune_days * 86400);
2281 $next_prune = time() + ($prune_freq * 86400);
2283 prune($forum_id, $prune_mode, $prune_date, $prune_flags, true);
2285 $sql = 'UPDATE ' . FORUMS_TABLE . "
2286 SET prune_next = $next_prune
2287 WHERE forum_id = $forum_id";
2288 $db->sql_query($sql);
2290 add_log('admin', 'LOG_AUTO_PRUNE', $row['forum_name']);
2297 * remove_comments will strip the sql comment lines out of an uploaded sql file
2298 * specifically for mssql and postgres type files in the install....
2300 function remove_comments(&$output)
2302 $lines = explode("\n", $output);
2305 // try to keep mem. use down
2306 $linecount = sizeof($lines);
2308 $in_comment = false;
2309 for ($i = 0; $i < $linecount; $i++)
2311 if (trim($lines[$i]) == '/*')
2318 $output .= $lines[$i] . "\n";
2321 if (trim($lines[$i]) == '*/')
2323 $in_comment = false;
2332 * Cache moderators, called whenever permissions are changed via admin_permissions. Changes of username
2333 * and group names must be carried through for the moderators table
2335 function cache_moderators()
2337 global $db, $cache, $auth, $phpbb_root_path, $phpEx;
2339 // Remove cached sql results
2340 $cache->destroy('sql', MODERATOR_CACHE_TABLE);
2343 switch ($db->sql_layer)
2347 $db->sql_query('DELETE FROM ' . MODERATOR_CACHE_TABLE);
2351 $db->sql_query('TRUNCATE TABLE ' . MODERATOR_CACHE_TABLE);
2355 // We add moderators who have forum moderator permissions without an explicit ACL_NEVER setting
2356 $hold_ary = $ug_id_ary = $sql_ary = array();
2358 // Grab all users having moderative options...
2359 $hold_ary = $auth->acl_user_raw_data(false, 'm_%', false);
2362 if (sizeof($hold_ary))
2364 // At least one moderative option warrants a display
2365 $ug_id_ary = array_keys($hold_ary);
2367 // Remove users who have group memberships with DENY moderator permissions
2368 $sql = $db->sql_build_query('SELECT', array(
2369 'SELECT' => 'a.forum_id, ug.user_id, g.group_id',
2372 ACL_OPTIONS_TABLE => 'o',
2373 USER_GROUP_TABLE => 'ug',
2374 GROUPS_TABLE => 'g',
2375 ACL_GROUPS_TABLE => 'a',
2378 'LEFT_JOIN' => array(
2380 'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
2381 'ON' => 'a.auth_role_id = r.role_id'
2385 'WHERE' => '(o.auth_option_id = a.auth_option_id OR o.auth_option_id = r.auth_option_id)
2386 AND ((a.auth_setting = ' . ACL_NEVER . ' AND r.auth_setting IS NULL)
2387 OR r.auth_setting = ' . ACL_NEVER . ')
2388 AND a.group_id = ug.group_id
2389 AND g.group_id = ug.group_id
2390 AND NOT (ug.group_leader = 1 AND g.group_skip_auth = 1)
2391 AND ' . $db->sql_in_set('ug.user_id', $ug_id_ary) . "
2392 AND ug.user_pending = 0
2393 AND o.auth_option " . $db->sql_like_expression('m_' . $db->any_char),
2395 $result = $db->sql_query($sql);
2397 while ($row = $db->sql_fetchrow($result))
2399 if (isset($hold_ary[$row['user_id']][$row['forum_id']]))
2401 unset($hold_ary[$row['user_id']][$row['forum_id']]);
2404 $db->sql_freeresult($result);
2406 if (sizeof($hold_ary))
2409 $sql = 'SELECT user_id, username
2410 FROM ' . USERS_TABLE . '
2411 WHERE ' . $db->sql_in_set('user_id', array_keys($hold_ary));
2412 $result = $db->sql_query($sql);
2414 $usernames_ary = array();
2415 while ($row = $db->sql_fetchrow($result))
2417 $usernames_ary[$row['user_id']] = $row['username'];
2420 foreach ($hold_ary as $user_id => $forum_id_ary)
2422 // Do not continue if user does not exist
2423 if (!isset($usernames_ary[$user_id]))
2428 foreach ($forum_id_ary as $forum_id => $auth_ary)
2431 'forum_id' => (int) $forum_id,
2432 'user_id' => (int) $user_id,
2433 'username' => (string) $usernames_ary[$user_id],
2442 // Now to the groups...
2443 $hold_ary = $auth->acl_group_raw_data(false, 'm_%', false);
2445 if (sizeof($hold_ary))
2447 $ug_id_ary = array_keys($hold_ary);
2449 // Make sure not hidden or special groups are involved...
2450 $sql = 'SELECT group_name, group_id, group_type
2451 FROM ' . GROUPS_TABLE . '
2452 WHERE ' . $db->sql_in_set('group_id', $ug_id_ary);
2453 $result = $db->sql_query($sql);
2455 $groupnames_ary = array();
2456 while ($row = $db->sql_fetchrow($result))
2458 if ($row['group_type'] == GROUP_HIDDEN || $row['group_type'] == GROUP_SPECIAL)
2460 unset($hold_ary[$row['group_id']]);
2463 $groupnames_ary[$row['group_id']] = $row['group_name'];
2465 $db->sql_freeresult($result);
2467 foreach ($hold_ary as $group_id => $forum_id_ary)
2469 // If there is no group, we do not assign it...
2470 if (!isset($groupnames_ary[$group_id]))
2475 foreach ($forum_id_ary as $forum_id => $auth_ary)
2478 foreach ($auth_ary as $auth_option => $setting)
2480 // Make sure at least one ACL_YES option is set...
2481 if ($setting == ACL_YES)
2494 'forum_id' => (int) $forum_id,
2497 'group_id' => (int) $group_id,
2498 'group_name' => (string) $groupnames_ary[$group_id]
2504 $db->sql_multi_insert(MODERATOR_CACHE_TABLE, $sql_ary);
2509 * If $log_count is set to false, we will skip counting all entries in the database.
2511 function view_log($mode, &$log, &$log_count, $limit = 0, $offset = 0, $forum_id = 0, $topic_id = 0, $user_id = 0, $limit_days = 0, $sort_by = 'l.log_time DESC', $keywords = '')
2513 global $db, $user, $auth, $phpEx, $phpbb_root_path, $phpbb_admin_path;
2515 $topic_id_list = $reportee_id_list = $is_auth = $is_mod = array();
2517 $profile_url = (defined('IN_ADMIN')) ? append_sid("{$phpbb_admin_path}index.$phpEx", 'i=users&mode=overview') : append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile');
2522 $log_type = LOG_ADMIN;
2527 $log_type = LOG_MOD;
2532 $sql_forum = 'AND l.topic_id = ' . (int) $topic_id;
2534 else if (is_array($forum_id))
2536 $sql_forum = 'AND ' . $db->sql_in_set('l.forum_id', array_map('intval', $forum_id));
2540 $sql_forum = 'AND l.forum_id = ' . (int) $forum_id;
2545 $log_type = LOG_USERS;
2546 $sql_forum = 'AND l.reportee_id = ' . (int) $user_id;
2550 $log_type = LOG_USERS;
2555 $log_type = LOG_CRITICAL;
2563 // Use no preg_quote for $keywords because this would lead to sole backslashes being added
2564 // We also use an OR connection here for spaces and the | string. Currently, regex is not supported for searching (but may come later).
2565 $keywords = preg_split('#[\s|]+#u', utf8_strtolower($keywords), 0, PREG_SPLIT_NO_EMPTY);
2568 if (!empty($keywords))
2570 $keywords_pattern = array();
2572 // Build pattern and keywords...
2573 for ($i = 0, $num_keywords = sizeof($keywords); $i < $num_keywords; $i++)
2575 $keywords_pattern[] = preg_quote($keywords[$i], '#');
2576 $keywords[$i] = $db->sql_like_expression($db->any_char . $keywords[$i] . $db->any_char);
2579 $keywords_pattern = '#' . implode('|', $keywords_pattern) . '#ui';
2581 $operations = array();
2582 foreach ($user->lang as $key => $value)
2584 if (substr($key, 0, 4) == 'LOG_' && preg_match($keywords_pattern, $value))
2586 $operations[] = $key;
2590 $sql_keywords = 'AND (';
2591 if (!empty($operations))
2593 $sql_keywords .= $db->sql_in_set('l.log_operation', $operations) . ' OR ';
2595 $sql_keywords .= 'LOWER(l.log_data) ' . implode(' OR LOWER(l.log_data) ', $keywords) . ')';
2598 if ($log_count !== false)
2600 $sql = 'SELECT COUNT(l.log_id) AS total_entries
2601 FROM ' . LOG_TABLE . ' l, ' . USERS_TABLE . " u
2602 WHERE l.log_type = $log_type
2603 AND l.user_id = u.user_id
2604 AND l.log_time >= $limit_days
2607 $result = $db->sql_query($sql);
2608 $log_count = (int) $db->sql_fetchfield('total_entries');
2609 $db->sql_freeresult($result);
2612 // $log_count may be false here if false was passed in for it,
2613 // because in this case we did not run the COUNT() query above.
2614 // If we ran the COUNT() query and it returned zero rows, return;
2615 // otherwise query for logs below.
2616 if ($log_count === 0)
2618 // Save the queries, because there are no logs to display
2622 if ($offset >= $log_count)
2624 $offset = ($offset - $limit < 0) ? 0 : $offset - $limit;
2627 $sql = "SELECT l.*, u.username, u.username_clean, u.user_colour
2628 FROM " . LOG_TABLE . " l, " . USERS_TABLE . " u
2629 WHERE l.log_type = $log_type
2630 AND u.user_id = l.user_id
2631 " . (($limit_days) ? "AND l.log_time >= $limit_days" : '') . "
2635 $result = $db->sql_query_limit($sql, $limit, $offset);
2639 while ($row = $db->sql_fetchrow($result))
2641 if ($row['topic_id'])
2643 $topic_id_list[] = $row['topic_id'];
2646 if ($row['reportee_id'])
2648 $reportee_id_list[] = $row['reportee_id'];
2652 'id' => $row['log_id'],
2654 'reportee_id' => $row['reportee_id'],
2655 'reportee_username' => '',
2656 'reportee_username_full'=> '',
2658 'user_id' => $row['user_id'],
2659 'username' => $row['username'],
2660 'username_full' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], false, $profile_url),
2662 'ip' => $row['log_ip'],
2663 'time' => $row['log_time'],
2664 'forum_id' => $row['forum_id'],
2665 'topic_id' => $row['topic_id'],
2667 'viewforum' => ($row['forum_id'] && $auth->acl_get('f_read', $row['forum_id'])) ? append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']) : false,
2668 'action' => (isset($user->lang[$row['log_operation']])) ? $user->lang[$row['log_operation']] : '{' . ucfirst(str_replace('_', ' ', $row['log_operation'])) . '}',
2671 if (!empty($row['log_data']))
2673 $log_data_ary = @unserialize($row['log_data']);
2674 $log_data_ary = ($log_data_ary === false) ? array() : $log_data_ary;
2676 if (isset($user->lang[$row['log_operation']]))
2678 // Check if there are more occurrences of % than arguments, if there are we fill out the arguments array
2679 // It doesn't matter if we add more arguments than placeholders
2680 if ((substr_count($log[$i]['action'], '%') - sizeof($log_data_ary)) > 0)
2682 $log_data_ary = array_merge($log_data_ary, array_fill(0, substr_count($log[$i]['action'], '%') - sizeof($log_data_ary), ''));
2685 $log[$i]['action'] = vsprintf($log[$i]['action'], $log_data_ary);
2687 // If within the admin panel we do not censor text out
2688 if (defined('IN_ADMIN'))
2690 $log[$i]['action'] = bbcode_nl2br($log[$i]['action']);
2694 $log[$i]['action'] = bbcode_nl2br(censor_text($log[$i]['action']));
2697 else if (!empty($log_data_ary))
2699 $log[$i]['action'] .= '<br />' . implode('', $log_data_ary);
2702 /* Apply make_clickable... has to be seen if it is for good. :/
2703 // Seems to be not for the moment, reconsider later...
2704 $log[$i]['action'] = make_clickable($log[$i]['action']);
2710 $db->sql_freeresult($result);
2712 if (sizeof($topic_id_list))
2714 $topic_id_list = array_unique($topic_id_list);
2716 // This query is not really needed if move_topics() updates the forum_id field,
2717 // although it's also used to determine if the topic still exists in the database
2718 $sql = 'SELECT topic_id, forum_id
2719 FROM ' . TOPICS_TABLE . '
2720 WHERE ' . $db->sql_in_set('topic_id', array_map('intval', $topic_id_list));
2721 $result = $db->sql_query($sql);
2723 $default_forum_id = 0;
2725 while ($row = $db->sql_fetchrow($result))
2727 if (!$row['forum_id'])
2729 if ($auth->acl_getf_global('f_read'))
2731 if (!$default_forum_id)
2733 $sql = 'SELECT forum_id
2734 FROM ' . FORUMS_TABLE . '
2735 WHERE forum_type = ' . FORUM_POST;
2736 $f_result = $db->sql_query_limit($sql, 1);
2737 $default_forum_id = (int) $db->sql_fetchfield('forum_id', false, $f_result);
2738 $db->sql_freeresult($f_result);
2741 $is_auth[$row['topic_id']] = $default_forum_id;
2746 if ($auth->acl_get('f_read', $row['forum_id']))
2748 $is_auth[$row['topic_id']] = $row['forum_id'];
2752 if ($auth->acl_gets('a_', 'm_', $row['forum_id']))
2754 $is_mod[$row['topic_id']] = $row['forum_id'];
2757 $db->sql_freeresult($result);
2759 foreach ($log as $key => $row)
2761 $log[$key]['viewtopic'] = (isset($is_auth[$row['topic_id']])) ? append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $is_auth[$row['topic_id']] . '&t=' . $row['topic_id']) : false;
2762 $log[$key]['viewlogs'] = (isset($is_mod[$row['topic_id']])) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=logs&mode=topic_logs&t=' . $row['topic_id'], true, $user->session_id) : false;
2766 if (sizeof($reportee_id_list))
2768 $reportee_id_list = array_unique($reportee_id_list);
2769 $reportee_names_list = array();
2771 $sql = 'SELECT user_id, username, user_colour
2772 FROM ' . USERS_TABLE . '
2773 WHERE ' . $db->sql_in_set('user_id', $reportee_id_list);
2774 $result = $db->sql_query($sql);
2776 while ($row = $db->sql_fetchrow($result))
2778 $reportee_names_list[$row['user_id']] = $row;
2780 $db->sql_freeresult($result);
2782 foreach ($log as $key => $row)
2784 if (!isset($reportee_names_list[$row['reportee_id']]))
2789 $log[$key]['reportee_username'] = $reportee_names_list[$row['reportee_id']]['username'];
2790 $log[$key]['reportee_username_full'] = get_username_string('full', $row['reportee_id'], $reportee_names_list[$row['reportee_id']]['username'], $reportee_names_list[$row['reportee_id']]['user_colour'], false, $profile_url);
2798 * Update foes - remove moderators and administrators from foe lists...
2800 function update_foes($group_id = false, $user_id = false)
2804 // update foes for some user
2805 if (is_array($user_id) && sizeof($user_id))
2807 $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
2808 WHERE ' . $db->sql_in_set('zebra_id', $user_id) . '
2810 $db->sql_query($sql);
2814 // update foes for some group
2815 if (is_array($group_id) && sizeof($group_id))
2817 // Grab group settings...
2818 $sql = $db->sql_build_query('SELECT', array(
2819 'SELECT' => 'a.group_id',
2822 ACL_OPTIONS_TABLE => 'ao',
2823 ACL_GROUPS_TABLE => 'a'
2826 'LEFT_JOIN' => array(
2828 'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
2829 'ON' => 'a.auth_role_id = r.role_id'
2833 'WHERE' => '(ao.auth_option_id = a.auth_option_id OR ao.auth_option_id = r.auth_option_id)
2834 AND ' . $db->sql_in_set('a.group_id', $group_id) . "
2835 AND ao.auth_option IN ('a_', 'm_')",
2837 'GROUP_BY' => 'a.group_id'
2839 $result = $db->sql_query($sql);
2842 while ($row = $db->sql_fetchrow($result))
2844 $groups[] = (int) $row['group_id'];
2846 $db->sql_freeresult($result);
2848 if (!sizeof($groups))
2853 switch ($db->sql_layer)
2857 $sql = 'DELETE ' . (($db->sql_layer === 'mysqli' || version_compare($db->sql_server_info(true), '4.1', '>=')) ? 'z.*' : ZEBRA_TABLE) . '
2858 FROM ' . ZEBRA_TABLE . ' z, ' . USER_GROUP_TABLE . ' ug
2859 WHERE z.zebra_id = ug.user_id
2861 AND ' . $db->sql_in_set('ug.group_id', $groups);
2862 $db->sql_query($sql);
2866 $sql = 'SELECT user_id
2867 FROM ' . USER_GROUP_TABLE . '
2868 WHERE ' . $db->sql_in_set('group_id', $groups);
2869 $result = $db->sql_query($sql);
2872 while ($row = $db->sql_fetchrow($result))
2874 $users[] = (int) $row['user_id'];
2876 $db->sql_freeresult($result);
2880 $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
2881 WHERE ' . $db->sql_in_set('zebra_id', $users) . '
2883 $db->sql_query($sql);
2891 // update foes for everyone
2893 foreach ($auth->acl_get_list(false, array('a_', 'm_'), false) as $forum_id => $forum_ary)
2895 foreach ($forum_ary as $auth_option => $user_ary)
2897 $perms = array_merge($perms, $user_ary);
2903 $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
2904 WHERE ' . $db->sql_in_set('zebra_id', array_unique($perms)) . '
2906 $db->sql_query($sql);
2912 * Lists inactive users
2914 function view_inactive_users(&$users, &$user_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'user_inactive_time DESC')
2918 $sql = 'SELECT COUNT(user_id) AS user_count
2919 FROM ' . USERS_TABLE . '
2920 WHERE user_type = ' . USER_INACTIVE .
2921 (($limit_days) ? " AND user_inactive_time >= $limit_days" : '');
2922 $result = $db->sql_query($sql);
2923 $user_count = (int) $db->sql_fetchfield('user_count');
2924 $db->sql_freeresult($result);
2926 if ($user_count == 0)
2928 // Save the queries, because there are no users to display
2932 if ($offset >= $user_count)
2934 $offset = ($offset - $limit < 0) ? 0 : $offset - $limit;
2938 FROM ' . USERS_TABLE . '
2939 WHERE user_type = ' . USER_INACTIVE .
2940 (($limit_days) ? " AND user_inactive_time >= $limit_days" : '') . "
2942 $result = $db->sql_query_limit($sql, $limit, $offset);
2944 while ($row = $db->sql_fetchrow($result))
2946 $row['inactive_reason'] = $user->lang['INACTIVE_REASON_UNKNOWN'];
2947 switch ($row['user_inactive_reason'])
2949 case INACTIVE_REGISTER:
2950 $row['inactive_reason'] = $user->lang['INACTIVE_REASON_REGISTER'];
2953 case INACTIVE_PROFILE:
2954 $row['inactive_reason'] = $user->lang['INACTIVE_REASON_PROFILE'];
2957 case INACTIVE_MANUAL:
2958 $row['inactive_reason'] = $user->lang['INACTIVE_REASON_MANUAL'];
2961 case INACTIVE_REMIND:
2962 $row['inactive_reason'] = $user->lang['INACTIVE_REASON_REMIND'];
2973 * Lists warned users
2975 function view_warned_users(&$users, &$user_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'user_warnings DESC')
2979 $sql = 'SELECT user_id, username, user_colour, user_warnings, user_last_warning
2980 FROM ' . USERS_TABLE . '
2981 WHERE user_warnings > 0
2982 ' . (($limit_days) ? "AND user_last_warning >= $limit_days" : '') . "
2984 $result = $db->sql_query_limit($sql, $limit, $offset);
2985 $users = $db->sql_fetchrowset($result);
2986 $db->sql_freeresult($result);
2988 $sql = 'SELECT count(user_id) AS user_count
2989 FROM ' . USERS_TABLE . '
2990 WHERE user_warnings > 0
2991 ' . (($limit_days) ? "AND user_last_warning >= $limit_days" : '');
2992 $result = $db->sql_query($sql);
2993 $user_count = (int) $db->sql_fetchfield('user_count');
2994 $db->sql_freeresult($result);
3001 * Currently only mysql and mssql are supported
3003 function get_database_size()
3005 global $db, $user, $table_prefix;
3007 $database_size = false;
3009 // This code is heavily influenced by a similar routine in phpMyAdmin 2.2.0
3010 switch ($db->sql_layer)
3015 $sql = 'SELECT VERSION() AS mysql_version';
3016 $result = $db->sql_query($sql);
3017 $row = $db->sql_fetchrow($result);
3018 $db->sql_freeresult($result);
3022 $version = $row['mysql_version'];
3024 if (preg_match('#(3\.23|[45]\.)#', $version))
3026 $db_name = (preg_match('#^(?:3\.23\.(?:[6-9]|[1-9]{2}))|[45]\.#', $version)) ? "`{$db->dbname}`" : $db->dbname;
3028 $sql = 'SHOW TABLE STATUS
3030 $result = $db->sql_query($sql, 7200);
3033 while ($row = $db->sql_fetchrow($result))
3035 if ((isset($row['Type']) && $row['Type'] != 'MRG_MyISAM') || (isset($row['Engine']) && ($row['Engine'] == 'MyISAM' || $row['Engine'] == 'InnoDB')))
3037 if ($table_prefix != '')
3039 if (strpos($row['Name'], $table_prefix) !== false)
3041 $database_size += $row['Data_length'] + $row['Index_length'];
3046 $database_size += $row['Data_length'] + $row['Index_length'];
3050 $db->sql_freeresult($result);
3058 // if it on the local machine, we can get lucky
3059 if (file_exists($dbname))
3061 $database_size = filesize($dbname);
3069 if (file_exists($dbhost))
3071 $database_size = filesize($dbhost);
3079 $sql = 'SELECT ((SUM(size) * 8.0) * 1024.0) as dbsize
3081 $result = $db->sql_query($sql, 7200);
3082 $database_size = ($row = $db->sql_fetchrow($result)) ? $row['dbsize'] : false;
3083 $db->sql_freeresult($result);
3087 $sql = "SELECT proname
3089 WHERE proname = 'pg_database_size'";
3090 $result = $db->sql_query($sql);
3091 $row = $db->sql_fetchrow($result);
3092 $db->sql_freeresult($result);
3094 if ($row['proname'] == 'pg_database_size')
3096 $database = $db->dbname;
3097 if (strpos($database, '.') !== false)
3099 list($database, ) = explode('.', $database);
3104 WHERE datname = '$database'";
3105 $result = $db->sql_query($sql);
3106 $row = $db->sql_fetchrow($result);
3107 $db->sql_freeresult($result);
3111 $sql = 'SELECT pg_database_size(' . $oid . ') as size';
3112 $result = $db->sql_query($sql);
3113 $row = $db->sql_fetchrow($result);
3114 $db->sql_freeresult($result);
3116 $database_size = $row['size'];
3121 $sql = 'SELECT SUM(bytes) as dbsize
3122 FROM user_segments';
3123 $result = $db->sql_query($sql, 7200);
3124 $database_size = ($row = $db->sql_fetchrow($result)) ? $row['dbsize'] : false;
3125 $db->sql_freeresult($result);
3129 $database_size = ($database_size !== false) ? get_formatted_filesize($database_size) : $user->lang['NOT_AVAILABLE'];
3131 return $database_size;
3135 * Retrieve contents from remotely stored file
3137 function get_remote_file($host, $directory, $filename, &$errstr, &$errno, $port = 80, $timeout = 6)
3141 if ($fsock = @fsockopen($host, $port, $errno, $errstr, $timeout))
3143 @fputs($fsock, "GET $directory/$filename HTTP/1.1\r\n");
3144 @fputs($fsock, "HOST: $host\r\n");
3145 @fputs($fsock, "Connection: close\r\n\r\n");
3147 $timer_stop = time() + $timeout;
3148 stream_set_timeout($fsock, $timeout);
3153 while (!@feof($fsock))
3157 $file_info .= @fread($fsock, 1024);
3161 $line = @fgets($fsock, 1024);
3162 if ($line == "\r\n")
3166 else if (stripos($line, '404 not found') !== false)
3168 $errstr = $user->lang['FILE_NOT_FOUND'] . ': ' . $filename;
3173 $stream_meta_data = stream_get_meta_data($fsock);
3175 if (!empty($stream_meta_data['timed_out']) || time() >= $timer_stop)
3177 $errstr = $user->lang['FSOCK_TIMEOUT'];
3187 $errstr = utf8_convert_message($errstr);
3192 $errstr = $user->lang['FSOCK_DISABLED'];
3202 * Remove all warnings which have now expired from the database
3203 * The duration of a warning can be defined by the administrator
3204 * This only removes the warning and reduces the associated count,
3205 * it does not remove the user note recording the contents of the warning
3207 function tidy_warnings()
3209 global $db, $config;
3211 $expire_date = time() - ($config['warnings_expire_days'] * 86400);
3212 $warning_list = $user_list = array();
3214 $sql = 'SELECT * FROM ' . WARNINGS_TABLE . "
3215 WHERE warning_time < $expire_date";
3216 $result = $db->sql_query($sql);
3218 while ($row = $db->sql_fetchrow($result))
3220 $warning_list[] = $row['warning_id'];
3221 $user_list[$row['user_id']] = isset($user_list[$row['user_id']]) ? ++$user_list[$row['user_id']] : 1;
3223 $db->sql_freeresult($result);
3225 if (sizeof($warning_list))
3227 $db->sql_transaction('begin');
3229 $sql = 'DELETE FROM ' . WARNINGS_TABLE . '
3230 WHERE ' . $db->sql_in_set('warning_id', $warning_list);
3231 $db->sql_query($sql);
3233 foreach ($user_list as $user_id => $value)
3235 $sql = 'UPDATE ' . USERS_TABLE . " SET user_warnings = user_warnings - $value
3236 WHERE user_id = $user_id";
3237 $db->sql_query($sql);
3240 $db->sql_transaction('commit');
3243 set_config('warnings_last_gc', time(), true);
3247 * Tidy database, doing some maintanance tasks
3249 function tidy_database()
3253 // Here we check permission consistency
3255 // Sometimes, it can happen permission tables having forums listed which do not exist
3256 $sql = 'SELECT forum_id
3257 FROM ' . FORUMS_TABLE;
3258 $result = $db->sql_query($sql);
3260 $forum_ids = array(0);
3261 while ($row = $db->sql_fetchrow($result))
3263 $forum_ids[] = $row['forum_id'];
3265 $db->sql_freeresult($result);
3267 // Delete those rows from the acl tables not having listed the forums above
3268 $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
3269 WHERE ' . $db->sql_in_set('forum_id', $forum_ids, true);
3270 $db->sql_query($sql);
3272 $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
3273 WHERE ' . $db->sql_in_set('forum_id', $forum_ids, true);
3274 $db->sql_query($sql);
3276 set_config('database_last_gc', time(), true);
3280 * Add permission language - this will make sure custom files will be included
3282 function add_permission_language()
3284 global $user, $phpEx;
3286 // First of all, our own file. We need to include it as the first file because it presets all relevant variables.
3287 $user->add_lang('acp/permissions_phpbb');
3289 $files_to_add = array();
3291 // Now search in acp and mods folder for permissions_ files.
3292 foreach (array('acp/', 'mods/') as $path)
3294 $dh = @opendir($user->lang_path . $user->lang_name . '/' . $path);
3298 while (($file = readdir($dh)) !== false)
3300 if ($file !== 'permissions_phpbb.' . $phpEx && strpos($file, 'permissions_') === 0 && substr($file, -(strlen($phpEx) + 1)) === '.' . $phpEx)
3302 $files_to_add[] = $path . substr($file, 0, -(strlen($phpEx) + 1));
3309 if (!sizeof($files_to_add))
3314 $user->add_lang($files_to_add);
3319 * Obtains the latest version information
3321 * @param bool $force_update Ignores cached data. Defaults to false.
3322 * @param bool $warn_fail Trigger a warning if obtaining the latest version information fails. Defaults to false.
3323 * @param int $ttl Cache version information for $ttl seconds. Defaults to 86400 (24 hours).
3325 * @return string | false Version info on success, false on failure.
3327 function obtain_latest_version_info($force_update = false, $warn_fail = false, $ttl = 86400)
3331 $info = $cache->get('versioncheck');
3333 if ($info === false || $force_update)
3338 $info = get_remote_file('version.phpbb.com', '/phpbb',
3339 ((defined('PHPBB_QA')) ? '30x_qa.txt' : '30x.txt'), $errstr, $errno);
3341 if ($info === false)
3343 $cache->destroy('versioncheck');
3346 trigger_error($errstr, E_USER_WARNING);
3351 $cache->put('versioncheck', $info, $ttl);
3358 * Enables a particular flag in a bitfield column of a given table.
3360 * @param string $table_name The table to update
3361 * @param string $column_name The column containing a bitfield to update
3362 * @param int $flag The binary flag which is OR-ed with the current column value
3363 * @param string $sql_more This string is attached to the sql query generated to update the table.
3367 function enable_bitfield_column_flag($table_name, $column_name, $flag, $sql_more = '')
3371 $sql = 'UPDATE ' . $table_name . '
3372 SET ' . $column_name . ' = ' . $db->sql_bit_or($column_name, $flag) . '
3374 $db->sql_query($sql);