Merge pull request #3911 from Nicofuma/ticket/12577

[ticket/12577] Lazy initialize the password manager
This commit is contained in:
Marc Alexander 2015-09-20 14:18:51 +02:00
commit db600d840d

View file

@ -49,22 +49,48 @@ class manager
*/ */
protected $config; protected $config;
/**
* @var bool Whether or not initialized() has been called
*/
private $initialized = false;
/**
* @var array Hashing driver service collection
*/
private $hashing_algorithms;
/**
* @var array List of default driver types
*/
private $defaults;
/** /**
* Construct a passwords object * Construct a passwords object
* *
* @param \phpbb\config\config $config phpBB configuration * @param \phpbb\config\config $config phpBB configuration
* @param array $hashing_algorithms Hashing driver * @param array $hashing_algorithms Hashing driver service collection
* service collection * @param \phpbb\passwords\helper $helper Passwords helper object
* @param \phpbb\passwords\helper $helper Passwords helper object * @param array $defaults List of default driver types
* @param array $defaults List of default driver types
*/ */
public function __construct(\phpbb\config\config $config, $hashing_algorithms, helper $helper, $defaults) public function __construct(\phpbb\config\config $config, $hashing_algorithms, helper $helper, $defaults)
{ {
$this->config = $config; $this->config = $config;
$this->helper = $helper; $this->helper = $helper;
$this->hashing_algorithms = $hashing_algorithms;
$this->defaults = $defaults;
}
$this->fill_type_map($hashing_algorithms); /**
$this->register_default_type($defaults); * Initialize the internal state
*/
protected function initialize()
{
if (!$this->initialized)
{
$this->initialized = true;
$this->fill_type_map($this->hashing_algorithms);
$this->register_default_type($this->defaults);
}
} }
/** /**
@ -144,6 +170,8 @@ class manager
return false; return false;
} }
$this->initialize();
// Be on the lookout for multiple hashing algorithms // Be on the lookout for multiple hashing algorithms
// 2 is correct: H\2a > 2, H\P > 2 // 2 is correct: H\2a > 2, H\P > 2
if (strlen($match[1]) > 2) if (strlen($match[1]) > 2)
@ -192,6 +220,8 @@ class manager
return false; return false;
} }
$this->initialize();
// Try to retrieve algorithm by service name if type doesn't // Try to retrieve algorithm by service name if type doesn't
// start with dollar sign // start with dollar sign
if (!is_array($type) && strpos($type, '$') !== 0 && isset($this->algorithms[$type])) if (!is_array($type) && strpos($type, '$') !== 0 && isset($this->algorithms[$type]))
@ -242,6 +272,8 @@ class manager
return false; return false;
} }
$this->initialize();
// First find out what kind of hash we're dealing with // First find out what kind of hash we're dealing with
$stored_hash_type = $this->detect_algorithm($hash); $stored_hash_type = $this->detect_algorithm($hash);
if ($stored_hash_type == false) if ($stored_hash_type == false)
@ -297,6 +329,8 @@ class manager
*/ */
public function combined_hash_password($password_hash, $type) public function combined_hash_password($password_hash, $type)
{ {
$this->initialize();
$data = array( $data = array(
'prefix' => '$', 'prefix' => '$',
'settings' => '$', 'settings' => '$',