phpcs: Fix some "Single space expected before elseif"
[lhc/web/wiklou.git] / includes / password / PasswordFactory.php
index 3b4ebb1..e1f272b 100644 (file)
@@ -68,6 +68,15 @@ final class PasswordFactory {
                $this->default = $type;
        }
 
+       /**
+        * Get the default password type
+        *
+        * @return string
+        */
+       public function getDefaultType() {
+               return $this->default;
+       }
+
        /**
         * Initialize the internal static variables using the global variables
         *
@@ -141,11 +150,15 @@ final class PasswordFactory {
         * If no existing object is given, make a new default object. If one is given, clone that
         * object. Then pass the plaintext to Password::crypt().
         *
-        * @param string $password Plaintext password
+        * @param string|null $password Plaintext password, or null for an invalid password
         * @param Password|null $existing Optional existing hash to get options from
         * @return Password
         */
        public function newFromPlaintext( $password, Password $existing = null ) {
+               if ( $password === null ) {
+                       return new InvalidPassword( $this, array( 'type' => '' ), null );
+               }
+
                if ( $existing === null ) {
                        $config = $this->types[$this->default];
                        $obj = new $config['class']( $this, $config );
@@ -175,4 +188,38 @@ final class PasswordFactory {
                        return $password->needsUpdate();
                }
        }
+
+       /**
+        * Generate a random string suitable for a password
+        *
+        * @param int $minLength Minimum length of password to generate
+        * @return string
+        */
+       public static function generateRandomPasswordString( $minLength = 10 ) {
+               // Decide the final password length based on our min password length,
+               // stopping at a minimum of 10 chars.
+               $length = max( 10, $minLength );
+               // Multiply by 1.25 to get the number of hex characters we need
+               $length = $length * 1.25;
+               // Generate random hex chars
+               $hex = MWCryptRand::generateHex( $length );
+               // Convert from base 16 to base 32 to get a proper password like string
+               return wfBaseConvert( $hex, 16, 32 );
+       }
+
+       /**
+        * Create an InvalidPassword
+        *
+        * @return InvalidPassword
+        */
+       public static function newInvalidPassword() {
+               static $password = null;
+
+               if ( $password === null ) {
+                       $factory = new self();
+                       $password = new InvalidPassword( $factory, array( 'type' => '' ), null );
+               }
+
+               return $password;
+       }
 }