src/ApplicationBundle/Listener/SessionListener.php line 581

Open in your IDE?
  1. <?php
  2. namespace ApplicationBundle\Listener;
  3. use ApplicationBundle\Constants\UserConstants;
  4. use ApplicationBundle\Interfaces\LoginInterface;
  5. use ApplicationBundle\Interfaces\SessionCheckInterface;
  6. use ApplicationBundle\Interfaces\SystemInterface;
  7. use ApplicationBundle\Modules\System\MiscActions;
  8. use ApplicationBundle\Modules\System\System;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  12. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  13. use Symfony\Component\HttpFoundation\Cookie;
  14. class SessionListener
  15. {
  16.     public $secretKey "";
  17.     private $em;
  18.     private $em_goc;
  19.     private $dm;
  20.     private $router;
  21.     private $session;
  22.     private $resolver;
  23.     private $applicationConnector;
  24.     private $kernelRootDir;
  25.     private $container;
  26.     public function __construct($kernelRootDir$container$resolver$applicationConnector$router$session$db_credentials$db_credentials_company_group$secret)
  27.     {
  28.         $this->secretKey $secret;
  29.         $this->em $db_credentials;
  30.         $this->em_goc $db_credentials_company_group;
  31. //        $this->dm=$odm_credentials;
  32.         $this->router $router;
  33.         $this->resolver $resolver;
  34.         $this->container $container;
  35.         $this->applicationConnector $applicationConnector;
  36.         $this->kernelRootDir $kernelRootDir;
  37.         $this->session $session;
  38.     }
  39.     public function onKernelController(FilterControllerEvent $event)
  40.     {
  41.         $controller $event->getController();
  42.         /*
  43.          * $controller passed can be either a class or a Closure.
  44.          * This is not usual in Symfony but it may happen.
  45.          * If it is a class, it comes in array format
  46.          */
  47.         if (!is_array($controller)) {
  48.             return;
  49.         }
  50.         $hbeeErrorCode UserConstants::ERROR_USER_EXISTS_ALREADY;
  51.         $request $event->getRequest();
  52.         $devAdminMode '_UNCHANGED_';
  53.         if ($request->request->has('devAdminOn') || $request->query->has('devAdminOn')) {
  54.             $devAdminMode $request->request->has('devAdminOn') ? $request->request->get('devAdminOn') : $request->query->get('devAdminOn');
  55.         }
  56. //        if ($request->request->has('devAdminOff')|| $request->query->has('devAdminOff')) {
  57. //            $devAdminMode=0;
  58. //
  59. //        }
  60.         $permissionOverride 0;
  61.         if ($request->query->has('OVP'))
  62.             $permissionOverride 1;
  63.         //check if mobile
  64.         $session $this->session;
  65.         if (empty($session->get('appValiditySeconds'))) {  //how many sevonds is valid
  66.             $session->set('appValiditySeconds''_UNSET_');
  67.         }
  68.         if (empty($session->get('devAdminMode'))) {  //how many sevonds is valid
  69.             $session->set('devAdminMode'0);
  70.         }
  71.         if ($devAdminMode != '_UNCHANGED_')
  72.             $session->set('devAdminMode'$devAdminMode);
  73.         $to_set_session_data = [];
  74.         $CurrentRoute $event->getRequest()->attributes->get('_route');
  75.         if ($CurrentRoute == 'switch_app') {
  76.             if ($request->query->has('hbeeSessionToken')) {
  77.                 $em_goc $this->em_goc;
  78.                 $to_set_session_data MiscActions::GetSessionDataFromToken($em_goc$request->query->get('hbeeSessionToken'))['sessionData'];
  79.                 if ($to_set_session_data != null) {
  80.                     foreach ($to_set_session_data as $k => $d) {
  81.                         //check if mobile
  82.                         $session->set($k$d);
  83.                     }
  84.                 } else {
  85.                     $hbeeErrorCode UserConstants::ERROR_TOKEN_EXPIRED;
  86.                 }
  87.             } else {
  88.                 $url $this->router->generate('user_login');
  89.                 $url .= ("?csToken=" $request->query->get('csToken''') . "&spd=" $request->query->get('spd'''));
  90.                 $session->clear();
  91.                 $session->set('CLEARLOGIN'1);
  92.                 $event->setController(function () use ($url) {
  93.                     return new RedirectResponse($url);
  94.                 });
  95.             }
  96.         }
  97.         if (!$session->has(UserConstants::USER_ID)) {
  98.             if ($request->request->has('token')) {
  99.                 $em_goc $this->em_goc;
  100.                 $to_set_session_data MiscActions::GetSessionDataFromToken($em_goc$request->request->get('token'))['sessionData'];
  101.                 if ($to_set_session_data != null) {
  102.                     foreach ($to_set_session_data as $k => $d) {
  103.                         //check if mobile
  104.                         $session->set($k$d);
  105.                     }
  106.                 } else {
  107.                     $hbeeErrorCode UserConstants::ERROR_TOKEN_EXPIRED;
  108.                 }
  109.             } else if ($request->request->has('hbeeSessionToken')) {
  110.                 $em_goc $this->em_goc;
  111.                 $to_set_session_data MiscActions::GetSessionDataFromToken($em_goc$request->request->get('hbeeSessionToken'))['sessionData'];
  112.                 if ($to_set_session_data != null) {
  113.                     foreach ($to_set_session_data as $k => $d) {
  114.                         //check if mobile
  115.                         $session->set($k$d);
  116.                     }
  117.                 } else {
  118.                     $hbeeErrorCode UserConstants::ERROR_TOKEN_EXPIRED;
  119.                 }
  120.             } else if ($request->query->has('hbeeSessionToken')) {
  121.                 $em_goc $this->em_goc;
  122.                 $to_set_session_data MiscActions::GetSessionDataFromToken($em_goc$request->query->get('hbeeSessionToken'))['sessionData'];
  123.                 if ($to_set_session_data != null) {
  124.                     foreach ($to_set_session_data as $k => $d) {
  125.                         //check if mobile
  126.                         $session->set($k$d);
  127.                     }
  128.                 } else {
  129.                     $hbeeErrorCode UserConstants::ERROR_TOKEN_EXPIRED;
  130.                 }
  131.             } else if ($request->request->has('sessionData')) {
  132.                 $to_set_session_data $request->request->get('sessionData');
  133.                 foreach ($to_set_session_data as $k => $d) {
  134.                     //check if mobile
  135.                     $session->set($k$d);
  136.                 }
  137.             } else if ($request->request->has('sessionDataString')) {
  138.                 $to_set_session_data json_decode($request->request->get('sessionDataString'), true);
  139.                 if ($to_set_session_data != null) {
  140.                     foreach ($to_set_session_data as $k => $d) {
  141.                         //check if mobile
  142.                         $session->set($k$d);
  143.                     }
  144.                 }
  145.             }
  146.             if ($request->query->has('iFrameData')) {
  147.                 $session->set(UserConstants::USER_ID$request->query->get('uId'));
  148.                 $session->set(UserConstants::USER_NAME$request->query->get('uName'));
  149.                 $session->set(UserConstants::USER_LOGIN_ID$request->query->get('loginId'));
  150.                 $session->set(UserConstants::APPLICATION_SECRET$request->query->get('sKey'));
  151.                 $session->set(UserConstants::USER_GOC_ID$request->query->get('gocId'));
  152.                 $session->set(UserConstants::USER_DB_NAME$request->query->get('gocDb'));
  153.                 $session->set(UserConstants::USER_DB_USER$request->query->get('gocUsr'));
  154.                 $session->set(UserConstants::USER_DB_PASS$request->query->get('gocPass'));
  155.                 $session->set(UserConstants::USER_DB_HOST$request->query->get('gocDh'));
  156.                 $session->set(UserConstants::USER_TYPE$request->query->get('uType'));
  157.                 $session->set(UserConstants::USER_CURRENT_POSITION$request->query->get('currPos'));
  158.                 $session->set(UserConstants::USER_COMPANY_ID$request->query->get('cmpId'));
  159.                 $session->set(UserConstants::USER_APP_ID$request->query->get('uAId'));
  160.                 $session->set(UserConstants::USER_IMAGE"");
  161.                 $session->set('productNameDisplayType'0);
  162.                 $cmpImg $request->query->get('cmpIm');
  163.                 $cmpName $request->query->get('cmpN');
  164.                 $app_cid $request->query->get('uAId') . "_" $request->query->get('cmpId');
  165.                 $cid $request->query->get('cmpId');
  166.                 $session->set('userCompanyDarkVibrantList'json_encode(array(
  167.                     $cid => ""
  168.                 )));
  169.                 $cmpNameListStr json_encode(array(
  170.                     $cid => $cmpName
  171.                 ));
  172.                 $cmpImageListStr json_encode(array(
  173.                     $cid => $cmpImg
  174.                 ));
  175.                 $session->set(UserConstants::USER_COMPANY_NAME_LIST$cmpNameListStr);
  176.                 $session->set(UserConstants::USER_COMPANY_IMAGE_LIST$cmpImageListStr);
  177.                 $session->set(UserConstants::USER_PROHIBIT_LIST"[]");
  178.                 $session->set(UserConstants::ALL_MODULE_ACCESS_FLAG1);
  179.             }
  180.         }
  181. //        $path=$this->kernelRootDir. '/gifnoc/invdata.json';
  182. //        file_put_contents($path, json_encode(array(
  183. //            'sessionDataString'=>$request->request->get('sessionDataString'),
  184. //            'productCode'=>$request->request->get('productCode'),
  185. //            'userId'=>$session->get(UserConstants::USER_ID),
  186. //            'sessionData'=>json_decode($request->request->get('sessionDataString'),true),
  187. //            'to_set_session_data'=>$to_set_session_data,
  188. ////            'invData'=>$data_searched,
  189. //
  190. //        )));//overwrite
  191.         if (!$session->has('isMobile')) {
  192. //        if
  193.             $useragent $event->getRequest()->headers->get('User-Agent');
  194.             if (!$useragent) {
  195.                 return false;
  196.             }
  197.             $isMobile = (
  198.                 preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i'$useragent) ||
  199.                 preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i'substr($useragent04))
  200.             );
  201.             $session->set('isMobile'$isMobile);
  202.         }
  203.         if ($controller[0] instanceof SessionCheckInterface) {
  204.             if (!empty($session->get(UserConstants::USER_ID))) {
  205.                 if ($session->get(UserConstants::APPLICATION_SECRET) == $this->secretKey) {
  206.                     //if session check variable is more than 1 hour from now, update app validity but if already disabled then check on every call
  207.                     $lastCheckDateTime null;
  208.                     $appValid 1;
  209.                     $currentDateTime = new \DateTime();
  210.                     $currentTime strtotime($currentDateTime->format('Y-m-d h:i:s'));
  211.                     $appIsValidTillTime $currentTime;
  212.                     $appIsValidTillDateTime $currentDateTime;
  213. //                    $session->set('appIsValidTillTime', $appIsValidTillDateTime->format('Y-m-d h:i:s'));
  214.                     if (!empty($session->get('lastCheckAppValidityTime'))) {
  215.                         $lastCheckDateTime strtotime($session->get('lastCheckAppValidityTime'));
  216.                     } else {
  217.                         $lastCheckDateTime strtotime('2022-01-01 00:00:00');
  218.                     }
  219.                     if (!empty($session->get('appIsValidTillTime'))) {
  220.                         $appIsValidTillTime strtotime($session->get('appIsValidTillTime'));
  221.                         $appIsValidTillDateTime = new \DateTime($session->get('appIsValidTillTime'));
  222.                     } else {
  223. //                        $appIsValidTillTime=$currentDateTime;
  224. //                        $session->set('appIsValidTillTime', $appIsValidTillTime->format('Y-m-d h:i:s'));
  225.                     }
  226.                     if (!empty($session->get('appValid'))) {
  227.                         $appValid $session->get('appValid');
  228.                     }
  229.                     $secondsTillLastCheck abs($currentTime $lastCheckDateTime);
  230. //                    $session->set('secondsTillLastCheck',$secondsTillLastCheck);
  231. //                    $session->set('lastCheckAppValiditySecond', abs($currentTime - $lastCheckDateTime));
  232.                     if ($secondsTillLastCheck 7200 || $appValid == 0) {
  233.                         $appValiditySeconds $session->get('appValiditySeconds');
  234.                         $appDataJson System::getAppDataByCurl();
  235. //                        $session->set('appDataJson',$appDataJson);
  236.                         $appData json_decode($appDataJsontrue);
  237. //                        $appIsValidTillTime=$currentDateTime;
  238.                         if ($appData == null)
  239.                             $appData = [];
  240.                         if (isset($appData[$session->get(UserConstants::USER_APP_ID)])) {
  241.                             $session->set('appDataCurl'$appData[$session->get(UserConstants::USER_APP_ID)]);
  242.                             if ($appData[$session->get(UserConstants::USER_APP_ID)]['expired'] == 1) {
  243.                                 $appValid 0;
  244.                                 $appValiditySeconds 0;
  245.                             } else {
  246.                                 $appIsValidTillTime strtotime($appData[$session->get(UserConstants::USER_APP_ID)]['suspensionDate']);
  247.                                 $appIsValidTillDateTime = new \DateTime($appData[$session->get(UserConstants::USER_APP_ID)]['suspensionDate']);
  248.                                 $appValiditySeconds $appIsValidTillTime $currentTime;
  249.                                 if ($appValiditySeconds 0)
  250.                                     $appValid 0;
  251.                                 else
  252.                                     $appValid 1;
  253.                             }
  254.                         } else {
  255.                         }
  256.                         $session->set('appValiditySeconds'$appValiditySeconds);
  257.                         $session->set('appIsValidTillTime'$appIsValidTillDateTime->format('Y-m-d h:i:s'));
  258.                         $session->set('lastCheckAppValidityTime'$currentDateTime->format('Y-m-d h:i:s'));
  259.                         $session->set('appValid'$appValid);
  260.                     }
  261.                     $session->set('appIsValidTillTime'$appIsValidTillDateTime->format('Y-m-d h:i:s'));
  262.                     if ($appValid != 1) {
  263.                         $url $this->router->generate('user_logout');
  264.                         if (strripos($request->server->get('REQUEST_URI'), 'select_data') === false) {
  265.                             if ($request->server->get('REQUEST_URI') != '/' && $request->server->get('REQUEST_URI') != '') {
  266.                                 $session->set('LAST_REQUEST_URI_BEFORE_LOGIN'$request->server->get('REQUEST_URI'));
  267.                             }
  268.                         } else {
  269.                             $session->set('LAST_REQUEST_URI_BEFORE_LOGIN''');
  270.                         }
  271. //                        if ($request->server->has("REQUEST_URI")) {
  272. //                            if ($request->server->get('REQUEST_URI') != '/' && $request->server->get('REQUEST_URI') != '') {
  273. ////                            return $this->redirect($request->request->get('HTTP_REFERER'));
  274. //                                $session->set('LAST_REQUEST_URI_BEFORE_LOGIN', $request->server->get('REQUEST_URI'));
  275. //                            }
  276. //                        }
  277. //                $request->headers->setCookie(Cookie::create('CLEARLOGINCOOKIE', 1));
  278.                         $event->setController(function () use ($url) {
  279.                             return new RedirectResponse($url);
  280.                         });
  281.                     }
  282.                     $gocId $session->get(UserConstants::USER_GOC_ID);
  283.                     if ($gocId != && $gocId != "") {
  284.                         $gocDbName $session->get(UserConstants::USER_DB_NAME);
  285.                         $gocDbUser $session->get(UserConstants::USER_DB_USER);
  286.                         $gocDbPass $session->get(UserConstants::USER_DB_PASS);
  287.                         $gocDbHost $session->get(UserConstants::USER_DB_HOST);
  288. //                        $connector = $this->container->get('application_connector');
  289.                         $connector $this->applicationConnector;
  290.                         $connector->resetConnection(
  291.                             'default',
  292.                             $gocDbName,
  293.                             $gocDbUser,
  294.                             $gocDbPass,
  295.                             $gocDbHost,
  296.                             $reset false);
  297.                     }
  298.                     // User already have logged in. lets check its type
  299.                     if (
  300.                         $session->get(UserConstants::USER_TYPE) == UserConstants::USER_TYPE_SYSTEM ||
  301.                         $session->get(UserConstants::USER_TYPE) == UserConstants::USER_TYPE_GENERAL ||
  302.                         $session->get(UserConstants::USER_TYPE) == UserConstants::USER_TYPE_SUPPLIER ||
  303.                         $session->get(UserConstants::USER_TYPE) == UserConstants::USER_TYPE_ENTITY_USER_GENERAL_USER ||
  304.                         $session->get(UserConstants::USER_TYPE) == UserConstants::USER_TYPE_ENTITY_USER_GROUP_OWNER ||
  305.                         $session->get(UserConstants::USER_TYPE) == UserConstants::USER_TYPE_CLIENT ||
  306.                         $session->get(UserConstants::USER_TYPE) == UserConstants::USER_TYPE_APPLICANT
  307.                     ) {
  308.                         if (!empty($session->get('TRIGGER_RESET_PASSWORD'))) {
  309.                             if ($session->get('TRIGGER_RESET_PASSWORD') == 1) {
  310.                                 $red $this->router->generate('reset_password_new_password');
  311.                                 if (!$request->request->has('remoteVerify')) {
  312.                                     $session->set('TRIGGER_RESET_PASSWORD'0);
  313.                                     $event->setController(function () use ($red) {
  314.                                         return new RedirectResponse($red);
  315.                                     });
  316.                                 }
  317.                             }
  318.                         } else if (!empty($session->get('LAST_REQUEST_URI_BEFORE_LOGIN'))) {
  319.                             if (strripos($session->get('LAST_REQUEST_URI_BEFORE_LOGIN'), 'select_data') === false) {
  320.                                 if ($session->get('LAST_REQUEST_URI_BEFORE_LOGIN') != '' && $session->get('LAST_REQUEST_URI_BEFORE_LOGIN') != null) {
  321.                                     $red $session->get('LAST_REQUEST_URI_BEFORE_LOGIN');
  322.                                     $session->set('LAST_REQUEST_URI_BEFORE_LOGIN''');
  323.                                     if (!$request->request->has('remoteVerify')) {
  324.                                         $event->setController(function () use ($red) {
  325.                                             return new RedirectResponse($red);
  326.                                         });
  327.                                     }
  328.                                 }
  329.                             } else {
  330.                                 $session->set('LAST_REQUEST_URI_BEFORE_LOGIN''');
  331.                             }
  332.                         }
  333.                     } else {
  334.                         $message "Session Not Found";
  335.                         if ($request->request->has('remoteVerify') || $request->request->has('returnJson') || $request->query->has('returnJson')) {
  336.                         } else {
  337.                             if ($request->server->has("REQUEST_URI")) {
  338.                                 if (strripos($request->server->get('REQUEST_URI'), 'select_data') === false) {
  339.                                     if ($request->server->get('REQUEST_URI') != '/' && $request->server->get('REQUEST_URI') != '') {
  340.                                         $session->set('LAST_REQUEST_URI_BEFORE_LOGIN'$request->server->get('REQUEST_URI'));
  341.                                     }
  342.                                 } else {
  343.                                     $session->set('LAST_REQUEST_URI_BEFORE_LOGIN''');
  344.                                 }
  345.                             }
  346.                         }
  347.                         if ($request->request->has('remoteVerify') || $request->request->has('returnJson') || $request->query->has('returnJson')) {
  348.                             $session->set('jsonStrForListenerData'json_encode(array(
  349.                                 'uid' => 0,
  350.                                 'session' => [],
  351.                                 'success' => false,
  352.                                 'sessionExists' => false,
  353.                                 'hbeeErrorCode' => $hbeeErrorCode,
  354.                                 'errorStr' => $message,
  355.                                 'session_data' => [],
  356.                                 'session2' => $_SESSION,
  357.                             )));
  358.                             $url $this->router->generate('return_json_for_listener');
  359. //                $request->headers->setCookie(Cookie::create('CLEARLOGINCOOKIE', 1));
  360.                             $event->getRequest()->attributes->set('_controller''ApplicationBundle:ConsultancyPublic:ReturnJsonForListener');
  361.                             $event->setController($this->resolver->getController($request));
  362. //                        $event->setController(function () use ($url) {
  363. //                            return new RedirectResponse($url);
  364. //                        });
  365.                         } else {
  366.                             $url $this->router->generate('user_logout');
  367.                             $event->setController(function () use ($url) {
  368.                                 return new RedirectResponse($url);
  369.                             });
  370.                         }
  371.                     }
  372.                 } else {
  373.                     $message "Session Not Found";
  374.                     if ($request->request->has('remoteVerify') || $request->request->has('returnJson') || $request->query->has('returnJson')) {
  375.                     } else {
  376.                         if ($request->server->has("REQUEST_URI")) {
  377.                             if (strripos($request->server->get('REQUEST_URI'), 'select_data') === false) {
  378.                                 if ($request->server->get('REQUEST_URI') != '/' && $request->server->get('REQUEST_URI') != '') {
  379.                                     $session->set('LAST_REQUEST_URI_BEFORE_LOGIN'$request->server->get('REQUEST_URI'));
  380.                                 }
  381.                             } else {
  382.                                 $session->set('LAST_REQUEST_URI_BEFORE_LOGIN''');
  383.                             }
  384. //                            if ($request->server->get('REQUEST_URI') != '/' && $request->server->get('REQUEST_URI') != '') {
  385. ////                            return $this->redirect($request->request->get('HTTP_REFERER'));
  386. //                                $session->set('LAST_REQUEST_URI_BEFORE_LOGIN', $request->server->get('REQUEST_URI'));
  387. //                            }
  388.                         }
  389.                     }
  390.                     if ($request->request->has('remoteVerify') || $request->request->has('returnJson') || $request->query->has('returnJson')) {
  391.                         $session->set('jsonStrForListenerData'json_encode(array(
  392.                             'uid' => 0,
  393.                             'session' => [],
  394.                             'success' => false,
  395.                             'sessionExists' => false,
  396.                             'hbeeErrorCode' => $hbeeErrorCode,
  397.                             'errorStr' => $message,
  398.                             'session_data' => [],
  399.                             'session2' => $_SESSION,
  400.                         )));
  401.                         $url $this->router->generate('return_json_for_listener');
  402. //                $request->headers->setCookie(Cookie::create('CLEARLOGINCOOKIE', 1));
  403.                         $event->getRequest()->attributes->set('_controller''ApplicationBundle:ConsultancyPublic:ReturnJsonForListener');
  404.                         $event->setController($this->resolver->getController($request));
  405. //                        $event->setController(function () use ($url) {
  406. //                            return new RedirectResponse($url);
  407. //                        });
  408.                     } else {
  409.                         $url $this->router->generate('user_logout');
  410.                         $event->setController(function () use ($url) {
  411.                             return new RedirectResponse($url);
  412.                         });
  413.                     }
  414.                 }
  415.             } else {
  416.                 // User can't see this page until do a login. let him/her login first.
  417.                 $message "Session Not Found";
  418.                 $hbeeErrorCode UserConstants::ERROR_TOKEN_EXPIRED;
  419.                 if ($request->request->has('remoteVerify') || $request->request->has('returnJson') || $request->query->has('returnJson')) {
  420.                 } else {
  421.                     if ($request->server->has("REQUEST_URI")) {
  422.                         if (strripos($request->server->get('REQUEST_URI'), 'select_data') === false) {
  423.                             if ($request->server->get('REQUEST_URI') != '/' && $request->server->get('REQUEST_URI') != '') {
  424.                                 $session->set('LAST_REQUEST_URI_BEFORE_LOGIN'$request->server->get('REQUEST_URI'));
  425.                             }
  426.                         } else {
  427.                             $session->set('LAST_REQUEST_URI_BEFORE_LOGIN''');
  428.                         }
  429. //                        if ($request->server->get('REQUEST_URI') != '/' && $request->server->get('REQUEST_URI') != '') {
  430. ////                            return $this->redirect($request->request->get('HTTP_REFERER'));
  431. //                            $session->set('LAST_REQUEST_URI_BEFORE_LOGIN', $request->server->get('REQUEST_URI'));
  432. //                        }
  433.                     }
  434.                 }
  435.                 $session->set('jsonStrForListenerData'json_encode([]));
  436.                 if ($request->request->has('remoteVerify') || $request->request->has('returnJson') || $request->query->has('returnJson')) {
  437.                     $session->set('jsonStrForListenerData'json_encode(array(
  438.                         'uid' => 0,
  439.                         'session' => [],
  440.                         'success' => false,
  441.                         'sessionExists' => false,
  442.                         'hbeeErrorCode' => $hbeeErrorCode,
  443.                         'errorStr' => $message,
  444.                         'session_data' => [],
  445.                         'session2' => $_SESSION,
  446.                     )));
  447.                     $url $this->router->generate('return_json_for_listener');
  448. //                $request->headers->setCookie(Cookie::create('CLEARLOGINCOOKIE', 1));
  449.                     $event->getRequest()->attributes->set('_controller''ApplicationBundle:ConsultancyPublic:ReturnJsonForListener');
  450.                     $event->setController($this->resolver->getController($request));
  451. //                    $event->setController(function () use ($url) {
  452. //                        return new RedirectResponse($url);
  453. //                    });
  454. //                    return new JsonResponse();
  455.                 } else {
  456.                     $url $this->router->generate('user_login');
  457. //                $request->headers->setCookie(Cookie::create('CLEARLOGINCOOKIE', 1));
  458.                     $event->setController(function () use ($url) {
  459.                         return new RedirectResponse($url);
  460.                     });
  461.                 }
  462.             }
  463. //            Here we got a general user who have all the right things. Let see if he/she have permission to tre current route.
  464. //            $ROUTE_LIST=json_decode($session->get(UserConstants::USER_ROUTE_LIST), true);
  465.             $PROHIBIT_LIST json_decode($session->get(UserConstants::USER_PROHIBIT_LIST), true);
  466.             $CurrentRoute $event->getRequest()->attributes->get('_route');
  467.             $PL json_decode($session->get(UserConstants::USER_POSITION_LIST), true);
  468.             if ($PL == null)
  469.                 $PL = [];
  470.             if (!empty($PL) && $session->get(UserConstants::USER_CURRENT_POSITION) == 0) {
  471.                 $url $this->router->generate('user_login_position');
  472.                 $event->setController(function () use ($url) {
  473.                     return new RedirectResponse($url);
  474.                 });
  475.             }
  476.             if ($CurrentRoute == "user_login_position") {
  477.                 if (!empty($session->get(UserConstants::USER_ID))) {
  478.                     // This is an system user. send him to system dashboard
  479.                     if ($session->get(UserConstants::USER_TYPE) == UserConstants::USER_TYPE_SYSTEM) {
  480.                         // This is an system user. send him to system dashboard
  481.                         $url $this->router->generate('system_admin_dashboard');
  482.                         $event->setController(function () use ($url) {
  483.                             return new RedirectResponse($url);
  484.                         });
  485.                     } else {
  486.                         if (!empty($session->get(UserConstants::USER_CURRENT_POSITION)) && $session->get(UserConstants::USER_CURRENT_POSITION) != 0) {
  487.                             // user position got set up. send him to the normal dashboard.
  488.                             $PL json_decode($session->get(UserConstants::USER_POSITION_LIST), true);
  489.                             if ($PL == null$PL = [];
  490.                             if (count($PL) <= 1) {
  491.                                 $url $this->router->generate('dashboard');
  492.                                 $event->setController(function () use ($url) {
  493.                                     return new RedirectResponse($url);
  494.                                 });
  495.                             }
  496.                         }
  497.                     }
  498.                 } else {
  499.                     // User can't see this page until do a login. let him/her login first.
  500.                     $url $this->router->generate('user_login');
  501.                     $event->setController(function () use ($url) {
  502.                         return new RedirectResponse($url);
  503.                     });
  504.                 }
  505.             }
  506.             if ($permissionOverride == && $CurrentRoute != "dashboard" && $CurrentRoute != $session->get(UserConstants::USER_DEFAULT_ROUTE)) {
  507.                 if ($PROHIBIT_LIST != null or $PROHIBIT_LIST != []) {
  508.                     if (in_array($CurrentRoute$PROHIBIT_LIST)) {
  509.                         // User is not authorized. send him to dashboard
  510. //                    $controller->addFlash(
  511. //                        'error',
  512. //                        'Sorry Couldnot insert Data.'
  513. //                    );
  514.                         $url $this->router->generate('permission_denied_page');
  515.                         $event->setController(function () use ($url) {
  516.                             return new RedirectResponse($url);
  517.                         });
  518.                     }
  519.                 }
  520.             }
  521.         }
  522.         if ($controller[0] instanceof LoginInterface) {
  523.             $session $this->session;
  524.             // Three situation here
  525.             //  1. Have no session
  526.             //  2. Have session but no position
  527.             //  3. Have session and position
  528.             $route $event->getRequest()->attributes->get('_route');
  529.             $systemType '_ERP';//default
  530.             $systemType $this->container->hasParameter('system_type') ? $this->container->getParameter('system_type') : '_ERP_';
  531.             if ($route == "user_login") {
  532.                 if ($request->request->get('remoteVerify'$request->query->get('remoteVerify'0)) == 1) {
  533.                     $session->clear();
  534.                 } else if (!empty($session->get(UserConstants::USER_ID))) {
  535.                     // User already have logged in. lets check its type
  536.                     if ($systemType == '_ERP_') {
  537.                         if ($session->get(UserConstants::USER_TYPE) == UserConstants::USER_TYPE_SYSTEM) {
  538.                             // This is an system user. send him to system dashboard
  539.                             $url $this->router->generate('system_admin_dashboard');
  540.                             $event->setController(function () use ($url) {
  541.                                 return new RedirectResponse($url);
  542.                             });
  543.                         } else {
  544.                             // This user is a normal user. Check if its login position got set.
  545.                             if (!empty($session->get(UserConstants::USER_CURRENT_POSITION))) {
  546.                                 // user position got set up. send him to the normal dashboard.
  547.                                 $url $this->router->generate('dashboard');
  548.                                 if ($request->request->has('remoteVerify')) {
  549.                                     $session->set('remoteVerified'1);
  550.                                     $url $this->router->generate('get_session_data_for_app');
  551.                                     $event->setController(function () use ($url) {
  552.                                         return new RedirectResponse($url);
  553.                                     });
  554.                                 }
  555.                                 $event->setController(function () use ($url) {
  556.                                     return new RedirectResponse($url);
  557.                                 });
  558.                             } else {
  559.                                 // user position have not set up. send it to the position page
  560.                                 $url $this->router->generate('user_login_position');
  561.                                 $event->setController(function () use ($url) {
  562.                                     return new RedirectResponse($url);
  563.                                 });
  564.                             }
  565.                         }
  566.                         //do nothing its default to dashboard index
  567.                     } else if ($systemType == '_BUDDYBEE_') {
  568. //                        $event->getRequest()->attributes->set('_controller', 'ApplicationBundle:ConsultancyPublic:ConsultancyHomePage');
  569. //                        $event->setController($this->resolver->getController($request));
  570.                     }
  571.                 } else {
  572.                     if ($systemType == '_ERP_') {
  573.                         //do nothing its default to dashboard index
  574.                     } else if ($systemType == '_BUDDYBEE_' && $request->isMethod('GET')) {
  575.                         if ($request->request->has('oAuthData') || $request->query->has('oAuthData') || $request->query->has('code')) {
  576.                         } else {
  577.                             $event->getRequest()->attributes->set('_controller''ApplicationBundle:UserLogin:applicantLogin');
  578.                             $event->setController($this->resolver->getController($request));
  579.                         }
  580.                     }
  581. //                    if ($systemType == '_BUDDYBEE_' && $request->isMethod('POST')) {
  582. //
  583. //                            $event->getRequest()->attributes->set('_controller', 'ApplicationBundle:UserLogin:applicantLogin');
  584. //                        $event->setController($this->resolver->getController($request));
  585. //                    }
  586.                 }
  587.             }
  588.         }
  589.         // Checking if its a system url request.
  590.         if ($controller[0] instanceof SystemInterface) {
  591.             // System controller.
  592.             $session $this->session;
  593.             if (!empty($session->get(UserConstants::USER_ID))) {
  594.                 if ($session->get(UserConstants::USER_TYPE) != UserConstants::USER_TYPE_SYSTEM) {
  595.                     // Kick the idiot out from here. If possible, keep a track
  596.                     $url $this->router->generate('user_logout');
  597.                     $event->setController(function () use ($url) {
  598.                         return new RedirectResponse($url);
  599.                     });
  600.                 } else {
  601.                     $lastCheckDateTime null;
  602.                     $appValid 1;
  603.                     $currentDateTime = new \DateTime();
  604.                     $currentTime strtotime($currentDateTime->format('Y-m-d h:i:s'));
  605.                     $appIsValidTillTime $currentTime;
  606.                     $appIsValidTillDateTime $currentDateTime;
  607. //                    $session->set('appIsValidTillTime', $appIsValidTillDateTime->format('Y-m-d h:i:s'));
  608.                     if (!empty($session->get('lastCheckAppValidityTime'))) {
  609.                         $lastCheckDateTime strtotime($session->get('lastCheckAppValidityTime'));
  610.                     } else {
  611.                         $lastCheckDateTime strtotime('2022-01-01 00:00:00');
  612.                     }
  613.                     if (!empty($session->get('appIsValidTillTime'))) {
  614.                         $appIsValidTillTime strtotime($session->get('appIsValidTillTime'));
  615.                         $appIsValidTillDateTime = new \DateTime($session->get('appIsValidTillTime'));
  616.                     } else {
  617. //                        $appIsValidTillTime=$currentDateTime;
  618. //                        $session->set('appIsValidTillTime', $appIsValidTillTime->format('Y-m-d h:i:s'));
  619.                     }
  620.                     if (!empty($session->get('appValid'))) {
  621.                         $appValid $session->get('appValid');
  622.                     }
  623.                     $secondsTillLastCheck abs($currentTime $lastCheckDateTime);
  624. //                    $session->set('lastCheckAppValiditySecond', abs($currentTime - $lastCheckDateTime));
  625.                     if ($secondsTillLastCheck 7200 || $appValid == 0) {
  626.                         $appValiditySeconds $session->get('appValiditySeconds');
  627.                         $appDataJson System::getAppDataByCurl();
  628.                         $appData json_decode($appDataJsontrue);
  629. //                        $appIsValidTillTime=$currentDateTime;
  630.                         if ($appData == null)
  631.                             $appData = [];
  632.                         if (isset($appData[$session->get(UserConstants::USER_APP_ID)])) {
  633.                             $session->set('appDataCurl'$appData[$session->get(UserConstants::USER_APP_ID)]);
  634.                             if ($appData[$session->get(UserConstants::USER_APP_ID)]['expired'] == 1) {
  635.                                 $appValid 0;
  636.                                 $appValiditySeconds 0;
  637.                             } else {
  638.                                 $appIsValidTillTime strtotime($appData[$session->get(UserConstants::USER_APP_ID)]['suspensionDate']);
  639.                                 $appIsValidTillDateTime = new \DateTime($appData[$session->get(UserConstants::USER_APP_ID)]['suspensionDate']);
  640.                                 $appValiditySeconds $appIsValidTillTime $currentTime;
  641.                                 if ($appValiditySeconds 0)
  642.                                     $appValid 0;
  643.                                 else
  644.                                     $appValid 1;
  645.                             }
  646.                         }
  647.                         $session->set('appValiditySeconds'$appValiditySeconds);
  648.                         $session->set('appIsValidTillTime'$appIsValidTillDateTime->format('Y-m-d h:i:s'));
  649.                         $session->set('lastCheckAppValidityTime'$currentDateTime->format('Y-m-d h:i:s'));
  650.                         $session->set('appValid'$appValid);
  651.                     }
  652.                     $session->set('appIsValidTillTime'$appIsValidTillDateTime->format('Y-m-d h:i:s'));
  653.                     if ($appValid != 1) {
  654.                         $url $this->router->generate('user_logout');
  655. //                $request->headers->setCookie(Cookie::create('CLEARLOGINCOOKIE', 1));
  656.                         $event->setController(function () use ($url) {
  657.                             return new RedirectResponse($url);
  658.                         });
  659.                     }
  660.                     $gocId $session->get(UserConstants::USER_GOC_ID);
  661.                     if ($gocId != && $gocId != "") {
  662.                         $gocDbName $session->get(UserConstants::USER_DB_NAME);
  663.                         $gocDbUser $session->get(UserConstants::USER_DB_USER);
  664.                         $gocDbPass $session->get(UserConstants::USER_DB_PASS);
  665.                         $gocDbHost $session->get(UserConstants::USER_DB_HOST);
  666. //                        $connector = $this->container->get('application_connector');
  667.                         $connector $this->applicationConnector;
  668.                         $connector->resetConnection(
  669.                             'default',
  670.                             $gocDbName,
  671.                             $gocDbUser,
  672.                             $gocDbPass,
  673.                             $gocDbHost,
  674.                             $reset true);
  675.                     }
  676.                 }
  677.             } else {
  678.                 // Seriously !! How did this person get this url. Track the pc if possible.
  679.                 $url $this->router->generate('user_login');
  680.                 $event->setController(function () use ($url) {
  681.                     return new RedirectResponse($url);
  682.                 });
  683.             }
  684.         }
  685.         //dashboard for diff aspects
  686.         if ($event->getRequest()->attributes->get('_route') == 'dashboard') {
  687.             $systemType '_ERP';//default
  688.             $systemType $this->container->hasParameter('system_type') ? $this->container->getParameter('system_type') : '_ERP_';
  689.             if ($systemType == '_ERP_') {
  690.                 //do nothing its default to dashboard index
  691.             } else if ($systemType == '_BUDDYBEE_') {
  692.                 $event->getRequest()->attributes->set('_controller''ApplicationBundle:ConsultancyPublic:ConsultancyHomePage');
  693.                 $event->setController($this->resolver->getController($request));
  694.             }
  695. //            $event->setController(function() use ($url) {
  696. //                return new RedirectResponse($url);
  697. //            });
  698. //            $event->setController((\ApplicationBundle\Controller\ConsultancyPublicController())->ConsultancyHomePageAction() );
  699.         }
  700.     }
  701.     public
  702.     function onKernelResponse(FilterResponseEvent $event)
  703.     {
  704.         $session $this->session;
  705.         $request $event->getRequest();
  706.         $response $event->getResponse();
  707.         //temp debug
  708.         $currRoute $request->attributes->get('_route');
  709.         if (
  710.             ($request->request->has('returnJson') || $request->request->has('remoteVerify')) &&
  711.             $request->request->has('token') &&
  712.             !in_array($currRoute, [
  713.                 'select_data_ajax',
  714.                 'select_data_api',
  715.                 'select_data_ajax_public',
  716.             ])
  717.         ) {
  718.             $currDate = new \DateTime();
  719.             $to_write '';
  720.             $to_write .= "\nToken--" $request->request->get('token''NONE') . '  : ' $currDate->format('F d Y H:i:s');
  721.             $to_write .= "\nResponse:\n
  722.             ";
  723.             if (is_string($response->getContent()))
  724.                 $to_write .= $response->getContent();
  725.             else
  726.                 $to_write .= json_encode($response->getContent());
  727. //            System::log_it($this->container->getParameter('kernel.root_dir'), $to_write, $currRoute, 1);
  728.         }
  729.         if ($session->has('remoteVerified')) {
  730.             $lifetime 86400// 1 day
  731. //            $response->headers->setCookie(Cookie::create('PHPSESSID', $event->getRequest()->cookies->get('PHPSESSID'),time()+$lifetime));
  732.             $response->headers->set('Access-Control-Allow-Origin'$event->getRequest()->headers->get('origin'));
  733.             $response->headers->set('Access-Control-Allow-Credentials''true');
  734. //            $response->headers->setCookie(Cookie::create('PIKAMASTER', $event->getRequest()->cookies->get('PHPSESSID'),time()+$lifetime));
  735.         } else {
  736.             // create a hash and set it as a response header
  737. //        $hash = sha1($response->getContent().$token);
  738. //            $response->headers->set('Access-Control-Allow-Origin', '*');
  739. //            $response->headers->set('Access-Control-Allow-Credentials', 'true');
  740.             $response->headers->set('Access-Control-Allow-Origin'$event->getRequest()->headers->get('origin'));
  741.             $response->headers->set('Access-Control-Allow-Credentials''true');
  742. //            $response->headers->setCookie(Cookie::create('honeybeeCAuthId', $session->get(UserConstants::USER_ID)));
  743. //            $response->headers->setCookie(Cookie::create('honeybeeCAuthId2', $event->getRequest()->cookies->get('PHPSESSID')));
  744.         }
  745. //        if($session->has(UserConstants::USER_ID)) {
  746. //            if ($session->get(UserConstants::USER_ID) == '' || $session->get(UserConstants::USER_ID) == '') {
  747. //                $response->headers->setCookie(Cookie::create('USRCKIE', ''));
  748. //                $response->headers->setCookie(Cookie::create('REMEMBERME', ''));
  749. //                $response->headers->setCookie(Cookie::create('CLEARLOGINCOOKIE', ''));
  750. //            }
  751. //        }
  752. //        else
  753. //        {
  754. //            $response->headers->setCookie(Cookie::create('USRCKIE', ''));
  755. //            $response->headers->setCookie(Cookie::create('REMEMBERME', ''));
  756. //            $response->headers->setCookie(Cookie::create('CLEARLOGINCOOKIE', ''));
  757. //        }
  758.         if ($session->has('CLEARLOGIN')) {
  759.             if ($session->get('CLEARLOGIN') == 1) {
  760.                 if (version_compare(PHP_VERSION'7.0.0''>=')) {
  761.                     $response->headers->setCookie(Cookie::create('USRCKIE'''));
  762.                     $response->headers->setCookie(Cookie::create('REMEMBERME'''));
  763.                     $response->headers->setCookie(Cookie::create('CLEARLOGINCOOKIE'''));
  764.                 } else {
  765.                     $response->headers->setCookie(new Cookie('USRCKIE'''));
  766.                     $response->headers->setCookie(new Cookie('REMEMBERME'''));
  767.                     $response->headers->setCookie(new Cookie('CLEARLOGINCOOKIE'''));
  768.                 }
  769.             }
  770.             $session->set('CLEARLOGIN'0);
  771.         } else {
  772.         }
  773.         if ($session->has('REMEMBERME')) {
  774.             if ($session->get('REMEMBERME') == 1) {
  775.                 $lifetime 86400// 1 day
  776.                 if (version_compare(PHP_VERSION'7.0.0''>=')) {
  777.                     $response->headers->setCookie(Cookie::create('USRCKIE'json_encode(array(
  778.                             'uid' => $session->get(UserConstants::USER_ID),
  779.                             'ut' => $session->get(UserConstants::USER_TYPE),
  780.                             'companyId' => $session->get(UserConstants::USER_COMPANY_ID),
  781.                             'gocId' => $session->get(UserConstants::USER_GOC_ID),
  782.                         )
  783.                     ), time() + $lifetime
  784.                     )
  785.                     );
  786.                     $response->headers->setCookie(Cookie::create('REMEMBERMEACTIVATED''PIKA'));
  787.                 } else {
  788.                     $response->headers->setCookie(new Cookie('USRCKIE'json_encode(array(
  789.                                 'uid' => $session->get(UserConstants::USER_ID),
  790.                                 'ut' => $session->get(UserConstants::USER_TYPE),
  791.                                 'companyId' => $session->get(UserConstants::USER_COMPANY_ID),
  792.                                 'gocId' => $session->get(UserConstants::USER_GOC_ID),
  793.                             )
  794.                         ), time() + $lifetime
  795.                         )
  796.                     );
  797.                     $response->headers->setCookie(new Cookie('REMEMBERMEACTIVATED''PIKA'));
  798.                 }
  799.             } else {
  800.                 if (version_compare(PHP_VERSION'7.0.0''>=')) {
  801.                     $response->headers->setCookie(Cookie::create('USRCKIE'json_encode(array(
  802.                                 'uid' => $session->get(UserConstants::USER_ID),
  803.                                 'ut' => $session->get(UserConstants::USER_TYPE),
  804.                                 'companyId' => $session->get(UserConstants::USER_COMPANY_ID),
  805.                                 'gocId' => $session->get(UserConstants::USER_GOC_ID),
  806.                             )
  807.                         )
  808.                     )
  809.                     );
  810.                     $response->headers->setCookie(Cookie::create('REMEMBERMEACTIVATED''MASTER'));
  811.                 } else {
  812.                     $response->headers->setCookie(new Cookie('USRCKIE'json_encode(array(
  813.                                     'uid' => $session->get(UserConstants::USER_ID),
  814.                                     'ut' => $session->get(UserConstants::USER_TYPE),
  815.                                     'companyId' => $session->get(UserConstants::USER_COMPANY_ID),
  816.                                     'gocId' => $session->get(UserConstants::USER_GOC_ID),
  817.                                 )
  818.                             )
  819.                         )
  820.                     );
  821.                     $response->headers->setCookie(new Cookie('REMEMBERMEACTIVATED''MASTER'));
  822.                 }
  823.             }
  824.         } else {
  825.             if (version_compare(PHP_VERSION'7.0.0''>='))
  826.                 $response->headers->setCookie(Cookie::create('REMEMBERMEACTIVATED''CHU'));
  827.             else
  828.                 $response->headers->setCookie(new Cookie('REMEMBERMEACTIVATED''CHU'));
  829.         }
  830.     }
  831. }