[ Index ]

PHP Cross Reference of MyBB 1.8.37

title

Body

[close]

/inc/ -> class_plugins.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   * Website: http://www.mybb.com
   6   * License: http://www.mybb.com/about/license
   7   */
   8  
   9  class pluginSystem
  10  {
  11      /**
  12       * The hooks to which plugins can be attached.
  13       *
  14       * @var array
  15       */
  16      public $hooks;
  17      /**
  18       * The current hook which we're in (if any)
  19       *
  20       * @var string
  21       */
  22      public $current_hook;
  23  
  24      /**
  25       * Load all plugins.
  26       */
  27  	function load()
  28      {
  29          global $cache, $plugins;
  30  
  31          $plugin_list = $cache->read("plugins");
  32          if(!empty($plugin_list['active']) && is_array($plugin_list['active']))
  33          {
  34              foreach($plugin_list['active'] as $plugin)
  35              {
  36                  if($plugin != "" && file_exists(MYBB_ROOT."inc/plugins/".$plugin.".php"))
  37                  {
  38                      require_once MYBB_ROOT."inc/plugins/".$plugin.".php";
  39                  }
  40              }
  41          }
  42      }
  43  
  44      /**
  45       * Add a hook onto which a plugin can be attached.
  46       *
  47       * @param string       $hook     The hook name.
  48       * @param array|string $function The function of this hook.
  49       * @param int          $priority The priority this hook has.
  50       * @param string       $file     The optional file belonging to this hook.
  51       * @return boolean Whether the hook was added.
  52       */
  53  	function add_hook($hook, $function, $priority=10, $file="")
  54      {
  55          if(is_array($function))
  56          {
  57              if(!count($function) == 2)
  58              { // must be an array of two items!
  59                  return false;
  60              }
  61  
  62              if(is_string($function[0]))
  63              {
  64                  // Static class method
  65                  $method_representation = sprintf('%s::%s', $function[0], $function[1]);
  66              }
  67              elseif(is_object($function[0]))
  68              {
  69                  // Instance class method
  70                  $method_representation = sprintf('%s->%s', spl_object_hash($function[0]), $function[1]);
  71              }
  72              else
  73              {
  74                  // Unknown array type
  75                  return false;
  76              }
  77  
  78              // Check to see if we already have this hook running at this priority
  79              if(!empty($this->hooks[$hook][$priority][$method_representation]) && is_array($this->hooks[$hook][$priority][$method_representation]))
  80              {
  81                  return true;
  82              }
  83  
  84              // Add the hook
  85              $this->hooks[$hook][$priority][$method_representation] = array(
  86                  'class_method' => $function,
  87                  'file'         => $file
  88              );
  89          }
  90          else
  91          {
  92              // Check to see if we already have this hook running at this priority
  93              if(!empty($this->hooks[$hook][$priority][$function]) && is_array($this->hooks[$hook][$priority][$function]))
  94              {
  95                  return true;
  96              }
  97  
  98              // Add the hook
  99              $this->hooks[$hook][$priority][$function] = array(
 100                  'function' => $function,
 101                  'file'     => $file
 102              );
 103          }
 104  
 105          return true;
 106      }
 107  
 108      /**
 109       * Run the hooks that have plugins.
 110       *
 111       * @param string $hook      The name of the hook that is run.
 112       * @param mixed $arguments The argument for the hook that is run. The passed value MUST be a variable
 113       * @return mixed The arguments for the hook.
 114       */
 115  	function run_hooks($hook, &$arguments="")
 116      {
 117          if(!isset($this->hooks[$hook]) || !is_array($this->hooks[$hook]))
 118          {
 119              return $arguments;
 120          }
 121          $this->current_hook = $hook;
 122          ksort($this->hooks[$hook]);
 123          foreach($this->hooks[$hook] as $priority => $hooks)
 124          {
 125              if(is_array($hooks))
 126              {
 127                  foreach($hooks as $key => $hook)
 128                  {
 129                      if($hook['file'])
 130                      {
 131                          require_once $hook['file'];
 132                      }
 133  
 134                      if(array_key_exists('class_method', $hook))
 135                      {
 136                          $return_args = call_user_func_array($hook['class_method'], array(&$arguments));
 137                      }
 138                      else
 139                      {
 140                          $func = $hook['function'];
 141  
 142                          $return_args = $func($arguments);
 143                      }
 144  
 145                      if($return_args)
 146                      {
 147                          $arguments = $return_args;
 148                      }
 149                  }
 150              }
 151          }
 152          $this->current_hook = '';
 153  
 154          return $arguments;
 155      }
 156  
 157      /**
 158       * Remove a specific hook.
 159       *
 160       * @param string       $hook     The name of the hook.
 161       * @param array|string $function The function of the hook.
 162       * @param string       $file     The filename of the plugin.
 163       * @param int          $priority The priority of the hook.
 164       * @return bool Whether the hook was removed successfully.
 165       */
 166  	function remove_hook($hook, $function, $file="", $priority=10)
 167      {
 168          if(is_array($function))
 169          {
 170              if(is_string($function[0]))
 171              { // Static class method
 172                  $method_representation = sprintf('%s::%s', $function[0], $function[1]);
 173              }
 174              elseif(is_object($function[0]))
 175              { // Instance class method
 176                  $method_representation = sprintf('%s->%s', get_class($function[0]), $function[1]);
 177              }
 178              else
 179              { // Unknown array type
 180                  return false;
 181              }
 182  
 183              if(!isset($this->hooks[$hook][$priority][$method_representation]))
 184              {
 185                  return true;
 186              }
 187              unset($this->hooks[$hook][$priority][$method_representation]);
 188          }
 189          else
 190          {
 191              // Check to see if we don't already have this hook running at this priority
 192              if(!isset($this->hooks[$hook][$priority][$function]))
 193              {
 194                  return true;
 195              }
 196              unset($this->hooks[$hook][$priority][$function]);
 197          }
 198  
 199          return true;
 200      }
 201  
 202      /**
 203       * Establishes if a particular plugin is compatible with this version of MyBB.
 204       *
 205       * @param string $plugin The name of the plugin.
 206       * @return boolean TRUE if compatible, FALSE if incompatible.
 207       */
 208  	function is_compatible($plugin)
 209      {
 210          global $mybb;
 211  
 212          // Ignore potentially missing plugins.
 213          if(!file_exists(MYBB_ROOT."inc/plugins/".$plugin.".php"))
 214          {
 215              return true;
 216          }
 217  
 218          require_once MYBB_ROOT."inc/plugins/".$plugin.".php";
 219  
 220          $info_func = "{$plugin}_info";
 221          if(!function_exists($info_func))
 222          {
 223              return false;
 224          }
 225          $plugin_info = $info_func();
 226  
 227          // No compatibility set or compatibility = * - assume compatible
 228          if(empty($plugin_info['compatibility']) || $plugin_info['compatibility'] == "*")
 229          {
 230              return true;
 231          }
 232          $compatibility = explode(",", $plugin_info['compatibility']);
 233          foreach($compatibility as $version)
 234          {
 235              $version = trim($version);
 236              $version = str_replace("*", ".+", preg_quote($version));
 237              $version = str_replace("\.+", ".+", $version);
 238              if(preg_match("#{$version}#i", $mybb->version_code))
 239              {
 240                  return true;
 241              }
 242          }
 243  
 244          // Nothing matches
 245          return false;
 246      }
 247  }
 248  


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref