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){
45 $config = ldap_get_config();
47 $ldap = Net_LDAP2::connect($config);
48 if (PEAR::isError($ldap)) {
49 common_log(LOG_WARNING, 'Could not connect to LDAP server: '.$ldap->getMessage());
56 function ldap_check_password($username, $password){
57 $ldap = ldap_get_connection();
61 $dn = ldap_get_user_dn($username);
65 $config = ldap_get_config();
66 $config['binddn']=$dn;
67 $config['bindpw']=$password;
68 if(ldap_get_connection($config)){
77 * get an LDAP user's DN given the user's username
79 * @param string $username
82 function ldap_get_user_dn($username){
83 $ldap = ldap_get_connection();
84 $filter = Net_LDAP2_Filter::create(common_config('ldap','nickname_attribute'), 'equals', $username);
87 'attributes' => array()
89 $search = $ldap->search(null,$filter,$options);
91 if (PEAR::isError($search)) {
92 common_log(LOG_WARNING, 'Error while getting DN for user: '.$search->getMessage());
96 if($search->count()==0){
98 }else if($search->count()==1){
99 $entry = $search->shiftEntry();
102 common_log(LOG_WARNING, 'Found ' . $search->count() . ' ldap user with the username: ' . $username);