<?php
/*
Plugin Name: ld_post
Plugin URI: http://dogmap.jp/
Description: Wordpressの投稿を、livedoor blog へも投稿するプラグイン。
Author: wokamoto
Author URI: http://dogmap.jp/
Version: 0.0.1
*/
require_once "HTTP/Request.php";

function wp2ld($post_id) {
	//livedoor ユーザ情報
	$livedoor_id = "ライブドアID";
	$password    = "ログインパスワード";

	$post  = get_post($post_id);
	if (strtolower($post->post_status)=='publish' && strtolower($post->post_type)=='post') {
		//カテゴリ,タイトル,投稿内容を取得
		$category = trim(get_the_category_by_ID((int)$post->post_category));
		$title = $post->post_title;
		$text  = $post->post_content;

		//blogを投稿
		$atom = new ld_post($livedoor_id,$password,$category,$title,$text);
		$ret = $atom->postBlogEntry();
	}
}

class ld_post {
	var $livedoor_id;
	var $password;
	var $category;
	var $title;
	var $text;

	function ld_post($livedoor_id,$password,$category,$title,$text) {
		$this->livedoor_id = $livedoor_id;
		$this->password	   = $password;

		$charset = get_settings('blog_charset');
		if (strtoupper(charset) != "UTF-8") {
			$category = mb_convert_encoding($category,"UTF-8", $charset);
			$title    = mb_convert_encoding($title,"UTF-8", $charset);
			$text     = mb_convert_encoding($text,"UTF-8", $charset);
		}
		$this->category = $category;
		$this->title    = $title;
		$this->text     = $text;
	}

	//blogに投稿
	function postBlogEntry() {
		$url = $this->getBlogAtomURL();
		if(trim($url) == "" ) return false; //エラー:Atom URLが設定されていない

		$created = date('Y-m-d\TH:i:s\Z');
		$nonce = pack('H*', sha1(md5(time())));
		$pass_digest = base64_encode(pack('H*', sha1($nonce.$created.$this->password))); 

		$wsse =	'UsernameToken Username="'.$this->livedoor_id.'", '.
			'PasswordDigest="'.$pass_digest.'", '.
			'Nonce="'.base64_encode($nonce).'",'.
			'Created="'.$created.'"';

		$rawd =	'<?xml version="1.0" encoding="UTF-8"?>'.
			'<entry xmlns="http://purl.org/atom/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/">'.
			'<title type="text/html" mode="escaped">'.$this->title.'</title>'.
			'<dc:subject type="text/html" mode="escaped">'.$this->category.'</dc:subject>';
//		if(strstr($this->text,'<!--more-->')) {
//			$content = explode('<!--more-->', $this->text, 2);
//			$rawd .='<content type="application/xhtml+xml" mode="base64">'.base64_encode($content[0]).'</content>'.
//				'<content type="application/xhtml+xml" mode="base64">'.base64_encode($content[1]).'</content>';
//		} else {
//			$rawd .='<content type="application/xhtml+xml" mode="base64">'.base64_encode($this->text).'</content>';
//		}
		$rawd .='<content type="application/xhtml+xml" mode="base64">'.base64_encode($this->text).'</content>';
		$rawd .='</entry>';

		$req = new HTTP_Request();
		$req->setURL($url);
		$req->addHeader('X-WSSE',$wsse );
		$req->addHeader('Content-Type', 'application/x.atom+xml');
		$req->addHeader('Cache-Control', 'no-cache');
		$req->setMethod(HTTP_REQUEST_METHOD_POST);
		$req->addRawPostData($rawd);

		$res = $req->sendRequest();
		if (PEAR::isError($res)) {
			return $res;
		} else {
			return true;
		}
	}

	//atom.xml内の投稿用URLを取得
	function getBlogAtomURL(){
		$href = "";

		// atom.xml オープン
		$intSes  = curl_init();
		curl_setopt($intSes,CURLOPT_URL,"http://blog.livedoor.jp/{$this->livedoor_id}/atom.xml");
		curl_setopt($intSes,CURLOPT_RETURNTRANSFER, true);
		$atomxml = curl_exec($intSes);
		curl_close($intSes);

		// 投稿用URL取得
		$objDoc = domxml_open_mem($atomxml);
		$objRoot= $objDoc->document_element();
		$aryCld = $objRoot->child_nodes();
		foreach($aryCld as $objCld) {
			if($objCld->node_type()!=XML_TEXT_NODE){
				if (strtolower($objCld->get_attribute('rel'))=='service.post'){
					$href = $objCld->get_attribute('href');
					break;
				}
			}
		}

		return $href;
	}
}

add_action('save_post', 'wp2ld');
?>

