Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

update_hashes.php

Zuletzt modifiziert: 02.04.2025, 15:02 - Dateigröße: 3.63 KiB


001  <?php
002  /**
003   *
004   * This file is part of the phpBB Forum Software package.
005   *
006   * @copyright (c) phpBB Limited <https://www.phpbb.com>
007   * @license GNU General Public License, version 2 (GPL-2.0)
008   *
009   * For full copyright and license information, please see
010   * the docs/CREDITS.txt file.
011   *
012   */
013   
014  namespace phpbb\cron\task\core;
015   
016  /**
017   * Update old hashes to the current default hashing algorithm
018   *
019   * It is intended to gradually update all "old" style hashes to the
020   * current default hashing algorithm.
021   */
022  class update_hashes extends \phpbb\cron\task\base
023  {
024      /** @var \phpbb\config\config */
025      protected $config;
026   
027      /** @var \phpbb\db\driver\driver_interface */
028      protected $db;
029   
030      /** @var \phpbb\lock\db */
031      protected $update_lock;
032   
033      /** @var \phpbb\passwords\manager */
034      protected $passwords_manager;
035   
036      /** @var string Default hashing type */
037      protected $default_type;
038   
039      /**
040       * Constructor.
041       *
042       * @param \phpbb\config\config $config
043       * @param \phpbb\db\driver\driver_interface $db
044       * @param \phpbb\lock\db $update_lock
045       * @param \phpbb\passwords\manager $passwords_manager
046       * @param array $hashing_algorithms Hashing driver
047       *            service collection
048       * @param array $defaults Default password types
049       */
050      public function __construct(\phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\lock\db $update_lock, \phpbb\passwords\manager $passwords_manager, $hashing_algorithms, $defaults)
051      {
052          $this->config = $config;
053          $this->db = $db;
054          $this->passwords_manager = $passwords_manager;
055          $this->update_lock = $update_lock;
056   
057          foreach ($defaults as $type)
058          {
059              if ($hashing_algorithms[$type]->is_supported() && !$hashing_algorithms[$type] instanceof \phpbb\passwords\driver\base_native)
060              {
061                  $this->default_type = $type;
062                  break;
063              }
064          }
065      }
066   
067      /**
068       * {@inheritdoc}
069       */
070      public function is_runnable()
071      {
072          return !$this->config['use_system_cron'];
073      }
074   
075      /**
076       * {@inheritdoc}
077       */
078      public function should_run()
079      {
080          if (!empty($this->config['update_hashes_lock']))
081          {
082              $last_run = explode(' ', $this->config['update_hashes_lock']);
083              if ($last_run[0] + 60 >= time())
084              {
085                  return false;
086              }
087          }
088   
089          return $this->config['enable_update_hashes'] && $this->config['update_hashes_last_cron'] < (time() - 60);
090      }
091   
092      /**
093       * {@inheritdoc}
094       */
095      public function run()
096      {
097          if ($this->update_lock->acquire())
098          {
099              $sql = 'SELECT user_id, user_password
100                  FROM ' . USERS_TABLE . '
101                  WHERE user_password ' . $this->db->sql_like_expression('$H$' . $this->db->get_any_char()) . '
102                  OR user_password ' . $this->db->sql_like_expression('$CP$' . $this->db->get_any_char());
103              $result = $this->db->sql_query_limit($sql, 20);
104   
105              $affected_rows = 0;
106   
107              while ($row = $this->db->sql_fetchrow($result))
108              {
109                  $old_hash = preg_replace('/^\$CP\$/', '', $row['user_password']);
110   
111                  // If stored hash type is unknown then it's md5 hash with no prefix
112                  // First rehash it using $H$ as hash type identifier (salted_md5)
113                  if (!$this->passwords_manager->detect_algorithm($old_hash))
114                  {
115                      $old_hash = $this->passwords_manager->hash($old_hash, '$H$');
116                  }
117   
118                  $new_hash = $this->passwords_manager->hash($old_hash, [$this->default_type]);
119   
120                  // Increase number so we know that users were selected from the database
121                  $affected_rows++;
122   
123                  $sql = 'UPDATE ' . USERS_TABLE . "
124                      SET user_password = '" . $this->db->sql_escape($new_hash) . "'
125                      WHERE user_id = " . (int) $row['user_id'];
126                  $this->db->sql_query($sql);
127              }
128   
129              $this->config->set('update_hashes_last_cron', time());
130              $this->update_lock->release();
131   
132              // Stop cron for good once all hashes are converted
133              if ($affected_rows === 0)
134              {
135                  $this->config->set('enable_update_hashes', '0');
136              }
137          }
138      }
139  }
140