Aleksander Machniak
2016-05-16 0b7e26c1bf6bc7a684eb3a214d92d3927306cd8a
commit | author | age
48e9c1 1 <?php
T 2
3 /**
86e97e 4  * Copy a new users identities and contacts from a nearby Squirrelmail installation
48e9c1 5  *
86e97e 6  * @version 1.5
48e9c1 7  * @author Thomas Bruederli, Johannes Hessellund, pommi, Thomas Lueder
T 8  */
9 class squirrelmail_usercopy extends rcube_plugin
10 {
09c252 11     public $task = 'login';
48e9c1 12
09c252 13     private $prefs            = null;
AM 14     private $identities_level = 0;
15     private $abook            = array();
48e9c1 16
09c252 17     public function init()
AM 18     {
19         $this->add_hook('user_create', array($this, 'create_user'));
20         $this->add_hook('identity_create', array($this, 'create_identity'));
21     }
48e9c1 22
09c252 23     public function create_user($p)
AM 24     {
25         $rcmail = rcmail::get_instance();
48e9c1 26
09c252 27         // Read plugin's config
AM 28         $this->initialize();
48e9c1 29
09c252 30         // read prefs and add email address
AM 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         }
48e9c1 38
09c252 39         return $p;
AM 40     }
48e9c1 41
09c252 42     public function create_identity($p)
AM 43     {
44         $rcmail = rcmail::get_instance();
48e9c1 45
09c252 46         // prefs are set in create_user()
AM 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
86e97e 96             $contacts  = $rcmail->get_address_book(null, true);
AM 97             $addresses = array();
98             $groups    = array();
99
100             if ($contacts && !empty($this->abook)) {
09c252 101                 foreach ($this->abook as $rec) {
86e97e 102                     // #1487096: handle multi-address and/or too long items
AM 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                         }
09c252 129                     }
AM 130                 }
131             }
48e9c1 132
09c252 133             // mark identity as complete for following hooks
AM 134             $p['complete'] = true;
135         }
48e9c1 136
09c252 137         return $p;
AM 138     }
48e9c1 139
09c252 140     private function initialize()
AM 141     {
142         $rcmail = rcmail::get_instance();
48e9c1 143
09c252 144         // Load plugin's config file
AM 145         $this->load_config();
48e9c1 146
09c252 147         // Set identities_level for operations of this plugin
AM 148         $ilevel = $rcmail->config->get('squirrelmail_identities_level');
149         if ($ilevel === null) {
150             $ilevel = $rcmail->config->get('identities_level', 0);
151         }
48e9c1 152
09c252 153         $this->identities_level = intval($ilevel);
AM 154     }
48e9c1 155
09c252 156     private function read_squirrel_prefs($uname)
AM 157     {
158         $rcmail = rcmail::get_instance();
48e9c1 159
09c252 160         /**** File based backend ****/
AM 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             }
48e9c1 165
09c252 166             $prefsfile = slashify($srcdir) . $uname . '.pref';
AM 167             $abookfile = slashify($srcdir) . $uname . '.abook';
168             $sigfile   = slashify($srcdir) . $uname . '.sig';
169             $sigbase   = slashify($srcdir) . $uname . '.si';
48e9c1 170
09c252 171             if (is_readable($prefsfile)) {
AM 172                 $this->prefs = array();
173                 foreach (file($prefsfile) as $line) {
174                     list($key, $value) = explode('=', $line);
175                     $this->prefs[$key] = utf8_encode(rtrim($value));
176                 }
48e9c1 177
09c252 178                 // also read signature file if exists
AM 179                 if (is_readable($sigfile)) {
180                     $this->prefs['___signature___'] = utf8_encode(file_get_contents($sigfile));
181                 }
48e9c1 182
09c252 183                 if (isset($this->prefs['identities']) && $this->prefs['identities'] > 1) {
AM 184                     for ($i=1; $i < $this->prefs['identities']; $i++) {
185                         // read signature file if exists
186                         if (is_readable($sigbase.$i)) {
187                             $this->prefs['___sig'.$i.'___'] = utf8_encode(file_get_contents($sigbase.$i));
188                         }
189                     }
190                 }
48e9c1 191
09c252 192                 // parse addres book file
AM 193                 if (filesize($abookfile)) {
194                     foreach(file($abookfile) as $line) {
195                         list($rec['name'], $rec['firstname'], $rec['surname'], $rec['email']) = explode('|', utf8_encode(rtrim($line)));
196                         if ($rec['name'] && $rec['email']) {
197                             $this->abook[] = $rec;
198                         }
199                     }
200                 }
201             }
202         }
203         // Database backend
204         else if ($rcmail->config->get('squirrelmail_driver') == 'sql') { 
205             $this->prefs = array();
48e9c1 206
09c252 207             // connect to squirrelmail database
AM 208             $db = rcube_db::factory($rcmail->config->get('squirrelmail_dsn'));
48e9c1 209
09c252 210             $db->set_debug($rcmail->config->get('sql_debug'));
AM 211             $db->db_connect('r'); // connect in read mode
48e9c1 212
09c252 213             // retrieve prefs
AM 214             $userprefs_table = $rcmail->config->get('squirrelmail_userprefs_table');
215             $address_table   = $rcmail->config->get('squirrelmail_address_table');
216             $db_charset      = $rcmail->config->get('squirrelmail_db_charset');
48e9c1 217
09c252 218             if ($db_charset) {
AM 219                 $db->query('SET NAMES '.$db_charset);
220             }
48e9c1 221
09c252 222             $sql_result = $db->query('SELECT * FROM ' . $db->quote_identifier($userprefs_table)
AM 223                 .' WHERE `user` = ?', $uname); // ? is replaced with emailaddress
48e9c1 224
09c252 225             while ($sql_array = $db->fetch_assoc($sql_result) ) { // fetch one row from result
AM 226                 $this->prefs[$sql_array['prefkey']] = rcube_charset::convert(rtrim($sql_array['prefval']), $db_charset);
227             }
48e9c1 228
09c252 229             // retrieve address table data
AM 230             $sql_result = $db->query('SELECT * FROM ' . $db->quote_identifier($address_table)
231                 .' WHERE `owner` = ?', $uname); // ? is replaced with emailaddress
48e9c1 232
09c252 233             // parse addres book
AM 234             while ($sql_array = $db->fetch_assoc($sql_result) ) { // fetch one row from result
235                 $rec['name']      = rcube_charset::convert(rtrim($sql_array['nickname']), $db_charset);
236                 $rec['firstname'] = rcube_charset::convert(rtrim($sql_array['firstname']), $db_charset);
237                 $rec['surname']   = rcube_charset::convert(rtrim($sql_array['lastname']), $db_charset);
238                 $rec['email']     = rcube_charset::convert(rtrim($sql_array['email']), $db_charset);
239                 $rec['notes']     = rcube_charset::convert(rtrim($sql_array['label']), $db_charset);
240
241                 if ($rec['name'] && $rec['email']) {
242                     $this->abook[] = $rec;
243                 }
244             }
245         } // end if 'sql'-driver
246     }
48e9c1 247 }