function th_move($id, $to_forum, $root_msg_id, $forum_id, $last_post_date, $last_post_id)
{
    if (!db_locked()) {
        db_lock('phpgw_fud_poll WRITE, phpgw_fud_thread_view WRITE, phpgw_fud_thread WRITE, phpgw_fud_forum WRITE, phpgw_fud_msg WRITE');
        $ll = 1;
    }
    $msg_count = q_singleval("SELECT count(*) FROM phpgw_fud_thread LEFT JOIN phpgw_fud_msg ON phpgw_fud_msg.thread_id=phpgw_fud_thread.id WHERE phpgw_fud_msg.apr=1 AND phpgw_fud_thread.id=" . $id);
    q('UPDATE phpgw_fud_thread SET forum_id=' . $to_forum . ' WHERE id=' . $id);
    q('UPDATE phpgw_fud_forum SET post_count=post_count-' . $msg_count . ' WHERE id=' . $forum_id);
    q('UPDATE phpgw_fud_forum SET thread_count=thread_count+1,post_count=post_count+' . $msg_count . ' WHERE id=' . $to_forum);
    q('DELETE FROM phpgw_fud_thread WHERE forum_id=' . $to_forum . ' AND root_msg_id=' . $root_msg_id . ' AND moved_to=' . $forum_id);
    if ($aff_rows = db_affected()) {
        q('UPDATE phpgw_fud_forum SET thread_count=thread_count-' . $aff_rows . ' WHERE id=' . $to_forum);
    }
    q('UPDATE phpgw_fud_thread SET moved_to=' . $to_forum . ' WHERE id!=' . $id . ' AND root_msg_id=' . $root_msg_id);
    q('INSERT INTO phpgw_fud_thread
		(forum_id, root_msg_id, last_post_date, last_post_id, moved_to)
	VALUES
		(' . $forum_id . ', ' . $root_msg_id . ', ' . $last_post_date . ', ' . $last_post_id . ', ' . $to_forum . ')');
    rebuild_forum_view($forum_id);
    rebuild_forum_view($to_forum);
    $c = q('SELECT poll_id FROM phpgw_fud_msg WHERE thread_id=' . $id . ' AND apr=1 AND poll_id>0');
    while ($r = db_rowarr($c)) {
        $p[] = $r[0];
    }
    unset($c);
    if (isset($p)) {
        q('UPDATE phpgw_fud_poll SET forum_id=' . $to_forum . ' WHERE id IN(' . implode(',', $p) . ')');
    }
    if (isset($ll)) {
        db_unlock();
    }
}
    function approve($id, $unlock_safe = false)
    {
        /* fetch info about the message, poll (if one exists), thread & forum */
        $mtf = db_sab('SELECT
					m.id, m.poster_id, m.apr, m.subject, m.foff, m.length, m.file_id, m.thread_id, m.poll_id, m.attach_cnt,
					m.post_stamp, m.reply_to, m.mlist_msg_id,
					t.forum_id, t.last_post_id, t.root_msg_id, t.last_post_date,
					m2.post_stamp AS frm_last_post_date,
					f.name AS frm_name,
					u.alias, u.email, u.sig,
					n.id AS nntp_id, ml.id AS mlist_id
				FROM phpgw_fud_msg m
				INNER JOIN phpgw_fud_thread t ON m.thread_id=t.id
				INNER JOIN phpgw_fud_forum f ON t.forum_id=f.id
				LEFT JOIN phpgw_fud_msg m2 ON f.last_post_id=m2.id
				LEFT JOIN phpgw_fud_users u ON m.poster_id=u.id
				LEFT JOIN phpgw_fud_mlist ml ON ml.forum_id=f.id
				LEFT JOIN phpgw_fud_nntp n ON n.forum_id=f.id
				WHERE m.id=' . $id . ' AND m.apr=0');
        /* nothing to do or bad message id */
        if (!$mtf) {
            return;
        }
        if ($mtf->alias) {
            reverse_fmt($mtf->alias);
        } else {
            $mtf->alias = $GLOBALS['ANON_NICK'];
        }
        if (!db_locked()) {
            db_lock('phpgw_fud_thread_view WRITE, phpgw_fud_level WRITE, phpgw_fud_users WRITE, phpgw_fud_forum WRITE, phpgw_fud_thread WRITE, phpgw_fud_msg WRITE');
            $ll = 1;
        }
        q("UPDATE phpgw_fud_msg SET apr=1 WHERE id=" . $mtf->id);
        if ($mtf->poster_id) {
            user_set_post_count($mtf->poster_id);
        }
        $last_post_id = $mtf->post_stamp > $mtf->frm_last_post_date ? $mtf->id : 0;
        if ($mtf->root_msg_id == $mtf->id) {
            /* new thread */
            rebuild_forum_view($mtf->forum_id);
            $threads = 1;
        } else {
            /* reply to thread */
            if ($mtf->post_stamp > $mtf->last_post_date) {
                th_inc_post_count($mtf->thread_id, 1, $mtf->id, $mtf->post_stamp);
            } else {
                th_inc_post_count($mtf->thread_id, 1);
            }
            rebuild_forum_view($mtf->forum_id, q_singleval('SELECT page FROM phpgw_fud_thread_view WHERE forum_id=' . $mtf->forum_id . ' AND thread_id=' . $mtf->thread_id));
            $threads = 0;
        }
        /* update forum thread & post count as well as last_post_id field */
        frm_updt_counts($mtf->forum_id, 1, $threads, $last_post_id);
        if ($unlock_safe || isset($ll)) {
            db_unlock();
        }
        if ($mtf->poll_id) {
            poll_activate($mtf->poll_id, $mtf->forum_id);
        }
        $mtf->body = read_msg_body($mtf->foff, $mtf->length, $mtf->file_id);
        if ($GLOBALS['FUD_OPT_1'] & 16777216) {
            index_text(preg_match('!Re: !i', $mtf->subject) ? '' : $mtf->subject, $mtf->body, $mtf->id);
        }
        /* handle notifications */
        if ($mtf->root_msg_id == $mtf->id) {
            if (empty($mtf->frm_last_post_date)) {
                $mtf->frm_last_post_date = 0;
            }
            /* send new thread notifications to forum subscribers */
            $c = uq('SELECT u.email, u.icq, u.users_opt
					FROM phpgw_fud_forum_notify fn
					INNER JOIN phpgw_fud_users u ON fn.user_id=u.id
					LEFT JOIN phpgw_fud_forum_read r ON r.forum_id=fn.forum_id AND r.user_id=fn.user_id
					INNER JOIN phpgw_fud_group_cache g1 ON g1.user_id=2147483647 AND g1.resource_id=' . $mtf->forum_id . '
					LEFT JOIN phpgw_fud_group_cache g2 ON g2.user_id=fn.user_id AND g2.resource_id=' . $mtf->forum_id . '
				WHERE
					fn.forum_id=' . $mtf->forum_id . ' AND fn.user_id!=' . (int) $mtf->poster_id . '
					AND (CASE WHEN (r.last_view IS NULL AND (u.last_read=0 OR u.last_read >= ' . $mtf->frm_last_post_date . ')) OR r.last_view > ' . $mtf->frm_last_post_date . ' THEN 1 ELSE 0 END)=1
					AND ((CASE WHEN g2.id IS NOT NULL THEN g2.group_cache_opt ELSE g1.group_cache_opt END) & 2) > 0');
            $notify_type = 'frm';
        } else {
            /* send new reply notifications to thread subscribers */
            $c = uq('SELECT u.email, u.icq, u.users_opt, r.msg_id, u.id
					FROM phpgw_fud_thread_notify tn
					INNER JOIN phpgw_fud_users u ON tn.user_id=u.id
					LEFT JOIN phpgw_fud_read r ON r.thread_id=tn.thread_id AND r.user_id=tn.user_id
					INNER JOIN phpgw_fud_group_cache g1 ON g1.user_id=2147483647 AND g1.resource_id=' . $mtf->forum_id . '
					LEFT JOIN phpgw_fud_group_cache g2 ON g2.user_id=tn.user_id AND g2.resource_id=' . $mtf->forum_id . '
				WHERE
					tn.thread_id=' . $mtf->thread_id . ' AND tn.user_id!=' . (int) $mtf->poster_id . '
					AND (r.msg_id=' . $mtf->last_post_id . ' OR (r.msg_id IS NULL AND ' . $mtf->post_stamp . ' > u.last_read))
					AND ((CASE WHEN g2.id IS NOT NULL THEN g2.group_cache_opt ELSE g1.group_cache_opt END) & 2) > 0');
            $notify_type = 'thr';
        }
        while ($r = db_rowarr($c)) {
            if ($r[2] & 16) {
                $to['EMAIL'] = $r[0];
            } else {
                $to['ICQ'] = $r[1] . '@pager.icq.com';
            }
            if (isset($r[4]) && is_null($r[3])) {
                $tl[] = $r[4];
            }
        }
        unset($c);
        if (isset($tl)) {
            /* this allows us to mark the message we are sending notification about as read, so that we do not re-notify the user
             * until this message is read.
             */
            q('INSERT INTO phpgw_fud_read (thread_id, msg_id, last_view, user_id) SELECT ' . $mtf->thread_id . ', 0, 0, id FROM phpgw_fud_users WHERE id IN(' . implode(',', $tl) . ')');
        }
        if (isset($to)) {
            send_notifications($to, $mtf->id, $mtf->subject, $mtf->alias, $notify_type, $notify_type == 'thr' ? $mtf->thread_id : $mtf->forum_id, $mtf->frm_name, $mtf->forum_id);
        }
        // Handle Mailing List and/or Newsgroup syncronization.
        if (($mtf->nntp_id || $mtf->mlist_id) && !$mtf->mlist_msg_id) {
            fud_use('email_msg_format.inc', true);
            reverse_fmt($mtf->alias);
            $from = $mtf->poster_id ? $mtf->alias . ' <' . $mtf->email . '>' : $GLOBALS['ANON_NICK'] . ' <' . $GLOBALS['NOTIFY_FROM'] . '>';
            $body = $mtf->body . ($mtf->msg_opt & 1 && $mtf->sig ? "\n--\n" . $mtf->sig : '');
            plain_text($body);
            plain_text($subject);
            if ($mtf->reply_to) {
                $replyto_id = q_singleval('SELECT mlist_msg_id FROM phpgw_fud_msg WHERE id=' . $mtf->reply_to);
            } else {
                $replyto_id = 0;
            }
            if ($mtf->attach_cnt) {
                $r = uq("SELECT a.id, a.original_name,\n\t\t\t\t\t\tCASE WHEN m.mime_hdr IS NULL THEN 'application/octet-stream' ELSE m.mime_hdr END\n\t\t\t\t\t\tFROM phpgw_fud_attach a\n\t\t\t\t\t\tLEFT JOIN phpgw_fud_mime m ON a.mime_type=m.id\n\t\t\t\t\t\tWHERE a.message_id=" . $mtf->id . " AND a.attach_opt=0");
                while ($ent = db_rowarr($r)) {
                    $attach[$ent[1]] = file_get_contents($GLOBALS['FILE_STORE'] . $ent[0] . '.atch');
                    if ($mtf->mlist_id) {
                        $attach_mime[$ent[1]] = $ent[2];
                    }
                }
            } else {
                $attach_mime = $attach = null;
            }
            if ($mtf->nntp_id) {
                fud_use('nntp.inc', true);
                $nntp_adm = db_sab('SELECT * FROM phpgw_fud_nntp WHERE id=' . $mtf->nntp_id);
                $nntp = new fud_nntp();
                $nntp->server = $nntp_adm->server;
                $nntp->newsgroup = $nntp_adm->newsgroup;
                $nntp->port = $nntp_adm->port;
                $nntp->timeout = $nntp_adm->timeout;
                $nntp->nntp_opt = $nntp_adm->nntp_opt;
                $nntp->login = $nntp_adm->login;
                $nntp->pass = $nntp_adm->pass;
                define('sql_p', 'phpgw_fud_');
                $lock = $nntp->get_lock();
                $nntp->post_message($mtf->subject, $body, $from, $mtf->id, $replyto_id, $attach);
                $nntp->close_connection();
                $nntp->release_lock($lock);
            } else {
                fud_use('mlist_post.inc', true);
                $GLOBALS['CHARSET'] = 'ISO-8859-15';
                $r = db_saq('SELECT name, additional_headers FROM phpgw_fud_mlist WHERE id=' . $mtf->mlist_id);
                mail_list_post($r[0], $from, $mtf->subject, $body, $mtf->id, $replyto_id, $attach, $attach_mime, $r[1]);
            }
        }
    }
			</form>
</div>
</body>
</html>
<?php 
        exit;
    } else {
        db_lock($DBHOST_TBL_PREFIX . 'thr_exchange WRITE, ' . $DBHOST_TBL_PREFIX . 'thread_view WRITE, ' . $DBHOST_TBL_PREFIX . 'level WRITE, ' . $DBHOST_TBL_PREFIX . 'forum WRITE, ' . $DBHOST_TBL_PREFIX . 'forum_read WRITE, ' . $DBHOST_TBL_PREFIX . 'thread WRITE, ' . $DBHOST_TBL_PREFIX . 'msg WRITE, ' . $DBHOST_TBL_PREFIX . 'attach WRITE, ' . $DBHOST_TBL_PREFIX . 'poll WRITE, ' . $DBHOST_TBL_PREFIX . 'poll_opt WRITE, ' . $DBHOST_TBL_PREFIX . 'poll_opt_track WRITE, ' . $DBHOST_TBL_PREFIX . 'users WRITE, ' . $DBHOST_TBL_PREFIX . 'thread_notify WRITE, ' . $DBHOST_TBL_PREFIX . 'msg_report WRITE, ' . $DBHOST_TBL_PREFIX . 'thread_rate_track WRITE');
        $c = q('SELECT root_msg_id, forum_id FROM ' . $DBHOST_TBL_PREFIX . 'thread WHERE last_post_date<' . $back . $lmt);
        while ($r = db_rowarr($c)) {
            fud_msg_edit::delete(false, $r[0], 1);
            $frm_list[$r[1]] = $r[1];
        }
        unset($r);
        foreach ($frm_list as $v) {
            rebuild_forum_view($v);
        }
        db_unlock();
        echo '<h2 color="red">It is highly recommended that you run a consitency checker after prunning.</h2>';
    }
}
require $WWW_ROOT_DISK . 'adm/admpanel.php';
?>
<h2>Topic Prunning</h2>
<form method="post" action="admprune.php">
<table class="datatable">
<tr class="field">
	<td nowrap>Topics with last post made:</td>
	<td ><input type="text" name="thread_age"></td>
	<td nowrap><?php 
draw_select("units", "Day(s)\nWeek(s)\nMonth(s)\nYear(s)", "86400\n604800\n2635200\n31622400", '86400');
delete_zero($tbl . 'forum_notify', 'SELECT fn.id FROM ' . $tbl . 'forum_notify fn LEFT JOIN ' . $tbl . 'forum f ON f.id=fn.forum_id LEFT JOIN ' . $tbl . 'users u ON u.id=fn.user_id WHERE u.id IS NULL OR f.id IS NULL');
draw_stat('Checking topic votes against topics');
delete_zero($tbl . 'thread_rate_track', 'SELECT trt.id FROM ' . $tbl . 'thread_rate_track trt LEFT JOIN ' . $tbl . 'thread t ON t.id=trt.thread_id LEFT JOIN ' . $tbl . 'users u ON u.id=trt.user_id WHERE u.id IS NULL OR t.id IS NULL');
draw_stat('Rebuild topic rating cache');
q('UPDATE ' . $tbl . 'thread SET rating=0, n_rating=0');
$c = q('SELECT thread_id, count(*), AVG(rating) FROM ' . $tbl . 'thread_rate_track GROUP BY thread_id');
while ($r = db_rowarr($c)) {
    q('UPDATE ' . $tbl . 'thread SET rating=' . round($r[2]) . ', n_rating=' . (int) $r[1] . ' WHERE id=' . $r[0]);
}
unset($c);
draw_stat('Done: Rebuild topic rating cache');
draw_stat('Rebuilding Topic Views');
q('DELETE FROM ' . $tbl . 'thread_view');
$c = q('SELECT id FROM ' . $tbl . 'forum');
while ($r = db_rowarr($c)) {
    rebuild_forum_view($r[0]);
}
unset($fr);
draw_stat('Done: Rebuilding Topic Views');
draw_stat('Rebuilding user ranks, message counts & last post ids');
q('UPDATE ' . $tbl . 'users SET level_id=0, posted_msg_count=0, u_last_post_id=0, custom_status=NULL');
if (__dbtype__ == 'mysql') {
    q('INSERT INTO ' . $tbl . 'tmp_consist (ps, p, c) SELECT MAX(post_stamp), poster_id, count(*) FROM ' . $tbl . 'msg WHERE apr=1 GROUP BY poster_id ORDER BY poster_id');
    if (version_compare("4.0.4", q_singleval("SELECT VERSION()")) < 1) {
        q("UPDATE " . $tbl . "users u, " . $tbl . "tmp_consist, " . $tbl . "msg m SET u.u_last_post_id=m.id, u.posted_msg_count=" . $tbl . "tmp_consist.c WHERE u.id=m.poster_id AND m.poster_id=" . $tbl . "tmp_consist.p AND m.post_stamp=" . $tbl . "tmp_consist.ps AND m.apr=1");
    } else {
        $c = q('SELECT ' . $tbl . 'tmp_consist.p, ' . $tbl . 'tmp_consist.c, m.id FROM ' . $tbl . 'tmp_consist INNER JOIN ' . $tbl . 'msg m ON m.apr=1 AND m.poster_id=' . $tbl . 'tmp_consist.p AND m.post_stamp=' . $tbl . 'tmp_consist.ps');
        while ($r = db_rowarr($c)) {
            if (!$r[1]) {
                continue;
            }