Advertisement
Guest User

Untitled

a guest
May 14th, 2017
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.50 KB | None | 0 0
  1. <?
  2.     /*
  3.         PHP/Mysql Account System
  4.         Written by Benjamin Knox
  5.         Email: knoxius@knoxius.com
  6.         ©Knoxius.com 2010
  7.     */
  8.    
  9.     //Connect to MySql - Select database to use
  10.     require($_SERVER['DOCUMENT_ROOT'].'/assets/class/connect.php');
  11.     mysql_select_db('knoxius8_account');
  12.    
  13.     class account {
  14.         //Random character string generator
  15.         //Credit to Mich (michhimself.com) and james@coretelecom.co.uk
  16.         private function str_rand($length) {       
  17.             $chars = array_merge(range('A', 'Z'), range('a', 'z'),range(0, 9));
  18.             $out = "";
  19.                 for($i=0; $i < $length; $i++) {
  20.                     $string .= $chars[mt_rand(0,count($chars)-1)];
  21.                 }
  22.             return $string;
  23.         }
  24.        
  25.         //Check fields lengths and validity
  26.         public function check_field($string) {
  27.             if(empty($string)) {
  28.                 return false;
  29.             } else {
  30.                 return true;
  31.             }
  32.         }
  33.        
  34.         //Clear the string of exploitive characters
  35.         public function clear_string($string) {
  36.             $string = mysql_real_escape_string($string);
  37.             $string = strip_tags($string);
  38.             $string = trim($string);
  39.             $string = addslashes($string);
  40.             return $string;
  41.         }
  42.        
  43.         //Locate the user in the database
  44.         public function find_user($username) {
  45.             $query = 'SELECT * FROM users WHERE username=\''.$username.'\'';
  46.             $check = mysql_query($query);
  47.            
  48.             $returned = mysql_num_rows($check);
  49.             if($returned == 0) {
  50.                 return false;
  51.             } else {
  52.                 return true;
  53.             }
  54.         }
  55.        
  56.         //Retrieve specified item from user's information
  57.             //Current columns within the table include:
  58.             /* 'id', 'username', 'password', 'email', 'usergroup' */
  59.         public function get_info($username,$col) {
  60.             $query = 'SELECT * FROM users WHERE username=\''.$username.'\'';
  61.             $check = mysql_query($query);
  62.            
  63.             $info = mysql_fetch_array($check);
  64.             $selected = $info[$col];
  65.             return $selected;
  66.         }
  67.     }
  68.    
  69.     class login extends account {
  70.         private $username;
  71.         private $password;
  72.         private $remember;
  73.        
  74.         //Account Construction Function
  75.         public function __construct($username,$password,$remember) {
  76.             $this->username = parent::clear_string($username);
  77.             $this->password = parent::clear_string($password);
  78.             $this->remember = $remember;
  79.            
  80.             $this->begin_login();
  81.         }
  82.        
  83.         //Check if the password matches the stored password
  84.         private function check_pswd() {
  85.             $username = $this->username;
  86.             $password = md5($this->password);
  87.            
  88.             $sql_pswd = parent::get_info($username,'password');
  89.            
  90.             if($password != $sql_pswd) {
  91.                 return false;
  92.             } else {
  93.                 return true;
  94.             }
  95.         }
  96.        
  97.         //Check if the user has validated their account
  98.         //The user is invalid if their usergroup is 0
  99.         private function check_validity() {
  100.             $username = $this->username;
  101.            
  102.             $usergroup = parent::get_info($username,'usergroup');
  103.            
  104.             if($usergroup == 0) {
  105.                 return false;
  106.             } else {
  107.                 return true;
  108.             }
  109.         }
  110.        
  111.         //Set the login cookie
  112.         private function set_cookie() {
  113.             $username = $this->username;
  114.             $remember = $this->remember;
  115.             $code = parent::str_rand(10);
  116.             $code = md5($username.$code);
  117.            
  118.             if($remember == true) {
  119.                 setcookie('knoxius_account',$code,time()+60+60+24+30,'/','.knoxius.com');
  120.             } else {
  121.                 setcookie('knoxius_account',$code,0,'/','.knoxius.com');
  122.             }
  123.            
  124.             return $code;
  125.         }
  126.        
  127.         //Set the login session
  128.         private function create_session() {
  129.             session_start();
  130.            
  131.             $username = $this->username;
  132.             $code = parent::str_rand(10);
  133.             $sess_id = md5($username.$code);
  134.            
  135.             $_SESSION['knoxius_account'] = $sess_id;
  136.            
  137.             $cookie = $this->set_cookie();
  138.            
  139.             $query = 'INSERT INTO session VALUES(NULL,\''.$username.'\',\''.$cookie.'\',\''.$sess_id.'\'';
  140.             $create_sess = mysql_query($query);
  141.            
  142.             if(!$create_sess) {
  143.                 return false;
  144.             } else {
  145.                 return true;
  146.             }
  147.         }
  148.        
  149.         //Log the user in
  150.         private function begin_login() {
  151.             $username = $this->username;
  152.             $password = $this->password;
  153.            
  154.             if(empty($username) || empty($password)) { return 'Error: One or more fields were left blank.'; exit; }
  155.             if(!$this->check_pswd()) { return 'Error: Password incorrect for user \''.$username.'\'.'; exit; }
  156.             if(!$this->check_validity()) { return 'Error: User \''.$username.'\' has not been validated.'; exit; }
  157.             if(!parent::find_user($username)) { return 'Error: User \''.$username.'\' not found in database.'; exit; }
  158.            
  159.             $login = $this->create_session();
  160.            
  161.             if(!$login) {
  162.                 return 'An unknown error occurred and you were not logged in.';
  163.             }
  164.         }
  165.     }
  166.    
  167.     class register extends account {
  168.    
  169.     }
  170.    
  171.     class validate extends account {
  172.    
  173.     }
  174. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement