commit | author | age
|
fba1f5
|
1 |
<?php |
T |
2 |
|
|
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
e019f2
|
5 |
| This file is part of the Roundcube Webmail client | |
9ab346
|
6 |
| Copyright (C) 2005-2012, The Roundcube Dev Team | |
7fe381
|
7 |
| | |
T |
8 |
| Licensed under the GNU General Public License version 3 or | |
|
9 |
| any later version with exceptions for skins & plugins. | |
|
10 |
| See the README file for a full license statement. | |
fba1f5
|
11 |
| | |
T |
12 |
| PURPOSE: | |
|
13 |
| This class represents a system user linked and provides access | |
|
14 |
| to the related database records. | |
|
15 |
+-----------------------------------------------------------------------+ |
|
16 |
| Author: Thomas Bruederli <roundcube@gmail.com> | |
9ab346
|
17 |
| Author: Aleksander Machniak <alec@alec.pl> | |
fba1f5
|
18 |
+-----------------------------------------------------------------------+ |
T |
19 |
*/ |
|
20 |
|
|
21 |
/** |
|
22 |
* Class representing a system user |
|
23 |
* |
9ab346
|
24 |
* @package Framework |
AM |
25 |
* @subpackage Core |
fba1f5
|
26 |
*/ |
T |
27 |
class rcube_user |
|
28 |
{ |
40a186
|
29 |
public $ID; |
A |
30 |
public $data; |
|
31 |
public $language; |
a78901
|
32 |
|
5c461b
|
33 |
/** |
A |
34 |
* Holds database connection. |
|
35 |
* |
0d94fd
|
36 |
* @var rcube_db |
5c461b
|
37 |
*/ |
40a186
|
38 |
private $db; |
A |
39 |
|
|
40 |
/** |
be98df
|
41 |
* Framework object. |
40a186
|
42 |
* |
be98df
|
43 |
* @var rcube |
40a186
|
44 |
*/ |
A |
45 |
private $rc; |
fba1f5
|
46 |
|
f410c9
|
47 |
/** |
AM |
48 |
* Internal identities cache |
|
49 |
* |
|
50 |
* @var array |
|
51 |
*/ |
|
52 |
private $identities = array(); |
|
53 |
|
f8e48d
|
54 |
const SEARCH_ADDRESSBOOK = 1; |
A |
55 |
const SEARCH_MAIL = 2; |
95987c
|
56 |
|
a78901
|
57 |
/** |
A |
58 |
* Object constructor |
|
59 |
* |
5c461b
|
60 |
* @param int $id User id |
A |
61 |
* @param array $sql_arr SQL result set |
a78901
|
62 |
*/ |
A |
63 |
function __construct($id = null, $sql_arr = null) |
|
64 |
{ |
be98df
|
65 |
$this->rc = rcube::get_instance(); |
40a186
|
66 |
$this->db = $this->rc->get_dbh(); |
e99991
|
67 |
|
a78901
|
68 |
if ($id && !$sql_arr) { |
A |
69 |
$sql_result = $this->db->query( |
0c2596
|
70 |
"SELECT * FROM ".$this->db->table_name('users')." WHERE user_id = ?", $id); |
a78901
|
71 |
$sql_arr = $this->db->fetch_assoc($sql_result); |
A |
72 |
} |
|
73 |
|
|
74 |
if (!empty($sql_arr)) { |
|
75 |
$this->ID = $sql_arr['user_id']; |
|
76 |
$this->data = $sql_arr; |
|
77 |
$this->language = $sql_arr['language']; |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
|
|
82 |
/** |
|
83 |
* Build a user name string (as e-mail address) |
|
84 |
* |
789e59
|
85 |
* @param string $part Username part (empty or 'local' or 'domain', 'mail') |
8dfe51
|
86 |
* @return string Full user name or its part |
a78901
|
87 |
*/ |
8dfe51
|
88 |
function get_username($part = null) |
a78901
|
89 |
{ |
A |
90 |
if ($this->data['username']) { |
789e59
|
91 |
// return real name |
AM |
92 |
if (!$part) { |
|
93 |
return $this->data['username']; |
|
94 |
} |
|
95 |
|
8dfe51
|
96 |
list($local, $domain) = explode('@', $this->data['username']); |
A |
97 |
|
|
98 |
// at least we should always have the local part |
|
99 |
if ($part == 'local') { |
|
100 |
return $local; |
|
101 |
} |
b0eeaa
|
102 |
// if no domain was provided... |
A |
103 |
if (empty($domain)) { |
40a186
|
104 |
$domain = $this->rc->config->mail_domain($this->data['mail_host']); |
b0eeaa
|
105 |
} |
8dfe51
|
106 |
|
A |
107 |
if ($part == 'domain') { |
|
108 |
return $domain; |
|
109 |
} |
|
110 |
|
|
111 |
if (!empty($domain)) |
|
112 |
return $local . '@' . $domain; |
a78901
|
113 |
else |
8dfe51
|
114 |
return $local; |
a78901
|
115 |
} |
A |
116 |
|
|
117 |
return false; |
|
118 |
} |
|
119 |
|
|
120 |
|
|
121 |
/** |
|
122 |
* Get the preferences saved for this user |
|
123 |
* |
|
124 |
* @return array Hash array with prefs |
|
125 |
*/ |
|
126 |
function get_prefs() |
|
127 |
{ |
e59471
|
128 |
$prefs = array(); |
TB |
129 |
|
a78901
|
130 |
if (!empty($this->language)) |
e59471
|
131 |
$prefs['language'] = $this->language; |
8dfe51
|
132 |
|
40a186
|
133 |
if ($this->ID) { |
A |
134 |
// Preferences from session (write-master is unavailable) |
|
135 |
if (!empty($_SESSION['preferences'])) { |
|
136 |
// Check last write attempt time, try to write again (every 5 minutes) |
|
137 |
if ($_SESSION['preferences_time'] < time() - 5 * 60) { |
0c2596
|
138 |
$saved_prefs = unserialize($_SESSION['preferences']); |
59ab0c
|
139 |
$this->rc->session->remove('preferences'); |
0c2596
|
140 |
$this->rc->session->remove('preferences_time'); |
59ab0c
|
141 |
$this->save_prefs($saved_prefs); |
40a186
|
142 |
} |
A |
143 |
else { |
|
144 |
$this->data['preferences'] = $_SESSION['preferences']; |
|
145 |
} |
|
146 |
} |
|
147 |
|
|
148 |
if ($this->data['preferences']) { |
|
149 |
$prefs += (array)unserialize($this->data['preferences']); |
|
150 |
} |
|
151 |
} |
8dfe51
|
152 |
|
a78901
|
153 |
return $prefs; |
A |
154 |
} |
8dfe51
|
155 |
|
A |
156 |
|
a78901
|
157 |
/** |
A |
158 |
* Write the given user prefs to the user's record |
|
159 |
* |
5c461b
|
160 |
* @param array $a_user_prefs User prefs to save |
bfe657
|
161 |
* @param bool $no_session Simplified language/preferences handling |
AM |
162 |
* |
a78901
|
163 |
* @return boolean True on success, False on failure |
A |
164 |
*/ |
bfe657
|
165 |
function save_prefs($a_user_prefs, $no_session = false) |
a78901
|
166 |
{ |
A |
167 |
if (!$this->ID) |
|
168 |
return false; |
e99991
|
169 |
|
4f432f
|
170 |
$plugin = $this->rc->plugins->exec_hook('preferences_update', array( |
TB |
171 |
'userid' => $this->ID, 'prefs' => $a_user_prefs, 'old' => (array)$this->get_prefs())); |
|
172 |
|
|
173 |
if (!empty($plugin['abort'])) { |
|
174 |
return; |
|
175 |
} |
|
176 |
|
|
177 |
$a_user_prefs = $plugin['prefs']; |
|
178 |
$old_prefs = $plugin['old']; |
|
179 |
$config = $this->rc->config; |
fba1f5
|
180 |
|
a78901
|
181 |
// merge (partial) prefs array with existing settings |
A |
182 |
$save_prefs = $a_user_prefs + $old_prefs; |
|
183 |
unset($save_prefs['language']); |
e99991
|
184 |
|
a78901
|
185 |
// don't save prefs with default values if they haven't been changed yet |
A |
186 |
foreach ($a_user_prefs as $key => $value) { |
f27c53
|
187 |
if ($value === null || (!isset($old_prefs[$key]) && ($value == $config->get($key)))) |
a78901
|
188 |
unset($save_prefs[$key]); |
A |
189 |
} |
|
190 |
|
|
191 |
$save_prefs = serialize($save_prefs); |
bfe657
|
192 |
if (!$no_session) { |
AM |
193 |
$this->language = $_SESSION['language']; |
|
194 |
} |
a78901
|
195 |
|
A |
196 |
$this->db->query( |
0c2596
|
197 |
"UPDATE ".$this->db->table_name('users'). |
a78901
|
198 |
" SET preferences = ?". |
A |
199 |
", language = ?". |
|
200 |
" WHERE user_id = ?", |
|
201 |
$save_prefs, |
bfe657
|
202 |
$this->language, |
a78901
|
203 |
$this->ID); |
A |
204 |
|
40a186
|
205 |
// Update success |
80809d
|
206 |
if ($this->db->affected_rows() !== false) { |
a78901
|
207 |
$this->data['preferences'] = $save_prefs; |
40a186
|
208 |
|
bfe657
|
209 |
if (!$no_session) { |
AM |
210 |
$config->set_user_prefs($a_user_prefs); |
|
211 |
|
|
212 |
if (isset($_SESSION['preferences'])) { |
|
213 |
$this->rc->session->remove('preferences'); |
|
214 |
$this->rc->session->remove('preferences_time'); |
|
215 |
} |
40a186
|
216 |
} |
bfe657
|
217 |
|
a78901
|
218 |
return true; |
A |
219 |
} |
40a186
|
220 |
// Update error, but we are using replication (we have read-only DB connection) |
A |
221 |
// and we are storing session not in the SQL database |
|
222 |
// we can store preferences in session and try to write later (see get_prefs()) |
bfe657
|
223 |
else if (!$no_session && $this->db->is_replicated() |
AM |
224 |
&& $config->get('session_storage', 'db') != 'db' |
|
225 |
) { |
40a186
|
226 |
$_SESSION['preferences'] = $save_prefs; |
A |
227 |
$_SESSION['preferences_time'] = time(); |
|
228 |
$config->set_user_prefs($a_user_prefs); |
|
229 |
$this->data['preferences'] = $save_prefs; |
|
230 |
} |
a78901
|
231 |
|
A |
232 |
return false; |
286420
|
233 |
} |
f52c93
|
234 |
|
85e60a
|
235 |
/** |
TB |
236 |
* Generate a unique hash to identify this user which |
|
237 |
*/ |
|
238 |
function get_hash() |
|
239 |
{ |
|
240 |
$key = substr($this->rc->config->get('des_key'), 1, 4); |
|
241 |
return md5($this->data['user_id'] . $key . $this->data['username'] . '@' . $this->data['mail_host']); |
|
242 |
} |
f52c93
|
243 |
|
a78901
|
244 |
/** |
A |
245 |
* Get default identity of this user |
|
246 |
* |
5c461b
|
247 |
* @param int $id Identity ID. If empty, the default identity is returned |
a78901
|
248 |
* @return array Hash array with all cols of the identity record |
A |
249 |
*/ |
|
250 |
function get_identity($id = null) |
fba1f5
|
251 |
{ |
f410c9
|
252 |
$id = (int)$id; |
AM |
253 |
// cache identities for better performance |
|
254 |
if (!array_key_exists($id, $this->identities)) { |
|
255 |
$result = $this->list_identities($id ? 'AND identity_id = ' . $id : ''); |
|
256 |
$this->identities[$id] = $result[0]; |
|
257 |
} |
|
258 |
|
|
259 |
return $this->identities[$id]; |
fba1f5
|
260 |
} |
55f54e
|
261 |
|
A |
262 |
|
a78901
|
263 |
/** |
A |
264 |
* Return a list of all identities linked with this user |
|
265 |
* |
0247b8
|
266 |
* @param string $sql_add Optional WHERE clauses |
AM |
267 |
* @param bool $formatted Format identity email and name |
|
268 |
* |
a78901
|
269 |
* @return array List of identities |
A |
270 |
*/ |
0247b8
|
271 |
function list_identities($sql_add = '', $formatted = false) |
fba1f5
|
272 |
{ |
a78901
|
273 |
$result = array(); |
fba1f5
|
274 |
|
a78901
|
275 |
$sql_result = $this->db->query( |
0c2596
|
276 |
"SELECT * FROM ".$this->db->table_name('identities'). |
a78901
|
277 |
" WHERE del <> 1 AND user_id = ?". |
A |
278 |
($sql_add ? " ".$sql_add : ""). |
51fe04
|
279 |
" ORDER BY ".$this->db->quote_identifier('standard')." DESC, name ASC, identity_id ASC", |
a78901
|
280 |
$this->ID); |
e99991
|
281 |
|
a78901
|
282 |
while ($sql_arr = $this->db->fetch_assoc($sql_result)) { |
0247b8
|
283 |
if ($formatted) { |
AM |
284 |
$ascii_email = format_email($sql_arr['email']); |
|
285 |
$utf8_email = format_email(rcube_utils::idn_to_utf8($ascii_email)); |
|
286 |
|
|
287 |
$sql_arr['email_ascii'] = $ascii_email; |
|
288 |
$sql_arr['email'] = $utf8_email; |
7c5d4b
|
289 |
$sql_arr['ident'] = format_email_recipient($ascii_email, $sql_arr['name']); |
0247b8
|
290 |
} |
AM |
291 |
|
a78901
|
292 |
$result[] = $sql_arr; |
A |
293 |
} |
e99991
|
294 |
|
a78901
|
295 |
return $result; |
A |
296 |
} |
fba1f5
|
297 |
|
a78901
|
298 |
|
A |
299 |
/** |
|
300 |
* Update a specific identity record |
|
301 |
* |
5c461b
|
302 |
* @param int $iid Identity ID |
A |
303 |
* @param array $data Hash array with col->value pairs to save |
a78901
|
304 |
* @return boolean True if saved successfully, false if nothing changed |
A |
305 |
*/ |
|
306 |
function update_identity($iid, $data) |
fba1f5
|
307 |
{ |
a78901
|
308 |
if (!$this->ID) |
A |
309 |
return false; |
|
310 |
|
|
311 |
$query_cols = $query_params = array(); |
e99991
|
312 |
|
a78901
|
313 |
foreach ((array)$data as $col => $value) { |
51fe04
|
314 |
$query_cols[] = $this->db->quote_identifier($col) . ' = ?'; |
a78901
|
315 |
$query_params[] = $value; |
A |
316 |
} |
|
317 |
$query_params[] = $iid; |
|
318 |
$query_params[] = $this->ID; |
|
319 |
|
0c2596
|
320 |
$sql = "UPDATE ".$this->db->table_name('identities'). |
a78901
|
321 |
" SET changed = ".$this->db->now().", ".join(', ', $query_cols). |
A |
322 |
" WHERE identity_id = ?". |
|
323 |
" AND user_id = ?". |
|
324 |
" AND del <> 1"; |
|
325 |
|
|
326 |
call_user_func_array(array($this->db, 'query'), |
|
327 |
array_merge(array($sql), $query_params)); |
e99991
|
328 |
|
f410c9
|
329 |
$this->identities = array(); |
AM |
330 |
|
a78901
|
331 |
return $this->db->affected_rows(); |
fba1f5
|
332 |
} |
e99991
|
333 |
|
A |
334 |
|
a78901
|
335 |
/** |
A |
336 |
* Create a new identity record linked with this user |
|
337 |
* |
5c461b
|
338 |
* @param array $data Hash array with col->value pairs to save |
a78901
|
339 |
* @return int The inserted identity ID or false on error |
A |
340 |
*/ |
|
341 |
function insert_identity($data) |
fba1f5
|
342 |
{ |
a78901
|
343 |
if (!$this->ID) |
A |
344 |
return false; |
|
345 |
|
|
346 |
unset($data['user_id']); |
|
347 |
|
|
348 |
$insert_cols = $insert_values = array(); |
|
349 |
foreach ((array)$data as $col => $value) { |
51fe04
|
350 |
$insert_cols[] = $this->db->quote_identifier($col); |
a78901
|
351 |
$insert_values[] = $value; |
A |
352 |
} |
|
353 |
$insert_cols[] = 'user_id'; |
|
354 |
$insert_values[] = $this->ID; |
|
355 |
|
0c2596
|
356 |
$sql = "INSERT INTO ".$this->db->table_name('identities'). |
a78901
|
357 |
" (changed, ".join(', ', $insert_cols).")". |
A |
358 |
" VALUES (".$this->db->now().", ".join(', ', array_pad(array(), sizeof($insert_values), '?')).")"; |
|
359 |
|
|
360 |
call_user_func_array(array($this->db, 'query'), |
|
361 |
array_merge(array($sql), $insert_values)); |
f410c9
|
362 |
|
AM |
363 |
$this->identities = array(); |
a78901
|
364 |
|
A |
365 |
return $this->db->insert_id('identities'); |
fba1f5
|
366 |
} |
e99991
|
367 |
|
A |
368 |
|
a78901
|
369 |
/** |
A |
370 |
* Mark the given identity as deleted |
|
371 |
* |
5c461b
|
372 |
* @param int $iid Identity ID |
a78901
|
373 |
* @return boolean True if deleted successfully, false if nothing changed |
A |
374 |
*/ |
|
375 |
function delete_identity($iid) |
fba1f5
|
376 |
{ |
a78901
|
377 |
if (!$this->ID) |
A |
378 |
return false; |
07722a
|
379 |
|
a78901
|
380 |
$sql_result = $this->db->query( |
0c2596
|
381 |
"SELECT count(*) AS ident_count FROM ".$this->db->table_name('identities'). |
a78901
|
382 |
" WHERE user_id = ? AND del <> 1", |
A |
383 |
$this->ID); |
fba1f5
|
384 |
|
a78901
|
385 |
$sql_arr = $this->db->fetch_assoc($sql_result); |
fba1f5
|
386 |
|
a78901
|
387 |
// we'll not delete last identity |
A |
388 |
if ($sql_arr['ident_count'] <= 1) |
bbb142
|
389 |
return -1; |
e99991
|
390 |
|
a78901
|
391 |
$this->db->query( |
0c2596
|
392 |
"UPDATE ".$this->db->table_name('identities'). |
a78901
|
393 |
" SET del = 1, changed = ".$this->db->now(). |
A |
394 |
" WHERE user_id = ?". |
|
395 |
" AND identity_id = ?", |
|
396 |
$this->ID, |
|
397 |
$iid); |
fba1f5
|
398 |
|
f410c9
|
399 |
$this->identities = array(); |
AM |
400 |
|
a78901
|
401 |
return $this->db->affected_rows(); |
A |
402 |
} |
e99991
|
403 |
|
A |
404 |
|
a78901
|
405 |
/** |
A |
406 |
* Make this identity the default one for this user |
|
407 |
* |
5c461b
|
408 |
* @param int $iid The identity ID |
a78901
|
409 |
*/ |
A |
410 |
function set_default($iid) |
|
411 |
{ |
|
412 |
if ($this->ID && $iid) { |
|
413 |
$this->db->query( |
0c2596
|
414 |
"UPDATE ".$this->db->table_name('identities'). |
51fe04
|
415 |
" SET ".$this->db->quote_identifier('standard')." = '0'". |
a78901
|
416 |
" WHERE user_id = ?". |
A |
417 |
" AND identity_id <> ?". |
|
418 |
" AND del <> 1", |
|
419 |
$this->ID, |
|
420 |
$iid); |
f410c9
|
421 |
|
AM |
422 |
unset($this->identities[0]); |
a78901
|
423 |
} |
A |
424 |
} |
e99991
|
425 |
|
A |
426 |
|
a78901
|
427 |
/** |
A |
428 |
* Update user's last_login timestamp |
|
429 |
*/ |
|
430 |
function touch() |
|
431 |
{ |
|
432 |
if ($this->ID) { |
|
433 |
$this->db->query( |
0c2596
|
434 |
"UPDATE ".$this->db->table_name('users'). |
a78901
|
435 |
" SET last_login = ".$this->db->now(). |
A |
436 |
" WHERE user_id = ?", |
|
437 |
$this->ID); |
|
438 |
} |
|
439 |
} |
e99991
|
440 |
|
A |
441 |
|
a78901
|
442 |
/** |
A |
443 |
* Clear the saved object state |
|
444 |
*/ |
|
445 |
function reset() |
|
446 |
{ |
|
447 |
$this->ID = null; |
|
448 |
$this->data = null; |
|
449 |
} |
e99991
|
450 |
|
A |
451 |
|
a78901
|
452 |
/** |
A |
453 |
* Find a user record matching the given name and host |
|
454 |
* |
5c461b
|
455 |
* @param string $user IMAP user name |
A |
456 |
* @param string $host IMAP host name |
|
457 |
* @return rcube_user New user instance |
a78901
|
458 |
*/ |
A |
459 |
static function query($user, $host) |
|
460 |
{ |
565c47
|
461 |
$dbh = rcube::get_instance()->get_dbh(); |
AM |
462 |
$config = rcube::get_instance()->config; |
e99991
|
463 |
|
a78901
|
464 |
// query for matching user name |
565c47
|
465 |
$sql_result = $dbh->query("SELECT * FROM " . $dbh->table_name('users') |
AM |
466 |
." WHERE mail_host = ? AND username = ?", $host, $user); |
e99991
|
467 |
|
565c47
|
468 |
$sql_arr = $dbh->fetch_assoc($sql_result); |
AM |
469 |
|
|
470 |
// username not found, try aliases from identities |
|
471 |
if (empty($sql_arr) && $config->get('user_aliases') && strpos($user, '@')) { |
|
472 |
$sql_result = $dbh->limitquery("SELECT u.*" |
|
473 |
." FROM " . $dbh->table_name('users') . " u" |
|
474 |
." JOIN " . $dbh->table_name('identities') . " i ON (i.user_id = u.user_id)" |
|
475 |
." WHERE email = ? AND del <> 1", 0, 1, $user); |
|
476 |
|
a78901
|
477 |
$sql_arr = $dbh->fetch_assoc($sql_result); |
A |
478 |
} |
e99991
|
479 |
|
a78901
|
480 |
// user already registered -> overwrite username |
A |
481 |
if ($sql_arr) |
|
482 |
return new rcube_user($sql_arr['user_id'], $sql_arr); |
|
483 |
else |
|
484 |
return false; |
|
485 |
} |
e99991
|
486 |
|
A |
487 |
|
a78901
|
488 |
/** |
A |
489 |
* Create a new user record and return a rcube_user instance |
|
490 |
* |
5c461b
|
491 |
* @param string $user IMAP user name |
A |
492 |
* @param string $host IMAP host |
|
493 |
* @return rcube_user New user instance |
a78901
|
494 |
*/ |
A |
495 |
static function create($user, $host) |
|
496 |
{ |
|
497 |
$user_name = ''; |
|
498 |
$user_email = ''; |
f708c8
|
499 |
$rcube = rcube::get_instance(); |
AM |
500 |
$dbh = $rcube->get_dbh(); |
ac9927
|
501 |
|
a78901
|
502 |
// try to resolve user in virtuser table and file |
A |
503 |
if ($email_list = self::user2email($user, false, true)) { |
|
504 |
$user_email = is_array($email_list[0]) ? $email_list[0]['email'] : $email_list[0]; |
|
505 |
} |
f20971
|
506 |
|
f708c8
|
507 |
$data = $rcube->plugins->exec_hook('user_create', array( |
AM |
508 |
'host' => $host, |
|
509 |
'user' => $user, |
|
510 |
'user_name' => $user_name, |
|
511 |
'user_email' => $user_email, |
|
512 |
'email_list' => $email_list, |
540de5
|
513 |
'language' => $_SESSION['language'], |
f708c8
|
514 |
)); |
a78901
|
515 |
|
A |
516 |
// plugin aborted this operation |
f708c8
|
517 |
if ($data['abort']) { |
a78901
|
518 |
return false; |
f708c8
|
519 |
} |
a78901
|
520 |
|
A |
521 |
$dbh->query( |
0c2596
|
522 |
"INSERT INTO ".$dbh->table_name('users'). |
ee2187
|
523 |
" (created, last_login, username, mail_host, language)". |
AM |
524 |
" VALUES (".$dbh->now().", ".$dbh->now().", ?, ?, ?)", |
1d67fe
|
525 |
$data['user'], |
AM |
526 |
$data['host'], |
|
527 |
$data['language']); |
a78901
|
528 |
|
A |
529 |
if ($user_id = $dbh->insert_id('users')) { |
|
530 |
// create rcube_user instance to make plugin hooks work |
540de5
|
531 |
$user_instance = new rcube_user($user_id, array( |
AM |
532 |
'user_id' => $user_id, |
|
533 |
'username' => $data['user'], |
|
534 |
'mail_host' => $data['host'], |
|
535 |
'language' => $data['language'], |
|
536 |
)); |
|
537 |
$rcube->user = $user_instance; |
f708c8
|
538 |
$mail_domain = $rcube->config->mail_domain($data['host']); |
AM |
539 |
$user_name = $data['user_name']; |
|
540 |
$user_email = $data['user_email']; |
|
541 |
$email_list = $data['email_list']; |
a78901
|
542 |
|
f708c8
|
543 |
if (empty($email_list)) { |
AM |
544 |
if (empty($user_email)) { |
|
545 |
$user_email = strpos($data['user'], '@') ? $user : sprintf('%s@%s', $data['user'], $mail_domain); |
|
546 |
} |
1d67fe
|
547 |
$email_list[] = $user_email; |
f708c8
|
548 |
} |
a78901
|
549 |
// identities_level check |
f708c8
|
550 |
else if (count($email_list) > 1 && $rcube->config->get('identities_level', 0) > 1) { |
a78901
|
551 |
$email_list = array($email_list[0]); |
f708c8
|
552 |
} |
AM |
553 |
|
|
554 |
if (empty($user_name)) { |
|
555 |
$user_name = $data['user']; |
|
556 |
} |
a78901
|
557 |
|
A |
558 |
// create new identities records |
|
559 |
$standard = 1; |
|
560 |
foreach ($email_list as $row) { |
622bce
|
561 |
$record = array(); |
a78901
|
562 |
|
A |
563 |
if (is_array($row)) { |
f708c8
|
564 |
if (empty($row['email'])) { |
AM |
565 |
continue; |
|
566 |
} |
622bce
|
567 |
$record = $row; |
a78901
|
568 |
} |
A |
569 |
else { |
|
570 |
$record['email'] = $row; |
|
571 |
} |
|
572 |
|
f708c8
|
573 |
if (empty($record['name'])) { |
AM |
574 |
$record['name'] = $user_name != $record['email'] ? $user_name : ''; |
|
575 |
} |
|
576 |
|
|
577 |
$record['user_id'] = $user_id; |
a78901
|
578 |
$record['standard'] = $standard; |
A |
579 |
|
be98df
|
580 |
$plugin = $rcube->plugins->exec_hook('identity_create', |
622bce
|
581 |
array('login' => true, 'record' => $record)); |
e99991
|
582 |
|
a78901
|
583 |
if (!$plugin['abort'] && $plugin['record']['email']) { |
be98df
|
584 |
$rcube->user->insert_identity($plugin['record']); |
a78901
|
585 |
} |
A |
586 |
$standard = 0; |
|
587 |
} |
08c8c3
|
588 |
} |
T |
589 |
else { |
0c2596
|
590 |
rcube::raise_error(array( |
a78901
|
591 |
'code' => 500, |
A |
592 |
'type' => 'php', |
|
593 |
'line' => __LINE__, |
|
594 |
'file' => __FILE__, |
|
595 |
'message' => "Failed to create new user"), true, false); |
08c8c3
|
596 |
} |
e99991
|
597 |
|
a78901
|
598 |
return $user_id ? $user_instance : false; |
A |
599 |
} |
e99991
|
600 |
|
A |
601 |
|
a78901
|
602 |
/** |
A |
603 |
* Resolve username using a virtuser plugins |
|
604 |
* |
5c461b
|
605 |
* @param string $email E-mail address to resolve |
a78901
|
606 |
* @return string Resolved IMAP username |
A |
607 |
*/ |
|
608 |
static function email2user($email) |
|
609 |
{ |
be98df
|
610 |
$rcube = rcube::get_instance(); |
A |
611 |
$plugin = $rcube->plugins->exec_hook('email2user', |
a78901
|
612 |
array('email' => $email, 'user' => NULL)); |
fba1f5
|
613 |
|
a78901
|
614 |
return $plugin['user']; |
A |
615 |
} |
fba1f5
|
616 |
|
T |
617 |
|
a78901
|
618 |
/** |
A |
619 |
* Resolve e-mail address from virtuser plugins |
|
620 |
* |
5c461b
|
621 |
* @param string $user User name |
A |
622 |
* @param boolean $first If true returns first found entry |
|
623 |
* @param boolean $extended If true returns email as array (email and name for identity) |
a78901
|
624 |
* @return mixed Resolved e-mail address string or array of strings |
A |
625 |
*/ |
|
626 |
static function user2email($user, $first=true, $extended=false) |
|
627 |
{ |
be98df
|
628 |
$rcube = rcube::get_instance(); |
A |
629 |
$plugin = $rcube->plugins->exec_hook('user2email', |
a78901
|
630 |
array('email' => NULL, 'user' => $user, |
A |
631 |
'first' => $first, 'extended' => $extended)); |
fba1f5
|
632 |
|
a78901
|
633 |
return empty($plugin['email']) ? NULL : $plugin['email']; |
A |
634 |
} |
8dfe51
|
635 |
|
f8e48d
|
636 |
|
A |
637 |
/** |
|
638 |
* Return a list of saved searches linked with this user |
|
639 |
* |
|
640 |
* @param int $type Search type |
|
641 |
* |
|
642 |
* @return array List of saved searches indexed by search ID |
|
643 |
*/ |
|
644 |
function list_searches($type) |
|
645 |
{ |
|
646 |
$plugin = $this->rc->plugins->exec_hook('saved_search_list', array('type' => $type)); |
|
647 |
|
|
648 |
if ($plugin['abort']) { |
|
649 |
return (array) $plugin['result']; |
|
650 |
} |
|
651 |
|
|
652 |
$result = array(); |
|
653 |
|
|
654 |
$sql_result = $this->db->query( |
51fe04
|
655 |
"SELECT search_id AS id, ".$this->db->quote_identifier('name') |
0c2596
|
656 |
." FROM ".$this->db->table_name('searches') |
f8e48d
|
657 |
." WHERE user_id = ?" |
51fe04
|
658 |
." AND ".$this->db->quote_identifier('type')." = ?" |
AM |
659 |
." ORDER BY ".$this->db->quote_identifier('name'), |
f8e48d
|
660 |
(int) $this->ID, (int) $type); |
A |
661 |
|
|
662 |
while ($sql_arr = $this->db->fetch_assoc($sql_result)) { |
|
663 |
$sql_arr['data'] = unserialize($sql_arr['data']); |
|
664 |
$result[$sql_arr['id']] = $sql_arr; |
|
665 |
} |
|
666 |
|
|
667 |
return $result; |
|
668 |
} |
|
669 |
|
|
670 |
|
|
671 |
/** |
|
672 |
* Return saved search data. |
|
673 |
* |
|
674 |
* @param int $id Row identifier |
|
675 |
* |
|
676 |
* @return array Data |
|
677 |
*/ |
|
678 |
function get_search($id) |
|
679 |
{ |
|
680 |
$plugin = $this->rc->plugins->exec_hook('saved_search_get', array('id' => $id)); |
|
681 |
|
|
682 |
if ($plugin['abort']) { |
|
683 |
return $plugin['result']; |
|
684 |
} |
|
685 |
|
|
686 |
$sql_result = $this->db->query( |
51fe04
|
687 |
"SELECT ".$this->db->quote_identifier('name') |
AM |
688 |
.", ".$this->db->quote_identifier('data') |
|
689 |
.", ".$this->db->quote_identifier('type') |
0c2596
|
690 |
." FROM ".$this->db->table_name('searches') |
f8e48d
|
691 |
." WHERE user_id = ?" |
A |
692 |
." AND search_id = ?", |
|
693 |
(int) $this->ID, (int) $id); |
|
694 |
|
|
695 |
while ($sql_arr = $this->db->fetch_assoc($sql_result)) { |
|
696 |
return array( |
|
697 |
'id' => $id, |
|
698 |
'name' => $sql_arr['name'], |
|
699 |
'type' => $sql_arr['type'], |
|
700 |
'data' => unserialize($sql_arr['data']), |
|
701 |
); |
|
702 |
} |
|
703 |
|
|
704 |
return null; |
|
705 |
} |
|
706 |
|
|
707 |
|
|
708 |
/** |
|
709 |
* Deletes given saved search record |
|
710 |
* |
|
711 |
* @param int $sid Search ID |
|
712 |
* |
|
713 |
* @return boolean True if deleted successfully, false if nothing changed |
|
714 |
*/ |
|
715 |
function delete_search($sid) |
|
716 |
{ |
|
717 |
if (!$this->ID) |
|
718 |
return false; |
|
719 |
|
|
720 |
$this->db->query( |
0c2596
|
721 |
"DELETE FROM ".$this->db->table_name('searches') |
f8e48d
|
722 |
." WHERE user_id = ?" |
A |
723 |
." AND search_id = ?", |
|
724 |
(int) $this->ID, $sid); |
|
725 |
|
|
726 |
return $this->db->affected_rows(); |
|
727 |
} |
|
728 |
|
|
729 |
|
|
730 |
/** |
|
731 |
* Create a new saved search record linked with this user |
|
732 |
* |
|
733 |
* @param array $data Hash array with col->value pairs to save |
|
734 |
* |
|
735 |
* @return int The inserted search ID or false on error |
|
736 |
*/ |
|
737 |
function insert_search($data) |
|
738 |
{ |
|
739 |
if (!$this->ID) |
|
740 |
return false; |
|
741 |
|
|
742 |
$insert_cols[] = 'user_id'; |
|
743 |
$insert_values[] = (int) $this->ID; |
51fe04
|
744 |
$insert_cols[] = $this->db->quote_identifier('type'); |
f8e48d
|
745 |
$insert_values[] = (int) $data['type']; |
51fe04
|
746 |
$insert_cols[] = $this->db->quote_identifier('name'); |
f8e48d
|
747 |
$insert_values[] = $data['name']; |
51fe04
|
748 |
$insert_cols[] = $this->db->quote_identifier('data'); |
f8e48d
|
749 |
$insert_values[] = serialize($data['data']); |
A |
750 |
|
0c2596
|
751 |
$sql = "INSERT INTO ".$this->db->table_name('searches') |
f8e48d
|
752 |
." (".join(', ', $insert_cols).")" |
A |
753 |
." VALUES (".join(', ', array_pad(array(), sizeof($insert_values), '?')).")"; |
|
754 |
|
|
755 |
call_user_func_array(array($this->db, 'query'), |
|
756 |
array_merge(array($sql), $insert_values)); |
|
757 |
|
|
758 |
return $this->db->insert_id('searches'); |
|
759 |
} |
|
760 |
|
fba1f5
|
761 |
} |