OSDN Git Service

merged from v3.31sp1
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / PLUGIN.php
index e8fe7e6..9827705 100755 (executable)
@@ -1,20 +1,25 @@
-<?php  /**
-         * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
-         * Copyright (C) 2002-2004 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
-         *
-         * $Id: PLUGIN.php,v 1.1.1.1 2005-02-28 07:14:52 kimitake Exp $
-         */
+<?php
+       /*
+        * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
+        * Copyright (C) 2002-2007 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-2007 The Nucleus Group
+        * @version $Id: PLUGIN.php,v 1.13 2008-02-08 09:31:22 kimitake Exp $
+        * $NucleusJP: PLUGIN.php,v 1.12.2.3 2007/12/03 02:22:42 kmorimatsu Exp $
+        */
        class NucleusPlugin {
 
                // these functions _have_ to be redefined in your plugin
@@ -53,6 +58,8 @@
                        call_user_func_array(array(&$this,'doSkinVar'),$args);
                }
                function doAction($type) { return 'No Such Action'; }
+               function doIf($key,$value) { return false; }
+               function doItemVar () {}
 
                /**
                 * Checks if a plugin supports a certain feature.
@@ -69,7 +76,7 @@
 
                /**
                 * Report a list of plugin that is required to function
-                * 
+                *
                 * @returns an array of names of plugin, an empty array indicates no dependency
                 */
                function getPluginDep() { return array(); }
                function createCategoryOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
                        return $this->_createOption('category', $name, $desc, $type, $defValue, $typeExtras);
                }
-        function createItemOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
+               function createItemOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
                        return $this->_createOption('item', $name, $desc, $type, $defValue, $typeExtras);
                }
 
                function deleteCategoryOption($name) {
                        return $this->_deleteOption('category', $name);
                }
