OSDN Git Service

MERGE: リビジョン1721をマージ。主要なクラス名をUpperCamelCaseに統一。
[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() { return 'Ping'; }
37         public function getAuthor() { return 'admun (Edmond Hui)'; }
38         public function getURL() { return 'http://edmondhui.homeip.net/nudn'; }
39         public function getVersion() { return '1.81'; }
40         public function getMinNucleusVersion() { return '330'; }
41         public function getDescription() { return _PING_DESC; }
42         public function supportsFeature($what) { return ( $what == 'SqlTablePrefix' ); }
43         public function getEventList(){ return array('JustPosted', 'PostAddItem', 'PostUpdateItem'); }
44         public function init()
45         {
46                 // include translation file for this plugin
47                 if ( file_exists($this->getDirectory() . i18n::get_current_locale() . '.' . i18n::get_current_charset() . '.php') )
48                 {
49                         include_once($this->getDirectory() . i18n::get_current_locale() . '.' . i18n::get_current_charset() . '.php');
50                 }
51                 else
52                 {
53                         include_once($this->getDirectory().'en_Latn_US.UTF-8.php');
54                 }
55                 return;
56         }
57         
58         public function install()
59         {
60                 // Default, http://pingomatic.com
61                 $this->createOption('pingpong_pingomatic', _PING_PINGOM, 'yesno', 'yes');
62                 // http://weblogs.com
63                 $this->createOption('pingpong_weblogs', _PING_WEBLOGS, 'yesno', 'no');
64                 // http://www.technorati.com
65                 $this->createOption('pingpong_technorati', _PING_TECHNOR, 'yesno', 'no');
66                 // http://www.blogrolling.com
67                 $this->createOption('pingpong_blogrolling', _PING_BLOGR, 'yesno', 'no');
68                 // http://blo.gs
69                 $this->createOption('pingpong_blogs', _PING_BLOGS, 'yesno', 'no');
70                 // http://weblogues.com/
71                 $this->createOption('pingpong_weblogues', _PING_WEBLOGUES, 'yesno', 'no');
72                 // http://blogg.de
73                 $this->createOption('pingpong_bloggde', _PING_BLOGGDE, 'yesno', 'no');
74                 // Pinging on background
75                 $this->createOption('ping_background', _PING_BG, 'yesno', 'no');
76                 
77                 /* plugin weblog option */
78                 $this->createBlogOption('ping_sendping', _PING_SENDPING, 'yesno', 'yes');
79                 return;
80         }
81         
82         public function event_JustPosted($data)
83         {
84                 global $DIR_PLUGINS, $DIR_NUCLEUS;
85                 
86                 // exit is another plugin already send ping
87                 if ( $data['pinged'] == true )
88                 {
89                         return;
90                 }
91                 
92                 $bid = intval($data['blogid']);
93                 if ($this->getBlogOption($bid, 'ping_sendping') == "yes")
94                 {
95                         if ( $this->getOption('ping_background') == "yes" )
96                         {
97                                 exec("php $DIR_PLUGINS/ping/ping.php " . $data['blogid'] . " &");
98                         }
99                         else
100                         {
101                                 $this->sendPings($data['blogid']);
102                         }
103                 }
104                 // mark the ping has been sent
105                 $data['pinged'] = true;
106                 return;
107         }
108         
109         public function event_PostAddItem($data)
110         {
111                 $this->_sendPingCheck($data['itemid']);
112                 return ;
113         }
114         
115         public function event_PostUpdateItem($data)
116         {
117                 $this->_sendPingCheck($data['itemid']);
118                 return;
119         }
120         
121         public function _sendPingCheck($itemid)
122         {
123                 $iid  = intval($itemid);
124                 global $manager;
125                 $item = $manager->getItem($iid,0,0);
126                 if ($item) {
127                         $bid = intval(getBlogIDFromItemID($iid));
128                         if ($this->getBlogOption($bid, 'ping_sendping') == "yes" ) {
129                                 $this->sendPings(array('blogid' => $bid));
130                         }
131                 }
132                 return;
133         }
134         
135         public function sendPings($data)
136         {
137                 if ( !class_exists('xmlrpcmsg') )
138                 {
139                         include_libs('xmlrpc.inc.php');
140                 }
141                 $this->myBlogId = $data['blogid'];
142                 
143                 $ping_result = '';
144                 
145                 if ( $this->getOption('pingpong_pingomatic') == 'yes' )
146                 {
147                         $ping_result .= _PINGING . "Ping-o-matic:\n";
148                         $ping_result .= $this->pingPingomatic();
149                         $ping_result .= " | ";
150                 }
151                 
152                 if ( $this->getOption('pingpong_weblogs') == 'yes' )
153                 {
154                         $ping_result .= _PINGING . "Weblogs.com:\n";
155                         $ping_result .= $this->pingWeblogs();
156                         $ping_result .= " | ";
157                 }
158                 
159                 if ( $this->getOption('pingpong_technorati') == 'yes' )
160                 {
161                         $ping_result .= _PINGING . "Technorati:\n";
162                         $ping_result .= $this->pingTechnorati();
163                         $ping_result .= " | ";
164                 }
165                 
166                 if ( $this->getOption('pingpong_blogrolling') == 'yes' )
167                 {
168                         $ping_result .= _PINGING . "Blogrolling.com:\n";
169                         $ping_result .= $this->pingBlogRollingDotCom();
170                         $ping_result .= " | ";
171                 }
172                 
173                 if ( $this->getOption('pingpong_blogs') == 'yes' )
174                 {
175                         $ping_result .= _PINGING . "Blog.gs:\n";
176                         $ping_result .= $this->pingBloGs();
177                         $ping_result .= " | ";
178                 }
179                 
180                 if ( $this->getOption('pingpong_weblogues') == 'yes' )
181                 {
182                         $ping_result .= _PINGING . "Weblogues.com:\n";
183                         $ping_result .= $this->pingWebloguesDotCom();
184                         $ping_result .= " | ";
185                 }
186                 
187                 if ( $this->getOption('pingpong_bloggde') == 'yes' )
188                 {
189                         $ping_result .= _PINGING . "Blog.de:\n";
190                         $ping_result .= $this->pingBloggDe();
191                         $ping_result .= " | ";
192                 }
193                 
194                 ActionLog::add(INFO, $ping_result);
195                 return;
196         }
197         
198         public function pingPingomatic()
199         {
200                 $b = new Blog($this->myBlogId);
201                 $message = new xmlrpcmsg(
202                         'weblogUpdates.ping',
203                         array(
204                                 new xmlrpcval($b->getName(), 'string'),
205                                 new xmlrpcval($b->getURL(), 'string')
206                         )
207                 );
208                 
209                 $c = new xmlrpc_client('/', 'rpc.pingomatic.com', 80);
210                 /* 30 seconds timeout... */
211                 $r = $c->send($message,30);
212                 return $this->processPingResult($r);
213         }
214         
215         public function pingWeblogs()
216         {
217                 $b = new Blog($this->myBlogId);
218                 $message = new xmlrpcmsg(
219                         'weblogupdates.ping',
220                         array(
221                                 new xmlrpcval($b->getName(), 'string'),
222                                 new xmlrpcval($b->getURL(), 'string')
223                         )
224                 );
225                 
226                 $c = new xmlrpc_client('/rpc2', 'rpc.weblogs.com', 80);
227                 /* 30 seconds timeout... */
228                 $r = $c->send($message,30);
229                 return $this->processPingResult($r);
230         }
231         
232         public function pingTechnorati()
233         {
234                 $b = new Blog($this->myBlogId);
235                 $message = new xmlrpcmsg(
236                         'weblogUpdates.ping',
237                         array(
238                                 new xmlrpcval($b->getName(),'string'),
239                                 new xmlrpcval($b->getURL(),'string')
240                         )
241                 );
242                 
243                 $c = new xmlrpc_client('/rpc/ping/', 'rpc.technorati.com', 80);
244                 /* 30 seconds timeout... */
245                 $r = $c->send($message,30);
246                 return $this->processPingResult($r);
247         }
248         
249         public function pingBlogRollingDotCom()
250         {
251                 $b = new Blog($this->myBlogId);
252                 $message = new xmlrpcmsg(
253                         'weblogUpdates.ping',
254                         array(
255                                 new xmlrpcval($b->getName(),'string'),
256                                 new xmlrpcval($b->getURL(),'string')
257                         )
258                 );
259                 
260                 $c = new xmlrpc_client('/pinger/', 'rpc.blogrolling.com', 80);
261                 /* 30 seconds timeout... */
262                 $r = $c->send($message,30);
263                 return $this->processPingResult($r);
264         }
265         
266         public function pingBloGs()
267         {
268                 $b = new Blog($this->myBlogId);
269                 $message = new xmlrpcmsg(
270                         'weblogUpdates.extendedPing',
271                         array(
272                                 new xmlrpcval($b->getName(),'string'),
273                                 new xmlrpcval($b->getURL(),'string')
274                         )
275                 );
276                 
277                 $c = new xmlrpc_client('/', 'ping.blo.gs', 80);
278                 /* 30 seconds timeout... */
279                 $r = $c->send($message,30);
280                 return $this->processPingResult($r);
281         }
282         
283         public function pingWebloguesDotCom()
284         {
285                 $b = new Blog($this->myBlogId);
286                 $message = new xmlrpcmsg(
287                         'weblogUpdates.extendedPing',
288                         array(
289                                 new xmlrpcval($b->getName(),'string'),
290                                 new xmlrpcval($b->getURL(),'string')
291                         )
292                 );
293                 
294                 $c = new xmlrpc_client('/RPC/', 'www.weblogues.com', 80);
295                 /* 30 seconds timeout... */
296                 $r = $c->send($message,30);
297                 return $this->processPingResult($r);
298         }
299         
300         public function pingBloggDe()
301         {
302                 $b = new Blog($this->myBlogId);
303                 $message = new xmlrpcmsg(
304                         'bloggUpdates.ping',
305                         array(
306                                 new xmlrpcval($b->getName(),'string'),
307                                 new xmlrpcval($b->getURL(),'string')
308                         )
309                 );
310                 
311                 $c = new xmlrpc_client('/', 'xmlrpc.blogg.de', 80);
312                 /* 30 seconds timeout... */
313                 $r = $c->send($message,30);
314                 return $this->processPingResult($r);
315         } 
316         
317         public function processPingResult($r)
318         {
319                 global $php_errormsg;
320                 
321                 if ( ($r == 0) && ($r->errno || $r->errstring) )
322                 {
323                         return _PING_ERROR . " " . $r->errno . ' : ' . $r->errstring;
324                 }
325                 elseif ( ($r == 0) && ($php_errormsg) )
326                 {
327                         return _PING_PHP_ERROR . $php_errormsg;
328                 }
329                 elseif ( $r == 0 )
330                 {
331                         return _PING_PHP_PING_ERROR;
332                 }
333                 elseif ( $r->faultCode() != 0 )
334                 {
335                         return _PING_ERROR . ': ' . $r->faultString();
336                 }
337                 else
338                 {
339                         // get response struct
340                         $r = $r->value();
341                         
342                         // get values
343                         $flerror = $r->structmem('flerror');
344                         $flerror = $flerror->scalarval();
345                         
346                         $message = $r->structmem('message');
347                         $message = $message->scalarval();
348                         
349                         if ( $flerror != 0 )
350                         {
351                                 return _PING_ERROR . ' (flerror=1): ' . $message;
352                         }
353                         else
354                         {
355                                 return _PING_SUCCESS . ': ' . $message;
356                         }
357                 }
358         }
359 }