aboutsummaryrefslogtreecommitdiff
path: root/app/Controllers/usersController.php
blob: a9e6c32bc2d6f87d8183e0c9897ad04deab562b1 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php

class FreshRSS_users_Controller extends Minz_ActionController {

	const BCRYPT_COST = 9;	//Will also have to be computed client side on mobile devices, so do not use a too high cost

	public function firstAction() {
		if (!$this->view->loginOk) {
			Minz_Error::error(
				403,
				array('error' => array(Minz_Translate::t('access_denied')))
			);
		}
	}

	public function authAction() {
		if (Minz_Request::isPost()) {
			$ok = true;

			$passwordPlain = Minz_Request::param('passwordPlain', '', true);
			if ($passwordPlain != '') {
				Minz_Request::_param('passwordPlain');	//Discard plain-text password ASAP
				$_POST['passwordPlain'] = '';
				if (!function_exists('password_hash')) {
					include_once(LIB_PATH . '/password_compat.php');
				}
				$passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
				$passwordPlain = '';
				$passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash);	//Compatibility with bcrypt.js
				$ok &= ($passwordHash != '');
				$this->view->conf->_passwordHash($passwordHash);
			}
			Minz_Session::_param('passwordHash', $this->view->conf->passwordHash);

			$passwordPlain = Minz_Request::param('apiPasswordPlain', '', true);
			if ($passwordPlain != '') {
				if (!function_exists('password_hash')) {
					include_once(LIB_PATH . '/password_compat.php');
				}
				$passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
				$passwordPlain = '';
				$passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash);	//Compatibility with bcrypt.js
				$ok &= ($passwordHash != '');
				$this->view->conf->_apiPasswordHash($passwordHash);
			}

			if (Minz_Configuration::isAdmin(Minz_Session::param('currentUser', '_'))) {
				$this->view->conf->_mail_login(Minz_Request::param('mail_login', '', true));
			}
			$email = $this->view->conf->mail_login;
			Minz_Session::_param('mail', $email);

			$ok &= $this->view->conf->save();

			if ($email != '') {
				$personaFile = DATA_PATH . '/persona/' . $email . '.txt';
				@unlink($personaFile);
				$ok &= (file_put_contents($personaFile, Minz_Session::param('currentUser', '_')) !== false);
			}

			if (Minz_Configuration::isAdmin(Minz_Session::param('currentUser', '_'))) {
				$current_token = $this->view->conf->token;
				$token = Minz_Request::param('token', $current_token);
				$this->view->conf->_token($token);
				$ok &= $this->view->conf->save();

				$anon = Minz_Request::param('anon_access', false);
				$anon = ((bool)$anon) && ($anon !== 'no');
				$anon_refresh = Minz_Request::param('anon_refresh', false);
				$anon_refresh = ((bool)$anon_refresh) && ($anon_refresh !== 'no');
				$auth_type = Minz_Request::param('auth_type', 'none');
				$unsafe_autologin = Minz_Request::param('unsafe_autologin', false);
				$api_enabled = Minz_Request::param('api_enabled', false);
				if ($anon != Minz_Configuration::allowAnonymous() ||
					$auth_type != Minz_Configuration::authType() ||
					$anon_refresh != Minz_Configuration::allowAnonymousRefresh() ||
					$unsafe_autologin != Minz_Configuration::unsafeAutologinEnabled() ||
					$api_enabled != Minz_Configuration::apiEnabled()) {

					Minz_Configuration::_authType($auth_type);
					Minz_Configuration::_allowAnonymous($anon);
					Minz_Configuration::_allowAnonymousRefresh($anon_refresh);
					Minz_Configuration::_enableAutologin($unsafe_autologin);
					Minz_Configuration::_enableApi($api_enabled);
					$ok &= Minz_Configuration::writeFile();
				}
			}

			invalidateHttpCache();

