HRDIODB
[ class tree: HRDIODB ] [ index: HRDIODB ] [ all elements ]

Source for file user_class.php

Documentation is available at user_class.php

  1. <?php
  2. /**
  3.  * User class.
  4.  * This file contains the user class.
  5.  * @package HRDIODB
  6.  */
  7.  
  8. /**
  9.  * This file is part of HRDIODB.
  10.  *
  11.  * HRDIODB is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * HRDIODB is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with HRDIODB; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24.  */
  25.  
  26. /**
  27.  * HRDI user class.
  28.  * The HRDI user class is responsible for tracking and maintaining user
  29.  * information.
  30.  * @package HRDIODB
  31.  */
  32. class user {
  33.   /**
  34.    * Regular expression for e-mail verification. Taken from:
  35.    * {@link http://www.developer.com/lang/php/article.php/3290141}
  36.    * @var string 
  37.    */
  38.   var $email_regexp = '^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$';
  39.  
  40.   /**
  41.    * Any error that gets generated.
  42.    * @var string 
  43.    */
  44.   var $error;
  45.  
  46.   /**
  47.    * User data.
  48.    * @var array 
  49.    */
  50.   var $data = array();
  51.  
  52.   /**
  53.    * Constructor.
  54.    * The user class constructor.
  55.    * @param hrdi_db $hdb A hrdi_db instance
  56.    * @param session $session A session instance.
  57.    *  the command line tools.
  58.    */
  59.   function user(&$hdb$session=FALSE{
  60.     $this->hdb $hdb;
  61.  
  62.     if (is_object($session)) {
  63.       $this->session $session;
  64.       $this->data = $this->session->get_var("user_data");
  65.     }
  66.   }
  67.  
  68.   /**
  69.    * Checks if the user is authenticated.
  70.    * Returns TRUE if the user is authenticated, FALSE otherwise.
  71.    * @return TRUE|FALSE
  72.    */
  73.   function authenticated({
  74.     return $this->session->get_var("authenticated");
  75.   }
  76.  
  77.   /**
  78.    * Authenticates a HRDI admin.
  79.    * A method that authenticates a user against the HRDI_Users table.
  80.    * @param string $email The user's e-mail
  81.    * @param string $password The user's password
  82.    * @return TRUE|FALSEStatus of the authentication attempt
  83.    */
  84.   function authenticate($email$password{
  85.     $email trim(strtolower($email));
  86.     $password trim(stripslashes($password));
  87.  
  88.     // Verify the e-mail format
  89.     if (!$this->verify_email($email)) {
  90.       $this->error = "E-mail address is not valid";
  91.       return FALSE;
  92.     }
  93.  
  94.     $sql "SELECT *
  95.             FROM HRDI_Users
  96.             WHERE Email='$email'
  97.             AND Password=MD5('$password')";
  98.  
  99.     if ($data $this->hdb->get_row($sql)) {
  100.       // Authentication successful
  101.       $this->session->set_var('authenticated'TRUE);
  102.       $this->session->set_var("user_data"$data);
  103.       return TRUE;
  104.     else {
  105.       // Authentication failed
  106.       $this->error = "Invalid email or password";
  107.       return FALSE;
  108.     }
  109.   }
  110.  
  111.   /**
  112.    * Registers a new user.
  113.    * This function is responsible for registering a new user. It adds an entry
  114.    * to the HRDI_Users table.
  115.    * @param array $data New user data
  116.    * @return TRUE|FALSE
  117.    */
  118.   function register($data{
  119.     // Clean and verify the user data
  120.     if (!$this->clean_and_verify($data"register")) {
  121.       return FALSE;
  122.     }
  123.  
  124.     $data["Password"md5($data["Password"]);
  125.     unset($data["Password2"]);
  126.  
  127.     return $this->hdb->add($data"HRDI_Users");
  128.   }
  129.  
  130.   /**
  131.    * Checks validity of an e-mail.
  132.    * This function checks the validity of an e-mail address by using a
  133.    * regular expression.
  134.    * @param string E-mail address
  135.    * @return TRUE|FALSETRUE if e-mail is valid, FALSE otherwise
  136.    */
  137.   function verify_email($email{
  138.     return preg_match("/{$this->email_regexp}/", $email);
  139.   }
  140.  
  141.   /**
  142.    * Updates the current user's data.
  143.    * This function is responsible for updating the current user's data.
  144.    * @param array $data New user data
  145.    * @return TRUE|FALSE
  146.    */
  147.   function update($data) {
  148.     // Clean and verify user data
  149.     if (!$this->clean_and_verify($data"update")) {
  150.       return FALSE;
  151.     }
  152.  
  153.     unset($data["Email"]);
  154.     unset($data["Password2"]);
  155.  
  156.     if (!strlen($data["Password"])) {
  157.       unset($data["Password"]);
  158.     } else {
  159.       $data["Password"] = md5($data["Password"]);
  160.     }
  161.  
  162.     if ($this->hdb->update($data"HRDI_Users""ID")) {
  163.       /* If the update succeeds, we need to update the data that stored in
  164.        * the session variable as well. This way, we don't need to log out
  165.        * and log back in for the changes to take place.
  166.        */
  167.       $d = $this->session->get_var("user_data");
  168.  
  169.       foreach ($data as $k => $v{
  170.         $d[$k] = $v;
  171.       }
  172.  
  173.       // Set the updated data, but only the keys that were there before
  174.       $this->session->set_var("user_data"$d);
  175.       return TRUE;
  176.     } else {
  177.       return FALSE;
  178.     }
  179.   }
  180.  
  181.   /** 
  182.    * Cleans and verifies user data.
  183.    * This function cleans and verifies user data for new or current users.
  184.    * @param array $data User data
  185.    * @param string $type Type of verification, one of "register" or "update"
  186.    * @return TRUE|FALSE
  187.    */
  188.   function clean_and_verify(&$data, $type) {
  189.     global $check_dns;
  190.  
  191.     if ($type == "register") {
  192.       // If we're registering, clean the e-mail
  193.       $data['Email'] = trim(strtolower($data['Email']));
  194.  
  195.       $sql = "SELECT *
  196.               FROM HRDI_Users
  197.               WHERE Email='{$data['Email']}'";
  198.  
  199.       if ($exists = $this->hdb->get_row($sql)) {
  200.         $this->error = "The e-mail address is already registered";
  201.         return FALSE;
  202.       }
  203.     }
  204.  
  205.     // Clean the first name, last name, password 1 and 2
  206.     $data['Firstname'] = trim(stripslashes($data['Firstname']));
  207.     $data['Lastname'] = trim(stripslashes($data['Lastname']));
  208.     $data['Password'] = trim(stripslashes($data['Password']));
  209.     $data['Password2'] = trim(stripslashes($data['Password2']));
  210.  
  211.     if ($type == "register") {
  212.       // If we're registering, verify the e-mail format
  213.       if (!$this->verify_email($data['Email'])) {
  214.         $this->error = "E-mail address is not valid";
  215.         return FALSE;
  216.       }
  217.  
  218.       if ($check_dns) {
  219.         // If we're checking DNS, extract the domain from the e-mail
  220.         list($uname, $domain) = split('@', $data['Email']);
  221.  
  222.         // Look up the domain in DNS
  223.         if (!checkdnsrr($domain)) {
  224.           $this->error = "DNS lookup failed for the domain $domain";
  225.           return FALSE;
  226.         }
  227.       }
  228.     }
  229.  
  230.     // Make sure we have a first name
  231.     if (!strlen($data['Firstname'])) {
  232.       $this->error = "First name cannot be empty";
  233.       return FALSE;
  234.     }
  235.  
  236.     // Make sure we have a last name
  237.     if (!strlen($data['Lastname'])) {
  238.       $this->error = "Last name cannot be empty";
  239.       return FALSE;
  240.     }
  241.  
  242.     if ($type == "register") {
  243.       // If we're registering, we better have a password
  244.       if (!strlen($data['Password'])) {
  245.         $this->error = "Password cannot be empty";
  246.         return FALSE;
  247.       }
  248.     }
  249.  
  250.     // Verify that the passwords are the same
  251.     if ($data['Password'] != $data['Password2']) {
  252.       $this->error = "Passwords do not match";
  253.       return FALSE;
  254.     }
  255.  
  256.     return TRUE;
  257.   }
  258.  
  259.   /**
  260.    * Resets a user's password.
  261.    * This function verifies that the e-mail exists in the database and resets
  262.    * the password to a random one. It then e-mails the user with the new
  263.    * password.
  264.    * @param array $data Data array containing the e-mail address
  265.    * @return TRUE|FALSE
  266.    */
  267.   function reset_password($data) {
  268.     global $hrdi_admin, $pass_length, $mail_from, $mail_subject, $mail_message;
  269.  
  270.     // Clean the e-mail for the database lookup
  271.     $data['Email'] = trim(strtolower($data['Email']));
  272.  
  273.     // Verify the e-mail format
  274.     if (!$this->verify_email($data['Email'])) {
  275.       $this->error = "E-mail address is not valid";
  276.       return FALSE;
  277.     }
  278.     
  279.     $sql = "SELECT ID,Email
  280.             FROM HRDI_Users
  281.             WHERE Email='{$data['Email']}'
  282.             LIMIT 1";
  283.  
  284.     if ($info = $this->hdb->get_row($sql)) {
  285.       // We found the e-mail, all is well. Now let's generate a new password.
  286.       $password = $this->generate_password($pass_length);
  287.  
  288.       $data['ID'$info['ID'];
  289.       $data['Password'md5($password);
  290.  
  291.       // Update the database with the new password
  292.       if (!$this->hdb->update($data"HRDI_Users""ID")) {
  293.         return FALSE;
  294.       }
  295.  
  296.       // Place the generated password in the e-mail body
  297.       $mail_message = str_replace("RANDOM_PASS", $password, $mail_message);
  298.  
  299.       // Add the e-mail headers
  300.       $headers = "From$mail_from\r\n";
  301.  
  302.       // Send out the e-mail
  303.       if (mail($info['Email'], $mail_subject, $mail_message, $headers)) {
  304.         return TRUE;
  305.       } else {
  306.         $this->error = "The e-mail failed to sendPlease contact
  307.                         <a href=\"mailto:$hrdi_admin\">$hrdi_admin</a>
  308.                         to report the problem.";
  309.         return FALSE;
  310.       }
  311.     } else {
  312.       $this->error = "That e-mail is not registered";
  313.       return FALSE;
  314.     }
  315.   }
  316.  
  317.   /**
  318.    * Generates a random password.
  319.    * This function generates a random password $length characters in length.
  320.    * It generates the password from the set of valid letters and numbers.
  321.    * @param string $length Desired length of the random password
  322.    * @return string The random password
  323.    */
  324.   function generate_password($length) {
  325.     // Valid numbers
  326.     $numbers = "1234567890";
  327.  
  328.     // Valid letters
  329.     $letters = "abcdefghijklmnopqrstuvwxyz";
  330.  
  331.     $password = "";
  332.  
  333.     // Generate the random password of length $length
  334.     for ($i = 0; $i < $length; $i++) {
  335.       if ($i % 4) {
  336.         // Insert a number every 4th letter. This can be changed.
  337.         $rn = mt_rand(0, (strlen($letters) - 1));
  338.         $password .= substr($letters, $rn, 1);
  339.       } else {
  340.         // Insert a letter
  341.         $rn = mt_rand(0, (strlen($numbers) - 1));
  342.         $password .= substr($numbers, $rn, 1);
  343.       }
  344.     }
  345.  
  346.     // Shuffle the characters arround and return the password
  347.     return str_shuffle($password);
  348.   }
  349.  
  350.   /**
  351.    * Getter for the user id.
  352.    * This function returns the user id of the authenticated user.
  353.    * @return integer User id
  354.    */
  355.   function get_user_id() {
  356.     return $this->data['ID'];
  357.   }
  358.  
  359.   /**
  360.    * Checks if the user is an admin.
  361.    * This function returns TRUE if the user has admin privileges.
  362.    * @return TRUE|FALSE
  363.    */
  364.   function is_admin() {
  365.     return $this->data["Admin"];
  366.   }
  367.  
  368.   /**
  369.    * Logs the user out.
  370.    * This function logs the user out by destroying the session.
  371.    */
  372.   function logout() {
  373.     session_destroy();
  374.   }
  375. }

Documentation generated on Tue, 23 Jan 2007 22:57:54 -0500 by phpDocumentor 1.3.0RC6