source: subversion/trunk/plugins/squirrelmail_usercopy/squirrelmail_usercopy.php @ 4485

Last change on this file since 4485 was 4485, checked in by alec, 2 years ago
  • Fix IDNA support when IDN/INTL modules are in use (#1487742)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Date Revision
File size: 6.7 KB
Line 
1<?php
2
3/**
4 * Copy a new users identity and settings from a nearby Squirrelmail installation
5 *
6 * @version 1.4
7 * @author Thomas Bruederli, Johannes Hessellund, pommi, Thomas Lueder
8 */
9class 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) && $rcmail->config->get('squirrelmail_set_alias') && $this->prefs['email_address'])
33                        $p['user_email'] = $this->prefs['email_address'];
34                return $p;
35        }
36
37        public function create_identity($p)
38        {
39                $rcmail = rcmail::get_instance();
40
41                // prefs are set in create_user()
42                if ($this->prefs) {
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++) {
53                                        unset($ident_data);
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];
59                                        else
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.'___'];
65                                        // insert identity
66                                        $identid = $rcmail->user->insert_identity($ident_data);
67                                }
68                        }
69
70                        // copy address book
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(rcube_idn_to_ascii($rec['email']))) {
77                        $rec['email'] = rcube_idn_to_utf8($rec['email']);
78                                        $contacts->insert($rec, true);
79                                }
80                            }
81                        }
82
83                        // mark identity as complete for following hooks
84                        $p['complete'] = true;
85                }
86
87                return $p;
88        }
89
90        private function initialize()
91        {
92                $rcmail = rcmail::get_instance();
93
94                // Load plugin's config file
95                $this->load_config();
96
97                // Set identities_level for operations of this plugin
98                $ilevel = $rcmail->config->get('squirrelmail_identities_level');
99                if ($ilevel === null)
100                        $ilevel = $rcmail->config->get('identities_level', 0);
101
102                $this->identities_level = intval($ilevel);
103        }
104
105        private function read_squirrel_prefs($uname)
106        {
107                $rcmail = rcmail::get_instance();
108
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';
117
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));
123                                }
124
125                                // also read signature file if exists
126                                if (is_readable($sigfile)) {
127                                        $this->prefs['___signature___'] = utf8_encode(file_get_contents($sigfile));
128                                }
129
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));
135                                                }
136                                        }
137                                }
138
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;
145                                        }
146                                }
147                        }
148                } 
149                /**** Database backend ****/
150                else if ($rcmail->config->get('squirrelmail_driver') == 'sql') { 
151                        $this->prefs = array();
152
153                        /* connect to squirrelmail database */
154                        $db = new rcube_mdb2($rcmail->config->get('squirrelmail_dsn'));
155                        $db->db_connect('r'); // connect in read mode
156
157                        // $db->set_debug(true);
158
159                        /* retrieve prefs */
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');
163
164                        if ($db_charset)
165                                $db->query('SET NAMES '.$db_charset);
166
167                        $sql_result = $db->query('SELECT * FROM '.$userprefs_table.' WHERE user=?', $uname); // ? is replaced with emailaddress
168
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);
171                        }
172
173                        /* retrieve address table data */
174                        $sql_result = $db->query('SELECT * FROM '.$address_table.' WHERE owner=?', $uname); // ? is replaced with emailaddress
175
176                        // parse addres book
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);
183
184                                if ($rec['name'] && $rec['email'])
185                                        $this->abook[] = $rec;
186                        }
187                } // end if 'sql'-driver
188        }
189
190}
Note: See TracBrowser for help on using the repository browser.