]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Ldap/ldap.php
Merge branch '0.9.x' into userflag
[quix0rs-gnu-social.git] / plugins / Ldap / ldap.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 require_once 'Net/LDAP2.php';
23
24 function ldap_get_config(){
25     static $config = null;
26     if($config == null){
27         $config = array();
28         $keys = array('host','port','version','starttls','binddn','bindpw','basedn','options','scope');
29         foreach($keys as $key){
30             $value = common_config('ldap', $key);
31             if($value!==false){
32                 $config[$key]=$value;
33             }
34         }
35     }
36     return $config;
37 }
38
39 function ldap_get_connection($config = null){
40     if($config == null){
41         $config = ldap_get_config();
42     }
43     
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);
49     $err=$ldap->bind();
50     if (Net_LDAP2::isError($err)) {
51         common_log(LOG_WARNING, 'Could not connect to LDAP server: '.$err->getMessage());
52         return false;
53     }
54     return $ldap;
55 }
56
57 function ldap_check_password($username, $password){
58     $ldap = ldap_get_connection();
59     if(!$ldap){
60         return false;
61     }
62     $entry = ldap_get_user($username);
63     if(!$entry){
64         return false;
65     }else{
66         $config = ldap_get_config();
67         $config['binddn']=$entry->dn();
68         $config['bindpw']=$password;
69         if(ldap_get_connection($config)){
70             return true;
71         }else{
72             return false;
73         }
74     }
75 }
76
77 /**
78  * get an LDAP entry for a user with a given username
79  * 
80  * @param string $username
81  * $param array $attributes LDAP attributes to retrieve
82  * @return string DN
83  */
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);
87     $options = array(
88         'scope' => 'sub',
89         'attributes' => $attributes
90     );
91     $search = $ldap->search(null,$filter,$options);
92     
93     if (PEAR::isError($search)) {
94         common_log(LOG_WARNING, 'Error while getting DN for user: '.$search->getMessage());
95         return false;
96     }
97
98     if($search->count()==0){
99         return false;
100     }else if($search->count()==1){
101         $entry = $search->shiftEntry();
102         return $entry;
103     }else{
104         common_log(LOG_WARNING, 'Found ' . $search->count() . ' ldap user with the username: ' . $username);
105         return false;
106     }
107 }
108