Use a Config class for parse config file
This commit is contained in:
parent
d860c69d27
commit
24fabced51
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
class Config {
|
||||
private static $name='Evoadmin Mail', $mail='root@localhost', $log_level='error', $ldap=array();
|
||||
public static function load($file) {
|
||||
$ini = parse_ini_file($file, true);
|
||||
self::$name = $ini['global']['name'];
|
||||
self::$mail = $ini['global']['mail'];
|
||||
self::$log_level = $ini['global']['log_level'];
|
||||
self::$ldap = $ini['ldap'];
|
||||
}
|
||||
|
||||
public static function getName() {
|
||||
return self::$name;
|
||||
}
|
||||
|
||||
public static function getMail() {
|
||||
return self::$mail;
|
||||
}
|
||||
|
||||
public static function getLogLevel() {
|
||||
return self::$log_level;
|
||||
}
|
||||
|
||||
public static function getLdapUri() {
|
||||
return 'ldap://'.self::$ldap['host'].':'.self::$ldap['port'];
|
||||
}
|
||||
|
||||
public static function getLdapDN() {
|
||||
return self::$ldap['admin_dn'];
|
||||
}
|
||||
|
||||
public static function getLdapPass() {
|
||||
return self::$ldap['admin_pass'];
|
||||
}
|
||||
|
||||
public static function getLdapBase() {
|
||||
return self::$ldap['base'];
|
||||
}
|
||||
|
||||
public static function getSuperadmin() {
|
||||
return self::$ldap['superadmin'];
|
||||
}
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
<?php
|
||||
|
||||
class DefaultController {
|
||||
protected static $config=array(), $alerts=array(),$server;
|
||||
protected static $alerts=array(),$server;
|
||||
public static function init() {
|
||||
self::$config = parse_ini_file('../config/config.ini', true);
|
||||
|
||||
Logger::configure(self::$config['global']['log_level']);
|
||||
MailNotify::configure(self::$config['global']);
|
||||
Config::load('../config/config.ini');
|
||||
|
||||
Logger::init();
|
||||
MailNotify::init();
|
||||
|
||||
session_name('EVOADMIN_SESS');
|
||||
session_start();
|
||||
|
@ -14,7 +14,7 @@ class DefaultController {
|
|||
// Get content from LDAP
|
||||
if (!empty($_SESSION['login'])) {
|
||||
try {
|
||||
self::$server = new LdapServer($_SESSION['login'], self::$config['ldap']);
|
||||
self::$server = new LdapServer($_SESSION['login']);
|
||||
} catch (Exception $e) {
|
||||
self::$alerts[] = array('type' => 2, 'message' => $e->getMessage());
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ class DefaultController {
|
|||
'login' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_STRIP_HIGH)
|
||||
,'password' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_STRIP_HIGH)
|
||||
));
|
||||
self::$server = new LdapServer($input['login'], self::$config['ldap']);
|
||||
self::$server = new LdapServer($input['login']);
|
||||
self::$server->login($input['password']);
|
||||
$_SESSION['login'] = self::$server->getLogin();
|
||||
} catch (Exception $e) {
|
||||
|
|
|
@ -54,24 +54,24 @@ class LdapServer {
|
|||
return '{SSHA}'.base64_encode(mhash(MHASH_SHA1, $pass.$salt).$salt);
|
||||
}
|
||||
|
||||
public function __construct($login, $config) {
|
||||
$uri = 'ldap://'.$config['host'].':'.$config['port'];
|
||||
public function __construct($login) {
|
||||
$uri = Config::getLdapUri();
|
||||
$this->login = $login;
|
||||
if (!$this->conn = ldap_connect($uri)) {
|
||||
throw new Exception("Impossible de se connecter au serveur LDAP ".$config['host']);
|
||||
throw new Exception("Impossible de se connecter au serveur LDAP ".$uri);
|
||||
}
|
||||
if (!ldap_set_option($this->conn, LDAP_OPT_PROTOCOL_VERSION, 3)) {
|
||||
throw new Exception("Impossible de modifier la version du protocole LDAP à 3");
|
||||
}
|
||||
if (!ldap_bind($this->conn, $config['admin_dn'], $config['admin_pass'])) {
|
||||
if (!ldap_bind($this->conn, Config::getLdapDN(), Config::getLdapPass())) {
|
||||
throw new Exception("Authentification LDAP échoué !");
|
||||
}
|
||||
if (in_array($this->login, $config['superadmin'])) {
|
||||
if (in_array($this->login, Config::getSuperAdmin())) {
|
||||
$this->superadmin = true;
|
||||
$this->base = $config['base'];
|
||||
$this->base = Config::getLdapBase();
|
||||
} else {
|
||||
$mydomain = preg_replace('/.*@/', '', $login);
|
||||
$this->base = LdapDomain::$dn.'='.$mydomain.','.$config['base'];
|
||||
$this->base = LdapDomain::$dn.'='.$mydomain.','.Config::getBaseDN();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,8 +10,8 @@ class Logger {
|
|||
|
||||
private static $level;
|
||||
|
||||
public static function configure($loglevel) {
|
||||
switch ($loglevel) {
|
||||
public static function init() {
|
||||
switch (Config::getLogLevel()) {
|
||||
case 'critical':
|
||||
self::$level = self::CRITICAL;
|
||||
case 'error':
|
||||
|
|
|
@ -5,13 +5,13 @@ require_once 'Twig/autoload.php';
|
|||
class MailNotify {
|
||||
private static $twig, $adminmail;
|
||||
|
||||
public static function configure($config) {
|
||||
public static function init() {
|
||||
$loader = new Twig_Loader_Filesystem('tpl/mail');
|
||||
self::$twig = new Twig_Environment($loader, array(
|
||||
'cache' => false
|
||||
));
|
||||
|
||||
self::$adminmail = !empty($config['mail']) ? $config['mail'] : 'root@localhost';
|
||||
self::$adminmail = Config::getMail();
|
||||
}
|
||||
|
||||
public static function addDomain($domain) {
|
||||
|
|
|
@ -4,6 +4,7 @@ require_once 'Twig/autoload.php';
|
|||
|
||||
class PageController extends DefaultController {
|
||||
private static $twig, $params=array(), $domain, $account, $alias;
|
||||
|
||||
public static function init() {
|
||||
$loader = new Twig_Loader_Filesystem('tpl/page');
|
||||
self::$twig = new Twig_Environment($loader, array(
|
||||
|
@ -38,7 +39,7 @@ class PageController extends DefaultController {
|
|||
if (!self::$server->isSuperAdmin()) {
|
||||
self::$alerts[] = array('type' => 2, 'message' => "Super Administrateur seulement !");
|
||||
print self::$twig->render('403.html', array(
|
||||
'page_name' => self::$config['global']['name']
|
||||
'page_name' => Config::getName()
|
||||
,'alerts' => self::$alerts
|
||||
,'login' => self::$server->getLogin()
|
||||
,'isSuperAdmin' => self::$server->isSuperAdmin()
|
||||
|
@ -86,7 +87,7 @@ class PageController extends DefaultController {
|
|||
|
||||
private static function login() {
|
||||
print self::$twig->render('login.html', array(
|
||||
'page_name' => self::$config['global']['name'].' - Login'
|
||||
'page_name' => Config::getName().' - Login'
|
||||
,'alerts' => self::$alerts
|
||||
,'logout' => false
|
||||
));
|
||||
|
@ -96,7 +97,7 @@ class PageController extends DefaultController {
|
|||
session_unset('EVOADMIN_SESS');
|
||||
session_destroy();
|
||||
print self::$twig->render('login.html', array(
|
||||
'page_name' => self::$config['global']['name'].' - Login'
|
||||
'page_name' => Config::getName().' - Login'
|
||||
,'alerts' => self::$alerts
|
||||
,'logout' => true
|
||||
));
|
||||
|
@ -104,11 +105,11 @@ class PageController extends DefaultController {
|
|||
|
||||
private static function help() {
|
||||
print self::$twig->render('help.html', array(
|
||||
'page_name' => self::$config['global']['name']
|
||||
'page_name' => Config::getName()
|
||||
,'alerts' => self::$alerts
|
||||
,'login' => self::$server->getLogin()
|
||||
,'isSuperAdmin' => self::$server->isSuperAdmin()
|
||||
,'notif_mail' => self::$config['global']['mail']
|
||||
,'notif_mail' => Config::getMail()
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -141,7 +142,7 @@ class PageController extends DefaultController {
|
|||
private static function addDomain() {
|
||||
self::needSuperAdmin();
|
||||
print self::$twig->render('add_domain.html', array(
|
||||
'page_name' => self::$config['global']['name']
|
||||
'page_name' => Config::getName()
|
||||
,'alerts' => self::$alerts
|
||||
,'login' => self::$server->getLogin()
|
||||
,'isSuperAdmin' => self::$server->isSuperAdmin()
|
||||
|
@ -150,7 +151,7 @@ class PageController extends DefaultController {
|
|||
|
||||
private static function listDomains() {
|
||||
print self::$twig->render('list_domain.html', array(
|
||||
'page_name' => self::$config['global']['name']
|
||||
'page_name' => Config::getName()
|
||||
,'alerts' => self::$alerts
|
||||
,'login' => self::$server->getLogin()
|
||||
,'isSuperAdmin' => self::$server->isSuperAdmin()
|
||||
|
@ -160,7 +161,7 @@ class PageController extends DefaultController {
|
|||
|
||||
private static function listAccounts() {
|
||||
print self::$twig->render('list_account.html', array(
|
||||
'page_name' => self::$config['global']['name']
|
||||
'page_name' => Config::getName()
|
||||
,'alerts' => self::$alerts
|
||||
,'login' => self::$server->getLogin()
|
||||
,'isSuperAdmin' => self::$server->isSuperAdmin()
|
||||
|
@ -173,7 +174,7 @@ class PageController extends DefaultController {
|
|||
|
||||
private static function listAlias() {
|
||||
print self::$twig->render('list_alias.html', array(
|
||||
'page_name' => self::$config['global']['name']
|
||||
'page_name' => Config::getName()
|
||||
,'alerts' => self::$alerts
|
||||
,'login' => self::$server->getLogin()
|
||||
,'isSuperAdmin' => self::$server->isSuperAdmin()
|
||||
|
@ -186,7 +187,7 @@ class PageController extends DefaultController {
|
|||
|
||||
private static function Account() {
|
||||
print self::$twig->render('account.html', array(
|
||||
'page_name' => self::$config['global']['name']
|
||||
'page_name' => Config::getName()
|
||||
,'alerts' => self::$alerts
|
||||
,'login' => self::$server->getLogin()
|
||||
,'isSuperAdmin' => self::$server->isSuperAdmin()
|
||||
|
@ -205,7 +206,7 @@ class PageController extends DefaultController {
|
|||
|
||||
private static function Alias() {
|
||||
print self::$twig->render('alias.html', array(
|
||||
'page_name' => self::$config['global']['name']
|
||||
'page_name' => Config::getName()
|
||||
,'alerts' => self::$alerts
|
||||
,'login' => self::$server->getLogin()
|
||||
,'isSuperAdmin' => self::$server->isSuperAdmin()
|
||||
|
|
Loading…
Reference in New Issue