MediaWiki  REL1_24
PasswordFactory.php
Go to the documentation of this file.
00001 <?php
00028 final class PasswordFactory {
00035     private $default = '';
00036 
00043     private $types = array(
00044         '' => array( 'type' => '', 'class' => 'InvalidPassword' ),
00045     );
00046 
00053     public function register( $type, array $config ) {
00054         $config['type'] = $type;
00055         $this->types[$type] = $config;
00056     }
00057 
00064     public function setDefaultType( $type ) {
00065         if ( !isset( $this->types[$type] ) ) {
00066             throw new InvalidArgumentException( "Invalid password type $type." );
00067         }
00068         $this->default = $type;
00069     }
00070 
00076     public function init( Config $config ) {
00077         foreach ( $config->get( 'PasswordConfig' ) as $type => $options ) {
00078             $this->register( $type, $options );
00079         }
00080 
00081         $this->setDefaultType( $config->get( 'PasswordDefault' ) );
00082     }
00083 
00089     public function getTypes() {
00090         return $this->types;
00091     }
00092 
00104     public function newFromCiphertext( $hash ) {
00105         if ( $hash === null || $hash === false || $hash === '' ) {
00106             return new InvalidPassword( $this, array( 'type' => '' ), null );
00107         } elseif ( $hash[0] !== ':' ) {
00108             throw new PasswordError( 'Invalid hash given' );
00109         }
00110 
00111         $type = substr( $hash, 1, strpos( $hash, ':', 1 ) - 1 );
00112         if ( !isset( $this->types[$type] ) ) {
00113             throw new PasswordError( "Unrecognized password hash type $type." );
00114         }
00115 
00116         $config = $this->types[$type];
00117 
00118         return new $config['class']( $this, $config, $hash );
00119     }
00120 
00128     public function newFromType( $type ) {
00129         if ( !isset( $this->types[$type] ) ) {
00130             throw new PasswordError( "Unrecognized password hash type $type." );
00131         }
00132 
00133         $config = $this->types[$type];
00134 
00135         return new $config['class']( $this, $config );
00136     }
00137 
00148     public function newFromPlaintext( $password, Password $existing = null ) {
00149         if ( $existing === null ) {
00150             $config = $this->types[$this->default];
00151             $obj = new $config['class']( $this, $config );
00152         } else {
00153             $obj = clone $existing;
00154         }
00155 
00156         $obj->crypt( $password );
00157 
00158         return $obj;
00159     }
00160 
00171     public function needsUpdate( Password $password ) {
00172         if ( $password->getType() !== $this->default ) {
00173             return true;
00174         } else {
00175             return $password->needsUpdate();
00176         }
00177     }
00178 }