[ Index ]

PHP Cross Reference of Nucleus CMS 3.32

title

Body

[close]

/nucleus/libs/ -> PLUGIN.php (source)

   1  <?php
   2      /*
   3       * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
   4       * Copyright (C) 2002-2007 The Nucleus Group
   5       *
   6       * This program is free software; you can redistribute it and/or
   7       * modify it under the terms of the GNU General Public License
   8       * as published by the Free Software Foundation; either version 2
   9       * of the License, or (at your option) any later version.
  10       * (see nucleus/documentation/index.html#license for more info)
  11       */
  12      /**
  13       * This is an (abstract) class of which all Nucleus Plugins must inherit
  14       *
  15       * for more information on plugins and how to write your own, see the
  16       * plugins.html file that is included with the Nucleus documenation
  17       *
  18       * @license http://nucleuscms.org/license.txt GNU General Public License
  19       * @copyright Copyright (C) 2002-2007 The Nucleus Group
  20       * @version $Id: PLUGIN.php 1196 2007-09-05 07:39:40Z kimitake $
  21       */
  22      class NucleusPlugin {
  23  
  24          // these functions _have_ to be redefined in your plugin
  25  
  26  		function getName() { return 'Undefined'; }
  27  		function getAuthor()  { return 'Undefined'; }
  28  		function getURL()  { return 'Undefined'; }
  29  		function getVersion() { return '0.0'; }
  30  		function getDescription() { return 'Undefined';}
  31  
  32          // these function _may_ be redefined in your plugin
  33  
  34  		function getMinNucleusVersion() { return 150; }
  35  		function getMinNucleusPatchLevel() { return 0; }
  36  		function getEventList() { return array(); }
  37  		function getTableList() { return array(); }
  38  		function hasAdminArea() { return 0; }
  39  
  40  		function install() {}
  41  		function unInstall() {}
  42  
  43  		function init() {}
  44  
  45  		function doSkinVar($skinType) {}
  46  		function doTemplateVar(&$item) {
  47              $args = func_get_args();
  48              array_shift($args);
  49              array_unshift($args, 'template');
  50              call_user_func_array(array(&$this,'doSkinVar'),$args);
  51          }
  52  		function doTemplateCommentsVar(&$item, &$comment) {
  53              $args = func_get_args();
  54              array_shift($args);
  55              array_shift($args);
  56              array_unshift($args, 'template');
  57              call_user_func_array(array(&$this,'doSkinVar'),$args);
  58          }
  59  		function doAction($type) { return 'No Such Action'; }
  60  		function doIf($key,$value) { return false; }
  61  		function doItemVar () {}
  62  
  63          /**
  64           * Checks if a plugin supports a certain feature.
  65           *
  66           * @returns 1 if the feature is reported, 0 if not
  67           * @param $feature
  68           *        Name of the feature. See plugin documentation for more info
  69           *            'SqlTablePrefix' -> if the plugin uses the sql_table() method to get table names
  70           *            'HelpPage' -> if the plugin provides a helppage
  71           */
  72  		function supportsFeature($feature) {
  73              return 0;
  74          }
  75  
  76          /**
  77           * Report a list of plugin that is required to function
  78           *
  79           * @returns an array of names of plugin, an empty array indicates no dependency
  80           */
  81  		function getPluginDep() { return array(); }
  82  
  83          // these helper functions should not be redefined in your plugin
  84  
  85          /**
  86            * Creates a new option for this plugin
  87            *
  88            * @param name
  89            *        A string uniquely identifying your option. (max. length is 20 characters)
  90            * @param description
  91            *        A description that will show up in the nucleus admin area (max. length: 255 characters)
  92            * @param type
  93            *        Either 'text', 'yesno' or 'password'
  94            *        This info is used when showing 'edit plugin options' screens
  95            * @param value
  96            *        Initial value for the option (max. value length is 128 characters)
  97            */
  98  		function createOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
  99              return $this->_createOption('global', $name, $desc, $type, $defValue, $typeExtras);
 100          }
 101  		function createBlogOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
 102              return $this->_createOption('blog', $name, $desc, $type, $defValue, $typeExtras);
 103          }
 104  		function createMemberOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
 105              return $this->_createOption('member', $name, $desc, $type, $defValue, $typeExtras);
 106          }
 107  		function createCategoryOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
 108              return $this->_createOption('category', $name, $desc, $type, $defValue, $typeExtras);
 109          }
 110  		function createItemOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
 111              return $this->_createOption('item', $name, $desc, $type, $defValue, $typeExtras);
 112          }
 113  
 114          /**
 115            * Removes the option from the database
 116            *
 117            * Note: Options get erased automatically on plugin uninstall
 118            */
 119  		function deleteOption($name) {
 120              return $this->_deleteOption('global', $name);
 121          }
 122  		function deleteBlogOption($name) {
 123              return $this->_deleteOption('blog', $name);
 124          }
 125  		function deleteMemberOption($name) {
 126              return $this->_deleteOption('member', $name);
 127          }
 128  		function deleteCategoryOption($name) {
 129              return $this->_deleteOption('category', $name);
 130          }
 131  		function deleteItemOption($name) {
 132              return $this->_deleteOption('item', $name);
 133          }
 134  
 135          /**
 136            * Sets the value of an option to something new
 137            */
 138  		function setOption($name, $value) {
 139              return $this->_setOption('global', 0, $name, $value);
 140          }
 141  		function setBlogOption($blogid, $name, $value) {
 142              return $this->_setOption('blog', $blogid, $name, $value);
 143          }
 144  		function setMemberOption($memberid, $name, $value) {
 145              return $this->_setOption('member', $memberid, $name, $value);
 146          }
 147  		function setCategoryOption($catid, $name, $value) {
 148              return $this->_setOption('category', $catid, $name, $value);
 149          }
 150  		function setItemOption($itemid, $name, $value) {
 151              return $this->_setOption('item', $itemid, $name, $value);
 152          }
 153  
 154          /**
 155            * Retrieves the current value for an option
 156            */
 157  		function getOption($name)
 158          {
 159              // only request the options the very first time. On subsequent requests
 160              // the static collection is used to save SQL queries.
 161              if ($this->plugin_options == 0)
 162              {
 163                  $this->plugin_options = array();
 164                  $query = sql_query(
 165                       'SELECT d.oname as name, o.ovalue as value '.
 166                       'FROM '.
 167                       sql_table('plugin_option').' o, '.
 168                       sql_table('plugin_option_desc').' d '.
 169                       'WHERE d.opid='. intval($this->getID()).' AND d.oid=o.oid'
 170                  );
 171                  while ($row = mysql_fetch_object($query))
 172                      $this->plugin_options[strtolower($row->name)] = $row->value;
 173            }
 174            if (isset($this->plugin_options[strtolower($name)]))
 175                  return $this->plugin_options[strtolower($name)];
 176            else
 177                  return $this->_getOption('global', 0, $name);
 178          }
 179  
 180  		function getBlogOption($blogid, $name) {
 181              return $this->_getOption('blog', $blogid, $name);
 182          }
 183  		function getMemberOption($memberid, $name) {
 184              return $this->_getOption('member', $memberid, $name);
 185          }
 186  		function getCategoryOption($catid, $name) {
 187              return $this->_getOption('category', $catid, $name);
 188          }
 189  		function getItemOption($itemid, $name) {
 190              return $this->_getOption('item', $itemid, $name);
 191          }
 192  
 193          /**
 194           * Retrieves an associative array with the option value for each
 195           * context id
 196           */
 197  		function getAllBlogOptions($name) {
 198              return $this->_getAllOptions('blog', $name);
 199          }
 200  		function getAllMemberOptions($name) {
 201              return $this->_getAllOptions('member', $name);
 202          }
 203  		function getAllCategoryOptions($name) {
 204              return $this->_getAllOptions('category', $name);
 205          }
 206  		function getAllItemOptions($name) {
 207              return $this->_getAllOptions('item', $name);
 208          }
 209  
 210          /**
 211           * Retrieves an indexed array with the top (or bottom) of an option
 212           * (delegates to _getOptionTop())
 213           */
 214  		function getBlogOptionTop($name, $amount = 10, $sort = 'desc') {
 215              return $this->_getOptionTop('blog', $name, $amount, $sort);
 216          }
 217  		function getMemberOptionTop($name, $amount = 10, $sort = 'desc') {
 218              return $this->_getOptionTop('member', $name, $amount, $sort);
 219          }
 220  		function getCategoryOptionTop($name, $amount = 10, $sort = 'desc') {
 221              return $this->_getOptionTop('category', $name, $amount, $sort);
 222          }
 223  		function getItemOptionTop($name, $amount = 10, $sort = 'desc') {
 224              return $this->_getOptionTop('item', $name, $amount, $sort);
 225          }
 226  
 227          /**
 228           * Retrieves an array of the top (or bottom) of an option from a plugin.
 229           * @author TeRanEX
 230           * @param  string $context the context for the option: item, blog, member,...
 231           * @param  string $name    the name of the option
 232           * @param  int    $amount  how many rows must be returned
 233           * @param  string $sort    desc or asc
 234           * @return array           array with both values and contextid's
 235           * @access private
 236           */
 237  		function _getOptionTop($context, $name, $amount = 10, $sort = 'desc') {
 238              if (($sort != 'desc') && ($sort != 'asc')) {
 239                  $sort= 'desc';
 240              }
 241  
 242              $oid = $this->_getOID($context, $name);
 243  
 244              // retrieve the data and return
 245              $q = 'SELECT otype, oextra FROM '.sql_table('plugin_option_desc').' WHERE oid = '.$oid;
 246              $query = sql_query($q);
 247  
 248              $o = mysql_fetch_array($query);
 249  
 250              if (($this->optionCanBeNumeric($o['otype'])) && ($o['oextra'] == 'number' )) {
 251                  $orderby = 'CAST(ovalue AS SIGNED)';
 252              } else {
 253                  $orderby = 'ovalue';
 254              }
 255              $q = 'SELECT ovalue value, ocontextid id FROM '.sql_table('plugin_option').' WHERE oid = '.$oid.' ORDER BY '.$orderby.' '.$sort.' LIMIT 0,'.intval($amount);
 256              $query = sql_query($q);
 257  
 258              // create the array
 259              $i = 0;
 260              $top = array();
 261              while($row = mysql_fetch_array($query)) {
 262                  $top[$i++] = $row;
 263              }
 264  
 265              // return the array (duh!)
 266              return $top;
 267          }
 268  
 269          /**
 270            * Returns the plugin ID
 271            */
 272  		function getID() {
 273              return $this->plugid;
 274          }
 275  
 276          /**
 277            * returns the URL of the admin area for this plugin (in case there's
 278            * no such area, the returned information is invalid)
 279            */
 280  		function getAdminURL() {
 281              global $CONF;
 282              return $CONF['PluginURL'] . $this->getShortName() . '/';
 283          }
 284  
 285          /**
 286            * Returns the directory where the admin directory is located and
 287            * where the plugin can maintain his extra files
 288            */
 289  		function getDirectory() {
 290              global $DIR_PLUGINS;
 291              return $DIR_PLUGINS . $this->getShortName() . '/';
 292          }
 293  
 294          /**
 295            * Derives the short name for the plugin from the classname (all lowercase)
 296            */
 297  		function getShortName() {
 298              return str_replace('np_','',strtolower(get_class($this)));
 299          }
 300  
 301          var $_aOptionValues;    // oid_contextid => value
 302          var $_aOptionToInfo;    // context_name => array('oid' => ..., 'default' => ...)
 303          var $plugin_options;    // see getOption()
 304          var $plugid;            // plugin id
 305  
 306  
 307          // constructor. Initializes some internal data
 308  		function NucleusPlugin() {
 309              $this->_aOptionValues = array();    // oid_contextid => value
 310              $this->_aOptionToInfo = array();    // context_name => array('oid' => ..., 'default' => ...)
 311              $this->plugin_options = 0;
 312          }
 313  
 314  		function clearOptionValueCache(){
 315              $this->_aOptionValues = array();
 316          }
 317  
 318          // private
 319  		function _createOption($context, $name, $desc, $type, $defValue, $typeExtras = '') {
 320              // create in plugin_option_desc
 321              $query = 'INSERT INTO ' . sql_table('plugin_option_desc')
 322                     .' (opid, oname, ocontext, odesc, otype, odef, oextra)'
 323                     .' VALUES ('.intval($this->plugid)
 324                               .', \''.addslashes($name).'\''
 325                               .', \''.addslashes($context).'\''
 326                               .', \''.addslashes($desc).'\''
 327                               .', \''.addslashes($type).'\''
 328                               .', \''.addslashes($defValue).'\''
 329                               .', \''.addslashes($typeExtras).'\')';
 330              sql_query($query);
 331              $oid = mysql_insert_id();
 332  
 333              $key = $context . '_' . $name;
 334              $this->_aOptionToInfo[$key] = array('oid' => $oid, 'default' => $defValue);
 335              return 1;
 336          }
 337  
 338  
 339          // private
 340  		function _deleteOption($context, $name) {
 341              $oid = $this->_getOID($context, $name);
 342              if (!$oid) return 0; // no such option
 343  
 344              // delete all things from plugin_option
 345              sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);
 346  
 347              // delete entry from plugin_option_desc
 348              sql_query('DELETE FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . $oid);
 349  
 350              // clear from cache
 351              unset($this->_aOptionToInfo[$context . '_' . $name]);
 352              $this->_aOptionValues = array();
 353              return 1;
 354          }
 355  
 356          /**
 357           * private
 358           * returns: 1 on success, 0 on failure
 359           */
 360  		function _setOption($context, $contextid, $name, $value) {
 361              global $manager;
 362  
 363              $oid = $this->_getOID($context, $name);
 364              if (!$oid) return 0;
 365  
 366              // check if context id exists
 367              switch ($context) {
 368                  case 'member':
 369                      if (!MEMBER::existsID($contextid)) return 0;
 370                      break;
 371                  case 'blog':
 372                      if (!$manager->existsBlogID($contextid)) return 0;
 373                      break;
 374                  case 'category':
 375                      if (!$manager->existsCategory($contextid)) return 0;
 376                      break;
 377                  case 'item':
 378                      if (!$manager->existsItem($contextid, true, true)) return 0;
 379                      break;
 380                  case 'global':
 381                      if ($contextid != 0) return 0;
 382                      break;
 383              }
 384  
 385  
 386              // update plugin_option
 387              sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid) . ' and ocontextid='. intval($contextid));
 388              sql_query('INSERT INTO ' . sql_table('plugin_option') . ' (ovalue, oid, ocontextid) VALUES (\''.addslashes($value).'\', '. intval($oid) . ', ' . intval($contextid) . ')');
 389  
 390              // update cache
 391              $this->_aOptionValues[$oid . '_' . $contextid] = $value;
 392  
 393              return 1;
 394          }
 395  
 396          // private
 397  		function _getOption($context, $contextid, $name) {
 398              $oid = $this->_getOID($context, $name);
 399              if (!$oid) return '';
 400  
 401  
 402              $key = $oid . '_' . $contextid;
 403  
 404              if (isset($this->_aOptionValues[$key]))
 405                  return $this->_aOptionValues[$key];
 406  
 407              // get from DB
 408              $res = sql_query('SELECT ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid).' and ocontextid=' . intval($contextid));
 409  
 410              if (!$res || (mysql_num_rows($res) == 0)) {
 411                  $defVal = $this->_getDefVal($context, $name);
 412                  $this->_aOptionValues[$key] = $defVal;
 413  
 414                  // fill DB with default value
 415                  $query = 'INSERT INTO ' . sql_table('plugin_option') . ' (oid,ocontextid,ovalue)'
 416                         .' VALUES ('.intval($oid).', '.intval($contextid).', \''.addslashes($defVal).'\')';
 417                  sql_query($query);
 418              }
 419              else {
 420                  $o = mysql_fetch_object($res);
 421                  $this->_aOptionValues[$key] = $o->ovalue;
 422              }
 423  
 424              return $this->_aOptionValues[$key];
 425          }
 426  
 427          /**
 428           * Returns assoc array with all values for a given option (one option per
 429           * possible context id)
 430           */
 431  		function _getAllOptions($context, $name) {
 432              $oid = $this->_getOID($context, $name);
 433              if (!$oid) return array();
 434              $defVal = $this->_getDefVal($context, $name);
 435  
 436              $aOptions = array();
 437              switch ($context) {
 438                  case 'blog':
 439                      $r = sql_query('SELECT bnumber as contextid FROM ' . sql_table('blog'));
 440                      break;
 441                  case 'category':
 442                      $r = sql_query('SELECT catid as contextid FROM ' . sql_table('category'));
 443                      break;
 444                  case 'member':
 445                      $r = sql_query('SELECT mnumber as contextid FROM ' . sql_table('member'));
 446                      break;
 447                  case 'item':
 448                      $r = sql_query('SELECT inumber as contextid FROM ' . sql_table('item'));
 449                      break;
 450              }
 451              if (