Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

new_user_identity.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * New user identity
  4. *
  5. * Populates a new user's default identity from LDAP on their first visit.
  6. *
  7. * This plugin requires that a working public_ldap directory be configured.
  8. *
  9. * @version @package_version@
  10. * @author Kris Steinhoff
  11. * @license GNU GPLv3+
  12. */
  13. class new_user_identity extends rcube_plugin
  14. {
  15. public $task = 'login';
  16. private $rc;
  17. private $ldap;
  18. function init()
  19. {
  20. $this->rc = rcmail::get_instance();
  21. $this->add_hook('user_create', array($this, 'lookup_user_name'));
  22. $this->add_hook('login_after', array($this, 'login_after'));
  23. }
  24. function lookup_user_name($args)
  25. {
  26. if ($this->init_ldap($args['host'])) {
  27. $results = $this->ldap->search('*', $args['user'], true);
  28. if (count($results->records) == 1) {
  29. $user_name = is_array($results->records[0]['name']) ? $results->records[0]['name'][0] : $results->records[0]['name'];
  30. $user_email = is_array($results->records[0]['email']) ? $results->records[0]['email'][0] : $results->records[0]['email'];
  31. $args['user_name'] = $user_name;
  32. $args['email_list'] = array();
  33. if (!$args['user_email'] && strpos($user_email, '@')) {
  34. $args['user_email'] = rcube_utils::idn_to_ascii($user_email);
  35. }
  36. foreach (array_keys($results[0]) as $key) {
  37. if (!preg_match('/^email($|:)/', $key)) {
  38. continue;
  39. }
  40. foreach ((array) $results->records[0][$key] as $alias) {
  41. if (strpos($alias, '@')) {
  42. $args['email_list'][] = rcube_utils::idn_to_ascii($alias);
  43. }
  44. }
  45. }
  46. }
  47. }
  48. return $args;
  49. }
  50. function login_after($args)
  51. {
  52. $this->load_config();
  53. if ($this->ldap || !$this->rc->config->get('new_user_identity_onlogin')) {
  54. return $args;
  55. }
  56. $identities = $this->rc->user->list_emails();
  57. $ldap_entry = $this->lookup_user_name(array(
  58. 'user' => $this->rc->user->data['username'],
  59. 'host' => $this->rc->user->data['mail_host'],
  60. ));
  61. foreach ((array) $ldap_entry['email_list'] as $email) {
  62. foreach ($identities as $identity) {
  63. if ($identity['email'] == $email) {
  64. continue 2;
  65. }
  66. }
  67. $plugin = $this->rc->plugins->exec_hook('identity_create', array(
  68. 'login' => true,
  69. 'record' => array(
  70. 'user_id' => $this->rc->user->ID,
  71. 'standard' => 0,
  72. 'email' => $email,
  73. 'name' => $ldap_entry['user_name']
  74. ),
  75. ));
  76. if (!$plugin['abort'] && $plugin['record']['email']) {
  77. $this->rc->user->insert_identity($plugin['record']);
  78. }
  79. }
  80. return $args;
  81. }
  82. private function init_ldap($host)
  83. {
  84. if ($this->ldap) {
  85. return $this->ldap->ready;
  86. }
  87. $this->load_config();
  88. $addressbook = $this->rc->config->get('new_user_identity_addressbook');
  89. $ldap_config = (array)$this->rc->config->get('ldap_public');
  90. $match = $this->rc->config->get('new_user_identity_match');
  91. if (empty($addressbook) || empty($match) || empty($ldap_config[$addressbook])) {
  92. return false;
  93. }
  94. $this->ldap = new new_user_identity_ldap_backend(
  95. $ldap_config[$addressbook],
  96. $this->rc->config->get('ldap_debug'),
  97. $this->rc->config->mail_domain($host),
  98. $match);
  99. return $this->ldap->ready;
  100. }
  101. }
  102. class new_user_identity_ldap_backend extends rcube_ldap
  103. {
  104. function __construct($p, $debug, $mail_domain, $search)
  105. {
  106. parent::__construct($p, $debug, $mail_domain);
  107. $this->prop['search_fields'] = (array)$search;
  108. }
  109. }