1<?php
2/*
3 * $Id: LDAP.php 501 2013-07-11 17:44:37Z imooreyahoo@gmail.com $
4 * Experimental!
5 */
6
7
8class phpvbAuthLDAP implements phpvbAuth {
9
10	var $capabilities = array(
11		'canChangePassword' => false,
12		'canLogout' => true
13	);
14
15	var $config = array(
16		'host' => '127.0.0.1', // LDAP server ip
17		'bind_dn' => 'uid=%s, ou=admins, dc=internal, dc=local', // %s will be replaced with login username
18		'adminUser' => ''
19	);
20
21	function phpvbAuthLDAP($userConfig = null) {
22		if($userConfig) $this->config = array_merge($this->config,$userConfig);
23	}
24
25	function login($username, $password)
26	{
27		global $_SESSION;
28
29		// Check for LDAP functions
30		if(!function_exists('ldap_connect')) {
31
32			$ex = 'LDAP support is not enabled in your PHP configuration.';
33
34			if(strtolower(substr(PHP_OS, 0, 3)) == 'win') {
35
36				ob_start();
37				phpinfo(INFO_GENERAL);
38				$phpinfo = ob_get_contents();
39				ob_end_clean();
40				preg_match('/Loaded Configuration File <\/td><td.*?>(.*?)\s*</', $phpinfo, $phpinfo);
41
42				$ex .= ' You probably just need to uncomment the line ;extension=php_ldap.dll in php.ini'.
43					(count($phpinfo) > 1 ? ' (' .trim($phpinfo[1]).')' : '') . ' by removing the ";" and restart your web server.';
44
45			} else if(strtolower(substr(PHP_OS, 0, 5)) == 'Linux') {
46
47				$ex .= ' You probably need to install the php5-ldap (or similar depending on your distribution) package.';
48
49			}
50			throw new Exception($ex);
51		}
52
53		$auth = ldap_connect($this->config['host']);
54
55		if(!$auth) return false;
56
57		ldap_set_option($auth,LDAP_OPT_PROTOCOL_VERSION, 3);
58
59		if(!@ldap_bind($auth, sprintf($this->config['bind_dn'], $username), $password))
60			return false;
61
62
63		$_SESSION['valid'] = true;
64		$_SESSION['user'] = $username;
65		$_SESSION['admin'] = (!$this->config['adminUser']) || ($_SESSION['user'] == $this->config['adminUser']);
66		$_SESSION['authCheckHeartbeat'] = time();
67
68	}
69
70
71	function heartbeat($vbox)
72	{
73		global $_SESSION;
74
75		$_SESSION['valid'] = true;
76		$_SESSION['authCheckHeartbeat'] = time();
77	}
78
79	function changePassword($old, $new)
80	{
81	}
82
83	function logout(&$response)
84	{
85		global $_SESSION;
86		if(function_exists('session_destroy')) session_destroy();
87		else unset($_SESSION['valid']);
88		$response['data']['result'] = 1;
89	}
90
91	function listUsers()
92	{
93
94	}
95
96	function updateUser($vboxRequest, $skipExistCheck)
97	{
98
99	}
100
101	function deleteUser($user)
102	{
103
104	}
105}
106