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

Last change on this file since 3867 was 3867, checked in by alec, 3 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Date Revision
File size: 6.3 KB
Line 
1<?php
2
3/**
4 * Copy a new users identity and settings from a nearby Squirrelmail installation
5 *
6 * @version 1.3
7 * @author Thomas Bruederli, Johannes Hessellund, pommi, Thomas Lueder
8 */
9class squirrelmail_usercopy extends rcube_plugin
10{
11        public $task = 'login|settings';
12
13        private $prefs = null;
14        private $identities_level = 0;
15        private $abook = array();
16
17        public function init()
18        {
19                $rcmail = rcmail::get_instance();
20
21        // Set identities_level for operations of this plugin
22                $ilevel = $rcmail->config->get('squirrelmail_identities_level');
23        if ($ilevel === null)
24                    $ilevel = $rcmail->config->get('identities_level', 0);
25        $this->identities_level = intval($ilevel);
26
27                $this->add_hook('user_create', array($this, 'create_user'));
28                $this->add_hook('identity_create', array($this, 'create_identity'));
29        }
30
31        public function create_user($p)
32        {
33                // read prefs and add email address
34                $this->read_squirrel_prefs($p['user']);
35                if (($this->identities_level == 0 || $this->identities_level == 2) && $this->prefs['email_address'])
36                        $p['user_email'] = $this->prefs['email_address'];
37                return $p;
38        }
39
40        public function create_identity($p)
41        {
42                $rcmail = rcmail::get_instance();
43
44                // only execute on login
45                if ($rcmail->task == 'login' && $this->prefs) {
46                        if ($this->prefs['full_name'])
47                                $p['record']['name'] = $this->prefs['full_name'];
48                        if (($this->identities_level == 0 || $this->identities_level == 2) && $this->prefs['email_address'])
49                                $p['record']['email'] = $this->prefs['email_address'];
50                        if ($this->prefs['___signature___'])
51                                $p['record']['signature'] = $this->prefs['___signature___'];
52                        if ($this->prefs['reply-to']) 
53                                $p['record']['reply-to'] = $this->prefs['reply-to']; 
54                        if (($this->identities_level == 0 || $this->identities_level == 1) && isset($this->prefs['identities']) && $this->prefs['identities'] > 1) {
55                                for ($i=1; $i < $this->prefs['identities']; $i++) {
56                                        unset($ident_data);
57                                        $ident_data = array('name' => '', 'email' => ''); // required data
58                                        if ($this->prefs['full_name'.$i])
59                                                $ident_data['name'] = $this->prefs['full_name'.$i];
60                                        if ($this->identities_level == 0 && $this->prefs['email_address'.$i])
61                                                $ident_data['email'] = $this->prefs['email_address'.$i];
62                                        else
63                                                $ident_data['email'] = $p['record']['email'];
64                                        if ($this->prefs['reply_to'.$i])
65                                                $ident_data['reply-to'] = $this->prefs['reply_to'.$i];
66                                        if ($this->prefs['___sig'.$i.'___'])
67                                                $ident_data['signature'] = $this->prefs['___sig'.$i.'___'];
68                                        // insert identity
69                                        $identid = $rcmail->user->insert_identity($ident_data);
70                                }
71                        }
72
73                        // copy address book
74                        $contacts = $rcmail->get_address_book(null, true);
75                        if ($contacts && count($this->abook)) {
76                                foreach ($this->abook as $rec)
77                                        $contacts->insert($rec, true);
78                        }
79
80                        // mark identity as complete for following hooks
81                        $p['complete'] = true;
82                }
83
84                return $p;
85        }
86
87        private function read_squirrel_prefs($uname)
88        {
89                $this->load_config();
90                $rcmail = rcmail::get_instance();
91
92                /**** File based backend ****/
93                if ($rcmail->config->get('squirrelmail_driver') == 'file' && ($srcdir = $rcmail->config->get('squirrelmail_data_dir'))) {
94                        if (($hash_level = $rcmail->config->get('squirrelmail_data_dir_hash_level')) > 0) 
95                                $srcdir = slashify($srcdir).chunk_split(substr(base_convert(crc32($uname), 10, 16), 0, $hash_level), 1, '/');
96                        $prefsfile = slashify($srcdir) . $uname . '.pref';
97                        $abookfile = slashify($srcdir) . $uname . '.abook';
98                        $sigfile = slashify($srcdir) . $uname . '.sig';
99                        $sigbase = slashify($srcdir) . $uname . '.si';
100
101                        if (is_readable($prefsfile)) {
102                                $this->prefs = array();
103                                foreach (file($prefsfile) as $line) {
104                                        list($key, $value) = explode('=', $line);
105                                        $this->prefs[$key] = utf8_encode(rtrim($value));
106                                }
107
108                                // also read signature file if exists
109                                if (is_readable($sigfile)) {
110                                        $this->prefs['___signature___'] = utf8_encode(file_get_contents($sigfile));
111                                }
112
113                                if (isset($this->prefs['identities']) && $this->prefs['identities'] > 1) {
114                                        for ($i=1; $i < $this->prefs['identities']; $i++) {
115                                                // read signature file if exists
116                                                if (is_readable($sigbase.$i)) {
117                                                        $this->prefs['___sig'.$i.'___'] = utf8_encode(file_get_contents($sigbase.$i));
118                                                }
119                                        }
120                                }
121
122                                // parse addres book file
123                                if (filesize($abookfile)) {
124                                        foreach(file($abookfile) as $line) {
125                                                list($rec['name'], $rec['firstname'], $rec['surname'], $rec['email']) = explode('|', utf8_encode(rtrim($line)));
126                                                if ($rec['name'] && $rec['email'])
127                                                        $this->abook[] = $rec;
128                                        }
129                                }
130                        }
131                } 
132                /**** Database backend ****/
133                else if ($rcmail->config->get('squirrelmail_driver') == 'sql') { 
134                        $this->prefs = array();
135
136                        /* connect to squirrelmail database */
137                        $db = new rcube_mdb2($rcmail->config->get('squirrelmail_dsn'));
138                        $db->db_connect('r'); // connect in read mode
139
140                        // $db->set_debug(true);
141
142                        /* retrieve prefs */
143                        $userprefs_table = $rcmail->config->get('squirrelmail_userprefs_table');
144                        $address_table = $rcmail->config->get('squirrelmail_address_table');
145                        $db_charset = $rcmail->config->get('squirrelmail_db_charset');
146
147                        $db->query('SET CHARACTER SET '.$db_charset);
148                        $db->query('SET NAMES '.$db_encoding);
149
150                        $sql_result = $db->query('SELECT * FROM '.$userprefs_table.' WHERE user=?', $uname); // ? is replaced with emailaddress
151
152                        while ($sql_array = $db->fetch_assoc($sql_result) ) { // fetch one row from result
153                                $this->prefs[$sql_array['prefkey']] = rcube_charset_convert(rtrim($sql_array['prefval']), $db_charset);
154                        }
155
156                        /* retrieve address table data */
157                        $sql_result = $db->query('SELECT * FROM '.$address_table.' WHERE owner=?', $uname); // ? is replaced with emailaddress
158
159                        // parse addres book
160                        while ($sql_array = $db->fetch_assoc($sql_result) ) { // fetch one row from result
161                                $rec['name']      = rcube_charset_convert(rtrim($sql_array['nickname']), $db_charset);
162                                $rec['firstname'] = rcube_charset_convert(rtrim($sql_array['firstname']), $db_charset);
163                                $rec['surname']   = rcube_charset_convert(rtrim($sql_array['lastname']), $db_charset);
164                                $rec['email']     = rcube_charset_convert(rtrim($sql_array['email']), $db_charset);
165                                $rec['note']      = rcube_charset_convert(rtrim($sql_array['label']), $db_charset);
166
167                                if ($rec['name'] && $rec['email'])
168                                        $this->abook[] = $rec;
169                        }
170                } // end if 'sql'-driver
171        }
172
173}
Note: See TracBrowser for help on using the repository browser.