summaryrefslogtreecommitdiff
path: root/includes/sys_auth.php
blob: d831f36a1b6121e1ad24eaaad1e30b7caee18ad4 (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
<?php


// Testet ob ein User eingeloggt ist und lädt die entsprechenden Privilegien
function load_auth() {
	global $user, $privileges;

	if (!isset ($_SESSION['IP']))
		$_SESSION['IP'] = $_SERVER['REMOTE_ADDR'];

	if ($_SESSION['IP'] != $_SERVER['REMOTE_ADDR']) {
		session_destroy();
		header("Location: " . link_to_page('start'));
	}

	$user = null;
	if (isset ($_SESSION['uid'])) {
		$user = sql_select("SELECT * FROM `User` WHERE `UID`=" . sql_escape($_SESSION['uid']) . " LIMIT 1");
		if (count($user) > 0) {
			// User ist eingeloggt, Datensatz zur Verfügung stellen und Timestamp updaten
			list ($user) = $user;
			sql_query("UPDATE `User` SET " . "`lastLogIn` = '" . time() . "'" . " WHERE `UID` = '" . sql_escape($_SESSION['uid']) . "' LIMIT 1;");
		} else
			unset ($_SESSION['uid']);
	}

	$privileges = isset ($user) ? privileges_for_user($user['UID']) : privileges_for_group(-1);
}

function PassCrypt($passwort) {
	global $crypt_system;

	switch ($crypt_system) {
		case "crypt" :
			return "{crypt}" . crypt($passwort, "77");
		case "md5" :
			return md5($passwort);
	}
}

// JSON Authorisierungs-Schnittstelle
function json_auth_service() {
	global $CurrentExternAuthPass;

	header("Content-Type: application/json");

	$User = $_REQUEST['user'];
	$Pass = $_REQUEST['pw'];
	$SourceOuth = $_REQUEST['so'];

	if (isset ($CurrentExternAuthPass) && $SourceOuth == $CurrentExternAuthPass) {
		$sql = "SELECT * FROM `User` WHERE `Nick`='" . sql_escape($User) . "'";
		$Erg = sql_query($sql);

		if (mysql_num_rows($Erg) == 1) {
			if (mysql_result($Erg, 0, "Passwort") == PassCrypt($Pass)) {
				$UID = mysql_result($Erg, 0, "UID");

				$user_privs = sql_select("SELECT `Privileges`.`name` FROM `User` JOIN `UserGroups` ON (`User`.`UID` = `UserGroups`.`uid`) JOIN `GroupPrivileges` ON (`UserGroups`.`group_id` = `GroupPrivileges`.`group_id`) JOIN `Privileges` ON (`GroupPrivileges`.`privilege_id` = `Privileges`.`id`) WHERE `User`.`UID`=" . sql_escape($UID) . ";");
				foreach ($user_privs as $user_priv)
					$privileges[] = $user_priv['name'];

				$msg = array (
					'status' => 'success',
					'rights' => $privileges
				);
				echo json_encode($msg);
				die();
			}
		}
	}

	echo json_encode(array (
		'status' => 'failed',
		'error' => "JSON Service GET syntax: https://engelsystem.de/?auth&user=<user>&pw=<password>&so=<key>, POST is possible too"
	));
	die();
}

function privileges_for_user($user_id) {
	$privileges = array ();
	$user_privs = sql_select("SELECT `Privileges`.`name` FROM `User` JOIN `UserGroups` ON (`User`.`UID` = `UserGroups`.`uid`) JOIN `GroupPrivileges` ON (`UserGroups`.`group_id` = `GroupPrivileges`.`group_id`) JOIN `Privileges` ON (`GroupPrivileges`.`privilege_id` = `Privileges`.`id`) WHERE `User`.`UID`=" . sql_escape($user_id) . ";");
	foreach ($user_privs as $user_priv)
		$privileges[] = $user_priv['name'];
	return $privileges;
}

function privileges_for_group($group_id) {
	$privileges = array ();
	$groups_privs = sql_select("SELECT * FROM `GroupPrivileges` JOIN `Privileges` ON (`GroupPrivileges`.`privilege_id` = `Privileges`.`id`) WHERE `group_id`=" . sql_escape($group_id));
	foreach ($groups_privs as $guest_priv)
		$privileges[] = $guest_priv['name'];
	return $privileges;
}
?>