OSDN Git Service

d097cc3c4bad85792cf23454f0d5899bfea5e44b
[nucleus-jp/nucleus-next.git] / nucleus / plugins / NP_Ping.php
1 <?php
2 /**
3  *
4  * Send weblog updates ping
5  *       plugin for NucleusCMS(version 3.30 or lator)
6  *       Note: based on NP_PingPong, adapt for the new ping mechanism
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      admun (Edmond Hui)
16  * @license   http://www.gnu.org/licenses/gpl.txt  GNU GENERAL PUBLIC LICENSE Version 2, June 1991
17  * @version   1.8
18  * @link          http://edmondhui.homeip.net/nudn
19  * $id$
20  * History
21  *   v1.0 - Initial version
22  *   v1.1 - Add JustPosted event support
23  *   v1.2 - JustPosted event handling in background
24  *   v1.3 - pinged variable support
25  *   v1.4 - language file support
26  *   v1.5 - remove arg1 in exec() call
27  *   v1.6 - move send update ping override option to plugin
28  *   v1.7 - move send ping option from blog to plugin/blog level
29  *              - remove ping override option
30  *   v1.8 - remove sendPing event handle, switch to use PostAddItem and PostUpdateItem event for new item ping
31  *   v1.81 - fix bug in _sendPingCheck() where ITEM class not found when creating new weblog
32  */
33
34 class NP_Ping extends NucleusPlugin
35 {
36         public function getName()
37         {
38                 return 'Ping';
39         }
40
41         public function getAuthor()
42         {
43                 return 'admun (Edmond Hui)';
44         }
45
46         public function getURL()
47         {
48                 return 'http://edmondhui.homeip.net/nudn';
49         }
50
51         public function getVersion()
52         {
53                 return '1.81';
54         }
55
56         public function getMinNucleusVersion()
57         {
58                 return '330';
59         }
60
61         public function getDescription()
62         {
63                 return _PING_DESC;
64         }
65
66         public function supportsFeature($what)
67         {
68                 return ( $what == 'SqlTablePrefix' );
69         }
70
71         public function getEventList()
72         {
73                 return array('JustPosted', 'PostAddItem', 'PostUpdateItem');
74         }
75
76         public function init()
77         {
78                 // include translation file for this plugin
79                 if ( file_exists($this->getDirectory() . i18n::get_current_locale() . '.' . i18n::get_current_charset() . '.php') )
80                 {
81                         include_once($this->getDirectory() . i18n::get_current_locale() . '.' . i18n::get_current_charset() . '.php');
82                 }
83                 else
84                 {
85                         include_once($this->getDirectory().'en_Latn_US.UTF-8.php');
86                 }
87                 return;
88         }
89         
90         public function install()
91         {
92                 // Default, http://pingomatic.com
93                 $this->createOption('pingpong_pingomatic',      '_PING_PINGOM',         'yesno', 'yes');
94                 // http://weblogs.com
95                 $this->createOption('pingpong_weblogs',         '_PING_WEBLOGS',        'yesno', 'no');
96                 // http://www.technorati.com
97                 $this->createOption('pingpong_technorati',      '_PING_TECHNOR',        'yesno', 'no');
98                 // http://www.blogrolling.com
99                 $this->createOption('pingpong_blogrolling',     '_PING_BLOGR',          'yesno', 'no');
100                 // http://blo.gs
101                 $this->createOption('pingpong_blogs',           '_PING_BLOGS',          'yesno', 'no');
102                 // http://weblogues.com/
103                 $this->createOption('pingpong_weblogues',       '_PING_WEBLOGUES',      'yesno', 'no');
104                 // http://blogg.de
105                 $this->createOption('pingpong_bloggde',         '_PING_BLOGGDE',        'yesno', 'no');
106                 // Pinging on background
107                 $this->createOption('ping_background',          '_PING_BG',                     'yesno', 'no');
108                 
109                 // plugin weblog option
110                 $this->createBlogOption('ping_sendping',        '_PING_SENDPING',       'yesno', 'yes');
111                 return;
112         }
113         
114         public function event_JustPosted($data)
115         {
116                 global $DIR_PLUGINS, $DIR_NUCLEUS;
117                 
118                 // exit is another plugin already send ping
119                 if ( $data['pinged'] == true )
120                 {
121                         return;
122                 }
123                 
124                 $bid = intval($data['blogid']);
125                 if ( $this->getBlogOption($bid, 'ping_sendping') == "yes" )
126                 {
127                         if ( $this->getOption('ping_background') == "yes" )
128                         {
129                                 exec("php $DIR_PLUGINS/ping/ping.php " . $data['blogid'] . " &");
130                         }
131                         else
132                         {
133                                 $this->sendPings($data['blogid']);
134                         }
135                 }
136                 // mark the ping has been sent
137                 $data['pinged'] = true;
138                 return;
139         }
140         
141         public function event_PostAddItem($data)
142         {
143                 $this->_sendPingCheck($data['itemid']);
144                 return ;
145         }
146         
147         public function event_PostUpdateItem($data)
148         {
149                 $this->_sendPingCheck($data['itemid']);
150                 return;
151         }
152         
153         public function _sendPingCheck($itemid)
154         {
155                 $iid  = intval($itemid);
156                 global $manager;
157                 $item = $manager->getItem($iid,0,0);
158                 if ( $item )
159                 {
160                         $bid = intval(getBlogIDFromItemID($iid));
161                         if ( $this->getBlogOption($bid, 'ping_sendping') == "yes" )
162                         {
163                                 $this->sendPings(array('blogid' => $bid));
164                         }
165                 }
166                 return;
167         }
168         
169         public function sendPings($data)
170         {
171                 if ( !class_exists('xmlrpcmsg') )
172                 {
173                         include_libs('xmlrpc.inc.php');
174                 }
175                 $this->myBlogId = $data['blogid'];
176                 
177                 $ping_result = '';
178                 
179                 if ( $this->getOption('pingpong_pingomatic') == 'yes' )
180                 {
181                         $ping_result .= _PINGING . "Ping-o-matic:\n";
182                         $ping_result .= $this->pingPingomatic();
183                         $ping_result .= " | ";
184                 }
185                 
186                 if ( $this->getOption('pingpong_weblogs') == 'yes' )
187                 {
188                         $ping_result .= _PINGING . "Weblogs.com:\n";
189                         $ping_result .= $this->pingWeblogs();
190                         $ping_result .= " | ";
191                 }
192                 
193                 if ( $this->getOption('pingpong_technorati') == 'yes' )
194                 {
195                         $ping_result .= _PINGING . "Technorati:\n";
196                         $ping_result .= $this->pingTechnorati();
197                         $ping_result .= " | ";
198                 }
199                 
200                 if ( $this->getOption('pingpong_blogrolling') == 'yes' )
201                 {
202                         $ping_result .= _PINGING . "Blogrolling.com:\n";
203                         $ping_result .= $this->pingBlogRollingDotCom();
204                         $ping_result .= " | ";
205                 }
206                 
207                 if ( $this->getOption('pingpong_blogs') == 'yes' )
208                 {
209                         $ping_result .= _PINGING . "Blog.gs:\n";
210                         $ping_result .= $this->pingBloGs();
211                         $ping_result .= " | ";
212                 }
213                 
214                 if ( $this->getOption('pingpong_weblogues') == 'yes' )
215                 {
216                         $ping_result .= _PINGING . "Weblogues.com:\n";
217                         $ping_result .= $this->pingWebloguesDotCom();
218                         $ping_result .= " | ";
219                 }
220                 
221                 if ( $this->getOption('pingpong_bloggde') == 'yes' )
222                 {
223                         $ping_result .= _PINGING . "Blog.de:\n";
224                         $ping_result .= $this->pingBloggDe();
225                         $ping_result .= " | ";
226                 }
227                 
228                 ActionLog::add(INFO, $ping_result);
229                 return;
230         }
231         
232         public function pingPingomatic()
233         {
234                 global $manager;
235                 
236                 $b =& $manager->getBlog($this->myBlogId);
237                 $message = new xmlrpcmsg(
238                         'weblogUpdates.ping',
239                         array(
240                                 new xmlrpcval($b->getName(), 'string'),
241                                 new xmlrpcval($b->getURL(), 'string')
242                         )
243                 );
244                 
245                 $c = new xmlrpc_client('/', 'rpc.pingomatic.com', 80);
246                 /* 30 seconds timeout... */
247                 $r = $c->send($message,30);
248                 return $this->processPingResult($r);
249         }
250         
251         public function pingWeblogs()
252         {
253                 global $manager;
254                 
255                 $b =& $manager->getBlog($this->myBlogId);
256                 $message = new xmlrpcmsg(
257                         'weblogupdates.ping',
258                         array(
259                                 new xmlrpcval($b->getName(), 'string'),
260                                 new xmlrpcval($b->getURL(), 'string')
261                         )
262                 );
263                 
264                 $c = new xmlrpc_client('/rpc2', 'rpc.weblogs.com', 80);
265                 /* 30 seconds timeout... */
266                 $r = $c->send($message,30);
267                 return $this->processPingResult($r);
268         }
269         
270         public function pingTechnorati()
271         {
272                 global $manager;
273                 
274                 $b =& $manager->getBlog($this->myBlogId);
275                 $message = new xmlrpcmsg(
276                         'weblogUpdates.ping',
277                         array(
278                                 new xmlrpcval($b->getName(),'string'),
279                                 new xmlrpcval($b->getURL(),'string')
280                         )
281                 );
282                 
283                 $c = new xmlrpc_client('/rpc/ping/', 'rpc.technorati.com', 80);
284                 /* 30 seconds timeout... */
285                 $r = $c->send($message,30);
286                 return $this->processPingResult($r);
287         }
288         
289         public function pingBlogRollingDotCom()
290         {
291                 global $manager;
292                 
293                 $b =& $manager->getBlog($this->myBlogId);
294                 $message = new xmlrpcmsg(
295                         'weblogUpdates.ping',
296                         array(
297                                 new xmlrpcval($b->getName(),'string'),
298                                 new xmlrpcval($b->getURL(),'string')
299                         )
300                 );
301                 
302                 $c = new xmlrpc_client('/pinger/', 'rpc.blogrolling.com', 80);
303                 /* 30 seconds timeout... */
304                 $r = $c->send($message,30);
305                 return $this->processPingResult($r);
306         }
307         
308         public function pingBloGs()
309         {
310                 global $manager;
311                 
312                 $b =& $manager->getBlog($this->myBlogId);
313                 $message = new xmlrpcmsg(
314                         'weblogUpdates.extendedPing',
315                         array(
316                                 new xmlrpcval($b->getName(),'string'),
317                                 new xmlrpcval($b->getURL(),'string')
318                         )
319                 );
320                 
321                 $c = new xmlrpc_client('/', 'ping.blo.gs', 80);
322                 /* 30 seconds timeout... */
323                 $r = $c->send($message,30);
324                 return $this->processPingResult($r);
325         }
326         
327         public function pingWebloguesDotCom()
328         {
329                 global $manager;
330                 
331                 $b =& $manager->getBlog($this->myBlogId);
332                 $message = new xmlrpcmsg(
333                         'weblogUpdates.extendedPing',
334                         array(
335                                 new xmlrpcval($b->getName(),'string'),
336                                 new xmlrpcval($b->getURL(),'string')
337                         )
338                 );
339                 
340                 $c = new xmlrpc_client('/RPC/', 'www.weblogues.com', 80);
341                 /* 30 seconds timeout... */
342                 $r = $c->send($message,30);
343                 return $this->processPingResult($r);
344         }
345         
346         public function pingBloggDe()
347         {
348                 global $manager;
349                 
350                 $b =& $manager->getBlog($this->myBlogId);
351                 $message = new xmlrpcmsg(
352                         'bloggUpdates.ping',
353                         array(
354                                 new xmlrpcval($b->getName(),'string'),
355                                 new xmlrpcval($b->getURL(),'string')
356                         )
357                 );
358                 
359                 $c = new xmlrpc_client('/', 'xmlrpc.blogg.de', 80);
360                 /* 30 seconds timeout... */
361                 $r = $c->send($message,30);
362                 return $this->processPingResult($r);
363         } 
364         
365         public function processPingResult($r)
366         {
367                 global $php_errormsg;
368                 
369                 if ( ($r == 0) && ($r->errno || $r->errstring) )
370                 {
371                         return _PING_ERROR . " " . $r->errno . ' : ' . $r->errstring;
372                 }
373                 elseif ( ($r == 0) && ($php_errormsg) )
374                 {
375                         return _PING_PHP_ERROR . $php_errormsg;
376                 }
377                 elseif ( $r == 0 )
378                 {
379                         return _PING_PHP_PING_ERROR;
380                 }
381                 elseif ( $r->faultCode() != 0 )
382                 {
383                         return _PING_ERROR . ': ' . $r->faultString();
384                 }
385                 else
386                 {
387                         // get response struct
388                         $r = $r->value();
389                         
390                         // get values
391                         $flerror = $r->structmem('flerror');
392                         $flerror = $flerror->scalarval();
393                         
394                         $message = $r->structmem('message');
395                         $message = $message->scalarval();
396                         
397                         if ( $flerror != 0 )
398                         {
399                                 return _PING_ERROR . ' (flerror=1): ' . $message;
400                         }
401                         else
402                         {
403                                 return _PING_SUCCESS . ': ' . $message;
404                         }
405                 }
406         }
407 }