Password strength checker for YII


(Tesion) #1

Hello,

a "dirty" password strength checker for Yii. Easiest way to use it to drop it into "components" folder. with 'minLevel' => 3 . You set the minlevel.

Some info: For the freedom of the user you shouldnt set the lelvel to high. Only if you sell things have auctions or something similar "important" things. You should bind the user to a "secure" password.



<?php


/*


 * A password strength checker!


 * Drop it where you like it :)


 *


 * LINK: www.u-r-reality.de


 */





class EPassValidator extends CValidator


{


    public $minLevel;





    private $password;


    private $lvl;


    private $length;





    protected function validateAttribute($object, $attribute)


    {


        if(!$object->hasErrors())


        {


            $this->password = $object->$attribute;





            $this->minLevel = $this->minLevel ? $this->minLevel : 3;





            echo $this->minLevel;





            if($this->getLevel($this->password) < $this->minLevel)


            {


                $message = Yii::t('yii','The password is not secure enough.');


                $this->addError($object,$attribute,$message);


            }


        }


    }





    protected function getLevel($password)


    {


        if ( strlen( $password ) == 0 )


            return 1;





        $this->lvl = 0;





        $this->length = strlen($password);





        if(strtolower($password) != $password)


        $this->lvl += 2;





        if(strtoupper($password) == $password)


        $this->lvl += 2;





        if($this->length >= 8 && $this->length <= 15)


        $this->lvl += 2;





        if($this->length >= 16 && $this->length <=35)


        $this->lvl += 3;





        if($this->length > 35)


        $this->lvl += 4;





        preg_match_all('/[0-9]/', $password, $numbers);


        $this->lvl += count($numbers[0]);





        preg_match_all('/[|!@#$%&*/=?,;.:-_+~^\]/', $password, $specialchars);


        $this->lvl += sizeof($specialchars[0]);





        $chars = str_split($password);


        $num_unique_chars = sizeof( array_unique($chars) );


        $this->lvl += $num_unique_chars * 2;





        $this->lvl = $this->lvl > 99 ? 99 : $this->lvl;


        $this->lvl = floor($this->lvl / 10 + 1);





        return $this->lvl;


    }





}


?>


Do what ever you like with it. Just keep the link. And dont claim it as your own :)