Teya Salat

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

', $message); } function jskit_check_auth_key() { if (!get_option("jskit-authKey")) { $import_link = jskit_get_import_link(); echo jskit_dialog_message( 'You have activated Echo plug-in, but have not performed import which ' . 'also establishes two way connection with Echo.
' . 'In order to guarantee live comments synchronization ' . 'from Echo to WordPress database please ' . 'import comments first.' ); } } function jskit_get_temp_dir() { if (defined('WP_TEMP_DIR') && @is_writable(WP_TEMP_DIR)) { return trailingslashit(WP_TEMP_DIR); } if (function_exists("sys_get_temp_dir")) { $tmp_dir = sys_get_temp_dir(); if (!empty($tmp_dir) && @is_writable($tmp_dir)) { return trailingslashit($tmp_dir); } } $possible_tmp_dir = array($_ENV['TMP'], $_ENV['TMPDIR'], $_ENV['TEMP']); foreach ($possible_tmp_dir as $tmp_dir) { if (!empty($tmp_dir) && @is_writable($tmp_dir)) { return trailingslashit($tmp_dir); } } return trailingslashit("/tmp"); } function jskit_log($str) { global $jskit_debug; static $jskit_temp_dir; if (!$jskit_debug) { return; } if (!isset($jskit_temp_dir)) { $jskit_temp_dir = jskit_get_temp_dir(); } $fp = @fopen($jskit_temp_dir . "jskit.wp.log", "a+"); if (!$fp) { return; } fwrite($fp, date("r")."\n".str_repeat("=", 80)."\n".$str."\n"); fclose($fp); } function escape(&$array) { global $wpdb; if (!is_array($array)) { return($wpdb->escape($array)); } else { foreach ( (array) $array as $k => $v ) { if (is_array($v)) { $this->escape($array[$k]); } else if (is_object($v)) { //skip } else { $array[$k] = $wpdb->escape($v); } } } } function jskit_use_echo($post) { if (!get_option('jskit-useStartDate')) { return true; } return strtotime($post->post_date) > get_option('jskit-startDate'); } # ]]] # URL helpers [[[ function jskit_get_wpsite_url() { global $current_site; if (isset($current_site->domain)) { return 'http://'.$current_site->domain.$current_site->path; // mu: the base url } return get_bloginfo('wpurl'); // wp: the blog url } function jskit_get_blog_url() { $blog_url = get_bloginfo('url'); if (substr($blog_url, -1, 1) != '/') { $blog_url .= '/'; } return $blog_url; } function jskit_get_blog_url_info() { $blog_url = jskit_get_blog_url(); return parse_url($blog_url); } function jskit_get_blog_relative_path() { $blog_url_info = jskit_get_blog_url_info(); $relative_path = '/'; if (isset($blog_url_info['path'])) { $relative_path = $blog_url_info['path']; } if (substr($relative_path, -1, 1) != '/') { $relative_path .= '/'; } return $relative_path; } function jskit_get_blog_absolute_path() { $blog_url_info = jskit_get_blog_url_info(); return $blog_url_info['host'].jskit_get_blog_relative_path(); } function jskit_get_blog_domain() { $blog_url_info = jskit_get_blog_url_info(); return $blog_url_info['host']; } function jskit_get_post_uniq_value($post_id) { static $blog_relative_path; if (!isset($blog_relative_path)) { $blog_relative_path = jskit_get_blog_relative_path(); } return $blog_relative_path."p=".$post_id; } function jskit_get_normalized_domain($host) { if (substr($host, 0, 4) == 'www.') { $host = substr($host, 4); } return $host; } function jskit_generate_query_string($args) { $query_string = array(); foreach ($args as $k => $v) { $query_string[] = $k . '=' . urlencode($v); } return join("&", $query_string); } function jskit_get_import_link() { global $jskit_url; $wpsite_url_info = parse_url(jskit_get_wpsite_url()); $host = jskit_get_normalized_domain($wpsite_url_info['host']); $user = wp_get_current_user(); $args = array( 'site' => $host, 'action' => 'launch_import', 'appkey' => get_option('jskit-authKey'), 'endpoint' => $wpsite_url_info['host'] . ($wpsite_url_info['path'] ? $wpsite_url_info['path'] : '/') ); return $jskit_url."/comments/wordpress.cgi?" . jskit_generate_query_string($args); } function jskit_get_moderation_link() { global $jskit_url; $wpsite_url_info = parse_url(jskit_get_wpsite_url()); $host = jskit_get_normalized_domain($wpsite_url_info['host']); $args = array( 'site' => $host, ); return $jskit_url."/moderate/?" . jskit_generate_query_string($args); } # ]]] # URL rewriting [[[ function jskit_trackback_link($trackback_url) { global $jskit_url, $post; return jskit_use_echo($post) ? $jskit_url . "/trackback/" . jskit_get_blog_domain() . jskit_get_post_uniq_value($post->ID) : $trackback_url; } function jskit_rss_link($rss_url) { global $jskit_url, $post; return jskit_use_echo($post) ? $jskit_url . "/rss/" . jskit_get_blog_domain() . jskit_get_post_uniq_value($post->ID) : $rss_url; } function jskit_url_rewrite($url) { global $jskit_url; if ($url == get_feed_link('comments_rss2')) { return $jskit_url . "/rss/" . jskit_get_blog_domain(); } return $url; } # ]]] # Content rewriting [[[ function jskit_add_page() { global $menu, $submenu; add_submenu_page('edit-comments.php', 'Echo', 'Echo', 10, 'echo', jskit_settings_page); foreach ($menu as $k => $v) { if ($menu[$k][2] == 'edit-comments.php') { $menu[$k][2] = 'edit-comments.php?page=echo'; } } } function jskit_comments_template($value) { global $post; return jskit_use_echo($post) ? dirname(__FILE__) . '/comments.php' : $value; } function jskit_comments_number($output) { global $post; if (!jskit_use_echo($post)) { return $output; } $uniq = jskit_get_post_uniq_value($post->ID); $number = '0 ' . str_replace("%", "", __("% Comments")); return $number; } function jskit_header(){ ?>