Update code_sniffer build.xml file to be executable on our system
[phpbb.git] / phpBB / includes / functions_admin.php
blobdee0f82566e9adb8b52c8b2b6d2dd9f3040caaf5
1 <?php
2 /**
4 * @package acp
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
20 * Recalculate Binary Tree
21 function recalc_btree($sql_id, $sql_table, $module_class = '')
23 if (!$sql_id || !$sql_table)
25 return;
28 $sql_where = ($module_class) ? " WHERE module_class = '" . phpbb::$db->sql_escape($module_class) . "'" : '';
30 // Reset to minimum possible left and right id
31 $sql = "SELECT MIN(left_id) as min_left_id, MIN(right_id) as min_right_id
32 FROM $sql_table
33 $sql_where";
34 $result = phpbb::$db->sql_query($sql);
35 $row = phpbb::$db->sql_fetchrow($result);
36 phpbb::$db->sql_freeresult($result);
38 $substract = (int) (min($row['min_left_id'], $row['min_right_id']) - 1);
40 if ($substract > 0)
42 $sql = "UPDATE $sql_table
43 SET left_id = left_id - $substract, right_id = right_id - $substract
44 $sql_where";
45 phpbb::$db->sql_query($sql);
48 $sql = "SELECT $sql_id, parent_id, left_id, right_id
49 FROM $sql_table
50 $sql_where
51 ORDER BY left_id ASC, parent_id ASC, $sql_id ASC";
52 $f_result = phpbb::$db->sql_query($sql);
54 while ($item_data = phpbb::$db->sql_fetchrow($f_result))
56 if ($item_data['parent_id'])
58 $sql = "SELECT left_id, right_id
59 FROM $sql_table
60 $sql_where " . (($sql_where) ? 'AND' : 'WHERE') . "
61 $sql_id = {$item_data['parent_id']}";
62 $result = phpbb::$db->sql_query($sql);
64 if (!$row = phpbb::$db->sql_fetchrow($result))
66 $sql = "UPDATE $sql_table SET parent_id = 0 WHERE $sql_id = " . $item_data[$sql_id];
67 phpbb::$db->sql_query($sql);
69 phpbb::$db->sql_freeresult($result);
71 $sql = "UPDATE $sql_table
72 SET left_id = left_id + 2, right_id = right_id + 2
73 $sql_where " . (($sql_where) ? 'AND' : 'WHERE') . "
74 left_id > {$row['right_id']}";
75 phpbb::$db->sql_query($sql);
77 $sql = "UPDATE $sql_table
78 SET right_id = right_id + 2
79 $sql_where " . (($sql_where) ? 'AND' : 'WHERE') . "
80 {$row['left_id']} BETWEEN left_id AND right_id";
81 phpbb::$db->sql_query($sql);
83 $item_data['left_id'] = $row['right_id'];
84 $item_data['right_id'] = $row['right_id'] + 1;
86 else
88 $sql = "SELECT MAX(right_id) AS right_id
89 FROM $sql_table
90 $sql_where";
91 $result = phpbb::$db->sql_query($sql);
92 $row = phpbb::$db->sql_fetchrow($result);
93 phpbb::$db->sql_freeresult($result);
95 $item_data['left_id'] = $row['right_id'] + 1;
96 $item_data['right_id'] = $row['right_id'] + 2;
99 $sql = "UPDATE $sql_table
100 SET left_id = {$item_data['left_id']}, right_id = {$item_data['right_id']}
101 WHERE $sql_id = " . $item_data[$sql_id];
102 phpbb::$db->sql_query($sql);
104 phpbb::$db->sql_freeresult($f_result);
109 * Simple version of jumpbox, just lists authed forums
111 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)
113 $acl = ($ignore_acl) ? '' : (($only_acl_post) ? 'f_post' : array('f_list', 'a_forum', 'a_forumadd', 'a_forumdel'));
115 // This query is identical to the jumpbox one
116 $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
117 FROM ' . FORUMS_TABLE . '
118 ORDER BY left_id ASC';
119 $result = phpbb::$db->sql_query($sql, 600);
121 $right = 0;
122 $padding_store = array('0' => '');
123 $padding = '';
124 $forum_list = ($return_array) ? array() : '';
126 // Sometimes it could happen that forums will be displayed here not be displayed within the index page
127 // This is the result of forums not displayed at index, having list permissions and a parent of a forum with no permissions.
128 // If this happens, the padding could be "broken"
130 while ($row = phpbb::$db->sql_fetchrow($result))
132 if ($row['left_id'] < $right)
134 $padding .= '&nbsp; &nbsp;';
135 $padding_store[$row['parent_id']] = $padding;
137 else if ($row['left_id'] > $right + 1)
139 $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
142 $right = $row['right_id'];
143 $disabled = false;
145 if ($acl && !phpbb::$acl->acl_gets($acl, $row['forum_id']))
147 // List permission?
148 if (phpbb::$acl->acl_get('f_list', $row['forum_id']))
150 $disabled = true;
152 else
154 continue;
158 if (
159 ((is_array($ignore_id) && in_array($row['forum_id'], $ignore_id)) || $row['forum_id'] == $ignore_id)
161 // Non-postable forum with no subforums, don't display
162 ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']) && $ignore_emptycat)
164 ($row['forum_type'] != FORUM_POST && $ignore_nonpost)
167 $disabled = true;
170 if ($return_array)
172 // Include some more information...
173 $selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? true : false) : (($row['forum_id'] == $select_id) ? true : false);
174 $forum_list[$row['forum_id']] = array_merge(array('padding' => $padding, 'selected' => ($selected && !$disabled), 'disabled' => $disabled), $row);
176 else
178 $selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? ' selected="selected"' : '') : (($row['forum_id'] == $select_id) ? ' selected="selected"' : '');
179 $forum_list .= '<option value="' . $row['forum_id'] . '"' . (($disabled) ? ' disabled="disabled" class="disabled-option"' : $selected) . '>' . $padding . $row['forum_name'] . '</option>';
182 phpbb::$db->sql_freeresult($result);
183 unset($padding_store);
185 return $forum_list;
189 * Generate size select options
191 function size_select_options($size_compare)
193 $size_types_text = array(phpbb::$user->lang['BYTES'], phpbb::$user->lang['KIB'], phpbb::$user->lang['MIB']);
194 $size_types = array('b', 'kb', 'mb');
196 $s_size_options = '';
198 for ($i = 0, $size = sizeof($size_types_text); $i < $size; $i++)
200 $selected = ($size_compare == $size_types[$i]) ? ' selected="selected"' : '';
201 $s_size_options .= '<option value="' . $size_types[$i] . '"' . $selected . '>' . $size_types_text[$i] . '</option>';
204 return $s_size_options;
208 * Generate list of groups (option fields without select)
210 * @param int $group_id The default group id to mark as selected
211 * @param array $exclude_ids The group ids to exclude from the list, false (default) if you whish to exclude no id
212 * @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.
214 * @return string The list of options.
216 function group_select_options($group_id, $exclude_ids = false, $manage_founder = false)
218 $exclude_sql = ($exclude_ids !== false && sizeof($exclude_ids)) ? 'WHERE ' . phpbb::$db->sql_in_set('group_id', array_map('intval', $exclude_ids), true) : '';
219 $sql_and = (!phpbb::$config['coppa_enable']) ? (($exclude_sql) ? ' AND ' : ' WHERE ') . "group_name <> 'REGISTERED_COPPA'" : '';
220 $sql_founder = ($manage_founder !== false) ? (($exclude_sql || $sql_and) ? ' AND ' : ' WHERE ') . 'group_founder_manage = ' . (int) $manage_founder : '';
222 $sql = 'SELECT group_id, group_name, group_type
223 FROM ' . GROUPS_TABLE . "
224 $exclude_sql
225 $sql_and
226 $sql_founder
227 ORDER BY group_type DESC, group_name ASC";
228 $result = phpbb::$db->sql_query($sql);
230 $s_group_options = '';
231 while ($row = phpbb::$db->sql_fetchrow($result))
233 $selected = ($row['group_id'] == $group_id) ? ' selected="selected"' : '';
234 $s_group_options .= '<option' . (($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '') . ' value="' . $row['group_id'] . '"' . $selected . '>' . (($row['group_type'] == GROUP_SPECIAL) ? phpbb::$user->lang['G_' . $row['group_name']] : $row['group_name']) . '</option>';
236 phpbb::$db->sql_freeresult($result);
238 return $s_group_options;
242 * Obtain authed forums list
244 function get_forum_list($acl_list = 'f_list', $id_only = true, $postable_only = false, $no_cache = false)
246 static $forum_rows;
248 if (!isset($forum_rows))
250 // This query is identical to the jumpbox one
251 $expire_time = ($no_cache) ? 0 : 600;
253 $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
254 FROM ' . FORUMS_TABLE . '
255 ORDER BY left_id ASC';
256 $result = phpbb::$db->sql_query($sql, $expire_time);
258 $forum_rows = array();
260 $right = $padding = 0;
261 $padding_store = array('0' => 0);
263 while ($row = phpbb::$db->sql_fetchrow($result))
265 if ($row['left_id'] < $right)
267 $padding++;
268 $padding_store[$row['parent_id']] = $padding;
270 else if ($row['left_id'] > $right + 1)
272 // Ok, if the $padding_store for this parent is empty there is something wrong. For now we will skip over it.
273 // @todo digging deep to find out "how" this can happen.
274 $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : $padding;
277 $right = $row['right_id'];
278 $row['padding'] = $padding;
280 $forum_rows[] = $row;
282 phpbb::$db->sql_freeresult($result);
283 unset($padding_store);
286 $rowset = array();
287 foreach ($forum_rows as $row)
289 if ($postable_only && $row['forum_type'] != FORUM_POST)
291 continue;
294 if ($acl_list == '' || ($acl_list != '' && phpbb::$acl->acl_gets($acl_list, $row['forum_id'])))
296 $rowset[] = ($id_only) ? $row['forum_id'] : $row;
300 return $rowset;
304 * Get forum branch
306 function get_forum_branch($forum_id, $type = 'all', $order = 'descending', $include_forum = true)
308 switch ($type)
310 case 'parents':
311 $condition = 'f1.left_id BETWEEN f2.left_id AND f2.right_id';
312 break;
314 case 'children':
315 $condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id';
316 break;
318 default:
319 $condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id OR f1.left_id BETWEEN f2.left_id AND f2.right_id';
320 break;
323 $rows = array();
325 $sql = 'SELECT f2.*
326 FROM ' . FORUMS_TABLE . ' f1
327 LEFT JOIN ' . FORUMS_TABLE . " f2 ON ($condition)
328 WHERE f1.forum_id = $forum_id
329 ORDER BY f2.left_id " . (($order == 'descending') ? 'ASC' : 'DESC');
330 $result = phpbb::$db->sql_query($sql);
332 while ($row = phpbb::$db->sql_fetchrow($result))
334 if (!$include_forum && $row['forum_id'] == $forum_id)
336 continue;
339 $rows[] = $row;
341 phpbb::$db->sql_freeresult($result);
343 return $rows;
347 * Get physical file listing
349 function filelist($rootdir, $dir = '', $type = 'gif|jpg|jpeg|png')
351 $matches = array();
353 // Remove initial / if present
354 $rootdir = (substr($rootdir, 0, 1) == '/') ? substr($rootdir, 1) : $rootdir;
355 // Add closing / if not present
356 $rootdir = ($rootdir && substr($rootdir, -1) != '/') ? $rootdir . '/' : $rootdir;
358 // Remove initial / if present
359 $dir = (substr($dir, 0, 1) == '/') ? substr($dir, 1) : $dir;
360 // Add closing / if not present
361 $dir = ($dir && substr($dir, -1) != '/') ? $dir . '/' : $dir;
363 if (!is_dir($rootdir . $dir))
365 return $matches;
368 $dh = @opendir($rootdir . $dir);
370 if (!$dh)
372 return $matches;
375 while (($fname = readdir($dh)) !== false)
377 if (is_file("$rootdir$dir$fname"))
379 if (filesize("$rootdir$dir$fname") && preg_match('#\.' . $type . '$#i', $fname))
381 $matches[$dir][] = $fname;
384 else if ($fname[0] != '.' && is_dir("$rootdir$dir$fname"))
386 $matches += filelist($rootdir, "$dir$fname", $type);
389 closedir($dh);
391 return $matches;
395 * Move topic(s)
397 function move_topics($topic_ids, $forum_id, $auto_sync = true)
399 if (empty($topic_ids))
401 return;
404 $forum_ids = array($forum_id);
406 if (!is_array($topic_ids))
408 $topic_ids = array($topic_ids);
411 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
412 WHERE ' . phpbb::$db->sql_in_set('topic_moved_id', $topic_ids) . '
413 AND forum_id = ' . $forum_id;
414 phpbb::$db->sql_query($sql);
416 if ($auto_sync)
418 $sql = 'SELECT DISTINCT forum_id
419 FROM ' . TOPICS_TABLE . '
420 WHERE ' . phpbb::$db->sql_in_set('topic_id', $topic_ids);
421 $result = phpbb::$db->sql_query($sql);
423 while ($row = phpbb::$db->sql_fetchrow($result))
425 $forum_ids[] = $row['forum_id'];
427 phpbb::$db->sql_freeresult($result);
430 $table_ary = array(TOPICS_TABLE, POSTS_TABLE, LOG_TABLE, DRAFTS_TABLE, TOPICS_TRACK_TABLE);
431 foreach ($table_ary as $table)
433 $sql = "UPDATE $table
434 SET forum_id = $forum_id
435 WHERE " . phpbb::$db->sql_in_set('topic_id', $topic_ids);
436 phpbb::$db->sql_query($sql);
438 unset($table_ary);
440 if ($auto_sync)
442 sync('forum', 'forum_id', $forum_ids, true, true);
443 unset($forum_ids);
448 * Move post(s)
450 function move_posts($post_ids, $topic_id, $auto_sync = true)
452 if (!is_array($post_ids))
454 $post_ids = array($post_ids);
457 $forum_ids = array();
458 $topic_ids = array($topic_id);
460 $sql = 'SELECT DISTINCT topic_id, forum_id
461 FROM ' . POSTS_TABLE . '
462 WHERE ' . phpbb::$db->sql_in_set('post_id', $post_ids);
463 $result = phpbb::$db->sql_query($sql);
465 while ($row = phpbb::$db->sql_fetchrow($result))
467 $forum_ids[] = $row['forum_id'];
468 $topic_ids[] = $row['topic_id'];
470 phpbb::$db->sql_freeresult($result);
472 $sql = 'SELECT forum_id
473 FROM ' . TOPICS_TABLE . '
474 WHERE topic_id = ' . $topic_id;
475 $result = phpbb::$db->sql_query($sql);
476 $forum_row = phpbb::$db->sql_fetchrow($result);
477 phpbb::$db->sql_freeresult($result);
479 if (!$forum_row)
481 trigger_error('NO_TOPIC');
484 $sql = 'UPDATE ' . POSTS_TABLE . '
485 SET forum_id = ' . $forum_row['forum_id'] . ", topic_id = $topic_id
486 WHERE " . phpbb::$db->sql_in_set('post_id', $post_ids);
487 phpbb::$db->sql_query($sql);
489 $sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
490 SET topic_id = $topic_id, in_message = 0
491 WHERE " . phpbb::$db->sql_in_set('post_msg_id', $post_ids);
492 phpbb::$db->sql_query($sql);
494 if ($auto_sync)
496 $forum_ids[] = $forum_row['forum_id'];
498 sync('topic_reported', 'topic_id', $topic_ids);
499 sync('topic_attachment', 'topic_id', $topic_ids);
500 sync('topic', 'topic_id', $topic_ids, true);
501 sync('forum', 'forum_id', $forum_ids, true, true);
504 // Update posted information
505 update_posted_info($topic_ids);
509 * Remove topic(s)
511 function delete_topics($where_type, $where_ids, $auto_sync = true, $post_count_sync = true, $call_delete_posts = true)
513 $approved_topics = 0;
514 $forum_ids = $topic_ids = array();
516 if ($where_type === 'range')
518 $where_clause = $where_ids;
520 else
522 $where_ids = (is_array($where_ids)) ? array_unique($where_ids) : array($where_ids);
524 if (!sizeof($where_ids))
526 return array('topics' => 0, 'posts' => 0);
529 $where_clause = phpbb::$db->sql_in_set($where_type, $where_ids);
532 // Making sure that delete_posts does not call delete_topics again...
533 $return = array(
534 'posts' => ($call_delete_posts) ? delete_posts($where_type, $where_ids, false, true, $post_count_sync, false) : 0,
537 $sql = 'SELECT topic_id, forum_id, topic_approved, topic_moved_id
538 FROM ' . TOPICS_TABLE . '
539 WHERE ' . $where_clause;
540 $result = phpbb::$db->sql_query($sql);
542 while ($row = phpbb::$db->sql_fetchrow($result))
544 $forum_ids[] = $row['forum_id'];
545 $topic_ids[] = $row['topic_id'];
547 if ($row['topic_approved'] && !$row['topic_moved_id'])
549 $approved_topics++;
552 phpbb::$db->sql_freeresult($result);
554 $return['topics'] = sizeof($topic_ids);
556 if (!sizeof($topic_ids))
558 return $return;
561 phpbb::$db->sql_transaction('begin');
563 $table_ary = array(TOPICS_TRACK_TABLE, TOPICS_POSTED_TABLE, POLL_VOTES_TABLE, POLL_OPTIONS_TABLE, TOPICS_WATCH_TABLE, TOPICS_TABLE);
565 foreach ($table_ary as $table)
567 $sql = "DELETE FROM $table
568 WHERE " . phpbb::$db->sql_in_set('topic_id', $topic_ids);
569 phpbb::$db->sql_query($sql);
571 unset($table_ary);
573 $moved_topic_ids = array();
575 // update the other forums
576 $sql = 'SELECT topic_id, forum_id
577 FROM ' . TOPICS_TABLE . '
578 WHERE ' . phpbb::$db->sql_in_set('topic_moved_id', $topic_ids);
579 $result = phpbb::$db->sql_query($sql);
581 while ($row = phpbb::$db->sql_fetchrow($result))
583 $forum_ids[] = $row['forum_id'];
584 $moved_topic_ids[] = $row['topic_id'];
586 phpbb::$db->sql_freeresult($result);
588 if (sizeof($moved_topic_ids))
590 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
591 WHERE ' . phpbb::$db->sql_in_set('topic_id', $moved_topic_ids);
592 phpbb::$db->sql_query($sql);
595 phpbb::$db->sql_transaction('commit');
597 if ($auto_sync)
599 sync('forum', 'forum_id', array_unique($forum_ids), true, true);
600 sync('topic_reported', $where_type, $where_ids);
603 if ($approved_topics)
605 set_config_count('num_topics', $approved_topics * (-1), true);
608 return $return;
612 * Remove post(s)
614 function delete_posts($where_type, $where_ids, $auto_sync = true, $posted_sync = true, $post_count_sync = true, $call_delete_topics = true)
616 if ($where_type === 'range')
618 $where_clause = $where_ids;
620 else
622 if (is_array($where_ids))
624 $where_ids = array_unique($where_ids);
626 else
628 $where_ids = array($where_ids);
631 if (!sizeof($where_ids))
633 return false;
636 $where_ids = array_map('intval', $where_ids);
638 // Split post deletion into chunks to overcome database limitations
639 if (sizeof($where_ids) >= 1001)
641 // Split into chunks of 1000
642 $chunks = array_chunk($where_ids, 1000);
643 $removed_posts = 0;
645 foreach ($chunks as $_where_ids)
647 $removed_posts += delete_posts($where_type, $_where_ids, $auto_sync, $posted_sync, $post_count_sync, $call_delete_topics);
650 return $removed_posts;
653 $where_clause = phpbb::$db->sql_in_set($where_type, $where_ids);
656 $approved_posts = 0;
657 $post_ids = $topic_ids = $forum_ids = $post_counts = $remove_topics = array();
659 $sql = 'SELECT post_id, poster_id, post_approved, post_postcount, topic_id, forum_id
660 FROM ' . POSTS_TABLE . '
661 WHERE ' . $where_clause;
662 $result = phpbb::$db->sql_query($sql);
664 while ($row = phpbb::$db->sql_fetchrow($result))
666 $post_ids[] = (int) $row['post_id'];
667 $poster_ids[] = (int) $row['poster_id'];
668 $topic_ids[] = (int) $row['topic_id'];
669 $forum_ids[] = (int) $row['forum_id'];
671 if ($row['post_postcount'] && $post_count_sync && $row['post_approved'])
673 $post_counts[$row['poster_id']] = (!empty($post_counts[$row['poster_id']])) ? $post_counts[$row['poster_id']] + 1 : 1;
676 if ($row['post_approved'])
678 $approved_posts++;
681 phpbb::$db->sql_freeresult($result);
683 if (!sizeof($post_ids))
685 return false;
688 phpbb::$db->sql_transaction('begin');
690 $table_ary = array(POSTS_TABLE, REPORTS_TABLE);
692 foreach ($table_ary as $table)
694 $sql = "DELETE FROM $table
695 WHERE " . phpbb::$db->sql_in_set('post_id', $post_ids);
696 phpbb::$db->sql_query($sql);
698 unset($table_ary);
700 // Adjust users post counts
701 if (sizeof($post_counts) && $post_count_sync)
703 foreach ($post_counts as $poster_id => $substract)
705 $sql = 'UPDATE ' . USERS_TABLE . '
706 SET user_posts = 0
707 WHERE user_id = ' . $poster_id . '
708 AND user_posts < ' . $substract;
709 phpbb::$db->sql_query($sql);
711 $sql = 'UPDATE ' . USERS_TABLE . '
712 SET user_posts = user_posts - ' . $substract . '
713 WHERE user_id = ' . $poster_id . '
714 AND user_posts >= ' . $substract;
715 phpbb::$db->sql_query($sql);
719 // Remove topics now having no posts?
720 if (sizeof($topic_ids))
722 $sql = 'SELECT topic_id
723 FROM ' . POSTS_TABLE . '
724 WHERE ' . phpbb::$db->sql_in_set('topic_id', $topic_ids) . '
725 GROUP BY topic_id';
726 $result = phpbb::$db->sql_query($sql);
728 while ($row = phpbb::$db->sql_fetchrow($result))
730 $remove_topics[] = $row['topic_id'];
732 phpbb::$db->sql_freeresult($result);
734 // Actually, those not within remove_topics should be removed. ;)
735 $remove_topics = array_diff($topic_ids, $remove_topics);
738 // Remove the message from the search index
739 $search_type = basename(phpbb::$config['search_type']);
741 if (!file_exists(PHPBB_ROOT_PATH . 'includes/search/' . $search_type . '.' . PHP_EXT))
743 trigger_error('NO_SUCH_SEARCH_MODULE');
746 include_once(PHPBB_ROOT_PATH . "includes/search/$search_type." . PHP_EXT);
748 $error = false;
749 $search = new $search_type($error);
751 if ($error)
753 trigger_error($error);
756 $search->index_remove($post_ids, $poster_ids, $forum_ids);
758 delete_attachments('post', $post_ids, false);
760 phpbb::$db->sql_transaction('commit');
762 // Resync topics_posted table
763 if ($posted_sync)
765 update_posted_info($topic_ids);
768 if ($auto_sync)
770 sync('topic_reported', 'topic_id', $topic_ids);
771 sync('topic', 'topic_id', $topic_ids, true);
772 sync('forum', 'forum_id', $forum_ids, true, true);
775 if ($approved_posts)
777 set_config_count('num_posts', $approved_posts * (-1), true);
780 // We actually remove topics now to not be inconsistent (the delete_topics function calls this function too)
781 if (sizeof($remove_topics) && $call_delete_topics)
783 delete_topics('topic_id', $remove_topics, $auto_sync, $post_count_sync, false);
786 return sizeof($post_ids);
790 * Delete Attachments
792 * @param string $mode can be: post|message|topic|attach|user
793 * @param mixed $ids can be: post_ids, message_ids, topic_ids, attach_ids, user_ids
794 * @param bool $resync set this to false if you are deleting posts or topics
796 function delete_attachments($mode, $ids, $resync = true)
798 if (is_array($ids) && sizeof($ids))
800 $ids = array_unique($ids);
801 $ids = array_map('intval', $ids);
803 else
805 $ids = array((int) $ids);
808 if (!sizeof($ids))
810 return false;
813 $sql_where = '';
815 switch ($mode)
817 case 'post':
818 case 'message':
819 $sql_id = 'post_msg_id';
820 $sql_where = ' AND in_message = ' . ($mode == 'message' ? 1 : 0);
821 break;
823 case 'topic':
824 $sql_id = 'topic_id';
825 break;
827 case 'user':
828 $sql_id = 'poster_id';
829 break;
831 case 'attach':
832 default:
833 $sql_id = 'attach_id';
834 $mode = 'attach';
835 break;
838 $post_ids = $message_ids = $topic_ids = $physical = array();
840 // Collect post and topic ids for later use if we need to touch remaining entries (if resync is enabled)
841 $sql = 'SELECT post_msg_id, topic_id, in_message, physical_filename, thumbnail, filesize, is_orphan
842 FROM ' . ATTACHMENTS_TABLE . '
843 WHERE ' . phpbb::$db->sql_in_set($sql_id, $ids);
845 $sql .= $sql_where;
847 $result = phpbb::$db->sql_query($sql);
849 while ($row = phpbb::$db->sql_fetchrow($result))
851 // We only need to store post/message/topic ids if resync is enabled and the file is not orphaned
852 if ($resync && !$row['is_orphan'])
854 if (!$row['in_message'])
856 $post_ids[] = $row['post_msg_id'];
857 $topic_ids[] = $row['topic_id'];
859 else
861 $message_ids[] = $row['post_msg_id'];
865 $physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize'], 'is_orphan' => $row['is_orphan']);
867 phpbb::$db->sql_freeresult($result);
869 // Delete attachments
870 $sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . '
871 WHERE ' . phpbb::$db->sql_in_set($sql_id, $ids);
873 $sql .= $sql_where;
875 phpbb::$db->sql_query($sql);
876 $num_deleted = phpbb::$db->sql_affectedrows();
878 if (!$num_deleted)
880 return 0;
883 // Delete attachments from filesystem
884 $space_removed = $files_removed = 0;
885 foreach ($physical as $file_ary)
887 if (phpbb_unlink($file_ary['filename'], 'file', true) && !$file_ary['is_orphan'])
889 // Only non-orphaned files count to the file size
890 $space_removed += $file_ary['filesize'];
891 $files_removed++;
894 if ($file_ary['thumbnail'])
896 phpbb_unlink($file_ary['filename'], 'thumbnail', true);
900 if ($space_removed || $files_removed)
902 set_config_count('upload_dir_size', $space_removed * (-1), true);
903 set_config_count('num_files', $files_removed * (-1), true);
906 // If we do not resync, we do not need to adjust any message, post, topic or user entries
907 if (!$resync)
909 return $num_deleted;
912 // No more use for the original ids
913 unset($ids);
915 // Now, we need to resync posts, messages, topics. We go through every one of them
916 $post_ids = array_unique($post_ids);
917 $message_ids = array_unique($message_ids);
918 $topic_ids = array_unique($topic_ids);
920 // Update post indicators for posts now no longer having attachments
921 if (sizeof($post_ids))
923 $sql = 'UPDATE ' . POSTS_TABLE . '
924 SET post_attachment = 0
925 WHERE ' . phpbb::$db->sql_in_set('post_id', $post_ids);
926 phpbb::$db->sql_query($sql);
929 // Update message table if messages are affected
930 if (sizeof($message_ids))
932 $sql = 'UPDATE ' . PRIVMSGS_TABLE . '
933 SET message_attachment = 0
934 WHERE ' . phpbb::$db->sql_in_set('msg_id', $message_ids);
935 phpbb::$db->sql_query($sql);
938 // Now update the topics. This is a bit trickier, because there could be posts still having attachments within the topic
939 if (sizeof($topic_ids))
941 // Just check which topics are still having an assigned attachment not orphaned by querying the attachments table (much less entries expected)
942 $sql = 'SELECT topic_id
943 FROM ' . ATTACHMENTS_TABLE . '
944 WHERE ' . phpbb::$db->sql_in_set('topic_id', $topic_ids) . '
945 AND is_orphan = 0';
946 $result = phpbb::$db->sql_query($sql);
948 $remaining_ids = array();
949 while ($row = phpbb::$db->sql_fetchrow($result))
951 $remaining_ids[] = $row['topic_id'];
953 phpbb::$db->sql_freeresult($result);
955 // Now only unset those ids remaining
956 $topic_ids = array_diff($topic_ids, $remaining_ids);
958 if (sizeof($topic_ids))
960 $sql = 'UPDATE ' . TOPICS_TABLE . '
961 SET topic_attachment = 0
962 WHERE ' . phpbb::$db->sql_in_set('topic_id', $topic_ids);
963 phpbb::$db->sql_query($sql);
967 return $num_deleted;
971 * Remove topic shadows
973 function delete_topic_shadows($max_age, $forum_id = '', $auto_sync = true)
975 $where = (is_array($forum_id)) ? 'AND ' . phpbb::$db->sql_in_set('t.forum_id', array_map('intval', $forum_id)) : (($forum_id) ? 'AND t.forum_id = ' . (int) $forum_id : '');
977 if (phpbb::$db->features['multi_table_deletion'])
979 $sql = 'DELETE FROM ' . TOPICS_TABLE . ' t
980 USING ' . TOPICS_TABLE . ' t2
981 WHERE t.topic_moved_id = t2.topic_id
982 AND t.topic_time < ' . (time() - $max_age)
983 . $where;
984 phpbb::$db->sql_query($sql);
986 else
988 $sql = 'SELECT t.topic_id
989 FROM ' . TOPICS_TABLE . ' t, ' . TOPICS_TABLE . ' t2
990 WHERE t.topic_moved_id = t2.topic_id
991 AND t.topic_time < ' . (time() - $max_age)
992 . $where;
993 $result = phpbb::$db->sql_query($sql);
995 $topic_ids = array();
996 while ($row = phpbb::$db->sql_fetchrow($result))
998 $topic_ids[] = $row['topic_id'];
1000 phpbb::$db->sql_freeresult($result);
1002 if (sizeof($topic_ids))
1004 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1005 WHERE ' . phpbb::$db->sql_in_set('topic_id', $topic_ids);
1006 phpbb::$db->sql_query($sql);
1010 if ($auto_sync)
1012 $where_type = ($forum_id) ? 'forum_id' : '';
1013 sync('forum', $where_type, $forum_id, true, true);
1018 * Update/Sync posted information for topics
1020 function update_posted_info(&$topic_ids)
1022 if (empty($topic_ids) || !phpbb::$config['load_db_track'])
1024 return;
1027 // First of all, let us remove any posted information for these topics
1028 $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
1029 WHERE ' . phpbb::$db->sql_in_set('topic_id', $topic_ids);
1030 phpbb::$db->sql_query($sql);
1032 // Now, let us collect the user/topic combos for rebuilding the information
1033 $sql = 'SELECT poster_id, topic_id
1034 FROM ' . POSTS_TABLE . '
1035 WHERE ' . phpbb::$db->sql_in_set('topic_id', $topic_ids) . '
1036 AND poster_id <> ' . ANONYMOUS . '
1037 GROUP BY poster_id, topic_id';
1038 $result = phpbb::$db->sql_query($sql);
1040 $posted = array();
1041 while ($row = phpbb::$db->sql_fetchrow($result))
1043 // Add as key to make them unique (grouping by) and circumvent empty keys on array_unique
1044 $posted[$row['poster_id']][] = $row['topic_id'];
1046 phpbb::$db->sql_freeresult($result);
1048 // Now add the information...
1049 $sql_ary = array();
1050 foreach ($posted as $user_id => $topic_row)
1052 foreach ($topic_row as $topic_id)
1054 $sql_ary[] = array(
1055 'user_id' => (int) $user_id,
1056 'topic_id' => (int) $topic_id,
1057 'topic_posted' => 1,
1061 unset($posted);
1063 phpbb::$db->sql_multi_insert(TOPICS_POSTED_TABLE, $sql_ary);
1067 * Delete attached file
1069 function phpbb_unlink($filename, $mode = 'file', $entry_removed = false)
1071 // Because of copying topics or modifications a physical filename could be assigned more than once. If so, do not remove the file itself.
1072 $sql = 'SELECT COUNT(attach_id) AS num_entries
1073 FROM ' . ATTACHMENTS_TABLE . "
1074 WHERE physical_filename = '" . phpbb::$db->sql_escape(basename($filename)) . "'";
1075 $result = phpbb::$db->sql_query($sql);
1076 $num_entries = (int) phpbb::$db->sql_fetchfield('num_entries');
1077 phpbb::$db->sql_freeresult($result);
1079 // Do not remove file if at least one additional entry with the same name exist.
1080 if (($entry_removed && $num_entries > 0) || (!$entry_removed && $num_entries > 1))
1082 return false;
1085 $filename = ($mode == 'thumbnail') ? 'thumb_' . basename($filename) : basename($filename);
1086 return @unlink(PHPBB_ROOT_PATH . phpbb::$config['upload_path'] . '/' . $filename);
1090 * All-encompasing sync function
1092 * Exaples:
1093 * <code>
1094 * sync('topic', 'topic_id', 123); // resync topic #123
1095 * sync('topic', 'forum_id', array(2, 3)); // resync topics from forum #2 and #3
1096 * sync('topic'); // resync all topics
1097 * sync('topic', 'range', 'topic_id BETWEEN 1 AND 60'); // resync a range of topics/forums (only available for 'topic' and 'forum' modes)
1098 * </code>
1100 * Modes:
1101 * - forum Resync complete forum
1102 * - topic Resync topics
1103 * - topic_moved Removes topic shadows that would be in the same forum as the topic they link to
1104 * - topic_approved Resyncs the topic_approved flag according to the status of the first post
1105 * - post_reported Resyncs the post_reported flag, relying on actual reports
1106 * - topic_reported Resyncs the topic_reported flag, relying on post_reported flags
1107 * - post_attachement Same as post_reported, but with attachment flags
1108 * - topic_attachement Same as topic_reported, but with attachment flags
1110 function sync($mode, $where_type = '', $where_ids = '', $resync_parents = false, $sync_extra = false)
1112 if (is_array($where_ids))
1114 $where_ids = array_unique($where_ids);
1115 $where_ids = array_map('intval', $where_ids);
1117 else if ($where_type != 'range')
1119 $where_ids = ($where_ids) ? array((int) $where_ids) : array();
1122 if ($mode == 'forum' || $mode == 'topic' || $mode == 'topic_approved' || $mode == 'topic_reported' || $mode == 'post_reported')
1124 if (!$where_type)
1126 $where_sql = '';
1127 $where_sql_and = 'WHERE';
1129 else if ($where_type == 'range')
1131 // Only check a range of topics/forums. For instance: 'topic_id BETWEEN 1 AND 60'
1132 $where_sql = 'WHERE (' . $mode[0] . ".$where_ids)";
1133 $where_sql_and = $where_sql . "\n\tAND";
1135 else
1137 // Do not sync the "global forum"
1138 $where_ids = array_diff($where_ids, array(0));
1140 if (!sizeof($where_ids))
1142 // Empty array with IDs. This means that we don't have any work to do. Just return.
1143 return;
1146 // Limit the topics/forums we are syncing, use specific topic/forum IDs.
1147 // $where_type contains the field for the where clause (forum_id, topic_id)
1148 $where_sql = 'WHERE ' . phpbb::$db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
1149 $where_sql_and = $where_sql . "\n\tAND";
1152 else
1154 if (!sizeof($where_ids))
1156 return;
1159 // $where_type contains the field for the where clause (forum_id, topic_id)
1160 $where_sql = 'WHERE ' . phpbb::$db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
1161 $where_sql_and = $where_sql . "\n\tAND";
1164 switch ($mode)
1166 case 'topic_moved':
1167 if (phpbb::$db->features['multi_table_deletion'])
1169 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1170 USING ' . TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
1171 WHERE t1.topic_moved_id = t2.topic_id
1172 AND t1.forum_id = t2.forum_id";
1173 phpbb::$db->sql_query($sql);
1175 else
1177 $sql = 'SELECT t1.topic_id
1178 FROM ' .TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
1179 WHERE t1.topic_moved_id = t2.topic_id
1180 AND t1.forum_id = t2.forum_id";
1181 $result = phpbb::$db->sql_query($sql);
1183 $topic_id_ary = array();
1184 while ($row = phpbb::$db->sql_fetchrow($result))
1186 $topic_id_ary[] = $row['topic_id'];
1188 phpbb::$db->sql_freeresult($result);
1190 if (!sizeof($topic_id_ary))
1192 return;
1195 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1196 WHERE ' . phpbb::$db->sql_in_set('topic_id', $topic_id_ary);
1197 phpbb::$db->sql_query($sql);
1199 break;
1201 case 'topic_approved':
1202 if (phpbb::$db->dbms_type == 'mysql')
1204 $sql = 'UPDATE ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
1205 SET t.topic_approved = p.post_approved
1206 $where_sql_and t.topic_first_post_id = p.post_id";
1207 phpbb::$db->sql_query($sql);
1209 else
1211 $sql = 'SELECT t.topic_id, p.post_approved
1212 FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
1213 $where_sql_and p.post_id = t.topic_first_post_id
1214 AND p.post_approved <> t.topic_approved";
1215 $result = phpbb::$db->sql_query($sql);
1217 $topic_ids = array();
1218 while ($row = phpbb::$db->sql_fetchrow($result))
1220 $topic_ids[] = $row['topic_id'];
1222 phpbb::$db->sql_freeresult($result);
1224 if (!sizeof($topic_ids))
1226 return;
1229 $sql = 'UPDATE ' . TOPICS_TABLE . '
1230 SET topic_approved = 1 - topic_approved
1231 WHERE ' . phpbb::$db->sql_in_set('topic_id', $topic_ids);
1232 phpbb::$db->sql_query($sql);
1234 break;
1236 case 'post_reported':
1237 $post_ids = $post_reported = array();
1239 $sql = 'SELECT p.post_id, p.post_reported
1240 FROM ' . POSTS_TABLE . " p
1241 $where_sql
1242 GROUP BY p.post_id, p.post_reported";
1243 $result = phpbb::$db->sql_query($sql);
1245 while ($row = phpbb::$db->sql_fetchrow($result))
1247 $post_ids[$row['post_id']] = $row['post_id'];
1248 if ($row['post_reported'])
1250 $post_reported[$row['post_id']] = 1;
1253 phpbb::$db->sql_freeresult($result);
1255 $sql = 'SELECT DISTINCT(post_id)
1256 FROM ' . REPORTS_TABLE . '
1257 WHERE ' . phpbb::$db->sql_in_set('post_id', $post_ids) . '
1258 AND report_closed = 0';
1259 $result = phpbb::$db->sql_query($sql);
1261 $post_ids = array();
1262 while ($row = phpbb::$db->sql_fetchrow($result))
1264 if (!isset($post_reported[$row['post_id']]))
1266 $post_ids[] = $row['post_id'];
1268 else
1270 unset($post_reported[$row['post_id']]);
1273 phpbb::$db->sql_freeresult($result);
1275 // $post_reported should be empty by now, if it's not it contains
1276 // posts that are falsely flagged as reported
1277 foreach ($post_reported as $post_id => $void)
1279 $post_ids[] = $post_id;
1282 if (sizeof($post_ids))
1284 $sql = 'UPDATE ' . POSTS_TABLE . '
1285 SET post_reported = 1 - post_reported
1286 WHERE ' . phpbb::$db->sql_in_set('post_id', $post_ids);
1287 phpbb::$db->sql_query($sql);
1289 break;
1291 case 'topic_reported':
1292 if ($sync_extra)
1294 sync('post_reported', $where_type, $where_ids);
1297 $topic_ids = $topic_reported = array();
1299 $sql = 'SELECT DISTINCT(t.topic_id)
1300 FROM ' . POSTS_TABLE . " t
1301 $where_sql_and t.post_reported = 1";
1302 $result = phpbb::$db->sql_query($sql);
1304 while ($row = phpbb::$db->sql_fetchrow($result))
1306 $topic_reported[$row['topic_id']] = 1;
1308 phpbb::$db->sql_freeresult($result);
1310 $sql = 'SELECT t.topic_id, t.topic_reported
1311 FROM ' . TOPICS_TABLE . " t
1312 $where_sql";
1313 $result = phpbb::$db->sql_query($sql);
1315 while ($row = phpbb::$db->sql_fetchrow($result))
1317 if ($row['topic_reported'] ^ isset($topic_reported[$row['topic_id']]))
1319 $topic_ids[] = $row['topic_id'];
1322 phpbb::$db->sql_freeresult($result);
1324 if (sizeof($topic_ids))
1326 $sql = 'UPDATE ' . TOPICS_TABLE . '
1327 SET topic_reported = 1 - topic_reported
1328 WHERE ' . phpbb::$db->sql_in_set('topic_id', $topic_ids);
1329 phpbb::$db->sql_query($sql);
1331 break;
1333 case 'post_attachment':
1334 $post_ids = $post_attachment = array();
1336 $sql = 'SELECT p.post_id, p.post_attachment
1337 FROM ' . POSTS_TABLE . " p
1338 $where_sql
1339 GROUP BY p.post_id, p.post_attachment";
1340 $result = phpbb::$db->sql_query($sql);
1342 while ($row = phpbb::$db->sql_fetchrow($result))
1344 $post_ids[$row['post_id']] = $row['post_id'];
1345 if ($row['post_attachment'])
1347 $post_attachment[$row['post_id']] = 1;
1350 phpbb::$db->sql_freeresult($result);
1352 $sql = 'SELECT DISTINCT(post_msg_id)
1353 FROM ' . ATTACHMENTS_TABLE . '
1354 WHERE ' . phpbb::$db->sql_in_set('post_msg_id', $post_ids) . '
1355 AND in_message = 0';
1356 $result = phpbb::$db->sql_query($sql);
1358 $post_ids = array();
1359 while ($row = phpbb::$db->sql_fetchrow($result))
1361 if (!isset($post_attachment[$row['post_msg_id']]))
1363 $post_ids[] = $row['post_msg_id'];
1365 else
1367 unset($post_attachment[$row['post_msg_id']]);
1370 phpbb::$db->sql_freeresult($result);
1372 // $post_attachment should be empty by now, if it's not it contains
1373 // posts that are falsely flagged as having attachments
1374 foreach ($post_attachment as $post_id => $void)
1376 $post_ids[] = $post_id;
1379 if (sizeof($post_ids))
1381 $sql = 'UPDATE ' . POSTS_TABLE . '
1382 SET post_attachment = 1 - post_attachment
1383 WHERE ' . phpbb::$db->sql_in_set('post_id', $post_ids);
1384 phpbb::$db->sql_query($sql);
1386 break;
1388 case 'topic_attachment':
1389 if ($sync_extra)
1391 sync('post_attachment', $where_type, $where_ids);
1394 $topic_ids = $topic_attachment = array();
1396 $sql = 'SELECT DISTINCT(t.topic_id)
1397 FROM ' . POSTS_TABLE . " t
1398 $where_sql_and t.post_attachment = 1";
1399 $result = phpbb::$db->sql_query($sql);
1401 while ($row = phpbb::$db->sql_fetchrow($result))
1403 $topic_attachment[$row['topic_id']] = 1;
1405 phpbb::$db->sql_freeresult($result);
1407 $sql = 'SELECT t.topic_id, t.topic_attachment
1408 FROM ' . TOPICS_TABLE . " t
1409 $where_sql";
1410 $result = phpbb::$db->sql_query($sql);
1412 while ($row = phpbb::$db->sql_fetchrow($result))
1414 if ($row['topic_attachment'] ^ isset($topic_attachment[$row['topic_id']]))
1416 $topic_ids[] = $row['topic_id'];
1419 phpbb::$db->sql_freeresult($result);
1421 if (sizeof($topic_ids))
1423 $sql = 'UPDATE ' . TOPICS_TABLE . '
1424 SET topic_attachment = 1 - topic_attachment
1425 WHERE ' . phpbb::$db->sql_in_set('topic_id', $topic_ids);
1426 phpbb::$db->sql_query($sql);
1428 break;
1430 case 'forum':
1432 // 1: Get the list of all forums
1433 $sql = 'SELECT f.*
1434 FROM ' . FORUMS_TABLE . " f
1435 $where_sql";
1436 $result = phpbb::$db->sql_query($sql);
1438 $forum_data = $forum_ids = $post_ids = $last_post_id = $post_info = array();
1439 while ($row = phpbb::$db->sql_fetchrow($result))
1441 if ($row['forum_type'] == FORUM_LINK)
1443 continue;
1446 $forum_id = (int) $row['forum_id'];
1447 $forum_ids[$forum_id] = $forum_id;
1449 $forum_data[$forum_id] = $row;
1450 if ($sync_extra)
1452 $forum_data[$forum_id]['posts'] = 0;
1453 $forum_data[$forum_id]['topics'] = 0;
1454 $forum_data[$forum_id]['topics_real'] = 0;
1456 $forum_data[$forum_id]['last_post_id'] = 0;
1457 $forum_data[$forum_id]['last_post_subject'] = '';
1458 $forum_data[$forum_id]['last_post_time'] = 0;
1459 $forum_data[$forum_id]['last_poster_id'] = 0;
1460 $forum_data[$forum_id]['last_poster_name'] = '';
1461 $forum_data[$forum_id]['last_poster_colour'] = '';
1463 phpbb::$db->sql_freeresult($result);
1465 if (!sizeof($forum_ids))
1467 break;
1470 $forum_ids = array_values($forum_ids);
1472 // 2: Get topic counts for each forum (optional)
1473 if ($sync_extra)
1475 $sql = 'SELECT forum_id, topic_approved, COUNT(topic_id) AS forum_topics
1476 FROM ' . TOPICS_TABLE . '
1477 WHERE ' . phpbb::$db->sql_in_set('forum_id', $forum_ids) . '
1478 GROUP BY forum_id, topic_approved';
1479 $result = phpbb::$db->sql_query($sql);
1481 while ($row = phpbb::$db->sql_fetchrow($result))
1483 $forum_id = (int) $row['forum_id'];
1484 $forum_data[$forum_id]['topics_real'] += $row['forum_topics'];
1486 if ($row['topic_approved'])
1488 $forum_data[$forum_id]['topics'] = $row['forum_topics'];
1491 phpbb::$db->sql_freeresult($result);
1494 // 3: Get post count for each forum (optional)
1495 if ($sync_extra)
1497 if (sizeof($forum_ids) == 1)
1499 $sql = 'SELECT SUM(t.topic_replies + 1) AS forum_posts
1500 FROM ' . TOPICS_TABLE . ' t
1501 WHERE ' . phpbb::$db->sql_in_set('t.forum_id', $forum_ids) . '
1502 AND t.topic_approved = 1
1503 AND t.topic_status <> ' . ITEM_MOVED;
1505 else
1507 $sql = 'SELECT t.forum_id, SUM(t.topic_replies + 1) AS forum_posts
1508 FROM ' . TOPICS_TABLE . ' t
1509 WHERE ' . phpbb::$db->sql_in_set('t.forum_id', $forum_ids) . '
1510 AND t.topic_approved = 1
1511 AND t.topic_status <> ' . ITEM_MOVED . '
1512 GROUP BY t.forum_id';
1515 $result = phpbb::$db->sql_query($sql);
1517 while ($row = phpbb::$db->sql_fetchrow($result))
1519 $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
1521 $forum_data[$forum_id]['posts'] = (int) $row['forum_posts'];
1523 phpbb::$db->sql_freeresult($result);
1526 // 4: Get last_post_id for each forum
1527 if (sizeof($forum_ids) == 1)
1529 $sql = 'SELECT MAX(t.topic_last_post_id) as last_post_id
1530 FROM ' . TOPICS_TABLE . ' t
1531 WHERE ' . phpbb::$db->sql_in_set('t.forum_id', $forum_ids) . '
1532 AND t.topic_approved = 1';
1534 else
1536 $sql = 'SELECT t.forum_id, MAX(t.topic_last_post_id) as last_post_id
1537 FROM ' . TOPICS_TABLE . ' t
1538 WHERE ' . phpbb::$db->sql_in_set('t.forum_id', $forum_ids) . '
1539 AND t.topic_approved = 1
1540 GROUP BY t.forum_id';
1543 $result = phpbb::$db->sql_query($sql);
1545 while ($row = phpbb::$db->sql_fetchrow($result))
1547 $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
1549 $forum_data[$forum_id]['last_post_id'] = (int) $row['last_post_id'];
1551 $post_ids[] = $row['last_post_id'];
1553 phpbb::$db->sql_freeresult($result);
1555 // 5: Retrieve last_post infos
1556 if (sizeof($post_ids))
1558 $sql = 'SELECT p.post_id, p.poster_id, p.post_subject, p.post_time, p.post_username, u.username, u.user_colour
1559 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1560 WHERE ' . phpbb::$db->sql_in_set('p.post_id', $post_ids) . '
1561 AND p.poster_id = u.user_id';
1562 $result = phpbb::$db->sql_query($sql);
1564 while ($row = phpbb::$db->sql_fetchrow($result))
1566 $post_info[$row['post_id']] = $row;
1568 phpbb::$db->sql_freeresult($result);
1570 foreach ($forum_data as $forum_id => $data)
1572 if ($data['last_post_id'])
1574 if (isset($post_info[$data['last_post_id']]))
1576 $forum_data[$forum_id]['last_post_subject'] = $post_info[$data['last_post_id']]['post_subject'];
1577 $forum_data[$forum_id]['last_post_time'] = $post_info[$data['last_post_id']]['post_time'];
1578 $forum_data[$forum_id]['last_poster_id'] = $post_info[$data['last_post_id']]['poster_id'];
1579 $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'];
1580 $forum_data[$forum_id]['last_poster_colour'] = $post_info[$data['last_post_id']]['user_colour'];
1582 else
1584 // For some reason we did not find the post in the db
1585 $forum_data[$forum_id]['last_post_id'] = 0;
1586 $forum_data[$forum_id]['last_post_subject'] = '';
1587 $forum_data[$forum_id]['last_post_time'] = 0;
1588 $forum_data[$forum_id]['last_poster_id'] = 0;
1589 $forum_data[$forum_id]['last_poster_name'] = '';
1590 $forum_data[$forum_id]['last_poster_colour'] = '';
1594 unset($post_info);
1597 // 6: Now do that thing
1598 $fieldnames = array('last_post_id', 'last_post_subject', 'last_post_time', 'last_poster_id', 'last_poster_name', 'last_poster_colour');
1600 if ($sync_extra)
1602 array_push($fieldnames, 'posts', 'topics', 'topics_real');
1605 foreach ($forum_data as $forum_id => $row)
1607 $sql_ary = array();
1609 foreach ($fieldnames as $fieldname)
1611 if ($row['forum_' . $fieldname] != $row[$fieldname])
1613 if (preg_match('#(name|colour|subject)$#', $fieldname))
1615 $sql_ary['forum_' . $fieldname] = (string) $row[$fieldname];
1617 else
1619 $sql_ary['forum_' . $fieldname] = (int) $row[$fieldname];
1624 if (sizeof($sql_ary))
1626 $sql = 'UPDATE ' . FORUMS_TABLE . '
1627 SET ' . phpbb::$db->sql_build_array('UPDATE', $sql_ary) . '
1628 WHERE forum_id = ' . $forum_id;
1629 phpbb::$db->sql_query($sql);
1632 break;
1634 case 'topic':
1635 $topic_data = $post_ids = $approved_unapproved_ids = $resync_forums = $delete_topics = $delete_posts = $moved_topics = array();
1637 $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
1638 FROM ' . TOPICS_TABLE . " t
1639 $where_sql";
1640 $result = phpbb::$db->sql_query($sql);
1642 while ($row = phpbb::$db->sql_fetchrow($result))
1644 if ($row['topic_moved_id'])
1646 $moved_topics[] = $row['topic_id'];
1647 continue;
1650 $topic_id = (int) $row['topic_id'];
1651 $topic_data[$topic_id] = $row;
1652 $topic_data[$topic_id]['replies_real'] = -1;
1653 $topic_data[$topic_id]['replies'] = 0;
1654 $topic_data[$topic_id]['first_post_id'] = 0;
1655 $topic_data[$topic_id]['last_post_id'] = 0;
1656 unset($topic_data[$topic_id]['topic_id']);
1658 // This array holds all topic_ids
1659 $delete_topics[$topic_id] = '';
1661 if ($sync_extra)
1663 $topic_data[$topic_id]['reported'] = 0;
1664 $topic_data[$topic_id]['attachment'] = 0;
1667 phpbb::$db->sql_freeresult($result);
1669 // Use "t" as table alias because of the $where_sql clause
1670 // NOTE: 't.post_approved' in the GROUP BY is causing a major slowdown.
1671 $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
1672 FROM ' . POSTS_TABLE . " t
1673 $where_sql
1674 GROUP BY t.topic_id, t.post_approved";
1675 $result = phpbb::$db->sql_query($sql);
1677 while ($row = phpbb::$db->sql_fetchrow($result))
1679 $topic_id = (int) $row['topic_id'];
1681 $row['first_post_id'] = (int) $row['first_post_id'];
1682 $row['last_post_id'] = (int) $row['last_post_id'];
1684 if (!isset($topic_data[$topic_id]))
1686 // Hey, these posts come from a topic that does not exist
1687 $delete_posts[$topic_id] = '';
1689 else
1691 // Unset the corresponding entry in $delete_topics
1692 // When we'll be done, only topics with no posts will remain
1693 unset($delete_topics[$topic_id]);
1695 $topic_data[$topic_id]['replies_real'] += $row['total_posts'];
1696 $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']);
1698 if ($row['post_approved'] || !$topic_data[$topic_id]['last_post_id'])
1700 $topic_data[$topic_id]['replies'] = $row['total_posts'] - 1;
1701 $topic_data[$topic_id]['last_post_id'] = $row['last_post_id'];
1705 phpbb::$db->sql_freeresult($result);
1707 foreach ($topic_data as $topic_id => $row)
1709 $post_ids[] = $row['first_post_id'];
1710 if ($row['first_post_id'] != $row['last_post_id'])
1712 $post_ids[] = $row['last_post_id'];
1716 // Now we delete empty topics and orphan posts
1717 if (sizeof($delete_posts))
1719 delete_posts('topic_id', array_keys($delete_posts), false);
1720 unset($delete_posts);
1723 if (!sizeof($topic_data))
1725 // If we get there, topic ids were invalid or topics did not contain any posts
1726 delete_topics($where_type, $where_ids, true);
1727 return;
1730 if (sizeof($delete_topics))
1732 $delete_topic_ids = array();
1733 foreach ($delete_topics as $topic_id => $void)
1735 unset($topic_data[$topic_id]);
1736 $delete_topic_ids[] = $topic_id;
1739 delete_topics('topic_id', $delete_topic_ids, false);
1740 unset($delete_topics, $delete_topic_ids);
1743 $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
1744 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1745 WHERE ' . phpbb::$db->sql_in_set('p.post_id', $post_ids) . '
1746 AND u.user_id = p.poster_id';
1747 $result = phpbb::$db->sql_query($sql);
1749 $post_ids = array();
1750 while ($row = phpbb::$db->sql_fetchrow($result))
1752 $topic_id = intval($row['topic_id']);
1754 if ($row['post_id'] == $topic_data[$topic_id]['first_post_id'])
1756 if ($topic_data[$topic_id]['topic_approved'] != $row['post_approved'])
1758 $approved_unapproved_ids[] = $topic_id;
1760 $topic_data[$topic_id]['time'] = $row['post_time'];
1761 $topic_data[$topic_id]['poster'] = $row['poster_id'];
1762 $topic_data[$topic_id]['first_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
1763 $topic_data[$topic_id]['first_poster_colour'] = $row['user_colour'];
1766 if ($row['post_id'] == $topic_data[$topic_id]['last_post_id'])
1768 $topic_data[$topic_id]['last_poster_id'] = $row['poster_id'];
1769 $topic_data[$topic_id]['last_post_subject'] = $row['post_subject'];
1770 $topic_data[$topic_id]['last_post_time'] = $row['post_time'];
1771 $topic_data[$topic_id]['last_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
1772 $topic_data[$topic_id]['last_poster_colour'] = $row['user_colour'];
1775 phpbb::$db->sql_freeresult($result);
1777 // Make sure shadow topics do link to existing topics
1778 if (sizeof($moved_topics))
1780 $delete_topics = array();
1782 $sql = 'SELECT t1.topic_id, t1.topic_moved_id
1783 FROM ' . TOPICS_TABLE . ' t1
1784 LEFT JOIN ' . TOPICS_TABLE . ' t2 ON (t2.topic_id = t1.topic_moved_id)
1785 WHERE ' . phpbb::$db->sql_in_set('t1.topic_id', $moved_topics) . '
1786 AND t2.topic_id IS NULL';
1787 $result = phpbb::$db->sql_query($sql);
1789 while ($row = phpbb::$db->sql_fetchrow($result))
1791 $delete_topics[] = $row['topic_id'];
1793 phpbb::$db->sql_freeresult($result);
1795 if (sizeof($delete_topics))
1797 delete_topics('topic_id', $delete_topics, false);
1799 unset($delete_topics);
1801 // Make sure shadow topics having no last post data being updated (this only rarely happens...)
1802 $sql = 'SELECT topic_id, topic_moved_id, topic_last_post_id, topic_first_post_id
1803 FROM ' . TOPICS_TABLE . '
1804 WHERE ' . phpbb::$db->sql_in_set('topic_id', $moved_topics) . '
1805 AND topic_last_post_time = 0';
1806 $result = phpbb::$db->sql_query($sql);
1808 $shadow_topic_data = $post_ids = array();
1809 while ($row = phpbb::$db->sql_fetchrow($result))
1811 $shadow_topic_data[$row['topic_moved_id']] = $row;
1812 $post_ids[] = $row['topic_last_post_id'];
1813 $post_ids[] = $row['topic_first_post_id'];
1815 phpbb::$db->sql_freeresult($result);
1817 $sync_shadow_topics = array();
1818 if (sizeof($post_ids))
1820 $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
1821 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1822 WHERE ' . phpbb::$db->sql_in_set('p.post_id', $post_ids) . '
1823 AND u.user_id = p.poster_id';
1824 $result = phpbb::$db->sql_query($sql);
1826 $post_ids = array();
1827 while ($row = phpbb::$db->sql_fetchrow($result))
1829 $topic_id = (int) $row['topic_id'];
1831 // Ok, there should be a shadow topic. If there isn't, then there's something wrong with the db.
1832 // However, there's not much we can do about it.
1833 if (!empty($shadow_topic_data[$topic_id]))
1835 if ($row['post_id'] == $shadow_topic_data[$topic_id]['topic_first_post_id'])
1837 $orig_topic_id = $shadow_topic_data[$topic_id]['topic_id'];
1839 if (!isset($sync_shadow_topics[$orig_topic_id]))
1841 $sync_shadow_topics[$orig_topic_id] = array();
1844 $sync_shadow_topics[$orig_topic_id]['topic_time'] = $row['post_time'];
1845 $sync_shadow_topics[$orig_topic_id]['topic_poster'] = $row['poster_id'];
1846 $sync_shadow_topics[$orig_topic_id]['topic_first_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
1847 $sync_shadow_topics[$orig_topic_id]['topic_first_poster_colour'] = $row['user_colour'];
1850 if ($row['post_id'] == $shadow_topic_data[$topic_id]['topic_last_post_id'])
1852 $orig_topic_id = $shadow_topic_data[$topic_id]['topic_id'];
1854 if (!isset($sync_shadow_topics[$orig_topic_id]))
1856 $sync_shadow_topics[$orig_topic_id] = array();
1859 $sync_shadow_topics[$orig_topic_id]['topic_last_poster_id'] = $row['poster_id'];
1860 $sync_shadow_topics[$orig_topic_id]['topic_last_post_subject'] = $row['post_subject'];
1861 $sync_shadow_topics[$orig_topic_id]['topic_last_post_time'] = $row['post_time'];
1862 $sync_shadow_topics[$orig_topic_id]['topic_last_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
1863 $sync_shadow_topics[$orig_topic_id]['topic_last_poster_colour'] = $row['user_colour'];
1867 phpbb::$db->sql_freeresult($result);
1869 $shadow_topic_data = array();
1871 // Update the information we collected
1872 if (sizeof($sync_shadow_topics))
1874 foreach ($sync_shadow_topics as $sync_topic_id => $sql_ary)
1876 $sql = 'UPDATE ' . TOPICS_TABLE . '
1877 SET ' . phpbb::$db->sql_build_array('UPDATE', $sql_ary) . '
1878 WHERE topic_id = ' . $sync_topic_id;
1879 phpbb::$db->sql_query($sql);
1884 unset($sync_shadow_topics, $shadow_topic_data);
1887 // approved becomes unapproved, and vice-versa
1888 if (sizeof($approved_unapproved_ids))
1890 $sql = 'UPDATE ' . TOPICS_TABLE . '
1891 SET topic_approved = 1 - topic_approved
1892 WHERE ' . phpbb::$db->sql_in_set('topic_id', $approved_unapproved_ids);
1893 phpbb::$db->sql_query($sql);
1895 unset($approved_unapproved_ids);
1897 // These are fields that will be synchronised
1898 $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');
1900 if ($sync_extra)
1902 // This routine assumes that post_reported values are correct
1903 // if they are not, use sync('post_reported') first
1904 $sql = 'SELECT t.topic_id, p.post_id
1905 FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
1906 $where_sql_and p.topic_id = t.topic_id
1907 AND p.post_reported = 1
1908 GROUP BY t.topic_id, p.post_id";
1909 $result = phpbb::$db->sql_query($sql);
1911 $fieldnames[] = 'reported';
1912 while ($row = phpbb::$db->sql_fetchrow($result))
1914 $topic_data[intval($row['topic_id'])]['reported'] = 1;
1916 phpbb::$db->sql_freeresult($result);
1918 // This routine assumes that post_attachment values are correct
1919 // if they are not, use sync('post_attachment') first
1920 $sql = 'SELECT t.topic_id, p.post_id
1921 FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
1922 $where_sql_and p.topic_id = t.topic_id
1923 AND p.post_attachment = 1
1924 GROUP BY t.topic_id, p.post_id";
1925 $result = phpbb::$db->sql_query($sql);
1927 $fieldnames[] = 'attachment';
1928 while ($row = phpbb::$db->sql_fetchrow($result))
1930 $topic_data[intval($row['topic_id'])]['attachment'] = 1;
1932 phpbb::$db->sql_freeresult($result);
1935 foreach ($topic_data as $topic_id => $row)
1937 $sql_ary = array();
1939 foreach ($fieldnames as $fieldname)
1941 if (isset($row[$fieldname]) && isset($row['topic_' . $fieldname]) && $row['topic_' . $fieldname] != $row[$fieldname])
1943 $sql_ary['topic_' . $fieldname] = $row[$fieldname];
1947 if (sizeof($sql_ary))
1949 $sql = 'UPDATE ' . TOPICS_TABLE . '
1950 SET ' . phpbb::$db->sql_build_array('UPDATE', $sql_ary) . '
1951 WHERE topic_id = ' . $topic_id;
1952 phpbb::$db->sql_query($sql);
1954 $resync_forums[$row['forum_id']] = $row['forum_id'];
1957 unset($topic_data);
1959 // if some topics have been resync'ed then resync parent forums
1960 // except when we're only syncing a range, we don't want to sync forums during
1961 // batch processing.
1962 if ($resync_parents && sizeof($resync_forums) && $where_type != 'range')
1964 sync('forum', 'forum_id', array_values($resync_forums), true, true);
1966 break;
1969 return;
1973 * Prune function
1975 function prune($forum_id, $prune_mode, $prune_date, $prune_flags = 0, $auto_sync = true)
1977 if (!is_array($forum_id))
1979 $forum_id = array($forum_id);
1982 if (!sizeof($forum_id))
1984 return;
1987 $sql_and = '';
1989 if (!($prune_flags & FORUM_FLAG_PRUNE_ANNOUNCE))
1991 $sql_and .= ' AND topic_type <> ' . POST_ANNOUNCE;
1994 if (!($prune_flags & FORUM_FLAG_PRUNE_STICKY))
1996 $sql_and .= ' AND topic_type <> ' . POST_STICKY;
1999 if ($prune_mode == 'posted')
2001 $sql_and .= " AND topic_last_post_time < $prune_date";
2004 if ($prune_mode == 'viewed')
2006 $sql_and .= " AND topic_last_view_time < $prune_date";
2009 $sql = 'SELECT topic_id
2010 FROM ' . TOPICS_TABLE . '
2011 WHERE ' . phpbb::$db->sql_in_set('forum_id', $forum_id) . "
2012 AND poll_start = 0
2013 $sql_and";
2014 $result = phpbb::$db->sql_query($sql);
2016 $topic_list = array();
2017 while ($row = phpbb::$db->sql_fetchrow($result))
2019 $topic_list[] = $row['topic_id'];
2021 phpbb::$db->sql_freeresult($result);
2023 if ($prune_flags & FORUM_FLAG_PRUNE_POLL)
2025 $sql = 'SELECT topic_id
2026 FROM ' . TOPICS_TABLE . '
2027 WHERE ' . phpbb::$db->sql_in_set('forum_id', $forum_id) . "
2028 AND poll_start > 0
2029 AND poll_last_vote < $prune_date
2030 $sql_and";
2031 $result = phpbb::$db->sql_query($sql);
2033 while ($row = phpbb::$db->sql_fetchrow($result))
2035 $topic_list[] = $row['topic_id'];
2037 phpbb::$db->sql_freeresult($result);
2039 $topic_list = array_unique($topic_list);
2042 return delete_topics('topic_id', $topic_list, $auto_sync, false);
2046 * Function auto_prune(), this function now relies on passed vars
2048 function auto_prune($forum_id, $prune_mode, $prune_flags, $prune_days, $prune_freq)
2050 $sql = 'SELECT forum_name
2051 FROM ' . FORUMS_TABLE . "
2052 WHERE forum_id = $forum_id";
2053 $result = phpbb::$db->sql_query($sql, 3600);
2054 $row = phpbb::$db->sql_fetchrow($result);
2055 phpbb::$db->sql_freeresult($result);
2057 if ($row)
2059 $prune_date = time() - ($prune_days * 86400);
2060 $next_prune = time() + ($prune_freq * 86400);
2062 prune($forum_id, $prune_mode, $prune_date, $prune_flags, true);
2064 $sql = 'UPDATE ' . FORUMS_TABLE . "
2065 SET prune_next = $next_prune
2066 WHERE forum_id = $forum_id";
2067 phpbb::$db->sql_query($sql);
2069 add_log('admin', 'LOG_AUTO_PRUNE', $row['forum_name']);
2072 return;
2076 * remove_comments will strip the sql comment lines out of an uploaded sql file
2077 * specifically for mssql and postgres type files in the install....
2079 function remove_comments(&$output)
2081 $lines = explode("\n", $output);
2082 $output = '';
2084 // try to keep mem. use down
2085 $linecount = sizeof($lines);
2087 $in_comment = false;
2088 for ($i = 0; $i < $linecount; $i++)
2090 if (trim($lines[$i]) == '/*')
2092 $in_comment = true;
2095 if (!$in_comment)
2097 $output .= $lines[$i] . "\n";
2100 if (trim($lines[$i]) == '*/')
2102 $in_comment = false;
2106 unset($lines);
2107 return $output;
2111 * Cache moderators, called whenever permissions are changed via admin_permissions. Changes of username
2112 * and group names must be carried through for the moderators table
2114 function cache_moderators()
2116 // Remove cached sql results
2117 phpbb::$acm->destroy_sql(MODERATOR_CACHE_TABLE);
2119 // Clear table
2120 if (phpbb::$db->features['truncate'])
2122 phpbb::$db->sql_query('TRUNCATE TABLE ' . MODERATOR_CACHE_TABLE);
2124 else
2126 phpbb::$db->sql_query('DELETE FROM ' . MODERATOR_CACHE_TABLE);
2129 // We add moderators who have forum moderator permissions without an explicit ACL_NEVER setting
2130 $hold_ary = $ug_id_ary = $sql_ary = array();
2132 // Grab all users having moderative options...
2133 $hold_ary = phpbb::$acl->acl_user_raw_data(false, 'm_%', false);
2135 // Add users?
2136 if (sizeof($hold_ary))
2138 // At least one moderative option warrants a display
2139 $ug_id_ary = array_keys($hold_ary);
2141 // Remove users who have group memberships with DENY moderator permissions
2142 $sql = phpbb::$db->sql_build_query('SELECT', array(
2143 'SELECT' => 'a.forum_id, ug.user_id',
2145 'FROM' => array(
2146 ACL_OPTIONS_TABLE => 'o',
2147 USER_GROUP_TABLE => 'ug',
2148 ACL_GROUPS_TABLE => 'a'
2151 'LEFT_JOIN' => array(
2152 array(
2153 'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
2154 'ON' => 'a.auth_role_id = r.role_id'
2158 'WHERE' => '(o.auth_option_id = a.auth_option_id OR o.auth_option_id = r.auth_option_id)
2159 AND ((a.auth_setting = ' . phpbb::ACL_NEVER . ' AND r.auth_setting IS NULL)
2160 OR r.auth_setting = ' . phpbb::ACL_NEVER . ')
2161 AND a.group_id = ug.group_id
2162 AND ' . phpbb::$db->sql_in_set('ug.user_id', $ug_id_ary) . "
2163 AND ug.user_pending = 0
2164 AND o.auth_option " . phpbb::$db->sql_like_expression('m_' . phpbb::$db->any_char),
2166 $result = phpbb::$db->sql_query($sql);
2168 while ($row = phpbb::$db->sql_fetchrow($result))
2170 if (isset($hold_ary[$row['user_id']][$row['forum_id']]))
2172 unset($hold_ary[$row['user_id']][$row['forum_id']]);
2175 phpbb::$db->sql_freeresult($result);
2177 if (sizeof($hold_ary))
2179 // Get usernames...
2180 $sql = 'SELECT user_id, username
2181 FROM ' . USERS_TABLE . '
2182 WHERE ' . phpbb::$db->sql_in_set('user_id', array_keys($hold_ary));
2183 $result = phpbb::$db->sql_query($sql);
2185 $usernames_ary = array();
2186 while ($row = phpbb::$db->sql_fetchrow($result))
2188 $usernames_ary[$row['user_id']] = $row['username'];
2191 foreach ($hold_ary as $user_id => $forum_id_ary)
2193 // Do not continue if user does not exist
2194 if (!isset($usernames_ary[$user_id]))
2196 continue;
2199 foreach ($forum_id_ary as $forum_id => $auth_ary)
2201 $sql_ary[] = array(
2202 'forum_id' => (int) $forum_id,
2203 'user_id' => (int) $user_id,
2204 'username' => (string) $usernames_ary[$user_id],
2205 'group_id' => 0,
2206 'group_name' => ''
2213 // Now to the groups...
2214 $hold_ary = phpbb::$acl->acl_group_raw_data(false, 'm_%', false);
2216 if (sizeof($hold_ary))
2218 $ug_id_ary = array_keys($hold_ary);
2220 // Make sure not hidden or special groups are involved...
2221 $sql = 'SELECT group_name, group_id, group_type
2222 FROM ' . GROUPS_TABLE . '
2223 WHERE ' . phpbb::$db->sql_in_set('group_id', $ug_id_ary);
2224 $result = phpbb::$db->sql_query($sql);
2226 $groupnames_ary = array();
2227 while ($row = phpbb::$db->sql_fetchrow($result))
2229 if ($row['group_type'] == GROUP_HIDDEN || $row['group_type'] == GROUP_SPECIAL)
2231 unset($hold_ary[$row['group_id']]);
2234 $groupnames_ary[$row['group_id']] = $row['group_name'];
2236 phpbb::$db->sql_freeresult($result);
2238 foreach ($hold_ary as $group_id => $forum_id_ary)
2240 // If there is no group, we do not assign it...
2241 if (!isset($groupnames_ary[$group_id]))
2243 continue;
2246 foreach ($forum_id_ary as $forum_id => $auth_ary)
2248 $flag = false;
2249 foreach ($auth_ary as $auth_option => $setting)
2251 // Make sure at least one ACL_YES option is set...
2252 if ($setting == phpbb::ACL_YES)
2254 $flag = true;
2255 break;
2259 if (!$flag)
2261 continue;
2264 $sql_ary[] = array(
2265 'forum_id' => (int) $forum_id,
2266 'user_id' => 0,
2267 'username' => '',
2268 'group_id' => (int) $group_id,
2269 'group_name' => (string) $groupnames_ary[$group_id]
2275 phpbb::$db->sql_multi_insert(MODERATOR_CACHE_TABLE, $sql_ary);
2279 * View log
2281 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')
2283 $topic_id_list = $reportee_id_list = $is_auth = $is_mod = array();
2285 $profile_url = (defined('IN_ADMIN')) ? phpbb::$url->append_sid(PHPBB_ADMIN_PATH . 'index.' . PHP_EXT, 'i=users&amp;mode=overview') : phpbb::$url->append_sid('memberlist', 'mode=viewprofile');
2287 switch ($mode)
2289 case 'admin':
2290 $log_type = LOG_ADMIN;
2291 $sql_forum = '';
2292 break;
2294 case 'mod':
2295 $log_type = LOG_MOD;
2297 if ($topic_id)
2299 $sql_forum = 'AND l.topic_id = ' . intval($topic_id);
2301 else if (is_array($forum_id))
2303 $sql_forum = 'AND ' . phpbb::$db->sql_in_set('l.forum_id', array_map('intval', $forum_id));
2305 else
2307 $sql_forum = ($forum_id) ? 'AND l.forum_id = ' . intval($forum_id) : '';
2309 break;
2311 case 'user':
2312 $log_type = LOG_USERS;
2313 $sql_forum = 'AND l.reportee_id = ' . (int) $user_id;
2314 break;
2316 case 'users':
2317 $log_type = LOG_USERS;
2318 $sql_forum = '';
2319 break;
2321 case 'critical':
2322 $log_type = LOG_CRITICAL;
2323 $sql_forum = '';
2324 break;
2326 default:
2327 return;
2330 $sql = "SELECT l.*, u.username, u.username_clean, u.user_colour
2331 FROM " . LOG_TABLE . " l, " . USERS_TABLE . " u
2332 WHERE l.log_type = $log_type
2333 AND u.user_id = l.user_id
2334 " . (($limit_days) ? "AND l.log_time >= $limit_days" : '') . "
2335 $sql_forum
2336 ORDER BY $sort_by";
2337 $result = phpbb::$db->sql_query_limit($sql, $limit, $offset);
2339 $i = 0;
2340 $log = array();
2341 while ($row = phpbb::$db->sql_fetchrow($result))
2343 if ($row['topic_id'])
2345 $topic_id_list[] = $row['topic_id'];
2348 if ($row['reportee_id'])
2350 $reportee_id_list[] = $row['reportee_id'];
2353 $log[$i] = array(
2354 'id' => $row['log_id'],
2356 'reportee_id' => $row['reportee_id'],
2357 'reportee_username' => '',
2358 'reportee_username_full'=> '',
2360 'user_id' => $row['user_id'],
2361 'username' => $row['username'],
2362 'username_full' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], false, $profile_url),
2364 'ip' => $row['log_ip'],
2365 'time' => $row['log_time'],
2366 'forum_id' => $row['forum_id'],
2367 'topic_id' => $row['topic_id'],
2369 'viewforum' => ($row['forum_id'] && phpbb::$acl->acl_get('f_read', $row['forum_id'])) ? phpbb::$url->append_sid('viewforum', 'f=' . $row['forum_id']) : false,
2370 'action' => (isset(phpbb::$user->lang[$row['log_operation']])) ? phpbb::$user->lang[$row['log_operation']] : '{' . ucfirst(str_replace('_', ' ', $row['log_operation'])) . '}',
2373 if (!empty($row['log_data']))
2375 $log_data_ary = unserialize($row['log_data']);
2377 if (isset(phpbb::$user->lang[$row['log_operation']]))
2379 // Check if there are more occurrences of % than arguments, if there are we fill out the arguments array
2380 // It doesn't matter if we add more arguments than placeholders
2381 if ((substr_count($log[$i]['action'], '%') - sizeof($log_data_ary)) > 0)
2383 $log_data_ary = array_merge($log_data_ary, array_fill(0, substr_count($log[$i]['action'], '%') - sizeof($log_data_ary), ''));
2386 $log[$i]['action'] = vsprintf($log[$i]['action'], $log_data_ary);
2388 // If within the admin panel we do not censor text out
2389 if (defined('IN_ADMIN'))
2391 $log[$i]['action'] = bbcode_nl2br($log[$i]['action']);
2393 else
2395 $log[$i]['action'] = bbcode_nl2br(censor_text($log[$i]['action']));
2398 else
2400 $log[$i]['action'] .= '<br />' . implode('', $log_data_ary);
2403 /* Apply make_clickable... has to be seen if it is for good. :/
2404 // Seems to be not for the moment, reconsider later...
2405 $log[$i]['action'] = make_clickable($log[$i]['action']);
2409 $i++;
2411 phpbb::$db->sql_freeresult($result);
2413 if (sizeof($topic_id_list))
2415 $topic_id_list = array_unique($topic_id_list);
2417 // This query is not really needed if move_topics() updates the forum_id field,
2418 // although it's also used to determine if the topic still exists in the database
2419 $sql = 'SELECT topic_id, forum_id
2420 FROM ' . TOPICS_TABLE . '
2421 WHERE ' . phpbb::$db->sql_in_set('topic_id', array_map('intval', $topic_id_list));
2422 $result = phpbb::$db->sql_query($sql);
2424 $default_forum_id = 0;
2426 while ($row = phpbb::$db->sql_fetchrow($result))
2428 if (!$row['forum_id'])
2430 if (phpbb::$acl->acl_getf_global('f_read'))
2432 if (!$default_forum_id)
2434 $sql = 'SELECT forum_id
2435 FROM ' . FORUMS_TABLE . '
2436 WHERE forum_type = ' . FORUM_POST;
2437 $f_result = phpbb::$db->sql_query_limit($sql, 1);
2438 $default_forum_id = (int) phpbb::$db->sql_fetchfield('forum_id', $f_result);
2439 phpbb::$db->sql_freeresult($f_result);
2442 $is_auth[$row['topic_id']] = $default_forum_id;
2445 else
2447 if (phpbb::$acl->acl_get('f_read', $row['forum_id']))
2449 $is_auth[$row['topic_id']] = $row['forum_id'];
2453 if (phpbb::$acl->acl_gets('a_', 'm_', $row['forum_id']))
2455 $is_mod[$row['topic_id']] = $row['forum_id'];
2458 phpbb::$db->sql_freeresult($result);
2460 foreach ($log as $key => $row)
2462 $log[$key]['viewtopic'] = (isset($is_auth[$row['topic_id']])) ? phpbb::$url->append_sid('viewtopic', 'f=' . $is_auth[$row['topic_id']] . '&amp;t=' . $row['topic_id']) : false;
2463 $log[$key]['viewlogs'] = (isset($is_mod[$row['topic_id']])) ? phpbb::$url->append_sid('mcp', 'i=logs&amp;mode=topic_logs&amp;t=' . $row['topic_id'], true, phpbb::$user->session_id) : false;
2467 if (sizeof($reportee_id_list))
2469 $reportee_id_list = array_unique($reportee_id_list);
2470 $reportee_names_list = array();
2472 $sql = 'SELECT user_id, username, user_colour
2473 FROM ' . USERS_TABLE . '
2474 WHERE ' . phpbb::$db->sql_in_set('user_id', $reportee_id_list);
2475 $result = phpbb::$db->sql_query($sql);
2477 while ($row = phpbb::$db->sql_fetchrow($result))
2479 $reportee_names_list[$row['user_id']] = $row;
2481 phpbb::$db->sql_freeresult($result);
2483 foreach ($log as $key => $row)
2485 if (!isset($reportee_names_list[$row['reportee_id']]))
2487 continue;
2490 $log[$key]['reportee_username'] = $reportee_names_list[$row['reportee_id']]['username'];
2491 $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);
2495 $sql = 'SELECT COUNT(l.log_id) AS total_entries
2496 FROM ' . LOG_TABLE . " l
2497 WHERE l.log_type = $log_type
2498 AND l.log_time >= $limit_days
2499 $sql_forum";
2500 $result = phpbb::$db->sql_query($sql);
2501 $log_count = (int) phpbb::$db->sql_fetchfield('total_entries');
2502 phpbb::$db->sql_freeresult($result);
2504 return;
2508 * Update foes - remove moderators and administrators from foe lists...
2510 function update_foes($group_id = false, $user_id = false)
2512 // update foes for some user
2513 if (is_array($user_id) && sizeof($user_id))
2515 $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
2516 WHERE ' . phpbb::$db->sql_in_set('zebra_id', $user_id) . '
2517 AND foe = 1';
2518 phpbb::$db->sql_query($sql);
2519 return;
2522 // update foes for some group
2523 if (is_array($group_id) && sizeof($group_id))
2525 // Grab group settings...
2526 $sql = phpbb::$db->sql_build_query('SELECT', array(
2527 'SELECT' => 'a.group_id',
2529 'FROM' => array(
2530 ACL_OPTIONS_TABLE => 'ao',
2531 ACL_GROUPS_TABLE => 'a'
2534 'LEFT_JOIN' => array(
2535 array(
2536 'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
2537 'ON' => 'a.auth_role_id = r.role_id'
2541 'WHERE' => '(ao.auth_option_id = a.auth_option_id OR ao.auth_option_id = r.auth_option_id)
2542 AND ' . phpbb::$db->sql_in_set('a.group_id', $group_id) . "
2543 AND ao.auth_option IN ('a_', 'm_')",
2545 'GROUP_BY' => 'a.group_id'
2547 $result = phpbb::$db->sql_query($sql);
2549 $groups = array();
2550 while ($row = phpbb::$db->sql_fetchrow($result))
2552 $groups[] = (int) $row['group_id'];
2554 phpbb::$db->sql_freeresult($result);
2556 if (!sizeof($groups))
2558 return;
2561 if (phpbb::$db->features['multi_table_deletion'])
2563 $sql = 'DELETE FROM' . ZEBRA_TABLE . ' z
2564 USING ' . USER_GROUP_TABLE . ' ug
2565 WHERE z.zebra_id = ug.user_id
2566 AND z.foe = 1
2567 AND ' . phpbb::$db->sql_in_set('ug.group_id', $groups);
2568 phpbb::$db->sql_query($sql);
2570 else
2572 $sql = 'SELECT user_id
2573 FROM ' . USER_GROUP_TABLE . '
2574 WHERE ' . phpbb::$db->sql_in_set('group_id', $groups);
2575 $result = phpbb::$db->sql_query($sql);
2577 $users = array();
2578 while ($row = phpbb::$db->sql_fetchrow($result))
2580 $users[] = (int) $row['user_id'];
2582 phpbb::$db->sql_freeresult($result);
2584 if (sizeof($users))
2586 $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
2587 WHERE ' . phpbb::$db->sql_in_set('zebra_id', $users) . '
2588 AND foe = 1';
2589 phpbb::$db->sql_query($sql);
2593 return;
2596 // update foes for everyone
2597 $perms = array();
2598 foreach (phpbb::$acl->acl_get_list(false, array('a_', 'm_'), false) as $forum_id => $forum_ary)
2600 foreach ($forum_ary as $auth_option => $user_ary)
2602 $perms = array_merge($perms, $user_ary);
2606 if (sizeof($perms))
2608 $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
2609 WHERE ' . phpbb::$db->sql_in_set('zebra_id', array_unique($perms)) . '
2610 AND foe = 1';
2611 phpbb::$db->sql_query($sql);
2613 unset($perms);
2617 * Lists inactive users
2619 function view_inactive_users(&$users, &$user_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'user_inactive_time DESC')
2621 $sql = 'SELECT COUNT(user_id) AS user_count
2622 FROM ' . USERS_TABLE . '
2623 WHERE user_type = ' . phpbb::USER_INACTIVE .
2624 (($limit_days) ? " AND user_inactive_time >= $limit_days" : '');
2625 $result = phpbb::$db->sql_query($sql);
2626 $user_count = (int) phpbb::$db->sql_fetchfield('user_count');
2627 phpbb::$db->sql_freeresult($result);
2629 if ($offset >= $user_count)
2631 $offset = ($offset - $limit < 0) ? 0 : $offset - $limit;
2634 $sql = 'SELECT user_id, username, user_regdate, user_lastvisit, user_inactive_time, user_inactive_reason
2635 FROM ' . USERS_TABLE . '
2636 WHERE user_type = ' . phpbb::USER_INACTIVE .
2637 (($limit_days) ? " AND user_inactive_time >= $limit_days" : '') . "
2638 ORDER BY $sort_by";
2639 $result = phpbb::$db->sql_query_limit($sql, $limit, $offset);
2641 while ($row = phpbb::$db->sql_fetchrow($result))
2643 $row['inactive_reason'] = phpbb::$user->lang['INACTIVE_REASON_UNKNOWN'];
2644 switch ($row['user_inactive_reason'])
2646 case INACTIVE_REGISTER:
2647 $row['inactive_reason'] = phpbb::$user->lang['INACTIVE_REASON_REGISTER'];
2648 break;
2650 case INACTIVE_PROFILE:
2651 $row['inactive_reason'] = phpbb::$user->lang['INACTIVE_REASON_PROFILE'];
2652 break;
2654 case INACTIVE_MANUAL:
2655 $row['inactive_reason'] = phpbb::$user->lang['INACTIVE_REASON_MANUAL'];
2656 break;
2658 case INACTIVE_REMIND:
2659 $row['inactive_reason'] = phpbb::$user->lang['INACTIVE_REASON_REMIND'];
2660 break;
2663 $users[] = $row;
2666 return $offset;
2670 * Lists warned users
2672 function view_warned_users(&$users, &$user_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'user_warnings DESC')
2674 $sql = 'SELECT user_id, username, user_colour, user_warnings, user_last_warning
2675 FROM ' . USERS_TABLE . '
2676 WHERE user_warnings > 0
2677 ' . (($limit_days) ? "AND user_last_warning >= $limit_days" : '') . "
2678 ORDER BY $sort_by";
2679 $result = phpbb::$db->sql_query_limit($sql, $limit, $offset);
2680 $users = phpbb::$db->sql_fetchrowset($result);
2681 phpbb::$db->sql_freeresult($result);
2683 $sql = 'SELECT count(user_id) AS user_count
2684 FROM ' . USERS_TABLE . '
2685 WHERE user_warnings > 0
2686 ' . (($limit_days) ? "AND user_last_warning >= $limit_days" : '');
2687 $result = phpbb::$db->sql_query($sql);
2688 $user_count = (int) phpbb::$db->sql_fetchfield('user_count');
2689 phpbb::$db->sql_freeresult($result);
2691 return;
2695 * Get database size
2697 function get_database_size()
2699 $database_size = false;
2701 // This code is heavily influenced by a similar routine in phpMyAdmin 2.2.0
2702 switch (phpbb::$db->dbms_type)
2704 case 'mysql':
2705 $sql = 'SELECT VERSION() AS mysql_version';
2706 $result = phpbb::$db->sql_query($sql);
2707 $row = phpbb::$db->sql_fetchrow($result);
2708 phpbb::$db->sql_freeresult($result);
2710 if ($row)
2712 $version = $row['mysql_version'];
2714 if (preg_match('#(3\.23|[45]\.)#', $version))
2716 $db_name = (preg_match('#^(?:3\.23\.(?:[6-9]|[1-9]{2}))|[45]\.#', $version)) ? "`" . phpbb::$db->dbname . "`" : phpbb::$db->dbname;
2718 $sql = 'SHOW TABLE STATUS
2719 FROM ' . $db_name;
2720 $result = phpbb::$db->sql_query($sql, 7200);
2722 $database_size = 0;
2723 while ($row = phpbb::$db->sql_fetchrow($result))
2725 if ((isset($row['Type']) && $row['Type'] != 'MRG_MyISAM') || (isset($row['Engine']) && ($row['Engine'] == 'MyISAM' || $row['Engine'] == 'InnoDB')))
2727 if (phpbb::$base_config['table_prefix'] != '')
2729 if (strpos($row['Name'], phpbb::$base_config['table_prefix']) !== false)
2731 $database_size += $row['Data_length'] + $row['Index_length'];
2734 else
2736 $database_size += $row['Data_length'] + $row['Index_length'];
2740 phpbb::$db->sql_freeresult($result);
2743 break;
2745 case 'firebird':
2746 global $dbname;
2748 // if it on the local machine, we can get lucky
2749 if (file_exists($dbname))
2751 $database_size = filesize($dbname);
2754 break;
2756 case 'sqlite':
2757 global $dbhost;
2759 if (file_exists($dbhost))
2761 $database_size = filesize($dbhost);
2764 break;
2766 case 'mssql':
2767 $sql = 'SELECT ((SUM(size) * 8.0) * 1024.0) as dbsize
2768 FROM sysfiles';
2769 $result = phpbb::$db->sql_query($sql, 7200);
2770 $database_size = ($row = phpbb::$db->sql_fetchrow($result)) ? $row['dbsize'] : false;
2771 phpbb::$db->sql_freeresult($result);
2772 break;
2774 case 'postgres':
2775 $sql = "SELECT proname
2776 FROM pg_proc
2777 WHERE proname = 'pg_database_size'";
2778 $result = phpbb::$db->sql_query($sql);
2779 $row = phpbb::$db->sql_fetchrow($result);
2780 phpbb::$db->sql_freeresult($result);
2782 if ($row['proname'] == 'pg_database_size')
2784 $database = phpbb::$db->dbname;
2785 if (strpos($database, '.') !== false)
2787 list($database, ) = explode('.', $database);
2790 $sql = "SELECT oid
2791 FROM pg_database
2792 WHERE datname = '$database'";
2793 $result = phpbb::$db->sql_query($sql);
2794 $row = phpbb::$db->sql_fetchrow($result);
2795 phpbb::$db->sql_freeresult($result);
2797 $oid = $row['oid'];
2799 $sql = 'SELECT pg_database_size(' . $oid . ') as size';
2800 $result = phpbb::$db->sql_query($sql);
2801 $row = phpbb::$db->sql_fetchrow($result);
2802 phpbb::$db->sql_freeresult($result);
2804 $database_size = $row['size'];
2806 break;
2808 case 'oracle':
2809 $sql = 'SELECT SUM(bytes) as dbsize
2810 FROM user_segments';
2811 $result = phpbb::$db->sql_query($sql, 7200);
2812 $database_size = ($row = phpbb::$db->sql_fetchrow($result)) ? $row['dbsize'] : false;
2813 phpbb::$db->sql_freeresult($result);
2814 break;
2817 $database_size = ($database_size !== false) ? get_formatted_filesize($database_size) : phpbb::$user->lang['NOT_AVAILABLE'];
2819 return $database_size;
2823 * Retrieve contents from remotely stored file
2825 function get_remote_file($host, $directory, $filename, &$errstr, &$errno, $port = 80, $timeout = 10)
2827 if ($fsock = @fsockopen($host, $port, $errno, $errstr, $timeout))
2829 @fputs($fsock, "GET $directory/$filename HTTP/1.1\r\n");
2830 @fputs($fsock, "HOST: $host\r\n");
2831 @fputs($fsock, "Connection: close\r\n\r\n");
2833 $file_info = '';
2834 $get_info = false;
2836 while (!@feof($fsock))
2838 if ($get_info)
2840 $file_info .= @fread($fsock, 1024);
2842 else
2844 $line = @fgets($fsock, 1024);
2845 if ($line == "\r\n")
2847 $get_info = true;
2849 else if (stripos($line, '404 not found') !== false)
2851 $errstr = phpbb::$user->lang['FILE_NOT_FOUND'] . ': ' . $filename;
2852 return false;
2856 @fclose($fsock);
2858 else
2860 if ($errstr)
2862 $errstr = utf8_convert_message($errstr);
2863 return false;
2865 else
2867 $errstr = phpbb::$user->lang['FSOCK_DISABLED'];
2868 return false;
2872 return $file_info;
2876 * Tidy Warnings
2877 * Remove all warnings which have now expired from the database
2878 * The duration of a warning can be defined by the administrator
2879 * This only removes the warning and reduces the associated count,
2880 * it does not remove the user note recording the contents of the warning
2882 function tidy_warnings()
2884 $expire_date = time() - (phpbb::$config['warnings_expire_days'] * 86400);
2885 $warning_list = $user_list = array();
2887 $sql = 'SELECT * FROM ' . WARNINGS_TABLE . "
2888 WHERE warning_time < $expire_date";
2889 $result = phpbb::$db->sql_query($sql);
2891 while ($row = phpbb::$db->sql_fetchrow($result))
2893 $warning_list[] = $row['warning_id'];
2894 $user_list[$row['user_id']] = isset($user_list[$row['user_id']]) ? ++$user_list[$row['user_id']] : 1;
2896 phpbb::$db->sql_freeresult($result);
2898 if (sizeof($warning_list))
2900 phpbb::$db->sql_transaction('begin');
2902 $sql = 'DELETE FROM ' . WARNINGS_TABLE . '
2903 WHERE ' . phpbb::$db->sql_in_set('warning_id', $warning_list);
2904 phpbb::$db->sql_query($sql);
2906 foreach ($user_list as $user_id => $value)
2908 $sql = 'UPDATE ' . USERS_TABLE . " SET user_warnings = user_warnings - $value
2909 WHERE user_id = $user_id";
2910 phpbb::$db->sql_query($sql);
2913 phpbb::$db->sql_transaction('commit');
2916 set_config('warnings_last_gc', time(), true);
2920 * Tidy database, doing some maintanance tasks
2922 function tidy_database()
2924 // Here we check permission consistency
2926 // Sometimes, it can happen permission tables having forums listed which do not exist
2927 $sql = 'SELECT forum_id
2928 FROM ' . FORUMS_TABLE;
2929 $result = phpbb::$db->sql_query($sql);
2931 $forum_ids = array(0);
2932 while ($row = phpbb::$db->sql_fetchrow($result))
2934 $forum_ids[] = $row['forum_id'];
2936 phpbb::$db->sql_freeresult($result);
2938 // Delete those rows from the acl tables not having listed the forums above
2939 $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
2940 WHERE ' . phpbb::$db->sql_in_set('forum_id', $forum_ids, true);
2941 phpbb::$db->sql_query($sql);
2943 $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
2944 WHERE ' . phpbb::$db->sql_in_set('forum_id', $forum_ids, true);
2945 phpbb::$db->sql_query($sql);
2947 set_config('database_last_gc', time(), true);
2951 * Add permission language - this will make sure custom files will be included
2953 function add_permission_language()
2955 // First of all, our own file. We need to include it as the first file because it presets all relevant variables.
2956 phpbb::$user->add_lang('acp/permissions_phpbb');
2958 $files_to_add = array();
2960 // Now search in acp and mods folder for permissions_ files.
2961 foreach (array('acp/', 'mods/') as $path)
2963 $dh = @opendir(phpbb::$user->lang_path . phpbb::$user->lang_name . '/' . $path);
2965 if ($dh)
2967 while (($file = readdir($dh)) !== false)
2969 if ($file !== 'permissions_phpbb.' . PHP_EXT && strpos($file, 'permissions_') === 0 && substr($file, -(strlen(PHP_EXT) + 1)) === '.' . PHP_EXT)
2971 $files_to_add[] = $path . substr($file, 0, -(strlen(PHP_EXT) + 1));
2974 closedir($dh);
2978 if (!sizeof($files_to_add))
2980 return false;
2983 phpbb::$user->add_lang($files_to_add);
2984 return true;
2988 * Upload files or let the user download them as appropriate
2990 function process_transfer($module_url, $update_list, $new_location, $download_filename)
2992 // todo: add $s_hidden_fields as a parameter to this function
2993 $s_hidden_fields = '';
2995 if (request_var('download', false))
2997 $use_method = request_var('use_method', '');
2998 $methods = array('.tar');
3000 $available_methods = array('.tar.gz' => 'zlib', '.tar.bz2' => 'bz2', '.zip' => 'zlib');
3001 foreach ($available_methods as $type => $module)
3003 if (!@extension_loaded($module))
3005 continue;
3008 $methods[] = $type;
3011 // Let the user decide in which format he wants to have the pack
3012 if (!$use_method)
3014 $radio_buttons = '';
3015 foreach ($methods as $method)
3017 $radio_buttons .= '<label><input type="radio"' . ((!$radio_buttons) ? ' id="use_method"' : '') . ' class="radio" value="' . $method . '" name="use_method" /> ' . $method . '</label>';
3020 phpbb::$template->assign_vars(array(
3021 'S_DOWNLOAD_FILES' => true,
3022 'U_ACTION' => append_sid($module_url),
3023 'RADIO_BUTTONS' => $radio_buttons,
3024 'S_HIDDEN_FIELDS' => $s_hidden_fields)
3027 // To ease the update process create a file location map
3028 $script_path = (phpbb::$config['force_server_vars']) ? ((phpbb::$config['script_path'] == '/') ? '/' : phpbb::$config['script_path'] . '/') : phpbb::$user->page['root_script_path'];
3030 foreach ($update_list as $status => $files)
3032 if ($status == 'up_to_date' || $status == 'no_update' || $status == 'status')
3034 continue;
3037 foreach ($files as $file_struct)
3039 phpbb::$template->assign_block_vars('location', array(
3040 'SOURCE' => htmlspecialchars($file_struct['filename']),
3041 'DESTINATION' => $script_path . htmlspecialchars($file_struct['filename']),
3045 return 'SELECT_DOWNLOAD_FORMAT';
3048 if (!in_array($use_method, $methods))
3050 $use_method = '.tar';
3053 $update_mode = 'download';
3055 else
3057 // Choose FTP, if not available use fsock...
3058 $method = basename(request_var('method', ''));
3059 $submit = phpbb_request::is_set_post('update');
3060 $test_ftp_connection = request_var('test_connection', '');
3062 if (!$method || !class_exists($method))
3064 $method = 'ftp';
3065 $methods = transfer::methods();
3067 if (!in_array('ftp', $methods))
3069 $method = $methods[0];
3073 $test_connection = false;
3074 if ($test_ftp_connection || $submit)
3076 $transfer = new $method(request_var('host', ''), request_var('username', ''), request_var('password', ''), request_var('root_path', ''), request_var('port', ''), request_var('timeout', ''));
3077 $test_connection = $transfer->open_session();
3079 // Make sure that the directory is correct by checking for the existence of common.php
3080 if ($test_connection === true)
3082 // Check for common.php file
3083 if (!$transfer->file_exists(PHPBB_ROOT_PATH, 'common.' . PHP_EXT))
3085 $test_connection = 'ERR_WRONG_PATH_TO_PHPBB';
3089 $transfer->close_session();
3091 // Make sure the login details are correct before continuing
3092 if ($submit && $test_connection !== true)
3094 $submit = false;
3095 $test_ftp_connection = true;
3099 $s_hidden_fields .= build_hidden_fields(array('method' => $method));
3101 if (!$submit)
3103 if (!class_exists($method))
3105 trigger_error('Method does not exist.', E_USER_ERROR);
3108 $requested_data = call_user_func(array($method, 'data'));
3109 foreach ($requested_data as $data => $default)
3111 phpbb::$template->assign_block_vars('data', array(
3112 'DATA' => $data,
3113 'NAME' => phpbb::$user->lang[strtoupper($method . '_' . $data)],
3114 'EXPLAIN' => phpbb::$user->lang[strtoupper($method . '_' . $data) . '_EXPLAIN'],
3115 'DEFAULT' => (request_var($data, false)) ? request_var($data, '') : $default
3119 phpbb::$template->assign_vars(array(
3120 'S_CONNECTION_SUCCESS' => ($test_ftp_connection && $test_connection === true) ? true : false,
3121 'S_CONNECTION_FAILED' => ($test_ftp_connection && $test_connection !== true) ? true : false,
3122 'ERROR_MSG' => ($test_ftp_connection && $test_connection !== true) ? phpbb::$user->lang[$test_connection] : '',
3124 'S_FTP_UPLOAD' => true,
3125 'UPLOAD_METHOD' => $method,
3126 'U_ACTION' => $module_url,
3127 'U_DOWNLOAD_METHOD' => $module_url . '&amp;download=1',
3128 'S_HIDDEN_FIELDS' => $s_hidden_fields,
3131 return 'SELECT_FTP_SETTINGS';
3134 $update_mode = 'upload';
3137 // Now init the connection
3138 if ($update_mode == 'download')
3140 // Now update the installation or download the archive...
3141 $archive_filename = $download_filename . '_' . time() . '_' . unique_id();
3143 if ($use_method == '.zip')
3145 $compress = new compress_zip('w', PHPBB_ROOT_PATH . 'store/' . $archive_filename . $use_method);
3147 else
3149 $compress = new compress_tar('w', PHPBB_ROOT_PATH . 'store/' . $archive_filename . $use_method, $use_method);
3152 else
3154 $transfer = new $method(request_var('host', ''), request_var('username', ''), request_var('password', ''), request_var('root_path', ''), request_var('port', ''), request_var('timeout', ''));
3155 $transfer->open_session();
3158 // Ok, go through the update list and do the operations based on their status
3159 foreach ($update_list as $status => $files)
3161 if (!is_array($files))
3163 continue;
3166 foreach ($files as $file_struct)
3168 $original_filename = ($file_struct['custom']) ? $file_struct['original'] : $file_struct['filename'];
3170 switch ($status)
3172 case 'new':
3173 case 'new_conflict':
3174 case 'not_modified':
3176 if ($update_mode == 'download')
3178 $compress->add_custom_file($new_location . $original_filename, $file_struct['filename']);
3180 else
3182 if ($status != 'new')
3184 $transfer->rename($file_struct['filename'], $file_struct['filename'] . '.bak');
3187 // New directory too?
3188 $dirname = dirname($file_struct['filename']);
3190 if ($dirname && !file_exists(PHPBB_ROOT_PATH . $dirname))
3192 $transfer->make_dir($dirname);
3195 $transfer->copy_file($new_location . $original_filename, $file_struct['filename']);
3197 break;
3199 case 'modified':
3201 $contents = base64_decode($cache->get($file_list[$file_struct['filename']]));
3203 if ($update_mode == 'download')
3205 $compress->add_data($contents, $file_struct['filename']);
3207 else
3209 // @todo add option to specify if a backup file should be created?
3210 $transfer->rename($file_struct['filename'], $file_struct['filename'] . '.bak');
3211 $transfer->write_file($file_struct['filename'], $contents);
3213 break;
3215 case 'conflict':
3217 $contents = base64_decode($cache->get($file_list[$file_struct['filename']]));
3219 if ($update_mode == 'download')
3221 $compress->add_data($contents, $file_struct['filename']);
3223 else
3225 $transfer->rename($file_struct['filename'], $file_struct['filename'] . '.bak');
3226 $transfer->write_file($file_struct['filename'], $contents);
3228 break;
3233 if ($update_mode == 'download')
3235 $compress->close();
3237 $compress->download($archive_filename, $download_filename);
3238 @unlink(PHPBB_ROOT_PATH . 'store/' . $archive_filename . $use_method);
3240 exit;
3242 else
3244 $transfer->close_session();
3246 phpbb::$template->assign_var('S_UPLOAD_SUCCESS', true);
3247 return;