Share Pastebin
Guest
Public paste!

XikeonUserlib v1

By: a guest | Jul 4th, 2007 | Syntax: PHP | Size: 6.06 KB | Hits: 99 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3.  * CodeIgniter
  4.  *
  5.  * An open source application development framework for PHP 4.3.2 or newer
  6.  *
  7.  * @package             CodeIgniter
  8.  * @author              Rick Ellis
  9.  * @copyright   Copyright (c) 2006, EllisLab, Inc.
  10.  * @license             http://www.codeignitor.com/user_guide/license.html
  11.  * @link                http://www.codeigniter.com
  12.  * @since               Version 1.0
  13.  * @filesource
  14.  */
  15.  
  16. // ------------------------------------------------------------------------
  17.  
  18. /**
  19.  * CodeIgniter User Class v1
  20.  *
  21.  * This class contains functions that will give you the option to run a Member/User/Login system
  22.  *
  23.  * @package             CodeIgniter
  24.  * @subpackage  Libraries
  25.  * @category    User System
  26.  * @author              Mike Owens [Xikeon]
  27.  * @link                http://www.xikeon.com/
  28.  */
  29. class Userlib {
  30.        
  31.         /**
  32.          * Constructor
  33.          *
  34.          * Get instance for Database Lib
  35.          *
  36.          * @access      public
  37.          */
  38.         function Userlib()
  39.         {
  40.                 $this->CI =& get_instance();
  41.                
  42.                 log_message('debug', "User Class Initialized");
  43.         }
  44.        
  45.         // --------------------------------------------------------------------
  46.  
  47.         /**
  48.          * Register User
  49.          *
  50.          * @access      public
  51.          * @param       string  the username
  52.          * @param       string  the password
  53.          * @param       string  the email
  54.          * @param       string  the ip
  55.          * @param       integer the status
  56.          * @param       boolean check for email
  57.          * @param       boolean check for IP
  58.          * @return      string  if the user was created, if not returns errors in Unordered List
  59.          */
  60.         function register( $username, $password, $email, $status = 1, $emailcheck = TRUE, $ipcheck = TRUE )
  61.         {
  62.                 if( $ipcheck )
  63.                 {
  64.                         $qip = $this->CI->db->query( "SELECT * FROM `users` WHERE ip='" . mysql_real_escape_string( $_SERVER[ 'REMOTE_ADDR' ] ) . "'" );
  65.                         if( $qip->num_rows( ) > 0 )
  66.                         {
  67.                                 return 'This IP is already assigned to an account.';
  68.                         }
  69.                 }
  70.                
  71.                 if( $emailcheck )
  72.                 {
  73.                         $qemail = $this->CI->db->query( "SELECT * FROM `users` WHERE email='" . mysql_real_escape_string( $email ) . "'" );
  74.                         if( $qemail->num_rows( ) > 0 )
  75.                         {
  76.                                 return 'This IP is already assigned to an account.';
  77.                         }
  78.                 }
  79.                
  80.                 $qusername = $this->CI->db->query( "SELECT * FROM `users` WHERE username='" . mysql_real_escape_string( $username ) . "'" );
  81.                 if( $qusername->num_rows( ) > 0 )
  82.                 {
  83.                         return 'This username is already taken.';
  84.                 }
  85.                
  86.                 $this->CI->db->query( "INSERT INTO `users`
  87.                                                         ( username, password, email, ip, status )
  88.                                                         VALUES
  89.                                                         ( '" . mysql_real_escape_string( $username ) . "', '" . sha1( md5( $password ) ) . "', '" . mysql_real_escape_string( $email ) . "',
  90.                                                         '" . mysql_real_escape_string( $_SERVER[ 'REMOTE_ADDR' ] ) . "', '" . mysql_real_escape_string( $status ) . "' )" );
  91.         }
  92.        
  93.         // --------------------------------------------------------------------
  94.  
  95.         /**
  96.          * Login User
  97.          *
  98.          * @access      public
  99.          * @param       string  the username
  100.          * @param       string  the password
  101.          * @return      boolean if the user was logged in or not
  102.          */
  103.         function login( $username, $password )
  104.         {
  105.                 $qcheck = $this->CI->db->query( "SELECT * FROM `users` WHERE username='" . mysql_real_escape_string( $username ) . "' AND password='" . sha1( md5( $password ) ) . "'" );
  106.                 if( $qcheck->num_rows( ) == 0 )
  107.                 {
  108.                         return FALSE;
  109.                 }
  110.                
  111.                 $newdata = array(
  112.                                                 'username'      =>      $username,
  113.                                                 'password'      =>      sha1( md5( $password ) )
  114.                 );
  115.  
  116.                 $this->CI->session->set_userdata( $newdata );
  117.                
  118.                 return TRUE;
  119.         }
  120.        
  121.         // --------------------------------------------------------------------
  122.  
  123.         /**
  124.          * Logged in
  125.          *
  126.          * @access      public
  127.          * @return      boolean if the user is logged in or not
  128.          */
  129.         function logged_in( )
  130.         {
  131.                 $lcheck = $this->CI->db->query( "SELECT * FROM `users` WHERE
  132.                                                                                 username='" . mysql_real_escape_string( $this->CI->session->userdata( 'username' ) ) . "' AND
  133.                                                                                 password='" . mysql_real_escape_string( $this->CI->session->userdata( 'password' ) ) . "'" );
  134.                 if( $lcheck->num_rows( ) == 1 )
  135.                 {
  136.                         return TRUE;
  137.                 } else {
  138.                         return FALSE;
  139.                 }
  140.         }
  141.        
  142.         // --------------------------------------------------------------------
  143.  
  144.         /**
  145.          * Forgot Username or Password
  146.          *
  147.          * @access      public
  148.          * @param       string  the email
  149.          * @param       integer length of new password
  150.          * @return      boolean if the email was found and email was sent
  151.          */
  152.         function forgot( $email, $length )
  153.         {
  154.                 $qemail = $this->CI->db->query( "SELECT email FROM `users` WHERE email='" . mysql_real_escape_string( $email ) . "'" );
  155.                 if( $qemail->num_rows( ) > 0 )
  156.                 {
  157.                         /**
  158.                          * For strict servers [Home Servers?]
  159.                          */
  160.                         $new = '';
  161.                        
  162.                         for( $i = 1; $i <= $length; $i++ )
  163.                         {
  164.                                 $new .= rand( 1, 9 );
  165.                         }
  166.                        
  167.                         $this->CI->db->query( "UPDATE `users` SET password='" . sha1( md5( $new ) ) . "' WHERE email='" . mysql_real_escape_string( $email ) . "'" );
  168.                        
  169.                         mail( $email, "New Password", "Your new password is: " . $new );
  170.                        
  171.                         return TRUE;
  172.                 } else {
  173.                         return FALSE;
  174.                 }
  175.         }
  176.        
  177.         // --------------------------------------------------------------------
  178.  
  179.         /**
  180.          * Get Data
  181.          *
  182.          * @access      public
  183.          * @param       string  the username
  184.          * @param       string  what row to grab
  185.          * @return      string  the data
  186.          */
  187.         function getData( $username, $what )
  188.         {
  189.                 $lcheck = $this->CI->db->query( "SELECT " . mysql_real_escape_string( $what ) . " FROM `users` WHERE
  190.                                                                                 username='" . mysql_real_escape_string( $username ) . "'" );
  191.                 if( $lcheck->num_rows( ) == 1 )
  192.                 {
  193.                         $data = $lcheck->row( );
  194.                         return $data->$what;
  195.                 } else {
  196.                         return 'Username or row does not exist.';
  197.                 }
  198.         }
  199.        
  200.         // --------------------------------------------------------------------
  201.  
  202.         /**
  203.          * Get Age
  204.          *
  205.          * @access      public
  206.          * @param       string  birth-day
  207.          * @param       string  birth-month
  208.          * @param       string  birth-year
  209.          * @return      integer how many years
  210.          */
  211.         function getAge( $day, $month, $year )
  212.         {
  213.                 $now_year = date( "Y" );
  214.                 $now_month = date( "m" );
  215.                 $now_day = date( "d" );
  216.                
  217.                 $years = $now_year - $year;
  218.                 $months = $now_month - $month;
  219.                 if( $months < 0 )
  220.                 {
  221.                         $years--;
  222.                 }
  223.                 else if( $months == 0 )
  224.                 {
  225.                         $days = $now_day - $day;
  226.                         if( $days < 0 )
  227.                         {
  228.                                 $years--;
  229.                         }
  230.                 }
  231.                
  232.                 return $years;
  233.         }
  234. }
  235. // END User Class
  236. ?>