<?php
/*
Plugin Name: 文章同步
Plugin URI: http://xiaohudie.net/wbto-plugin.html
Description: 自动把你的博客文章同步到微博通,支持标题+内容截断+抓取图像+更新不同步。
Version: 1.2
Author: 小蝴蝶
Author URI: http://xiaohudie.net
*/
function wbto_install() {
global $wpdb;
$table_name = $wpdb->prefix."wbto";
if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
$sql = "CREATE TABLE " . $table_name . " (id mediumint(9) NOT NULL AUTO_INCREMENT, wbto_username VARCHAR(100) NOT NULL, wbto_password VARCHAR(100) NOT NULL, );";
}
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}

include_once(ABSPATH.'/wp-includes/class-IXR.php');
class netease{
public $useragent="Hostscheme/1.0"; //定义要模拟的浏览器名称
private $token="";
private $thisch; //CURL对象句柄
private $cookie; //保存Cookie的临时文件
private $data; //临时数据保存地址
public $sblog_class;
public function login($blogurl,$user,$pass)
{

$d = tempnam('../tmp/', 'cookie.txt'); //创建随机临时文件保存cookie.
$this->cookie=$d;
$ch = curl_init("http://blog.163.com");
$this->thisch=$ch;
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent);
$data= curl_exec($ch);
curl_close($ch);
//unset($this->thisch);


$ch = curl_init($this->thisch);
$posturl="https://reg.163.com:443/logins.jsp";
$post="savelogin=0"."&username=".$user."&password=".$pass."&url=http://blog.163.com/loginGate.do?blogActivation=true&from=login&type=1&product=blog";

curl_setopt($ch, CURLOPT_REFERER, "http://blog.163.com");
curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_POST, 1); // how many parameters to post
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, 1);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
$data= curl_exec($ch);
curl_close($ch);
//unset($this->thisch);

//以上完成登录。

}


public function send($tags,$blogurl,$title,$content,$allowview,$msyn)
{
$ch = curl_init($this->thisch);
$creaturl="http://".$blogurl.".blog.163.com/blog/getBlog.do?fromString=bloglist";
$reff="http://".$blogurl.".blog.163.com/blog/";
curl_setopt($ch, CURLOPT_URL, $creaturl);
curl_setopt($ch, CURLOPT_REFERER,$reff);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
$data= curl_exec($ch);
curl_close($ch);
preg_match_all( "/name=\"NETEASE_BLOG_TOKEN_EDITBLOG\" value=\"(.*?)\"\/\>/s",$data, $tokens );
$token=$tokens[1][0];
preg_match_all( "/c:\[{id:\'(.*?)\',/s",$data, $clss );
$cls=$clss[1][0];
//unset($this->thisch);

$refer="http://api.blog.163.com/crossdomain.html?t=20100205";

$posturl="http://api.blog.163.com/".$blogurl."/editBlogNew.do?p=1&n=1&from=bloglist";
$post="tag=".urlencode($tags)."&cls=".urlencode($cls)."&allowview=".$allowview."&refurl=&abstract=&bid=&origClassId=&origPublishState=&oldtitle=&todayPublishedCount=0&NETEASE_BLOG_TOKEN_EDITBLOG=".urlencode($token)."&title=".$title."&HEContent=".$content."&copyPhotos=&suggestedSortedIds=&suggestedRecomCnt=&suggestedStyle=0&isSuggestedEachOther=0&photoBookImgUrl=&msyn=".$msyn."&miniBlogCard=0&p=1";
$ch = curl_init($this->thisch);
curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_POST, 1); // how many parameters to post
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, $refer);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
curl_setopt($ch, CURLOPT_HEADER, 1);
// curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data= curl_exec($ch);
curl_close($ch);
preg_match_all( "/\',sfx:\'(.*?)\'}/s",$data, $nexturls );
$nexturl=$nexturls[1][0];

