Пример #1
0
/**
 * NASTY ALERT, Exact copy of the internal bb_new_forum() function only this one does not check whether the user is allowed to create forums or not
 *
 * @Param: An array containing info for the new forum.
 * @return: The Id of the newly created forum
 * @author: Tom Willmot
 * @version 1.0
 **/
function nm_bb_new_forum($args)
{
    global $bbdb, $bb_cache;
    $defaults = array('forum_name' => '', 'forum_desc' => '', 'forum_parent' => 0, 'forum_order' => false);
    $args = wp_parse_args($args, $defaults);
    if (1 < func_num_args()) {
        // For back compat
        $args['forum_name'] = func_get_arg(0);
        $args['forum_desc'] = func_get_arg(1);
        $args['forum_order'] = 2 < func_num_args() ? func_get_arg(2) : 0;
    }
    extract($args, EXTR_SKIP);
    if (!is_numeric($forum_order)) {
        $forum_order = (int) $bbdb->get_var("SELECT MAX(forum_order) FROM {$bbdb->forums}") + 1;
    }
    $forum_order = (int) $forum_order;
    $forum_parent = (int) $forum_parent;
    $forum_name = apply_filters('bb_pre_forum_name', stripslashes($forum_name));
    $forum_desc = apply_filters('bb_pre_forum_desc', stripslashes($forum_desc));
    $forum_name = bb_trim_for_db($forum_name, 150);
    $forum_name = $bbdb->escape($forum_name);
    $forum_desc = $bbdb->escape($forum_desc);
    if (strlen($forum_name) < 1) {
        return false;
    }
    $forum_slug = $_forum_slug = bb_slug_sanitize($forum_name);
    while (is_numeric($forum_slug) || ($existing_slug = $bbdb->get_var("SELECT forum_slug FROM {$bbdb->forums} WHERE forum_slug = '{$forum_slug}'"))) {
        $forum_slug = bb_slug_increment($_forum_slug, $existing_slug);
    }
    $bbdb->query("INSERT INTO {$bbdb->forums} (forum_name, forum_slug, forum_desc, forum_parent, forum_order) VALUES ('{$forum_name}', '{$forum_slug}', '{$forum_desc}', '{$forum_parent}', '{$forum_order}')");
    $bb_cache->flush_one('forums');
    return $bbdb->insert_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;
}
function bb_upgrade_process_all_slugs()
{
    global $bbdb;
    // Forums
    $forums = (array) $bbdb->get_results("SELECT forum_id, forum_name FROM {$bbdb->forums} ORDER BY forum_order ASC");
    $slugs = array();
    foreach ($forums as $forum) {
        $slug = bb_slug_sanitize(nxt_specialchars_decode($forum->forum_name, ENT_QUOTES));
        $slugs[$slug][] = $forum->forum_id;
    }
    foreach ($slugs as $slug => $forum_ids) {
        foreach ($forum_ids as $count => $forum_id) {
            $_slug = $slug;
            $count = -$count;
            // madness
            if (is_numeric($slug) || $count) {
                $_slug = bb_slug_increment($slug, $count);
            }
            $bbdb->query("UPDATE {$bbdb->forums} SET forum_slug = '{$_slug}' WHERE forum_id = '{$forum_id}';");
        }
    }
    unset($forums, $forum, $slugs, $slug, $_slug, $forum_ids, $forum_id, $count);
    // Topics
    $topics = (array) $bbdb->get_results("SELECT topic_id, topic_title FROM {$bbdb->topics} ORDER BY topic_start_time ASC");
    $slugs = array();
    foreach ($topics as $topic) {
        $slug = bb_slug_sanitize(nxt_specialchars_decode($topic->topic_title, ENT_QUOTES));
        $slugs[$slug][] = $topic->topic_id;
    }
    foreach ($slugs as $slug => $topic_ids) {
        foreach ($topic_ids as $count => $topic_id) {
            $_slug = $slug;
            $count = -$count;
            if (is_numeric($slug) || $count) {
                $_slug = bb_slug_increment($slug, $count);
            }
            $bbdb->query("UPDATE {$bbdb->topics} SET topic_slug = '{$_slug}' WHERE topic_id = '{$topic_id}';");
        }
    }
    unset($topics, $topic, $slugs, $slug, $_slug, $topic_ids, $topic_id, $count);
}
Пример #4
0
 function generate_topic_sql($_part_of_post_query = false)
 {
     global $bbdb;
     $q =& $this->query_vars;
     $distinct = '';
     $sql_calc_found_rows = 'found_rows' === $q['count'] ? 'SQL_CALC_FOUND_ROWS' : '';
     // unfiltered
     $fields = 't.*';
     $index_hint = '';
     $join = '';
     $where = '';
     $group_by = '';
     $having = '';
     $order_by = '';
     $post_where = '';
     $post_queries = array('post_author_id', 'post_author', 'posted', 'post_status', 'position', 'post_text', 'poster_ip');
     if (!$_part_of_post_query && ($q['search'] || array_diff($post_queries, $this->not_set))) {
         $join .= " JOIN {$bbdb->posts} as p ON ( t.topic_id = p.topic_id )";
         $post_where = $this->generate_post_sql(true);
         if ($q['search']) {
             $post_where .= ' AND ( ';
             $post_where .= $this->generate_topic_title_sql($q['search']);
             $post_where .= ' OR ';
             $post_where .= $this->generate_post_text_sql($q['search']);
             $post_where .= ' )';
         }
         $group_by = 't.topic_id';
         $fields .= ", MIN(p.post_id) as post_id";
         if ($bbdb->has_cap('GROUP_CONCAT', $bbdb->posts)) {
             $fields .= ", GROUP_CONCAT(p.post_text SEPARATOR ' ') AS post_text";
         } else {
             $fields .= ", p.post_text";
         }
         if ($this->match_query) {
             $fields .= ", AVG({$this->match_query}) AS search_score";
             if (!$q['order_by']) {
                 $q['order_by'] = 'search_score';
             }
         } elseif ($q['search'] || $q['post_text']) {
             $fields .= ", 0 AS search_score";
         }
     }
     if (!$_part_of_post_query) {
         if ($q['post_id']) {
             $post_topics = $post_topics_no = array();
             $op = substr($q['post_id'], 0, 1);
             if (in_array($op, array('>', '<'))) {
                 $post_topics = $bbdb->get_col("SELECT DISTINCT topic_id FROM {$bbdb->posts} WHERE post_id {$op} '" . (int) substr($q['post_id'], 1) . "'");
             } else {
                 $posts = explode(',', $q['post_id']);
                 $get_posts = array();
                 foreach ($posts as $post_id) {
                     $post_id = (int) $post_id;
                     $_post_id = abs($post_id);
                     $get_posts[] = $_post_id;
                 }
                 bb_cache_posts($get_posts);
                 foreach ($posts as $post_id) {
                     $post = bb_get_post(abs($post_id));
                     if ($post_id < 0) {
                         $post_topics_no[] = $post->topic_id;
                     } else {
                         $post_topics[] = $post->topic_id;
                     }
                 }
             }
             if ($post_topics) {
                 $where .= " AND t.topic_id IN (" . join(',', $post_topics) . ")";
             }
             if ($post_topics_no) {
                 $where .= " AND t.topic_id NOT IN (" . join(',', $post_topics_no) . ")";
             }
         }
         if ($q['topic_id']) {
             $where .= $this->parse_value('t.topic_id', $q['topic_id']);
         } elseif ($q['topic']) {
             $q['topic'] = bb_slug_sanitize($q['topic']);
             $where .= " AND t.topic_slug = '{$q['topic']}'";
         }
         if ($q['forum_id']) {
             $where .= $this->parse_value('t.forum_id', $q['forum_id']);
         } elseif ($q['forum']) {
             if (!($q['forum_id'] = bb_get_id_from_slug('forum', $q['forum']))) {
                 $this->error('query_var:forum', 'No forum by that name');
             }
             $where .= " AND t.forum_id = {$q['forum_id']}";
         }
         if ($q['tag'] && !is_int($q['tag_id'])) {
             $q['tag_id'] = (int) bb_get_tag_id($q['tag']);
         }
         if (is_numeric($q['tag_id'])) {
             $join .= " JOIN `{$bbdb->term_relationships}` AS tr ON ( t.`topic_id` = tr.`object_id` AND tr.`term_taxonomy_id` = {$q['tag_id']} )";
         }
         if (is_numeric($q['favorites']) && ($f_user = bb_get_user($q['favorites']))) {
             $where .= $this->parse_value('t.topic_id', $f_user->favorites);
         }
     }
     // !_part_of_post_query
     if ($q['topic_title']) {
         $where .= ' AND ' . $this->generate_topic_title_sql($q['topic_title']);
     }
     if ($q['started']) {
         $where .= $this->date('t.topic_start_time', $q['started']);
     }
     if ($q['updated']) {
         $where .= $this->date('t.topic_time', $q['updated']);
     }
     if ($q['topic_author_id']) {
         $where .= $this->parse_value('t.topic_poster', $q['topic_author_id']);
     } elseif ($q['topic_author']) {
         $user = bb_get_user($q['topic_author'], array('by' => 'login'));
         if (!($q['topic_author_id'] = (int) $user->ID)) {
             $this->error('query_var:user', 'No user by that name');
         }
         $where .= " AND t.topic_poster = {$q['topic_author_id']}";
     }
     if (!$q['topic_status']) {
         $where .= " AND t.topic_status = '0'";
     } elseif (false === strpos($q['topic_status'], 'all')) {
         $stati = array('normal' => 0, 'deleted' => 1);
         $q['topic_status'] = str_replace(array_keys($stati), array_values($stati), $q['topic_status']);
         $where .= $this->parse_value('t.topic_status', $q['topic_status']);
     }
     if (false !== $q['open'] && false === strpos($q['open'], 'all')) {
         $stati = array('no' => 0, 'closed' => 0, 'yes' => 1, 'open' => 1);
         $q['open'] = str_replace(array_keys($stati), array_values($stati), $q['open']);
         $where .= $this->parse_value('t.topic_open', $q['open']);
     }
     if (false !== $q['sticky'] && false === strpos($q['sticky'], 'all')) {
         $stickies = array('no' => 0, 'normal' => 0, 'forum' => 1, 'super' => 2, 'front' => 2, 'sticky' => '-0');
         $q['sticky'] = str_replace(array_keys($stickies), array_values($stickies), $q['sticky']);
         $where .= $this->parse_value('t.topic_sticky', $q['sticky']);
     }
     if (false !== $q['post_count']) {
         $where .= $this->parse_value('t.topic_posts', $q['post_count']);
     }
     if (false !== $q['tag_count']) {
         $where .= $this->parse_value('t.tag_count', $q['tag_count']);
     }
     if ($q['meta_key'] && ($q['meta_key'] = preg_replace('|[^a-z0-9_-]|i', '', $q['meta_key']))) {
         if ('-' == substr($q['meta_key'], 0, 1)) {
             $join .= " LEFT JOIN {$bbdb->meta} AS tm ON ( tm.object_type = 'bb_topic' AND t.topic_id = tm.object_id AND tm.meta_key = '" . substr($q['meta_key'], 1) . "' )";
             $where .= " AND tm.meta_key IS NULL";
         } else {
             $join .= " JOIN {$bbdb->meta} AS tm ON ( tm.object_type = 'bb_topic' AND t.topic_id = tm.object_id AND tm.meta_key = '{$q['meta_key']}' )";
             if ($q['meta_value']) {
                 $q['meta_value'] = maybe_serialize($q['meta_value']);
                 if (strpos($q['meta_value'], 'NULL') !== false) {
                     $join = ' LEFT' . $join;
                 }
                 $where .= $this->parse_value('tm.meta_value', $q['meta_value']);
             }
         }
     }
     // Just getting topic part for inclusion in post query
     if ($_part_of_post_query) {
         return $where;
     }
     $where .= $post_where;
     if ($where) {
         // Get rid of initial " AND " (this is pre-filters)
         $where = substr($where, 5);
     }
     if ($q['index_hint']) {
         $index_hint = $q['index_hint'];
     }
     if ($q['order_by']) {
         $order_by = $q['order_by'];
     } else {
         $order_by = 't.topic_time';
     }
     $bits = compact(array('distinct', 'sql_calc_found_rows', 'fields', 'index_hint', 'join', 'where', 'group_by', 'having', 'order_by'));
     $this->request = $this->_filter_sql($bits, "{$bbdb->topics} AS t");
     return $this->request;
 }
function bb_update_forum($args)
{
    global $bbdb;
    if (!bb_current_user_can('manage_forums')) {
        return false;
    }
    $defaults = array('forum_id' => 0, 'forum_name' => '', 'forum_slug' => '', 'forum_desc' => '', 'forum_parent' => 0, 'forum_order' => 0, 'forum_is_category' => 0);
    $fields = array('forum_name', 'forum_desc', 'forum_parent', 'forum_order');
    $args = nxt_parse_args($args, $defaults);
    if (1 < func_num_args()) {
        // For back compat
        $args['forum_id'] = func_get_arg(0);
        $args['forum_name'] = func_get_arg(1);
        $args['forum_desc'] = 2 < func_num_args() ? func_get_arg(2) : '';
        $args['forum_order'] = 3 < func_num_args() && is_numeric(func_get_arg(3)) ? func_get_arg(3) : 0;
    }
    extract($args, EXTR_SKIP);
    if (!($forum_id = (int) $forum_id)) {
        return false;
    }
    if (!($forum = bb_get_forum($forum_id))) {
        return false;
    }
    $forum_order = (int) $forum_order;
    $forum_parent = (int) $forum_parent;
    $forum_is_category = (int) $forum_is_category;
    $forum_name = apply_filters('bb_pre_forum_name', stripslashes(nxt_specialchars_decode($forum_name, ENT_QUOTES)), $forum_id);
    $forum_desc = apply_filters('bb_pre_forum_desc', stripslashes($forum_desc), $forum_id);
    if (strlen($forum_name) < 1) {
        return false;
    }
    // Slug is not changing, don't update it
    if (!$forum_slug || $forum_slug == $forum->forum_slug) {
        // [sic]
    } else {
        $forum_slug = $_forum_slug = bb_slug_sanitize($forum_slug);
        if (strlen($_forum_slug) < 1) {
            return false;
        }
        $forum_sql = "SELECT forum_slug FROM {$bbdb->forums} WHERE forum_slug = %s";
        while (is_numeric($forum_slug) || ($existing_slug = $bbdb->get_var($bbdb->prepare($forum_sql, $forum_slug)))) {
            $forum_slug = bb_slug_increment($_forum_slug, $existing_slug);
        }
        $fields[] = 'forum_slug';
    }
    nxt_cache_delete($forum_id, 'bb_forum');
    nxt_cache_flush('bb_forums');
    $update_result = $bbdb->update($bbdb->forums, compact($fields), compact('forum_id'));
    if ($forum_is_category) {
        bb_update_forummeta($forum_id, 'forum_is_category', $forum_is_category);
    } else {
        bb_delete_forummeta($forum_id, 'forum_is_category');
    }
    return $update_result;
}
Пример #6
0
function bb_get_sql_from_slug($table, $slug, $slug_length = 255)
{
    global $bbdb;
    // Look for new style equiv of old style slug
    $_slug = bb_slug_sanitize((string) $slug);
    if (strlen($_slug) < 1) {
        return '';
    }
    if (strlen($_slug) > $slug_length && preg_match('/^.*-([0-9]+)$/', $_slug, $m)) {
        $_slug = bb_encoded_utf8_cut($_slug, $slug_length - 1 - strlen($number));
        $number = (int) $m[1];
        $_slug = "{$_slug}-{$number}";
    }
    return array($_slug, $bbdb->prepare("{$table}_slug = %s", $_slug));
}
function get_view_link($_view = false, $page = 1, $context = BB_URI_CONTEXT_A_HREF)
{
    global $view, $bb_views;
    if ($_view) {
        $v = bb_slug_sanitize($_view);
    } else {
        $v =& $view;
    }
    if (!$context || !is_integer($context)) {
        $context = BB_URI_CONTEXT_A_HREF;
    }
    if (!array_key_exists($v, $bb_views)) {
        return bb_get_uri(null, null, $context);
    }
    if (bb_get_option('mod_rewrite')) {
        $page = 1 < $page ? '/page/' . $page : '';
        $link = bb_get_uri('view/' . $v . $page, null, $context);
    } else {
        $query = array('view' => $v, 'page' => 1 < $page ? $page : false);
        $link = bb_get_uri('view.php', $query, $context);
    }
    return apply_filters('get_view_link', $link, $v, $page, $context);
}
Пример #8
0
<?php

require_once './bb-load.php';
bb_repermalink();
$view = bb_slug_sanitize($view);
$sticky_count = $topic_count = 0;
$stickies = $topics = $view_count = false;
if (isset($bb_views[$view])) {
    if ($bb_views[$view]['sticky']) {
        $sticky_query = bb_view_query($view, array('sticky' => '-no'));
        // -no = yes
        $stickies = $sticky_query->results;
        $sticky_count = $sticky_query->found_rows;
    }
    $topic_query = bb_view_query($view, array('count' => true));
    $topics = $topic_query->results;
    $topic_count = $topic_query->found_rows;
    $view_count = max($sticky_count, $topic_count);
}
do_action('bb_custom_view', $view, $page);
bb_load_template('view.php', array('view_count', 'stickies'), $view);
Пример #9
0
/**
 * Custom insert topic 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. Update topic things have also been removed.
 *
 * @param mixed $args
 * @return int|bool New topic ID if post was created, otherwise false
 */
function w2bc_insert_topic($args = null)
{
    global $bbdb;
    if (!($args = wp_parse_args($args))) {
        return false;
    }
    $fields = array_keys($args);
    $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' => 1);
    // 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']);
    $forum_id = (int) $forum_id;
    if (bb_is_user_logged_in() || bb_is_login_required()) {
        if (!($user = bb_get_user($topic_poster))) {
            if (!($user = bb_get_user($topic_poster_name, array('by' => 'login')))) {
                return false;
            }
        }
        $topic_poster = $topic_last_poster = $user->ID;
        $topic_poster_name = $topic_last_poster_name = $user->user_login;
    }
    if (in_array('topic_title', $fields)) {
        $topic_title = stripslashes($topic_title);
        $topic_title = apply_filters('pre_topic_title', $topic_title, $topic_id);
        if (strlen($topic_title) < 1) {
            return false;
        }
    }
    if (in_array('topic_slug', $fields)) {
        $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';
        }
        if ($slug = $bbdb->get_var($bbdb->prepare("SELECT topic_slug FROM {$bbdb->topics} WHERE topic_slug = %s", $topic_slug))) {
            echo "<li>A topic with the slug <em>{$slug}</em> already exists and hence to prevent duplicate topics, the topic wasn't added.";
            return false;
        }
    }
    $bbdb->insert($bbdb->topics, compact($fields));
    $topic_id = $bbdb->insert_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 ($tags = stripslashes($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;
}