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