/svntrunk/bp-forums/bbpress/bb-admin/admin-ajax.php

https://bitbucket.org/simplemediacode/bptrunk · PHP · 264 lines · 174 code · 47 blank · 43 comment · 42 complexity · 5e282a4fb72ade293f16979ac1b80bcf MD5 · raw file

  1. <?php
  2. define( 'BB_IS_ADMIN', true );
  3. define( 'DOING_AJAX', true );
  4. require_once('../bb-load.php');
  5. if ( !class_exists( 'WP_Ajax_Response' ) )
  6. require_once( BACKPRESS_PATH . 'class.wp-ajax-response.php' );
  7. require_once( BB_PATH . 'bb-admin/includes/functions.bb-admin.php' );
  8. if ( !$bb_current_id = bb_get_current_user_info( 'id' ) )
  9. die('-1');
  10. function bb_grab_results() {
  11. global $ajax_results;
  12. $ajax_results = @ unserialize(func_get_arg(0));
  13. if ( false === $ajax_results )
  14. $ajax_results = func_get_args();
  15. return;
  16. }
  17. $id = (int) @$_POST['id'];
  18. switch ( $action = $_POST['action'] ) :
  19. case 'add-tag' : // $id is topic_id
  20. if ( !bb_current_user_can('edit_tag_by_on', $bb_current_id, $id) )
  21. die('-1');
  22. bb_check_ajax_referer( "add-tag_$id" );
  23. global $tag, $topic;
  24. add_action('bb_tag_added', 'bb_grab_results', 10, 3);
  25. add_action('bb_already_tagged', 'bb_grab_results', 10, 3);
  26. $tag_name = @$_POST['tag'];
  27. $tag_name = stripslashes( $tag_name );
  28. $topic = get_topic( $id );
  29. if ( !$topic )
  30. die('0');
  31. $tag_name = rawurldecode($tag_name);
  32. $x = new WP_Ajax_Response();
  33. foreach ( bb_add_topic_tags( $id, $tag_name ) as $tag_id ) {
  34. if ( !is_numeric($tag_id) || !$tag = bb_get_tag( (int) $tag_id, bb_get_current_user_info( 'id' ), $topic->topic_id ) ) {
  35. if ( !$tag = bb_get_tag( $tag_id ) ) {
  36. continue;
  37. }
  38. }
  39. $tag->tag_id = $tag_id;
  40. $tag->user_id = bb_get_current_user_info( 'id' );
  41. $tag_id_val = $tag->tag_id . '_' . $tag->user_id;
  42. $tag->raw_tag = esc_attr( $tag_name );
  43. $x->add( array(
  44. 'what' => 'tag',
  45. 'id' => $tag_id_val,
  46. 'data' => _bb_list_tag_item( $tag, array( 'list_id' => 'tags-list', 'format' => 'list' ) )
  47. ) );
  48. }
  49. $x->send();
  50. break;
  51. case 'delete-tag' :
  52. list($tag_id, $user_id) = explode('_', $_POST['id']);
  53. $tag_id = (int) $tag_id;
  54. $user_id = (int) $user_id;
  55. $topic_id = (int) $_POST['topic_id'];
  56. if ( !bb_current_user_can('edit_tag_by_on', $user_id, $topic_id) )
  57. die('-1');
  58. bb_check_ajax_referer( "remove-tag_$tag_id|$topic_id" );
  59. add_action('bb_rpe_tag_removed', 'bb_grab_results', 10, 3);
  60. $tag = bb_get_tag( $tag_id );
  61. $user = bb_get_user( $user_id );
  62. $topic = get_topic ( $topic_id );
  63. if ( !$tag || !$topic )
  64. die('0');
  65. if ( false !== bb_remove_topic_tag( $tag_id, $user_id, $topic_id ) )
  66. die('1');
  67. break;
  68. case 'dim-favorite' :
  69. $user_id = bb_get_current_user_info( 'id' );
  70. if ( !$topic = get_topic( $id ) )
  71. die('0');
  72. if ( !bb_current_user_can( 'edit_favorites_of', $user_id ) )
  73. die('-1');
  74. bb_check_ajax_referer( "toggle-favorite_$topic->topic_id" );
  75. $is_fav = is_user_favorite( $user_id, $topic->topic_id );
  76. if ( 1 == $is_fav ) {
  77. if ( bb_remove_user_favorite( $user_id, $topic->topic_id ) )
  78. die('1');
  79. } elseif ( false === $is_fav ) {
  80. if ( bb_add_user_favorite( $user_id, $topic->topic_id ) )
  81. die('1');
  82. }
  83. break;
  84. case 'delete-post' : // $id is post_id
  85. if ( !bb_current_user_can( 'delete_post', $id ) )
  86. die('-1');
  87. bb_check_ajax_referer( "delete-post_$id" );
  88. $status = (int) $_POST['status'];
  89. if ( !$bb_post = bb_get_post( $id ) )
  90. die('0');
  91. if ( $status == $bb_post->post_status )
  92. die('1'); // We're already there
  93. if ( bb_delete_post( $id, $status ) ) {
  94. $topic = get_topic( $bb_post->topic_id );
  95. if ( 0 == $topic->topic_posts ) {
  96. // If we deleted the only post, send back a WP_Ajax_Response object with a URL to redirect to
  97. if ( $ref = wp_get_referer() ) {
  98. $ref_topic = bb_get_topic_from_uri( $ref );
  99. if ( $ref_topic && $ref_topic->topic_id == $topic->topic_id )
  100. $ref = add_query_arg( 'view', 'all', $ref );
  101. if ( false === strpos( $ref, '#' ) )
  102. $ref .= "#post-{$bb_post->post_id}";
  103. } else {
  104. $ref = add_query_arg( 'view', 'all', get_post_link( $topic->topic_id ) );
  105. }
  106. $x = new WP_Ajax_Response( array(
  107. 'what' => 'post',
  108. 'id' => $bb_post->post_id,
  109. 'data' => $ref,
  110. ) );
  111. $x->send();
  112. }
  113. die('1');
  114. }
  115. break;
  116. /*
  117. case 'add-post' : // Can put last_modified stuff back in later
  118. bb_check_ajax_referer( $action );
  119. $error = false;
  120. $post_id = 0;
  121. $topic_id = (int) $_POST['topic_id'];
  122. $last_mod = (int) $_POST['last_mod'];
  123. if ( !$post_content = trim($_POST['post_content']) )
  124. $error = new WP_Error( 'no-content', __('You need to actually submit some content!') );
  125. if ( !bb_current_user_can( 'write_post', $topic_id ) )
  126. die('-1');
  127. if ( !$topic = get_topic( $topic_id ) )
  128. die('0');
  129. if ( !topic_is_open( $topic_id ) )
  130. $error = new WP_Error( 'topic-closed', __('This topic is closed.') );
  131. if ( $throttle_time = bb_get_option( 'throttle_time' ) )
  132. if ( isset($bb_current_user->data->last_posted) && time() < $bb_current_user->data->last_posted + $throttle_time && !bb_current_user_can('throttle') )
  133. $error = new WP_Error( 'throttle-limit', sprintf( __('Slow down! You can only post every %d seconds.'), $throttle_time );
  134. if ( !$error ) :
  135. if ( !$post_id = bb_new_post( $topic_id, rawurldecode($_POST['post_content']) ) )
  136. die('0');
  137. $bb_post = bb_get_post( $post_id );
  138. $new_page = bb_get_page_number( $bb_post->post_position );
  139. ob_start();
  140. echo "<li id='post-$post_id'>";
  141. bb_post_template();
  142. echo '</li>';
  143. $data = ob_get_contents();
  144. ob_end_clean();
  145. endif;
  146. $x = new WP_Ajax_Response( array(
  147. 'what' => 'post',
  148. 'id' => $post_id,
  149. 'data' => is_wp_error($error) ? $error : $data
  150. ) );
  151. $x->send();
  152. break;
  153. */
  154. case 'add-forum' :
  155. if ( !bb_current_user_can( 'manage_forums' ) )
  156. die('-1');
  157. bb_check_ajax_referer( $action );
  158. if ( !$forum_id = bb_new_forum( $_POST ) )
  159. die('0');
  160. global $forums_count;
  161. $forums_count = 2; // Hack
  162. $data = bb_forum_row( $forum_id, false, true );
  163. $forum = bb_get_forum( $forum_id );
  164. if ( $forum->forum_parent ) {
  165. $siblings = bb_get_forums( $forum->forum_parent );
  166. $last_sibling = array_pop( $siblings );
  167. if ( $last_sibling->forum_id == $forum_id )
  168. $last_sibling = array_pop( $siblings );
  169. if ( $last_sibling ) {
  170. $position = "forum-$last_sibling->forum_id";
  171. } else {
  172. $position = "+forum-$forum->forum_parent";
  173. $data = "<ul id='forum-root-$forum->forum_parent' class='list-block holder'>$data</ul>";
  174. }
  175. } else {
  176. $position = 1;
  177. }
  178. $x = new WP_Ajax_Response( array(
  179. 'what' => 'forum',
  180. 'id' => $forum_id,
  181. 'data' => $data,
  182. 'position' => $position,
  183. 'supplemental' => array( 'name' => $forum->forum_name )
  184. ) );
  185. $x->send();
  186. break;
  187. case 'order-forums' :
  188. if ( !bb_current_user_can( 'manage_forums' ) )
  189. die('-1');
  190. bb_check_ajax_referer( $action );
  191. if ( !is_array($_POST['order']) )
  192. die('0');
  193. global $bbdb;
  194. $forums = array();
  195. bb_get_forums(); // cache
  196. foreach ( $_POST['order'] as $pos => $forum_id ) :
  197. $forum = $bbdb->escape_deep( get_object_vars( bb_get_forum( $forum_id ) ) );
  198. $forum['forum_order'] = $pos;
  199. $forums[(int) $forum_id] = $forum;
  200. endforeach;
  201. foreach ( $_POST['root'] as $root => $ids )
  202. foreach ( $ids as $forum_id )
  203. $forums[(int) $forum_id]['forum_parent'] = (int) $root;
  204. foreach ( $forums as $forum )
  205. bb_update_forum( $forum );
  206. die('1');
  207. break;
  208. default :
  209. do_action( 'bb_ajax_' . $_POST['action'] );
  210. break;
  211. endswitch;
  212. die('0');
  213. ?>