· 8 years ago · Jun 04, 2017, 06:32 PM
1<?php
2///////////////////////////////// CONFIG /////////////////////////////
3
4// CONNECT TO DB, OTHER CRAP BLAH
5require_once("session.php");
6
7$session = new Session;
8
9$secret_key = '103041231'; // Random string, change it.
10
11if(isset($_SESSION['id']) && isset($_SESSION['hash'])) {
12 $user = $session->checkLogin($secret_key);
13}
14
15
16///////////////////////////////// SESSION /////////////////////////////
17
18//OPEN CLASS
19class fuck
20{
21 public $time;
22 public $logged_in = 0;
23
24 public function __construct()
25 {
26 $this->time = time();
27
28 $this->startSession();
29 }
30
31 public function startSession()
32 {
33 session_start();
34 }
35
36 // Check login
37 public function checkLogin($secret_key) {
38 global $db;
39
40 if (isset($_COOKIE["SITE_COOKIE"])) :
41 $data = explode('-', $_COOKIE["SITE_COOKIE"]);
42 $_SESSION['id'] = $data[1];
43 $_SESSION['hash'] = $data[0];
44 endif;
45
46 $this->id = $_SESSION['id'];
47 $this->hashkey = $_SESSION['hash'];
48
49 if (!isset($this->id) || !isset($this->hashkey)) {
50 $this->logout();
51 } else {
52 $check = sha1($this->id . $_SERVER['REMOTE_ADDR'] . $secret_key);
53 if ($check != $this->hashkey) {
54 $this->logout();
55 } else {
56 $query = $db->execute("SELECT * FROM users WHERE id='".$this->id."'");
57 $userarray = $db->fetchassoc($query);
58 if ($db->numrows($query) == 0) {
59 $this->logout();
60 }
61 foreach($userarray as $key=>$value) {
62 $user->$key = $value;
63 }
64 $this->logged_in = 1;
65 return $user;
66 }
67 }
68 }
69
70 //Login
71 public function login($username, $password, $uremember, $secret_key) {
72 global $msgError;
73
74 $this->username = mysql_real_escape_string($username);
75 $this->password = mysql_real_escape_string($password);
76
77 if (empty($this->username) || empty($this->password)) {
78 $msgError = "You have left empty fields!";
79
80 return;
81 }
82
83 $result = User::confirmUserPass($this->username, $this->password);
84
85 if ($result == 0 || $result == 2) {
86 $msgError = "<span>Error!</span>Please enter valid username and password.";
87
88 return;
89 } elseif ($result == 3) {
90 $msgError = "<span>Error!</span>Your user account has not been activated yet!";
91
92 return;
93 }
94
95 if (empty($msgError)) {
96
97 $this->userinfo = User::getUserInfo($this->username);
98
99 $this->id = $_SESSION['id'] = $this->userinfo['id'];
100 $this->hashkey = $_SESSION['hash'] = sha1($this->id . $_SERVER['REMOTE_ADDR'] . $secret_key);
101 $this->username = $_SESSION['username'] = $this->userinfo['username'];
102
103 User::updateUserField($this->username, "timestamp", $this->time);
104 User::updateUserField($this->username, "cookie_id", $this->cookie_id);
105 User::addActiveUser($this->username, $this->time);
106 User::removeActiveGuest($_SERVER['REMOTE_ADDR']);
107 User::updateUserField($this->username, "ip", $_SERVER['REMOTE_ADDR']);
108
109 if ($uremember) {
110 setcookie("SITE_COOKIE", $this->hashkey . '-' . $this->id, time() + COOKIE_EXPIRE, COOKIE_PATH);
111 }
112
113 $this->logged_in = 1;
114
115 return true;
116 } else {
117 return false;
118 }
119 }
120
121 // LOG OUT
122 public function logout() {
123 if (isset($_COOKIE["SITE_COOKIE"])) {
124 setcookie("SITE_COOKIE", "", time() - COOKIE_EXPIRE, COOKIE_PATH);
125 }
126
127 session_unset();
128
129 session_destroy();
130
131 $this->logged_in = 0;
132
133 redirect("index.php");
134 }
135
136// CLOSE CLASS
137}