[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/phpbb/console/command/fixup/ -> update_hashes.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  namespace phpbb\console\command\fixup;
  14  
  15  use Symfony\Component\Console\Input\InputInterface;
  16  use Symfony\Component\Console\Output\OutputInterface;
  17  use Symfony\Component\Console\Helper\ProgressBar;
  18  
  19  class update_hashes extends \phpbb\console\command\command
  20  {
  21      /** @var \phpbb\config\config */
  22      protected $config;
  23  
  24      /** @var \phpbb\db\driver\driver_interface */
  25      protected $db;
  26  
  27      /** @var \phpbb\passwords\manager */
  28      protected $passwords_manager;
  29  
  30      /** @var string Default hashing type */
  31      protected $default_type;
  32  
  33      /**
  34       * Update_hashes constructor
  35       *
  36       * @param \phpbb\config\config $config
  37       * @param \phpbb\user $user
  38       * @param \phpbb\db\driver\driver_interface $db
  39       * @param \phpbb\passwords\manager $passwords_manager
  40       * @param array $hashing_algorithms Hashing driver
  41       *            service collection
  42       * @param array $defaults Default password types
  43       */
  44  	public function __construct(\phpbb\config\config $config, \phpbb\user $user,
  45                                  \phpbb\db\driver\driver_interface $db, \phpbb\passwords\manager $passwords_manager,
  46                                  $hashing_algorithms, $defaults)
  47      {
  48          $this->config = $config;
  49          $this->db = $db;
  50  
  51          $this->passwords_manager = $passwords_manager;
  52  
  53          foreach ($defaults as $type)
  54          {
  55              if ($hashing_algorithms[$type]->is_supported())
  56              {
  57                  $this->default_type = $type;
  58                  break;
  59              }
  60          }
  61  
  62          parent::__construct($user);
  63      }
  64  
  65      /**
  66       * {@inheritdoc}
  67       */
  68  	protected function configure()
  69      {
  70          $this
  71              ->setName('fixup:update-hashes')
  72              ->setDescription($this->user->lang('CLI_DESCRIPTION_UPDATE_HASH_BCRYPT'))
  73          ;
  74      }
  75  
  76      /**
  77       * {@inheritdoc}
  78       */
  79  	protected function execute(InputInterface $input, OutputInterface $output)
  80      {
  81          // Get count to be able to display progress
  82          $sql = 'SELECT COUNT(user_id) AS count
  83                  FROM ' . USERS_TABLE . '
  84                  WHERE user_password ' . $this->db->sql_like_expression('$H$' . $this->db->get_any_char()) . '
  85                      OR user_password ' . $this->db->sql_like_expression('$CP$' . $this->db->get_any_char());
  86          $result = $this->db->sql_query($sql);
  87          $total_update_passwords = $this->db->sql_fetchfield('count');
  88          $this->db->sql_freeresult($result);
  89  
  90          // Create progress bar
  91          $progress_bar = new ProgressBar($output, $total_update_passwords);
  92          $progress_bar->start();
  93  
  94          $sql = 'SELECT user_id, user_password
  95                  FROM ' . USERS_TABLE . '
  96                  WHERE user_password ' . $this->db->sql_like_expression('$H$' . $this->db->get_any_char()) . '
  97                      OR user_password ' . $this->db->sql_like_expression('$CP$' . $this->db->get_any_char());
  98          $result = $this->db->sql_query($sql);
  99  
 100          while ($row = $this->db->sql_fetchrow($result))
 101          {
 102              $old_hash = preg_replace('/^\$CP\$/', '', $row['user_password']);
 103  
 104              // If stored hash type is unknown then it's md5 hash with no prefix
 105              // First rehash it using $H$ as hash type identifier (salted_md5)
 106              if (!$this->passwords_manager->detect_algorithm($old_hash))
 107              {
 108                  $old_hash = $this->passwords_manager->hash($old_hash, '$H$');
 109              }
 110  
 111              $new_hash = $this->passwords_manager->hash($old_hash, [$this->default_type]);
 112  
 113              $sql = 'UPDATE ' . USERS_TABLE . "
 114                      SET user_password = '" . $this->db->sql_escape($new_hash) . "'
 115                      WHERE user_id = " . (int) $row['user_id'];
 116              $this->db->sql_query($sql);
 117              $progress_bar->advance();
 118          }
 119  
 120          $this->config->set('update_hashes_last_cron', time());
 121  
 122          $progress_bar->finish();
 123  
 124          $output->writeln('<info>' . $this->user->lang('CLI_FIXUP_UPDATE_HASH_BCRYPT_SUCCESS') . '</info>');
 125      }
 126  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1