ILIAS  Release_5_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  if(is_array($value))
134  {
135  if($value['count'] > 1)
136  {
137  for($i = 0; $i < $value['count']; $i++)
138  {
139  $data[$key][] = $value[$i];
140  }
141  }
142  elseif($value['count'] == 1)
143  {
144  $data[$key] = $value[0];
145  }
146  }
147  else
148  {
149  $data[$key] = $value;
150  }
151  }
152  $this->all_rows[] = $data;
153  if($row_counter == 0)
154  {
155  $this->data = $data;
156  }
157  }
158  return true;
159  }
160 
161  public function __destruct()
162  {
163  @ldap_free_result($this->result);
164  }
165 
172  private function getEntries()
173  {
174  return $this->entries = @ldap_get_entries($this->ldap_handle,$this->result);
175 
176  // this way ldap_get_entries is binary safe
177 
178  $i=0;
179  $tmp_entries = array();
180  $entry = ldap_first_entry($this->ldap_handle,$this->result);
181  do {
182  $attributes = @ldap_get_attributes($this->ldap_handle, $entry);
183  for($j=0; $j<$attributes['count']; $j++)
184  {
185  $values = ldap_get_values_len($this->ldap_handle, $entry,$attributes[$j]);
186  $tmp_entries[$i][strtolower($attributes[$j])] = $values;
187  }
188  $i++;
189  } while ($entry = @ldap_next_entry($this->ldap_handle,$entry));
190 
191  if($i)
192  {
193  $tmp_entries['count'] = $i;
194  }
195  $this->entries = $tmp_entries;
196  return $this->entries;
197  }
198 }
199 
200 ?>