comparison plugins/squirrelmail_usercopy/squirrelmail_usercopy.php @ 0:1e000243b222

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:50:29 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1e000243b222
1 <?php
2
3 /**
4 * Copy a new users identities and contacts from a nearby Squirrelmail installation
5 *
6 * @version 1.6
7 * @author Thomas Bruederli, Johannes Hessellund, pommi, Thomas Lueder
8 */
9 class squirrelmail_usercopy extends rcube_plugin
10 {
11 public $task = 'login';
12
13 private $prefs = null;
14 private $identities_level = 0;
15 private $abook = array();
16
17 public function init()
18 {
19 $this->add_hook('user_create', array($this, 'create_user'));
20 $this->add_hook('identity_create', array($this, 'create_identity'));
21 }
22
23 public function create_user($p)
24 {
25 $rcmail = rcmail::get_instance();
26
27 // Read plugin's config
28 $this->initialize();
29
30 // read prefs and add email address
31 $this->read_squirrel_prefs($p['user']);
32 if (($this->identities_level == 0 || $this->identities_level == 2)
33 && $rcmail->config->get('squirrelmail_set_alias')
34 && $this->prefs['email_address']
35 ) {
36 $p['user_email'] = $this->prefs['email_address'];
37 }
38
39 return $p;
40 }
41
42 public function create_identity($p)
43 {
44 $rcmail = rcmail::get_instance();
45
46 // prefs are set in create_user()
47 if ($this->prefs) {
48 if ($this->prefs['full_name']) {
49 $p['record']['name'] = $this->prefs['full_name'];
50 }
51
52 if (($this->identities_level == 0 || $this->identities_level == 2) && $this->prefs['email_address']) {
53 $p['record']['email'] = $this->prefs['email_address'];
54 }
55
56 if ($this->prefs['___signature___']) {
57 $p['record']['signature'] = $this->prefs['___signature___'];
58 }
59
60 if ($this->prefs['reply_to']) {
61 $p['record']['reply-to'] = $this->prefs['reply_to'];
62 }
63
64 if (($this->identities_level == 0 || $this->identities_level == 1)
65 && isset($this->prefs['identities']) && $this->prefs['identities'] > 1
66 ) {
67 for ($i = 1; $i < $this->prefs['identities']; $i++) {
68 unset($ident_data);
69 $ident_data = array('name' => '', 'email' => ''); // required data
70
71 if ($this->prefs['full_name'.$i]) {
72 $ident_data['name'] = $this->prefs['full_name'.$i];
73 }
74
75 if ($this->identities_level == 0 && $this->prefs['email_address'.$i]) {
76 $ident_data['email'] = $this->prefs['email_address'.$i];
77 }
78 else {
79 $ident_data['email'] = $p['record']['email'];
80 }
81
82 if ($this->prefs['reply_to'.$i]) {
83 $ident_data['reply-to'] = $this->prefs['reply_to'.$i];
84 }
85
86 if ($this->prefs['___sig'.$i.'___']) {
87 $ident_data['signature'] = $this->prefs['___sig'.$i.'___'];
88 }
89
90 // insert identity
91 $rcmail->user->insert_identity($ident_data);
92 }
93 }
94
95 // copy address book
96 $contacts = $rcmail->get_address_book(null, true);
97 $addresses = array();
98 $groups = array();
99
100 if ($contacts && !empty($this->abook)) {
101 foreach ($this->abook as $rec) {
102 // #1487096: handle multi-address and/or too long items
103 // #1487858: convert multi-address contacts into groups
104 $emails = preg_split('/[;,]/', $rec['email'], -1, PREG_SPLIT_NO_EMPTY);
105 $group_id = null;
106
107 // create group for addresses
108 if (count($emails) > 1) {
109 if (!($group_id = $groups[$rec['name']])) {
110 if ($group = $contacts->create_group($rec['name'])) {
111 $group_id = $group['id'];
112 $groups[$rec['name']] = $group_id;
113 }
114 }
115 }
116
117 // create contacts
118 foreach ($emails as $email) {
119 if (!($contact_id = $addresses[$email]) && rcube_utils::check_email(rcube_utils::idn_to_ascii($email))) {
120 $rec['email'] = rcube_utils::idn_to_utf8($email);
121 if ($contact_id = $contacts->insert($rec, true)) {
122 $addresses[$email] = $contact_id;
123 }
124 }
125
126 if ($group_id && $contact_id) {
127 $contacts->add_to_group($group_id, array($contact_id));
128 }
129 }
130 }
131 }
132
133 // mark identity as complete for following hooks
134 $p['complete'] = true;
135 }
136
137 return $p;
138 }
139
140 private function initialize()
141 {
142 $rcmail = rcmail::get_instance();
143
144 // Load plugin's config file
145 $this->load_config();
146
147 // Set identities_level for operations of this plugin
148 $ilevel = $rcmail->config->get('squirrelmail_identities_level');
149 if ($ilevel === null) {
150 $ilevel = $rcmail->config->get('identities_level', 0);
151 }
152
153 $this->identities_level = intval($ilevel);
154 }
155
156 private function read_squirrel_prefs($uname)
157 {
158 $rcmail = rcmail::get_instance();
159
160 /**** File based backend ****/
161 if ($rcmail->config->get('squirrelmail_driver') == 'file' && ($srcdir = $rcmail->config->get('squirrelmail_data_dir'))) {
162 if (($hash_level = $rcmail->config->get('squirrelmail_data_dir_hash_level')) > 0) {
163 $srcdir = slashify($srcdir).chunk_split(substr(base_convert(crc32($uname), 10, 16), 0, $hash_level), 1, '/');
164 }
165 $file_charset = $rcmail->config->get('squirrelmail_file_charset');
166
167 $prefsfile = slashify($srcdir) . $uname . '.pref';
168 $abookfile = slashify($srcdir) . $uname . '.abook';
169 $sigfile = slashify($srcdir) . $uname . '.sig';
170 $sigbase = slashify($srcdir) . $uname . '.si';
171
172 if (is_readable($prefsfile)) {
173 $this->prefs = array();
174 foreach (file($prefsfile) as $line) {
175 list($key, $value) = explode('=', $line);
176 $this->prefs[$key] = $this->convert_charset(rtrim($value), $file_charset);
177 }
178
179 // also read signature file if exists
180 if (is_readable($sigfile)) {
181 $sig = file_get_contents($sigfile);
182 $this->prefs['___signature___'] = $this->convert_charset($sig, $file_charset);
183 }
184
185 if (isset($this->prefs['identities']) && $this->prefs['identities'] > 1) {
186 for ($i=1; $i < $this->prefs['identities']; $i++) {
187 // read signature file if exists
188 if (is_readable($sigbase.$i)) {
189 $sig = file_get_contents($sigbase.$i);
190 $this->prefs['___sig'.$i.'___'] = $this->convert_charset($sig, $file_charset);
191 }
192 }
193 }
194
195 // parse address book file
196 if (filesize($abookfile)) {
197 foreach (file($abookfile) as $line) {
198 $line = $this->convert_charset(rtrim($line), $file_charset);
199 list($rec['name'], $rec['firstname'], $rec['surname'], $rec['email']) = explode('|', $line);
200 if ($rec['name'] && $rec['email']) {
201 $this->abook[] = $rec;
202 }
203 }
204 }
205 }
206 }
207 // Database backend
208 else if ($rcmail->config->get('squirrelmail_driver') == 'sql') {
209 $this->prefs = array();
210
211 // connect to squirrelmail database
212 $db = rcube_db::factory($rcmail->config->get('squirrelmail_dsn'));
213
214 $db->set_debug($rcmail->config->get('sql_debug'));
215 $db->db_connect('r'); // connect in read mode
216
217 // retrieve prefs
218 $userprefs_table = $rcmail->config->get('squirrelmail_userprefs_table');
219 $address_table = $rcmail->config->get('squirrelmail_address_table');
220 $db_charset = $rcmail->config->get('squirrelmail_db_charset');
221
222 if ($db_charset) {
223 $db->query('SET NAMES '.$db_charset);
224 }
225
226 $sql_result = $db->query('SELECT * FROM ' . $db->quote_identifier($userprefs_table)
227 .' WHERE `user` = ?', $uname); // ? is replaced with emailaddress
228
229 while ($sql_array = $db->fetch_assoc($sql_result) ) { // fetch one row from result
230 $this->prefs[$sql_array['prefkey']] = rcube_charset::convert(rtrim($sql_array['prefval']), $db_charset);
231 }
232
233 // retrieve address table data
234 $sql_result = $db->query('SELECT * FROM ' . $db->quote_identifier($address_table)
235 .' WHERE `owner` = ?', $uname); // ? is replaced with emailaddress
236
237 // parse address book
238 while ($sql_array = $db->fetch_assoc($sql_result) ) { // fetch one row from result
239 $rec['name'] = rcube_charset::convert(rtrim($sql_array['nickname']), $db_charset);
240 $rec['firstname'] = rcube_charset::convert(rtrim($sql_array['firstname']), $db_charset);
241 $rec['surname'] = rcube_charset::convert(rtrim($sql_array['lastname']), $db_charset);
242 $rec['email'] = rcube_charset::convert(rtrim($sql_array['email']), $db_charset);
243 $rec['notes'] = rcube_charset::convert(rtrim($sql_array['label']), $db_charset);
244
245 if ($rec['name'] && $rec['email']) {
246 $this->abook[] = $rec;
247 }
248 }
249 } // end if 'sql'-driver
250 }
251
252 private function convert_charset($str, $charset = null)
253 {
254 if (!$charset) {
255 return utf8_encode($sig);
256 }
257
258 return rcube_charset::convert($str, $charset, RCMAIL_CHARSET);
259 }
260 }