# HG changeset patch # User Shoshi TAMAKI # Date 1344423755 -32400 # Node ID 1a30ce1834e5f65566753d325da12084426b1d12 added README diff -r 000000000000 -r 1a30ce1834e5 README --- /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...) diff -r 000000000000 -r 1a30ce1834e5 debug/test_refresh.php --- /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 @@ + diff -r 000000000000 -r 1a30ce1834e5 include/syncwp2nntp_functions.php --- /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 @@ +'."\n"; + $out .= '

syncwp2nmtp configuration

'."\n"; + $out .= '
'."\n"; + $out .= '

server address.

'."\n"; + $out .= ''."\n"; + $out .= '

refresh interval. (seconds)

'."\n"; + $out .= ''."\n"; + $out .= '

wordpress post user id. (used when import article from newsgroup)

'."\n"; + $out .= ''."\n"; + $out .= '

'."\n"; + $out .= ''."\n"; + $out .= '
'; + $out .= '
'."\n"; + $out .= '

export wordpress entry to newsgroup (import only)

'."\n"; + $out .= ''."\n"; + $out .= '
'; + $out .= '

execute manual refresh.

'."\n"; + $out .= '
'."\n"; + $out .= ''."\n"; + $out .= ''."\n"; + $out .= '
'; + $out .= ''."\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); +} + +?> diff -r 000000000000 -r 1a30ce1834e5 syncwp2nntp.php --- /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 @@ +