Page MenuHomePhorge

No OneTemporary

Size
3 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/Kolab/FreeBusy/DirectoryLDAP.php b/lib/Kolab/FreeBusy/DirectoryLDAP.php
index ae80bfc..1c675f9 100644
--- a/lib/Kolab/FreeBusy/DirectoryLDAP.php
+++ b/lib/Kolab/FreeBusy/DirectoryLDAP.php
@@ -1,52 +1,111 @@
<?php
namespace Kolab\FreeBusy;
// PEAR modules operate in global namespace
use \Net_LDAP3;
+use \Monolog\Logger as Monolog;
/**
*
*/
class DirectoryLDAP extends Directory
{
private $ldap;
private $logger;
+ private $ready = false;
/**
* Default constructor loading directory configuration
*/
public function __construct($config)
{
+ $this->config = $config;
+
+ $host = parse_url($config['host']);
$ldap_config = array(
-
- );
+ 'hosts' => array($host['host']),
+ 'port' => $host['port'] ?: 389,
+ 'use_tls' => $host['scheme'] == 'tls' || $host['scheme'] == 'ldaps',
+ 'root_dn' => $config['base_dn'],
+ 'return_attributes' => (array)$config['attributes'],
+ 'sizelimit' => 0,
+ 'timelimit' => 0,
+ ) + $config;
- $this->logger = Logger::get('ldap');
+ // instantiate Net_LDAP3 and connect with logger
+ $this->logger = Logger::get('ldap', $config['loglevel']);
$this->ldap = new Net_LDAP3($ldap_config);
$this->ldap->config_set('log_hook', array($this, 'log'));
- $this->ldap->connect();
+ $this->ldap->config_set('return_attributes', (array)$config['attributes']);
+
+ // connect + bind to LDAP server
+ if ($this->ldap->connect()) {
+ $this->ready = $this->ldap->bind($config['bind_dn'], $config['bind_pw']);
+ }
+
+ if ($this->ready) {
+ $this->logger->addInfo("Connected to $config[host] with '$config[bind_dn]'");
+ }
+ else {
+ $this->logger->addWarning("Connectiion to $config[host] with '$config[bind_dn]' failed!");
+ }
}
/**
* Callback for Net_LDAP3 logging
*/
- public function log($log)
+ public function log($level, $msg)
{
- // TODO: map $log[0] levels
- $msg = is_array($log[1]) ? join('; ', $log[1]) : strval($log[1]);
- $this->logger->addRecord($log[0], $msg);
+ // map PHP log levels to Monolog levels
+ static $loglevels = array(
+ LOG_DEBUG => Monolog::DEBUG,
+ LOG_NOTICE => Monolog::NOTICE,
+ LOG_INFO => Monolog::INFO,
+ LOG_WARNING => Monolog::WARNING,
+ LOG_ERR => Monolog::ERROR,
+ LOG_CRIT => Monolog::CRITICAL,
+ LOG_ALERT => Monolog::ALERT,
+ LOG_EMERG => Monolog::EMERGENCY,
+ );
+
+ $msg = is_array($msg) ? join('; ', $msg) : strval($msg);
+ $this->logger->addRecord($loglevels[$level], $msg);
}
/**
* @see Directory::resolve()
*/
public function resolve($user)
{
$result = array('u' => $user);
+ if ($this->ready) {
+ // search with configured filter
+ $filter = preg_replace('/%u/i', $user, $this->config['filter']);
+ $ldapresult = $this->ldap->search($this->config['base_dn'], $filter, 'sub');
+
+ // got a valid result
+ if ($ldapresult && $ldapresult->count()) {
+ $ldapresult->rewind();
+ $entry = Net_LDAP3::normalize_entry($ldapresult->current()); // get the first entry
+ $this->logger->addInfo("Found " . $ldapresult->count() . " entries for $filter", $entry);
+
+ // convert entry attributes to strings and add them to the final result hash array
+ foreach ($entry as $k => $v) {
+ if (!empty($v)) {
+ $result[$k] = strval(is_array($v) ? $v[0] : $v);
+ }
+ }
+
+ return $result;
+ }
+
+ $this->logger->addInfo("No entry found for $filter");
+ }
+
return false;
}
}

File Metadata

Mime Type
text/x-diff
Expires
Mon, Feb 2, 11:02 AM (1 d, 14 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
426874
Default Alt Text
(3 KB)

Event Timeline