4 * Copy a new users identity and settings from a nearby Squirrelmail installation
7 * @author Thomas Bruederli, Johannes Hessellund, pommi, Thomas Lueder
9 class squirrelmail_usercopy extends rcube_plugin
11 public $task = 'login';
13 private $prefs = null;
14 private $identities_level = 0;
15 private $abook = array();
17 public function init()
19 $this->add_hook('user_create', array($this, 'create_user'));
20 $this->add_hook('identity_create', array($this, 'create_identity'));
23 public function create_user($p)
25 $rcmail = rcmail::get_instance();
27 // Read plugin's config
30 // read prefs and add email address
31 $this->read_squirrel_prefs($p['user']);
32 if (($this->identities_level == 0 || $this->identities_level == 2) && $rcmail->config->get('squirrelmail_set_alias') && $this->prefs['email_address'])
33 $p['user_email'] = $this->prefs['email_address'];
37 public function create_identity($p)
39 $rcmail = rcmail::get_instance();
41 // prefs are set in create_user()
43 if ($this->prefs['full_name'])
44 $p['record']['name'] = $this->prefs['full_name'];
45 if (($this->identities_level == 0 || $this->identities_level == 2) && $this->prefs['email_address'])
46 $p['record']['email'] = $this->prefs['email_address'];
47 if ($this->prefs['___signature___'])
48 $p['record']['signature'] = $this->prefs['___signature___'];
49 if ($this->prefs['reply_to'])
50 $p['record']['reply-to'] = $this->prefs['reply_to'];
51 if (($this->identities_level == 0 || $this->identities_level == 1) && isset($this->prefs['identities']) && $this->prefs['identities'] > 1) {
52 for ($i=1; $i < $this->prefs['identities']; $i++) {
54 $ident_data = array('name' => '', 'email' => ''); // required data
55 if ($this->prefs['full_name'.$i])
56 $ident_data['name'] = $this->prefs['full_name'.$i];
57 if ($this->identities_level == 0 && $this->prefs['email_address'.$i])
58 $ident_data['email'] = $this->prefs['email_address'.$i];
60 $ident_data['email'] = $p['record']['email'];
61 if ($this->prefs['reply_to'.$i])
62 $ident_data['reply-to'] = $this->prefs['reply_to'.$i];
63 if ($this->prefs['___sig'.$i.'___'])
64 $ident_data['signature'] = $this->prefs['___sig'.$i.'___'];
66 $identid = $rcmail->user->insert_identity($ident_data);
71 $contacts = $rcmail->get_address_book(null, true);
72 if ($contacts && count($this->abook)) {
73 foreach ($this->abook as $rec) {
74 // #1487096 handle multi-address and/or too long items
75 $rec['email'] = array_shift(explode(';', $rec['email']));
76 if (check_email(idn_to_ascii($rec['email']))) {
77 $rec['email'] = idn_to_utf8($rec['email']);
78 $contacts->insert($rec, true);
83 // mark identity as complete for following hooks
84 $p['complete'] = true;
90 private function initialize()
92 $rcmail = rcmail::get_instance();
94 // Load plugin's config file
97 // Set identities_level for operations of this plugin
98 $ilevel = $rcmail->config->get('squirrelmail_identities_level');
100 $ilevel = $rcmail->config->get('identities_level', 0);
102 $this->identities_level = intval($ilevel);
105 private function read_squirrel_prefs($uname)
107 $rcmail = rcmail::get_instance();
109 /**** File based backend ****/
110 if ($rcmail->config->get('squirrelmail_driver') == 'file' && ($srcdir = $rcmail->config->get('squirrelmail_data_dir'))) {
111 if (($hash_level = $rcmail->config->get('squirrelmail_data_dir_hash_level')) > 0)
112 $srcdir = slashify($srcdir).chunk_split(substr(base_convert(crc32($uname), 10, 16), 0, $hash_level), 1, '/');
113 $prefsfile = slashify($srcdir) . $uname . '.pref';
114 $abookfile = slashify($srcdir) . $uname . '.abook';
115 $sigfile = slashify($srcdir) . $uname . '.sig';
116 $sigbase = slashify($srcdir) . $uname . '.si';
118 if (is_readable($prefsfile)) {
119 $this->prefs = array();
120 foreach (file($prefsfile) as $line) {
121 list($key, $value) = explode('=', $line);
122 $this->prefs[$key] = utf8_encode(rtrim($value));
125 // also read signature file if exists
126 if (is_readable($sigfile)) {
127 $this->prefs['___signature___'] = utf8_encode(file_get_contents($sigfile));
130 if (isset($this->prefs['identities']) && $this->prefs['identities'] > 1) {
131 for ($i=1; $i < $this->prefs['identities']; $i++) {
132 // read signature file if exists
133 if (is_readable($sigbase.$i)) {
134 $this->prefs['___sig'.$i.'___'] = utf8_encode(file_get_contents($sigbase.$i));
139 // parse addres book file
140 if (filesize($abookfile)) {
141 foreach(file($abookfile) as $line) {
142 list($rec['name'], $rec['firstname'], $rec['surname'], $rec['email']) = explode('|', utf8_encode(rtrim($line)));
143 if ($rec['name'] && $rec['email'])
144 $this->abook[] = $rec;
149 /**** Database backend ****/
150 else if ($rcmail->config->get('squirrelmail_driver') == 'sql') {
151 $this->prefs = array();
153 /* connect to squirrelmail database */
154 $db = new rcube_mdb2($rcmail->config->get('squirrelmail_dsn'));
155 $db->db_connect('r'); // connect in read mode
157 // $db->set_debug(true);
160 $userprefs_table = $rcmail->config->get('squirrelmail_userprefs_table');
161 $address_table = $rcmail->config->get('squirrelmail_address_table');
162 $db_charset = $rcmail->config->get('squirrelmail_db_charset');
165 $db->query('SET NAMES '.$db_charset);
167 $sql_result = $db->query('SELECT * FROM '.$userprefs_table.' WHERE user=?', $uname); // ? is replaced with emailaddress
169 while ($sql_array = $db->fetch_assoc($sql_result) ) { // fetch one row from result
170 $this->prefs[$sql_array['prefkey']] = rcube_charset_convert(rtrim($sql_array['prefval']), $db_charset);
173 /* retrieve address table data */
174 $sql_result = $db->query('SELECT * FROM '.$address_table.' WHERE owner=?', $uname); // ? is replaced with emailaddress
177 while ($sql_array = $db->fetch_assoc($sql_result) ) { // fetch one row from result
178 $rec['name'] = rcube_charset_convert(rtrim($sql_array['nickname']), $db_charset);
179 $rec['firstname'] = rcube_charset_convert(rtrim($sql_array['firstname']), $db_charset);
180 $rec['surname'] = rcube_charset_convert(rtrim($sql_array['lastname']), $db_charset);
181 $rec['email'] = rcube_charset_convert(rtrim($sql_array['email']), $db_charset);
182 $rec['note'] = rcube_charset_convert(rtrim($sql_array['label']), $db_charset);
184 if ($rec['name'] && $rec['email'])
185 $this->abook[] = $rec;
187 } // end if 'sql'-driver