Пример #1
0
 public function init()
 {
     /** @var Uri $uri */
     $uri = $this->grav['uri'];
     $config = $this->grav['config'];
     $is_admin = false;
     $session_timeout = $config->get('system.session.timeout', 1800);
     $session_path = $config->get('system.session.path', '/' . ltrim($uri->rootUrl(false), '/'));
     // Activate admin if we're inside the admin path.
     if ($config->get('plugins.admin.enabled')) {
         $route = $config->get('plugins.admin.route');
         $base = '/' . trim($route, '/');
         if (substr($uri->route(), 0, strlen($base)) == $base) {
             $session_timeout = $config->get('plugins.admin.session.timeout', 1800);
             $is_admin = true;
         }
     }
     if ($config->get('system.session.enabled') || $is_admin) {
         // Define session service.
         parent::__construct($session_timeout, $session_path);
         $unique_identifier = GRAV_ROOT;
         $this->setName($config->get('system.session.name', 'grav_site') . '-' . substr(md5($unique_identifier), 0, 7) . ($is_admin ? '-admin' : ''));
         $this->start();
         setcookie(session_name(), session_id(), time() + $session_timeout, $session_path);
     }
 }
Пример #2
0
 public function init()
 {
     /** @var Uri $uri */
     $uri = $this->grav['uri'];
     $config = $this->grav['config'];
     if ($config->get('system.session.enabled')) {
         // Define session service.
         parent::__construct($config->get('system.session.timeout', 1800), $config->get('system.session.path', '/' . ltrim($uri->rootUrl(false), '/')));
         $site_identifier = $config->get('site.title', 'unkown');
         $this->setName($config->get('system.session.name', 'grav_site') . '_' . substr(md5($site_identifier), 0, 7));
         $this->start();
     }
 }
Пример #3
0
 /**
  * Session init
  */
 public function init()
 {
     /** @var Uri $uri */
     $uri = $this->grav['uri'];
     $config = $this->grav['config'];
     $is_admin = false;
     $base_url = $uri->rootUrl(false);
     $session_timeout = $config->get('system.session.timeout', 1800);
     $session_path = $config->get('system.session.path');
     if (!$session_path) {
         $session_path = '/' . ltrim($base_url, '/');
     }
     // Activate admin if we're inside the admin path.
     if ($config->get('plugins.admin.enabled')) {
         $route = $config->get('plugins.admin.route');
         // Uri::route() is not processed yet, let's quickly get what we need
         $current_route = str_replace($base_url, '', parse_url($uri->url(true), PHP_URL_PATH));
         $base = '/' . trim($route, '/');
         if (substr($current_route, 0, strlen($base)) == $base || substr($current_route, 3, strlen($base)) == $base || substr($current_route, 6, strlen($base)) == $base) {
             //handle region specific language prefix (en-US)
             $session_timeout = $config->get('plugins.admin.session.timeout', 1800);
             $is_admin = true;
         }
     }
     if ($config->get('system.session.enabled') || $is_admin) {
         $domain = $uri->host();
         if ($domain === 'localhost') {
             $domain = '';
         }
         // Fix for HUGE session timeouts
         if ($session_timeout > 99999999999) {
             $session_timeout = 9999999999;
         }
         // Define session service.
         parent::__construct($session_timeout, $session_path, $domain);
         $secure = $config->get('system.session.secure', false);
         $httponly = $config->get('system.session.httponly', true);
         $unique_identifier = GRAV_ROOT;
         $inflector = new Inflector();
         $session_name = $inflector->hyphenize($config->get('system.session.name', 'grav_site')) . '-' . substr(md5($unique_identifier), 0, 7);
         $split_session = $config->get('system.session.split', true);
         if ($is_admin && $split_session) {
             $session_name .= '-admin';
         }
         $this->setName($session_name);
         $this->start();
         setcookie(session_name(), session_id(), time() + $session_timeout, $session_path, $domain, $secure, $httponly);
     }
 }
