Erik Thuning 692c2e0aeb Broke all classes into separate files and activated class autoloading.
All free functions are now in functions.php
2019-06-11 17:05:48 +02:00

45 lines
1.3 KiB
PHP

<?php
class Ldap {
private $conn;
private $base_dn = "dc=su,dc=se";
public function __construct() {
$this->conn = ldap_connect('ldaps://ldap.su.se');
ldap_set_option($this->conn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_bind($this->conn);
}
private function search($term, ...$attributes) {
$result = ldap_search($this->conn, $this->base_dn, $term, $attributes);
return ldap_get_entries($this->conn, $result);
}
public function get_user($uid) {
$data = $this->search("uid=$uid", 'cn', 'uid');
if($data['count'] !== 1) {
throw new Exception("LDAP search for '$uid' did not return exactly one result");
}
return $data[0]['cn'][0];
}
public function get_user_email($uid) {
$data = $this->search("uid=$uid", 'mail', 'uid');
if($data['count'] !== 1) {
throw new Exception("LDAP search for '$uid' did not return exactly one result");
}
return $data[0]['mail'][0];
}
public function search_user($uid) {
$data = $this->search("uid=$uid", 'cn', 'uid');
$out = array();
foreach($data as $result) {
if(isset($result['uid'])) {
$out[$result['uid'][0]] = $result['cn'][0];
}
}
return $out;
}
}
?>