			$notif = array(
				'type' => $ok ? 'good' : 'bad',
				'content' => Minz_Translate::t($ok ? 'configuration_updated' : 'error_occurred')
			);
			Minz_Session::_param('notification', $notif);
		}
		Minz_Request::forward(array('c' => 'configure', 'a' => 'users'), true);
	}

	public function createAction() {
		if (Minz_Request::isPost() && Minz_Configuration::isAdmin(Minz_Session::param('currentUser', '_'))) {
			$db = Minz_Configuration::dataBase();
			require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');

			$new_user_language = Minz_Request::param('new_user_language', $this->view->conf->language);
			if (!in_array($new_user_language, $this->view->conf->availableLanguages())) {
				$new_user_language = $this->view->conf->language;
			}

			$new_user_name = Minz_Request::param('new_user_name');
			$ok = ($new_user_name != '') && ctype_alnum($new_user_name);

			if ($ok) {
				$ok &= (strcasecmp($new_user_name, Minz_Configuration::defaultUser()) !== 0);	//It is forbidden to alter the default user

				$ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers()));	//Not an existing user, case-insensitive

				$configPath = DATA_PATH . '/' . $new_user_name . '_user.php';
				$ok &= !file_exists($configPath);
			}
			if ($ok) {
			
				$passwordPlain = Minz_Request::param('new_user_passwordPlain', '', true);
				$passwordHash = '';
				if ($passwordPlain != '') {
					Minz_Request::_param('new_user_passwordPlain');	//Discard plain-text password ASAP
					$_POST['new_user_passwordPlain'] = '';
					if (!function_exists('password_hash')) {
						include_once(LIB_PATH . '/password_compat.php');
					}
					$passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
					$passwordPlain = '';
					$passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash);	//Compatibility with bcrypt.js
					$ok &= ($passwordHash != '');
				}
				if (empty($passwordHash)) {
					$passwordHash = '';
				}

				$new_user_email = filter_var($_POST['new_user_email'], FILTER_VALIDATE_EMAIL);
				if (empty($new_user_email)) {
					$new_user_email = '';
				} else {
					$personaFile = DATA_PATH . '/persona/' . $new_user_email . '.txt';
					@unlink($personaFile);
					$ok &= (file_put_contents($personaFile, $new_user_name) !== false);
				}
			}
			if ($ok) {
				$config_array = array(
					'language' => $new_user_language,
					'passwordHash' => $passwordHash,
					'mail_login' => $new_user_email,
				);
				$ok &= (file_put_contents($configPath, "<?php\n return " . var_export($config_array, true) . ';') !== false);
			}
			if ($ok) {
				$userDAO = new FreshRSS_UserDAO();
				$ok &= $userDAO->createUser($new_user_name);
			}
			invalidateHttpCache();

			$notif = array(
				'type' => $ok ? 'good' : 'bad',
				'content' => Minz_Translate::t($ok ? 'user_created' : 'error_occurred', $new_user_name)
			);
			Minz_Session::_param('notification', $notif);
		}
		Minz_Request::forward(array('c' => 'configure', 'a' => 'users'), true);
	}

	public function deleteAction() {
		if (Minz_Request::isPost() && Minz_Configuration::isAdmin(Minz_Session::param('currentUser', '_'))) {
			$db = Minz_Configuration::dataBase();
			require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');

			$username = Minz_Request::param('username');
			$ok = ctype_alnum($username);

			if ($ok) {
				$ok &= (strcasecmp($username, Minz_Configuration::defaultUser()) !== 0);	//It is forbidden to delete the default user
			}
			if ($ok) {
				$configPath = DATA_PATH . '/' . $username . '_user.php';
				$ok &= file_exists($configPath);
			}
			if ($ok) {
				$userDAO = new FreshRSS_UserDAO();
				$ok &= $userDAO->deleteUser($username);
				$ok &= unlink($configPath);
				//TODO: delete Persona file
			}
			invalidateHttpCache();

			$notif = array(
				'type' => $ok ? 'good' : 'bad',
				'content' => Minz_Translate::t($ok ? 'user_deleted' : 'error_occurred', $username)
			);
			Minz_Session::_param('notification', $notif);
		}
		Minz_Request::forward(array('c' => 'configure', 'a' => 'users'), true);
	}
}