Comments: on this page. Click to read or post your own.

Source for file PasswordValidator.php

Documentation is available at PasswordValidator.php

  1. <?php
  2.  
  3. /**
  4.  * This class represents a validator for member passwords.
  5.  * 
  6.  * <code>
  7.  * $pwdVal = new PasswordValidator();
  8.  * $pwdValidator->minLength(7);
  9.  * $pwdValidator->checkHistoricalPasswords(6);
  10.  * $pwdValidator->characterStrength('lowercase','uppercase','digits','punctuation');
  11.  * 
  12.  * Member::set_password_validator($pwdValidator);
  13.  * </code>
  14.  */
  15. class PasswordValidator extends Object {
  16.     static $character_strength_tests array(
  17.         'lowercase' => '/[a-z]/',
  18.         'uppercase' => '/[A-Z]/',
  19.         'digits' => '/[0-9]/',
  20.         'punctuation' => '/[^A-Za-z0-9]/',
  21.     );
  22.     
  23.  
  24.     /**
  25.      * Minimum password length
  26.      */
  27.     function minLength($minLength{
  28.         $this->minLength = $minLength;
  29.     }
  30.     
  31.     /**
  32.      * Check the character strength of the password.
  33.      *
  34.      * Eg: $this->characterStrength(3, array("lowercase", "uppercase", "digits", "punctuation"))
  35.      * 
  36.      * @param $minScore The minimum number of character tests that must pass
  37.      * @param $testNames The names of the tests to perform
  38.      */
  39.     function characterStrength($minScore$testNames{
  40.         $this->minScore = $minScore;
  41.         $this->testNames = $testNames;
  42.     }
  43.     
  44.     /**
  45.      * Check a number of previous passwords that the user has used, and don't let them change to that.
  46.      */
  47.     function checkHistoricalPasswords($count{
  48.         $this->historicalPasswordCount = $count;
  49.     }
  50.     
  51.     function validate($password$member{
  52.         $valid new ValidationResult();
  53.         
  54.         if($this->minLength{
  55.             if(strlen($password$this->minLength$valid->error("Password is too short, it must be 7 or more characters long.""TOO_SHORT");
  56.         }
  57.  
  58.         if($this->minScore{
  59.             $score 0;
  60.             $missedTests array();
  61.             foreach($this->testNames as $name{
  62.                 if(preg_match(self::$character_strength_tests[$name]$password)) $score++;
  63.                 else $missedTests[$name;
  64.             }
  65.             
  66.             if($score $this->minScore{
  67.                 $valid->error("You need to increase the strength of your passwords by adding some of the following characters: " implode(", "$missedTests)"LOW_CHARACTER_STRENGTH");
  68.             }
  69.         }
  70.         
  71.         if($this->historicalPasswordCount{
  72.             $previousPasswords DataObject::get("MemberPassword""MemberID = $member->ID""Created DESC, ID Desc"""$this->historicalPasswordCount);
  73.             if($previousPasswordsforeach($previousPasswords as $previousPasswords{
  74.                 if($previousPasswords->checkPassword($password)) {
  75.                     $valid->error("You've already used that password in the past, please choose a new password""PREVIOUS_PASSWORD");
  76.                     break;
  77.                 }
  78.             }
  79.         }
  80.         
  81.         return $valid;
  82.     }
  83.     
  84. }
blog comments powered by Disqus

Documentation generated on Mon, 12 May 2008 15:15:52 +1200 by phpDocumentor 1.3.2