Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Some settings
- //define('PUN', '1');
- define('PUN_ROOT', './forum/');//Where are the forum placed? Relative to the current working dir
- class fluxbb{
- private static $db;
- private static $config;
- //
- //Loads settings from the fluxbb config file so it they becomes avaible for this class
- //
- static function init(){
- include_once PUN_ROOT . "config.php";
- self::$config['cookie_name'] = $cookie_name;
- self::$config['cookie_domain'] = $cookie_domain;
- self::$config['cookie_path'] = $cookie_path;
- self::$config['cookie_secure'] = $cookie_secure;
- self::$config['cookie_seed'] = $cookie_seed;
- include PUN_ROOT . "include/dblayer/common_db.php";
- self::$db = $db;
- }
- //
- //Returns the user ID for the given username
- static function getUserId($user){
- $result = self::$db->query("SELECT * FROM ". self::$db->prefix."users WHERE username ='" . $user . "'");
- $row = self::$db->fetch_assoc($result);
- if(empty($row)){
- return false;
- }else{
- return $row['id'];
- }
- }
- static function hash($str){
- return sha1($str);
- }
- static function setcookie($name, $value, $expire)
- {
- // Enable sending of a P3P header
- header('P3P: CP="CUR ADM"');
- if (version_compare(PHP_VERSION, '5.2.0', '>=')){
- if(empty($name) OR empty($value) OR empty(self::$config)){
- die("die");
- }
- if(!setcookie($name, $value, $expire, self::$config['cookie_path'], self::$config['cookie_domain'], self::$config['cookie_secure'], true)){
- echo "Failed to set cookie" . var_dump(self::$config), $name, $value;
- }else{
- echo "Cookie set";
- }
- }else{
- setcookie($name, $value, $expire, self::$config['cookie_path'].'; HttpOnly', self::$config['cookie_domain'], self::$config['cookie_secure']);
- }
- }
- //
- // Authenticates the provided username and password against the user database
- // $user can be either a user ID (integer) or a username (string)
- // $password can be either a plaintext password or a password hash including salt ($password_is_hash must be set accordingly)
- //
- static function authenticate_user($user, $password)
- {
- $sql = "SELECT * FROM users WHERE username = '" . $user . "' AND password = '" . self::hash($password) . "'";
- $result = self::$db-> query($sql);
- if(self::$db->affected_rows($result) == true){
- return true;
- }else{
- return false;
- }
- }
- //
- //Log in a user after checking username and password against the database
- //
- static function login($user, $password){
- if(self::authenticate_user($user, $password)===true){
- $expire = time() + 1209600;
- self::setcookie(self::getUserId($user), self::hash($password), $expire);
- return true;
- }else{
- return false;
- }
- }
- }
- fluxbb::init();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement