All pastes #634569 Raw Edit

Untitled

public php v1 · immutable
#634569 ·published 2007-07-26 14:28 UTC
rendered paste body
<?php /** * @author Fidel Nunez * @copyright 2007, all rights reserved * @abstract user object */class User{			//instance of data adapter and registry object	private $DataAdapter;          	//settings	private $settings;					/**	 * constructor	 * @param array, $settings 	 * @param object, $DataAdapter	 */	public function __construct( $DataAdapter, $settings	=	array()  ){						$this->settings		=	$settings;		$this->DataAdapter	=	$DataAdapter;	}	/**	 * gets an atributes max lenght	 *	 * @param string, $attribute	 * @return int	 */	public function getMax($attribute){				if ( array_key_exists( $attribute, $this->settings )) {					return $this->settings[$attribute]['max'];			}	}	/**	 * gets an atributes min lenght	 *	 * @param string, $attribute	 * @return int	 */	public function getMin($attribute){				if ( array_key_exists( $attribute, $this->settings )) {					return $this->settings[$attribute]['min'];			}	}		/**	 * processes the username	 *	 * @param string, $username	 * @return  string, $username	 */	public function filterUsername( $username ){				//convert to ascii		$username 	= 	mb_convert_encoding($username, 'ASCII');				//convert to lowercase and trim whitespace		$username	=	strtolower( $username );		$username	=	trim( $username );				return $username;	}	/**	 * checks the username validity	 *	 * @param string, $username	 */	public function checkUsername( $username ){						//check if username is too short		if( strlen($username) < $this->getMin('username') ){						throw new UserException( 'USERNAME_TOO_SHORT', 1 );		}										//check if username is too long		if( strlen($username) > $this->getMax('username') ){						throw new UserException( 'USERNAME_TOO_LONG', 1 );		}								//check if username is of correct format		if ( $this->isIncorrectFormat( 'username', $username ) == true ) {						throw new UserException( 'USERNAME_WRONG_FORMAT', 1 );		}								//check username is not already used		if ( $this->isUsernameTaken( $username ) == true ) {						throw new UserException( 'USERNAME_USED', 1 );		}	}		/**	 * processes the password and checks the password validity	 *	 * @param string, $password	 * @return  string, $password	 */	public function filterPassword( $password ){				//convert to ascii		$password 	= 	mb_convert_encoding($password, 'ASCII');				//trim whitespace		$password	=	trim( $password );						return $password;	}	/**	 * checks the passwords validity	 *	 * @param string, $password	 */	public function checkPassword( $password ){						//check if password is too short		if( strlen($password) < $this->getMin('password') ){						throw new UserException( 'PASSWORD_TOO_SHORT', 1 );		}								//check if password is too long		if( strlen($password) > $this->getMax('password') ){						throw new UserException( 'PASSWORD_TOO_LONG', 1 );		}	}	/**	 * processes the email	 *	 * @param string, $email	 * @return  string, $email	 */	public function filterEmail( $email ){				//convert to ascii		$email 	= 	mb_convert_encoding($email, 'ASCII');				//convert to lowercase and trim whitespace		$email	=	strtolower( $email );		$email	=	trim( $email );				return $email;	}	/**	 * checks the email validity	 *	 * @param string, $email	 */	public function checkEmail( $email ){						//check if email is too short		if( strlen($email) < $this->getMin('email') ){						throw new UserException( 'EMAIL_TOO_SHORT', 1 );		}										//check if email is too long		if( strlen($email) > $this->getMax('email') ){						throw new UserException( 'EMAIL_TOO_LONG', 1 );		}								//check if email is of correct format		if ( $this->isIncorrectFormat( 'email', $email ) == true ) {						throw new UserException( 'EMAIL_WRONG_FORMAT', 1 );		}								//check email is not already used		if ( $this->isEmailTaken( $email ) == true ) {						throw new UserException( 'EMAIL_USED', 1 );		}	}	/**	 * checks an attribute is correctly formatted	 *	 * @param string, $attribute	 * @param string, $value	 * @return boolean	 */	public function isIncorrectFormat($attribute, $value){				if (eregi( $this->settings[$attribute]['format'], $value )) {						return false;		}		else {			return true;		}	}	/**	 * checks whether a username is taken	 *	 * @param string, $username	 * @return boolean, $is_taken	 */	public function isUsernameTaken( $username ){				$is_taken	=	false;				try {						$user	=	$this->DataAdapter->retrieveByUsername( $username );		}		catch (PDOException $e ) {		   			throw new UserException( $e->getMessage() );		}								//check username and username from database match		if( $user['name'] == $username ){						$is_taken = true; 		}								return $is_taken;	}	/**	 * checks whether a email is taken	 *	 * @param string, $email	 * @return boolean, $is_taken	 */	public function isEmailTaken( $email ){				$is_taken	=	false;				try {						$user	=	$this->DataAdapter->retrieveByEmail( $email );		}		catch (PDOException $e ) {		   			throw new UserException( $e->getMessage() );		}								//check email and email from database match		if( $user['email'] == $email ){						$is_taken = true; 		}								return $is_taken;	}	/**	 * registers a new user	 *	 * @param string, $username	 * @param string,  $password	 * @param string,  $email	 */	public function register($username, $password, $email){				$user	=	array();		$user['name']	=	$username;		$user['pass']	=	$this->createHash( $password );		$user['email']	=	$email;						//add to database		try {						$user_id	=	$this->DataAdapter->insert( $user );		}		catch (PDOException $e ) {		   			throw new UserException( $e->getMessage() );		}								return $user_id;	}	/**	 * creates a hash	 * @param String, key	 * @return String, hash	 */	public function createHash( $key ){				return md5( md5($key) );	}}/** * @author Fidel Nunez * @copyright 2007, all rights reserved * @name UserException * @abstract exception thrown by User */class UserException extends Exception{    	public function __construct($message, $code = 0) {		        parent::__construct($message, $code);    }}/** * @author Fidel Nunez * @copyright 2007, all rights reserved * @abstract user database data class */class UserDatabase{					//instance of a database object	private $DB;			/**	 * constructor	 * @param PDO database object	 * @throws Exception	 */	public function __construct( $DB ){				if( is_a( $DB, 'PDO' ) == false){						throw new Exception( 'Database object is not PDO type' );		}				$this->DB	=	$DB;	}	/**	 * retrieves a user given a username	 * @param string, $username	 * @return array, $rows	 	 	 * @throws PDOException	 	 	 */	public function retrieveByUsername( $username ){				$row		=	array();				try {						$STMT = $this->DB->prepare(							"SELECT *  				FROM users   				WHERE users.name = :name 				LIMIT 0,1" 			);						$STMT->bindParam( ":name", $username, PDO::PARAM_STR );			$STMT->execute();						$row	=	$STMT->fetch( PDO::FETCH_ASSOC );		} 		catch (PDOException $e ) {		   		   	throw new PDOException( $e->getMessage() );		}				return $row;	}	/**	 * retrieves a user given a email	 * @param string, $email	 * @return array, $rows	 	 	 * @throws PDOException	 	 	 */	public function retrieveByEmail( $email ){				$row		=	array();				try {						$STMT = $this->DB->prepare(							"SELECT *  				FROM users   				WHERE users.email = :email 				LIMIT 0,1" 			);						$STMT->bindParam( ":email", $email, PDO::PARAM_STR );			$STMT->execute();						$row	=	$STMT->fetch( PDO::FETCH_ASSOC );		} 		catch (PDOException $e ) {		   		   	throw new PDOException( $e->getMessage() );		}				return $row;	}	/**	 * adds a new user to the system	 * @param array, $user	 * @return int, $insert_id 	 	 * @throws PDOException	 	 	 */	public function insert( $user ){				$time	=	time();		try {						$STMT = $this->DB->prepare(					"INSERT INTO users (					name, 					pass, 					email, 					time_created, 					time_updated 				) VALUES (					:name, 					:pass, 					:email, 					:time_created,					:time_updated 				)" 			);							$STMT->bindParam( ":name", 			$user['name'], 	PDO::PARAM_STR );			$STMT->bindParam( ":pass", 			$user['pass'], 	PDO::PARAM_STR );			$STMT->bindParam( ":email", 		$user['email'], PDO::PARAM_STR );			$STMT->bindParam( ":time_created", 	$time,			PDO::PARAM_INT );			$STMT->bindParam( ":time_updated", 	$time, 			PDO::PARAM_INT );			$STMT->execute();						$insert_id = $this->DB->lastInsertId();					} 		catch (PDOException $e ) {		   		   	throw new PDOException( $e->getMessage() );		}				return $insert_id;	}}?>