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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * Copy a new users identities and contacts from a nearby Squirrelmail installation
  4. *
  5. * @version 1.6
  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. $file_charset = $rcmail->config->get('squirrelmail_file_charset');
  135. $prefsfile = slashify($srcdir) . $uname . '.pref';
  136. $abookfile = slashify($srcdir) . $uname . '.abook';
  137. $sigfile = slashify($srcdir) . $uname . '.sig';
  138. $sigbase = slashify($srcdir) . $uname . '.si';
  139. if (is_readable($prefsfile)) {
  140. $this->prefs = array();
  141. foreach (file($prefsfile) as $line) {
  142. list($key, $value) = explode('=', $line);
  143. $this->prefs[$key] = $this->convert_charset(rtrim($value), $file_charset);
  144. }
  145. // also read signature file if exists
  146. if (is_readable($sigfile)) {
  147. $sig = file_get_contents($sigfile);
  148. $this->prefs['___signature___'] = $this->convert_charset($sig, $file_charset);
  149. }
  150. if (isset($this->prefs['identities']) && $this->prefs['identities'] > 1) {
  151. for ($i=1; $i < $this->prefs['identities']; $i++) {
  152. // read signature file if exists
  153. if (is_readable($sigbase.$i)) {
  154. $sig = file_get_contents($sigbase.$i);
  155. $this->prefs['___sig'.$i.'___'] = $this->convert_charset($sig, $file_charset);
  156. }
  157. }
  158. }
  159. // parse address book file
  160. if (filesize($abookfile)) {
  161. foreach (file($abookfile) as $line) {
  162. $line = $this->convert_charset(rtrim($line), $file_charset);
  163. list($rec['name'], $rec['firstname'], $rec['surname'], $rec['email']) = explode('|', $line);
  164. if ($rec['name'] && $rec['email']) {
  165. $this->abook[] = $rec;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. // Database backend
  172. else if ($rcmail->config->get('squirrelmail_driver') == 'sql') {
  173. $this->prefs = array();
  174. // connect to squirrelmail database
  175. $db = rcube_db::factory($rcmail->config->get('squirrelmail_dsn'));
  176. $db->set_debug($rcmail->config->get('sql_debug'));
  177. $db->db_connect('r'); // connect in read mode
  178. // retrieve prefs
  179. $userprefs_table = $rcmail->config->get('squirrelmail_userprefs_table');
  180. $address_table = $rcmail->config->get('squirrelmail_address_table');
  181. $db_charset = $rcmail->config->get('squirrelmail_db_charset');
  182. if ($db_charset) {
  183. $db->query('SET NAMES '.$db_charset);
  184. }
  185. $sql_result = $db->query('SELECT * FROM ' . $db->quote_identifier($userprefs_table)
  186. .' WHERE `user` = ?', $uname); // ? is replaced with emailaddress
  187. while ($sql_array = $db->fetch_assoc($sql_result) ) { // fetch one row from result
  188. $this->prefs[$sql_array['prefkey']] = rcube_charset::convert(rtrim($sql_array['prefval']), $db_charset);
  189. }
  190. // retrieve address table data
  191. $sql_result = $db->query('SELECT * FROM ' . $db->quote_identifier($address_table)
  192. .' WHERE `owner` = ?', $uname); // ? is replaced with emailaddress
  193. // parse address book
  194. while ($sql_array = $db->fetch_assoc($sql_result) ) { // fetch one row from result
  195. $rec['name'] = rcube_charset::convert(rtrim($sql_array['nickname']), $db_charset);
  196. $rec['firstname'] = rcube_charset::convert(rtrim($sql_array['firstname']), $db_charset);
  197. $rec['surname'] = rcube_charset::convert(rtrim($sql_array['lastname']), $db_charset);
  198. $rec['email'] = rcube_charset::convert(rtrim($sql_array['email']), $db_charset);
  199. $rec['notes'] = rcube_charset::convert(rtrim($sql_array['label']), $db_charset);
  200. if ($rec['name'] && $rec['email']) {
  201. $this->abook[] = $rec;
  202. }
  203. }
  204. } // end if 'sql'-driver
  205. }
  206. private function convert_charset($str, $charset = null)
  207. {
  208. if (!$charset) {
  209. return utf8_encode($sig);
  210. }
  211. return rcube_charset::convert($str, $charset, RCMAIL_CHARSET);
  212. }
  213. }