2012-11-21 12:35:53 +00:00
|
|
|
<?php
|
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
|
2016-04-23 23:22:43 +00:00
|
|
|
* @version 1.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
|
|
|
|
*/
|
|
|
|
const VERSION = '1.1.0-dev';
|
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
|
|
|
|
*/
|
|
|
|
const VERSION_ID = 10100;
|
2016-03-05 23:38:51 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
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()
|
2015-10-27 00:48:58 +00:00
|
|
|
* @var object[]|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected $plugins;
|
2015-06-10 09:40:26 +00:00
|
|
|
|
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()
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $is404Content = false;
|
|
|
|
|
2016-12-06 19:22:38 +00:00
|
|
|
/**
|
|
|
|
* Symfony YAML instance used for meta header parsing
|
|
|
|
*
|
|
|
|
* @see Pico::registerYamlParser()
|
|
|
|
* @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()
|
|
|
|
* @var ParsedownExtra|null
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2015-10-29 01:55:30 +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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a new Pico instance
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* To carry out all the processing in Pico, call {@link Pico::run()}.
|
2015-10-27 00:39:28 +00:00
|
|
|
*
|
|
|
|
* @param string $rootDir root directory of this Pico instance
|
|
|
|
* @param string $configDir config directory of this Pico instance
|
|
|
|
* @param string $pluginsDir plugins directory of this Pico instance
|
|
|
|
* @param string $themesDir themes directory of this Pico instance
|
2015-10-01 13:05:50 +00:00
|
|
|
*/
|
|
|
|
public function __construct($rootDir, $configDir, $pluginsDir, $themesDir)
|
|
|
|
{
|
2015-12-23 15:17:06 +00:00
|
|
|
$this->rootDir = rtrim($rootDir, '/\\') . '/';
|
2015-10-01 13:14:45 +00:00
|
|
|
$this->configDir = $this->getAbsolutePath($configDir);
|
|
|
|
$this->pluginsDir = $this->getAbsolutePath($pluginsDir);
|
|
|
|
$this->themesDir = $this->getAbsolutePath($themesDir);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2016-12-06 18:03:58 +00:00
|
|
|
* @return string rendered Pico contents
|
2015-12-13 21:19:02 +00:00
|
|
|
* @throws Exception thrown when a not recoverable 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();
|
2015-08-28 16:22:32 +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
|
|
|
|
$this->triggerEvent('onContentLoading', array(&$this->requestFile));
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2016-04-24 02:06:04 +00:00
|
|
|
$notFoundFile = '404' . $this->getConfig('content_ext');
|
|
|
|
if (file_exists($this->requestFile) && (basename($this->requestFile) !== $notFoundFile)) {
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->rawContent = $this->loadFileContent($this->requestFile);
|
2015-06-10 09:40:26 +00:00
|
|
|
} else {
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->triggerEvent('on404ContentLoading', array(&$this->requestFile));
|
|
|
|
|
|
|
|
header($_SERVER['SERVER_PROTOCOL'] . ' 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));
|
|
|
|
|
|
|
|
// parse file meta
|
2016-12-06 19:22:38 +00:00
|
|
|
$this->triggerEvent('onYamlParserRegistration');
|
|
|
|
$this->registerYamlParser();
|
|
|
|
|
2016-12-06 19:01:38 +00:00
|
|
|
$this->metaHeaders = $this->getMetaHeaders();
|
|
|
|
$this->triggerEvent('onMetaHeaders', array(&$this->metaHeaders));
|
2015-08-28 16:22:32 +00:00
|
|
|
|
2016-12-06 19:01:38 +00:00
|
|
|
$this->triggerEvent('onMetaParsing', array(&$this->rawContent, &$this->metaHeaders));
|
|
|
|
$this->meta = $this->parseFileMeta($this->rawContent, $this->metaHeaders);
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->triggerEvent('onMetaParsed', array(&$this->meta));
|
|
|
|
|
2015-11-12 14:33:48 +00:00
|
|
|
// register parsedown
|
|
|
|
$this->triggerEvent('onParsedownRegistration');
|
|
|
|
$this->registerParsedown();
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
// parse file content
|
|
|
|
$this->triggerEvent('onContentParsing', array(&$this->rawContent));
|
|
|
|
|
2015-11-06 00:08:31 +00:00
|
|
|
$this->content = $this->prepareFileContent($this->rawContent, $this->meta);
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->triggerEvent('onContentPrepared', array(&$this->content));
|
|
|
|
|
|
|
|
$this->content = $this->parseFileContent($this->content);
|
|
|
|
$this->triggerEvent('onContentParsed', array(&$this->content));
|
|
|
|
|
|
|
|
// read pages
|
|
|
|
$this->triggerEvent('onPagesLoading');
|
|
|
|
|
|
|
|
$this->readPages();
|
2015-10-04 20:39:38 +00:00
|
|
|
$this->sortPages();
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->discoverCurrentPage();
|
|
|
|
|
|
|
|
$this->triggerEvent('onPagesLoaded', array(
|
|
|
|
&$this->pages,
|
|
|
|
&$this->currentPage,
|
|
|
|
&$this->previousPage,
|
|
|
|
&$this->nextPage
|
|
|
|
));
|
|
|
|
|
|
|
|
// register twig
|
|
|
|
$this->triggerEvent('onTwigRegistration');
|
|
|
|
$this->registerTwig();
|
|
|
|
|
|
|
|
// render template
|
|
|
|
$this->twigVariables = $this->getTwigVariables();
|
2016-12-06 18:03:58 +00:00
|
|
|
if (!empty($this->meta['template'])) {
|
2015-08-28 16:22:32 +00:00
|
|
|
$templateName = $this->meta['template'];
|
2015-06-10 09:40:26 +00:00
|
|
|
} else {
|
2015-08-28 16:22:32 +00:00
|
|
|
$templateName = 'index';
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
2015-10-01 13:05:50 +00:00
|
|
|
if (file_exists($this->getThemesDir() . $this->getConfig('theme') . '/' . $templateName . '.twig')) {
|
2015-08-28 16:22:32 +00:00
|
|
|
$templateName .= '.twig';
|
2015-06-10 09:40:26 +00:00
|
|
|
} else {
|
2015-08-28 16:22:32 +00:00
|
|
|
$templateName .= '.html';
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
2015-08-28 16:22:32 +00:00
|
|
|
|
|
|
|
$this->triggerEvent('onPageRendering', array(&$this->twig, &$this->twigVariables, &$templateName));
|
|
|
|
|
|
|
|
$output = $this->twig->render($templateName, $this->twigVariables);
|
|
|
|
$this->triggerEvent('onPageRendered', array(&$output));
|
|
|
|
|
2015-10-01 13:05:50 +00:00
|
|
|
return $output;
|
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
|
|
|
*
|
2015-11-12 14:33:48 +00:00
|
|
|
* Plugin files MAY be prefixed by a number (e.g. 00-PicoDeprecated.php)
|
|
|
|
* to indicate their processing order. Plugins without a prefix will be
|
|
|
|
* loaded last. If you want to use a prefix, you MUST consider the
|
|
|
|
* following directives:
|
|
|
|
* - 00 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
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
2016-02-29 18:50:35 +00:00
|
|
|
* @see Pico::loadPlugin()
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getPlugin()
|
|
|
|
* @see Pico::getPlugins()
|
2015-08-28 16:22:32 +00:00
|
|
|
* @return void
|
|
|
|
* @throws RuntimeException thrown when a plugin couldn't be loaded
|
|
|
|
*/
|
|
|
|
protected function loadPlugins()
|
|
|
|
{
|
2016-03-06 19:06:24 +00:00
|
|
|
// scope isolated require_once()
|
|
|
|
$includeClosure = function ($pluginFile) {
|
|
|
|
require_once($pluginFile);
|
|
|
|
};
|
|
|
|
if (PHP_VERSION_ID >= 50400) {
|
|
|
|
$includeClosure = $includeClosure->bindTo(null);
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->plugins = array();
|
2015-10-01 13:05:50 +00:00
|
|
|
$pluginFiles = $this->getFiles($this->getPluginsDir(), '.php');
|
2015-08-28 16:22:32 +00:00
|
|
|
foreach ($pluginFiles as $pluginFile) {
|
2016-03-06 19:06:24 +00:00
|
|
|
$includeClosure($pluginFile);
|
2015-08-28 16:22:32 +00:00
|
|
|
|
|
|
|
$className = preg_replace('/^[0-9]+-/', '', basename($pluginFile, '.php'));
|
|
|
|
if (class_exists($className)) {
|
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;
|
2015-08-28 16:22:32 +00:00
|
|
|
} else {
|
|
|
|
// TODO: breaks backward compatibility
|
2016-03-02 21:10:49 +00:00
|
|
|
/*
|
|
|
|
$pluginFileName = substr($pluginFile, strlen($this->getPluginsDir()));
|
|
|
|
throw new RuntimeException(
|
|
|
|
"Unable to load plugin '" . $className . "' "
|
|
|
|
. "from '" . $pluginFileName . "'"
|
|
|
|
);
|
|
|
|
*/
|
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
|
|
|
|
*
|
|
|
|
* Manually loaded plugins must implement {@see PicoPluginInterface}.
|
|
|
|
*
|
|
|
|
* @see Pico::loadPlugins()
|
|
|
|
* @see Pico::getPlugin()
|
|
|
|
* @see Pico::getPlugins()
|
|
|
|
* @param PicoPluginInterface|string $plugin either the class name of a
|
|
|
|
* plugin to instantiate or a plugin instance
|
|
|
|
* @return PicoPluginInterface instance of the loaded plugin
|
|
|
|
* @throws RuntimeException thrown when a plugin couldn't
|
|
|
|
* be loaded
|
|
|
|
*/
|
|
|
|
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);
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->plugins === null) {
|
|
|
|
$this->plugins = array();
|
|
|
|
}
|
|
|
|
$this->plugins[$className] = $plugin;
|
|
|
|
|
|
|
|
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`).
|
|
|
|
*
|
|
|
|
* @see Pico::loadPlugins()
|
|
|
|
* @see Pico::getPlugins()
|
|
|
|
* @see https://github.com/marcj/topsort.php
|
|
|
|
* Marc J. Schmidt's Topological Sort / Dependency resolver in PHP
|
|
|
|
* @see https://github.com/marcj/topsort.php/blob/1.1.0/src/Implementations/ArraySort.php
|
|
|
|
* \MJS\TopSort\Implementations\ArraySort class
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function sortPlugins()
|
|
|
|
{
|
|
|
|
$plugins = $this->plugins;
|
|
|
|
$sortedPlugins = array();
|
|
|
|
$visitedPlugins = array();
|
|
|
|
$visitPlugin = function ($plugin) use ($plugins, &$sortedPlugins, &$visitedPlugins, &$visitPlugin) {
|
|
|
|
$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();
|
|
|
|
} else {
|
|
|
|
$dependencies = array('PicoDeprecated');
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach ($this->plugins as $plugin) {
|
|
|
|
$visitPlugin($plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->plugins = $sortedPlugins;
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* Returns the instance of a named plugin
|
|
|
|
*
|
2015-09-15 11:15:45 +00:00
|
|
|
* Plugins SHOULD implement {@link PicoPluginInterface}, but you MUST NOT
|
|
|
|
* rely on it. For more information see {@link PicoPluginInterface}.
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
|
|
|
* @see Pico::loadPlugins()
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getPlugins()
|
2015-08-28 16:22:32 +00:00
|
|
|
* @param string $pluginName name of the plugin
|
|
|
|
* @return object instance of the plugin
|
|
|
|
* @throws RuntimeException thrown when the plugin wasn't found
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @see Pico::loadPlugins()
|
2015-10-29 01:55:30 +00:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-06 19:54:58 +00:00
|
|
|
* Loads the config.php and any *.config.php from Pico::$configDir
|
|
|
|
*
|
|
|
|
* After loading the {@path "config/config.php"}, Pico proceeds with any
|
2016-03-06 20:00:00 +00:00
|
|
|
* existing `config/*.config.php` in alphabetical order. The file order is
|
|
|
|
* crucial: Config values which has been set already, cannot be overwritten
|
|
|
|
* by a succeeding file. This is also true for arrays, i.e. when specifying
|
|
|
|
* `$config['test'] = array('foo' => 'bar')` in `config/a.config.php` and
|
|
|
|
* `$config['test'] = array('baz' => 42)` in `config/b.config.php`,
|
|
|
|
* `$config['test']['baz']` will be undefined!
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::setConfig()
|
|
|
|
* @see Pico::getConfig()
|
2015-08-28 16:22:32 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function loadConfig()
|
|
|
|
{
|
2016-03-06 19:54:58 +00:00
|
|
|
// scope isolated require()
|
|
|
|
$includeClosure = function ($configFile) {
|
|
|
|
require($configFile);
|
|
|
|
return (isset($config) && is_array($config)) ? $config : array();
|
|
|
|
};
|
|
|
|
if (PHP_VERSION_ID >= 50400) {
|
|
|
|
$includeClosure = $includeClosure->bindTo(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// load main config file (config/config.php)
|
|
|
|
$this->config = is_array($this->config) ? $this->config : array();
|
2015-11-13 18:10:30 +00:00
|
|
|
if (file_exists($this->getConfigDir() . 'config.php')) {
|
2016-03-06 19:54:58 +00:00
|
|
|
$this->config += $includeClosure($this->getConfigDir() . 'config.php');
|
|
|
|
}
|
2016-03-06 19:06:24 +00:00
|
|
|
|
2016-03-06 19:54:58 +00:00
|
|
|
// merge $config of config/*.config.php files
|
|
|
|
$configFiles = glob($this->getConfigDir() . '?*.config.php', GLOB_MARK);
|
|
|
|
if ($configFiles) {
|
|
|
|
foreach ($configFiles as $configFile) {
|
|
|
|
if (substr($configFile, -1) !== '/') {
|
|
|
|
$this->config += $includeClosure($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',
|
|
|
|
'base_url' => '',
|
|
|
|
'rewrite_url' => null,
|
|
|
|
'theme' => 'default',
|
2016-12-06 18:03:58 +00:00
|
|
|
'theme_url' => '',
|
2015-08-28 16:22:32 +00:00
|
|
|
'date_format' => '%D %T',
|
|
|
|
'twig_config' => array('cache' => false, 'autoescape' => false, 'debug' => false),
|
|
|
|
'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',
|
|
|
|
'timezone' => ''
|
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();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if (is_dir($this->getRootDir() . 'content')) {
|
|
|
|
$this->config['content_dir'] = $this->getRootDir() . 'content/';
|
|
|
|
} else {
|
|
|
|
$this->config['content_dir'] = $this->getRootDir() . 'content-sample/';
|
|
|
|
}
|
|
|
|
} 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
|
|
|
|
2016-12-06 18:03:58 +00:00
|
|
|
if (!$this->config['theme_url']) {
|
2016-04-23 23:22:43 +00:00
|
|
|
$this->config['theme_url'] = $this->getBaseThemeUrl();
|
|
|
|
} elseif (preg_match('#^[A-Za-z][A-Za-z0-9+\-.]*://#', $this->config['theme_url'])) {
|
|
|
|
$this->config['theme_url'] = rtrim($this->config['theme_url'], '/') . '/';
|
|
|
|
} else {
|
|
|
|
$this->config['theme_url'] = $this->getBaseUrl() . rtrim($this->config['theme_url'], '/') . '/';
|
|
|
|
}
|
|
|
|
|
2016-12-06 18:03:58 +00:00
|
|
|
if (!$this->config['timezone']) {
|
2015-08-28 16:22:32 +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
|
2016-02-04 13:19:14 +00:00
|
|
|
$this->config['timezone'] = @date_default_timezone_get();
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
2015-11-04 18:43:54 +00:00
|
|
|
date_default_timezone_set($this->config['timezone']);
|
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
|
2015-10-06 18:23:28 +00:00
|
|
|
* {@path "config/config.php"} 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
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* You can call this method between {@link Pico::__construct()} and
|
|
|
|
* {@link Pico::run()} only. Options set with this method cannot be
|
|
|
|
* overwritten by {@path "config/config.php"}.
|
|
|
|
*
|
|
|
|
* @see Pico::loadConfig()
|
|
|
|
* @see Pico::getConfig()
|
2016-01-25 18:31:53 +00:00
|
|
|
* @param array $config array with config variables
|
2015-10-06 18:23:28 +00:00
|
|
|
* @return void
|
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
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::setConfig()
|
2015-08-28 16:22:32 +00:00
|
|
|
* @see Pico::loadConfig()
|
|
|
|
* @param string $configName optional name of a config variable
|
|
|
|
* @return mixed returns either the value of the named config
|
|
|
|
* variable, null if the config variable doesn't exist or the config
|
|
|
|
* array if no config name was supplied
|
2015-06-10 09:40:26 +00:00
|
|
|
*/
|
2015-08-28 16:22:32 +00:00
|
|
|
public function getConfig($configName = null)
|
2015-06-10 09:40:26 +00:00
|
|
|
{
|
2015-08-28 16:22:32 +00:00
|
|
|
if ($configName !== null) {
|
|
|
|
return isset($this->config[$configName]) ? $this->config[$configName] : null;
|
|
|
|
} 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`
|
|
|
|
* principles. Starting with version 1.1, Pico additionally supports the
|
|
|
|
* `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.
|
|
|
|
*
|
2015-10-29 01:55:30 +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()) {
|
|
|
|
$basePath = dirname($_SERVER['SCRIPT_NAME']) . '/';
|
|
|
|
$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);
|
2016-10-05 12:17:10 +00:00
|
|
|
$requestUri = strstr($requestUri, '?', true) ?: $requestUri;
|
2016-04-24 18:11:05 +00:00
|
|
|
$this->requestUrl = rtrim(rawurldecode($requestUri), '/');
|
|
|
|
}
|
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
|
|
|
*
|
|
|
|
* @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
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getRequestFile()
|
2016-07-20 17:23:19 +00:00
|
|
|
* @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 {
|
2016-07-20 17:23:19 +00:00
|
|
|
// prevent content_dir breakouts
|
|
|
|
$requestUrl = str_replace('\\', '/', $requestUrl);
|
2015-10-28 00:36:28 +00:00
|
|
|
$requestUrlParts = explode('/', $requestUrl);
|
|
|
|
|
|
|
|
$requestFileParts = array();
|
|
|
|
foreach ($requestUrlParts as $requestUrlPart) {
|
|
|
|
if (($requestUrlPart === '') || ($requestUrlPart === '.')) {
|
|
|
|
continue;
|
|
|
|
} elseif ($requestUrlPart === '..') {
|
|
|
|
array_pop($requestFileParts);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$requestFileParts[] = $requestUrlPart;
|
|
|
|
}
|
|
|
|
|
2016-12-06 18:03:58 +00:00
|
|
|
if (!$requestFileParts) {
|
2016-07-20 17:23:19 +00:00
|
|
|
return $contentDir . 'index' . $contentExt;
|
2015-10-28 00:41:26 +00:00
|
|
|
}
|
|
|
|
|
2015-10-28 00:36:28 +00:00
|
|
|
// discover the content file to serve
|
|
|
|
// Note: $requestFileParts neither contains a trailing nor a leading slash
|
2016-07-20 17:23:19 +00:00
|
|
|
$requestFile = $contentDir . implode('/', $requestFileParts);
|
|
|
|
if (is_dir($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)
|
2016-07-20 17:23:19 +00:00
|
|
|
$indexFile = $requestFile . '/index' . $contentExt;
|
|
|
|
if (file_exists($indexFile) || !file_exists($requestFile . $contentExt)) {
|
|
|
|
return $indexFile;
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-20 17:23:19 +00:00
|
|
|
return $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
|
|
|
*
|
2016-07-20 17:23:19 +00:00
|
|
|
* @see Pico::resolveFilePath()
|
2015-10-27 00:39:28 +00:00
|
|
|
* @return string|null file path
|
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
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getRawContent()
|
2015-08-28 16:22:32 +00:00
|
|
|
* @param string $file file path
|
|
|
|
* @return string raw contents of the file
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getRawContent()
|
2015-10-04 14:57:57 +00:00
|
|
|
* @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
|
|
|
|
2016-03-05 23:29:40 +00:00
|
|
|
if (file_exists($contentDir . $errorFile)) {
|
|
|
|
return $this->loadFileContent($contentDir . $errorFile);
|
2016-03-02 23:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-05 23:29:40 +00:00
|
|
|
} elseif (file_exists($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
|
|
|
*
|
2015-08-28 16:22:32 +00:00
|
|
|
* @see Pico::loadFileContent()
|
2015-10-29 01:55:30 +00:00
|
|
|
* @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
|
|
|
|
*
|
|
|
|
* @see Pico::load404Content()
|
|
|
|
* @return boolean TRUE if Pico is serving a 404 page, FALSE otherwise
|
|
|
|
*/
|
|
|
|
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
|
|
|
*
|
2015-10-27 00:48:58 +00:00
|
|
|
* @return string[] known meta headers; the array value specifies the
|
|
|
|
* YAML key to search for, the array key is later used to access the
|
|
|
|
* found value
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
public function getMetaHeaders()
|
|
|
|
{
|
2016-12-06 19:01:38 +00:00
|
|
|
if ($this->metaHeaders !== null) {
|
|
|
|
return $this->metaHeaders;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
2015-06-10 09:40:26 +00:00
|
|
|
'title' => 'Title',
|
|
|
|
'description' => 'Description',
|
|
|
|
'author' => 'Author',
|
|
|
|
'date' => 'Date',
|
|
|
|
'robots' => 'Robots',
|
|
|
|
'template' => 'Template'
|
|
|
|
);
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 19:22:38 +00:00
|
|
|
/**
|
|
|
|
* Registers the Symfony YAML parser
|
|
|
|
*
|
|
|
|
* @see Pico::getYamlParser()
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function registerYamlParser()
|
|
|
|
{
|
|
|
|
$this->yamlParser = new \Symfony\Component\Yaml\Parser();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Symfony YAML parser
|
|
|
|
*
|
|
|
|
* @see Pico::registerYamlParser()
|
|
|
|
* @return \Symfony\Component\Yaml\Parser|null Symfony YAML parser
|
|
|
|
*/
|
|
|
|
public function getYamlParser()
|
|
|
|
{
|
|
|
|
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
|
2015-10-04 23:50:55 +00:00
|
|
|
* parsed by the YAML component of the Symfony project, keys are lowered.
|
|
|
|
* 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
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getFileMeta()
|
2016-05-23 13:13:56 +00:00
|
|
|
* @see http://symfony.com/doc/current/components/yaml/introduction.html
|
2015-10-27 00:48:58 +00:00
|
|
|
* @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])) {
|
2016-12-06 19:22:38 +00:00
|
|
|
$meta = $this->yamlParser->parse($rawMetaMatches[3]);
|
2016-07-15 01:13:58 +00:00
|
|
|
|
|
|
|
if ($meta !== null) {
|
|
|
|
// the parser may return a string for non-YAML 1-liners
|
|
|
|
// assume that this string is the page title
|
|
|
|
$meta = is_array($meta) ? array_change_key_case($meta, CASE_LOWER) : array('title' => $meta);
|
|
|
|
} else {
|
|
|
|
$meta = array();
|
|
|
|
}
|
2015-08-28 16:22:32 +00:00
|
|
|
|
|
|
|
foreach ($headers as $fieldId => $fieldName) {
|
|
|
|
$fieldName = strtolower($fieldName);
|
2015-10-04 23:50:55 +00:00
|
|
|
if (isset($meta[$fieldName])) {
|
|
|
|
// rename field (e.g. remove whitespaces)
|
|
|
|
if ($fieldId != $fieldName) {
|
|
|
|
$meta[$fieldId] = $meta[$fieldName];
|
|
|
|
unset($meta[$fieldName]);
|
|
|
|
}
|
2015-11-29 20:58:30 +00:00
|
|
|
} elseif (!isset($meta[$fieldId])) {
|
2015-10-04 23:50:55 +00:00
|
|
|
// guarantee array key existance
|
2015-08-28 16:22:32 +00:00
|
|
|
$meta[$fieldId] = '';
|
|
|
|
}
|
|
|
|
}
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
if (!empty($meta['date'])) {
|
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'];
|
|
|
|
|
|
|
|
$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']);
|
|
|
|
} else {
|
|
|
|
$meta['time'] = strtotime($meta['date']);
|
|
|
|
}
|
2015-08-28 16:22:32 +00:00
|
|
|
$meta['date_formatted'] = utf8_encode(strftime($this->getConfig('date_format'), $meta['time']));
|
2015-06-10 09:40:26 +00:00
|
|
|
} else {
|
2016-12-06 18:03:58 +00:00
|
|
|
$meta['date'] = $meta['time'] = $meta['date_formatted'] = '';
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-10-04 23:50:55 +00:00
|
|
|
// guarantee array key existance
|
2015-11-29 20:58:30 +00:00
|
|
|
$meta = array_fill_keys(array_keys($headers), '');
|
2015-08-28 16:22:32 +00:00
|
|
|
$meta['time'] = $meta['date_formatted'] = '';
|
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
|
|
|
*
|
2015-08-28 16:22:32 +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
|
|
|
/**
|
|
|
|
* Registers the Parsedown Extra markdown parser
|
|
|
|
*
|
|
|
|
* @see Pico::getParsedown()
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function registerParsedown()
|
|
|
|
{
|
|
|
|
$this->parsedown = new ParsedownExtra();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Parsedown Extra markdown parser
|
|
|
|
*
|
|
|
|
* @see Pico::registerParsedown()
|
|
|
|
* @return ParsedownExtra|null Parsedown Extra markdown parser
|
|
|
|
*/
|
|
|
|
public function getParsedown()
|
|
|
|
{
|
|
|
|
return $this->parsedown;
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* Applies some static preparations to the raw contents of a page,
|
|
|
|
* e.g. removing the meta header and replacing %base_url%
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::parseFileContent()
|
|
|
|
* @see Pico::getFileContent()
|
2015-08-28 16:22:32 +00:00
|
|
|
* @param string $rawContent raw contents of a page
|
2015-11-06 00:08:31 +00:00
|
|
|
* @param array $meta meta data to use for %meta.*% replacement
|
2015-08-28 16:22:32 +00:00
|
|
|
* @return string contents prepared for parsing
|
|
|
|
*/
|
2015-11-06 00:08:31 +00:00
|
|
|
public function prepareFileContent($rawContent, array $meta)
|
2015-08-28 16:22:32 +00:00
|
|
|
{
|
2016-05-06 21:19:40 +00:00
|
|
|
$variables = 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";
|
2015-08-28 16:22:32 +00:00
|
|
|
$content = preg_replace($metaHeaderPattern, '', $rawContent, 1);
|
|
|
|
|
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
|
|
|
|
|
|
|
// replace %theme_url%
|
2016-04-23 23:22:43 +00:00
|
|
|
$variables['%theme_url%'] = $this->getBaseThemeUrl() . $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)) {
|
2016-03-05 23:49:45 +00:00
|
|
|
$variables['%meta.' . $metaKey . '%'] = strval($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
|
|
|
|
2016-03-05 23:49:45 +00:00
|
|
|
$content = str_replace(array_keys($variables), $variables, $content);
|
2015-08-28 16:22:32 +00:00
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the contents of a page using ParsedownExtra
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::prepareFileContent()
|
|
|
|
* @see Pico::getFileContent()
|
2015-08-28 16:22:32 +00:00
|
|
|
* @param string $content raw contents of a page (Markdown)
|
|
|
|
* @return string parsed contents (HTML)
|
|
|
|
*/
|
|
|
|
public function parseFileContent($content)
|
|
|
|
{
|
2015-11-12 14:33:48 +00:00
|
|
|
if ($this->parsedown === null) {
|
|
|
|
throw new LogicException("Unable to parse file contents: Parsedown instance wasn't registered yet");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->parsedown->text($content);
|
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
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::prepareFileContent()
|
2015-08-28 16:22:32 +00:00
|
|
|
* @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
|
|
|
*
|
2015-10-29 17:13:35 +00:00
|
|
|
* | Array key | Type | Description |
|
2015-12-07 14:17:39 +00:00
|
|
|
* | -------------- | ------ | ---------------------------------------- |
|
2015-10-29 17:13:35 +00:00
|
|
|
* | 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 | string | timestamp derived from the Date header |
|
|
|
|
* | date | string | date of the page (YAML header) |
|
|
|
|
* | date_formatted | string | formatted date of the page |
|
|
|
|
* | raw_content | string | raw, not yet parsed contents of the page |
|
|
|
|
* | meta | string | parsed meta data of the page |
|
2015-10-27 00:39:28 +00:00
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::sortPages()
|
|
|
|
* @see Pico::getPages()
|
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');
|
|
|
|
$contentDirLength = strlen($contentDir);
|
|
|
|
$contentExt = $this->getConfig('content_ext');
|
|
|
|
$contentExtLength = strlen($contentExt);
|
|
|
|
|
2015-10-01 19:54:30 +00:00
|
|
|
$this->pages = array();
|
2016-03-05 23:29:40 +00:00
|
|
|
$files = $this->getFiles($contentDir, $contentExt, Pico::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;
|
|
|
|
}
|
|
|
|
|
2016-03-05 23:29:40 +00:00
|
|
|
$id = substr($file, $contentDirLength, -$contentExtLength);
|
2015-10-01 15:22:14 +00:00
|
|
|
|
2016-03-05 23:54:36 +00:00
|
|
|
// trigger onSinglePageLoading event
|
|
|
|
$this->triggerEvent('onSinglePageLoading', array(&$id));
|
|
|
|
|
2015-10-01 15:22:14 +00:00
|
|
|
// drop inaccessible pages (e.g. drop "sub.md" if "sub/index.md" exists)
|
2016-03-05 23:29:40 +00:00
|
|
|
$conflictFile = $contentDir . $id . '/index' . $contentExt;
|
2015-10-01 15:22:14 +00:00
|
|
|
if (in_array($conflictFile, $files, true)) {
|
|
|
|
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
|
|
|
|
|
|
|
$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'],
|
|
|
|
'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
|
|
|
|
*
|
2015-10-29 01:55:30 +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
|
|
|
|
$order = $this->getConfig('pages_order');
|
2016-12-06 18:47:37 +00:00
|
|
|
$orderBy = $this->getConfig('pages_order_by');
|
|
|
|
|
|
|
|
if (($orderBy !== 'date') && ($orderBy !== 'alpha')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-01 19:54:30 +00:00
|
|
|
$alphaSortClosure = function ($a, $b) use ($order) {
|
|
|
|
$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
|
|
|
|
2016-12-06 18:47:37 +00:00
|
|
|
if ($orderBy === 'date') {
|
2015-10-01 19:54:30 +00:00
|
|
|
// sort by date
|
|
|
|
uasort($this->pages, function ($a, $b) use ($alphaSortClosure, $order) {
|
2015-08-28 16:22:32 +00:00
|
|
|
if (empty($a['time']) || empty($b['time'])) {
|
|
|
|
$cmp = (empty($a['time']) - empty($b['time']));
|
|
|
|
} 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
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* Returns the list of known pages
|
|
|
|
*
|
|
|
|
* @see Pico::readPages()
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::sortPages()
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-28 16:22:32 +00:00
|
|
|
* Walks through the list of known pages and discovers the requested page
|
|
|
|
* as well as the previous and next page relative to it
|
2015-06-10 09:40:26 +00:00
|
|
|
*
|
2015-10-29 01:55:30 +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
|
|
|
{
|
2015-08-28 16:22:32 +00:00
|
|
|
$pageIds = array_keys($this->pages);
|
|
|
|
|
|
|
|
$contentDir = $this->getConfig('content_dir');
|
2016-03-02 23:03:53 +00:00
|
|
|
$contentDirLength = strlen($contentDir);
|
|
|
|
|
|
|
|
// the requested file is not in the regular content directory, therefore its ID
|
|
|
|
// isn't specified and it's impossible to determine the current page automatically
|
|
|
|
if (substr($this->requestFile, 0, $contentDirLength) !== $contentDir) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-05 23:29:40 +00:00
|
|
|
$currentPageId = substr($this->requestFile, $contentDirLength, -strlen($this->getConfig('content_ext')));
|
2015-08-28 16:22:32 +00:00
|
|
|
$currentPageIndex = array_search($currentPageId, $pageIds);
|
|
|
|
if ($currentPageIndex !== false) {
|
|
|
|
$this->currentPage = &$this->pages[$currentPageId];
|
|
|
|
|
2015-11-29 15:39:03 +00:00
|
|
|
if (($this->getConfig('order_by') === 'date') && ($this->getConfig('order') === 'desc')) {
|
2015-08-28 16:22:32 +00:00
|
|
|
$previousPageOffset = 1;
|
|
|
|
$nextPageOffset = -1;
|
|
|
|
} else {
|
|
|
|
$previousPageOffset = -1;
|
|
|
|
$nextPageOffset = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($pageIds[$currentPageIndex + $previousPageOffset])) {
|
|
|
|
$previousPageId = $pageIds[$currentPageIndex + $previousPageOffset];
|
|
|
|
$this->previousPage = &$this->pages[$previousPageId];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($pageIds[$currentPageIndex + $nextPageOffset])) {
|
|
|
|
$nextPageId = $pageIds[$currentPageIndex + $nextPageOffset];
|
|
|
|
$this->nextPage = &$this->pages[$nextPageId];
|
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
|
|
|
*
|
2015-08-28 16:22:32 +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
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the twig template engine
|
|
|
|
*
|
2016-03-16 12:33:52 +00:00
|
|
|
* This method also registers Pico's core Twig filters `link` and `content`
|
|
|
|
* as well as Pico's {@link PicoTwigExtension} Twig extension.
|
2015-11-13 15:49:53 +00:00
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::getTwig()
|
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 registerTwig()
|
2015-06-10 09:40:26 +00:00
|
|
|
{
|
2015-10-01 13:05:50 +00:00
|
|
|
$twigLoader = new Twig_Loader_Filesystem($this->getThemesDir() . $this->getConfig('theme'));
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->twig = new Twig_Environment($twigLoader, $this->getConfig('twig_config'));
|
|
|
|
$this->twig->addExtension(new Twig_Extension_Debug());
|
2015-11-13 15:48:01 +00:00
|
|
|
$this->twig->addExtension(new PicoTwigExtension($this));
|
2015-11-06 00:08:31 +00:00
|
|
|
|
2016-09-17 18:09:04 +00:00
|
|
|
// register link filter and the url_param and form_param functions
|
2015-11-12 14:33:48 +00:00
|
|
|
$this->twig->addFilter(new Twig_SimpleFilter('link', array($this, 'getPageUrl')));
|
2016-09-17 18:09:04 +00:00
|
|
|
$this->twig->addFunction(new Twig_SimpleFunction('url_param', array($this, 'getUrlParameter')));
|
|
|
|
$this->twig->addFunction(new Twig_SimpleFunction('form_param', array($this, 'getFormParameter')));
|
2015-11-12 14:33:48 +00:00
|
|
|
|
2015-11-13 15:49:53 +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;
|
2015-11-06 00:08:31 +00:00
|
|
|
$pages = &$this->pages;
|
2015-11-12 14:33:48 +00:00
|
|
|
$this->twig->addFilter(new Twig_SimpleFilter('content', function ($page) use ($pico, &$pages) {
|
|
|
|
if (isset($pages[$page])) {
|
|
|
|
$pageData = &$pages[$page];
|
2015-11-06 00:08:31 +00:00
|
|
|
if (!isset($pageData['content'])) {
|
|
|
|
$pageData['content'] = $pico->prepareFileContent($pageData['raw_content'], $pageData['meta']);
|
|
|
|
$pageData['content'] = $pico->parseFileContent($pageData['content']);
|
|
|
|
}
|
|
|
|
return $pageData['content'];
|
|
|
|
}
|
2015-11-12 14:33:48 +00:00
|
|
|
return null;
|
|
|
|
}));
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the twig template engine
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see Pico::registerTwig()
|
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()
|
|
|
|
{
|
|
|
|
return $this->twig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the variables passed to the template
|
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* URLs and paths (namely `base_dir`, `base_url`, `theme_dir` and
|
|
|
|
* `theme_url`) 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()
|
|
|
|
{
|
2016-12-06 19:04:07 +00:00
|
|
|
if ($this->twigVariables !== null) {
|
|
|
|
return $this->twigVariables;
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
$frontPage = $this->getConfig('content_dir') . 'index' . $this->getConfig('content_ext');
|
|
|
|
return array(
|
|
|
|
'config' => $this->getConfig(),
|
2015-10-01 13:05:50 +00:00
|
|
|
'base_dir' => rtrim($this->getRootDir(), '/'),
|
2015-08-28 16:22:32 +00:00
|
|
|
'base_url' => rtrim($this->getBaseUrl(), '/'),
|
2015-10-01 13:05:50 +00:00
|
|
|
'theme_dir' => $this->getThemesDir() . $this->getConfig('theme'),
|
2016-04-23 23:22:43 +00:00
|
|
|
'theme_url' => $this->getBaseThemeUrl() . $this->getConfig('theme'),
|
2015-08-28 16:22:32 +00:00
|
|
|
'rewrite_url' => $this->isUrlRewritingEnabled(),
|
|
|
|
'site_title' => $this->getConfig('site_title'),
|
|
|
|
'meta' => $this->meta,
|
|
|
|
'content' => $this->content,
|
|
|
|
'pages' => $this->pages,
|
|
|
|
'prev_page' => $this->previousPage,
|
|
|
|
'current_page' => $this->currentPage,
|
|
|
|
'next_page' => $this->nextPage,
|
2015-11-29 15:39:03 +00:00
|
|
|
'is_front_page' => ($this->requestFile === $frontPage),
|
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
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* Returns the base URL of this Pico instance
|
|
|
|
*
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
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'])));
|
|
|
|
$protocol = in_array($secureProxyHeader, array('https', 'on', 'ssl', '1')) ? 'https' : 'http';
|
|
|
|
} elseif (!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] !== 'off')) {
|
2015-11-27 18:28:48 +00:00
|
|
|
$protocol = 'https';
|
|
|
|
} elseif ($_SERVER['SERVER_PORT'] == 443) {
|
|
|
|
$protocol = 'https';
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->config['base_url'] =
|
|
|
|
$protocol . "://" . $_SERVER['HTTP_HOST']
|
2015-12-23 15:17:06 +00:00
|
|
|
. rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\') . '/';
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2016-03-05 23:29:40 +00:00
|
|
|
return $this->config['base_url'];
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-28 16:22:32 +00:00
|
|
|
* Returns true if URL rewriting is enabled
|
2015-06-10 09:40:26 +00:00
|
|
|
*
|
2015-08-28 16:22:32 +00:00
|
|
|
* @return boolean 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
|
|
|
}
|
|
|
|
|
2015-11-27 19:13:17 +00:00
|
|
|
$this->config['rewrite_url'] = (isset($_SERVER['PICO_URL_REWRITING']) && $_SERVER['PICO_URL_REWRITING']);
|
2016-03-05 23:29:40 +00:00
|
|
|
return $this->config['rewrite_url'];
|
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
|
|
|
|
* to a string representing a page identifier.
|
|
|
|
*
|
2015-12-13 21:01:25 +00:00
|
|
|
* @param string $page identifier of the page to link to
|
|
|
|
* @param array|string $queryData either an array containing properties to
|
|
|
|
* create a URL-encoded query string from, or a already encoded string
|
2016-07-13 22:24:06 +00:00
|
|
|
* @param boolean $dropIndex when the last path component is "index",
|
|
|
|
* then passing TRUE (default) leads to removing this path component
|
2015-12-13 21:01:25 +00:00
|
|
|
* @return string URL
|
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(
|
|
|
|
'Argument 2 passed to ' . get_called_class() . '::getPageUrl() must be of the type array or string, '
|
|
|
|
. (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;
|
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
|
|
|
}
|
|
|
|
|
2016-04-23 23:22:43 +00:00
|
|
|
/**
|
|
|
|
* Returns the URL of the themes folder of this Pico instance
|
|
|
|
*
|
|
|
|
* We assume that the themes folder is a arbitrary deep sub folder of the
|
|
|
|
* script's base path (i.e. the directory {@path "index.php"} is in resp.
|
|
|
|
* the `httpdocs` directory). Usually the script's base path is identical
|
|
|
|
* to {@link Pico::$rootDir}, but this may aberrate when Pico got installed
|
|
|
|
* as a composer dependency. However, ultimately it allows us to use
|
|
|
|
* {@link Pico::getBaseUrl()} as origin of the theme URL. Otherwise Pico
|
|
|
|
* falls back to the basename of {@link Pico::$themesDir} (i.e. assuming
|
|
|
|
* that `Pico::$themesDir` is `foo/bar/baz`, the base URL of the themes
|
|
|
|
* folder will be `baz/`; this ensures BC to Pico < 1.1). Pico's base URL
|
|
|
|
* always gets prepended appropriately.
|
|
|
|
*
|
|
|
|
* @return string the URL of the themes folder
|
|
|
|
*/
|
|
|
|
public function getBaseThemeUrl()
|
|
|
|
{
|
|
|
|
$themeUrl = $this->getConfig('theme_url');
|
2016-12-06 18:03:58 +00:00
|
|
|
if ($themeUrl) {
|
2016-04-23 23:22:43 +00:00
|
|
|
return $themeUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
$basePath = dirname($_SERVER['SCRIPT_FILENAME']) . '/';
|
|
|
|
$basePathLength = strlen($basePath);
|
|
|
|
if (substr($this->getThemesDir(), 0, $basePathLength) === $basePath) {
|
|
|
|
$this->config['theme_url'] = $this->getBaseUrl() . substr($this->getThemesDir(), $basePathLength);
|
|
|
|
} else {
|
|
|
|
$this->config['theme_url'] = $this->getBaseUrl() . basename($this->getThemesDir()) . '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->config['theme_url'];
|
|
|
|
}
|
|
|
|
|
2016-09-17 18:09:04 +00:00
|
|
|
/**
|
|
|
|
* Filters a URL GET parameter with a specified filter
|
|
|
|
*
|
|
|
|
* This method is just an alias for {@link Pico::filterVariable()}, see
|
|
|
|
* {@link Pico::filterVariable()} for a detailed description. It can be
|
|
|
|
* used in Twig templates by calling the `url_param` function.
|
|
|
|
*
|
|
|
|
* @see Pico::filterVariable()
|
|
|
|
* @param string $name name of the URL GET parameter
|
|
|
|
* to filter
|
|
|
|
* @param int|string $filter the filter to apply
|
|
|
|
* @param mixed|array $options either a associative options
|
|
|
|
* array to be used by the filter or a scalar default value
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* This method is just an alias for {@link Pico::filterVariable()}, see
|
|
|
|
* {@link Pico::filterVariable()} for a detailed description. It can be
|
|
|
|
* used in Twig templates by calling the `form_param` function.
|
|
|
|
*
|
|
|
|
* @see Pico::filterVariable()
|
|
|
|
* @param string $name name of the HTTP POST
|
|
|
|
* parameter to filter
|
|
|
|
* @param int|string $filter the filter to apply
|
|
|
|
* @param mixed|array $options either a associative options
|
|
|
|
* array to be used by the filter or a scalar default value
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
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!
|
|
|
|
*
|
|
|
|
* @see https://secure.php.net/manual/en/function.filter-var.php
|
|
|
|
* PHP's `filter_var()` function
|
|
|
|
* @see https://secure.php.net/manual/en/filter.filters.validate.php
|
|
|
|
* Validate filters
|
|
|
|
* @see https://secure.php.net/manual/en/filter.filters.sanitize.php
|
|
|
|
* Sanitize filters
|
|
|
|
* @param mixed $variable value to filter
|
|
|
|
* @param int|string $filter ID (int) or name (string) of
|
|
|
|
* the filter to apply; if omitted, the method will return FALSE
|
|
|
|
* @param mixed|array $options either a associative array
|
|
|
|
* 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)
|
|
|
|
* @param int|string|int[]|string[] $flags either a bitwise disjunction
|
|
|
|
* 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)
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
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
|
|
|
*
|
2015-09-13 18:46:09 +00:00
|
|
|
* @param string $directory start directory
|
|
|
|
* @param string $fileExtension return files with the given file extension
|
|
|
|
* only (optional)
|
2015-10-01 19:54:30 +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
|
2015-09-13 18:46:09 +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)
|
2016-03-05 23:29:40 +00:00
|
|
|
if (($file[0] === '.') || in_array(substr($file, -1), array('~', '#'))) {
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
|
|
|
* This method also guarantees a trailing slash.
|
|
|
|
*
|
|
|
|
* @param string $path relative or absolute path
|
|
|
|
* @return string absolute path
|
|
|
|
*/
|
2015-11-13 18:10:30 +00:00
|
|
|
public function getAbsolutePath($path)
|
2015-10-01 13:14:45 +00:00
|
|
|
{
|
2015-12-23 15:17:06 +00:00
|
|
|
if (strncasecmp(PHP_OS, 'WIN', 3) === 0) {
|
|
|
|
if (preg_match('/^([a-zA-Z]:\\\\|\\\\\\\\)/', $path) !== 1) {
|
|
|
|
$path = $this->getRootDir() . $path;
|
|
|
|
}
|
|
|
|
} else {
|
2016-03-05 23:29:40 +00:00
|
|
|
if ($path[0] !== '/') {
|
2015-12-23 15:17:06 +00:00
|
|
|
$path = $this->getRootDir() . $path;
|
|
|
|
}
|
2015-10-01 13:14:45 +00:00
|
|
|
}
|
2015-12-23 15:17:06 +00:00
|
|
|
return rtrim($path, '/\\') . '/';
|
2015-10-01 13:14:45 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 09:40:26 +00:00
|
|
|
/**
|
2015-10-29 01:55:30 +00:00
|
|
|
* Triggers events on plugins which implement PicoPluginInterface
|
2015-06-10 09:40:26 +00:00
|
|
|
*
|
2015-08-28 16:22:32 +00:00
|
|
|
* Deprecated events (as used by plugins not implementing
|
2015-12-28 21:41:39 +00:00
|
|
|
* {@link PicoPluginInterface}) are triggered by {@link PicoDeprecated}.
|
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
|
|
|
*
|
2015-10-29 01:55:30 +00:00
|
|
|
* @see PicoPluginInterface
|
|
|
|
* @see AbstractPicoPlugin
|
|
|
|
* @see DummyPlugin
|
2015-08-28 16:22:32 +00:00
|
|
|
* @param string $eventName name of the event to trigger
|
|
|
|
* @param array $params optional parameters to pass
|
|
|
|
* @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
|
|
|
{
|
2016-12-06 18:03:58 +00:00
|
|
|
if ($this->plugins) {
|
2015-11-03 22:49:34 +00:00
|
|
|
foreach ($this->plugins as $plugin) {
|
|
|
|
// only trigger events for plugins that implement PicoPluginInterface
|
2015-11-22 13:08:35 +00:00
|
|
|
// deprecated events (plugins for Pico 0.9 and older) will be triggered by `PicoDeprecated`
|
2016-03-11 18:07:45 +00:00
|
|
|
if ($plugin instanceof PicoPluginInterface) {
|
2015-11-03 22:49:34 +00:00
|
|
|
$plugin->handleEvent($eventName, $params);
|
|
|
|
}
|
2015-08-28 16:22:32 +00:00
|
|
|
}
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-04 13:45:09 +00:00
|
|
|
}
|