3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2007 The Nucleus Group
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)
13 * This is an (abstract) class of which all Nucleus Plugins must inherit
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
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.9 2007-03-13 05:03:23 shizuki Exp $
21 * $NucleusJP: PLUGIN.php,v 1.8 2007/02/19 22:29:31 kmorimatsu Exp $
25 // these functions _have_ to be redefined in your plugin
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';}
33 // these function _may_ be redefined in your plugin
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; }
42 function unInstall() {}
46 function doSkinVar($skinType) {}
47 function doTemplateVar(&$item) {
48 $args = func_get_args();
50 array_unshift($args, 'template');
51 call_user_func_array(array(&$this,'doSkinVar'),$args);
53 function doTemplateCommentsVar(&$item, &$comment) {
54 $args = func_get_args();
57 array_unshift($args, 'template');
58 call_user_func_array(array(&$this,'doSkinVar'),$args);
60 function doAction($type) { return 'No Such Action'; }
61 function doIf($key,$value) { return false; }
62 function doItemVar () {}
65 * Checks if a plugin supports a certain feature.
67 * @returns 1 if the feature is reported, 0 if not
69 * Name of the feature. See plugin documentation for more info
70 * 'SqlTablePrefix' -> if the plugin uses the sql_table() method to get table names
71 * 'HelpPage' -> if the plugin provides a helppage
73 function supportsFeature($feature) {
78 * Report a list of plugin that is required to function
80 * @returns an array of names of plugin, an empty array indicates no dependency
82 function getPluginDep() { return array(); }
84 // these helper functions should not be redefined in your plugin
87 * Creates a new option for this plugin
90 * A string uniquely identifying your option. (max. length is 20 characters)
92 * A description that will show up in the nucleus admin area (max. length: 255 characters)
94 * Either 'text', 'yesno' or 'password'
95 * This info is used when showing 'edit plugin options' screens
97 * Initial value for the option (max. value length is 128 characters)
99 function createOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
100 return $this->_createOption('global', $name, $desc, $type, $defValue, $typeExtras);
102 function createBlogOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
103 return $this->_createOption('blog', $name, $desc, $type, $defValue, $typeExtras);
105 function createMemberOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
106 return $this->_createOption('member', $name, $desc, $type, $defValue, $typeExtras);
108 function createCategoryOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
109 return $this->_createOption('category', $name, $desc, $type, $defValue, $typeExtras);
111 function createItemOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
112 return $this->_createOption('item', $name, $desc, $type, $defValue, $typeExtras);
116 * Removes the option from the database
118 * Note: Options get erased automatically on plugin uninstall
120 function deleteOption($name) {
121 return $this->_deleteOption('global', $name);
123 function deleteBlogOption($name) {
124 return $this->_deleteOption('blog', $name);
126 function deleteMemberOption($name) {
127 return $this->_deleteOption('member', $name);
129 function deleteCategoryOption($name) {
130 return $this->_deleteOption('category', $name);
132 function deleteItemOption($name) {
133 return $this->_deleteOption('item', $name);
137 * Sets the value of an option to something new
139 function setOption($name, $value) {
140 return $this->_setOption('global', 0, $name, $value);
142 function setBlogOption($blogid, $name, $value) {
143 return $this->_setOption('blog', $blogid, $name, $value);
145 function setMemberOption($memberid, $name, $value) {
146 return $this->_setOption('member', $memberid, $name, $value);
148 function setCategoryOption($catid, $name, $value) {
149 return $this->_setOption('category', $catid, $name, $value);
151 function setItemOption($itemid, $name, $value) {
152 return $this->_setOption('item', $itemid, $name, $value);
156 * Retrieves the current value for an option
158 function getOption($name)
160 // only request the options the very first time. On subsequent requests
161 // the static collection is used to save SQL queries.
162 if ($this->plugin_options == 0)
164 $this->plugin_options = array();
165 $query = mysql_query(
166 'SELECT d.oname as name, o.ovalue as value '.
168 sql_table('plugin_option').' o, '.
169 sql_table('plugin_option_desc').' d '.
170 'WHERE d.opid='. intval($this->getID()).' AND d.oid=o.oid'
172 while ($row = mysql_fetch_object($query))
173 $this->plugin_options[strtolower($row->name)] = $row->value;
175 if (isset($this->plugin_options[strtolower($name)]))
176 return $this->plugin_options[strtolower($name)];
178 return $this->_getOption('global', 0, $name);
180 function getBlogOption($blogid, $name) {
181 return $this->_getOption('blog', $blogid, $name);
183 function getMemberOption($memberid, $name) {
184 return $this->_getOption('member', $memberid, $name);
186 function getCategoryOption($catid, $name) {
187 return $this->_getOption('category', $catid, $name);
189 function getItemOption($itemid, $name) {
190 return $this->_getOption('item', $itemid, $name);
194 * Retrieves an associative array with the option value for each
197 function getAllBlogOptions($name) {
198 return $this->_getAllOptions('blog', $name);
200 function getAllMemberOptions($name) {
201 return $this->_getAllOptions('member', $name);
203 function getAllCategoryOptions($name) {
204 return $this->_getAllOptions('category', $name);
206 function getAllItemOptions($name) {
207 return $this->_getAllOptions('item', $name);
211 * Retrieves an indexed array with the top (or bottom) of an option
212 * (delegates to _getOptionTop())
214 function getBlogOptionTop($name, $amount = 10, $sort = 'desc') {
215 return $this->_getOptionTop('blog', $name, $amount, $sort);
217 function getMemberOptionTop($name, $amount = 10, $sort = 'desc') {
218 return $this->_getOptionTop('member', $name, $amount, $sort);
220 function getCategoryOptionTop($name, $amount = 10, $sort = 'desc') {
221 return $this->_getOptionTop('category', $name, $amount, $sort);
223 function getItemOptionTop($name, $amount = 10, $sort = 'desc') {
224 return $this->_getOptionTop('item', $name, $amount, $sort);
228 * Retrieves an array of the top (or bottom) of an option from a plugin.
230 * @param string $context the context for the option: item, blog, member,...
231 * @param string $name the name of the option
232 * @param int $amount how many rows must be returned
233 * @param string $sort desc or asc
234 * @return array array with both values and contextid's
237 function _getOptionTop($context, $name, $amount = 10, $sort = 'desc') {
238 if (($sort != 'desc') && ($sort != 'asc')) {
242 $oid = $this->_getOID($context, $name);
244 // retrieve the data and return
245 $q = 'SELECT otype, oextra FROM '.sql_table('plugin_option_desc').' WHERE oid = '.$oid;
246 $query = mysql_query($q);
248 $o = mysql_fetch_array($query);
250 if (($this->optionCanBeNumeric($o['otype'])) && ($o['oextra'] == 'number' )) {
251 $orderby = 'CAST(ovalue AS SIGNED)';
255 $q = 'SELECT ovalue value, ocontextid id FROM '.sql_table('plugin_option').' WHERE oid = '.$oid.' ORDER BY '.$orderby.' '.$sort.' LIMIT 0,'.$amount;
256 $query = mysql_query($q);
261 while($row = mysql_fetch_array($query)) {
265 // return the array (duh!)
270 * Returns the plugin ID
273 return $this->plugid;
277 * returns the URL of the admin area for this plugin (in case there's
278 * no such area, the returned information is invalid)
280 function getAdminURL() {
282 return $CONF['PluginURL'] . $this->getShortName() . '/';
286 * Returns the directory where the admin directory is located and
287 * where the plugin can maintain his extra files
289 function getDirectory() {
291 return $DIR_PLUGINS . $this->getShortName() . '/';
295 * Derives the short name for the plugin from the classname (all lowercase)
297 function getShortName() {
298 return str_replace('np_','',strtolower(get_class($this)));
301 var $_aOptionValues; // oid_contextid => value
302 var $_aOptionToInfo; // context_name => array('oid' => ..., 'default' => ...)
303 var $plugin_options; // see getOption()
304 var $plugid; // plugin id
307 // constructor. Initializes some internal data
308 function NucleusPlugin() {
309 $this->_aOptionValues = array(); // oid_contextid => value
310 $this->_aOptionToInfo = array(); // context_name => array('oid' => ..., 'default' => ...)
311 $this->plugin_options = 0;
315 function _createOption($context, $name, $desc, $type, $defValue, $typeExtras = '') {
316 // create in plugin_option_desc
317 $query = 'INSERT INTO ' . sql_table('plugin_option_desc')
318 .' (opid, oname, ocontext, odesc, otype, odef, oextra)'
319 .' VALUES ('.intval($this->plugid)
320 .', \''.addslashes($name).'\''
321 .', \''.addslashes($context).'\''
322 .', \''.addslashes($desc).'\''
323 .', \''.addslashes($type).'\''
324 .', \''.addslashes($defValue).'\''
325 .', \''.addslashes($typeExtras).'\')';
327 $oid = mysql_insert_id();
329 $key = $context . '_' . $name;
330 $this->_aOptionToInfo[$key] = array('oid' => $oid, 'default' => $defValue);
336 function _deleteOption($context, $name) {
337 $oid = $this->_getOID($context, $name);
338 if (!$oid) return 0; // no such option
340 // delete all things from plugin_option
341 sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);
343 // delete entry from plugin_option_desc
344 sql_query('DELETE FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . $oid);
347 unset($this->_aOptionToInfo[$context . '_' . $name]);
348 $this->_aOptionValues = array();
354 * returns: 1 on success, 0 on failure
356 function _setOption($context, $contextid, $name, $value) {
359 $oid = $this->_getOID($context, $name);
362 // check if context id exists
365 if (!MEMBER::existsID($contextid)) return 0;
368 if (!$manager->existsBlogID($contextid)) return 0;
371 if (!$manager->existsCategory($contextid)) return 0;
374 if (!$manager->existsItem($contextid, true, true)) return 0;
377 if ($contextid != 0) return 0;
382 // update plugin_option
383 sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid) . ' and ocontextid='. intval($contextid));
384 @mysql_query('INSERT INTO ' . sql_table('plugin_option') . ' (ovalue, oid, ocontextid) VALUES (\''.addslashes($value).'\', '. intval($oid) . ', ' . intval($contextid) . ')');
387 $this->_aOptionValues[$oid . '_' . $contextid] = $value;
393 function _getOption($context, $contextid, $name) {
394 $oid = $this->_getOID($context, $name);
395 if (!$oid) return '';
398 $key = $oid . '_' . $contextid;
400 if (isset($this->_aOptionValues[$key]))
401 return $this->_aOptionValues[$key];
404 $res = sql_query('SELECT ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid).' and ocontextid=' . intval($contextid));
406 if (!$res || (mysql_num_rows($res) == 0)) {
407 $defVal = $this->_getDefVal($context, $name);
408 $this->_aOptionValues[$key] = $defVal;
410 // fill DB with default value
411 $query = 'INSERT INTO ' . sql_table('plugin_option') . ' (oid,ocontextid,ovalue)'
412 .' VALUES ('.intval($oid).', '.intval($contextid).', \''.addslashes($defVal).'\')';
416 $o = mysql_fetch_object($res);
417 $this->_aOptionValues[$key] = $o->ovalue;
420 return $this->_aOptionValues[$key];
424 * Returns assoc array with all values for a given option (one option per
425 * possible context id)
427 function _getAllOptions($context, $name) {
428 $oid = $this->_getOID($context, $name);
429 if (!$oid) return array();
430 $defVal = $this->_getDefVal($context, $name);
435 $r = sql_query('SELECT bnumber as contextid FROM ' . sql_table('blog'));
438 $r = sql_query('SELECT catid as contextid FROM ' . sql_table('category'));
441 $r = sql_query('SELECT mnumber as contextid FROM ' . sql_table('member'));
444 $r = sql_query('SELECT inumber as contextid FROM ' . sql_table('item'));
448 while ($o = mysql_fetch_object($r))
449 $aOptions[$o->contextid] = $defVal;
452 $res = sql_query('SELECT ocontextid, ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);
453 while ($o = mysql_fetch_object($res))
454 $aOptions[$o->ocontextid] = $o->ovalue;
460 * Gets the 'option identifier' that corresponds to a given option name.
461 * When this method is called for the first time, all the OIDs for the plugin
462 * are loaded into memory, to avoid re-doing the same query all over.
464 function _getOID($context, $name) {
465 $key = $context . '_' . $name;
466 $info = $this->_aOptionToInfo[$key];
467 if (is_array($info)) return $info['oid'];
469 // load all OIDs for this plugin from the database
470 $this->_aOptionToInfo = array();
471 $query = 'SELECT oid, oname, ocontext, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE opid=' . intval($this->plugid);
472 $res = sql_query($query);
473 while ($o = mysql_fetch_object($res)) {
474 $k = $o->ocontext . '_' . $o->oname;
475 $this->_aOptionToInfo[$k] = array('oid' => $o->oid, 'default' => $o->odef);
477 mysql_free_result($res);
479 return $this->_aOptionToInfo[$key]['oid'];
481 function _getDefVal($context, $name) {
482 $key = $context . '_' . $name;
483 $info = $this->_aOptionToInfo[$key];
484 if (is_array($info)) return $info['default'];
489 * Deletes all option values for a given context and contextid
490 * (used when e.g. a blog, member or category is deleted)
494 function _deleteOptionValues($context, $contextid) {
495 // delete all associated plugin options
498 $query = 'SELECT oid FROM '.sql_table('plugin_option_desc') . ' WHERE ocontext=\''.addslashes($context).'\'';
499 $res = sql_query($query);
500 while ($o = mysql_fetch_object($res))
501 array_push($aOIDs, $o->oid);
502 mysql_free_result($res);
503 // delete those options. go go go
504 if (count($aOIDs) > 0) {
505 $query = 'DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid in ('.implode(',',$aOIDs).') and ocontextid=' . intval($contextid);
511 * splits the option's typeextra field (at ;'s) to split the meta collection
512 * @param string $typeExtra the value of the typeExtra field of an option
513 * @return array array of the meta-key/value-pairs
517 function getOptionMeta($typeExtra) {
518 $tmpMeta = explode(';', $typeExtra);
520 for ($i = 0; $i < count($tmpMeta); $i++) {
521 if (($i == 0) && (!strstr($tmpMeta[0], '='))) {
522 // we have the select-list
523 $meta['select'] = $tmpMeta[0];
525 $tmp = explode('=', $tmpMeta[$i]);
526 $meta[$tmp[0]] = $tmp[1];
533 * filters the selectlists out of the meta collection
534 * @param string $typeExtra the value of the typeExtra field of an option
535 * @return string the selectlist
538 function getOptionSelectValues($typeExtra) {
539 $meta = NucleusPlugin::getOptionMeta($typeExtra);
540 //the select list must always be the first part
541 return $meta['select'];
545 * checks if the eventlist in the database is up-to-date
546 * @return bool if it is up-to-date it return true, else false
549 function subscribtionListIsUptodate() {
550 $res = sql_query('SELECT event FROM '.sql_table('plugin_event').' WHERE pid = '.$this->getID());
552 while($a = mysql_fetch_array($res)) {
553 array_push($ev, $a['event']);
555 if (count($ev) != count($this->getEventList())) {
558 $d = array_diff($ev, $this->getEventList());
560 // there are differences so the db is not up-to-date
567 * @param $aOptions: array ( 'oid' => array( 'contextid' => 'value'))
568 * (taken from request using requestVar())
569 * @param $newContextid: integer (accepts a contextid when it is for a new
570 * contextid there was no id available at the moment of writing the
571 * formcontrols into the page (by ex: itemOptions for new item)
574 function _applyPluginOptions(&$aOptions, $newContextid = 0) {
576 if (!is_array($aOptions)) return;
578 foreach ($aOptions as $oid => $values) {
580 // get option type info
581 $query = 'SELECT opid, oname, ocontext, otype, oextra, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . intval($oid);
582 $res = sql_query($query);
583 if ($o = mysql_fetch_object($res))
585 foreach ($values as $contextid => $value) {
586 // retreive any metadata
587 $meta = NucleusPlugin::getOptionMeta($o->oextra);
589 // if the option is readonly or hidden it may not be saved
590 if (($meta['access'] != 'readonly') && ($meta['access'] != 'hidden')) {
592 $value = undoMagic($value); // value comes from request
596 if (($value != 'yes') && ($value != 'no')) $value = 'no';
602 // check the validity of numerical options
603 if (($meta['datatype'] == 'numerical') && (!is_numeric($value))) {
604 //the option must be numeric, but the it isn't
605 //use the default for this option
609 // decide wether we are using the contextid of newContextid
610 if ($newContextid != 0) {
611 $contextid = $newContextid;
614 //trigger event PrePluginOptionsUpdate to give the plugin the
615 //possibility to change/validate the new value for the option
616 $manager->notify('PrePluginOptionsUpdate',array('context' => $o->ocontext, 'plugid' => $o->opid, 'optionname' => $o->oname, 'contextid' => $contextid, 'value' => &$value));
618 // delete the old value for the option
619 sql_query('DELETE FROM '.sql_table('plugin_option').' WHERE oid='.intval($oid).' AND ocontextid='.intval($contextid));
620 @mysql_query('INSERT INTO '.sql_table('plugin_option')." (oid, ocontextid, ovalue) VALUES (".intval($oid).",".intval($contextid).",'" . addslashes($value) . "')");