<?php
/*
Plugin Name: Post to Qzone 
Plugin URI: http://liguangming.com
Description: publish post to Qzone,<a href="http://service.mail.qq.com/cgi-bin/help?subtype=1&id=23&no=242" target="_blank">如何通过发送邮件的方式发表Qzone</a>.
Version: 0.2.1
Author: Ian Lee
Author URI: http://liguangming.com
*/

defined('ABSPATH') or exit('access denied');

if ( !class_exists('PHPMailer') ) 
{
	require_once ABSPATH . WPINC . '/class-phpmailer.php';
	require_once ABSPATH . WPINC . '/class-smtp.php';
}

class Mailer extends PHPMailer 
{
	var $qq=null;
	function Mailer($qq,$psw) {
		$this->qq=$qq;
		$this->From	 = "{$qq}@qq.com";
		$this->FromName = $qq;
		$this->Host	 = "smtp.qq.com";
		$this->Mailer   = "smtp";
		$this->WordWrap = 75;
		$this->CharSet = 'utf-8';
		$this->Encoding = 'base64';
		$this->SMTPAuth = true;
		$this->IsHTML(true);
		$this->Username = $qq;
		$this->Password = $psw;
	}

	function Halo($subject,$body){
		$this->AddAddress("{$this->qq}@qzone.qq.com", "{$this->qq}@qzone.qq.com");
		$this->Subject = $subject;
		$this->Body	= $body;
		return $this->Send();
	}
}

class Crypter{

   var $key;

   function Crypter($clave){
	  $this->key = $clave;
   }

   function keyED($txt) { 
	  $encrypt_key = md5($this->key); 
	  $ctr=0; 
	  $tmp = ""; 
	  for ($i=0;$i<strlen($txt);$i++) { 
		 if ($ctr==strlen($encrypt_key)) $ctr=0; 
		 $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1); 
		 $ctr++; 
	  } 
	  return $tmp; 
   } 
   
   function encrypt($txt){ 
	  srand((double)microtime()*1000000); 
	  $encrypt_key = md5(rand(0,32000)); 
	  $ctr=0; 
	  $tmp = ""; 
	  for ($i=0;$i<strlen($txt);$i++){ 
		 if ($ctr==strlen($encrypt_key)) $ctr=0; 
		 $tmp.= substr($encrypt_key,$ctr,1) . 
			 (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1)); 
		 $ctr++; 
	  } 
	  return base64_encode($this->keyED($tmp)); 
   } 

   function decrypt($txt) { 
	  $txt = $this->keyED(base64_decode($txt)); 
	  $tmp = ""; 
	  for ($i=0;$i<strlen($txt);$i++){ 
		 $md5 = substr($txt,$i,1); 
		 $i++; 
		 $tmp.= (substr($txt,$i,1) ^ $md5); 
	  } 
	  return $tmp; 
   } 
}


function post_to_qzone_admin()
{
	if (function_exists('add_options_page')) {
		add_options_page('post_to_qzone', 'Post2Qzone',8, basename(__FILE__), 'post_to_qzone_option');	
	}
}

function post_to_qzone_default($post_to_qzone){
	if(strpos($post_to_qzone->title,'{post_title}')===false){
		$post_to_qzone->title = '{post_title}';
	}

	if(strpos($post_to_qzone->content,'{post_content}')===false){
		$post_to_qzone->content = '{post_content}<br />Original:{post_link}';
	}
	return $post_to_qzone;
}

