summaryrefslogtreecommitdiffstats
path: root/endpoints/lib/auth/LDAP.php
blob: 7e7d49e6945bb6f1c20966267bbcad5f9a3f9fdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/*
 * $Id: LDAP.php 501 2013-07-11 17:44:37Z imooreyahoo@gmail.com $
 * Experimental!
 */


class phpvbAuthLDAP implements phpvbAuth {
	
	var $capabilities = array(
		'canChangePassword' => false,
		'canLogout' => true
	);
	
	var $config = array(
		'host' => '127.0.0.1', // LDAP server ip
		'bind_dn' => 'uid=%s, ou=admins, dc=internal, dc=local', // %s will be replaced with login username
		'adminUser' => ''
	);
	
	function phpvbAuthLDAP($userConfig = null) {
		if($userConfig) $this->config = array_merge($this->config,$userConfig);
	}
	
	function login($username, $password)
	{
		global $_SESSION;
		
		// Check for LDAP functions
		if(!function_exists('ldap_connect')) {
			
			$ex = 'LDAP support is not enabled in your PHP configuration.';
			
			if(strtolower(substr(PHP_OS, 0, 3)) == 'win') {
				
				ob_start();
				phpinfo(INFO_GENERAL);
				$phpinfo = ob_get_contents();
				ob_end_clean();
				preg_match('/Loaded Configuration File <\/td><td.*?>(.*?)\s*</', $phpinfo, $phpinfo);
				
				$ex .= ' You probably just need to uncomment the line ;extension=php_ldap.dll in php.ini'. 
					(count($phpinfo) > 1 ? ' (' .trim($phpinfo[1]).')' : '') . ' by removing the ";" and restart your web server.';
				
			} else if(strtolower(substr(PHP_OS, 0, 5)) == 'Linux') {
				
				$ex .= ' You probably need to install the php5-ldap (or similar depending on your distribution) package.';	
			
			}
			throw new Exception($ex);
		}

		$auth = ldap_connect($this->config['host']);
		
		if(!$auth) return false;
		
		ldap_set_option($auth,LDAP_OPT_PROTOCOL_VERSION, 3);
		
		if(!@ldap_bind($auth, sprintf($this->config['bind_dn'], $username), $password))
			return false;
		
		
		$_SESSION['valid'] = true;
		$_SESSION['user'] = $username;
		$_SESSION['admin'] = (!$this->config['adminUser']) || ($_SESSION['user'] == $this->config['adminUser']);
		$_SESSION['authCheckHeartbeat'] = time();
		
	}
	
	
	function heartbeat($vbox)
	{
		global $_SESSION;

		$_SESSION['valid'] = true;
		$_SESSION['authCheckHeartbeat'] = time();
	}
	
	function changePassword($old, $new)
	{
	}
	
	function logout(&$response)
	{
		global $_SESSION;
		if(function_exists('session_destroy')) session_destroy();
		else unset($_SESSION['valid']);
		$response['data']['result'] = 1;
	}
	
	function listUsers()
	{
		
	}
	
	function updateUser($vboxRequest, $skipExistCheck)
	{
		
	}
	
	function deleteUser($user)
	{
		
	}
}