FS-Mod/FSMod/FSModLdap.php

60 lines
1.8 KiB
PHP

<?php
/**
*
* @file
* @ingroup Extensions
* @author Bennet Bleßmann
* @copyright © 2021 Bennet Bleßmann
* @license GNU General Public Licence 2.0 or later
*/
if( !defined( 'MEDIAWIKI' ) ) {
echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
die( 1 );
}
class FSModLdap {
static function perform_search($filter): ?array {
global $wgLdapBase, $wgLdapServer, $wgLdapPwd, $wgLdapUser;
try {
if (!$ldap_con = ldap_connect($wgLdapServer)) {
throw new Exception('Could not connect to ldap server.');
}
ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);
if (!ldap_start_tls($ldap_con)) {
throw new Exception('Could not start TLS');
}
if (!$ldap_bd = ldap_bind($ldap_con, $wgLdapUser, $wgLdapPwd)) {
throw new Exception('Could not bind to server. Error is ' . ldap_error($ldap_con));
}
// the attributes that shall be returned, empty list results in all
$attributes = [];
$attributes_only = 0; // default
$sizelimit = -1; // default
$timelimit = -1; // default
$deref = LDAP_DEREF_NEVER; // default
$controls = null; // default
if (!$result = ldap_search($ldap_con, $wgLdapBase, $filter , $attributes, $attributes_only, $sizelimit , $timelimit , $deref, $controls)) {
throw new Exception('Error in query.');
}
return ldap_get_entries($ldap_con, $result) ?: null;
} catch (Exception $e) {
return null;
} finally {
// finally wir auch nach return noch ausgeführt
if ($ldap_con) {
ldap_close($ldap_con);
}
}
}
}