2006-04-26 08:01:21 +02:00
|
|
|
<?php
|
|
|
|
/**************************************************************************\
|
|
|
|
* eGroupWare API - Accounts manager for LDAP *
|
|
|
|
* This file written by Lars Kneschke <l.kneschke@metaways.de> *
|
|
|
|
* View and manipulate contact records using LDAP *
|
|
|
|
* ------------------------------------------------------------------------ *
|
|
|
|
* This library is part of the eGroupWare API *
|
|
|
|
* http://www.egroupware.org/api *
|
|
|
|
* -------------------------------------------- *
|
|
|
|
* This program is free software; you can redistribute it and/or modify it *
|
|
|
|
* under the terms of the GNU General Public License as published by the *
|
|
|
|
* Free Software Foundation; version 2 of the License. *
|
|
|
|
\**************************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@class contacts
|
|
|
|
@abstract Contact List System
|
|
|
|
@discussion Author: jengo/Milosch <br>
|
|
|
|
This class provides a contact database scheme. <br>
|
|
|
|
It attempts to be based on the vcard 2.1 standard, with mods as needed to make for more reasonable sql storage. <br>
|
|
|
|
The LDAP schema used here may require installation of schema files available in the phpgwapi/doc/ldap dir.
|
|
|
|
Please see the README file there.
|
|
|
|
Syntax: CreateObject('phpgwapi.contacts'); <br>
|
|
|
|
Example1: $contacts = CreateObject('phpgwapi.contacts');
|
|
|
|
*/
|
|
|
|
class ldap
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var resource $ds holds the LDAP link identifier
|
|
|
|
*/
|
|
|
|
var $ds;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array $ldapServerInfo holds the detected information about the different ldap servers
|
|
|
|
*/
|
|
|
|
var $ldapServerInfo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the constructor for this class
|
|
|
|
*/
|
|
|
|
function ldap() {
|
|
|
|
$this->restoreSessionData();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* escapes a string for use in searchfilters meant for ldap_search.
|
|
|
|
*
|
|
|
|
* Escaped Characters are: '*', '(', ')', ' ', '\', NUL
|
|
|
|
* It's actually a PHP-Bug, that we have to escape space.
|
|
|
|
* For all other Characters, refer to RFC2254.
|
|
|
|
* @param $string either a string to be escaped, or an array of values to be escaped
|
|
|
|
*/
|
|
|
|
function getLDAPServerInfo($_host)
|
|
|
|
{
|
|
|
|
if(is_a($this->ldapServerInfo[$_host], 'ldapserverinfo')) {
|
|
|
|
return $this->ldapServerInfo[$_host];
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* escapes a string for use in searchfilters meant for ldap_search.
|
|
|
|
*
|
|
|
|
* Escaped Characters are: '*', '(', ')', ' ', '\', NUL
|
|
|
|
* It's actually a PHP-Bug, that we have to escape space.
|
|
|
|
* For all other Characters, refer to RFC2254.
|
2006-06-13 06:22:24 +02:00
|
|
|
*
|
|
|
|
* @param string/array $string either a string to be escaped, or an array of values to be escaped
|
2006-04-26 08:01:21 +02:00
|
|
|
*/
|
2006-06-13 06:22:24 +02:00
|
|
|
function quote($string)
|
2006-04-26 08:01:21 +02:00
|
|
|
{
|
|
|
|
return str_replace(array('\\','*','(',')','\0',' '),array('\\\\','\*','\(','\)','\\0','\20'),$string);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* connect to the ldap server and return a handle
|
|
|
|
*
|
|
|
|
* @param $host ldap host
|
|
|
|
* @param $dn ldap dn
|
|
|
|
* @param $passwd ldap pw
|
|
|
|
*/
|
|
|
|
function ldapConnect($host='', $dn='', $passwd='')
|
|
|
|
{
|
|
|
|
if(!function_exists('ldap_connect'))
|
|
|
|
{
|
|
|
|
/* log does not exist in setup(, yet) */
|
2008-03-21 21:50:13 +01:00
|
|
|
if(isset($GLOBALS['egw']->log))
|
2006-04-26 08:01:21 +02:00
|
|
|
{
|
|
|
|
$GLOBALS['egw']->log->message('F-Abort, LDAP support unavailable');
|
|
|
|
$GLOBALS['egw']->log->commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
printf('<b>Error: LDAP support unavailable</b><br>',$host);
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
if(!$host)
|
|
|
|
{
|
|
|
|
$host = $GLOBALS['egw_info']['server']['ldap_host'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$dn)
|
|
|
|
{
|
|
|
|
$dn = $GLOBALS['egw_info']['server']['ldap_root_dn'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$passwd)
|
|
|
|
{
|
|
|
|
$passwd = $GLOBALS['egw_info']['server']['ldap_root_pw'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// connects to ldap server
|
2006-06-16 06:54:55 +02:00
|
|
|
if(!$this->ds = ldap_connect($host))
|
2006-04-26 08:01:21 +02:00
|
|
|
{
|
|
|
|
/* log does not exist in setup(, yet) */
|
2008-03-21 21:50:13 +01:00
|
|
|
if(isset($GLOBALS['egw']->log))
|
2006-04-26 08:01:21 +02:00
|
|
|
{
|
|
|
|
$GLOBALS['egw']->log->message('F-Abort, Failed connecting to LDAP server');
|
|
|
|
$GLOBALS['egw']->log->commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("<b>Error: Can't connect to LDAP server %s!</b><br>",$host);
|
|
|
|
echo function_backtrace(1);
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2006-10-28 10:38:17 +02:00
|
|
|
if(ldap_set_option($this->ds, LDAP_OPT_PROTOCOL_VERSION, 3)) {
|
|
|
|
$supportedLDAPVersion = 3;
|
|
|
|
} else {
|
|
|
|
$supportedLDAPVersion = 2;
|
|
|
|
}
|
|
|
|
|
2006-04-26 08:01:21 +02:00
|
|
|
if(!isset($this->ldapServerInfo[$host])) {
|
2006-10-28 10:38:17 +02:00
|
|
|
//error_log("no ldap server info found");
|
2007-05-08 13:44:43 +02:00
|
|
|
$ldapbind = @ldap_bind($this->ds, $GLOBALS['egw_info']['server']['ldap_root_dn'], $GLOBALS['egw_info']['server']['ldap_root_pw']);
|
2006-10-28 10:38:17 +02:00
|
|
|
|
2006-04-26 08:01:21 +02:00
|
|
|
$filter='(objectclass=*)';
|
|
|
|
$justthese = array('structuralObjectClass','namingContexts','supportedLDAPVersion','subschemaSubentry');
|
|
|
|
|
2006-06-24 17:52:42 +02:00
|
|
|
if(($sr = @ldap_read($this->ds, '', $filter, $justthese))) {
|
2006-04-26 08:01:21 +02:00
|
|
|
if($info = ldap_get_entries($this->ds, $sr)) {
|
|
|
|
$ldapServerInfo = new ldapserverinfo();
|
|
|
|
|
2006-10-28 10:38:17 +02:00
|
|
|
$ldapServerInfo->setVersion($supportedLDAPVersion);
|
2006-04-26 08:01:21 +02:00
|
|
|
|
|
|
|
// check for naming contexts
|
|
|
|
if($info[0]['namingcontexts']) {
|
|
|
|
for($i=0; $i<$info[0]['namingcontexts']['count']; $i++) {
|
|
|
|
$namingcontexts[] = $info[0]['namingcontexts'][$i];
|
|
|
|
}
|
|
|
|
$ldapServerInfo->setNamingContexts($namingcontexts);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for ldap server type
|
|
|
|
if($info[0]['structuralobjectclass']) {
|
|
|
|
switch($info[0]['structuralobjectclass'][0]) {
|
|
|
|
case 'OpenLDAProotDSE':
|
|
|
|
$ldapServerType = OPENLDAP_LDAPSERVER;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$ldapServerType = UNKNOWN_LDAPSERVER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$ldapServerInfo->setServerType($ldapServerType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for subschema entry dn
|
|
|
|
if($info[0]['subschemasubentry']) {
|
|
|
|
$subschemasubentry = $info[0]['subschemasubentry'][0];
|
|
|
|
$ldapServerInfo->setSubSchemaEntry($subschemasubentry);
|
|
|
|
}
|
2006-10-28 10:38:17 +02:00
|
|
|
|
2006-04-26 08:01:21 +02:00
|
|
|
// create list of supported objetclasses
|
|
|
|
if(!empty($subschemasubentry)) {
|
|
|
|
$filter='(objectclass=*)';
|
|
|
|
$justthese = array('objectClasses');
|
|
|
|
|
|
|
|
if($sr=ldap_read($this->ds, $subschemasubentry, $filter, $justthese)) {
|
|
|
|
if($info = ldap_get_entries($this->ds, $sr)) {
|
|
|
|
if($info[0]['objectclasses']) {
|
|
|
|
for($i=0; $i<$info[0]['objectclasses']['count']; $i++) {
|
|
|
|
$pattern = '/^\( (.*) NAME \'(\w*)\' /';
|
|
|
|
if(preg_match($pattern, $info[0]['objectclasses'][$i], $matches)) {
|
2006-10-28 10:38:17 +02:00
|
|
|
#_debug_array($matches);
|
2006-04-26 08:01:21 +02:00
|
|
|
if(count($matches) == 3) {
|
|
|
|
$supportedObjectClasses[$matches[1]] = strtolower($matches[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-28 10:38:17 +02:00
|
|
|
|
2006-04-26 08:01:21 +02:00
|
|
|
$ldapServerInfo->setSupportedObjectClasses($supportedObjectClasses);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-28 10:38:17 +02:00
|
|
|
|
2006-04-26 08:01:21 +02:00
|
|
|
$this->ldapServerInfo[$host] = $ldapServerInfo;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->ldapServerInfo[$host] = false;
|
|
|
|
}
|
|
|
|
$this->saveSessionData();
|
|
|
|
} else {
|
|
|
|
$ldapServerInfo = $this->ldapServerInfo[$host];
|
|
|
|
}
|
|
|
|
|
2007-05-08 13:44:43 +02:00
|
|
|
if(!@ldap_bind($this->ds, $dn, $passwd)) {
|
2008-03-21 21:50:13 +01:00
|
|
|
if(isset($GLOBALS['egw']->log)) {
|
2006-04-26 08:01:21 +02:00
|
|
|
$GLOBALS['egw']->log->message('F-Abort, Failed binding to LDAP server');
|
|
|
|
$GLOBALS['egw']->log->commit();
|
|
|
|
}
|
|
|
|
|
2007-05-08 13:44:43 +02:00
|
|
|
printf("<b>Error: Can't bind to LDAP server: %s!</b> %s<br />",$dn,function_backtrace(1));
|
2006-04-26 08:01:21 +02:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->ds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* disconnect from the ldap server
|
|
|
|
*/
|
2006-06-08 00:58:11 +02:00
|
|
|
function ldapDisconnect()
|
|
|
|
{
|
|
|
|
if(is_resource($this->ds))
|
|
|
|
{
|
2006-04-26 08:01:21 +02:00
|
|
|
ldap_unbind($this->ds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* restore the session data
|
|
|
|
*/
|
2006-06-08 00:58:11 +02:00
|
|
|
function restoreSessionData()
|
|
|
|
{
|
2008-03-21 21:50:13 +01:00
|
|
|
if (isset($GLOBALS['egw']->session)) // no availible in setup
|
2006-06-08 00:58:11 +02:00
|
|
|
{
|
2008-08-29 14:43:30 +02:00
|
|
|
$this->ldapServerInfo = (array) unserialize($GLOBALS['egw']->session->appsession('ldapServerInfo'));
|
2006-06-08 00:58:11 +02:00
|
|
|
}
|
2006-04-26 08:01:21 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* save the session data
|
|
|
|
*/
|
2006-06-08 00:58:11 +02:00
|
|
|
function saveSessionData()
|
|
|
|
{
|
2008-03-21 21:50:13 +01:00
|
|
|
if (isset($GLOBALS['egw']->session)) // no availible in setup
|
2006-06-08 00:58:11 +02:00
|
|
|
{
|
2008-08-29 14:43:30 +02:00
|
|
|
$GLOBALS['egw']->session->appsession('ldapServerInfo','',serialize($this->ldapServerInfo));
|
2006-06-08 00:58:11 +02:00
|
|
|
}
|
2006-04-26 08:01:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
?>
|