Mercurial > hg > Members > shoshi > syncwp2nntp
comparison include/syncwp2nntp_functions.php @ 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1a30ce1834e5 |
---|---|
1 <?php | |
2 /* | |
3 syncwp2nntp_functions.php | |
4 function definition. | |
5 */ | |
6 | |
7 require_once "Net/NNTP/Client.php"; | |
8 | |
9 //for nntpd | |
10 define("DEFAULT_SERVER_ADDR","news.example.com"); //please change this address to own nntpd. | |
11 define("NNTP_ENCODING","ISO-2022-JP"); | |
12 define("WP_ENCODING","UTF-8"); | |
13 define("NNTP_DETECT_ENCODINGS","ISO-2022-JP,UTF-8,EUC-JP,Shift_JIS"); | |
14 | |
15 //if you want to authenticate on nntpd , enable code below. | |
16 /* | |
17 | |
18 define("NNTP_ENABLE_AUTHENTICATE","TRUE"); | |
19 define("NNTP_USERNAME","nntpuser"); // change username | |
20 define("NNTP_PASSWORD","nntppass"); // change password | |
21 | |
22 */ | |
23 | |
24 //for wordpress | |
25 define("EVENT_NAME","syncwp2nntp_refresh_event_2"); | |
26 define("DEFAULT_REFRESH_INTERVAL",360); //default 360sec | |
27 define("FUNC_PUBLISH_POST","syncwp2nntp_publish_post"); | |
28 define("FUNC_REFRESH_EVENT_CALLBACK","syncwp2nntp_refresh_event_callback"); | |
29 define("FUNC_ADD_PAGE","syncwp2nntp_add_page"); | |
30 define("ARTICLE_LASTN_COUNT",5); //default 5 articles. | |
31 define("DEFAULT_POST_USER",1); //default 1 (admin). | |
32 | |
33 //for options | |
34 define("SYNCWP2NNTP_SERVER_ADDRESS","syncwp2nntp_server_address"); | |
35 define("SYNCWP2NNTP_REFRESH_INTERVAL","syncwp2nntp_refresh_interval"); | |
36 define("SYNCWP2NNTP_POST_USER","syncwp2nntp_post_user"); | |
37 define("SYNCWP2NNTP_EXPORT_NNTP","syncwp2nntp_export_nntp"); | |
38 | |
39 /* | |
40 syncwp2nntp_add_page | |
41 create submenu page | |
42 */ | |
43 function syncwp2nntp_add_page($_arg) | |
44 { | |
45 // User Level Permission | |
46 // -- Subscriber = 0,Contributor = 1,Author = 2,Editor= 7,Administrator = 9 | |
47 add_submenu_page('options-general.php','syncwp2nntp','syncwp2nntp',7, __FILE__, 'syncwp2nntp_add_menu'); | |
48 | |
49 return $_arg; | |
50 } | |
51 | |
52 /* | |
53 syncwp2nntp_add_menu | |
54 create menu page | |
55 */ | |
56 function syncwp2nntp_add_menu() | |
57 { | |
58 //commit configuration. | |
59 if(isset($_POST["srv"]) && isset($_POST["int"])){ | |
60 $new_server = $_POST["srv"]; | |
61 $new_interval = $_POST["int"]; | |
62 | |
63 update_option(SYNCWP2NNTP_SERVER_ADDRESS,$new_server); | |
64 update_option(SYNCWP2NNTP_REFRESH_INTERVAL,$new_interval); | |
65 } | |
66 if(isset($_POST["exp"])){ | |
67 update_option(SYNCWP2NNTP_EXPORT_NNTP,$_POST["exp"]); | |
68 } | |
69 if(isset($_POST["ref"])){ | |
70 syncwp2nntp_refresh(); | |
71 } | |
72 | |
73 $triger = "disable"; | |
74 if(get_option(SYNCWP2NNTP_EXPORT_NNTP) === "disable"){ | |
75 $triger = "enable"; | |
76 } | |
77 | |
78 //generate configuration html. | |
79 $out = '<div class="wrap">'."\n"; | |
80 $out .= '<h2>syncwp2nmtp configuration</h2>'."\n"; | |
81 $out .= '<form method="post">'."\n"; | |
82 $out .= '<p>server address.</p>'."\n"; | |
83 $out .= '<input type="text" name="srv" value="'.get_option(SYNCWP2NNTP_SERVER_ADDRESS).'"/>'."\n"; | |
84 $out .= '<p>refresh interval. (seconds)</p>'."\n"; | |
85 $out .= '<input type="text" name="int" value="'.get_option(SYNCWP2NNTP_REFRESH_INTERVAL).'"/>'."\n"; | |
86 $out .= '<p>wordpress post user id. (used when import article from newsgroup)</p>'."\n"; | |
87 $out .= '<input type="text" name="wpusr" value="'.get_option(SYNCWP2NNTP_POST_USER).'"/>'."\n"; | |
88 $out .= '<p></p>'."\n"; | |
89 $out .= '<input name="submit" type="submit" value="save"/>'."\n"; | |
90 $out .= '</form>'; | |
91 $out .= '<form method="post">'."\n"; | |
92 $out .= '<p>export wordpress entry to newsgroup (import only)</p>'."\n"; | |
93 $out .= '<input type="submit" name="exp" value="'.$triger.'"/>'."\n"; | |
94 $out .= '</form>'; | |
95 $out .= '<p>execute manual refresh.</p>'."\n"; | |
96 $out .= '<form method="post">'."\n"; | |
97 $out .= '<input type="hidden" name="ref" value="1"/>'."\n"; | |
98 $out .= '<input name="submit" type="submit" value="execute"/>'."\n"; | |
99 $out .= '</form>'; | |
100 $out .= '</div>'."\n"; | |
101 | |
102 echo($out); | |
103 } | |
104 | |
105 /* | |
106 syncwp2nntp_activate | |
107 called on activate plugin. | |
108 */ | |
109 function syncwp2nntp_activate() | |
110 { | |
111 add_option(SYNCWP2NNTP_SERVER_ADDRESS,DEFAULT_SERVER_ADDR); | |
112 add_option(SYNCWP2NNTP_REFRESH_INTERVAL,DEFAULT_REFRESH_INTERVAL); | |
113 add_option(SYNCWP2NNTP_POST_USER,DEFAULT_POST_USER); | |
114 add_option(SYNCWP2NNTP_EXPORT_NNTP,"enabled"); | |
115 syncwp2nntp_schedule_event(DEFAULT_REFRESH_INTERVAL); | |
116 } | |
117 | |
118 /* | |
119 syncwp2nntp_deactivate | |
120 called on deactivate plugin. | |
121 */ | |
122 function syncwp2nntp_deactivate() | |
123 { | |
124 delete_option(SYNCWP2NNTP_SERVER_ADDRESS); | |
125 delete_option(SYNCWP2NNTP_REFRESH_INTERVAL); | |
126 delete_option(SYNCWP2NNTP_POST_USER,DEFAULT_POST_USER); | |
127 delete_option(SYNCWP2NNTP_EXPORT_NNTP); | |
128 syncwp2nntp_unschedule_event(); | |
129 } | |
130 | |
131 /* | |
132 syncwp2nntp_refresh_event_callback | |
133 called when refresh event has activated | |
134 */ | |
135 function syncwp2nntp_refresh_event_callback() | |
136 { | |
137 syncwp2nntp_refresh(); | |
138 syncwp2nntp_schedule_event(intval(get_option(SYNCWP2NNTP_REFRESH_INTERVAL))); | |
139 } | |
140 | |
141 /* | |
142 syncwp2nntp_refresh | |
143 import article from server. | |
144 */ | |
145 function syncwp2nntp_refresh($_debug = false) | |
146 { | |
147 //check newsgroup has modified? | |
148 $server = get_option(SYNCWP2NNTP_SERVER_ADDRESS); | |
149 | |
150 $categories_data = syncwp2nntp_refresh_get_all_category_data(); | |
151 | |
152 if($_debug){ | |
153 echo "\$categories_data\n"; | |
154 //print_r($categories_data); | |
155 } | |
156 | |
157 $objnntp = new Net_NNTP_Client(); | |
158 if(PEAR::isError($objnntp->connect($server))){ | |
159 echo "cloud not connect to server.\n"; | |
160 } | |
161 $articles = array(); | |
162 foreach($categories_data as $category){ | |
163 if(PEAR::isError($objnntp->selectGroup($category["cat_name"]))){ | |
164 continue; | |
165 } | |
166 | |
167 $last = $objnntp->last(); | |
168 $article_count = ARTICLE_LASTN_COUNT; | |
169 if($category["cat_last_id"] != -1){ | |
170 $article_count = $last - $category["cat_last_id"]; | |
171 } | |
172 | |
173 for($i = 0;$i < $article_count;++ $i){ | |
174 if(PEAR::isError($objnntp->selectArticle($last - $i))){ | |
175 continue; | |
176 } | |
177 $article = array(); | |
178 | |
179 //get article subject,content,from,newsgroups | |
180 //article array will be like this | |
181 //$article["Subject"] => article subject | |
182 //$article["Content"] => article content | |
183 //$article["From"] => article author | |
184 //$article["Date"] => article posted date | |
185 //$article["Newsgroups"] => article's cross-posted newsgroup list (hashtable key => newsgroup name,value => article id) | |
186 | |
187 //subject | |
188 $subject = $objnntp->getHeaderField("Subject"); | |
189 if(ereg("=\?.*\?=",$subject)){ | |
190 $subject = mb_decode_mimeheader($subject); | |
191 } | |
192 $subject = mb_convert_encoding($subject,WP_ENCODING,NNTP_DETECT_ENCODINGS); | |
193 $article["Subject"] = $subject; | |
194 | |
195 //content | |
196 $content = $objnntp->getBody(null,true); | |
197 $content = mb_convert_encoding($content,WP_ENCODING,NNTP_DETECT_ENCODINGS); | |
198 $article["Content"] = $content; | |
199 | |
200 //contenttype | |
201 $content_type = $objnntp->getHeaderField("Content-Type"); | |
202 if(ereg("multipart",$content_type)){ | |
203 //this is multipart format. | |
204 $tmp = explode('"',$content_type); | |
205 $boundary = $tmp[1]; | |
206 $tmp = explode($boundary,$content); | |
207 $tmp = strstr($tmp[1],"\r\n\r\n"); | |
208 $article["Content"] = $tmp."\nThis message contains attachments"; | |
209 } | |
210 | |
211 //from | |
212 $from = $objnntp->getHeaderField("From"); | |
213 if(ereg("^=\?.*\?=",$from)){ | |
214 $from = mb_decode_mimeheader($from); | |
215 } | |
216 $from = mb_convert_encoding($from,WP_ENCODING,NNTP_DETECT_ENCODINGS); | |
217 $article["From"] = $from; | |
218 | |
219 //xref | |
220 $xref = $objnntp->getHeaderField("Xref"); | |
221 $exp_xref = explode(" ",$xref); | |
222 $newsgroups = array(); | |
223 for($j = 1;$j < count($exp_xref);++ $j){ | |
224 $newsgroup = array(); | |
225 $tmp = explode(":",$exp_xref[$j]); | |
226 // ex, some.news.group:9999 | |
227 // $tmp[0] => newsgroup | |
228 // $tmp[1] => article id | |
229 $newsgroups[$tmp[0]] = $tmp[1]; | |
230 } | |
231 $article["Newsgroups"] = $newsgroups; | |
232 | |
233 //date | |
234 $date = $objnntp->getHeaderField("Date"); | |
235 $article["Date"] = $date; | |
236 | |
237 //message-id | |
238 $message_id = $objnntp->getHeaderField("Message-ID"); | |
239 $article["Message-ID"] = $message_id; | |
240 | |
241 $articles[$message_id] = $article; | |
242 } | |
243 } | |
244 $objnntp->disconnect(); | |
245 | |
246 | |
247 remove_action("publish_post",FUNC_PUBLISH_POST); | |
248 foreach($articles as $article){ | |
249 syncwp2nntp_refresh_insert_into_wordpress($article); | |
250 } | |
251 add_action("publish_post",FUNC_PUBLISH_POST); | |
252 | |
253 return $articles; | |
254 } | |
255 | |
256 /* | |
257 syncwp2nntp_refresh_insert_into_wordpress | |
258 create new wordpress post from $_article | |
259 to know $_article format written in syncwp2nntp_refresh function | |
260 */ | |
261 function syncwp2nntp_refresh_insert_into_wordpress($_article) | |
262 { | |
263 //build post data. | |
264 $the_post = array(); | |
265 $the_post["post_title"] = $_article["Subject"]; | |
266 $the_post["post_content"] = "From:".$_article["From"]."\n".$_article["Content"]; | |
267 $the_post["post_date"] = date("Y-m-d H:i:s",strtotime($_article["Date"])); | |
268 $categories = array(); | |
269 foreach(array_keys($_article["Newsgroups"]) as $newsgroup){ | |
270 $cat_id = get_cat_ID($newsgroup); | |
271 if($cat_id != 0){ | |
272 array_push($categories,$cat_id); | |
273 } | |
274 } | |
275 $the_post["post_category"] = $categories; | |
276 $the_post["post_author"] = 1; | |
277 $the_post["post_status"] = "publish"; //to change private | |
278 | |
279 $the_post_id = wp_insert_post($the_post); | |
280 if($the_post_id == 0){ | |
281 return false; | |
282 } | |
283 | |
284 foreach(array_keys($_article["Newsgroups"]) as $category){ | |
285 add_post_meta($the_post_id,$category,$_article["Newsgroups"][$category],true); | |
286 } | |
287 return true; | |
288 } | |
289 | |
290 /* | |
291 syncwp2nntp_refresh_get_all_category_data | |
292 | |
293 this function returns two dimensional array | |
294 $categories_data[category_name][categry_attribute] | |
295 category_attributes list. | |
296 cat_name : category name | |
297 cat_id : category id | |
298 cat_last_id : the last article ID corresponds to newsgroup. | |
299 */ | |
300 function syncwp2nntp_refresh_get_all_category_data() | |
301 { | |
302 $categories_data = array(); | |
303 foreach(get_all_category_ids() as $cat_id){ | |
304 $category = array(); | |
305 $category["cat_name"] = get_cat_name($cat_id); | |
306 $category["cat_id"] = $cat_id; | |
307 | |
308 $posts = get_posts("numberposts=1&post_status=publish,private&category=".$cat_id); | |
309 if(count($posts) == 0){ | |
310 $category["cat_last_id"] = -1; | |
311 }else{ | |
312 $category["cat_last_id"] = get_post_meta($posts[0]->ID,$category["cat_name"],true); | |
313 } | |
314 array_push($categories_data,$category); | |
315 } | |
316 return $categories_data; | |
317 } | |
318 | |
319 /* | |
320 syncwp2nntp_publish_post | |
321 export article to newsgroup | |
322 */ | |
323 function syncwp2nntp_publish_post($_post_id) | |
324 { | |
325 if(get_option(SYNCWP2NNTP_EXPORT_NNTP) === "disable"){ | |
326 return; | |
327 } | |
328 | |
329 $the_post = get_post($_post_id); | |
330 | |
331 $server = get_option(SYNCWP2NNTP_SERVER_ADDRESS); | |
332 | |
333 $objnntp = new Net_NNTP_Client(); | |
334 $ret = $objnntp->connect($server); | |
335 if(PEAR::isError($ret)){ | |
336 //failed to connecting server. | |
337 return; | |
338 } | |
339 | |
340 $userinfo = get_userdata($the_post->post_author); | |
341 | |
342 $subject = $the_post->post_title; | |
343 $subject = mb_encode_mimeheader($subject,NNTP_ENCODING); | |
344 $content = mb_convert_encoding($the_post->post_content,NNTP_ENCODING); | |
345 | |
346 $categories = ""; | |
347 foreach((get_the_category($_post_id)) as $category){ | |
348 if(PEAR::isError($objnntp->selectGroup($category->cat_name))){ | |
349 //if news-group does not exist. | |
350 continue; | |
351 } | |
352 $categories .= $category->cat_name.","; | |
353 } | |
354 $categories = rtrim($categories,","); | |
355 $objnntp->mail($categories,$subject,$content,"From: ".$userinfo->user_email,"Content-Type: text/plain;charset=".NNTP_ENCODING); | |
356 | |
357 $draft_post = array(); | |
358 $draft_post["ID"] = $_post_id; | |
359 $draft_post["post_status"] = "draft"; | |
360 | |
361 wp_update_post($draft_post); | |
362 | |
363 $objnntp->disconnect(); | |
364 | |
365 syncwp2nntp_refresh(); | |
366 } | |
367 | |
368 /* | |
369 schedule user defined event. | |
370 */ | |
371 function syncwp2nntp_schedule_event($_interval) | |
372 { | |
373 wp_schedule_single_event(time()+$_interval,EVENT_NAME); | |
374 } | |
375 | |
376 /* | |
377 unschedule user defined event. | |
378 */ | |
379 function syncwp2nntp_unschedule_event() | |
380 { | |
381 wp_clear_scheduled_hook(EVENT_NAME); | |
382 } | |
383 | |
384 ?> |