3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2009 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-2009 The Nucleus Group
20 * @version $Id: PLUGIN.php 1629 2012-01-28 09:52:25Z sakamocchi $
24 // these functions _have_ to be redefined in your plugin
45 function getDescription()
50 // these function _may_ be redefined in your plugin
52 function getMinNucleusVersion()
57 function getMinNucleusPatchLevel()
62 function getEventList()
67 function getTableList()
72 function hasAdminArea()
89 function doSkinVar($skinType)
93 function doTemplateVar(&$item)
95 $args = func_get_args();
97 array_unshift($args, 'template');
98 call_user_func_array(array(&$this,'doSkinVar'),$args);
101 function doTemplateCommentsVar(&$item, &$comment)
103 $args = func_get_args();
106 array_unshift($args, 'template');
107 call_user_func_array(array(&$this,'doSkinVar'),$args);
110 function doAction($type)
112 return _ERROR_PLUGIN_NOSUCHACTION;
115 function doIf($key,$value)
120 function doItemVar (&$item)
125 * Checks if a plugin supports a certain feature.
127 * @returns 1 if the feature is reported, 0 if not
129 * Name of the feature. See plugin documentation for more info
130 * 'SqlTablePrefix' -> if the plugin uses the sql_table() method to get table names
131 * 'HelpPage' -> if the plugin provides a helppage
132 * 'SqlApi' -> if the plugin uses the complete sql_* api (must also require nucleuscms 3.5)
134 function supportsFeature($feature)
140 * Report a list of plugin that is required to function
142 * @returns an array of names of plugin, an empty array indicates no dependency
144 function getPluginDep()
149 // these helper functions should not be redefined in your plugin
152 * Creates a new option for this plugin
155 * A string uniquely identifying your option. (max. length is 20 characters)
157 * A description that will show up in the nucleus admin area (max. length: 255 characters)
159 * Either 'text', 'yesno' or 'password'
160 * This info is used when showing 'edit plugin options' screens
162 * Initial value for the option (max. value length is 128 characters)
164 function createOption($name, $desc, $type, $defValue = '', $typeExtras = '')
166 return $this->_createOption('global', $name, $desc, $type, $defValue, $typeExtras);
169 function createBlogOption($name, $desc, $type, $defValue = '', $typeExtras = '')
171 return $this->_createOption('blog', $name, $desc, $type, $defValue, $typeExtras);
174 function createMemberOption($name, $desc, $type, $defValue = '', $typeExtras = '')
176 return $this->_createOption('member', $name, $desc, $type, $defValue, $typeExtras);
179 function createCategoryOption($name, $desc, $type, $defValue = '', $typeExtras = '')
181 return $this->_createOption('category', $name, $desc, $type, $defValue, $typeExtras);
184 function createItemOption($name, $desc, $type, $defValue = '', $typeExtras = '')
186 return $this->_createOption('item', $name, $desc, $type, $defValue, $typeExtras);
190 * Removes the option from the database
192 * Note: Options get erased automatically on plugin uninstall
194 function deleteOption($name)
196 return $this->_deleteOption('global', $name);
199 function deleteBlogOption($name)
201 return $this->_deleteOption('blog', $name);
204 function deleteMemberOption($name)
206 return $this->_deleteOption('member', $name);
209 function deleteCategoryOption($name)
211 return $this->_deleteOption('category', $name);
214 function deleteItemOption($name)
216 return $this->_deleteOption('item', $name);
220 * Sets the value of an option to something new
222 function setOption($name, $value)
224 return $this->_setOption('global', 0, $name, $value);
227 function setBlogOption($blogid, $name, $value)
229 return $this->_setOption('blog', $blogid, $name, $value);
232 function setMemberOption($memberid, $name, $value)
234 return $this->_setOption('member', $memberid, $name, $value);
237 function setCategoryOption($catid, $name, $value)
239 return $this->_setOption('category', $catid, $name, $value);
242 function setItemOption($itemid, $name, $value) {
243 return $this->_setOption('item', $itemid, $name, $value);
247 * Retrieves the current value for an option
249 function getOption($name)
251 // only request the options the very first time. On subsequent requests
252 // the static collection is used to save SQL queries.
253 if ( $this->plugin_options == 0 )
255 $this->plugin_options = array();
257 'SELECT d.oname as name, o.ovalue as value '.
259 sql_table('plugin_option').' o, '.
260 sql_table('plugin_option_desc').' d '.
261 'WHERE d.opid='. intval($this->getID()).' AND d.oid=o.oid'
263 while ( $row = sql_fetch_object($query) )
265 $this->plugin_options[strtolower($row->name)] = $row->value;
268 if ( isset($this->plugin_options[strtolower($name)]) )
270 return $this->plugin_options[strtolower($name)];
274 return $this->_getOption('global', 0, $name);
278 function getBlogOption($blogid, $name)
280 return $this->_getOption('blog', $blogid, $name);
283 function getMemberOption($memberid, $name)
285 return $this->_getOption('member', $memberid, $name);
288 function getCategoryOption($catid, $name)
290 return $this->_getOption('category', $catid, $name);
293 function getItemOption($itemid, $name)
295 return $this->_getOption('item', $itemid, $name);
299 * Retrieves an associative array with the option value for each
302 function getAllBlogOptions($name)
304 return $this->_getAllOptions('blog', $name);
307 function getAllMemberOptions($name)
309 return $this->_getAllOptions('member', $name);
312 function getAllCategoryOptions($name)
314 return $this->_getAllOptions('category', $name);
317 function getAllItemOptions($name)
319 return $this->_getAllOptions('item', $name);
323 * Retrieves an indexed array with the top (or bottom) of an option
324 * (delegates to _getOptionTop())
326 function getBlogOptionTop($name, $amount = 10, $sort = 'desc')
328 return $this->_getOptionTop('blog', $name, $amount, $sort);
331 function getMemberOptionTop($name, $amount = 10, $sort = 'desc')
333 return $this->_getOptionTop('member', $name, $amount, $sort);
336 function getCategoryOptionTop($name, $amount = 10, $sort = 'desc')
338 return $this->_getOptionTop('category', $name, $amount, $sort);
341 function getItemOptionTop($name, $amount = 10, $sort = 'desc')
343 return $this->_getOptionTop('item', $name, $amount, $sort);
347 * Returns the plugin ID
353 return $this->plugid;
357 * Returns the URL of the admin area for this plugin (in case there's
358 * no such area, the returned information is invalid)
362 function getAdminURL()
365 return $CONF['PluginURL'] . $this->getShortName() . '/';
369 * Returns the directory where the admin directory is located and
370 * where the plugin can maintain his extra files
374 function getDirectory()
377 return $DIR_PLUGINS . $this->getShortName() . '/';
381 * Derives the short name for the plugin from the classname (all
386 function getShortName()
388 return str_replace('np_','',strtolower(get_class($this)));
392 * Clears the option value cache which saves the option values during
393 * the plugin execution. This function is usefull if the options has
394 * changed during the plugin execution (especially in association with
395 * the PrePluginOptionsUpdate and the PostPluginOptionsUpdate events)
399 function clearOptionValueCache()
401 $this->_aOptionValues = array();
402 $this->plugin_options = 0;
405 // internal functions of the class starts here
407 var $_aOptionValues; // oid_contextid => value
408 var $_aOptionToInfo; // context_name => array('oid' => ..., 'default' => ...)
409 var $plugin_options; // see getOption()
410 var $plugid; // plugin id
413 * Class constructor: Initializes some internal data
415 function NucleusPlugin()
417 $this->_aOptionValues = array(); // oid_contextid => value
418 $this->_aOptionToInfo = array(); // context_name => array('oid' => ..., 'default' => ...)
419 $this->plugin_options = 0;
423 * Retrieves an array of the top (or bottom) of an option from a plugin.
425 * @param string $context the context for the option: item, blog, member,...
426 * @param string $name the name of the option
427 * @param int $amount how many rows must be returned
428 * @param string $sort desc or asc
429 * @return array array with both values and contextid's
432 function _getOptionTop($context, $name, $amount = 10, $sort = 'desc')
434 if ( ($sort != 'desc') && ($sort != 'asc') )
439 $oid = $this->_getOID($context, $name);
441 // retrieve the data and return
442 $q = 'SELECT otype, oextra FROM '.sql_table('plugin_option_desc').' WHERE oid = '.$oid;
443 $query = sql_query($q);
445 $o = sql_fetch_array($query);
447 if ( ($this->optionCanBeNumeric($o['otype'])) && ($o['oextra'] == 'number' ) )
449 $orderby = 'CAST(ovalue AS SIGNED)';
455 $q = 'SELECT ovalue value, ocontextid id FROM '.sql_table('plugin_option').' WHERE oid = '.$oid.' ORDER BY '.$orderby.' '.$sort.' LIMIT 0,'.intval($amount);
456 $query = sql_query($q);
461 while( $row = sql_fetch_array($query) )
466 // return the array (duh!)
471 * Creates an option in the database table plugin_option_desc
475 function _createOption($context, $name, $desc, $type, $defValue, $typeExtras = '')
477 // create in plugin_option_desc
478 $query = 'INSERT INTO ' . sql_table('plugin_option_desc')
479 .' (opid, oname, ocontext, odesc, otype, odef, oextra)'
480 .' VALUES ('.intval($this->plugid)
481 .', \''.sql_real_escape_string($name).'\''
482 .', \''.sql_real_escape_string($context).'\''
483 .', \''.sql_real_escape_string($desc).'\''
484 .', \''.sql_real_escape_string($type).'\''
485 .', \''.sql_real_escape_string($defValue).'\''
486 .', \''.sql_real_escape_string($typeExtras).'\')';
488 $oid = sql_insert_id();
490 $key = $context . '_' . $name;
491 $this->_aOptionToInfo[$key] = array('oid' => $oid, 'default' => $defValue);
496 * Deletes an option from the database tables
497 * plugin_option and plugin_option_desc
501 function _deleteOption($context, $name)
503 $oid = $this->_getOID($context, $name);
506 return 0; // no such option
509 // delete all things from plugin_option
510 sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);
512 // delete entry from plugin_option_desc
513 sql_query('DELETE FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . $oid);
516 unset($this->_aOptionToInfo[$context . '_' . $name]);
517 $this->_aOptionValues = array();
522 * Update an option in the database table plugin_option
524 * returns: 1 on success, 0 on failure
527 function _setOption($context, $contextid, $name, $value)
531 $oid = $this->_getOID($context, $name);
537 // check if context id exists
541 if ( !MEMBER::existsID($contextid) )
547 if ( !$manager->existsBlogID($contextid) )
553 if ( !$manager->existsCategory($contextid) )
559 if ( !$manager->existsItem($contextid, true, true) )
565 if ( $contextid != 0 )
572 // update plugin_option
573 sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid) . ' and ocontextid='. intval($contextid));
574 sql_query('INSERT INTO ' . sql_table('plugin_option') . ' (ovalue, oid, ocontextid) VALUES (\''.sql_real_escape_string($value).'\', '. intval($oid) . ', ' . intval($contextid) . ')');
577 $this->_aOptionValues[$oid . '_' . $contextid] = $value;
578 if ( $context == 'global' )
580 $this->plugin_options[strtolower($name)] = $value;
587 * Get an option from Cache or database
588 * - if not in the option Cache read it from the database
589 * - if not in the database write default values into the database
593 function _getOption($context, $contextid, $name)
595 $oid = $this->_getOID($context, $name);
601 $key = $oid . '_' . $contextid;
603 if ( isset($this->_aOptionValues[$key]) )
605 return $this->_aOptionValues[$key];
609 $res = sql_query('SELECT ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid).' and ocontextid=' . intval($contextid));
611 if ( !$res || (sql_num_rows($res) == 0) )
613 $defVal = $this->_getDefVal($context, $name);
614 $this->_aOptionValues[$key] = $defVal;
616 // fill DB with default value
617 $query = 'INSERT INTO ' . sql_table('plugin_option') . ' (oid,ocontextid,ovalue)'
618 .' VALUES ('.intval($oid).', '.intval($contextid).', \''.sql_real_escape_string($defVal).'\')';
623 $o = sql_fetch_object($res);
624 $this->_aOptionValues[$key] = $o->ovalue;
627 return $this->_aOptionValues[$key];
631 * Returns assoc array with all values for a given option
632 * (one option per possible context id)
636 function _getAllOptions($context, $name)
638 $oid = $this->_getOID($context, $name);
643 $defVal = $this->_getDefVal($context, $name);
649 $r = sql_query('SELECT bnumber as contextid FROM ' . sql_table('blog'));
652 $r = sql_query('SELECT catid as contextid FROM ' . sql_table('category'));
655 $r = sql_query('SELECT mnumber as contextid FROM ' . sql_table('member'));
658 $r = sql_query('SELECT inumber as contextid FROM ' . sql_table('item'));
663 while ( $o = sql_fetch_object($r) )
665 $aOptions[$o->contextid] = $defVal;
669 $res = sql_query('SELECT ocontextid, ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);
670 while ( $o = sql_fetch_object($res) )
672 $aOptions[$o->ocontextid] = $o->ovalue;
679 * Gets the 'option identifier' that corresponds to a given option name.
680 * When this method is called for the first time, all the OIDs for the plugin
681 * are loaded into memory, to avoid re-doing the same query all over.
683 function _getOID($context, $name)
685 $key = $context . '_' . $name;
686 $info = $this->_aOptionToInfo[$key];
687 if ( is_array($info) )
692 // load all OIDs for this plugin from the database
693 $this->_aOptionToInfo = array();
694 $query = 'SELECT oid, oname, ocontext, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE opid=' . intval($this->plugid);
695 $res = sql_query($query);
696 while ( $o = sql_fetch_object($res) )
698 $k = $o->ocontext . '_' . $o->oname;
699 $this->_aOptionToInfo[$k] = array('oid' => $o->oid, 'default' => $o->odef);
701 sql_free_result($res);
703 return $this->_aOptionToInfo[$key]['oid'];
705 function _getDefVal($context, $name)
707 $key = $context . '_' . $name;
708 $info = $this->_aOptionToInfo[$key];
709 if ( is_array($info) )
711 return $info['default'];
716 * Deletes all option values for a given context and contextid
717 * (used when e.g. a blog, member or category is deleted)
721 function _deleteOptionValues($context, $contextid)
723 // delete all associated plugin options
726 $query = 'SELECT oid FROM '.sql_table('plugin_option_desc') . ' WHERE ocontext=\''.sql_real_escape_string($context).'\'';
727 $res = sql_query($query);
728 while ( $o = sql_fetch_object($res) )
730 array_push($aOIDs, $o->oid);
732 sql_free_result($res);
733 // delete those options. go go go
734 if ( count($aOIDs) > 0 )
736 $query = 'DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid in ('.implode(',',$aOIDs).') and ocontextid=' . intval($contextid);
742 * splits the option's typeextra field (at ;'s) to split the meta collection
743 * @param string $typeExtra the value of the typeExtra field of an option
744 * @return array array of the meta-key/value-pairs
748 function getOptionMeta($typeExtra)
750 $tmpMeta = i18n::explode(';', $typeExtra);
752 for ( $i = 0; $i < count($tmpMeta); $i++ )
754 if ( ($i == 0) && (!strstr($tmpMeta[0], '=')) )
756 // we have the select-list
757 $meta['select'] = $tmpMeta[0];
761 $tmp = i18n::explode('=', $tmpMeta[$i]);
762 $meta[$tmp[0]] = $tmp[1];
769 * filters the selectlists out of the meta collection
770 * @param string $typeExtra the value of the typeExtra field of an option
771 * @return string the selectlist
774 function getOptionSelectValues($typeExtra)
776 $meta = NucleusPlugin::getOptionMeta($typeExtra);
777 //the select list must always be the first part
778 return $meta['select'];
782 * checks if the eventlist in the database is up-to-date
783 * @return bool if it is up-to-date it return true, else false
786 function subscribtionListIsUptodate()
788 $res = sql_query('SELECT event FROM '.sql_table('plugin_event').' WHERE pid = '.$this->getID());
790 while( $a = sql_fetch_array($res) )
792 array_push($ev, $a['event']);
794 if ( count($ev) != count($this->getEventList()) )
798 $d = array_diff($ev, $this->getEventList());
801 // there are differences so the db is not up-to-date
808 * @param $aOptions: array ( 'oid' => array( 'contextid' => 'value'))
809 * (taken from request using requestVar())
810 * @param $newContextid: integer (accepts a contextid when it is for a new
811 * contextid there was no id available at the moment of writing the
812 * formcontrols into the page (by ex: itemOptions for new item)
815 function _applyPluginOptions(&$aOptions, $newContextid = 0)
818 if ( !is_array($aOptions) )
823 foreach ( $aOptions as $oid => $values )
826 // get option type info
827 $query = 'SELECT opid, oname, ocontext, otype, oextra, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . intval($oid);
828 $res = sql_query($query);
829 if ( $o = sql_fetch_object($res) )
831 foreach ( $values as $key => $value )
833 // avoid overriding the key used by foreach statement
836 // retreive any metadata
837 $meta = NucleusPlugin::getOptionMeta($o->oextra);
839 // if the option is readonly or hidden it may not be saved
840 if ( ($meta['access'] != 'readonly') && ($meta['access'] != 'hidden') )
843 $value = undoMagic($value); // value comes from request
848 if ( ($value != 'yes') && ($value != 'no') )
857 // check the validity of numerical options
858 if ( ($meta['datatype'] == 'numerical') && (!is_numeric($value)) )
860 //the option must be numeric, but the it isn't
861 //use the default for this option
865 // decide wether we are using the contextid of newContextid
866 if ( $newContextid != 0 )
868 $contextid = $newContextid;
871 //trigger event PrePluginOptionsUpdate to give the plugin the
872 //possibility to change/validate the new value for the option
873 $manager->notify('PrePluginOptionsUpdate',array('context' => $o->ocontext, 'plugid' => $o->opid, 'optionname' => $o->oname, 'contextid' => $contextid, 'value' => &$value));
875 // delete the old value for the option
876 sql_query('DELETE FROM '.sql_table('plugin_option').' WHERE oid='.intval($oid).' AND ocontextid='.intval($contextid));
877 sql_query('INSERT INTO '.sql_table('plugin_option')." (oid, ocontextid, ovalue) VALUES (".intval($oid).",".intval($contextid).",'" . sql_real_escape_string($value) . "')");
881 // clear option value cache if the plugin object is already loaded
884 $plugin=& $manager->pidLoaded($o->opid);
887 $plugin->clearOptionValueCache();