Ver Mensaje Individual
  #2 (permalink)  
Antiguo 15/02/2010, 06:48
Nisrokh
 
Fecha de Ingreso: septiembre-2009
Ubicación: Neuquén
Mensajes: 142
Antigüedad: 14 años, 6 meses
Puntos: 12
Respuesta: [APORTE] Sistema de Sesiones y Autentificación de usuario con MySQL

SimpleSession.php
Código PHP:
Ver original
  1. <?php
  2.  
  3. /**
  4.  * @author estilodg.com <[email protected]>
  5.  * @copyright 2010
  6.  */
  7.  
  8. // Error code constants
  9. define('LOGIN_ACCESS_NO_ATTEMPT', -1);
  10. define('LOGIN_ACCESS_GRANTED', 0);
  11. define('LOGIN_ACCESS_ERROR_ATTEMPT_FAILED', 1);
  12. define('LOGIN_ACCESS_ERROR_NONEXISTENT_ACCOUNT', 2);
  13. define('LOGIN_ACCESS_ERROR_UNAUTHORIZED_ACCOUNT', 3);
  14. define('LOGIN_ACCESS_ERROR_UNVALIDATED_ACCOUNT', 4);
  15. define('LOGIN_ACCESS_ERROR_BLOCKED_ACCOUNT', 5);
  16. define('LOGIN_ACCESS_ERROR_MISSING_USERNAME', 6);
  17. define('LOGIN_ACCESS_ERROR_MISSING_PASSWORD', 7);
  18. define('LOGIN_ACCESS_ERROR_UNKNOWN_ERROR', 8);
  19.  
  20. // Session data encryption keys
  21. define('SESSION_DATA_ENCRYPTION_KEY_1', '944672318');
  22. define('SESSION_DATA_ENCRYPTION_KEY_2', '410344323');
  23.  
  24. /**
  25.  * Simple Login Management
  26.  *
  27.  * @package SimpleSession
  28.  * @subpackage SimpleLogin
  29.  * @author Diego P. M. Baltar <[email protected]>
  30.  * @copyright 2010
  31.  * @version 0.1
  32.  */
  33. abstract class SimpleLogin {
  34.    
  35.     /**
  36.      * Authentification salt
  37.      *
  38.      * @access private
  39.      * @var string
  40.      */
  41.     private $auth_salt = '$1$CZ..jd4.$XUyjB06VQ7K.E3yKw7hXh/';
  42.    
  43.     /**
  44.      * Login status
  45.      *
  46.      * @access public
  47.      * @var integer
  48.      */
  49.     public $status = LOGIN_ACCESS_NO_ATTEMPT;
  50.    
  51.     /**
  52.      * Login
  53.      *
  54.      * @access public
  55.      * @return void
  56.      */
  57.     public function login($username = '', $password = '') {
  58.        
  59.         if ($this->logged_in())
  60.             return;
  61.        
  62.         $username = trim($username);
  63.         $password = trim($password);
  64.        
  65.         if (!$username or empty($username)) {
  66.             $this->status = LOGIN_ACCESS_ERROR_MISSING_USERNAME;
  67.             return;
  68.         } else if (!$password or empty($password)) {
  69.             $this->status = LOGIN_ACCESS_ERROR_MISSING_PASSWORD;
  70.             return;
  71.         }
  72.        
  73.         // Encrypt password
  74.         $password = crypt($password, $this->auth_salt);
  75.        
  76.         // Build and execute query
  77.         $query_sentence = "
  78.             SELECT
  79.                 id, account_name, account_pass, account_type, account_status
  80.             FROM
  81.                 {$this->mysql->tables['users']}
  82.             WHERE
  83.                 account_name = '%s'
  84.         ";
  85.         $this->mysql->query(sprintf($query_sentence, $this->mysql->escape($username)));
  86.        
  87.         // If user exists...
  88.         if ($this->mysql->affected_rows > 0) {
  89.             $row = $this->mysql->query_result[0];
  90.             $this->mysql->clean();
  91.            
  92.             // 0 => Administrator, 1 => Moderator, 2 => User
  93.             // Allow administrators and moderators only
  94.             if ((int)$row->account_type > 1) {
  95.                 $this->status = LOGIN_ACCESS_ERROR_UNAUTHORIZED_ACCOUNT;
  96.                 return;
  97.             }
  98.            
  99.             // Check whether the user account is unvalidated
  100.             if ((int)$row->account_status == 0) {
  101.                 $this->status = LOGIN_ACCESS_ERROR_UNVALIDATED_ACCOUNT;
  102.                 return;
  103.             }
  104.            
  105.             // Check user password
  106.             if (substr($password, 0, 32) == $row->account_pass) {
  107.                
  108.                 // Now we check if the user account has been blocked
  109.                 if ((int)$row->account_status == -1) {
  110.                     $this->status = LOGIN_ACCESS_ERROR_BLOCKED_ACCOUNT;
  111.                     return;
  112.                 } else if ((int)$row->account_status == 1) {
  113.                     $this->status = LOGIN_ACCESS_GRANTED;
  114.                     $this->data->account_id = (int)$row->id;
  115.                     $this->data->account_name = $row->account_name;
  116.                     $this->data->account_type = (int)$row->account_type;
  117.                     $this->data->access = true;
  118.                     $this->regenerate_id(true);
  119.                     return;
  120.                 }
  121.             } else {
  122.                 $this->status = LOGIN_ACCESS_ERROR_ATTEMPT_FAILED;
  123.                 return;
  124.             }
  125.         } else {
  126.             $this->status = LOGIN_ACCESS_ERROR_NONEXISTENT_ACCOUNT;
  127.             return;
  128.         }
  129.     }
  130.    
  131.     public function logout() {
  132.         if ($this->logged_in())
  133.             $this->destroy();
  134.     }
  135.    
  136.     /**
  137.      * Check if user is logged in
  138.      *
  139.      * @access public
  140.      * @return boolean
  141.      */
  142.     public function logged_in() {
  143.         if (!isset($this->data->account_name))
  144.             unset($this->data->access);
  145.         else {
  146.             $username = trim($this->data->account_name);
  147.             if (!$username or empty($username)) {
  148.                 unset($this->data->account_id);
  149.                 unset($this->data->account_name);
  150.                 unset($this->data->account_type);
  151.                 unset($this->data->access);
  152.             }
  153.         } return $this->data->access;
  154.     }
  155. }
  156.  
  157. // Continua...
  158.  
  159. ?>