Untitled
user_3839718
php
2 years ago
8.1 kB
10
Indexable
<?php
/**
* Created by PhpStorm.
* User: jorge
* Date: 4/21/17
* Time: 2:34 PM
*/
namespace App;
use Laminas\Mvc\MvcEvent;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\ModuleManager\ModuleManger;
use App\Model\AuthSessionStorage;
use App\Controller\AuthController;
use App\Model\AuthManager;
use App\Helper\RouteMatch;
use App\Helper\Factory\RouteMatchFactory;
use Laminas\ModuleManager\ModuleManager;
use Zend\Locale\Zend_Locale;
use Laminas\Session\SessionManager;
use Laminas\View\Model\JsonModel;
class Module
{
public function getConfig() {
return include __DIR__ . '/../config/module.config.php';
}
// The "init" method is called on application start-up and
// allows to register an event listener.
public function init(ModuleManager $manager) {
// Get event manager.
$eventManager = $manager->getEventManager();
$sharedEventManager = $eventManager->getSharedManager();
// Register the event listener method.
$sharedEventManager->attach(
'Laminas\Mvc\Application', MvcEvent::EVENT_DISPATCH_ERROR,
[$this, 'onError'],
100
);
$sharedEventManager->attach(
'Laminas\Mvc\Application', MvcEvent::EVENT_RENDER_ERROR,
[$this, 'onError'],
100
);
$sharedEventManager->attach(
'Laminas\Mvc\Application',
MvcEvent::EVENT_FINISH,
[$this, 'compressOutput'],
100
);
}
public function handleError(MvcEvent $e) {
if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'ChickenBoy') !== false) {
$viewModel = new JsonModel([
'error' => $e->getResult()->exception->getMessage()
]);
$e->setViewModel($viewModel);
$e->stopPropagation(true);
}
}
public function onBootstrap(MvcEvent $event) {
$eventManager = $event->getApplication()->getEventManager();
$sharedEventManager = $eventManager->getSharedManager();
$sharedEventManager->attach(
AbstractActionController::class,
MvcEvent::EVENT_DISPATCH,
[$this, 'onController'],
100
);
$eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'handleError'));
$eventManager->attach(MvcEvent::EVENT_RENDER_ERROR, array($this, 'handleError'));
// $eventManager->attach("finish", array($this, "compressOutput"), 100);
$application = $event->getApplication();
$sm = $application->getServiceManager();
$auth = $sm->get(\Laminas\Authentication\AuthenticationService::class);
$authStorage = $sm->get('AuthSessionStorage');
$userManager = $sm->get(\App\Model\UserManager::class);
if ($auth->hasIdentity()) {
$user = $auth->getIdentity();
$userLang = $userManager->getLanguage($user['id']);
$translator = $sm->get(\MvcTranslator::class);
$userManager->setLocale($userLang);
\Laminas\Validator\AbstractValidator::setDefaultTranslator($translator);
}
$authStorage = $sm->get('AuthSessionStorage');
$authService = $sm->get(\Laminas\Authentication\AuthenticationService::class);
//Used this check to prevent an ajax call from extending the session and also do nothing if remember me is set.
if (!$event->getRequest()->isXmlHttpRequest() && !isset($auth->getIdentity()['rememberMe'])) {
if ($authStorage->isExpiredAuthenticationTime()) {
$user = $auth->getIdentity();
$userManager->updateLogoutTime($user['id']);
$authStorage->clearAuthenticationExpirationTime();
$authService->clearIdentity();
} else {
$authStorage->setAuthenticationExpirationTime();
}
}
}
public function compressOutput($e) {
// Check if the output buffering is active
if (ob_get_level() === 0) {
ob_start(); // Start buffering the output
}
$response = $e->getResponse();
$content = $response->getBody();
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) {
// Set the Content-Encoding header to gzip
header('Content-Encoding: gzip');
$content = gzencode($content, 9);
}
$response->setContent($content);
ob_end_flush();
}
// Just a little trick to display the actual layout when the controller is userController.
public function onController(MvcEvent $e) {
$routeMatch = $e->getRouteMatch();
$controller = $routeMatch->getParam('Controller');
/*if ($controller == \App\Controller\UserController::class || \App\Controller\AuthController::class || \App\Controller\RobotController::class) {
$controller = $e->getTarget();
$controller->layout('layout/loggedin');
}*/
}
/**
* This method helps to access the robot and farm manager for the layout.
*
* @return \|array The view helper configuration.
*/
public function getViewHelperConfig() {
return [
'factories' => [
'loginLayoutHelper' => function ($sm) {
$robotManager = new \App\Model\RobotManager($sm);
$farmManager = new \App\Model\FarmManager($sm);
$houseManager = new \App\Model\HouseManager($sm);
$alertManager = new \App\Model\AlertManager($sm);
$helper = new \App\Helper\LoginLayoutHelper($robotManager, $farmManager, $houseManager, $alertManager);
return $helper;
},
RouteMatch::class => RouteMatchFactory::class,
],
'aliases' => [
'routeMatch' => RouteMatch::class,
],
];
}
// Event listener method.
// This handler is called regardless of which module the error happens in
public function onError(MvcEvent $event) {
// Get the exception information.
$exception = $event->getParam('exception');
if ($exception != null) {
$exceptionName = $exception->getMessage();
$file = $exception->getFile();
$line = $exception->getLine();
$stackTrace = $exception->getTraceAsString();
}
$errorMessage = $event->getError();
$controllerName = $event->getController();
$body = '';
if (isset($_SERVER['REQUEST_URI'])) {
$body .= "Request URI: " . $_SERVER['REQUEST_URI'] . "\n\n";
}
$body .= "Controller: $controllerName\n";
$body .= "Error message: $errorMessage\n";
if ($exception != null) {
$body .= "Exception: $exceptionName\n";
$body .= "File: $file\n";
$body .= "Line: $line\n";
$body .= "Stack trace:\n\n" . $stackTrace;
}
error_log($body, E_USER_ERROR);
//$vm = $event->getViewModel();
//$vm->setTemplate('error/404');
}
public function getServiceConfig() {
return [
'factories' => [
'AuthSessionStorage' => function ($sm) {
$config = $sm->get('config');
$authSessionStorage = new AuthSessionStorage($sm->get('Laminas\Session\SessionManager'));
$authSessionStorage->setAllowedIdleTimeInSeconds($config['authentication_expiration_time']);
return $authSessionStorage;
},
'AuthService' => function ($sm) {
$authAdapter = new AuthAdapter();
$authAdapter->prepareAdapter($sm->get('Laminas\Db\Adapter\Adapter'));
$authAdapter->initStorage($sm->get('AuthSessionStorage'));
return $authAdapter;
},
],
];
}
}Editor is loading...