[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/phpbb/install/module/update_filesystem/task/ -> update_files.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\install\module\update_filesystem\task;
  15  
  16  use phpbb\exception\runtime_exception;
  17  use phpbb\install\exception\resource_limit_reached_exception;
  18  use phpbb\install\helper\config;
  19  use phpbb\install\helper\container_factory;
  20  use phpbb\install\helper\file_updater\factory;
  21  use phpbb\install\helper\file_updater\file_updater_interface;
  22  use phpbb\install\helper\iohandler\iohandler_interface;
  23  use phpbb\install\helper\update_helper;
  24  use phpbb\install\task_base;
  25  
  26  /**
  27   * File updater task
  28   */
  29  class update_files extends task_base
  30  {
  31      /**
  32       * @var \phpbb\cache\driver\driver_interface
  33       */
  34      protected $cache;
  35  
  36      /**
  37       * @var config
  38       */
  39      protected $installer_config;
  40  
  41      /**
  42       * @var iohandler_interface
  43       */
  44      protected $iohandler;
  45  
  46      /**
  47       * @var factory
  48       */
  49      protected $factory;
  50  
  51      /**
  52       * @var file_updater_interface
  53       */
  54      protected $file_updater;
  55  
  56      /**
  57       * @var update_helper
  58       */
  59      protected $update_helper;
  60  
  61      /**
  62       * @var string
  63       */
  64      protected $phpbb_root_path;
  65  
  66      /**
  67       * Constructor
  68       *
  69       * @param container_factory        $container
  70       * @param config                $config
  71       * @param iohandler_interface    $iohandler
  72       * @param factory                $file_updater_factory
  73       * @param update_helper            $update_helper
  74       * @param string                $phpbb_root_path
  75       */
  76  	public function __construct(container_factory $container, config $config, iohandler_interface $iohandler, factory $file_updater_factory, update_helper $update_helper, $phpbb_root_path)
  77      {
  78          $this->factory            = $file_updater_factory;
  79          $this->installer_config    = $config;
  80          $this->iohandler        = $iohandler;
  81          $this->update_helper    = $update_helper;
  82          $this->phpbb_root_path    = $phpbb_root_path;
  83  
  84          $this->cache            = $container->get('cache.driver');
  85          $this->file_updater        = null;
  86  
  87          parent::__construct(false);
  88      }
  89  
  90      /**
  91       * {@inheritdoc}
  92       */
  93  	public function check_requirements()
  94      {
  95          return $this->installer_config->get('do_update_files', false);
  96      }
  97  
  98      /**
  99       * {@inheritdoc}
 100       */
 101  	public function run()
 102      {
 103          $new_path = $this->update_helper->get_path_to_new_update_files();
 104  
 105          $file_update_info = $this->installer_config->get('update_files', array());
 106  
 107          $update_type_progress = $this->installer_config->get('file_updater_type_progress', '');
 108          $update_elem_progress = $this->installer_config->get('file_updater_elem_progress', '');
 109          $type_progress_found = false;
 110          $elem_progress_found = false;
 111  
 112          // Progress bar
 113          $task_count = 0;
 114          foreach ($file_update_info as $sub_array)
 115          {
 116              $task_count += count($sub_array);
 117          }
 118  
 119          // Everything is up to date, so just continue
 120          if ($task_count === 0)
 121          {
 122              return;
 123          }
 124  
 125          $progress_count = $this->installer_config->get('file_update_progress_count', 0);
 126          $this->iohandler->set_task_count($task_count, true);
 127          $this->iohandler->set_progress('UPDATE_UPDATING_FILES', 0);
 128  
 129          $this->file_updater = $this->get_file_updater();
 130  
 131          // File updater fallback logic
 132          try
 133          {
 134              // Update files
 135              foreach ($file_update_info as $type => $file_update_vector)
 136              {
 137                  if (!$type_progress_found)
 138                  {
 139                      if ($type === $update_type_progress || empty($update_elem_progress))
 140                      {
 141                          $type_progress_found = true;
 142                      }
 143                      else
 144                      {
 145                          continue;
 146                      }
 147                  }
 148  
 149                  foreach ($file_update_vector as $path)
 150                  {
 151                      if (!$elem_progress_found)
 152                      {
 153                          if ($path === $update_elem_progress || empty($update_elem_progress))
 154                          {
 155                              $elem_progress_found = true;
 156                          }
 157                          else
 158                          {
 159                              continue;
 160                          }
 161                      }
 162  
 163                      switch ($type)
 164                      {
 165                          case 'delete':
 166                              $this->file_updater->delete_file($path);
 167                          break;
 168                          case 'new':
 169                              $this->file_updater->create_new_file($path, $new_path . $path);
 170                          break;
 171                          case 'update_without_diff':
 172                              $this->file_updater->update_file($path, $new_path . $path);
 173                          break;
 174                          case 'update_with_diff':
 175                              $this->file_updater->update_file(
 176                                  $path,
 177                                  base64_decode($this->cache->get('_file_' . md5($path))),
 178                                  true
 179                              );
 180                          break;
 181                      }
 182  
 183                      // Save progress
 184                      $this->installer_config->set('file_updater_type_progress', $type);
 185                      $this->installer_config->set('file_updater_elem_progress', $path);
 186                      $progress_count++;
 187                      $this->iohandler->set_progress('UPDATE_UPDATING_FILES', $progress_count);
 188  
 189                      if ($this->installer_config->get_time_remaining() <= 0 || $this->installer_config->get_memory_remaining() <= 0)
 190                      {
 191                          // Request refresh
 192                          throw new resource_limit_reached_exception();
 193                      }
 194                  }
 195              }
 196  
 197              $this->iohandler->finish_progress('UPDATE_UPDATING_FILES');
 198          }
 199          catch (runtime_exception $e)
 200          {
 201              if ($e instanceof resource_limit_reached_exception)
 202              {
 203                  throw new resource_limit_reached_exception();
 204              }
 205  
 206              $current_method = $this->installer_config->get('file_update_method', '');
 207  
 208              // File updater failed, try to fallback to download file update mode
 209              if ($current_method !== 'compression')
 210              {
 211                  $this->iohandler->add_warning_message(array(
 212                      'UPDATE_FILE_UPDATER_HAS_FAILED',
 213                      $current_method,
 214                      'compression'
 215                  ));
 216                  $this->installer_config->set('file_update_method', 'compression');
 217  
 218                  // We only want a simple refresh here
 219                  throw new resource_limit_reached_exception();
 220              }
 221              else
 222              {
 223                  // Nowhere to fallback to :(
 224                  // Due to the way the installer handles fatal errors, we need to throw a low level exception
 225                  throw new runtime_exception('UPDATE_FILE_UPDATERS_HAVE_FAILED');
 226              }
 227          }
 228  
 229          $file_updater_method = $this->installer_config->get('file_update_method', '');
 230          if ($file_updater_method === 'compression' || $file_updater_method === 'ftp')
 231          {
 232              $this->file_updater->close();
 233          }
 234      }
 235  
 236      /**
 237       * Get file updater
 238       *
 239       * @param null|string    $file_updater_method    Name of the file updater to use
 240       *
 241       * @return file_updater_interface    File updater
 242       */
 243  	protected function get_file_updater($file_updater_method = null)
 244      {
 245          $file_updater_method = ($file_updater_method === null) ? $this->installer_config->get('file_update_method', '') : $file_updater_method;
 246  
 247          if ($file_updater_method === 'compression')
 248          {
 249              $compression_method = $this->installer_config->get('file_update_compression', '');
 250  
 251              /** @var \phpbb\install\helper\file_updater\compression_file_updater $file_updater */
 252              $file_updater = $this->factory->get('compression');
 253              $archive_path = $file_updater->init($compression_method);
 254              $this->installer_config->set('update_file_archive', $archive_path);
 255          }
 256          else if ($file_updater_method === 'ftp')
 257          {
 258              /** @var \phpbb\install\helper\file_updater\ftp_file_updater $file_updater */
 259              $file_updater = $this->factory->get('ftp');
 260              $file_updater->init(
 261                  $this->installer_config->get('ftp_method', ''),
 262                  $this->installer_config->get('ftp_host', ''),
 263                  $this->installer_config->get('ftp_user', ''),
 264                  $this->installer_config->get('ftp_pass', ''),
 265                  $this->installer_config->get('ftp_path', ''),
 266                  $this->installer_config->get('ftp_port', 0),
 267                  $this->installer_config->get('ftp_timeout', 10)
 268              );
 269          }
 270          else
 271          {
 272              /** @var file_updater_interface $file_updater */
 273              $file_updater = $this->factory->get('direct_file');
 274          }
 275  
 276          return $file_updater;
 277      }
 278  
 279      /**
 280       * {@inheritdoc}
 281       */
 282  	static public function get_step_count()
 283      {
 284          return 0;
 285      }
 286  
 287      /**
 288       * {@inheritdoc}
 289       */
 290  	public function get_task_lang_name()
 291      {
 292          return '';
 293      }
 294  }


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1