[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/inc/ -> class_plugins.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.6
   4   * Copyright 2010 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://mybb.com
   7   * License: http://mybb.com/about/license
   8   *
   9   * $Id$
  10   */
  11  
  12  class pluginSystem
  13  {
  14  
  15      /**
  16       * The hooks to which plugins can be attached.
  17       *
  18       * @var array
  19       */
  20      public $hooks;
  21  
  22      /**
  23       * The current hook which we're in (if any)
  24       *
  25       * @var string
  26       */
  27      public $current_hook;
  28  
  29      /**
  30       * Load all plugins.
  31       *
  32       */
  33  	function load()
  34      {
  35          global $cache, $plugins;
  36          $pluginlist = $cache->read("plugins");
  37          if(!empty($pluginlist['active']) && is_array($pluginlist['active']))
  38          {
  39              foreach($pluginlist['active'] as $plugin)
  40              {
  41                  if($plugin != "" && file_exists(MYBB_ROOT."inc/plugins/".$plugin.".php"))
  42                  {
  43                      require_once MYBB_ROOT."inc/plugins/".$plugin.".php";
  44                  }
  45              }
  46          }
  47      }
  48  
  49      /**
  50       * Add a hook onto which a plugin can be attached.
  51       *
  52       * @param string The hook name.
  53       * @param string The function of this hook.
  54       * @param int The priority this hook has.
  55       * @param string The optional file belonging to this hook.
  56       * @return boolean Always true.
  57       */
  58  	function add_hook($hook, $function, $priority=10, $file="")
  59      {
  60          // Check to see if we already have this hook running at this priority
  61          if(!empty($this->hooks[$hook][$priority][$function]) && is_array($this->hooks[$hook][$priority][$function]))
  62          {
  63              return true;
  64          }
  65  
  66          // Add the hook
  67          $this->hooks[$hook][$priority][$function] = array(
  68              "function" => $function,
  69              "file" => $file
  70          );
  71          return true;
  72      }
  73  
  74      /**
  75       * Run the hooks that have plugins.
  76       *
  77       * @param string The name of the hook that is run.
  78       * @param string The argument for the hook that is run. The passed value MUST be a variable
  79       * @return string The arguments for the hook.
  80       */
  81  	function run_hooks($hook, &$arguments="")
  82      {
  83          if(!isset($this->hooks[$hook]) || !is_array($this->hooks[$hook]))
  84          {
  85              return $arguments;
  86          }
  87          $this->current_hook = $hook;
  88          ksort($this->hooks[$hook]);
  89          foreach($this->hooks[$hook] as $priority => $hooks)
  90          {
  91              if(is_array($hooks))
  92              {
  93                  foreach($hooks as $hook)
  94                  {
  95                      if($hook['file'])
  96                      {
  97                          require_once $hook['file'];
  98                      }
  99                      
 100                      $func = $hook['function'];
 101                      $returnargs = $func($arguments);
 102                      
 103                      
 104                      if($returnargs)
 105                      {
 106                          $arguments = $returnargs;
 107                      }
 108                  }
 109              }
 110          }
 111          $this->current_hook = '';
 112          return $arguments;
 113      }
 114      
 115      /**
 116       * Run hooks by ref
 117       * Wrapper for run hooks to maintain backwards compatibility
 118       */
 119  	function run_hooks_by_ref($hook, &$arguments)
 120      {
 121          $this->run_hooks($hook, $arguments);
 122      }
 123  
 124      /**
 125       * Remove a specific hook.
 126       *
 127       * @param string The name of the hook.
 128       * @param string The function of the hook.
 129       * @param string The filename of the plugin.
 130       * @param int The priority of the hook.
 131       */
 132  	function remove_hook($hook, $function, $file="", $priority=10)
 133      {
 134          // Check to see if we don't already have this hook running at this priority
 135          if(!isset($this->hooks[$hook][$priority][$function]))
 136          {
 137              return true;
 138          }
 139          unset($this->hooks[$hook][$priority][$function]);
 140      }
 141  
 142      /**
 143       * Establishes if a particular plugin is compatible with this version of MyBB.
 144       *
 145       * @param string The name of the plugin.
 146       * @return boolean TRUE if compatible, FALSE if incompatible.
 147       */
 148  	function is_compatible($plugin)
 149      {
 150          global $mybb;
 151  
 152          // Ignore potentially missing plugins.
 153          if(!file_exists(MYBB_ROOT."inc/plugins/".$plugin.".php"))
 154          {
 155              return true;
 156          }
 157  
 158          require_once MYBB_ROOT."inc/plugins/".$plugin.".php";
 159  
 160          $info_func = "{$plugin}_info";
 161          if(!function_exists($info_func))
 162          {
 163              return false;
 164          }
 165          $plugin_info = $info_func();
 166          
 167          // No compatibility set or compatibility = * - assume compatible
 168          if(!$plugin_info['compatibility'] || $plugin_info['compatibility'] == "*")
 169          {
 170              return true;
 171          }
 172          $compatibility = explode(",", $plugin_info['compatibility']);
 173          foreach($compatibility as $version)
 174          {
 175              $version = trim($version);
 176              $version = str_replace("*", ".+", preg_quote($version));
 177              $version = str_replace("\.+", ".+", $version);
 178              if(preg_match("#{$version}#i", $mybb->version_code))
 179              {
 180                  return true;
 181              }
 182          }
 183  
 184          // Nothing matches
 185          return false;
 186      }
 187  }
 188  ?>


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1