116 lines
3.6 KiB
PHP
116 lines
3.6 KiB
PHP
<?php
|
|
abstract class Responder {
|
|
protected $fragments = array();
|
|
protected $ldap = null;
|
|
protected $logged_in_user = null;
|
|
|
|
public function __construct() {
|
|
global $language, $required_entitlements;
|
|
|
|
$this->ldap = new Ldap();
|
|
|
|
$this->authorized = false;
|
|
$entitlements = explode(';', $_SERVER['entitlement']);
|
|
foreach($entitlements as $entitlement) {
|
|
if(in_array($entitlement, $required_entitlements)) {
|
|
$this->authorized = true;
|
|
}
|
|
}
|
|
|
|
$remote_user = explode('@', $_SERVER['REMOTE_USER'])[0];
|
|
$this->logged_in_user = $this->user_init($remote_user, null);
|
|
$this->fragments = get_fragments("./html/$language/fragments.html");
|
|
}
|
|
|
|
public function respond() {
|
|
if(!$this->authorized) {
|
|
die("Unauthorized.");
|
|
}
|
|
return $this->render();
|
|
}
|
|
|
|
abstract public function render();
|
|
|
|
final protected function user_init($name, $email) {
|
|
$nameuser = null;
|
|
$emailuser = null;
|
|
if($name) {
|
|
try {
|
|
$nameuser = new User($name, 'name');
|
|
} catch(Exception $ue) {
|
|
# The user wasn't found locally
|
|
try {
|
|
$this->ldap->get_user($name);
|
|
$nameuser = User::create_user($name);
|
|
} catch(Exception $le) {
|
|
$err = i18n("Username {name} not found.",
|
|
$name);
|
|
throw new Exception($err);
|
|
}
|
|
}
|
|
}
|
|
if($email) {
|
|
try {
|
|
$search = $email;
|
|
if(strpos($email, '@') === false) {
|
|
$search = $email .'@dsv.su.se';
|
|
}
|
|
# Lookup email directly in ldap since we don't store it
|
|
$uid = $this->ldap->search_email($search);
|
|
} catch(Exception $le) {
|
|
$err = i18n('Email address {address} not found.',
|
|
$search);
|
|
throw new Exception($err);
|
|
}
|
|
try {
|
|
$emailuser = new User($uid, 'name');
|
|
} catch(Exception $ue) {
|
|
# User wasn't found locally, so initialize a new user
|
|
$emailuser = User::create_user($uid);
|
|
}
|
|
}
|
|
if($nameuser && $emailuser) {
|
|
if($nameuser != $emailuser) {
|
|
$err = i18n('Username and email match different users.');
|
|
throw new Exception($err);
|
|
}
|
|
return $nameuser;
|
|
}
|
|
if($nameuser) {
|
|
return $nameuser;
|
|
}
|
|
return $emailuser;
|
|
}
|
|
|
|
final protected function escape_tags($tags) {
|
|
foreach($tags as $key => $tag) {
|
|
$tags[$key] = $this->escape_string(strtolower($tag));
|
|
}
|
|
return $tags;
|
|
}
|
|
|
|
final protected function unescape_tags($tags) {
|
|
foreach($tags as $key => $tag) {
|
|
$tags[$key] = $this->unescape_string(strtolower($tag));
|
|
}
|
|
return $tags;
|
|
}
|
|
|
|
final protected function escape_string($string) {
|
|
return str_replace(array("'",
|
|
'"'),
|
|
array(''',
|
|
'"'),
|
|
$string);
|
|
}
|
|
|
|
final protected function unescape_string($string) {
|
|
return str_replace(array(''',
|
|
'"'),
|
|
array("'",
|
|
'"'),
|
|
$string);
|
|
}
|
|
}
|
|
?>
|