function post_to_qzone_option()
{
	$post_to_qzone = get_option('post_to_qzone');
	if($post_to_qzone !== false){
		$post_to_qzone = unserialize($post_to_qzone);
	}else{
		$post_to_qzone = new stdClass;
	}

	if(isset($_POST['post_to_qzone_title'])  && isset($_POST['post_to_qzone_content'])){
		$n = trim($_POST['post_to_qzone_number']);
		$p = trim($_POST['post_to_qzone_password']);
		$t = trim($_POST['post_to_qzone_title']);
		$c = trim($_POST['post_to_qzone_content']);

		$post_to_qzone->number = $n;
		$post_to_qzone->title = $t;
		$post_to_qzone->content = $c;
		if(strlen($p)>=6){
			$crypter = new Crypter(get_option('secret'));
			$post_to_qzone->password = $crypter->encrypt($p);
		}else{
			$post_to_qzone->password = '';
		}
		$post_to_qzone = post_to_qzone_default($post_to_qzone);
		update_option('post_to_qzone', serialize($post_to_qzone));
	}else{
		$post_to_qzone = post_to_qzone_default($post_to_qzone);
	}
?>
	<div class="wrap" style="padding:5px 0 0 5px;text-align:left" id="poststuff">
	<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>" id="post">
	<h3>QQ Number</h3>
	<div id="titlewrap">
		<input type="text" name="post_to_qzone_number" size="30" tabindex="1" value="<?php echo $post_to_qzone->number;?>" id="post_to_qzone_number" autocomplete="off" /> 
	</div><br />
	<h3>QQ Mail Password</h3>
	<div id="titlewrap">
		<input type="password" name="post_to_qzone_password" size="30" tabindex="2" value="<?php echo $post_to_qzone->password;?>" id="post_to_qzone_password" autocomplete="off" />
	</div><br />
	<h3>Post Title Template </h3>
	<div id="titlewrap">
		<input type="text" name="post_to_qzone_title" size="30" tabindex="3" value="<?php echo $post_to_qzone->title;?>" id="post_to_qzone_title" autocomplete="off" />
	</div><br />
	<h3>Post Content Template</h3>
	<div id="titlewrap"><textarea name="post_to_qzone_content" id="post_to_qzone_content" tabindex="4" rows="6" cols="100%" autocomplete="off"><?php echo $post_to_qzone->content;?></textarea>
	</div>
	<p>Above the fields are not required!if not,you will input information when publish post to qzone everytime!</p>
	<p class="submit"><input type="submit" value="<?php _e("Save Qzone Account Information");?>" name="submit" /></p>
	</form>
	</div>
<?php
}

function post_qzone_config(){
	$post_to_qzone = get_option('post_to_qzone');
	if($post_to_qzone !== false){
		$post_to_qzone = unserialize($post_to_qzone);
	}else{
		$post_to_qzone = new stdClass;
	}
	
	$action = $_GET['action'];

	if(extension_loaded("sockets")){
		echo '<div id="qzonediv" class="postbox"><h3>Post to Qzone</h3><div class="inside"><p><strong>QQ number:</strong> &nbsp; &nbsp;<a href="options-general.php?page='.basename(__FILE__).'">Config Account?</a></p><input name="qqnumber" type="text" size="50" id="qqnumber" value="'.$post_to_qzone->number.'" /></div><div class="inside"><p><strong>QQ email password:</strong></p><input name="qqpsw" type="password" size="50" id="qqpsw" value="'.$post_to_qzone->password.'" />';
		if(strlen($post_to_qzone->password) > 0){
			echo '<input name="qqed" type="hidden" id="qqed" value="1" />';
		}
		echo '</div><div class="inside"><p><input name="qqpub" type="checkbox" id="qqpub" value="1" '.($action=='edit'?'':'checked="checked"').' /><strong>Confirm publish!</strong></p></div></div>';
	}else{
		echo '<div id="qzonediv" class="postbox"><h3>Post to Qzone</h3><div class="inside"><p>Sorry,Your webserver do <strong>not</strong> support socket!</p></div></div>';
	}
}

function post_qzone_publish(){
	global $wpdb;

	if(isset($_POST['qqnumber']) && isset($_POST['qqpsw']) && isset($_POST['qqpub']) ){
		$qq = intval($_POST['qqnumber']);
		$psw = trim($_POST['qqpsw']);

		if(strlen($psw)<6){
			return false;
		}

		if(isset($_POST['qqed'])){
			$crypter = new Crypter(get_option('secret'));
			$psw->password = $crypter->decrypt($psw);
		}

		$post_to_qzone = get_option('post_to_qzone');
		if($post_to_qzone !== false){
			$post_to_qzone = unserialize($post_to_qzone);
		}else{
			$post_to_qzone = new stdClass;
		}

		$post_to_qzone = post_to_qzone_default($post_to_qzone);

		if (get_magic_quotes_gpc()) {
			$post_title=stripslashes($_POST['post_title']);
			$post_content=stripslashes($_POST['post_content']);
		}else{
			$post_title=trim($_POST['post_title']);
			$post_content=trim($_POST['post_content']);
		}

		$post_id=intval($_POST['ID']);

		if($qq>1000 && !empty($post_title)  && !empty($post_content) && $post_id>0){

			$post_content = str_replace('{post_content}',$post_content,$post_to_qzone->content);
			$post_content = str_replace('{post_link}',get_permalink($post_id),$post_content);
			$post_content = str_replace('{post_title}',$post_title,$post_content);

			$post_title = str_replace('{post_title}',$post_title,$post_to_qzone->title);
			
			$m=new Mailer($qq,$psw);
			$m->Halo($post_title,$post_content);
		}

		unset($_POST['qqnumber'],$_POST['qqpsw'],$_POST['qqpub'],$_POST['qqed']);
	}
}

add_action('admin_menu', 'post_to_qzone_admin');
add_action('dbx_post_sidebar', 'post_qzone_config');
add_action('publish_post', 'post_qzone_publish');



