Page MenuHomec4science

lib_sel.php
No OneTemporary

File Metadata

Created
Tue, Jan 7, 03:33

lib_sel.php

<?php
class EPFL_LDAP {
protected $ldap_host = null;
protected $ldap_port = null;
protected $ldap_connector = null;
protected $ldap_dn = null;
protected $ldap_attributes = null;
protected $ldap_entries = null;
protected $formatted_entries = null;
public function __construct($ldap_host = null, $ldap_port = null) {
if (is_null($ldap_host)) $ldap_host = 'ldap.epfl.ch';
if (is_null($ldap_port)) $ldap_port = 389;
$this->ldap_port = $ldap_port;
$this->ldap_host = $ldap_host;
$this->ldap_dn = "o=epfl,c=ch";
$this->ldap_connector = ldap_connect($ldap_host, $ldap_port) or die("Could not connect to $ldap_host:$ldap_port.");
$this->ldap_attributes = array('personaltitle', 'uniqueidentifier', 'mail', 'sn', 'givenname', 'edupersonaffiliation', 'ou', 'title');
}
protected function find_by_filter($filter, $limit) {
$ldap_results = null;
$this->ldap_entries = null;
$this->formatted_entries = null;
$ldap_results = @ldap_search($this->ldap_connector, $this->ldap_dn, $filter, $this->ldap_attributes, 0, $limit);
#$ldap_results = @ldap_search($this->ldap_connector, $this->ldap_dn, $filter);
$this->ldap_entries = ldap_get_entries($this->ldap_connector, $ldap_results);
$count = $this->ldap_entries['count'];
if ($count) {
$this->format_entries();
return $count;
}
return false;
}
public function find_by_sciper($sciper, $type = null, $limit = 50) {
if (is_null($sciper) or !is_numeric($sciper) or ($sciper < 100000) or ($sciper > 999999))
throw new Exception("Invalid sciper: '$sciper'");
$filter = 'uniqueIdentifier='.$sciper;
if (!is_null($type)) {
$filter = "(&($filter)(eduPersonAffiliation=$type))";
}
return $this->find_by_filter($filter, $limit);
}
public function find_by_givenname($name, $limit = 50) {
$filter = 'givenname='.$name;
return $this->find_by_filter($filter, $limit);
}
public function find_by_surname($name, $limit = 50) {
$filter = 'sn='.$name;
return $this->find_by_filter($filter, $limit);
}
public function find_by_ldap_filter($filter, $limit = 50) {
return $this->find_by_filter($filter, $limit);
}
protected function format_entries() {
$this->formatted_entries = array();
foreach ($this->ldap_entries as $entry) {
if (! is_array($entry)) continue;
$formatted_entry = array( 'found' => 1 );
# Sex
switch ($entry['personaltitle'][0]) {
case 'Madame':
$formatted_entry['sex'] = 'F';
break;
case 'Monsieur':
$formatted_entry['sex'] = 'M';
break;
default:
$formatted_entry['sex'] = 'X';
}
# SCIPER
$formatted_entry['sciper'] = $entry['uniqueidentifier'][0];
# email address
if (!array_key_exists('mail', $entry)) {
$formatted_entry['email'] = $formatted_entry['sciper'].'@epfl.ch';
} else {
$formatted_entry['email'] = $entry['mail'][0];
}
# Surname
$i = null;
if (array_key_exists(2, $entry['sn'])) {
$i = 2;
} elseif (array_key_exists(0, $entry['sn'])) {
$i = 0;
}
if (is_null($i)) {
$formatted_entry['surname'] = 'X';
} else {
$formatted_entry['surname'] = $entry['sn'][$i];
}
# Givenname
$i = null;
if (array_key_exists(2, $entry['givenname'])) {
$i = 2;
} elseif (array_key_exists(0, $entry['givenname'])) {
$i = 0;
}
if (is_null($i)) {
$givenname = 'X';
} else {
$givenname = $entry['givenname'][$i];
}
$email_givenname_size = strlen(explode('.', $formatted_entry['email'])[0]);
$givennames = explode(' ', $givenname);
$i = 1;
$final_givenname = $givennames[0];
$max = count($givennames)-1;
while ((mb_strlen($final_givenname, 'UTF-8') < $email_givenname_size) and ($i <= $max)) {
$final_givenname .= ' '.$givennames[$i];
$i++;
}
# if email as a '-', put it back in the given name is missing
if (preg_match('/-/', explode('.', $formatted_entry['email'])[0]) and preg_match('/ /', $final_givenname)) {
$final_givenname = preg_replace('/ /', '-', $final_givenname);
}
$formatted_entry['givenname'] = $final_givenname;
$formatted_entry['name'] = $formatted_entry['surname']." ".$formatted_entry['givenname'];
# Type
$formatted_entry['type'] = $entry['edupersonaffiliation'][0];
# Details
switch ($formatted_entry['type']) {
case 'student':
if (array_key_exists(0, $entry['ou'])) {
if (preg_match('/-/', $entry['ou'][0])) {
$formatted_entry['section'] = explode('-', $entry['ou'][0])[0];
$formatted_entry['cursus'] = explode('-', $entry['ou'][0])[1];
} else {
$formatted_entry['section'] = $entry['ou'][0];
$formatted_entry['cursus'] = 'XX';
}
} else {
$formatted_entry['section'] = 'XXX';
$formatted_entry['cursus'] = 'XX';
}
break;
case 'staff':
if (array_key_exists('ou', $entry)) {
if (array_key_exists(0, $entry['ou'])) $formatted_entry['where'] = $entry['ou'][0];
if (array_key_exists(1, $entry['ou'])) $formatted_entry['where_1'] = $entry['ou'][1];
}
if (array_key_exists('title', $entry)) {
if (array_key_exists(0, $entry['title'])) $formatted_entry['title'] = $entry['title'][0];
if (array_key_exists(1, $entry['title'])) $formatted_entry['title_1'] = $entry['title'][1];
}
break;
}
# Add the newly formatted entry
$this->formatted_entries[] = $formatted_entry;
}
}
public function get_entry() {
if (!is_null($this->formatted_entries) and array_key_exists(0, $this->formatted_entries))
return $this->formatted_entries[0];
else
return null;
}
public function get_entries() {
return $this->formatted_entries;
}
}
class Exporter {
public function to_bash($array) {
if (is_array($array))
foreach($array as $key => $value) echo "$key:$value\n";
}
}
?>

Event Timeline