Version: 2.3.3 Author URI: http://js-kit.com/ */ global $error; include(ABSPATH . 'wp-config.php'); include_once(ABSPATH . WPINC . '/class-IXR.php'); include_once(dirname(__FILE__) . '/settings.php'); @ini_set("memory_limit", "128M"); @set_time_limit(900); $jskit_wp_plugin_version = '2.3.3'; $jskit_domain = 'js-kit.com'; $jskit_url = 'http://' . $jskit_domain; $jskit_debug = 0; # Authentication [[[ function jskit_try_login($args){ $user_login = $args[1]; $user_pass = $args[2]; if ($user_login == "authKey") { jskit_log("Trying to authenticate using jskit auth key"); $jskit_auth_key = get_option("jskit-authKey"); if (strlen($jskit_auth_key) > 0 && $jskit_auth_key == $user_pass) { jskit_log("auth key authentication succeeded."); return true; } jskit_log("auth key authentication failed."); } jskit_log(__FUNCTION__ . " Authentication failed (user_login: $user_login; password: $user_pass)."); $error = new IXR_Error(403, "Authentication using auth key failed"); return false; } # ]]] # XML-RPC call handlers [[[ function jskit_set_status($args){ global $error; jskit_log(__FUNCTION__ . " call. args: " . var_export($args, true)); if (!jskit_try_login($args)) { jskit_log("Failed authentication check."); return $error; } $comment_id = $args[0]; $status = $args[3]; $rez = false; switch ($status) { case 'A': jskit_log("approve comment $comment_id"); $rez = wp_set_comment_status($comment_id, 'approve'); break; case 'S': jskit_log("mark as spam comment $comment_id"); $rez = wp_set_comment_status($comment_id, 'spam'); break; case 'D': jskit_log("deleting comment $comment_id"); if (get_comment($comment_id)) { $rez = wp_delete_comment($comment_id); } else { $rez = true; } break; default: jskit_log("should not be there"); } $call_result = $rez ? 1 : -1 ; jskit_log(__FUNCTION__ . " call result: " . var_export($call_result, true) . "; rez: " . var_export($rez, true)); return $call_result; } function jskit_new_comment($args) { global $error; jskit_log(__FUNCTION__ . " call. args: " . var_export($args, true)); if (!jskit_try_login($args)) { jskit_log("Failed authentication check."); return $error; } $comment = $args[0]; # Retrieve destination post ID from the comment's path. # We assume that the comment path for WP synchronized comments should look # like /blog/p=123 or just /p=123. jskit_log("Trying to detect the comment's post ID"); $comment_post_id = jskit_get_comment_post_id($comment); if ($comment_post_id == 0) { jskit_log("Failed to determine the comment's post ID."); return -1; } jskit_log("Comment's post ID is " . $comment_post_id); $comment['post_ID'] = $comment_post_id; jskit_log("comment: " . var_export($comment, true)); # Check for duplicates $duplicate_comment_id = jskit_get_duplicate_comment($comment); if ($duplicate_comment_id) { jskit_log(__FUNCTION__ . " call result: got duplicate: " . var_export($duplicate_comment_id, true)); return $duplicate_comment_id; } # Prepare comment data for insertion $commentdata = jskit_prepare_new_comment($comment); jskit_log("commentdata: " . var_export($commentdata, true)); # Insert comment into database $comment_id = wp_insert_comment($commentdata); jskit_log("inserted comment: $comment_id"); if (!$comment_id) { jskit_log("Failed to insert comment"); return -1; } # Check data integrity $inserted_comment = get_comment($comment_id); if (!$inserted_comment) { jskit_log("Failed to get comments data for just added comment"); return -1; } # Set comment's status $sParam = $args; $sParam[0] = $comment_id; $sParam[3] = $comment['status']; $rez = jskit_set_status($sParam); jskit_log("jskit_set_status result: " . $rez); $call_result = $rez == -1 ? -1 : $comment_id; jskit_log(__FUNCTION__ . " call result: " . var_export($call_result, true)); return $call_result; } function jskit_validate_auth($args) { global $error; jskit_log(__FUNCTION__ . " call. args: " . var_export($args, true)); if (!jskit_try_login($args)) { jskit_log("Failed authentication check."); return $error; } return 1; } function jskit_get_comments($args) { global $wpdb, $error; jskit_log(__FUNCTION__ . " call. args: " . var_export($args, true)); if (!jskit_try_login($args)) { jskit_log("Failed authentication check."); return $error; } $limit_offset = null; $limit_count = null; if (isset($args[3]) && is_numeric($args[3])) { $limit_offset = intval($args[3]); } if (isset($args[4]) && is_numeric($args[4]) && intval($args[4]) > 0) { $limit_count = intval($args[4]); } $limit_condition = ''; if (!is_null($limit_offset) && !is_null($limit_count)) { $limit_condition = " LIMIT $limit_offset, $limit_count"; } jskit_log("limit_offset: $limit_offset; limit_count: $limit_count; limit_condition: $limit_condition"); $query = "SELECT a.comment_id, a.comment_post_id, a.comment_content, a.comment_approved, a.comment_author, a.comment_author_email, a.comment_author_IP, a.comment_date, a.comment_date_gmt FROM $wpdb->comments a, $wpdb->posts b WHERE a.comment_post_id = b.id ORDER BY a.comment_id $limit_condition"; jskit_log("query: " . $query); $Comments = $wpdb->get_results($query); foreach ($Comments as $key => $elem) { $Comments[$key]->comment_date = new IXR_Date(mysql2date("Ymd\TH:i:s", $elem->comment_date)); $Comments[$key]->comment_date_gmt = new IXR_Date(mysql2date("Ymd\TH:i:s", $elem->comment_date_gmt)); $Comments[$key]->post_uniq = jskit_get_post_uniq_value($Comments[$key]->comment_post_id); $Comments[$key]->post_permalink = get_permalink($Comments[$key]->comment_post_id); } $Cmts = array(); $blog_charset = get_option('blog_charset'); foreach ($Comments as $key => $Comment) { $Cmt = array(); foreach ($Comment as $attribute => $value) { $Cmt[$attribute] = $value; if (gettype($value) == "string") { $Cmt[$attribute] = jskit_convert_charset($value, $blog_charset, "UTF-8"); } } $Cmts[$key] = $Cmt; } jskit_log(__FUNCTION__. " call result: return information about " . count($Cmts) . " comment(s)"); return $Cmts; } function jskit_get_comments_count($args) { global $wpdb, $error; jskit_log(__FUNCTION__ . " call. args: " . var_export($args, true)); if (!jskit_try_login($args)) { jskit_log("Failed authentication check."); return $error; } $comments_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments a, $wpdb->posts b WHERE a.comment_post_id = b.id"); jskit_log(__FUNCTION__ . " call result:" . $comments_count); return $comments_count; } function jskit_plugin_info($args) { global $jskit_wp_plugin_version, $error; jskit_log(__FUNCTION__ . " call. args: " . var_export($args, true)); $plugin_info = array( "jskit_wp_plugin_version" => $jskit_wp_plugin_version ); jskit_log(__FUNCTION__. " call result plugin_info: " . var_export($plugin_info, true)); return $plugin_info; } function jskit_plugin_ping($args) { global $jskit_wp_plugin_version, $error; jskit_log(__FUNCTION__ . " call. args: " . var_export($args, true)); $result = "pong"; jskit_log(__FUNCTION__." call result: $result"); return $result; } # ]]] # Utility functions [[[ function jskit_attach_xmlrpc_methods($methods) { $methods['wp.JSKitPluginInfo'] = 'jskit_plugin_info'; $methods['wp.JSKitPluginPing'] = 'jskit_plugin_ping'; $methods['wp.getComments'] = 'jskit_get_comments'; $methods['wp.getCommentsCount'] = 'jskit_get_comments_count'; $methods['wp.newComment'] = 'jskit_new_comment'; $methods['wp.validateAuth'] = 'jskit_validate_auth'; $methods['wp.setStatus'] = 'jskit_set_status'; return $methods; } function jskit_get_duplicate_comment($comment) { #jskit_log("in jskit_get_duplicate_comment. comment: " . var_export($comment, true)); $all_in_post = get_approved_comments($comment['post_ID']); foreach($all_in_post as $value) { #jskit_log("value->comment_author: " . var_export($value->comment_author, true) . "; value->comment_content: " . var_export($value->comment_content, true)); if ($comment['author'] == $value->comment_author && $comment['text'] == $value->comment_content) { return $value->comment_ID; } } return NULL; } function jskit_get_comment_post_id($comment) { if (!isset($comment['path'])) { jskit_log(__FUNCTION__." Comment data does not have path."); return 0; } $comment_post_id = 0; if (preg_match('/p=([0-9]+)$/', $comment['path'], $matches)) { $comment_post_id = intval($matches[1]); } else { $comment_post_id = url_to_postid($comment['path']); } jskit_log(__FUNCTION__ . " Returning detected post ID: " . var_export($comment_post_id, true)); return $comment_post_id; } function jskit_prepare_new_comment($comment) { $comment_post_ID = $comment['post_ID']; $comment_author = $comment['author']; $comment_author_email = $comment['email']; $comment_author_IP = $comment['IP']; $comment_content = $comment['text']; $comment_date_gmt = gmdate('Y-m-d H:i:s', (int)$comment['TS']); $comment_date = gmdate('Y-m-d H:i:s', (int)$comment['TS'] + get_option('gmt_offset') * 3600); $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_IP', 'comment_content', 'comment_date', 'comment_date_gmt'); $blog_charset = get_option('blog_charset'); foreach ($commentdata as $key => $elem){ $commentdata[$key] = jskit_convert_charset($elem, 'UTF-8', $blog_charset); } $commentdata['comment_agent'] = $_SERVER['HTTP_USER_AGENT']; $commentdata['comment_approved'] = 0; $commentdata = wp_filter_comment($commentdata); return $commentdata; } function jskit_xmlize_utf8($str, $utf8) { if (!$utf8) { return $str; } # XML only allows TAB, NL and LF chars # out of control characters set $search = range(chr(0), chr(31)); foreach(array(9, 10, 13) as $i) unset($search[$i]); return str_replace($search, "", $str); } function jskit_convert_charset($str, $from, $to) { if (($to == 'UTF-8' && seems_utf8($str) == false) || $from == 'UTF-8') { $res = ""; if (function_exists("iconv")) { $res = iconv($from, $to, $str); } elseif (function_exists("mb_convert_encoding")) { $res = mb_convert_encoding($str, $to, $from); } else { $res = utf8_encode($str); } return jskit_xmlize_utf8($res, $to == 'UTF-8'); } return jskit_xmlize_utf8($str, $from == 'UTF-8'); } function jskit_deactivate() { jskit_log("[deactivate] deactivating js-kit plugin. deleting js-kit specific options"); $options = array ( 'streamType', 'splitStreams', 'useStartDate', 'startDate' ); foreach ($options as $name) { delete_option('jskit-' . $name); } return 0; } function jskit_dialog_message($message) { return sprintf('
%s