OSDN Git Service

git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/plugin@881 1ca29b6e-896d...
[nucleus-jp/nucleus-plugins.git] / NP_UpdatePingServer / trunk / NP_UpdatePingServer.php
1 <?php
2
3 global $DIR_PLUGINS, $DIR_LIBS;
4 if (version_compare(phpversion(), "5.0.1", ">=")) {
5         require_once($DIR_PLUGINS . "updatepingserver/xmlrpc.inc");
6         require_once($DIR_PLUGINS . "updatepingserver/xmlrpcs.inc");
7 } else {
8         include_once($DIR_LIBS . "xmlrpc.inc.php");
9         include_once($DIR_LIBS . "xmlrpcs.inc.php");
10 }
11
12 class NP_UpdatePingServer extends NucleusPlugin
13 {
14
15 var $moduleAdmin;
16
17         function getName()
18         {
19                 return 'XML-RPC Updateping Server';
20         }
21
22         function getAuthor()
23         {
24                 return 'shizuki';
25         }
26
27         function getURL()
28         {
29                 return 'http://shizuki.kinezumi.net';
30         }
31
32         function getVersion()
33         {
34                 return '1.10';
35         }
36
37         function getDescription()
38         {
39                 return _NP_PINGSERVER_DESCRIPTION;
40         }
41
42         function getEventList()
43         {
44                  return array(
45                                           'QuickMenu'
46                                          );
47         }
48
49         function install()
50         {
51                 $this->createOption('desclen',     _NP_PINGSERVER_GLOBALOPTION_DESCLEN,     'text',
52                                                                                    '250');
53                 $this->createOption('addstr',      _NP_PINGSERVER_GLOBALOPTION_ADDSTR,      'text',
54                                                                                    '...');
55                 $this->createOption('dateformat',  _NP_PINGSERVER_GLOBALOPTION_DATEFORMAT,  'text',
56                                                                                    '%Y-%m-%d %H:%M:%S');
57                 $this->createOption('listHeader',  _NP_PINGSERVER_GLOBALOPTION_LISTHEADER,  'text',
58                                                                                    _NP_PINGSERVER_GLOBALOPTION_LISTHEADER_VALUE);
59                 $this->createOption('listFooter',  _NP_PINGSERVER_GLOBALOPTION_LISTFOOTER,  'text',
60                                                                                    _NP_PINGSERVER_GLOBALOPTION_LISTFOOTER_VALUE);
61                 $this->createOption('listBody',    _NP_PINGSERVER_GLOBALOPTION_LISTBODY,    'textarea',
62                                                                                    _NP_PINGSERVER_GLOBALOPTION_LISTBODY_VALUE);
63                 $this->createOption('logFlag',     _NP_PINGSERVER_GLOBALOPTION_LOGFLAG,     'yesno',
64                                                                                    'yes');
65                 $this->createOption('dataMaxHold', _NP_PINGSERVER_GLOBALOPTION_DATAMAXHOLD, 'text',
66                                                                                    '1000');
67                 $this->createOption('quickmenu',   _NP_PINGSERVER_GLOBALOPTION_QUICKMENU,   'yesno',
68                                                                                    'yes');
69                 $this->createOption('dbFlag',      _NP_PINGSERVER_GLOBALOPTION_DBFLAG,      'yesno',
70                                                                                    'no');
71 //              $this->createOption('reserved',    _NP_PINGSERVER_GLOBALOPTION_RESERVED,    'text',
72 //                                                                                 _NP_PINGSERVER_GLOBALOPTION_RESERVED_VALUE);
73 //              $this->createOption('reserved',    _NP_PINGSERVER_GLOBALOPTION_RESERVED,    'text',
74 //                                                                                 _NP_PINGSERVER_GLOBALOPTION_RESERVED_VALUE);
75                 $table_q = 'CREATE TABLE IF NOT EXISTS ' . sql_table('plug_updatepingserver_sitedata') . ' ('
76                                  . '    pingid     INT(11)       NOT NULL AUTO_INCREMENT, '
77                                  . '    updatetime INT(12)       NOT NULL, '
78                                  . '    btitle     VARCHAR(255)  NOT NULL, '
79                                  . '    burl       VARCHAR(255)  NOT NULL, '
80                                  . '    feedurl    VARCHAR(255)  NOT NULL, '
81                                  . ' PRIMARY KEY        (pingid) '
82                                  . ' )';
83                 sql_query($table_q);
84                 $table_q = 'CREATE TABLE IF NOT EXISTS ' . sql_table('plug_updatepingserver_changes') . ' ('
85                                  . '    id          INT(11)    NOT NULL AUTO_INCREMENT, '
86                                  . '    updatetime  TIMESTAMP  NOT NULL, '
87                                  . '    changestext VARCHAR(7) NOT NULL, '
88                                  . ' UNIQUE  KEY changestext (changestext), '
89                                  . ' PRIMARY KEY             (id) '
90                                  . ' )';
91                 sql_query($table_q);
92                 $table_q = 'CREATE TABLE IF NOT EXISTS ' . sql_table('plug_updatepingserver_modules') . ' ('
93                                  . '    moduleid    INT(11)       NOT NULL AUTO_INCREMENT, '
94                                  . '    modulename  VARCHAR(255)  NOT NULL, '
95                                  . '    moduleorder INT(11)       NOT NULL, '
96                                  . ' UNIQUE  KEY modulename (modulename), '
97                                  . ' PRIMARY KEY            (moduleid) '
98                                  . ' )';
99                 sql_query($table_q);
100                 $this->moduleAdmin =& new moduleAdmin('UpdatePingServer');
101                 $moduleNames = array();
102                 $handle = @fopen($this->getDirectory() . "preinstallmodules.txt", "r");
103                 if ($handle) {
104                         while (!feof($handle)) {
105                                 $moduleNames[] = fgets($handle);
106                         }
107                         fclose($handle);
108                 }
109                 if (!empty($moduleNames)) {
110                         foreach ($moduleNames as $moduleName) {
111                                 if (!empty($moduleName)) {
112                                         $this->moduleAdmin->_moduleInstall($moduleName);
113                                 }
114                         }
115                 }
116         }
117
118         function uninstall()
119         {
120                 if ($this->getOption('dbFlag') == 'yes') {
121                         $tables = $this->getTableList();
122                         foreach ($tables as $table) {
123                                 sql_query('DROP TABLE IF EXISTS ' . $table);
124                         }
125                 }
126         }
127
128         function supportsFeature($what)
129         {
130                 switch ($what) {
131                         case 'SqlTablePrefix':
132                                 return 1;
133                         default:
134                                 return 0;
135                 }
136         }
137
138         function event_QuickMenu(&$data)
139         {
140                 // only show when option enabled
141                 if ($this->getOption('quickmenu') != 'yes') return;
142                 
143                 global $member;
144                 // only show to admins
145                 if (!($member->isLoggedIn() && $member->isAdmin())) return;
146                 
147                 array_push(
148                         $data['options'],
149                         array(
150                                 'title'   => _NP_PINGSERVER_QUICKMENU_TITLE,
151                                 'url'     => $this->getAdminURL(),
152                                 'tooltip' => _NP_PINGSERVER_QUICKMENU_TOOLTIP
153                         )
154                 );
155         }
156
157         function getTableList()
158         { 
159                 $tables = array(
160                                                 sql_table('plug_updatepingserver_modules'),
161                                                 sql_table('plug_updatepingserver_changes'),
162                                                 sql_table('plug_updatepingserver_sitedata')
163                                            );
164                 if (!is_array($this->moduleAdmin->moduleList)) {
165                         $this->moduleAdmin->getModuleList();
166                 }
167                 $listEneries = $this->moduleAdmin->moduleList;
168                 if (is_array($listEneries)) {
169                         foreach($listEneries as $listEnery) {
170                                 $this->moduleAdmin->_loadModule($listEnery);
171                                 if (method_exists($this->moduleAdmin->modules[$listEnery], 'getModuleTableList')) {
172                                         $moduleTables = $this->moduleAdmin->modules[$listEnery]->getModuleTableList();
173                                 }
174                                 $tables = array_merge($tables, $moduleTables);
175                         }
176                 }
177                 return $tables;
178         }
179
180         function hasAdminArea()
181         {
182                 return 1;
183         }
184
185         function init()
186         {
187                 global $CONF;
188                 $langfile = $this->getDirectory() . 'language/' . $CONF['Language'] . '.php';
189                 if (!file_exists($langfile)) {
190                         $langfile = $this->getDirectory() . 'language/english.php';
191                 }
192                 include_once($langfile);
193                 require_once($this->getDirectory() . 'modules/moduleAdmin.php');
194                 include_once($this->getDirectory() . 'sharedFunctions.php');
195                 if (@mysql_query('SELECT * FROM ' . sql_table('plug_updatepingserver_modules'))) {
196                         $this->moduleAdmin =& new moduleAdmin('UpdatePingServer');
197                 }
198         }
199
200         function updatepingEventNotify($eventName, $data)
201         {
202                 if (!is_array($this->moduleAdmin->moduleList)) {
203                         $this->moduleAdmin->getModuleList();
204                 }
205                 $listEneries = $this->moduleAdmin->moduleList;
206                 if (is_array($listEneries)) {
207                         foreach($listEneries as $listEnery) {
208                                 $this->moduleAdmin->_loadModule($listEnery);
209                                 $eventMethod = 'np_updatePingServerEvent_' . $eventName;
210                                 if (method_exists($this->moduleAdmin->modules[$listEnery], $eventMethod)) {
211                                         $data['myPlugin'] = $this;
212                                         call_user_func(array(&$this->moduleAdmin->modules[$listEnery], $eventMethod), $data);
213                                 }
214                         }
215                 }
216         }
217
218         function weblogUpdates($msg)
219         {
220
221                 for ($i = 0; $i < $msg->getNumParams(); $i++) {
222                         if ($i == 0) {
223                                 $title = $msg->getParam($i);
224                                 $title = $title->scalarval();
225                         } else if ($i == 1) {
226                                 $url = $msg->getParam($i);
227                                 $url = $url->scalarval();
228                         } else if ($i == 3) {
229                                 $furl = $msg->getParam($i);
230                                 $furl = $furl->scalarval();
231                                 if (!preg_match("/^(http)(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)$/", $furl)) {
232                                         $furl = '';
233                                 }
234                         }
235                 }
236
237                 $response   = TRUE;
238                 $message    = '';
239                 $siteData   = array(
240                                                         'siteTitle'  => $title,
241                                                         'siteURL'    => $url,
242                                                         'feedURL'    => $furl,
243                                                    );
244                 $moduleData = array(
245                                                         'siteTitle' => &$title,
246                                                         'response'  => &$response,
247                                                         'message'   => &$message,
248                                                         'siteData'  => &$siteData,
249                                                    );
250                 $this->updatepingEventNotify('receivePing', $moduleData);
251
252                 $GLOBALS['xmlrpc_defencoding'] = 'UTF-8';
253                 if ($response) {
254                         $this->updateTable($siteData);
255                         if ($this->getOption('logFlag') == 'yes') {
256                                 addToLog(INFO, 'Updateping Server Received ping. from:' . $title);
257                         }
258                         $flerror = new xmlrpcval(0, 'boolean');
259                         if (!$message) {
260                                 $message = new xmlrpcval('Thanks for your update ping.');
261                         } else {
262                                 $message = new xmlrpcval($message);
263                         }
264                         $values = array(
265                                                          'flerror' => $flerror,
266                                                          'message' => $message
267                                                         );
268                     $value  = new xmlrpcval($values, 'struct');
269                     return new xmlrpcresp($value);
270                 } else {
271                         $eCode = $GLOBALS['xmlrpcerruser'] + 21;
272                         if (!$message) {
273                                 $message = 'Your updateping can not received.';
274                         }
275                         return new xmlrpcresp(0, $eCode, $message);
276                 }
277         }
278
279         function changesOutput()
280         {
281                 $last  = intGetVar('last');
282                 $upd   = date('r');
283                 if ($last > 300) {
284                         $last = 300;
285                 }
286                 $query = 'SELECT '
287                            . '               updatetime, '
288                            . '               btitle, '
289                            . '               burl, '
290                            . '               feedurl '
291                            . ' FROM '
292                            .      sql_table('plug_updatepingserver_sitedata');
293                 if ($last > 0) {
294                         $query .= ' WHERE '
295                                         . '               updatetime > ' . (strtotime($upd) - $last);
296                 }
297                 $query .= ' ORDER BY updatetime DESC';
298
299                 $eventData = array (
300                                                         'nowTime' => $upd,
301                                                         'last'    => $last,
302                                                         'query'   => &$query
303                                                    );
304                 $this->updatepingEventNotify('changesOutput', $eventData);
305
306                 $res = sql_query($query);
307                 if (mysql_num_rows($res)) {
308                         $out = '';
309                         while ($list = mysql_fetch_assoc($res)) {
310                                 $when = (strtotime($upd) - $list['updatetime']);
311                                 $list = array_map($sharedFunctions->_hsc, $list);
312                                 $out .= '    '
313                                           . '<weblog'
314                                           . ' name="'   . $list['btitle']  . '"'
315                                           . ' url="'    . $list['burl']    . '"'
316                                           . ' rssUrl="' . $list['feedurl'] . '"'
317                                           . ' when="'   . $when            . '"'
318                                           . ' />' . "\n";
319                         }
320                         $que = 'SELECT '
321                                  . '      id as result '
322                                  . 'FROM '
323                                  .        sql_table('plug_updatepingserver_changes');
324                         $ver = quickQuery($que);
325                         $ver += 1;
326                         $out = '<' . '?xml version="1.0" encoding="UTF-8"?' . '>' . "\n"
327                                  . '<weblogUpdates version="2" updated="' . $upd . '" count="' . $ver . '">' . "\n"
328                                  . $out
329                                  . '</weblogUpdates>';
330                         header("Content-Type: text/xml");
331                         echo $out;
332                         $query = 'REPLACE ' . sql_table('plug_updatepingserver_changes')
333                                    . ' SET '
334                                    . '     changestext = "changes"';
335                         sql_query($query);
336                 } else {
337                         echo 'No Data.';
338                 }
339
340         }
341
342         function updateTable($data)
343         {
344                 $sharedFunctions = new sharedFunctions();
345
346                 $query = 'REPLACE '
347                            .        sql_table('plug_updatepingserver_sitedata')
348                            . ' ('
349                            . '      updatetime, '
350                            . '      btitle, '
351                            . '      burl, '
352                            . '      feedurl '
353                            . ' ) '
354                            . 'VALUES '
355                            . ' ('
356                            .        date('U')                                        . ', '
357                            .        $sharedFunctions->quoteSmart($data['siteTitle']) . ', '
358                            .        $sharedFunctions->quoteSmart($data['siteURL'])   . ', '
359                            .        $sharedFunctions->quoteSmart($data['feedURL'])
360                            . ' )';
361                 sql_query($query);
362                 $query = 'SELECT '
363                            . '      COUNT(pingid) as result '
364                            . 'FROM '
365                            .        sql_table('plug_updatepingserver_sitedata');
366                 if (quickQuery($query) > $this->getOption('dataMaxHold')) {
367                         $query = 'DELETE FROM '
368                                    .        sql_table('plug_updatepingserver_sitedata')
369                                    . ' ORDER BY '
370                                    . '      pingid ASC '
371                                    . ' LIMIT 1'; 
372                         sql_query($query);
373                 }
374         }
375
376         function doSkinVar($skinType, $amount = 5)
377         {
378                 if (empty($amount)) $amount = 5;
379                 echo $this->getOption('listHeader');
380                 $query = 'SELECT '
381                            . '               updatetime  as tstamp, '
382                            . '               btitle      as blogtitle, '
383                            . '               burl        as blogurl, '
384                            . '               etitle      as entrytitle, '
385                            . '               eurl        as entryurl, '
386                            . '               edesc       as entrydesc '
387                            . 'FROM '
388                            .        sql_table('plug_updatepingserver_feeddata')
389                            . ' ORDER BY '
390                            . '      updatetime DESC'
391                            . ' LIMIT '
392                            .        intval($amount);
393                 $res   = sql_query($query);
394                 $len   = $this->getOption('desclen');
395                 $add   = $this->getOption('addstr');
396                 while ($values = mysql_fetch_assoc($res)) {
397                         $values['formatdate'] = strftime($this->getOption('dateformat'), $values['tstamp']);
398                         $values               = array_map(array(&$this, '_hsc'), $values);
399                         $values['entrydesc']  = shorten($values['entrydesc'], $len, $add);
400                         echo TEMPLATE::fill($this->getOption('listBody'), $values);
401                 }
402                 echo $this->getOption('listFooter');
403         }
404
405         function doTemplateVar(&$item, $amount = 5)
406         {
407                 if (empty($amount)) $amount = 5;
408                 $this->doSkinVar('template', $amount);
409         }
410
411         function doItemVar($item, $amount = 5)
412         {
413                 if (empty($amount)) $amount = 5;
414                 $this->doSkinVar('item', $amount);
415         }
416
417         function doAction($type)
418         {
419                 switch ($type) {
420                         case 'updateping':
421 /*
422 global $HTTP_RAW_POST_DATA;
423 $HTTP_RAW_POST_DATA = '<' . '?xml version="1.0"?' . '>
424 <methodCall>
425   <methodName>weblogUpdates.ping</methodName>
426   <params>
427     <param>
428       <value>YOUR WEBLOG NAME HERE</value>
429     </param>
430     <param>
431       <value>http://shizuki.kinezumi.net</value>
432     </param>
433   </params>
434 </methodCall>';
435 //*/
436                                 $parser = xml_parser_create();
437                                 xml_parse_into_struct($parser, $GLOBALS['HTTP_RAW_POST_DATA'], $vals, $index);
438                                 xml_parser_free($parser);
439                                 $method = $vals[$index['METHODNAME'][0]]['value'];
440                                 $GLOBALS['xmlrpc_internalencoding'] = _CHARSET;
441                                 $GLOBALS['xmlrpc_defencoding']      = 'UTF-8';
442
443                                 $fnc = array(
444                                                          &$this,
445                                                          'weblogUpdates'
446                                                         );
447                                 $png = array(
448                                                          'function' => $fnc
449                                                         );
450                                 $map = array(
451                                                          $method => $png
452                                                         );
453
454                                 if (!version_compare(phpversion(), "5.0.0", "=<")) {// && !is_array($GLOBALS['xmlrpc_valid_parents'])) {
455                                         $GLOBALS['xmlrpc_valid_parents'] = array(
456                                                         'BOOLEAN'          => array('VALUE'),
457                                                         'I4'               => array('VALUE'),
458                                                         'INT'              => array('VALUE'),
459                                                         'STRING'           => array('VALUE'),
460                                                         'DOUBLE'           => array('VALUE'),
461                                                         'DATETIME.ISO8601' => array('VALUE'),
462                                                         'BASE64'           => array('VALUE'),
463                                                         'ARRAY'            => array('VALUE'),
464                                                         'STRUCT'           => array('VALUE'),
465                                                         'PARAM'            => array('PARAMS'),
466                                                         'METHODNAME'       => array('METHODCALL'),              
467                                                         'PARAMS'           => array('METHODCALL', 'METHODRESPONSE'),
468                                                         'MEMBER'           => array('STRUCT'),
469                                                         'NAME'             => array('MEMBER'),
470                                                         'DATA'             => array('ARRAY'),
471                                                         'FAULT'            => array('METHODRESPONSE'),
472                                                         'VALUE'            => array('MEMBER', 'DATA', 'PARAM', 'FAULT'),
473                                         );
474                                 }
475
476                                 $svr =& new xmlrpc_server($map);
477                                 exit(0);
478                                 break;
479                         case 'changes':
480                                 $this->changesOutput();
481                                 exit(0);
482                                 break;
483                 }
484         }
485
486 }