$ch = curl_init("http://".$blogurl.".blog.163.com/".$nexturl);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent);
$data= curl_exec($ch);
curl_close($ch);

unset($this->thisch);
}

public function logoff()
{
unset($this->thisch);
unlink($this->cookie);
}
}
function make_newpost($post)
{
$title=$post->post_title;
if (strlen($title)==0){$title="无题";}

$content=$post->post_content;
$sendurl=get_option('wp2sinaxsend_url');
if ($sendurl==1)
{
$content="查看原文:<a href=".get_permalink($post->ID).">".get_permalink($post->ID)."</a><br/>".$content;
}
elseif($sendurl==2)
{
$content.="<br/><br/>( 这是同步过来的,如需交流扯蛋请访问原文:<a href=".get_permalink($post->ID).">".get_permalink($post->ID)."</a>)";
}
else
{

if (strlen($content)==0){$content="a blank ";}
}

$wp2sinabloguser=get_option('wp2sinabloguser');
$wp2sinablogpass=get_option('wp2sinablogpass');
if (strlen($wp2sinabloguser)>4)
{
if (strlen($wp2sinablogpass)>5)
{
$client->debug = false;
$xmlclient = "http://upload.move.blog.sina.com.cn/blog_rebuild/blog/xmlrpc.php";
$client = new IXR_Client($xmlclient);

$content= wp_richedit_pre($content);
$content=htmlspecialchars_decode($content);

$catlog=get_option('wp2sina_blog_class');
$catlog= array($catlog);

$post1=array('title'=>$title,'description'=>$content,'categories'=>$catlog);
$params = array(1,$wp2sinabloguser,$wp2sinablogpass,$post1,true); // Last parameter is 'true' which means post immideately, to save as draft set it as 'false'

$client->query('metaWeblog.newPost', $params);
$wp2sinaid=$client->getResponse();
add_post_meta($post->ID,'_wp2sinaid',$wp2sinaid,true);
unset($client);
}
}

$tags="";
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$tags=$tags . ',' . $tag->name ;
}
}
if(tags){
$tags=substr($tags,1);
}
//echo "tags:" . $tags;

$wp2neteaseuser=get_option('wp2neteaseuser');
$wp2neteasepass=get_option('wp2neteasepass');
$wp2neteaseurl=get_option('wp2neteaseurl');
if (strlen($wp2neteaseuser)>1)
{
if (strlen($wp2neteasepass)>3)
{
if(!function_exists('iconv'))
{
require_once(dirname(__FILE__).'/iconv.php');
}

$user=$wp2neteaseuser;
$pass=$wp2neteasepass;
$blogurl=$wp2neteaseurl;
$content=urlencode($content);
$title=urlencode($title);
$title = str_replace("+", "%20", $title);
$content = str_replace("+", "%20", $content);
$arr = array("%0D%0A" => "<br />");
$content= strtr($content,$arr);

$blog=new netease();
$blog->login($blogurl,$user,$pass);
$blog->send($tags,$blogurl,$title,$content,"-100","1");
$blog->logoff();
}
}
}
function publish_post_2_sinablog($post_ID){

$posted = get_post($post_ID);
preg_match_all('/<img[^>]+src=[\'"](http[^\'"]+)[\'"].*>/isU',$posted->post_content, $image);
$p_sum = count($image[1]);
if ($p_sum > 0) {
$p = $image[1][0];
}
if (!$p) {
if (function_exists('has_post_thumbnail') && has_post_thumbnail($post_ID)) {
if ($image_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'post-thumbnail'))
$p = $image_url[0];
}
}
$t1=$posted->post_date;
$t2=$posted->post_modified;
$diff=strtotime($t2)-strtotime($t1);
if($diff>0){return;}
make_newpost($posted);
$username = get_option('wbto_username');
$password = get_option('wbto_password');
$posted = get_post($post_ID);
$image_url = $p;
$excerpt=mb_strimwidth(strip_tags($posted->post_content),0,130,'...');
$fields = array();
$fields['source'] = 'wordpress';
$fields['content'] = urlencode('['.$posted->post_title.']'.mb_strimwidth(strip_tags($excerpt),0,130,'...').' '.$posted->guid);
$ch = curl_init();
if ($image_url) {
$fields['imgurl'] =$image_url;
curl_setopt($ch, CURLOPT_URL, "http://wbto.cn/api/upload.json");
} else {
curl_setopt($ch, CURLOPT_URL, "http://wbto.cn/api/update.json");
}
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
curl_close($ch);
//////////

}


// Hook for adding admin menus
add_action('admin_menu', 'mt_add_sinapages');
add_action('publish_post', 'publish_post_2_sinablog');

//add_action('future_to_publish ','future_publish_post_2_sinablog');

// action function for above hook
function mt_add_sinapages() {
//call register settings function
add_action( 'admin_init', 'register_mysettings' );
// Add a new submenu under Options:
add_options_page('文章同步', '文章同步', 'administrator', 'wpsinablog', 'mt_options_page');


}

function register_mysettings() {
//register our settings
register_setting( 'WP2Sinablog-settings-group', 'wp2sinabloguser' );
register_setting( 'WP2Sinablog-settings-group', 'wp2sinablogpass' );
register_setting( 'WP2Sinablog-settings-group', 'wp2sinaxsend_url' );
register_setting( 'WP2Sinablog-settings-group', 'wp2sina_blog_class' );
//register our settings
register_setting( 'WP2Netease-settings-group', 'wp2neteaseuser' );
register_setting( 'WP2Netease-settings-group', 'wp2neteasepass' );
register_setting( 'WP2Netease-settings-group', 'wp2neteaseurl' );
register_setting( 'WP2Netease-settings-group', 'netease_sdurl' );
register_setting( 'WP2Netease-settings-group', 'netease_allowview' );
register_setting( 'WP2Netease-settings-group', 'netease_msyn' );
}


