| | |
| | | /** |
| | | * Object constructor |
| | | * |
| | | * @param array LDAP connection properties |
| | | * @param boolean Enables debug mode |
| | | * @param string Current user mail domain name |
| | | * @param integer User-ID |
| | | * @param array $p LDAP connection properties |
| | | * @param boolean $debug Enables debug mode |
| | | * @param string $mail_domain Current user mail domain name |
| | | */ |
| | | function __construct($p, $debug=false, $mail_domain=NULL) |
| | | function __construct($p, $debug = false, $mail_domain = null) |
| | | { |
| | | $this->prop = $p; |
| | | |
| | |
| | | */ |
| | | private function _connect() |
| | | { |
| | | global $RCMAIL; |
| | | $RCMAIL = rcmail::get_instance(); |
| | | |
| | | if (!function_exists('ldap_connect')) |
| | | raise_error(array('code' => 100, 'type' => 'ldap', |
| | | rcube::raise_error(array('code' => 100, 'type' => 'ldap', |
| | | 'file' => __FILE__, 'line' => __LINE__, |
| | | 'message' => "No ldap support in this installation of PHP"), |
| | | true, true); |
| | |
| | | |
| | | foreach ($this->prop['hosts'] as $host) |
| | | { |
| | | $host = idn_to_ascii(rcube_parse_host($host)); |
| | | $host = idn_to_ascii(rcmail::parse_host($host)); |
| | | $hostname = $host.($this->prop['port'] ? ':'.$this->prop['port'] : ''); |
| | | |
| | | $this->_debug("C: Connect [$hostname] [{$this->prop['name']}]"); |
| | |
| | | } |
| | | |
| | | if (!is_resource($this->conn)) { |
| | | raise_error(array('code' => 100, 'type' => 'ldap', |
| | | rcube::raise_error(array('code' => 100, 'type' => 'ldap', |
| | | 'file' => __FILE__, 'line' => __LINE__, |
| | | 'message' => "Could not connect to any LDAP server, last tried $hostname"), true); |
| | | |
| | |
| | | } |
| | | |
| | | // Get the pieces needed for variable replacement. |
| | | if ($fu = $RCMAIL->user->get_username()) |
| | | if ($fu = $RCMAIL->get_user_name()) |
| | | list($u, $d) = explode('@', $fu); |
| | | else |
| | | $d = $this->mail_domain; |
| | |
| | | if (!empty($this->prop['search_dn_default'])) |
| | | $replaces['%dn'] = $this->prop['search_dn_default']; |
| | | else { |
| | | raise_error(array( |
| | | rcube::raise_error(array( |
| | | 'code' => 100, 'type' => 'ldap', |
| | | 'file' => __FILE__, 'line' => __LINE__, |
| | | 'message' => "DN not found using LDAP search."), true); |
| | |
| | | } |
| | | |
| | | if (!function_exists('ldap_sasl_bind')) { |
| | | raise_error(array('code' => 100, 'type' => 'ldap', |
| | | rcube::raise_error(array('code' => 100, 'type' => 'ldap', |
| | | 'file' => __FILE__, 'line' => __LINE__, |
| | | 'message' => "Unable to bind: ldap_sasl_bind() not exists"), |
| | | true, true); |
| | |
| | | |
| | | $this->_debug("S: ".ldap_error($this->conn)); |
| | | |
| | | raise_error(array( |
| | | rcube::raise_error(array( |
| | | 'code' => ldap_errno($this->conn), 'type' => 'ldap', |
| | | 'file' => __FILE__, 'line' => __LINE__, |
| | | 'message' => "Bind failed for authcid=$authc ".ldap_error($this->conn)), |
| | |
| | | |
| | | $this->_debug("S: ".ldap_error($this->conn)); |
| | | |
| | | raise_error(array( |
| | | rcube::raise_error(array( |
| | | 'code' => ldap_errno($this->conn), 'type' => 'ldap', |
| | | 'file' => __FILE__, 'line' => __LINE__, |
| | | 'message' => "Bind failed for dn=$dn: ".ldap_error($this->conn)), |
| | |
| | | if ($missing) { |
| | | // try to complete record automatically |
| | | if ($autofix) { |
| | | $reverse_map = array_flip($this->fieldmap); |
| | | $name_parts = preg_split('/[\s,.]+/', $save_data['name']); |
| | | $sn_field = $this->fieldmap['surname']; |
| | | $fn_field = $this->fieldmap['firstname']; |
| | | $mail_field = $this->fieldmap['email']; |
| | | |
| | | // try to extract surname and firstname from displayname |
| | | $reverse_map = array_flip($this->fieldmap); |
| | | $name_parts = preg_split('/[\s,.]+/', $save_data['name']); |
| | | |
| | | if ($sn_field && $missing[$sn_field]) { |
| | | $save_data['surname'] = array_pop($name_parts); |
| | |
| | | if ($fn_field && $missing[$fn_field]) { |
| | | $save_data['firstname'] = array_shift($name_parts); |
| | | unset($missing[$fn_field]); |
| | | } |
| | | |
| | | // try to fix missing e-mail, very often on import |
| | | // from vCard we have email:other only defined |
| | | if ($mail_field && $missing[$mail_field]) { |
| | | $emails = $this->get_col_values('email', $save_data, true); |
| | | if (!empty($emails) && ($email = array_shift($emails))) { |
| | | $save_data['email'] = $email; |
| | | unset($missing[$mail_field]); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | private function _debug($str) |
| | | { |
| | | if ($this->debug) |
| | | write_log('ldap', $str); |
| | | if ($this->debug) { |
| | | rcmail::write_log('ldap', $str); |
| | | } |
| | | } |
| | | |
| | | |