/** * Create form for the given page. * * @param Page $page * @param null $name * @param null $form */ public function __construct(Page $page, $name = null, $form = null) { parent::__construct(); $this->page = $page->route(); $header = $page->header(); $this->rules = isset($header->rules) ? $header->rules : []; $this->header_data = isset($header->data) ? $header->data : []; if ($form) { $this->items = $form; } else { if (isset($header->form)) { $this->items = $header->form; // for backwards compatibility } } // Add form specific rules. if (!empty($this->items['rules']) && is_array($this->items['rules'])) { $this->rules += $this->items['rules']; } // Set form name if not set. if ($name && !is_int($name)) { $this->items['name'] = $name; } elseif (empty($this->items['name'])) { $this->items['name'] = $page->slug(); } // Set form id if not set. if (empty($this->items['id'])) { $inflector = new Inflector(); $this->items['id'] = $inflector->hyphenize($this->items['name']); } // Reset and initialize the form $this->reset(); }
/** * Replaces page object with admin one. */ public function onPagesInitialized() { // Create admin page. $page = new Page(); $page->init(new \SplFileInfo(__DIR__ . "/pages/gantry5.md")); $page->slug($this->template); $this->grav['page'] = $page; }
/** * Create form for the given page. * * @param Page $page */ public function __construct(Page $page) { $this->page = $page; $header = $page->header(); $this->rules = isset($header->rules) ? $header->rules : array(); $this->data = isset($header->data) ? $header->data : array(); $this->items = $header->form; // Set form name if not set. if (empty($this->items['name'])) { $this->items['name'] = $page->slug(); } }
/** * Create form for the given page. * * @param Page $page */ public function __construct(Page $page) { $this->page = $page; $header = $page->header(); $this->rules = isset($header->rules) ? $header->rules : array(); $this->data = isset($header->data) ? $header->data : array(); $this->items = $header->form; // Set form name if not set. if (empty($this->items['name'])) { $this->items['name'] = $page->slug(); } $this->reset(); // Fire event self::getGrav()->fireEvent('onFormInitialized', new Event(['form' => $this])); }
/** * Takes an individual page and processes the taxonomies configured in its header. It * then adds those taxonomies to the map * * @param Page $page the page to process * @param array $page_taxonomy */ public function addTaxonomy(Page $page, $page_taxonomy = null) { if (!$page_taxonomy) { $page_taxonomy = $page->taxonomy(); } /** @var Config $config */ $config = $this->grav['config']; if ($config->get('site.taxonomies') && count($page_taxonomy) > 0) { foreach ((array) $config->get('site.taxonomies') as $taxonomy) { if (isset($page_taxonomy[$taxonomy])) { foreach ((array) $page_taxonomy[$taxonomy] as $item) { // TODO: move to pages class? $this->taxonomy_map[$taxonomy][(string) $item][$page->path()] = array('slug' => $page->slug()); } } } } }
/** * Takes an individual page and processes the taxonomies configured in its header. It * then adds those taxonomies to the map * * @param Page $page the page to process * @param array $page_taxonomy */ public function addTaxonomy(Page $page, $page_taxonomy = null) { if (!$page_taxonomy) { $page_taxonomy = $page->taxonomy(); } if (!$page->published() || empty($page_taxonomy)) { return; } /** @var Config $config */ $config = $this->grav['config']; if ($config->get('site.taxonomies')) { foreach ((array) $config->get('site.taxonomies') as $taxonomy) { if (isset($page_taxonomy[$taxonomy])) { foreach ((array) $page_taxonomy[$taxonomy] as $item) { $this->taxonomy_map[$taxonomy][(string) $item][$page->path()] = ['slug' => $page->slug()]; } } } } }
/** * Return an array with the routes of other translated languages * @return array the page translated languages */ public function translatedLanguages() { $filename = substr($this->name, 0, -strlen($this->extension())); $config = self::getGrav()['config']; $languages = $config->get('system.languages.supported', []); $translatedLanguages = []; foreach ($languages as $language) { $path = $this->path . DS . $this->folder . DS . $filename . '.' . $language . '.md'; if (file_exists($path)) { $aPage = new Page(); $aPage->init(new \SplFileInfo($path), $language . '.md'); $route = isset($aPage->header()->routes['default']) ? $aPage->header()->routes['default'] : $aPage->rawRoute(); if (!$route) { $route = $aPage->slug(); } $translatedLanguages[$language] = $route; } } return $translatedLanguages; }
/** * @param Page $page * @return mixed */ private function getFormName(Page $page) { $name = filter_input(INPUT_POST, '__form-name__'); if (!$name) { $name = $page->slug(); } return $name; }
/** * Recursive function to load & build page relationships. * * @param string $directory * @param Page|null $parent * @return Page * @throws \RuntimeException * @internal */ protected function recurse($directory, Page &$parent = null) { $directory = rtrim($directory, DS); $iterator = new \DirectoryIterator($directory); $page = new Page(); /** @var Config $config */ $config = $this->grav['config']; $page->path($directory); if ($parent) { $page->parent($parent); } $page->orderDir($config->get('system.pages.order.dir')); $page->orderBy($config->get('system.pages.order.by')); // Add into instances if (!isset($this->instances[$page->path()])) { $this->instances[$page->path()] = $page; if ($parent && $page->path()) { $this->children[$parent->path()][$page->path()] = array('slug' => $page->slug()); } } else { throw new \RuntimeException('Fatal error when creating page instances.'); } // set current modified of page $last_modified = $page->modified(); // flat for content availability $content_exists = false; /** @var \DirectoryIterator $file */ foreach ($iterator as $file) { if ($file->isDot()) { continue; } $name = $file->getFilename(); if ($file->isFile()) { // Update the last modified if it's newer than already found if ($file->getBasename() !== '.DS_Store' && ($modified = $file->getMTime()) > $last_modified) { $last_modified = $modified; } if (preg_match('/^[^.].*' . CONTENT_EXT . '$/', $name)) { $page->init($file); $content_exists = true; if ($config->get('system.pages.events.page')) { $this->grav->fireEvent('onPageProcessed', new Event(['page' => $page])); } } } elseif ($file->isDir()) { if (!$page->path()) { $page->path($file->getPath()); } $path = $directory . DS . $name; $child = $this->recurse($path, $page); if (Utils::startsWith($name, '_')) { $child->routable(false); } $this->children[$page->path()][$child->path()] = array('slug' => $child->slug()); if ($config->get('system.pages.events.page')) { $this->grav->fireEvent('onFolderProcessed', new Event(['page' => $page])); } } } // Set routability to false if no page found if (!$content_exists) { $page->routable(false); } // Override the modified and ID so that it takes the latest change into account $page->modified($last_modified); $page->id($last_modified . md5($page->filePath())); // Sort based on Defaults or Page Overridden sort order $this->children[$page->path()] = $this->sort($page); return $page; }
/** * Recursive function to load & build page relationships. * * @param string $directory * @param Page|null $parent * @return Page * @throws \RuntimeException * @internal */ protected function recurse($directory, Page &$parent = null) { $directory = rtrim($directory, DS); $page = new Page(); /** @var Config $config */ $config = $this->grav['config']; /** @var Language $language */ $language = $this->grav['language']; // stuff to do at root page if ($parent === null) { // Fire event for memory and time consuming plugins... if ($config->get('system.pages.events.page')) { $this->grav->fireEvent('onBuildPagesInitialized'); } } $page->path($directory); if ($parent) { $page->parent($parent); } $page->orderDir($config->get('system.pages.order.dir')); $page->orderBy($config->get('system.pages.order.by')); // Add into instances if (!isset($this->instances[$page->path()])) { $this->instances[$page->path()] = $page; if ($parent && $page->path()) { $this->children[$parent->path()][$page->path()] = array('slug' => $page->slug()); } } else { throw new \RuntimeException('Fatal error when creating page instances.'); } $content_exists = false; $pages_found = glob($directory . '/*' . CONTENT_EXT); $page_extensions = $language->getFallbackPageExtensions(); if ($pages_found) { foreach ($page_extensions as $extension) { foreach ($pages_found as $found) { if (preg_match('/^.*\\/[0-9A-Za-z\\-\\_]+(' . $extension . ')$/', $found)) { $page_found = $found; $page_extension = $extension; break 2; } } } } if ($parent && !empty($page_found)) { $file = new \SplFileInfo($page_found); $page->init($file, $page_extension); $content_exists = true; if ($config->get('system.pages.events.page')) { $this->grav->fireEvent('onPageProcessed', new Event(['page' => $page])); } } // set current modified of page $last_modified = $page->modified(); /** @var \DirectoryIterator $file */ foreach (new \FilesystemIterator($directory) as $file) { $name = $file->getFilename(); // Ignore all hidden files if set. if ($this->ignore_hidden) { if ($name && $name[0] == '.') { continue; } } if ($file->isFile()) { // Update the last modified if it's newer than already found if (!in_array($file->getBasename(), $this->ignore_files) && ($modified = $file->getMTime()) > $last_modified) { $last_modified = $modified; } } elseif ($file->isDir() && !in_array($file->getFilename(), $this->ignore_folders)) { if (!$page->path()) { $page->path($file->getPath()); } $path = $directory . DS . $name; $child = $this->recurse($path, $page); if (Utils::startsWith($name, '_')) { $child->routable(false); } $this->children[$page->path()][$child->path()] = array('slug' => $child->slug()); if ($config->get('system.pages.events.page')) { $this->grav->fireEvent('onFolderProcessed', new Event(['page' => $page])); } } } // Set routability to false if no page found if (!$content_exists) { $page->routable(false); } // Override the modified and ID so that it takes the latest change into account $page->modified($last_modified); $page->id($last_modified . md5($page->filePath())); // Sort based on Defaults or Page Overridden sort order $this->children[$page->path()] = $this->sort($page); return $page; }
/** * Add a single page to a collection * * @param Page $page * * @return $this */ public function addPage(Page $page) { $this->items[$page->path()] = ['slug' => $page->slug()]; return $this; }
/** * Authorize Page */ public function authorizePage() { /** @var User $user */ $user = $this->grav['user']; /** @var Page $page */ $page = $this->grav['page']; if (!$page) { return; } $header = $page->header(); $rules = isset($header->access) ? (array) $header->access : []; $config = $this->mergeConfig($page); if ($config->get('parent_acl')) { // If page has no ACL rules, use its parent's rules if (!$rules) { $parent = $page->parent(); while (!$rules and $parent) { $header = $parent->header(); $rules = isset($header->access) ? (array) $header->access : []; $parent = $parent->parent(); } } } // Continue to the page if it has no ACL rules. if (!$rules) { return; } // Continue to the page if user is authorized to access the page. foreach ($rules as $rule => $value) { if ($user->authorize($rule) == $value) { return; } } // User is not logged in; redirect to login page. if ($this->route && !$user->authenticated) { $this->grav->redirect($this->route, 302); } /** @var Language $l */ $l = $this->grav['language']; // Reset page with login page. if (!$user->authenticated) { $page = new Page(); // Get the admin Login page is needed, else teh default if ($this->isAdmin()) { $login_file = $this->grav['locator']->findResource("plugins://admin/pages/admin/login.md"); $page->init(new \SplFileInfo($login_file)); } else { $page->init(new \SplFileInfo(__DIR__ . "/pages/login.md")); } $page->slug(basename($this->route)); $this->authenticated = false; unset($this->grav['page']); $this->grav['page'] = $page; } else { $this->grav['messages']->add($l->translate('PLUGIN_LOGIN.ACCESS_DENIED'), 'info'); $this->authenticated = false; $twig = $this->grav['twig']; $twig->twig_vars['notAuthorized'] = true; } }
/** * 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)); }
/** * Recursive function to load & build page relationships. * * @param string $directory * @param null $parent * @return Page * @throws \RuntimeException * @internal */ protected function recurse($directory = PAGES_DIR, Page &$parent = null) { $directory = rtrim($directory, DS); $iterator = new \DirectoryIterator($directory); $page = new Page(); $config = $this->grav['config']; $page->path($directory); if ($parent) { $page->parent($parent); } $page->orderDir($config->get('system.pages.order.dir')); $page->orderBy($config->get('system.pages.order.by')); // Add into instances if (!isset($this->instances[$page->path()])) { $this->instances[$page->path()] = $page; if ($parent && $page->path()) { $this->children[$parent->path()][$page->path()] = array('slug' => $page->slug()); } } else { throw new \RuntimeException('Fatal error when creating page instances.'); } $last_modified = 0; /** @var \DirectoryIterator $file */ foreach ($iterator as $file) { $name = $file->getFilename(); $date = $file->getMTime(); if ($date > $last_modified) { $last_modified = $date; } if ($file->isFile() && Utils::endsWith($name, CONTENT_EXT)) { $page->init($file); if ($config->get('system.pages.events.page')) { $this->grav->fireEvent('onPageProcessed', new Event(['page' => $page])); } } elseif ($file->isDir() && !$file->isDot()) { if (!$page->path()) { $page->path($file->getPath()); } $path = $directory . DS . $name; $child = $this->recurse($path, $page); if (Utils::startsWith($name, '_')) { $child->routable(false); } $this->children[$page->path()][$child->path()] = array('slug' => $child->slug()); // set the modified time if not already set if (!$page->date()) { $page->date($file->getMTime()); } // set the last modified time on pages $this->lastModified($file->getMTime()); if ($config->get('system.pages.events.page')) { $this->grav->fireEvent('onFolderProcessed', new Event(['page' => $page])); } } } // Override the modified and ID so that it takes the latest change // into account $page->modified($last_modified); $page->id($last_modified . md5($page->filePath())); // Sort based on Defaults or Page Overridden sort order $this->children[$page->path()] = $this->sort($page); return $page; }
/** * 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'), '/'); // Disable Asset pipelining $this->config->set('system.assets.css_pipeline', false); $this->config->set('system.assets.js_pipeline', false); // 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 : array(); // Handle tasks. $this->admin->task = $task = !empty($post['task']) ? $post['task'] : $this->uri->param('task'); if ($task) { require_once __DIR__ . '/classes/controller.php'; $controller = new AdminController($this->grav, $this->template, $task, $this->route, $post); $controller->execute(); $controller->redirect(); } elseif ($this->template == 'logs' && $this->route) { // Display RAW error message. echo $this->admin->logEntry(); exit; } $self = $this; // Replace page service with admin. $this->grav['page'] = function () use($self) { $page = new Page(); $page->init(new \SplFileInfo(__DIR__ . "/pages/admin/{$self->template}.md")); $page->slug(basename($self->template)); return $page; }; }
/** * Replaces page object with admin one. */ public function onAdminPagesInitialized() { // Create admin page. $page = new Page(); $page->init(new \SplFileInfo(__DIR__ . "/pages/gantry.md")); $page->slug('gantry'); // Dispatch Gantry in output buffer. ob_start(); $gantry = Gantry::instance(); $gantry['router']->dispatch(); $content = ob_get_clean(); // Store response into the page. $page->content($content); // Hook page into Grav as current page. unset($this->grav['page']); $this->grav['page'] = function () use($page) { return $page; }; }
/** * 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 : array(); // 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; } $self = $this; // make sure page is not frozen! unset($this->grav['page']); // Replace page service with admin. $this->grav['page'] = function () use($self) { $page = new Page(); 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; } // If the page cannot be found, try looking in plugins. // Allows pages added by plugins in admin $plugins = Grav::instance()['config']->get('plugins', []); foreach ($plugins as $plugin => $data) { $path = $this->grav['locator']->findResource("user://plugins/{$plugin}/admin/pages/{$self->template}.md"); if (file_exists($path)) { $page->init(new \SplFileInfo($path)); $page->slug(basename($self->template)); return $page; } } }; if (empty($this->grav['page'])) { $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); } } }
/** * 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; } // check for existence of a user account $account_dir = $file_path = $this->grav['locator']->findResource('account://'); $user_check = (array) glob($account_dir . '/*.yaml'); if (!count($user_check) > 0) { $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.NO_USER_ACCOUNTS'), 'info'); } /** @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 : array(); // Handle tasks. $this->admin->task = $task = !empty($post['task']) ? $post['task'] : $this->uri->param('task'); if ($task) { require_once __DIR__ . '/classes/controller.php'; $controller = new AdminController($this->grav, $this->template, $task, $this->route, $post); $controller->execute(); $controller->redirect(); } elseif ($this->template == 'logs' && $this->route) { // Display RAW error message. echo $this->admin->logEntry(); exit; } $self = $this; // Replace page service with admin. $this->grav['page'] = function () use($self) { $page = new Page(); 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; } // If the page cannot be found, try looking in plugins. // Allows pages added by plugins in admin $plugins = Grav::instance()['config']->get('plugins', []); foreach ($plugins as $plugin => $data) { $folder = GRAV_ROOT . "/user/plugins/" . $plugin . "/admin"; if (file_exists($folder)) { $file = $folder . "/pages/{$self->template}.md"; if (file_exists($file)) { $page->init(new \SplFileInfo($file)); $page->slug(basename($self->template)); return $page; } } } }; }
/** * Authorize Page */ public function authorizePage() { /** @var User $user */ $user = $this->grav['user']; /** @var Page $page */ $page = $this->grav['page']; $header = $page->header(); $rules = isset($header->access) ? (array) $header->access : []; // Continue to the page if it has no ACL rules. if (!$rules) { return; } // Continue to the page if user is authorized to access the page. foreach ($rules as $rule => $value) { if ($user->authorize($rule) == $value) { return; } } // User is not logged in; redirect to login page. if ($this->route && !$user->authenticated) { $this->grav->redirect($this->route, 302); } /** @var Language $l */ $l = $this->grav['language']; // Reset page with login page. if (!$user->authenticated) { $page = new Page(); $page->init(new \SplFileInfo(__DIR__ . "/pages/login.md")); $page->slug(basename($this->route)); $this->authenticated = false; unset($this->grav['page']); $this->grav['page'] = $page; } else { $this->grav['messages']->add($l->translate('LOGIN_PLUGIN.ACCESS_DENIED'), 'info'); $this->authenticated = false; $twig = $this->grav['twig']; $twig->twig_vars['notAuthorized'] = true; } }