OSDN Git Service

sync with original 3.3
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / PLUGIN.php
1 <?php
2         /*
3          * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4          * Copyright (C) 2002-2007 The Nucleus Group
5          *
6          * This program is free software; you can redistribute it and/or
7          * modify it under the terms of the GNU General Public License
8          * as published by the Free Software Foundation; either version 2
9          * of the License, or (at your option) any later version.
10          * (see nucleus/documentation/index.html#license for more info)
11          */
12         /**
13          * This is an (abstract) class of which all Nucleus Plugins must inherit
14          *
15          * for more information on plugins and how to write your own, see the
16          * plugins.html file that is included with the Nucleus documenation
17          *
18          * @license http://nucleuscms.org/license.txt GNU General Public License
19          * @copyright Copyright (C) 2002-2007 The Nucleus Group
20          * @version $Id: PLUGIN.php,v 1.7 2007-02-04 06:28:46 kimitake Exp $
21          * $NucleusJP: PLUGIN.php,v 1.6 2006/12/07 03:18:33 kmorimatsu Exp $
22          */
23         class NucleusPlugin {
24
25                 // these functions _have_ to be redefined in your plugin
26
27                 function getName() { return 'Undefined'; }
28                 function getAuthor()  { return 'Undefined'; }
29                 function getURL()  { return 'Undefined'; }
30                 function getVersion() { return '0.0'; }
31                 function getDescription() { return 'Undefined';}
32
33                 // these function _may_ be redefined in your plugin
34
35                 function getMinNucleusVersion() { return 150; }
36                 function getMinNucleusPatchLevel() { return 0; }
37                 function getEventList() { return array(); }
38                 function getTableList() { return array(); }
39                 function hasAdminArea() { return 0; }
40
41                 function install() {}
42                 function unInstall() {}
43
44                 function init() {}
45
46                 function doSkinVar($skinType) {}
47                 function doTemplateVar(&$item) {
48                         $args = func_get_args();
49                         array_shift($args);
50                         array_unshift($args, 'template');
51                         call_user_func_array(array(&$this,'doSkinVar'),$args);
52                 }
53                 function doTemplateCommentsVar(&$item, &$comment) {
54                         $args = func_get_args();
55                         array_shift($args);
56                         array_shift($args);
57                         array_unshift($args, 'template');
58                         call_user_func_array(array(&$this,'doSkinVar'),$args);
59                 }
60                 function doAction($type) { return 'No Such Action'; }
61                 function doIf($key,$value) { return false; }
62
63                 /**
64                  * Checks if a plugin supports a certain feature.
65                  *
66                  * @returns 1 if the feature is reported, 0 if not
67                  * @param $feature
68                  *              Name of the feature. See plugin documentation for more info
69                  *                      'SqlTablePrefix' -> if the plugin uses the sql_table() method to get table names
70                  *                      'HelpPage' -> if the plugin provides a helppage
71                  */
72                 function supportsFeature($feature) {
73                         return 0;
74                 }
75
76                 /**
77                  * Report a list of plugin that is required to function
78                  *
79                  * @returns an array of names of plugin, an empty array indicates no dependency
80                  */
81                 function getPluginDep() { return array(); }
82
83                 // these helper functions should not be redefined in your plugin
84
85                 /**
86                   * Creates a new option for this plugin
87                   *
88                   * @param name
89                   *             A string uniquely identifying your option. (max. length is 20 characters)
90                   * @param description
91                   *             A description that will show up in the nucleus admin area (max. length: 255 characters)
92                   * @param type
93                   *             Either 'text', 'yesno' or 'password'
94                   *             This info is used when showing 'edit plugin options' screens
95                   * @param value
96                   *             Initial value for the option (max. value length is 128 characters)
97                   */
98                 function createOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
99                         return $this->_createOption('global', $name, $desc, $type, $defValue, $typeExtras);
100                 }
101                 function createBlogOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
102                         return $this->_createOption('blog', $name, $desc, $type, $defValue, $typeExtras);
103                 }
104                 function createMemberOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
105                         return $this->_createOption('member', $name, $desc, $type, $defValue, $typeExtras);
106                 }
107                 function createCategoryOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
108                         return $this->_createOption('category', $name, $desc, $type, $defValue, $typeExtras);
109                 }
110                 function createItemOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
111                         return $this->_createOption('item', $name, $desc, $type, $defValue, $typeExtras);
112                 }
113
114                 /**
115                   * Removes the option from the database
116                   *
117                   * Note: Options get erased automatically on plugin uninstall
118                   */
119                 function deleteOption($name) {
120                         return $this->_deleteOption('global', $name);
121                 }
122                 function deleteBlogOption($name) {
123                         return $this->_deleteOption('blog', $name);
124                 }
125                 function deleteMemberOption($name) {
126                         return $this->_deleteOption('member', $name);
127                 }
128                 function deleteCategoryOption($name) {
129                         return $this->_deleteOption('category', $name);
130                 }
131                 function deleteItemOption($name) {
132                         return $this->_deleteOption('item', $name);
133                 }
134
135                 /**
136                   * Sets the value of an option to something new
137                   */
138                 function setOption($name, $value) {
139                         return $this->_setOption('global', 0, $name, $value);
140                 }
141                 function setBlogOption($blogid, $name, $value) {
142                         return $this->_setOption('blog', $blogid, $name, $value);
143                 }
144                 function setMemberOption($memberid, $name, $value) {
145                         return $this->_setOption('member', $memberid, $name, $value);
146                 }
147                 function setCategoryOption($catid, $name, $value) {
148                         return $this->_setOption('category', $catid, $name, $value);
149                 }
150                 function setItemOption($itemid, $name, $value) {
151                         return $this->_setOption('item', $itemid, $name, $value);
152                 }
153
154                 /**
155                   * Retrieves the current value for an option
156                   */
157                 function getOption($name) {
158                         return $this->_getOption('global', 0, $name);
159                 }
160                 function getBlogOption($blogid, $name) {
161                         return $this->_getOption('blog', $blogid, $name);
162                 }
163                 function getMemberOption($memberid, $name) {
164                         return $this->_getOption('member', $memberid, $name);
165                 }
166                 function getCategoryOption($catid, $name) {
167                         return $this->_getOption('category', $catid, $name);
168                 }
169                 function getItemOption($itemid, $name) {
170                         return $this->_getOption('item', $itemid, $name);
171                 }
172
173                 /**
174                  * Retrieves an associative array with the option value for each
175                  * context id
176                  */
177                 function getAllBlogOptions($name) {
178                         return $this->_getAllOptions('blog', $name);
179                 }
180                 function getAllMemberOptions($name) {
181                         return $this->_getAllOptions('member', $name);
182                 }
183                 function getAllCategoryOptions($name) {
184                         return $this->_getAllOptions('category', $name);
185                 }
186                 function getAllItemOptions($name) {
187                         return $this->_getAllOptions('item', $name);
188                 }
189
190                 /**
191                  * Retrieves an indexed array with the top (or bottom) of an option
192                  * (delegates to _getOptionTop())
193                  */
194                 function getBlogOptionTop($name, $amount = 10, $sort = 'desc') {
195                         return $this->_getOptionTop('blog', $name, $amount, $sort);
196                 }
197                 function getMemberOptionTop($name, $amount = 10, $sort = 'desc') {
198                         return $this->_getOptionTop('member', $name, $amount, $sort);
199                 }
200                 function getCategoryOptionTop($name, $amount = 10, $sort = 'desc') {
201                         return $this->_getOptionTop('category', $name, $amount, $sort);
202                 }
203                 function getItemOptionTop($name, $amount = 10, $sort = 'desc') {
204                         return $this->_getOptionTop('item', $name, $amount, $sort);
205                 }
206
207                 /**
208                  * Retrieves an array of the top (or bottom) of an option from a plugin.
209                  * @author TeRanEX
210                  * @param  string $context the context for the option: item, blog, member,...
211                  * @param  string $name    the name of the option
212                  * @param  int    $amount  how many rows must be returned
213                  * @param  string $sort    desc or asc
214                  * @return array           array with both values and contextid's
215                  * @access private
216                  */
217                 function _getOptionTop($context, $name, $amount = 10, $sort = 'desc') {
218                         if (($sort != 'desc') && ($sort != 'asc')) {
219                                 $sort= 'desc';
220                         }
221
222                         $oid = $this->_getOID($context, $name);
223
224                         // retrieve the data and return
225                         $q = 'SELECT otype, oextra FROM '.sql_table('plugin_option_desc').' WHERE oid = '.$oid;
226                         $query = mysql_query($q);
227
228                         $o = mysql_fetch_array($query);
229
230                         if (($this->optionCanBeNumeric($o['otype'])) && ($o['oextra'] == 'number' )) {
231                                 $orderby = 'CAST(ovalue AS SIGNED)';
232                         } else {
233                                 $orderby = 'ovalue';
234                         }
235                         $q = 'SELECT ovalue value, ocontextid id FROM '.sql_table('plugin_option').' WHERE oid = '.$oid.' ORDER BY '.$orderby.' '.$sort.' LIMIT 0,'.$amount;
236                         $query = mysql_query($q);
237
238                         // create the array
239                         $i = 0;
240                         $top = array();
241                         while($row = mysql_fetch_array($query)) {
242                                 $top[$i++] = $row;
243                         }
244
245                         // return the array (duh!)
246                         return $top;
247                 }
248
249                 /**
250                   * Returns the plugin ID
251                   */
252                 function getID() {
253                         return $this->plugid;
254                 }
255
256                 /**
257                   * returns the URL of the admin area for this plugin (in case there's
258                   * no such area, the returned information is invalid)
259                   */
260                 function getAdminURL() {
261                         global $CONF;
262                         return $CONF['PluginURL'] . $this->getShortName() . '/';
263                 }
264
265                 /**
266                   * Returns the directory where the admin directory is located and
267                   * where the plugin can maintain his extra files
268                   */
269                 function getDirectory() {
270                         global $DIR_PLUGINS;
271                         return $DIR_PLUGINS . $this->getShortName() . '/';
272                 }
273
274                 /**
275                   * Derives the short name for the plugin from the classname (all lowercase)
276                   */
277                 function getShortName() {
278                         return str_replace('np_','',strtolower(get_class($this)));
279                 }
280
281                 var $_aOptionValues;    // oid_contextid => value
282                 var $_aOptionToInfo;    // context_name => array('oid' => ..., 'default' => ...)
283                 var $plugid;                    // plugin id
284
285
286                 // constructor. Initializes some internal data
287                 function NucleusPlugin() {
288                         $this->_aOptionValues = array();        // oid_contextid => value
289                         $this->_aOptionToInfo = array();        // context_name => array('oid' => ..., 'default' => ...)
290                 }
291
292                 // private
293                 function _createOption($context, $name, $desc, $type, $defValue, $typeExtras = '') {
294                         // create in plugin_option_desc
295                         $query = 'INSERT INTO ' . sql_table('plugin_option_desc')
296                                    .' (opid, oname, ocontext, odesc, otype, odef, oextra)'
297                                    .' VALUES ('.intval($this->plugid)
298                                                          .', \''.addslashes($name).'\''
299                                                          .', \''.addslashes($context).'\''
300                                                          .', \''.addslashes($desc).'\''
301                                                          .', \''.addslashes($type).'\''
302                                                          .', \''.addslashes($defValue).'\''
303                                                          .', \''.addslashes($typeExtras).'\')';
304                         sql_query($query);
305                         $oid = mysql_insert_id();
306
307                         $key = $context . '_' . $name;
308                         $this->_aOptionToInfo[$key] = array('oid' => $oid, 'default' => $defValue);
309                         return 1;
310                 }
311
312
313                 // private
314                 function _deleteOption($context, $name) {
315                         $oid = $this->_getOID($context, $name);
316                         if (!$oid) return 0; // no such option
317
318                         // delete all things from plugin_option
319                         sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);
320
321                         // delete entry from plugin_option_desc
322                         sql_query('DELETE FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . $oid);
323
324                         // clear from cache
325                         unset($this->_aOptionToInfo[$context . '_' . $name]);
326                         $this->_aOptionValues = array();
327                         return 1;
328                 }
329
330                 /**
331                  * private
332                  * returns: 1 on success, 0 on failure
333                  */
334                 function _setOption($context, $contextid, $name, $value) {
335                         global $manager;
336
337                         $oid = $this->_getOID($context, $name);
338                         if (!$oid) return 0;
339
340                         // check if context id exists
341                         switch ($context) {
342                                 case 'member':
343                                         if (!MEMBER::existsID($contextid)) return 0;
344                                         break;
345                                 case 'blog':
346                                         if (!$manager->existsBlogID($contextid)) return 0;
347                                         break;
348                                 case 'category':
349                                         if (!$manager->existsCategory($contextid)) return 0;
350                                         break;
351                                 case 'item':
352                                         if (!$manager->existsItem($contextid, true, true)) return 0;
353                                         break;
354                                 case 'global':
355                                         if ($contextid != 0) return 0;
356                                         break;
357                         }
358
359
360                         // update plugin_option
361                         sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid) . ' and ocontextid='. intval($contextid));
362                         sql_query('INSERT INTO ' . sql_table('plugin_option') . ' (ovalue, oid, ocontextid) VALUES (\''.addslashes($value).'\', '. intval($oid) . ', ' . intval($contextid) . ')');
363
364                         // update cache
365                         $this->_aOptionValues[$oid . '_' . $contextid] = $value;
366
367                         return 1;
368                 }
369
370                 // private
371                 function _getOption($context, $contextid, $name) {
372                         $oid = $this->_getOID($context, $name);
373                         if (!$oid) return '';
374
375
376                         $key = $oid . '_' . $contextid;
377
378                         if (isset($this->_aOptionValues[$key]))
379                                 return $this->_aOptionValues[$key];
380
381                         // get from DB
382                         $res = sql_query('SELECT ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid).' and ocontextid=' . intval($contextid));
383
384                         if (!$res || (mysql_num_rows($res) == 0)) {
385                                 $defVal = $this->_getDefVal($context, $name);
386                                 $this->_aOptionValues[$key] = $defVal;
387
388                                 // fill DB with default value
389                                 $query = 'INSERT INTO ' . sql_table('plugin_option') . ' (oid,ocontextid,ovalue)'
390                                            .' VALUES ('.intval($oid).', '.intval($contextid).', \''.addslashes($defVal).'\')';
391                                 sql_query($query);
392                         }
393                         else {
394                                 $o = mysql_fetch_object($res);
395                                 $this->_aOptionValues[$key] = $o->ovalue;
396                         }
397
398                         return $this->_aOptionValues[$key];
399                 }
400
401                 /**
402                  * Returns assoc array with all values for a given option (one option per
403                  * possible context id)
404                  */
405                 function _getAllOptions($context, $name) {
406                         $oid = $this->_getOID($context, $name);
407                         if (!$oid) return array();
408                         $defVal = $this->_getDefVal($context, $name);
409
410                         $aOptions = array();
411                         switch ($context) {
412                                 case 'blog':
413                                         $r = sql_query('SELECT bnumber as contextid FROM ' . sql_table('blog'));
414                                         break;
415                                 case 'category':
416                                         $r = sql_query('SELECT catid as contextid FROM ' . sql_table('category'));
417                                         break;
418                                 case 'member':
419                                         $r = sql_query('SELECT mnumber as contextid FROM ' . sql_table('member'));
420                                         break;
421                         }
422                         if ($r) {
423                                 while ($o = mysql_fetch_object($r))
424                                         $aOptions[$o->contextid] = $defVal;
425                         }
426
427                         $res = sql_query('SELECT ocontextid, ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);
428                         while ($o = mysql_fetch_object($res))
429                                 $aOptions[$o->ocontextid] = $o->ovalue;
430
431                         return $aOptions;
432                 }
433
434                 /**
435                  * Gets the 'option identifier' that corresponds to a given option name.
436                  * When this method is called for the first time, all the OIDs for the plugin
437                  * are loaded into memory, to avoid re-doing the same query all over.
438                  */
439                 function _getOID($context, $name) {
440                         $key = $context . '_' . $name;
441                         $info = $this->_aOptionToInfo[$key];
442                         if (is_array($info)) return $info['oid'];
443
444                         // load all OIDs for this plugin from the database
445                         $this->_aOptionToInfo = array();
446                         $query = 'SELECT oid, oname, ocontext, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE opid=' . intval($this->plugid);
447                         $res = sql_query($query);
448                         while ($o = mysql_fetch_object($res)) {
449                                 $k = $o->ocontext . '_' . $o->oname;
450                                 $this->_aOptionToInfo[$k] = array('oid' => $o->oid, 'default' => $o->odef);
451                         }
452                         mysql_free_result($res);
453
454                         return $this->_aOptionToInfo[$key]['oid'];
455                 }
456                 function _getDefVal($context, $name) {
457                         $key = $context . '_' . $name;
458                         $info = $this->_aOptionToInfo[$key];
459                         if (is_array($info)) return $info['default'];
460                 }
461
462
463                 /**
464                  * Deletes all option values for a given context and contextid
465                  * (used when e.g. a blog, member or category is deleted)
466                  *
467                  * (static method)
468                  */
469                 function _deleteOptionValues($context, $contextid) {
470                         // delete all associated plugin options
471                         $aOIDs = array();
472                                 // find ids
473                         $query = 'SELECT oid FROM '.sql_table('plugin_option_desc') . ' WHERE ocontext=\''.addslashes($context).'\'';
474                         $res = sql_query($query);
475                         while ($o = mysql_fetch_object($res))
476                                 array_push($aOIDs, $o->oid);
477                         mysql_free_result($res);
478                                 // delete those options. go go go
479                         if (count($aOIDs) > 0) {
480                                 $query = 'DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid in ('.implode(',',$aOIDs).') and ocontextid=' . intval($contextid);
481                                 sql_query($query);
482                         }
483                 }
484
485                 /**
486                  * splits the option's typeextra field (at ;'s) to split the meta collection
487                  * @param string $typeExtra the value of the typeExtra field of an option
488                  * @return array array of the meta-key/value-pairs
489                  * @author TeRanEX
490                  * @static
491                  */
492                 function getOptionMeta($typeExtra) {
493                         $tmpMeta = explode(';', $typeExtra);
494                         $meta = array();
495                         for ($i = 0; $i < count($tmpMeta); $i++) {
496                                 if (($i == 0) && (!strstr($tmpMeta[0], '='))) {
497                                         // we have the select-list
498                                         $meta['select'] = $tmpMeta[0];
499                                 } else {
500                                         $tmp = explode('=', $tmpMeta[$i]);
501                                         $meta[$tmp[0]] = $tmp[1];
502                                 }
503                         }
504                         return $meta;
505                 }
506
507                 /**
508                  * filters the selectlists out of the meta collection
509                  * @param string $typeExtra the value of the typeExtra field of an option
510                  * @return string the selectlist
511                  * @author TeRanEX
512                  */
513                 function getOptionSelectValues($typeExtra) {
514                         $meta = NucleusPlugin::getOptionMeta($typeExtra);
515                         //the select list must always be the first part
516                         return $meta['select'];
517                 }
518
519                 /**
520                  * checks if the eventlist in the database is up-to-date
521                  * @return bool if it is up-to-date it return true, else false
522                  * @author TeRanEX
523                  */
524                 function subscribtionListIsUptodate() {
525                         $res = sql_query('SELECT event FROM '.sql_table('plugin_event').' WHERE pid = '.$this->getID());
526                         $ev = array();
527                         while($a = mysql_fetch_array($res)) {
528                                 array_push($ev, $a['event']);
529                         }
530                         if (count($ev) != count($this->getEventList())) {
531                                 return false;
532                         }
533                         $d = array_diff($ev, $this->getEventList());
534                         if (count($d) > 0) {
535                                 // there are differences so the db is not up-to-date
536                                 return false;
537                         }
538                         return true;
539                 }
540
541                 /**
542                  * @param $aOptions: array ( 'oid' => array( 'contextid' => 'value'))
543                  *        (taken from request using requestVar())
544                  * @param $newContextid: integer (accepts a contextid when it is for a new
545                  *        contextid there was no id available at the moment of writing the
546                  *        formcontrols into the page (by ex: itemOptions for new item)
547                  * @static
548                  */
549                 function _applyPluginOptions(&$aOptions, $newContextid = 0) {
550                         global $manager;
551                         if (!is_array($aOptions)) return;
552
553                         foreach ($aOptions as $oid => $values) {
554
555                                 // get option type info
556                                 $query = 'SELECT opid, oname, ocontext, otype, oextra, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . intval($oid);
557                                 $res = sql_query($query);
558                                 if ($o = mysql_fetch_object($res))
559                                 {
560                                         foreach ($values as $contextid => $value) {
561                                                 // retreive any metadata
562                                                 $meta = NucleusPlugin::getOptionMeta($o->oextra);
563
564                                                 // if the option is readonly or hidden it may not be saved
565                                                 if (($meta['access'] != 'readonly') && ($meta['access'] != 'hidden')) {
566
567                                                         $value = undoMagic($value);     // value comes from request
568
569                                                         switch($o->otype) {
570                                                                 case 'yesno':
571                                                                         if (($value != 'yes') && ($value != 'no')) $value = 'no';
572                                                                         break;
573                                                                 default:
574                                                                         break;
575                                                         }
576
577                                                         // check the validity of numerical options
578                                                         if (($meta['datatype'] == 'numerical') && (!is_numeric($value))) {
579                                                                 //the option must be numeric, but the it isn't
580                                                                 //use the default for this option
581                                                                 $value = $o->odef;
582                                                         }
583
584                                                         // decide wether we are using the contextid of newContextid
585                                                         if ($newContextid != 0) {
586                                                                 $contextid = $newContextid;
587                                                         }
588
589                                                         //trigger event PrePluginOptionsUpdate to give the plugin the
590                                                         //possibility to change/validate the new value for the option
591                                                         $manager->notify('PrePluginOptionsUpdate',array('context' => $o->ocontext, 'plugid' => $o->opid, 'optionname' => $o->oname, 'contextid' => $contextid, 'value' => &$value));
592
593                                                         // delete the old value for the option
594                                                         sql_query('DELETE FROM '.sql_table('plugin_option').' WHERE oid='.intval($oid).' AND ocontextid='.intval($contextid));
595                                                         sql_query('INSERT INTO '.sql_table('plugin_option')." (oid, ocontextid, ovalue) VALUES (".intval($oid).",".intval($contextid).",'" . addslashes($value) . "')");
596                                                 }
597                                         }
598                                 }
599                         }
600                 }
601
602         }
603 ?>