[ Index ]

PHP Cross Reference of phpBB-3.3.0-deutsch

title

Body

[close]

/phpbb/extension/ -> manager.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\extension;
  15  
  16  use phpbb\exception\runtime_exception;
  17  use phpbb\file_downloader;
  18  use Symfony\Component\DependencyInjection\ContainerInterface;
  19  
  20  /**
  21  * The extension manager provides means to activate/deactivate extensions.
  22  */
  23  class manager
  24  {
  25      /** @var ContainerInterface */
  26      protected $container;
  27  
  28      protected $db;
  29      protected $config;
  30      protected $cache;
  31      protected $php_ext;
  32      protected $extensions;
  33      protected $extension_table;
  34      protected $phpbb_root_path;
  35      protected $cache_name;
  36  
  37      /**
  38      * Creates a manager and loads information from database
  39      *
  40      * @param ContainerInterface $container A container
  41      * @param \phpbb\db\driver\driver_interface $db A database connection
  42      * @param \phpbb\config\config $config Config object
  43      * @param \phpbb\filesystem\filesystem_interface $filesystem
  44      * @param string $extension_table The name of the table holding extensions
  45      * @param string $phpbb_root_path Path to the phpbb includes directory.
  46      * @param string $php_ext php file extension, defaults to php
  47      * @param \phpbb\cache\service $cache A cache instance or null
  48      * @param string $cache_name The name of the cache variable, defaults to _ext
  49      */
  50  	public function __construct(ContainerInterface $container, \phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, \phpbb\filesystem\filesystem_interface $filesystem, $extension_table, $phpbb_root_path, $php_ext = 'php', \phpbb\cache\service $cache = null, $cache_name = '_ext')
  51      {
  52          $this->cache = $cache;
  53          $this->cache_name = $cache_name;
  54          $this->config = $config;
  55          $this->container = $container;
  56          $this->db = $db;
  57          $this->extension_table = $extension_table;
  58          $this->filesystem = $filesystem;
  59          $this->phpbb_root_path = $phpbb_root_path;
  60          $this->php_ext = $php_ext;
  61  
  62          $this->extensions = ($this->cache) ? $this->cache->get($this->cache_name) : false;
  63  
  64          if ($this->extensions === false)
  65          {
  66              $this->load_extensions();
  67          }
  68      }
  69  
  70      /**
  71      * Loads all extension information from the database
  72      *
  73      * @return null
  74      */
  75  	public function load_extensions()
  76      {
  77          $this->extensions = array();
  78  
  79          // Do not try to load any extensions if the extension table
  80          // does not exist or when installing or updating.
  81          // Note: database updater invokes this code, and in 3.0
  82          // there is no extension table therefore the rest of this function
  83          // fails
  84          if (defined('IN_INSTALL') || version_compare($this->config['version'], '3.1.0-dev', '<'))
  85          {
  86              return;
  87          }
  88  
  89          $sql = 'SELECT *
  90              FROM ' . $this->extension_table;
  91  
  92          $result = $this->db->sql_query($sql);
  93          $extensions = $this->db->sql_fetchrowset($result);
  94          $this->db->sql_freeresult($result);
  95  
  96          foreach ($extensions as $extension)
  97          {
  98              $extension['ext_path'] = $this->get_extension_path($extension['ext_name']);
  99              $this->extensions[$extension['ext_name']] = $extension;
 100          }
 101  
 102          ksort($this->extensions);
 103  
 104          if ($this->cache)
 105          {
 106              $this->cache->put($this->cache_name, $this->extensions);
 107          }
 108      }
 109  
 110      /**
 111      * Generates the path to an extension
 112      *
 113      * @param string $name The name of the extension
 114      * @param bool $phpbb_relative Whether the path should be relative to phpbb root
 115      * @return string Path to an extension
 116      */
 117  	public function get_extension_path($name, $phpbb_relative = false)
 118      {
 119          $name = str_replace('.', '', $name);
 120  
 121          return (($phpbb_relative) ? $this->phpbb_root_path : '') . 'ext/' . $name . '/';
 122      }
 123  
 124      /**
 125      * Instantiates the extension meta class for the extension with the given name
 126      *
 127      * @param string $name The extension name
 128      * @return \phpbb\extension\extension_interface Instance of the extension meta class or
 129      *                     \phpbb\extension\base if the class does not exist
 130      */
 131  	public function get_extension($name)
 132      {
 133          $extension_class_name = str_replace('/', '\\', $name) . '\\ext';
 134  
 135          $migrator = $this->container->get('migrator');
 136  
 137          if (class_exists($extension_class_name))
 138          {
 139              return new $extension_class_name($this->container, $this->get_finder(), $migrator, $name, $this->get_extension_path($name, true));
 140          }
 141          else
 142          {
 143              return new \phpbb\extension\base($this->container, $this->get_finder(), $migrator, $name, $this->get_extension_path($name, true));
 144          }
 145      }
 146  
 147      /**
 148      * Instantiates the metadata manager for the extension with the given name
 149      *
 150      * @param string $name The extension name
 151      * @return \phpbb\extension\metadata_manager Instance of the metadata manager
 152      */
 153  	public function create_extension_metadata_manager($name)
 154      {
 155          if (!isset($this->extensions[$name]['metadata']))
 156          {
 157              $metadata = new \phpbb\extension\metadata_manager($name, $this->get_extension_path($name, true));
 158              $this->extensions[$name]['metadata'] = $metadata;
 159          }
 160          return $this->extensions[$name]['metadata'];
 161      }
 162  
 163      /**
 164      * Update the database entry for an extension
 165      *
 166      * @param string $name Extension name to update
 167      * @param array    $data Data to update in the database
 168      * @param string    $action Action to perform, by default 'update', may be also 'insert' or 'delete'
 169      */
 170  	protected function update_state($name, $data, $action = 'update')
 171      {
 172          switch ($action)
 173          {
 174              case 'insert':
 175                  $this->extensions[$name] = $data;
 176                  $this->extensions[$name]['ext_path'] = $this->get_extension_path($name);
 177                  ksort($this->extensions);
 178                  $sql = 'INSERT INTO ' . $this->extension_table . ' ' . $this->db->sql_build_array('INSERT', $data);
 179                  $this->db->sql_query($sql);
 180              break;
 181  
 182              case 'update':
 183                  $this->extensions[$name] = array_merge($this->extensions[$name], $data);
 184                  $sql = 'UPDATE ' . $this->extension_table . '
 185                      SET ' . $this->db->sql_build_array('UPDATE', $data) . "
 186                      WHERE ext_name = '" . $this->db->sql_escape($name) . "'";
 187                  $this->db->sql_query($sql);
 188              break;
 189  
 190              case 'delete':
 191                  unset($this->extensions[$name]);
 192                  $sql = 'DELETE FROM ' . $this->extension_table . "
 193                      WHERE ext_name = '" . $this->db->sql_escape($name) . "'";
 194                  $this->db->sql_query($sql);
 195              break;
 196          }
 197  
 198          if ($this->cache)
 199          {
 200              $this->cache->purge();
 201          }
 202      }
 203  
 204      /**
 205      * Runs a step of the extension enabling process.
 206      *
 207      * Allows the exentension to enable in a long running script that works
 208      * in multiple steps across requests. State is kept for the extension
 209      * in the extensions table.
 210      *
 211      * @param    string    $name    The extension's name
 212      * @return    bool            False if enabling is finished, true otherwise
 213      */
 214  	public function enable_step($name)
 215      {
 216          // ignore extensions that are already enabled
 217          if ($this->is_enabled($name))
 218          {
 219              return false;
 220          }
 221  
 222          $old_state = (isset($this->extensions[$name]['ext_state'])) ? unserialize($this->extensions[$name]['ext_state']) : false;
 223  
 224          $extension = $this->get_extension($name);
 225  
 226          if (!$extension->is_enableable())
 227          {
 228              return false;
 229          }
 230  
 231          $state = $extension->enable_step($old_state);
 232  
 233          $active = ($state === false);
 234  
 235          $extension_data = array(
 236              'ext_name'        => $name,
 237              'ext_active'    => $active,
 238              'ext_state'        => serialize($state),
 239          );
 240  
 241          $this->update_state($name, $extension_data, $this->is_configured($name) ? 'update' : 'insert');
 242  
 243          if ($active)
 244          {
 245              $this->config->increment('assets_version', 1);
 246          }
 247  
 248          return !$active;
 249      }
 250  
 251      /**
 252      * Enables an extension
 253      *
 254      * This method completely enables an extension. But it could be long running
 255      * so never call this in a script that has a max_execution time.
 256      *
 257      * @param string $name The extension's name
 258      * @return null
 259      */
 260  	public function enable($name)
 261      {
 262          // @codingStandardsIgnoreStart
 263          while ($this->enable_step($name));
 264          // @codingStandardsIgnoreEnd
 265      }
 266  
 267      /**
 268      * Disables an extension
 269      *
 270      * Calls the disable method on the extension's meta class to allow it to
 271      * process the event.
 272      *
 273      * @param string $name The extension's name
 274      * @return bool False if disabling is finished, true otherwise
 275      */
 276  	public function disable_step($name)
 277      {
 278          // ignore extensions that are not enabled
 279          if (!$this->is_enabled($name))
 280          {
 281              return false;
 282          }
 283  
 284          $old_state = unserialize($this->extensions[$name]['ext_state']);
 285  
 286          $extension = $this->get_extension($name);
 287          $state = $extension->disable_step($old_state);
 288          $active = ($state !== false);
 289  
 290          $extension_data = array(
 291              'ext_active'    => $active,
 292              'ext_state'        => serialize($state),
 293          );
 294          $this->update_state($name, $extension_data);
 295  
 296          return $active;
 297      }
 298  
 299      /**
 300      * Disables an extension
 301      *
 302      * Disables an extension completely at once. This process could run for a
 303      * while so never call this in a script that has a max_execution time.
 304      *
 305      * @param string $name The extension's name
 306      * @return null
 307      */
 308  	public function disable($name)
 309      {
 310          // @codingStandardsIgnoreStart
 311          while ($this->disable_step($name));
 312          // @codingStandardsIgnoreEnd
 313      }
 314  
 315      /**
 316      * Purge an extension
 317      *
 318      * Disables the extension first if active, and then calls purge on the
 319      * extension's meta class to delete the extension's database content.
 320      *
 321      * @param string $name The extension's name
 322      * @return bool False if purging is finished, true otherwise
 323      */
 324  	public function purge_step($name)
 325      {
 326          // ignore extensions that are not configured
 327          if (!$this->is_configured($name))
 328          {
 329              return false;
 330          }
 331  
 332          // disable first if necessary
 333          if ($this->extensions[$name]['ext_active'])
 334          {
 335              $this->disable($name);
 336          }
 337  
 338          $old_state = unserialize($this->extensions[$name]['ext_state']);
 339  
 340          $extension = $this->get_extension($name);
 341          $state = $extension->purge_step($old_state);
 342          $purged = ($state === false);
 343  
 344          $extension_data = array(
 345              'ext_state'    => serialize($state),
 346          );
 347  
 348          $this->update_state($name, $extension_data, $purged ? 'delete' : 'update');
 349  
 350          // continue until the state is false
 351          return !$purged;
 352      }
 353  
 354      /**
 355      * Purge an extension
 356      *
 357      * Purges an extension completely at once. This process could run for a while
 358      * so never call this in a script that has a max_execution time.
 359      *
 360      * @param string $name The extension's name
 361      * @return null
 362      */
 363  	public function purge($name)
 364      {
 365          // @codingStandardsIgnoreStart
 366          while ($this->purge_step($name));
 367          // @codingStandardsIgnoreEnd
 368      }
 369  
 370      /**
 371      * Retrieves a list of all available extensions on the filesystem
 372      *
 373      * @return array An array with extension names as keys and paths to the
 374      *               extension as values
 375      */
 376  	public function all_available()
 377      {
 378          $available = array();
 379          if (!is_dir($this->phpbb_root_path . 'ext/'))
 380          {
 381              return $available;
 382          }
 383  
 384          $iterator = new \RecursiveIteratorIterator(
 385              new \phpbb\recursive_dot_prefix_filter_iterator(
 386                  new \RecursiveDirectoryIterator($this->phpbb_root_path . 'ext/', \FilesystemIterator::NEW_CURRENT_AND_KEY | \FilesystemIterator::FOLLOW_SYMLINKS)
 387              ),
 388              \RecursiveIteratorIterator::SELF_FIRST
 389          );
 390          $iterator->setMaxDepth(2);
 391  
 392          foreach ($iterator as $file_info)
 393          {
 394              if ($file_info->isFile() && $file_info->getFilename() == 'composer.json')
 395              {
 396                  $ext_name = $iterator->getInnerIterator()->getSubPath();
 397                  $ext_name = str_replace(DIRECTORY_SEPARATOR, '/', $ext_name);
 398                  if ($this->is_available($ext_name))
 399                  {
 400                      $available[$ext_name] = $this->get_extension_path($ext_name, true);
 401                  }
 402              }
 403          }
 404          ksort($available);
 405          return $available;
 406      }
 407  
 408      /**
 409      * Retrieves all configured extensions.
 410      *
 411      * All enabled and disabled extensions are considered configured. A purged
 412      * extension that is no longer in the database is not configured.
 413      *
 414      * @param bool $phpbb_relative Whether the path should be relative to phpbb root
 415      *
 416      * @return array An array with extension names as keys and and the
 417      *               database stored extension information as values
 418      */
 419  	public function all_configured($phpbb_relative = true)
 420      {
 421          $configured = array();
 422          foreach ($this->extensions as $name => $data)
 423          {
 424              if ($this->is_configured($name))
 425              {
 426                  unset($data['metadata']);
 427                  $data['ext_path'] = ($phpbb_relative ? $this->phpbb_root_path : '') . $data['ext_path'];
 428                  $configured[$name] = $data;
 429              }
 430          }
 431          return $configured;
 432      }
 433  
 434      /**
 435      * Retrieves all enabled extensions.
 436      * @param bool $phpbb_relative Whether the path should be relative to phpbb root
 437      *
 438      * @return array An array with extension names as keys and and the
 439      *               database stored extension information as values
 440      */
 441  	public function all_enabled($phpbb_relative = true)
 442      {
 443          $enabled = array();
 444          foreach ($this->extensions as $name => $data)
 445          {
 446              if ($this->is_enabled($name))
 447              {
 448                  $enabled[$name] = ($phpbb_relative ? $this->phpbb_root_path : '') . $data['ext_path'];
 449              }
 450          }
 451          return $enabled;
 452      }
 453  
 454      /**
 455      * Retrieves all disabled extensions.
 456      *
 457      * @param bool $phpbb_relative Whether the path should be relative to phpbb root
 458      *
 459      * @return array An array with extension names as keys and and the
 460      *               database stored extension information as values
 461      */
 462  	public function all_disabled($phpbb_relative = true)
 463      {
 464          $disabled = array();
 465          foreach ($this->extensions as $name => $data)
 466          {
 467              if ($this->is_disabled($name))
 468              {
 469                  $disabled[$name] = ($phpbb_relative ? $this->phpbb_root_path : '') . $data['ext_path'];
 470              }
 471          }
 472          return $disabled;
 473      }
 474  
 475      /**
 476      * Check to see if a given extension is available on the filesystem
 477      *
 478      * @param string $name Extension name to check NOTE: Can be user input
 479      * @return bool Depending on whether or not the extension is available
 480      */
 481  	public function is_available($name)
 482      {
 483          $md_manager = $this->create_extension_metadata_manager($name);
 484          try
 485          {
 486              return $md_manager->get_metadata('all') && $md_manager->validate_enable();
 487          }
 488          catch (\phpbb\extension\exception $e)
 489          {
 490              return false;
 491          }
 492      }
 493  
 494      /**
 495      * Check to see if a given extension is enabled
 496      *
 497      * @param string $name Extension name to check
 498      * @return bool Depending on whether or not the extension is enabled
 499      */
 500  	public function is_enabled($name)
 501      {
 502          return isset($this->extensions[$name]['ext_active']) && $this->extensions[$name]['ext_active'];
 503      }
 504  
 505      /**
 506      * Check to see if a given extension is disabled
 507      *
 508      * @param string $name Extension name to check
 509      * @return bool Depending on whether or not the extension is disabled
 510      */
 511  	public function is_disabled($name)
 512      {
 513          return isset($this->extensions[$name]['ext_active']) && !$this->extensions[$name]['ext_active'];
 514      }
 515  
 516      /**
 517      * Check to see if a given extension is configured
 518      *
 519      * All enabled and disabled extensions are considered configured. A purged
 520      * extension that is no longer in the database is not configured.
 521      *
 522      * @param string $name Extension name to check
 523      * @return bool Depending on whether or not the extension is configured
 524      */
 525  	public function is_configured($name)
 526      {
 527          return isset($this->extensions[$name]['ext_active']);
 528      }
 529  
 530      /**
 531      * Check the version and return the available updates (for an extension).
 532      *
 533      * @param \phpbb\extension\metadata_manager $md_manager The metadata manager for the version to check.
 534      * @param bool $force_update Ignores cached data. Defaults to false.
 535      * @param bool $force_cache Force the use of the cache. Override $force_update.
 536      * @param string $stability Force the stability (null by default).
 537      * @return array
 538      * @throws runtime_exception
 539      */
 540  	public function version_check(\phpbb\extension\metadata_manager $md_manager, $force_update = false, $force_cache = false, $stability = null)
 541      {
 542          $meta = $md_manager->get_metadata('all');
 543  
 544          if (!isset($meta['extra']['version-check']))
 545          {
 546              throw new runtime_exception('NO_VERSIONCHECK');
 547          }
 548  
 549          $version_check = $meta['extra']['version-check'];
 550  
 551          $version_helper = new \phpbb\version_helper($this->cache, $this->config, new file_downloader());
 552          $version_helper->set_current_version($meta['version']);
 553          $version_helper->set_file_location($version_check['host'], $version_check['directory'], $version_check['filename'], isset($version_check['ssl']) ? $version_check['ssl'] : false);
 554          $version_helper->force_stability($stability);
 555  
 556          return $version_helper->get_ext_update_on_branch($force_update, $force_cache);
 557      }
 558  
 559      /**
 560      * Check to see if a given extension is purged
 561      *
 562      * An extension is purged if it is available, not enabled and not disabled.
 563      *
 564      * @param string $name Extension name to check
 565      * @return bool Depending on whether or not the extension is purged
 566      */
 567  	public function is_purged($name)
 568      {
 569          return $this->is_available($name) && !$this->is_configured($name);
 570      }
 571  
 572      /**
 573      * Instantiates a \phpbb\finder.
 574      *
 575      * @param bool $use_all_available Should we load all extensions, or just enabled ones
 576      * @return \phpbb\finder An extension finder instance
 577      */
 578  	public function get_finder($use_all_available = false)
 579      {
 580          $finder = new \phpbb\finder($this->filesystem, $this->phpbb_root_path, $this->cache, $this->php_ext, $this->cache_name . '_finder');
 581          if ($use_all_available)
 582          {
 583              $finder->set_extensions(array_keys($this->all_available()));
 584          }
 585          else
 586          {
 587              $finder->set_extensions(array_keys($this->all_enabled()));
 588          }
 589          return $finder;
 590      }
 591  }


Generated: Tue Apr 7 19:44:41 2020 Cross-referenced by PHPXref 0.7.1