Example #1
0
function bb_insert_post($args = null)
{
    global $bbdb, $bb_current_user, $bb;
    if (!($args = nxt_parse_args($args))) {
        return false;
    }
    $fields = array_keys($args);
    if (isset($args['post_id']) && false !== $args['post_id']) {
        $update = true;
        if (!($post_id = (int) get_post_id($args['post_id']))) {
            return false;
        }
        // Get from db, not cache.  Good idea?
        $post = $bbdb->get_row($bbdb->prepare("SELECT * FROM {$bbdb->posts} WHERE post_id = %d", $post_id));
        $defaults = get_object_vars($post);
        unset($defaults['post_id']);
        // Only update the args we passed
        $fields = array_intersect($fields, array_keys($defaults));
        if (in_array('topic_id', $fields)) {
            $fields[] = 'forum_id';
        }
        // No need to run filters if these aren't changing
        // bb_new_post() and bb_update_post() will always run filters
        $run_filters = (bool) array_intersect(array('post_status', 'post_text'), $fields);
    } else {
        $post_id = false;
        $update = false;
        $now = bb_current_time('mysql');
        $current_user_id = bb_get_current_user_info('id');
        $ip_address = $_SERVER['REMOTE_ADDR'];
        $defaults = array('topic_id' => 0, 'post_text' => '', 'post_time' => $now, 'poster_id' => $current_user_id, 'poster_ip' => $ip_address, 'post_status' => 0, 'post_position' => false);
        // Insert all args
        $fields = array_keys($defaults);
        $fields[] = 'forum_id';
        $run_filters = true;
    }
    $defaults['throttle'] = true;
    extract(nxt_parse_args($args, $defaults));
    // If the user is not logged in and loginless posting is ON, then this function expects $post_author, $post_email and $post_url to be sanitized (check bb-post.php for example)
    if (!($topic = get_topic($topic_id))) {
        return false;
    }
    if (bb_is_login_required() && !($user = bb_get_user($poster_id))) {
        return false;
    }
    $topic_id = (int) $topic->topic_id;
    $forum_id = (int) $topic->forum_id;
    if ($run_filters && !($post_text = apply_filters('pre_post', $post_text, $post_id, $topic_id))) {
        return false;
    }
    if ($update) {
        // Don't change post_status with this function.  Use bb_delete_post().
        $post_status = $post->post_status;
    }
    if ($run_filters) {
        $post_status = (int) apply_filters('pre_post_status', $post_status, $post_id, $topic_id);
    }
    if (false === $post_position) {
        $post_position = $topic_posts = intval(0 == $post_status ? $topic->topic_posts + 1 : $topic->topic_posts);
    }
    unset($defaults['throttle']);
    if ($update) {
        $bbdb->update($bbdb->posts, compact($fields), compact('post_id'));
        nxt_cache_delete($post_id, 'bb_post');
    } else {
        $bbdb->insert($bbdb->posts, compact($fields));
        $post_id = $topic_last_post_id = (int) $bbdb->insert_id;
        if (0 == $post_status) {
            $topic_time = $post_time;
            $topic_last_poster = !bb_is_user_logged_in() && !bb_is_login_required() ? -1 : $poster_id;
            $topic_last_poster_name = !bb_is_user_logged_in() && !bb_is_login_required() ? $post_author : $user->user_login;
            $bbdb->query($bbdb->prepare("UPDATE {$bbdb->forums} SET posts = posts + 1 WHERE forum_id = %d;", $topic->forum_id));
            $bbdb->update($bbdb->topics, compact('topic_time', 'topic_last_poster', 'topic_last_poster_name', 'topic_last_post_id', 'topic_posts'), compact('topic_id'));
            $query = new BB_Query('post', array('post_author_id' => $poster_id, 'topic_id' => $topic_id, 'post_id' => "-{$post_id}"));
            if (!$query->results) {
                $topics_replied_key = $bbdb->prefix . 'topics_replied';
                bb_update_usermeta($poster_id, $topics_replied_key, $user->{$topics_replied_key} + 1);
            }
        } else {
            bb_update_topicmeta($topic->topic_id, 'deleted_posts', isset($topic->deleted_posts) ? $topic->deleted_posts + 1 : 1);
        }
    }
    bb_update_topic_voices($topic_id);
    // if user not logged in, save user data as meta data
    if (!$user) {
        bb_update_meta($post_id, 'post_author', $post_author, 'post');
        bb_update_meta($post_id, 'post_email', $post_email, 'post');
        bb_update_meta($post_id, 'post_url', $post_url, 'post');
    }
    if ($throttle && !bb_current_user_can('throttle')) {
        if ($user) {
            bb_update_usermeta($poster_id, 'last_posted', time());
        } else {
            bb_set_transient($_SERVER['REMOTE_ADDR'] . '_last_posted', time());
        }
    }
    if (!bb_is_login_required() && !($user = bb_get_user($poster_id))) {
        $post_cookie_lifetime = apply_filters('bb_post_cookie_lifetime', 30000000);
        setcookie('post_author_' . BB_HASH, $post_author, time() + $post_cookie_lifetime, $bb->cookiepath, $bb->cookiedomain);
        setcookie('post_author_email_' . BB_HASH, $post_email, time() + $post_cookie_lifetime, $bb->cookiepath, $bb->cookiedomain);
        setcookie('post_author_url_' . BB_HASH, $post_url, time() + $post_cookie_lifetime, $bb->cookiepath, $bb->cookiedomain);
    }
    nxt_cache_delete($topic_id, 'bb_topic');
    nxt_cache_delete($topic_id, 'bb_thread');
    nxt_cache_delete($forum_id, 'bb_forum');
    nxt_cache_flush('bb_forums');
    nxt_cache_flush('bb_query');
    nxt_cache_flush('bb_cache_posts_post_ids');
    if ($update) {
        // fire actions after cache is flushed
        do_action('bb_update_post', $post_id);
    } else {
        do_action('bb_new_post', $post_id);
    }
    do_action('bb_insert_post', $post_id, $args, compact(array_keys($args)));
    // post_id, what was passed, what was used
    if (bb_get_option('enable_pingback')) {
        bb_update_postmeta($post_id, 'pingback_queued', '');
        nxt_schedule_single_event(time(), 'do_pingbacks');
    }
    return $post_id;
}
Example #2
0
 function set($transient, $value, $expiration = 0)
 {
     return bb_set_transient(BP_Transients::prefix() . $transient, $value, $expiration);
 }
Example #3
0
function nospamuser_check($type, $data)
{
    $settings = bb_get_option('nospamuser-settings');
    if (!$settings) {
        bb_update_option('nospamuser-settings', $settings = array('days' => 30, 'min_occur' => 5, 'max_occur' => 10, 'api_key' => '', 'recaptcha_mode' => 'aggressive', 'recapthca_pub' => '', 'recaptcha_priv' => '', 'stats_public' => 0));
    }
    if (!is_array($result = bb_get_transient('nospamuser-' . $type . '-' . md5($data)))) {
        $wp_http = new WP_Http();
        $response = $wp_http->get('http://www.stopforumspam.com/api?' . urlencode($type) . '=' . urlencode($data), array('user-agent' => apply_filters('http_headers_useragent', backpress_get_option('wp_http_version')) . NOSPAMUSER_AGENT));
        $response = $response['body'];
        if (strpos($response, '<response success="true">') === false) {
            return;
        }
        if (strpos($response, '<appears>no</appears>') !== false) {
            $result = array(0, 0);
        } else {
            preg_match('/<lastseen>([^<>]+)<\\/lastseen>/', $response, $matches);
            $result = array((int) substr($response, strpos($response, '<frequency>') + 11), strtotime($matches[1]));
        }
        bb_set_transient('nospamuser-' . $type . '-' . md5($data), $result, 604800);
    }
    if ($result == array(0, 0)) {
        // Even if the settings are set incorrectly, non-spammers shouldn't be blocked.
        return;
    }
    if ($result[0] >= $settings['min_occur'] && $result[1] >= time() - $settings['days'] * 86400 || $result[0] >= $settings['max_occur'] && $settings['recaptcha_mode'] == 'aggressive') {
        if ($result[0] >= $settings['max_occur'] && $settings['recaptcha_mode'] == 'adaptive') {
            nospamuser_block($type, $data, true);
        } elseif ($settings['recaptcha_mode'] == 'aggressive' || !$settings['recaptcha_pub'] || !$settings['recaptcha_priv']) {
            nospamuser_block($type, $data, true);
        } else {
            nospamuser_block($type, $data, false);
        }
    }
}