commit | author | age
|
fba1f5
|
1 |
<?php |
T |
2 |
|
|
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
|
5 |
| program/include/rcube_user.inc | |
|
6 |
| | |
e019f2
|
7 |
| This file is part of the Roundcube Webmail client | |
f5e7b3
|
8 |
| Copyright (C) 2005-2010, The Roundcube Dev Team | |
fba1f5
|
9 |
| Licensed under the GNU GPL | |
T |
10 |
| | |
|
11 |
| PURPOSE: | |
|
12 |
| This class represents a system user linked and provides access | |
|
13 |
| to the related database records. | |
|
14 |
| | |
|
15 |
+-----------------------------------------------------------------------+ |
|
16 |
| Author: Thomas Bruederli <roundcube@gmail.com> | |
|
17 |
+-----------------------------------------------------------------------+ |
|
18 |
|
638fb8
|
19 |
$Id$ |
fba1f5
|
20 |
|
T |
21 |
*/ |
|
22 |
|
|
23 |
|
|
24 |
/** |
|
25 |
* Class representing a system user |
|
26 |
* |
45f56c
|
27 |
* @package Core |
fba1f5
|
28 |
* @author Thomas Bruederli <roundcube@gmail.com> |
T |
29 |
*/ |
|
30 |
class rcube_user |
|
31 |
{ |
a78901
|
32 |
public $ID = null; |
A |
33 |
public $data = null; |
|
34 |
public $language = null; |
|
35 |
|
5c461b
|
36 |
/** |
A |
37 |
* Holds database connection. |
|
38 |
* |
|
39 |
* @var rcube_mdb2 |
|
40 |
*/ |
a78901
|
41 |
private $db = null; |
fba1f5
|
42 |
|
95987c
|
43 |
|
a78901
|
44 |
/** |
A |
45 |
* Object constructor |
|
46 |
* |
5c461b
|
47 |
* @param int $id User id |
A |
48 |
* @param array $sql_arr SQL result set |
a78901
|
49 |
*/ |
A |
50 |
function __construct($id = null, $sql_arr = null) |
|
51 |
{ |
|
52 |
$this->db = rcmail::get_instance()->get_dbh(); |
e99991
|
53 |
|
a78901
|
54 |
if ($id && !$sql_arr) { |
A |
55 |
$sql_result = $this->db->query( |
|
56 |
"SELECT * FROM ".get_table_name('users')." WHERE user_id = ?", $id); |
|
57 |
$sql_arr = $this->db->fetch_assoc($sql_result); |
|
58 |
} |
|
59 |
|
|
60 |
if (!empty($sql_arr)) { |
|
61 |
$this->ID = $sql_arr['user_id']; |
|
62 |
$this->data = $sql_arr; |
|
63 |
$this->language = $sql_arr['language']; |
|
64 |
} |
|
65 |
} |
|
66 |
|
|
67 |
|
|
68 |
/** |
|
69 |
* Build a user name string (as e-mail address) |
|
70 |
* |
5c461b
|
71 |
* @param string $part Username part (empty or 'local' or 'domain') |
8dfe51
|
72 |
* @return string Full user name or its part |
a78901
|
73 |
*/ |
8dfe51
|
74 |
function get_username($part = null) |
a78901
|
75 |
{ |
A |
76 |
if ($this->data['username']) { |
8dfe51
|
77 |
list($local, $domain) = explode('@', $this->data['username']); |
A |
78 |
|
|
79 |
// at least we should always have the local part |
|
80 |
if ($part == 'local') { |
|
81 |
return $local; |
|
82 |
} |
b0eeaa
|
83 |
// if no domain was provided... |
A |
84 |
if (empty($domain)) { |
|
85 |
$rcmail = rcmail::get_instance(); |
|
86 |
$domain = $rcmail->config->mail_domain($this->data['mail_host']); |
|
87 |
} |
8dfe51
|
88 |
|
A |
89 |
if ($part == 'domain') { |
|
90 |
return $domain; |
|
91 |
} |
|
92 |
|
|
93 |
if (!empty($domain)) |
|
94 |
return $local . '@' . $domain; |
a78901
|
95 |
else |
8dfe51
|
96 |
return $local; |
a78901
|
97 |
} |
A |
98 |
|
|
99 |
return false; |
|
100 |
} |
|
101 |
|
|
102 |
|
|
103 |
/** |
|
104 |
* Get the preferences saved for this user |
|
105 |
* |
|
106 |
* @return array Hash array with prefs |
|
107 |
*/ |
|
108 |
function get_prefs() |
|
109 |
{ |
|
110 |
if (!empty($this->language)) |
|
111 |
$prefs = array('language' => $this->language); |
8dfe51
|
112 |
|
a78901
|
113 |
if ($this->ID && $this->data['preferences']) |
A |
114 |
$prefs += (array)unserialize($this->data['preferences']); |
8dfe51
|
115 |
|
a78901
|
116 |
return $prefs; |
A |
117 |
} |
8dfe51
|
118 |
|
A |
119 |
|
a78901
|
120 |
/** |
A |
121 |
* Write the given user prefs to the user's record |
|
122 |
* |
5c461b
|
123 |
* @param array $a_user_prefs User prefs to save |
a78901
|
124 |
* @return boolean True on success, False on failure |
A |
125 |
*/ |
|
126 |
function save_prefs($a_user_prefs) |
|
127 |
{ |
|
128 |
if (!$this->ID) |
|
129 |
return false; |
e99991
|
130 |
|
a78901
|
131 |
$config = rcmail::get_instance()->config; |
A |
132 |
$old_prefs = (array)$this->get_prefs(); |
fba1f5
|
133 |
|
a78901
|
134 |
// merge (partial) prefs array with existing settings |
A |
135 |
$save_prefs = $a_user_prefs + $old_prefs; |
|
136 |
unset($save_prefs['language']); |
e99991
|
137 |
|
a78901
|
138 |
// don't save prefs with default values if they haven't been changed yet |
A |
139 |
foreach ($a_user_prefs as $key => $value) { |
|
140 |
if (!isset($old_prefs[$key]) && ($value == $config->get($key))) |
|
141 |
unset($save_prefs[$key]); |
|
142 |
} |
|
143 |
|
|
144 |
$save_prefs = serialize($save_prefs); |
|
145 |
|
|
146 |
$this->db->query( |
|
147 |
"UPDATE ".get_table_name('users'). |
|
148 |
" SET preferences = ?". |
|
149 |
", language = ?". |
|
150 |
" WHERE user_id = ?", |
|
151 |
$save_prefs, |
|
152 |
$_SESSION['language'], |
|
153 |
$this->ID); |
|
154 |
|
|
155 |
$this->language = $_SESSION['language']; |
|
156 |
|
80809d
|
157 |
if ($this->db->affected_rows() !== false) { |
a78901
|
158 |
$config->set_user_prefs($a_user_prefs); |
A |
159 |
$this->data['preferences'] = $save_prefs; |
|
160 |
return true; |
|
161 |
} |
|
162 |
|
|
163 |
return false; |
286420
|
164 |
} |
f52c93
|
165 |
|
T |
166 |
|
a78901
|
167 |
/** |
A |
168 |
* Get default identity of this user |
|
169 |
* |
5c461b
|
170 |
* @param int $id Identity ID. If empty, the default identity is returned |
a78901
|
171 |
* @return array Hash array with all cols of the identity record |
A |
172 |
*/ |
|
173 |
function get_identity($id = null) |
fba1f5
|
174 |
{ |
a78901
|
175 |
$result = $this->list_identities($id ? sprintf('AND identity_id = %d', $id) : ''); |
A |
176 |
return $result[0]; |
fba1f5
|
177 |
} |
55f54e
|
178 |
|
A |
179 |
|
a78901
|
180 |
/** |
A |
181 |
* Return a list of all identities linked with this user |
|
182 |
* |
5c461b
|
183 |
* @param string $sql_add Optional WHERE clauses |
a78901
|
184 |
* @return array List of identities |
A |
185 |
*/ |
|
186 |
function list_identities($sql_add = '') |
fba1f5
|
187 |
{ |
a78901
|
188 |
$result = array(); |
fba1f5
|
189 |
|
a78901
|
190 |
$sql_result = $this->db->query( |
A |
191 |
"SELECT * FROM ".get_table_name('identities'). |
|
192 |
" WHERE del <> 1 AND user_id = ?". |
|
193 |
($sql_add ? " ".$sql_add : ""). |
|
194 |
" ORDER BY ".$this->db->quoteIdentifier('standard')." DESC, name ASC, identity_id ASC", |
|
195 |
$this->ID); |
e99991
|
196 |
|
a78901
|
197 |
while ($sql_arr = $this->db->fetch_assoc($sql_result)) { |
A |
198 |
$result[] = $sql_arr; |
|
199 |
} |
e99991
|
200 |
|
a78901
|
201 |
return $result; |
A |
202 |
} |
fba1f5
|
203 |
|
a78901
|
204 |
|
A |
205 |
/** |
|
206 |
* Update a specific identity record |
|
207 |
* |
5c461b
|
208 |
* @param int $iid Identity ID |
A |
209 |
* @param array $data Hash array with col->value pairs to save |
a78901
|
210 |
* @return boolean True if saved successfully, false if nothing changed |
A |
211 |
*/ |
|
212 |
function update_identity($iid, $data) |
fba1f5
|
213 |
{ |
a78901
|
214 |
if (!$this->ID) |
A |
215 |
return false; |
|
216 |
|
|
217 |
$query_cols = $query_params = array(); |
e99991
|
218 |
|
a78901
|
219 |
foreach ((array)$data as $col => $value) { |
A |
220 |
$query_cols[] = $this->db->quoteIdentifier($col) . ' = ?'; |
|
221 |
$query_params[] = $value; |
|
222 |
} |
|
223 |
$query_params[] = $iid; |
|
224 |
$query_params[] = $this->ID; |
|
225 |
|
|
226 |
$sql = "UPDATE ".get_table_name('identities'). |
|
227 |
" SET changed = ".$this->db->now().", ".join(', ', $query_cols). |
|
228 |
" WHERE identity_id = ?". |
|
229 |
" AND user_id = ?". |
|
230 |
" AND del <> 1"; |
|
231 |
|
|
232 |
call_user_func_array(array($this->db, 'query'), |
|
233 |
array_merge(array($sql), $query_params)); |
e99991
|
234 |
|
a78901
|
235 |
return $this->db->affected_rows(); |
fba1f5
|
236 |
} |
e99991
|
237 |
|
A |
238 |
|
a78901
|
239 |
/** |
A |
240 |
* Create a new identity record linked with this user |
|
241 |
* |
5c461b
|
242 |
* @param array $data Hash array with col->value pairs to save |
a78901
|
243 |
* @return int The inserted identity ID or false on error |
A |
244 |
*/ |
|
245 |
function insert_identity($data) |
fba1f5
|
246 |
{ |
a78901
|
247 |
if (!$this->ID) |
A |
248 |
return false; |
|
249 |
|
|
250 |
unset($data['user_id']); |
|
251 |
|
|
252 |
$insert_cols = $insert_values = array(); |
|
253 |
foreach ((array)$data as $col => $value) { |
|
254 |
$insert_cols[] = $this->db->quoteIdentifier($col); |
|
255 |
$insert_values[] = $value; |
|
256 |
} |
|
257 |
$insert_cols[] = 'user_id'; |
|
258 |
$insert_values[] = $this->ID; |
|
259 |
|
|
260 |
$sql = "INSERT INTO ".get_table_name('identities'). |
|
261 |
" (changed, ".join(', ', $insert_cols).")". |
|
262 |
" VALUES (".$this->db->now().", ".join(', ', array_pad(array(), sizeof($insert_values), '?')).")"; |
|
263 |
|
|
264 |
call_user_func_array(array($this->db, 'query'), |
|
265 |
array_merge(array($sql), $insert_values)); |
|
266 |
|
|
267 |
return $this->db->insert_id('identities'); |
fba1f5
|
268 |
} |
e99991
|
269 |
|
A |
270 |
|
a78901
|
271 |
/** |
A |
272 |
* Mark the given identity as deleted |
|
273 |
* |
5c461b
|
274 |
* @param int $iid Identity ID |
a78901
|
275 |
* @return boolean True if deleted successfully, false if nothing changed |
A |
276 |
*/ |
|
277 |
function delete_identity($iid) |
fba1f5
|
278 |
{ |
a78901
|
279 |
if (!$this->ID) |
A |
280 |
return false; |
07722a
|
281 |
|
a78901
|
282 |
$sql_result = $this->db->query( |
A |
283 |
"SELECT count(*) AS ident_count FROM ".get_table_name('identities'). |
|
284 |
" WHERE user_id = ? AND del <> 1", |
|
285 |
$this->ID); |
fba1f5
|
286 |
|
a78901
|
287 |
$sql_arr = $this->db->fetch_assoc($sql_result); |
fba1f5
|
288 |
|
a78901
|
289 |
// we'll not delete last identity |
A |
290 |
if ($sql_arr['ident_count'] <= 1) |
bbb142
|
291 |
return -1; |
e99991
|
292 |
|
a78901
|
293 |
$this->db->query( |
A |
294 |
"UPDATE ".get_table_name('identities'). |
|
295 |
" SET del = 1, changed = ".$this->db->now(). |
|
296 |
" WHERE user_id = ?". |
|
297 |
" AND identity_id = ?", |
|
298 |
$this->ID, |
|
299 |
$iid); |
fba1f5
|
300 |
|
a78901
|
301 |
return $this->db->affected_rows(); |
A |
302 |
} |
e99991
|
303 |
|
A |
304 |
|
a78901
|
305 |
/** |
A |
306 |
* Make this identity the default one for this user |
|
307 |
* |
5c461b
|
308 |
* @param int $iid The identity ID |
a78901
|
309 |
*/ |
A |
310 |
function set_default($iid) |
|
311 |
{ |
|
312 |
if ($this->ID && $iid) { |
|
313 |
$this->db->query( |
|
314 |
"UPDATE ".get_table_name('identities'). |
|
315 |
" SET ".$this->db->quoteIdentifier('standard')." = '0'". |
|
316 |
" WHERE user_id = ?". |
|
317 |
" AND identity_id <> ?". |
|
318 |
" AND del <> 1", |
|
319 |
$this->ID, |
|
320 |
$iid); |
|
321 |
} |
|
322 |
} |
e99991
|
323 |
|
A |
324 |
|
a78901
|
325 |
/** |
A |
326 |
* Update user's last_login timestamp |
|
327 |
*/ |
|
328 |
function touch() |
|
329 |
{ |
|
330 |
if ($this->ID) { |
|
331 |
$this->db->query( |
|
332 |
"UPDATE ".get_table_name('users'). |
|
333 |
" SET last_login = ".$this->db->now(). |
|
334 |
" WHERE user_id = ?", |
|
335 |
$this->ID); |
|
336 |
} |
|
337 |
} |
e99991
|
338 |
|
A |
339 |
|
a78901
|
340 |
/** |
A |
341 |
* Clear the saved object state |
|
342 |
*/ |
|
343 |
function reset() |
|
344 |
{ |
|
345 |
$this->ID = null; |
|
346 |
$this->data = null; |
|
347 |
} |
e99991
|
348 |
|
A |
349 |
|
a78901
|
350 |
/** |
A |
351 |
* Find a user record matching the given name and host |
|
352 |
* |
5c461b
|
353 |
* @param string $user IMAP user name |
A |
354 |
* @param string $host IMAP host name |
|
355 |
* @return rcube_user New user instance |
a78901
|
356 |
*/ |
A |
357 |
static function query($user, $host) |
|
358 |
{ |
|
359 |
$dbh = rcmail::get_instance()->get_dbh(); |
e99991
|
360 |
|
e17553
|
361 |
// use BINARY (case-sensitive) comparison on MySQL, other engines are case-sensitive |
7fb11e
|
362 |
$mod = preg_match('/^mysql/', $dbh->db_provider) ? 'BINARY' : ''; |
e17553
|
363 |
|
a78901
|
364 |
// query for matching user name |
a1bbc2
|
365 |
$query = "SELECT * FROM ".get_table_name('users')." WHERE mail_host = ? AND %s = $mod ?"; |
T |
366 |
$sql_result = $dbh->query(sprintf($query, 'username'), $host, $user); |
e99991
|
367 |
|
a78901
|
368 |
// query for matching alias |
A |
369 |
if (!($sql_arr = $dbh->fetch_assoc($sql_result))) { |
a1bbc2
|
370 |
$sql_result = $dbh->query(sprintf($query, 'alias'), $host, $user); |
a78901
|
371 |
$sql_arr = $dbh->fetch_assoc($sql_result); |
A |
372 |
} |
e99991
|
373 |
|
a78901
|
374 |
// user already registered -> overwrite username |
A |
375 |
if ($sql_arr) |
|
376 |
return new rcube_user($sql_arr['user_id'], $sql_arr); |
|
377 |
else |
|
378 |
return false; |
|
379 |
} |
e99991
|
380 |
|
A |
381 |
|
a78901
|
382 |
/** |
A |
383 |
* Create a new user record and return a rcube_user instance |
|
384 |
* |
5c461b
|
385 |
* @param string $user IMAP user name |
A |
386 |
* @param string $host IMAP host |
|
387 |
* @return rcube_user New user instance |
a78901
|
388 |
*/ |
A |
389 |
static function create($user, $host) |
|
390 |
{ |
|
391 |
$user_name = ''; |
|
392 |
$user_email = ''; |
|
393 |
$rcmail = rcmail::get_instance(); |
ac9927
|
394 |
|
a78901
|
395 |
// try to resolve user in virtuser table and file |
A |
396 |
if ($email_list = self::user2email($user, false, true)) { |
|
397 |
$user_email = is_array($email_list[0]) ? $email_list[0]['email'] : $email_list[0]; |
|
398 |
} |
f20971
|
399 |
|
e6ce00
|
400 |
$data = $rcmail->plugins->exec_hook('user_create', |
a78901
|
401 |
array('user'=>$user, 'user_name'=>$user_name, 'user_email'=>$user_email)); |
A |
402 |
|
|
403 |
// plugin aborted this operation |
|
404 |
if ($data['abort']) |
|
405 |
return false; |
|
406 |
|
|
407 |
$user_name = $data['user_name']; |
|
408 |
$user_email = $data['user_email']; |
|
409 |
|
|
410 |
$dbh = $rcmail->get_dbh(); |
|
411 |
|
|
412 |
$dbh->query( |
|
413 |
"INSERT INTO ".get_table_name('users'). |
|
414 |
" (created, last_login, username, mail_host, alias, language)". |
|
415 |
" VALUES (".$dbh->now().", ".$dbh->now().", ?, ?, ?, ?)", |
|
416 |
strip_newlines($user), |
|
417 |
strip_newlines($host), |
|
418 |
strip_newlines($data['alias'] ? $data['alias'] : $user_email), |
532c25
|
419 |
strip_newlines($data['language'] ? $data['language'] : $_SESSION['language'])); |
a78901
|
420 |
|
A |
421 |
if ($user_id = $dbh->insert_id('users')) { |
|
422 |
// create rcube_user instance to make plugin hooks work |
|
423 |
$user_instance = new rcube_user($user_id); |
|
424 |
$rcmail->user = $user_instance; |
|
425 |
|
|
426 |
$mail_domain = $rcmail->config->mail_domain($host); |
|
427 |
|
|
428 |
if ($user_email == '') { |
|
429 |
$user_email = strpos($user, '@') ? $user : sprintf('%s@%s', $user, $mail_domain); |
|
430 |
} |
|
431 |
if ($user_name == '') { |
|
432 |
$user_name = $user != $user_email ? $user : ''; |
|
433 |
} |
|
434 |
|
|
435 |
if (empty($email_list)) |
|
436 |
$email_list[] = strip_newlines($user_email); |
|
437 |
// identities_level check |
|
438 |
else if (count($email_list) > 1 && $rcmail->config->get('identities_level', 0) > 1) |
|
439 |
$email_list = array($email_list[0]); |
|
440 |
|
|
441 |
// create new identities records |
|
442 |
$standard = 1; |
|
443 |
foreach ($email_list as $row) { |
|
444 |
$record = array(); |
|
445 |
|
|
446 |
if (is_array($row)) { |
|
447 |
$record = $row; |
|
448 |
} |
|
449 |
else { |
|
450 |
$record['email'] = $row; |
|
451 |
} |
|
452 |
|
|
453 |
if (empty($record['name'])) |
|
454 |
$record['name'] = $user_name; |
|
455 |
$record['name'] = strip_newlines($record['name']); |
|
456 |
$record['user_id'] = $user_id; |
|
457 |
$record['standard'] = $standard; |
|
458 |
|
e6ce00
|
459 |
$plugin = $rcmail->plugins->exec_hook('identity_create', |
a78901
|
460 |
array('login' => true, 'record' => $record)); |
e99991
|
461 |
|
a78901
|
462 |
if (!$plugin['abort'] && $plugin['record']['email']) { |
A |
463 |
$rcmail->user->insert_identity($plugin['record']); |
|
464 |
} |
|
465 |
$standard = 0; |
|
466 |
} |
08c8c3
|
467 |
} |
T |
468 |
else { |
a78901
|
469 |
raise_error(array( |
A |
470 |
'code' => 500, |
|
471 |
'type' => 'php', |
|
472 |
'line' => __LINE__, |
|
473 |
'file' => __FILE__, |
|
474 |
'message' => "Failed to create new user"), true, false); |
08c8c3
|
475 |
} |
e99991
|
476 |
|
a78901
|
477 |
return $user_id ? $user_instance : false; |
A |
478 |
} |
e99991
|
479 |
|
A |
480 |
|
a78901
|
481 |
/** |
A |
482 |
* Resolve username using a virtuser plugins |
|
483 |
* |
5c461b
|
484 |
* @param string $email E-mail address to resolve |
a78901
|
485 |
* @return string Resolved IMAP username |
A |
486 |
*/ |
|
487 |
static function email2user($email) |
|
488 |
{ |
|
489 |
$rcmail = rcmail::get_instance(); |
|
490 |
$plugin = $rcmail->plugins->exec_hook('email2user', |
|
491 |
array('email' => $email, 'user' => NULL)); |
fba1f5
|
492 |
|
a78901
|
493 |
return $plugin['user']; |
A |
494 |
} |
fba1f5
|
495 |
|
T |
496 |
|
a78901
|
497 |
/** |
A |
498 |
* Resolve e-mail address from virtuser plugins |
|
499 |
* |
5c461b
|
500 |
* @param string $user User name |
A |
501 |
* @param boolean $first If true returns first found entry |
|
502 |
* @param boolean $extended If true returns email as array (email and name for identity) |
a78901
|
503 |
* @return mixed Resolved e-mail address string or array of strings |
A |
504 |
*/ |
|
505 |
static function user2email($user, $first=true, $extended=false) |
|
506 |
{ |
|
507 |
$rcmail = rcmail::get_instance(); |
|
508 |
$plugin = $rcmail->plugins->exec_hook('user2email', |
|
509 |
array('email' => NULL, 'user' => $user, |
|
510 |
'first' => $first, 'extended' => $extended)); |
fba1f5
|
511 |
|
a78901
|
512 |
return empty($plugin['email']) ? NULL : $plugin['email']; |
A |
513 |
} |
8dfe51
|
514 |
|
fba1f5
|
515 |
} |