summaryrefslogtreecommitdiff
path: root/inc/mod/auth.php
blob: f9b863c826df0b7e0bff684b33461101f1d690a7 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php

/*
 *  Copyright (c) 2010-2012 Tinyboard Development Group
 */

if (realpath($_SERVER['SCRIPT_FILENAME']) == str_replace('\\', '/', __FILE__)) {
	// You cannot request this file directly.
	exit;
}

// create a hash/salt pair for validate logins
function mkhash($username, $password, $salt = false) {
	global $config;
	
	if (!$salt) {
		// create some sort of salt for the hash
		$salt = substr(base64_encode(sha1(rand() . time(), true) . $config['cookies']['salt']), 0, 15);
		
		$generated_salt = true;
	}
	
	// generate hash (method is not important as long as it's strong)
	$hash = substr(base64_encode(md5($username . sha1($username . $password . $salt . ($config['mod']['lock_ip'] ? $_SERVER['REMOTE_ADDR'] : ''), true), true)), 0, 20);
	
	if (isset($generated_salt))
		return Array($hash, $salt);
	else
		return $hash;
}

function login($username, $password, $makehash=true) {
	global $mod;
	
	// SHA1 password
	if ($makehash) {
		$password = sha1($password);
	}
	
	$query = prepare("SELECT `id`,`type`,`boards` FROM `mods` WHERE `username` = :username AND `password` = :password LIMIT 1");
	$query->bindValue(':username', $username);
	$query->bindValue(':password', $password);
	$query->execute() or error(db_error($query));
	
	if ($user = $query->fetch()) {
		return $mod = Array(
			'id' => $user['id'],
			'type' => $user['type'],
			'username' => $username,
			'hash' => mkhash($username, $password),
			'boards' => explode(',', $user['boards'])
			);
	} else return false;
}

function setCookies() {
	global $mod, $config;
	if (!$mod)
		error('setCookies() was called for a non-moderator!');
	
	setcookie($config['cookies']['mod'],
			$mod['username'] . // username
			':' . 
			$mod['hash'][0] . // password
			':' .
			$mod['hash'][1], // salt
		time() + $config['cookies']['expire'], $config['cookies']['jail'] ? $config['cookies']['path'] : '/', null, false, true);
}

function destroyCookies() {
	global $config;
	// Delete the cookies
	setcookie($config['cookies']['mod'], 'deleted', time() - $config['cookies']['expire'], $config['cookies']['jail']?$config['cookies']['path'] : '/', null, false, true);
}

function modLog($action, $_board=null) {
	global $mod, $board, $config;
	$query = prepare("INSERT INTO `modlogs` VALUES (:id, :ip, :board, :time, :text)");
	$query->bindValue(':id', $mod['id'], PDO::PARAM_INT);
	$query->bindValue(':ip', $_SERVER['REMOTE_ADDR']);
	$query->bindValue(':time', time(), PDO::PARAM_INT);
	$query->bindValue(':text', $action);
	if (isset($_board))
		$query->bindValue(':board', $_board);
	elseif (isset($board))
		$query->bindValue(':board', $board['uri']);
	else
		$query->bindValue(':board', null, PDO::PARAM_NULL);
	$query->execute() or error(db_error($query));
	
	if ($config['syslog'])
		_syslog(LOG_INFO, '[mod/' . $mod['username'] . ']: ' . $action);
}

// Validate session

if (isset($_COOKIE[$config['cookies']['mod']])) {
	// Should be username:hash:salt
	$cookie = explode(':', $_COOKIE[$config['cookies']['mod']]);
	if (count($cookie) != 3) {
		destroyCookies();
		error($config['error']['malformed']);
	}
	
	$query = prepare("SELECT `id`, `type`, `boards`, `password` FROM `mods` WHERE `username` = :username LIMIT 1");
	$query->bindValue(':username', $cookie[0]);
	$query->execute() or error(db_error($query));
	$user = $query->fetch();
	
	// validate password hash
	if ($cookie[1] != mkhash($cookie[0], $user['password'], $cookie[2])) {
		// Malformed cookies
		destroyCookies();
		error($config['error']['malformed']);
	}
	
	$mod = Array(
		'id' => $user['id'],
		'type' => $user['type'],
		'username' => $cookie[0],
		'boards' => explode(',', $user['boards'])
	);
}

function create_pm_header() {
	global $mod;
	$query = prepare("SELECT `id` FROM `pms` WHERE `to` = :id AND `unread` = 1");
	$query->bindValue(':id', $mod['id'], PDO::PARAM_INT);
	$query->execute() or error(db_error($query));
	
	if ($pm = $query->fetch()) {
		return Array('id' => $pm['id'], 'waiting' => $query->rowCount() - 1);
	}
	
	return false;
}