/www/forum/bb-admin/admin-ajax.php

https://github.com/micz/elencode · PHP · 219 lines · 134 code · 43 blank · 42 comment · 26 complexity · 276eb16f6ab1fef7efac755e45062871 MD5 · raw file

  1. <?php
  2. require_once('../bb-load.php');
  3. require_once(BB_PATH . 'bb-admin/admin-functions.php');
  4. if ( !$bb_current_id = bb_get_current_user_info( 'id' ) )
  5. die('-1');
  6. define('DOING_AJAX', true);
  7. function bb_grab_results() {
  8. global $ajax_results;
  9. $ajax_results = @ unserialize(func_get_arg(0));
  10. if ( false === $ajax_results )
  11. $ajax_results = func_get_args();
  12. return;
  13. }
  14. $id = (int) @$_POST['id'];
  15. switch ( $action = $_POST['action'] ) :
  16. case 'add-tag' : // $id is topic_id
  17. if ( !bb_current_user_can('edit_tag_by_on', $bb_current_id, $id) )
  18. die('-1');
  19. bb_check_ajax_referer( "add-tag_$id" );
  20. global $tag, $topic;
  21. add_action('bb_tag_added', 'bb_grab_results', 10, 3);
  22. add_action('bb_already_tagged', 'bb_grab_results', 10, 3);
  23. $tag_name = @$_POST['tag'];
  24. $tag_name = stripslashes( $tag_name );
  25. $topic = get_topic( $id );
  26. if ( !$topic )
  27. die('0');
  28. $tag_name = rawurldecode($tag_name);
  29. $x = new WP_Ajax_Response();
  30. foreach ( bb_add_topic_tags( $id, $tag_name ) as $tag_id ) {
  31. if ( !is_numeric($tag_id) || !$tag = bb_get_tag( $tag_id, bb_get_current_user_info( 'id' ), $topic->topic_id ) )
  32. if ( !$tag = bb_get_tag( $tag_id ) )
  33. continue;
  34. $tag_id_val = $tag->tag_id . '_' . $tag->user_id;
  35. $tag->raw_tag = attribute_escape( $tag->raw_tag );
  36. $x->add( array(
  37. 'what' => 'tag',
  38. 'id' => $tag_id_val,
  39. 'data' => "<li id='tag-$tag_id_val'><a href='" . bb_get_tag_link() . "' rel='tag'>$tag->raw_tag</a> " . bb_get_tag_remove_link() . '</li>'
  40. ) );
  41. }
  42. $x->send();
  43. break;
  44. case 'delete-tag' :
  45. list($tag_id, $user_id) = explode('_', $_POST['id']);
  46. $tag_id = (int) $tag_id;
  47. $user_id = (int) $user_id;
  48. $topic_id = (int) $_POST['topic_id'];
  49. if ( !bb_current_user_can('edit_tag_by_on', $user_id, $topic_id) )
  50. die('-1');
  51. bb_check_ajax_referer( "remove-tag_$tag_id|$topic_id" );
  52. add_action('bb_rpe_tag_removed', 'bb_grab_results', 10, 3);
  53. $tag = bb_get_tag( $tag_id );
  54. $user = bb_get_user( $user_id );
  55. $topic = get_topic ( $topic_id );
  56. if ( !$tag || !$topic )
  57. die('0');
  58. if ( bb_remove_topic_tag( $tag_id, $user_id, $topic_id ) )
  59. die('1');
  60. break;
  61. case 'dim-favorite' :
  62. $topic_id = (int) @$_POST['topic_id'];
  63. $user_id = (int) @$_POST['user_id'];
  64. $topic = get_topic( $topic_id );
  65. $user = bb_get_user( $user_id );
  66. if ( !$topic || !$user )
  67. die('0');
  68. if ( !bb_current_user_can( 'edit_favorites_of', $user->ID ) )
  69. die('-1');
  70. bb_check_ajax_referer( "toggle-favorite_$topic_id" );
  71. $is_fav = is_user_favorite( $user_id, $topic_id );
  72. if ( 1 == $is_fav ) {
  73. if ( bb_remove_user_favorite( $user_id, $topic_id ) )
  74. die('1');
  75. } elseif ( false === $is_fav ) {
  76. if ( bb_add_user_favorite( $user_id, $topic_id ) )
  77. die('1');
  78. }
  79. break;
  80. case 'delete-post' : // $id is post_id
  81. if ( !bb_current_user_can( 'delete_post', $id ) )
  82. die('-1');
  83. bb_check_ajax_referer( "delete-post_$id" );
  84. $page = (int) $_POST['page'];
  85. $last_mod = (int) $_POST['last_mod'];
  86. $bb_post = bb_get_post( $id );
  87. if ( !$bb_post )
  88. die('0');
  89. $topic = get_topic( $bb_post->topic_id );
  90. if ( bb_delete_post( $id, 1 ) )
  91. die('1');
  92. break;
  93. /*
  94. case 'add-post' : // Can put last_modified stuff back in later
  95. bb_check_ajax_referer( $action );
  96. $error = false;
  97. $post_id = 0;
  98. $topic_id = (int) $_POST['topic_id'];
  99. $last_mod = (int) $_POST['last_mod'];
  100. if ( !$post_content = trim($_POST['post_content']) )
  101. $error = new WP_Error( 'no-content', __('You need to actually submit some content!') );
  102. if ( !bb_current_user_can( 'write_post', $topic_id ) )
  103. die('-1');
  104. if ( !$topic = get_topic( $topic_id ) )
  105. die('0');
  106. if ( !topic_is_open( $topic_id ) )
  107. $error = new WP_Error( 'topic-closed', __('This topic is closed.') );
  108. if ( $throttle_time = bb_get_option( 'throttle_time' ) )
  109. if ( isset($bb_current_user->data->last_posted) && time() < $bb_current_user->data->last_posted + $throttle_time && !bb_current_user_can('throttle') )
  110. $error = new WP_Error( 'throttle-limit', sprintf( __('Slow down! You can only post every %d seconds.'), $throttle_time );
  111. if ( !$error ) :
  112. if ( !$post_id = bb_new_post( $topic_id, rawurldecode($_POST['post_content']) ) )
  113. die('0');
  114. $bb_post = bb_get_post( $post_id );
  115. $new_page = get_page_number( $bb_post->post_position );
  116. ob_start();
  117. echo "<li id='post-$post_id'>";
  118. bb_post_template();
  119. echo '</li>';
  120. $data = ob_get_contents();
  121. ob_end_clean();
  122. endif;
  123. $x = new WP_Ajax_Response( array(
  124. 'what' => 'post',
  125. 'id' => $post_id,
  126. 'data' => is_wp_error($error) ? $error : $data
  127. ) );
  128. $x->send();
  129. break;
  130. */
  131. case 'add-forum' :
  132. if ( !bb_current_user_can( 'manage_forums' ) )
  133. die('-1');
  134. bb_check_ajax_referer( $action );
  135. if ( !$forum_id = bb_new_forum( $_POST ) )
  136. die('0');
  137. global $forums_count;
  138. $forums_count = 2; // Hack
  139. $x = new WP_Ajax_Response( array(
  140. 'what' => 'forum',
  141. 'id' => $forum_id,
  142. 'data' => bb_forum_row( $forum_id, false, true )
  143. ) );
  144. $x->send();
  145. break;
  146. case 'order-forums' :
  147. if ( !bb_current_user_can( 'manage_forums' ) )
  148. die('-1');
  149. bb_check_ajax_referer( $action );
  150. if ( !is_array($_POST['order']) )
  151. die('0');
  152. global $bbdb;
  153. $forums = array();
  154. get_forums(); // cache
  155. foreach ( $_POST['order'] as $pos => $forum_id ) :
  156. $forum = $bbdb->escape_deep( get_object_vars( get_forum( $forum_id ) ) );
  157. $forum['forum_order'] = $pos;
  158. $forums[(int) $forum_id] = $forum;
  159. endforeach;
  160. foreach ( $_POST['root'] as $root => $ids )
  161. foreach ( $ids as $forum_id )
  162. $forums[(int) $forum_id]['forum_parent'] = (int) $root;
  163. foreach ( $forums as $forum )
  164. bb_update_forum( $forum );
  165. die('1');
  166. break;
  167. default :
  168. do_action( 'bb_ajax_' . $_POST['action'] );
  169. break;
  170. endswitch;
  171. die('0');
  172. ?>