OSDN Git Service

Add some codes from 3.61. Currently files under /nucleus/libs and /nucleus/libs/sql...
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / PLUGIN.php
index 071b2de..c07aa26 100755 (executable)
@@ -1,7 +1,7 @@
 <?php
        /*
         * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
-        * Copyright (C) 2002-2007 The Nucleus Group
+        * Copyright (C) 2002-2010 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
@@ -16,9 +16,9 @@
         * 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.9 2007-03-13 05:03:23 shizuki Exp $
-        * $NucleusJP: PLUGIN.php,v 1.8 2007/02/19 22:29:31 kmorimatsu Exp $
+        * @copyright Copyright (C) 2002-2010 The Nucleus Group
+        * @version $Id$
+        * $NucleusJP: PLUGIN.php,v 1.12.2.3 2007/12/03 02:22:42 kmorimatsu Exp $
         */
        class NucleusPlugin {
 
@@ -57,9 +57,9 @@
                        array_unshift($args, 'template');
                        call_user_func_array(array(&$this,'doSkinVar'),$args);
                }
-               function doAction($type) { return 'No Such Action'; }
+               function doAction($type) { return _ERROR_PLUGIN_NOSUCHACTION; }
                function doIf($key,$value) { return false; }
-               function doItemVar () {}
+               function doItemVar (&$item) {}
 
                /**
                 * Checks if a plugin supports a certain feature.
@@ -69,6 +69,7 @@
                 *              Name of the feature. See plugin documentation for more info
                 *                      'SqlTablePrefix' -> if the plugin uses the sql_table() method to get table names
                 *                      'HelpPage' -> if the plugin provides a helppage
+                *                              'SqlApi' -> if the plugin uses the complete sql_* api (must also require nucleuscms 3.5)
                 */
                function supportsFeature($feature) {
                        return 0;
                        if ($this->plugin_options == 0)
                        {
                                $this->plugin_options = array();
-                               $query = mysql_query(
+                               $query = sql_query(
                                         'SELECT d.oname as name, o.ovalue as value '.
                                         'FROM '.
                                         sql_table('plugin_option').' o, '.
                                         sql_table('plugin_option_desc').' d '.
                                         'WHERE d.opid='. intval($this->getID()).' AND d.oid=o.oid'
                                );
-                               while ($row = mysql_fetch_object($query))
+                               while ($row = sql_fetch_object($query))
                                        $this->plugin_options[strtolower($row->name)] = $row->value;
                  }
                  if (isset($this->plugin_options[strtolower($name)]))
                                return $this->plugin_options[strtolower($name)];
                  else
-                       return $this->_getOption('global', 0, $name);
+                               return $this->_getOption('global', 0, $name);
                }
+
                function getBlogOption($blogid, $name) {
                        return $this->_getOption('blog', $blogid, $name);
                }
                }
 
                /**
-                * Retrieves an array of the top (or bottom) of an option from a plugin.
-                * @author TeRanEX
-                * @param  string $context the context for the option: item, blog, member,...
-                * @param  string $name    the name of the option
-                * @param  int    $amount  how many rows must be returned
-                * @param  string $sort    desc or asc
-                * @return array           array with both values and contextid's
-                * @access private
-                */
-               function _getOptionTop($context, $name, $amount = 10, $sort = 'desc') {
-                       if (($sort != 'desc') && ($sort != 'asc')) {
-                               $sort= 'desc';
-                       }
-
-                       $oid = $this->_getOID($context, $name);
-
-                       // retrieve the data and return
-                       $q = 'SELECT otype, oextra FROM '.sql_table('plugin_option_desc').' WHERE oid = '.$oid;
-                       $query = mysql_query($q);
-
-                       $o = mysql_fetch_array($query);
-
-                       if (($this->optionCanBeNumeric($o['otype'])) && ($o['oextra'] == 'number' )) {
-                               $orderby = 'CAST(ovalue AS SIGNED)';
-                       } else {
-                               $orderby = 'ovalue';
-                       }
-                       $q = 'SELECT ovalue value, ocontextid id FROM '.sql_table('plugin_option').' WHERE oid = '.$oid.' ORDER BY '.$orderby.' '.$sort.' LIMIT 0,'.$amount;
-                       $query = mysql_query($q);
-
-                       // create the array
-                       $i = 0;
-                       $top = array();
-                       while($row = mysql_fetch_array($query)) {
-                               $top[$i++] = $row;
-                       }
-
-                       // return the array (duh!)
-                       return $top;
-               }
-
-               /**
                  * Returns the plugin ID
+                 * 
+                 * public                                
                  */
                function getID() {
                        return $this->plugid;
                }
 
                /**
-                 * returns the URL of the admin area for this plugin (in case there's
+                 * Returns the URL of the admin area for this plugin (in case there's
                  * no such area, the returned information is invalid)
+                 * 
+                 * public                                
                  */
                function getAdminURL() {
                        global $CONF;
                /**
                  * Returns the directory where the admin directory is located and
                  * where the plugin can maintain his extra files
+                 * 
+                 * public                                
                  */
                function getDirectory() {
                        global $DIR_PLUGINS;
                }
 
                /**
-                 * Derives the short name for the plugin from the classname (all lowercase)
+                 * Derives the short name for the plugin from the classname (all 
+                 * lowercase)
+                 * 
+                 * public                                
                  */
                function getShortName() {
                        return str_replace('np_','',strtolower(get_class($this)));
                }
 
+               /**
+                *      Clears the option value cache which saves the option values during
+                *      the plugin execution. This function is usefull if the options has 
+                *      changed during the plugin execution (especially in association with
+                *      the PrePluginOptionsUpdate and the PostPluginOptionsUpdate events)
+                *      
+                *  public                               
+                **/                            
+               function clearOptionValueCache(){
+                       $this->_aOptionValues = array();
+                       $this->plugin_options = 0;
+               }
+
+               // internal functions of the class starts here
+
                var $_aOptionValues;    // oid_contextid => value
                var $_aOptionToInfo;    // context_name => array('oid' => ..., 'default' => ...)
                var $plugin_options;    // see getOption()
                var $plugid;                    // plugin id
 
 
-               // constructor. Initializes some internal data
+               /**
+                * Class constructor: Initializes some internal data
+                */                                             
                function NucleusPlugin() {
                        $this->_aOptionValues = array();        // oid_contextid => value
                        $this->_aOptionToInfo = array();        // context_name => array('oid' => ..., 'default' => ...)
                        $this->plugin_options = 0;
                }
 
-               // private
+               /**
+                * Retrieves an array of the top (or bottom) of an option from a plugin.
+                * @author TeRanEX
+                * @param  string $context the context for the option: item, blog, member,...
+                * @param  string $name    the name of the option
+                * @param  int    $amount  how many rows must be returned
+                * @param  string $sort    desc or asc
+                * @return array           array with both values and contextid's
+                * @access private
+                */
+               function _getOptionTop($context, $name, $amount = 10, $sort = 'desc') {
+                       if (($sort != 'desc') && ($sort != 'asc')) {
+                               $sort= 'desc';
+                       }
+
+                       $oid = $this->_getOID($context, $name);
+
+                       // retrieve the data and return
+                       $q = 'SELECT otype, oextra FROM '.sql_table('plugin_option_desc').' WHERE oid = '.$oid;
+                       $query = sql_query($q);
+
+                       $o = sql_fetch_array($query);
+
+                       if (($this->optionCanBeNumeric($o['otype'])) && ($o['oextra'] == 'number' )) {
+                               $orderby = 'CAST(ovalue AS SIGNED)';
+                       } else {
+                               $orderby = 'ovalue';
+                       }
+                       $q = 'SELECT ovalue value, ocontextid id FROM '.sql_table('plugin_option').' WHERE oid = '.$oid.' ORDER BY '.$orderby.' '.$sort.' LIMIT 0,'.intval($amount);
+                       $query = sql_query($q);
+
+                       // create the array
+                       $i = 0;
+                       $top = array();
+                       while($row = sql_fetch_array($query)) {
+                               $top[$i++] = $row;
+                       }
+
+                       // return the array (duh!)
+                       return $top;
+               }
+
+               /**
+                * Creates an option in the database table plugin_option_desc
+                *               
+                * private
+                */                                             
                function _createOption($context, $name, $desc, $type, $defValue, $typeExtras = '') {
                        // create in plugin_option_desc
                        $query = 'INSERT INTO ' . sql_table('plugin_option_desc')
                                   .' (opid, oname, ocontext, odesc, otype, odef, oextra)'
                                   .' VALUES ('.intval($this->plugid)
-                                                        .', \''.addslashes($name).'\''
-                                                        .', \''.addslashes($context).'\''
-                                                        .', \''.addslashes($desc).'\''
-                                                        .', \''.addslashes($type).'\''
-                                                        .', \''.addslashes($defValue).'\''
-                                                        .', \''.addslashes($typeExtras).'\')';
+                                                        .', \''.sql_real_escape_string($name).'\''
+                                                        .', \''.sql_real_escape_string($context).'\''
+                                                        .', \''.sql_real_escape_string($desc).'\''
+                                                        .', \''.sql_real_escape_string($type).'\''
+                                                        .', \''.sql_real_escape_string($defValue).'\''
+                                                        .', \''.sql_real_escape_string($typeExtras).'\')';
                        sql_query($query);
-                       $oid = mysql_insert_id();
+                       $oid = sql_insert_id();
 
                        $key = $context . '_' . $name;
                        $this->_aOptionToInfo[$key] = array('oid' => $oid, 'default' => $defValue);
                }
 
 
-               // private
+               /**
+                * Deletes an option from the database tables
+                * plugin_option and plugin_option_desc 
+                *
+                * private               
+                */                                             
                function _deleteOption($context, $name) {
                        $oid = $this->_getOID($context, $name);
                        if (!$oid) return 0; // no such option
                }
 
                /**
-                * private
+                * Update an option in the database table plugin_option
+                *               
                 * returns: 1 on success, 0 on failure
+                * private
                 */
                function _setOption($context, $contextid, $name, $value) {
                        global $manager;
 
                        // update plugin_option
                        sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid) . ' and ocontextid='. intval($contextid));
-                       @mysql_query('INSERT INTO ' . sql_table('plugin_option') . ' (ovalue, oid, ocontextid) VALUES (\''.addslashes($value).'\', '. intval($oid) . ', ' . intval($contextid) . ')');
+                       sql_query('INSERT INTO ' . sql_table('plugin_option') . ' (ovalue, oid, ocontextid) VALUES (\''.sql_real_escape_string($value).'\', '. intval($oid) . ', ' . intval($contextid) . ')');
 
                        // update cache
                        $this->_aOptionValues[$oid . '_' . $contextid] = $value;
                        return 1;
                }
 
-               // private
+               /**
+                * Get an option from Cache or database
+                *       - if not in the option Cache read it from the database
+                *   - if not in the database write default values into the database
+                *                
+                * private               
+                */                                             
                function _getOption($context, $contextid, $name) {
                        $oid = $this->_getOID($context, $name);
                        if (!$oid) return '';
                        // get from DB
                        $res = sql_query('SELECT ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid).' and ocontextid=' . intval($contextid));
 
-                       if (!$res || (mysql_num_rows($res) == 0)) {
+                       if (!$res || (sql_num_rows($res) == 0)) {
                                $defVal = $this->_getDefVal($context, $name);
                                $this->_aOptionValues[$key] = $defVal;
 
                                // fill DB with default value
                                $query = 'INSERT INTO ' . sql_table('plugin_option') . ' (oid,ocontextid,ovalue)'
-                                          .' VALUES ('.intval($oid).', '.intval($contextid).', \''.addslashes($defVal).'\')';
+                                          .' VALUES ('.intval($oid).', '.intval($contextid).', \''.sql_real_escape_string($defVal).'\')';
                                sql_query($query);
                        }
                        else {
-                               $o = mysql_fetch_object($res);
+                               $o = sql_fetch_object($res);
                                $this->_aOptionValues[$key] = $o->ovalue;
                        }
 
                }
 
                /**
-                * Returns assoc array with all values for a given option (one option per
-                * possible context id)
+                * Returns assoc array with all values for a given option 
+                * (one option per possible context id)
+                * 
+                * private                               
                 */
                function _getAllOptions($context, $name) {
                        $oid = $this->_getOID($context, $name);
                                        break;
                        }
                        if ($r) {
-                               while ($o = mysql_fetch_object($r))
+                               while ($o = sql_fetch_object($r))
                                        $aOptions[$o->contextid] = $defVal;
                        }
 
                        $res = sql_query('SELECT ocontextid, ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);
-                       while ($o = mysql_fetch_object($res))
+                       while ($o = sql_fetch_object($res))
                                $aOptions[$o->ocontextid] = $o->ovalue;
 
                        return $aOptions;
                        $this->_aOptionToInfo = array();
                        $query = 'SELECT oid, oname, ocontext, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE opid=' . intval($this->plugid);
                        $res = sql_query($query);
-                       while ($o = mysql_fetch_object($res)) {
+                       while ($o = sql_fetch_object($res)) {
                                $k = $o->ocontext . '_' . $o->oname;
                                $this->_aOptionToInfo[$k] = array('oid' => $o->oid, 'default' => $o->odef);
                        }
-                       mysql_free_result($res);
+                       sql_free_result($res);
 
                        return $this->_aOptionToInfo[$key]['oid'];
                }
                        // delete all associated plugin options
                        $aOIDs = array();
                                // find ids
-                       $query = 'SELECT oid FROM '.sql_table('plugin_option_desc') . ' WHERE ocontext=\''.addslashes($context).'\'';
+                       $query = 'SELECT oid FROM '.sql_table('plugin_option_desc') . ' WHERE ocontext=\''.sql_real_escape_string($context).'\'';
                        $res = sql_query($query);
-                       while ($o = mysql_fetch_object($res))
+                       while ($o = sql_fetch_object($res))
                                array_push($aOIDs, $o->oid);
-                       mysql_free_result($res);
+                       sql_free_result($res);
                                // delete those options. go go go
                        if (count($aOIDs) > 0) {
                                $query = 'DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid in ('.implode(',',$aOIDs).') and ocontextid=' . intval($contextid);
                function subscribtionListIsUptodate() {
                        $res = sql_query('SELECT event FROM '.sql_table('plugin_event').' WHERE pid = '.$this->getID());
                        $ev = array();
-                       while($a = mysql_fetch_array($res)) {
+                       while($a = sql_fetch_array($res)) {
                                array_push($ev, $a['event']);
                        }
                        if (count($ev) != count($this->getEventList())) {
                                // get option type info
                                $query = 'SELECT opid, oname, ocontext, otype, oextra, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . intval($oid);
                                $res = sql_query($query);
-                               if ($o = mysql_fetch_object($res))
+                               if ($o = sql_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);
 
 
                                                        // delete the old value for the option
                                                        sql_query('DELETE FROM '.sql_table('plugin_option').' WHERE oid='.intval($oid).' AND ocontextid='.intval($contextid));
-                                                       @mysql_query('INSERT INTO '.sql_table('plugin_option')." (oid, ocontextid, ovalue) VALUES (".intval($oid).",".intval($contextid).",'" . addslashes($value) . "')");
+                                                       sql_query('INSERT INTO '.sql_table('plugin_option')." (oid, ocontextid, ovalue) VALUES (".intval($oid).",".intval($contextid).",'" . sql_real_escape_string($value) . "')");
                                                }
                                        }
                                }
+                               // clear option value cache if the plugin object is already loaded
+                               if (is_object($o)) {
+                                       $plugin=& $manager->pidLoaded($o->opid);
+                                       if ($plugin) $plugin->clearOptionValueCache();
+                               }
                        }
                }
-
        }
 ?>
\ No newline at end of file