| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | ** abook_ldap_server.php |
| 5 | ** |
| 6 | ** Address book backend for LDAP server |
| 7 | ** |
| 8 | ** An array with the following elements must be passed to |
| 9 | ** the class constructor (elements marked ? are optional): |
| 10 | ** |
| 11 | ** host => LDAP server hostname/IP-address |
| 12 | ** base => LDAP server root (base dn). Empty string allowed. |
| 13 | ** ? port => LDAP server TCP port number (default: 389) |
| 14 | ** ? charset => LDAP server charset (default: utf-8) |
| 15 | ** ? name => Name for LDAP server (default "LDAP: hostname") |
| 16 | ** Used to tag the result data |
| 17 | ** ? maxrows => Maximum # of rows in search result |
| 18 | ** ? timeout => Timeout for LDAP operations (in seconds, default: 30) |
| 19 | ** Might not work for all LDAP libraries or servers. |
| 20 | ** |
| 21 | ** NOTE. This class should not be used directly. Use the |
| 22 | ** "AddressBook" class instead. |
| 23 | ** |
| 24 | ** $Id$ |
| 25 | **/ |
| 26 | |
| 27 | class abook_ldap_server extends addressbook_backend { |
| 28 | var $btype = 'remote'; |
| 29 | var $bname = 'ldap_server'; |
| 30 | |
| 31 | // Parameters changed by class |
| 32 | var $sname = 'LDAP'; // Service name |
| 33 | var $server = ''; // LDAP server name |
| 34 | var $port = 389; // LDAP server port |
| 35 | var $basedn = ''; // LDAP base DN |
| 36 | var $charset = 'utf-8'; // LDAP server charset |
| 37 | var $linkid = false; // PHP LDAP link ID |
| 38 | var $bound = false; // True if LDAP server is bound |
| 39 | var $maxrows = 250; // Max rows in result |
| 40 | var $timeout = 30; // Timeout for LDAP operations (in seconds) |
| 41 | |
| 42 | // Constructor. Connects to database |
| 43 | function abook_ldap_server($param) { |
| 44 | if(!function_exists('ldap_connect')) { |
| 45 | $this->set_error('LDAP support missing from PHP'); |
| 46 | return; |
| 47 | } |
| 48 | if(is_array($param)) { |
| 49 | $this->server = $param['host']; |
| 50 | $this->basedn = $param['base']; |
| 51 | if(!empty($param['port'])) |
| 52 | $this->port = $param['port']; |
| 53 | if(!empty($param['charset'])) |
| 54 | $this->charset = strtolower($param['charset']); |
| 55 | if(isset($param['maxrows'])) |
| 56 | $this->maxrows = $param['maxrows']; |
| 57 | if(isset($param['timeout'])) |
| 58 | $this->timeout = $param['timeout']; |
| 59 | if(empty($param['name'])) |
| 60 | $this->sname = 'LDAP: ' . $param['host']; |
| 61 | else |
| 62 | $this->sname = $param['name']; |
| 63 | |
| 64 | $this->open(true); |
| 65 | } else { |
| 66 | $this->set_error('Invalid argument to constructor'); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | // Open the LDAP server. New connection if $new is true |
| 72 | function open($new = false) { |
| 73 | $this->error = ''; |
| 74 | |
| 75 | // Connection is already open |
| 76 | if($this->linkid != false && !$new) |
| 77 | return true; |
| 78 | |
| 79 | $this->linkid = @ldap_connect($this->server, $this->port); |
| 80 | if(!$this->linkid) |
| 81 | if(function_exists('ldap_error')) |
| 82 | return $this->set_error(ldap_error($this->linkid)); |
| 83 | else |
| 84 | return $this->set_error('ldap_connect failed'); |
| 85 | |
| 86 | if(!@ldap_bind($this->linkid)) |
| 87 | if(function_exists('ldap_error')) |
| 88 | return $this->set_error(ldap_error($this->linkid)); |
| 89 | else |
| 90 | return $this->set_error('ldap_bind failed'); |
| 91 | |
| 92 | $this->bound = true; |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | // Encode iso8859-1 string to the charset used by this LDAP server |
| 99 | function charset_encode($str) { |
| 100 | if($this->charset == 'utf-8') { |
| 101 | if(function_exists('utf8_encode')) |
| 102 | return utf8_encode($str); |
| 103 | else |
| 104 | return $str; |
| 105 | } else { |
| 106 | return $str; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | |
| 111 | // Decode from charset used by this LDAP server to iso8859-1 |
| 112 | function charset_decode($str) { |
| 113 | if($this->charset == 'utf-8') { |
| 114 | if(function_exists('utf8_decode')) |
| 115 | return utf8_decode($str); |
| 116 | else |
| 117 | return $str; |
| 118 | } else { |
| 119 | return $str; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | // ========================== Public ======================== |
| 125 | |
| 126 | // Search the LDAP server |
| 127 | function search($expr) { |
| 128 | |
| 129 | // To be replaced by advanded search expression parsing |
| 130 | if(is_array($expr)) return false; |
| 131 | |
| 132 | // Encode the expression |
| 133 | $expr = $this->charset_encode($expr); |
| 134 | if(strstr($expr, "*") === false) |
| 135 | $expr = "*$expr*"; |
| 136 | $expression = "cn=$expr"; |
| 137 | |
| 138 | // Make sure connection is there |
| 139 | if(!$this->open()) |
| 140 | return false; |
| 141 | |
| 142 | // Do the search. Use improved ldap_search() if PHP version is |
| 143 | // 4.0.2 or newer. |
| 144 | if(sqCheckPHPVersion(4, 0, 2)) { |
| 145 | $sret = @ldap_search($this->linkid, $this->basedn, $expression, |
| 146 | array('dn', 'o', 'ou', 'sn', 'givenname', |
| 147 | 'cn', 'mail', 'telephonenumber'), |
| 148 | 0, $this->maxrows, $this->timeout); |
| 149 | } else { |
| 150 | $sret = @ldap_search($this->linkid, $this->basedn, $expression, |
| 151 | array('dn', 'o', 'ou', 'sn', 'givenname', |
| 152 | 'cn', 'mail', 'telephonenumber')); |
| 153 | } |
| 154 | |
| 155 | // Should get error from server using the ldap_error() function, |
| 156 | // but it only exist in the PHP LDAP documentation. |
| 157 | if(!$sret) |
| 158 | if(function_exists('ldap_error')) |
| 159 | return $this->set_error(ldap_error($this->linkid)); |
| 160 | else |
| 161 | return $this->set_error('ldap_search failed'); |
| 162 | |
| 163 | if(@ldap_count_entries($this->linkid, $sret) <= 0) |
| 164 | return array(); |
| 165 | |
| 166 | // Get results |
| 167 | $ret = array(); |
| 168 | $returned_rows = 0; |
| 169 | $res = @ldap_get_entries($this->linkid, $sret); |
| 170 | for($i = 0 ; $i < $res['count'] ; $i++) { |
| 171 | $row = $res[$i]; |
| 172 | |
| 173 | // Extract data common for all e-mail addresses |
| 174 | // of an object. Use only the first name |
| 175 | $nickname = $this->charset_decode($row['dn']); |
| 176 | $fullname = $this->charset_decode($row['cn'][0]); |
| 177 | |
| 178 | if(empty($row['telephonenumber'][0])) $phone = ''; |
| 179 | else $phone = $this->charset_decode($row['telephonenumber'][0]); |
| 180 | |
| 181 | if(!empty($row['ou'][0])) |
| 182 | $label = $this->charset_decode($row['ou'][0]); |
| 183 | else if(!empty($row['o'][0])) |
| 184 | $label = $this->charset_decode($row['o'][0]); |
| 185 | else |
| 186 | $label = ''; |
| 187 | |
| 188 | if(empty($row['givenname'][0])) $firstname = ''; |
| 189 | else $firstname = $this->charset_decode($row['givenname'][0]); |
| 190 | |
| 191 | if(empty($row['sn'][0])) $surname = ''; |
| 192 | else $surname = $this->charset_decode($row['sn'][0]); |
| 193 | |
| 194 | // Add one row to result for each e-mail address |
| 195 | if(isset($row['mail']['count'])) { |
| 196 | for($j = 0 ; $j < $row['mail']['count'] ; $j++) { |
| 197 | array_push($ret, array('nickname' => $nickname, |
| 198 | 'name' => $fullname, |
| 199 | 'firstname' => $firstname, |
| 200 | 'lastname' => $surname, |
| 201 | 'email' => $row['mail'][$j], |
| 202 | 'label' => $label, |
| 203 | 'phone' => $phone, |
| 204 | 'backend' => $this->bnum, |
| 205 | 'source' => &$this->sname)); |
| 206 | |
| 207 | // Limit number of hits |
| 208 | $returned_rows++; |
| 209 | if(($returned_rows >= $this->maxrows) && |
| 210 | ($this->maxrows > 0) ) { |
| 211 | ldap_free_result($sret); |
| 212 | return $ret; |
| 213 | } |
| 214 | |
| 215 | } // for($j ...) |
| 216 | |
| 217 | } // isset($row['mail']['count']) |
| 218 | |
| 219 | } |
| 220 | |
| 221 | ldap_free_result($sret); |
| 222 | return $ret; |
| 223 | } // end search() |
| 224 | |
| 225 | |
| 226 | // If you run a tiny LDAP server and you want the "List All" button |
| 227 | // to show EVERYONE, then uncomment this tiny block of code: |
| 228 | // |
| 229 | // function list_addr() { |
| 230 | // return $this->search("*"); |
| 231 | // } |
| 232 | // |
| 233 | // Careful with this -- it could get quite large for big sites. |
| 234 | } |
| 235 | ?> |