195 lines
8.3 KiB
PHP
195 lines
8.3 KiB
PHP
<?php
|
|
|
|
class Default_ErrorController extends Zend_Controller_Action
|
|
{
|
|
|
|
protected $_allowedController;
|
|
protected $_allowedModule;
|
|
protected $_restModule;
|
|
protected $_ignoredMessages;
|
|
protected $_ipManager;
|
|
|
|
public function init()
|
|
{
|
|
$this->_allowedController = array('admin', 'index', 'error', 'services');
|
|
$this->_allowedModule = array('admin', 'default', 'v1', 'monitoring', 'user');
|
|
$this->_restModule = array('v1','auth');
|
|
$this->_ignoredMessages = array(
|
|
"Accesso Scaduto",
|
|
"Username e/o password non ricevuti.",
|
|
"I dati inseriti non sono validi",
|
|
"Le credenziali inserite non sono valide",
|
|
"Account disattivato. Per informazioni contattare l'amministratore.",
|
|
"Username inserito inesistente"
|
|
);
|
|
$this->_ipManager = new App_Service_IpManager();
|
|
}
|
|
|
|
public function preDispatch()
|
|
{
|
|
$this->_response->clearHeader('Access-Control-Allow-Headers');
|
|
$this->_response->setHeader('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers,Access-Control-Allow-Methods,Access-Control-Allow-Origin,Authorization,x-requested-with,Content-Type,X-As-Dev-On,,X-As-Dev-Pw,MongoDbName');
|
|
|
|
$this->_response->clearHeader('Access-Control-Allow-Methods');
|
|
$this->_response->setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, PATCH, DELETE');
|
|
|
|
$this->_response->clearHeader('Access-Control-Allow-Origin');
|
|
$this->_response->setHeader('Access-Control-Allow-Origin', '*');
|
|
|
|
$this->_response->clearHeader('Access-Control-Allow-Credentials');
|
|
$this->_response->setHeader('Access-Control-Allow-Credentials', 'true');
|
|
|
|
$this->view->allowedIp = $this->_ipManager->allowedIp();
|
|
|
|
$this->view->responseCode = null;
|
|
}
|
|
|
|
public function errorAction()
|
|
{
|
|
$errors = $this->_getParam('error_handler');
|
|
if ($this->_getParam('module') != 'default') {
|
|
$content = array('status' => false);
|
|
}
|
|
|
|
if (!$errors || !$errors instanceof ArrayObject) {
|
|
$content['message'] = 'You have reached the error page';
|
|
return;
|
|
}
|
|
|
|
switch ($errors->type) {
|
|
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
|
|
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
|
|
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
|
|
// 404 error -- controller or action not found
|
|
$this->getResponse()->setHttpResponseCode(404);
|
|
$this->view->responseCode = 404;
|
|
$this->view->title = "Adriatica Sistemi soc. coop. - Pagina non trovata - Errore 404";
|
|
|
|
$priority = Zend_Log::NOTICE;
|
|
if (in_array($this->_getParam('module'), $this->_restModule)) {
|
|
$content['message'] = 'Page not found';
|
|
} else {
|
|
$this->view->message = 'Page not found';
|
|
}
|
|
break;
|
|
default:
|
|
if ($errors->exception instanceof App_Controller_Errors_WebServiceError) {
|
|
// web services error
|
|
$content['message'] = $errors->exception->getMyMessage();
|
|
$content['errorCode'] = $errors->exception->getErrorCode();
|
|
|
|
if ($errors->exception->getErrorDetail()) {
|
|
$content['errorDetail'] = $errors->exception->getErrorDetail();
|
|
}
|
|
|
|
//Recupero tutti i parametri disponibili sulla richiesta
|
|
$allParams = $this->getRequest()->getRawBody();
|
|
if(!$allParams){
|
|
$allParams = Zend_Json::encode($errors->request->getParams());
|
|
}
|
|
|
|
$dbName = '';$token = ''; $username = '';
|
|
if(Zend_Registry::isRegistered('TOKEN')){
|
|
$model = new Application_Model_Generic();
|
|
$token = Zend_Registry::get('TOKEN');
|
|
$user = $model->getByColumnName('users', 'token', $token);
|
|
if($user){
|
|
$dbName = $user['NomeDB'];
|
|
$username = $user['username'];
|
|
}
|
|
}
|
|
|
|
$this->getResponse()->setHttpResponseCode(400);
|
|
|
|
//Zend_Registry::get('log')->info(print_r($errors,true));
|
|
|
|
if (!in_array($errors->exception->getMessage(), $this->_ignoredMessages))
|
|
Zend_Registry::get('error_log')
|
|
->setEventItem('url', $errors->request->getRequestUri())
|
|
->setEventItem('trace', Zend_Json::encode($errors->exception->getTrace()))
|
|
->setEventItem('params', $allParams)
|
|
->setEventItem('db_name', $dbName)
|
|
->setEventItem('username', $username)
|
|
->setEventItem('token', $token)
|
|
->setEventItem('section', 'web_service')
|
|
->info($content['message']);
|
|
|
|
} else {
|
|
// application error
|
|
$this->getResponse()->setHttpResponseCode(500);
|
|
$priority = Zend_Log::CRIT;
|
|
if (in_array($this->_getParam('module'), $this->_restModule)) {
|
|
$content['message'] = $errors->exception->getMessage();
|
|
} else {
|
|
$this->view->message = $errors->exception->getMessage();
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
|
|
// Log exception, if logger available
|
|
if ($log = $this->getLog()) {
|
|
$log->log($this->view->message, $priority, $errors->exception);
|
|
$log->log('Request Parameters', $priority, $errors->request->getParams());
|
|
}
|
|
|
|
// conditionally display exceptions
|
|
if ($this->getInvokeArg('displayExceptions') == true || in_array($this->_getParam('module'), array('admin','monitoring'))) {
|
|
if (in_array($this->_getParam('module'), $this->_restModule)) {
|
|
$content['exception_trace'] = (array)$errors->exception->getTrace();
|
|
} else {
|
|
$this->view->exception = $errors->exception;
|
|
}
|
|
}
|
|
|
|
if (in_array($this->_getParam('module'), $this->_restModule) && APPLICATION_ENV != 'production') {
|
|
$content['request'] = (array)$errors->request;
|
|
} else {
|
|
$this->view->request = $errors->request;
|
|
}
|
|
|
|
if (in_array($this->_getParam('module'), $this->_restModule)) {
|
|
$this->_helper->getHelper('layout')->disableLayout();
|
|
$this->_helper->viewRenderer->setNoRender();
|
|
$this->getResponse()
|
|
->setHeader('Content-Type', 'json')
|
|
->setBody($this->_helper->Globalfunction->safeJsonEncode($content))
|
|
->sendResponse();
|
|
exit(1);
|
|
}
|
|
|
|
//Registro il log dell'errore
|
|
//Zend_Registry::get('log')->info(print_r($errors,true));
|
|
if (in_array($this->_getParam('module'), $this->_allowedModule)
|
|
&& in_array($this->_getParam('controller'), $this->_allowedController)
|
|
&& !in_array($errors->exception->getMessage(), $this->_ignoredMessages))
|
|
Zend_Registry::get('error_log')->setEventItem('url', $errors->request->getRequestUri())
|
|
->setEventItem('trace', Zend_Json::encode($errors->exception->getTrace()))
|
|
->setEventItem('params', Zend_Json::encode($errors->request->getParams()))
|
|
->setEventItem('section', $this->_getParam('module'))
|
|
->info($errors->exception->getMessage());
|
|
}
|
|
|
|
public function getLog()
|
|
{
|
|
$bootstrap = $this->getInvokeArg('bootstrap');
|
|
if (!$bootstrap->hasResource('Log')) {
|
|
return false;
|
|
}
|
|
$log = $bootstrap->getResource('Log');
|
|
return $log;
|
|
}
|
|
|
|
public function invalidAction()
|
|
{
|
|
$content = $this->_helper->json(array('status' => 'error', 'message' => $this->_getParam('message')));
|
|
$this->getResponse()
|
|
->setHeader('Content-Type', 'json')
|
|
->setBody($content)
|
|
->sendResponse();
|
|
}
|
|
|
|
|
|
}
|
|
|