replace constants with class constants.
[phpbb.git] / phpBB / includes / functions_admin.php
blob89c6a21468da8bd5139b48146e684b038346b5c7
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 global $db;
25 if (!$sql_id || !$sql_table)
27 return;
30 $sql_where = ($module_class) ? " WHERE module_class = '" . $db->sql_escape($module_class) . "'" : '';
32 // Reset to minimum possible left and right id
33 $sql = "SELECT MIN(left_id) as min_left_id, MIN(right_id) as min_right_id
34 FROM $sql_table
35 $sql_where";
36 $result = $db->sql_query($sql);
37 $row = $db->sql_fetchrow($result);
38 $db->sql_freeresult($result);
40 $substract = (int) (min($row['min_left_id'], $row['min_right_id']) - 1);
42 if ($substract > 0)
44 $sql = "UPDATE $sql_table
45 SET left_id = left_id - $substract, right_id = right_id - $substract
46 $sql_where";
47 $db->sql_query($sql);
50 $sql = "SELECT $sql_id, parent_id, left_id, right_id
51 FROM $sql_table
52 $sql_where
53 ORDER BY left_id ASC, parent_id ASC, $sql_id ASC";
54 $f_result = $db->sql_query($sql);
56 while ($item_data = $db->sql_fetchrow($f_result))
58 if ($item_data['parent_id'])
60 $sql = "SELECT left_id, right_id
61 FROM $sql_table
62 $sql_where " . (($sql_where) ? 'AND' : 'WHERE') . "
63 $sql_id = {$item_data['parent_id']}";
64 $result = $db->sql_query($sql);
66 if (!$row = $db->sql_fetchrow($result))
68 $sql = "UPDATE $sql_table SET parent_id = 0 WHERE $sql_id = " . $item_data[$sql_id];
69 $db->sql_query($sql);
71 $db->sql_freeresult($result);
73 $sql = "UPDATE $sql_table
74 SET left_id = left_id + 2, right_id = right_id + 2
75 $sql_where " . (($sql_where) ? 'AND' : 'WHERE') . "
76 left_id > {$row['right_id']}";
77 $db->sql_query($sql);
79 $sql = "UPDATE $sql_table
80 SET right_id = right_id + 2
81 $sql_where " . (($sql_where) ? 'AND' : 'WHERE') . "
82 {$row['left_id']} BETWEEN left_id AND right_id";
83 $db->sql_query($sql);
85 $item_data['left_id'] = $row['right_id'];
86 $item_data['right_id'] = $row['right_id'] + 1;
88 else
90 $sql = "SELECT MAX(right_id) AS right_id
91 FROM $sql_table
92 $sql_where";
93 $result = $db->sql_query($sql);
94 $row = $db->sql_fetchrow($result);
95 $db->sql_freeresult($result);
97 $item_data['left_id'] = $row['right_id'] + 1;
98 $item_data['right_id'] = $row['right_id'] + 2;
101 $sql = "UPDATE $sql_table
102 SET left_id = {$item_data['left_id']}, right_id = {$item_data['right_id']}
103 WHERE $sql_id = " . $item_data[$sql_id];
104 $db->sql_query($sql);
106 $db->sql_freeresult($f_result);
111 * Simple version of jumpbox, just lists authed forums
113 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)
115 global $db, $user, $auth;
117 $acl = ($ignore_acl) ? '' : (($only_acl_post) ? 'f_post' : array('f_list', 'a_forum', 'a_forumadd', 'a_forumdel'));
119 // This query is identical to the jumpbox one
120 $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
121 FROM ' . FORUMS_TABLE . '
122 ORDER BY left_id ASC';
123 $result = $db->sql_query($sql, 600);
125 $right = 0;
126 $padding_store = array('0' => '');
127 $padding = '';
128 $forum_list = ($return_array) ? array() : '';
130 // Sometimes it could happen that forums will be displayed here not be displayed within the index page
131 // This is the result of forums not displayed at index, having list permissions and a parent of a forum with no permissions.
132 // If this happens, the padding could be "broken"
134 while ($row = $db->sql_fetchrow($result))
136 if ($row['left_id'] < $right)
138 $padding .= '&nbsp; &nbsp;';
139 $padding_store[$row['parent_id']] = $padding;
141 else if ($row['left_id'] > $right + 1)
143 $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
146 $right = $row['right_id'];
147 $disabled = false;
149 if ($acl && !$auth->acl_gets($acl, $row['forum_id']))
151 // List permission?
152 if ($auth->acl_get('f_list', $row['forum_id']))
154 $disabled = true;
156 else
158 continue;
162 if (
163 ((is_array($ignore_id) && in_array($row['forum_id'], $ignore_id)) || $row['forum_id'] == $ignore_id)
165 // Non-postable forum with no subforums, don't display
166 ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']) && $ignore_emptycat)
168 ($row['forum_type'] != FORUM_POST && $ignore_nonpost)
171 $disabled = true;
174 if ($return_array)
176 // Include some more information...
177 $selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? true : false) : (($row['forum_id'] == $select_id) ? true : false);
178 $forum_list[$row['forum_id']] = array_merge(array('padding' => $padding, 'selected' => ($selected && !$disabled), 'disabled' => $disabled), $row);
180 else
182 $selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? ' selected="selected"' : '') : (($row['forum_id'] == $select_id) ? ' selected="selected"' : '');
183 $forum_list .= '<option value="' . $row['forum_id'] . '"' . (($disabled) ? ' disabled="disabled" class="disabled-option"' : $selected) . '>' . $padding . $row['forum_name'] . '</option>';
186 $db->sql_freeresult($result);
187 unset($padding_store);
189 return $forum_list;
193 * Generate size select options
195 function size_select_options($size_compare)
197 global $user;
199 $size_types_text = array($user->lang['BYTES'], $user->lang['KIB'], $user->lang['MIB']);
200 $size_types = array('b', 'kb', 'mb');
202 $s_size_options = '';
204 for ($i = 0, $size = sizeof($size_types_text); $i < $size; $i++)
206 $selected = ($size_compare == $size_types[$i]) ? ' selected="selected"' : '';
207 $s_size_options .= '<option value="' . $size_types[$i] . '"' . $selected . '>' . $size_types_text[$i] . '</option>';
210 return $s_size_options;
214 * Generate list of groups (option fields without select)
216 * @param int $group_id The default group id to mark as selected
217 * @param array $exclude_ids The group ids to exclude from the list, false (default) if you whish to exclude no id
218 * @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.
220 * @return string The list of options.
222 function group_select_options($group_id, $exclude_ids = false, $manage_founder = false)
224 global $db, $user, $config;
226 $exclude_sql = ($exclude_ids !== false && sizeof($exclude_ids)) ? 'WHERE ' . $db->sql_in_set('group_id', array_map('intval', $exclude_ids), true) : '';
227 $sql_and = (!$config['coppa_enable']) ? (($exclude_sql) ? ' AND ' : ' WHERE ') . "group_name <> 'REGISTERED_COPPA'" : '';
228 $sql_founder = ($manage_founder !== false) ? (($exclude_sql || $sql_and) ? ' AND ' : ' WHERE ') . 'group_founder_manage = ' . (int) $manage_founder : '';
230 $sql = 'SELECT group_id, group_name, group_type
231 FROM ' . GROUPS_TABLE . "
232 $exclude_sql
233 $sql_and
234 $sql_founder
235 ORDER BY group_type DESC, group_name ASC";
236 $result = $db->sql_query($sql);
238 $s_group_options = '';
239 while ($row = $db->sql_fetchrow($result))
241 $selected = ($row['group_id'] == $group_id) ? ' selected="selected"' : '';
242 $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>';
244 $db->sql_freeresult($result);
246 return $s_group_options;
250 * Obtain authed forums list
252 function get_forum_list($acl_list = 'f_list', $id_only = true, $postable_only = false, $no_cache = false)
254 global $db, $auth;
255 static $forum_rows;
257 if (!isset($forum_rows))
259 // This query is identical to the jumpbox one
260 $expire_time = ($no_cache) ? 0 : 600;
262 $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
263 FROM ' . FORUMS_TABLE . '
264 ORDER BY left_id ASC';
265 $result = $db->sql_query($sql, $expire_time);
267 $forum_rows = array();
269 $right = $padding = 0;
270 $padding_store = array('0' => 0);
272 while ($row = $db->sql_fetchrow($result))
274 if ($row['left_id'] < $right)
276 $padding++;
277 $padding_store[$row['parent_id']] = $padding;
279 else if ($row['left_id'] > $right + 1)
281 // Ok, if the $padding_store for this parent is empty there is something wrong. For now we will skip over it.
282 // @todo digging deep to find out "how" this can happen.
283 $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : $padding;
286 $right = $row['right_id'];
287 $row['padding'] = $padding;
289 $forum_rows[] = $row;
291 $db->sql_freeresult($result);
292 unset($padding_store);
295 $rowset = array();
296 foreach ($forum_rows as $row)
298 if ($postable_only && $row['forum_type'] != FORUM_POST)
300 continue;
303 if ($acl_list == '' || ($acl_list != '' && $auth->acl_gets($acl_list, $row['forum_id'])))
305 $rowset[] = ($id_only) ? $row['forum_id'] : $row;
309 return $rowset;
313 * Get forum branch
315 function get_forum_branch($forum_id, $type = 'all', $order = 'descending', $include_forum = true)
317 global $db;
319 switch ($type)
321 case 'parents':
322 $condition = 'f1.left_id BETWEEN f2.left_id AND f2.right_id';
323 break;
325 case 'children':
326 $condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id';
327 break;
329 default:
330 $condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id OR f1.left_id BETWEEN f2.left_id AND f2.right_id';
331 break;
334 $rows = array();
336 $sql = 'SELECT f2.*
337 FROM ' . FORUMS_TABLE . ' f1
338 LEFT JOIN ' . FORUMS_TABLE . " f2 ON ($condition)
339 WHERE f1.forum_id = $forum_id
340 ORDER BY f2.left_id " . (($order == 'descending') ? 'ASC' : 'DESC');
341 $result = $db->sql_query($sql);
343 while ($row = $db->sql_fetchrow($result))
345 if (!$include_forum && $row['forum_id'] == $forum_id)
347 continue;
350 $rows[] = $row;
352 $db->sql_freeresult($result);
354 return $rows;
358 * Get physical file listing
360 function filelist($rootdir, $dir = '', $type = 'gif|jpg|jpeg|png')
362 $matches = array();
364 // Remove initial / if present
365 $rootdir = (substr($rootdir, 0, 1) == '/') ? substr($rootdir, 1) : $rootdir;
366 // Add closing / if not present
367 $rootdir = ($rootdir && substr($rootdir, -1) != '/') ? $rootdir . '/' : $rootdir;
369 // Remove initial / if present
370 $dir = (substr($dir, 0, 1) == '/') ? substr($dir, 1) : $dir;
371 // Add closing / if not present
372 $dir = ($dir && substr($dir, -1) != '/') ? $dir . '/' : $dir;
374 if (!is_dir($rootdir . $dir))
376 return $matches;
379 $dh = @opendir($rootdir . $dir);
381 if (!$dh)
383 return $matches;
386 while (($fname = readdir($dh)) !== false)
388 if (is_file("$rootdir$dir$fname"))
390 if (filesize("$rootdir$dir$fname") && preg_match('#\.' . $type . '$#i', $fname))
392 $matches[$dir][] = $fname;
395 else if ($fname[0] != '.' && is_dir("$rootdir$dir$fname"))
397 $matches += filelist($rootdir, "$dir$fname", $type);
400 closedir($dh);
402 return $matches;
406 * Move topic(s)
408 function move_topics($topic_ids, $forum_id, $auto_sync = true)
410 global $db;
412 if (empty($topic_ids))
414 return;
417 $forum_ids = array($forum_id);
419 if (!is_array($topic_ids))
421 $topic_ids = array($topic_ids);
424 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
425 WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids) . '
426 AND forum_id = ' . $forum_id;
427 $db->sql_query($sql);
429 if ($auto_sync)
431 $sql = 'SELECT DISTINCT forum_id
432 FROM ' . TOPICS_TABLE . '
433 WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
434 $result = $db->sql_query($sql);
436 while ($row = $db->sql_fetchrow($result))
438 $forum_ids[] = $row['forum_id'];
440 $db->sql_freeresult($result);
443 $table_ary = array(TOPICS_TABLE, POSTS_TABLE, LOG_TABLE, DRAFTS_TABLE, TOPICS_TRACK_TABLE);
444 foreach ($table_ary as $table)
446 $sql = "UPDATE $table
447 SET forum_id = $forum_id
448 WHERE " . $db->sql_in_set('topic_id', $topic_ids);
449 $db->sql_query($sql);
451 unset($table_ary);
453 if ($auto_sync)
455 sync('forum', 'forum_id', $forum_ids, true, true);
456 unset($forum_ids);
461 * Move post(s)
463 function move_posts($post_ids, $topic_id, $auto_sync = true)
465 global $db;
467 if (!is_array($post_ids))
469 $post_ids = array($post_ids);
472 $forum_ids = array();
473 $topic_ids = array($topic_id);
475 $sql = 'SELECT DISTINCT topic_id, forum_id
476 FROM ' . POSTS_TABLE . '
477 WHERE ' . $db->sql_in_set('post_id', $post_ids);
478 $result = $db->sql_query($sql);
480 while ($row = $db->sql_fetchrow($result))
482 $forum_ids[] = $row['forum_id'];
483 $topic_ids[] = $row['topic_id'];
485 $db->sql_freeresult($result);
487 $sql = 'SELECT forum_id
488 FROM ' . TOPICS_TABLE . '
489 WHERE topic_id = ' . $topic_id;
490 $result = $db->sql_query($sql);
491 $forum_row = $db->sql_fetchrow($result);
492 $db->sql_freeresult($result);
494 if (!$forum_row)
496 trigger_error('NO_TOPIC');
499 $sql = 'UPDATE ' . POSTS_TABLE . '
500 SET forum_id = ' . $forum_row['forum_id'] . ", topic_id = $topic_id
501 WHERE " . $db->sql_in_set('post_id', $post_ids);
502 $db->sql_query($sql);
504 $sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
505 SET topic_id = $topic_id, in_message = 0
506 WHERE " . $db->sql_in_set('post_msg_id', $post_ids);
507 $db->sql_query($sql);
509 if ($auto_sync)
511 $forum_ids[] = $forum_row['forum_id'];
513 sync('topic_reported', 'topic_id', $topic_ids);
514 sync('topic_attachment', 'topic_id', $topic_ids);
515 sync('topic', 'topic_id', $topic_ids, true);
516 sync('forum', 'forum_id', $forum_ids, true, true);
519 // Update posted information
520 update_posted_info($topic_ids);
524 * Remove topic(s)
526 function delete_topics($where_type, $where_ids, $auto_sync = true, $post_count_sync = true, $call_delete_posts = true)
528 global $db, $config;
530 $approved_topics = 0;
531 $forum_ids = $topic_ids = array();
533 if ($where_type === 'range')
535 $where_clause = $where_ids;
537 else
539 $where_ids = (is_array($where_ids)) ? array_unique($where_ids) : array($where_ids);
541 if (!sizeof($where_ids))
543 return array('topics' => 0, 'posts' => 0);
546 $where_clause = $db->sql_in_set($where_type, $where_ids);
549 // Making sure that delete_posts does not call delete_topics again...
550 $return = array(
551 'posts' => ($call_delete_posts) ? delete_posts($where_type, $where_ids, false, true, $post_count_sync, false) : 0,
554 $sql = 'SELECT topic_id, forum_id, topic_approved, topic_moved_id
555 FROM ' . TOPICS_TABLE . '
556 WHERE ' . $where_clause;
557 $result = $db->sql_query($sql);
559 while ($row = $db->sql_fetchrow($result))
561 $forum_ids[] = $row['forum_id'];
562 $topic_ids[] = $row['topic_id'];
564 if ($row['topic_approved'] && !$row['topic_moved_id'])
566 $approved_topics++;
569 $db->sql_freeresult($result);
571 $return['topics'] = sizeof($topic_ids);
573 if (!sizeof($topic_ids))
575 return $return;
578 $db->sql_transaction('begin');
580 $table_ary = array(TOPICS_TRACK_TABLE, TOPICS_POSTED_TABLE, POLL_VOTES_TABLE, POLL_OPTIONS_TABLE, TOPICS_WATCH_TABLE, TOPICS_TABLE);
582 foreach ($table_ary as $table)
584 $sql = "DELETE FROM $table
585 WHERE " . $db->sql_in_set('topic_id', $topic_ids);
586 $db->sql_query($sql);
588 unset($table_ary);
590 $moved_topic_ids = array();
592 // update the other forums
593 $sql = 'SELECT topic_id, forum_id
594 FROM ' . TOPICS_TABLE . '
595 WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids);
596 $result = $db->sql_query($sql);
598 while ($row = $db->sql_fetchrow($result))
600 $forum_ids[] = $row['forum_id'];
601 $moved_topic_ids[] = $row['topic_id'];
603 $db->sql_freeresult($result);
605 if (sizeof($moved_topic_ids))
607 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
608 WHERE ' . $db->sql_in_set('topic_id', $moved_topic_ids);
609 $db->sql_query($sql);
612 $db->sql_transaction('commit');
614 if ($auto_sync)
616 sync('forum', 'forum_id', array_unique($forum_ids), true, true);
617 sync('topic_reported', $where_type, $where_ids);
620 if ($approved_topics)
622 set_config('num_topics', $config['num_topics'] - $approved_topics, true);
625 return $return;
629 * Remove post(s)
631 function delete_posts($where_type, $where_ids, $auto_sync = true, $posted_sync = true, $post_count_sync = true, $call_delete_topics = true)
633 global $db, $config;
635 if ($where_type === 'range')
637 $where_clause = $where_ids;
639 else
641 if (is_array($where_ids))
643 $where_ids = array_unique($where_ids);
645 else
647 $where_ids = array($where_ids);
650 if (!sizeof($where_ids))
652 return false;
655 $where_clause = $db->sql_in_set($where_type, array_map('intval', $where_ids));
658 $approved_posts = 0;
659 $post_ids = $topic_ids = $forum_ids = $post_counts = $remove_topics = array();
661 $sql = 'SELECT post_id, poster_id, post_approved, post_postcount, topic_id, forum_id
662 FROM ' . POSTS_TABLE . '
663 WHERE ' . $where_clause;
664 $result = $db->sql_query($sql);
666 while ($row = $db->sql_fetchrow($result))
668 $post_ids[] = $row['post_id'];
669 $poster_ids[] = $row['poster_id'];
670 $topic_ids[] = $row['topic_id'];
671 $forum_ids[] = $row['forum_id'];
673 if ($row['post_postcount'] && $post_count_sync && $row['post_approved'])
675 $post_counts[$row['poster_id']] = (!empty($post_counts[$row['poster_id']])) ? $post_counts[$row['poster_id']] + 1 : 1;
678 if ($row['post_approved'])
680 $approved_posts++;
683 $db->sql_freeresult($result);
685 if (!sizeof($post_ids))
687 return false;
690 $db->sql_transaction('begin');
692 $table_ary = array(POSTS_TABLE, REPORTS_TABLE);
694 foreach ($table_ary as $table)
696 $sql = "DELETE FROM $table
697 WHERE " . $db->sql_in_set('post_id', $post_ids);
698 $db->sql_query($sql);
700 unset($table_ary);
702 // Adjust users post counts
703 if (sizeof($post_counts) && $post_count_sync)
705 foreach ($post_counts as $poster_id => $substract)
707 $sql = 'UPDATE ' . USERS_TABLE . '
708 SET user_posts = 0
709 WHERE user_id = ' . $poster_id . '
710 AND user_posts < ' . $substract;
711 $db->sql_query($sql);
713 $sql = 'UPDATE ' . USERS_TABLE . '
714 SET user_posts = user_posts - ' . $substract . '
715 WHERE user_id = ' . $poster_id . '
716 AND user_posts >= ' . $substract;
717 $db->sql_query($sql);
721 // Remove topics now having no posts?
722 if (sizeof($topic_ids))
724 $sql = 'SELECT topic_id
725 FROM ' . POSTS_TABLE . '
726 WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
727 GROUP BY topic_id';
728 $result = $db->sql_query($sql);
730 while ($row = $db->sql_fetchrow($result))
732 $remove_topics[] = $row['topic_id'];
734 $db->sql_freeresult($result);
736 // Actually, those not within remove_topics should be removed. ;)
737 $remove_topics = array_diff($topic_ids, $remove_topics);
740 // Remove the message from the search index
741 $search_type = basename($config['search_type']);
743 if (!file_exists(PHPBB_ROOT_PATH . 'includes/search/' . $search_type . '.' . PHP_EXT))
745 trigger_error('NO_SUCH_SEARCH_MODULE');
748 include_once(PHPBB_ROOT_PATH . "includes/search/$search_type." . PHP_EXT);
750 $error = false;
751 $search = new $search_type($error);
753 if ($error)
755 trigger_error($error);
758 $search->index_remove($post_ids, $poster_ids, $forum_ids);
760 delete_attachments('post', $post_ids, false);
762 $db->sql_transaction('commit');
764 // Resync topics_posted table
765 if ($posted_sync)
767 update_posted_info($topic_ids);
770 if ($auto_sync)
772 sync('topic_reported', 'topic_id', $topic_ids);
773 sync('topic', 'topic_id', $topic_ids, true);
774 sync('forum', 'forum_id', $forum_ids, true, true);
777 if ($approved_posts)
779 set_config('num_posts', $config['num_posts'] - $approved_posts, true);
782 // We actually remove topics now to not be inconsistent (the delete_topics function calls this function too)
783 if (sizeof($remove_topics) && $call_delete_topics)
785 delete_topics('topic_id', $remove_topics, $auto_sync, $post_count_sync, false);
788 return sizeof($post_ids);
792 * Delete Attachments
794 * @param string $mode can be: post|message|topic|attach|user
795 * @param mixed $ids can be: post_ids, message_ids, topic_ids, attach_ids, user_ids
796 * @param bool $resync set this to false if you are deleting posts or topics
798 function delete_attachments($mode, $ids, $resync = true)
800 global $db, $config;
802 if (is_array($ids) && sizeof($ids))
804 $ids = array_unique($ids);
805 $ids = array_map('intval', $ids);
807 else
809 $ids = array((int) $ids);
812 if (!sizeof($ids))
814 return false;
817 switch ($mode)
819 case 'post':
820 case 'message':
821 $sql_id = 'post_msg_id';
822 break;
824 case 'topic':
825 $sql_id = 'topic_id';
826 break;
828 case 'user':
829 $sql_id = 'poster_id';
830 break;
832 case 'attach':
833 default:
834 $sql_id = 'attach_id';
835 $mode = 'attach';
836 break;
839 $post_ids = $message_ids = $topic_ids = $physical = array();
841 // Collect post and topic ids for later use if we need to touch remaining entries (if resync is enabled)
842 $sql = 'SELECT post_msg_id, topic_id, in_message, physical_filename, thumbnail, filesize, is_orphan
843 FROM ' . ATTACHMENTS_TABLE . '
844 WHERE ' . $db->sql_in_set($sql_id, $ids);
845 $result = $db->sql_query($sql);
847 while ($row = $db->sql_fetchrow($result))
849 // We only need to store post/message/topic ids if resync is enabled and the file is not orphaned
850 if ($resync && !$row['is_orphan'])
852 if (!$row['in_message'])
854 $post_ids[] = $row['post_msg_id'];
855 $topic_ids[] = $row['topic_id'];
857 else
859 $message_ids[] = $row['post_msg_id'];
863 $physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize'], 'is_orphan' => $row['is_orphan']);
865 $db->sql_freeresult($result);
867 // Delete attachments
868 $sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . '
869 WHERE ' . $db->sql_in_set($sql_id, $ids);
870 $db->sql_query($sql);
871 $num_deleted = $db->sql_affectedrows();
873 if (!$num_deleted)
875 return 0;
878 // Delete attachments from filesystem
879 $space_removed = $files_removed = 0;
880 foreach ($physical as $file_ary)
882 if (phpbb_unlink($file_ary['filename'], 'file', true) && !$file_ary['is_orphan'])
884 // Only non-orphaned files count to the file size
885 $space_removed += $file_ary['filesize'];
886 $files_removed++;
889 if ($file_ary['thumbnail'])
891 phpbb_unlink($file_ary['filename'], 'thumbnail', true);
895 if ($space_removed || $files_removed)
897 set_config('upload_dir_size', $config['upload_dir_size'] - $space_removed, true);
898 set_config('num_files', $config['num_files'] - $files_removed, true);
901 // If we do not resync, we do not need to adjust any message, post, topic or user entries
902 if (!$resync)
904 return $num_deleted;
907 // No more use for the original ids
908 unset($ids);
910 // Now, we need to resync posts, messages, topics. We go through every one of them
911 $post_ids = array_unique($post_ids);
912 $message_ids = array_unique($message_ids);
913 $topic_ids = array_unique($topic_ids);
915 // Update post indicators for posts now no longer having attachments
916 if (sizeof($post_ids))
918 $sql = 'UPDATE ' . POSTS_TABLE . '
919 SET post_attachment = 0
920 WHERE ' . $db->sql_in_set('post_id', $post_ids);
921 $db->sql_query($sql);
924 // Update message table if messages are affected
925 if (sizeof($message_ids))
927 $sql = 'UPDATE ' . PRIVMSGS_TABLE . '
928 SET message_attachment = 0
929 WHERE ' . $db->sql_in_set('msg_id', $message_ids);
930 $db->sql_query($sql);
933 // Now update the topics. This is a bit trickier, because there could be posts still having attachments within the topic
934 if (sizeof($topic_ids))
936 // Just check which topics are still having an assigned attachment not orphaned by querying the attachments table (much less entries expected)
937 $sql = 'SELECT topic_id
938 FROM ' . ATTACHMENTS_TABLE . '
939 WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
940 AND is_orphan = 0';
941 $result = $db->sql_query($sql);
943 $remaining_ids = array();
944 while ($row = $db->sql_fetchrow($result))
946 $remaining_ids[] = $row['topic_id'];
948 $db->sql_freeresult($result);
950 // Now only unset those ids remaining
951 $topic_ids = array_diff($topic_ids, $remaining_ids);
953 if (sizeof($topic_ids))
955 $sql = 'UPDATE ' . TOPICS_TABLE . '
956 SET topic_attachment = 0
957 WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
958 $db->sql_query($sql);
962 return $num_deleted;
966 * Remove topic shadows
968 function delete_topic_shadows($max_age, $forum_id = '', $auto_sync = true)
970 $where = (is_array($forum_id)) ? 'AND ' . $db->sql_in_set('t.forum_id', array_map('intval', $forum_id)) : (($forum_id) ? 'AND t.forum_id = ' . (int) $forum_id : '');
972 if ($db->multi_table_deletion)
974 $sql = 'DELETE FROM ' . TOPICS_TABLE . ' t
975 USING ' . TOPICS_TABLE . ' t2
976 WHERE t.topic_moved_id = t2.topic_id
977 AND t.topic_time < ' . (time() - $max_age)
978 . $where;
979 $db->sql_query($sql);
981 else
983 $sql = 'SELECT t.topic_id
984 FROM ' . TOPICS_TABLE . ' t, ' . TOPICS_TABLE . ' t2
985 WHERE t.topic_moved_id = t2.topic_id
986 AND t.topic_time < ' . (time() - $max_age)
987 . $where;
988 $result = $db->sql_query($sql);
990 $topic_ids = array();
991 while ($row = $db->sql_fetchrow($result))
993 $topic_ids[] = $row['topic_id'];
995 $db->sql_freeresult($result);
997 if (sizeof($topic_ids))
999 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1000 WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1001 $db->sql_query($sql);
1005 if ($auto_sync)
1007 $where_type = ($forum_id) ? 'forum_id' : '';
1008 sync('forum', $where_type, $forum_id, true, true);
1013 * Update/Sync posted information for topics
1015 function update_posted_info(&$topic_ids)
1017 global $db, $config;
1019 if (empty($topic_ids) || !$config['load_db_track'])
1021 return;
1024 // First of all, let us remove any posted information for these topics
1025 $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
1026 WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1027 $db->sql_query($sql);
1029 // Now, let us collect the user/topic combos for rebuilding the information
1030 $sql = 'SELECT poster_id, topic_id
1031 FROM ' . POSTS_TABLE . '
1032 WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
1033 AND poster_id <> ' . ANONYMOUS . '
1034 GROUP BY poster_id, topic_id';
1035 $result = $db->sql_query($sql);
1037 $posted = array();
1038 while ($row = $db->sql_fetchrow($result))
1040 // Add as key to make them unique (grouping by) and circumvent empty keys on array_unique
1041 $posted[$row['poster_id']][] = $row['topic_id'];
1043 $db->sql_freeresult($result);
1045 // Now add the information...
1046 $sql_ary = array();
1047 foreach ($posted as $user_id => $topic_row)
1049 foreach ($topic_row as $topic_id)
1051 $sql_ary[] = array(
1052 'user_id' => (int) $user_id,
1053 'topic_id' => (int) $topic_id,
1054 'topic_posted' => 1,
1058 unset($posted);
1060 $db->sql_multi_insert(TOPICS_POSTED_TABLE, $sql_ary);
1064 * Delete attached file
1066 function phpbb_unlink($filename, $mode = 'file', $entry_removed = false)
1068 global $db, $config;
1070 // Because of copying topics or modifications a physical filename could be assigned more than once. If so, do not remove the file itself.
1071 $sql = 'SELECT COUNT(attach_id) AS num_entries
1072 FROM ' . ATTACHMENTS_TABLE . "
1073 WHERE physical_filename = '" . $db->sql_escape(basename($filename)) . "'";
1074 $result = $db->sql_query($sql);
1075 $num_entries = (int) $db->sql_fetchfield('num_entries');
1076 $db->sql_freeresult($result);
1078 // Do not remove file if at least one additional entry with the same name exist.
1079 if (($entry_removed && $num_entries > 0) || (!$entry_removed && $num_entries > 1))
1081 return false;
1084 $filename = ($mode == 'thumbnail') ? 'thumb_' . basename($filename) : basename($filename);
1085 return @unlink(PHPBB_ROOT_PATH . $config['upload_path'] . '/' . $filename);
1089 * All-encompasing sync function
1091 * Exaples:
1092 * <code>
1093 * sync('topic', 'topic_id', 123); // resync topic #123
1094 * sync('topic', 'forum_id', array(2, 3)); // resync topics from forum #2 and #3
1095 * sync('topic'); // resync all topics
1096 * sync('topic', 'range', 'topic_id BETWEEN 1 AND 60'); // resync a range of topics/forums (only available for 'topic' and 'forum' modes)
1097 * </code>
1099 * Modes:
1100 * - forum Resync complete forum
1101 * - topic Resync topics
1102 * - topic_moved Removes topic shadows that would be in the same forum as the topic they link to
1103 * - topic_approved Resyncs the topic_approved flag according to the status of the first post
1104 * - post_reported Resyncs the post_reported flag, relying on actual reports
1105 * - topic_reported Resyncs the topic_reported flag, relying on post_reported flags
1106 * - post_attachement Same as post_reported, but with attachment flags
1107 * - topic_attachement Same as topic_reported, but with attachment flags
1109 function sync($mode, $where_type = '', $where_ids = '', $resync_parents = false, $sync_extra = false)
1111 global $db;
1113 if (is_array($where_ids))
1115 $where_ids = array_unique($where_ids);
1116 $where_ids = array_map('intval', $where_ids);
1118 else if ($where_type != 'range')
1120 $where_ids = ($where_ids) ? array((int) $where_ids) : array();
1123 if ($mode == 'forum' || $mode == 'topic' || $mode == 'topic_approved' || $mode == 'topic_reported' || $mode == 'post_reported')
1125 if (!$where_type)
1127 $where_sql = '';
1128 $where_sql_and = 'WHERE';
1130 else if ($where_type == 'range')
1132 // Only check a range of topics/forums. For instance: 'topic_id BETWEEN 1 AND 60'
1133 $where_sql = 'WHERE (' . $mode[0] . ".$where_ids)";
1134 $where_sql_and = $where_sql . "\n\tAND";
1136 else
1138 // Do not sync the "global forum"
1139 $where_ids = array_diff($where_ids, array(0));
1141 if (!sizeof($where_ids))
1143 // Empty array with IDs. This means that we don't have any work to do. Just return.
1144 return;
1147 // Limit the topics/forums we are syncing, use specific topic/forum IDs.
1148 // $where_type contains the field for the where clause (forum_id, topic_id)
1149 $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
1150 $where_sql_and = $where_sql . "\n\tAND";
1153 else
1155 if (!sizeof($where_ids))
1157 return;
1160 // $where_type contains the field for the where clause (forum_id, topic_id)
1161 $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
1162 $where_sql_and = $where_sql . "\n\tAND";
1165 switch ($mode)
1167 case 'topic_moved':
1168 if ($db->multi_table_deletion)
1170 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1171 USING ' . TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
1172 WHERE t1.topic_moved_id = t2.topic_id
1173 AND t1.forum_id = t2.forum_id";
1174 $db->sql_query($sql);
1176 else
1178 $sql = 'SELECT t1.topic_id
1179 FROM ' .TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
1180 WHERE t1.topic_moved_id = t2.topic_id
1181 AND t1.forum_id = t2.forum_id";
1182 $result = $db->sql_query($sql);
1184 $topic_id_ary = array();
1185 while ($row = $db->sql_fetchrow($result))
1187 $topic_id_ary[] = $row['topic_id'];
1189 $db->sql_freeresult($result);
1191 if (!sizeof($topic_id_ary))
1193 return;
1196 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1197 WHERE ' . $db->sql_in_set('topic_id', $topic_id_ary);
1198 $db->sql_query($sql);
1200 break;
1202 case 'topic_approved':
1203 if ($db->dbms_type == 'mysql')
1205 $sql = 'UPDATE ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
1206 SET t.topic_approved = p.post_approved
1207 $where_sql_and t.topic_first_post_id = p.post_id";
1208 $db->sql_query($sql);
1210 else
1212 $sql = 'SELECT t.topic_id, p.post_approved
1213 FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
1214 $where_sql_and p.post_id = t.topic_first_post_id
1215 AND p.post_approved <> t.topic_approved";
1216 $result = $db->sql_query($sql);
1218 $topic_ids = array();
1219 while ($row = $db->sql_fetchrow($result))
1221 $topic_ids[] = $row['topic_id'];
1223 $db->sql_freeresult($result);
1225 if (!sizeof($topic_ids))
1227 return;
1230 $sql = 'UPDATE ' . TOPICS_TABLE . '
1231 SET topic_approved = 1 - topic_approved
1232 WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1233 $db->sql_query($sql);
1235 break;
1237 case 'post_reported':
1238 $post_ids = $post_reported = array();
1240 $sql = 'SELECT p.post_id, p.post_reported
1241 FROM ' . POSTS_TABLE . " p
1242 $where_sql
1243 GROUP BY p.post_id, p.post_reported";
1244 $result = $db->sql_query($sql);
1246 while ($row = $db->sql_fetchrow($result))
1248 $post_ids[$row['post_id']] = $row['post_id'];
1249 if ($row['post_reported'])
1251 $post_reported[$row['post_id']] = 1;
1254 $db->sql_freeresult($result);
1256 $sql = 'SELECT DISTINCT(post_id)
1257 FROM ' . REPORTS_TABLE . '
1258 WHERE ' . $db->sql_in_set('post_id', $post_ids) . '
1259 AND report_closed = 0';
1260 $result = $db->sql_query($sql);
1262 $post_ids = array();
1263 while ($row = $db->sql_fetchrow($result))
1265 if (!isset($post_reported[$row['post_id']]))
1267 $post_ids[] = $row['post_id'];
1269 else
1271 unset($post_reported[$row['post_id']]);
1274 $db->sql_freeresult($result);
1276 // $post_reported should be empty by now, if it's not it contains
1277 // posts that are falsely flagged as reported
1278 foreach ($post_reported as $post_id => $void)
1280 $post_ids[] = $post_id;
1283 if (sizeof($post_ids))
1285 $sql = 'UPDATE ' . POSTS_TABLE . '
1286 SET post_reported = 1 - post_reported
1287 WHERE ' . $db->sql_in_set('post_id', $post_ids);
1288 $db->sql_query($sql);
1290 break;
1292 case 'topic_reported':
1293 if ($sync_extra)
1295 sync('post_reported', $where_type, $where_ids);
1298 $topic_ids = $topic_reported = array();
1300 $sql = 'SELECT DISTINCT(t.topic_id)
1301 FROM ' . POSTS_TABLE . " t
1302 $where_sql_and t.post_reported = 1";
1303 $result = $db->sql_query($sql);
1305 while ($row = $db->sql_fetchrow($result))
1307 $topic_reported[$row['topic_id']] = 1;
1309 $db->sql_freeresult($result);
1311 $sql = 'SELECT t.topic_id, t.topic_reported
1312 FROM ' . TOPICS_TABLE . " t
1313 $where_sql";
1314 $result = $db->sql_query($sql);
1316 while ($row = $db->sql_fetchrow($result))
1318 if ($row['topic_reported'] ^ isset($topic_reported[$row['topic_id']]))
1320 $topic_ids[] = $row['topic_id'];
1323 $db->sql_freeresult($result);
1325 if (sizeof($topic_ids))
1327 $sql = 'UPDATE ' . TOPICS_TABLE . '
1328 SET topic_reported = 1 - topic_reported
1329 WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1330 $db->sql_query($sql);
1332 break;
1334 case 'post_attachment':
1335 $post_ids = $post_attachment = array();
1337 $sql = 'SELECT p.post_id, p.post_attachment
1338 FROM ' . POSTS_TABLE . " p
1339 $where_sql
1340 GROUP BY p.post_id, p.post_attachment";
1341 $result = $db->sql_query($sql);
1343 while ($row = $db->sql_fetchrow($result))
1345 $post_ids[$row['post_id']] = $row['post_id'];
1346 if ($row['post_attachment'])
1348 $post_attachment[$row['post_id']] = 1;
1351 $db->sql_freeresult($result);
1353 $sql = 'SELECT DISTINCT(post_msg_id)
1354 FROM ' . ATTACHMENTS_TABLE . '
1355 WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
1356 AND in_message = 0';
1357 $result = $db->sql_query($sql);
1359 $post_ids = array();
1360 while ($row = $db->sql_fetchrow($result))
1362 if (!isset($post_attachment[$row['post_msg_id']]))
1364 $post_ids[] = $row['post_msg_id'];
1366 else
1368 unset($post_attachment[$row['post_msg_id']]);
1371 $db->sql_freeresult($result);
1373 // $post_attachment should be empty by now, if it's not it contains
1374 // posts that are falsely flagged as having attachments
1375 foreach ($post_attachment as $post_id => $void)
1377 $post_ids[] = $post_id;
1380 if (sizeof($post_ids))
1382 $sql = 'UPDATE ' . POSTS_TABLE . '
1383 SET post_attachment = 1 - post_attachment
1384 WHERE ' . $db->sql_in_set('post_id', $post_ids);
1385 $db->sql_query($sql);
1387 break;
1389 case 'topic_attachment':
1390 if ($sync_extra)
1392 sync('post_attachment', $where_type, $where_ids);
1395 $topic_ids = $topic_attachment = array();
1397 $sql = 'SELECT DISTINCT(t.topic_id)
1398 FROM ' . POSTS_TABLE . " t
1399 $where_sql_and t.post_attachment = 1";
1400 $result = $db->sql_query($sql);
1402 while ($row = $db->sql_fetchrow($result))
1404 $topic_attachment[$row['topic_id']] = 1;
1406 $db->sql_freeresult($result);
1408 $sql = 'SELECT t.topic_id, t.topic_attachment
1409 FROM ' . TOPICS_TABLE . " t
1410 $where_sql";
1411 $result = $db->sql_query($sql);
1413 while ($row = $db->sql_fetchrow($result))
1415 if ($row['topic_attachment'] ^ isset($topic_attachment[$row['topic_id']]))
1417 $topic_ids[] = $row['topic_id'];
1420 $db->sql_freeresult($result);
1422 if (sizeof($topic_ids))
1424 $sql = 'UPDATE ' . TOPICS_TABLE . '
1425 SET topic_attachment = 1 - topic_attachment
1426 WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1427 $db->sql_query($sql);
1429 break;
1431 case 'forum':
1433 // 1: Get the list of all forums
1434 $sql = 'SELECT f.*
1435 FROM ' . FORUMS_TABLE . " f
1436 $where_sql";
1437 $result = $db->sql_query($sql);
1439 $forum_data = $forum_ids = $post_ids = $last_post_id = $post_info = array();
1440 while ($row = $db->sql_fetchrow($result))
1442 if ($row['forum_type'] == FORUM_LINK)
1444 continue;
1447 $forum_id = (int) $row['forum_id'];
1448 $forum_ids[$forum_id] = $forum_id;
1450 $forum_data[$forum_id] = $row;
1451 if ($sync_extra)
1453 $forum_data[$forum_id]['posts'] = 0;
1454 $forum_data[$forum_id]['topics'] = 0;
1455 $forum_data[$forum_id]['topics_real'] = 0;
1457 $forum_data[$forum_id]['last_post_id'] = 0;
1458 $forum_data[$forum_id]['last_post_subject'] = '';
1459 $forum_data[$forum_id]['last_post_time'] = 0;
1460 $forum_data[$forum_id]['last_poster_id'] = 0;
1461 $forum_data[$forum_id]['last_poster_name'] = '';
1462 $forum_data[$forum_id]['last_poster_colour'] = '';
1464 $db->sql_freeresult($result);
1466 if (!sizeof($forum_ids))
1468 break;
1471 $forum_ids = array_values($forum_ids);
1473 // 2: Get topic counts for each forum (optional)
1474 if ($sync_extra)
1476 $sql = 'SELECT forum_id, topic_approved, COUNT(topic_id) AS forum_topics
1477 FROM ' . TOPICS_TABLE . '
1478 WHERE ' . $db->sql_in_set('forum_id', $forum_ids) . '
1479 GROUP BY forum_id, topic_approved';
1480 $result = $db->sql_query($sql);
1482 while ($row = $db->sql_fetchrow($result))
1484 $forum_id = (int) $row['forum_id'];
1485 $forum_data[$forum_id]['topics_real'] += $row['forum_topics'];
1487 if ($row['topic_approved'])
1489 $forum_data[$forum_id]['topics'] = $row['forum_topics'];
1492 $db->sql_freeresult($result);
1495 // 3: Get post count for each forum (optional)
1496 if ($sync_extra)
1498 if (sizeof($forum_ids) == 1)
1500 $sql = 'SELECT SUM(t.topic_replies + 1) AS forum_posts
1501 FROM ' . TOPICS_TABLE . ' t
1502 WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
1503 AND t.topic_approved = 1
1504 AND t.topic_status <> ' . ITEM_MOVED;
1506 else
1508 $sql = 'SELECT t.forum_id, SUM(t.topic_replies + 1) AS forum_posts
1509 FROM ' . TOPICS_TABLE . ' t
1510 WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
1511 AND t.topic_approved = 1
1512 AND t.topic_status <> ' . ITEM_MOVED . '
1513 GROUP BY t.forum_id';
1516 $result = $db->sql_query($sql);
1518 while ($row = $db->sql_fetchrow($result))
1520 $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
1522 $forum_data[$forum_id]['posts'] = (int) $row['forum_posts'];
1524 $db->sql_freeresult($result);
1527 // 4: Get last_post_id for each forum
1528 if (sizeof($forum_ids) == 1)
1530 $sql = 'SELECT MAX(t.topic_last_post_id) as last_post_id
1531 FROM ' . TOPICS_TABLE . ' t
1532 WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
1533 AND t.topic_approved = 1';
1535 else
1537 $sql = 'SELECT t.forum_id, MAX(t.topic_last_post_id) as last_post_id
1538 FROM ' . TOPICS_TABLE . ' t
1539 WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
1540 AND t.topic_approved = 1
1541 GROUP BY t.forum_id';
1544 $result = $db->sql_query($sql);
1546 while ($row = $db->sql_fetchrow($result))
1548 $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
1550 $forum_data[$forum_id]['last_post_id'] = (int) $row['last_post_id'];
1552 $post_ids[] = $row['last_post_id'];
1554 $db->sql_freeresult($result);
1556 // 5: Retrieve last_post infos
1557 if (sizeof($post_ids))
1559 $sql = 'SELECT p.post_id, p.poster_id, p.post_subject, p.post_time, p.post_username, u.username, u.user_colour
1560 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1561 WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
1562 AND p.poster_id = u.user_id';
1563 $result = $db->sql_query($sql);
1565 while ($row = $db->sql_fetchrow($result))
1567 $post_info[$row['post_id']] = $row;
1569 $db->sql_freeresult($result);
1571 foreach ($forum_data as $forum_id => $data)
1573 if ($data['last_post_id'])
1575 if (isset($post_info[$data['last_post_id']]))
1577 $forum_data[$forum_id]['last_post_subject'] = $post_info[$data['last_post_id']]['post_subject'];
1578 $forum_data[$forum_id]['last_post_time'] = $post_info[$data['last_post_id']]['post_time'];
1579 $forum_data[$forum_id]['last_poster_id'] = $post_info[$data['last_post_id']]['poster_id'];
1580 $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'];
1581 $forum_data[$forum_id]['last_poster_colour'] = $post_info[$data['last_post_id']]['user_colour'];
1583 else
1585 // For some reason we did not find the post in the db
1586 $forum_data[$forum_id]['last_post_id'] = 0;
1587 $forum_data[$forum_id]['last_post_subject'] = '';
1588 $forum_data[$forum_id]['last_post_time'] = 0;
1589 $forum_data[$forum_id]['last_poster_id'] = 0;
1590 $forum_data[$forum_id]['last_poster_name'] = '';
1591 $forum_data[$forum_id]['last_poster_colour'] = '';
1595 unset($post_info);
1598 // 6: Now do that thing
1599 $fieldnames = array('last_post_id', 'last_post_subject', 'last_post_time', 'last_poster_id', 'last_poster_name', 'last_poster_colour');
1601 if ($sync_extra)
1603 array_push($fieldnames, 'posts', 'topics', 'topics_real');
1606 foreach ($forum_data as $forum_id => $row)
1608 $sql_ary = array();
1610 foreach ($fieldnames as $fieldname)
1612 if ($row['forum_' . $fieldname] != $row[$fieldname])
1614 if (preg_match('#(name|colour|subject)$#', $fieldname))
1616 $sql_ary['forum_' . $fieldname] = (string) $row[$fieldname];
1618 else
1620 $sql_ary['forum_' . $fieldname] = (int) $row[$fieldname];
1625 if (sizeof($sql_ary))
1627 $sql = 'UPDATE ' . FORUMS_TABLE . '
1628 SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
1629 WHERE forum_id = ' . $forum_id;
1630 $db->sql_query($sql);
1633 break;
1635 case 'topic':
1636 $topic_data = $post_ids = $approved_unapproved_ids = $resync_forums = $delete_topics = $delete_posts = $moved_topics = array();
1638 $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
1639 FROM ' . TOPICS_TABLE . " t
1640 $where_sql";
1641 $result = $db->sql_query($sql);
1643 while ($row = $db->sql_fetchrow($result))
1645 if ($row['topic_moved_id'])
1647 $moved_topics[] = $row['topic_id'];
1648 continue;
1651 $topic_id = (int) $row['topic_id'];
1652 $topic_data[$topic_id] = $row;
1653 $topic_data[$topic_id]['replies_real'] = -1;
1654 $topic_data[$topic_id]['replies'] = 0;
1655 $topic_data[$topic_id]['first_post_id'] = 0;
1656 $topic_data[$topic_id]['last_post_id'] = 0;
1657 unset($topic_data[$topic_id]['topic_id']);
1659 // This array holds all topic_ids
1660 $delete_topics[$topic_id] = '';
1662 if ($sync_extra)
1664 $topic_data[$topic_id]['reported'] = 0;
1665 $topic_data[$topic_id]['attachment'] = 0;
1668 $db->sql_freeresult($result);
1670 // Use "t" as table alias because of the $where_sql clause
1671 // NOTE: 't.post_approved' in the GROUP BY is causing a major slowdown.
1672 $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
1673 FROM ' . POSTS_TABLE . " t
1674 $where_sql
1675 GROUP BY t.topic_id, t.post_approved";
1676 $result = $db->sql_query($sql);
1678 while ($row = $db->sql_fetchrow($result))
1680 $topic_id = (int) $row['topic_id'];
1682 $row['first_post_id'] = (int) $row['first_post_id'];
1683 $row['last_post_id'] = (int) $row['last_post_id'];
1685 if (!isset($topic_data[$topic_id]))
1687 // Hey, these posts come from a topic that does not exist
1688 $delete_posts[$topic_id] = '';
1690 else
1692 // Unset the corresponding entry in $delete_topics
1693 // When we'll be done, only topics with no posts will remain
1694 unset($delete_topics[$topic_id]);
1696 $topic_data[$topic_id]['replies_real'] += $row['total_posts'];
1697 $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']);
1699 if ($row['post_approved'] || !$topic_data[$topic_id]['last_post_id'])
1701 $topic_data[$topic_id]['replies'] = $row['total_posts'] - 1;
1702 $topic_data[$topic_id]['last_post_id'] = $row['last_post_id'];
1706 $db->sql_freeresult($result);
1708 foreach ($topic_data as $topic_id => $row)
1710 $post_ids[] = $row['first_post_id'];
1711 if ($row['first_post_id'] != $row['last_post_id'])
1713 $post_ids[] = $row['last_post_id'];
1717 // Now we delete empty topics and orphan posts
1718 if (sizeof($delete_posts))
1720 delete_posts('topic_id', array_keys($delete_posts), false);
1721 unset($delete_posts);
1724 if (!sizeof($topic_data))
1726 // If we get there, topic ids were invalid or topics did not contain any posts
1727 delete_topics($where_type, $where_ids, true);
1728 return;
1731 if (sizeof($delete_topics))
1733 $delete_topic_ids = array();
1734 foreach ($delete_topics as $topic_id => $void)
1736 unset($topic_data[$topic_id]);
1737 $delete_topic_ids[] = $topic_id;
1740 delete_topics('topic_id', $delete_topic_ids, false);
1741 unset($delete_topics, $delete_topic_ids);
1744 $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
1745 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1746 WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
1747 AND u.user_id = p.poster_id';
1748 $result = $db->sql_query($sql);
1750 $post_ids = array();
1751 while ($row = $db->sql_fetchrow($result))
1753 $topic_id = intval($row['topic_id']);
1755 if ($row['post_id'] == $topic_data[$topic_id]['first_post_id'])
1757 if ($topic_data[$topic_id]['topic_approved'] != $row['post_approved'])
1759 $approved_unapproved_ids[] = $topic_id;
1761 $topic_data[$topic_id]['time'] = $row['post_time'];
1762 $topic_data[$topic_id]['poster'] = $row['poster_id'];
1763 $topic_data[$topic_id]['first_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
1764 $topic_data[$topic_id]['first_poster_colour'] = $row['user_colour'];
1767 if ($row['post_id'] == $topic_data[$topic_id]['last_post_id'])
1769 $topic_data[$topic_id]['last_poster_id'] = $row['poster_id'];
1770 $topic_data[$topic_id]['last_post_subject'] = $row['post_subject'];
1771 $topic_data[$topic_id]['last_post_time'] = $row['post_time'];
1772 $topic_data[$topic_id]['last_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
1773 $topic_data[$topic_id]['last_poster_colour'] = $row['user_colour'];
1776 $db->sql_freeresult($result);
1778 // Make sure shadow topics do link to existing topics
1779 if (sizeof($moved_topics))
1781 $delete_topics = array();
1783 $sql = 'SELECT t1.topic_id, t1.topic_moved_id
1784 FROM ' . TOPICS_TABLE . ' t1
1785 LEFT JOIN ' . TOPICS_TABLE . ' t2 ON (t2.topic_id = t1.topic_moved_id)
1786 WHERE ' . $db->sql_in_set('t1.topic_id', $moved_topics) . '
1787 AND t2.topic_id IS NULL';
1788 $result = $db->sql_query($sql);
1790 while ($row = $db->sql_fetchrow($result))
1792 $delete_topics[] = $row['topic_id'];
1794 $db->sql_freeresult($result);
1796 if (sizeof($delete_topics))
1798 delete_topics('topic_id', $delete_topics, false);
1800 unset($delete_topics);
1802 // Make sure shadow topics having no last post data being updated (this only rarely happens...)
1803 $sql = 'SELECT topic_id, topic_moved_id, topic_last_post_id, topic_first_post_id
1804 FROM ' . TOPICS_TABLE . '
1805 WHERE ' . $db->sql_in_set('topic_id', $moved_topics) . '
1806 AND topic_last_post_time = 0';
1807 $result = $db->sql_query($sql);
1809 $shadow_topic_data = $post_ids = array();
1810 while ($row = $db->sql_fetchrow($result))
1812 $shadow_topic_data[$row['topic_moved_id']] = $row;
1813 $post_ids[] = $row['topic_last_post_id'];
1814 $post_ids[] = $row['topic_first_post_id'];
1816 $db->sql_freeresult($result);
1818 $sync_shadow_topics = array();
1819 if (sizeof($post_ids))
1821 $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
1822 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1823 WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
1824 AND u.user_id = p.poster_id';
1825 $result = $db->sql_query($sql);
1827 $post_ids = array();
1828 while ($row = $db->sql_fetchrow($result))
1830 $topic_id = (int) $row['topic_id'];
1832 // Ok, there should be a shadow topic. If there isn't, then there's something wrong with the db.
1833 // However, there's not much we can do about it.
1834 if (!empty($shadow_topic_data[$topic_id]))
1836 if ($row['post_id'] == $shadow_topic_data[$topic_id]['topic_first_post_id'])
1838 $orig_topic_id = $shadow_topic_data[$topic_id]['topic_id'];
1840 if (!isset($sync_shadow_topics[$orig_topic_id]))
1842 $sync_shadow_topics[$orig_topic_id] = array();
1845 $sync_shadow_topics[$orig_topic_id]['topic_time'] = $row['post_time'];
1846 $sync_shadow_topics[$orig_topic_id]['topic_poster'] = $row['poster_id'];
1847 $sync_shadow_topics[$orig_topic_id]['topic_first_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
1848 $sync_shadow_topics[$orig_topic_id]['topic_first_poster_colour'] = $row['user_colour'];
1851 if ($row['post_id'] == $shadow_topic_data[$topic_id]['topic_last_post_id'])
1853 $orig_topic_id = $shadow_topic_data[$topic_id]['topic_id'];
1855 if (!isset($sync_shadow_topics[$orig_topic_id]))
1857 $sync_shadow_topics[$orig_topic_id] = array();
1860 $sync_shadow_topics[$orig_topic_id]['topic_last_poster_id'] = $row['poster_id'];
1861 $sync_shadow_topics[$orig_topic_id]['topic_last_post_subject'] = $row['post_subject'];
1862 $sync_shadow_topics[$orig_topic_id]['topic_last_post_time'] = $row['post_time'];
1863 $sync_shadow_topics[$orig_topic_id]['topic_last_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
1864 $sync_shadow_topics[$orig_topic_id]['topic_last_poster_colour'] = $row['user_colour'];
1868 $db->sql_freeresult($result);
1870 $shadow_topic_data = array();
1872 // Update the information we collected
1873 if (sizeof($sync_shadow_topics))
1875 foreach ($sync_shadow_topics as $sync_topic_id => $sql_ary)
1877 $sql = 'UPDATE ' . TOPICS_TABLE . '
1878 SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
1879 WHERE topic_id = ' . $sync_topic_id;
1880 $db->sql_query($sql);
1885 unset($sync_shadow_topics, $shadow_topic_data);
1888 // approved becomes unapproved, and vice-versa
1889 if (sizeof($approved_unapproved_ids))
1891 $sql = 'UPDATE ' . TOPICS_TABLE . '
1892 SET topic_approved = 1 - topic_approved
1893 WHERE ' . $db->sql_in_set('topic_id', $approved_unapproved_ids);
1894 $db->sql_query($sql);
1896 unset($approved_unapproved_ids);
1898 // These are fields that will be synchronised
1899 $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');
1901 if ($sync_extra)
1903 // This routine assumes that post_reported values are correct
1904 // if they are not, use sync('post_reported') first
1905 $sql = 'SELECT t.topic_id, p.post_id
1906 FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
1907 $where_sql_and p.topic_id = t.topic_id
1908 AND p.post_reported = 1
1909 GROUP BY t.topic_id, p.post_id";
1910 $result = $db->sql_query($sql);
1912 $fieldnames[] = 'reported';
1913 while ($row = $db->sql_fetchrow($result))
1915 $topic_data[intval($row['topic_id'])]['reported'] = 1;
1917 $db->sql_freeresult($result);
1919 // This routine assumes that post_attachment values are correct
1920 // if they are not, use sync('post_attachment') first
1921 $sql = 'SELECT t.topic_id, p.post_id
1922 FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
1923 $where_sql_and p.topic_id = t.topic_id
1924 AND p.post_attachment = 1
1925 GROUP BY t.topic_id, p.post_id";
1926 $result = $db->sql_query($sql);
1928 $fieldnames[] = 'attachment';
1929 while ($row = $db->sql_fetchrow($result))
1931 $topic_data[intval($row['topic_id'])]['attachment'] = 1;
1933 $db->sql_freeresult($result);
1936 foreach ($topic_data as $topic_id => $row)
1938 $sql_ary = array();
1940 foreach ($fieldnames as $fieldname)
1942 if (isset($row[$fieldname]) && isset($row['topic_' . $fieldname]) && $row['topic_' . $fieldname] != $row[$fieldname])
1944 $sql_ary['topic_' . $fieldname] = $row[$fieldname];
1948 if (sizeof($sql_ary))
1950 $sql = 'UPDATE ' . TOPICS_TABLE . '
1951 SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
1952 WHERE topic_id = ' . $topic_id;
1953 $db->sql_query($sql);
1955 $resync_forums[$row['forum_id']] = $row['forum_id'];
1958 unset($topic_data);
1960 // if some topics have been resync'ed then resync parent forums
1961 // except when we're only syncing a range, we don't want to sync forums during
1962 // batch processing.
1963 if ($resync_parents && sizeof($resync_forums) && $where_type != 'range')
1965 sync('forum', 'forum_id', array_values($resync_forums), true, true);
1967 break;
1970 return;
1974 * Prune function
1976 function prune($forum_id, $prune_mode, $prune_date, $prune_flags = 0, $auto_sync = true)
1978 global $db;
1980 if (!is_array($forum_id))
1982 $forum_id = array($forum_id);
1985 if (!sizeof($forum_id))
1987 return;
1990 $sql_and = '';
1992 if (!($prune_flags & FORUM_FLAG_PRUNE_ANNOUNCE))
1994 $sql_and .= ' AND topic_type <> ' . POST_ANNOUNCE;
1997 if (!($prune_flags & FORUM_FLAG_PRUNE_STICKY))
1999 $sql_and .= ' AND topic_type <> ' . POST_STICKY;
2002 if ($prune_mode == 'posted')
2004 $sql_and .= " AND topic_last_post_time < $prune_date";
2007 if ($prune_mode == 'viewed')
2009 $sql_and .= " AND topic_last_view_time < $prune_date";
2012 $sql = 'SELECT topic_id
2013 FROM ' . TOPICS_TABLE . '
2014 WHERE ' . $db->sql_in_set('forum_id', $forum_id) . "
2015 AND poll_start = 0
2016 $sql_and";
2017 $result = $db->sql_query($sql);
2019 $topic_list = array();
2020 while ($row = $db->sql_fetchrow($result))
2022 $topic_list[] = $row['topic_id'];
2024 $db->sql_freeresult($result);
2026 if ($prune_flags & FORUM_FLAG_PRUNE_POLL)
2028 $sql = 'SELECT topic_id
2029 FROM ' . TOPICS_TABLE . '
2030 WHERE ' . $db->sql_in_set('forum_id', $forum_id) . "
2031 AND poll_start > 0
2032 AND poll_last_vote < $prune_date
2033 $sql_and";
2034 $result = $db->sql_query($sql);
2036 while ($row = $db->sql_fetchrow($result))
2038 $topic_list[] = $row['topic_id'];
2040 $db->sql_freeresult($result);
2042 $topic_list = array_unique($topic_list);
2045 return delete_topics('topic_id', $topic_list, $auto_sync, false);
2049 * Function auto_prune(), this function now relies on passed vars
2051 function auto_prune($forum_id, $prune_mode, $prune_flags, $prune_days, $prune_freq)
2053 global $db;
2055 $sql = 'SELECT forum_name
2056 FROM ' . FORUMS_TABLE . "
2057 WHERE forum_id = $forum_id";
2058 $result = $db->sql_query($sql, 3600);
2059 $row = $db->sql_fetchrow($result);
2060 $db->sql_freeresult($result);
2062 if ($row)
2064 $prune_date = time() - ($prune_days * 86400);
2065 $next_prune = time() + ($prune_freq * 86400);
2067 prune($forum_id, $prune_mode, $prune_date, $prune_flags, true);
2069 $sql = 'UPDATE ' . FORUMS_TABLE . "
2070 SET prune_next = $next_prune
2071 WHERE forum_id = $forum_id";
2072 $db->sql_query($sql);
2074 add_log('admin', 'LOG_AUTO_PRUNE', $row['forum_name']);
2077 return;
2081 * remove_comments will strip the sql comment lines out of an uploaded sql file
2082 * specifically for mssql and postgres type files in the install....
2084 function remove_comments(&$output)
2086 $lines = explode("\n", $output);
2087 $output = '';
2089 // try to keep mem. use down
2090 $linecount = sizeof($lines);
2092 $in_comment = false;
2093 for ($i = 0; $i < $linecount; $i++)
2095 if (trim($lines[$i]) == '/*')
2097 $in_comment = true;
2100 if (!$in_comment)
2102 $output .= $lines[$i] . "\n";
2105 if (trim($lines[$i]) == '*/')
2107 $in_comment = false;
2111 unset($lines);
2112 return $output;
2116 * Cache moderators, called whenever permissions are changed via admin_permissions. Changes of username
2117 * and group names must be carried through for the moderators table
2119 function cache_moderators()
2121 global $db, $cache, $auth;
2123 // Remove cached sql results
2124 $cache->destroy('sql', MODERATOR_CACHE_TABLE);
2126 // Clear table
2127 if ($db->truncate)
2129 $db->sql_query('TRUNCATE TABLE ' . MODERATOR_CACHE_TABLE);
2131 else
2133 $db->sql_query('DELETE FROM ' . MODERATOR_CACHE_TABLE);
2136 // We add moderators who have forum moderator permissions without an explicit ACL_NEVER setting
2137 $hold_ary = $ug_id_ary = $sql_ary = array();
2139 // Grab all users having moderative options...
2140 $hold_ary = $auth->acl_user_raw_data(false, 'm_%', false);
2142 // Add users?
2143 if (sizeof($hold_ary))
2145 // At least one moderative option warrants a display
2146 $ug_id_ary = array_keys($hold_ary);
2148 // Remove users who have group memberships with DENY moderator permissions
2149 $sql = $db->sql_build_query('SELECT', array(
2150 'SELECT' => 'a.forum_id, ug.user_id',
2152 'FROM' => array(
2153 ACL_OPTIONS_TABLE => 'o',
2154 USER_GROUP_TABLE => 'ug',
2155 ACL_GROUPS_TABLE => 'a'
2158 'LEFT_JOIN' => array(
2159 array(
2160 'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
2161 'ON' => 'a.auth_role_id = r.role_id'
2165 'WHERE' => '(o.auth_option_id = a.auth_option_id OR o.auth_option_id = r.auth_option_id)
2166 AND ((a.auth_setting = ' . phpbb::ACL_NEVER . ' AND r.auth_setting IS NULL)
2167 OR r.auth_setting = ' . phpbb::ACL_NEVER . ')
2168 AND a.group_id = ug.group_id
2169 AND ' . $db->sql_in_set('ug.user_id', $ug_id_ary) . "
2170 AND ug.user_pending = 0
2171 AND o.auth_option " . $db->sql_like_expression('m_' . $db->any_char),
2173 $result = $db->sql_query($sql);
2175 while ($row = $db->sql_fetchrow($result))
2177 if (isset($hold_ary[$row['user_id']][$row['forum_id']]))
2179 unset($hold_ary[$row['user_id']][$row['forum_id']]);
2182 $db->sql_freeresult($result);
2184 if (sizeof($hold_ary))
2186 // Get usernames...
2187 $sql = 'SELECT user_id, username
2188 FROM ' . USERS_TABLE . '
2189 WHERE ' . $db->sql_in_set('user_id', array_keys($hold_ary));
2190 $result = $db->sql_query($sql);
2192 $usernames_ary = array();
2193 while ($row = $db->sql_fetchrow($result))
2195 $usernames_ary[$row['user_id']] = $row['username'];
2198 foreach ($hold_ary as $user_id => $forum_id_ary)
2200 // Do not continue if user does not exist
2201 if (!isset($usernames_ary[$user_id]))
2203 continue;
2206 foreach ($forum_id_ary as $forum_id => $auth_ary)
2208 $sql_ary[] = array(
2209 'forum_id' => (int) $forum_id,
2210 'user_id' => (int) $user_id,
2211 'username' => (string) $usernames_ary[$user_id],
2212 'group_id' => 0,
2213 'group_name' => ''
2220 // Now to the groups...
2221 $hold_ary = $auth->acl_group_raw_data(false, 'm_%', false);
2223 if (sizeof($hold_ary))
2225 $ug_id_ary = array_keys($hold_ary);
2227 // Make sure not hidden or special groups are involved...
2228 $sql = 'SELECT group_name, group_id, group_type
2229 FROM ' . GROUPS_TABLE . '
2230 WHERE ' . $db->sql_in_set('group_id', $ug_id_ary);
2231 $result = $db->sql_query($sql);
2233 $groupnames_ary = array();
2234 while ($row = $db->sql_fetchrow($result))
2236 if ($row['group_type'] == GROUP_HIDDEN || $row['group_type'] == GROUP_SPECIAL)
2238 unset($hold_ary[$row['group_id']]);
2241 $groupnames_ary[$row['group_id']] = $row['group_name'];
2243 $db->sql_freeresult($result);
2245 foreach ($hold_ary as $group_id => $forum_id_ary)
2247 // If there is no group, we do not assign it...
2248 if (!isset($groupnames_ary[$group_id]))
2250 continue;
2253 foreach ($forum_id_ary as $forum_id => $auth_ary)
2255 $flag = false;
2256 foreach ($auth_ary as $auth_option => $setting)
2258 // Make sure at least one ACL_YES option is set...
2259 if ($setting == phpbb::ACL_YES)
2261 $flag = true;
2262 break;
2266 if (!$flag)
2268 continue;
2271 $sql_ary[] = array(
2272 'forum_id' => (int) $forum_id,
2273 'user_id' => 0,
2274 'username' => '',
2275 'group_id' => (int) $group_id,
2276 'group_name' => (string) $groupnames_ary[$group_id]
2282 $db->sql_multi_insert(MODERATOR_CACHE_TABLE, $sql_ary);
2286 * View log
2288 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')
2290 global $db, $user, $auth;
2292 $topic_id_list = $reportee_id_list = $is_auth = $is_mod = array();
2294 $profile_url = (defined('IN_ADMIN')) ? append_sid(PHPBB_ADMIN_PATH . 'index.' . PHP_EXT, 'i=users&amp;mode=overview') : append_sid('memberlist', 'mode=viewprofile');
2296 switch ($mode)
2298 case 'admin':
2299 $log_type = LOG_ADMIN;
2300 $sql_forum = '';
2301 break;
2303 case 'mod':
2304 $log_type = LOG_MOD;
2306 if ($topic_id)
2308 $sql_forum = 'AND l.topic_id = ' . intval($topic_id);
2310 else if (is_array($forum_id))
2312 $sql_forum = 'AND ' . $db->sql_in_set('l.forum_id', array_map('intval', $forum_id));
2314 else
2316 $sql_forum = ($forum_id) ? 'AND l.forum_id = ' . intval($forum_id) : '';
2318 break;
2320 case 'user':
2321 $log_type = LOG_USERS;
2322 $sql_forum = 'AND l.reportee_id = ' . (int) $user_id;
2323 break;
2325 case 'users':
2326 $log_type = LOG_USERS;
2327 $sql_forum = '';
2328 break;
2330 case 'critical':
2331 $log_type = LOG_CRITICAL;
2332 $sql_forum = '';
2333 break;
2335 default:
2336 return;
2339 $sql = "SELECT l.*, u.username, u.username_clean, u.user_colour
2340 FROM " . LOG_TABLE . " l, " . USERS_TABLE . " u
2341 WHERE l.log_type = $log_type
2342 AND u.user_id = l.user_id
2343 " . (($limit_days) ? "AND l.log_time >= $limit_days" : '') . "
2344 $sql_forum
2345 ORDER BY $sort_by";
2346 $result = $db->sql_query_limit($sql, $limit, $offset);
2348 $i = 0;
2349 $log = array();
2350 while ($row = $db->sql_fetchrow($result))
2352 if ($row['topic_id'])
2354 $topic_id_list[] = $row['topic_id'];
2357 if ($row['reportee_id'])
2359 $reportee_id_list[] = $row['reportee_id'];
2362 $log[$i] = array(
2363 'id' => $row['log_id'],
2365 'reportee_id' => $row['reportee_id'],
2366 'reportee_username' => '',
2367 'reportee_username_full'=> '',
2369 'user_id' => $row['user_id'],
2370 'username' => $row['username'],
2371 'username_full' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], false, $profile_url),
2373 'ip' => $row['log_ip'],
2374 'time' => $row['log_time'],
2375 'forum_id' => $row['forum_id'],
2376 'topic_id' => $row['topic_id'],
2378 'viewforum' => ($row['forum_id'] && $auth->acl_get('f_read', $row['forum_id'])) ? append_sid('viewforum', 'f=' . $row['forum_id']) : false,
2379 'action' => (isset($user->lang[$row['log_operation']])) ? $user->lang[$row['log_operation']] : '{' . ucfirst(str_replace('_', ' ', $row['log_operation'])) . '}',
2382 if (!empty($row['log_data']))
2384 $log_data_ary = unserialize($row['log_data']);
2386 if (isset($user->lang[$row['log_operation']]))
2388 // Check if there are more occurrences of % than arguments, if there are we fill out the arguments array
2389 // It doesn't matter if we add more arguments than placeholders
2390 if ((substr_count($log[$i]['action'], '%') - sizeof($log_data_ary)) > 0)
2392 $log_data_ary = array_merge($log_data_ary, array_fill(0, substr_count($log[$i]['action'], '%') - sizeof($log_data_ary), ''));
2395 $log[$i]['action'] = vsprintf($log[$i]['action'], $log_data_ary);
2397 // If within the admin panel we do not censor text out
2398 if (defined('IN_ADMIN'))
2400 $log[$i]['action'] = bbcode_nl2br($log[$i]['action']);
2402 else
2404 $log[$i]['action'] = bbcode_nl2br(censor_text($log[$i]['action']));
2407 else
2409 $log[$i]['action'] .= '<br />' . implode('', $log_data_ary);
2412 /* Apply make_clickable... has to be seen if it is for good. :/
2413 // Seems to be not for the moment, reconsider later...
2414 $log[$i]['action'] = make_clickable($log[$i]['action']);
2418 $i++;
2420 $db->sql_freeresult($result);
2422 if (sizeof($topic_id_list))
2424 $topic_id_list = array_unique($topic_id_list);
2426 // This query is not really needed if move_topics() updates the forum_id field,
2427 // although it's also used to determine if the topic still exists in the database
2428 $sql = 'SELECT topic_id, forum_id
2429 FROM ' . TOPICS_TABLE . '
2430 WHERE ' . $db->sql_in_set('topic_id', array_map('intval', $topic_id_list));
2431 $result = $db->sql_query($sql);
2433 $default_forum_id = 0;
2435 while ($row = $db->sql_fetchrow($result))
2437 if (!$row['forum_id'])
2439 if ($auth->acl_getf_global('f_read'))
2441 if (!$default_forum_id)
2443 $sql = 'SELECT forum_id
2444 FROM ' . FORUMS_TABLE . '
2445 WHERE forum_type = ' . FORUM_POST;
2446 $f_result = $db->sql_query_limit($sql, 1);
2447 $default_forum_id = (int) $db->sql_fetchfield('forum_id', $f_result);
2448 $db->sql_freeresult($f_result);
2451 $is_auth[$row['topic_id']] = $default_forum_id;
2454 else
2456 if ($auth->acl_get('f_read', $row['forum_id']))
2458 $is_auth[$row['topic_id']] = $row['forum_id'];
2462 if ($auth->acl_gets('a_', 'm_', $row['forum_id']))
2464 $is_mod[$row['topic_id']] = $row['forum_id'];
2467 $db->sql_freeresult($result);
2469 foreach ($log as $key => $row)
2471 $log[$key]['viewtopic'] = (isset($is_auth[$row['topic_id']])) ? append_sid('viewtopic', 'f=' . $is_auth[$row['topic_id']] . '&amp;t=' . $row['topic_id']) : false;
2472 $log[$key]['viewlogs'] = (isset($is_mod[$row['topic_id']])) ? append_sid('mcp', 'i=logs&amp;mode=topic_logs&amp;t=' . $row['topic_id'], true, $user->session_id) : false;
2476 if (sizeof($reportee_id_list))
2478 $reportee_id_list = array_unique($reportee_id_list);
2479 $reportee_names_list = array();
2481 $sql = 'SELECT user_id, username, user_colour
2482 FROM ' . USERS_TABLE . '
2483 WHERE ' . $db->sql_in_set('user_id', $reportee_id_list);
2484 $result = $db->sql_query($sql);
2486 while ($row = $db->sql_fetchrow($result))
2488 $reportee_names_list[$row['user_id']] = $row;
2490 $db->sql_freeresult($result);
2492 foreach ($log as $key => $row)
2494 if (!isset($reportee_names_list[$row['reportee_id']]))
2496 continue;
2499 $log[$key]['reportee_username'] = $reportee_names_list[$row['reportee_id']]['username'];
2500 $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);
2504 $sql = 'SELECT COUNT(l.log_id) AS total_entries
2505 FROM ' . LOG_TABLE . " l
2506 WHERE l.log_type = $log_type
2507 AND l.log_time >= $limit_days
2508 $sql_forum";
2509 $result = $db->sql_query($sql);
2510 $log_count = (int) $db->sql_fetchfield('total_entries');
2511 $db->sql_freeresult($result);
2513 return;
2517 * Update foes - remove moderators and administrators from foe lists...
2519 function update_foes($group_id = false, $user_id = false)
2521 global $db, $auth;
2523 // update foes for some user
2524 if (is_array($user_id) && sizeof($user_id))
2526 $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
2527 WHERE ' . $db->sql_in_set('zebra_id', $user_id) . '
2528 AND foe = 1';
2529 $db->sql_query($sql);
2530 return;
2533 // update foes for some group
2534 if (is_array($group_id) && sizeof($group_id))
2536 // Grab group settings...
2537 $sql = $db->sql_build_query('SELECT', array(
2538 'SELECT' => 'a.group_id',
2540 'FROM' => array(
2541 ACL_OPTIONS_TABLE => 'ao',
2542 ACL_GROUPS_TABLE => 'a'
2545 'LEFT_JOIN' => array(
2546 array(
2547 'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
2548 'ON' => 'a.auth_role_id = r.role_id'
2552 'WHERE' => '(ao.auth_option_id = a.auth_option_id OR ao.auth_option_id = r.auth_option_id)
2553 AND ' . $db->sql_in_set('a.group_id', $group_id) . "
2554 AND ao.auth_option IN ('a_', 'm_')",
2556 'GROUP_BY' => 'a.group_id'
2558 $result = $db->sql_query($sql);
2560 $groups = array();
2561 while ($row = $db->sql_fetchrow($result))
2563 $groups[] = (int) $row['group_id'];
2565 $db->sql_freeresult($result);
2567 if (!sizeof($groups))
2569 return;
2572 if ($db->multi_table_deletion)
2574 $sql = 'DELETE FROM' . ZEBRA_TABLE . ' z
2575 USING ' . USER_GROUP_TABLE . ' ug
2576 WHERE z.zebra_id = ug.user_id
2577 AND z.foe = 1
2578 AND ' . $db->sql_in_set('ug.group_id', $groups);
2579 $db->sql_query($sql);
2581 else
2583 $sql = 'SELECT user_id
2584 FROM ' . USER_GROUP_TABLE . '
2585 WHERE ' . $db->sql_in_set('group_id', $groups);
2586 $result = $db->sql_query($sql);
2588 $users = array();
2589 while ($row = $db->sql_fetchrow($result))
2591 $users[] = (int) $row['user_id'];
2593 $db->sql_freeresult($result);
2595 if (sizeof($users))
2597 $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
2598 WHERE ' . $db->sql_in_set('zebra_id', $users) . '
2599 AND foe = 1';
2600 $db->sql_query($sql);
2604 return;
2607 // update foes for everyone
2608 $perms = array();
2609 foreach ($auth->acl_get_list(false, array('a_', 'm_'), false) as $forum_id => $forum_ary)
2611 foreach ($forum_ary as $auth_option => $user_ary)
2613 $perms = array_merge($perms, $user_ary);
2617 if (sizeof($perms))
2619 $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
2620 WHERE ' . $db->sql_in_set('zebra_id', array_unique($perms)) . '
2621 AND foe = 1';
2622 $db->sql_query($sql);
2624 unset($perms);
2628 * Lists inactive users
2630 function view_inactive_users(&$users, &$user_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'user_inactive_time DESC')
2632 global $db, $user;
2634 $sql = 'SELECT COUNT(user_id) AS user_count
2635 FROM ' . USERS_TABLE . '
2636 WHERE user_type = ' . phpbb::USER_INACTIVE .
2637 (($limit_days) ? " AND user_inactive_time >= $limit_days" : '');
2638 $result = $db->sql_query($sql);
2639 $user_count = (int) $db->sql_fetchfield('user_count');
2640 $db->sql_freeresult($result);
2642 if ($offset >= $user_count)
2644 $offset = ($offset - $limit < 0) ? 0 : $offset - $limit;
2647 $sql = 'SELECT user_id, username, user_regdate, user_lastvisit, user_inactive_time, user_inactive_reason
2648 FROM ' . USERS_TABLE . '
2649 WHERE user_type = ' . phpbb::USER_INACTIVE .
2650 (($limit_days) ? " AND user_inactive_time >= $limit_days" : '') . "
2651 ORDER BY $sort_by";
2652 $result = $db->sql_query_limit($sql, $limit, $offset);
2654 while ($row = $db->sql_fetchrow($result))
2656 $row['inactive_reason'] = $user->lang['INACTIVE_REASON_UNKNOWN'];
2657 switch ($row['user_inactive_reason'])
2659 case INACTIVE_REGISTER:
2660 $row['inactive_reason'] = $user->lang['INACTIVE_REASON_REGISTER'];
2661 break;
2663 case INACTIVE_PROFILE:
2664 $row['inactive_reason'] = $user->lang['INACTIVE_REASON_PROFILE'];
2665 break;
2667 case INACTIVE_MANUAL:
2668 $row['inactive_reason'] = $user->lang['INACTIVE_REASON_MANUAL'];
2669 break;
2671 case INACTIVE_REMIND:
2672 $row['inactive_reason'] = $user->lang['INACTIVE_REASON_REMIND'];
2673 break;
2676 $users[] = $row;
2679 return $offset;
2683 * Lists warned users
2685 function view_warned_users(&$users, &$user_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'user_warnings DESC')
2687 global $db;
2689 $sql = 'SELECT user_id, username, user_colour, user_warnings, user_last_warning
2690 FROM ' . USERS_TABLE . '
2691 WHERE user_warnings > 0
2692 ' . (($limit_days) ? "AND user_last_warning >= $limit_days" : '') . "
2693 ORDER BY $sort_by";
2694 $result = $db->sql_query_limit($sql, $limit, $offset);
2695 $users = $db->sql_fetchrowset($result);
2696 $db->sql_freeresult($result);
2698 $sql = 'SELECT count(user_id) AS user_count
2699 FROM ' . USERS_TABLE . '
2700 WHERE user_warnings > 0
2701 ' . (($limit_days) ? "AND user_last_warning >= $limit_days" : '');
2702 $result = $db->sql_query($sql);
2703 $user_count = (int) $db->sql_fetchfield('user_count');
2704 $db->sql_freeresult($result);
2706 return;
2710 * Get database size
2712 function get_database_size()
2714 global $db, $user, $table_prefix;
2716 $database_size = false;
2718 // This code is heavily influenced by a similar routine in phpMyAdmin 2.2.0
2719 switch ($db->dbms_type)
2721 case 'mysql':
2722 $sql = 'SELECT VERSION() AS mysql_version';
2723 $result = $db->sql_query($sql);
2724 $row = $db->sql_fetchrow($result);
2725 $db->sql_freeresult($result);
2727 if ($row)
2729 $version = $row['mysql_version'];
2731 if (preg_match('#(3\.23|[45]\.)#', $version))
2733 $db_name = (preg_match('#^(?:3\.23\.(?:[6-9]|[1-9]{2}))|[45]\.#', $version)) ? "`{$db->dbname}`" : $db->dbname;
2735 $sql = 'SHOW TABLE STATUS
2736 FROM ' . $db_name;
2737 $result = $db->sql_query($sql, 7200);
2739 $database_size = 0;
2740 while ($row = $db->sql_fetchrow($result))
2742 if ((isset($row['Type']) && $row['Type'] != 'MRG_MyISAM') || (isset($row['Engine']) && ($row['Engine'] == 'MyISAM' || $row['Engine'] == 'InnoDB')))
2744 if ($table_prefix != '')
2746 if (strpos($row['Name'], $table_prefix) !== false)
2748 $database_size += $row['Data_length'] + $row['Index_length'];
2751 else
2753 $database_size += $row['Data_length'] + $row['Index_length'];
2757 $db->sql_freeresult($result);
2760 break;
2762 case 'firebird':
2763 global $dbname;
2765 // if it on the local machine, we can get lucky
2766 if (file_exists($dbname))
2768 $database_size = filesize($dbname);
2771 break;
2773 case 'sqlite':
2774 global $dbhost;
2776 if (file_exists($dbhost))
2778 $database_size = filesize($dbhost);
2781 break;
2783 case 'mssql':
2784 $sql = 'SELECT ((SUM(size) * 8.0) * 1024.0) as dbsize
2785 FROM sysfiles';
2786 $result = $db->sql_query($sql, 7200);
2787 $database_size = ($row = $db->sql_fetchrow($result)) ? $row['dbsize'] : false;
2788 $db->sql_freeresult($result);
2789 break;
2791 case 'postgres':
2792 $sql = "SELECT proname
2793 FROM pg_proc
2794 WHERE proname = 'pg_database_size'";
2795 $result = $db->sql_query($sql);
2796 $row = $db->sql_fetchrow($result);
2797 $db->sql_freeresult($result);
2799 if ($row['proname'] == 'pg_database_size')
2801 $database = $db->dbname;
2802 if (strpos($database, '.') !== false)
2804 list($database, ) = explode('.', $database);
2807 $sql = "SELECT oid
2808 FROM pg_database
2809 WHERE datname = '$database'";
2810 $result = $db->sql_query($sql);
2811 $row = $db->sql_fetchrow($result);
2812 $db->sql_freeresult($result);
2814 $oid = $row['oid'];
2816 $sql = 'SELECT pg_database_size(' . $oid . ') as size';
2817 $result = $db->sql_query($sql);
2818 $row = $db->sql_fetchrow($result);
2819 $db->sql_freeresult($result);
2821 $database_size = $row['size'];
2823 break;
2825 case 'oracle':
2826 $sql = 'SELECT SUM(bytes) as dbsize
2827 FROM user_segments';
2828 $result = $db->sql_query($sql, 7200);
2829 $database_size = ($row = $db->sql_fetchrow($result)) ? $row['dbsize'] : false;
2830 $db->sql_freeresult($result);
2831 break;
2834 $database_size = ($database_size !== false) ? get_formatted_filesize($database_size) : $user->lang['NOT_AVAILABLE'];
2836 return $database_size;
2840 * Retrieve contents from remotely stored file
2842 function get_remote_file($host, $directory, $filename, &$errstr, &$errno, $port = 80, $timeout = 10)
2844 global $user;
2846 if ($fsock = @fsockopen($host, $port, $errno, $errstr, $timeout))
2848 @fputs($fsock, "GET $directory/$filename HTTP/1.1\r\n");
2849 @fputs($fsock, "HOST: $host\r\n");
2850 @fputs($fsock, "Connection: close\r\n\r\n");
2852 $file_info = '';
2853 $get_info = false;
2855 while (!@feof($fsock))
2857 if ($get_info)
2859 $file_info .= @fread($fsock, 1024);
2861 else
2863 $line = @fgets($fsock, 1024);
2864 if ($line == "\r\n")
2866 $get_info = true;
2868 else if (stripos($line, '404 not found') !== false)
2870 $errstr = $user->lang['FILE_NOT_FOUND'] . ': ' . $filename;
2871 return false;
2875 @fclose($fsock);
2877 else
2879 if ($errstr)
2881 $errstr = utf8_convert_message($errstr);
2882 return false;
2884 else
2886 $errstr = $user->lang['FSOCK_DISABLED'];
2887 return false;
2891 return $file_info;
2895 * Tidy Warnings
2896 * Remove all warnings which have now expired from the database
2897 * The duration of a warning can be defined by the administrator
2898 * This only removes the warning and reduces the associated count,
2899 * it does not remove the user note recording the contents of the warning
2901 function tidy_warnings()
2903 global $db, $config;
2905 $expire_date = time() - ($config['warnings_expire_days'] * 86400);
2906 $warning_list = $user_list = array();
2908 $sql = 'SELECT * FROM ' . WARNINGS_TABLE . "
2909 WHERE warning_time < $expire_date";
2910 $result = $db->sql_query($sql);
2912 while ($row = $db->sql_fetchrow($result))
2914 $warning_list[] = $row['warning_id'];
2915 $user_list[$row['user_id']] = isset($user_list[$row['user_id']]) ? ++$user_list[$row['user_id']] : 1;
2917 $db->sql_freeresult($result);
2919 if (sizeof($warning_list))
2921 $db->sql_transaction('begin');
2923 $sql = 'DELETE FROM ' . WARNINGS_TABLE . '
2924 WHERE ' . $db->sql_in_set('warning_id', $warning_list);
2925 $db->sql_query($sql);
2927 foreach ($user_list as $user_id => $value)
2929 $sql = 'UPDATE ' . USERS_TABLE . " SET user_warnings = user_warnings - $value
2930 WHERE user_id = $user_id";
2931 $db->sql_query($sql);
2934 $db->sql_transaction('commit');
2937 set_config('warnings_last_gc', time(), true);
2941 * Tidy database, doing some maintanance tasks
2943 function tidy_database()
2945 global $db;
2947 // Here we check permission consistency
2949 // Sometimes, it can happen permission tables having forums listed which do not exist
2950 $sql = 'SELECT forum_id
2951 FROM ' . FORUMS_TABLE;
2952 $result = $db->sql_query($sql);
2954 $forum_ids = array(0);
2955 while ($row = $db->sql_fetchrow($result))
2957 $forum_ids[] = $row['forum_id'];
2959 $db->sql_freeresult($result);
2961 // Delete those rows from the acl tables not having listed the forums above
2962 $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
2963 WHERE ' . $db->sql_in_set('forum_id', $forum_ids, true);
2964 $db->sql_query($sql);
2966 $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
2967 WHERE ' . $db->sql_in_set('forum_id', $forum_ids, true);
2968 $db->sql_query($sql);
2970 set_config('database_last_gc', time(), true);
2974 * Add permission language - this will make sure custom files will be included
2976 function add_permission_language()
2978 global $user;
2980 // First of all, our own file. We need to include it as the first file because it presets all relevant variables.
2981 $user->add_lang('acp/permissions_phpbb');
2983 $files_to_add = array();
2985 // Now search in acp and mods folder for permissions_ files.
2986 foreach (array('acp/', 'mods/') as $path)
2988 $dh = @opendir($user->lang_path . $user->lang_name . '/' . $path);
2990 if ($dh)
2992 while (($file = readdir($dh)) !== false)
2994 if ($file !== 'permissions_phpbb.' . PHP_EXT && strpos($file, 'permissions_') === 0 && substr($file, -(strlen(PHP_EXT) + 1)) === '.' . PHP_EXT)
2996 $files_to_add[] = $path . substr($file, 0, -(strlen(PHP_EXT) + 1));
2999 closedir($dh);
3003 if (!sizeof($files_to_add))
3005 return false;
3008 $user->add_lang($files_to_add);
3009 return true;