root/tags/0.8.2.1/bb-includes/functions.php

Revision 864, 71.2 kB (checked in by mdawaffe, 1 year ago)

tag 0.8.2.1

  • Property svn:eol-style set to native
Line 
1 <?php
2 /* INIT */
3
4 function bb_global_sanitize( $array, $trim = true ) {
5     foreach ($array as $k => $v) {
6         if ( is_array($v) ) {
7             $array[$k] = bb_global_sanitize($v);
8         } else {
9             if ( !get_magic_quotes_gpc() )
10                 $array[$k] = addslashes($v);
11             if ( $trim )
12                 $array[$k] = trim($array[$k]);
13         }
14     }
15     return $array;
16 }
17
18 function bb_is_installed() { // Maybe we should grab all forums and cache them.
19     global $bbdb;
20     $bbdb->hide_errors();
21     $installed = $bbdb->get_var("SELECT * FROM $bbdb->forums LIMIT 1");
22     $bbdb->show_errors();
23     return $installed;
24 }
25
26 /* Forums */
27
28 function bb_get_forums_hierarchical( $root = 0, $depth = 0, $_leaves = false ) {
29     $root = (int) $root;
30
31     $_recursed = (bool) $_leaves;
32
33     if ( false === $_leaves )
34         $_leaves = get_forums();
35
36     if ( !$_leaves )
37         return false;
38
39     $branch = array();
40
41     foreach ( $_leaves as $l => $leaf ) {
42         if ( $root == $leaf->forum_parent ) {
43             $new_root = (int) $leaf->forum_id;
44             unset($_leaves[$l]);
45             $branch[$new_root] = 1 == $depth ? true : bb_get_forums_hierarchical( $new_root, $depth - 1, $_leaves );
46         }
47     }
48
49     if ( !$_recursed )
50         return $tree = empty($branch) ? false : $branch;
51
52     return $branch ? $branch : true;
53 }
54
55 function get_forums( $args = null ) {
56     if ( is_numeric($args) ) {
57         $args = array( 'child_of' => $args, 'hierarchical' => 1, 'depth' => 0 );
58     } elseif ( is_callable($args) ) {
59         $args = array( 'callback' => $args );
60         if ( 1 < func_num_args() )
61             $args['callback_args'] = func_get_arg(1);
62     }
63
64     $defaults = array( 'callback' => false, 'callback_args' => false, 'child_of' => 0, 'hierarchical' => 0, 'depth' => 0 );
65     $args = wp_parse_args( $args, $defaults );
66
67     extract($args, EXTR_SKIP);
68     $child_of = (int) $child_of;
69     $hierarchical = 'false' == $hierarchical ? false : (bool) $hierarchical;
70     $depth = (int) $depth;
71
72     global $bb_cache;
73     $forums = (array) apply_filters( 'get_forums', $bb_cache->get_forums() );
74
75     if ( $child_of || $hierarchical || $depth ) {
76         $_forums = bb_get_forums_hierarchical( $child_of, $depth );
77         $_forums = (array) bb_flatten_array( $_forums );
78
79         foreach ( array_keys($_forums) as $_id )
80             $_forums[$_id] = $forums[$_id];
81
82         $forums = $_forums;
83     }
84
85     if ( !is_callable($callback) )
86         return $forums;
87
88     if ( !is_array($callback_args) )
89         $callback_args = array();
90
91     foreach ( array_keys($forums) as $f ) :
92         $_callback_args = $callback_args;
93         array_push( $_callback_args, $forums[$f]->forum_id );
94         if ( false == call_user_func_array( $callback, $_callback_args ) ) // $forum_id will be last arg;
95             unset($forums[$f]);
96     endforeach;
97     return $forums;
98 }
99
100 function get_forum( $id ) {
101     global $bb_cache;
102     return $bb_cache->get_forum( $id );
103 }
104
105 /* Topics */
106
107 function get_topic( $id, $cache = true ) {
108     global $bb_cache, $bb_topic_cache;
109     if ( !$id = (int) $id )
110         return false;
111     if ( isset( $bb_topic_cache[$id] ) && $cache )
112         return $bb_topic_cache[$id];
113     else
114         return $bb_cache->get_topic($id, $cache);
115 }
116
117 function get_latest_topics( $forum = 0, $page = 1, $exclude = '') {
118     global $bbdb;
119     $forum = (int) $forum;
120     $page = (int) $page;
121     $where = 'WHERE topic_status = 0';
122     if ( $forum )
123         $where .= " AND forum_id = $forum ";
124     if ( !empty( $exclude ) )
125         $where .= " AND forum_id NOT IN ('$exclude') ";
126     if ( is_front() )
127         $where .= " AND topic_sticky <> 2 ";
128     elseif ( is_forum() || is_view() )
129         $where .= " AND topic_sticky = 0 ";
130     $limit = bb_get_option('page_topics');
131     $where = apply_filters('get_latest_topics_where', $where);
132     $order_by = apply_filters('get_latest_topics_order_by', 'topic_time DESC' );
133     if ( 1 < $page )
134         $limit = ($limit * ($page - 1)) . ", $limit";
135     if ( $topics = $bbdb->get_results("SELECT SQL_CALC_FOUND_ROWS * FROM $bbdb->topics $where ORDER BY $order_by LIMIT $limit") )
136         return bb_append_meta( $topics, 'topic' );
137     else
138         return false;
139 }
140
141 function get_sticky_topics( $forum = 0, $display = 1 ) {
142     global $bbdb;
143     if ( 1 != $display )
144         return false;
145     $forum = (int) $forum;
146     if ( is_front() )
147         $where = 'WHERE topic_sticky = 2  AND topic_status = 0';
148     else    $where = 'WHERE topic_sticky <> 0 AND topic_status = 0';
149     if ( $forum )
150         $where .= " AND forum_id = $forum ";
151     $where = apply_filters('get_sticky_topics_where', $where);
152     $order_by = apply_filters('get_sticky_topics_order_by', 'topic_time DESC' );
153     if ( $stickies = $bbdb->get_results("SELECT SQL_CALC_FOUND_ROWS * FROM $bbdb->topics $where ORDER BY $order_by") )
154         return bb_append_meta( $stickies, 'topic' );   
155     else    return false;
156 }
157
158 function get_recent_user_threads( $user_id ) {
159     global $bbdb, $page;
160     $limit = bb_get_option('page_topics');
161     if ( 1 < $page )
162         $limit = ($limit * ($page - 1)) . ", $limit";
163     $join = apply_filters('get_recent_user_threads_join', '', $user_id);
164     $where = apply_filters('get_recent_user_threads_where', 'AND topic_status = 0', $user_id);
165     $order_by = apply_filters( 'get_recent_user_threads_order_by', 'topic_start_time DESC', $user_id);
166     if ( $topics = $bbdb->get_results("SELECT SQL_CALC_FOUND_ROWS * FROM $bbdb->topics $join WHERE topic_poster = $user_id $where ORDER BY $order_by LIMIT $limit") )
167         $topics = bb_append_meta( $topics, 'topic' );
168     return $topics;
169 }
170
171 function bb_new_topic( $title, $forum, $tags = '' ) {
172     global $bbdb, $bb_cache;
173     $title = apply_filters('pre_topic_title', $title, false);
174     $title = bb_trim_for_db( $title, 150 );
175     $slug  = $_slug = bb_slug_sanitize($title);
176     while ( is_numeric($slug) || $existing_slug = $bbdb->get_var("SELECT topic_slug FROM $bbdb->topics WHERE topic_slug = '$slug'") )
177         $slug = bb_slug_increment($_slug, $existing_slug);
178
179     $forum = (int) $forum;
180     $now   = bb_current_time('mysql');
181
182     $id = bb_get_current_user_info( 'id' );
183     $name = bb_get_current_user_info( 'name' );
184
185     if ( $forum && $title ) {
186         $bbdb->query("INSERT INTO $bbdb->topics
187         (topic_title, topic_slug, topic_poster, topic_poster_name, topic_last_poster, topic_last_poster_name, topic_start_time, topic_time, forum_id)
188         VALUES
189         ('$title',    '$slug',    $id,          '$name',           $id,               '$name',                '$now',           '$now',     $forum)");
190         $topic_id = $bbdb->insert_id;
191         if ( !empty( $tags ) )
192             add_topic_tags( $topic_id, $tags );
193         $bbdb->query("UPDATE $bbdb->forums SET topics = topics + 1 WHERE forum_id = $forum");
194         $bb_cache->flush_many( 'forum', $forum_id );
195         do_action('bb_new_topic', $topic_id);
196         return $topic_id;
197     } else {
198         return false;
199     }
200 }
201
202 function bb_update_topic( $title, $topic_id ) {
203     global $bbdb, $bb_cache;
204     $title = apply_filters('pre_topic_title', $title, $topic_id);
205     $topic_id = (int) $topic_id;
206
207     if ( $topic_id && $title ) {
208         $bbdb->query("UPDATE $bbdb->topics SET topic_title = '$title' WHERE topic_id = $topic_id");
209         $bb_cache->flush_one( 'topic', $topic_id );
210         do_action('bb_update_topic', $topic_id);
211         return $topic_id;
212     } else {
213         return false;
214     }
215 }
216
217 function bb_delete_topic( $topic_id, $new_status = 0 ) {
218     global $bbdb, $bb_cache, $bb_table_prefix;
219     $topic_id = (int) $topic_id;
220     add_filter( 'get_topic_where', 'no_where' );
221     if ( $topic = get_topic( $topic_id ) ) {
222         $new_status = (int) $new_status;
223         $old_status = (int) $topic->topic_status;
224         if ( $new_status == $old_status )
225             return;
226         if ( 0 != $old_status && 0 == $new_status )
227             add_filter('get_thread_post_ids_where', 'no_where');
228         $post_ids = get_thread_post_ids( $topic_id );
229         $post_ids['post'] = array_reverse((array) $post_ids['post']);
230         foreach ( $post_ids['post'] as $post_id )
231             _bb_delete_post( $post_id, $new_status );
232
233         $ids = array_unique((array) $post_ids['poster']);
234         foreach ( $ids as $id )
235             if ( $user = bb_get_user( $id ) )
236                 bb_update_usermeta( $user->ID, $bb_table_prefix . 'topics_replied', ( $old_status ? $user->topics_replied + 1 : $user->topics_replied - 1 ) );
237
238         if ( $new_status ) {
239             bb_remove_topic_tags( $topic_id );
240             $bbdb->query("UPDATE $bbdb->topics SET topic_status = '$new_status', tag_count = 0 WHERE topic_id = '$topic_id'");
241             $bbdb->query("UPDATE $bbdb->forums SET topics = topics - 1, posts = posts - '$topic->topic_posts' WHERE forum_id = '$topic->forum_id'");
242         } else {
243             $bbdb->query("UPDATE $bbdb->topics SET topic_status = '$new_status' WHERE topic_id = '$topic_id'");
244             $topic_posts = $bbdb->get_var("SELECT COUNT(*) FROM $bbdb->posts WHERE topic_id = '$topic_id' AND post_status = 0");
245             $all_posts = $bbdb->get_var("SELECT COUNT(*) FROM $bbdb->posts WHERE topic_id = '$topic_id'");
246             bb_update_topicmeta( $topic_id, 'deleted_posts', $all_posts - $topic_posts );
247             $bbdb->query("UPDATE $bbdb->forums SET topics = topics + 1, posts = posts + '$topic_posts' WHERE forum_id = '$topic->forum_id'");
248             $bbdb->query("UPDATE $bbdb->topics SET topic_posts = '$topic_posts' WHERE topic_id = '$topic_id'");
249             bb_topic_set_last_post( $topic_id );
250             update_post_positions( $topic_id );
251         }
252             
253         do_action( 'bb_delete_topic', $topic_id, $new_status, $old_status );
254         $bb_cache->flush_one( 'topic', $topic_id );
255         $bb_cache->flush_many( 'thread', $topic_id );
256         return $topic_id;
257     } else {
258         return false;
259     }
260 }
261
262 function bb_move_topic( $topic_id, $forum_id ) {
263     global $bbdb, $bb_cache;
264     $topic_id = (int) $topic_id;
265     $forum_id = (int) $forum_id;
266     $topic = get_topic( $topic_id );
267     if ( $topic && $topic->forum_id != $forum_id && get_forum( $forum_id ) ) {
268         $bbdb->query("UPDATE $bbdb->posts SET forum_id = $forum_id WHERE topic_id = $topic_id");
269         $bbdb->query("UPDATE $bbdb->topics SET forum_id = $forum_id WHERE topic_id = $topic_id");
270         $bbdb->query("UPDATE $bbdb->forums SET topics = topics + 1, posts = posts + $topic->topic_posts WHERE forum_id = $forum_id");
271         $bbdb->query("UPDATE $bbdb->forums SET topics = topics - 1, posts = posts - $topic->topic_posts WHERE forum_id = $topic->forum_id");
272         $bb_cache->flush_one( 'topic', $topic_id );
273         $bb_cache->flush_many( 'forum', $forum_id );
274         return $forum_id;
275     }
276     return false;
277 }
278
279 function bb_topic_set_last_post( $topic_id ) {
280     global $bbdb;
281     $old_post = $bbdb->get_row("SELECT post_id, poster_id, post_time FROM $bbdb->posts WHERE topic_id = $topic_id AND post_status = 0 ORDER BY post_time DESC LIMIT 1");
282     $old_name = $bbdb->get_var("SELECT user_login FROM $bbdb->users WHERE ID = $old_post->poster_id");
283     $bbdb->query("UPDATE $bbdb->topics SET topic_time = '$old_post->post_time', topic_last_poster = $old_post->poster_id, topic_last_poster_name = '$old_name', topic_last_post_id = $old_post->post_id WHERE topic_id = $topic_id");
284 }   
285
286 function bb_close_topic( $topic_id ) {
287     global $bbdb, $bb_cache;
288     $topic_id = (int) $topic_id;
289     $bb_cache->flush_one( 'topic', $topic_id );
290     $r = $bbdb->query("UPDATE $bbdb->topics SET topic_open = '0' WHERE topic_id = $topic_id");
291     do_action('close_topic', $topic_id, $r);
292     return $r;
293 }
294
295 function bb_open_topic( $topic_id ) {
296     global $bbdb, $bb_cache;
297     $topic_id = (int) $topic_id;
298     $bb_cache->flush_one( 'topic', $topic_id );
299     $r = $bbdb->query("UPDATE $bbdb->topics SET topic_open = '1' WHERE topic_id = $topic_id");
300     do_action('open_topic', $topic_id, $r);
301     return $r;
302 }
303
304 function bb_stick_topic( $topic_id, $super = 0 ) {
305     global $bbdb, $bb_cache;
306     $topic_id = (int) $topic_id;
307     $stick = 1 + abs((int) $super);
308     $bb_cache->flush_one( 'topic', $topic_id );
309     $r = $bbdb->query("UPDATE $bbdb->topics SET topic_sticky = '$stick' WHERE topic_id = $topic_id");
310     do_action('stick_topic', $topic_id, $r);
311 }
312
313 function bb_unstick_topic( $topic_id ) {
314     global $bbdb, $bb_cache;
315     $topic_id = (int) $topic_id;
316     $bb_cache->flush_one( 'topic', $topic_id );
317     $r = $bbdb->query("UPDATE $bbdb->topics SET topic_sticky = '0' WHERE topic_id = $topic_id");
318     do_action('unstick_topic', $topic_id, $r);
319     return $r;
320 }
321
322 function topic_is_open( $topic_id = 0 ) {
323     $topic = get_topic( get_topic_id( $topic_id ) );
324     return 1 == $topic->topic_open;
325 }
326
327 function topic_is_sticky( $topic_id = 0 ) {
328     $topic = get_topic( get_topic_id( $topic_id ) );
329     return '0' !== $topic->topic_sticky;
330 }
331
332 /* Thread */ // Thread, topic?  Guh-wah?  TODO: consistency in nomenclature
333
334 function get_thread( $topic_id, $page = 1, $reverse = 0 ) {
335     global $bb_cache;
336     return $bb_cache->get_thread( $topic_id, $page, $reverse );
337 }
338
339 function get_thread_post_ids( $topic_id ) {
340     global $bbdb, $thread_ids_cache;
341     if ( !isset( $thread_ids_cache[$topic_id] ) ) {
342         $where = apply_filters('get_thread_post_ids_where', 'AND post_status = 0');
343         $thread_ids_cache[$topic_id]['post'] = (array) $bbdb->get_col("SELECT post_id, poster_id FROM $bbdb->posts WHERE topic_id = $topic_id $where ORDER BY post_time");
344         $thread_ids_cache[$topic_id]['poster'] = (array) $bbdb->get_col('', 1);
345     }
346     return $thread_ids_cache[$topic_id];
347 }
348
349 /* Posts */
350
351 function bb_get_post( $post_id ) {
352     global $bb_post_cache, $bbdb;
353     $post_id = (int) $post_id;
354     if ( !isset( $bb_post_cache[$post_id] ) )
355         $bb_post_cache[$post_id] = $bbdb->get_row("SELECT * FROM $bbdb->posts WHERE post_id = $post_id");
356     return $bb_post_cache[$post_id];
357 }
358
359 function bb_is_first( $post_id ) { // First post in thread
360     global $bbdb;
361     $bb_post = bb_get_post( $post_id );
362     $where = apply_filters('bb_is_first_where', 'AND post_status = 0');
363     $first_post = $bbdb->get_var("SELECT post_id FROM $bbdb->posts WHERE topic_id = $bb_post->topic_id $where ORDER BY post_id ASC LIMIT 1");
364
365     return $post_id == $first_post;
366 }
367
368 // Globalizes the result.
369 function bb_get_first_post( $_topic = false, $author_cache = true ) {
370     global $topic, $bb_first_post_cache, $bb_post;
371     if ( !$_topic )
372         $topic_id = (int) $topic->topic_id;
373     else if ( is_object($_topic) )
374         $topic_id = (int) $_topic->topic_id;
375     else if ( is_numeric($_topic) )
376         $topic_id = (int) $_topic;
377
378     if ( !$topic_id )
379         return false;
380
381     if ( isset($bb_first_post_cache[$topic_id]) ) {
382         $post = bb_get_post( $bb_first_post_cache[$topic_id] );
383     } else {
384         $first_posts = bb_cache_first_posts( array($topic_id), $author_cache );
385         if ( isset($first_posts[$topic_id]) )
386             $post = $first_posts[$topic_id];
387     }
388
389     if ( $post ) {
390         $bb_post = $post;
391         return $bb_post;
392     }
393
394     return false;
395 }
396
397 // Ignore the return value.  Cache first posts with this function and use bb_get_first_post to grab each.
398 function bb_cache_first_posts( $_topics = false, $author_cache = true ) {
399     global $topics, $bb_first_post_cache, $bb_cache, $bbdb;
400     if ( !$_topics )
401         $_topics =& $topics;
402     if ( !is_array($_topics) )
403         return false;
404
405     $topic_ids = array();
406     foreach ( $_topics as $topic )
407         if ( is_object($topic) )
408             $topic_ids[] = (int) $topic->topic_id;
409         else if ( is_numeric($topic) )
410             $topic_ids[] = (int) $topic;
411
412     $_topic_ids = join(',', $topic_ids);
413
414     $posts = (array) $bb_cache->cache_posts( "SELECT * FROM $bbdb->posts WHERE topic_id IN ($_topic_ids) AND post_position = 1 AND post_status = 0" );
415
416     $first_posts = array();
417     foreach ( $posts as $post ) {
418         $bb_first_post_cache[(int) $post->topic_id] = (int) $post->post_id;
419         $first_posts[(int) $post->topic_id] = $post;
420     }
421
422     if ( $author_cache )
423         post_author_cache( $posts );
424
425     return $first_posts;
426 }
427
428 // Globalizes the result
429 function bb_get_last_post( $_topic = false, $author_cache = true ) {
430     global $topic, $bb_post;
431     if ( !$_topic )
432         $topic_id = (int) $topic->topic_id;
433     else if ( is_object($_topic) )
434         $topic_id = (int) $_topic->topic_id;
435     else if ( is_numeric($_topic) )
436         $topic_id = (int) $_topic;
437
438     if ( !$topic_id )
439         return false;
440
441     $_topic = get_topic( $topic_id );
442
443     if ( $post = bb_get_post( $_topic->topic_last_post_id ) ) {
444         if ( $author_cache )
445             post_author_cache( array($post) );
446         $bb_post = $post;
447     }
448
449     return $post;
450 }
451
452 // No return value. Cache last posts with this function and use bb_get_last_post to grab each.
453 function bb_cache_last_posts( $_topics = false, $author_cache = true ) {
454     global $topics, $bb_topic_cache, $bb_cache, $bbdb;
455     if ( !$_topics )
456         $_topics =& $topics;
457     if ( !is_array($_topics) )
458         return false;
459
460     $last_post_ids = array();
461     $topic_ids = array();
462     foreach ( $_topics