summaryrefslogtreecommitdiff
path: root/forum/utils/login.php
blob: 87f76a9ee6ce40b11fd41aeb0d9ed45dd6afb269 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php

include_once($UTIL_DIR . "/users.php");
include_once($UTIL_DIR . "/log.php");

$users = new Users($DATA_DIR . "/users.xml");

function checklogin()
{
	// The cookies...
	global $HTTP_COOKIE_VARS;

	// User vars
	global $users;
	global $current_user;
	global $current_username;
	global $current_password;

	// What are we doin'?
	global $action;

	// Config vars
	global $DATA_DIR;
	global $ADMIN_TIMEOUT;

	if($action == "login") {
		/**
		 * Login
		 */

		$current_uid = $users->getUserID($current_username);
		$u = $users->getUser($current_uid);

		if($current_uid != -1 && $u && $u->checkPassword($current_password) ) {
			$current_user = $u;
			setcookie("current_uid", $current_uid, time()+$ADMIN_TIMEOUT);
			setcookie("current_password", $current_password, time()+$ADMIN_TIMEOUT);

			_log($u->username, "logged in");

			if($current_user->notified > 0) {
				$current_user->notified = 0;
				$users->write();
			}

		} else {
			// Remove cookies
			setcookie("current_uid", "", time()-1);
			setcookie("current_password", "", time()-1);
		}

	} else if($action == "logout") {

		/**
		 * Logout
		 */
		$u = $users->getUser($HTTP_COOKIE_VARS["current_uid"]);

		// Remove cookies
		setcookie("current_uid", "", time()-1);
		setcookie("current_password", "", time()-1);

		$current_uid = "";
		$current_password = "";
		$current_user = false;

		_log($u->username, "logged out");

	} else {

		/**
		 * Usage
		 */

		if($HTTP_COOKIE_VARS["current_uid"] == "") return;

		$u = $users->getUser($HTTP_COOKIE_VARS["current_uid"]);
		if($u->checkPassword($HTTP_COOKIE_VARS["current_password"]) ) {
			setcookie("current_uid", $HTTP_COOKIE_VARS["current_uid"], time()+$ADMIN_TIMEOUT);
			setcookie("current_password", $HTTP_COOKIE_VARS["current_password"], time()+$ADMIN_TIMEOUT);

			$current_user = $u;

			if($current_user->notified > 0) {
				$current_user->notified = 0;
				$users->write();
			}

			_log($u->username, "seen");

		} else {
			// Remove cookies
			setcookie("current_uid", "", time()-1);
			setcookie("current_password", "", time()-1);

			$current_uid = "";
			$current_password = "";
			$current_user = false;

		}
	}

}

?>