function bb_anon_activate_plugin()
{
    global $bbdb;
    $bbdb->query("INSERT INTO {$bbdb->users} (user_login,user_nicename, user_registered) VALUES ('anonymous','Anonymous', '" . bb_current_time('mysql') . "')");
    if ($anon_id = bb_get_option('bb_anon_user_id')) {
        $bbdb->query("UPDATE {$bbdb->users} SET ID = {$anon_id} where ID = " . $bbdb->insert_id);
    } else {
        $anon_id = $bbdb->insert_id;
        bb_update_option('bb_anon_user_id', $anon_id);
    }
    $user = new BP_User($anon_id);
    $user->add_role('anonymous');
    $user->remove_role('member');
    $user->add_cap('anonymous');
    $user->remove_cap('member');
}
 function bb_new_user($user_login, $user_email, $user_url, $user_status = 0)
 {
     global $wp_users_object, $bbdb;
     // is_email check + dns
     if (!($user_email = bb_verify_email($user_email))) {
         return new WP_Error('user_email', __('Invalid email address'), $user_email);
     }
     if (!($user_login = sanitize_user($user_login, true))) {
         return new WP_Error('user_login', __('Invalid username'), $user_login);
     }
     // user_status = 1 means the user has not yet been verified
     $user_status = is_numeric($user_status) ? (int) $user_status : 0;
     $user_nicename = $_user_nicename = bb_user_nicename_sanitize($user_login);
     if (strlen($_user_nicename) < 1) {
         return new WP_Error('user_login', __('Invalid username'), $user_login);
     }
     while (is_numeric($user_nicename) || ($existing_user = bb_get_user_by_nicename($user_nicename))) {
         $user_nicename = bb_slug_increment($_user_nicename, $existing_user->user_nicename, 50);
     }
     $user_url = bb_fix_link($user_url);
     $user_registered = bb_current_time('mysql');
     $password = wp_generate_password();
     $user_pass = wp_hash_password($password);
     $user = $wp_users_object->new_user(compact('user_login', 'user_email', 'user_url', 'user_nicename', 'user_status', 'user_pass'));
     if (is_wp_error($user)) {
         if ('user_nicename' == $user->get_error_code()) {
             return new WP_Error('user_login', $user->get_error_message());
         }
         return $user;
     }
     $user_id = $bbdb->insert_id;
     $options = bb_get_option('approve_user_registration_options');
     bb_update_usermeta($user_id, $bbdb->prefix . 'capabilities', array('waitingapproval' => true, 'member' => true));
     approve_user_registration_send_pass($user_id, $password);
     do_action('bb_new_user', $user['ID'], $user['plain_pass']);
     return $user['ID'];
 }
function bb_insert_topic($args = null)
{
    global $bbdb;
    if (!($args = wp_parse_args($args))) {
        return false;
    }
    $fields = array_keys($args);
    if (isset($args['topic_id']) && false !== $args['topic_id']) {
        $update = true;
        if (!($topic_id = (int) get_topic_id($args['topic_id']))) {
            return false;
        }
        // Get from db, not cache.  Good idea?  Prevents trying to update meta_key names in the topic table (get_topic() returns appended topic obj)
        $topic = $bbdb->get_row($bbdb->prepare("SELECT * FROM {$bbdb->topics} WHERE topic_id = %d", $topic_id));
        $defaults = get_object_vars($topic);
        unset($defaults['topic_id']);
        // Only update the args we passed
        $fields = array_intersect($fields, array_keys($defaults));
        if (in_array('topic_poster', $fields)) {
            $fields[] = 'topic_poster_name';
        }
        if (in_array('topic_last_poster', $fields)) {
            $fields[] = 'topic_last_poster_name';
        }
    } else {
        $topic_id = false;
        $update = false;
        $now = bb_current_time('mysql');
        $current_user_id = bb_get_current_user_info('id');
        $defaults = array('topic_title' => '', 'topic_slug' => '', 'topic_poster' => $current_user_id, 'topic_poster_name' => '', 'topic_last_poster' => $current_user_id, 'topic_last_poster_name' => '', 'topic_start_time' => $now, 'topic_time' => $now, 'topic_open' => 1, 'forum_id' => 0);
        // Insert all args
        $fields = array_keys($defaults);
    }
    $defaults['tags'] = false;
    // accepts array or comma delimited string
    extract(wp_parse_args($args, $defaults));
    unset($defaults['tags']);
    if (!($forum = bb_get_forum($forum_id))) {
        return false;
    }
    $forum_id = (int) $forum->forum_id;
    if (!($user = bb_get_user($topic_poster))) {
        $user = bb_get_user($topic_poster_name, array('by' => 'login'));
    }
    if (!empty($user)) {
        $topic_poster = $user->ID;
        $topic_poster_name = $user->user_login;
    }
    if (!($last_user = bb_get_user($topic_last_poster))) {
        $last_user = bb_get_user($topic_last_poster_name, array('by' => 'login'));
    }
    if (!empty($last_user)) {
        $topic_last_poster = $last_user->ID;
        $topic_last_poster_name = $last_user->user_login;
    }
    if (in_array('topic_title', $fields)) {
        $topic_title = apply_filters('pre_topic_title', $topic_title, $topic_id);
        if (strlen($topic_title) < 1) {
            return false;
        }
    }
    if (in_array('topic_slug', $fields)) {
        $slug_sql = $update ? "SELECT topic_slug FROM {$bbdb->topics} WHERE topic_slug = %s AND topic_id != %d" : "SELECT topic_slug FROM {$bbdb->topics} WHERE topic_slug = %s";
        $topic_slug = $_topic_slug = bb_slug_sanitize($topic_slug ? $topic_slug : wp_specialchars_decode($topic_title, ENT_QUOTES));
        if (strlen($_topic_slug) < 1) {
            $topic_slug = $_topic_slug = '0';
        }
        while (is_numeric($topic_slug) || ($existing_slug = $bbdb->get_var($bbdb->prepare($slug_sql, $topic_slug, $topic_id)))) {
            $topic_slug = bb_slug_increment($_topic_slug, $existing_slug);
        }
    }
    if ($update) {
        $bbdb->update($bbdb->topics, compact($fields), compact('topic_id'));
        wp_cache_delete($topic_id, 'bb_topic');
        if (in_array('topic_slug', $fields)) {
            wp_cache_delete($topic->topic_slug, 'bb_topic_slug');
        }
        wp_cache_flush('bb_query');
        wp_cache_flush('bb_cache_posts_post_ids');
        do_action('bb_update_topic', $topic_id);
    } else {
        $bbdb->insert($bbdb->topics, compact($fields));
        $topic_id = $bbdb->insert_id;
        $bbdb->query($bbdb->prepare("UPDATE {$bbdb->forums} SET topics = topics + 1 WHERE forum_id = %d", $forum_id));
        wp_cache_delete($forum_id, 'bb_forum');
        wp_cache_flush('bb_forums');
        wp_cache_flush('bb_query');
        wp_cache_flush('bb_cache_posts_post_ids');
        do_action('bb_new_topic', $topic_id);
    }
    if (!empty($tags)) {
        bb_add_topic_tags($topic_id, $tags);
    }
    do_action('bb_insert_topic', $topic_id, $args, compact(array_keys($args)));
    // topic_id, what was passed, what was used
    return $topic_id;
}
Exemple #4
0
function bb_akismet_delete_old()
{
    // Delete old every 20
    $n = mt_rand(1, 20);
    if ($n % 20) {
        return;
    }
    global $bbdb;
    $now = bb_current_time('mysql');
    $posts = (array) $bbdb->get_col($bbdb->prepare("SELECT post_id FROM {$bbdb->posts} WHERE DATE_SUB(%s, INTERVAL 15 DAY) > post_time AND post_status = '2'", $now));
    foreach ($posts as $post) {
        bb_delete_post($post, 1);
    }
}
        $timezone_object = new DateTimeZone($timezone_string);
        $found_transition = false;
        foreach (timezone_transitions_get($timezone_object) as $timezone_transition) {
            if ($timezone_transition['ts'] > time()) {
                $note = $timezone_transition['isdst'] ? __('Daylight savings time begins on <code>%s</code>') : __('Standard time begins on <code>%s</code>');
                $_time_options['timezone_string']['note'][] = sprintf($note, bb_gmdate_i18n(bb_get_datetime_formatstring_i18n(), $timezone_transition['ts'], false));
                break;
            }
        }
    }
    $time_options = array_merge($_time_options, $time_options);
} else {
    // Tidy up the old style dropdown
    $time_options['gmt_offset']['note'] = array(1 => sprintf(__('<abbr title="Coordinated Universal Time">UTC</abbr> %s is <code>%s</code>'), $time_options['gmt_offset']['options'][$gmt_offset], bb_datetime_format_i18n(bb_current_time())), 2 => __('Unfortunately, you have to manually update this for Daylight Savings Time.'));
    if ($gmt_offset) {
        $time_options['gmt_offset']['note'][0] = sprintf(__('<abbr title="Coordinated Universal Time">UTC</abbr> time is <code>%s</code>'), bb_gmdate_i18n(bb_get_datetime_formatstring_i18n(), bb_current_time(), true));
        ksort($time_options['gmt_offset']['note']);
    }
    foreach ($time_options['gmt_offset']['options'] as $_key => $_value) {
        $time_options['gmt_offset']['options'][$_key] = sprintf(__('UTC %s'), $_value);
    }
}
$bb_admin_body_class = ' bb-admin-settings';
bb_get_admin_header();
?>