Пример #4
0
 public function onOutputGenerated()
 {
     // Clear flash objects for previously uploaded files
     // whenever the user switches page / reloads
     // ignoring any JSON / extension call
     if (is_null($this->uri->extension()) && $this->admin->task !== 'save') {
         // Discard any previously uploaded files session.
         // and if there were any uploaded file, remove them from the filesystem
         if ($flash = $this->session->getFlashObject('files-upload')) {
             $flash = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($flash));
             foreach ($flash as $key => $value) {
                 if ($key !== 'tmp_name') {
                     continue;
                 }
                 @unlink($value);
             }
         }
     }
 }
Пример #5
0
 /**
  * Sets longer path to the home page allowing us to have list of pages when we enter to pages section.
  */
 public function onPagesInitialized()
 {
     $this->session = $this->grav['session'];
     // Set original route for the home page.
     $home = '/' . trim($this->config->get('system.home.alias'), '/');
     // set the default if not set before
     $this->session->expert = $this->session->expert ?: false;
     // set session variable if it's passed via the url
     if ($this->uri->param('mode') == 'expert') {
         $this->session->expert = true;
     } elseif ($this->uri->param('mode') == 'normal') {
         $this->session->expert = false;
     }
     /** @var Pages $pages */
     $pages = $this->grav['pages'];
     $this->grav['admin']->routes = $pages->routes();
     // Remove default route from routes.
     if (isset($this->grav['admin']->routes['/'])) {
         unset($this->grav['admin']->routes['/']);
     }
     $page = $pages->dispatch('/', true);
     // If page is null, the default page does not exist, and we cannot route to it
     if ($page) {
         $page->route($home);
     }
     // Make local copy of POST.
     $post = !empty($_POST) ? $_POST : [];
     // Handle tasks.
     $this->admin->task = $task = !empty($post['task']) ? $post['task'] : $this->uri->param('task');
     if ($task) {
         $this->initializeController($task, $post);
     } elseif ($this->template == 'logs' && $this->route) {
         // Display RAW error message.
         echo $this->admin->logEntry();
         exit;
     }
     // Clear flash objects for previously uploaded files
     // whenever the user switches page / reloads
     // ignoring any JSON / extension call
     if (is_null($this->uri->extension()) && $task !== 'save') {
         // Discard any previously uploaded files session.
         // and if there were any uploaded file, remove them from the filesystem
         if ($flash = $this->session->getFlashObject('files-upload')) {
             $flash = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($flash));
             foreach ($flash as $key => $value) {
                 if ($key !== 'tmp_name') {
                     continue;
                 }
                 @unlink($value);
             }
         }
     }
     $self = $this;
     // make sure page is not frozen!
     unset($this->grav['page']);
     $this->admin->pagesCount();
     // Replace page service with admin.
     $this->grav['page'] = function () use($self) {
         $page = new Page();
         // If the page cannot be found in other plugins, try looking in admin plugin itself.
         if (file_exists(__DIR__ . "/pages/admin/{$self->template}.md")) {
             $page->init(new \SplFileInfo(__DIR__ . "/pages/admin/{$self->template}.md"));
             $page->slug(basename($self->template));
             return $page;
         }
         // Allows pages added by plugins in admin
         $plugins = $this->grav['plugins'];
         $locator = $this->grav['locator'];
         foreach ($plugins as $plugin) {
             $path = $locator->findResource("user://plugins/{$plugin->name}/admin/pages/{$self->template}.md");
             if ($path) {
                 $page->init(new \SplFileInfo($path));
                 $page->slug(basename($self->template));
                 return $page;
             }
         }
         return null;
     };
     if (empty($this->grav['page'])) {
         if ($this->grav['user']->authenticated) {
             $event = $this->grav->fireEvent('onPageNotFound');
             if (isset($event->page)) {
                 unset($this->grav['page']);
                 $this->grav['page'] = $event->page;
             } else {
                 throw new \RuntimeException('Page Not Found', 404);
             }
         } else {
             $this->grav->redirect($this->admin_route);
         }
     }
     // Explicitly set a timestamp on assets
     $this->grav['assets']->setTimestamp(substr(md5(GRAV_VERSION . $this->grav['config']->checksum()), 0, 10));
 }