1<?php
2
3/*
4 +-----------------------------------------------------------------------+
5 | Net/LDAP3/Result.php                                                  |
6 |                                                                       |
7 | Based on code created by the Roundcube Webmail team.                  |
8 |                                                                       |
9 | Copyright (C) 2006-2014, The Roundcube Dev Team                       |
10 | Copyright (C) 2012-2014, Kolab Systems AG                             |
11 |                                                                       |
12 | This program is free software: you can redistribute it and/or modify  |
13 | it under the terms of the GNU General Public License as published by  |
14 | the Free Software Foundation, either version 3 of the License, or     |
15 | (at your option) any later version.                                   |
16 |                                                                       |
17 | This program is distributed in the hope that it will be useful,       |
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of        |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          |
20 | GNU General Public License for more details.                          |
21 |                                                                       |
22 | You should have received a copy of the GNU General Public License     |
23 | along with this program.  If not, see <http://www.gnu.org/licenses/>. |
24 |                                                                       |
25 | PURPOSE:                                                              |
26 |   Provide advanced functionality for accessing LDAP directories       |
27 |                                                                       |
28 +-----------------------------------------------------------------------+
29 | Authors: Thomas Bruederli <roundcube@gmail.com>                       |
30 |          Jeroen van Meeuwen <vanmeeuwen@kolabsys.com>                 |
31 +-----------------------------------------------------------------------+
32*/
33
34/**
35 * Model class representing an LDAP search result
36 *
37 * @package LDAP
38 */
39class Net_LDAP3_Result implements Iterator
40{
41    protected $conn;
42    protected $base_dn;
43    protected $filter;
44    protected $scope;
45    protected $result;
46
47    private $count;
48    private $current;
49    private $iteratorkey = 0;
50
51    /**
52     * Default constructor
53     *
54     * @param resource $conn      LDAP link identifier
55     * @param string   $base_dn   Base DN used to get this result
56     * @param string   $filter    Filter query used to get this result
57     * @param string   $scope     Scope of the result
58     * @param resource $result    LDAP result entry identifier
59     */
60    function __construct($conn, $base_dn, $filter, $scope, $result)
61    {
62        $this->conn    = $conn;
63        $this->base_dn = $base_dn;
64        $this->filter  = $filter;
65        $this->scope   = $scope;
66        $this->result  = $result;
67    }
68
69    /**
70     * Property value getter
71     *
72     * @param string $property Property name
73     * @param mixed  $default  Return value if proprty is not set
74     *
75     * @return mixed Property value
76     */
77    public function get($property, $default = null)
78    {
79        return isset($this->$property) ? $this->$property : $default;
80    }
81
82    /**
83     * Property value setter
84     *
85     * @param string $property Property name
86     * @param mixed  $value    Property value
87     */
88    public function set($property, $value)
89    {
90        $this->$property = $value;
91    }
92
93    /**
94     * Wrapper for ldap_sort()
95     *
96     * @param string $attr The attribute to use as a key in the sort
97     *
98     * @return bool True on success, False on failure
99     */
100    public function sort($attr)
101    {
102        // @TODO: Don't use ldap_sort() it's deprecated since PHP7
103        // and will be removed in future
104        return @ldap_sort($this->conn, $this->result, $attr);
105    }
106
107    /**
108     * Get entries count
109     *
110     * @return int Number of entries in the result
111     */
112    public function count()
113    {
114        if (!isset($this->count)) {
115            $this->count = ldap_count_entries($this->conn, $this->result);
116        }
117
118        return $this->count;
119    }
120
121    /**
122     * Wrapper for ldap_get_entries()
123     *
124     * @param bool $normalize Optionally normalize the entries to a list of hash arrays
125     *
126     * @return array List of LDAP entries
127     */
128    public function entries($normalize = false)
129    {
130        $entries = ldap_get_entries($this->conn, $this->result);
131
132        if ($normalize) {
133            return Net_LDAP3::normalize_result($entries);
134        }
135
136        return $entries;
137    }
138
139    /**
140     * Wrapper for ldap_get_dn() using the current entry pointer
141     */
142    public function get_dn()
143    {
144        return $this->current ? ldap_get_dn($this->conn, $this->current) : null;
145    }
146
147
148    /***  Implement PHP 5 Iterator interface to make foreach work  ***/
149
150    function current()
151    {
152        $attrib       = ldap_get_attributes($this->conn, $this->current);
153        $attrib['dn'] = ldap_get_dn($this->conn, $this->current);
154
155        return $attrib;
156    }
157
158    function key()
159    {
160        return $this->iteratorkey;
161    }
162
163    function rewind()
164    {
165        $this->iteratorkey = 0;
166        $this->current = ldap_first_entry($this->conn, $this->result);
167    }
168
169    function next()
170    {
171        $this->iteratorkey++;
172        $this->current = ldap_next_entry($this->conn, $this->current);
173    }
174
175    function valid()
176    {
177        return (bool)$this->current;
178    }
179}
180