+ $dbh = $rcmail->get_dbh();
+
+ $dbh->query(
+ "INSERT INTO ".get_table_name('users').
+ " (created, last_login, username, mail_host, alias, language)".
+ " VALUES (".$dbh->now().", ".$dbh->now().", ?, ?, ?, ?)",
+ strip_newlines($user),
+ strip_newlines($host),
+ strip_newlines($data['alias'] ? $data['alias'] : $user_email),
+ strip_newlines($data['language'] ? $data['language'] : $_SESSION['language']));
+
+ if ($user_id = $dbh->insert_id('users')) {
+ // create rcube_user instance to make plugin hooks work
+ $user_instance = new rcube_user($user_id);
+ $rcmail->user = $user_instance;
+
+ $mail_domain = $rcmail->config->mail_domain($host);
+
+ if ($user_email == '') {
+ $user_email = strpos($user, '@') ? $user : sprintf('%s@%s', $user, $mail_domain);
+ }
+ if ($user_name == '') {
+ $user_name = $user != $user_email ? $user : '';
+ }
+
+ if (empty($email_list))
+ $email_list[] = strip_newlines($user_email);
+ // identities_level check
+ else if (count($email_list) > 1 && $rcmail->config->get('identities_level', 0) > 1)
+ $email_list = array($email_list[0]);
+
+ // create new identities records
+ $standard = 1;
+ foreach ($email_list as $row) {
+ $record = array();
+
+ if (is_array($row)) {
+ $record = $row;
+ }
+ else {
+ $record['email'] = $row;
+ }
+
+ if (empty($record['name']))
+ $record['name'] = $user_name;
+ $record['name'] = strip_newlines($record['name']);
+ $record['user_id'] = $user_id;
+ $record['standard'] = $standard;
+
+ $plugin = $rcmail->plugins->exec_hook('identity_create',
+ array('login' => true, 'record' => $record));
+
+ if (!$plugin['abort'] && $plugin['record']['email']) {
+ $rcmail->user->insert_identity($plugin['record']);
+ }
+ $standard = 0;
+ }
+ }
+ else {
+ raise_error(array(
+ 'code' => 500,
+ 'type' => 'php',
+ 'line' => __LINE__,
+ 'file' => __FILE__,
+ 'message' => "Failed to create new user"), true, false);
+ }
+
+ return $user_id ? $user_instance : false;
+ }