-<?php
-/*
- * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
- * Copyright (C) 2002-2009 The Nucleus Group
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- * (see nucleus/documentation/index.html#license for more info)
- */
-/**
- * This is an (abstract) class of which all Nucleus Plugins must inherit
- *
- * for more information on plugins and how to write your own, see the
- * plugins.html file that is included with the Nucleus documenation
- *
- * @license http://nucleuscms.org/license.txt GNU General Public License
- * @copyright Copyright (C) 2002-2009 The Nucleus Group
- * @version $Id: PLUGIN.php 1629 2012-01-28 09:52:25Z sakamocchi $
- */
-class NucleusPlugin
-{
- // these functions _have_ to be redefined in your plugin
- function getName()
- {
- return 'Undefined';
- }
-
- function getAuthor()
- {
- return 'Undefined';
- }
-
- function getURL()
- {
- return 'Undefined';
- }
-
- function getVersion()
- {
- return '0.0';
- }
-
- function getDescription()
- {
- return 'Undefined';
- }
-
- // these function _may_ be redefined in your plugin
-
- function getMinNucleusVersion()
- {
- return 150;
- }
-
- function getMinNucleusPatchLevel()
- {
- return 0;
- }
-
- function getEventList()
- {
- return array();
- }
-
- function getTableList()
- {
- return array();
- }
-
- function hasAdminArea()
- {
- return 0;
- }
-
- function install()
- {
- }
-
- function unInstall()
- {
- }
-
- function init()
- {
- }
-
- function doSkinVar($skinType)
- {
- }
-
- function doTemplateVar(&$item)
- {
- $args = func_get_args();
- array_shift($args);
- array_unshift($args, 'template');
- call_user_func_array(array(&$this,'doSkinVar'),$args);
- }
-
- function doTemplateCommentsVar(&$item, &$comment)
- {
- $args = func_get_args();
- array_shift($args);
- array_shift($args);
- array_unshift($args, 'template');
- call_user_func_array(array(&$this,'doSkinVar'),$args);
- }
-
- function doAction($type)
- {
- return _ERROR_PLUGIN_NOSUCHACTION;
- }
-
- function doIf($key,$value)
- {
- return false;
- }
-
- function doItemVar (&$item)
- {
- }
-
- /**
- * Checks if a plugin supports a certain feature.
- *
- * @returns 1 if the feature is reported, 0 if not
- * @param $feature
- * Name of the feature. See plugin documentation for more info
- * 'SqlTablePrefix' -> if the plugin uses the sql_table() method to get table names
- * 'HelpPage' -> if the plugin provides a helppage
- * 'SqlApi' -> if the plugin uses the complete sql_* api (must also require nucleuscms 3.5)
- */
- function supportsFeature($feature)
- {
- return 0;
- }
-
- /**
- * Report a list of plugin that is required to function
- *
- * @returns an array of names of plugin, an empty array indicates no dependency
- */
- function getPluginDep()
- {
- return array();
- }
-
- // these helper functions should not be redefined in your plugin
-
- /**
- * Creates a new option for this plugin
- *
- * @param name
- * A string uniquely identifying your option. (max. length is 20 characters)
- * @param description
- * A description that will show up in the nucleus admin area (max. length: 255 characters)
- * @param type
- * Either 'text', 'yesno' or 'password'
- * This info is used when showing 'edit plugin options' screens
- * @param value
- * Initial value for the option (max. value length is 128 characters)
- */
- function createOption($name, $desc, $type, $defValue = '', $typeExtras = '')
- {
- return $this->_createOption('global', $name, $desc, $type, $defValue, $typeExtras);
- }
-
- function createBlogOption($name, $desc, $type, $defValue = '', $typeExtras = '')
- {
- return $this->_createOption('blog', $name, $desc, $type, $defValue, $typeExtras);
- }
-
- function createMemberOption($name, $desc, $type, $defValue = '', $typeExtras = '')
- {
- return $this->_createOption('member', $name, $desc, $type, $defValue, $typeExtras);
- }
-
- function createCategoryOption($name, $desc, $type, $defValue = '', $typeExtras = '')
- {
- return $this->_createOption('category', $name, $desc, $type, $defValue, $typeExtras);
- }
-
- function createItemOption($name, $desc, $type, $defValue = '', $typeExtras = '')
- {
- return $this->_createOption('item', $name, $desc, $type, $defValue, $typeExtras);
- }
-
- /**
- * Removes the option from the database
- *
- * Note: Options get erased automatically on plugin uninstall
- */
- function deleteOption($name)
- {
- return $this->_deleteOption('global', $name);
- }
-
- function deleteBlogOption($name)
- {
- return $this->_deleteOption('blog', $name);
- }
-
- function deleteMemberOption($name)
- {
- return $this->_deleteOption('member', $name);
- }
-
- function deleteCategoryOption($name)
- {
- return $this->_deleteOption('category', $name);
- }
-
- function deleteItemOption($name)
- {
- return $this->_deleteOption('item', $name);
- }
-
- /**
- * Sets the value of an option to something new
- */
- function setOption($name, $value)
- {
- return $this->_setOption('global', 0, $name, $value);
- }
-
- function setBlogOption($blogid, $name, $value)
- {
- return $this->_setOption('blog', $blogid, $name, $value);
- }
-
- function setMemberOption($memberid, $name, $value)
- {
- return $this->_setOption('member', $memberid, $name, $value);
- }
-
- function setCategoryOption($catid, $name, $value)
- {
- return $this->_setOption('category', $catid, $name, $value);
- }
-
- function setItemOption($itemid, $name, $value) {
- return $this->_setOption('item', $itemid, $name, $value);
- }
-
- /**
- * Retrieves the current value for an option
- */
- function getOption($name)
- {
- // only request the options the very first time. On subsequent requests
- // the static collection is used to save SQL queries.
- if ( $this->plugin_options == 0 )
- {
- $this->plugin_options = array();
- $query = sql_query(
- 'SELECT d.oname as name, o.ovalue as value '.
- 'FROM '.
- sql_table('plugin_option').' o, '.
- sql_table('plugin_option_desc').' d '.
- 'WHERE d.opid='. intval($this->getID()).' AND d.oid=o.oid'
- );
- while ( $row = sql_fetch_object($query) )
- {
- $this->plugin_options[strtolower($row->name)] = $row->value;
- }
- }
- if ( isset($this->plugin_options[strtolower($name)]) )
- {
- return $this->plugin_options[strtolower($name)];
- }
- else
- {
- return $this->_getOption('global', 0, $name);
- }
- }
-
- function getBlogOption($blogid, $name)
- {
- return $this->_getOption('blog', $blogid, $name);
- }
-
- function getMemberOption($memberid, $name)
- {
- return $this->_getOption('member', $memberid, $name);
- }
-
- function getCategoryOption($catid, $name)
- {
- return $this->_getOption('category', $catid, $name);
- }
-
- function getItemOption($itemid, $name)
- {
- return $this->_getOption('item', $itemid, $name);
- }
-
- /**
- * Retrieves an associative array with the option value for each
- * context id
- */
- function getAllBlogOptions($name)
- {
- return $this->_getAllOptions('blog', $name);
- }
-
- function getAllMemberOptions($name)
- {
- return $this->_getAllOptions('member', $name);
- }
-
- function getAllCategoryOptions($name)
- {
- return $this->_getAllOptions('category', $name);
- }
-
- function getAllItemOptions($name)
- {
- return $this->_getAllOptions('item', $name);
- }
-
- /**
- * Retrieves an indexed array with the top (or bottom) of an option
- * (delegates to _getOptionTop())
- */
- function getBlogOptionTop($name, $amount = 10, $sort = 'desc')
- {
- return $this->_getOptionTop('blog', $name, $amount, $sort);
- }
-
- function getMemberOptionTop($name, $amount = 10, $sort = 'desc')
- {
- return $this->_getOptionTop('member', $name, $amount, $sort);
- }
-
- function getCategoryOptionTop($name, $amount = 10, $sort = 'desc')
- {
- return $this->_getOptionTop('category', $name, $amount, $sort);
- }
-
- function getItemOptionTop($name, $amount = 10, $sort = 'desc')
- {
- return $this->_getOptionTop('item', $name, $amount, $sort);
- }
-
- /**
- * Returns the plugin ID
- *
- * public
- */
- function getID()
- {
- return $this->plugid;
- }
-
- /**
- * Returns the URL of the admin area for this plugin (in case there's
- * no such area, the returned information is invalid)
- *
- * public
- */
- function getAdminURL()
- {
- global $CONF;
- return $CONF['PluginURL'] . $this->getShortName() . '/';
- }
-
- /**
- * Returns the directory where the admin directory is located and
- * where the plugin can maintain his extra files
- *
- * public
- */
- function getDirectory()
- {
- global $DIR_PLUGINS;
- return $DIR_PLUGINS . $this->getShortName() . '/';
- }
-
- /**
- * Derives the short name for the plugin from the classname (all
- * lowercase)
- *
- * public
- */
- function getShortName()
- {
- return str_replace('np_','',strtolower(get_class($this)));
- }
-
- /**
- * Clears the option value cache which saves the option values during
- * the plugin execution. This function is usefull if the options has
- * changed during the plugin execution (especially in association with
- * the PrePluginOptionsUpdate and the PostPluginOptionsUpdate events)
- *
- * public
- **/
- function clearOptionValueCache()
- {
- $this->_aOptionValues = array();
- $this->plugin_options = 0;
- }
-
- // internal functions of the class starts here
-
- var $_aOptionValues; // oid_contextid => value
- var $_aOptionToInfo; // context_name => array('oid' => ..., 'default' => ...)
- var $plugin_options; // see getOption()
- var $plugid; // plugin id
-
- /**
- * Class constructor: Initializes some internal data
- */
- function NucleusPlugin()
- {
- $this->_aOptionValues = array(); // oid_contextid => value
- $this->_aOptionToInfo = array(); // context_name => array('oid' => ..., 'default' => ...)
- $this->plugin_options = 0;
- }
-
- /**
- * Retrieves an array of the top (or bottom) of an option from a plugin.
- * @author TeRanEX
- * @param string $context the context for the option: item, blog, member,...
- * @param string $name the name of the option
- * @param int $amount how many rows must be returned
- * @param string $sort desc or asc
- * @return array array with both values and contextid's
- * @access private
- */
- function _getOptionTop($context, $name, $amount = 10, $sort = 'desc')
- {
- if ( ($sort != 'desc') && ($sort != 'asc') )
- {
- $sort= 'desc';
- }
-
- $oid = $this->_getOID($context, $name);
-
- // retrieve the data and return
- $q = 'SELECT otype, oextra FROM '.sql_table('plugin_option_desc').' WHERE oid = '.$oid;
- $query = sql_query($q);
-
- $o = sql_fetch_array($query);
-
- if ( ($this->optionCanBeNumeric($o['otype'])) && ($o['oextra'] == 'number' ) )
- {
- $orderby = 'CAST(ovalue AS SIGNED)';
- }
- else
- {
- $orderby = 'ovalue';
- }
- $q = 'SELECT ovalue value, ocontextid id FROM '.sql_table('plugin_option').' WHERE oid = '.$oid.' ORDER BY '.$orderby.' '.$sort.' LIMIT 0,'.intval($amount);
- $query = sql_query($q);
-
- // create the array
- $i = 0;
- $top = array();
- while( $row = sql_fetch_array($query) )
- {
- $top[$i++] = $row;
- }
-
- // return the array (duh!)
- return $top;
- }
-
- /**
- * Creates an option in the database table plugin_option_desc
- *
- * private
- */
- function _createOption($context, $name, $desc, $type, $defValue, $typeExtras = '')
- {
- // create in plugin_option_desc
- $query = 'INSERT INTO ' . sql_table('plugin_option_desc')
- .' (opid, oname, ocontext, odesc, otype, odef, oextra)'
- .' VALUES ('.intval($this->plugid)
- .', \''.sql_real_escape_string($name).'\''
- .', \''.sql_real_escape_string($context).'\''
- .', \''.sql_real_escape_string($desc).'\''
- .', \''.sql_real_escape_string($type).'\''
- .', \''.sql_real_escape_string($defValue).'\''
- .', \''.sql_real_escape_string($typeExtras).'\')';
- sql_query($query);
- $oid = sql_insert_id();
-
- $key = $context . '_' . $name;
- $this->_aOptionToInfo[$key] = array('oid' => $oid, 'default' => $defValue);
- return 1;
- }
-
- /**
- * Deletes an option from the database tables
- * plugin_option and plugin_option_desc
- *
- * private
- */
- function _deleteOption($context, $name)
- {
- $oid = $this->_getOID($context, $name);
- if ( !$oid )
- {
- return 0; // no such option
- }
-
- // delete all things from plugin_option
- sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);
-
- // delete entry from plugin_option_desc
- sql_query('DELETE FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . $oid);
-
- // clear from cache
- unset($this->_aOptionToInfo[$context . '_' . $name]);
- $this->_aOptionValues = array();
- return 1;
- }
-
- /**
- * Update an option in the database table plugin_option
- *
- * returns: 1 on success, 0 on failure
- * private
- */
- function _setOption($context, $contextid, $name, $value)
- {
- global $manager;
-
- $oid = $this->_getOID($context, $name);
- if ( !$oid )
- {
- return 0;
- }
-
- // check if context id exists
- switch ( $context )
- {
- case 'member':
- if ( !MEMBER::existsID($contextid) )
- {
- return 0;
- }
- break;
- case 'blog':
- if ( !$manager->existsBlogID($contextid) )
- {
- return 0;
- }
- break;
- case 'category':
- if ( !$manager->existsCategory($contextid) )
- {
- return 0;
- }
- break;
- case 'item':
- if ( !$manager->existsItem($contextid, true, true) )
- {
- return 0;
- }
- break;
- case 'global':
- if ( $contextid != 0 )
- {
- return 0;
- }
- break;
- }
-
- // update plugin_option
- sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid) . ' and ocontextid='. intval($contextid));
- sql_query('INSERT INTO ' . sql_table('plugin_option') . ' (ovalue, oid, ocontextid) VALUES (\''.sql_real_escape_string($value).'\', '. intval($oid) . ', ' . intval($contextid) . ')');
-
- // update cache
- $this->_aOptionValues[$oid . '_' . $contextid] = $value;
- if ( $context == 'global' )
- {
- $this->plugin_options[strtolower($name)] = $value;
- }
-
- return 1;
- }
-
- /**
- * Get an option from Cache or database
- * - if not in the option Cache read it from the database
- * - if not in the database write default values into the database
- *
- * private
- */
- function _getOption($context, $contextid, $name)
- {
- $oid = $this->_getOID($context, $name);
- if ( !$oid )
- {
- return '';
- }
-
- $key = $oid . '_' . $contextid;
-
- if ( isset($this->_aOptionValues[$key]) )
- {
- return $this->_aOptionValues[$key];
- }
-
- // get from DB
- $res = sql_query('SELECT ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid).' and ocontextid=' . intval($contextid));
-
- if ( !$res || (sql_num_rows($res) == 0) )
- {
- $defVal = $this->_getDefVal($context, $name);
- $this->_aOptionValues[$key] = $defVal;
-
- // fill DB with default value
- $query = 'INSERT INTO ' . sql_table('plugin_option') . ' (oid,ocontextid,ovalue)'
- .' VALUES ('.intval($oid).', '.intval($contextid).', \''.sql_real_escape_string($defVal).'\')';
- sql_query($query);
- }
- else
- {
- $o = sql_fetch_object($res);
- $this->_aOptionValues[$key] = $o->ovalue;
- }
-
- return $this->_aOptionValues[$key];
- }
-
- /**
- * Returns assoc array with all values for a given option
- * (one option per possible context id)
- *
- * private
- */
- function _getAllOptions($context, $name)
- {
- $oid = $this->_getOID($context, $name);
- if ( !$oid )
- {
- return array();
- }
- $defVal = $this->_getDefVal($context, $name);
-
- $aOptions = array();
- switch ( $context )
- {
- case 'blog':
- $r = sql_query('SELECT bnumber as contextid FROM ' . sql_table('blog'));
- break;
- case 'category':
- $r = sql_query('SELECT catid as contextid FROM ' . sql_table('category'));
- break;
- case 'member':
- $r = sql_query('SELECT mnumber as contextid FROM ' . sql_table('member'));
- break;
- case 'item':
- $r = sql_query('SELECT inumber as contextid FROM ' . sql_table('item'));
- break;
- }
- if ( $r )
- {
- while ( $o = sql_fetch_object($r) )
- {
- $aOptions[$o->contextid] = $defVal;
- }
- }
-
- $res = sql_query('SELECT ocontextid, ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);
- while ( $o = sql_fetch_object($res) )
- {
- $aOptions[$o->ocontextid] = $o->ovalue;
- }
-
- return $aOptions;
- }
-
- /**
- * Gets the 'option identifier' that corresponds to a given option name.
- * When this method is called for the first time, all the OIDs for the plugin
- * are loaded into memory, to avoid re-doing the same query all over.
- */
- function _getOID($context, $name)
- {
- $key = $context . '_' . $name;
- $info = $this->_aOptionToInfo[$key];
- if ( is_array($info) )
- {
- return $info['oid'];
- }
-
- // load all OIDs for this plugin from the database
- $this->_aOptionToInfo = array();
- $query = 'SELECT oid, oname, ocontext, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE opid=' . intval($this->plugid);
- $res = sql_query($query);
- while ( $o = sql_fetch_object($res) )
- {
- $k = $o->ocontext . '_' . $o->oname;
- $this->_aOptionToInfo[$k] = array('oid' => $o->oid, 'default' => $o->odef);
- }
- sql_free_result($res);
-
- return $this->_aOptionToInfo[$key]['oid'];
- }
- function _getDefVal($context, $name)
- {
- $key = $context . '_' . $name;
- $info = $this->_aOptionToInfo[$key];
- if ( is_array($info) )
- {
- return $info['default'];
- }
- }
-
- /**
- * Deletes all option values for a given context and contextid
- * (used when e.g. a blog, member or category is deleted)
- *
- * (static method)
- */
- function _deleteOptionValues($context, $contextid)
- {
- // delete all associated plugin options
- $aOIDs = array();
- // find ids
- $query = 'SELECT oid FROM '.sql_table('plugin_option_desc') . ' WHERE ocontext=\''.sql_real_escape_string($context).'\'';
- $res = sql_query($query);
- while ( $o = sql_fetch_object($res) )
- {
- array_push($aOIDs, $o->oid);
- }
- sql_free_result($res);
- // delete those options. go go go
- if ( count($aOIDs) > 0 )
- {
- $query = 'DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid in ('.implode(',',$aOIDs).') and ocontextid=' . intval($contextid);
- sql_query($query);
- }
- }
-
- /**
- * splits the option's typeextra field (at ;'s) to split the meta collection
- * @param string $typeExtra the value of the typeExtra field of an option
- * @return array array of the meta-key/value-pairs
- * @author TeRanEX
- * @static
- */
- function getOptionMeta($typeExtra)
- {
- $tmpMeta = i18n::explode(';', $typeExtra);
- $meta = array();
- for ( $i = 0; $i < count($tmpMeta); $i++ )
- {
- if ( ($i == 0) && (!strstr($tmpMeta[0], '=')) )
- {
- // we have the select-list
- $meta['select'] = $tmpMeta[0];
- }
- else
- {
- $tmp = i18n::explode('=', $tmpMeta[$i]);
- $meta[$tmp[0]] = $tmp[1];
- }
- }
- return $meta;
- }
-
- /**
- * filters the selectlists out of the meta collection
- * @param string $typeExtra the value of the typeExtra field of an option
- * @return string the selectlist
- * @author TeRanEX
- */
- function getOptionSelectValues($typeExtra)
- {
- $meta = NucleusPlugin::getOptionMeta($typeExtra);
- //the select list must always be the first part
- return $meta['select'];
- }
-
- /**
- * checks if the eventlist in the database is up-to-date
- * @return bool if it is up-to-date it return true, else false
- * @author TeRanEX
- */
- function subscribtionListIsUptodate()
- {
- $res = sql_query('SELECT event FROM '.sql_table('plugin_event').' WHERE pid = '.$this->getID());
- $ev = array();
- while( $a = sql_fetch_array($res) )
- {
- array_push($ev, $a['event']);
- }
- if ( count($ev) != count($this->getEventList()) )
- {
- return false;
- }
- $d = array_diff($ev, $this->getEventList());
- if ( count($d) > 0 )
- {
- // there are differences so the db is not up-to-date
- return false;
- }
- return true;
- }
-
- /**
- * @param $aOptions: array ( 'oid' => array( 'contextid' => 'value'))
- * (taken from request using requestVar())
- * @param $newContextid: integer (accepts a contextid when it is for a new
- * contextid there was no id available at the moment of writing the
- * formcontrols into the page (by ex: itemOptions for new item)
- * @static
- */
- function _applyPluginOptions(&$aOptions, $newContextid = 0)
- {
- global $manager;
- if ( !is_array($aOptions) )
- {
- return;
- }
-
- foreach ( $aOptions as $oid => $values )
- {
-
- // get option type info
- $query = 'SELECT opid, oname, ocontext, otype, oextra, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . intval($oid);
- $res = sql_query($query);
- if ( $o = sql_fetch_object($res) )
- {
- foreach ( $values as $key => $value )
- {
- // avoid overriding the key used by foreach statement
- $contextid=$key;
-
- // retreive any metadata
- $meta = NucleusPlugin::getOptionMeta($o->oextra);
-
- // if the option is readonly or hidden it may not be saved
- if ( ($meta['access'] != 'readonly') && ($meta['access'] != 'hidden') )
- {
-
- $value = undoMagic($value); // value comes from request
-
- switch ( $o->otype )
- {
- case 'yesno':
- if ( ($value != 'yes') && ($value != 'no') )
- {
- $value = 'no';
- }
- break;
- default:
- break;
- }
-
- // check the validity of numerical options
- if ( ($meta['datatype'] == 'numerical') && (!is_numeric($value)) )
- {
- //the option must be numeric, but the it isn't
- //use the default for this option
- $value = $o->odef;
- }
-
- // decide wether we are using the contextid of newContextid
- if ( $newContextid != 0 )
- {
- $contextid = $newContextid;
- }
-
- //trigger event PrePluginOptionsUpdate to give the plugin the
- //possibility to change/validate the new value for the option
- $manager->notify('PrePluginOptionsUpdate',array('context' => $o->ocontext, 'plugid' => $o->opid, 'optionname' => $o->oname, 'contextid' => $contextid, 'value' => &$value));
-
- // delete the old value for the option
- sql_query('DELETE FROM '.sql_table('plugin_option').' WHERE oid='.intval($oid).' AND ocontextid='.intval($contextid));
- sql_query('INSERT INTO '.sql_table('plugin_option')." (oid, ocontextid, ovalue) VALUES (".intval($oid).",".intval($contextid).",'" . sql_real_escape_string($value) . "')");
- }
- }
- }
- // clear option value cache if the plugin object is already loaded
- if ( is_object($o) )
- {
- $plugin=& $manager->pidLoaded($o->opid);
- if ( $plugin )
- {
- $plugin->clearOptionValueCache();
- }
- }
- }
- }
-}
+<?php\r
+/*\r
+ * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
+ * Copyright (C) 2002-2012 The Nucleus Group\r
+ *\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * as published by the Free Software Foundation; either version 2\r
+ * of the License, or (at your option) any later version.\r
+ * (see nucleus/documentation/index.html#license for more info)\r
+ */\r
+/**\r
+ * This is an (abstract) class of which all Nucleus Plugins must inherit\r
+ *\r
+ * for more information on plugins and how to write your own, see the\r
+ * plugins.html file that is included with the Nucleus documenation\r
+ *\r
+ * @license http://nucleuscms.org/license.txt GNU General Public License\r
+ * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
+ * @version $Id: PLUGIN.php 1630 2012-01-28 12:16:14Z sakamocchi $\r
+ */\r
+abstract class NucleusPlugin\r
+{\r
+ // these public functions should to be redefined in your plugin\r
+ public function getName()\r
+ {\r
+ return __CLASS__;\r
+ }\r
+ \r
+ public function getAuthor()\r
+ {\r
+ return 'Undefined';\r
+ }\r
+ \r
+ public function getURL()\r
+ {\r
+ return 'Undefined';\r
+ }\r
+ \r
+ public function getVersion()\r
+ {\r
+ return '0.0';\r
+ }\r
+ \r
+ public function getDescription()\r
+ {\r
+ return 'Undefined';\r
+ }\r
+ \r
+ // these final public function _may_ be redefined in your plugin\r
+ \r
+ public function getMinNucleusVersion()\r
+ {\r
+ return 150;\r
+ }\r
+ \r
+ public function getMinNucleusPatchLevel()\r
+ {\r
+ return 0;\r
+ }\r
+ \r
+ public function getEventList()\r
+ {\r
+ return array();\r
+ }\r
+ \r
+ public function getTableList()\r
+ {\r
+ return array();\r
+ }\r
+ \r
+ public function hasAdminArea()\r
+ {\r
+ return 0;\r
+ }\r
+ \r
+ public function install()\r
+ {\r
+ return;\r
+ }\r
+ \r
+ public function unInstall()\r
+ {\r
+ return;\r
+ }\r
+ \r
+ public function init()\r
+ {\r
+ return;\r
+ }\r
+ \r
+ public function doSkinVar($skinType)\r
+ {\r
+ return;\r
+ }\r
+ \r
+ public function doTemplateVar(&$item)\r
+ {\r
+ $args = func_get_args();\r
+ array_shift($args);\r
+ array_unshift($args, 'template');\r
+ call_user_func_array(array(&$this,'doSkinVar'),$args);\r
+ return;\r
+ }\r
+ \r
+ public function doTemplateCommentsVar(&$item, &$comment)\r
+ {\r
+ $args = func_get_args();\r
+ array_shift($args);\r
+ array_shift($args);\r
+ array_unshift($args, 'template');\r
+ call_user_func_array(array(&$this,'doSkinVar'),$args);\r
+ return;\r
+ }\r
+ \r
+ public function doAction($type)\r
+ {\r
+ return _ERROR_PLUGIN_NOSUCHACTION;\r
+ }\r
+ \r
+ public function doIf($key,$value)\r
+ {\r
+ return false;\r
+ }\r
+ \r
+ public function doItemVar (&$item)\r
+ {\r
+ return;\r
+ }\r
+ \r
+ /**\r
+ * Checks if a plugin supports a certain feature.\r
+ *\r
+ * @returns 1 if the feature is reported, 0 if not\r
+ * @param $feature\r
+ * Name of the feature. See plugin documentation for more info\r
+ * 'SqlTablePrefix' -> if the plugin uses the sql_table() method to get table names\r
+ * 'HelpPage' -> if the plugin provides a helppage\r
+ * 'SqlApi' -> if the plugin uses the complete sql_* or DB::* api (must also require nucleuscms 3.5)\r
+ */\r
+ public function supportsFeature($feature)\r
+ {\r
+ return 0;\r
+ }\r
+ \r
+ /**\r
+ * Report a list of plugin that is required to final public function\r
+ *\r
+ * @returns an array of names of plugin, an empty array indicates no dependency\r
+ */\r
+ public function getPluginDep()\r
+ {\r
+ return array();\r
+ }\r
+ \r
+ // these helper final public functions should not be redefined in your plugin\r
+ \r
+ /**\r
+ * Creates a new option for this plugin\r
+ *\r
+ * @param name\r
+ * A string uniquely identifying your option. (max. length is 20 characters)\r
+ * @param description\r
+ * A description that will show up in the nucleus admin area (max. length: 255 characters)\r
+ * @param type\r
+ * Either 'text', 'yesno' or 'password'\r
+ * This info is used when showing 'edit plugin options' screens\r
+ * @param value\r
+ * Initial value for the option (max. value length is 128 characters)\r
+ */\r
+ final public function createOption($name, $desc, $type, $defValue = '', $typeExtras = '')\r
+ {\r
+ return $this->create_option('global', $name, $desc, $type, $defValue, $typeExtras);\r
+ }\r
+ \r
+ final public function createBlogOption($name, $desc, $type, $defValue = '', $typeExtras = '')\r
+ {\r
+ return $this->create_option('blog', $name, $desc, $type, $defValue, $typeExtras);\r
+ }\r
+ \r
+ final public function createMemberOption($name, $desc, $type, $defValue = '', $typeExtras = '')\r
+ {\r
+ return $this->create_option('member', $name, $desc, $type, $defValue, $typeExtras);\r
+ }\r
+ \r
+ final public function createCategoryOption($name, $desc, $type, $defValue = '', $typeExtras = '')\r
+ {\r
+ return $this->create_option('category', $name, $desc, $type, $defValue, $typeExtras);\r
+ }\r
+ \r
+ final public function createItemOption($name, $desc, $type, $defValue = '', $typeExtras = '')\r
+ {\r
+ return $this->create_option('item', $name, $desc, $type, $defValue, $typeExtras);\r
+ }\r
+ \r
+ /**\r
+ * Removes the option from the database\r
+ *\r
+ * Note: Options get erased automatically on plugin uninstall\r
+ */\r
+ final public function deleteOption($name)\r
+ {\r
+ return $this->delete_option('global', $name);\r
+ }\r
+ \r
+ final public function deleteBlogOption($name)\r
+ {\r
+ return $this->delete_option('blog', $name);\r
+ }\r
+ \r
+ final public function deleteMemberOption($name)\r
+ {\r
+ return $this->delete_option('member', $name);\r
+ }\r
+ \r
+ final public function deleteCategoryOption($name)\r
+ {\r
+ return $this->delete_option('category', $name);\r
+ }\r
+ \r
+ final public function deleteItemOption($name)\r
+ {\r
+ return $this->delete_option('item', $name);\r
+ }\r
+ \r
+ /**\r
+ * Sets the value of an option to something new\r
+ */\r
+ final public function setOption($name, $value)\r
+ {\r
+ return $this->set_option('global', 0, $name, $value);\r
+ }\r
+ \r
+ final public function setBlogOption($blogid, $name, $value)\r
+ {\r
+ return $this->set_option('blog', $blogid, $name, $value);\r
+ }\r
+ \r
+ final public function setMemberOption($memberid, $name, $value)\r
+ {\r
+ return $this->set_option('member', $memberid, $name, $value);\r
+ }\r
+ \r
+ final public function setCategoryOption($catid, $name, $value)\r
+ {\r
+ return $this->set_option('category', $catid, $name, $value);\r
+ }\r
+ \r
+ final public function setItemOption($itemid, $name, $value) {\r
+ return $this->set_option('item', $itemid, $name, $value);\r
+ }\r
+ \r
+ /**\r
+ * Retrieves the current value for an option\r
+ */\r
+ final public function getOption($name)\r
+ {\r
+ // only request the options the very first time. On subsequent requests\r
+ // the static collection is used to save SQL queries.\r
+ if ( $this->plugin_options == 0 )\r
+ {\r
+ $this->plugin_options = array();\r
+ \r
+ $query = "SELECT d.oname as name, o.ovalue as value FROM %s o, %s d WHERE d.opid=%d AND d.oid=o.oid;";\r
+ $query = sprintf($query, sql_table('plugin_option'), sql_table('plugin_option_desc'), (integer) $this->plugid);\r
+ $result = DB::getResult($query);\r
+ foreach ( $result as $row )\r
+ {\r
+ $this->plugin_options[strtolower($row['name'])] = $row['value'];\r
+ }\r
+ }\r
+ if ( isset($this->plugin_options[strtolower($name)]) )\r
+ {\r
+ return $this->plugin_options[strtolower($name)];\r
+ }\r
+ else\r
+ {\r
+ return $this->get_option('global', 0, $name);\r
+ }\r
+ }\r
+ \r
+ final public function getBlogOption($blogid, $name)\r
+ {\r
+ return $this->get_option('blog', $blogid, $name);\r
+ }\r
+ \r
+ final public function getMemberOption($memberid, $name)\r
+ {\r
+ return $this->get_option('member', $memberid, $name);\r
+ }\r
+ \r
+ final public function getCategoryOption($catid, $name)\r
+ {\r
+ return $this->get_option('category', $catid, $name);\r
+ }\r
+ \r
+ final public function getItemOption($itemid, $name)\r
+ {\r
+ return $this->get_option('item', $itemid, $name);\r
+ }\r
+ \r
+ /**\r
+ * Retrieves an associative array with the option value for each\r
+ * context id\r
+ */\r
+ final public function getAllBlogOptions($name)\r
+ {\r
+ return $this->get_all_options('blog', $name);\r
+ }\r
+ \r
+ final public function getAllMemberOptions($name)\r
+ {\r
+ return $this->get_all_options('member', $name);\r
+ }\r
+ \r
+ final public function getAllCategoryOptions($name)\r
+ {\r
+ return $this->get_all_options('category', $name);\r
+ }\r
+ \r
+ final public function getAllItemOptions($name)\r
+ {\r
+ return $this->get_all_options('item', $name);\r
+ }\r
+ \r
+ /**\r
+ * Retrieves an indexed array with the top (or bottom) of an option\r
+ * (delegates to getOptionTop())\r
+ */\r
+ final public function getBlogOptionTop($name, $amount = 10, $sort = 'desc')\r
+ {\r
+ return $this->get_option_top('blog', $name, $amount, $sort);\r
+ }\r
+ \r
+ final public function getMemberOptionTop($name, $amount = 10, $sort = 'desc')\r
+ {\r
+ return $this->get_option_top('member', $name, $amount, $sort);\r
+ }\r
+ \r
+ final public function getCategoryOptionTop($name, $amount = 10, $sort = 'desc')\r
+ {\r
+ return $this->get_option_top('category', $name, $amount, $sort);\r
+ }\r
+ \r
+ final public function getItemOptionTop($name, $amount = 10, $sort = 'desc')\r
+ {\r
+ return $this->get_option_top('item', $name, $amount, $sort);\r
+ }\r
+ \r
+ /**\r
+ * NucleusPlugin::getID()\r
+ * get id for this plugin\r
+ * \r
+ * @access public\r
+ * @param void\r
+ * @return integer this plugid id\r
+ */\r
+ final public function getID()\r
+ {\r
+ return (integer) $this->plugid;\r
+ }\r
+ \r
+ /**\r
+ * NucleusPlugin::setID()\r
+ * set favorite id for this plugin\r
+ * \r
+ * @access public\r
+ * @param integer $plugid favorite id for plugin\r
+ * @return void\r
+ */\r
+ final public function setID($plugid)\r
+ {\r
+ $this->plugid = (integer) $plugid;\r
+ return;\r
+ }\r
+ \r
+ /**\r
+ * Returns the URL of the admin area for this plugin (in case there's\r
+ * no such area, the returned information is invalid)\r
+ *\r
+ * public\r
+ */\r
+ final public function getAdminURL()\r
+ {\r
+ global $CONF;\r
+ return $CONF['PluginURL'] . $this->getShortName() . '/';\r
+ }\r
+ \r
+ /**\r
+ * Returns the directory where the admin directory is located and\r
+ * where the plugin can maintain his extra files\r
+ *\r
+ * public\r
+ */\r
+ final public function getDirectory()\r
+ {\r
+ global $DIR_PLUGINS;\r
+ return $DIR_PLUGINS . $this->getShortName() . '/';\r
+ }\r
+ \r
+ /**\r
+ * Derives the short name for the plugin from the classname (all\r
+ * lowercase)\r
+ *\r
+ * public\r
+ */\r
+ final public function getShortName()\r
+ {\r
+ return str_replace('np_','',strtolower(get_class($this)));\r
+ }\r
+ \r
+ /**\r
+ * Clears the option value cache which saves the option values during\r
+ * the plugin execution. This function is usefull if the options has\r
+ * changed during the plugin execution (especially in association with\r
+ * the PrePluginOptionsUpdate and the PostPluginOptionsUpdate events)\r
+ * \r
+ * public\r
+ **/\r
+ final public function clearOptionValueCache()\r
+ {\r
+ $this->option_values = array();\r
+ $this->plugin_options = 0;\r
+ return;\r
+ }\r
+ \r
+ // internal functions of the class starts here\r
+ protected $option_values; // oid_contextid => value\r
+ protected $option_info; // context_name => array('oid' => ..., 'default' => ...)\r
+ protected $plugin_options; // see getOption()\r
+ protected $plugid; // plugin id\r
+ \r
+ /**\r
+ * Class constructor: Initializes some internal data\r
+ */\r
+ public function __construct()\r
+ {\r
+ $this->option_values = array(); // oid_contextid => value\r
+ $this->option_info = array(); // context_name => array('oid' => ..., 'default' => ...)\r
+ $this->plugin_options = 0;\r
+ }\r
+ \r
+ /**\r
+ * Retrieves an array of the top (or bottom) of an option from a plugin.\r
+ * @author TeRanEX\r
+ * @param string $context the context for the option: item, blog, member,...\r
+ * @param string $name the name of the option\r
+ * @param int $amount how many rows must be returned\r
+ * @param string $sort desc or asc\r
+ * @return array array with both values and contextid's\r
+ * @access private\r
+ */\r
+ final protected function get_option_top($context, $name, $amount = 10, $sort = 'desc')\r
+ {\r
+ if ( ($sort != 'desc') && ($sort != 'asc') )\r
+ {\r
+ $sort= 'desc';\r
+ }\r
+ \r
+ $oid = $this->get_option_id($context, $name);\r
+ \r
+ // retrieve the data and return\r
+ $query = "SELECT otype, oextra FROM %s WHERE oid = %d;";\r
+ $query = sprintf($query, sql_table('plugin_option_desc'), $oid);\r
+ $row = DB::getRow($query);\r
+ \r
+ if ( ($this->optionCanBeNumeric($row['otype'])) && ($row['oextra'] == 'number' ) )\r
+ {\r
+ $orderby = 'CAST(ovalue AS SIGNED)';\r
+ }\r
+ else\r
+ {\r
+ $orderby = 'ovalue';\r
+ }\r
+ $query = "SELECT ovalue value, ocontextid id FROM %s WHERE oid = %d ORDER BY %s %s LIMIT 0,%d;";\r
+ $query = sprintf($query, sql_table('plugin_option'), $oid, $orderby, $sort, (integer) $amount);\r
+ $result = DB::getResult($query);\r
+ \r
+ // create the array\r
+ $i = 0;\r
+ $top = array();\r
+ foreach( $result as $row )\r
+ {\r
+ $top[$i++] = $row;\r
+ }\r
+ \r
+ // return the array (duh!)\r
+ return $top;\r
+ }\r
+ \r
+ /**\r
+ * Creates an option in the database table plugin_option_desc\r
+ * \r
+ * private\r
+ */\r
+ final protected function create_option($context, $name, $desc, $type, $defValue, $typeExtras = '')\r
+ {\r
+ // create in plugin_option_desc\r
+ $query = 'INSERT INTO ' . sql_table('plugin_option_desc')\r
+ .' (opid, oname, ocontext, odesc, otype, odef, oextra)'\r
+ .' VALUES ('.intval($this->plugid)\r
+ .', '.DB::quoteValue($name)\r
+ .', '.DB::quoteValue($context)\r
+ .', '.DB::quoteValue($desc)\r
+ .', '.DB::quoteValue($type)\r
+ .', '.DB::quoteValue($defValue)\r
+ .', '.DB::quoteValue($typeExtras).')';\r
+ DB::execute($query);\r
+ $oid = DB::getInsertId();\r
+ \r
+ $key = $context . '_' . $name;\r
+ $this->option_info[$key] = array('oid' => $oid, 'default' => $defValue);\r
+ return 1;\r
+ }\r
+ \r
+ /**\r
+ * Deletes an option from the database tables\r
+ * plugin_option and plugin_option_desc\r
+ *\r
+ * private\r
+ */\r
+ final protected function delete_option($context, $name)\r
+ {\r
+ $oid = $this->get_option_id($context, $name);\r
+ if ( !$oid )\r
+ {\r
+ return 0; // no such option\r
+ }\r
+ \r
+ // delete all things from plugin_option\r
+ $query = "DELETE FROM %s WHERE oid=%d;";\r
+ $query = sprintf($query, sql_table('plugin_option'), (integer) $oid);\r
+ DB::execute($query);\r
+ \r
+ // delete entry from plugin_option_desc\r
+ $query = "DELETE FROM %s WHERE oid=%d;";\r
+ $query = sprintf($query, sql_table('plugin_option_desc'), $oid);\r
+ DB::execute($query);\r
+ \r
+ // clear from cache\r
+ unset($this->option_info["{$context}_{$name}"]);\r
+ $this->option_values = array();\r
+ return 1;\r
+ }\r
+ \r
+ /**\r
+ * Update an option in the database table plugin_option\r
+ * \r
+ * returns: 1 on success, 0 on failure\r
+ * private\r
+ */\r
+ final protected function set_option($context, $contextid, $name, $value)\r
+ {\r
+ global $manager;\r
+ \r
+ $oid = $this->get_option_id($context, $name);\r
+ if ( !$oid )\r
+ {\r
+ return 0;\r
+ }\r
+ \r
+ // check if context id exists\r
+ switch ( $context )\r
+ {\r
+ case 'member':\r
+ if ( !Member::existsID($contextid) )\r
+ {\r
+ return 0;\r
+ }\r
+ break;\r
+ case 'blog':\r
+ if ( !$manager->existsBlogID($contextid) )\r
+ {\r
+ return 0;\r
+ }\r
+ break;\r
+ case 'category':\r
+ if ( !$manager->existsCategory($contextid) )\r
+ {\r
+ return 0;\r
+ }\r
+ break;\r
+ case 'item':\r
+ if ( !$manager->existsItem($contextid, true, true) )\r
+ {\r
+ return 0;\r
+ }\r
+ break;\r
+ case 'global':\r
+ if ( $contextid != 0 )\r
+ {\r
+ return 0;\r
+ }\r
+ break;\r
+ }\r
+ \r
+ // update plugin_option\r
+ $query = "DELETE FROM %s WHERE oid=%d and ocontextid=%d;";\r
+ $query = sprintf($query, sql_table('plugin_option'), (integer) $oid, (integer) $contextid);\r
+ DB::execute($query);\r
+ \r
+ $query = "INSERT INTO %s (ovalue, oid, ocontextid) VALUES (%s, %d, %d);";\r
+ $query = sprintf($query, sql_table('plugin_option'), DB::quoteValue($value), $oid, $contextid);\r
+ DB::execute($query);\r
+ \r
+ // update cache\r
+ $this->option_values["{$oid}_{$contextid}"] = $value;\r
+ if ( $context == 'global' )\r
+ {\r
+ $this->plugin_options[strtolower($name)] = $value;\r
+ }\r
+ \r
+ return 1;\r
+ }\r
+ \r
+ /**\r
+ * Get an option from Cache or database\r
+ * - if not in the option Cache read it from the database\r
+ * - if not in the database write default values into the database\r
+ * \r
+ * private \r
+ */ \r
+ final protected function get_option($context, $contextid, $name)\r
+ {\r
+ $oid = $this->get_option_id($context, $name);\r
+ if ( !$oid )\r
+ {\r
+ return '';\r
+ }\r
+ \r
+ $key = "{$oid}_{$contextid}";\r
+ \r
+ if ( isset($this->option_values[$key]) )\r
+ {\r
+ return $this->option_values[$key];\r
+ }\r
+ \r
+ // get from DB\r
+ $query = "SELECT ovalue FROM %s WHERE oid=%d and ocontextid=%d;";\r
+ $query = sprintf($query, sql_table('plugin_option'), (integer) $oid, (integer) $contextid);\r
+ $result = DB::getResult($query);\r
+ \r
+ if ( !$result || ($result->rowCount() == 0) )\r
+ {\r
+ // fill DB with default value\r
+ $this->option_values[$key] = $this->get_default_value($context, $name);\r
+ $query = "INSERT INTO %s (oid, ocontextid, ovalue) VALUES (%d, %d, %s);";\r
+ $query = sprintf($query, sql_table('plugin_option'), (integer) $oid, (integer) $contextid, DB::quoteValue($this->option_values[$key]));\r
+ DB::execute($query);\r
+ }\r
+ else\r
+ {\r
+ $row = $result->fetch();\r
+ $this->option_values[$key] = $row['ovalue'];\r
+ }\r
+ \r
+ return $this->option_values[$key];\r
+ }\r
+ \r
+ /**\r
+ * Returns assoc array with all values for a given option\r
+ * (one option per possible context id)\r
+ *\r
+ * private \r
+ */\r
+ final protected function get_all_options($context, $name)\r
+ {\r
+ $oid = $this->get_option_id($context, $name);\r
+ if ( !$oid )\r
+ {\r
+ return array();\r
+ }\r
+ $default_value = $this->get_default_value($context, $name);\r
+ \r
+ $options = array();\r
+ $query = "SELECT %s as contextid FROM %s;";\r
+ switch ( $context )\r
+ {\r
+ case 'blog':\r
+ $query = sprintf($query, 'bnumber', sql_table('blog'));\r
+ break;\r
+ case 'category':\r
+ $query = sprintf($query, 'catid', sql_table('category'));\r
+ break;\r
+ case 'member':\r
+ $query = sprintf($query, 'mnumber', sql_table('member'));\r
+ break;\r
+ case 'item':\r
+ $query = sprintf($query, 'inumber', sql_table('item'));\r
+ break;\r
+ }\r
+ \r
+ $result = DB::getResult($query);\r
+ if ( $result )\r
+ {\r
+ foreach ( $result as $row )\r
+ {\r
+ $options[$row['contextid']] = $default_value;\r
+ }\r
+ }\r
+ \r
+ $query = "SELECT ocontextid, ovalue FROM %s WHERE oid=%d;";\r
+ $query = sprintf($query, sql_table('plugin_option'), $oid);\r
+ $result = DB::getResult($query);\r
+ foreach ( $result as $row )\r
+ {\r
+ $options[$row['ocontextid']] = $row['ovalue'];\r
+ }\r
+\r
+ return $options;\r
+ }\r
+ \r
+ /**\r
+ * NucleusPlugin::get_option_id\r
+ * \r
+ * Gets the 'option identifier' that corresponds to a given option name.\r
+ * When this method is called for the first time, all the OIDs for the plugin\r
+ * are loaded into memory, to avoid re-doing the same query all over.\r
+ * \r
+ * @param string $context option context\r
+ * @param string $name plugin name\r
+ * @return integer option id\r
+ */\r
+ final protected function get_option_id($context, $name)\r
+ {\r
+ $key = "{$context}_{$name}";\r
+ \r
+ if ( array_key_exists($key, $this->option_info)\r
+ && array_key_exists('oid', $this->option_info[$key]) )\r
+ {\r
+ return $this->option_info[$key]['oid'];\r
+ }\r
+ \r
+ // load all OIDs for this plugin from the database\r
+ $this->option_info = array();\r
+ $query = "SELECT oid, oname, ocontext, odef FROM %s WHERE opid=%d;";\r
+ $query = sprintf($query, sql_table('plugin_option_desc'), $this->plugid);\r
+ $result = DB::getResult($query);\r
+ foreach ( $result as $row )\r
+ {\r
+ $k = $row['ocontext'] . '_' . $row['oname'];\r
+ $this->option_info[$k] = array('oid' => $row['oid'], 'default' => $row['odef']);\r
+ }\r
+ $result->closeCursor();\r
+ \r
+ return $this->option_info[$key]['oid'];\r
+ }\r
+ final protected function get_default_value($context, $name)\r
+ {\r
+ $key = $context . '_' . $name;\r
+ \r
+ if ( array_key_exists($key, $this->option_info)\r
+ && array_key_exists('default', $this->option_info[$key]) )\r
+ {\r
+ return $this->option_info[$key]['default'];\r
+ }\r
+ return;\r
+ }\r
+ \r
+ /**\r
+ * NucleusPlugin::delete_option_values()\r
+ * Deletes all option values for a given context and contextid\r
+ * (used when e.g. a blog, member or category is deleted)\r
+ *\r
+ *@static\r
+ *@param String $context global/blog/category/item/member\r
+ *@param Integer $contextid ID\r
+ *@return Void\r
+ */\r
+ static public function delete_option_values($context, $contextid)\r
+ {\r
+ // delete all associated plugin options\r
+ $aOIDs = array();\r
+ // find ids\r
+ $query = "SELECT oid FROM %s WHERE ocontext=%s;";\r
+ $query = sprintf($query, sql_table('plugin_option_desc'), DB::quoteValue($context));\r
+ \r
+ $result = DB::getResult($query);\r
+ foreach ( $result as $row )\r
+ {\r
+ array_push($aOIDs, $row['oid']);\r
+ }\r
+ $result->closeCursor();\r
+ // delete those options. go go go\r
+ if ( count($aOIDs) > 0 )\r
+ {\r
+ $query = "DELETE FROM %s WHERE oid in (%s) and ocontextid=%d;";\r
+ $query = sprintf($query, sql_table('plugin_option'), implode(',',$aOIDs), (integer) $contextid);\r
+ DB::execute($query);\r
+ }\r
+ return;\r
+ }\r
+ \r
+ /**\r
+ * NucleusPlugin::getOptionMeta()\r
+ * splits the option's typeextra field (at ;'s) to split the meta collection\r
+ * \r
+ * @static\r
+ * @param string $typeExtra the value of the typeExtra field of an option\r
+ * @return array array of the meta-key/value-pairs\r
+ */\r
+ static public function getOptionMeta($typeExtra)\r
+ {\r
+ $meta = array();\r
+ \r
+ /* 1. if $typeExtra includes delimiter ';', split it to tokens */\r
+ $tokens = preg_split('#;#', $typeExtra);\r
+ \r
+ /*\r
+ * 2. if each of tokens includes "=", it consists of key => value\r
+ * else it's 'select' option\r
+ */\r
+ foreach ( $tokens as $token )\r
+ {\r
+ $matches = array();\r
+ if ( preg_match("#^([^=]+)?=([^=]+)?$#", $token, $matches) )\r
+ {\r
+ $meta[$matches[1]] = $matches[2];\r
+ }\r
+ else\r
+ {\r
+ $meta['select'] = $token;\r
+ }\r
+ }\r
+ return $meta;\r
+ }\r
+ \r
+ /**\r
+ * NucleusPlugin::getOptionSelectValues()\r
+ * filters the selectlists out of the meta collection\r
+ * \r
+ * @static\r
+ * @param string $typeExtra the value of the typeExtra field of an option\r
+ * @return string the selectlist\r
+ */\r
+ static public function getOptionSelectValues($typeExtra)\r
+ {\r
+ $meta = NucleusPlugin::getOptionMeta($typeExtra);\r
+ \r
+ if ( array_key_exists('select', $meta) )\r
+ {\r
+ return $meta['select'];\r
+ }\r
+ return;\r
+ }\r
+ \r
+ /**\r
+ * checks if the eventlist in the database is up-to-date\r
+ * @return bool if it is up-to-date it return true, else false\r
+ * @author TeRanEX\r
+ */\r
+ public function subscribtionListIsUptodate()\r
+ {\r
+ $res = DB::getResult('SELECT event FROM '.sql_table('plugin_event').' WHERE pid = '.$this->plugid);\r
+ $ev = array();\r
+ foreach ( $res as $row )\r
+ {\r
+ array_push($ev, $row['event']);\r
+ }\r
+ if ( count($ev) != count($this->getEventList()) )\r
+ {\r
+ return false;\r
+ }\r
+ $d = array_diff($ev, $this->getEventList());\r
+ if ( count($d) > 0 )\r
+ {\r
+ // there are differences so the db is not up-to-date\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+ \r
+ /**\r
+ * NucleusPlugin::apply_plugin_options()\r
+ * Update its entry in database table\r
+ * \r
+ * @static\r
+ * @param $options: array ( 'oid' => array( 'contextid' => 'value'))\r
+ * (taken from request using requestVar())\r
+ * @param $new_contextid: integer (accepts a contextid when it is for a new\r
+ * contextid there was no id available at the moment of writing the\r
+ * formcontrols into the page (by ex: itemOptions for new item)\r
+ * @return void\r
+ */\r
+ static public function apply_plugin_options(&$options, $new_contextid = 0)\r
+ {\r
+ global $manager;\r
+ \r
+ if ( !is_array($options) )\r
+ {\r
+ return;\r
+ }\r
+ \r
+ foreach ( $options as $oid => $values )\r
+ {\r
+ // get option type info\r
+ $query = "SELECT opid, oname, ocontext, otype, oextra, odef FROM %s WHERE oid=%d;";\r
+ $query = sprintf($query, sql_table('plugin_option_desc'), (integer) $oid);\r
+ $result = DB::getRow($query);\r
+ if ( $result )\r
+ {\r
+ foreach ( $values as $id => $value )\r
+ {\r
+ // decide wether we are using the contextid of newContextid\r
+ if ( $new_contextid != 0 )\r
+ {\r
+ $contextid = $new_contextid;\r
+ }\r
+ else\r
+ {\r
+ $contextid = $id;\r
+ }\r
+ \r
+ // retreive any metadata\r
+ $meta = NucleusPlugin::getOptionMeta($result['oextra']);\r
+ \r
+ // if the option is readonly or hidden it may not be saved\r
+ if ( array_key_exists('access', $meta)\r
+ && in_array($meta['access'], array('readonly', 'hidden')) )\r
+ {\r
+ return;\r
+ }\r
+ \r
+ // value comes from request\r
+ $value = undoMagic($value);\r
+ \r
+ /* validation the value according to its type */\r
+ switch ( $result['otype'] )\r
+ {\r
+ case 'yesno':\r
+ if ( ($value != 'yes') && ($value != 'no') )\r
+ {\r
+ $value = 'no';\r
+ }\r
+ break;\r
+ case 'text':\r
+ case 'select':\r
+ if ( array_key_exists('datatype', $meta)\r
+ && ($meta['datatype'] == 'numerical') && ($value != (integer) $value) )\r
+ {\r
+ $value = (integer) $result['odef'];\r
+ }\r
+ break;\r
+ case 'password':\r
+ case 'textarea':\r
+ default:\r
+ break;\r
+ }\r
+ \r
+ /*\r
+ * trigger event PrePluginOptionsUpdate to give the plugin the\r
+ * possibility to change/validate the new value for the option\r
+ */\r
+ $data = array(\r
+ 'context' => $result['ocontext'],\r
+ 'plugid' => $result['opid'],\r
+ 'optionname' => $result['oname'],\r
+ 'contextid' => $contextid,\r
+ 'value' => &$value);\r
+ $manager->notify('PrePluginOptionsUpdate', $data);\r
+ \r
+ // delete and insert its fields of table in database\r
+ $query = "DELETE FROM %s WHERE oid=%d AND ocontextid=%d;";\r
+ $query = sprintf($query, sql_table('plugin_option'), (integer) $oid, (integer) $contextid);\r
+ DB::execute($query);\r
+ $query = "INSERT INTO %s (oid, ocontextid, ovalue) VALUES (%d, %d, %s);";\r
+ $query = sprintf($query, sql_table('plugin_option'), (integer) $oid, (integer) $contextid, DB::quoteValue($value));\r
+ DB::execute($query);\r
+ \r
+ // clear option value cache if the plugin object is already loaded\r
+ $plugin=& $manager->pidLoaded($result['opid']);\r
+ if ( $plugin )\r
+ {\r
+ $plugin->clearOptionValueCache();\r
+ }\r
+ \r
+ continue;\r
+ }\r
+ }\r
+ continue;\r
+ }\r
+ return;\r
+ }\r
+}\r