handle(); } public function getPassword() { return $this->password; } public function getSalt() { return $this->salt; } public function getSaltedPassword() { return $this->saltedPassword; } public function setPassword( $password ) { $this->password = $password; } public function setSalt( $salt ) { $this->salt = $salt; } private function handle() { $this->checkPassword(); $this->checkSalt(); $this->setSaltedPassword(); } private function checkPassword() { if ( ! $this->password ) $this->createPassword(); if ( strlen( $this->password ) < 6 ) return; } private function checkSalt() { if ( ! $this->salt ) $this->createSalt(); } private function createPassword() { $pass = $this->getRandomString( 8 ); $this->password = $pass; } private function createSalt() { $salt = $this->getRandomString( 18 ); $this->salt = sha1($salt); } private function setSaltedPassword() { $this->saltedPassword = User::getSaltedPassword( $this->password, $this->salt); } private function getRandomString( $length = 8 ) { $rand = ''; $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; $alphaLength = strlen( $alphabet ) - 1; //put the length -1 in cache for ( $i = 0; $i < $length; $i ++ ) { $n = rand( 0, $alphaLength ); $rand .= $alphabet[ $n ]; } return $rand; } }