我想在PHP中创建一个基于Web的应用程序,它接收LDAP请求并发回LDAP响应,但实际上并不使用LDAP服务器.具体来说,我想将MySQL数据库中的联系人表作为LDAP地址簿提供给Thunderbird.
两个问题:
是否有用于使用PHP实现LDAP服务器的现有库?(PHP_LDAP包用于创建LDAP客户端,PHP应用程序连接到现有LDAP服务器.)
LDAP数据实际上是如何从客户端进入我的脚本的?LDAP是否通过HTTP传输?请求将显示在以下位置:
$HTTP_RAW_POST_DATA
Run Code Online (Sandbox Code Playgroud)或者类似的?Apache可以处理LDAP请求并将它们传递到我的脚本中,还是一个完全不同的协议,需要一个不同的"监听器"应用程序来处理?
可以使用这个库创建一个纯 PHP LDAP 服务器(我最初为 LDAP 客户端编写它):
https://github.com/FreeDSx/LDAP
它基于客户端请求的请求处理程序(只是一个接口)工作。基本上,您扩展了一个将处理客户端请求并发送回响应的类(无论如何在搜索的情况下)。一个基本的例子:
namespace Foo;
use FreeDSx\Ldap\Server\RequestHandler\GenericRequestHandler;
class LdapRequestHandler extends GenericRequestHandler
{
/**
* @var array
*/
protected $users = [
'user' => '12345',
];
/**
* Validates the username/password of a simple bind request
*
* @param string $username
* @param string $password
* @return bool
*/
public function bind(string $username, string $password): bool
{
return isset($this->users[$username]) && $this->users[$username] === $password;
}
/**
* Override the search request. This must send back an entries object.
*
* @param RequestContext $context
* @param SearchRequest $search
* @return Entries
*/
public function search(RequestContext $context, SearchRequest $search): Entries
{
// Do your logic here with the search request, return entries...
return new Entries(
Entry::create('cn=Foo,dc=FreeDSx,dc=local', [
'cn' => 'Foo',
'sn' => 'Bar',
'givenName' => 'Foo',
]),
Entry::create('cn=Chad,dc=FreeDSx,dc=local', [
'cn' => 'Chad',
'sn' => 'Sikorra',
'givenName' => 'Chad',
])
);
}
}
Run Code Online (Sandbox Code Playgroud)
use FreeDSx\Ldap\LdapServer;
use Foo\LdapRequestHandler;
$server = new LdapServer([ 'request_handler' => LdapRequestHandler::class ]);
$server->run();
Run Code Online (Sandbox Code Playgroud)
这里有更多关于库的服务器组件的文档:
https://github.com/FreeDSx/LDAP/tree/master/docs/Server
对此有几点警告:
LDAP 协议不是由 Apache 本地处理的,而且我还没有看到任何处理该协议的 Apache 模块。我不相信您能够通过 Apache 使用 PHP 来完成此操作。您也许能够实现一个纯 PHP 服务器(请参阅http://php.net/manual/en/function.stream-socket-server.php),然后在 PHP 中实现 LDAP 协议数据包解析器。我不相信 PHP 有一个原生的 ASN1 解析器,但你也许可以找到一个 C 语言的解析器并以某种方式集成它。