2012-11-21 12:35:53 +00:00
|
|
|
<?php
|
2017-10-19 19:55:41 +00:00
|
|
|
/**
|
|
|
|
* This file is part of Pico. It's copyrighted by the contributors recorded
|
|
|
|
* in the version control history of the file, available from the following
|
|
|
|
* original location:
|
|
|
|
*
|
|
|
|
* <https://github.com/picocms/Pico/blob/master/lib/Pico.php>
|
|
|
|
*
|
|
|
|
* The file has been renamed in the past; the version control history of the
|
|
|
|
* original file applies accordingly, available from the following original
|
|
|
|
* location:
|
|
|
|
*
|
|
|
|
* <https://github.com/picocms/Pico/blob/adc356251ecd79689935ec1446c7c846db167d46/lib/pico.php>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
* License-Filename: LICENSE
|
|
|
|
*/
|
2013-09-04 11:10:26 +00:00
|
|
|
|
2013-04-26 16:38:10 +00:00
|
|
|
/**
|
|
|
|
* Pico
|
|
|
|
*
|
2015-08-28 16:22:32 +00:00
|
|
|
* Pico is a stupidly simple, blazing fast, flat file CMS.
|
2016-04-24 19:13:47 +00:00
|
|
|
*
|
2015-11-04 18:50:44 +00:00
|
|
|
* - Stupidly Simple: Pico makes creating and maintaining a
|
2015-08-28 16:22:32 +00:00
|
|
|
* website as simple as editing text files.
|
|
|
|
* - Blazing Fast: Pico is seriously lightweight and doesn't
|
|
|
|
* use a database, making it super fast.
|
|
|
|
* - No Database: Pico is a "flat file" CMS, meaning no
|
|
|
|
* database woes, no MySQL queries, nothing.
|
|
|
|
* - Markdown Formatting: Edit your website in your favourite
|
|
|
|
* text editor using simple Markdown formatting.
|
|
|
|
* - Twig Templates: Pico uses the Twig templating engine,
|
|
|
|
* for powerful and flexible themes.
|
|
|
|
* - Open Source: Pico is completely free and open source,
|
|
|
|
* released under the MIT license.
|
2016-04-24 19:13:47 +00:00
|
|
|
*
|
2015-08-28 16:22:32 +00:00
|
|
|
* See <http://picocms.org/> for more info.
|
|
|
|
*
|
|
|
|
* @author Gilbert Pellegrom
|
|
|
|
* @author Daniel Rudolf
|
2016-05-23 13:13:56 +00:00
|
|
|
* @link http://picocms.org
|
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License
|
2019-07-14 13:07:12 +00:00
|
|
|
* @version 2.1
|
2013-04-26 16:38:10 +00:00
|
|
|
*/
|
2015-06-10 09:40:26 +00:00
|
|
|
class Pico
|
|
|
|
{
|
2016-03-05 23:38:51 +00:00
|
|
|
/**
|
|
|
|
* Pico version
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-07-14 13:07:12 +00:00
|
|
|
const VERSION = '2.1.0-nightly';
|
2016-04-22 12:31:14 +00:00
|
|
|
|
2016-04-22 12:23:46 +00:00
|
|
|
/**
|
|
|
|
* Pico version ID
|
2016-04-22 12:31:14 +00:00
|
|
|
*
|
2016-04-22 12:23:46 +00:00
|
|
|
* @var int
|
|
|
|
*/
|
2019-07-14 13:07:12 +00:00
|
|
|
const VERSION_ID = 20100;
|
2016-03-05 23:38:51 +00:00
|
|
|
|
2017-05-10 15:00:48 +00:00
|
|
|
/**
|
|
|
|
* Pico API version
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const API_VERSION = 2;
|
|
|
|
|
2015-10-04 12:15:11 +00:00
|
|
|
/**
|
|
|
|
* Sort files in alphabetical ascending order
|
|
|
|
*
|
|
|
|
* @see Pico::getFiles()
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const SORT_ASC = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort files in alphabetical descending order
|
|
|
|
*
|
|
|
|
* @see Pico::getFiles()
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const SORT_DESC = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Don't sort files
|
|
|
|
*
|
|
|
|
* @see Pico::getFiles()
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const SORT_NONE = 2;
|
|
|
|
|
2015-10-01 13:05:50 +00:00
|
|
|
/**
|
|
|
|
* Root directory of this Pico instance
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getRootDir()
|
2015-10-01 13:05:50 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $rootDir;
|
|
|
|
|
2017-05-01 20:27:57 +00:00
|
|
|
/**
|
|
|
|
* Vendor directory of this Pico instance
|
|
|
|
*
|
|
|
|
* @see Pico::getVendorDir()
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $vendorDir;
|
|
|
|
|
2015-10-01 13:05:50 +00:00
|
|
|
/**
|
|
|
|
* Config directory of this Pico instance
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getConfigDir()
|
2015-10-01 13:05:50 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $configDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plugins directory of this Pico instance
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getPluginsDir()
|
2015-10-01 13:05:50 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $pluginsDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Themes directory of this Pico instance
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getThemesDir()
|
2015-10-01 13:05:50 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $themesDir;
|
|
|
|
|
2015-10-06 18:38:34 +00:00
|
|
|
/**
|
2015-11-04 18:50:44 +00:00
|
|
|
* Boolean indicating whether Pico started processing yet
|
2015-10-06 18:38:34 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @var bool
|
2015-10-06 18:38:34 +00:00
|
|
|
*/
|
|
|
|
protected $locked = false;
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* List of loaded plugins
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getPlugins()
|
2017-05-07 12:20:44 +00:00
|
|
|
* @var object[]
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
2017-05-07 12:20:44 +00:00
|
|
|
protected $plugins = array();
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2017-05-10 15:00:48 +00:00
|
|
|
/**
|
|
|
|
* List of loaded plugins using the current API version
|
|
|
|
*
|
2017-10-07 22:36:29 +00:00
|
|
|
* @var PicoPluginInterface[]
|
2017-05-10 15:00:48 +00:00
|
|
|
*/
|
|
|
|
protected $nativePlugins = array();
|
|
|
|
|
2017-10-13 19:02:51 +00:00
|
|
|
/**
|
|
|
|
* Boolean indicating whether Pico loads plugins from the filesystem
|
|
|
|
*
|
|
|
|
* @see Pico::loadPlugins()
|
|
|
|
* @see Pico::loadLocalPlugins()
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $enableLocalPlugins = true;
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* Current configuration of this Pico instance
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getConfig()
|
2016-01-25 18:31:53 +00:00
|
|
|
* @var array|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected $config;
|
2015-06-10 09:40:26 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-27 00:39:28 +00:00
|
|
|
* Part of the URL describing the requested contents
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getRequestUrl()
|
2015-10-27 00:39:28 +00:00
|
|
|
* @var string|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected $requestUrl;
|
|
|
|
|
|
|
|
/**
|
2015-10-27 00:39:28 +00:00
|
|
|
* Absolute path to the content file being served
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getRequestFile()
|
2015-10-27 00:39:28 +00:00
|
|
|
* @var string|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected $requestFile;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Raw, not yet parsed contents to serve
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getRawContent()
|
2015-10-27 00:39:28 +00:00
|
|
|
* @var string|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected $rawContent;
|
|
|
|
|
2016-11-26 15:48:10 +00:00
|
|
|
/**
|
|
|
|
* Boolean indicating whether Pico is serving a 404 page
|
|
|
|
*
|
|
|
|
* @see Pico::is404Content()
|
2017-10-19 19:55:41 +00:00
|
|
|
* @var bool
|
2016-11-26 15:48:10 +00:00
|
|
|
*/
|
|
|
|
protected $is404Content = false;
|
|
|
|
|
2016-12-06 19:22:38 +00:00
|
|
|
/**
|
|
|
|
* Symfony YAML instance used for meta header parsing
|
|
|
|
*
|
2017-02-05 14:55:53 +00:00
|
|
|
* @see Pico::getYamlParser()
|
2016-12-06 19:22:38 +00:00
|
|
|
* @var \Symfony\Component\Yaml\Parser|null
|
|
|
|
*/
|
|
|
|
protected $yamlParser;
|
|
|
|
|
2016-12-06 19:01:38 +00:00
|
|
|
/**
|
|
|
|
* List of known meta headers
|
|
|
|
*
|
|
|
|
* @see Pico::getMetaHeaders()
|
|
|
|
* @var string[]|null
|
|
|
|
*/
|
|
|
|
protected $metaHeaders;
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* Meta data of the page to serve
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getFileMeta()
|
2016-01-07 02:00:21 +00:00
|
|
|
* @var array|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected $meta;
|
|
|
|
|
2015-11-12 14:33:48 +00:00
|
|
|
/**
|
|
|
|
* Parsedown Extra instance used for markdown parsing
|
|
|
|
*
|
|
|
|
* @see Pico::getParsedown()
|
2018-12-03 10:52:05 +00:00
|
|
|
* @var Parsedown|null
|
2015-11-12 14:33:48 +00:00
|
|
|
*/
|
|
|
|
protected $parsedown;
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* Parsed content being served
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getFileContent()
|
2015-10-27 00:39:28 +00:00
|
|
|
* @var string|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected $content;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of known pages
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getPages()
|
2015-10-27 00:48:58 +00:00
|
|
|
* @var array[]|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected $pages;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data of the page being served
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getCurrentPage()
|
2015-10-27 00:39:28 +00:00
|
|
|
* @var array|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected $currentPage;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data of the previous page relative to the page being served
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getPreviousPage()
|
2015-10-27 00:39:28 +00:00
|
|
|
* @var array|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected $previousPage;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data of the next page relative to the page being served
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getNextPage()
|
2015-10-27 00:39:28 +00:00
|
|
|
* @var array|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected $nextPage;
|
|
|
|
|
Add Pico::buildPageTree()
Pico's page tree is a list of all the tree's branches (no matter the depth). Thus, by iterating a array element, you get the nodes of a given branch. All leaf nodes do represent a page, but inner nodes may or may not represent a page (e.g. if there's a `sub/page.md`, but neither a `sub/index.md` nor a `sub.md`, the inner node `sub`, that is the parent of the `sub/page` node, represents no page itself).
A page's file path describes its node's path in the tree (e.g. the page `sub/page.md` is represented by the `sub/page` node, thus a child of the `sub` node and a element of the `sub` branch). However, the index page of a folder (e.g. `sub/index.md`), is *not* a node of the `sub` branch, but rather of the `/` branch. The page's node is not `sub/index`, but `sub`. If two pages are described by the same node (e.g. if both a `sub/index.md` and a `sub.md` exist), the index page takes precedence. Pico's main index page (i.e. `index.md`) is represented by the tree's root node `/` and a special case: it is the only node of the `` (i.e. the empty string) branch.
A node is represented by an array with the keys `id`, `page` and `children`. The `id` key contains a string with the node's name. If the node represents a page, the `page` key is a reference to the page's data array. If the node is a inner node, the `children` key is a reference to its matching branch (i.e. a list of the node's children). The order of a node's children matches the order in Pico's pages array.
If you want to walk the whole page tree, start with the tree's root node at `$pageTree[""]["/"]`. The root node's `children` key is a reference to the `/` branch at `$pageTree["/"]`, that is a list of the root node's direct child nodes and their siblings.
You MUST NOT iterate the page tree itself (i.e. the list of the tree's branches), its order is undefined and the array will be replaced by a non-iterable data structure with Pico 3.0.
2017-11-18 17:34:57 +00:00
|
|
|
/**
|
|
|
|
* Tree structure of known pages
|
|
|
|
*
|
|
|
|
* @see Pico::getPageTree()
|
|
|
|
* @var array[]|null
|
|
|
|
*/
|
|
|
|
protected $pageTree;
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* Twig instance used for template parsing
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getTwig()
|
2015-10-27 00:39:28 +00:00
|
|
|
* @var Twig_Environment|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected $twig;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Variables passed to the twig template
|
|
|
|
*
|
2017-02-05 19:20:26 +00:00
|
|
|
* @see Pico::getTwigVariables()
|
2016-01-25 18:31:53 +00:00
|
|
|
* @var array|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected $twigVariables;
|
|
|
|
|
2017-02-05 19:20:26 +00:00
|
|
|
/**
|
|
|
|
* Name of the Twig template to render
|
|
|
|
*
|
|
|
|
* @see Pico::getTwigTemplate()
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $twigTemplate;
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* Constructs a new Pico instance
|
|
|
|
*
|
2017-05-13 16:17:58 +00:00
|
|
|
* To carry out all the processing in Pico, call {@see Pico::run()}.
|
2015-10-27 00:39:28 +00:00
|
|
|
*
|
2017-10-13 19:02:51 +00:00
|
|
|
* @param string $rootDir root dir of this Pico instance
|
|
|
|
* @param string $configDir config dir of this Pico instance
|
|
|
|
* @param string $pluginsDir plugins dir of this Pico instance
|
|
|
|
* @param string $themesDir themes dir of this Pico instance
|
|
|
|
* @param bool $enableLocalPlugins enables (TRUE; default) or disables
|
|
|
|
* (FALSE) loading plugins from the filesystem
|
2015-10-01 13:05:50 +00:00
|
|
|
*/
|
2017-10-13 19:02:51 +00:00
|
|
|
public function __construct($rootDir, $configDir, $pluginsDir, $themesDir, $enableLocalPlugins = true)
|
2015-10-01 13:05:50 +00:00
|
|
|
{
|
2015-12-23 15:17:06 +00:00
|
|
|
$this->rootDir = rtrim($rootDir, '/\\') . '/';
|
2017-05-01 20:27:57 +00:00
|
|
|
$this->vendorDir = dirname(__DIR__) . '/';
|
2015-10-01 13:14:45 +00:00
|
|
|
$this->configDir = $this->getAbsolutePath($configDir);
|
|
|
|
$this->pluginsDir = $this->getAbsolutePath($pluginsDir);
|
|
|
|
$this->themesDir = $this->getAbsolutePath($themesDir);
|
2017-10-13 19:02:51 +00:00
|
|
|
$this->enableLocalPlugins = (bool) $enableLocalPlugins;
|
2015-10-01 13:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the root directory of this Pico instance
|
|
|
|
*
|
|
|
|
* @return string root directory path
|
|
|
|
*/
|
|
|
|
public function getRootDir()
|
|
|
|
{
|
|
|
|
return $this->rootDir;
|
|
|
|
}
|
|
|
|
|
2017-05-01 20:27:57 +00:00
|
|
|
/**
|
|
|
|
* Returns the vendor directory of this Pico instance
|
|
|
|
*
|
|
|
|
* @return string vendor directory path
|
|
|
|
*/
|
|
|
|
public function getVendorDir()
|
|
|
|
{
|
|
|
|
return $this->vendorDir;
|
|
|
|
}
|
|
|
|
|
2015-10-01 13:05:50 +00:00
|
|
|
/**
|
|
|
|
* Returns the config directory of this Pico instance
|
|
|
|
*
|
|
|
|
* @return string config directory path
|
|
|
|
*/
|
|
|
|
public function getConfigDir()
|
|
|
|
{
|
|
|
|
return $this->configDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the plugins directory of this Pico instance
|
|
|
|
*
|
|
|
|
* @return string plugins directory path
|
|
|
|
*/
|
|
|
|
public function getPluginsDir()
|
|
|
|
{
|
|
|
|
return $this->pluginsDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the themes directory of this Pico instance
|
|
|
|
*
|
|
|
|
* @return string themes directory path
|
|
|
|
*/
|
|
|
|
public function getThemesDir()
|
|
|
|
{
|
|
|
|
return $this->themesDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs this Pico instance
|
|
|
|
*
|
|
|
|
* Loads plugins, evaluates the config file, does URL routing, parses
|
|
|
|
* meta headers, processes Markdown, does Twig processing and returns
|
|
|
|
* the rendered contents.
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @return string rendered Pico contents
|
|
|
|
*
|
|
|
|
* @throws Exception thrown when a irrecoverable error occurs
|
2015-06-10 09:40:26 +00:00
|
|
|
*/
|
2015-10-01 13:05:50 +00:00
|
|
|
public function run()
|
2015-06-10 09:40:26 +00:00
|
|
|
{
|
2016-12-06 18:03:58 +00:00
|
|
|
// check lock
|
|
|
|
if ($this->locked) {
|
|
|
|
throw new LogicException('You cannot run the same Pico instance multiple times');
|
|
|
|
}
|
|
|
|
|
2015-10-06 18:38:34 +00:00
|
|
|
// lock Pico
|
|
|
|
$this->locked = true;
|
2015-10-06 18:23:28 +00:00
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
// load plugins
|
|
|
|
$this->loadPlugins();
|
2016-12-06 16:18:59 +00:00
|
|
|
$this->sortPlugins();
|
2017-05-13 16:17:19 +00:00
|
|
|
$this->triggerEvent('onPluginsLoaded', array($this->plugins));
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
// load config
|
|
|
|
$this->loadConfig();
|
|
|
|
$this->triggerEvent('onConfigLoaded', array(&$this->config));
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2015-11-19 03:48:22 +00:00
|
|
|
// check content dir
|
|
|
|
if (!is_dir($this->getConfig('content_dir'))) {
|
|
|
|
throw new RuntimeException('Invalid content directory "' . $this->getConfig('content_dir') . '"');
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
// evaluate request url
|
|
|
|
$this->evaluateRequestUrl();
|
|
|
|
$this->triggerEvent('onRequestUrl', array(&$this->requestUrl));
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
// discover requested file
|
2016-07-20 17:23:19 +00:00
|
|
|
$this->requestFile = $this->resolveFilePath($this->requestUrl);
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->triggerEvent('onRequestFile', array(&$this->requestFile));
|
|
|
|
|
|
|
|
// load raw file content
|
2017-10-14 20:08:11 +00:00
|
|
|
$this->triggerEvent('onContentLoading');
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2016-12-12 15:37:20 +00:00
|
|
|
$hiddenFileRegex = '/(?:^|\/)(?:_|404' . preg_quote($this->getConfig('content_ext'), '/') . '$)/';
|
2019-08-29 21:06:27 +00:00
|
|
|
if (is_file($this->requestFile) && !preg_match($hiddenFileRegex, $this->requestFile)) {
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->rawContent = $this->loadFileContent($this->requestFile);
|
2015-06-10 09:40:26 +00:00
|
|
|
} else {
|
2017-10-14 20:08:11 +00:00
|
|
|
$this->triggerEvent('on404ContentLoading');
|
2015-08-28 16:22:32 +00:00
|
|
|
|
2018-09-09 23:51:35 +00:00
|
|
|
$serverProtocol = !empty($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
|
|
|
|
header($serverProtocol . ' 404 Not Found');
|
|
|
|
|
2015-10-04 13:24:38 +00:00
|
|
|
$this->rawContent = $this->load404Content($this->requestFile);
|
2016-11-26 15:48:10 +00:00
|
|
|
$this->is404Content = true;
|
2015-08-28 16:22:32 +00:00
|
|
|
|
|
|
|
$this->triggerEvent('on404ContentLoaded', array(&$this->rawContent));
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->triggerEvent('onContentLoaded', array(&$this->rawContent));
|
|
|
|
|
2017-02-05 14:55:53 +00:00
|
|
|
// parse file meta
|
2017-10-14 20:08:11 +00:00
|
|
|
$this->triggerEvent('onMetaParsing');
|
2017-05-13 23:25:10 +00:00
|
|
|
$this->meta = $this->parseFileMeta($this->rawContent, $this->getMetaHeaders());
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->triggerEvent('onMetaParsed', array(&$this->meta));
|
|
|
|
|
|
|
|
// parse file content
|
2017-10-14 20:08:11 +00:00
|
|
|
$this->triggerEvent('onContentParsing');
|
2015-08-28 16:22:32 +00:00
|
|
|
|
2017-10-14 20:08:11 +00:00
|
|
|
$markdown = $this->prepareFileContent($this->rawContent, $this->meta);
|
|
|
|
$this->triggerEvent('onContentPrepared', array(&$markdown));
|
2015-08-28 16:22:32 +00:00
|
|
|
|
2017-10-14 20:08:11 +00:00
|
|
|
$this->content = $this->parseFileContent($markdown);
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->triggerEvent('onContentParsed', array(&$this->content));
|
|
|
|
|
|
|
|
// read pages
|
|
|
|
$this->triggerEvent('onPagesLoading');
|
|
|
|
|
|
|
|
$this->readPages();
|
2017-05-13 16:08:54 +00:00
|
|
|
$this->triggerEvent('onPagesDiscovered', array(&$this->pages));
|
|
|
|
|
2015-10-04 20:39:38 +00:00
|
|
|
$this->sortPages();
|
2017-05-13 16:08:54 +00:00
|
|
|
$this->triggerEvent('onPagesLoaded', array(&$this->pages));
|
|
|
|
|
2016-12-12 13:49:57 +00:00
|
|
|
$this->discoverPageSiblings();
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->discoverCurrentPage();
|
2017-05-13 16:08:54 +00:00
|
|
|
$this->triggerEvent(
|
|
|
|
'onCurrentPageDiscovered',
|
|
|
|
array(&$this->currentPage, &$this->previousPage, &$this->nextPage)
|
|
|
|
);
|
2015-08-28 16:22:32 +00:00
|
|
|
|
Add Pico::buildPageTree()
Pico's page tree is a list of all the tree's branches (no matter the depth). Thus, by iterating a array element, you get the nodes of a given branch. All leaf nodes do represent a page, but inner nodes may or may not represent a page (e.g. if there's a `sub/page.md`, but neither a `sub/index.md` nor a `sub.md`, the inner node `sub`, that is the parent of the `sub/page` node, represents no page itself).
A page's file path describes its node's path in the tree (e.g. the page `sub/page.md` is represented by the `sub/page` node, thus a child of the `sub` node and a element of the `sub` branch). However, the index page of a folder (e.g. `sub/index.md`), is *not* a node of the `sub` branch, but rather of the `/` branch. The page's node is not `sub/index`, but `sub`. If two pages are described by the same node (e.g. if both a `sub/index.md` and a `sub.md` exist), the index page takes precedence. Pico's main index page (i.e. `index.md`) is represented by the tree's root node `/` and a special case: it is the only node of the `` (i.e. the empty string) branch.
A node is represented by an array with the keys `id`, `page` and `children`. The `id` key contains a string with the node's name. If the node represents a page, the `page` key is a reference to the page's data array. If the node is a inner node, the `children` key is a reference to its matching branch (i.e. a list of the node's children). The order of a node's children matches the order in Pico's pages array.
If you want to walk the whole page tree, start with the tree's root node at `$pageTree[""]["/"]`. The root node's `children` key is a reference to the `/` branch at `$pageTree["/"]`, that is a list of the root node's direct child nodes and their siblings.
You MUST NOT iterate the page tree itself (i.e. the list of the tree's branches), its order is undefined and the array will be replaced by a non-iterable data structure with Pico 3.0.
2017-11-18 17:34:57 +00:00
|
|
|
$this->buildPageTree();
|
|
|
|
$this->triggerEvent('onPageTreeBuilt', array(&$this->pageTree));
|
|
|
|
|
2017-05-13 16:08:54 +00:00
|
|
|
// render template
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->twigVariables = $this->getTwigVariables();
|
2017-02-05 19:20:26 +00:00
|
|
|
$this->twigTemplate = $this->getTwigTemplate();
|
2017-05-13 16:08:54 +00:00
|
|
|
$this->triggerEvent('onPageRendering', array(&$this->twigTemplate, &$this->twigVariables));
|
2015-08-28 16:22:32 +00:00
|
|
|
|
2017-05-13 23:26:07 +00:00
|
|
|
$output = $this->getTwig()->render($this->twigTemplate, $this->twigVariables);
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->triggerEvent('onPageRendered', array(&$output));
|
|
|
|
|
2015-10-01 13:05:50 +00:00
|
|
|
return $output;
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
|
|
|
|
2017-06-14 15:08:33 +00:00
|
|
|
/**
|
|
|
|
* Loads plugins from vendor/pico-plugin.php and Pico::$pluginsDir
|
|
|
|
*
|
2017-10-07 22:35:36 +00:00
|
|
|
* See {@see Pico::loadComposerPlugins()} for details about plugins loaded
|
|
|
|
* from `vendor/pico-plugin.php` (i.e. plugins that were installed using
|
2017-12-27 20:32:52 +00:00
|
|
|
* composer), and {@see Pico::loadLocalPlugins()} for details about plugins
|
|
|
|
* installed to {@see Pico::$pluginsDir}. Pico loads plugins from the
|
|
|
|
* filesystem only if {@see Pico::$enableLocalPlugins} is set to TRUE
|
2017-10-13 19:02:51 +00:00
|
|
|
* (this is the default).
|
2017-10-07 22:35:36 +00:00
|
|
|
*
|
|
|
|
* Pico always loads plugins from `vendor/pico-plugin.php` first and
|
|
|
|
* ignores conflicting plugins in {@see Pico::$pluginsDir}.
|
2017-06-14 15:08:33 +00:00
|
|
|
*
|
2017-12-27 20:32:52 +00:00
|
|
|
* The official PicoDeprecated plugin must be loaded when plugins that use
|
|
|
|
* an older API version than Pico's API version ({@see Pico::API_VERSION})
|
|
|
|
* are loaded.
|
|
|
|
*
|
2017-06-14 15:08:33 +00:00
|
|
|
* Please note that Pico will change the processing order when needed to
|
|
|
|
* incorporate plugin dependencies. See {@see Pico::sortPlugins()} for
|
|
|
|
* details.
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::loadPlugin()
|
|
|
|
* @see Pico::getPlugin()
|
|
|
|
* @see Pico::getPlugins()
|
|
|
|
*
|
2017-06-14 15:08:33 +00:00
|
|
|
* @return void
|
2017-10-19 19:55:41 +00:00
|
|
|
*
|
2017-06-14 15:08:33 +00:00
|
|
|
* @throws RuntimeException thrown when a plugin couldn't be loaded
|
|
|
|
*/
|
|
|
|
protected function loadPlugins()
|
|
|
|
{
|
2017-10-13 19:02:51 +00:00
|
|
|
$composerPlugins = $this->loadComposerPlugins();
|
|
|
|
|
|
|
|
if ($this->enableLocalPlugins) {
|
|
|
|
$this->loadLocalPlugins($composerPlugins);
|
|
|
|
}
|
2017-12-27 20:32:52 +00:00
|
|
|
|
|
|
|
if (!isset($this->plugins['PicoDeprecated']) && (count($this->plugins) !== count($this->nativePlugins))) {
|
|
|
|
throw new RuntimeException(
|
|
|
|
"Plugins using an older API than version " . static::API_VERSION . " found, "
|
|
|
|
. "but PicoDeprecated isn't loaded"
|
|
|
|
);
|
|
|
|
}
|
2017-10-07 22:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads plugins from vendor/pico-plugin.php
|
|
|
|
*
|
2017-12-27 20:32:52 +00:00
|
|
|
* This method loads all plugins installed using composer and Pico's
|
|
|
|
* `picocms/composer-installer` installer by reading the `pico-plugin.php`
|
|
|
|
* in composer's `vendor` dir.
|
2017-10-07 22:35:36 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::loadPlugins()
|
|
|
|
* @see Pico::loadLocalPlugins()
|
|
|
|
*
|
|
|
|
* @param string[] $pluginBlacklist class names of plugins not to load
|
|
|
|
*
|
|
|
|
* @return string[] installer names of the loaded plugins
|
2019-04-30 13:30:00 +00:00
|
|
|
*
|
|
|
|
* @throws RuntimeException thrown when a plugin couldn't be loaded
|
2017-10-07 22:35:36 +00:00
|
|
|
*/
|
2017-10-13 19:02:51 +00:00
|
|
|
protected function loadComposerPlugins(array $pluginBlacklist = array())
|
2017-10-07 22:35:36 +00:00
|
|
|
{
|
|
|
|
$composerPlugins = array();
|
2019-08-29 21:06:27 +00:00
|
|
|
if (is_file($this->getVendorDir() . 'vendor/pico-plugin.php')) {
|
2017-10-07 22:35:36 +00:00
|
|
|
// composer root package
|
|
|
|
$composerPlugins = require($this->getVendorDir() . 'vendor/pico-plugin.php') ?: array();
|
2019-08-29 21:06:27 +00:00
|
|
|
} elseif (is_file($this->getVendorDir() . '../../../vendor/pico-plugin.php')) {
|
2017-10-07 22:35:36 +00:00
|
|
|
// composer dependency package
|
|
|
|
$composerPlugins = require($this->getVendorDir() . '../../../vendor/pico-plugin.php') ?: array();
|
|
|
|
}
|
|
|
|
|
2017-10-13 19:02:51 +00:00
|
|
|
$pluginBlacklist = array_fill_keys($pluginBlacklist, true);
|
|
|
|
|
|
|
|
$loadedPlugins = array();
|
|
|
|
foreach ($composerPlugins as $package => $pluginData) {
|
|
|
|
$loadedPlugins[] = $pluginData['installerName'];
|
|
|
|
|
|
|
|
foreach ($pluginData['classNames'] as $className) {
|
2017-10-07 22:35:36 +00:00
|
|
|
$plugin = new $className($this);
|
|
|
|
$className = get_class($plugin);
|
|
|
|
|
2017-10-13 19:02:51 +00:00
|
|
|
if (isset($this->plugins[$className]) || isset($pluginBlacklist[$className])) {
|
2017-10-07 22:35:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!($plugin instanceof PicoPluginInterface)) {
|
|
|
|
throw new RuntimeException(
|
|
|
|
"Unable to load plugin '" . $className . "' via 'vendor/pico-plugin.php': "
|
|
|
|
. "Plugins installed by composer must implement 'PicoPluginInterface'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->plugins[$className] = $plugin;
|
|
|
|
|
|
|
|
if (defined($className . '::API_VERSION') && ($className::API_VERSION >= static::API_VERSION)) {
|
|
|
|
$this->nativePlugins[$className] = $plugin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-13 19:02:51 +00:00
|
|
|
|
|
|
|
return $loadedPlugins;
|
2017-06-14 15:08:33 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
2015-10-01 20:52:10 +00:00
|
|
|
* Loads plugins from Pico::$pluginsDir in alphabetical order
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
2016-12-12 19:37:17 +00:00
|
|
|
* Pico tries to load plugins from `<plugin name>/<plugin name>.php` and
|
|
|
|
* `<plugin name>.php` only. Plugin names are treated case insensitive.
|
|
|
|
* Pico will throw a RuntimeException if it can't load a plugin.
|
|
|
|
*
|
2017-10-07 22:35:36 +00:00
|
|
|
* Plugin files MAY be prefixed by a number (e.g. `00-PicoDeprecated.php`)
|
2015-11-12 14:33:48 +00:00
|
|
|
* to indicate their processing order. Plugins without a prefix will be
|
2016-12-12 19:37:17 +00:00
|
|
|
* loaded last. If you want to use a prefix, you MUST NOT use the reserved
|
2016-12-12 19:58:38 +00:00
|
|
|
* prefixes `00` to `09`. Prefixes are completely optional, however, you
|
|
|
|
* SHOULD take the following prefix classification into consideration:
|
|
|
|
* - 10 to 19: Reserved
|
|
|
|
* - 20 to 39: Low level code helper plugins
|
|
|
|
* - 40 to 59: Plugins manipulating routing or the pages array
|
|
|
|
* - 60 to 79: Plugins hooking into template or markdown parsing
|
|
|
|
* - 80 to 99: Plugins using the `onPageRendered` event
|
2016-12-12 19:37:17 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::loadPlugins()
|
|
|
|
* @see Pico::loadComposerPlugins()
|
|
|
|
*
|
|
|
|
* @param string[] $pluginBlacklist class names of plugins not to load
|
|
|
|
*
|
2015-08-28 16:22:32 +00:00
|
|
|
* @return void
|
2017-10-19 19:55:41 +00:00
|
|
|
*
|
2015-08-28 16:22:32 +00:00
|
|
|
* @throws RuntimeException thrown when a plugin couldn't be loaded
|
|
|
|
*/
|
2017-10-13 19:02:51 +00:00
|
|
|
protected function loadLocalPlugins(array $pluginBlacklist = array())
|
2015-08-28 16:22:32 +00:00
|
|
|
{
|
2017-10-12 10:11:55 +00:00
|
|
|
// scope isolated require()
|
|
|
|
$includeClosure = function ($pluginFile) {
|
|
|
|
require($pluginFile);
|
|
|
|
};
|
|
|
|
if (PHP_VERSION_ID >= 50400) {
|
|
|
|
$includeClosure = $includeClosure->bindTo(null);
|
|
|
|
}
|
2017-10-07 22:35:36 +00:00
|
|
|
|
2017-10-13 19:02:51 +00:00
|
|
|
$pluginBlacklist = array_fill_keys($pluginBlacklist, true);
|
|
|
|
|
2017-10-07 22:35:36 +00:00
|
|
|
$files = scandir($this->getPluginsDir()) ?: array();
|
|
|
|
foreach ($files as $file) {
|
|
|
|
if ($file[0] === '.') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-12 10:11:55 +00:00
|
|
|
$className = $pluginFile = null;
|
2017-10-07 22:35:36 +00:00
|
|
|
if (is_dir($this->getPluginsDir() . $file)) {
|
|
|
|
$className = preg_replace('/^[0-9]+-/', '', $file);
|
2017-10-12 10:11:55 +00:00
|
|
|
$pluginFile = $file . '/' . $className . '.php';
|
2017-10-07 22:35:36 +00:00
|
|
|
|
2019-08-29 21:06:27 +00:00
|
|
|
if (!is_file($this->getPluginsDir() . $pluginFile)) {
|
2017-10-07 22:35:36 +00:00
|
|
|
throw new RuntimeException(
|
2017-10-12 10:11:55 +00:00
|
|
|
"Unable to load plugin '" . $className . "' from '" . $pluginFile . "': File not found"
|
2017-10-07 22:35:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} elseif (substr($file, -4) === '.php') {
|
|
|
|
$className = preg_replace('/^[0-9]+-/', '', substr($file, 0, -4));
|
2017-10-12 10:11:55 +00:00
|
|
|
$pluginFile = $file;
|
2017-10-07 22:35:36 +00:00
|
|
|
} else {
|
|
|
|
throw new RuntimeException("Unable to load plugin from '" . $file . "': Not a valid plugin file");
|
2016-12-12 19:37:17 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 19:02:51 +00:00
|
|
|
if (isset($this->plugins[$className]) || isset($pluginBlacklist[$className])) {
|
2017-10-12 10:11:55 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-03-06 19:06:24 +00:00
|
|
|
|
2016-12-12 19:37:17 +00:00
|
|
|
$includeClosure($this->getPluginsDir() . $pluginFile);
|
2015-08-28 16:22:32 +00:00
|
|
|
|
2016-12-12 19:37:17 +00:00
|
|
|
if (class_exists($className, false)) {
|
2015-09-06 12:35:23 +00:00
|
|
|
// class name and file name can differ regarding case sensitivity
|
|
|
|
$plugin = new $className($this);
|
|
|
|
$className = get_class($plugin);
|
|
|
|
|
|
|
|
$this->plugins[$className] = $plugin;
|
2017-05-10 15:00:48 +00:00
|
|
|
|
|
|
|
if ($plugin instanceof PicoPluginInterface) {
|
|
|
|
if (defined($className . '::API_VERSION') && ($className::API_VERSION >= static::API_VERSION)) {
|
|
|
|
$this->nativePlugins[$className] = $plugin;
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 16:22:32 +00:00
|
|
|
} else {
|
2017-10-12 10:11:55 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
"Unable to load plugin '" . $className . "' from '" . $pluginFile . "': Plugin class not found"
|
|
|
|
);
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 18:50:35 +00:00
|
|
|
/**
|
|
|
|
* Manually loads a plugin
|
|
|
|
*
|
2017-05-13 16:17:19 +00:00
|
|
|
* Manually loaded plugins MUST implement {@see PicoPluginInterface}. They
|
|
|
|
* are simply appended to the plugins array without any additional checks,
|
|
|
|
* so you might get unexpected results, depending on *when* you're loading
|
|
|
|
* a plugin. You SHOULD NOT load plugins after a event has been triggered
|
|
|
|
* by Pico. In-depth knowledge of Pico's inner workings is strongly advised
|
|
|
|
* otherwise, and you MUST NOT rely on {@see PicoDeprecated} to maintain
|
|
|
|
* backward compatibility in such cases.
|
|
|
|
*
|
|
|
|
* If you e.g. load a plugin after the `onPluginsLoaded` event, Pico
|
|
|
|
* doesn't guarantee the plugin's order ({@see Pico::sortPlugins()}).
|
|
|
|
* Already triggered events won't get triggered on the manually loaded
|
|
|
|
* plugin. Thus you SHOULD load plugins either before {@see Pico::run()}
|
|
|
|
* is called, or via the constructor of another plugin (i.e. the plugin's
|
|
|
|
* `__construct()` method; plugins are instanced in
|
|
|
|
* {@see Pico::loadPlugins()}).
|
|
|
|
*
|
|
|
|
* This method triggers the `onPluginManuallyLoaded` event.
|
2016-02-29 18:50:35 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::loadPlugins()
|
|
|
|
* @see Pico::getPlugin()
|
|
|
|
* @see Pico::getPlugins()
|
|
|
|
*
|
|
|
|
* @param PicoPluginInterface|string $plugin either the class name of a
|
2016-02-29 18:50:35 +00:00
|
|
|
* plugin to instantiate or a plugin instance
|
2017-10-19 19:55:41 +00:00
|
|
|
*
|
|
|
|
* @return PicoPluginInterface instance of the loaded plugin
|
|
|
|
*
|
2019-04-30 13:30:00 +00:00
|
|
|
* @throws RuntimeException thrown when the plugin couldn't be loaded
|
2016-02-29 18:50:35 +00:00
|
|
|
*/
|
|
|
|
public function loadPlugin($plugin)
|
|
|
|
{
|
|
|
|
if (!is_object($plugin)) {
|
|
|
|
$className = (string) $plugin;
|
|
|
|
if (class_exists($className)) {
|
|
|
|
$plugin = new $className($this);
|
|
|
|
} else {
|
2016-03-02 21:10:49 +00:00
|
|
|
throw new RuntimeException("Unable to load plugin '" . $className . "': Class not found");
|
2016-02-29 18:50:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$className = get_class($plugin);
|
2017-10-07 22:36:29 +00:00
|
|
|
|
2016-03-11 18:07:45 +00:00
|
|
|
if (!($plugin instanceof PicoPluginInterface)) {
|
2016-02-29 18:50:35 +00:00
|
|
|
throw new RuntimeException(
|
2016-03-02 21:10:49 +00:00
|
|
|
"Unable to load plugin '" . $className . "': "
|
|
|
|
. "Manually loaded plugins must implement 'PicoPluginInterface'"
|
2016-02-29 18:50:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->plugins[$className] = $plugin;
|
|
|
|
|
2017-05-10 15:00:48 +00:00
|
|
|
if (defined($className . '::API_VERSION') && ($className::API_VERSION >= static::API_VERSION)) {
|
|
|
|
$this->nativePlugins[$className] = $plugin;
|
|
|
|
}
|
|
|
|
|
2017-05-13 16:17:19 +00:00
|
|
|
// trigger onPluginManuallyLoaded event
|
2017-12-27 20:36:56 +00:00
|
|
|
// the event is also triggered on the newly loaded plugin, allowing you to distinguish manual and auto loading
|
2017-10-07 22:36:29 +00:00
|
|
|
$this->triggerEvent('onPluginManuallyLoaded', array($plugin));
|
2017-05-13 16:17:19 +00:00
|
|
|
|
2016-02-29 18:50:35 +00:00
|
|
|
return $plugin;
|
|
|
|
}
|
|
|
|
|
2016-12-06 16:18:59 +00:00
|
|
|
/**
|
|
|
|
* Sorts all loaded plugins using a plugin dependency topology
|
|
|
|
*
|
|
|
|
* Execution order matters: if plugin A depends on plugin B, it usually
|
|
|
|
* means that plugin B does stuff which plugin A requires. However, Pico
|
|
|
|
* loads plugins in alphabetical order, so events might get fired on
|
|
|
|
* plugin A before plugin B.
|
|
|
|
*
|
|
|
|
* Hence plugins need to be sorted. Pico sorts plugins using a dependency
|
|
|
|
* topology, this means that it moves all plugins, on which a plugin
|
|
|
|
* depends, in front of that plugin. The order isn't touched apart from
|
|
|
|
* that, so they are still sorted alphabetically, as long as this doesn't
|
|
|
|
* interfere with the dependency topology. Circular dependencies are being
|
|
|
|
* ignored; their behavior is undefiend. Missing dependencies are being
|
|
|
|
* ignored until you try to enable the dependant plugin.
|
|
|
|
*
|
|
|
|
* This method bases on Marc J. Schmidt's Topological Sort library in
|
|
|
|
* version 1.1.0, licensed under the MIT license. It uses the `ArraySort`
|
|
|
|
* implementation (class `\MJS\TopSort\Implementations\ArraySort`).
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::loadPlugins()
|
|
|
|
* @see Pico::getPlugins()
|
|
|
|
* @see https://github.com/marcj/topsort.php
|
2016-12-06 16:18:59 +00:00
|
|
|
* Marc J. Schmidt's Topological Sort / Dependency resolver in PHP
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see https://github.com/marcj/topsort.php/blob/1.1.0/src/Implementations/ArraySort.php
|
2016-12-06 16:18:59 +00:00
|
|
|
* \MJS\TopSort\Implementations\ArraySort class
|
2017-10-19 19:55:41 +00:00
|
|
|
*
|
2016-12-06 16:18:59 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function sortPlugins()
|
|
|
|
{
|
|
|
|
$plugins = $this->plugins;
|
2017-10-12 10:12:37 +00:00
|
|
|
$nativePlugins = $this->nativePlugins;
|
2016-12-06 16:18:59 +00:00
|
|
|
$sortedPlugins = array();
|
2018-03-29 20:45:12 +00:00
|
|
|
$sortedNativePlugins = array();
|
2016-12-06 16:18:59 +00:00
|
|
|
$visitedPlugins = array();
|
2017-10-12 10:12:37 +00:00
|
|
|
|
|
|
|
$visitPlugin = function ($plugin) use (
|
|
|
|
$plugins,
|
|
|
|
$nativePlugins,
|
|
|
|
&$sortedPlugins,
|
2018-03-29 20:45:12 +00:00
|
|
|
&$sortedNativePlugins,
|
2017-10-12 10:12:37 +00:00
|
|
|
&$visitedPlugins,
|
|
|
|
&$visitPlugin
|
|
|
|
) {
|
2016-12-06 16:18:59 +00:00
|
|
|
$pluginName = get_class($plugin);
|
|
|
|
|
|
|
|
// skip already visited plugins and ignore circular dependencies
|
|
|
|
if (!isset($visitedPlugins[$pluginName])) {
|
|
|
|
$visitedPlugins[$pluginName] = true;
|
|
|
|
|
|
|
|
$dependencies = array();
|
|
|
|
if ($plugin instanceof PicoPluginInterface) {
|
|
|
|
$dependencies = $plugin->getDependencies();
|
2017-05-10 15:00:48 +00:00
|
|
|
}
|
2017-10-12 10:12:37 +00:00
|
|
|
if (!isset($nativePlugins[$pluginName])) {
|
2017-05-10 15:00:48 +00:00
|
|
|
$dependencies[] = 'PicoDeprecated';
|
2016-12-06 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($dependencies as $dependency) {
|
|
|
|
// ignore missing dependencies
|
|
|
|
// this is only a problem when the user tries to enable this plugin
|
|
|
|
if (isset($plugins[$dependency])) {
|
|
|
|
$visitPlugin($plugins[$dependency]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$sortedPlugins[$pluginName] = $plugin;
|
2018-03-29 20:45:12 +00:00
|
|
|
if (isset($nativePlugins[$pluginName])) {
|
|
|
|
$sortedNativePlugins[$pluginName] = $plugin;
|
|
|
|
}
|
2016-12-06 16:18:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-13 23:11:16 +00:00
|
|
|
if (isset($this->plugins['PicoDeprecated'])) {
|
|
|
|
$visitPlugin($this->plugins['PicoDeprecated']);
|
|
|
|
}
|
|
|
|
|
2016-12-06 16:18:59 +00:00
|
|
|
foreach ($this->plugins as $plugin) {
|
|
|
|
$visitPlugin($plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->plugins = $sortedPlugins;
|
2018-03-29 20:45:12 +00:00
|
|
|
$this->nativePlugins = $sortedNativePlugins;
|
2016-12-06 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* Returns the instance of a named plugin
|
|
|
|
*
|
2017-05-13 16:17:58 +00:00
|
|
|
* Plugins SHOULD implement {@see PicoPluginInterface}, but you MUST NOT
|
|
|
|
* rely on it. For more information see {@see PicoPluginInterface}.
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::loadPlugins()
|
|
|
|
* @see Pico::getPlugins()
|
|
|
|
*
|
|
|
|
* @param string $pluginName name of the plugin
|
|
|
|
*
|
|
|
|
* @return object instance of the plugin
|
|
|
|
*
|
|
|
|
* @throws RuntimeException thrown when the plugin wasn't found
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
public function getPlugin($pluginName)
|
|
|
|
{
|
|
|
|
if (isset($this->plugins[$pluginName])) {
|
|
|
|
return $this->plugins[$pluginName];
|
|
|
|
}
|
|
|
|
|
2015-10-04 19:34:37 +00:00
|
|
|
throw new RuntimeException("Missing plugin '" . $pluginName . "'");
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all loaded plugins
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::loadPlugins()
|
|
|
|
* @see Pico::getPlugin()
|
|
|
|
*
|
2015-10-27 00:48:58 +00:00
|
|
|
* @return object[]|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
public function getPlugins()
|
|
|
|
{
|
|
|
|
return $this->plugins;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-05 23:01:25 +00:00
|
|
|
* Loads the config.yml and any other *.yml from Pico::$configDir
|
2016-03-06 19:54:58 +00:00
|
|
|
*
|
2017-02-05 23:01:25 +00:00
|
|
|
* After loading {@path "config/config.yml"}, Pico proceeds with any other
|
|
|
|
* existing `config/*.yml` file in alphabetical order. The file order is
|
|
|
|
* crucial: Config values which have been set already, cannot be
|
|
|
|
* overwritten by a succeeding file. This is also true for arrays, i.e.
|
|
|
|
* when specifying `test: { foo: bar }` in `config/a.yml` and
|
|
|
|
* `test: { baz: 42 }` in `config/b.yml`, `{{ config.test.baz }}` will be
|
|
|
|
* undefined!
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::setConfig()
|
|
|
|
* @see Pico::getConfig()
|
|
|
|
*
|
2015-08-28 16:22:32 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function loadConfig()
|
|
|
|
{
|
2017-02-05 15:52:18 +00:00
|
|
|
// load config closure
|
|
|
|
$yamlParser = $this->getYamlParser();
|
|
|
|
$loadConfigClosure = function ($configFile) use ($yamlParser) {
|
|
|
|
$yaml = file_get_contents($configFile);
|
|
|
|
$config = $yamlParser->parse($yaml);
|
|
|
|
return is_array($config) ? $config : array();
|
2016-03-06 19:54:58 +00:00
|
|
|
};
|
|
|
|
|
2017-02-05 15:52:18 +00:00
|
|
|
// load main config file (config/config.yml)
|
2016-03-06 19:54:58 +00:00
|
|
|
$this->config = is_array($this->config) ? $this->config : array();
|
2019-08-29 21:06:27 +00:00
|
|
|
if (is_file($this->getConfigDir() . 'config.yml')) {
|
2017-02-05 15:52:18 +00:00
|
|
|
$this->config += $loadConfigClosure($this->getConfigDir() . 'config.yml');
|
2016-03-06 19:54:58 +00:00
|
|
|
}
|
2016-03-06 19:06:24 +00:00
|
|
|
|
2017-02-05 15:52:18 +00:00
|
|
|
// merge $config of config/*.yml files
|
|
|
|
$configFiles = $this->getFilesGlob($this->getConfigDir() . '*.yml');
|
2016-12-12 19:36:46 +00:00
|
|
|
foreach ($configFiles as $configFile) {
|
2018-04-29 15:21:27 +00:00
|
|
|
if ($configFile !== $this->getConfigDir() . 'config.yml') {
|
2017-02-05 15:52:18 +00:00
|
|
|
$this->config += $loadConfigClosure($configFile);
|
|
|
|
}
|
2015-11-13 18:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 19:54:58 +00:00
|
|
|
// merge default config
|
|
|
|
$this->config += array(
|
2015-08-28 16:22:32 +00:00
|
|
|
'site_title' => 'Pico',
|
2019-04-30 13:30:00 +00:00
|
|
|
'base_url' => null,
|
2015-08-28 16:22:32 +00:00
|
|
|
'rewrite_url' => null,
|
2019-03-28 19:12:46 +00:00
|
|
|
'debug' => null,
|
2017-02-05 20:49:54 +00:00
|
|
|
'timezone' => null,
|
2015-08-28 16:22:32 +00:00
|
|
|
'theme' => 'default',
|
2019-04-30 13:34:11 +00:00
|
|
|
'themes_url' => null,
|
2017-02-05 20:49:54 +00:00
|
|
|
'twig_config' => null,
|
2015-08-28 16:22:32 +00:00
|
|
|
'date_format' => '%D %T',
|
2019-04-30 13:30:00 +00:00
|
|
|
'pages_order_by_meta' => 'author',
|
2015-08-28 16:22:32 +00:00
|
|
|
'pages_order_by' => 'alpha',
|
|
|
|
'pages_order' => 'asc',
|
2015-11-02 18:48:58 +00:00
|
|
|
'content_dir' => null,
|
2015-08-28 16:22:32 +00:00
|
|
|
'content_ext' => '.md',
|
2019-04-30 13:34:11 +00:00
|
|
|
'content_config' => null,
|
|
|
|
'assets_dir' => 'assets/',
|
2019-07-14 14:16:57 +00:00
|
|
|
'assets_url' => null,
|
|
|
|
'plugins_url' => null
|
2015-06-10 09:40:26 +00:00
|
|
|
);
|
|
|
|
|
2016-12-06 18:03:58 +00:00
|
|
|
if (!$this->config['base_url']) {
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->config['base_url'] = $this->getBaseUrl();
|
2015-11-04 18:43:54 +00:00
|
|
|
} else {
|
|
|
|
$this->config['base_url'] = rtrim($this->config['base_url'], '/') . '/';
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
2015-11-04 18:43:54 +00:00
|
|
|
|
2015-11-27 19:13:17 +00:00
|
|
|
if ($this->config['rewrite_url'] === null) {
|
|
|
|
$this->config['rewrite_url'] = $this->isUrlRewritingEnabled();
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:12:46 +00:00
|
|
|
if ($this->config['debug'] === null) {
|
|
|
|
$this->config['debug'] = $this->isDebugModeEnabled();
|
|
|
|
}
|
|
|
|
|
2017-02-05 20:49:54 +00:00
|
|
|
if (!$this->config['timezone']) {
|
2017-12-27 20:36:56 +00:00
|
|
|
// explicitly set a default timezone to prevent a E_NOTICE when no timezone is set;
|
|
|
|
// the `date_default_timezone_get()` function always returns a timezone, at least UTC
|
2017-02-05 20:49:54 +00:00
|
|
|
$this->config['timezone'] = @date_default_timezone_get();
|
|
|
|
}
|
|
|
|
date_default_timezone_set($this->config['timezone']);
|
|
|
|
|
2019-04-30 13:34:11 +00:00
|
|
|
if (!$this->config['plugins_url']) {
|
|
|
|
$this->config['plugins_url'] = $this->getUrlFromPath($this->getPluginsDir());
|
2017-02-05 20:49:54 +00:00
|
|
|
} else {
|
2019-04-30 13:34:11 +00:00
|
|
|
$this->config['plugins_url'] = $this->getAbsoluteUrl($this->config['plugins_url']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->config['themes_url']) {
|
|
|
|
$this->config['themes_url'] = $this->getUrlFromPath($this->getThemesDir());
|
|
|
|
} else {
|
|
|
|
$this->config['themes_url'] = $this->getAbsoluteUrl($this->config['themes_url']);
|
2017-02-05 20:49:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:11:46 +00:00
|
|
|
$defaultTwigConfig = array(
|
|
|
|
'autoescape' => false,
|
|
|
|
'strict_variables' => false,
|
|
|
|
'debug' => null,
|
|
|
|
'cache' => false,
|
|
|
|
'auto_reload' => null
|
|
|
|
);
|
|
|
|
|
2017-02-05 20:49:54 +00:00
|
|
|
if (!is_array($this->config['twig_config'])) {
|
|
|
|
$this->config['twig_config'] = $defaultTwigConfig;
|
|
|
|
} else {
|
|
|
|
$this->config['twig_config'] += $defaultTwigConfig;
|
2019-03-28 18:14:23 +00:00
|
|
|
|
|
|
|
if ($this->config['twig_config']['cache']) {
|
|
|
|
$this->config['twig_config']['cache'] = $this->getAbsolutePath($this->config['twig_config']['cache']);
|
|
|
|
}
|
2019-03-28 19:12:46 +00:00
|
|
|
if ($this->config['twig_config']['debug'] === null) {
|
|
|
|
$this->config['twig_config']['debug'] = $this->isDebugModeEnabled();
|
|
|
|
}
|
2017-02-05 20:49:54 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 18:03:58 +00:00
|
|
|
if (!$this->config['content_dir']) {
|
2015-11-02 18:48:58 +00:00
|
|
|
// try to guess the content directory
|
2017-05-01 22:22:35 +00:00
|
|
|
if (is_file($this->getRootDir() . 'content/index' . $this->config['content_ext'])) {
|
2015-11-02 18:48:58 +00:00
|
|
|
$this->config['content_dir'] = $this->getRootDir() . 'content/';
|
2017-05-01 22:22:35 +00:00
|
|
|
} elseif (is_file($this->getRootDir() . 'content-sample/index' . $this->config['content_ext'])) {
|
2015-11-02 18:48:58 +00:00
|
|
|
$this->config['content_dir'] = $this->getRootDir() . 'content-sample/';
|
2017-05-01 20:27:57 +00:00
|
|
|
} else {
|
|
|
|
$this->config['content_dir'] = $this->getVendorDir() . 'content-sample/';
|
2015-11-02 18:48:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-10-01 13:14:45 +00:00
|
|
|
$this->config['content_dir'] = $this->getAbsolutePath($this->config['content_dir']);
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
2015-11-04 18:43:54 +00:00
|
|
|
|
2017-02-05 20:49:54 +00:00
|
|
|
$defaultContentConfig = array('extra' => true, 'breaks' => false, 'escape' => false, 'auto_urls' => true);
|
|
|
|
if (!is_array($this->config['content_config'])) {
|
|
|
|
$this->config['content_config'] = $defaultContentConfig;
|
2016-04-23 23:22:43 +00:00
|
|
|
} else {
|
2017-02-05 20:49:54 +00:00
|
|
|
$this->config['content_config'] += $defaultContentConfig;
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
2019-04-30 13:34:11 +00:00
|
|
|
|
|
|
|
if (!$this->config['assets_dir']) {
|
|
|
|
$this->config['assets_dir'] = $this->getRootDir() . 'assets/';
|
|
|
|
} else {
|
|
|
|
$this->config['assets_dir'] = $this->getAbsolutePath($this->config['assets_dir']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->config['assets_url']) {
|
|
|
|
$this->config['assets_url'] = $this->getUrlFromPath($this->config['assets_dir']);
|
|
|
|
} else {
|
|
|
|
$this->config['assets_url'] = $this->getAbsoluteUrl($this->config['assets_url']);
|
|
|
|
}
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 18:23:28 +00:00
|
|
|
/**
|
2015-11-04 18:50:44 +00:00
|
|
|
* Sets Pico's config before calling Pico::run()
|
2015-10-06 18:23:28 +00:00
|
|
|
*
|
2015-11-04 18:50:44 +00:00
|
|
|
* This method allows you to modify Pico's config without creating a
|
2017-02-05 23:01:25 +00:00
|
|
|
* {@path "config/config.yml"} or changing some of its variables before
|
2015-10-29 01:55:30 +00:00
|
|
|
* Pico starts processing.
|
2015-10-06 18:23:28 +00:00
|
|
|
*
|
2017-05-13 16:17:58 +00:00
|
|
|
* You can call this method between {@see Pico::__construct()} and
|
|
|
|
* {@see Pico::run()} only. Options set with this method cannot be
|
2017-02-05 23:01:25 +00:00
|
|
|
* overwritten by {@path "config/config.yml"}.
|
2015-10-29 01:55:30 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::loadConfig()
|
|
|
|
* @see Pico::getConfig()
|
|
|
|
*
|
|
|
|
* @param array $config array with config variables
|
|
|
|
*
|
2015-10-06 18:23:28 +00:00
|
|
|
* @return void
|
2017-10-19 19:55:41 +00:00
|
|
|
*
|
2015-12-13 21:19:02 +00:00
|
|
|
* @throws LogicException thrown if Pico already started processing
|
2015-10-06 18:23:28 +00:00
|
|
|
*/
|
|
|
|
public function setConfig(array $config)
|
|
|
|
{
|
2015-10-06 18:38:34 +00:00
|
|
|
if ($this->locked) {
|
2015-12-13 21:19:02 +00:00
|
|
|
throw new LogicException("You cannot modify Pico's config after processing has started");
|
2015-10-06 18:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
2015-06-10 09:40:26 +00:00
|
|
|
/**
|
2015-08-28 16:22:32 +00:00
|
|
|
* Returns either the value of the specified config variable or
|
|
|
|
* the config array
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::setConfig()
|
|
|
|
* @see Pico::loadConfig()
|
|
|
|
*
|
|
|
|
* @param string $configName optional name of a config variable
|
|
|
|
* @param mixed $default optional default value to return when the
|
2017-08-04 22:19:03 +00:00
|
|
|
* named config variable doesn't exist
|
2017-10-19 19:55:41 +00:00
|
|
|
*
|
|
|
|
* @return mixed if no name of a config variable has been supplied, the
|
|
|
|
* config array is returned; otherwise it returns either the value of
|
|
|
|
* the named config variable, or, if the named config variable doesn't
|
|
|
|
* exist, the provided default value or NULL
|
2017-08-04 22:19:03 +00:00
|
|
|
*/
|
|
|
|
public function getConfig($configName = null, $default = null)
|
2015-06-10 09:40:26 +00:00
|
|
|
{
|
2015-08-28 16:22:32 +00:00
|
|
|
if ($configName !== null) {
|
2017-08-04 22:19:03 +00:00
|
|
|
return isset($this->config[$configName]) ? $this->config[$configName] : $default;
|
2015-08-28 16:22:32 +00:00
|
|
|
} else {
|
|
|
|
return $this->config;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates the requested URL
|
|
|
|
*
|
2016-04-24 18:11:05 +00:00
|
|
|
* Pico uses the `QUERY_STRING` routing method (e.g. `/pico/?sub/page`)
|
2015-10-29 01:55:30 +00:00
|
|
|
* to support SEO-like URLs out-of-the-box with any webserver. You can
|
2016-04-24 18:11:05 +00:00
|
|
|
* still setup URL rewriting to basically remove the `?` from URLs.
|
|
|
|
* However, URL rewriting requires some special configuration of your
|
|
|
|
* webserver, but this should be "basic work" for any webmaster...
|
|
|
|
*
|
|
|
|
* With Pico 1.0 you had to setup URL rewriting (e.g. using `mod_rewrite`
|
|
|
|
* on Apache) in a way that rewritten URLs follow the `QUERY_STRING`
|
2016-12-12 14:31:06 +00:00
|
|
|
* principles. Starting with version 2.0, Pico additionally supports the
|
2016-04-24 18:11:05 +00:00
|
|
|
* `REQUEST_URI` routing method, what allows you to simply rewrite all
|
|
|
|
* requests to just `index.php`. Pico then reads the requested page from
|
|
|
|
* the `REQUEST_URI` environment variable provided by the webserver.
|
|
|
|
* Please note that `QUERY_STRING` takes precedence over `REQUEST_URI`.
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* Pico 0.9 and older required Apache with `mod_rewrite` enabled, thus old
|
2015-08-28 16:22:32 +00:00
|
|
|
* plugins, templates and contents may require you to enable URL rewriting
|
2015-10-09 15:19:18 +00:00
|
|
|
* to work. If you're upgrading from Pico 0.9, you will probably have to
|
|
|
|
* update your rewriting rules.
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
|
|
|
* We recommend you to use the `link` filter in templates to create
|
|
|
|
* internal links, e.g. `{{ "sub/page"|link }}` is equivalent to
|
2015-11-27 21:52:15 +00:00
|
|
|
* `{{ base_url }}/sub/page` and `{{ base_url }}?sub/page`, depending on
|
|
|
|
* enabled URL rewriting. In content files you can use the `%base_url%`
|
|
|
|
* variable; e.g. `%base_url%?sub/page` will be replaced accordingly.
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
2016-04-24 18:11:05 +00:00
|
|
|
* Heads up! Pico always interprets the first parameter as name of the
|
|
|
|
* requested page (provided that the parameter has no value). According to
|
|
|
|
* that you MUST NOT call Pico with a parameter without value as first
|
|
|
|
* parameter (e.g. http://example.com/pico/?someBooleanParam), otherwise
|
|
|
|
* Pico interprets `someBooleanParam` as name of the requested page. Use
|
|
|
|
* `/pico/?someBooleanParam=` or `/pico/?index&someBooleanParam` instead.
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::getRequestUrl()
|
|
|
|
*
|
2015-08-28 16:22:32 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function evaluateRequestUrl()
|
|
|
|
{
|
|
|
|
// use QUERY_STRING; e.g. /pico/?sub/page
|
2016-09-17 18:02:29 +00:00
|
|
|
$pathComponent = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '';
|
2016-12-06 18:03:58 +00:00
|
|
|
if ($pathComponent) {
|
2016-10-05 12:17:10 +00:00
|
|
|
$pathComponent = strstr($pathComponent, '&', true) ?: $pathComponent;
|
2016-04-24 18:11:05 +00:00
|
|
|
if (strpos($pathComponent, '=') === false) {
|
|
|
|
$this->requestUrl = trim(rawurldecode($pathComponent), '/');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// use REQUEST_URI (requires URL rewriting); e.g. /pico/sub/page
|
|
|
|
if (($this->requestUrl === null) && $this->isUrlRewritingEnabled()) {
|
2018-09-09 23:51:35 +00:00
|
|
|
$scriptName = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : '/index.php';
|
|
|
|
|
|
|
|
$basePath = dirname($scriptName);
|
2018-08-06 21:47:52 +00:00
|
|
|
$basePath = !in_array($basePath, array('.', '/', '\\'), true) ? $basePath . '/' : '/';
|
2016-04-24 18:11:05 +00:00
|
|
|
$basePathLength = strlen($basePath);
|
|
|
|
|
2016-12-06 18:03:58 +00:00
|
|
|
$requestUri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
|
|
|
|
if ($requestUri && (substr($requestUri, 0, $basePathLength) === $basePath)) {
|
2016-04-24 18:11:05 +00:00
|
|
|
$requestUri = substr($requestUri, $basePathLength);
|
2017-08-03 20:17:48 +00:00
|
|
|
if ($requestUri && (($queryStringPos = strpos($requestUri, '?')) !== false)) {
|
|
|
|
$requestUri = substr($requestUri, 0, $queryStringPos);
|
|
|
|
}
|
2018-09-09 23:51:35 +00:00
|
|
|
if ($requestUri && ($requestUri !== basename($scriptName))) {
|
2017-08-03 20:17:48 +00:00
|
|
|
$this->requestUrl = rtrim(rawurldecode($requestUri), '/');
|
|
|
|
}
|
2016-04-24 18:11:05 +00:00
|
|
|
}
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
2018-12-03 10:51:39 +00:00
|
|
|
|
|
|
|
if ($this->requestUrl === null) {
|
|
|
|
$this->requestUrl = '';
|
|
|
|
}
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-09 15:19:18 +00:00
|
|
|
* Returns the URL where a user requested the page
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::evaluateRequestUrl()
|
|
|
|
*
|
2015-10-27 00:39:28 +00:00
|
|
|
* @return string|null request URL
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
public function getRequestUrl()
|
|
|
|
{
|
|
|
|
return $this->requestUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-20 17:23:19 +00:00
|
|
|
* Resolves a given file path to its corresponding content file
|
|
|
|
*
|
|
|
|
* This method also prevents `content_dir` breakouts using malicious
|
|
|
|
* request URLs. We don't use `realpath()`, because we neither want to
|
|
|
|
* check for file existance, nor prohibit symlinks which intentionally
|
|
|
|
* point to somewhere outside the `content_dir` folder. It is STRONGLY
|
|
|
|
* RECOMMENDED to use PHP's `open_basedir` feature - always, not just
|
|
|
|
* with Pico!
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::getRequestFile()
|
|
|
|
*
|
|
|
|
* @param string $requestUrl path name (likely from a URL) to resolve
|
|
|
|
*
|
|
|
|
* @return string path to the resolved content file
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
2016-07-20 17:23:19 +00:00
|
|
|
public function resolveFilePath($requestUrl)
|
2015-08-28 16:22:32 +00:00
|
|
|
{
|
2016-03-05 23:29:40 +00:00
|
|
|
$contentDir = $this->getConfig('content_dir');
|
|
|
|
$contentExt = $this->getConfig('content_ext');
|
|
|
|
|
2016-12-06 18:03:58 +00:00
|
|
|
if (!$requestUrl) {
|
2016-07-20 17:23:19 +00:00
|
|
|
return $contentDir . 'index' . $contentExt;
|
2015-08-28 16:22:32 +00:00
|
|
|
} else {
|
2019-04-30 13:22:49 +00:00
|
|
|
// normalize path and prevent content_dir breakouts
|
|
|
|
$requestFile = $this->getNormalizedPath($requestUrl, false, false);
|
2015-10-28 00:41:26 +00:00
|
|
|
|
2015-10-28 00:36:28 +00:00
|
|
|
// discover the content file to serve
|
2019-04-30 13:22:49 +00:00
|
|
|
if (!$requestFile) {
|
|
|
|
return $contentDir . 'index' . $contentExt;
|
|
|
|
} elseif (is_dir($contentDir . $requestFile)) {
|
2015-08-28 16:22:32 +00:00
|
|
|
// if no index file is found, try a accordingly named file in the previous dir
|
|
|
|
// if this file doesn't exist either, show the 404 page, but assume the index
|
|
|
|
// file as being requested (maintains backward compatibility to Pico < 1.0)
|
2019-04-30 13:22:49 +00:00
|
|
|
$indexFile = $contentDir . $requestFile . '/index' . $contentExt;
|
2019-08-29 21:06:27 +00:00
|
|
|
if (is_file($indexFile) || !is_file($contentDir . $requestFile . $contentExt)) {
|
2016-07-20 17:23:19 +00:00
|
|
|
return $indexFile;
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-30 13:22:49 +00:00
|
|
|
return $contentDir . $requestFile . $contentExt;
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-27 00:39:28 +00:00
|
|
|
* Returns the absolute path to the content file to serve
|
2015-06-10 09:40:26 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::resolveFilePath()
|
|
|
|
*
|
|
|
|
* @return string|null file pat
|
2015-06-10 09:40:26 +00:00
|
|
|
*/
|
2015-08-28 16:22:32 +00:00
|
|
|
public function getRequestFile()
|
2015-06-10 09:40:26 +00:00
|
|
|
{
|
2015-08-28 16:22:32 +00:00
|
|
|
return $this->requestFile;
|
|
|
|
}
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* Returns the raw contents of a file
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::getRawContent()
|
|
|
|
*
|
|
|
|
* @param string $file file path
|
|
|
|
*
|
|
|
|
* @return string raw contents of the file
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
public function loadFileContent($file)
|
|
|
|
{
|
|
|
|
return file_get_contents($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-04 14:57:57 +00:00
|
|
|
* Returns the raw contents of the first found 404 file when traversing
|
|
|
|
* up from the directory the requested file is in
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
2016-10-01 17:01:21 +00:00
|
|
|
* If no suitable `404.md` is found, fallback to a built-in error message.
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::getRawContent()
|
|
|
|
*
|
|
|
|
* @param string $file path to requested (but not existing) file
|
|
|
|
*
|
|
|
|
* @return string raw contents of the 404 file
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
2015-10-04 13:24:38 +00:00
|
|
|
public function load404Content($file)
|
2015-08-28 16:22:32 +00:00
|
|
|
{
|
2016-03-02 23:03:53 +00:00
|
|
|
$contentDir = $this->getConfig('content_dir');
|
|
|
|
$contentDirLength = strlen($contentDir);
|
2016-03-05 23:29:40 +00:00
|
|
|
$contentExt = $this->getConfig('content_ext');
|
2016-03-02 23:03:53 +00:00
|
|
|
|
|
|
|
if (substr($file, 0, $contentDirLength) === $contentDir) {
|
|
|
|
$errorFileDir = substr($file, $contentDirLength);
|
|
|
|
|
|
|
|
while ($errorFileDir !== '.') {
|
|
|
|
$errorFileDir = dirname($errorFileDir);
|
2016-03-05 23:29:40 +00:00
|
|
|
$errorFile = $errorFileDir . '/404' . $contentExt;
|
2016-03-02 23:03:53 +00:00
|
|
|
|
2019-08-29 21:06:27 +00:00
|
|
|
if (is_file($contentDir . $errorFile)) {
|
2016-03-05 23:29:40 +00:00
|
|
|
return $this->loadFileContent($contentDir . $errorFile);
|
2016-03-02 23:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-29 21:06:27 +00:00
|
|
|
} elseif (is_file($contentDir . '404' . $contentExt)) {
|
2016-03-02 23:03:53 +00:00
|
|
|
// provided that the requested file is not in the regular
|
|
|
|
// content directory, fallback to Pico's global `404.md`
|
2016-03-05 23:29:40 +00:00
|
|
|
return $this->loadFileContent($contentDir . '404' . $contentExt);
|
2015-10-04 13:24:38 +00:00
|
|
|
}
|
|
|
|
|
2016-10-01 16:58:04 +00:00
|
|
|
// fallback to built-in error message
|
|
|
|
$rawErrorContent = "---\n"
|
|
|
|
. "Title: Error 404\n"
|
|
|
|
. "Robots: noindex,nofollow\n"
|
|
|
|
. "---\n\n"
|
|
|
|
. "# Error 404\n\n"
|
|
|
|
. "Woops. Looks like this page doesn't exist.\n";
|
|
|
|
return $rawErrorContent;
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-29 01:55:30 +00:00
|
|
|
* Returns the raw contents, either of the requested or the 404 file
|
2015-06-10 09:40:26 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::loadFileContent()
|
|
|
|
* @see Pico::load404Content()
|
|
|
|
*
|
2015-10-27 00:39:28 +00:00
|
|
|
* @return string|null raw contents
|
2015-06-10 09:40:26 +00:00
|
|
|
*/
|
2015-08-28 16:22:32 +00:00
|
|
|
public function getRawContent()
|
2015-06-10 09:40:26 +00:00
|
|
|
{
|
2015-08-28 16:22:32 +00:00
|
|
|
return $this->rawContent;
|
|
|
|
}
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2016-11-26 15:48:10 +00:00
|
|
|
/**
|
|
|
|
* Returns TRUE when Pico is serving a 404 page
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::load404Content()
|
|
|
|
*
|
|
|
|
* @return bool TRUE if Pico is serving a 404 page, FALSE otherwise
|
2016-11-26 15:48:10 +00:00
|
|
|
*/
|
|
|
|
public function is404Content()
|
|
|
|
{
|
|
|
|
return $this->is404Content;
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
2016-12-06 19:01:38 +00:00
|
|
|
* Returns known meta headers
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
2017-05-13 23:25:10 +00:00
|
|
|
* This method triggers the `onMetaHeaders` event when the known meta
|
|
|
|
* headers weren't assembled yet.
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @return string[] known meta headers; the array key specifies the YAML
|
|
|
|
* key to search for, the array value is later used to access the
|
2015-10-27 00:48:58 +00:00
|
|
|
* found value
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
public function getMetaHeaders()
|
|
|
|
{
|
2017-05-13 23:25:10 +00:00
|
|
|
if ($this->metaHeaders === null) {
|
|
|
|
$this->metaHeaders = array(
|
2017-10-14 21:12:16 +00:00
|
|
|
'Title' => 'title',
|
|
|
|
'Description' => 'description',
|
|
|
|
'Author' => 'author',
|
|
|
|
'Date' => 'date',
|
2018-07-29 10:51:28 +00:00
|
|
|
'Formatted Date' => 'date_formatted',
|
|
|
|
'Time' => 'time',
|
2017-10-14 21:12:16 +00:00
|
|
|
'Robots' => 'robots',
|
2018-07-29 10:51:28 +00:00
|
|
|
'Template' => 'template',
|
|
|
|
'Hidden' => 'hidden'
|
2017-05-13 23:25:10 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->triggerEvent('onMetaHeaders', array(&$this->metaHeaders));
|
2016-12-06 19:01:38 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 23:25:10 +00:00
|
|
|
return $this->metaHeaders;
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 19:22:38 +00:00
|
|
|
/**
|
|
|
|
* Returns the Symfony YAML parser
|
|
|
|
*
|
2017-05-13 16:08:54 +00:00
|
|
|
* This method triggers the `onYamlParserRegistered` event when the Symfony
|
|
|
|
* YAML parser wasn't initiated yet.
|
|
|
|
*
|
2017-02-05 14:55:53 +00:00
|
|
|
* @return \Symfony\Component\Yaml\Parser Symfony YAML parser
|
2016-12-06 19:22:38 +00:00
|
|
|
*/
|
|
|
|
public function getYamlParser()
|
|
|
|
{
|
2017-02-05 14:55:53 +00:00
|
|
|
if ($this->yamlParser === null) {
|
|
|
|
$this->yamlParser = new \Symfony\Component\Yaml\Parser();
|
2017-05-13 16:08:54 +00:00
|
|
|
$this->triggerEvent('onYamlParserRegistered', array(&$this->yamlParser));
|
2017-02-05 14:55:53 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 19:22:38 +00:00
|
|
|
return $this->yamlParser;
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* Parses the file meta from raw file contents
|
|
|
|
*
|
|
|
|
* Meta data MUST start on the first line of the file, either opened and
|
2015-10-29 01:55:30 +00:00
|
|
|
* closed by `---` or C-style block comments (deprecated). The headers are
|
2018-12-03 10:52:05 +00:00
|
|
|
* parsed by the YAML component of the Symfony project. If you're a plugin
|
|
|
|
* developer, you MUST register new headers during the `onMetaHeaders`
|
|
|
|
* event first. The implicit availability of headers is for users and
|
|
|
|
* pure (!) theme developers ONLY.
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::getFileMeta()
|
|
|
|
*
|
|
|
|
* @param string $rawContent the raw file contents
|
|
|
|
* @param string[] $headers known meta headers
|
|
|
|
*
|
|
|
|
* @return array parsed meta data
|
|
|
|
*
|
2015-11-29 19:58:41 +00:00
|
|
|
* @throws \Symfony\Component\Yaml\Exception\ParseException thrown when the
|
|
|
|
* meta data is invalid
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
public function parseFileMeta($rawContent, array $headers)
|
|
|
|
{
|
|
|
|
$meta = array();
|
|
|
|
$pattern = "/^(\/(\*)|---)[[:blank:]]*(?:\r)?\n"
|
2015-12-21 02:42:59 +00:00
|
|
|
. "(?:(.*?)(?:\r)?\n)?(?(2)\*\/|---)[[:blank:]]*(?:(?:\r)?\n|$)/s";
|
|
|
|
if (preg_match($pattern, $rawContent, $rawMetaMatches) && isset($rawMetaMatches[3])) {
|
2017-10-14 21:12:16 +00:00
|
|
|
$meta = $this->getYamlParser()->parse($rawMetaMatches[3]) ?: array();
|
|
|
|
$meta = is_array($meta) ? $meta : array('title' => $meta);
|
2016-07-15 01:13:58 +00:00
|
|
|
|
2017-10-14 21:12:16 +00:00
|
|
|
foreach ($headers as $name => $key) {
|
|
|
|
if (isset($meta[$name])) {
|
2015-10-04 23:50:55 +00:00
|
|
|
// rename field (e.g. remove whitespaces)
|
2017-10-14 21:12:16 +00:00
|
|
|
if ($key != $name) {
|
|
|
|
$meta[$key] = $meta[$name];
|
|
|
|
unset($meta[$name]);
|
2015-10-04 23:50:55 +00:00
|
|
|
}
|
2017-10-14 21:12:16 +00:00
|
|
|
} elseif (!isset($meta[$key])) {
|
2015-10-04 23:50:55 +00:00
|
|
|
// guarantee array key existance
|
2017-10-14 21:12:16 +00:00
|
|
|
$meta[$key] = '';
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2017-07-28 10:16:26 +00:00
|
|
|
if (!empty($meta['date']) || !empty($meta['time'])) {
|
2016-03-16 13:27:42 +00:00
|
|
|
// workaround for issue #336
|
|
|
|
// Symfony YAML interprets ISO-8601 datetime strings and returns timestamps instead of the string
|
|
|
|
// this behavior conforms to the YAML standard, i.e. this is no bug of Symfony YAML
|
|
|
|
if (is_int($meta['date'])) {
|
|
|
|
$meta['time'] = $meta['date'];
|
2017-07-28 10:16:26 +00:00
|
|
|
$meta['date'] = '';
|
|
|
|
}
|
2016-03-16 13:27:42 +00:00
|
|
|
|
2017-07-28 10:16:26 +00:00
|
|
|
if (empty($meta['time'])) {
|
|
|
|
$meta['time'] = strtotime($meta['date']) ?: '';
|
|
|
|
} elseif (empty($meta['date'])) {
|
2016-03-16 13:27:42 +00:00
|
|
|
$rawDateFormat = (date('H:i:s', $meta['time']) === '00:00:00') ? 'Y-m-d' : 'Y-m-d H:i:s';
|
|
|
|
$meta['date'] = date($rawDateFormat, $meta['time']);
|
|
|
|
}
|
2015-06-10 09:40:26 +00:00
|
|
|
} else {
|
2017-07-28 10:16:26 +00:00
|
|
|
$meta['date'] = $meta['time'] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($meta['date_formatted'])) {
|
|
|
|
$dateFormat = $this->getConfig('date_format');
|
|
|
|
$meta['date_formatted'] = $meta['time'] ? utf8_encode(strftime($dateFormat, $meta['time'])) : '';
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-10-04 23:50:55 +00:00
|
|
|
// guarantee array key existance
|
2017-12-27 20:36:56 +00:00
|
|
|
$meta = array_fill_keys($headers, '');
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
return $meta;
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-28 16:22:32 +00:00
|
|
|
* Returns the parsed meta data of the requested page
|
2015-06-10 09:40:26 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::parseFileMeta()
|
|
|
|
*
|
2015-10-27 00:39:28 +00:00
|
|
|
* @return array|null parsed meta data
|
2015-06-10 09:40:26 +00:00
|
|
|
*/
|
2015-08-28 16:22:32 +00:00
|
|
|
public function getFileMeta()
|
2015-06-10 09:40:26 +00:00
|
|
|
{
|
2015-08-28 16:22:32 +00:00
|
|
|
return $this->meta;
|
|
|
|
}
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2015-11-12 14:33:48 +00:00
|
|
|
/**
|
2018-03-29 20:47:23 +00:00
|
|
|
* Returns the Parsedown markdown parser
|
2015-11-12 14:33:48 +00:00
|
|
|
*
|
2017-05-13 16:08:54 +00:00
|
|
|
* This method triggers the `onParsedownRegistered` event when the
|
2018-03-29 20:47:23 +00:00
|
|
|
* Parsedown markdown parser wasn't initiated yet.
|
2017-05-13 16:08:54 +00:00
|
|
|
*
|
2018-03-29 20:47:23 +00:00
|
|
|
* @return Parsedown Parsedown markdown parser
|
2015-11-12 14:33:48 +00:00
|
|
|
*/
|
|
|
|
public function getParsedown()
|
|
|
|
{
|
2017-02-05 14:55:53 +00:00
|
|
|
if ($this->parsedown === null) {
|
2017-02-05 20:49:54 +00:00
|
|
|
$className = $this->config['content_config']['extra'] ? 'ParsedownExtra' : 'Parsedown';
|
|
|
|
$this->parsedown = new $className();
|
|
|
|
|
|
|
|
$this->parsedown->setBreaksEnabled((bool) $this->config['content_config']['breaks']);
|
|
|
|
$this->parsedown->setMarkupEscaped((bool) $this->config['content_config']['escape']);
|
|
|
|
$this->parsedown->setUrlsLinked((bool) $this->config['content_config']['auto_urls']);
|
2017-05-13 16:08:54 +00:00
|
|
|
|
|
|
|
$this->triggerEvent('onParsedownRegistered', array(&$this->parsedown));
|
2017-02-05 14:55:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:33:48 +00:00
|
|
|
return $this->parsedown;
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
2017-12-27 20:36:56 +00:00
|
|
|
* Applies some static preparations to the raw contents of a page
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
2017-12-27 20:36:56 +00:00
|
|
|
* This method removes the meta header and replaces `%...%` placeholders
|
|
|
|
* by calling the {@see Pico::substituteFileContent()} method.
|
2017-06-21 12:27:34 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::substituteFileContent()
|
|
|
|
* @see Pico::parseFileContent()
|
|
|
|
* @see Pico::getFileContent()
|
|
|
|
*
|
|
|
|
* @param string $rawContent raw contents of a page
|
|
|
|
* @param array $meta meta data to use for %meta.*% replacement
|
|
|
|
*
|
|
|
|
* @return string prepared Markdown contents
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
2018-01-21 22:06:38 +00:00
|
|
|
public function prepareFileContent($rawContent, array $meta = array())
|
2015-08-28 16:22:32 +00:00
|
|
|
{
|
|
|
|
// remove meta header
|
|
|
|
$metaHeaderPattern = "/^(\/(\*)|---)[[:blank:]]*(?:\r)?\n"
|
2015-12-21 02:42:59 +00:00
|
|
|
. "(?:(.*?)(?:\r)?\n)?(?(2)\*\/|---)[[:blank:]]*(?:(?:\r)?\n|$)/s";
|
2017-06-21 12:27:34 +00:00
|
|
|
$markdown = preg_replace($metaHeaderPattern, '', $rawContent, 1);
|
|
|
|
|
|
|
|
// replace placeholders
|
|
|
|
$markdown = $this->substituteFileContent($markdown, $meta);
|
|
|
|
|
|
|
|
return $markdown;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces all %...% placeholders in a page's contents
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @param string $markdown Markdown contents of a page
|
|
|
|
* @param array $meta meta data to use for %meta.*% replacement
|
|
|
|
*
|
|
|
|
* @return string substituted Markdown contents
|
2017-06-21 12:27:34 +00:00
|
|
|
*/
|
|
|
|
public function substituteFileContent($markdown, array $meta = array())
|
|
|
|
{
|
|
|
|
$variables = array();
|
2015-08-28 16:22:32 +00:00
|
|
|
|
2016-03-05 23:38:51 +00:00
|
|
|
// replace %version%
|
2016-03-05 23:49:45 +00:00
|
|
|
$variables['%version%'] = static::VERSION;
|
2016-03-05 23:38:51 +00:00
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
// replace %site_title%
|
2016-03-05 23:49:45 +00:00
|
|
|
$variables['%site_title%'] = $this->getConfig('site_title');
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
// replace %base_url%
|
|
|
|
if ($this->isUrlRewritingEnabled()) {
|
|
|
|
// always use `%base_url%?sub/page` syntax for internal links
|
|
|
|
// we'll replace the links accordingly, depending on enabled rewriting
|
2016-03-05 23:49:45 +00:00
|
|
|
$variables['%base_url%?'] = $this->getBaseUrl();
|
2015-06-10 09:40:26 +00:00
|
|
|
} else {
|
2015-10-01 19:54:30 +00:00
|
|
|
// actually not necessary, but makes the URL look a little nicer
|
2016-03-05 23:49:45 +00:00
|
|
|
$variables['%base_url%?'] = $this->getBaseUrl() . '?';
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
2016-03-05 23:49:45 +00:00
|
|
|
$variables['%base_url%'] = rtrim($this->getBaseUrl(), '/');
|
2015-08-28 16:22:32 +00:00
|
|
|
|
2019-04-30 13:34:11 +00:00
|
|
|
// replace %plugins_url%, %themes_url% and %assets_url%
|
|
|
|
$variables['%plugins_url%'] = rtrim($this->getConfig('plugins_url'), '/');
|
|
|
|
$variables['%themes_url%'] = rtrim($this->getConfig('themes_url'), '/');
|
|
|
|
$variables['%assets_url%'] = rtrim($this->getConfig('assets_url'), '/');
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
// replace %theme_url%
|
2019-04-30 13:34:11 +00:00
|
|
|
$variables['%theme_url%'] = $this->getConfig('themes_url') . $this->getConfig('theme');
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
// replace %meta.*%
|
2016-12-06 18:03:58 +00:00
|
|
|
if ($meta) {
|
2015-11-06 00:08:31 +00:00
|
|
|
foreach ($meta as $metaKey => $metaValue) {
|
2015-11-03 22:49:34 +00:00
|
|
|
if (is_scalar($metaValue) || ($metaValue === null)) {
|
2017-12-27 20:36:56 +00:00
|
|
|
$variables['%meta.' . $metaKey . '%'] = (string) $metaValue;
|
2015-11-03 22:49:34 +00:00
|
|
|
}
|
2015-10-28 00:08:45 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-28 16:22:32 +00:00
|
|
|
|
2019-03-11 22:02:45 +00:00
|
|
|
// replace %config.*%
|
|
|
|
foreach ($this->config as $configKey => $configValue) {
|
|
|
|
if (is_scalar($configValue) || ($configValue === null)) {
|
|
|
|
$variables['%config.' . $configKey . '%'] = (string) $configValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-21 12:27:34 +00:00
|
|
|
return str_replace(array_keys($variables), $variables, $markdown);
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the contents of a page using ParsedownExtra
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::prepareFileContent()
|
|
|
|
* @see Pico::substituteFileContent()
|
|
|
|
* @see Pico::getFileContent()
|
|
|
|
*
|
|
|
|
* @param string $markdown Markdown contents of a page
|
|
|
|
*
|
|
|
|
* @return string parsed contents (HTML)
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
2017-06-21 12:27:34 +00:00
|
|
|
public function parseFileContent($markdown)
|
2015-08-28 16:22:32 +00:00
|
|
|
{
|
2017-06-21 12:27:34 +00:00
|
|
|
return $this->getParsedown()->text($markdown);
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-28 16:22:32 +00:00
|
|
|
* Returns the cached contents of the requested page
|
2015-06-10 09:40:26 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::prepareFileContent()
|
|
|
|
* @see Pico::substituteFileContent()
|
|
|
|
* @see Pico::parseFileContent()
|
|
|
|
*
|
2015-10-27 00:39:28 +00:00
|
|
|
* @return string|null parsed contents
|
2015-06-10 09:40:26 +00:00
|
|
|
*/
|
2015-08-28 16:22:32 +00:00
|
|
|
public function getFileContent()
|
2015-06-10 09:40:26 +00:00
|
|
|
{
|
2015-08-28 16:22:32 +00:00
|
|
|
return $this->content;
|
|
|
|
}
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* Reads the data of all pages known to Pico
|
|
|
|
*
|
2015-10-27 00:39:28 +00:00
|
|
|
* The page data will be an array containing the following values:
|
2015-12-07 14:17:39 +00:00
|
|
|
*
|
2017-12-27 20:36:56 +00:00
|
|
|
* | Array key | Type | Description |
|
|
|
|
* | -------------- | ------- | ---------------------------------------- |
|
|
|
|
* | id | string | relative path to the content file |
|
|
|
|
* | url | string | URL to the page |
|
|
|
|
* | title | string | title of the page (YAML header) |
|
|
|
|
* | description | string | description of the page (YAML header) |
|
|
|
|
* | author | string | author of the page (YAML header) |
|
|
|
|
* | time | int | timestamp derived from the Date header |
|
|
|
|
* | date | string | date of the page (YAML header) |
|
|
|
|
* | date_formatted | string | formatted date of the page |
|
|
|
|
* | hidden | bool | this page shouldn't be visible |
|
|
|
|
* | raw_content | string | raw, not yet parsed contents of the page |
|
|
|
|
* | meta | string[] | parsed meta data of the page |
|
|
|
|
* | previous_page | &array[] | reference to the previous page |
|
|
|
|
* | next_page | &array[] | reference to the next page |
|
|
|
|
* | tree_node | &array[] | reference to the page's tree node |
|
|
|
|
*
|
|
|
|
* Please note that the `previous_page` and `next_page` keys don't
|
|
|
|
* exist until the `onCurrentPageDiscovered` event is triggered
|
|
|
|
* ({@see Pico::discoverPageSiblings()}). The `tree_node` key doesn't
|
|
|
|
* exit until the `onPageTreeBuilt` event is triggered
|
Add Pico::buildPageTree()
Pico's page tree is a list of all the tree's branches (no matter the depth). Thus, by iterating a array element, you get the nodes of a given branch. All leaf nodes do represent a page, but inner nodes may or may not represent a page (e.g. if there's a `sub/page.md`, but neither a `sub/index.md` nor a `sub.md`, the inner node `sub`, that is the parent of the `sub/page` node, represents no page itself).
A page's file path describes its node's path in the tree (e.g. the page `sub/page.md` is represented by the `sub/page` node, thus a child of the `sub` node and a element of the `sub` branch). However, the index page of a folder (e.g. `sub/index.md`), is *not* a node of the `sub` branch, but rather of the `/` branch. The page's node is not `sub/index`, but `sub`. If two pages are described by the same node (e.g. if both a `sub/index.md` and a `sub.md` exist), the index page takes precedence. Pico's main index page (i.e. `index.md`) is represented by the tree's root node `/` and a special case: it is the only node of the `` (i.e. the empty string) branch.
A node is represented by an array with the keys `id`, `page` and `children`. The `id` key contains a string with the node's name. If the node represents a page, the `page` key is a reference to the page's data array. If the node is a inner node, the `children` key is a reference to its matching branch (i.e. a list of the node's children). The order of a node's children matches the order in Pico's pages array.
If you want to walk the whole page tree, start with the tree's root node at `$pageTree[""]["/"]`. The root node's `children` key is a reference to the `/` branch at `$pageTree["/"]`, that is a list of the root node's direct child nodes and their siblings.
You MUST NOT iterate the page tree itself (i.e. the list of the tree's branches), its order is undefined and the array will be replaced by a non-iterable data structure with Pico 3.0.
2017-11-18 17:34:57 +00:00
|
|
|
* ({@see Pico::buildPageTree()}).
|
2017-10-19 19:55:41 +00:00
|
|
|
*
|
|
|
|
* @see Pico::sortPages()
|
|
|
|
* @see Pico::discoverPageSiblings()
|
|
|
|
* @see Pico::getPages()
|
2015-10-27 00:39:28 +00:00
|
|
|
*
|
2015-08-28 16:22:32 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function readPages()
|
|
|
|
{
|
2016-03-05 23:29:40 +00:00
|
|
|
$contentDir = $this->getConfig('content_dir');
|
|
|
|
$contentExt = $this->getConfig('content_ext');
|
|
|
|
|
2015-10-01 19:54:30 +00:00
|
|
|
$this->pages = array();
|
2017-05-07 12:20:44 +00:00
|
|
|
$files = $this->getFiles($contentDir, $contentExt, self::SORT_NONE);
|
2015-08-28 16:22:32 +00:00
|
|
|
foreach ($files as $i => $file) {
|
|
|
|
// skip 404 page
|
2016-03-05 23:29:40 +00:00
|
|
|
if (basename($file) === '404' . $contentExt) {
|
2015-08-28 16:22:32 +00:00
|
|
|
unset($files[$i]);
|
2015-06-10 09:40:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-21 22:06:38 +00:00
|
|
|
$id = substr($file, strlen($contentDir), -strlen($contentExt));
|
2015-10-01 15:22:14 +00:00
|
|
|
|
2016-03-05 23:54:36 +00:00
|
|
|
// trigger onSinglePageLoading event
|
2017-10-14 20:08:11 +00:00
|
|
|
// skip inaccessible pages (e.g. drop "sub.md" if "sub/index.md" exists) by default
|
2016-03-05 23:29:40 +00:00
|
|
|
$conflictFile = $contentDir . $id . '/index' . $contentExt;
|
2017-10-14 20:08:11 +00:00
|
|
|
$skipFile = in_array($conflictFile, $files, true) ?: null;
|
|
|
|
$this->triggerEvent('onSinglePageLoading', array($id, &$skipFile));
|
|
|
|
|
|
|
|
if ($skipFile) {
|
2015-10-01 15:22:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
$url = $this->getPageUrl($id);
|
2016-03-05 23:29:40 +00:00
|
|
|
if ($file !== $this->requestFile) {
|
2016-10-09 00:49:30 +00:00
|
|
|
$rawContent = $this->loadFileContent($file);
|
2015-11-29 19:58:41 +00:00
|
|
|
|
2017-05-13 23:26:29 +00:00
|
|
|
// trigger onSinglePageContent event
|
|
|
|
$this->triggerEvent('onSinglePageContent', array($id, &$rawContent));
|
|
|
|
|
2015-11-29 19:58:41 +00:00
|
|
|
$headers = $this->getMetaHeaders();
|
|
|
|
try {
|
|
|
|
$meta = $this->parseFileMeta($rawContent, $headers);
|
|
|
|
} catch (\Symfony\Component\Yaml\Exception\ParseException $e) {
|
2015-12-21 03:08:35 +00:00
|
|
|
$meta = $this->parseFileMeta('', $headers);
|
2015-11-29 19:58:41 +00:00
|
|
|
$meta['YAML_ParseError'] = $e->getMessage();
|
|
|
|
}
|
2015-08-28 16:22:32 +00:00
|
|
|
} else {
|
|
|
|
$rawContent = &$this->rawContent;
|
|
|
|
$meta = &$this->meta;
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
// build page data
|
|
|
|
// title, description, author and date are assumed to be pretty basic data
|
|
|
|
// everything else is accessible through $page['meta']
|
|
|
|
$page = array(
|
|
|
|
'id' => $id,
|
|
|
|
'url' => $url,
|
|
|
|
'title' => &$meta['title'],
|
|
|
|
'description' => &$meta['description'],
|
|
|
|
'author' => &$meta['author'],
|
|
|
|
'time' => &$meta['time'],
|
|
|
|
'date' => &$meta['date'],
|
|
|
|
'date_formatted' => &$meta['date_formatted'],
|
2018-07-29 10:51:28 +00:00
|
|
|
'hidden' => (preg_match('/(?:^|\/)_/', $id) || $meta['hidden']),
|
2015-08-28 16:22:32 +00:00
|
|
|
'raw_content' => &$rawContent,
|
|
|
|
'meta' => &$meta
|
2015-06-10 09:40:26 +00:00
|
|
|
);
|
|
|
|
|
2015-11-29 15:39:03 +00:00
|
|
|
if ($file === $this->requestFile) {
|
2015-08-28 16:22:32 +00:00
|
|
|
$page['content'] = &$this->content;
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
2015-08-28 16:22:32 +00:00
|
|
|
|
|
|
|
unset($rawContent, $meta);
|
|
|
|
|
2016-03-05 23:54:36 +00:00
|
|
|
// trigger onSinglePageLoaded event
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->triggerEvent('onSinglePageLoaded', array(&$page));
|
|
|
|
|
2016-03-05 23:54:36 +00:00
|
|
|
if ($page !== null) {
|
|
|
|
$this->pages[$id] = $page;
|
|
|
|
}
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
2015-10-04 20:39:38 +00:00
|
|
|
}
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2015-10-04 20:39:38 +00:00
|
|
|
/**
|
|
|
|
* Sorts all pages known to Pico
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::readPages()
|
|
|
|
* @see Pico::getPages()
|
|
|
|
*
|
2015-10-04 20:39:38 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function sortPages()
|
|
|
|
{
|
2015-10-01 19:54:30 +00:00
|
|
|
// sort pages
|
2017-02-05 15:52:18 +00:00
|
|
|
$order = strtolower($this->getConfig('pages_order'));
|
|
|
|
$orderBy = strtolower($this->getConfig('pages_order_by'));
|
2016-12-06 18:47:37 +00:00
|
|
|
|
2017-07-14 18:37:05 +00:00
|
|
|
if (($orderBy !== 'alpha') && ($orderBy !== 'date') && ($orderBy !== 'meta')) {
|
2016-12-06 18:47:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-01 19:54:30 +00:00
|
|
|
$alphaSortClosure = function ($a, $b) use ($order) {
|
2017-11-18 17:28:02 +00:00
|
|
|
if (!empty($a['hidden']) xor !empty($b['hidden'])) {
|
|
|
|
return (!empty($a['hidden']) - !empty($b['hidden'])) * (($order === 'desc') ? -1 : 1);
|
2016-12-12 13:49:05 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 19:54:30 +00:00
|
|
|
$aSortKey = (basename($a['id']) === 'index') ? dirname($a['id']) : $a['id'];
|
|
|
|
$bSortKey = (basename($b['id']) === 'index') ? dirname($b['id']) : $b['id'];
|
|
|
|
|
|
|
|
$cmp = strcmp($aSortKey, $bSortKey);
|
2015-11-29 15:39:03 +00:00
|
|
|
return $cmp * (($order === 'desc') ? -1 : 1);
|
2015-10-01 19:54:30 +00:00
|
|
|
};
|
2015-08-28 16:22:32 +00:00
|
|
|
|
2017-07-14 18:37:05 +00:00
|
|
|
if ($orderBy === 'meta') {
|
|
|
|
// sort by arbitrary meta value
|
|
|
|
$orderByMeta = $this->getConfig('pages_order_by_meta');
|
|
|
|
uasort($this->pages, function ($a, $b) use ($alphaSortClosure, $order, $orderByMeta) {
|
|
|
|
$aSortValue = isset($a['meta'][$orderByMeta]) ? $a['meta'][$orderByMeta] : null;
|
|
|
|
$aSortValueNull = ($aSortValue === null);
|
|
|
|
|
|
|
|
$bSortValue = isset($b['meta'][$orderByMeta]) ? $b['meta'][$orderByMeta] : null;
|
|
|
|
$bSortValueNull = ($bSortValue === null);
|
|
|
|
|
|
|
|
$cmp = 0;
|
|
|
|
if ($aSortValueNull || $bSortValueNull) {
|
|
|
|
$cmp = ($aSortValueNull - $bSortValueNull);
|
|
|
|
} elseif ($aSortValue != $bSortValue) {
|
|
|
|
$cmp = ($aSortValue > $bSortValue) ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($cmp === 0) {
|
|
|
|
// never assume equality; fallback to alphabetical order
|
|
|
|
return $alphaSortClosure($a, $b);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cmp * (($order === 'desc') ? -1 : 1);
|
|
|
|
});
|
|
|
|
} elseif ($orderBy === 'date') {
|
2015-10-01 19:54:30 +00:00
|
|
|
// sort by date
|
|
|
|
uasort($this->pages, function ($a, $b) use ($alphaSortClosure, $order) {
|
2017-11-18 17:28:02 +00:00
|
|
|
if (!empty($a['hidden']) xor !empty($b['hidden'])) {
|
2016-12-12 13:49:05 +00:00
|
|
|
return $alphaSortClosure($a, $b);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$a['time'] || !$b['time']) {
|
|
|
|
$cmp = (!$a['time'] - !$b['time']);
|
2015-08-28 16:22:32 +00:00
|
|
|
} else {
|
|
|
|
$cmp = ($b['time'] - $a['time']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($cmp === 0) {
|
2015-10-01 19:54:30 +00:00
|
|
|
// never assume equality; fallback to alphabetical order
|
|
|
|
return $alphaSortClosure($a, $b);
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
|
|
|
|
2015-11-29 15:39:03 +00:00
|
|
|
return $cmp * (($order === 'desc') ? 1 : -1);
|
2015-08-28 16:22:32 +00:00
|
|
|
});
|
2015-10-01 19:54:30 +00:00
|
|
|
} else {
|
|
|
|
// sort alphabetically
|
|
|
|
uasort($this->pages, $alphaSortClosure);
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2016-12-12 13:49:57 +00:00
|
|
|
/**
|
|
|
|
* Walks through the list of all known pages and discovers the previous and
|
|
|
|
* next page respectively
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::readPages()
|
|
|
|
* @see Pico::getPages()
|
|
|
|
*
|
2016-12-12 13:49:57 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function discoverPageSiblings()
|
|
|
|
{
|
|
|
|
if (($this->getConfig('order_by') === 'date') && ($this->getConfig('order') === 'desc')) {
|
|
|
|
$precedingPageKey = 'next_page';
|
|
|
|
$succeedingPageKey = 'previous_page';
|
|
|
|
} else {
|
|
|
|
$precedingPageKey = 'previous_page';
|
|
|
|
$succeedingPageKey = 'next_page';
|
|
|
|
}
|
|
|
|
|
|
|
|
$precedingPageId = null;
|
|
|
|
foreach ($this->pages as $id => &$pageData) {
|
|
|
|
$pageData[$precedingPageKey] = null;
|
|
|
|
$pageData[$succeedingPageKey] = null;
|
|
|
|
|
2017-11-18 17:28:02 +00:00
|
|
|
if (!empty($pageData['hidden'])) {
|
2016-12-12 13:49:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($precedingPageId !== null) {
|
|
|
|
$precedingPageData = &$this->pages[$precedingPageId];
|
|
|
|
$pageData[$precedingPageKey] = &$precedingPageData;
|
|
|
|
$precedingPageData[$succeedingPageKey] = &$pageData;
|
|
|
|
}
|
|
|
|
|
|
|
|
$precedingPageId = $id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* Returns the list of known pages
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::readPages()
|
|
|
|
*
|
2015-11-25 03:07:46 +00:00
|
|
|
* @return array[]|null the data of all pages
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
public function getPages()
|
|
|
|
{
|
|
|
|
return $this->pages;
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-12 13:49:57 +00:00
|
|
|
* Discovers the page data of the requested page as well as the previous
|
|
|
|
* and next page relative to it
|
2015-06-10 09:40:26 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::getCurrentPage()
|
|
|
|
* @see Pico::getPreviousPage()
|
|
|
|
* @see Pico::getNextPage()
|
|
|
|
*
|
2015-08-28 16:22:32 +00:00
|
|
|
* @return void
|
2015-06-10 09:40:26 +00:00
|
|
|
*/
|
2015-08-28 16:22:32 +00:00
|
|
|
protected function discoverCurrentPage()
|
2015-06-10 09:40:26 +00:00
|
|
|
{
|
2017-10-07 22:36:14 +00:00
|
|
|
$currentPageId = $this->getPageId($this->requestFile);
|
|
|
|
if ($currentPageId && isset($this->pages[$currentPageId])) {
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->currentPage = &$this->pages[$currentPageId];
|
2016-12-12 13:49:57 +00:00
|
|
|
$this->previousPage = &$this->pages[$currentPageId]['previous_page'];
|
|
|
|
$this->nextPage = &$this->pages[$currentPageId]['next_page'];
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-28 16:22:32 +00:00
|
|
|
* Returns the data of the requested page
|
2015-06-10 09:40:26 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::discoverCurrentPage()
|
|
|
|
*
|
2015-10-27 00:39:28 +00:00
|
|
|
* @return array|null page data
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
public function getCurrentPage()
|
|
|
|
{
|
|
|
|
return $this->currentPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the data of the previous page relative to the page being served
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::discoverCurrentPage()
|
|
|
|
*
|
2015-10-27 00:39:28 +00:00
|
|
|
* @return array|null page data
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
public function getPreviousPage()
|
|
|
|
{
|
|
|
|
return $this->previousPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the data of the next page relative to the page being served
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::discoverCurrentPage()
|
|
|
|
*
|
2015-10-27 00:39:28 +00:00
|
|
|
* @return array|null page data
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
public function getNextPage()
|
|
|
|
{
|
|
|
|
return $this->nextPage;
|
|
|
|
}
|
|
|
|
|
Add Pico::buildPageTree()
Pico's page tree is a list of all the tree's branches (no matter the depth). Thus, by iterating a array element, you get the nodes of a given branch. All leaf nodes do represent a page, but inner nodes may or may not represent a page (e.g. if there's a `sub/page.md`, but neither a `sub/index.md` nor a `sub.md`, the inner node `sub`, that is the parent of the `sub/page` node, represents no page itself).
A page's file path describes its node's path in the tree (e.g. the page `sub/page.md` is represented by the `sub/page` node, thus a child of the `sub` node and a element of the `sub` branch). However, the index page of a folder (e.g. `sub/index.md`), is *not* a node of the `sub` branch, but rather of the `/` branch. The page's node is not `sub/index`, but `sub`. If two pages are described by the same node (e.g. if both a `sub/index.md` and a `sub.md` exist), the index page takes precedence. Pico's main index page (i.e. `index.md`) is represented by the tree's root node `/` and a special case: it is the only node of the `` (i.e. the empty string) branch.
A node is represented by an array with the keys `id`, `page` and `children`. The `id` key contains a string with the node's name. If the node represents a page, the `page` key is a reference to the page's data array. If the node is a inner node, the `children` key is a reference to its matching branch (i.e. a list of the node's children). The order of a node's children matches the order in Pico's pages array.
If you want to walk the whole page tree, start with the tree's root node at `$pageTree[""]["/"]`. The root node's `children` key is a reference to the `/` branch at `$pageTree["/"]`, that is a list of the root node's direct child nodes and their siblings.
You MUST NOT iterate the page tree itself (i.e. the list of the tree's branches), its order is undefined and the array will be replaced by a non-iterable data structure with Pico 3.0.
2017-11-18 17:34:57 +00:00
|
|
|
/**
|
|
|
|
* Builds a tree structure containing all known pages
|
|
|
|
*
|
|
|
|
* Pico's page tree is a list of all the tree's branches (no matter the
|
|
|
|
* depth). Thus, by iterating a array element, you get the nodes of a given
|
|
|
|
* branch. All leaf nodes do represent a page, but inner nodes may or may
|
|
|
|
* not represent a page (e.g. if there's a `sub/page.md`, but neither a
|
|
|
|
* `sub/index.md` nor a `sub.md`, the inner node `sub`, that is the parent
|
|
|
|
* of the `sub/page` node, represents no page itself).
|
|
|
|
*
|
|
|
|
* A page's file path describes its node's path in the tree (e.g. the page
|
|
|
|
* `sub/page.md` is represented by the `sub/page` node, thus a child of the
|
|
|
|
* `sub` node and a element of the `sub` branch). However, the index page
|
|
|
|
* of a folder (e.g. `sub/index.md`), is *not* a node of the `sub` branch,
|
|
|
|
* but rather of the `/` branch. The page's node is not `sub/index`, but
|
|
|
|
* `sub`. If two pages are described by the same node (e.g. if both a
|
|
|
|
* `sub/index.md` and a `sub.md` exist), the index page takes precedence.
|
|
|
|
* Pico's main index page (i.e. `index.md`) is represented by the tree's
|
|
|
|
* root node `/` and a special case: it is the only node of the `` (i.e.
|
|
|
|
* the empty string) branch.
|
|
|
|
*
|
|
|
|
* A node is represented by an array with the keys `id`, `page` and
|
|
|
|
* `children`. The `id` key contains a string with the node's name. If the
|
|
|
|
* node represents a page, the `page` key is a reference to the page's
|
|
|
|
* data array. If the node is a inner node, the `children` key is a
|
|
|
|
* reference to its matching branch (i.e. a list of the node's children).
|
|
|
|
* The order of a node's children matches the order in Pico's pages array.
|
|
|
|
*
|
|
|
|
* If you want to walk the whole page tree, start with the tree's root node
|
2017-12-27 20:36:56 +00:00
|
|
|
* at `$pageTree[""]["/"]`, or rather, use `$pages["index"]["tree_node"]`.
|
|
|
|
* The root node's `children` key is a reference to the `/` branch at
|
|
|
|
* `$pageTree["/"]`, that is a list of the root node's direct child nodes
|
|
|
|
* and their siblings.
|
Add Pico::buildPageTree()
Pico's page tree is a list of all the tree's branches (no matter the depth). Thus, by iterating a array element, you get the nodes of a given branch. All leaf nodes do represent a page, but inner nodes may or may not represent a page (e.g. if there's a `sub/page.md`, but neither a `sub/index.md` nor a `sub.md`, the inner node `sub`, that is the parent of the `sub/page` node, represents no page itself).
A page's file path describes its node's path in the tree (e.g. the page `sub/page.md` is represented by the `sub/page` node, thus a child of the `sub` node and a element of the `sub` branch). However, the index page of a folder (e.g. `sub/index.md`), is *not* a node of the `sub` branch, but rather of the `/` branch. The page's node is not `sub/index`, but `sub`. If two pages are described by the same node (e.g. if both a `sub/index.md` and a `sub.md` exist), the index page takes precedence. Pico's main index page (i.e. `index.md`) is represented by the tree's root node `/` and a special case: it is the only node of the `` (i.e. the empty string) branch.
A node is represented by an array with the keys `id`, `page` and `children`. The `id` key contains a string with the node's name. If the node represents a page, the `page` key is a reference to the page's data array. If the node is a inner node, the `children` key is a reference to its matching branch (i.e. a list of the node's children). The order of a node's children matches the order in Pico's pages array.
If you want to walk the whole page tree, start with the tree's root node at `$pageTree[""]["/"]`. The root node's `children` key is a reference to the `/` branch at `$pageTree["/"]`, that is a list of the root node's direct child nodes and their siblings.
You MUST NOT iterate the page tree itself (i.e. the list of the tree's branches), its order is undefined and the array will be replaced by a non-iterable data structure with Pico 3.0.
2017-11-18 17:34:57 +00:00
|
|
|
*
|
|
|
|
* You MUST NOT iterate the page tree itself (i.e. the list of the tree's
|
|
|
|
* branches), its order is undefined and the array will be replaced by a
|
|
|
|
* non-iterable data structure with Pico 3.0.
|
|
|
|
*
|
|
|
|
* @see Pico::getPageTree()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function buildPageTree()
|
|
|
|
{
|
|
|
|
$this->pageTree = array();
|
|
|
|
foreach ($this->pages as $id => &$pageData) {
|
|
|
|
// main index page
|
|
|
|
if ($id === 'index') {
|
2018-01-31 11:29:59 +00:00
|
|
|
$this->pageTree['']['/']['id'] = '/';
|
|
|
|
$this->pageTree['']['/']['page'] = &$pageData;
|
Add Pico::buildPageTree()
Pico's page tree is a list of all the tree's branches (no matter the depth). Thus, by iterating a array element, you get the nodes of a given branch. All leaf nodes do represent a page, but inner nodes may or may not represent a page (e.g. if there's a `sub/page.md`, but neither a `sub/index.md` nor a `sub.md`, the inner node `sub`, that is the parent of the `sub/page` node, represents no page itself).
A page's file path describes its node's path in the tree (e.g. the page `sub/page.md` is represented by the `sub/page` node, thus a child of the `sub` node and a element of the `sub` branch). However, the index page of a folder (e.g. `sub/index.md`), is *not* a node of the `sub` branch, but rather of the `/` branch. The page's node is not `sub/index`, but `sub`. If two pages are described by the same node (e.g. if both a `sub/index.md` and a `sub.md` exist), the index page takes precedence. Pico's main index page (i.e. `index.md`) is represented by the tree's root node `/` and a special case: it is the only node of the `` (i.e. the empty string) branch.
A node is represented by an array with the keys `id`, `page` and `children`. The `id` key contains a string with the node's name. If the node represents a page, the `page` key is a reference to the page's data array. If the node is a inner node, the `children` key is a reference to its matching branch (i.e. a list of the node's children). The order of a node's children matches the order in Pico's pages array.
If you want to walk the whole page tree, start with the tree's root node at `$pageTree[""]["/"]`. The root node's `children` key is a reference to the `/` branch at `$pageTree["/"]`, that is a list of the root node's direct child nodes and their siblings.
You MUST NOT iterate the page tree itself (i.e. the list of the tree's branches), its order is undefined and the array will be replaced by a non-iterable data structure with Pico 3.0.
2017-11-18 17:34:57 +00:00
|
|
|
$pageData['tree_node'] = &$this->pageTree['']['/'];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a page's node and branch
|
|
|
|
$basename = basename($id);
|
|
|
|
$branch = dirname($id);
|
|
|
|
|
|
|
|
$isIndexPage = ($basename === 'index');
|
|
|
|
if ($isIndexPage) {
|
|
|
|
$basename = basename($branch);
|
|
|
|
$branch = dirname($branch);
|
|
|
|
}
|
|
|
|
|
|
|
|
$branch = ($branch !== '.') ? $branch : '/';
|
|
|
|
$node = ($branch !== '/') ? $branch . '/' . $basename : $basename;
|
|
|
|
|
|
|
|
// skip inaccessible pages (e.g. drop "sub.md" if "sub/index.md" exists)
|
|
|
|
if (isset($this->pageTree[$branch][$node]['page']) && !$isIndexPage) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add node to page tree
|
|
|
|
$isNewBranch = !isset($this->pageTree[$branch]);
|
|
|
|
$this->pageTree[$branch][$node]['id'] = $node;
|
|
|
|
$this->pageTree[$branch][$node]['page'] = &$pageData;
|
|
|
|
|
|
|
|
$pageData['tree_node'] = &$this->pageTree[$branch][$node];
|
|
|
|
|
|
|
|
// add parent nodes to page tree, if necessary
|
|
|
|
while ($isNewBranch && $branch) {
|
|
|
|
$parentNode = $branch;
|
|
|
|
$parentBranch = ($branch !== '/') ? dirname($parentNode) : '';
|
|
|
|
$parentBranch = ($parentBranch !== '.') ? $parentBranch : '/';
|
|
|
|
|
|
|
|
$isNewBranch = !isset($this->pageTree[$parentBranch]);
|
|
|
|
$this->pageTree[$parentBranch][$parentNode]['id'] = $parentNode;
|
|
|
|
$this->pageTree[$parentBranch][$parentNode]['children'] = &$this->pageTree[$branch];
|
|
|
|
|
|
|
|
$branch = $parentBranch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a tree structure of known pages
|
|
|
|
*
|
|
|
|
* @see Pico::buildPageTree()
|
|
|
|
*
|
|
|
|
* @return array[]|null the tree structure of all pages
|
|
|
|
*/
|
|
|
|
public function getPageTree()
|
|
|
|
{
|
|
|
|
return $this->pageTree;
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
2017-05-13 23:26:07 +00:00
|
|
|
* Returns the Twig template engine
|
2015-11-13 15:49:53 +00:00
|
|
|
*
|
2017-05-13 16:08:54 +00:00
|
|
|
* This method triggers the `onTwigRegistered` event when the Twig template
|
2017-05-13 23:26:07 +00:00
|
|
|
* engine wasn't initiated yet. When initiating Twig, this method also
|
2017-12-27 20:36:56 +00:00
|
|
|
* registers Pico's core Twig filter `content` as well as Pico's
|
|
|
|
* {@see PicoTwigExtension} Twig extension.
|
2017-05-13 16:08:54 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::getTwig()
|
|
|
|
* @see http://twig.sensiolabs.org/ Twig website
|
|
|
|
* @see https://github.com/twigphp/Twig Twig on GitHub
|
|
|
|
*
|
2015-11-12 14:33:48 +00:00
|
|
|
* @return Twig_Environment|null Twig template engine
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
public function getTwig()
|
|
|
|
{
|
2017-02-05 14:55:53 +00:00
|
|
|
if ($this->twig === null) {
|
2017-06-20 21:34:07 +00:00
|
|
|
$twigConfig = $this->getConfig('twig_config');
|
|
|
|
|
2017-05-13 23:26:07 +00:00
|
|
|
$twigLoader = new Twig_Loader_Filesystem($this->getThemesDir() . $this->getConfig('theme'));
|
2017-06-20 21:34:07 +00:00
|
|
|
$this->twig = new Twig_Environment($twigLoader, $twigConfig);
|
2017-05-13 23:26:07 +00:00
|
|
|
$this->twig->addExtension(new PicoTwigExtension($this));
|
|
|
|
|
2017-06-20 21:34:07 +00:00
|
|
|
if (!empty($twigConfig['debug'])) {
|
|
|
|
$this->twig->addExtension(new Twig_Extension_Debug());
|
|
|
|
}
|
|
|
|
|
2017-05-13 23:26:07 +00:00
|
|
|
// register content filter
|
|
|
|
// we pass the $pages array by reference to prevent multiple parser runs for the same page
|
|
|
|
// this is the reason why we can't register this filter as part of PicoTwigExtension
|
|
|
|
$pico = $this;
|
|
|
|
$pages = &$this->pages;
|
|
|
|
$this->twig->addFilter(new Twig_SimpleFilter('content', function ($page) use ($pico, &$pages) {
|
|
|
|
if (isset($pages[$page])) {
|
|
|
|
$pageData = &$pages[$page];
|
|
|
|
if (!isset($pageData['content'])) {
|
|
|
|
$pageData['content'] = $pico->prepareFileContent($pageData['raw_content'], $pageData['meta']);
|
|
|
|
$pageData['content'] = $pico->parseFileContent($pageData['content']);
|
|
|
|
}
|
|
|
|
return $pageData['content'];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}));
|
|
|
|
|
|
|
|
// trigger onTwigRegistration event
|
|
|
|
$this->triggerEvent('onTwigRegistered', array(&$this->twig));
|
2017-02-05 14:55:53 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
return $this->twig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the variables passed to the template
|
|
|
|
*
|
2019-04-30 13:30:00 +00:00
|
|
|
* URLs and paths don't add a trailing slash for historic reasons.
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
2016-01-25 18:31:53 +00:00
|
|
|
* @return array template variables
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected function getTwigVariables()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'config' => $this->getConfig(),
|
|
|
|
'base_url' => rtrim($this->getBaseUrl(), '/'),
|
2019-04-30 13:34:11 +00:00
|
|
|
'plugins_url' => rtrim($this->getConfig('plugins_url'), '/'),
|
|
|
|
'themes_url' => rtrim($this->getConfig('themes_url'), '/'),
|
|
|
|
'assets_url' => rtrim($this->getConfig('assets_url'), '/'),
|
|
|
|
'theme_url' => $this->getConfig('themes_url') . $this->getConfig('theme'),
|
2015-08-28 16:22:32 +00:00
|
|
|
'site_title' => $this->getConfig('site_title'),
|
|
|
|
'meta' => $this->meta,
|
|
|
|
'content' => $this->content,
|
|
|
|
'pages' => $this->pages,
|
2019-03-11 22:02:57 +00:00
|
|
|
'previous_page' => $this->previousPage,
|
2015-08-28 16:22:32 +00:00
|
|
|
'current_page' => $this->currentPage,
|
|
|
|
'next_page' => $this->nextPage,
|
2016-03-05 23:38:51 +00:00
|
|
|
'version' => static::VERSION
|
2015-08-28 16:22:32 +00:00
|
|
|
);
|
|
|
|
}
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2017-02-05 19:20:26 +00:00
|
|
|
/**
|
|
|
|
* Returns the name of the Twig template to render
|
|
|
|
*
|
|
|
|
* @return string template name
|
|
|
|
*/
|
|
|
|
protected function getTwigTemplate()
|
|
|
|
{
|
2017-10-12 14:03:41 +00:00
|
|
|
$templateName = !empty($this->meta['template']) ? $this->meta['template'] : 'index';
|
2017-11-25 20:52:03 +00:00
|
|
|
return $templateName . '.twig';
|
2017-02-05 19:20:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* Returns the base URL of this Pico instance
|
|
|
|
*
|
2016-11-23 22:26:55 +00:00
|
|
|
* Security Notice: You MUST configure Pico's base URL explicitly when
|
|
|
|
* using the base URL in contexts that are potentially vulnerable to
|
|
|
|
* HTTP Host Header Injection attacks (e.g. when generating emails).
|
|
|
|
*
|
2015-08-28 16:22:32 +00:00
|
|
|
* @return string the base url
|
|
|
|
*/
|
|
|
|
public function getBaseUrl()
|
|
|
|
{
|
2015-09-14 21:01:08 +00:00
|
|
|
$baseUrl = $this->getConfig('base_url');
|
2016-12-06 18:03:58 +00:00
|
|
|
if ($baseUrl) {
|
2015-09-14 21:01:08 +00:00
|
|
|
return $baseUrl;
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 23:51:35 +00:00
|
|
|
$host = 'localhost';
|
|
|
|
if (!empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
|
|
|
|
$host = $_SERVER['HTTP_X_FORWARDED_HOST'];
|
|
|
|
} elseif (!empty($_SERVER['HTTP_HOST'])) {
|
|
|
|
$host = $_SERVER['HTTP_HOST'];
|
|
|
|
} elseif (!empty($_SERVER['SERVER_NAME'])) {
|
|
|
|
$host = $_SERVER['SERVER_NAME'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$port = 80;
|
|
|
|
if (!empty($_SERVER['HTTP_X_FORWARDED_PORT'])) {
|
|
|
|
$port = (int) $_SERVER['HTTP_X_FORWARDED_PORT'];
|
|
|
|
} elseif (!empty($_SERVER['SERVER_PORT'])) {
|
|
|
|
$port = (int) $_SERVER['SERVER_PORT'];
|
|
|
|
}
|
|
|
|
|
2018-12-03 10:51:39 +00:00
|
|
|
$hostPortPosition = ($host[0] === '[') ? strpos($host, ':', strrpos($host, ']') ?: 0) : strrpos($host, ':');
|
2018-09-09 23:51:35 +00:00
|
|
|
if ($hostPortPosition !== false) {
|
|
|
|
$port = (int) substr($host, $hostPortPosition + 1);
|
2018-12-17 12:09:44 +00:00
|
|
|
$host = substr($host, 0, $hostPortPosition);
|
2018-09-09 23:51:35 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 18:28:48 +00:00
|
|
|
$protocol = 'http';
|
2016-04-17 00:44:41 +00:00
|
|
|
if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
|
|
|
|
$secureProxyHeader = strtolower(current(explode(',', $_SERVER['HTTP_X_FORWARDED_PROTO'])));
|
2017-11-18 17:28:02 +00:00
|
|
|
$protocol = in_array($secureProxyHeader, array('https', 'on', 'ssl', '1'), true) ? 'https' : 'http';
|
2016-04-17 00:44:41 +00:00
|
|
|
} elseif (!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] !== 'off')) {
|
2015-11-27 18:28:48 +00:00
|
|
|
$protocol = 'https';
|
2018-09-09 23:51:35 +00:00
|
|
|
} elseif ($port === 443) {
|
2015-11-27 18:28:48 +00:00
|
|
|
$protocol = 'https';
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 23:51:35 +00:00
|
|
|
$basePath = isset($_SERVER['SCRIPT_NAME']) ? dirname($_SERVER['SCRIPT_NAME']) : '/';
|
|
|
|
$basePath = !in_array($basePath, array('.', '/', '\\'), true) ? $basePath . '/' : '/';
|
2016-11-23 22:26:55 +00:00
|
|
|
|
2018-09-09 23:51:35 +00:00
|
|
|
if ((($protocol === 'http') && ($port !== 80)) || (($protocol === 'https') && ($port !== 443))) {
|
|
|
|
$host = $host . ':' . $port;
|
|
|
|
}
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2018-09-09 23:51:35 +00:00
|
|
|
$this->config['base_url'] = $protocol . "://" . $host . $basePath;
|
2016-03-05 23:29:40 +00:00
|
|
|
return $this->config['base_url'];
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-12 14:03:41 +00:00
|
|
|
* Returns TRUE if URL rewriting is enabled
|
2015-06-10 09:40:26 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @return bool TRUE if URL rewriting is enabled, FALSE otherwise
|
2015-06-10 09:40:26 +00:00
|
|
|
*/
|
2015-08-28 16:22:32 +00:00
|
|
|
public function isUrlRewritingEnabled()
|
2015-06-10 09:40:26 +00:00
|
|
|
{
|
2015-11-27 19:13:17 +00:00
|
|
|
$urlRewritingEnabled = $this->getConfig('rewrite_url');
|
|
|
|
if ($urlRewritingEnabled !== null) {
|
|
|
|
return $urlRewritingEnabled;
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 19:36:15 +00:00
|
|
|
if (isset($_SERVER['PICO_URL_REWRITING'])) {
|
|
|
|
$this->config['rewrite_url'] = (bool) $_SERVER['PICO_URL_REWRITING'];
|
|
|
|
} elseif (isset($_SERVER['REDIRECT_PICO_URL_REWRITING'])) {
|
|
|
|
$this->config['rewrite_url'] = (bool) $_SERVER['REDIRECT_PICO_URL_REWRITING'];
|
|
|
|
} else {
|
|
|
|
$this->config['rewrite_url'] = false;
|
|
|
|
}
|
|
|
|
|
2016-03-05 23:29:40 +00:00
|
|
|
return $this->config['rewrite_url'];
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:12:46 +00:00
|
|
|
/**
|
|
|
|
* Returns TRUE if Pico's debug mode is enabled
|
|
|
|
*
|
|
|
|
* @return bool TRUE if Pico's debug mode is enabled, FALSE otherwise
|
|
|
|
*/
|
|
|
|
public function isDebugModeEnabled()
|
|
|
|
{
|
|
|
|
$debugModeEnabled = $this->getConfig('debug');
|
|
|
|
if ($debugModeEnabled !== null) {
|
|
|
|
return $debugModeEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_SERVER['PICO_DEBUG'])) {
|
|
|
|
$this->config['debug'] = (bool) $_SERVER['PICO_DEBUG'];
|
|
|
|
} elseif (isset($_SERVER['REDIRECT_PICO_DEBUG'])) {
|
|
|
|
$this->config['debug'] = (bool) $_SERVER['REDIRECT_PICO_DEBUG'];
|
|
|
|
} else {
|
|
|
|
$this->config['debug'] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->config['debug'];
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* Returns the URL to a given page
|
|
|
|
*
|
2016-09-17 18:02:49 +00:00
|
|
|
* This method can be used in Twig templates by applying the `link` filter
|
2017-12-27 20:36:56 +00:00
|
|
|
* to a string representing a page ID.
|
2016-09-17 18:02:49 +00:00
|
|
|
*
|
2018-12-03 10:52:05 +00:00
|
|
|
* @param string $page ID of the page to link to
|
|
|
|
* @param array|string|null $queryData either an array of properties to
|
2015-12-13 21:01:25 +00:00
|
|
|
* create a URL-encoded query string from, or a already encoded string
|
2018-12-03 10:52:05 +00:00
|
|
|
* @param bool $dropIndex if the last path component is
|
|
|
|
* "index", passing TRUE (default) will remove this path component
|
2017-10-19 19:55:41 +00:00
|
|
|
*
|
|
|
|
* @return string URL
|
2019-04-30 13:30:00 +00:00
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException thrown when invalid arguments got passed
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
2016-07-13 22:24:06 +00:00
|
|
|
public function getPageUrl($page, $queryData = null, $dropIndex = true)
|
2015-08-28 16:22:32 +00:00
|
|
|
{
|
2015-12-13 21:01:25 +00:00
|
|
|
if (is_array($queryData)) {
|
|
|
|
$queryData = http_build_query($queryData, '', '&');
|
|
|
|
} elseif (($queryData !== null) && !is_string($queryData)) {
|
|
|
|
throw new InvalidArgumentException(
|
2019-04-30 13:30:00 +00:00
|
|
|
'Argument 2 passed to ' . __METHOD__ . ' must be of the type array or string, '
|
2015-12-13 21:01:25 +00:00
|
|
|
. (is_object($queryData) ? get_class($queryData) : gettype($queryData)) . ' given'
|
|
|
|
);
|
|
|
|
}
|
2016-04-23 19:40:30 +00:00
|
|
|
|
|
|
|
// drop "index"
|
2016-07-13 22:24:06 +00:00
|
|
|
if ($dropIndex) {
|
|
|
|
if ($page === 'index') {
|
|
|
|
$page = '';
|
|
|
|
} elseif (($pagePathLength = strrpos($page, '/')) !== false) {
|
|
|
|
if (substr($page, $pagePathLength + 1) === 'index') {
|
|
|
|
$page = substr($page, 0, $pagePathLength);
|
|
|
|
}
|
2016-04-23 19:40:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 18:03:58 +00:00
|
|
|
if ($queryData) {
|
|
|
|
$queryData = ($this->isUrlRewritingEnabled() || !$page) ? '?' . $queryData : '&' . $queryData;
|
2018-12-03 10:51:39 +00:00
|
|
|
} else {
|
|
|
|
$queryData = '';
|
2015-12-13 21:01:25 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 18:03:58 +00:00
|
|
|
if (!$page) {
|
2015-12-13 21:01:25 +00:00
|
|
|
return $this->getBaseUrl() . $queryData;
|
2015-11-08 13:01:35 +00:00
|
|
|
} elseif (!$this->isUrlRewritingEnabled()) {
|
2015-12-13 21:01:25 +00:00
|
|
|
return $this->getBaseUrl() . '?' . rawurlencode($page) . $queryData;
|
2015-11-08 13:01:35 +00:00
|
|
|
} else {
|
2015-12-13 21:01:25 +00:00
|
|
|
return $this->getBaseUrl() . implode('/', array_map('rawurlencode', explode('/', $page))) . $queryData;
|
2015-11-08 13:01:35 +00:00
|
|
|
}
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 22:36:14 +00:00
|
|
|
/**
|
|
|
|
* Returns the page ID of a given content file
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @param string $path path to the content file
|
|
|
|
*
|
|
|
|
* @return string|null either the corresponding page ID or NULL
|
2017-10-07 22:36:14 +00:00
|
|
|
*/
|
|
|
|
public function getPageId($path)
|
|
|
|
{
|
|
|
|
$contentDir = $this->getConfig('content_dir');
|
|
|
|
$contentDirLength = strlen($contentDir);
|
|
|
|
|
|
|
|
if (substr($path, 0, $contentDirLength) !== $contentDir) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-10-12 14:03:41 +00:00
|
|
|
|
|
|
|
$contentExt = $this->getConfig('content_ext');
|
|
|
|
$contentExtLength = strlen($contentExt);
|
|
|
|
|
2017-10-07 22:36:14 +00:00
|
|
|
if (substr($path, -$contentExtLength) !== $contentExt) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return substr($path, $contentDirLength, -$contentExtLength) ?: null;
|
|
|
|
}
|
|
|
|
|
2019-07-14 13:21:12 +00:00
|
|
|
/**
|
|
|
|
* Returns the URL of the themes folder of this Pico instance
|
|
|
|
*
|
2019-08-29 21:07:16 +00:00
|
|
|
* @see Pico::getUrlFromPath()
|
|
|
|
*
|
2019-07-14 13:21:12 +00:00
|
|
|
* @deprecated 3.0.0
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getBaseThemeUrl()
|
|
|
|
{
|
|
|
|
return $this->getConfig('themes_url');
|
|
|
|
}
|
|
|
|
|
2016-04-23 23:22:43 +00:00
|
|
|
/**
|
2019-04-30 13:26:31 +00:00
|
|
|
* Returns the URL of a given absolute path within this Pico instance
|
2016-04-23 23:22:43 +00:00
|
|
|
*
|
2019-04-30 13:26:31 +00:00
|
|
|
* We assume that the given path is a arbitrary deep sub folder of the
|
2016-04-23 23:22:43 +00:00
|
|
|
* script's base path (i.e. the directory {@path "index.php"} is in resp.
|
2019-04-30 13:26:31 +00:00
|
|
|
* the `httpdocs` directory). If this isn't the case, we check whether it's
|
|
|
|
* a sub folder of {@see Pico::$rootDir} (what is often identical to the
|
|
|
|
* script's base path). If this isn't the case either, we fall back to
|
|
|
|
* the basename of the given folder. This whole process ultimately allows
|
|
|
|
* us to use {@see Pico::getBaseUrl()} as origin for the URL.
|
|
|
|
*
|
|
|
|
* This method is used to guess Pico's `plugins_url`, `themes_url` and
|
|
|
|
* `assets_url`. However, guessing might fail, requiring a manual config.
|
|
|
|
*
|
2019-08-29 21:07:16 +00:00
|
|
|
* @param string $absolutePath the absolute path to interpret
|
2019-04-30 13:26:31 +00:00
|
|
|
*
|
|
|
|
* @return string the URL of the given folder
|
|
|
|
*/
|
|
|
|
public function getUrlFromPath($absolutePath)
|
2016-04-23 23:22:43 +00:00
|
|
|
{
|
2019-04-30 14:55:47 +00:00
|
|
|
$absolutePath = str_replace('\\', '/', $absolutePath);
|
|
|
|
|
2019-04-30 13:26:31 +00:00
|
|
|
$basePath = '';
|
|
|
|
if (isset($_SERVER['SCRIPT_FILENAME']) && strrpos($_SERVER['SCRIPT_FILENAME'], '/')) {
|
2018-09-09 23:51:35 +00:00
|
|
|
$basePath = dirname($_SERVER['SCRIPT_FILENAME']);
|
|
|
|
$basePath = !in_array($basePath, array('.', '/', '\\'), true) ? $basePath . '/' : '/';
|
|
|
|
$basePathLength = strlen($basePath);
|
|
|
|
|
2019-04-30 13:26:31 +00:00
|
|
|
if ((substr($absolutePath, 0, $basePathLength) === $basePath) && ($basePath !== '/')) {
|
|
|
|
return $this->getBaseUrl() . substr($absolutePath, $basePathLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($basePath !== $this->getRootDir()) {
|
|
|
|
$basePath = $this->getRootDir();
|
|
|
|
$basePathLength = strlen($basePath);
|
|
|
|
|
|
|
|
if (substr($absolutePath, 0, $basePathLength) === $basePath) {
|
|
|
|
return $this->getBaseUrl() . substr($absolutePath, $basePathLength);
|
2018-09-09 23:51:35 +00:00
|
|
|
}
|
2016-04-23 23:22:43 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 13:26:31 +00:00
|
|
|
return $this->getBaseUrl() . basename($absolutePath) . '/';
|
2016-04-23 23:22:43 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 18:09:04 +00:00
|
|
|
/**
|
|
|
|
* Filters a URL GET parameter with a specified filter
|
|
|
|
*
|
2017-05-13 16:17:58 +00:00
|
|
|
* This method is just an alias for {@see Pico::filterVariable()}, see
|
|
|
|
* {@see Pico::filterVariable()} for a detailed description. It can be
|
2016-09-17 18:09:04 +00:00
|
|
|
* used in Twig templates by calling the `url_param` function.
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::filterVariable()
|
|
|
|
*
|
|
|
|
* @param string $name name of the URL GET parameter
|
2016-09-17 18:09:04 +00:00
|
|
|
* to filter
|
2017-10-19 19:55:41 +00:00
|
|
|
* @param int|string $filter the filter to apply
|
|
|
|
* @param mixed|array $options either a associative options
|
2016-09-17 18:09:04 +00:00
|
|
|
* array to be used by the filter or a scalar default value
|
2017-10-19 19:55:41 +00:00
|
|
|
* @param int|string|int[]|string[] $flags flags and flag strings to be
|
|
|
|
* used by the filter
|
|
|
|
*
|
|
|
|
* @return mixed either the filtered data, FALSE if the filter fails, or
|
|
|
|
* NULL if the URL GET parameter doesn't exist and no default value is
|
|
|
|
* given
|
2016-09-17 18:09:04 +00:00
|
|
|
*/
|
|
|
|
public function getUrlParameter($name, $filter = '', $options = null, $flags = null)
|
|
|
|
{
|
|
|
|
$variable = (isset($_GET[$name]) && is_scalar($_GET[$name])) ? $_GET[$name] : null;
|
|
|
|
return $this->filterVariable($variable, $filter, $options, $flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters a HTTP POST parameter with a specified filter
|
|
|
|
*
|
2017-05-13 16:17:58 +00:00
|
|
|
* This method is just an alias for {@see Pico::filterVariable()}, see
|
|
|
|
* {@see Pico::filterVariable()} for a detailed description. It can be
|
2016-09-17 18:09:04 +00:00
|
|
|
* used in Twig templates by calling the `form_param` function.
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see Pico::filterVariable()
|
|
|
|
*
|
|
|
|
* @param string $name name of the HTTP POST
|
2016-09-17 18:09:04 +00:00
|
|
|
* parameter to filter
|
2017-10-19 19:55:41 +00:00
|
|
|
* @param int|string $filter the filter to apply
|
|
|
|
* @param mixed|array $options either a associative options
|
2016-09-17 18:09:04 +00:00
|
|
|
* array to be used by the filter or a scalar default value
|
2017-10-19 19:55:41 +00:00
|
|
|
* @param int|string|int[]|string[] $flags flags and flag strings to be
|
|
|
|
* used by the filter
|
|
|
|
*
|
|
|
|
* @return mixed either the filtered data, FALSE if the filter fails, or
|
|
|
|
* NULL if the HTTP POST parameter doesn't exist and no default value
|
|
|
|
* is given
|
2016-09-17 18:09:04 +00:00
|
|
|
*/
|
|
|
|
public function getFormParameter($name, $filter = '', $options = null, $flags = null)
|
|
|
|
{
|
|
|
|
$variable = (isset($_POST[$name]) && is_scalar($_POST[$name])) ? $_POST[$name] : null;
|
|
|
|
return $this->filterVariable($variable, $filter, $options, $flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters a variable with a specified filter
|
|
|
|
*
|
|
|
|
* This method basically wraps around PHP's `filter_var()` function. It
|
|
|
|
* filters data by either validating or sanitizing it. This is especially
|
|
|
|
* useful when the data source contains unknown (or foreign) data, like
|
|
|
|
* user supplied input. Validation is used to validate or check if the data
|
|
|
|
* meets certain qualifications, but will not change the data itself.
|
|
|
|
* Sanitization will sanitize the data, so it may alter it by removing
|
|
|
|
* undesired characters. It doesn't actually validate the data! The
|
|
|
|
* behaviour of most filters can optionally be tweaked by flags.
|
|
|
|
*
|
|
|
|
* Heads up! Input validation is hard! Always validate your input data the
|
|
|
|
* most paranoid way you can imagine. Always prefer validation filters over
|
|
|
|
* sanitization filters; be very careful with sanitization filters, you
|
|
|
|
* might create cross-site scripting vulnerabilities!
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see https://secure.php.net/manual/en/function.filter-var.php
|
2016-09-17 18:09:04 +00:00
|
|
|
* PHP's `filter_var()` function
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see https://secure.php.net/manual/en/filter.filters.validate.php
|
2016-09-17 18:09:04 +00:00
|
|
|
* Validate filters
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see https://secure.php.net/manual/en/filter.filters.sanitize.php
|
2016-09-17 18:09:04 +00:00
|
|
|
* Sanitize filters
|
2017-10-19 19:55:41 +00:00
|
|
|
*
|
|
|
|
* @param mixed $variable value to filter
|
|
|
|
* @param int|string $filter ID (int) or name (string) of
|
2016-09-17 18:09:04 +00:00
|
|
|
* the filter to apply; if omitted, the method will return FALSE
|
2017-10-19 19:55:41 +00:00
|
|
|
* @param mixed|array $options either a associative array
|
2016-09-17 18:09:04 +00:00
|
|
|
* of options to be used by the filter (e.g. `array('default' => 42)`),
|
|
|
|
* or a scalar default value that will be returned when the passed
|
|
|
|
* value is NULL (optional)
|
2017-10-19 19:55:41 +00:00
|
|
|
* @param int|string|int[]|string[] $flags either a bitwise disjunction
|
2016-09-17 18:09:04 +00:00
|
|
|
* of flags or a string with the significant part of a flag constant
|
|
|
|
* (the constant name is the result of "FILTER_FLAG_" and the given
|
|
|
|
* string in ASCII-only uppercase); you may also pass an array of flags
|
|
|
|
* and flag strings (optional)
|
2017-10-19 19:55:41 +00:00
|
|
|
*
|
|
|
|
* @return mixed with a validation filter, the method either returns the
|
|
|
|
* validated value or, provided that the value wasn't valid, the given
|
|
|
|
* default value or FALSE; with a sanitization filter, the method
|
|
|
|
* returns the sanitized value; if no value (i.e. NULL) was given, the
|
|
|
|
* method always returns either the provided default value or NULL
|
2016-09-17 18:09:04 +00:00
|
|
|
*/
|
|
|
|
protected function filterVariable($variable, $filter = '', $options = null, $flags = null)
|
|
|
|
{
|
|
|
|
$defaultValue = null;
|
|
|
|
if (is_array($options)) {
|
|
|
|
$defaultValue = isset($options['default']) ? $options['default'] : null;
|
|
|
|
} elseif ($options !== null) {
|
|
|
|
$defaultValue = $options;
|
|
|
|
$options = array('default' => $defaultValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($variable === null) {
|
|
|
|
return $defaultValue;
|
|
|
|
}
|
|
|
|
|
2016-12-06 18:03:58 +00:00
|
|
|
$filter = $filter ? (is_string($filter) ? filter_id($filter) : (int) $filter) : false;
|
2016-09-17 18:09:04 +00:00
|
|
|
if (!$filter) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$filterOptions = array('options' => $options, 'flags' => 0);
|
|
|
|
foreach ((array) $flags as $flag) {
|
|
|
|
if (is_numeric($flag)) {
|
|
|
|
$filterOptions['flags'] |= (int) $flag;
|
|
|
|
} elseif (is_string($flag)) {
|
|
|
|
$flag = strtoupper(preg_replace('/[^a-zA-Z0-9_]/', '', $flag));
|
2016-12-06 18:03:58 +00:00
|
|
|
if (($flag === 'NULL_ON_FAILURE') && ($filter === FILTER_VALIDATE_BOOLEAN)) {
|
2016-09-17 18:09:04 +00:00
|
|
|
$filterOptions['flags'] |= FILTER_NULL_ON_FAILURE;
|
|
|
|
} else {
|
|
|
|
$filterOptions['flags'] |= (int) constant('FILTER_FLAG_' . $flag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filter_var($variable, $filter, $filterOptions);
|
|
|
|
}
|
|
|
|
|
2015-06-10 09:40:26 +00:00
|
|
|
/**
|
2015-08-28 16:22:32 +00:00
|
|
|
* Recursively walks through a directory and returns all containing files
|
2015-10-01 19:54:30 +00:00
|
|
|
* matching the specified file extension
|
2015-06-10 09:40:26 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @param string $directory start directory
|
|
|
|
* @param string $fileExtension return files with the given file extension
|
2015-09-13 18:46:09 +00:00
|
|
|
* only (optional)
|
2017-10-19 19:55:41 +00:00
|
|
|
* @param int $order specify whether and how files should be
|
2015-10-04 12:15:11 +00:00
|
|
|
* sorted; use Pico::SORT_ASC for a alphabetical ascending order (this
|
|
|
|
* is the default behaviour), Pico::SORT_DESC for a descending order
|
|
|
|
* or Pico::SORT_NONE to leave the result unsorted
|
2017-10-19 19:55:41 +00:00
|
|
|
*
|
|
|
|
* @return array list of found files
|
2015-06-10 09:40:26 +00:00
|
|
|
*/
|
2016-07-13 22:20:22 +00:00
|
|
|
public function getFiles($directory, $fileExtension = '', $order = self::SORT_ASC)
|
2015-06-10 09:40:26 +00:00
|
|
|
{
|
2015-08-28 16:22:32 +00:00
|
|
|
$directory = rtrim($directory, '/');
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
// scandir() reads files in alphabetical order
|
2015-10-01 19:54:30 +00:00
|
|
|
$files = scandir($directory, $order);
|
2015-08-28 16:22:32 +00:00
|
|
|
$fileExtensionLength = strlen($fileExtension);
|
|
|
|
if ($files !== false) {
|
2015-07-14 11:31:51 +00:00
|
|
|
foreach ($files as $file) {
|
2015-08-28 16:22:32 +00:00
|
|
|
// exclude hidden files/dirs starting with a .; this also excludes the special dirs . and ..
|
|
|
|
// exclude files ending with a ~ (vim/nano backup) or # (emacs backup)
|
2017-11-18 17:28:02 +00:00
|
|
|
if (($file[0] === '.') || in_array(substr($file, -1), array('~', '#'), true)) {
|
2015-06-10 09:40:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-08-28 16:22:32 +00:00
|
|
|
|
|
|
|
if (is_dir($directory . '/' . $file)) {
|
|
|
|
// get files recursively
|
2015-10-04 12:15:11 +00:00
|
|
|
$result = array_merge($result, $this->getFiles($directory . '/' . $file, $fileExtension, $order));
|
2016-12-06 18:03:58 +00:00
|
|
|
} elseif (!$fileExtension || (substr($file, -$fileExtensionLength) === $fileExtension)) {
|
2015-08-28 16:22:32 +00:00
|
|
|
$result[] = $directory . '/' . $file;
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
return $result;
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 19:36:46 +00:00
|
|
|
/**
|
|
|
|
* Returns all files in a directory matching a libc glob() pattern
|
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see https://secure.php.net/manual/en/function.glob.php
|
2016-12-12 19:36:46 +00:00
|
|
|
* PHP's glob() function
|
2017-10-19 19:55:41 +00:00
|
|
|
*
|
|
|
|
* @param string $pattern the pattern to search for; see PHP's glob()
|
2016-12-12 19:36:46 +00:00
|
|
|
* function for details
|
2017-10-19 19:55:41 +00:00
|
|
|
* @param int $order specify whether and how files should be sorted;
|
2016-12-12 19:36:46 +00:00
|
|
|
* use Pico::SORT_ASC for a alphabetical ascending order (this is the
|
|
|
|
* default behaviour), Pico::SORT_DESC for a descending order or
|
|
|
|
* Pico::SORT_NONE to leave the result unsorted
|
2017-10-19 19:55:41 +00:00
|
|
|
*
|
|
|
|
* @return array list of found files
|
2016-12-12 19:36:46 +00:00
|
|
|
*/
|
|
|
|
public function getFilesGlob($pattern, $order = self::SORT_ASC)
|
|
|
|
{
|
|
|
|
$result = array();
|
|
|
|
$sortFlag = ($order === self::SORT_NONE) ? GLOB_NOSORT : 0;
|
|
|
|
|
|
|
|
$files = glob($pattern, GLOB_MARK | $sortFlag);
|
|
|
|
if ($files) {
|
|
|
|
foreach ($files as $file) {
|
|
|
|
// exclude dirs and files ending with a ~ (vim/nano backup) or # (emacs backup)
|
2017-11-18 17:28:02 +00:00
|
|
|
if (in_array(substr($file, -1), array('/', '~', '#'), true)) {
|
2016-12-12 19:36:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result[] = $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($order === self::SORT_DESC) ? array_reverse($result) : $result;
|
|
|
|
}
|
|
|
|
|
2015-10-01 13:14:45 +00:00
|
|
|
/**
|
2015-11-04 18:50:44 +00:00
|
|
|
* Makes a relative path absolute to Pico's root dir
|
2015-10-01 13:14:45 +00:00
|
|
|
*
|
2019-03-28 18:14:23 +00:00
|
|
|
* @param string $path relative or absolute path
|
|
|
|
* @param string $basePath treat relative paths relative to the given path;
|
|
|
|
* defaults to Pico::$rootDir
|
2019-04-30 13:30:00 +00:00
|
|
|
* @param bool $endSlash whether to add a trailing slash to the absolute
|
|
|
|
* path or not (defaults to TRUE)
|
2017-10-19 19:55:41 +00:00
|
|
|
*
|
|
|
|
* @return string absolute path
|
2015-10-01 13:14:45 +00:00
|
|
|
*/
|
2019-04-30 13:30:00 +00:00
|
|
|
public function getAbsolutePath($path, $basePath = null, $endSlash = true)
|
2015-10-01 13:14:45 +00:00
|
|
|
{
|
2019-03-28 18:14:23 +00:00
|
|
|
if ($basePath === null) {
|
|
|
|
$basePath = $this->getRootDir();
|
|
|
|
}
|
|
|
|
|
2017-02-19 20:53:29 +00:00
|
|
|
if (DIRECTORY_SEPARATOR === '\\') {
|
2017-10-07 22:36:29 +00:00
|
|
|
if (preg_match('/^(?>[a-zA-Z]:\\\\|\\\\\\\\)/', $path) !== 1) {
|
2019-03-28 18:14:23 +00:00
|
|
|
$path = $basePath . $path;
|
2015-12-23 15:17:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-03-05 23:29:40 +00:00
|
|
|
if ($path[0] !== '/') {
|
2019-03-28 18:14:23 +00:00
|
|
|
$path = $basePath . $path;
|
2015-12-23 15:17:06 +00:00
|
|
|
}
|
2015-10-01 13:14:45 +00:00
|
|
|
}
|
2019-03-28 18:14:23 +00:00
|
|
|
|
2019-04-30 13:30:00 +00:00
|
|
|
return rtrim($path, '/\\') . ($endSlash ? '/' : '');
|
2015-10-01 13:14:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 13:22:49 +00:00
|
|
|
/**
|
|
|
|
* Normalizes a path by taking care of '', '.' and '..' parts
|
|
|
|
*
|
|
|
|
* @param string $path path to normalize
|
|
|
|
* @param bool $allowsAbsolutePath whether absolute paths are allowed
|
|
|
|
* @param bool $endSlash whether to add a trailing slash to the
|
|
|
|
* normalized path or not (defaults to TRUE)
|
|
|
|
*
|
|
|
|
* @return string normalized path
|
|
|
|
*
|
|
|
|
* @throws UnexpectedValueException thrown when a absolute path is passed
|
|
|
|
* although absolute paths aren't allowed
|
|
|
|
*/
|
|
|
|
public function getNormalizedPath($path, $allowsAbsolutePath = false, $endSlash = true)
|
|
|
|
{
|
|
|
|
$absolutePath = '';
|
|
|
|
if (DIRECTORY_SEPARATOR === '\\') {
|
|
|
|
if (preg_match('/^(?>[a-zA-Z]:\\\\|\\\\\\\\)/', $path, $pathMatches) === 1) {
|
|
|
|
$absolutePath = $pathMatches[0];
|
|
|
|
$path = substr($path, strlen($absolutePath));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($path[0] === '/') {
|
|
|
|
$absolutePath = '/';
|
|
|
|
$path = substr($path, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($absolutePath && !$allowsAbsolutePath) {
|
|
|
|
throw new UnexpectedValueException(
|
|
|
|
'Argument 1 passed to ' . __METHOD__ . ' must be a relative path, absolute path "' . $path . '" given'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$path = str_replace('\\', '/', $path);
|
|
|
|
$pathParts = explode('/', $path);
|
|
|
|
|
|
|
|
$resultParts = array();
|
|
|
|
foreach ($pathParts as $pathPart) {
|
|
|
|
if (($pathPart === '') || ($pathPart === '.')) {
|
|
|
|
continue;
|
|
|
|
} elseif ($pathPart === '..') {
|
|
|
|
array_pop($resultParts);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$resultParts[] = $pathPart;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$resultParts) {
|
|
|
|
return $absolutePath ?: '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $absolutePath . implode('/', $resultParts) . ($endSlash ? '/' : '');
|
|
|
|
}
|
|
|
|
|
2019-04-30 13:26:31 +00:00
|
|
|
/**
|
|
|
|
* Makes a relative URL absolute to Pico's base URL
|
|
|
|
*
|
|
|
|
* Please note that URLs starting with a slash are considered absolute URLs
|
|
|
|
* even though they don't include a scheme and host.
|
|
|
|
*
|
|
|
|
* @param string $url relative or absolute URL
|
|
|
|
* @param string $baseUrl treat relative URLs relative to the given URL;
|
|
|
|
* defaults to Pico::getBaseUrl()
|
|
|
|
* @param bool $endSlash whether to add a trailing slash to the absolute
|
|
|
|
* URL or not (defaults to TRUE)
|
|
|
|
*
|
|
|
|
* @return string absolute URL
|
|
|
|
*/
|
|
|
|
public function getAbsoluteUrl($url, $baseUrl = null, $endSlash = true)
|
|
|
|
{
|
|
|
|
if ($baseUrl === null) {
|
|
|
|
$baseUrl = $this->getBaseUrl();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($url[0] !== '/') && !preg_match('#^[A-Za-z][A-Za-z0-9+\-.]*://#', $url)) {
|
|
|
|
$url = $baseUrl . $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rtrim($url, '/') . ($endSlash ? '/' : '');
|
|
|
|
}
|
|
|
|
|
2015-06-10 09:40:26 +00:00
|
|
|
/**
|
2017-05-10 15:00:48 +00:00
|
|
|
* Triggers events on plugins using the current API version
|
|
|
|
*
|
|
|
|
* Plugins using older API versions are handled by {@see PicoDeprecated}.
|
|
|
|
* Please note that {@see PicoDeprecated} also triggers custom events on
|
|
|
|
* plugins using older API versions, thus you can safely use this method
|
|
|
|
* to trigger custom events on all loaded plugins, no matter what API
|
2017-06-14 15:08:33 +00:00
|
|
|
* version - the event will be triggered in any case.
|
2015-06-10 09:40:26 +00:00
|
|
|
*
|
2016-03-02 21:10:49 +00:00
|
|
|
* You MUST NOT trigger events of Pico's core with a plugin!
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
2017-10-19 19:55:41 +00:00
|
|
|
* @see PicoPluginInterface
|
|
|
|
* @see AbstractPicoPlugin
|
|
|
|
* @see DummyPlugin
|
|
|
|
*
|
|
|
|
* @param string $eventName name of the event to trigger
|
|
|
|
* @param array $params optional parameters to pass
|
|
|
|
*
|
2015-08-28 16:22:32 +00:00
|
|
|
* @return void
|
2015-06-10 09:40:26 +00:00
|
|
|
*/
|
2016-02-29 18:51:06 +00:00
|
|
|
public function triggerEvent($eventName, array $params = array())
|
2015-06-10 09:40:26 +00:00
|
|
|
{
|
2017-05-10 15:00:48 +00:00
|
|
|
foreach ($this->nativePlugins as $plugin) {
|
|
|
|
$plugin->handleEvent($eventName, $params);
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-04 13:45:09 +00:00
|
|
|
}
|