// mt_options_page() displays the page content for the Test Options submenu
function mt_options_page() {


?>


<div class="wrap"><?php
echo '<div class="wrap">';
echo '<h2>微博通同步</h2>';
echo '<form method="post" action="options.php">';
echo wp_nonce_field('update-options');
echo '<table class="form-table">';
echo '<tr valign="top">';
echo '<th scope="row">用户名 <a href="http://www.wbto.cn/?app=wp">注册</a></th>';
echo '<td><input type="text" name="wbto_username" value="'.get_option('wbto_username').'" /></td>';
echo '</tr>';
echo '<tr valign="top">';
echo '<th scope="row">密码</th>';
echo '<td><input type="password" name="wbto_password" value="'.get_option('wbto_password').'" /></td>';
echo '</tr>';
echo '</table>';
echo '<input type="hidden" name="action" value="update" />';
echo '<input type="hidden" name="page_options" value="wbto_username,wbto_password" />';
echo '<p class="submit">';
echo '<input type="submit" name="submit" id="submit" class="button-primary" value="保存更改" />';
echo '</p>';
echo '</form>';
echo '</div>';?>

<h2>WP2Sinablog 选项</h2><br />
设置仅适用于新浪博客,不支持Wordpress中<b>private</b>属性的文章发布到新浪博客。
<br/><br/>
<form method="post" action="options.php">

<?php settings_fields( 'WP2Sinablog-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">新浪博客的登录名</th>
<td>
<input name="wp2sinabloguser" type="text" id="wp2sinabloguser" value="<?php form_option('wp2sinabloguser'); ?>" class="regular-text" />
</td>
</tr>
<tr valign="top">
<th scope="row">新浪博客的登录密码</th>
<td>
<input name="wp2sinablogpass" type="password" id="wp2sinabloguser" value="<?php form_option('wp2sinablogpass'); ?>" class="regular-text" />
</td>
</tr>
<tr valign="top">
<th scope="row">发布文件目录设置</th>
<td>
<?php
$wp2sinabloguser=get_option('wp2sinabloguser');
$wp2sinablogpass=get_option('wp2sinablogpass');
if (strlen($wp2sinabloguser)>3)
{
if (strlen($wp2sinablogpass)>3)
{
$client->debug = false;

$xmlclient = "http://upload.move.blog.sina.com.cn/blog_rebuild/blog/xmlrpc.php";
$client = new IXR_Client($xmlclient);
$params = array(0,$wp2sinabloguser,$wp2sinablogpass);
if (!$client->query("metaWeblog.getCategories", $params))
{
?>
<font color="red">尝试登录新浪博客失败,请检查用户名/密码是否正确!</font></b>
<?php

}
else
{
$catarrays=$client->getResponse();
if (count($catarrays)>0)
{
foreach($catarrays as $catarray)
{
$catarrayid=$catarray['categoryId'];
$catarrayname=$catarray['categoryName'];
?>
<input name="wp2sina_blog_class" value="<?php echo $catarrayname; ?>" id="componentSelect<?php echo $catarrayid; ?>" type="radio" <?php checked($catarrayname, get_option('wp2sina_blog_class')); ?> >
<label for="componentSelect<?php echo $catarrayid; ?>"><?php echo $catarrayname; ?></label>
<?php
}
}
else
{
echo "您的新浪博客只存在默认目录。";
}

}
unset($client);
}
}
?>

</td>
</tr>
<tr valign="top">
<th scope="row">原文链接设置</th>
<td>

<input name="wp2sinaxsend_url" value="0" <?php checked(0, get_option('wp2sinaxsend_url')); ?> id="cwp2sinaxsend_url1" type="radio">
<label for="cwp2sinaxsend_url1">不发送</label>
<input name="wp2sinaxsend_url" value="1" <?php checked(1, get_option('wp2sinaxsend_url')); ?> id="cwp2sinaxsend_url2" type="radio">
<label for="cwp2sinaxsend_url2">发送(链接在文章头部)</label>
<input name="wp2sinaxsend_url" value="2" <?php checked(2, get_option('wp2sinaxsend_url')); ?> id="cwp2sinaxsend_url3" type="radio">
<label for="cwp2sinaxsend_url3">发送(链接在文章尾部)</label>
</td>
</tr>

</table>

<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
<h2>WP2Netease 选项</h2>
设置仅适用于网易博客,不支持Wordpress中<b>private</b>属性的文章发布到网易博客。

<br/><br/>
<form method="post" action="options.php">

<?php settings_fields( 'WP2Netease-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">网易博客的登录名</th>
<td>
<input name="wp2neteaseuser" type="text" id="wp2neteaseuser" value="<?php form_option('wp2neteaseuser'); ?>" class="regular-text" />

</td>
</tr>
<tr valign="top">
<th scope="row">网易博客的登录密码</th>
<td>
<input name="wp2neteasepass" type="password" id="wp2neteaseuser" value="<?php form_option('wp2neteasepass'); ?>" class="regular-text" />

</td>

</tr>

<tr valign="top">
<th scope="row">网易博客的UR</th>
<td>
<input name="wp2neteaseurl" type="text" id="wp2neteaseurl" value="<?php form_option('wp2neteaseurl'); ?>" class="regular-text" />
</td>

</tr>

 

</table>

<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
<br/>说明:1.如网易博客地址为<a href="http://hostscheme.blog.163.com/blog">http://hostscheme.blog.163.com/blog</a>
<br/>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;则网易博客的UR应填写<b>hostscheme</b>,如果为中文请使用urlencode
<br/>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.网易博客限制发文频率,频率高了以后会出现验证码。如果同步不成功,可能是频率太高所致。
</div>
<?php

}

?>