changeset 0:1a30ce1834e5 draft default tip

added README
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Wed, 08 Aug 2012 20:02:35 +0900
parents
children
files README debug/test_refresh.php include/syncwp2nntp_functions.php syncwp2nntp.php
diffstat 4 files changed, 449 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README	Wed Aug 08 20:02:35 2012 +0900
@@ -0,0 +1,21 @@
+syncwp2nntp
+polls articles from newsgroup in specified interval.
+
+1, installation
+ just put to wordpress plugin directory
+
+2, configuration
+ 1, enable plugin
+
+ 2, how to subscribe newsgroup
+   add newsgroup as category to subscribe
+   ex: ura.ie.announce
+
+ 3, how to configure nntp server
+   open settings -> syncwp2nntp
+   modifly "server address." to your server
+
+ 4, how to configure refresh interval
+   open settings -> syncwp2nntp -> refresh interval. (seconds)
+
+defalut imports 5 articles from server (maybe...)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/debug/test_refresh.php	Wed Aug 08 20:02:35 2012 +0900
@@ -0,0 +1,8 @@
+<?php
+
+require_once "../../../../wp-load.php";
+require_once "../include/syncwp2nntp_functions.php";
+
+print_r(syncwp2nntp_refresh(true));
+
+?>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/syncwp2nntp_functions.php	Wed Aug 08 20:02:35 2012 +0900
@@ -0,0 +1,384 @@
+<?php
+/*
+	syncwp2nntp_functions.php
+	function definition.
+*/
+
+require_once "Net/NNTP/Client.php";
+
+//for nntpd
+define("DEFAULT_SERVER_ADDR","news.example.com"); //please change this address to own nntpd.
+define("NNTP_ENCODING","ISO-2022-JP");
+define("WP_ENCODING","UTF-8");
+define("NNTP_DETECT_ENCODINGS","ISO-2022-JP,UTF-8,EUC-JP,Shift_JIS");
+
+//if you want to authenticate on nntpd , enable code below.
+/*
+
+define("NNTP_ENABLE_AUTHENTICATE","TRUE");
+define("NNTP_USERNAME","nntpuser"); // change username
+define("NNTP_PASSWORD","nntppass"); // change password
+
+*/
+
+//for wordpress
+define("EVENT_NAME","syncwp2nntp_refresh_event_2");
+define("DEFAULT_REFRESH_INTERVAL",360); //default 360sec 
+define("FUNC_PUBLISH_POST","syncwp2nntp_publish_post");
+define("FUNC_REFRESH_EVENT_CALLBACK","syncwp2nntp_refresh_event_callback");
+define("FUNC_ADD_PAGE","syncwp2nntp_add_page");
+define("ARTICLE_LASTN_COUNT",5); //default 5 articles.
+define("DEFAULT_POST_USER",1); //default 1 (admin).
+
+//for options
+define("SYNCWP2NNTP_SERVER_ADDRESS","syncwp2nntp_server_address");
+define("SYNCWP2NNTP_REFRESH_INTERVAL","syncwp2nntp_refresh_interval");
+define("SYNCWP2NNTP_POST_USER","syncwp2nntp_post_user");
+define("SYNCWP2NNTP_EXPORT_NNTP","syncwp2nntp_export_nntp");
+
+/*
+	syncwp2nntp_add_page
+	create submenu page
+*/
+function syncwp2nntp_add_page($_arg)
+{
+	// User Level Permission
+	//  -- Subscriber = 0,Contributor = 1,Author = 2,Editor= 7,Administrator = 9
+	add_submenu_page('options-general.php','syncwp2nntp','syncwp2nntp',7, __FILE__, 'syncwp2nntp_add_menu');
+
+	return $_arg;
+}
+
+/*
+	syncwp2nntp_add_menu
+	create menu page
+*/
+function syncwp2nntp_add_menu()
+{
+	//commit configuration.
+	if(isset($_POST["srv"]) && isset($_POST["int"])){
+		$new_server = $_POST["srv"];
+		$new_interval = $_POST["int"];
+
+		update_option(SYNCWP2NNTP_SERVER_ADDRESS,$new_server);
+		update_option(SYNCWP2NNTP_REFRESH_INTERVAL,$new_interval);
+	}
+	if(isset($_POST["exp"])){
+		update_option(SYNCWP2NNTP_EXPORT_NNTP,$_POST["exp"]);
+	}
+	if(isset($_POST["ref"])){
+		syncwp2nntp_refresh();
+	}
+
+	$triger = "disable";
+	if(get_option(SYNCWP2NNTP_EXPORT_NNTP) === "disable"){
+		$triger = "enable";		
+	}
+
+	//generate configuration html.
+	$out = '<div class="wrap">'."\n";
+	$out .= '<h2>syncwp2nmtp configuration</h2>'."\n";
+	$out .= '<form method="post">'."\n";
+	$out .= '<p>server address.</p>'."\n";
+	$out .= '<input type="text" name="srv" value="'.get_option(SYNCWP2NNTP_SERVER_ADDRESS).'"/>'."\n";
+	$out .= '<p>refresh interval. (seconds)</p>'."\n";
+	$out .= '<input type="text" name="int" value="'.get_option(SYNCWP2NNTP_REFRESH_INTERVAL).'"/>'."\n";
+	$out .= '<p>wordpress post user id. (used when import article from newsgroup)</p>'."\n";
+	$out .= '<input type="text" name="wpusr" value="'.get_option(SYNCWP2NNTP_POST_USER).'"/>'."\n";
+	$out .= '<p></p>'."\n";
+	$out .= '<input name="submit" type="submit" value="save"/>'."\n";
+	$out .= '</form>';
+	$out .= '<form method="post">'."\n";
+	$out .= '<p>export wordpress entry to newsgroup (import only)</p>'."\n";
+	$out .= '<input type="submit" name="exp" value="'.$triger.'"/>'."\n";
+	$out .= '</form>';
+	$out .= '<p>execute manual refresh.</p>'."\n";
+	$out .= '<form method="post">'."\n";
+	$out .= '<input type="hidden" name="ref" value="1"/>'."\n";
+	$out .= '<input name="submit" type="submit" value="execute"/>'."\n";
+	$out .= '</form>';
+	$out .= '</div>'."\n";
+
+	echo($out);
+}
+
+/*
+	syncwp2nntp_activate
+	called on activate plugin.
+*/
+function syncwp2nntp_activate()
+{
+	add_option(SYNCWP2NNTP_SERVER_ADDRESS,DEFAULT_SERVER_ADDR);
+	add_option(SYNCWP2NNTP_REFRESH_INTERVAL,DEFAULT_REFRESH_INTERVAL);
+	add_option(SYNCWP2NNTP_POST_USER,DEFAULT_POST_USER);
+	add_option(SYNCWP2NNTP_EXPORT_NNTP,"enabled");
+	syncwp2nntp_schedule_event(DEFAULT_REFRESH_INTERVAL);
+}
+
+/*
+	syncwp2nntp_deactivate
+	called on deactivate plugin.
+*/
+function syncwp2nntp_deactivate()
+{
+	delete_option(SYNCWP2NNTP_SERVER_ADDRESS);
+	delete_option(SYNCWP2NNTP_REFRESH_INTERVAL);
+	delete_option(SYNCWP2NNTP_POST_USER,DEFAULT_POST_USER);
+	delete_option(SYNCWP2NNTP_EXPORT_NNTP);
+	syncwp2nntp_unschedule_event();
+}
+
+/*
+	syncwp2nntp_refresh_event_callback
+	called when refresh event has activated
+*/
+function syncwp2nntp_refresh_event_callback()
+{
+	syncwp2nntp_refresh();
+	syncwp2nntp_schedule_event(intval(get_option(SYNCWP2NNTP_REFRESH_INTERVAL)));
+}
+
+/*
+	syncwp2nntp_refresh
+	import article from server.
+*/
+function syncwp2nntp_refresh($_debug = false)
+{
+	//check newsgroup has modified?
+	$server = get_option(SYNCWP2NNTP_SERVER_ADDRESS);
+
+	$categories_data = syncwp2nntp_refresh_get_all_category_data();
+
+	if($_debug){
+		echo "\$categories_data\n";
+		//print_r($categories_data);
+	}
+
+	$objnntp = new Net_NNTP_Client();
+	if(PEAR::isError($objnntp->connect($server))){
+		echo "cloud not connect to server.\n";
+	}
+	$articles = array();
+	foreach($categories_data as $category){
+		if(PEAR::isError($objnntp->selectGroup($category["cat_name"]))){
+			continue;
+		}
+
+		$last = $objnntp->last();
+		$article_count = ARTICLE_LASTN_COUNT;
+		if($category["cat_last_id"] != -1){
+			$article_count = $last - $category["cat_last_id"];
+		}
+		
+		for($i = 0;$i < $article_count;++ $i){
+			if(PEAR::isError($objnntp->selectArticle($last - $i))){
+				continue;	
+			}
+			$article = array();
+
+			//get article subject,content,from,newsgroups
+			//article array will be like this
+			//$article["Subject"] => article subject
+			//$article["Content"] => article content
+			//$article["From"] => article author
+			//$article["Date"] => article posted date
+			//$article["Newsgroups"] => article's cross-posted newsgroup list (hashtable key => newsgroup name,value => article id)
+
+			//subject
+			$subject = $objnntp->getHeaderField("Subject");
+			if(ereg("=\?.*\?=",$subject)){
+				$subject = mb_decode_mimeheader($subject);
+			}
+			$subject = mb_convert_encoding($subject,WP_ENCODING,NNTP_DETECT_ENCODINGS);
+			$article["Subject"] = $subject;
+
+			//content
+			$content = $objnntp->getBody(null,true);
+			$content = mb_convert_encoding($content,WP_ENCODING,NNTP_DETECT_ENCODINGS);
+			$article["Content"] = $content;
+
+			//contenttype
+			$content_type = $objnntp->getHeaderField("Content-Type");
+			if(ereg("multipart",$content_type)){
+				//this is multipart format.
+				$tmp = explode('"',$content_type);
+				$boundary = $tmp[1];
+				$tmp = explode($boundary,$content);
+				$tmp = strstr($tmp[1],"\r\n\r\n");
+				$article["Content"] = $tmp."\nThis message contains attachments";
+			}
+			
+			//from
+			$from = $objnntp->getHeaderField("From");
+			if(ereg("^=\?.*\?=",$from)){
+				$from = mb_decode_mimeheader($from);
+			}
+			$from = mb_convert_encoding($from,WP_ENCODING,NNTP_DETECT_ENCODINGS);
+			$article["From"] = $from;
+		
+			//xref
+			$xref = $objnntp->getHeaderField("Xref");
+			$exp_xref = explode(" ",$xref);
+			$newsgroups = array();
+			for($j = 1;$j < count($exp_xref);++ $j){
+				$newsgroup = array();
+				$tmp = explode(":",$exp_xref[$j]);
+				// ex, some.news.group:9999
+				// $tmp[0] => newsgroup
+				// $tmp[1] => article id
+				$newsgroups[$tmp[0]] = $tmp[1];	
+			}
+			$article["Newsgroups"] = $newsgroups;
+
+			//date
+			$date = $objnntp->getHeaderField("Date");
+			$article["Date"] = $date;
+
+			//message-id
+			$message_id = $objnntp->getHeaderField("Message-ID");
+			$article["Message-ID"] = $message_id;
+
+			$articles[$message_id] = $article;	
+		}
+	}
+	$objnntp->disconnect();
+
+
+	remove_action("publish_post",FUNC_PUBLISH_POST);
+	foreach($articles as $article){
+		syncwp2nntp_refresh_insert_into_wordpress($article);
+	}
+	add_action("publish_post",FUNC_PUBLISH_POST);
+
+	return $articles;
+}
+
+/*
+	syncwp2nntp_refresh_insert_into_wordpress
+	create new wordpress post from $_article
+	to know $_article format written in syncwp2nntp_refresh function 
+*/
+function syncwp2nntp_refresh_insert_into_wordpress($_article)
+{
+	//build post data.
+	$the_post = array();
+	$the_post["post_title"] = $_article["Subject"];
+	$the_post["post_content"] = "From:".$_article["From"]."\n".$_article["Content"];
+	$the_post["post_date"] = date("Y-m-d H:i:s",strtotime($_article["Date"]));
+	$categories = array();
+	foreach(array_keys($_article["Newsgroups"]) as $newsgroup){
+		$cat_id = get_cat_ID($newsgroup);
+		if($cat_id != 0){
+			array_push($categories,$cat_id);
+		}
+	}
+	$the_post["post_category"] = $categories;
+	$the_post["post_author"] = 1;
+	$the_post["post_status"] = "publish"; //to change private
+
+	$the_post_id = wp_insert_post($the_post);
+	if($the_post_id == 0){
+		return false;
+	}
+
+	foreach(array_keys($_article["Newsgroups"]) as $category){
+		add_post_meta($the_post_id,$category,$_article["Newsgroups"][$category],true);
+	}
+	return true;
+}
+
+/*
+	syncwp2nntp_refresh_get_all_category_data
+
+	this function returns two dimensional array
+	$categories_data[category_name][categry_attribute]
+	category_attributes list.
+	cat_name : category name
+	cat_id : category id
+	cat_last_id : the last article ID corresponds to newsgroup. 
+*/
+function syncwp2nntp_refresh_get_all_category_data()
+{
+	$categories_data = array();
+	foreach(get_all_category_ids() as $cat_id){
+		$category = array();
+		$category["cat_name"] = get_cat_name($cat_id);
+		$category["cat_id"] = $cat_id;	
+		
+		$posts = get_posts("numberposts=1&post_status=publish,private&category=".$cat_id);
+		if(count($posts) == 0){
+			$category["cat_last_id"] = -1;
+		}else{
+			$category["cat_last_id"] = get_post_meta($posts[0]->ID,$category["cat_name"],true);
+		}
+		array_push($categories_data,$category);
+	}
+	return $categories_data;	
+}
+
+/*
+	syncwp2nntp_publish_post
+	export article to newsgroup
+*/
+function syncwp2nntp_publish_post($_post_id)
+{
+	if(get_option(SYNCWP2NNTP_EXPORT_NNTP) === "disable"){
+		return;
+	}
+
+	$the_post = get_post($_post_id);
+
+	$server = get_option(SYNCWP2NNTP_SERVER_ADDRESS);
+
+	$objnntp = new Net_NNTP_Client();
+	$ret = $objnntp->connect($server);
+	if(PEAR::isError($ret)){
+		//failed to connecting server.
+		return;	
+	}
+
+	$userinfo = get_userdata($the_post->post_author);
+
+	$subject = $the_post->post_title;
+	$subject = mb_encode_mimeheader($subject,NNTP_ENCODING);
+	$content = mb_convert_encoding($the_post->post_content,NNTP_ENCODING);
+
+	$categories = "";
+	foreach((get_the_category($_post_id)) as $category){
+		if(PEAR::isError($objnntp->selectGroup($category->cat_name))){
+			//if news-group does not exist.
+			continue;
+		}
+		$categories .= $category->cat_name.",";
+	}
+	$categories = rtrim($categories,",");
+	$objnntp->mail($categories,$subject,$content,"From: ".$userinfo->user_email,"Content-Type: text/plain;charset=".NNTP_ENCODING);
+
+	$draft_post = array();
+	$draft_post["ID"] = $_post_id;
+	$draft_post["post_status"] = "draft";
+
+	wp_update_post($draft_post);
+
+	$objnntp->disconnect();
+
+	syncwp2nntp_refresh();
+}
+
+/*
+	schedule user defined event.
+*/
+function syncwp2nntp_schedule_event($_interval)
+{
+	wp_schedule_single_event(time()+$_interval,EVENT_NAME);
+}
+
+/*
+	unschedule user defined event.
+*/
+function syncwp2nntp_unschedule_event()
+{
+	wp_clear_scheduled_hook(EVENT_NAME);
+}
+
+?>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/syncwp2nntp.php	Wed Aug 08 20:02:35 2012 +0900
@@ -0,0 +1,36 @@
+<?php
+
+/*
+Plugin Name: syncwp2nntp
+Plugin URI: http://misaka.cr.ie.u-ryukyu.ac.jp/
+Description: syncronizes wordpress between netnews.
+Version: 10.0422
+Author:	e075766
+Author URI: http://misaka.cr.ie.u-ryukyu.ac.jp/
+*/
+
+/*  Copyright 2010/01/19 e075766 (e075766@ie.u-ryukyu.ac.jp)
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+require_once "include/syncwp2nntp_functions.php";
+
+add_action("admin_menu",FUNC_ADD_PAGE);
+add_action("publish_post",FUNC_PUBLISH_POST);
+add_action(EVENT_NAME,FUNC_REFRESH_EVENT_CALLBACK);
+register_activation_hook(__FILE__,"syncwp2nntp_activate");
+register_deactivation_hook(__FILE__,"syncwp2nntp_deactivate");
+?>