4 * Copy a new users identity and settings from a nearby Squirrelmail installation
6 * Currently only file-based data storage of Squirrelmail is supported.
9 * @author Thomas Bruederli
11 class squirrelmail_usercopy extends rcube_plugin
13 private $prefs = null;
14 private $abook = array();
16 public function init()
18 $this->add_hook('create_user', array($this, 'create_user'));
19 $this->add_hook('create_identity', array($this, 'create_identity'));
22 public function create_user($p)
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'];
32 public function create_identity($p)
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'];
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);
51 // mark identity as complete for following hooks
52 $p['complete'] = true;
58 private function read_squirrel_prefs($uname)
61 $rcmail = rcmail::get_instance();
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';
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));
75 // also read signature file if exists
76 if (is_readable($sigfile)) {
77 $this->prefs['signature'] = utf8_encode(file_get_contents($sigfile));
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;