OSDN Git Service

とりあえず出来てるところまで
[nucleus-jp/nucleus-plugins.git] / trunk / NP_PingJP / NP_PingJP.php
1 <?php
2 /**
3   *
4   * Send weblog updates ping
5   *     plugin for NucleusCMS(version 3.31 or lator)
6   *     Note: based on NP_Ping v1.5
7   * PHP versions 4 and 5
8   *
9   * This program is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; either version 2
12   * of the License, or (at your option) any later version.
13   * (see nucleus/documentation/index.html#license for more info)
14   *
15   * @author    shizuki
16   * @copyright 2007 shizuki
17   * @license   http://www.gnu.org/licenses/gpl.txt  GNU GENERAL PUBLIC LICENSE Version 2, June 1991
18   * @version   1.6
19   * @link      http://shizuki.kinezumi.net/
20   *
21   * History of NP_Ping
22   *   v1.0 - Initial version
23   *   v1.1 - Add JustPosted event support
24   *   v1.2 - JustPosted event handling in background
25   *   v1.3 - pinged variable support
26   *   v1.4 - language file support
27   *   v1.5 - remove arg1 in exec() call
28   *
29   * History of NP_PingJP
30   *   v1.6 - Modified NP_Ping v1.5
31   *          merge NP_SendPing(by Tokitake) code
32   *
33   **/
34
35
36 class NP_PingJP extends NucleusPlugin
37 {
38
39 var $debug = false;
40
41         // {{{ function getName()
42
43         /**
44           * Name of the plugin
45           *
46           * @access public
47           *
48           * @return string
49           *     The name easy to understand for man of the plugin
50           **/
51         function getName()
52         {
53                 return 'Ping';
54         }
55
56         // }}}
57         // {{{ function getAuthor()
58
59         /**
60           * Author of the plugin
61           *
62           * @access public
63           *
64           * @return string
65           *     The name of the plugin author
66           **/
67         function getAuthor()
68         {
69                 return 'admun (Edmond Hui)+ Tokitake + shizuki';
70         }
71
72         // }}}
73         // {{{ function getURL()
74
75         /**
76           * URL of the site which can download a plugin
77           *
78           * @access public
79           *
80           * @return string
81           *     URL of the site which can download a plugin
82           **/
83         function getURL()
84         {
85                 return 'http://shizuki.kinezumi.net/';
86         }
87
88         // }}}
89         // {{{ function getVersion()
90
91         /**
92           * Version of the plugin
93           *
94           * @access public
95           *
96           * @return string
97           *     Version of the plugin
98           **/
99         function getVersion()
100         {
101                 return '1.6';
102         }
103
104         // }}}
105         // {{{ function getMinNucleusVersion()
106
107         /**
108           * Requier NucleusCMS version of the plugin
109           *
110           * @access public
111           *
112           * @return string
113           *     Requier NucleusCMS version of a plugin
114           **/
115         function getMinNucleusVersion()
116         {
117                 return '331';
118         }
119
120         // }}}
121         // {{{ function getDescription()
122
123         /**
124           * Description of the plugin
125           *
126           * @access public
127           *
128           * @return string
129           *     Description of a plugin
130           **/
131         function getDescription()
132         {
133                 return _PINGJP_DESC;
134         }
135
136         // }}}
137         // {{{ function supportsFeature($what)
138
139         /**
140           * Check whether the feature is being supported.
141           *
142           * @access public
143           *
144           * @param  string
145           *     Feature name
146           *
147           * @return boolean
148           **/
149         function supportsFeature($what)
150         {
151                 switch($what) {
152                         case 'SqlTablePrefix':
153                                 return 1;
154                         default:
155                                 return 0;
156                 }
157         }
158
159         // }}}
160         // {{{ function install()
161
162         /**
163           * Plugin installing action
164           *
165           * @access public
166           *
167           * @return void
168           **/
169         function install()
170         {
171                 // Default, http://pingomatic.com
172                 $this->createBlogOption('pingjp_pingomatic',  _PINGJP_PINGOM,    'yesno',    'yes');
173                 // http://weblogs.com
174                 $this->createBlogOption('pingjp_weblogs',     _PINGJP_WEBLOGS,   'yesno',    'no');
175                 // http://www.technorati.com
176                 $this->createBlogOption('pingjp_technorati',  _PINGJP_TECHNOR,   'yesno',    'no');
177                 // http://www.blogrolling.com
178                 $this->createBlogOption('pingjp_blogrolling', _PINGJP_BLOGR,     'yesno',    'no');
179                 // http://www.google.com
180                 $this->createBlogOption('pingjp_google',      _PINGJP_GOOGLE,    'yesno',    'yes');
181                 // http://www.yahoo.co.jp
182                 $this->createBlogOption('pingjp_yahoo',       _PINGJP_YAHOO,     'yesno',    'yes');
183                 // http://www.goo.ne.jp
184                 $this->createBlogOption('pingjp_goo',         _PINGJP_GOO,       'yesno',    'no');
185                 // http://ask.jp
186                 $this->createBlogOption('pingjp_ask',         _PINGJP_ASK,       'yesno',    'no');
187                 // http://blog360.jp
188                 $this->createBlogOption('pingjp_blog360',     _PINGJP_BLOG360,   'yesno',    'yes');
189                 // http://pingoo.jp
190                 $this->createBlogOption('pingjp_pingoo',      _PINGJP_PINGOO,    'yesno',    'no');
191                 // http://blo.gs
192                 $this->createBlogOption('pingjp_blogs',       _PINGJP_BLOGS,     'yesno',    'no');
193                 // http://weblogues.com/
194                 $this->createBlogOption('pingjp_weblogues',   _PINGJP_WEBLOGUES, 'yesno',    'no');
195                 // http://blogg.de
196                 $this->createBlogOption('pingjp_bloggde',     _PINGJP_BLOGGDE,   'yesno',    'no');
197                 // other ping server
198                 $this->createBlogOption('pingjp_otherurl',    _PINGJP_OTHER,     'textarea', '');
199                 // background ?
200                 $this->createBlogOption('pingjp_background',  _PINGJP_BG,        'yesno',    'yes');
201                 // Your blog URL
202                 $this->createBlogOption('pingjp_updateurl',   _PINGJP_UPDURL,    'text',     '');
203                 // Your RSS URL
204                 $this->createBlogOption('pingjp_feedsurl',    _PINGJP_UPDFEED,   'text',     '');
205         }
206
207         // }}}
208         // {{{ function init()
209
210         /**
211           * Plugin initialize action
212           *
213           * @access public
214           *
215           * @return void
216           **/
217         function init()
218         {
219                 $language = ereg_replace( '[\\|/]', '', getLanguageName());
220                 if (file_exists($this->getDirectory() . 'language/' . $language . '.php')) {
221                         include_once($this->getDirectory() . 'language/' . $language . '.php');
222                 } else {
223                         include_once($this->getDirectory() . 'language/english.php');
224                 }
225         }
226
227         // }}}
228         // {{{ function getEventList()
229
230         /**
231           * Event list plugin exist
232           *
233           * @access public
234           *
235           * @return array
236           *     exist events
237           **/
238         function getEventList()
239         {
240                 return array(
241                         'SendPing',
242                         'JustPosted',
243                         'EditItemFormExtras',
244                         'PostUpdateItem',
245                 );
246         }
247
248         // }}}
249         // {{{ function event_PostUpdateItem($data)
250
251         /**
252           * Event ITEM updated
253           *
254           * @access public
255           *
256           * @param  array
257           *     itemid : value intger
258           *         update item ID
259           *
260           * @return void
261           **/
262         function event_PostUpdateItem($data) {
263                 global $manager;
264                 if (requestVar('np_pingjp_check') == 1) {
265                         $iid  = intval($data['itemid']);
266                         $item =& $manager->getItem($iid, 0, 0);
267                         // don't ping on draft or future items
268                         if (!$item || $item['draft']) return;
269                         $this->sendPing(getBlogIDFromItemID($iid));
270                 }
271     }
272
273         // }}}
274         // {{{ function event_EditItemFormExtras($data)
275
276         /**
277           * Event display ITEM edit form
278           *     adding plugin specify for ITEM edit form
279           *
280           * @access public
281           *
282           * @param  array
283           *     blog      : reference object
284           *         BLOG object
285           *     variables : value array
286           *         containing all sorts of information on the item that's being edited
287           *             itemid    : intger
288           *                 item ID
289           *             draft     : intger(boolean)
290           *                 ITEM draft status
291           *                     public : 0
292           *                     draft  : 1
293           *             closed    : intger(boolean)
294           *                 ITEM comments status
295           *                     accept     : 0
296           *                     not accept : 1
297           *             title     : string
298           *                 item title
299           *             body      : string
300           *                 item main text
301           *             more      : string
302           *                 item extended text
303           *             author    : string
304           *                 item author
305           *             authorid  : intger
306           *                 item author ID
307           *             timestamp : intger
308           *                 item timestamp
309           *             karmapos  : intger
310           *                 item karmapos
311           *             karmaneg  : intger
312           *                 item karmaneg
313           *             catid     : intger
314           *                 item category ID
315           *     itemid    : value intger
316           *         editing item ID
317           *
318           * @return void
319           **/
320         function event_EditItemFormExtras($data)
321         {
322                 ?>
323                 <div style="display:block">
324                         <h3>NP_PingJP</h3>
325                         <p>
326                                 <label for="np_pingjp_check">Send Ping ?:</label>
327                                 <input type="checkbox" value="1" id="np_pingjp_check" name="np_pingjp_check" />
328                         </p>
329                 </div>
330                 <?php
331         }
332
333         // }}}
334         // {{{ function event_JustPosted($data)
335
336         /**
337           * Event ITEM timstamp as now
338           *     send update ping or etc.
339           *
340           * @access public
341           *
342           * @param  array
343           *     blogid : value intger
344           *         blog ID
345           *     pinged : reference boolean
346           *         Update ping completed as true
347           *
348           * @return void
349           **/
350         function event_JustPosted($data)
351         {
352                 if ($data['pinged'] == true) {
353                         return;
354                 }
355                 if ($this->getBlogOption($data['blogid'], 'pingjp_background') == "yes") {
356                         $this->sendPings($data['blogid'], true);
357                 } else {
358                         $this->sendPings($data['blogid']);
359                 }
360                 $data['pinged'] = true;
361         }
362
363         // }}}
364         // {{{ function event_SendPing($data)
365
366         /**
367           * Event send weblog updates ping
368           *     when add ITEM
369           *
370           * @access public
371           *
372           * @param  array
373           *     blogid : value intger
374           *         blog ID
375           *
376           * @return void
377           **/
378         function event_SendPing($data)
379         {
380                 $this->sendPing($data['blogid']);
381         }
382
383         // }}}
384         // {{{ function sendPing($myBlogId, $background = false)
385
386         /**
387           * Setting ping servers
388           *
389           * @access public
390           *
391           * @param  intger
392           *     blog ID
393           * @param  boolean
394           *     Send ping background or foreground
395           *
396           * @return void
397           **/
398         function sendPing($myBlogId, $background = false)
399         {
400                 $pinging = array();
401                 if ($this->getBlogOption($myBlogId, 'pingjp_pingomatic') == 'yes') {
402                         $pinging[]['target'] = _PINGJP_PINGOM;
403                         $pinging[]['host']   = 'rpc.pingomatic.com';
404                         $pinging[]['path']   = '/';
405                         $pinging[]['port']   = 80;
406                         $pinging[]['method'] = 'weblogUpdates.ping';
407                 }
408
409                 if ($this->getBlogOption($myBlogId, 'pingjp_weblogs') == 'yes') { 
410                         $pinging[]['target'] = _PINGJP_WEBLOGS;
411                         $pinging[]['host']   = 'rpc.weblogs.com';
412                         $pinging[]['path']   = '/rpc2';
413                         $pinging[]['port']   = 80;
414                         $pinging[]['method'] = 'weblogUpdates.extendedPing';
415                 }
416
417                 if ($this->getBlogOption($myBlogId, 'pingjp_technorati') == 'yes') {
418                         $pinging[]['target'] = _PINGJP_TECHNOR;
419                         $pinging[]['host']   = 'rpc.technorati.com';
420                         $pinging[]['path']   = '/rpc/ping';
421                         $pinging[]['port']   = 80;
422                         $pinging[]['method'] = 'weblogUpdates.ping';
423                 }
424
425                 if ($this->getBlogOption($myBlogId, 'pingjp_blogrolling') == 'yes') {
426                         $pinging[]['target'] = _PINGJP_BLOGR;
427                         $pinging[]['host']   = 'rpc.blogrolling.com';
428                         $pinging[]['path']   = '/pinger/';
429                         $pinging[]['port']   = 80;
430                         $pinging[]['method'] = 'weblogUpdates.ping';
431                 }
432
433                 if ($this->getBlogOption($myBlogId, 'pingjp_google') == 'yes') {
434                         $pinging[]['target'] = _PINGJP_GOOGLE;
435                         $pinging[]['host']   = 'blogsearch.google.co.jp';
436                         $pinging[]['path']   = '/ping/RPC2';
437                         $pinging[]['port']   = 80;
438                         $pinging[]['method'] = 'weblogUpdates.extendedPing';
439                 }
440
441                 if ($this->getBlogOption($myBlogId, 'pingjp_yahoo') == 'yes') {
442                         $pinging[]['target'] = _PINGJP_YAHOO;
443                         $pinging[]['host']   = 'api.my.yahoo.co.jp';
444                         $pinging[]['path']   = '/RPC2';
445                         $pinging[]['port']   = 80;
446                         $pinging[]['method'] = 'weblogUpdates.ping';
447                 }
448
449                 if ($this->getBlogOption($myBlogId, 'pingjp_goo') == 'yes') {
450                         $pinging[]['target'] = _PINGJP_GOO;
451                         $pinging[]['host']   = 'blog.goo.ne.jp';
452                         $pinging[]['path']   = '/XMLRPC';
453                         $pinging[]['port']   = 80;
454                         $pinging[]['method'] = 'weblogUpdates.ping';
455                 }
456
457                 if ($this->getBlogOption($myBlogId, 'pingjp_ask') == 'yes') {
458                         $pinging[]['target'] = _PINGJP_ASK;
459                         $pinging[]['host']   = 'ping.ask.jp';
460                         $pinging[]['path']   = '/xmlrpc.m';
461                         $pinging[]['port']   = 80;
462                         $pinging[]['method'] = 'weblogUpdates.ping';
463                 }
464
465                 if ($this->getBlogOption($myBlogId, 'pingjp_blog360') == 'yes') {
466                         $pinging[]['target'] = _PINGJP_BLOG360;
467                         $pinging[]['host']   = 'ping.blog360.jp';
468                         $pinging[]['path']   = '/rpc';
469                         $pinging[]['port']   = 80;
470                         $pinging[]['method'] = 'weblogUpdates.ping';
471                 }
472
473                 if ($this->getBlogOption($myBlogId, 'pingjp_pingoo') == 'yes') {
474                         $pinging[]['target'] = _PINGJP_PINGOO;
475                         $pinging[]['host']   = 'pingoo.jp';
476                         $pinging[]['path']   = '/ping';
477                         $pinging[]['port']   = 80;
478                         $pinging[]['method'] = 'weblogUpdates.ping';
479                 }
480
481                 if ($this->getBlogOption($myBlogId, 'pingjp_blogs') == 'yes') {
482                         $pinging[]['target'] = _PINGJP_BLOGS;
483                         $pinging[]['host']   = 'ping.blo.gs';
484                         $pinging[]['path']   = '/';
485                         $pinging[]['port']   = 80;
486                         $pinging[]['method'] = 'weblogUpdates.extendedPing';
487                 }
488
489                 if ($this->getBlogOption($myBlogId, 'pingjp_weblogues') == 'yes') {
490                         $pinging[]['target'] = _PINGJP_WEBLOGUES;
491                         $pinging[]['host']   = 'www.weblogues.com';
492                         $pinging[]['path']   = '/RPC/';
493                         $pinging[]['port']   = 80;
494                         $pinging[]['method'] = 'weblogUpdates.extendedPing';
495                 }
496
497                 if ($this->getBlogOption($myBlogId, 'pingjp_bloggde') == 'yes') {
498                         $pinging[]['target'] = _PINGJP_BLOGGDE;
499                         $pinging[]['host']   = 'xmlrpc.blogg.de';
500                         $pinging[]['path']   = '/ping';
501                         $pinging[]['port']   = 80;
502                         $pinging[]['method'] = 'bloggUpdates.ping';
503                 }
504
505                 if ($this->getBlogOption($myBlogId, 'pingjp_otherurl') != '') {
506                         $others  = $this->getBlogOption($myBlogId, 'pingjp_otherurl')
507                         $servers = preg_split("/[\s,]+/", $others);
508                         foreach ($servers as $target) {
509                                 if (strpos($target), ',') {
510                                         list($url, $method) = explode(',', $target);
511                                         $parsed = parse_url($url);
512                                         if ($method == 'ex') {
513                                                 $method = 'weblogUpdates.extendedPing';
514                                         }
515                                 } else {
516                                         $parsed = parse_url($target);
517                                         $method = 'weblogUpdates.ping';
518                                 }
519                                 $pinging[]['target'] = $parsed['host'];
520                                 $pinging[]['host']   = $parsed['host'];
521                                 $pinging[]['path']   = $parsed['path'];
522                                 $pinging[]['port']   = (!$parsed['port']) ? 80 : $parsed['port'];
523                                 $pinging[]['method'] = $method;
524                         }
525                 }
526                 foreach ($pinging as $sendPing) {
527                         $this->sendUpdatePing($myBlogId, $sendPing, $background);
528                 }
529         }
530
531         // }}}
532         // {{{ function sendUpdatePing($myBlogId, $sendPing, $background = false)
533
534         /**
535           * Setting ping servers
536           *
537           * @access public
538           *
539           * @param  intger
540           *     blog ID
541           * @param  array
542           *     ping server settigs
543           *     target : name of ping server
544           *     host   : host of ping server
545           *     path   : path of ping server
546           *     port   : port of ping server
547           *     method : method of ping server accept
548           * @param  boolean
549           *     Send ping background or foreground
550           *
551           * @return void
552           **/
553         function sendUpdatePing($myBlogId, $sendPing, $background = false)
554         {
555                 global $manager, $DIR_LIBS;
556                 if (!class_exists('xmlrpcmsg')) {
557                         global $DIR_LIBS;
558                         include($DIR_LIBS . 'xmlrpc.inc.php');
559                 }
560                 if (!$background) {
561                         echo _PINGJP_PINGING . $parsed['target'] . ':<br />';
562                 } else {
563                         $logMsg = 'NP_PingJP: Sending ping (from background):' . $parsed['target'];
564                         ACTIONLOG::add(INFO, $logMsg);
565                 }
566                 $b    =& $manager->getBlog($myBlogId);
567                 $name =  $b->getName();
568                 $burl =  $this->getBlogOption($myBlogId, 'pingjp_updateurl');
569                 if (!$burl) {
570                         $burl = $b->getURL();
571                 }
572                 if (_CHARSET != 'ISO-8859-1' &&
573                         _CHARSET != 'US-ASCII' &&
574                         _CHARSET != 'UTF-8' &&
575                         function_exists('mb_convert_encoding')
576                 ) {
577                         mb_convert_encoding($name, 'UTF-8', _CHARSET);
578                 }
579                 $data = array(
580                         new xmlrpcval($name),
581                         new xmlrpcval($burl)
582                 );
583                 if ($sendPing['method'] == 'weblogUpdates.extendedPing') {
584                         $feedURL = $this->getBlogOption($myBlogid, 'pingjp_feedsurl');
585                         if (!$feedURL) {
586                                 if (substr($burl, -1) != '/') {
587                                         $base = $burl . '/';
588                                 } else {
589                                         $base = $burl;
590                                 }
591                                 $feedURL = $base . 'xml-rss2.php?blogid=' . $myBlogId;
592                         }
593                         $data[3] = new xmlrpcval($burl);
594                         $data[4] = new xmlrpcval($feedURL);
595                 }
596                 $message  = new xmlrpcmsg($sendPing['method'], $data);
597                 $connect  = new xmlrpc_client($sendPing['path'], $sendPing['host'], $sendPing['port']);
598                 $response = $connect->send($message, 30); // 30 seconds timeout...
599                 $results  = $this->processPingResult($response);
600                 if ($results['error']) {
601                         $logMsg = 'NP_PingJP Errror: ' . $results['message'];
602                         ACTIONLOG::add(WARNING, $logMsg);
603                 } elseif ($this->debug) {
604                         $logMsg = 'NP_PingJP: ' . $results['message'];
605                         ACTIONLOG::add(INFO, $logMsg);
606                 }
607                 if (!$background) {
608                         echo $results['message'] . '<br />';
609                 }
610         }
611
612         // }}}
613         // {{{ function processPingResult($response)
614
615         /**
616           * Pinging result
617           *
618           * @access public
619           *
620           * @param  object
621           *     weblog updates ping response
622           *
623           * @return array
624           *     error   : boolean
625           *               ping response status
626           *     message : string
627           *               ping response messages
628           **/
629         function processPingResult($response)
630         {
631                 global $php_errormsg;
632                 if (($response == 0) && ($response->errno || $response->errstring)) {
633                         $ret['error']   = true;
634                         $ret['message'] = _PINGJP_ERROR
635                                                         . ' ' . $response->errno
636                                                         . ' : ' . $response->errstring;
637                 } elseif (($response == 0) && ($php_errormsg)) {
638                         $ret['error']   = true;
639                         $ret['message'] = _PINGJP_PHP_ERROR . $php_errormsg;
640                 } elseif ($response == 0) {
641                         $ret['error']   = true;
642                         $ret['message'] = _PINGJP_PHP_PING_ERROR;
643                 } elseif ($response->faultCode() != 0) {
644                         $ret['error']   = true;
645                         $ret['message'] = _PINGJP_ERROR . ': ' . $response->faultString();
646                 } else {
647                         $response = $response->value(); // get response struct
648                         // get values
649                         $flerror = $response->structmem('flerror');
650                         $flerror = $flerror->scalarval();
651                         $message = $response->structmem('message');
652                         $message = $message->scalarval();
653                         if ($flerror != 0) {
654                                 $ret['error']   = true;
655                                 $ret['message'] = _PINGJP_ERROR . ' (flerror=1): ' . $message;
656                         } else {
657                                 $ret['error']   = false;
658                                 $ret['message'] = _PINGJP_SUCCESS . ' : ' . $message;
659                         }
660                 }
661                 return $ret;
662         }
663
664         // }}}
665
666
667 }