2016-05-05 14:34:19 +02:00

134 lines
4.1 KiB
PHP

<?php
class Application_Controller_Plugin_Auth extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
Zend_Registry::get('firebug')->info('PLUGIN AUTH - START');
$module = $request->getModuleName();
$controller = $request->getControllerName();
$action = $request->getActionName();
$checkAuth = true;
if ($module == 'auth') {
$checkAuth = false;
}
if ($module == 'file' && $request->getClientIp(false) == '78.31.45.206') {
$checkAuth = false;
}
if ($checkAuth) {
$login = $request->getParam('login');
$pass = $request->getParam('pass', '');
$hach = $request->getParam('hach');
$checkIp = $request->getParam('checkIp');
$auth = Zend_Auth::getInstance();
$mode = null;
// --- Mode checkIp=only
if ($checkIp == 'only') {
$hach = 'iponly:'.$_SERVER['REMOTE_ADDR'];
$mode = 'iponly';
}
// --- On vérifie le tout lors d'une connexion par url
if ( !empty($login) && !empty($hach) ) {
// --- Mode hach
if ($mode === null) {
$mode = 'hach';
}
$authAdapter = new Scores_Auth_Adapter_Ws($login, $hach, $mode);
$result = $auth->authenticate($authAdapter);
if ( $result->isValid() ) {
// --- Store identity in sesssion
$storage = new Zend_Auth_Storage_Session();
$session = new Zend_Session_Namespace($storage->getNamespace());
$auth->setStorage($storage);
$user = new Scores_Utilisateur();
$info = get_browser();
if ( $info ) {
$isMobile = ($info->ismobiledevice==1) ? 1 : 0;
$user->setBrowserInfo($info->platform, $info->browser, $info->version, $isMobile);
} else {
//Save botnet information
}
} else {
$messageF = '';
foreach ($result->getMessages() as $message) {
$messageF.= $message."<br/>";
}
$request->setModuleName('auth')->setControllerName('user')
->setActionName('logout')->setParam('message', $messageF);
}
// --- Sinon on reste sur le standard
} else {
// --- Authentifié
if ( $auth->hasIdentity() ) {
// --- Mise à jour du délai de connexion
if ( time() < $auth->getIdentity()->time ) {
$identity = $auth->getIdentity();
$identity->time = time() + $identity->timeout;
$auth->getStorage()->write($identity);
if (Zend_Session::namespaceIsset('login')){
Zend_Session::namespaceUnset('login');
}
// --- Check CGU
if ($module!='help' && $controller!='cgu' || $module!='auth') {
if ( empty($identity->acceptationCGU) || $identity->acceptationCGU=='0000-00-00 00:00:00' ) {
$request->setModuleName('help')->setControllerName('cgu')->setActionName('index');
}
}
// --- Temps de connexion dépassé
} elseif ( time() > $auth->getIdentity()->time ) {
$auth->clearIdentity();
$storage = $auth->getStorage();
Zend_Session::namespaceUnset($storage->getNamespace());
if ( !$request->isXmlHttpRequest() ) {
$session = new Zend_Session_Namespace('login');
$session->url = $_SERVER['REQUEST_URI'];
}
if ($module=='default' && $controller=='index' && $action=='index') {
$request->setModuleName('auth')->setControllerName('user')->setActionName('login');
} else {
$request->setModuleName('auth')->setControllerName('user')->setActionName('logout')
->setParam('ajax', $request->isXmlHttpRequest());
}
}
}
// --- Pas Authentifié
else {
if ( $request->isXmlHttpRequest() ) {
$request->setModuleName('auth')->setControllerName('user')->setActionName('logout')
->setParam('ajax', 1);
} else {
$session = new Zend_Session_Namespace('login');
$session->url = $_SERVER['REQUEST_URI'];
$request->setModuleName('auth')->setControllerName('user')->setActionName('login');
}
}
}
}
Zend_Registry::get('firebug')->info('PLUGIN AUTH - END');
}
}