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.7 2007-02-04 06:28:46 kimitake Exp $
21 * $NucleusJP: PLUGIN.php,v 1.6 2006/12/07 03:18:33 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; }
64 * Checks if a plugin supports a certain feature.
66 * @returns 1 if the feature is reported, 0 if not
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
72 function supportsFeature($feature) {
77 * Report a list of plugin that is required to function
79 * @returns an array of names of plugin, an empty array indicates no dependency
81 function getPluginDep() { return array(); }
83 // these helper functions should not be redefined in your plugin
86 * Creates a new option for this plugin
89 * A string uniquely identifying your option. (max. length is 20 characters)
91 * A description that will show up in the nucleus admin area (max. length: 255 characters)
93 * Either 'text', 'yesno' or 'password'
94 * This info is used when showing 'edit plugin options' screens
96 * Initial value for the option (max. value length is 128 characters)
98 function createOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
99 return $this->_createOption('global', $name, $desc, $type, $defValue, $typeExtras);
101 function createBlogOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
102 return $this->_createOption('blog', $name, $desc, $type, $defValue, $typeExtras);
104 function createMemberOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
105 return $this->_createOption('member', $name, $desc, $type, $defValue, $typeExtras);
107 function createCategoryOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
108 return $this->_createOption('category', $name, $desc, $type, $defValue, $typeExtras);
110 function createItemOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
111 return $this->_createOption('item', $name, $desc, $type, $defValue, $typeExtras);
115 * Removes the option from the database
117 * Note: Options get erased automatically on plugin uninstall
119 function deleteOption($name) {
120 return $this->_deleteOption('global', $name);
122 function deleteBlogOption($name) {
123 return $this->_deleteOption('blog', $name);
125 function deleteMemberOption($name) {
126 return $this->_deleteOption('member', $name);
128 function deleteCategoryOption($name) {
129 return $this->_deleteOption('category', $name);
131 function deleteItemOption($name) {
132 return $this->_deleteOption('item', $name);
136 * Sets the value of an option to something new
138 function setOption($name, $value) {
139 return $this->_setOption('global', 0, $name, $value);
141 function setBlogOption($blogid, $name, $value) {
142 return $this->_setOption('blog', $blogid, $name, $value);
144 function setMemberOption($memberid, $name, $value) {
145 return $this->_setOption('member', $memberid, $name, $value);
147 function setCategoryOption($catid, $name, $value) {
148 return $this->_setOption('category', $catid, $name, $value);
150 function setItemOption($itemid, $name, $value) {
151 return $this->_setOption('item', $itemid, $name, $value);
155 * Retrieves the current value for an option
157 function getOption($name) {
158 return $this->_getOption('global', 0, $name);
160 function getBlogOption($blogid, $name) {
161 return $this->_getOption('blog', $blogid, $name);
163 function getMemberOption($memberid, $name) {
164 return $this->_getOption('member', $memberid, $name);
166 function getCategoryOption($catid, $name) {
167 return $this->_getOption('category', $catid, $name);
169 function getItemOption($itemid, $name) {
170 return $this->_getOption('item', $itemid, $name);
174 * Retrieves an associative array with the option value for each
177 function getAllBlogOptions($name) {
178 return $this->_getAllOptions('blog', $name);
180 function getAllMemberOptions($name) {
181 return $this->_getAllOptions('member', $name);
183 function getAllCategoryOptions($name) {
184 return $this->_getAllOptions('category', $name);
186 function getAllItemOptions($name) {
187 return $this->_getAllOptions('item', $name);
191 * Retrieves an indexed array with the top (or bottom) of an option
192 * (delegates to _getOptionTop())
194 function getBlogOptionTop($name, $amount = 10, $sort = 'desc') {
195 return $this->_getOptionTop('blog', $name, $amount, $sort);
197 function getMemberOptionTop($name, $amount = 10, $sort = 'desc') {
198 return $this->_getOptionTop('member', $name, $amount, $sort);
200 function getCategoryOptionTop($name, $amount = 10, $sort = 'desc') {
201 return $this->_getOptionTop('category', $name, $amount, $sort);
203 function getItemOptionTop($name, $amount = 10, $sort = 'desc') {
204 return $this->_getOptionTop('item', $name, $amount, $sort);
208 * Retrieves an array of the top (or bottom) of an option from a plugin.
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
217 function _getOptionTop($context, $name, $amount = 10, $sort = 'desc') {
218 if (($sort != 'desc') && ($sort != 'asc')) {
222 $oid = $this->_getOID($context, $name);
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);
228 $o = mysql_fetch_array($query);
230 if (($this->optionCanBeNumeric($o['otype'])) && ($o['oextra'] == 'number' )) {
231 $orderby = 'CAST(ovalue AS SIGNED)';
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);
241 while($row = mysql_fetch_array($query)) {
245 // return the array (duh!)
250 * Returns the plugin ID
253 return $this->plugid;
257 * returns the URL of the admin area for this plugin (in case there's
258 * no such area, the returned information is invalid)
260 function getAdminURL() {
262 return $CONF['PluginURL'] . $this->getShortName() . '/';
266 * Returns the directory where the admin directory is located and
267 * where the plugin can maintain his extra files
269 function getDirectory() {
271 return $DIR_PLUGINS . $this->getShortName() . '/';
275 * Derives the short name for the plugin from the classname (all lowercase)
277 function getShortName() {
278 return str_replace('np_','',strtolower(get_class($this)));
281 var $_aOptionValues; // oid_contextid => value
282 var $_aOptionToInfo; // context_name => array('oid' => ..., 'default' => ...)
283 var $plugid; // plugin id
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' => ...)
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).'\')';
305 $oid = mysql_insert_id();
307 $key = $context . '_' . $name;
308 $this->_aOptionToInfo[$key] = array('oid' => $oid, 'default' => $defValue);
314 function _deleteOption($context, $name) {
315 $oid = $this->_getOID($context, $name);
316 if (!$oid) return 0; // no such option
318 // delete all things from plugin_option
319 sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);
321 // delete entry from plugin_option_desc
322 sql_query('DELETE FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . $oid);
325 unset($this->_aOptionToInfo[$context . '_' . $name]);
326 $this->_aOptionValues = array();
332 * returns: 1 on success, 0 on failure
334 function _setOption($context, $contextid, $name, $value) {
337 $oid = $this->_getOID($context, $name);
340 // check if context id exists
343 if (!MEMBER::existsID($contextid)) return 0;
346 if (!$manager->existsBlogID($contextid)) return 0;
349 if (!$manager->existsCategory($contextid)) return 0;
352 if (!$manager->existsItem($contextid, true, true)) return 0;
355 if ($contextid != 0) return 0;
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) . ')');
365 $this->_aOptionValues[$oid . '_' . $contextid] = $value;
371 function _getOption($context, $contextid, $name) {
372 $oid = $this->_getOID($context, $name);
373 if (!$oid) return '';
376 $key = $oid . '_' . $contextid;
378 if (isset($this->_aOptionValues[$key]))
379 return $this->_aOptionValues[$key];
382 $res = sql_query('SELECT ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid).' and ocontextid=' . intval($contextid));
384 if (!$res || (mysql_num_rows($res) == 0)) {
385 $defVal = $this->_getDefVal($context, $name);
386 $this->_aOptionValues[$key] = $defVal;
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).'\')';
394 $o = mysql_fetch_object($res);
395 $this->_aOptionValues[$key] = $o->ovalue;
398 return $this->_aOptionValues[$key];
402 * Returns assoc array with all values for a given option (one option per
403 * possible context id)
405 function _getAllOptions($context, $name) {
406 $oid = $this->_getOID($context, $name);
407 if (!$oid) return array();
408 $defVal = $this->_getDefVal($context, $name);
413 $r = sql_query('SELECT bnumber as contextid FROM ' . sql_table('blog'));
416 $r = sql_query('SELECT catid as contextid FROM ' . sql_table('category'));
419 $r = sql_query('SELECT mnumber as contextid FROM ' . sql_table('member'));
423 while ($o = mysql_fetch_object($r))
424 $aOptions[$o->contextid] = $defVal;
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;
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.
439 function _getOID($context, $name) {
440 $key = $context . '_' . $name;
441 $info = $this->_aOptionToInfo[$key];
442 if (is_array($info)) return $info['oid'];
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);
452 mysql_free_result($res);
454 return $this->_aOptionToInfo[$key]['oid'];
456 function _getDefVal($context, $name) {
457 $key = $context . '_' . $name;
458 $info = $this->_aOptionToInfo[$key];
459 if (is_array($info)) return $info['default'];
464 * Deletes all option values for a given context and contextid
465 * (used when e.g. a blog, member or category is deleted)
469 function _deleteOptionValues($context, $contextid) {
470 // delete all associated plugin options
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);
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
492 function getOptionMeta($typeExtra) {
493 $tmpMeta = explode(';', $typeExtra);
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];
500 $tmp = explode('=', $tmpMeta[$i]);
501 $meta[$tmp[0]] = $tmp[1];
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
513 function getOptionSelectValues($typeExtra) {
514 $meta = NucleusPlugin::getOptionMeta($typeExtra);
515 //the select list must always be the first part
516 return $meta['select'];
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
524 function subscribtionListIsUptodate() {
525 $res = sql_query('SELECT event FROM '.sql_table('plugin_event').' WHERE pid = '.$this->getID());
527 while($a = mysql_fetch_array($res)) {
528 array_push($ev, $a['event']);
530 if (count($ev) != count($this->getEventList())) {
533 $d = array_diff($ev, $this->getEventList());
535 // there are differences so the db is not up-to-date
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)
549 function _applyPluginOptions(&$aOptions, $newContextid = 0) {
551 if (!is_array($aOptions)) return;
553 foreach ($aOptions as $oid => $values) {
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))
560 foreach ($values as $contextid => $value) {
561 // retreive any metadata
562 $meta = NucleusPlugin::getOptionMeta($o->oextra);
564 // if the option is readonly or hidden it may not be saved
565 if (($meta['access'] != 'readonly') && ($meta['access'] != 'hidden')) {
567 $value = undoMagic($value); // value comes from request
571 if (($value != 'yes') && ($value != 'no')) $value = 'no';
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
584 // decide wether we are using the contextid of newContextid
585 if ($newContextid != 0) {
586 $contextid = $newContextid;
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));
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) . "')");