/** * Set all twig variables for generating output. */ public function onTwigSiteVariables() { $twig = $this->grav['twig']; // Dynamic type support $format = $this->uri->extension(); $ext = '.' . ($format ? $format : 'html') . TWIG_EXT; $twig->twig_vars['location'] = $this->template; $twig->twig_vars['base_url_relative_frontend'] = $twig->twig_vars['base_url_relative'] ?: '/'; $twig->twig_vars['admin_route'] = trim($this->config->get('plugins.admin.route'), '/'); $twig->twig_vars['base_url_relative'] .= ($twig->twig_vars['base_url_relative'] != '/' ? '/' : '') . $twig->twig_vars['admin_route']; $twig->twig_vars['theme_url'] = '/user/plugins/admin/themes/' . $this->theme; $twig->twig_vars['base_url'] = $twig->twig_vars['base_url_relative']; $twig->twig_vars['base_path'] = GRAV_ROOT; $twig->twig_vars['admin'] = $this->admin; switch ($this->template) { case 'dashboard': $twig->twig_vars['popularity'] = $this->popularity; break; case 'pages': $page = $this->admin->page(true); if ($page != null) { $twig->twig_vars['file'] = File::instance($page->filePath()); $twig->twig_vars['media_types'] = str_replace('defaults,', '', implode(',.', array_keys($this->config->get('media')))); } break; } }
public function testExtension() { $this->uri->initializeWithURL('http://localhost/a-page')->init(); $this->assertSame(null, $this->uri->extension()); $this->uri->initializeWithURL('http://localhost/a-page')->init(); $this->assertSame('x', $this->uri->extension('x')); $this->uri->initializeWithURL('http://localhost/a-page.html')->init(); $this->assertSame('html', $this->uri->extension()); $this->uri->initializeWithURL('http://localhost/a-page.xml')->init(); $this->assertSame('xml', $this->uri->extension()); $this->uri->initializeWithURL('http://localhost/a-page.foo')->init(); $this->assertSame('foo', $this->uri->extension()); }
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); } } } }
/** * 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)); }