function register_vote(&$options, $poll_id, $opt_id, $mid)
{
    /* invalid option or previously voted */
    if (!isset($options[$opt_id]) || q_singleval('SELECT id FROM phpgw_fud_poll_opt_track WHERE poll_id=' . $poll_id . ' AND user_id=' . _uid)) {
        return;
    }
    if (db_li('INSERT INTO phpgw_fud_poll_opt_track(poll_id, user_id, poll_opt) VALUES(' . $poll_id . ', ' . _uid . ', ' . $opt_id . ')', $a)) {
        q('UPDATE phpgw_fud_poll_opt SET count=count+1 WHERE id=' . $opt_id);
        q('UPDATE phpgw_fud_poll SET total_votes=total_votes+1 WHERE id=' . $poll_id);
        poll_cache_rebuild($opt_id, $options);
        q('UPDATE phpgw_fud_msg SET poll_cache=' . strnull(addslashes(@serialize($options))) . ' WHERE id=' . $mid);
    }
    return 1;
}
function check_return($returnto)
{
    if (!$returnto || !strncmp($returnto, 't=error', 7)) {
        header('Location: /egroupware/fudforum/3814588639/index.php?t=index&' . _rsidl);
    } else {
        if (strpos($returnto, 'S=') === false && $GLOBALS['FUD_OPT_1'] & 128) {
            header('Location: /egroupware/fudforum/3814588639/index.php?' . $returnto . '&S=' . s);
        } else {
            header('Location: /egroupware/fudforum/3814588639/index.php?' . $returnto);
        }
    }
    exit;
}
if (isset($_POST['rate_thread_id'], $_POST['sel_vote'])) {
    $th = (int) $_POST['rate_thread_id'];
    $rt = (int) $_POST['sel_vote'];
    /* determine if the user has permission to rate the thread */
    if (!q_singleval('SELECT t.id
				FROM phpgw_fud_thread t
				LEFT JOIN phpgw_fud_mod m ON t.forum_id=m.forum_id AND m.user_id=' . _uid . '
				INNER JOIN phpgw_fud_group_cache g1 ON g1.user_id=' . (_uid ? 2147483647 : 0) . ' AND g1.resource_id=t.forum_id
				' . (_uid ? ' LEFT JOIN phpgw_fud_group_cache g2 ON g2.user_id=' . _uid . ' AND g2.resource_id=t.forum_id ' : '') . '
				WHERE t.id=' . $th . ($usr->users_opt & 1048576 ? '' : ' AND (m.id IS NOT NULL OR ((CASE WHEN g1.id IS NULL THEN g2.group_cache_opt ELSE g1.group_cache_opt END) & 1024) > 0)') . ' LIMIT 1')) {
        std_error('access');
    }
    if (db_li('INSERT INTO phpgw_fud_thread_rate_track (thread_id, user_id, stamp, rating) VALUES(' . $th . ', ' . _uid . ', ' . __request_timestamp__ . ', ' . $rt . ')', $ef)) {
        $rt = db_saq('SELECT count(*), ROUND(AVG(rating)) FROM phpgw_fud_thread_rate_track WHERE thread_id=' . $th);
        q('UPDATE phpgw_fud_thread SET rating=' . (int) $rt[1] . ', n_rating=' . (int) $rt[0] . ' WHERE id=' . $th);
    }
}
check_return($usr->returnto);
function user_mark_forum_read($id, $fid, $last_view)
{
    if (__dbtype__ == 'mysql') {
        q('REPLACE INTO phpgw_fud_read (user_id, thread_id, msg_id, last_view) SELECT ' . $id . ', id, last_post_id, ' . __request_timestamp__ . ' FROM phpgw_fud_thread WHERE forum_id=' . $fid);
    } else {
        if (!db_li('INSERT INTO phpgw_fud_read (user_id, thread_id, msg_id, last_view) SELECT ' . $id . ', id, last_post_id, ' . __request_timestamp__ . ' FROM phpgw_fud_thread WHERE forum_id=' . $fid)) {
            q("UPDATE phpgw_fud_read SET user_id=" . $id . ", thread_id=id, msg_id=last_post_id, last_view=" . __request_timestamp__ . " WHERE user_id=" . $id . " SELECT id, last_post_id FROM phpgw_fud_thread WHERE forum_id=" . $fid);
        }
    }
}
function index_text($subj, $body, $msg_id)
{
    /* Remove Stuff In Quotes */
    while (preg_match('!<table border="0" align="center" width="90%" cellpadding="3" cellspacing="1"><tr><td class="SmallText"><b>(.*?)</b></td></tr><tr><td class="quote"><br>(.*?)<br></td></tr></table>!is', $body)) {
        $body = preg_replace('!<table border="0" align="center" width="90%" cellpadding="3" cellspacing="1"><tr><td class="SmallText"><b>(.*?)</b></td></tr><tr><td class="quote"><br>(.*?)<br></td></tr></table>!is', '', $body);
    }
    /* this is mostly a hack for php verison < 4.3 because isset(string[bad offset]) returns a warning */
    error_reporting(0);
    if (strncmp($GLOBALS['usr']->lang, 'chinese', 7)) {
        $cs = array('!\\W!', '!\\s+!');
        $cd = array(' ', ' ');
        reverse_fmt($subj);
        $subj = trim(preg_replace($cs, $cd, strip_tags(strtolower($subj))));
        reverse_fmt($body);
        $body = trim(preg_replace($cs, $cd, strip_tags(strtolower($body))));
        /* build full text index */
        $t1 = array_unique(explode(' ', $subj));
        $t2 = array_unique(explode(' ', $body));
        foreach ($t1 as $v) {
            if (isset($v[51]) || !isset($v[3])) {
                continue;
            }
            $w1[] = "'" . addslashes($v) . "'";
        }
        if (isset($w1)) {
            $w2 = $w1;
        }
        foreach ($t2 as $v) {
            if (isset($v[51]) || !isset($v[3])) {
                continue;
            }
            $w2[] = "'" . addslashes($v) . "'";
        }
    } else {
        /* handling for multibyte languages */
        $w1 = mb_word_split($subj);
        if ($w1) {
            $w2 = array_merge($w1, mb_word_split($body));
        } else {
            unset($w1);
        }
    }
    if (!$w2) {
        return;
    }
    $w2 = array_unique($w2);
    if (__dbtype__ == 'mysql') {
        ins_m('phpgw_fud_search', 'word', $w2);
    } else {
        ins_m('phpgw_fud_search', 'word', $w2, 'text');
    }
    /* This allows us to return right away, meaning we don't need to wait
     * for any locks to be released etc... */
    if (__dbtype__ == 'mysql') {
        $del = 'DELAYED';
    } else {
        $del = '';
    }
    if (isset($w1)) {
        db_li('INSERT ' . $del . ' INTO phpgw_fud_title_index (word_id, msg_id) SELECT id, ' . $msg_id . ' FROM phpgw_fud_search WHERE word IN(' . implode(',', $w1) . ')', $ef);
    }
    db_li('INSERT ' . $del . ' INTO phpgw_fud_index (word_id, msg_id) SELECT id, ' . $msg_id . ' FROM phpgw_fud_search WHERE word IN(' . implode(',', $w2) . ')', $ef);
}
 $c = q("SELECT id, alias FROM " . $DBHOST_TBL_PREFIX . "users WHERE alias='" . $srch . "'");
 if (!db_count($c)) {
     unset($c);
     $c = q("SELECT id, alias FROM " . $DBHOST_TBL_PREFIX . "users WHERE alias LIKE '" . $srch . "%' LIMIT 50");
 }
 switch ($cnt = db_count($c)) {
     case 0:
         $error = 'Could not find a user who matches the "' . $srch . '" login mask';
         break;
     case 1:
         $r = db_rowarr($c);
         if (__dbtype__ == 'mysql') {
             q('REPLACE INTO ' . $DBHOST_TBL_PREFIX . 'group_members (group_id, user_id, group_members_opt) SELECT id, ' . $r[0] . ', groups_opt|65536|131072 FROM ' . $DBHOST_TBL_PREFIX . 'groups WHERE id=' . $group_id);
         } else {
             $opt = q_singleval('groups_opt|65536|131072 FROM ' . $DBHOST_TBL_PREFIX . 'groups WHERE id=' . $group_id);
             if (!db_li('INSERT INTO ' . $DBHOST_TBL_PREFIX . 'group_members (group_id, user_id, group_members_opt) SELECT id, ' . $r[0] . ', groups_opt|65536|131072 FROM ' . $DBHOST_TBL_PREFIX . 'groups WHERE id=' . $group_id)) {
                 q("UPDATE {SQL_TABLE_PREFIX}group_members SET group_members_opt=" . $opt . " WHERE user_id=" . $r[0] . " AND group_id=" . $group_id);
             }
         }
         rebuild_group_ldr_cache($r[0]);
         grp_rebuild_cache(array($r[0]));
         $gr_leader = '';
         break;
     default:
         /* more then 1 user found, draw a selection form */
         echo '<html><body bgcolor="#ffffff">There are ' . $cnt . ' users matching your search mask:<br><table border=0 cellspacing=0 cellpadding=3>';
         while ($r = db_rowarr($c)) {
             echo '<tr><td><a href="admgrouplead.php?gr_leader=' . urlencode($r[1]) . '&group_id=' . $group_id . '&' . _rsidl . '">' . $r[1] . '</a></td></tr>';
         }
         unset($c);
         echo '</table></body></html>';