Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.69 KB | None | 0 0
  1. <?php
  2. //Some settings
  3. //define('PUN', '1');
  4. define('PUN_ROOT', './forum/');//Where are the forum placed? Relative to the current working dir
  5.  
  6. class fluxbb{
  7.     private static $db;
  8.     private static $config;
  9.     //
  10.     //Loads settings from the fluxbb config file so it they becomes avaible for this class
  11.     //
  12.     static function init(){
  13.         include_once PUN_ROOT . "config.php";
  14.         self::$config['cookie_name'] = $cookie_name;
  15.         self::$config['cookie_domain'] = $cookie_domain;
  16.         self::$config['cookie_path'] = $cookie_path;
  17.         self::$config['cookie_secure'] = $cookie_secure;
  18.         self::$config['cookie_seed'] = $cookie_seed;
  19.         include PUN_ROOT . "include/dblayer/common_db.php";
  20.         self::$db = $db;
  21.     }
  22.     //
  23.     //Returns the user ID for the given username
  24.     static function getUserId($user){  
  25.         $result = self::$db->query("SELECT * FROM ". self::$db->prefix."users WHERE username ='" . $user . "'");
  26.         $row = self::$db->fetch_assoc($result);
  27.         if(empty($row)){
  28.             return false;
  29.         }else{
  30.             return $row['id'];
  31.         }
  32.     }
  33.     static function hash($str){
  34.         return sha1($str);
  35.     }
  36.    
  37.     static function setcookie($name, $value, $expire)
  38.     {  
  39.  
  40.  
  41.     // Enable sending of a P3P header
  42.     header('P3P: CP="CUR ADM"');
  43.  
  44.     if (version_compare(PHP_VERSION, '5.2.0', '>=')){
  45.         if(empty($name) OR empty($value) OR empty(self::$config)){
  46.         die("die");
  47.         }
  48.         if(!setcookie($name, $value, $expire, self::$config['cookie_path'], self::$config['cookie_domain'], self::$config['cookie_secure'], true)){
  49.             echo "Failed to set cookie" . var_dump(self::$config), $name, $value;
  50.         }else{
  51.             echo "Cookie set";
  52.            
  53.         }
  54.     }else{
  55.         setcookie($name, $value, $expire, self::$config['cookie_path'].'; HttpOnly', self::$config['cookie_domain'], self::$config['cookie_secure']);
  56.     }
  57.     }
  58.  
  59.     //
  60.     // Authenticates the provided username and password against the user database
  61.     // $user can be either a user ID (integer) or a username (string)
  62.     // $password can be either a plaintext password or a password hash including salt ($password_is_hash must be set accordingly)
  63.     //
  64.  
  65.     static function authenticate_user($user, $password)
  66.     {
  67.         $sql = "SELECT * FROM users WHERE username = '" . $user . "' AND password = '" . self::hash($password) . "'";
  68.         $result = self::$db-> query($sql);
  69.         if(self::$db->affected_rows($result) == true){
  70.             return true;
  71.         }else{
  72.             return false;
  73.         }
  74.        
  75.     }
  76.  
  77.     //
  78.     //Log in a user after checking username and password against the database
  79.     //
  80.     static function login($user, $password){
  81.         if(self::authenticate_user($user, $password)===true){
  82.             $expire = time() + 1209600;
  83.             self::setcookie(self::getUserId($user), self::hash($password), $expire);
  84.             return true;   
  85.         }else{
  86.             return false;
  87.         }
  88.     }
  89.    
  90. }
  91. fluxbb::init();
  92. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement