OSDN Git Service

Merge branch 'skinnable-master'
[nucleus-jp/nucleus-next.git] / nucleus / libs / PLUGIN.php
index 4cebb26..b52b93c 100644 (file)
@@ -1,3 +1,989 @@
+<<<<<<< HEAD
+<?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
+=======
 <?php
 /*
  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
@@ -982,3 +1968,4 @@ abstract class NucleusPlugin
                return;
        }
 }
+>>>>>>> skinnable-master