Exemple #1
0
 /**
  * Check session state
  *
  * @param  Application  $app
  * @param  Request      $request
  * @return JsonResponse
  */
 public function updateSession(Application $app, Request $request)
 {
     if (!$request->isXmlHttpRequest()) {
         $app->abort(400);
     }
     $ret = ['status' => 'unknown', 'message' => '', 'notifications' => false, 'changed' => []];
     if ($app['authentication']->isAuthenticated()) {
         $usr_id = $app['authentication']->getUser()->getId();
         if ($usr_id != $request->request->get('usr')) {
             // I logged with another user
             $ret['status'] = 'disconnected';
             return $app->json($ret);
         }
     } else {
         $ret['status'] = 'disconnected';
         return $app->json($ret);
     }
     try {
         $app['phraseanet.appbox']->get_connection();
     } catch (\Exception $e) {
         return $app->json($ret);
     }
     if (1 > ($moduleId = (int) $request->request->get('module'))) {
         $ret['message'] = 'Missing or Invalid `module` parameter';
         return $app->json($ret);
     }
     $session = $app['EM']->find('Phraseanet:Session', $app['session']->get('session_id'));
     $session->setUpdated(new \DateTime());
     if (!$session->hasModuleId($moduleId)) {
         $module = new SessionModule();
         $module->setModuleId($moduleId);
         $module->setSession($session);
         $app['EM']->persist($module);
     } else {
         $app['EM']->persist($session->getModuleById($moduleId)->setUpdated(new \DateTime()));
     }
     $app['EM']->persist($session);
     $app['EM']->flush();
     $ret['status'] = 'ok';
     $ret['notifications'] = $app['twig']->render('prod/notifications.html.twig', ['notifications' => $app['events-manager']->get_notifications()]);
     $baskets = $app['EM']->getRepository('Phraseanet:Basket')->findUnreadActiveByUser($app['authentication']->getUser());
     foreach ($baskets as $basket) {
         $ret['changed'][] = $basket->getId();
     }
     if (in_array($app['session']->get('phraseanet.message'), ['1', null])) {
         if ($app['conf']->get(['main', 'maintenance'])) {
             $ret['message'] .= $app->trans('The application is going down for maintenance, please logout.');
         }
         if ($app['conf']->get(['registry', 'maintenance', 'enabled'])) {
             $ret['message'] .= strip_tags($app['conf']->get(['registry', 'maintenance', 'message']));
         }
     }
     return $app->json($ret);
 }
Exemple #2
0
 /**
  * {@inheritdoc}
  */
 public function apply(base $appbox, Application $app)
 {
     try {
         $sql = 'SELECT usr_id, user_agent, ip, platform, browser, app,
                     browser_version, screen, token, nonce, lastaccess, created_on
                 FROM cache';
         $stmt = $appbox->get_connection()->prepare($sql);
         $stmt->execute();
         $rs = $stmt->fetchAll(\PDO::FETCH_ASSOC);
         $stmt->closeCursor();
     } catch (DBALException $e) {
         // this may fail on oldest versions
         return false;
     }
     foreach ($rs as $row) {
         if (null === ($user = $this->loadUser($app['EM'], $row['usr_id']))) {
             continue;
         }
         $created = $updated = null;
         if ('0000-00-00 00:00:00' !== $row['created_on']) {
             $created = \DateTime::createFromFormat('Y-m-d H:i:s', $row['created_on']);
         }
         if ('0000-00-00 00:00:00' !== $row['lastaccess']) {
             $updated = \DateTime::createFromFormat('Y-m-d H:i:s', $row['lastaccess']);
         }
         $session = new Session();
         $session->setUser($user)->setUserAgent($row['user_agent'])->setUpdated($updated)->setToken($row['token'])->setPlatform($row['platform'])->setNonce($row['nonce'])->setIpAddress($row['ip'])->setCreated($created)->setBrowserVersion($row['browser_version'])->setBrowserName($row['browser']);
         $sizes = explode('x', $row['screen']);
         if (2 === count($sizes)) {
             $session->setScreenWidth($sizes[0])->setScreenHeight($sizes[1]);
         }
         if (false !== ($apps = @unserialize($row['app']))) {
             foreach ($apps as $appli) {
                 $module = new SessionModule();
                 $module->setModuleId($appli)->setCreated($created)->setSession($session)->setUpdated($updated);
                 $session->addModule($module);
                 $app['EM']->persist($module);
             }
         }
         $app['EM']->persist($session);
     }
     $app['EM']->flush();
     return true;
 }
 /**
  * {@inheritDoc}
  */
 public function getSession()
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'getSession', array());
     return parent::getSession();
 }