-        function deleteItemOption($name) {
+               function deleteItemOption($name) {
                        return $this->_deleteOption('item', $name);
                }
 
                function setCategoryOption($catid, $name, $value) {
                        return $this->_setOption('category', $catid, $name, $value);
                }
-        function setItemOption($itemid, $name, $value) {
+               function setItemOption($itemid, $name, $value) {
                        return $this->_setOption('item', $itemid, $name, $value);
                }
 
                        // the static collection is used to save SQL queries.
                        if ($this->plugin_options == 0)
                        {
-                               $this->plugin_options = array();        
-                               $query = mysql_query(
+                               $this->plugin_options = array();
+                               $query = sql_query(
                                         'SELECT d.oname as name, o.ovalue as value '.
                                         'FROM '.
                                         sql_table('plugin_option').' o, '.
                                return $this->plugin_options[strtolower($name)];
                  else
                                return $this->_getOption('global', 0, $name);
-               }                 
+               }
 
                function getBlogOption($blogid, $name) {
                        return $this->_getOption('blog', $blogid, $name);
                function getCategoryOption($catid, $name) {
                        return $this->_getOption('category', $catid, $name);
                }
-        function getItemOption($itemid, $name) {
+               function getItemOption($itemid, $name) {
                        return $this->_getOption('item', $itemid, $name);
                }
 
                function getAllCategoryOptions($name) {
                        return $this->_getAllOptions('category', $name);
                }
-        function getAllItemOptions($name) {
+               function getAllItemOptions($name) {
                        return $this->_getAllOptions('item', $name);
                }
-               
+
                /**
-         * Retrieves an indexed array with the top (or bottom) of an option
+                * Retrieves an indexed array with the top (or bottom) of an option
                 * (delegates to _getOptionTop())
-         */
+                */
                function getBlogOptionTop($name, $amount = 10, $sort = 'desc') {
                        return $this->_getOptionTop('blog', $name, $amount, $sort);
                }
                function getItemOptionTop($name, $amount = 10, $sort = 'desc') {
                        return $this->_getOptionTop('item', $name, $amount, $sort);
                }
-               
+
                /**
                 * Retrieves an array of the top (or bottom) of an option from a plugin.
                 * @author TeRanEX
                 * @return array           array with both values and contextid's
                 * @access private
                 */
-        function _getOptionTop($context, $name, $amount = 10, $sort = 'desc') {
+               function _getOptionTop($context, $name, $amount = 10, $sort = 'desc') {
                        if (($sort != 'desc') && ($sort != 'asc')) {
                                $sort= 'desc';
                        }
 
                        // retrieve the data and return
                        $q = 'SELECT otype, oextra FROM '.sql_table('plugin_option_desc').' WHERE oid = '.$oid;
-                       $query = mysql_query($q);
+                       $query = sql_query($q);
 
                        $o = mysql_fetch_array($query);
 
                        } else {
                                $orderby = 'ovalue';
                        }
-                       $q = 'SELECT ovalue value, ocontextid id FROM '.sql_table('plugin_option').' WHERE oid = '.$oid.' ORDER BY '.$orderby.' '.$sort.' LIMIT 0,'.$amount;
-                       $query = mysql_query($q);
-                       
+                       $q = 'SELECT ovalue value, ocontextid id FROM '.sql_table('plugin_option').' WHERE oid = '.$oid.' ORDER BY '.$orderby.' '.$sort.' LIMIT 0,'.intval($amount);
+                       $query = sql_query($q);
+
                        // create the array
                        $i = 0;
                        $top = array();
                        while($row = mysql_fetch_array($query)) {
                                $top[$i++] = $row;
                        }
-                       
+
                        // return the array (duh!)
                        return $top;
                }
                        $this->plugin_options = 0;
                }
 
+               function clearOptionValueCache(){
+                       $this->_aOptionValues = array();
+               }
+
                // private
                function _createOption($context, $name, $desc, $type, $defValue, $typeExtras = '') {
                        // create in plugin_option_desc
                        $query = 'INSERT INTO ' . sql_table('plugin_option_desc')
-                              .' (opid, oname, ocontext, odesc, otype, odef, oextra)'
-                              .' VALUES ('.intval($this->plugid)
-                             .', \''.addslashes($name).'\''
-                             .', \''.addslashes($context).'\''
-                             .', \''.addslashes($desc).'\''
-                             .', \''.addslashes($type).'\''
-                             .', \''.addslashes($defValue).'\''
-                                        .', \''.addslashes($typeExtras).'\')';
+                                  .' (opid, oname, ocontext, odesc, otype, odef, oextra)'
+                                  .' VALUES ('.intval($this->plugid)
+                                                        .', \''.addslashes($name).'\''
+                                                        .', \''.addslashes($context).'\''
+                                                        .', \''.addslashes($desc).'\''
+                                                        .', \''.addslashes($type).'\''
+                                                        .', \''.addslashes($defValue).'\''
+                                                        .', \''.addslashes($typeExtras).'\')';
                        sql_query($query);
                        $oid = mysql_insert_id();
 
                                case 'category':
                                        if (!$manager->existsCategory($contextid)) return 0;
                                        break;
-                case 'item':
-                    if (!$manager->existsItem($contextid, true, true)) return 0;
+                               case 'item':
+                                       if (!$manager->existsItem($contextid, true, true)) return 0;
                                        break;
                                case 'global':
                                        if ($contextid != 0) return 0;
 
                                // fill DB with default value
                                $query = 'INSERT INTO ' . sql_table('plugin_option') . ' (oid,ocontextid,ovalue)'
-                                      .' VALUES ('.intval($oid).', '.intval($contextid).', \''.addslashes($defVal).'\')';
+                                          .' VALUES ('.intval($oid).', '.intval($contextid).', \''.addslashes($defVal).'\')';
                                sql_query($query);
                        }
                        else {
                                case 'member':
                                        $r = sql_query('SELECT mnumber as contextid FROM ' . sql_table('member'));
                                        break;
+                               case 'item':
+                                       $r = sql_query('SELECT inumber as contextid FROM ' . sql_table('item'));
+                                       break;
                        }
                        if ($r) {
                                while ($o = mysql_fetch_object($r))
                 */
                function _getOID($context, $name) {
                        $key = $context . '_' . $name;
-                       $info = $this->_aOptionToInfo[$key];
+                       $info = @$this->_aOptionToInfo[$key];
                        if (is_array($info)) return $info['oid'];
 
                        // load all OIDs for this plugin from the database
                        }
                        mysql_free_result($res);
 
-                       return $this->_aOptionToInfo[$key]['oid'];
+                       return @$this->_aOptionToInfo[$key]['oid'];
                }
                function _getDefVal($context, $name) {
                        $key = $context . '_' . $name;
                        //the select list must always be the first part
                        return $meta['select'];
                }
-               
+
+               /**
+                * checks if the eventlist in the database is up-to-date
+                * @return bool if it is up-to-date it return true, else false
+                * @author TeRanEX
+                */
+               function subscribtionListIsUptodate() {
+                       $res = sql_query('SELECT event FROM '.sql_table('plugin_event').' WHERE pid = '.$this->getID());
+                       $ev = array();
+                       while($a = mysql_fetch_array($res)) {
+                               array_push($ev, $a['event']);
+                       }
+                       if (count($ev) != count($this->getEventList())) {
+                               return false;
+                       }
+                       $d = array_diff($ev, $this->getEventList());
+                       if (count($d) > 0) {
+                               // there are differences so the db is not up-to-date
+                               return false;
+                       }
+                       return true;
+               }
+
                /**
                 * @param $aOptions: array ( 'oid' => array( 'contextid' => 'value'))
                 *        (taken from request using requestVar())
-                * @param $newContextid: integer (accepts a contextid when it is for a new 
+                * @param $newContextid: integer (accepts a contextid when it is for a new
                 *        contextid there was no id available at the moment of writing the
                 *        formcontrols into the page (by ex: itemOptions for new item)
-                * @static 
+                * @static
                 */
                function _applyPluginOptions(&$aOptions, $newContextid = 0) {
                        global $manager;
                                $res = sql_query($query);
                                if ($o = mysql_fetch_object($res))
                                {
-                                       foreach ($values as $contextid => $value) {
+                                       foreach ($values as $key => $value) {
+                                               // avoid overriding the key used by foreach statement
+                                               $contextid=$key;
+
                                                // retreive any metadata
                                                $meta = NucleusPlugin::getOptionMeta($o->oextra);
-                                               
+
                                                // if the option is readonly or hidden it may not be saved
-                                               if (($meta['access'] != 'readonly') && ($meta['access'] != 'hidden')) {
-                                                       
+                                               if ((@$meta['access'] != 'readonly') && (@$meta['access'] != 'hidden')) {
+
                                                        $value = undoMagic($value);     // value comes from request
-       
+
                                                        switch($o->otype) {
                                                                case 'yesno':
                                                                        if (($value != 'yes') && ($value != 'no')) $value = 'no';
                                                                default:
                                                                        break;
                                                        }
-                                                       
+
                                                        // check the validity of numerical options
-                                                       if (($meta['datatype'] == 'numerical') && (!is_numeric($value))) { 
+                                                       if ((@$meta['datatype'] == 'numerical') && (!is_numeric($value))) {
                                                                //the option must be numeric, but the it isn't
                                                                //use the default for this option
                                                                $value = $o->odef;
                                                        }
-       
+
                                                        // decide wether we are using the contextid of newContextid
                                                        if ($newContextid != 0) {
                                                                $contextid = $newContextid;
                                                        }
-                                                       
+
                                                        //trigger event PrePluginOptionsUpdate to give the plugin the
                                                        //possibility to change/validate the new value for the option
                                                        $manager->notify('PrePluginOptionsUpdate',array('context' => $o->ocontext, 'plugid' => $o->opid, 'optionname' => $o->oname, 'contextid' => $contextid, 'value' => &$value));
-                                                       
+
                                                        // delete the old value for the option
                                                        sql_query('DELETE FROM '.sql_table('plugin_option').' WHERE oid='.intval($oid).' AND ocontextid='.intval($contextid));
                                                        sql_query('INSERT INTO '.sql_table('plugin_option')." (oid, ocontextid, ovalue) VALUES (".intval($oid).",".intval($contextid).",'" . addslashes($value) . "')");
                                                }
                                        }
                                }
+                               // clear option value cache if the plugin object is already loaded
+                               if (is_object($o)) {
+                                       $plugin=& $manager->pidLoaded($o->opid);
+                                       if ($plugin) $plugin->clearOptionValueCache();
+                               }
                        }
                }
-               
        }
 ?>