/** * Initializes the URI object based on the url set on the object */ public function init() { $grav = Grav::instance(); $config = $grav['config']; $language = $grav['language']; // resets $this->paths = []; $this->params = []; $this->query = []; // get any params and remove them $uri = str_replace($this->root, '', $this->url); // remove the setup.php based base if set: $setup_base = $grav['pages']->base(); if ($setup_base) { $uri = str_replace($setup_base, '', $uri); } // If configured to, redirect trailing slash URI's with a 301 redirect if ($config->get('system.pages.redirect_trailing_slash', false) && $uri != '/' && Utils::endsWith($uri, '/')) { $grav->redirect(rtrim($uri, '/'), 301); } // process params $uri = $this->processParams($uri, $config->get('system.param_sep')); // set active language $uri = $language->setActiveFromUri($uri); // redirect to language specific homepage if configured to do so if ($uri == '/' && $language->enabled()) { if ($config->get('system.languages.home_redirect.include_route', true)) { $prefix = $config->get('system.languages.home_redirect.include_lang', true) ? $language->getLanguage() . '/' : ''; $grav->redirect($prefix . Pages::getHomeRoute()); } elseif ($config->get('system.languages.home_redirect.include_lang', true)) { $grav->redirect($language->getLanguage() . '/'); } } // split the URL and params $bits = parse_url($uri); // process query string if (isset($bits['query'])) { parse_str($bits['query'], $this->query); $uri = $bits['path']; } // remove the extension if there is one set $parts = pathinfo($uri); // set the original basename $this->basename = $parts['basename']; $valid_page_types = implode('|', $config->get('system.pages.types')); if (preg_match("/\\.(" . $valid_page_types . ")\$/", $parts['basename'])) { $uri = rtrim(str_replace(DIRECTORY_SEPARATOR, DS, $parts['dirname']), DS) . '/' . $parts['filename']; $this->extension = $parts['extension']; } // set the new url $this->url = $this->root . $uri; $this->path = $uri; $this->content_path = trim(str_replace($this->base, '', $this->path), '/'); if ($this->content_path != '') { $this->paths = explode('/', $this->content_path); } }
public function testEndsWith() { $this->assertTrue(Utils::endsWith('english', 'sh')); $this->assertTrue(Utils::endsWith('EngliSh', 'Sh')); $this->assertTrue(Utils::endsWith('ENGLISH', 'SH')); $this->assertTrue(Utils::endsWith('ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH', 'ENGLISH')); $this->assertFalse(Utils::endsWith('english', 'de')); $this->assertFalse(Utils::endsWith('EngliSh', 'sh')); $this->assertFalse(Utils::endsWith('ENGLISH', 'Sh')); $this->assertFalse(Utils::endsWith('ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH', 'DEUSTCH')); }
public function endsWithFilter($haystack, $needle) { return Utils::endsWith($haystack, $needle); }
/** * Dispatch URI to a page. * * @param $url * @param bool $all * @return Page|null */ public function dispatch($url, $all = false) { // Fetch page if there's a defined route to it. $page = isset($this->routes[$url]) ? $this->get($this->routes[$url]) : null; // Try without trailing slash if (!$page && Utils::endsWith($url, '/')) { $page = isset($this->routes[rtrim($url, '/')]) ? $this->get($this->routes[rtrim($url, '/')]) : null; } // Are we in the admin? this is important! $not_admin = !isset($this->grav['admin']); // If the page cannot be reached, look into site wide redirects, routes + wildcards if (!$all && $not_admin && (!$page || $page && !$page->routable() || $page && $page->redirect())) { // If the page is a simple redirect, just do it. if ($page && $page->redirect()) { $this->grav->redirectLangSafe($page->redirect()); } /** @var Config $config */ $config = $this->grav['config']; // See if route matches one in the site configuration $route = $config->get("site.routes.{$url}"); if ($route) { $page = $this->dispatch($route, $all); } else { // Try Regex style redirects foreach ((array) $config->get("site.redirects") as $pattern => $replace) { $pattern = '#' . $pattern . '#'; try { $found = preg_replace($pattern, $replace, $url); if ($found != $url) { $this->grav->redirectLangSafe($found); } } catch (ErrorException $e) { $this->grav['log']->error('site.redirects: ' . $pattern . '-> ' . $e->getMessage()); } } // Try Regex style routes foreach ((array) $config->get("site.routes") as $pattern => $replace) { $pattern = '#' . $pattern . '#'; try { $found = preg_replace($pattern, $replace, $url); if ($found != $url) { $page = $this->dispatch($found, $all); } } catch (ErrorException $e) { $this->grav['log']->error('site.routes: ' . $pattern . '-> ' . $e->getMessage()); } } } } return $page; }
/** * @param $package * * @return bool|string */ private function getSymlinkSource($package) { $matches = $this->getGitRegexMatches($package); foreach ($this->local_config as $path) { if (Utils::endsWith($matches[2], '.git')) { $repo_dir = preg_replace('/\\.git$/', '', $matches[2]); } else { $repo_dir = $matches[2]; } $from = rtrim($path, '/') . '/' . $repo_dir; if (file_exists($from)) { return $from; } } return false; }
/** * Initializes the URI object based on the url set on the object */ public function init() { $grav = Grav::instance(); $config = $grav['config']; $language = $grav['language']; // resets $this->paths = []; $this->params = []; $this->query = []; // get any params and remove them $uri = str_replace($this->root, '', $this->url); // remove double slashes $uri = preg_replace('#/{2,}#', '/', $uri); // remove the setup.php based base if set: $setup_base = $grav['pages']->base(); if ($setup_base) { $uri = str_replace($setup_base, '', $uri); } // If configured to, redirect trailing slash URI's with a 301 redirect if ($config->get('system.pages.redirect_trailing_slash', false) && $uri != '/' && Utils::endsWith($uri, '/')) { $grav->redirect(rtrim($uri, '/'), 301); } // process params $uri = $this->processParams($uri, $config->get('system.param_sep')); // set active language $uri = $language->setActiveFromUri($uri); // split the URL and params $bits = parse_url($uri); // process query string if (isset($bits['query']) && isset($bits['path'])) { $this->query = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING); $uri = $bits['path']; } // remove the extension if there is one set $parts = pathinfo($uri); // set the original basename $this->basename = $parts['basename']; // set the extension if (isset($parts['extension'])) { $this->extension = $parts['extension']; } $valid_page_types = implode('|', $config->get('system.pages.types')); // Strip the file extension for valid page types if (preg_match("/\\.(" . $valid_page_types . ")\$/", $parts['basename'])) { $uri = rtrim(str_replace(DIRECTORY_SEPARATOR, DS, $parts['dirname']), DS) . '/' . $parts['filename']; } // set the new url $this->url = $this->root . $uri; $this->path = $uri; $this->content_path = trim(str_replace($this->base, '', $this->path), '/'); if ($this->content_path != '') { $this->paths = explode('/', $this->content_path); } }
/** * Initializes the URI object based on the url set on the object */ public function init() { $grav = Grav::instance(); $config = $grav['config']; $language = $grav['language']; // add the port to the base for non-standard ports if ($config->get('system.reverse_proxy_setup') === false && $this->port != '80' && $this->port != '443') { $this->base .= ":" . $this->port; } // Set some defaults $this->root = $this->base . $this->root_path; $this->url = $this->base . $this->uri; // get any params and remove them $uri = str_replace($this->root, '', $this->url); // remove double slashes $uri = preg_replace('#/{2,}#', '/', $uri); // remove the setup.php based base if set: $setup_base = $grav['pages']->base(); if ($setup_base) { $uri = str_replace($setup_base, '', $uri); } // If configured to, redirect trailing slash URI's with a 301 redirect if ($config->get('system.pages.redirect_trailing_slash', false) && $uri != '/' && Utils::endsWith($uri, '/')) { $grav->redirect(rtrim($uri, '/'), 301); } // process params $uri = $this->processParams($uri, $config->get('system.param_sep')); // set active language $uri = $language->setActiveFromUri($uri); // split the URL and params $bits = parse_url($uri); // process query string if (isset($bits['query']) && isset($bits['path'])) { if (!$this->query) { $this->query = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING); } $uri = $bits['path']; } //process fragment if (isset($bits['fragment'])) { $this->fragment = $bits['fragment']; } // remove the extension if there is one set $parts = pathinfo($uri); // set the original basename $this->basename = $parts['basename']; // set the extension if (isset($parts['extension'])) { $this->extension = $parts['extension']; } $valid_page_types = implode('|', $config->get('system.pages.types')); // Strip the file extension for valid page types if (preg_match('/\\.(' . $valid_page_types . ')$/', $parts['basename'])) { $uri = rtrim(str_replace(DIRECTORY_SEPARATOR, DS, $parts['dirname']), DS) . '/' . $parts['filename']; } // set the new url $this->url = $this->root . $uri; $this->path = $uri; $this->content_path = trim(str_replace($this->base, '', $this->path), '/'); if ($this->content_path != '') { $this->paths = explode('/', $this->content_path); } // Set some Grav stuff $grav['base_url_absolute'] = $this->rootUrl(true); $grav['base_url_relative'] = $this->rootUrl(false); $grav['base_url'] = $grav['config']->get('system.absolute_urls') ? $grav['base_url_absolute'] : $grav['base_url_relative']; }
/** * 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; }