Exemple #4
0
 public static function updateClientInfos(Application $app, $appId)
 {
     if (!$app['authentication']->isAuthenticated()) {
         return;
     }
     $session = $app['EM']->find('Phraseanet:Session', $app['session']->get('session_id'));
     if (!$session) {
         throw new SessionNotFound('No session found');
     }
     if (!$session->hasModuleId($appId)) {
         $module = new SessionModule();
         $module->setModuleId($appId);
         $module->setSession($session);
         $session->addModule($module);
         $app['EM']->persist($module);
         $app['EM']->persist($session);
         $app['EM']->flush();
     }
     $appName = ['1' => 'Prod', '2' => 'Client', '3' => 'Admin', '4' => 'Report', '5' => 'Thesaurus', '6' => 'Compare', '7' => 'Validate', '8' => 'Upload', '9' => 'API'];
     if (isset($appName[$appId])) {
         $sbas_ids = array_keys($app['acl']->get($app['authentication']->getUser())->get_granted_sbas());
         foreach ($sbas_ids as $sbas_id) {
             try {
                 $logger = $app['phraseanet.logger']($app['phraseanet.appbox']->get_databox($sbas_id));
                 $databox = $app['phraseanet.appbox']->get_databox($sbas_id);
                 $connbas = $databox->get_connection();
                 $sql = 'SELECT appli FROM log WHERE id = :log_id';
                 $stmt = $connbas->prepare($sql);
                 $stmt->execute([':log_id' => $logger->get_id()]);
                 $row3 = $stmt->fetch(PDO::FETCH_ASSOC);
                 $stmt->closeCursor();
                 if (!$row3) {
                     throw new Exception('no log');
                 }
                 $applis = unserialize($row3['appli']);
                 if (!in_array($appId, $applis)) {
                     $applis[] = $appId;
                 }
                 $sql = 'UPDATE log SET appli = :applis WHERE id = :log_id';
                 $params = [':applis' => serialize($applis), ':log_id' => $logger->get_id()];
                 $stmt = $connbas->prepare($sql);
                 $stmt->execute($params);
                 $stmt->closeCursor();
             } catch (\Exception $e) {
             }
         }
     }
     return;
 }
 public function checkSessionActivity(GetResponseEvent $event)
 {
     $modulesIds = ["prod" => 1, "client" => 2, "admin" => 3, "thesaurus" => 5, "report" => 10, "lightbox" => 6];
     $pathInfo = array_filter(explode('/', $event->getRequest()->getPathInfo()));
     if (count($pathInfo) < 1) {
         return;
     }
     $moduleName = strtolower($pathInfo[1]);
     if (!array_key_exists($moduleName, $modulesIds)) {
         return;
     }
     // this route is polled by js in admin/databox to refresh infos (progress bar...)
     if (preg_match("#^/admin/databox/[0-9]+/informations/documents/#", $event->getRequest()->getPathInfo()) == 1) {
         return;
     }
     // this route is polled by js in admin/tasks to refresh tasks status
     if ($event->getRequest()->getPathInfo() == "/admin/task-manager/tasks/" && $event->getRequest()->getContentType() == 'json') {
         return;
     }
     if ($this->isFlashUploadRequest($event->getRequest())) {
         return;
     }
     if ($event->getRequest()->query->has('LOG')) {
         return;
     }
     // if we are already disconnected (ex. from another window), quit immediatly
     if (!$this->app['authentication']->isAuthenticated()) {
         if ($event->getRequest()->isXmlHttpRequest()) {
             $response = new Response("End-Session", 403);
         } else {
             $response = new RedirectResponse($this->app["url_generator"]->generate("homepage", ["redirect" => '..' . $event->getRequest()->getPathInfo()]));
         }
         $response->headers->set('X-Phraseanet-End-Session', '1');
         $event->setResponse($response);
         return;
     }
     $session = $this->app['repo.sessions']->find($this->app['session']->get('session_id'));
     $idle = 0;
     if (isset($this->app["phraseanet.configuration"]["session"]["idle"])) {
         $idle = (int) $this->app["phraseanet.configuration"]["session"]["idle"];
     }
     $now = new \DateTime();
     $dt = $now->getTimestamp() - $session->getUpdated()->getTimestamp();
     if ($idle > 0 && $dt > $idle) {
         // we must disconnet due to idletime
         $this->app['authentication']->closeAccount();
         if ($event->getRequest()->isXmlHttpRequest()) {
             $response = new Response("End-Session", 403);
         } else {
             $response = new RedirectResponse($this->app["url_generator"]->generate("homepage", ["redirect" => '..' . $event->getRequest()->getPathInfo()]));
         }
         $response->headers->set('X-Phraseanet-End-Session', '1');
         $event->setResponse($response);
         return;
     }
     $moduleId = $modulesIds[$moduleName];
     $session->setUpdated(new \DateTime());
     if (!$session->hasModuleId($moduleId)) {
         $module = new SessionModule();
         $module->setModuleId($moduleId);
         $module->setSession($session);
         $session->addModule($module);
         $this->app['EM']->persist($module);
     } else {
         $this->app['EM']->persist($session->getModuleById($moduleId)->setUpdated(new \DateTime()));
     }
     $this->app['EM']->persist($session);
     $this->app['EM']->flush();
 }