]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Ldap/ldap.php
Made the ldap plugin work, and add a readme
[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         static $ldap = null;
42         if($ldap!=null){
43             return $ldap;
44         }
45         $config = ldap_get_config();
46     }
47     $ldap = Net_LDAP2::connect($config);
48     if (PEAR::isError($ldap)) {
49         common_log(LOG_WARNING, 'Could not connect to LDAP server: '.$ldap->getMessage());
50         return false;
51     }else{
52         return $ldap;
53     }
54 }
55
56 function ldap_check_password($username, $password){
57     $ldap = ldap_get_connection();
58     if(!$ldap){
59         return false;
60     }
61     $dn = ldap_get_user_dn($username);
62     if(!$dn){
63         return false;
64     }else{
65         $config = ldap_get_config();
66         $config['binddn']=$dn;
67         $config['bindpw']=$password;
68         if(ldap_get_connection($config)){
69             return true;
70         }else{
71             return false;
72         }
73     }
74 }
75
76 /**
77  * get an LDAP user's DN given the user's username
78  * 
79  * @param string $username
80  * @return string DN
81  */
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);
85     $options = array(
86         'scope' => 'sub',
87         'attributes' => array()
88     );
89     $search = $ldap->search(null,$filter,$options);
90     
91     if (PEAR::isError($search)) {
92         common_log(LOG_WARNING, 'Error while getting DN for user: '.$search->getMessage());
93         return false;
94     }
95
96     if($search->count()==0){
97         return false;
98     }else if($search->count()==1){
99         $entry = $search->shiftEntry();
100         return $entry->dn();
101     }else{
102         common_log(LOG_WARNING, 'Found ' . $search->count() . ' ldap user with the username: ' . $username);
103         return false;
104     }
105 }
106