aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/drivers/ldap/resources_driver_ldap.php
blob: c377393d64cf92725e92fe3fc7b41f7abf53e6a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php

/**
 * LDAP-based resource directory class using rcube_ldap functionality
 *
 * @author Thomas Bruederli <bruederli@kolabsys.com>
 *
 * Copyright (C) 2014, Kolab Systems AG <contact@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * LDAP-based resource directory implementation
 */
class resources_driver_ldap extends resources_driver
{
    private $rc;
    private $ldap;

    /**
     * Default constructor
     */
    function __construct($cal)
    {
        $this->cal = $cal;
        $this->rc = $cal->rc;
    }

    /**
     * Fetch resource objects to be displayed for booking
     *
     * @param  string  Search query (optional)
     * @return array  List of resource records available for booking
     */
    public function load_resources($query = null, $num = 5000)
    {
      if (!($ldap = $this->connect())) {
        return array();
      }

      // TODO: apply paging
      $ldap->set_pagesize($num);

      if (isset($query)) {
        $results = $ldap->search('*', $query, 0, true, true);
      }
      else {
        $results = $ldap->list_records();
      }

      if ($results instanceof ArrayAccess) {
        foreach ($results as $i => $rec) {
          $results[$i] = $this->decode_resource($rec);
        }
      }

      return $results;
    }

    /**
     * Return properties of a single resource
     *
     * @param string  Unique resource identifier
     * @return array Resource object as hash array
     */
    public function get_resource($dn)
    {
      $rec = null;

      if ($ldap = $this->connect()) {
        $rec = $ldap->get_record(rcube_ldap::dn_encode($dn), true);

        if (!empty($rec)) {
          $rec = $this->decode_resource($rec);
        }
      }

      return $rec;
    }

    /**
     * Return properties of a resource owner
     *
     * @param string  Owner identifier
     * @return array  Resource object as hash array
     */
    public function get_resource_owner($dn)
    {
      $owner = null;

      if ($ldap = $this->connect()) {
        $owner = $ldap->get_record(rcube_ldap::dn_encode($dn), true);
        $owner['ID'] = rcube_ldap::dn_decode($owner['ID']);
        unset($owner['_raw_attrib'], $owner['_type']);
      }

      return $owner;
    }

    /**
     * Extract JSON-serialized attributes
     */
    private function decode_resource($rec)
    {
      $rec['ID'] = rcube_ldap::dn_decode($rec['ID']);

      if (is_array($rec['attributes']) && $rec['attributes'][0]) {
        $attributes = array();

        foreach ($rec['attributes'] as $sattr) {
          $attr = @json_decode($sattr, true);
          $attributes += $attr;
        }

        $rec['attributes'] = $attributes;
      }

      // force $rec['members'] to be an array
      if (!empty($rec['members']) && !is_array($rec['members'])) {
        $rec['members'] = array($rec['members']);
      }

      // remove unused cruft
      unset($rec['_raw_attrib']);

      return $rec;
    }

    private function connect()
    {
      if (!isset($this->ldap)) {
        $this->ldap = new rcube_ldap($this->rc->config->get('calendar_resources_directory'), true);
      }

      return $this->ldap->ready ? $this->ldap : null;
    }

}

© 2014-2024 Faster IT GmbH | imprint | privacy policy