Version: 0.8.0 Author URI: http://bobrik.name Copyright 2008 Ivan Babroŭ (email : ibobrik@gmail.com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the license, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ // for version checking $bbwp_version = 80; $min_version = 78; require_once(dirname(__FILE__).'/../../bb-load.php'); // for mode checking $bbwp_plugin = 0; function bbwp_add_textdomain() { // setting textdomain for translation load_plugin_textdomain('bbwp-sync', dirname(__FILE__).'/'); } if (substr($_SERVER['PHP_SELF'], -13) != 'bbwp-sync.php') { global $bbwp_plugin; $bbwp_plugin = 1; } else { // listening commands from WordPress part bbwp_listener(); } // action => answer // for actions that only return data and don't change their arguments $bbwp_cachable_requests = array( 'test' => '', 'keytest' => '', 'get_wpbb_version' => '', 'get_categories' => '', 'check_wp_settings' => '' ); function bbwp_send_command($pairs = array()) { global $bbwp_cachable_requests; if (isset($bbwp_cachable_requests[$pairs['action']]) && !empty($bbwp_cachable_requests[$pairs['action']])) return $bbwp_cachable_requests[$pairs['action']]; $url = bb_get_option('bbwp_wordpress_url')."?wpbb-listener"; preg_match('@https?://([\-_\w\.]+)+(:(\d+))?/(.*)@', $url, $matches); if (!$matches) return; if (!isset($pairs['user'])) { $user = bb_get_current_user(); if ($user->ID) { $pairs['user'] = $user->ID; $pairs['username'] = $user->user_login; } else { // anonymous user $pairs['user'] = 0; } } $answer = ''; if (substr($url, 0, 5) == 'https') { // must use php-curl to work with https // FIXME: really works? :) $ch = curl_init($url); curl_setopt ($ch, CURLOPT_POST, 1); curl_setopt ($ch, CURLOPT_POSTFIELDS, $pairs); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $answer = curl_exec($ch); curl_close($ch); } else { $port = $matches[3] ? $matches[3] : 80; $request = ''; foreach ($pairs as $key => $data) $request .= $key.'='.urlencode(stripslashes($data)).'&'; $http_request = "POST /$matches[4] HTTP/1.0\r\n"; $http_request .= "Host: $matches[1]\r\n"; $http_request .= "Content-Type: application/x-www-form-urlencoded; charset=" . bb_get_option('charset') . "\r\n"; $http_request .= "Content-Length: " . strlen($request) . "\r\n"; $http_request .= "User-Agent: WordPress/".bb_get_option('version')." | WordPress-bbPress syncronization\r\n"; $http_request .= "\r\n"; $http_request .= $request; $response = ''; if( false != ( $fs = @fsockopen($matches[1], $port, $errno, $errstr, 10) ) ) { fwrite($fs, $http_request); while ( !feof($fs) ) $response .= fgets($fs, 1160); // One TCP-IP packet fclose($fs); $response = explode("\r\n\r\n", $response, 2); } $answer = $response[1]; } // f*cking windows dirty hacks. hate you, dumb idiots from micro$oft! $answer = trim(trim($answer, "\xEF\xBB\xBF")); $bbwp_cachable_requests[$pairs['action']] = $answer; return $answer; } function bbwp_test_pair() { $answer = bbwp_send_command(array('action' => 'test')); // return 1 if test passed, 0 otherwise // TODO: check configuration! $data = unserialize($answer); return $data['test'] == 1 ? 1 : 0; } function bbwp_secret_key_equal() { $answer = bbwp_send_command(array('action' => 'keytest', 'secret_key' => md5(bb_get_option('bbwp_secret_key')))); $data = unserialize($answer); return $data['keytest']; } function bbwp_compare_keys_local() { return $_POST['secret_key'] == md5(bb_get_option('bbwp_secret_key')) ? 1 : 0; } function bbwp_check_wpbb_version() { $answer = unserialize(bbwp_send_command(array('action' => 'get_wpbb_version'))); global $min_version; return ($answer['version'] < $min_version) ? 0 : 1; } function bbwp_listener() { if (empty($_POST['action'])) echo 'If you see that, plugin must connect well. WordPress test response (must be a:1:{s:4:"test";i:1;}): '.bbwp_send_command(array('action' => 'test'));; // setting authorized user if ($_POST['user'] != 0) { bb_set_current_user($_POST['user']); } else { bb_set_current_user(bb_get_option('bbwp_anonymous_user_id')); } // error_log("GOT COMMAND for bbPress part: ".$_POST['action']); if ($_POST['action'] == 'test') { echo serialize(array('test' => 1)); return; } elseif ($_POST['action'] == 'keytest') { echo serialize(array('keytest' => bbwp_compare_keys_local())); return; } // here we need secret key, only if not checking settings if (!bbwp_secret_key_equal() && $_POST['action'] != 'check_bb_settings') { // go away, damn cheater! return; } if ($_POST['action'] == 'check_bb_settings') { $code = bbwp_check_bb_settings(); echo serialize(array('code' => $code, 'message' => bbwp_status_error($code))); } elseif ($_POST['action'] == 'set_bb_plugin_status') { bbwp_set_bb_plugin_status(); } elseif ($_POST['action'] == 'get_bbwp_version') { global $bbwp_version; echo serialize(array('version' => $bbwp_version)); } // we need enabled plugins for next actions if (bb_get_option('bbwp_plugin_status') != 'enabled') { // stop sync return; } if ($_POST['action'] == 'create_topic') { bbwp_create_bb_topic(); } elseif ($_POST['action'] == 'continue_topic') { bbwp_continue_bb_topic(); } elseif ($_POST['action'] == 'edit_post') { bbwp_edit_bb_post(); } elseif ($_POST['action'] == 'open_bb_topic') { bbwp_open_bb_topic(); } elseif ($_POST['action'] == 'close_bb_topic') { bbwp_close_bb_topic(); } elseif ($_POST['action'] == 'edit_bb_tags') { bbwp_edit_bb_tags(); } elseif ($_POST['action'] == 'delete_post') { bbwp_delete_bb_post(); } elseif ($_POST['action'] == 'get_topic_link') { echo serialize(array('link' => get_topic_link($_POST['topic_id']))); } } function bbwp_do_sync() { if (bb_get_option('bbwp_plugin_status') != 'enabled') return false; global $bbwp_plugin; if (!$bbwp_plugin) return false; return true; } function bbwp_sync_that_status($id) { if (bbwp_get_real_post_status($id) == 0 || bb_get_option('bbwp_sync_all_posts') == 'enabled') return true; else return false; } function bbwp_get_real_post_status($id) { global $bbdb; return $bbdb->get_var('SELECT post_status FROM '.$bbdb->prefix.'posts WHERE post_id = '.$id); } function bbwp_create_bb_topic() { $forum_id = bb_get_option('bbwp_forum_id'); $post_categories = unserialize(stripslashes($_POST['categories'])); $accordance = unserialize(bb_get_option('bbwp_forum_categories')); if (is_array($accordance)) foreach ($accordance as $category => $forum) if (in_array($category, $post_categories)) $forum_id = $forum; $tags = stripslashes($_POST['tags']); if (trim(bb_get_option('bbwp_crosspost_tag')) != '') $tags .= ', '.bb_get_option('bbwp_crosspost_tag'); $topic_id = bb_insert_topic(array('topic_title' => stripslashes($_POST['topic']), 'forum_id' => $forum_id, 'tags' => $tags)); remove_all_filters('pre_post'); $post_id = bb_insert_post(array('topic_id' => $topic_id, 'post_text' => stripslashes($_POST['post_content']))); bb_delete_post($post_id, bbwp_status_wp2bb($_POST['comment_approved'])); $result = bbwp_add_table_item($_POST["post_id"], 0, $topic_id, $post_id, '', '', ''); bb_update_meta($topic_id, 'bbwp_sync', '1', 'topic'); $data = serialize(array("topic_id" => $topic_id, "post_id" => $post_id, "result" => $result)); echo $data; } function bbwp_continue_bb_topic() { $row = bbwp_get_table_item('wp_post_id', $_POST["post_id"]); remove_all_filters('pre_post'); $post_id = bb_insert_post(array("topic_id" => $row['bb_topic_id'], "post_text" => stripslashes($_POST["post_content"]))); bb_delete_post($post_id, bbwp_status_wp2bb($_POST['comment_approved'])); // empty user info if not anonymous user $user = bb_get_current_user(); if (bb_get_option('bbwp_anonymous_user_id') != $user->ID) { $_POST['comment_author'] = ''; $_POST['comment_author_email'] = ''; $_POST['comment_author_url'] = ''; } $result = bbwp_add_table_item($_POST['post_id'], $_POST['comment_id'], $row['bb_topic_id'], $post_id, $_POST['comment_author'], $_POST['comment_author_email'], $_POST['comment_author_url']); $data = serialize(array("topic_id" => $row['bb_topic_id'], "post_id" => $post_id, "result" => $result)); echo $data; } function bbwp_edit_bb_post() { if (isset($_POST['get_row_by']) && $_POST['get_row_by'] == 'bb_post_id') $row = bbwp_get_table_item('bb_post_id', $_POST["bb_post_id"]); else $row = bbwp_get_table_item('wp_comment_id', $_POST["comment_id"]); // updating topic title if (isset($_POST['topic_title'])) bb_insert_topic(array('topic_title' => $_POST['topic_title'], 'topic_id' => $row['bb_topic_id'])); // remove filters to save formatting remove_all_filters('pre_post'); $user = bb_get_current_user(); if (bb_get_option('bbwp_anonymous_user_id') != $user->ID) { $_POST['comment_author'] = ''; $_POST['comment_author_email'] = ''; $_POST['comment_author_url'] = ''; } if (isset($_POST['get_row_by']) && $_POST['get_row_by'] == 'bb_post_id') bbwp_update_table_item('bb_post_id', $_POST['bb_post_id'], $_POST['comment_author'], $_POST['comment_author_email'], $_POST['comment_author_url']); else bbwp_update_table_item('wp_comment_id', $_POST['comment_id'], $_POST['comment_author'], $_POST['comment_author_email'], $_POST['comment_author_url']); bb_insert_post(array('post_text' => stripslashes($_POST['post_content']), 'post_id' => $row['bb_post_id'], 'topic_id' => $row['bb_topic_id'])); bb_delete_post($row['bb_post_id'], bbwp_status_wp2bb($_POST['comment_approved'])); } function bbwp_delete_bb_post() { bb_remove_filter('bb_delete_post', 'bbwp_afteredit'); $row = bbwp_get_table_item('wp_comment_id', $_POST['comment_id']); bb_delete_post('bb_post_id', $row['bb_post_id']); global $bbdb; // REAL post deletion from database // FIXME: may do something a little bit inconsistent $bbdb->query('DELETE FROM '.$bbdb->prefix.'posts WHERE post_id = '.$row['bb_post_id']); bbwp_delete_table_item('wp_comment_id', $_POST['comment_id']); } function bbwp_open_bb_topic() { bb_open_topic($_POST['topic_id']); } function bbwp_close_bb_topic() { bb_close_topic($_POST['topic_id']); } function bbwp_add_table_item($wp_post, $wp_comment, $bb_topic, $bb_post, $wp_anon_user, $wp_anon_email, $wp_anon_url) { global $bbdb; return $bbdb->query($bbdb->prepare("INSERT INTO ".$bbdb->prefix."bbwp_ids (wp_post_id, wp_comment_id, bb_topic_id, bb_post_id, wp_comment_author, wp_comment_author_email, wp_comment_author_url) VALUES (%d, %d, %d, %d, %s, %s, %s)", $wp_post, $wp_comment, $bb_topic, $bb_post, $wp_anon_user, $wp_anon_email, $wp_anon_url)); } function bbwp_update_table_item($field, $value, $wp_anon_user, $wp_anon_email, $wp_anon_url) { // for anonymous userinfo updating global $bbdb; $bbdb->query($bbdb->prepare('UPDATE '.$bbdb->prefix."bbwp_ids SET wp_comment_author = %s, wp_comment_author_email = %s, wp_comment_author_url = %s WHERE $field = $value", $wp_anon_user, $wp_anon_email, $wp_anon_url)); } function bbwp_get_table_item($field, $value) { global $bbdb; return $bbdb->get_row('SELECT * FROM '.$bbdb->prefix."bbwp_ids WHERE $field = $value LIMIT 1", ARRAY_A); } function bbwp_delete_table_item($field, $value) { global $bbdb; $bbdb->query("DELETE FROM ".$bbdb->prefix."bbwp_ids WHERE $field = $value"); } function bbwp_afteredit($id) { if (!bbwp_do_sync()) return; $post = bb_get_post($id); $row = bbwp_get_table_item('bb_post_id', $post->post_id); if ($row) { // have it in database, must sync bbwp_edit_wp_comment($post, $row['wp_comment_id']); } else { if (bbwp_sync_that_status($id)) { if (bb_get_option('bbwp_sync_posts_back') == 'disabled') return; // syncing back disabled $row = bbwp_get_table_item('bb_topic_id', $post->topic_id); if ($row) bbwp_add_wp_comment($post, $row['wp_post_id']); } } } function bbwp_afterpost($id) { if (!bbwp_do_sync()) return; $post = bb_get_post($id); $row = bbwp_get_table_item('bb_topic_id', $post->topic_id); if ($row) { if (bb_get_option('bbwp_sync_posts_back') == 'disabled') return; // syncing back disabled // need to duplicate in WordPress if (bbwp_sync_that_status($id)) bbwp_add_wp_comment($post, $row['wp_post_id']); } } function bbwp_add_wp_comment($post, $wp_post_id) { $request = array( 'action' => 'add_comment', 'post_text' => bbwp_correct_links(apply_filters('post_text', $post->post_text)), 'post_id' => $post->post_id, 'post_status' => $post->post_status, 'topic_id' => $post->topic_id, 'user' => $post->poster_id, 'wp_post_id' => $wp_post_id ); $answer = bbwp_send_command($request); $data = unserialize($answer); bbwp_add_table_item($wp_post_id, $data['comment_id'], $post->topic_id, $post->post_id, '', '', ''); } function bbwp_edit_wp_comment($post, $comment_id) { // FIXME: get post text original way global $bbdb; remove_filter('post_text', 'bbwp_anonymous_userinfo'); $request = array( 'action' => 'edit_comment', 'post_text' => bbwp_correct_links(apply_filters('post_text', $bbdb->get_var("SELECT post_text FROM ".$bbdb->prefix."posts WHERE post_id = ".$post->post_id))), 'post_status' => bbwp_get_real_post_status($post->post_id), 'user' => $post->poster_id, 'comment_id' => $comment_id, ); bbwp_send_command($request); } function bbwp_afterclosing($id) { if (!bbwp_do_sync()) return; global $bbdb; $wp_post_id = $bbdb->get_var("SELECT wp_post_id FROM ".$bbdb->prefix."bbwp_ids WHERE bb_topic_id = ".$id); $request = array( 'action' => 'close_wp_comments', 'post_id' => $wp_post_id, ); bbwp_send_command($request); } function bbwp_afteropening($id) { if (!bbwp_do_sync()) return; global $bbdb; $wp_post_id = $bbdb->get_var("SELECT wp_post_id FROM ".$bbdb->prefix."bbwp_ids WHERE bb_topic_id = ".$id); $request = array( 'action' => 'open_wp_comments', 'post_id' => $wp_post_id, ); bbwp_send_command($request); } function bbwp_status_wp2bb($status) { // return bbPress post status equal to WordPres comment status if ($status == 1) return 0; // hold if ($status == 0) return 1; // approved if ($status == 'spam') return 2; // spam } function bbwp_options_page() { bb_admin_add_submenu(__('WordPress syncronization', 'bbwp-sync'), 'moderate', 'bbwp_options', 'options-general.php'); } function bbwp_options() { ?>

'.__('Synchronization with WordPress is not enabled.', 'bbwp-sync').' '.sprintf(__('You must check options and enable plugin to make it work.', 'bbwp-sync'), 'admin-base.php?plugin=bbwp_options').'