4 +-----------------------------------------------------------------------+
5 | program/include/rcube_user.inc |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2005-2007, RoundCube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | This class represents a system user linked and provides access |
13 | to the related database records. |
15 +-----------------------------------------------------------------------+
16 | Author: Thomas Bruederli <roundcube@gmail.com> |
17 +-----------------------------------------------------------------------+
19 $Id: rcube_user.inc 933 2007-11-29 14:17:32Z thomasb $
25 * Class representing a system user
28 * @author Thomas Bruederli <roundcube@gmail.com>
34 public $language = 'en_US';
42 * @param object DB Database connection
44 function __construct($id = null, $sql_arr = null)
46 $this->db = rcmail::get_instance()->get_dbh();
50 $sql_result = $this->db->query("SELECT * FROM ".get_table_name('users')." WHERE user_id=?", $id);
51 $sql_arr = $this->db->fetch_assoc($sql_result);
56 $this->ID = $sql_arr['user_id'];
57 $this->data = $sql_arr;
58 $this->language = $sql_arr['language'];
63 * PHP 4 object constructor
65 * @see rcube_user::__construct
67 function rcube_user($id = null, $sql_arr = null)
69 $this->__construct($id, $sql_arr);
74 * Build a user name string (as e-mail address)
76 * @return string Full user name
78 function get_username()
80 return $this->data['username'] ? $this->data['username'] . (!strpos($this->data['username'], '@') ? '@'.$this->data['mail_host'] : '') : false;
85 * Get the preferences saved for this user
87 * @return array Hash array with prefs
91 if ($this->ID && $this->data['preferences'])
92 return array('language' => $this->language) + unserialize($this->data['preferences']);
99 * Write the given user prefs to the user's record
101 * @param mixed User prefs to save
102 * @return boolean True on success, False on failure
104 function save_prefs($a_user_prefs)
109 // merge (partial) prefs array with existing settings
110 $a_user_prefs += (array)$this->get_prefs();
111 unset($a_user_prefs['language']);
114 "UPDATE ".get_table_name('users')."
118 serialize($a_user_prefs),
119 $_SESSION['language'],
122 $this->language = $_SESSION['language'];
123 if ($this->db->affected_rows())
125 rcmail::get_instance()->config->merge($a_user_prefs);
134 * Get default identity of this user
136 * @param int Identity ID. If empty, the default identity is returned
137 * @return array Hash array with all cols of the
139 function get_identity($id = null)
141 $sql_result = $this->list_identities($id ? sprintf('AND identity_id=%d', $id) : '');
142 return $this->db->fetch_assoc($sql_result);
147 * Return a list of all identities linked with this user
149 * @return array List of identities
151 function list_identities($sql_add = '')
153 // get contacts from DB
154 $sql_result = $this->db->query(
155 "SELECT * FROM ".get_table_name('identities')."
159 ORDER BY ".$this->db->quoteIdentifier('standard')." DESC, name ASC",
167 * Update a specific identity record
169 * @param int Identity ID
170 * @param array Hash array with col->value pairs to save
171 * @return boolean True if saved successfully, false if nothing changed
173 function update_identity($iid, $data)
178 $write_sql = array();
180 foreach ((array)$data as $col => $value)
182 $write_sql[] = sprintf("%s=%s",
183 $this->db->quoteIdentifier($col),
184 $this->db->quote($value));
188 "UPDATE ".get_table_name('identities')."
189 SET ".join(', ', $write_sql)."
196 return $this->db->affected_rows();
201 * Create a new identity record linked with this user
203 * @param array Hash array with col->value pairs to save
204 * @return int The inserted identity ID or false on error
206 function insert_identity($data)
211 $insert_cols = $insert_values = array();
212 foreach ((array)$data as $col => $value)
214 $insert_cols[] = $this->db->quoteIdentifier($col);
215 $insert_values[] = $this->db->quote($value);
219 "INSERT INTO ".get_table_name('identities')."
220 (user_id, ".join(', ', $insert_cols).")
221 VALUES (?, ".join(', ', $insert_values).")",
224 return $this->db->insert_id(get_sequence_name('identities'));
229 * Mark the given identity as deleted
231 * @param int Identity ID
232 * @return boolean True if deleted successfully, false if nothing changed
234 function delete_identity($iid)
239 if (!$this->ID || $this->ID == '')
242 $sql_result = $this->db->query("SELECT count(*) AS ident_count FROM " .
243 get_table_name('identities') .
244 " WHERE user_id = ? AND del <> 1",
247 $sql_arr = $this->db->fetch_assoc($sql_result);
248 if ($sql_arr['ident_count'] <= 1)
252 "UPDATE ".get_table_name('identities')."
259 return $this->db->affected_rows();
264 * Make this identity the default one for this user
266 * @param int The identity ID
268 function set_default($iid)
270 if ($this->ID && $iid)
273 "UPDATE ".get_table_name('identities')."
274 SET ".$this->db->quoteIdentifier('standard')."='0'
285 * Update user's last_login timestamp
292 "UPDATE ".get_table_name('users')."
293 SET last_login=".$this->db->now()."
301 * Clear the saved object state
311 * Find a user record matching the given name and host
313 * @param string IMAP user name
314 * @param string IMAP host name
315 * @return object rcube_user New user instance
317 static function query($user, $host)
319 $dbh = rcmail::get_instance()->get_dbh();
321 // query if user already registered
322 $sql_result = $dbh->query(
323 "SELECT * FROM ".get_table_name('users')."
324 WHERE mail_host=? AND (username=? OR alias=?)",
329 // user already registered -> overwrite username
330 if ($sql_arr = $dbh->fetch_assoc($sql_result))
331 return new rcube_user($sql_arr['user_id'], $sql_arr);
338 * Create a new user record and return a rcube_user instance
340 * @param string IMAP user name
341 * @param string IMAP host
342 * @return object rcube_user New user instance
344 static function create($user, $host)
347 $rcmail = rcmail::get_instance();
348 $dbh = $rcmail->get_dbh();
350 // try to resolve user in virtusertable
351 if ($rcmail->config->get('virtuser_file') && !strpos($user, '@'))
352 $user_email = rcube_user::user2email($user);
355 "INSERT INTO ".get_table_name('users')."
356 (created, last_login, username, mail_host, alias, language)
357 VALUES (".$dbh->now().", ".$dbh->now().", ?, ?, ?, ?)",
358 strip_newlines($user),
359 strip_newlines($host),
360 strip_newlines($user_email),
361 $_SESSION['language']);
363 if ($user_id = $dbh->insert_id(get_sequence_name('users')))
365 $mail_domain = rcmail_mail_domain($host);
368 $user_email = strpos($user, '@') ? $user : sprintf('%s@%s', $user, $mail_domain);
370 $user_name = $user != $user_email ? $user : '';
372 // try to resolve the e-mail address from the virtuser table
373 if ($virtuser_query = $rcmail->config->get('virtuser_query') &&
374 ($sql_result = $dbh->query(preg_replace('/%u/', $dbh->escapeSimple($user), $virtuser_query))) &&
375 ($dbh->num_rows() > 0))
377 while ($sql_arr = $dbh->fetch_array($sql_result))
380 "INSERT INTO ".get_table_name('identities')."
381 (user_id, del, standard, name, email)
382 VALUES (?, 0, 1, ?, ?)",
384 strip_newlines($user_name),
385 preg_replace('/^@/', $user . '@', $sql_arr[0]));
390 // also create new identity records
392 "INSERT INTO ".get_table_name('identities')."
393 (user_id, del, standard, name, email)
394 VALUES (?, 0, 1, ?, ?)",
396 strip_newlines($user_name),
397 strip_newlines($user_email));
407 'message' => "Failed to create new user"), true, false);
410 return $user_id ? new rcube_user($user_id) : false;
415 * Resolve username using a virtuser table
417 * @param string E-mail address to resolve
418 * @return string Resolved IMAP username
420 static function email2user($email)
423 $r = rcmail_findinvirtual("^$email");
425 for ($i=0; $i<count($r); $i++)
428 $arr = preg_split('/\s+/', $data);
431 $user = trim($arr[count($arr)-1]);
441 * Resolve e-mail address from virtuser table
443 * @param string User name
444 * @return string Resolved e-mail address
446 static function user2email($user)
449 $r = rcmail_findinvirtual("$user$");
451 for ($i=0; $i<count($r); $i++)
454 $arr = preg_split('/\s+/', $data);
457 $email = trim(str_replace('\\@', '@', $arr[0]));