3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 require_once 'Net/LDAP2.php';
24 function ldap_get_config(){
25 static $config = null;
28 $keys = array('host','port','version','starttls','binddn','bindpw','basedn','options','scope');
29 foreach($keys as $key){
30 $value = common_config('ldap', $key);
39 function ldap_get_connection($config = null){
41 $config = ldap_get_config();
44 //cannot use Net_LDAP2::connect() as StatusNet uses
45 //PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
46 //PEAR handling can be overridden on instance objects, so we do that.
47 $ldap = new Net_LDAP2($config);
48 $ldap->setErrorHandling(PEAR_ERROR_RETURN);
50 if (Net_LDAP2::isError($err)) {
51 common_log(LOG_WARNING, 'Could not connect to LDAP server: '.$err->getMessage());
57 function ldap_check_password($username, $password){
58 $ldap = ldap_get_connection();
62 $entry = ldap_get_user($username);
66 $config = ldap_get_config();
67 $config['binddn']=$entry->dn();
68 $config['bindpw']=$password;
69 if(ldap_get_connection($config)){
78 * get an LDAP entry for a user with a given username
80 * @param string $username
81 * $param array $attributes LDAP attributes to retrieve
84 function ldap_get_user($username,$attributes=array()){
85 $ldap = ldap_get_connection();
86 $filter = Net_LDAP2_Filter::create(common_config('ldap','nickname_attribute'), 'equals', $username);
89 'attributes' => $attributes
91 $search = $ldap->search(null,$filter,$options);
93 if (PEAR::isError($search)) {
94 common_log(LOG_WARNING, 'Error while getting DN for user: '.$search->getMessage());
98 if($search->count()==0){
100 }else if($search->count()==1){
101 $entry = $search->shiftEntry();
104 common_log(LOG_WARNING, 'Found ' . $search->count() . ' ldap user with the username: ' . $username);