/**
  * Include HTML layout file and extract data.
  *
  * @return string
  */
 public function render()
 {
     $getRoute = function ($rout, $params = null) {
         $router = ServiceContainer::get('route');
         return $router->buildRoute($rout, $params);
     };
     $user = ServiceContainer::get('session')->get('authenticated');
     $route = ServiceContainer::get('route')->findRoute();
     $include = function ($controller, $action, $vars = array()) {
         $response = ServiceContainer::get('app')->startController($controller, $action, $vars);
         if ($response) {
             $response->send();
         }
     };
     $generateToken = function () {
         $token = ServiceContainer::get('security')->generateToken();
         echo '<input type="hidden" name="_token" value="' . $token . '">';
     };
     $data['getRoute'] = $getRoute;
     $data['generateToken'] = $generateToken;
     ob_start();
     // начался буфер
     extract($this->data);
     include $this->layout;
     return ob_get_clean();
     // закончился буфер
 }
 /**
  * Redirects user to specified url.
  */
 public function send()
 {
     $request = ServiceContainer::get('request');
     header('Referer: ' . $request->getUri());
     header('Location: ' . $this->url, $this->replace, $this->code);
     exit;
 }
 public function run()
 {
     ServiceContainer::get('security')->generateToken();
     try {
         if (!ServiceContainer::get('security')->checkToken()) {
             die('Invalid token');
         }
         $map = $this->config['routes'];
         ServiceContainer::set('route', new Router($map));
         $match_route = ServiceContainer::get('route');
         $route = $match_route->findRoute();
         if (!empty($route['security'])) {
             $user = ServiceContainer::get('session')->get('authenticated');
             if (!empty($user->user_role) != 'ROLE_USER') {
                 $msg = 'Access denied, please login to your account!';
                 $fsg = ServiceContainer::get('session');
                 $fsg->setFlush('error', $msg);
                 $redirect = new ResponseRedirect(ServiceContainer::get('route')->buildRoute('security_signin'));
                 $redirect->send();
             }
         }
         if (class_exists($route['controller'])) {
             $controller = $route['controller'];
             $action = $route['action'];
             $vars = NULL;
             if (!empty($route['vars'])) {
                 $vars = $route['vars'];
             }
             $response = $this->startController($controller, $action, $vars);
         } else {
             throw new HttpNotFoundException();
         }
     } catch (HttpNotFoundException $e) {
         $error_layout = $this->config['not_found'];
         $renderer = new Renderer($error_layout, array('message' => $e->getMessage(), 'code' => $e->getCode()));
         $response = new Response($renderer->render());
     }
     $flush = ServiceContainer::get('session')->get('flush') ? ServiceContainer::get('session')->get('flush') : array();
     ServiceContainer::get('session')->unsetSession('flush');
     if ($response instanceof Response) {
         if ($response->getType() == 'html') {
             $view = $this->config['main_layout'];
             $renderer = new Renderer($view, array('content' => $response->getContent(), 'flush' => $flush));
             $wrapped = $renderer->render();
             $response = new Response($wrapped);
             $response->send();
         } elseif ($response->getType() == 'json') {
             $response = new ResponseJson();
             $response->send();
         }
     }
 }
 /**
  * Save avatar to new dir
  *
  * @return bool
  */
 public static function avatarSave()
 {
     $file = ServiceContainer::get('request')->file('avatarupload');
     $user = ServiceContainer::get('session')->get('authenticated');
     $fname = $file['name'];
     $email = $user->user_email;
     $explode = explode('@', $email);
     $email_name = array_shift($explode);
     $name = $user->user_name;
     $path_to_avatar = $name . $email_name;
     // Переделать сохранение аватарки !!!!
     if (empty(is_dir(__DIR__ . '/../../web/upload/avatar/' . $path_to_avatar))) {
         mkdir(__DIR__ . '/../../web/upload/avatar/' . $path_to_avatar, 0777, true);
     }
     $path = '/upload/avatar/' . $path_to_avatar . '/' . $fname;
     $uploadfile = __DIR__ . '/../../web/upload/avatar/' . $path_to_avatar . '/' . $fname;
     move_uploaded_file($file['tmp_name'], $uploadfile);
     $db = QueryConnect::getDatabase();
     $user_id = self::isUserAuth();
     $query = 'UPDATE users SET user_avatar = :path WHERE user_id =' . $user_id;
     $statement = $db->prepare($query);
     $statement->execute([':path' => $path]);
     return true;
 }
 /**
  * Takes an array with a map of the routes.
  *
  * @param array $map
  */
 public function __construct($map = array())
 {
     $this->routing_map = $map;
     $this->request = ServiceContainer::get('request');
 }
 /**
  * Checks if this token the user in the cookie.
  *
  * @return bool
  */
 public function checkToken()
 {
     $token = ServiceContainer::get('request')->post('_token') ? ServiceContainer::get('request')->post('_token') : null;
     if (!is_null($token)) {
         return $token == $_COOKIE['token'] ? true : false;
     } else {
         return true;
     }
 }
 /**
  * Logout
  *
  * @return ResponseRedirect
  */
 public function logoutAction()
 {
     ServiceContainer::get('security')->clear();
     return $this->redirect($this->generateRoute('performance_home'));
 }