OSDN Git Service

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