OSDN Git Service

FIX: リファレンスにまつわるコードを修正
[nucleus-jp/nucleus-next.git] / nucleus / libs / PLUGIN.php
index 6582ef9..4cebb26 100644 (file)
-<?php\r
-/*\r
- * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
- * Copyright (C) 2002-2009 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-2009 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_* 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 = sql_query($query);\r
-                       while ( $row = sql_fetch_object($result) )\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
-               $result = sql_query($query);\r
-               \r
-               $o = sql_fetch_array($result);\r
-               \r
-               if ( ($this->optionCanBeNumeric($o['otype'])) && ($o['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 = sql_query($query);\r
-               \r
-               // create the array\r
-               $i = 0;\r
-               $top = array();\r
-               while( $row = sql_fetch_array($result) )\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
-                                       .', \''.sql_real_escape_string($name).'\''\r
-                                       .', \''.sql_real_escape_string($context).'\''\r
-                                       .', \''.sql_real_escape_string($desc).'\''\r
-                                       .', \''.sql_real_escape_string($type).'\''\r
-                                       .', \''.sql_real_escape_string($defValue).'\''\r
-                                       .', \''.sql_real_escape_string($typeExtras).'\');';\r
-               sql_query($query);\r
-               $oid = sql_insert_id();\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
-               sql_query($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
-               sql_query($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
-               sql_query($query);\r
-               \r
-               $query = "INSERT INTO %s (ovalue, oid, ocontextid) VALUES ('%s', %d, %d);";\r
-               $query = sprintf($query, sql_table('plugin_option'), sql_real_escape_string($value), $oid, $contextid);\r
-               sql_query($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 = sql_query($query);\r
-               \r
-               if ( !$result || (sql_num_rows($result) == 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, sql_real_escape_string($defVal));\r
-                       sql_query($query);\r
-               }\r
-               else\r
-               {\r
-                       $o = sql_fetch_object($result);\r
-                       $this->option_values[$key] = $o->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 = sql_query($query);\r
-               if ( $result )\r
-               {\r
-                       while ( $o = sql_fetch_object($r) )\r
-                       {\r
-                               $options[$o->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 = sql_query($query);\r
-               while ( $o = sql_fetch_object($result) )\r
-               {\r
-                       $options[$o->ocontextid] = $o->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 = sql_query($query);\r
-               while ( $o = sql_fetch_object($result) )\r
-               {\r
-                       $k = $o->ocontext . '_' . $o->oname;\r
-                       $this->option_info[$k] = array('oid' => $o->oid, 'default' => $o->odef);\r
-               }\r
-               sql_free_result($result);\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'), sql_real_escape_string($context));\r
-               \r
-               $result = sql_query($query);\r
-               while ( $o = sql_fetch_object($result) )\r
-               {\r
-                       array_push($aOIDs, $o->oid);\r
-               }\r
-               sql_free_result($result);\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
-                       sql_query($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 = i18n::explode(';', $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 = sql_query('SELECT event FROM '.sql_table('plugin_event').' WHERE pid = '.$this->plugid);\r
-               $ev = array();\r
-               while( $a = sql_fetch_array($res) )\r
-               {\r
-                       array_push($ev, $a['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 = sql_query($query);\r
-                       if ( $info = sql_fetch_object($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($info->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 ( $info->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) $info->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'               => $info->ocontext,\r
-                                               'plugid'                => $info->opid,\r
-                                               'optionname'    => $info->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
-                                       sql_query($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, sql_real_escape_string($value));\r
-                                       sql_query($query);\r
-                               }\r
-                       }\r
-                       // clear option value cache if the plugin object is already loaded\r
-                       if ( is_object($info) )\r
-                       {\r
-                               $plugin=& $manager->pidLoaded($info->opid);\r
-                               if ( $plugin )\r
-                               {\r
-                                       $plugin->clearOptionValueCache();\r
-                               }\r
-                       }\r
-               }\r
-               return;\r
-       }\r
-}\r
+<?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 1866 2012-05-20 13:21:55Z sakamocchi $
+ */
+abstract class NucleusPlugin
+{
+       // these public functions should to be redefined in your plugin
+       public function getName()
+       {
+               return __CLASS__;
+       }
+       
+       public function getAuthor()
+       {
+               return 'Undefined';
+       }
+       
+       public function getURL()
+       {
+               return 'Undefined';
+       }
+       
+       public function getVersion()
+       {
+               return '0.0';
+       }
+       
+       public function getDescription()
+       {
+               return 'Undefined';
+       }
+       
+       // these final public function _may_ be redefined in your plugin
+       
+       public function getMinNucleusVersion()
+       {
+               return 150;
+       }
+       
+       public function getMinNucleusPatchLevel()
+       {
+               return 0;
+       }
+       
+       public function getEventList()
+       {
+               return array();
+       }
+       
+       public function getTableList()
+       {
+               return array();
+       }
+       
+       public function hasAdminArea()
+       {
+               return 0;
+       }
+       
+       public function install()
+       {
+               return;
+       }
+       
+       public function unInstall()
+       {
+               return;
+       }
+       
+       public function init()
+       {
+               return;
+       }
+       
+       public function doSkinVar($skinType)
+       {
+               return;
+       }
+       
+       public function doTemplateVar(&$item)
+       {
+               $args = func_get_args();
+               array_shift($args);
+               array_unshift($args, 'template');
+               call_user_func_array(array($this, 'doSkinVar'), $args);
+               return;
+       }
+       
+       public 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);
+               return;
+       }
+       
+       public function doAction($type)
+       {
+               return _ERROR_PLUGIN_NOSUCHACTION;
+       }
+       
+       public function doIf($key,$value)
+       {
+               return false;
+       }
+       
+       public function doItemVar (&$item)
+       {
+               return;
+       }
+       
+       /**
+        * 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_* or DB::* api (must also require nucleuscms 3.5)
+        */
+       public function supportsFeature($feature)
+       {
+               return 0;
+       }
+       
+       /**
+        * Report a list of plugin that is required to final public function
+        *
+        * @returns an array of names of plugin, an empty array indicates no dependency
+        */
+       public function getPluginDep()
+       {
+               return array();
+       }
+       
+       // these helper final public 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)
+        */
+       final public function createOption($name, $desc, $type, $defValue = '', $typeExtras = '')
+       {
+               return $this->create_option('global', $name, $desc, $type, $defValue, $typeExtras);
+       }
+       
+       final public function createBlogOption($name, $desc, $type, $defValue = '', $typeExtras = '')
+       {
+               return $this->create_option('blog', $name, $desc, $type, $defValue, $typeExtras);
+       }
+       
+       final public function createMemberOption($name, $desc, $type, $defValue = '', $typeExtras = '')
+       {
+               return $this->create_option('member', $name, $desc, $type, $defValue, $typeExtras);
+       }
+       
+       final public function createCategoryOption($name, $desc, $type, $defValue = '', $typeExtras = '')
+       {
+               return $this->create_option('category', $name, $desc, $type, $defValue, $typeExtras);
+       }
+       
+       final public function createItemOption($name, $desc, $type, $defValue = '', $typeExtras = '')
+       {
+               return $this->create_option('item', $name, $desc, $type, $defValue, $typeExtras);
+       }
+       
+       /**
+        * Removes the option from the database
+        *
+        * Note: Options get erased automatically on plugin uninstall
+        */
+       final public function deleteOption($name)
+       {
+               return $this->delete_option('global', $name);
+       }
+       
+       final public function deleteBlogOption($name)
+       {
+               return $this->delete_option('blog', $name);
+       }
+       
+       final public function deleteMemberOption($name)
+       {
+               return $this->delete_option('member', $name);
+       }
+       
+       final public function deleteCategoryOption($name)
+       {
+               return $this->delete_option('category', $name);
+       }
+       
+       final public function deleteItemOption($name)
+       {
+               return $this->delete_option('item', $name);
+       }
+       
+       /**
+        * Sets the value of an option to something new
+        */
+       final public function setOption($name, $value)
+       {
+               return $this->set_option('global', 0, $name, $value);
+       }
+       
+       final public function setBlogOption($blogid, $name, $value)
+       {
+               return $this->set_option('blog', $blogid, $name, $value);
+       }
+       
+       final public function setMemberOption($memberid, $name, $value)
+       {
+               return $this->set_option('member', $memberid, $name, $value);
+       }
+       
+       final public function setCategoryOption($catid, $name, $value)
+       {
+               return $this->set_option('category', $catid, $name, $value);
+       }
+       
+       final public function setItemOption($itemid, $name, $value) {
+               return $this->set_option('item', $itemid, $name, $value);
+       }
+       
+       /**
+        * Retrieves the current value for an option
+        */
+       final public 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 =  "SELECT d.oname as name, o.ovalue as value FROM %s o, %s d WHERE d.opid=%d AND d.oid=o.oid;";
+                       $query = sprintf($query, sql_table('plugin_option'), sql_table('plugin_option_desc'), (integer) $this->plugid);
+                       $result = DB::getResult($query);
+                       foreach ( $result as $row )
+                       {
+                               $this->plugin_options[strtolower($row['name'])] = $row['value'];
+                       }
+               }
+               if ( isset($this->plugin_options[strtolower($name)]) )
+               {
+                       return $this->plugin_options[strtolower($name)];
+               }
+               else
+               {
+                       return $this->get_option('global', 0, $name);
+               }
+       }
+       
+       final public function getBlogOption($blogid, $name)
+       {
+               return $this->get_option('blog', $blogid, $name);
+       }
+       
+       final public function getMemberOption($memberid, $name)
+       {
+               return $this->get_option('member', $memberid, $name);
+       }
+       
+       final public function getCategoryOption($catid, $name)
+       {
+               return $this->get_option('category', $catid, $name);
+       }
+       
+       final public function getItemOption($itemid, $name)
+       {
+               return $this->get_option('item', $itemid, $name);
+       }
+       
+       /**
+        * Retrieves an associative array with the option value for each
+        * context id
+        */
+       final public function getAllBlogOptions($name)
+       {
+               return $this->get_all_options('blog', $name);
+       }
+       
+       final public function getAllMemberOptions($name)
+       {
+               return $this->get_all_options('member', $name);
+       }
+       
+       final public function getAllCategoryOptions($name)
+       {
+               return $this->get_all_options('category', $name);
+       }
+       
+       final public function getAllItemOptions($name)
+       {
+               return $this->get_all_options('item', $name);
+       }
+       
+       /**
+        * Retrieves an indexed array with the top (or bottom) of an option
+        * (delegates to getOptionTop())
+        */
+       final public function getBlogOptionTop($name, $amount = 10, $sort = 'desc')
+       {
+               return $this->get_option_top('blog', $name, $amount, $sort);
+       }
+       
+       final public function getMemberOptionTop($name, $amount = 10, $sort = 'desc')
+       {
+               return $this->get_option_top('member', $name, $amount, $sort);
+       }
+       
+       final public function getCategoryOptionTop($name, $amount = 10, $sort = 'desc')
+       {
+               return $this->get_option_top('category', $name, $amount, $sort);
+       }
+       
+       final public function getItemOptionTop($name, $amount = 10, $sort = 'desc')
+       {
+               return $this->get_option_top('item', $name, $amount, $sort);
+       }
+       
+       /**
+        * NucleusPlugin::getID()
+        * get id for this plugin
+        * 
+        * @access      public
+        * @param       void
+        * @return      integer this plugid id
+        */
+       final public function getID()
+       {
+               return (integer) $this->plugid;
+       }
+       
+       /**
+        * NucleusPlugin::setID()
+        * set favorite id for this plugin
+        * 
+        * @access      public
+        * @param       integer $plugid favorite id for plugin
+        * @return      void
+        */
+       final public function setID($plugid)
+       {
+               $this->plugid = (integer) $plugid;
+               return;
+       }
+       
+       /**
+        * Returns the URL of the admin area for this plugin (in case there's
+        * no such area, the returned information is invalid)
+        *
+        * public
+        */
+       final 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
+        */
+       final public function getDirectory()
+       {
+               global $DIR_PLUGINS;
+               return $DIR_PLUGINS . $this->getShortName() . '/';
+       }
+       
+       /**
+        * Derives the short name for the plugin from the classname (all
+        * lowercase)
+        *
+        * public
+        */
+       final 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
+        **/
+       final public function clearOptionValueCache()
+       {
+               $this->option_values = array();
+               $this->plugin_options = 0;
+               return;
+       }
+       
+       // internal functions of the class starts here
+       protected $option_values;       // oid_contextid => value
+       protected $option_info;         // context_name => array('oid' => ..., 'default' => ...)
+       protected $plugin_options;      // see getOption()
+       protected $plugid;                      // plugin id
+       
+       /**
+        * Class constructor: Initializes some internal data
+        */
+       public function __construct()
+       {
+               $this->option_values = array(); // oid_contextid => value
+               $this->option_info = 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
+        */
+       final protected function get_option_top($context, $name, $amount = 10, $sort = 'desc')
+       {
+               if ( ($sort != 'desc') && ($sort != 'asc') )
+               {
+                       $sort= 'desc';
+               }
+               
+               $oid = $this->get_option_id($context, $name);
+               
+               // retrieve the data and return
+               $query = "SELECT otype, oextra FROM %s WHERE oid = %d;";
+               $query = sprintf($query, sql_table('plugin_option_desc'), $oid);
+               $row = DB::getRow($query);
+               
+               if ( ($this->optionCanBeNumeric($row['otype'])) && ($row['oextra'] == 'number' ) )
+               {
+                       $orderby = 'CAST(ovalue AS SIGNED)';
+               }
+               else
+               {
+                       $orderby = 'ovalue';
+               }
+               $query = "SELECT ovalue value, ocontextid id FROM %s WHERE oid = %d ORDER BY %s %s LIMIT 0,%d;";
+               $query = sprintf($query, sql_table('plugin_option'), $oid, $orderby, $sort, (integer) $amount);
+               $result = DB::getResult($query);
+               
+               // create the array
+               $i = 0;
+               $top = array();
+               foreach( $result as $row )
+               {
+                       $top[$i++] = $row;
+               }
+               
+               // return the array (duh!)
+               return $top;
+       }
+       
+       /**
+        * Creates an option in the database table plugin_option_desc
+        *      
+        * private
+        */
+       final protected function create_option($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)
+                                       .', '.DB::quoteValue($name)
+                                       .', '.DB::quoteValue($context)
+                                       .', '.DB::quoteValue($desc)
+                                       .', '.DB::quoteValue($type)
+                                       .', '.DB::quoteValue($defValue)
+                                       .', '.DB::quoteValue($typeExtras).')';
+               DB::execute($query);
+               $oid = DB::getInsertId();
+               
+               $key = $context . '_' . $name;
+               $this->option_info[$key] = array('oid' => $oid, 'default' => $defValue);
+               return 1;
+       }
+       
+       /**
+        * Deletes an option from the database tables
+        * plugin_option and plugin_option_desc
+        *
+        * private
+        */
+       final protected function delete_option($context, $name)
+       {
+               $oid = $this->get_option_id($context, $name);
+               if ( !$oid )
+               {
+                       return 0; // no such option
+               }
+               
+               // delete all things from plugin_option
+               $query = "DELETE FROM %s WHERE oid=%d;";
+               $query = sprintf($query, sql_table('plugin_option'), (integer) $oid);
+               DB::execute($query);
+               
+               // delete entry from plugin_option_desc
+               $query = "DELETE FROM %s WHERE oid=%d;";
+               $query = sprintf($query, sql_table('plugin_option_desc'), $oid);
+               DB::execute($query);
+               
+               // clear from cache
+               unset($this->option_info["{$context}_{$name}"]);
+               $this->option_values = array();
+               return 1;
+       }
+       
+       /**
+        * Update an option in the database table plugin_option
+        *              
+        * returns: 1 on success, 0 on failure
+        * private
+        */
+       final protected function set_option($context, $contextid, $name, $value)
+       {
+               global $manager;
+               
+               $oid = $this->get_option_id($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
+               $query = "DELETE FROM %s WHERE oid=%d and ocontextid=%d;";
+               $query = sprintf($query, sql_table('plugin_option'), (integer) $oid, (integer) $contextid);
+               DB::execute($query);
+               
+               $query = "INSERT INTO %s (ovalue, oid, ocontextid) VALUES (%s, %d, %d);";
+               $query = sprintf($query, sql_table('plugin_option'), DB::quoteValue($value), $oid, $contextid);
+               DB::execute($query);
+               
+               // update cache
+               $this->option_values["{$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              
+        */                                             
+       final protected function get_option($context, $contextid, $name)
+       {
+               $oid = $this->get_option_id($context, $name);
+               if ( !$oid )
+               {
+                       return '';
+               }
+               
+               $key = "{$oid}_{$contextid}";
+               
+               if ( isset($this->option_values[$key]) )
+               {
+                       return $this->option_values[$key];
+               }
+               
+               // get from DB
+               $query = "SELECT ovalue FROM %s WHERE oid=%d and ocontextid=%d;";
+               $query = sprintf($query, sql_table('plugin_option'), (integer) $oid, (integer) $contextid);
+               $result = DB::getResult($query);
+               
+               if ( !$result || ($result->rowCount() == 0) )
+               {
+                       // fill DB with default value
+                       $this->option_values[$key] = $this->get_default_value($context, $name);
+                       $query = "INSERT INTO %s (oid, ocontextid, ovalue) VALUES (%d, %d, %s);";
+                       $query = sprintf($query, sql_table('plugin_option'), (integer) $oid, (integer) $contextid, DB::quoteValue($this->option_values[$key]));
+                       DB::execute($query);
+               }
+               else
+               {
+                       $row = $result->fetch();
+                       $this->option_values[$key] = $row['ovalue'];
+               }
+               
+               return $this->option_values[$key];
+       }
+       
+       /**
+        * Returns assoc array with all values for a given option
+        * (one option per possible context id)
+        *
+        * private                              
+        */
+       final protected function get_all_options($context, $name)
+       {
+               $oid = $this->get_option_id($context, $name);
+               if ( !$oid )
+               {
+                       return array();
+               }
+               $default_value = $this->get_default_value($context, $name);
+               
+               $options = array();
+               $query = "SELECT %s as contextid FROM %s;";
+               switch ( $context )
+               {
+                       case 'blog':
+                               $query = sprintf($query, 'bnumber', sql_table('blog'));
+                               break;
+                       case 'category':
+                               $query = sprintf($query, 'catid', sql_table('category'));
+                               break;
+                       case 'member':
+                               $query = sprintf($query, 'mnumber', sql_table('member'));
+                               break;
+                       case 'item':
+                               $query = sprintf($query, 'inumber', sql_table('item'));
+                               break;
+               }
+               
+               $result = DB::getResult($query);
+               if ( $result )
+               {
+                       foreach ( $result as $row )
+                       {
+                               $options[$row['contextid']] = $default_value;
+                       }
+               }
+               
+               $query = "SELECT ocontextid, ovalue FROM %s WHERE oid=%d;";
+               $query = sprintf($query, sql_table('plugin_option'), $oid);
+               $result = DB::getResult($query);
+               foreach ( $result as $row )
+               {
+                       $options[$row['ocontextid']] = $row['ovalue'];
+               }
+
+               return $options;
+       }
+       
+       /**
+        * NucleusPlugin::get_option_id
+        * 
+        * 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.
+        * 
+        * @param       string  $context        option context
+        * @param       string  $name           plugin name
+        * @return              integer option id
+        */
+       final protected function get_option_id($context, $name)
+       {
+               $key = "{$context}_{$name}";
+               
+               if ( array_key_exists($key, $this->option_info)
+                && array_key_exists('oid', $this->option_info[$key]) )
+               {
+                       return $this->option_info[$key]['oid'];
+               }
+               
+               // load all OIDs for this plugin from the database
+               $this->option_info = array();
+               $query = "SELECT oid, oname, ocontext, odef FROM %s WHERE opid=%d;";
+               $query = sprintf($query, sql_table('plugin_option_desc'), $this->plugid);
+               $result = DB::getResult($query);
+               foreach ( $result as $row )
+               {
+                       $k = $row['ocontext'] . '_' . $row['oname'];
+                       $this->option_info[$k] = array('oid' => $row['oid'], 'default' => $row['odef']);
+               }
+               $result->closeCursor();
+               
+               return $this->option_info[$key]['oid'];
+       }
+       final protected function get_default_value($context, $name)
+       {
+               $key = $context . '_' . $name;
+               
+               if ( array_key_exists($key, $this->option_info)
+                && array_key_exists('default', $this->option_info[$key]) )
+               {
+                       return $this->option_info[$key]['default'];
+               }
+               return;
+       }
+       
+       /**
+        * NucleusPlugin::delete_option_values()
+        * Deletes all option values for a given context and contextid
+        * (used when e.g. a blog, member or category is deleted)
+        *
+        *@static
+        *@param        String  $context        global/blog/category/item/member
+        *@param        Integer $contextid              ID
+        *@return       Void
+        */
+       static public function delete_option_values($context, $contextid)
+       {
+               // delete all associated plugin options
+               $aOIDs = array();
+               // find ids
+               $query = "SELECT oid FROM %s WHERE ocontext=%s;";
+               $query = sprintf($query, sql_table('plugin_option_desc'), DB::quoteValue($context));
+               
+               $result = DB::getResult($query);
+               foreach ( $result as $row )
+               {
+                       array_push($aOIDs, $row['oid']);
+               }
+               $result->closeCursor();
+               // delete those options. go go go
+               if ( count($aOIDs) > 0 )
+               {
+                       $query = "DELETE FROM %s WHERE oid in (%s) and ocontextid=%d;";
+                       $query = sprintf($query, sql_table('plugin_option'), implode(',',$aOIDs), (integer) $contextid);
+                       DB::execute($query);
+               }
+               return;
+       }
+       
+       /**
+        * NucleusPlugin::getOptionMeta()
+        * splits the option's typeextra field (at ;'s) to split the meta collection
+        * 
+        * @static
+        * @param string $typeExtra the value of the typeExtra field of an option
+        * @return array array of the meta-key/value-pairs
+        */
+       static public function getOptionMeta($typeExtra)
+       {
+               $meta = array();
+               
+               /* 1. if $typeExtra includes delimiter ';', split it to tokens */
+               $tokens = preg_split('#;#', $typeExtra);
+               
+               /*
+                * 2. if each of tokens includes "=", it consists of key => value
+                *    else it's 'select' option
+                */
+               foreach ( $tokens as $token )
+               {
+                       $matches = array();
+                       if ( preg_match("#^([^=]+)?=([^=]+)?$#", $token, $matches) )
+                       {
+                               $meta[$matches[1]] = $matches[2];
+                       }
+                       else
+                       {
+                               $meta['select'] = $token;
+                       }
+               }
+               return $meta;
+       }
+       
+       /**
+        * NucleusPlugin::getOptionSelectValues()
+        * filters the selectlists out of the meta collection
+        * 
+        * @static
+        * @param string $typeExtra the value of the typeExtra field of an option
+        * @return string the selectlist
+        */
+       static public function getOptionSelectValues($typeExtra)
+       {
+               $meta = NucleusPlugin::getOptionMeta($typeExtra);
+               
+               if ( array_key_exists('select', $meta) )
+               {
+                       return $meta['select'];
+               }
+               return;
+       }
+       
+       /**
+        * 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
+        */
+       public function subscribtionListIsUptodate()
+       {
+               $res = DB::getResult('SELECT event FROM '.sql_table('plugin_event').' WHERE pid = '.$this->plugid);
+               $ev = array();
+               foreach ( $res as $row )
+               {
+                       array_push($ev, $row['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;
+       }
+       
+       /**
+        * NucleusPlugin::apply_plugin_options()
+        * Update its entry in database table
+        * 
+        * @static
+        * @param       $options: array ( 'oid' => array( 'contextid' => 'value'))
+        *                       (taken from request using requestVar())
+        * @param       $new_contextid: 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)
+        * @return void
+        */
+       static public function apply_plugin_options(&$options, $new_contextid = 0)
+       {
+               global $manager;
+               
+               if ( !is_array($options) )
+               {
+                       return;
+               }
+               
+               foreach ( $options as $oid => $values )
+               {
+                       // get option type info
+                       $query = "SELECT opid, oname, ocontext, otype, oextra, odef FROM %s WHERE oid=%d;";
+                       $query = sprintf($query, sql_table('plugin_option_desc'), (integer) $oid);
+                       $result = DB::getRow($query);
+                       if ( $result )
+                       {
+                               foreach ( $values as $id => $value )
+                               {
+                                       // decide wether we are using the contextid of newContextid
+                                       if ( $new_contextid != 0 )
+                                       {
+                                               $contextid = $new_contextid;
+                                       }
+                                       else
+                                       {
+                                               $contextid = $id;
+                                       }
+                                       
+                                       // retreive any metadata
+                                       $meta = NucleusPlugin::getOptionMeta($result['oextra']);
+                                       
+                                       // if the option is readonly or hidden it may not be saved
+                                       if ( array_key_exists('access', $meta)
+                                        && in_array($meta['access'], array('readonly', 'hidden')) )
+                                       {
+                                               return;
+                                       }
+                                       
+                                       // value comes from request
+                                       $value = undoMagic($value);
+                                       
+                                       /* validation the value according to its type */
+                                       switch ( $result['otype'] )
+                                       {
+                                               case 'yesno':
+                                                       if ( ($value != 'yes') && ($value != 'no') )
+                                                       {
+                                                               $value = 'no';
+                                                       }
+                                                       break;
+                                               case 'text':
+                                               case 'select':
+                                                       if ( array_key_exists('datatype', $meta)
+                                                        && ($meta['datatype'] == 'numerical') && ($value != (integer) $value) )
+                                                       {
+                                                               $value = (integer) $result['odef'];
+                                                       }
+                                                       break;
+                                               case 'password':
+                                               case 'textarea':
+                                               default:
+                                                       break;
+                                       }
+                                       
+                                       /*
+                                        * trigger event PrePluginOptionsUpdate to give the plugin the
+                                        * possibility to change/validate the new value for the option
+                                        */
+                                       $data = array(
+                                               'context'               => $result['ocontext'],
+                                               'plugid'                => $result['opid'],
+                                               'optionname'    => $result['oname'],
+                                               'contextid'     => $contextid,
+                                               'value'         => &$value);
+                                       $manager->notify('PrePluginOptionsUpdate', $data);
+                                       
+                                       // delete and insert its fields of table in database
+                                       $query = "DELETE FROM %s WHERE oid=%d AND ocontextid=%d;";
+                                       $query = sprintf($query, sql_table('plugin_option'), (integer) $oid, (integer) $contextid);
+                                       DB::execute($query);
+                                       $query = "INSERT INTO %s (oid, ocontextid, ovalue) VALUES (%d, %d, %s);";
+                                       $query = sprintf($query, sql_table('plugin_option'), (integer) $oid, (integer) $contextid, DB::quoteValue($value));
+                                       DB::execute($query);
+                                       
+                                       // clear option value cache if the plugin object is already loaded
+                                       $plugin=& $manager->pidLoaded($result['opid']);
+                                       if ( $plugin )
+                                       {
+                                               $plugin->clearOptionValueCache();
+                                       }
+                                       
+                                       continue;
+                               }
+                       }
+                       continue;
+               }
+               return;
+       }
+}