Session.php 2.64 KB
Newer Older
Tobias Reich's avatar
Tobias Reich committed
1
2
3
4
5
6
7
8
9
10
<?php

###
# @name		Session Module
# @author		Tobias Reich
# @copyright	2014 by Tobias Reich
###

if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');

Tobias Reich's avatar
Tobias Reich committed
11
class Session extends Module {
Tobias Reich's avatar
Tobias Reich committed
12

Tobias Reich's avatar
Tobias Reich committed
13
	private $settings = null;
Tobias Reich's avatar
Tobias Reich committed
14
15
16
17
18
19
20
21
22
23
24

	public function __construct($plugins, $settings) {

		# Init vars
		$this->plugins	= $plugins;
		$this->settings	= $settings;

		return true;

	}

Tobias Reich's avatar
Tobias Reich committed
25
	public function init($database, $dbName, $public, $version) {
Tobias Reich's avatar
Tobias Reich committed
26

27
		# Check dependencies
Tobias Reich's avatar
Tobias Reich committed
28
		self::dependencies(isset($this->settings, $public, $version));
Tobias Reich's avatar
Tobias Reich committed
29

Tobias Reich's avatar
Tobias Reich committed
30
31
32
		# Call plugins
		$this->plugins(__METHOD__, 0, func_get_args());

Tobias Reich's avatar
Tobias Reich committed
33
		# Update
Tobias Reich's avatar
Tobias Reich committed
34
35
36
37
38
39
		if (!isset($this->settings['version'])||$this->settings['version']!==$version) {
			if (!Database::update($database, $dbName, @$this->settings['version'])) {
				Log::error($database, __METHOD__, __LINE__, 'Updating the database failed');
				exit('Error: Updating the database failed!');
			}
		}
Tobias Reich's avatar
Tobias Reich committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

		# Return settings
		$return['config'] = $this->settings;
		unset($return['config']['password']);

		# No login
		if ($this->settings['username']===''&&$this->settings['password']==='') $return['config']['login'] = false;
		else $return['config']['login'] = true;

		if ($public===false) {

			# Logged in
			$return['loggedIn'] = true;

		} else {

			# Unset unused vars
			unset($return['config']['username']);
			unset($return['config']['thumbQuality']);
			unset($return['config']['sorting']);
			unset($return['config']['dropboxKey']);
			unset($return['config']['login']);
62
			unset($return['config']['plugins']);
Tobias Reich's avatar
Tobias Reich committed
63
64
65
66
67
68

			# Logged out
			$return['loggedIn'] = false;

		}

Tobias Reich's avatar
Tobias Reich committed
69
70
71
		# Call plugins
		$this->plugins(__METHOD__, 1, func_get_args());

Tobias Reich's avatar
Tobias Reich committed
72
73
74
75
76
77
		return $return;

	}

	public function login($username, $password) {

78
		# Check dependencies
Tobias Reich's avatar
Tobias Reich committed
79
		self::dependencies(isset($this->settings, $username, $password));
Tobias Reich's avatar
Tobias Reich committed
80

Tobias Reich's avatar
Tobias Reich committed
81
82
83
		# Call plugins
		$this->plugins(__METHOD__, 0, func_get_args());

84
		# Check login with MD5 hash
Tobias Reich's avatar
Tobias Reich committed
85
86
87
88
89
		if ($username===$this->settings['username']&&$password===$this->settings['password']) {
			$_SESSION['login'] = true;
			return true;
		}

90
91
92
93
94
95
		# Check login with crypted hash
		if ($username===$this->settings['username']&&$this->settings['password']===crypt($password, $this->settings['password'])) {
			$_SESSION['login'] = true;
			return true;
		}

Tobias Reich's avatar
Tobias Reich committed
96
97
98
99
100
101
		# No login
		if ($this->settings['username']===''&&$this->settings['password']==='') {
			$_SESSION['login'] = true;
			return true;
		}

Tobias Reich's avatar
Tobias Reich committed
102
103
104
		# Call plugins
		$this->plugins(__METHOD__, 1, func_get_args());

Tobias Reich's avatar
Tobias Reich committed
105
106
107
108
109
110
		return false;

	}

	public function logout() {

Tobias Reich's avatar
Tobias Reich committed
111
112
113
		# Call plugins
		$this->plugins(__METHOD__, 0, func_get_args());

Tobias Reich's avatar
Tobias Reich committed
114
		session_destroy();
Tobias Reich's avatar
Tobias Reich committed
115
116
117
118

		# Call plugins
		$this->plugins(__METHOD__, 1, func_get_args());

Tobias Reich's avatar
Tobias Reich committed
119
120
121
122
123
124
125
		return true;

	}

}

?>