ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLDAPResult.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
35 {
36  private $ldap_handle = null;
37  private $result = null;
38  private $entries = null;
39 
40  private $num_results = null;
41  private $data = null;
42 
51  public function __construct($a_ldap_connection,$a_result)
52  {
53  $this->ldap_handle = $a_ldap_connection;
54  $this->result = $a_result;
55 
56  $this->toSimpleArray();
57  }
58 
66  public function get()
67  {
68  return $this->data ? $this->data : array();
69  }
70 
77  public function getRows()
78  {
79  return $this->all_rows ? $this->all_rows : array();
80  }
81 
89  public function numRows()
90  {
91  return $this->num_results;
92  }
93 
101  private function toSimpleArray()
102  {
103  $this->data = array();
104  $this->num_results = 0;
105 
106  if(!$this->entries = $this->getEntries())
107  {
108  return false;
109  }
110 
111  $this->num_results = $this->entries['count'];
112  if($this->entries['count'] == 0)
113  {
114  return true;
115  }
116 
117  for($row_counter = 0; $row_counter < $this->entries['count'];$row_counter++)
118  {
119  $data = array();
120  foreach($this->entries[$row_counter] as $key => $value)
121  {
122  $key = strtolower($key);
123 
124  if(is_int($key))
125  {
126  continue;
127  }
128  if($key == 'dn')
129  {
130  $data['dn'] = $value;
131  continue;
132  }
133 
134  if($value['count'] > 1)
135  {
136  for($i = 0; $i < $value['count']; $i++)
137  {
138  $data[$key][] = $value[$i];
139  }
140  }
141  elseif($value['count'] == 1)
142  {
143  $data[$key] = $value[0];
144  }
145  }
146  $this->all_rows[] = $data;
147  if($row_counter == 0)
148  {
149  $this->data = $data;
150  }
151  }
152  return true;
153  }
154 
155  public function __destruct()
156  {
157  @ldap_free_result($this->result);
158  }
159 
166  private function getEntries()
167  {
168  return $this->entries = @ldap_get_entries($this->ldap_handle,$this->result);
169 
170  // this way ldap_get_entries is binary safe
171 
172  $i=0;
173  $tmp_entries = array();
174  $entry = ldap_first_entry($this->ldap_handle,$this->result);
175  do {
176  $attributes = @ldap_get_attributes($this->ldap_handle, $entry);
177  for($j=0; $j<$attributes['count']; $j++)
178  {
179  $values = ldap_get_values_len($this->ldap_handle, $entry,$attributes[$j]);
180  $tmp_entries[$i][strtolower($attributes[$j])] = $values;
181  }
182  $i++;
183  } while ($entry = @ldap_next_entry($this->ldap_handle,$entry));
184 
185  if($i)
186  {
187  $tmp_entries['count'] = $i;
188  }
189  $this->entries = $tmp_entries;
190  return $this->entries;
191  }
192 }
193 
194 ?>