[ Index ]

PHP Cross Reference of phpBB-3.2.11-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      * Runs a step of the extension enabling process.
 165      *
 166      * Allows the exentension to enable in a long running script that works
 167      * in multiple steps across requests. State is kept for the extension
 168      * in the extensions table.
 169      *
 170      * @param    string    $name    The extension's name
 171      * @return    bool            False if enabling is finished, true otherwise
 172      */
 173  	public function enable_step($name)
 174      {
 175          // ignore extensions that are already enabled
 176          if ($this->is_enabled($name))
 177          {
 178              return false;
 179          }
 180  
 181          $old_state = (isset($this->extensions[$name]['ext_state'])) ? unserialize($this->extensions[$name]['ext_state']) : false;
 182  
 183          $extension = $this->get_extension($name);
 184  
 185          if (!$extension->is_enableable())
 186          {
 187              return false;
 188          }
 189  
 190          $state = $extension->enable_step($old_state);
 191  
 192          $active = ($state === false);
 193  
 194          $extension_data = array(
 195              'ext_name'        => $name,
 196              'ext_active'    => $active,
 197              'ext_state'        => serialize($state),
 198          );
 199  
 200          $this->extensions[$name] = $extension_data;
 201          $this->extensions[$name]['ext_path'] = $this->get_extension_path($extension_data['ext_name']);
 202          ksort($this->extensions);
 203  
 204          $sql = 'SELECT COUNT(ext_name) as row_count
 205              FROM ' . $this->extension_table . "
 206              WHERE ext_name = '" . $this->db->sql_escape($name) . "'";
 207          $result = $this->db->sql_query($sql);
 208          $count = $this->db->sql_fetchfield('row_count');
 209          $this->db->sql_freeresult($result);
 210  
 211          if ($count)
 212          {
 213              $sql = 'UPDATE ' . $this->extension_table . '
 214                  SET ' . $this->db->sql_build_array('UPDATE', $extension_data) . "
 215                  WHERE ext_name = '" . $this->db->sql_escape($name) . "'";
 216              $this->db->sql_query($sql);
 217          }
 218          else
 219          {
 220              $sql = 'INSERT INTO ' . $this->extension_table . '
 221                  ' . $this->db->sql_build_array('INSERT', $extension_data);
 222              $this->db->sql_query($sql);
 223          }
 224  
 225          if ($this->cache)
 226          {
 227              $this->cache->purge();
 228          }
 229  
 230          if ($active)
 231          {
 232              $this->config->increment('assets_version', 1);
 233          }
 234  
 235          return !$active;
 236      }
 237  
 238      /**
 239      * Enables an extension
 240      *
 241      * This method completely enables an extension. But it could be long running
 242      * so never call this in a script that has a max_execution time.
 243      *
 244      * @param string $name The extension's name
 245      * @return null
 246      */
 247  	public function enable($name)
 248      {
 249          // @codingStandardsIgnoreStart
 250          while ($this->enable_step($name));
 251          // @codingStandardsIgnoreEnd
 252      }
 253  
 254      /**
 255      * Disables an extension
 256      *
 257      * Calls the disable method on the extension's meta class to allow it to
 258      * process the event.
 259      *
 260      * @param string $name The extension's name
 261      * @return bool False if disabling is finished, true otherwise
 262      */
 263  	public function disable_step($name)
 264      {
 265          // ignore extensions that are not enabled
 266          if (!$this->is_enabled($name))
 267          {
 268              return false;
 269          }
 270  
 271          $old_state = unserialize($this->extensions[$name]['ext_state']);
 272  
 273          $extension = $this->get_extension($name);
 274          $state = $extension->disable_step($old_state);
 275  
 276          // continue until the state is false
 277          if ($state !== false)
 278          {
 279              $extension_data = array(
 280                  'ext_state'        => serialize($state),
 281              );
 282              $this->extensions[$name]['ext_state'] = serialize($state);
 283  
 284              $sql = 'UPDATE ' . $this->extension_table . '
 285                  SET ' . $this->db->sql_build_array('UPDATE', $extension_data) . "
 286                  WHERE ext_name = '" . $this->db->sql_escape($name) . "'";
 287              $this->db->sql_query($sql);
 288  
 289              if ($this->cache)
 290              {
 291                  $this->cache->purge();
 292              }
 293  
 294              return true;
 295          }
 296  
 297          $extension_data = array(
 298              'ext_active'    => false,
 299              'ext_state'        => serialize(false),
 300          );
 301          $this->extensions[$name]['ext_active'] = false;
 302          $this->extensions[$name]['ext_state'] = serialize(false);
 303  
 304          $sql = 'UPDATE ' . $this->extension_table . '
 305              SET ' . $this->db->sql_build_array('UPDATE', $extension_data) . "
 306              WHERE ext_name = '" . $this->db->sql_escape($name) . "'";
 307          $this->db->sql_query($sql);
 308  
 309          if ($this->cache)
 310          {
 311              $this->cache->purge();
 312          }
 313  
 314          return false;
 315      }
 316  
 317      /**
 318      * Disables an extension
 319      *
 320      * Disables an extension completely at once. This process could run for a
 321      * while so never call this in a script that has a max_execution time.
 322      *
 323      * @param string $name The extension's name
 324      * @return null
 325      */
 326  	public function disable($name)
 327      {
 328          // @codingStandardsIgnoreStart
 329          while ($this->disable_step($name));
 330          // @codingStandardsIgnoreEnd
 331      }
 332  
 333      /**
 334      * Purge an extension
 335      *
 336      * Disables the extension first if active, and then calls purge on the
 337      * extension's meta class to delete the extension's database content.
 338      *
 339      * @param string $name The extension's name
 340      * @return bool False if purging is finished, true otherwise
 341      */
 342  	public function purge_step($name)
 343      {
 344          // ignore extensions that are not configured
 345          if (!$this->is_configured($name))
 346          {
 347              return false;
 348          }
 349  
 350          // disable first if necessary
 351          if ($this->extensions[$name]['ext_active'])
 352          {
 353              $this->disable($name);
 354          }
 355  
 356          $old_state = unserialize($this->extensions[$name]['ext_state']);
 357  
 358          $extension = $this->get_extension($name);
 359          $state = $extension->purge_step($old_state);
 360  
 361          // continue until the state is false
 362          if ($state !== false)
 363          {
 364              $extension_data = array(
 365                  'ext_state'        => serialize($state),
 366              );
 367              $this->extensions[$name]['ext_state'] = serialize($state);
 368  
 369              $sql = 'UPDATE ' . $this->extension_table . '
 370                  SET ' . $this->db->sql_build_array('UPDATE', $extension_data) . "
 371                  WHERE ext_name = '" . $this->db->sql_escape($name) . "'";
 372              $this->db->sql_query($sql);
 373  
 374              if ($this->cache)
 375              {
 376                  $this->cache->purge();
 377              }
 378  
 379              return true;
 380          }
 381  
 382          unset($this->extensions[$name]);
 383  
 384          $sql = 'DELETE FROM ' . $this->extension_table . "
 385              WHERE ext_name = '" . $this->db->sql_escape($name) . "'";
 386          $this->db->sql_query($sql);
 387  
 388          if ($this->cache)
 389          {
 390              $this->cache->purge();
 391          }
 392  
 393          return false;
 394      }
 395  
 396      /**
 397      * Purge an extension
 398      *
 399      * Purges an extension completely at once. This process could run for a while
 400      * so never call this in a script that has a max_execution time.
 401      *
 402      * @param string $name The extension's name
 403      * @return null
 404      */
 405  	public function purge($name)
 406      {
 407          // @codingStandardsIgnoreStart
 408          while ($this->purge_step($name));
 409          // @codingStandardsIgnoreEnd
 410      }
 411  
 412      /**
 413      * Retrieves a list of all available extensions on the filesystem
 414      *
 415      * @return array An array with extension names as keys and paths to the
 416      *               extension as values
 417      */
 418  	public function all_available()
 419      {
 420          $available = array();
 421          if (!is_dir($this->phpbb_root_path . 'ext/'))
 422          {
 423              return $available;
 424          }
 425  
 426          $iterator = new \RecursiveIteratorIterator(
 427              new \phpbb\recursive_dot_prefix_filter_iterator(
 428                  new \RecursiveDirectoryIterator($this->phpbb_root_path . 'ext/', \FilesystemIterator::NEW_CURRENT_AND_KEY | \FilesystemIterator::FOLLOW_SYMLINKS)
 429              ),
 430              \RecursiveIteratorIterator::SELF_FIRST
 431          );
 432          $iterator->setMaxDepth(2);
 433  
 434          foreach ($iterator as $file_info)
 435          {
 436              if ($file_info->isFile() && $file_info->getFilename() == 'composer.json')
 437              {
 438                  $ext_name = $iterator->getInnerIterator()->getSubPath();
 439                  $ext_name = str_replace(DIRECTORY_SEPARATOR, '/', $ext_name);
 440                  if ($this->is_available($ext_name))
 441                  {
 442                      $available[$ext_name] = $this->get_extension_path($ext_name, true);
 443                  }
 444              }
 445          }
 446          ksort($available);
 447          return $available;
 448      }
 449  
 450      /**
 451      * Retrieves all configured extensions.
 452      *
 453      * All enabled and disabled extensions are considered configured. A purged
 454      * extension that is no longer in the database is not configured.
 455      *
 456      * @param bool $phpbb_relative Whether the path should be relative to phpbb root
 457      *
 458      * @return array An array with extension names as keys and and the
 459      *               database stored extension information as values
 460      */
 461  	public function all_configured($phpbb_relative = true)
 462      {
 463          $configured = array();
 464          foreach ($this->extensions as $name => $data)
 465          {
 466              if ($this->is_configured($name))
 467              {
 468                  unset($data['metadata']);
 469                  $data['ext_path'] = ($phpbb_relative ? $this->phpbb_root_path : '') . $data['ext_path'];
 470                  $configured[$name] = $data;
 471              }
 472          }
 473          return $configured;
 474      }
 475  
 476      /**
 477      * Retrieves all enabled extensions.
 478      * @param bool $phpbb_relative Whether the path should be relative to phpbb root
 479      *
 480      * @return array An array with extension names as keys and and the
 481      *               database stored extension information as values
 482      */
 483  	public function all_enabled($phpbb_relative = true)
 484      {
 485          $enabled = array();
 486          foreach ($this->extensions as $name => $data)
 487          {
 488              if ($this->is_enabled($name))
 489              {
 490                  $enabled[$name] = ($phpbb_relative ? $this->phpbb_root_path : '') . $data['ext_path'];
 491              }
 492          }
 493          return $enabled;
 494      }
 495  
 496      /**
 497      * Retrieves all disabled extensions.
 498      *
 499      * @param bool $phpbb_relative Whether the path should be relative to phpbb root
 500      *
 501      * @return array An array with extension names as keys and and the
 502      *               database stored extension information as values
 503      */
 504  	public function all_disabled($phpbb_relative = true)
 505      {
 506          $disabled = array();
 507          foreach ($this->extensions as $name => $data)
 508          {
 509              if ($this->is_disabled($name))
 510              {
 511                  $disabled[$name] = ($phpbb_relative ? $this->phpbb_root_path : '') . $data['ext_path'];
 512              }
 513          }
 514          return $disabled;
 515      }
 516  
 517      /**
 518      * Check to see if a given extension is available on the filesystem
 519      *
 520      * @param string $name Extension name to check NOTE: Can be user input
 521      * @return bool Depending on whether or not the extension is available
 522      */
 523  	public function is_available($name)
 524      {
 525          $md_manager = $this->create_extension_metadata_manager($name);
 526          try
 527          {
 528              return $md_manager->get_metadata('all') && $md_manager->validate_enable();
 529          }
 530          catch (\phpbb\extension\exception $e)
 531          {
 532              return false;
 533          }
 534      }
 535  
 536      /**
 537      * Check to see if a given extension is enabled
 538      *
 539      * @param string $name Extension name to check
 540      * @return bool Depending on whether or not the extension is enabled
 541      */
 542  	public function is_enabled($name)
 543      {
 544          return isset($this->extensions[$name]['ext_active']) && $this->extensions[$name]['ext_active'];
 545      }
 546  
 547      /**
 548      * Check to see if a given extension is disabled
 549      *
 550      * @param string $name Extension name to check
 551      * @return bool Depending on whether or not the extension is disabled
 552      */
 553  	public function is_disabled($name)
 554      {
 555          return isset($this->extensions[$name]['ext_active']) && !$this->extensions[$name]['ext_active'];
 556      }
 557  
 558      /**
 559      * Check to see if a given extension is configured
 560      *
 561      * All enabled and disabled extensions are considered configured. A purged
 562      * extension that is no longer in the database is not configured.
 563      *
 564      * @param string $name Extension name to check
 565      * @return bool Depending on whether or not the extension is configured
 566      */
 567  	public function is_configured($name)
 568      {
 569          return isset($this->extensions[$name]['ext_active']);
 570      }
 571  
 572      /**
 573      * Check the version and return the available updates (for an extension).
 574      *
 575      * @param \phpbb\extension\metadata_manager $md_manager The metadata manager for the version to check.
 576      * @param bool $force_update Ignores cached data. Defaults to false.
 577      * @param bool $force_cache Force the use of the cache. Override $force_update.
 578      * @param string $stability Force the stability (null by default).
 579      * @return array
 580      * @throws runtime_exception
 581      */
 582  	public function version_check(\phpbb\extension\metadata_manager $md_manager, $force_update = false, $force_cache = false, $stability = null)
 583      {
 584          $meta = $md_manager->get_metadata('all');
 585  
 586          if (!isset($meta['extra']['version-check']))
 587          {
 588              throw new runtime_exception('NO_VERSIONCHECK');
 589          }
 590  
 591          $version_check = $meta['extra']['version-check'];
 592  
 593          $version_helper = new \phpbb\version_helper($this->cache, $this->config, new file_downloader());
 594          $version_helper->set_current_version($meta['version']);
 595          $version_helper->set_file_location($version_check['host'], $version_check['directory'], $version_check['filename'], isset($version_check['ssl']) ? $version_check['ssl'] : false);
 596          $version_helper->force_stability($stability);
 597  
 598          return $version_helper->get_ext_update_on_branch($force_update, $force_cache);
 599      }
 600  
 601      /**
 602      * Check to see if a given extension is purged
 603      *
 604      * An extension is purged if it is available, not enabled and not disabled.
 605      *
 606      * @param string $name Extension name to check
 607      * @return bool Depending on whether or not the extension is purged
 608      */
 609  	public function is_purged($name)
 610      {
 611          return $this->is_available($name) && !$this->is_configured($name);
 612      }
 613  
 614      /**
 615      * Instantiates a \phpbb\finder.
 616      *
 617      * @param bool $use_all_available Should we load all extensions, or just enabled ones
 618      * @return \phpbb\finder An extension finder instance
 619      */
 620  	public function get_finder($use_all_available = false)
 621      {
 622          $finder = new \phpbb\finder($this->filesystem, $this->phpbb_root_path, $this->cache, $this->php_ext, $this->cache_name . '_finder');
 623          if ($use_all_available)
 624          {
 625              $finder->set_extensions(array_keys($this->all_available()));
 626          }
 627          else
 628          {
 629              $finder->set_extensions(array_keys($this->all_enabled()));
 630          }
 631          return $finder;
 632      }
 633  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1