xref: /webtrees/app/User.php (revision 8d68cabe4cf02d6d8507faf4f53889852be0b6aa)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees;
17a25f0a04SGreg Roach
1860bc3e3fSGreg Roachuse stdclass;
1960bc3e3fSGreg Roach
20a25f0a04SGreg Roach/**
2176692c8bSGreg Roach * Provide an interface to the wt_user table.
22a25f0a04SGreg Roach */
23a25f0a04SGreg Roachclass User {
24a25f0a04SGreg Roach	/** @var  string The primary key of this user. */
25a25f0a04SGreg Roach	private $user_id;
26a25f0a04SGreg Roach
27a25f0a04SGreg Roach	/** @var  string The login name of this user. */
28a25f0a04SGreg Roach	private $user_name;
29a25f0a04SGreg Roach
30a25f0a04SGreg Roach	/** @var  string The real (display) name of this user. */
31a25f0a04SGreg Roach	private $real_name;
32a25f0a04SGreg Roach
33a25f0a04SGreg Roach	/** @var  string The email address of this user. */
34a25f0a04SGreg Roach	private $email;
35a25f0a04SGreg Roach
3615d603e7SGreg Roach	/** @var string[] Cached copy of the wt_user_setting table. */
3715d603e7SGreg Roach	private $preferences = [];
38a25f0a04SGreg Roach
39a25f0a04SGreg Roach	/** @var  User[] Only fetch users from the database once. */
4013abd6f3SGreg Roach	private static $cache = [];
41a25f0a04SGreg Roach
42a25f0a04SGreg Roach	/**
43f7fb7d41SGreg Roach	 * Create a new user object from a row in the database.
44f7fb7d41SGreg Roach	 *
45f7fb7d41SGreg Roach	 * @param stdclass $user A row from the wt_user table
46f7fb7d41SGreg Roach	 */
47f7fb7d41SGreg Roach	public function __construct(stdClass $user) {
48f7fb7d41SGreg Roach		$this->user_id   = $user->user_id;
49f7fb7d41SGreg Roach		$this->user_name = $user->user_name;
50f7fb7d41SGreg Roach		$this->real_name = $user->real_name;
51f7fb7d41SGreg Roach		$this->email     = $user->email;
52f7fb7d41SGreg Roach	}
53f7fb7d41SGreg Roach
54f7fb7d41SGreg Roach	/**
55f7fb7d41SGreg Roach	 * Create a new user.
56f7fb7d41SGreg Roach	 *
57f7fb7d41SGreg Roach	 * The calling code needs to check for duplicates identifiers before calling
58f7fb7d41SGreg Roach	 * this function.
59f7fb7d41SGreg Roach	 *
60f7fb7d41SGreg Roach	 * @param string $user_name
61f7fb7d41SGreg Roach	 * @param string $real_name
62f7fb7d41SGreg Roach	 * @param string $email
63f7fb7d41SGreg Roach	 * @param string $password
64f7fb7d41SGreg Roach	 *
65f7fb7d41SGreg Roach	 * @return User
66f7fb7d41SGreg Roach	 */
67f7fb7d41SGreg Roach	public static function create($user_name, $real_name, $email, $password) {
68f7fb7d41SGreg Roach		Database::prepare(
69f7fb7d41SGreg Roach			"INSERT INTO `##user` (user_name, real_name, email, password) VALUES (:user_name, :real_name, :email, :password)"
70f7fb7d41SGreg Roach		)->execute([
71f7fb7d41SGreg Roach			'user_name' => $user_name,
72f7fb7d41SGreg Roach			'real_name' => $real_name,
73f7fb7d41SGreg Roach			'email'     => $email,
74f7fb7d41SGreg Roach			'password'  => password_hash($password, PASSWORD_DEFAULT),
75f7fb7d41SGreg Roach		]);
76f7fb7d41SGreg Roach
77f7fb7d41SGreg Roach		// Set default blocks for this user
78f7fb7d41SGreg Roach		$user = self::findByIdentifier($user_name);
79f7fb7d41SGreg Roach		Database::prepare(
80f7fb7d41SGreg Roach			"INSERT INTO `##block` (`user_id`, `location`, `block_order`, `module_name`)" .
81f7fb7d41SGreg Roach			" SELECT :user_id , `location`, `block_order`, `module_name` FROM `##block` WHERE `user_id` = -1"
82f7fb7d41SGreg Roach		)->execute(['user_id' => $user->getUserId()]);
83f7fb7d41SGreg Roach
84f7fb7d41SGreg Roach		return $user;
85f7fb7d41SGreg Roach	}
86f7fb7d41SGreg Roach
87f7fb7d41SGreg Roach	/**
88f7fb7d41SGreg Roach	 * Delete a user
89f7fb7d41SGreg Roach	 */
90f7fb7d41SGreg Roach	public function delete() {
91f7fb7d41SGreg Roach		// Don't delete the logs.
92f7fb7d41SGreg Roach		Database::prepare("UPDATE `##log` SET user_id=NULL WHERE user_id =?")->execute([$this->user_id]);
93f7fb7d41SGreg Roach		// Take over the user’s pending changes. (What else could we do with them?)
94f7fb7d41SGreg Roach		Database::prepare("DELETE FROM `##change` WHERE user_id=? AND status='rejected'")->execute([$this->user_id]);
95f7fb7d41SGreg Roach		Database::prepare("UPDATE `##change` SET user_id=? WHERE user_id=?")->execute([Auth::id(), $this->user_id]);
96f7fb7d41SGreg Roach		Database::prepare("DELETE `##block_setting` FROM `##block_setting` JOIN `##block` USING (block_id) WHERE user_id=?")->execute([$this->user_id]);
97f7fb7d41SGreg Roach		Database::prepare("DELETE FROM `##block` WHERE user_id=?")->execute([$this->user_id]);
98f7fb7d41SGreg Roach		Database::prepare("DELETE FROM `##user_gedcom_setting` WHERE user_id=?")->execute([$this->user_id]);
99f7fb7d41SGreg Roach		Database::prepare("DELETE FROM `##gedcom_setting` WHERE setting_value=? AND setting_name IN ('CONTACT_USER_ID', 'WEBMASTER_USER_ID')")->execute([$this->user_id]);
100f7fb7d41SGreg Roach		Database::prepare("DELETE FROM `##user_setting` WHERE user_id=?")->execute([$this->user_id]);
101f7fb7d41SGreg Roach		Database::prepare("DELETE FROM `##message` WHERE user_id=?")->execute([$this->user_id]);
102f7fb7d41SGreg Roach		Database::prepare("DELETE FROM `##user` WHERE user_id=?")->execute([$this->user_id]);
103f7fb7d41SGreg Roach	}
104f7fb7d41SGreg Roach
105f7fb7d41SGreg Roach	/**
106a25f0a04SGreg Roach	 * Find the user with a specified user_id.
107a25f0a04SGreg Roach	 *
108cbc1590aSGreg Roach	 * @param int|null $user_id
109a25f0a04SGreg Roach	 *
110a25f0a04SGreg Roach	 * @return User|null
111a25f0a04SGreg Roach	 */
112a25f0a04SGreg Roach	public static function find($user_id) {
113a25f0a04SGreg Roach		if (!array_key_exists($user_id, self::$cache)) {
114a25f0a04SGreg Roach			$row = Database::prepare(
115a25f0a04SGreg Roach				"SELECT SQL_CACHE user_id, user_name, real_name, email FROM `##user` WHERE user_id = ?"
11613abd6f3SGreg Roach			)->execute([$user_id])->fetchOneRow();
117a25f0a04SGreg Roach			if ($row) {
11806ef8e02SGreg Roach				self::$cache[$user_id] = new self($row);
119a25f0a04SGreg Roach			} else {
120a25f0a04SGreg Roach				self::$cache[$user_id] = null;
121a25f0a04SGreg Roach			}
122a25f0a04SGreg Roach		}
123a25f0a04SGreg Roach
124a25f0a04SGreg Roach		return self::$cache[$user_id];
125a25f0a04SGreg Roach	}
126a25f0a04SGreg Roach
127a25f0a04SGreg Roach	/**
128a982a56eSGreg Roach	 * Find the user with a specified email address.
129a982a56eSGreg Roach	 *
130a982a56eSGreg Roach	 * @param string $email
131a982a56eSGreg Roach	 *
132a982a56eSGreg Roach	 * @return User|null
133a982a56eSGreg Roach	 */
134a982a56eSGreg Roach	public static function findByEmail($email) {
135a982a56eSGreg Roach		$user_id = Database::prepare(
136a982a56eSGreg Roach			"SELECT SQL_CACHE user_id FROM `##user` WHERE email = :email"
13713abd6f3SGreg Roach		)->execute([
138a982a56eSGreg Roach			'email' => $email,
13913abd6f3SGreg Roach		])->fetchOne();
140a982a56eSGreg Roach
141a982a56eSGreg Roach		return self::find($user_id);
142a982a56eSGreg Roach	}
143a982a56eSGreg Roach
144a982a56eSGreg Roach	/**
145a982a56eSGreg Roach	 * Find the user with a specified user_name or email address.
146a25f0a04SGreg Roach	 *
147a25f0a04SGreg Roach	 * @param string $identifier
148a25f0a04SGreg Roach	 *
149a25f0a04SGreg Roach	 * @return User|null
150a25f0a04SGreg Roach	 */
151a25f0a04SGreg Roach	public static function findByIdentifier($identifier) {
152a25f0a04SGreg Roach		$user_id = Database::prepare(
153a25f0a04SGreg Roach			"SELECT SQL_CACHE user_id FROM `##user` WHERE ? IN (user_name, email)"
15413abd6f3SGreg Roach		)->execute([$identifier])->fetchOne();
155a25f0a04SGreg Roach
156a25f0a04SGreg Roach		return self::find($user_id);
157a25f0a04SGreg Roach	}
158a25f0a04SGreg Roach
159a25f0a04SGreg Roach	/**
160a25f0a04SGreg Roach	 * Find the user with a specified genealogy record.
161a25f0a04SGreg Roach	 *
162a25f0a04SGreg Roach	 * @param Individual $individual
163a25f0a04SGreg Roach	 *
164a25f0a04SGreg Roach	 * @return User|null
165a25f0a04SGreg Roach	 */
166f7fb7d41SGreg Roach	public static function findByIndividual(Individual $individual) {
167a25f0a04SGreg Roach		$user_id = Database::prepare(
168a25f0a04SGreg Roach			"SELECT SQL_CACHE user_id" .
169a25f0a04SGreg Roach			" FROM `##user_gedcom_setting`" .
170a5adda01SGreg Roach			" WHERE gedcom_id = :tree_id AND setting_name = 'gedcomid' AND setting_value = :xref"
17113abd6f3SGreg Roach		)->execute([
172a5adda01SGreg Roach			'tree_id' => $individual->getTree()->getTreeId(),
173cbc1590aSGreg Roach			'xref'    => $individual->getXref(),
17413abd6f3SGreg Roach		])->fetchOne();
175a25f0a04SGreg Roach
176a25f0a04SGreg Roach		return self::find($user_id);
177a25f0a04SGreg Roach	}
178a25f0a04SGreg Roach
179a25f0a04SGreg Roach	/**
180f7fb7d41SGreg Roach	 * Find the user with a specified user_name.
181f7fb7d41SGreg Roach	 *
182f7fb7d41SGreg Roach	 * @param string $user_name
183f7fb7d41SGreg Roach	 *
184f7fb7d41SGreg Roach	 * @return User|null
185f7fb7d41SGreg Roach	 */
186f7fb7d41SGreg Roach	public static function findByUserName($user_name) {
187f7fb7d41SGreg Roach		$user_id = Database::prepare(
188f7fb7d41SGreg Roach			"SELECT SQL_CACHE user_id FROM `##user` WHERE user_name = :user_name"
189f7fb7d41SGreg Roach		)->execute([
190f7fb7d41SGreg Roach			'user_name' => $user_name,
191f7fb7d41SGreg Roach		])->fetchOne();
192f7fb7d41SGreg Roach
193f7fb7d41SGreg Roach		return self::find($user_id);
194f7fb7d41SGreg Roach	}
195f7fb7d41SGreg Roach
196f7fb7d41SGreg Roach	/**
197a25f0a04SGreg Roach	 * Find the latest user to register.
198a25f0a04SGreg Roach	 *
199a25f0a04SGreg Roach	 * @return User|null
200a25f0a04SGreg Roach	 */
201a25f0a04SGreg Roach	public static function findLatestToRegister() {
202a25f0a04SGreg Roach		$user_id = Database::prepare(
203a25f0a04SGreg Roach			"SELECT SQL_CACHE u.user_id" .
204a25f0a04SGreg Roach			" FROM `##user` u" .
205a25f0a04SGreg Roach			" LEFT JOIN `##user_setting` us ON (u.user_id=us.user_id AND us.setting_name='reg_timestamp') " .
206a25f0a04SGreg Roach			" ORDER BY us.setting_value DESC LIMIT 1"
207a25f0a04SGreg Roach		)->execute()->fetchOne();
208a25f0a04SGreg Roach
209a25f0a04SGreg Roach		return self::find($user_id);
210a25f0a04SGreg Roach	}
211a25f0a04SGreg Roach
212a25f0a04SGreg Roach	/**
213a25f0a04SGreg Roach	 * Get a list of all users.
214a25f0a04SGreg Roach	 *
215a25f0a04SGreg Roach	 * @return User[]
216a25f0a04SGreg Roach	 */
217a25f0a04SGreg Roach	public static function all() {
218a25f0a04SGreg Roach		$rows = Database::prepare(
219a25f0a04SGreg Roach			"SELECT SQL_CACHE user_id, user_name, real_name, email" .
220a25f0a04SGreg Roach			" FROM `##user`" .
221a25f0a04SGreg Roach			" WHERE user_id > 0" .
222f7fb7d41SGreg Roach			" ORDER BY real_name"
223a25f0a04SGreg Roach		)->fetchAll();
224a25f0a04SGreg Roach
225*8d68cabeSGreg Roach		return array_map(function($row) {
226*8d68cabeSGreg Roach			return new static($row);
227*8d68cabeSGreg Roach		}, $rows);
228a25f0a04SGreg Roach	}
229a25f0a04SGreg Roach
230a25f0a04SGreg Roach	/**
231a25f0a04SGreg Roach	 * Get a list of all administrators.
232a25f0a04SGreg Roach	 *
233a25f0a04SGreg Roach	 * @return User[]
234a25f0a04SGreg Roach	 */
235f7fb7d41SGreg Roach	public static function administrators() {
236a25f0a04SGreg Roach		$rows = Database::prepare(
237a25f0a04SGreg Roach			"SELECT SQL_CACHE user_id, user_name, real_name, email" .
238a25f0a04SGreg Roach			" FROM `##user`" .
239a25f0a04SGreg Roach			" JOIN `##user_setting` USING (user_id)" .
240f7fb7d41SGreg Roach			" WHERE user_id > 0 AND setting_name = 'canadmin' AND setting_value = '1'" .
241f7fb7d41SGreg Roach			" ORDER BY real_name"
242a25f0a04SGreg Roach		)->fetchAll();
243a25f0a04SGreg Roach
244*8d68cabeSGreg Roach		return array_map(function($row) {
245*8d68cabeSGreg Roach			return new static($row);
246*8d68cabeSGreg Roach		}, $rows);
247a25f0a04SGreg Roach	}
248a25f0a04SGreg Roach
249a25f0a04SGreg Roach	/** Validate a supplied password
250a25f0a04SGreg Roach	 * @param string $password
251a25f0a04SGreg Roach	 *
252cbc1590aSGreg Roach	 * @return bool
253a25f0a04SGreg Roach	 */
254a25f0a04SGreg Roach	public function checkPassword($password) {
255a25f0a04SGreg Roach		$password_hash = Database::prepare(
256a25f0a04SGreg Roach			"SELECT password FROM `##user` WHERE user_id = ?"
25713abd6f3SGreg Roach		)->execute([$this->user_id])->fetchOne();
258a25f0a04SGreg Roach
259015c99a2SGreg Roach		if (password_verify($password, $password_hash)) {
260015c99a2SGreg Roach			if (password_needs_rehash($password_hash, PASSWORD_DEFAULT)) {
261a25f0a04SGreg Roach				$this->setPassword($password);
262a25f0a04SGreg Roach			}
263cbc1590aSGreg Roach
264a25f0a04SGreg Roach			return true;
265a25f0a04SGreg Roach		} else {
266a25f0a04SGreg Roach			return false;
267a25f0a04SGreg Roach		}
268a25f0a04SGreg Roach	}
269a25f0a04SGreg Roach
270a25f0a04SGreg Roach	/**
271f7fb7d41SGreg Roach	 * Get a list of all managers.
272f7fb7d41SGreg Roach	 *
273f7fb7d41SGreg Roach	 * @return User[]
274f7fb7d41SGreg Roach	 */
275f7fb7d41SGreg Roach	public static function managers() {
276f7fb7d41SGreg Roach		$rows = Database::prepare(
277f7fb7d41SGreg Roach			"SELECT SQL_CACHE user_id, user_name, real_name, email" .
278f7fb7d41SGreg Roach			" FROM `##user` JOIN `##user_gedcom_setting` USING (user_id)" .
279f7fb7d41SGreg Roach			" WHERE setting_name = 'canedit' AND setting_value='admin'" .
280f7fb7d41SGreg Roach			" GROUP BY user_id, real_name" .
281f7fb7d41SGreg Roach			" ORDER BY real_name"
282f7fb7d41SGreg Roach		)->fetchAll();
283f7fb7d41SGreg Roach
284*8d68cabeSGreg Roach		return array_map(function($row) {
285*8d68cabeSGreg Roach			return new static($row);
286*8d68cabeSGreg Roach		}, $rows);
287f7fb7d41SGreg Roach	}
288f7fb7d41SGreg Roach
289f7fb7d41SGreg Roach	/**
290f7fb7d41SGreg Roach	 * Get a list of all moderators.
291f7fb7d41SGreg Roach	 *
292f7fb7d41SGreg Roach	 * @return User[]
293f7fb7d41SGreg Roach	 */
294f7fb7d41SGreg Roach	public static function moderators() {
295f7fb7d41SGreg Roach		$rows = Database::prepare(
296f7fb7d41SGreg Roach			"SELECT SQL_CACHE user_id, user_name, real_name, email" .
297f7fb7d41SGreg Roach			" FROM `##user` JOIN `##user_gedcom_setting` USING (user_id)" .
298f7fb7d41SGreg Roach			" WHERE setting_name = 'canedit' AND setting_value='accept'" .
299f7fb7d41SGreg Roach			" GROUP BY user_id, real_name" .
300f7fb7d41SGreg Roach			" ORDER BY real_name"
301f7fb7d41SGreg Roach		)->fetchAll();
302f7fb7d41SGreg Roach
303*8d68cabeSGreg Roach		return array_map(function($row) {
304*8d68cabeSGreg Roach			return new static($row);
305*8d68cabeSGreg Roach		}, $rows);
306f7fb7d41SGreg Roach	}
307f7fb7d41SGreg Roach
308f7fb7d41SGreg Roach	/**
309f7fb7d41SGreg Roach	 * Get a list of all verified users.
310f7fb7d41SGreg Roach	 *
311f7fb7d41SGreg Roach	 * @return User[]
312f7fb7d41SGreg Roach	 */
313f7fb7d41SGreg Roach	public static function unapproved() {
314f7fb7d41SGreg Roach		$rows = Database::prepare(
315f7fb7d41SGreg Roach			"SELECT SQL_CACHE user_id, user_name, real_name, email" .
316f7fb7d41SGreg Roach			" FROM `##user` JOIN `##user_setting` USING (user_id)" .
317f7fb7d41SGreg Roach			" WHERE setting_name = 'verified_by_admin' AND setting_value = '0'" .
318f7fb7d41SGreg Roach			" ORDER BY real_name"
319f7fb7d41SGreg Roach		)->fetchAll();
320f7fb7d41SGreg Roach
321*8d68cabeSGreg Roach		return array_map(function($row) {
322*8d68cabeSGreg Roach			return new static($row);
323*8d68cabeSGreg Roach		}, $rows);
324f7fb7d41SGreg Roach	}
325f7fb7d41SGreg Roach
326f7fb7d41SGreg Roach	/**
327f7fb7d41SGreg Roach	 * Get a list of all verified users.
328f7fb7d41SGreg Roach	 *
329f7fb7d41SGreg Roach	 * @return User[]
330f7fb7d41SGreg Roach	 */
331f7fb7d41SGreg Roach	public static function unverified() {
332f7fb7d41SGreg Roach		$rows = Database::prepare(
333f7fb7d41SGreg Roach			"SELECT SQL_CACHE user_id, user_name, real_name, email" .
334f7fb7d41SGreg Roach			" FROM `##user` JOIN `##user_setting` USING (user_id)" .
335f7fb7d41SGreg Roach			" WHERE setting_name = 'verified' AND setting_value = '0'" .
336f7fb7d41SGreg Roach			" ORDER BY real_name"
337f7fb7d41SGreg Roach		)->fetchAll();
338f7fb7d41SGreg Roach
339*8d68cabeSGreg Roach		return array_map(function($row) {
340*8d68cabeSGreg Roach			return new static($row);
341*8d68cabeSGreg Roach		}, $rows);
342f7fb7d41SGreg Roach	}
343f7fb7d41SGreg Roach
344f7fb7d41SGreg Roach	/**
345f7fb7d41SGreg Roach	 * Get a list of all users who are currently logged in.
346f7fb7d41SGreg Roach	 *
347f7fb7d41SGreg Roach	 * @return User[]
348f7fb7d41SGreg Roach	 */
349f7fb7d41SGreg Roach	public static function allLoggedIn() {
350f7fb7d41SGreg Roach		$rows = Database::prepare(
351f7fb7d41SGreg Roach			"SELECT SQL_NO_CACHE DISTINCT user_id, user_name, real_name, email" .
352f7fb7d41SGreg Roach			" FROM `##user`" .
353f7fb7d41SGreg Roach			" JOIN `##session` USING (user_id)"
354f7fb7d41SGreg Roach		)->fetchAll();
355f7fb7d41SGreg Roach
356*8d68cabeSGreg Roach		return array_map(function($row) {
357*8d68cabeSGreg Roach			return new static($row);
358*8d68cabeSGreg Roach		}, $rows);
359f7fb7d41SGreg Roach	}
360f7fb7d41SGreg Roach
361f7fb7d41SGreg Roach	/**
362a25f0a04SGreg Roach	 * Get the numeric ID for this user.
363a25f0a04SGreg Roach	 *
364a25f0a04SGreg Roach	 * @return string
365a25f0a04SGreg Roach	 */
366a25f0a04SGreg Roach	public function getUserId() {
367a25f0a04SGreg Roach		return $this->user_id;
368a25f0a04SGreg Roach	}
369a25f0a04SGreg Roach
370a25f0a04SGreg Roach	/**
371a25f0a04SGreg Roach	 * Get the login name for this user.
372a25f0a04SGreg Roach	 *
373a25f0a04SGreg Roach	 * @return string
374a25f0a04SGreg Roach	 */
375a25f0a04SGreg Roach	public function getUserName() {
376a25f0a04SGreg Roach		return $this->user_name;
377a25f0a04SGreg Roach	}
378a25f0a04SGreg Roach
379a25f0a04SGreg Roach	/**
380a25f0a04SGreg Roach	 * Set the login name for this user.
381a25f0a04SGreg Roach	 *
382a25f0a04SGreg Roach	 * @param string $user_name
383a25f0a04SGreg Roach	 *
384a25f0a04SGreg Roach	 * @return $this
385a25f0a04SGreg Roach	 */
386a25f0a04SGreg Roach	public function setUserName($user_name) {
387a25f0a04SGreg Roach		if ($this->user_name !== $user_name) {
388a25f0a04SGreg Roach			$this->user_name = $user_name;
389a25f0a04SGreg Roach			Database::prepare(
390a25f0a04SGreg Roach				"UPDATE `##user` SET user_name = ? WHERE user_id = ?"
39113abd6f3SGreg Roach			)->execute([$user_name, $this->user_id]);
392a25f0a04SGreg Roach		}
393a25f0a04SGreg Roach
394a25f0a04SGreg Roach		return $this;
395a25f0a04SGreg Roach	}
396a25f0a04SGreg Roach
397a25f0a04SGreg Roach	/**
398a25f0a04SGreg Roach	 * Get the real name of this user.
399a25f0a04SGreg Roach	 *
400a25f0a04SGreg Roach	 * @return string
401a25f0a04SGreg Roach	 */
402a25f0a04SGreg Roach	public function getRealName() {
403a25f0a04SGreg Roach		return $this->real_name;
404a25f0a04SGreg Roach	}
405a25f0a04SGreg Roach
406a25f0a04SGreg Roach	/**
407524c0fabSGreg Roach	 * Get the real name of this user, for display on screen.
408524c0fabSGreg Roach	 *
409524c0fabSGreg Roach	 * @return string
410524c0fabSGreg Roach	 */
411524c0fabSGreg Roach	public function getRealNameHtml() {
412cc5ab399SGreg Roach		return '<span dir="auto">' . Html::escape($this->real_name) . '</span>';
413524c0fabSGreg Roach	}
414524c0fabSGreg Roach
415524c0fabSGreg Roach	/**
416a25f0a04SGreg Roach	 * Set the real name of this user.
417a25f0a04SGreg Roach	 *
418a25f0a04SGreg Roach	 * @param string $real_name
419a25f0a04SGreg Roach	 *
420a25f0a04SGreg Roach	 * @return User
421a25f0a04SGreg Roach	 */
422a25f0a04SGreg Roach	public function setRealName($real_name) {
423a25f0a04SGreg Roach		if ($this->real_name !== $real_name) {
424a25f0a04SGreg Roach			$this->real_name = $real_name;
425a25f0a04SGreg Roach			Database::prepare(
426a25f0a04SGreg Roach				"UPDATE `##user` SET real_name = ? WHERE user_id = ?"
42713abd6f3SGreg Roach			)->execute([$real_name, $this->user_id]);
428a25f0a04SGreg Roach		}
429a25f0a04SGreg Roach
430a25f0a04SGreg Roach		return $this;
431a25f0a04SGreg Roach	}
432a25f0a04SGreg Roach
433a25f0a04SGreg Roach	/**
434a25f0a04SGreg Roach	 * Get the email address of this user.
435a25f0a04SGreg Roach	 *
436a25f0a04SGreg Roach	 * @return string
437a25f0a04SGreg Roach	 */
438a25f0a04SGreg Roach	public function getEmail() {
439a25f0a04SGreg Roach		return $this->email;
440a25f0a04SGreg Roach	}
441a25f0a04SGreg Roach
442a25f0a04SGreg Roach	/**
443a25f0a04SGreg Roach	 * Set the email address of this user.
444a25f0a04SGreg Roach	 *
445a25f0a04SGreg Roach	 * @param string $email
446a25f0a04SGreg Roach	 *
447a25f0a04SGreg Roach	 * @return User
448a25f0a04SGreg Roach	 */
449a25f0a04SGreg Roach	public function setEmail($email) {
450a25f0a04SGreg Roach		if ($this->email !== $email) {
451a25f0a04SGreg Roach			$this->email = $email;
452a25f0a04SGreg Roach			Database::prepare(
453a25f0a04SGreg Roach				"UPDATE `##user` SET email = ? WHERE user_id = ?"
45413abd6f3SGreg Roach			)->execute([$email, $this->user_id]);
455a25f0a04SGreg Roach		}
456a25f0a04SGreg Roach
457a25f0a04SGreg Roach		return $this;
458a25f0a04SGreg Roach	}
459a25f0a04SGreg Roach
460a25f0a04SGreg Roach	/**
461a25f0a04SGreg Roach	 * Set the password of this user.
462a25f0a04SGreg Roach	 *
463a25f0a04SGreg Roach	 * @param string $password
464a25f0a04SGreg Roach	 *
465a25f0a04SGreg Roach	 * @return User
466a25f0a04SGreg Roach	 */
467a25f0a04SGreg Roach	public function setPassword($password) {
468a25f0a04SGreg Roach		Database::prepare(
469015c99a2SGreg Roach			"UPDATE `##user` SET password = :password WHERE user_id = :user_id"
470015c99a2SGreg Roach		)->execute([
471015c99a2SGreg Roach			'password' => password_hash($password, PASSWORD_DEFAULT),
472015c99a2SGreg Roach			'user_id'  => $this->user_id,
473015c99a2SGreg Roach		]);
474a25f0a04SGreg Roach
475a25f0a04SGreg Roach		return $this;
476a25f0a04SGreg Roach	}
477a25f0a04SGreg Roach
478a25f0a04SGreg Roach	/**
479a25f0a04SGreg Roach	 * Fetch a user option/setting from the wt_user_setting table.
480a25f0a04SGreg Roach	 *
481a25f0a04SGreg Roach	 * Since we'll fetch several settings for each user, and since there aren’t
482a25f0a04SGreg Roach	 * that many of them, fetch them all in one database query
483a25f0a04SGreg Roach	 *
484a25f0a04SGreg Roach	 * @param string $setting_name
48515d603e7SGreg Roach	 * @param string $default
486a25f0a04SGreg Roach	 *
48715d603e7SGreg Roach	 * @return string
488a25f0a04SGreg Roach	 */
48915d603e7SGreg Roach	public function getPreference($setting_name, $default = '') {
49015d603e7SGreg Roach		if (empty($this->preferences) && $this->user_id !== null) {
491a25f0a04SGreg Roach			$this->preferences = Database::prepare(
49215d603e7SGreg Roach				"SELECT SQL_CACHE setting_name, setting_value" .
49315d603e7SGreg Roach				" FROM `##user_setting`" .
49415d603e7SGreg Roach				" WHERE user_id = :user_id"
49515d603e7SGreg Roach			)->execute([
49615d603e7SGreg Roach				'user_id' => $this->user_id,
49715d603e7SGreg Roach			])->fetchAssoc();
498a25f0a04SGreg Roach		}
499a25f0a04SGreg Roach
50015d603e7SGreg Roach		if (!array_key_exists($setting_name, $this->preferences)) {
50115d603e7SGreg Roach			$this->preferences[$setting_name] = $default;
502a25f0a04SGreg Roach		}
50315d603e7SGreg Roach
50415d603e7SGreg Roach		return $this->preferences[$setting_name];
505a25f0a04SGreg Roach	}
506a25f0a04SGreg Roach
507a25f0a04SGreg Roach	/**
508a25f0a04SGreg Roach	 * Update a setting for the user.
509a25f0a04SGreg Roach	 *
510a25f0a04SGreg Roach	 * @param string $setting_name
511a25f0a04SGreg Roach	 * @param string $setting_value
512a25f0a04SGreg Roach	 *
513a25f0a04SGreg Roach	 * @return User
514a25f0a04SGreg Roach	 */
515a25f0a04SGreg Roach	public function setPreference($setting_name, $setting_value) {
516a25f0a04SGreg Roach		if ($this->user_id && $this->getPreference($setting_name) !== $setting_value) {
517a25f0a04SGreg Roach			Database::prepare("REPLACE INTO `##user_setting` (user_id, setting_name, setting_value) VALUES (?, ?, LEFT(?, 255))")
51813abd6f3SGreg Roach				->execute([$this->user_id, $setting_name, $setting_value]);
51915d603e7SGreg Roach
520a25f0a04SGreg Roach			$this->preferences[$setting_name] = $setting_value;
521a25f0a04SGreg Roach		}
522a25f0a04SGreg Roach
523a25f0a04SGreg Roach		return $this;
524a25f0a04SGreg Roach	}
525a25f0a04SGreg Roach}
526