<div class="wrap">

<h2><?php 
_e('General Settings');
?>
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;
}
function bb_insert_post($args = null)
{
    global $bbdb, $bb_current_user;
    if (!($args = wp_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(wp_parse_args($args, $defaults));
    if (!($topic = get_topic($topic_id))) {
        return false;
    }
    if (!($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'));
        wp_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 = $poster_id;
            $topic_last_poster_name = $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) {
                bb_update_usermeta($poster_id, $bbdb->prefix . 'topics_replied', $user->topics_replied + 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 ($throttle && !bb_current_user_can('throttle')) {
        bb_update_usermeta($poster_id, 'last_posted', time());
    }
    wp_cache_delete($topic_id, 'bb_topic');
    wp_cache_delete($topic_id, 'bb_thread');
    wp_cache_delete($forum_id, 'bb_forum');
    wp_cache_flush('bb_forums');
    wp_cache_flush('bb_query');
    wp_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', '');
        wp_schedule_single_event(time(), 'do_pingbacks');
    }
    return $post_id;
}
/**
 * Custom insert post function so that we could do what we need
 *
 * All counting functions have been removed from here, recount should be done
 * after running this script.
 *
 * @param mixed $args
 * @return int|bool New post ID if post was created, otherwise false
 */
function w2bc_insert_post($args = null)
{
    global $bbdb, $bb_current_user, $bb;
    if (!($args = wp_parse_args($args))) {
        return false;
    }
    $fields = array_keys($args);
    $defaults = array('topic_id' => 0, 'post_text' => '', 'post_time' => bb_current_time('mysql'), 'poster_id' => bb_get_current_user_info('id'), 'poster_ip' => $_SERVER['REMOTE_ADDR'], 'post_status' => 0, 'post_position' => false);
    // Insert all args
    $fields = array_keys($defaults);
    $fields[] = 'forum_id';
    extract(wp_parse_args($args, $defaults));
    if (!($topic = get_topic($topic_id))) {
        return false;
    }
    $topic_id = (int) $topic->topic_id;
    $forum_id = (int) $topic->forum_id;
    if (false === $post_position) {
        $post_position = $topic_posts = intval(0 == $post_status ? $topic->topic_posts + 1 : $topic->topic_posts);
    }
    $bbdb->insert($bbdb->posts, compact($fields));
    $post_id = $topic_last_post_id = (int) $bbdb->insert_id;
    // if anonymous posting, save user data as meta data
    if (!$user) {
        if ($post_author) {
            bb_update_meta($post_id, 'post_author', $post_author, 'post');
        }
        // Atleast this should be there
        if ($post_email) {
            bb_update_meta($post_id, 'post_email', $post_email, 'post');
        }
        if ($post_url) {
            bb_update_meta($post_id, 'post_url', $post_url, 'post');
        }
    }
    $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->update($bbdb->topics, compact('topic_time', 'topic_last_poster', 'topic_last_poster_name', 'topic_last_post_id', 'topic_posts'), compact('topic_id'));
    wp_cache_delete($topic_id, 'bb_topic');
    wp_cache_delete($topic_id, 'bb_thread');
    wp_cache_delete($forum_id, 'bb_forum');
    wp_cache_flush('bb_forums');
    wp_cache_flush('bb_query');
    wp_cache_flush('bb_cache_posts_post_ids');
    if (bb_get_option('enable_pingback')) {
        bb_update_postmeta($post_id, 'pingback_queued', '');
        wp_schedule_single_event(time(), 'do_pingbacks');
    }
    return $post_id;
}