comparison plugins/calendar/drivers/ldap/resources_driver_ldap.php @ 3:f6fe4b6ae66a

calendar plugin nearly as distributed
author Charlie Root
date Sat, 13 Jan 2018 08:56:12 -0500
parents
children
comparison
equal deleted inserted replaced
2:c828b0fd4a6e 3:f6fe4b6ae66a
1 <?php
2
3 /**
4 * LDAP-based resource directory class using rcube_ldap functionality
5 *
6 * @author Thomas Bruederli <bruederli@kolabsys.com>
7 *
8 * Copyright (C) 2014, Kolab Systems AG <contact@kolabsys.com>
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as
12 * published by the Free Software Foundation, either version 3 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 /**
25 * LDAP-based resource directory implementation
26 */
27 class resources_driver_ldap extends resources_driver
28 {
29 private $rc;
30 private $ldap;
31
32 /**
33 * Default constructor
34 */
35 function __construct($cal)
36 {
37 $this->cal = $cal;
38 $this->rc = $cal->rc;
39 }
40
41 /**
42 * Fetch resource objects to be displayed for booking
43 *
44 * @param string Search query (optional)
45 * @return array List of resource records available for booking
46 */
47 public function load_resources($query = null, $num = 5000)
48 {
49 if (!($ldap = $this->connect())) {
50 return array();
51 }
52
53 // TODO: apply paging
54 $ldap->set_pagesize($num);
55
56 if (isset($query)) {
57 $results = $ldap->search('*', $query, 0, true, true);
58 }
59 else {
60 $results = $ldap->list_records();
61 }
62
63 if ($results instanceof ArrayAccess) {
64 foreach ($results as $i => $rec) {
65 $results[$i] = $this->decode_resource($rec);
66 }
67 }
68
69 return $results;
70 }
71
72 /**
73 * Return properties of a single resource
74 *
75 * @param string Unique resource identifier
76 * @return array Resource object as hash array
77 */
78 public function get_resource($dn)
79 {
80 $rec = null;
81
82 if ($ldap = $this->connect()) {
83 $rec = $ldap->get_record(rcube_ldap::dn_encode($dn), true);
84
85 if (!empty($rec)) {
86 $rec = $this->decode_resource($rec);
87 }
88 }
89
90 return $rec;
91 }
92
93 /**
94 * Return properties of a resource owner
95 *
96 * @param string Owner identifier
97 * @return array Resource object as hash array
98 */
99 public function get_resource_owner($dn)
100 {
101 $owner = null;
102
103 if ($ldap = $this->connect()) {
104 $owner = $ldap->get_record(rcube_ldap::dn_encode($dn), true);
105 $owner['ID'] = rcube_ldap::dn_decode($owner['ID']);
106 unset($owner['_raw_attrib'], $owner['_type']);
107 }
108
109 return $owner;
110 }
111
112 /**
113 * Extract JSON-serialized attributes
114 */
115 private function decode_resource($rec)
116 {
117 $rec['ID'] = rcube_ldap::dn_decode($rec['ID']);
118
119 if (is_array($rec['attributes']) && $rec['attributes'][0]) {
120 $attributes = array();
121
122 foreach ($rec['attributes'] as $sattr) {
123 $attr = @json_decode($sattr, true);
124 $attributes += $attr;
125 }
126
127 $rec['attributes'] = $attributes;
128 }
129
130 // force $rec['members'] to be an array
131 if (!empty($rec['members']) && !is_array($rec['members'])) {
132 $rec['members'] = array($rec['members']);
133 }
134
135 // remove unused cruft
136 unset($rec['_raw_attrib']);
137
138 return $rec;
139 }
140
141 private function connect()
142 {
143 if (!isset($this->ldap)) {
144 $this->ldap = new rcube_ldap($this->rc->config->get('calendar_resources_directory'), true);
145 }
146
147 return $this->ldap->ready ? $this->ldap : null;
148 }
149
150 }