Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- # Login
- function session_defaults()
- {
- $_SESSION['logged'] = false;
- $_SESSION['uid'] = 0;
- $_SESSION['account'] = "";
- $_SESSION['cookie'] = 0;
- $_SESSION['remember'] = false;
- if(!isset($_SESSION['uid']))
- {
- session_defaults();
- }
- }
- class User
- {
- var $db = null; // PEAR::DB pointer
- var $failed = false; // failed login attempt
- var $date; // current date GMT
- var $id = 0; // the current user's id
- function User(&$db)
- {
- $this->db = $db;
- $this->date = $GLOBALS['date'];
- if($_SESSION['logged'])
- {
- $this->_checkSession();
- }
- elseif(isset($_COOKIE['mtwebLogin']))
- {
- $this->_checkRemembered($_COOKIE['mtwebLogin']);
- }
- }
- }
- $date = gmdate("d-m-Y");
- $db = db_connect();
- $user = new User($db);
- function _checkLogin($account, $password, $remember)
- {
- $account = $this->db->quote($account);
- $password = $this->db->quote(md5($password));
- $sql = "SELECT * FROM controlpannel_users WHERE " .
- "account = $account AND " .
- "password = $password";
- $result = $this->db->getRow($sql);
- if(is_object($result))
- {
- $this->_setSession($result, $remember);
- return true;
- }
- else
- {
- $this->failed = true;
- $this->_logout();
- return false;
- }
- }
- function _setSession(&$values, $remember, $init = true)
- {
- $this->id = $values->id;
- $_SESSION['uid'] = $this->id;
- $_SESSION['account'] = htmlspecialchars($values->account);
- $_SESSION['cookie'] = $values->cookie;
- $_SESSION['logged'] = true;
- if($remember)
- {
- $this->updateCookie($values->cookie, true);
- }
- if($init)
- {
- $session = $this->db->quote(session_id());
- $ip = $this->db->quote($_SERVER['REMOTE_ADDR']);
- $sql = "UPDATE controlpannel SET session = $session, ip = $ip WHERE " .
- "id = $this->id";
- $this->db->query($sql);
- }
- }
- function updateCookie($cookie, $save)
- {
- $_SESSION['cookie'] = $cookie;
- if($save)
- {
- $cookie = serialize(array($_SESSION['account'], $cookie));
- set_cookie('mtwebLogin', $cookie, time() + 31104000, '/directory/');
- }
- }
- function _checkRemembered($cookie)
- {
- list($account, $cookie) = @unserialize($cookie);
- if(!$account or !$cookie)
- return;
- $account = $this->db->quote($account);
- $cookie = $this->db->quote($cookie);
- $sql = "SELECT * FROM controlpannel WHERE " .
- "(account = $account) AND (cookie = $cookie)";
- $result = $this->db->getRow($sql);
- if(is_object($result))
- {
- $this->_setSession($result, true);
- }
- }
- function _checkSession()
- {
- $account = $this->db->quote($_SESSION['account']);
- $cookie = $this->db->quote($_SESSION['cookie']);
- $session = $this->db->quote(session_id());
- $ip = $this->db->quote($_SERVER['REMOTE_ADDR']);
- $sql = "SELECT * FROM member WHERE " .
- "(account = $account) AND (cookie = $cookie) AND " .
- "(session = $session) AND (ip = $ip)";
- $result = $this->db->getRow($sql);
- if (is_object($result))
- {
- $this->_setSession($result, false, false);
- }
- else
- {
- $this->_logout();
- }
- }
- ?>
Add Comment
Please, Sign In to add comment