Changeset View
Changeset View
Standalone View
Standalone View
plugins/calendar/drivers/ldap/resources_driver_ldap.php
Show All 35 Lines | class resources_driver_ldap extends resources_driver | ||||
{ | { | ||||
$this->cal = $cal; | $this->cal = $cal; | ||||
$this->rc = $cal->rc; | $this->rc = $cal->rc; | ||||
} | } | ||||
/** | /** | ||||
* Fetch resource objects to be displayed for booking | * Fetch resource objects to be displayed for booking | ||||
* | * | ||||
* @param string Search query (optional) | * @param string $query Search query (optional) | ||||
* @param int $num Max size of the result | |||||
* | |||||
* @return array List of resource records available for booking | * @return array List of resource records available for booking | ||||
*/ | */ | ||||
public function load_resources($query = null, $num = 5000) | public function load_resources($query = null, $num = 5000) | ||||
{ | { | ||||
if (!($ldap = $this->connect())) { | if (!($ldap = $this->connect())) { | ||||
return array(); | return []; | ||||
} | } | ||||
// TODO: apply paging | // TODO: apply paging | ||||
$ldap->set_pagesize($num); | $ldap->set_pagesize($num); | ||||
if (isset($query)) { | if (isset($query)) { | ||||
$results = $ldap->search('*', $query, 0, true, true); | $results = $ldap->search('*', $query, 0, true, true); | ||||
} | } | ||||
else { | else { | ||||
$results = $ldap->list_records(); | $results = $ldap->list_records(); | ||||
} | } | ||||
if ($results instanceof ArrayAccess) { | if ($results instanceof ArrayAccess) { | ||||
foreach ($results as $i => $rec) { | foreach ($results as $i => $rec) { | ||||
$results[$i] = $this->decode_resource($rec); | $results[$i] = $this->decode_resource($rec); | ||||
} | } | ||||
} | } | ||||
return $results; | return $results; | ||||
} | } | ||||
/** | /** | ||||
* Return properties of a single resource | * Return properties of a single resource | ||||
* | * | ||||
* @param string Unique resource identifier | * @param string $id Unique resource identifier | ||||
* | |||||
* @return array Resource object as hash array | * @return array Resource object as hash array | ||||
*/ | */ | ||||
public function get_resource($dn) | public function get_resource($dn) | ||||
{ | { | ||||
$rec = null; | $rec = null; | ||||
if ($ldap = $this->connect()) { | if ($ldap = $this->connect()) { | ||||
$rec = $ldap->get_record(rcube_ldap::dn_encode($dn), true); | $rec = $ldap->get_record(rcube_ldap::dn_encode($dn), true); | ||||
if (!empty($rec)) { | if (!empty($rec)) { | ||||
$rec = $this->decode_resource($rec); | $rec = $this->decode_resource($rec); | ||||
} | } | ||||
} | } | ||||
return $rec; | return $rec; | ||||
} | } | ||||
/** | /** | ||||
* Return properties of a resource owner | * Return properties of a resource owner | ||||
* | * | ||||
* @param string Owner identifier | * @param string $dn Owner identifier | ||||
* | |||||
* @return array Resource object as hash array | * @return array Resource object as hash array | ||||
*/ | */ | ||||
public function get_resource_owner($dn) | public function get_resource_owner($dn) | ||||
{ | { | ||||
$owner = null; | $owner = null; | ||||
if ($ldap = $this->connect()) { | if ($ldap = $this->connect()) { | ||||
$owner = $ldap->get_record(rcube_ldap::dn_encode($dn), true); | $owner = $ldap->get_record(rcube_ldap::dn_encode($dn), true); | ||||
$owner['ID'] = rcube_ldap::dn_decode($owner['ID']); | $owner['ID'] = rcube_ldap::dn_decode($owner['ID']); | ||||
unset($owner['_raw_attrib'], $owner['_type']); | unset($owner['_raw_attrib'], $owner['_type']); | ||||
} | } | ||||
return $owner; | return $owner; | ||||
} | } | ||||
/** | /** | ||||
* Extract JSON-serialized attributes | * Extract JSON-serialized attributes | ||||
*/ | */ | ||||
private function decode_resource($rec) | private function decode_resource($rec) | ||||
{ | { | ||||
$rec['ID'] = rcube_ldap::dn_decode($rec['ID']); | $rec['ID'] = rcube_ldap::dn_decode($rec['ID']); | ||||
$attributes = array(); | $attributes = []; | ||||
foreach ((array) $rec['attributes'] as $sattr) { | foreach ((array) $rec['attributes'] as $sattr) { | ||||
$sattr = trim($sattr); | $sattr = trim($sattr); | ||||
if ($sattr && $sattr[0] === '{') { | if (!empty($sattr) && $sattr[0] === '{') { | ||||
$attr = @json_decode($sattr, true); | $attr = @json_decode($sattr, true); | ||||
$attributes += $attr; | $attributes += $attr; | ||||
} | } | ||||
else if ($sattr && empty($rec['description'])) { | else if (!empty($sattr) && empty($rec['description'])) { | ||||
$rec['description'] = $sattr; | $rec['description'] = $sattr; | ||||
} | } | ||||
} | } | ||||
$rec['attributes'] = $attributes; | $rec['attributes'] = $attributes; | ||||
// force $rec['members'] to be an array | // force $rec['members'] to be an array | ||||
if (!empty($rec['members']) && !is_array($rec['members'])) { | if (!empty($rec['members']) && !is_array($rec['members'])) { | ||||
$rec['members'] = array($rec['members']); | $rec['members'] = [$rec['members']]; | ||||
} | } | ||||
// remove unused cruft | // remove unused cruft | ||||
unset($rec['_raw_attrib']); | unset($rec['_raw_attrib']); | ||||
return $rec; | return $rec; | ||||
} | } | ||||
private function connect() | private function connect() | ||||
{ | { | ||||
if (!isset($this->ldap)) { | if (!isset($this->ldap)) { | ||||
$this->ldap = new rcube_ldap($this->rc->config->get('calendar_resources_directory'), true); | $this->ldap = new rcube_ldap($this->rc->config->get('calendar_resources_directory'), true); | ||||
} | } | ||||
return $this->ldap->ready ? $this->ldap : null; | return $this->ldap->ready ? $this->ldap : null; | ||||
} | } | ||||
} | |||||
} | |||||
No newline at end of file |