Source for file user_class.php
Documentation is available at user_class.php
* This file contains the user class.
* This file is part of HRDIODB.
* HRDIODB is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* HRDIODB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with HRDIODB; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* The HRDI user class is responsible for tracking and maintaining user
* Regular expression for e-mail verification. Taken from:
* {@link http://www.developer.com/lang/php/article.php/3290141}
var $email_regexp = '^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$';
* Any error that gets generated.
* The user class constructor.
* @param hrdi_db $hdb A hrdi_db instance
* @param session $session A session instance.
* the command line tools.
function user(&$hdb, $session= FALSE) {
$this->session = $session;
$this->data = $this->session->get_var("user_data");
* Checks if the user is authenticated.
* Returns TRUE if the user is authenticated, FALSE otherwise.
return $this->session->get_var("authenticated");
* Authenticates a HRDI admin.
* A method that authenticates a user against the HRDI_Users table.
* @param string $email The user's e-mail
* @param string $password The user's password
* @return TRUE|FALSEStatus of the authentication attempt
// Verify the e-mail format
$this->error = "E-mail address is not valid";
AND Password=MD5('$password')";
if ($data = $this->hdb->get_row($sql)) {
// Authentication successful
$this->session->set_var('authenticated', TRUE);
$this->session->set_var("user_data", $data);
$this->error = "Invalid email or password";
* This function is responsible for registering a new user. It adds an entry
* to the HRDI_Users table.
* @param array $data New user data
// Clean and verify the user data
$data["Password"] = md5($data["Password"]);
unset ($data["Password2"]);
return $this->hdb->add($data, "HRDI_Users");
* Checks validity of an e-mail.
* This function checks the validity of an e-mail address by using a
* @param string E-mail address
* @return TRUE|FALSETRUE if e-mail is valid, FALSE otherwise
* Updates the current user's data.
* This function is responsible for updating the current user's data.
* @param array $data New user data
// Clean and verify user data
unset($data["Password2"]);
if (!strlen($data["Password"])) {
unset($data["Password"]);
$data["Password"] = md5($data["Password"]);
if ($this->hdb->update($data, "HRDI_Users", "ID")) {
/* If the update succeeds, we need to update the data that stored in
* the session variable as well. This way, we don't need to log out
* and log back in for the changes to take place.
$d = $this->session->get_var("user_data");
foreach ($data as $k => $v) {
// Set the updated data, but only the keys that were there before
$this->session->set_var("user_data", $d);
* Cleans and verifies user data.
* This function cleans and verifies user data for new or current users.
* @param array $data User data
* @param string $type Type of verification, one of "register" or "update"
function clean_and_verify(&$data, $type) {
if ($type == "register") {
// If we're registering, clean the e-mail
$data['Email'] = trim(strtolower($data['Email']));
WHERE Email='{ $data['Email']}' ";
if ($exists = $this->hdb->get_row($sql)) {
$this->error = "The e-mail address is already registered";
// Clean the first name, last name, password 1 and 2
$data['Firstname'] = trim(stripslashes($data['Firstname']));
$data['Lastname'] = trim(stripslashes($data['Lastname']));
$data['Password'] = trim(stripslashes($data['Password']));
$data['Password2'] = trim(stripslashes($data['Password2']));
if ($type == "register") {
// If we're registering, verify the e-mail format
$this->error = "E-mail address is not valid";
// If we're checking DNS, extract the domain from the e-mail
list($uname, $domain) = split('@', $data['Email']);
// Look up the domain in DNS
if (!checkdnsrr($domain)) {
$this->error = "DNS lookup failed for the domain $domain";
// Make sure we have a first name
if (!strlen($data['Firstname'])) {
$this->error = "First name cannot be empty";
// Make sure we have a last name
if (!strlen($data['Lastname'])) {
$this->error = "Last name cannot be empty";
if ($type == "register") {
// If we're registering, we better have a password
if (!strlen($data['Password'])) {
$this->error = "Password cannot be empty";
// Verify that the passwords are the same
if ($data['Password'] != $data['Password2']) {
$this->error = "Passwords do not match";
* Resets a user's password.
* This function verifies that the e-mail exists in the database and resets
* the password to a random one. It then e-mails the user with the new
* @param array $data Data array containing the e-mail address
function reset_password($data) {
global $hrdi_admin, $pass_length, $mail_from, $mail_subject, $mail_message;
// Clean the e-mail for the database lookup
$data['Email'] = trim(strtolower($data['Email']));
// Verify the e-mail format
$this->error = "E-mail address is not valid";
WHERE Email='{ $data['Email']}'
if ($info = $this->hdb->get_row($sql)) {
// We found the e-mail, all is well. Now let's generate a new password.
$data['ID'] = $info['ID'];
$data['Password'] = md5($password);
// Update the database with the new password
if (!$this->hdb->update($data, "HRDI_Users", "ID")) {
// Place the generated password in the e-mail body
$mail_message = str_replace("RANDOM_PASS", $password, $mail_message);
// Add the e-mail headers
$headers = "From: $mail_from\r\n";
if (mail($info['Email'], $mail_subject, $mail_message, $headers)) {
$this->error = "The e-mail failed to send. Please contact
<a href=\"mailto:$hrdi_admin\">$hrdi_admin</a>
$this->error = "That e-mail is not registered";
* Generates a random password.
* This function generates a random password $length characters in length.
* It generates the password from the set of valid letters and numbers.
* @param string $length Desired length of the random password
* @return string The random password
function generate_password($length) {
$letters = "abcdefghijklmnopqrstuvwxyz";
// Generate the random password of length $length
for ($i = 0; $i < $length; $i++) {
// Insert a number every 4th letter. This can be changed.
$rn = mt_rand(0, (strlen($letters) - 1));
$password .= substr($letters, $rn, 1);
$rn = mt_rand(0, (strlen($numbers) - 1));
$password .= substr($numbers, $rn, 1);
// Shuffle the characters arround and return the password
return str_shuffle($password);
* Getter for the user id.
* This function returns the user id of the authenticated user.
* @return integer User id
return $this->data['ID'];
* Checks if the user is an admin.
* This function returns TRUE if the user has admin privileges.
return $this->data["Admin"];
* This function logs the user out by destroying the session.
|