3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2012 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-2012 The Nucleus Group
21 * $NucleusJP: PLUGIN.php,v 1.12.2.3 2007/12/03 02:22:42 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 _ERROR_PLUGIN_NOSUCHACTION; }
61 function doIf($key,$value) { return false; }
62 function doItemVar (&$item) {}
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
72 * 'SqlApi' -> if the plugin uses the complete sql_* api (must also require nucleuscms 3.5)
74 function supportsFeature($feature) {
79 * Report a list of plugin that is required to function
81 * @returns an array of names of plugin, an empty array indicates no dependency
83 function getPluginDep() { return array(); }
85 // these helper functions should not be redefined in your plugin
88 * Creates a new option for this plugin
91 * A string uniquely identifying your option. (max. length is 20 characters)
93 * A description that will show up in the nucleus admin area (max. length: 255 characters)
95 * Either 'text', 'yesno' or 'password'
96 * This info is used when showing 'edit plugin options' screens
98 * Initial value for the option (max. value length is 128 characters)
100 function createOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
101 return $this->_createOption('global', $name, $desc, $type, $defValue, $typeExtras);
103 function createBlogOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
104 return $this->_createOption('blog', $name, $desc, $type, $defValue, $typeExtras);
106 function createMemberOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
107 return $this->_createOption('member', $name, $desc, $type, $defValue, $typeExtras);
109 function createCategoryOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
110 return $this->_createOption('category', $name, $desc, $type, $defValue, $typeExtras);
112 function createItemOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
113 return $this->_createOption('item', $name, $desc, $type, $defValue, $typeExtras);
117 * Removes the option from the database
119 * Note: Options get erased automatically on plugin uninstall
121 function deleteOption($name) {
122 return $this->_deleteOption('global', $name);
124 function deleteBlogOption($name) {
125 return $this->_deleteOption('blog', $name);
127 function deleteMemberOption($name) {
128 return $this->_deleteOption('member', $name);
130 function deleteCategoryOption($name) {
131 return $this->_deleteOption('category', $name);
133 function deleteItemOption($name) {
134 return $this->_deleteOption('item', $name);
138 * Sets the value of an option to something new
140 function setOption($name, $value) {
141 return $this->_setOption('global', 0, $name, $value);
143 function setBlogOption($blogid, $name, $value) {
144 return $this->_setOption('blog', $blogid, $name, $value);
146 function setMemberOption($memberid, $name, $value) {
147 return $this->_setOption('member', $memberid, $name, $value);
149 function setCategoryOption($catid, $name, $value) {
150 return $this->_setOption('category', $catid, $name, $value);
152 function setItemOption($itemid, $name, $value) {
153 return $this->_setOption('item', $itemid, $name, $value);
157 * Retrieves the current value for an option
159 function getOption($name)
161 // only request the options the very first time. On subsequent requests
162 // the static collection is used to save SQL queries.
163 if ($this->plugin_options == 0)
165 $this->plugin_options = array();
167 'SELECT d.oname as name, o.ovalue as value '.
169 sql_table('plugin_option').' o, '.
170 sql_table('plugin_option_desc').' d '.
171 'WHERE d.opid='. intval($this->getID()).' AND d.oid=o.oid'
173 while ($row = sql_fetch_object($query))
174 $this->plugin_options[strtolower($row->name)] = $row->value;
176 if (isset($this->plugin_options[strtolower($name)]))
177 return $this->plugin_options[strtolower($name)];
179 return $this->_getOption('global', 0, $name);
182 function getBlogOption($blogid, $name) {
183 return $this->_getOption('blog', $blogid, $name);
185 function getMemberOption($memberid, $name) {
186 return $this->_getOption('member', $memberid, $name);
188 function getCategoryOption($catid, $name) {
189 return $this->_getOption('category', $catid, $name);
191 function getItemOption($itemid, $name) {
192 return $this->_getOption('item', $itemid, $name);
196 * Retrieves an associative array with the option value for each
199 function getAllBlogOptions($name) {
200 return $this->_getAllOptions('blog', $name);
202 function getAllMemberOptions($name) {
203 return $this->_getAllOptions('member', $name);
205 function getAllCategoryOptions($name) {
206 return $this->_getAllOptions('category', $name);
208 function getAllItemOptions($name) {
209 return $this->_getAllOptions('item', $name);
213 * Retrieves an indexed array with the top (or bottom) of an option
214 * (delegates to _getOptionTop())
216 function getBlogOptionTop($name, $amount = 10, $sort = 'desc') {
217 return $this->_getOptionTop('blog', $name, $amount, $sort);
219 function getMemberOptionTop($name, $amount = 10, $sort = 'desc') {
220 return $this->_getOptionTop('member', $name, $amount, $sort);
222 function getCategoryOptionTop($name, $amount = 10, $sort = 'desc') {
223 return $this->_getOptionTop('category', $name, $amount, $sort);
225 function getItemOptionTop($name, $amount = 10, $sort = 'desc') {
226 return $this->_getOptionTop('item', $name, $amount, $sort);
230 * Returns the plugin ID
235 return $this->plugid;
239 * Returns the URL of the admin area for this plugin (in case there's
240 * no such area, the returned information is invalid)
244 function getAdminURL() {
246 return $CONF['PluginURL'] . $this->getShortName() . '/';
250 * Returns the directory where the admin directory is located and
251 * where the plugin can maintain his extra files
255 function getDirectory() {
257 return $DIR_PLUGINS . $this->getShortName() . '/';
261 * Derives the short name for the plugin from the classname (all
266 function getShortName() {
267 return str_replace('np_','',strtolower(get_class($this)));
271 * Clears the option value cache which saves the option values during
272 * the plugin execution. This function is usefull if the options has
273 * changed during the plugin execution (especially in association with
274 * the PrePluginOptionsUpdate and the PostPluginOptionsUpdate events)
278 function clearOptionValueCache(){
279 $this->_aOptionValues = array();
280 $this->plugin_options = 0;
283 // internal functions of the class starts here
285 var $_aOptionValues; // oid_contextid => value
286 var $_aOptionToInfo; // context_name => array('oid' => ..., 'default' => ...)
287 var $plugin_options; // see getOption()
288 var $plugid; // plugin id
292 * Class constructor: Initializes some internal data
294 function NucleusPlugin() {
295 $this->_aOptionValues = array(); // oid_contextid => value
296 $this->_aOptionToInfo = array(); // context_name => array('oid' => ..., 'default' => ...)
297 $this->plugin_options = 0;
301 * Retrieves an array of the top (or bottom) of an option from a plugin.
303 * @param string $context the context for the option: item, blog, member,...
304 * @param string $name the name of the option
305 * @param int $amount how many rows must be returned
306 * @param string $sort desc or asc
307 * @return array array with both values and contextid's
310 function _getOptionTop($context, $name, $amount = 10, $sort = 'desc') {
311 if (($sort != 'desc') && ($sort != 'asc')) {
315 $oid = $this->_getOID($context, $name);
317 // retrieve the data and return
318 $q = 'SELECT otype, oextra FROM '.sql_table('plugin_option_desc').' WHERE oid = '.$oid;
319 $query = sql_query($q);
321 $o = sql_fetch_array($query);
323 if (($this->optionCanBeNumeric($o['otype'])) && ($o['oextra'] == 'number' )) {
324 $orderby = 'CAST(ovalue AS SIGNED)';
328 $q = 'SELECT ovalue value, ocontextid id FROM '.sql_table('plugin_option').' WHERE oid = '.$oid.' ORDER BY '.$orderby.' '.$sort.' LIMIT 0,'.intval($amount);
329 $query = sql_query($q);
334 while($row = sql_fetch_array($query)) {
338 // return the array (duh!)
343 * Creates an option in the database table plugin_option_desc
347 function _createOption($context, $name, $desc, $type, $defValue, $typeExtras = '') {
348 // create in plugin_option_desc
349 $query = 'INSERT INTO ' . sql_table('plugin_option_desc')
350 .' (opid, oname, ocontext, odesc, otype, odef, oextra)'
351 .' VALUES ('.intval($this->plugid)
352 .', \''.sql_real_escape_string($name).'\''
353 .', \''.sql_real_escape_string($context).'\''
354 .', \''.sql_real_escape_string($desc).'\''
355 .', \''.sql_real_escape_string($type).'\''
356 .', \''.sql_real_escape_string($defValue).'\''
357 .', \''.sql_real_escape_string($typeExtras).'\')';
359 $oid = sql_insert_id();
361 $key = $context . '_' . $name;
362 $this->_aOptionToInfo[$key] = array('oid' => $oid, 'default' => $defValue);
368 * Deletes an option from the database tables
369 * plugin_option and plugin_option_desc
373 function _deleteOption($context, $name) {
374 $oid = $this->_getOID($context, $name);
375 if (!$oid) return 0; // no such option
377 // delete all things from plugin_option
378 sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);
380 // delete entry from plugin_option_desc
381 sql_query('DELETE FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . $oid);
384 unset($this->_aOptionToInfo[$context . '_' . $name]);
385 $this->_aOptionValues = array();
390 * Update an option in the database table plugin_option
392 * returns: 1 on success, 0 on failure
395 function _setOption($context, $contextid, $name, $value) {
398 $oid = $this->_getOID($context, $name);
401 // check if context id exists
404 if (!MEMBER::existsID($contextid)) return 0;
407 if (!$manager->existsBlogID($contextid)) return 0;
410 if (!$manager->existsCategory($contextid)) return 0;
413 if (!$manager->existsItem($contextid, true, true)) return 0;
416 if ($contextid != 0) return 0;
421 // update plugin_option
422 sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid) . ' and ocontextid='. intval($contextid));
423 sql_query('INSERT INTO ' . sql_table('plugin_option') . ' (ovalue, oid, ocontextid) VALUES (\''.sql_real_escape_string($value).'\', '. intval($oid) . ', ' . intval($contextid) . ')');
426 $this->_aOptionValues[$oid . '_' . $contextid] = $value;
432 * Get an option from Cache or database
433 * - if not in the option Cache read it from the database
434 * - if not in the database write default values into the database
438 function _getOption($context, $contextid, $name) {
439 $oid = $this->_getOID($context, $name);
440 if (!$oid) return '';
443 $key = $oid . '_' . $contextid;
445 if (isset($this->_aOptionValues[$key]))
446 return $this->_aOptionValues[$key];
449 $res = sql_query('SELECT ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid).' and ocontextid=' . intval($contextid));
451 if (!$res || (sql_num_rows($res) == 0)) {
452 $defVal = $this->_getDefVal($context, $name);
453 $this->_aOptionValues[$key] = $defVal;
455 // fill DB with default value
456 $query = 'INSERT INTO ' . sql_table('plugin_option') . ' (oid,ocontextid,ovalue)'
457 .' VALUES ('.intval($oid).', '.intval($contextid).', \''.sql_real_escape_string($defVal).'\')';
461 $o = sql_fetch_object($res);
462 $this->_aOptionValues[$key] = $o->ovalue;
465 return $this->_aOptionValues[$key];
469 * Returns assoc array with all values for a given option
470 * (one option per possible context id)
474 function _getAllOptions($context, $name) {
475 $oid = $this->_getOID($context, $name);
476 if (!$oid) return array();
477 $defVal = $this->_getDefVal($context, $name);
482 $r = sql_query('SELECT bnumber as contextid FROM ' . sql_table('blog'));
485 $r = sql_query('SELECT catid as contextid FROM ' . sql_table('category'));
488 $r = sql_query('SELECT mnumber as contextid FROM ' . sql_table('member'));
491 $r = sql_query('SELECT inumber as contextid FROM ' . sql_table('item'));
495 while ($o = sql_fetch_object($r))
496 $aOptions[$o->contextid] = $defVal;
499 $res = sql_query('SELECT ocontextid, ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);
500 while ($o = sql_fetch_object($res))
501 $aOptions[$o->ocontextid] = $o->ovalue;
507 * Gets the 'option identifier' that corresponds to a given option name.
508 * When this method is called for the first time, all the OIDs for the plugin
509 * are loaded into memory, to avoid re-doing the same query all over.
511 function _getOID($context, $name) {
512 $key = $context . '_' . $name;
513 if (array_key_exists($key, $this->_aOptionToInfo)) {
514 $info = $this->_aOptionToInfo[$key];
515 if (is_array($info)) return $info['oid'];
518 // load all OIDs for this plugin from the database
519 $this->_aOptionToInfo = array();
520 $query = 'SELECT oid, oname, ocontext, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE opid=' . intval($this->plugid);
521 $res = sql_query($query);
522 while ($o = sql_fetch_object($res)) {
523 $k = $o->ocontext . '_' . $o->oname;
524 $this->_aOptionToInfo[$k] = array('oid' => $o->oid, 'default' => $o->odef);
526 sql_free_result($res);
528 if (array_key_exists($key, $this->_aOptionToInfo)) {
529 return $this->_aOptionToInfo[$key]['oid'];
535 function _getDefVal($context, $name) {
536 $key = $context . '_' . $name;
537 $info = $this->_aOptionToInfo[$key];
538 if (is_array($info)) return $info['default'];
543 * Deletes all option values for a given context and contextid
544 * (used when e.g. a blog, member or category is deleted)
548 function _deleteOptionValues($context, $contextid) {
549 // delete all associated plugin options
552 $query = 'SELECT oid FROM '.sql_table('plugin_option_desc') . ' WHERE ocontext=\''.sql_real_escape_string($context).'\'';
553 $res = sql_query($query);
554 while ($o = sql_fetch_object($res))
555 array_push($aOIDs, $o->oid);
556 sql_free_result($res);
557 // delete those options. go go go
558 if (count($aOIDs) > 0) {
559 $query = 'DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid in ('.implode(',',$aOIDs).') and ocontextid=' . intval($contextid);
565 * splits the option's typeextra field (at ;'s) to split the meta collection
566 * @param string $typeExtra the value of the typeExtra field of an option
567 * @return array array of the meta-key/value-pairs
571 function getOptionMeta($typeExtra) {
572 $tmpMeta = explode(';', $typeExtra);
574 for ($i = 0; $i < count($tmpMeta); $i++) {
575 if (($i == 0) && (!strstr($tmpMeta[0], '='))) {
576 // we have the select-list
577 $meta['select'] = $tmpMeta[0];
579 $tmp = explode('=', $tmpMeta[$i]);
580 $meta[$tmp[0]] = $tmp[1];
587 * filters the selectlists out of the meta collection
588 * @param string $typeExtra the value of the typeExtra field of an option
589 * @return string the selectlist
592 function getOptionSelectValues($typeExtra) {
593 $meta = NucleusPlugin::getOptionMeta($typeExtra);
594 //the select list must always be the first part
595 return $meta['select'];
599 * checks if the eventlist in the database is up-to-date
600 * @return bool if it is up-to-date it return true, else false
603 function subscribtionListIsUptodate() {
604 $res = sql_query('SELECT event FROM '.sql_table('plugin_event').' WHERE pid = '.$this->getID());
606 while($a = sql_fetch_array($res)) {
607 array_push($ev, $a['event']);
609 if (count($ev) != count($this->getEventList())) {
612 $d = array_diff($ev, $this->getEventList());
614 // there are differences so the db is not up-to-date
621 * @param $aOptions: array ( 'oid' => array( 'contextid' => 'value'))
622 * (taken from request using requestVar())
623 * @param $newContextid: integer (accepts a contextid when it is for a new
624 * contextid there was no id available at the moment of writing the
625 * formcontrols into the page (by ex: itemOptions for new item)
628 function _applyPluginOptions(&$aOptions, $newContextid = 0) {
630 if (!is_array($aOptions)) return;
632 foreach ($aOptions as $oid => $values) {
634 // get option type info
635 $query = 'SELECT opid, oname, ocontext, otype, oextra, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . intval($oid);
636 $res = sql_query($query);
637 if ($o = sql_fetch_object($res))
639 foreach ($values as $key => $value) {
640 // avoid overriding the key used by foreach statement
643 // retreive any metadata
644 $meta = NucleusPlugin::getOptionMeta($o->oextra);
646 // if the option is readonly or hidden it may not be saved
647 if (!array_key_exists('access', $meta) || (($meta['access'] != 'readonly') && ($meta['access'] != 'hidden'))) {
649 $value = undoMagic($value); // value comes from request
653 if (($value != 'yes') && ($value != 'no')) $value = 'no';
659 // check the validity of numerical options
660 if (array_key_exists('datatype', $meta) && ($meta['datatype'] == 'numerical') && (!is_numeric($value))) {
661 //the option must be numeric, but the it isn't
662 //use the default for this option
666 // decide wether we are using the contextid of newContextid
667 if ($newContextid != 0) {
668 $contextid = $newContextid;
671 //trigger event PrePluginOptionsUpdate to give the plugin the
672 //possibility to change/validate the new value for the option
674 'context' => $o->ocontext,
675 'plugid' => $o->opid,
676 'optionname' => $o->oname,
677 'contextid' => $contextid,
680 $manager->notify('PrePluginOptionsUpdate', $param);
682 // delete the old value for the option
683 sql_query('DELETE FROM '.sql_table('plugin_option').' WHERE oid='.intval($oid).' AND ocontextid='.intval($contextid));
684 sql_query('INSERT INTO '.sql_table('plugin_option')." (oid, ocontextid, ovalue) VALUES (".intval($oid).",".intval($contextid).",'" . sql_real_escape_string($value) . "')");
688 // clear option value cache if the plugin object is already loaded
690 $plugin=& $manager->pidLoaded($o->opid);
691 if ($plugin) $plugin->clearOptionValueCache();