src/EventListener/AccessDeniedListener.php line 35
<?php
namespace App\EventListener;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class AccessDeniedListener implements EventSubscriberInterface
{
private $gUrl;
private $kernel;
public function __construct(UrlGeneratorInterface $gUrl, KernelInterface $kernel)
{
$this->gUrl = $gUrl;
$this->kernel = $kernel;
}
public static function getSubscribedEvents(): array
{
return [
// the priority must be greater than the Security HTTP
// ExceptionListener, to make sure it's called before
// the default exception listener
KernelEvents::EXCEPTION => 'onKernelException',
];
}
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
$request = $event->getRequest();
$env = $this->kernel->getEnvironment();
$baseUrl = $request->getHttpHost();
$local = strpos($baseUrl, "zekin.me") === false ?? false;
$prod = $env === "prod" ?? false;
if($exception instanceof NotFoundHttpException){ // 404 error
// $event->setResponse(new RedirectResponse($this->gUrl->generate('app_404')));
}else if($exception->getCode() == 403){ // AccessDenied
}else{ // Others error
/*$message = sprintf('Error: %s with code: %s', $exception->getMessage(), $exception->getCode());
$this->notify($message, $prod, $local, $request);
$filePath = $this->kernel->getProjectDir() . '/templates/vitrine/t0/500.html.twig';
$htmlContent = file_get_contents($filePath);
if($local){
$htmlContent = str_replace("_1_", "", $htmlContent);
$htmlContent = str_replace("_2_", "<code class='message'>".$message."</code>", $htmlContent);
}else{
$htmlContent = str_replace("_1_", "/public", $htmlContent);
$htmlContent = str_replace("_2_", "", $htmlContent);
}
if($prod){
$event->setResponse(new Response($htmlContent, Response::HTTP_OK, ['Content-Type' => 'text/html']));
}
// $event->stopPropagation();
*/
}
}
private function notify($message, $envProd, $envLocal, $request = null)
{
$dir = "./log/ExceptionEvent/";
if(!is_dir($dir)) mkdir($dir, 0777, true);
$file = $dir.date("YmdH").'.log';
$log = "ExceptionEvent : ".date("F j, Y, g:i a").PHP_EOL.
"Message..: ".$message.PHP_EOL.
$this->additional($request).
"-------------------------".PHP_EOL;
//Save string to log, use FILE_APPEND to append.
file_put_contents($file, $log, FILE_APPEND);
$this->sendSMS($file, $envProd, $envLocal, $message);
}
private function corrigeCarac($originMessage)
{
$str = str_replace(['<br>', '<br/>', '<br />', "\n", "\r", "`", '{', '}', '[', ']', '|', '^', '~', '€', 'WhatsApp', 'whatsapp', 'WHATSAPP'], '', $originMessage);
$str = preg_replace('#<[^>]*>#', '', $str );
$str = htmlentities($str, ENT_NOQUOTES, 'utf-8');
$str = preg_replace('#&([A-za-z])(?:acute|grave|cedil|circ|orn|ring|slash|th|tilde|uml);#', '\1', $str);
$str = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $str);
$str = preg_replace('#&[^;]+;#', '', $str);
return $str;
}
private function additional($request = null)
{
$addtional = "";
if($request){
$addtional .= "Controller..: ".$request->attributes->get("_controller").PHP_EOL;
$session = $request->getSession()->all();
if(isset($session["_security.last_username"])) $addtional .= "LastUser..: ".$session["_security.last_username"].PHP_EOL;
if(isset($session["_security.main.target_path"])) $addtional .= "TargetPath..: ".$session["_security.main.target_path"].PHP_EOL;
$params = $request->attributes->get("_route_params");
foreach ($params?$params:[] as $key => $value) {
$addtional .= $key."..: ".$value.PHP_EOL;
}
}
return $addtional;
}
private function sendSMS($file, $envProd, $envLocal, $message)
{
$support = "22952735555";
// if($envProd){
if(!is_file($file) && $envProd && !$envLocal){
try {
$sendsmsurl_prefix = "http://51.38.34.139:13013/cgi-bin/sendsms";
$curl = curl_init();
$sendsmsurl_params = array(
"username" => "VpsovhFaster",
"password" => "Tbc6aBL38yMh",
"to" => $support,
"text" => $this->corrigeCarac($message)." ".date("Y.m.d H:i"),
"from" => "ZEKIN",
"smsc" => "Fastermessage_moov",
'priority' => 3,
'dlr_url' => "",
'dlr_mask' => 31,
"mclass" => 1,
);
$sendsmsurl = $sendsmsurl_prefix."?".http_build_query($sendsmsurl_params);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $sendsmsurl);
curl_exec($curl);
curl_close($curl);
} catch (\Exception $e) {
/*\App\Service\FasterApi::call_api([
"from"=>"ZEKIN",
"to"=>$support,
"text"=>$message." ".date("Y.m.d H:i")
], "send_sms");*/
}
}
}
}