alecpl
2009-10-06 ffeab7fe7e0ccf1d458d6a863600359c38769950
commit | author | age
2a4abc 1 <?php
T 2
3 /**
4  * Copy a new users identity and settings from a nearby Squirrelmail installation
5  *
6  * Currently only file-based data storage of Squirrelmail is supported.
7  *
8  * @version 1.0
9  * @author Thomas Bruederli
10  */
11 class squirrelmail_usercopy extends rcube_plugin
12 {
13     private $prefs = null;
14     private $abook = array();
15
16     public function init()
17     {
18         $this->add_hook('create_user', array($this, 'create_user'));
19         $this->add_hook('create_identity', array($this, 'create_identity'));
20     }
21
22     public function create_user($p)
23     {
24         // read prefs and add email address
25         $this->read_squirrel_prefs($p['user']);
26         if ($this->prefs['email_address'])
27             $p['user_email'] = $this->prefs['email_address'];
28
29         return $p;
30     }
31
32     public function create_identity($p)
33     {
34         // only execute on login
35         if ($p['login'] && $this->prefs) {
36             if ($this->prefs['full_name'])
37                 $p['record']['name'] = $this->prefs['full_name'];
38             if ($this->prefs['email_address'])
39                 $p['record']['email'] = $this->prefs['email_address'];
40             if ($this->prefs['signature'])
41                 $p['record']['signature'] = $this->prefs['signature'];
42         
43             // copy address book
44             $rcmail = rcmail::get_instance();
45             $contacts = $rcmail->get_address_book(null, true);
46             if ($contacts && count($this->abook)) {
47                 foreach ($this->abook as $rec)
48                     $contacts->insert($rec, true);
5edb5b 49             }
T 50             
51             // mark identity as complete for following hooks
52             $p['complete'] = true;
2a4abc 53         }
T 54
55         return $p;
56     }
57
58     private function read_squirrel_prefs($uname)
59     {
60         $this->load_config();
61         $rcmail = rcmail::get_instance();
62
63         if ($srcdir = $rcmail->config->get('squirrelmail_data_dir')) {
64             $prefsfile = slashify($srcdir) . $uname . '.pref';
65             $abookfile = slashify($srcdir) . $uname . '.abook';
66             $sigfile = slashify($srcdir) . $uname . '.sig';
67
68             if (is_readable($prefsfile)) {
69                 $this->prefs = array();
70                 foreach (file($prefsfile) as $line) {
71                     list($key, $value) = explode('=', $line);
72                     $this->prefs[$key] = utf8_encode(rtrim($value));
73                 }
74
75                 // also read signature file if exists
76                 if (is_readable($sigfile)) {
77                     $this->prefs['signature'] = utf8_encode(file_get_contents($sigfile));
78                 }
79
80                 // parse addres book file
81                 if (filesize($abookfile)) {
82                     foreach(file($abookfile) as $line) {
83                         list($rec['name'], $rec['firstname'], $rec['surname'], $rec['email']) = explode('|', utf8_encode(rtrim($line)));
84                         if ($rec['name'] && $rec['email'])
85                             $this->abook[] = $rec;
86                     }
87                 }
88             }
89         }
90     }
91
92 }
93
94 ?>