You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

squirrelmail_usercopy.php 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * Copy a new users identities and contacts from a nearby Squirrelmail installation
  4. *
  5. * @version 1.5
  6. * @author Thomas Bruederli, Johannes Hessellund, pommi, Thomas Lueder
  7. */
  8. class squirrelmail_usercopy extends rcube_plugin
  9. {
  10. public $task = 'login';
  11. private $prefs = null;
  12. private $identities_level = 0;
  13. private $abook = array();
  14. public function init()
  15. {
  16. $this->add_hook('user_create', array($this, 'create_user'));
  17. $this->add_hook('identity_create', array($this, 'create_identity'));
  18. }
  19. public function create_user($p)
  20. {
  21. $rcmail = rcmail::get_instance();
  22. // Read plugin's config
  23. $this->initialize();
  24. // read prefs and add email address
  25. $this->read_squirrel_prefs($p['user']);
  26. if (($this->identities_level == 0 || $this->identities_level == 2)
  27. && $rcmail->config->get('squirrelmail_set_alias')
  28. && $this->prefs['email_address']
  29. ) {
  30. $p['user_email'] = $this->prefs['email_address'];
  31. }
  32. return $p;
  33. }
  34. public function create_identity($p)
  35. {
  36. $rcmail = rcmail::get_instance();
  37. // prefs are set in create_user()
  38. if ($this->prefs) {
  39. if ($this->prefs['full_name']) {
  40. $p['record']['name'] = $this->prefs['full_name'];
  41. }
  42. if (($this->identities_level == 0 || $this->identities_level == 2) && $this->prefs['email_address']) {
  43. $p['record']['email'] = $this->prefs['email_address'];
  44. }
  45. if ($this->prefs['___signature___']) {
  46. $p['record']['signature'] = $this->prefs['___signature___'];
  47. }
  48. if ($this->prefs['reply_to']) {
  49. $p['record']['reply-to'] = $this->prefs['reply_to'];
  50. }
  51. if (($this->identities_level == 0 || $this->identities_level == 1)
  52. && isset($this->prefs['identities']) && $this->prefs['identities'] > 1
  53. ) {
  54. for ($i = 1; $i < $this->prefs['identities']; $i++) {
  55. unset($ident_data);
  56. $ident_data = array('name' => '', 'email' => ''); // required data
  57. if ($this->prefs['full_name'.$i]) {
  58. $ident_data['name'] = $this->prefs['full_name'.$i];
  59. }
  60. if ($this->identities_level == 0 && $this->prefs['email_address'.$i]) {
  61. $ident_data['email'] = $this->prefs['email_address'.$i];
  62. }
  63. else {
  64. $ident_data['email'] = $p['record']['email'];
  65. }
  66. if ($this->prefs['reply_to'.$i]) {
  67. $ident_data['reply-to'] = $this->prefs['reply_to'.$i];
  68. }
  69. if ($this->prefs['___sig'.$i.'___']) {
  70. $ident_data['signature'] = $this->prefs['___sig'.$i.'___'];
  71. }
  72. // insert identity
  73. $rcmail->user->insert_identity($ident_data);
  74. }
  75. }
  76. // copy address book
  77. $contacts = $rcmail->get_address_book(null, true);
  78. $addresses = array();
  79. $groups = array();
  80. if ($contacts && !empty($this->abook)) {
  81. foreach ($this->abook as $rec) {
  82. // #1487096: handle multi-address and/or too long items
  83. // #1487858: convert multi-address contacts into groups
  84. $emails = preg_split('/[;,]/', $rec['email'], -1, PREG_SPLIT_NO_EMPTY);
  85. $group_id = null;
  86. // create group for addresses
  87. if (count($emails) > 1) {
  88. if (!($group_id = $groups[$rec['name']])) {
  89. if ($group = $contacts->create_group($rec['name'])) {
  90. $group_id = $group['id'];
  91. $groups[$rec['name']] = $group_id;
  92. }
  93. }
  94. }
  95. // create contacts
  96. foreach ($emails as $email) {
  97. if (!($contact_id = $addresses[$email]) && rcube_utils::check_email(rcube_utils::idn_to_ascii($email))) {
  98. $rec['email'] = rcube_utils::idn_to_utf8($email);
  99. if ($contact_id = $contacts->insert($rec, true)) {
  100. $addresses[$email] = $contact_id;
  101. }
  102. }
  103. if ($group_id && $contact_id) {
  104. $contacts->add_to_group($group_id, array($contact_id));
  105. }
  106. }
  107. }
  108. }
  109. // mark identity as complete for following hooks
  110. $p['complete'] = true;
  111. }
  112. return $p;
  113. }
  114. private function initialize()
  115. {
  116. $rcmail = rcmail::get_instance();
  117. // Load plugin's config file
  118. $this->load_config();
  119. // Set identities_level for operations of this plugin
  120. $ilevel = $rcmail->config->get('squirrelmail_identities_level');
  121. if ($ilevel === null) {
  122. $ilevel = $rcmail->config->get('identities_level', 0);
  123. }
  124. $this->identities_level = intval($ilevel);
  125. }
  126. private function read_squirrel_prefs($uname)
  127. {
  128. $rcmail = rcmail::get_instance();
  129. /**** File based backend ****/
  130. if ($rcmail->config->get('squirrelmail_driver') == 'file' && ($srcdir = $rcmail->config->get('squirrelmail_data_dir'))) {
  131. if (($hash_level = $rcmail->config->get('squirrelmail_data_dir_hash_level')) > 0) {
  132. $srcdir = slashify($srcdir).chunk_split(substr(base_convert(crc32($uname), 10, 16), 0, $hash_level), 1, '/');
  133. }
  134. $prefsfile = slashify($srcdir) . $uname . '.pref';
  135. $abookfile = slashify($srcdir) . $uname . '.abook';
  136. $sigfile = slashify($srcdir) . $uname . '.sig';
  137. $sigbase = slashify($srcdir) . $uname . '.si';
  138. if (is_readable($prefsfile)) {
  139. $this->prefs = array();
  140. foreach (file($prefsfile) as $line) {
  141. list($key, $value) = explode('=', $line);
  142. $this->prefs[$key] = utf8_encode(rtrim($value));
  143. }
  144. // also read signature file if exists
  145. if (is_readable($sigfile)) {
  146. $this->prefs['___signature___'] = utf8_encode(file_get_contents($sigfile));
  147. }
  148. if (isset($this->prefs['identities']) && $this->prefs['identities'] > 1) {
  149. for ($i=1; $i < $this->prefs['identities']; $i++) {
  150. // read signature file if exists
  151. if (is_readable($sigbase.$i)) {
  152. $this->prefs['___sig'.$i.'___'] = utf8_encode(file_get_contents($sigbase.$i));
  153. }
  154. }
  155. }
  156. // parse addres book file
  157. if (filesize($abookfile)) {
  158. foreach(file($abookfile) as $line) {
  159. list($rec['name'], $rec['firstname'], $rec['surname'], $rec['email']) = explode('|', utf8_encode(rtrim($line)));
  160. if ($rec['name'] && $rec['email']) {
  161. $this->abook[] = $rec;
  162. }
  163. }
  164. }
  165. }
  166. }
  167. // Database backend
  168. else if ($rcmail->config->get('squirrelmail_driver') == 'sql') {
  169. $this->prefs = array();
  170. // connect to squirrelmail database
  171. $db = rcube_db::factory($rcmail->config->get('squirrelmail_dsn'));
  172. $db->set_debug($rcmail->config->get('sql_debug'));
  173. $db->db_connect('r'); // connect in read mode
  174. // retrieve prefs
  175. $userprefs_table = $rcmail->config->get('squirrelmail_userprefs_table');
  176. $address_table = $rcmail->config->get('squirrelmail_address_table');
  177. $db_charset = $rcmail->config->get('squirrelmail_db_charset');
  178. if ($db_charset) {
  179. $db->query('SET NAMES '.$db_charset);
  180. }
  181. $sql_result = $db->query('SELECT * FROM ' . $db->quote_identifier($userprefs_table)
  182. .' WHERE `user` = ?', $uname); // ? is replaced with emailaddress
  183. while ($sql_array = $db->fetch_assoc($sql_result) ) { // fetch one row from result
  184. $this->prefs[$sql_array['prefkey']] = rcube_charset::convert(rtrim($sql_array['prefval']), $db_charset);
  185. }
  186. // retrieve address table data
  187. $sql_result = $db->query('SELECT * FROM ' . $db->quote_identifier($address_table)
  188. .' WHERE `owner` = ?', $uname); // ? is replaced with emailaddress
  189. // parse addres book
  190. while ($sql_array = $db->fetch_assoc($sql_result) ) { // fetch one row from result
  191. $rec['name'] = rcube_charset::convert(rtrim($sql_array['nickname']), $db_charset);
  192. $rec['firstname'] = rcube_charset::convert(rtrim($sql_array['firstname']), $db_charset);
  193. $rec['surname'] = rcube_charset::convert(rtrim($sql_array['lastname']), $db_charset);
  194. $rec['email'] = rcube_charset::convert(rtrim($sql_array['email']), $db_charset);
  195. $rec['notes'] = rcube_charset::convert(rtrim($sql_array['label']), $db_charset);
  196. if ($rec['name'] && $rec['email']) {
  197. $this->abook[] = $rec;
  198. }
  199. }
  200. } // end if 'sql'-driver
  201. }
  202. }