| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | +-----------------------------------------------------------------------+ |
|---|
| 5 | | program/include/rcube_ldap.inc | |
|---|
| 6 | | | |
|---|
| 7 | | This file is part of the RoundCube Webmail client | |
|---|
| 8 | | Copyright (C) 2006-2007, RoundCube Dev. - Switzerland | |
|---|
| 9 | | Licensed under the GNU GPL | |
|---|
| 10 | | | |
|---|
| 11 | | PURPOSE: | |
|---|
| 12 | | Interface to an LDAP address directory | |
|---|
| 13 | | | |
|---|
| 14 | +-----------------------------------------------------------------------+ |
|---|
| 15 | | Author: Thomas Bruederli <roundcube@gmail.com> | |
|---|
| 16 | +-----------------------------------------------------------------------+ |
|---|
| 17 | |
|---|
| 18 | $Id$ |
|---|
| 19 | |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | class rcube_ldap |
|---|
| 23 | { |
|---|
| 24 | var $conn; |
|---|
| 25 | var $prop = array(); |
|---|
| 26 | |
|---|
| 27 | var $filter = '1'; |
|---|
| 28 | var $result = null; |
|---|
| 29 | var $search_fields; |
|---|
| 30 | var $search_string; |
|---|
| 31 | |
|---|
| 32 | /** public properties */ |
|---|
| 33 | var $primary_key = 'ID'; |
|---|
| 34 | var $readonly = true; |
|---|
| 35 | var $list_page = 1; |
|---|
| 36 | var $page_size = 10; |
|---|
| 37 | var $ready = false; |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * Object constructor |
|---|
| 42 | * |
|---|
| 43 | * @param array LDAP connection properties |
|---|
| 44 | * @param integer User-ID |
|---|
| 45 | */ |
|---|
| 46 | function __construct($p) |
|---|
| 47 | { |
|---|
| 48 | $this->prop = $p; |
|---|
| 49 | $this->connect(); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * PHP 4 object constructor |
|---|
| 54 | * |
|---|
| 55 | * @see rcube_ldap::__construct |
|---|
| 56 | */ |
|---|
| 57 | function rcube_contacts($p) |
|---|
| 58 | { |
|---|
| 59 | $this->__construct($p); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Establish a connection to the LDAP server |
|---|
| 65 | */ |
|---|
| 66 | function connect() |
|---|
| 67 | { |
|---|
| 68 | if (!function_exists('ldap_connect')) |
|---|
| 69 | raise_error(array('type' => "ldap", |
|---|
| 70 | 'message' => "No ldap support in this installation of PHP"), |
|---|
| 71 | true); |
|---|
| 72 | |
|---|
| 73 | if (is_resource($this->conn)) |
|---|
| 74 | return true; |
|---|
| 75 | |
|---|
| 76 | if (!is_array($this->prop['hosts'])) |
|---|
| 77 | $this->prop['hosts'] = array($this->prop['hosts']); |
|---|
| 78 | |
|---|
| 79 | foreach ($this->prop['hosts'] as $host) |
|---|
| 80 | { |
|---|
| 81 | if ($lc = @ldap_connect($host, $this->prop['port'])) |
|---|
| 82 | { |
|---|
| 83 | @ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, $this->prop['port']); |
|---|
| 84 | $this->prop['host'] = $host; |
|---|
| 85 | $this->conn = $lc; |
|---|
| 86 | return true; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | if (is_resource($this->conn)) |
|---|
| 91 | $this->ready = true; |
|---|
| 92 | else |
|---|
| 93 | raise_error(array('type' => "ldap", |
|---|
| 94 | 'message' => "Could not connect to any LDAP server, tried $host:{$this->prop[port]} last"), |
|---|
| 95 | true); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | /** |
|---|
| 100 | * Close connection to LDAP server |
|---|
| 101 | */ |
|---|
| 102 | function close() |
|---|
| 103 | { |
|---|
| 104 | if ($this->conn) |
|---|
| 105 | @ldap_unbind($this->conn); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * Set internal list page |
|---|
| 111 | * |
|---|
| 112 | * @param number Page number to list |
|---|
| 113 | * @access public |
|---|
| 114 | */ |
|---|
| 115 | function set_page($page) |
|---|
| 116 | { |
|---|
| 117 | $this->list_page = (int)$page; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | /** |
|---|
| 122 | * Set internal page size |
|---|
| 123 | * |
|---|
| 124 | * @param number Number of messages to display on one page |
|---|
| 125 | * @access public |
|---|
| 126 | */ |
|---|
| 127 | function set_pagesize($size) |
|---|
| 128 | { |
|---|
| 129 | $this->page_size = (int)$size; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | /** |
|---|
| 134 | * Save a search string for future listings |
|---|
| 135 | * |
|---|
| 136 | * @param string ?? |
|---|
| 137 | */ |
|---|
| 138 | function set_search_set($filter) |
|---|
| 139 | { |
|---|
| 140 | // TODO |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | /** |
|---|
| 145 | * Getter for saved search properties |
|---|
| 146 | * |
|---|
| 147 | * @return mixed Search properties used by this class |
|---|
| 148 | */ |
|---|
| 149 | function get_search_set() |
|---|
| 150 | { |
|---|
| 151 | // TODO |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | /** |
|---|
| 156 | * Reset all saved results and search parameters |
|---|
| 157 | */ |
|---|
| 158 | function reset() |
|---|
| 159 | { |
|---|
| 160 | // TODO |
|---|
| 161 | $this->result = null; |
|---|
| 162 | $this->filter = '1'; |
|---|
| 163 | $this->search_fields = null; |
|---|
| 164 | $this->search_string = null; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | /** |
|---|
| 169 | * List the current set of contact records |
|---|
| 170 | * |
|---|
| 171 | * @param array List of cols to show |
|---|
| 172 | * @return array Indexed list of contact records, each a hash array |
|---|
| 173 | */ |
|---|
| 174 | function list_records($cols=null, $subset=0) |
|---|
| 175 | { |
|---|
| 176 | // count contacts for this user |
|---|
| 177 | $this->result = $this->count(); |
|---|
| 178 | |
|---|
| 179 | // TODO |
|---|
| 180 | |
|---|
| 181 | return $this->result; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | /** |
|---|
| 186 | * Search contacts |
|---|
| 187 | * |
|---|
| 188 | * @param array List of fields to search in |
|---|
| 189 | * @param string Search value |
|---|
| 190 | * @param boolean True if results are requested, False if count only |
|---|
| 191 | * @return Indexed list of contact records and 'count' value |
|---|
| 192 | */ |
|---|
| 193 | function search($fields, $value, $select=true) |
|---|
| 194 | { |
|---|
| 195 | // TODO |
|---|
| 196 | |
|---|
| 197 | //$this->set_search_set(); |
|---|
| 198 | if ($select) |
|---|
| 199 | $this->list_records(); |
|---|
| 200 | else |
|---|
| 201 | $this->result = $this->count(); |
|---|
| 202 | |
|---|
| 203 | return $this->result; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | /** |
|---|
| 208 | * Count number of available contacts in database |
|---|
| 209 | * |
|---|
| 210 | * @return Result array with values for 'count' and 'first' |
|---|
| 211 | */ |
|---|
| 212 | function count() |
|---|
| 213 | { |
|---|
| 214 | $count = 0; |
|---|
| 215 | // TODO |
|---|
| 216 | return new rcube_result_set($count, ($this->list_page-1) * $this->page_size);; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | /** |
|---|
| 221 | * Return the last result set |
|---|
| 222 | * |
|---|
| 223 | * @return Result array or NULL if nothing selected yet |
|---|
| 224 | */ |
|---|
| 225 | function get_result() |
|---|
| 226 | { |
|---|
| 227 | return $this->result; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | /** |
|---|
| 232 | * Get a specific contact record |
|---|
| 233 | * |
|---|
| 234 | * @param mixed record identifier |
|---|
| 235 | * @return Hash array with all record fields or False if not found |
|---|
| 236 | */ |
|---|
| 237 | function get_record($id, $assoc=false) |
|---|
| 238 | { |
|---|
| 239 | // TODO |
|---|
| 240 | |
|---|
| 241 | return $assoc && $sql_arr ? $sql_arr : $this->result; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | /** |
|---|
| 246 | * Create a new contact record |
|---|
| 247 | * |
|---|
| 248 | * @param array Assoziative array with save data |
|---|
| 249 | * @return The create record ID on success, False on error |
|---|
| 250 | */ |
|---|
| 251 | function insert($save_cols) |
|---|
| 252 | { |
|---|
| 253 | // TODO |
|---|
| 254 | return false; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | /** |
|---|
| 259 | * Update a specific contact record |
|---|
| 260 | * |
|---|
| 261 | * @param mixed Record identifier |
|---|
| 262 | * @param array Assoziative array with save data |
|---|
| 263 | * @return True on success, False on error |
|---|
| 264 | */ |
|---|
| 265 | function update($id, $save_cols) |
|---|
| 266 | { |
|---|
| 267 | // TODO |
|---|
| 268 | return false; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | /** |
|---|
| 273 | * Mark one or more contact records as deleted |
|---|
| 274 | * |
|---|
| 275 | * @param array Record identifiers |
|---|
| 276 | */ |
|---|
| 277 | function delete($ids) |
|---|
| 278 | { |
|---|
| 279 | // TODO |
|---|
| 280 | return false; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | ?> |
|---|