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.
|
|
|
|
* - Stupidly Simple: Picos makes creating and maintaining a
|
|
|
|
* 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.
|
|
|
|
* See <http://picocms.org/> for more info.
|
|
|
|
*
|
|
|
|
* @author Gilbert Pellegrom
|
|
|
|
* @author Daniel Rudolf
|
|
|
|
* @link <http://picocms.org>
|
|
|
|
* @license The MIT License <http://opensource.org/licenses/MIT>
|
|
|
|
* @version 1.0
|
2013-04-26 16:38:10 +00:00
|
|
|
*/
|
2015-06-10 09:40:26 +00:00
|
|
|
class Pico
|
|
|
|
{
|
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
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $rootDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Config directory of this Pico instance
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $configDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plugins directory of this Pico instance
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $pluginsDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Themes directory of this Pico instance
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $themesDir;
|
|
|
|
|
2015-10-06 18:38:34 +00:00
|
|
|
/**
|
|
|
|
* Boolean indicating whether Picos processing started yet
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $locked = false;
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* List of loaded plugins
|
|
|
|
*
|
|
|
|
* @see Pico::loadPlugins()
|
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
|
|
|
|
*
|
|
|
|
* @see Pico::loadConfig()
|
2015-10-27 00:48:58 +00:00
|
|
|
* @var mixed[]|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
|
|
|
*
|
|
|
|
* @see Pico::evaluateRequestUrl()
|
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
|
|
|
*
|
|
|
|
* @see Pico::discoverRequestFile()
|
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
|
|
|
|
*
|
|
|
|
* @see Pico::loadFileContent()
|
2015-10-27 00:39:28 +00:00
|
|
|
* @var string|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected $rawContent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Meta data of the page to serve
|
|
|
|
*
|
|
|
|
* @see Pico::parseFileMeta()
|
2015-10-27 00:48:58 +00:00
|
|
|
* @var string[]|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected $meta;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parsed content being served
|
|
|
|
*
|
|
|
|
* @see Pico::prepareFileContent()
|
|
|
|
* @see Pico::parseFileContent()
|
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
|
|
|
|
*
|
|
|
|
* @see Pico::readPages()
|
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
|
|
|
|
*
|
|
|
|
* @see Pico::discoverCurrentPage()
|
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
|
|
|
|
*
|
|
|
|
* @see Pico::discoverCurrentPage()
|
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
|
|
|
|
*
|
|
|
|
* @see Pico::discoverCurrentPage()
|
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
|
|
|
|
*
|
|
|
|
* @see Pico::registerTwig()
|
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-27 00:48:58 +00:00
|
|
|
* @var mixed[]|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected $twigVariables;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a new Pico instance
|
|
|
|
*
|
2015-10-01 13:05:50 +00:00
|
|
|
* To carry out all the processing in Pico, call the run() method.
|
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)
|
|
|
|
{
|
|
|
|
$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.
|
|
|
|
*
|
|
|
|
* @return string rendered Pico contents
|
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
|
|
|
{
|
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();
|
|
|
|
$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-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
|
|
|
|
$this->discoverRequestFile();
|
|
|
|
$this->triggerEvent('onRequestFile', array(&$this->requestFile));
|
|
|
|
|
|
|
|
// load raw file content
|
|
|
|
$this->triggerEvent('onContentLoading', array(&$this->requestFile));
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
if (file_exists($this->requestFile)) {
|
|
|
|
$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);
|
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
|
|
|
|
$headers = $this->getMetaHeaders();
|
|
|
|
|
|
|
|
$this->triggerEvent('onMetaParsing', array(&$this->rawContent, &$headers));
|
|
|
|
$this->meta = $this->parseFileMeta($this->rawContent, $headers);
|
|
|
|
$this->triggerEvent('onMetaParsed', array(&$this->meta));
|
|
|
|
|
|
|
|
// parse file content
|
|
|
|
$this->triggerEvent('onContentParsing', array(&$this->rawContent));
|
|
|
|
|
|
|
|
$this->content = $this->prepareFileContent($this->rawContent);
|
|
|
|
$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();
|
|
|
|
if (isset($this->meta['template']) && $this->meta['template']) {
|
|
|
|
$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
|
|
|
*
|
|
|
|
* Plugin files may be prefixed by a number (e.g. 00-PicoDeprecated.php)
|
2015-09-13 18:46:09 +00:00
|
|
|
* to indicate their processing order. You MUST NOT use prefixes between
|
2015-08-28 16:22:32 +00:00
|
|
|
* 00 and 19 (reserved for built-in plugins).
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @throws RuntimeException thrown when a plugin couldn't be loaded
|
|
|
|
*/
|
|
|
|
protected function loadPlugins()
|
|
|
|
{
|
|
|
|
$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) {
|
|
|
|
require_once($pluginFile);
|
|
|
|
|
|
|
|
$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
|
|
|
|
//throw new RuntimeException("Unable to load plugin '".$className."'");
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
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()
|
|
|
|
* @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-27 00:48:58 +00:00
|
|
|
* @return object[]|null
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
public function getPlugins()
|
|
|
|
{
|
|
|
|
return $this->plugins;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-01 20:52:10 +00:00
|
|
|
* Loads the config.php from Pico::$configDir
|
2015-08-28 16:22:32 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function loadConfig()
|
|
|
|
{
|
|
|
|
$defaultConfig = array(
|
|
|
|
'site_title' => 'Pico',
|
|
|
|
'base_url' => '',
|
|
|
|
'rewrite_url' => null,
|
|
|
|
'theme' => 'default',
|
|
|
|
'date_format' => '%D %T',
|
|
|
|
'twig_config' => array('cache' => false, 'autoescape' => false, 'debug' => false),
|
|
|
|
'pages_order_by' => 'alpha',
|
|
|
|
'pages_order' => 'asc',
|
2015-10-01 13:05:50 +00:00
|
|
|
'content_dir' => $this->getRootDir() . 'content-sample/',
|
2015-08-28 16:22:32 +00:00
|
|
|
'content_ext' => '.md',
|
|
|
|
'timezone' => ''
|
2015-06-10 09:40:26 +00:00
|
|
|
);
|
|
|
|
|
2015-10-01 13:05:50 +00:00
|
|
|
$configFile = $this->getConfigDir() . 'config.php';
|
|
|
|
$config = file_exists($configFile) ? require($configFile) : null;
|
2015-10-06 18:38:34 +00:00
|
|
|
|
|
|
|
$this->config = is_array($this->config) ? $this->config : array();
|
2015-10-06 18:23:28 +00:00
|
|
|
$this->config += is_array($config) ? $config + $defaultConfig : $defaultConfig;
|
2015-08-28 16:22:32 +00:00
|
|
|
|
|
|
|
if (empty($this->config['base_url'])) {
|
|
|
|
$this->config['base_url'] = $this->getBaseUrl();
|
|
|
|
}
|
|
|
|
if (!empty($this->config['content_dir'])) {
|
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
|
|
|
}
|
|
|
|
if (!empty($this->config['timezone'])) {
|
|
|
|
date_default_timezone_set($this->config['timezone']);
|
|
|
|
} else {
|
|
|
|
// 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
|
|
|
|
$defaultTimezone = date_default_timezone_get();
|
|
|
|
date_default_timezone_set($defaultTimezone);
|
|
|
|
}
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 18:23:28 +00:00
|
|
|
/**
|
|
|
|
* Sets Picos config before calling Pico::run()
|
|
|
|
*
|
|
|
|
* This method allows you to modify Picos config without creating a
|
|
|
|
* {@path "config/config.php"} or changing some of its variables before
|
|
|
|
* Pico starts processing. It can only be called between the constructor
|
|
|
|
* call and Pico::run(). Options set with this method cannot be overwritten
|
|
|
|
* by {@path "config/config.php"}.
|
|
|
|
*
|
2015-10-27 00:48:58 +00:00
|
|
|
* @param mixed[] $config array with configuration variables, like
|
|
|
|
* $config in {@path "config/config.php"}
|
2015-10-06 18:23:28 +00:00
|
|
|
* @return void
|
2015-10-27 00:48:58 +00:00
|
|
|
* @throws RuntimeException 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-10-06 18:23:28 +00:00
|
|
|
throw new RuntimeException('You cannot modify Picos config after processing has started');
|
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*
|
|
|
|
* Pico 1.0 uses the QUERY_STRING routing method (e.g. /pico/?sub/page) to
|
|
|
|
* support SEO-like URLs out-of-the-box with any webserver. You can still
|
|
|
|
* setup URL rewriting (e.g. using mod_rewrite on Apache) to basically
|
|
|
|
* remove the `?` from URLs, but your rewritten URLs must follow the
|
|
|
|
* new QUERY_STRING principles. URL rewriting requires some special
|
|
|
|
* configuration on your webserver, but this should be "basic work" for
|
|
|
|
* any webmaster...
|
|
|
|
*
|
|
|
|
* Pico 0.9 and older required Apache with mod_rewrite enabled, thus old
|
|
|
|
* 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
|
|
|
|
* `{{ base_url }}sub/page`. In content files you can still use the
|
2015-10-09 15:19:18 +00:00
|
|
|
* `%base_url%` variable; e.g. `%base_url%?sub/page` will be automatically
|
2015-08-28 16:22:32 +00:00
|
|
|
* replaced accordingly.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function evaluateRequestUrl()
|
|
|
|
{
|
|
|
|
// use QUERY_STRING; e.g. /pico/?sub/page
|
|
|
|
// if you want to use rewriting, you MUST make your rules to
|
|
|
|
// rewrite the URLs to follow the QUERY_STRING method
|
|
|
|
//
|
|
|
|
// Note: you MUST NOT call the index page with /pico/?someBooleanParameter;
|
|
|
|
// use /pico/?someBooleanParameter= or /pico/?index&someBooleanParameter instead
|
2015-08-30 19:31:47 +00:00
|
|
|
$pathComponent = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '';
|
2015-08-28 16:22:32 +00:00
|
|
|
if (($pathComponentLength = strpos($pathComponent, '&')) !== false) {
|
|
|
|
$pathComponent = substr($pathComponent, 0, $pathComponentLength);
|
|
|
|
}
|
|
|
|
$this->requestUrl = (strpos($pathComponent, '=') === false) ? urldecode($pathComponent) : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses the request URL to discover the content file to serve
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function discoverRequestFile()
|
|
|
|
{
|
|
|
|
if (empty($this->requestUrl)) {
|
|
|
|
$this->requestFile = $this->getConfig('content_dir') . 'index' . $this->getConfig('content_ext');
|
|
|
|
} else {
|
|
|
|
$this->requestFile = $this->getConfig('content_dir') . $this->requestUrl;
|
|
|
|
if (is_dir($this->requestFile)) {
|
|
|
|
// 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)
|
|
|
|
$indexFile = $this->requestFile . '/index' . $this->getConfig('content_ext');
|
|
|
|
if (file_exists($indexFile) || !file_exists($this->requestFile . $this->getConfig('content_ext'))) {
|
|
|
|
$this->requestFile = $indexFile;
|
|
|
|
return;
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->requestFile .= $this->getConfig('content_ext');
|
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
|
|
|
*
|
2015-08-28 16:22:32 +00:00
|
|
|
* @see Pico::discoverRequestFile()
|
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
|
|
|
|
*
|
|
|
|
* @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
|
|
|
*
|
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
|
|
|
|
* @throws RuntimeException thrown when no suitable 404 file is found
|
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
|
|
|
{
|
2015-10-04 13:24:38 +00:00
|
|
|
$errorFileDir = substr($file, strlen($this->getConfig('content_dir')));
|
|
|
|
do {
|
|
|
|
$errorFileDir = dirname($errorFileDir);
|
|
|
|
$errorFile = $errorFileDir . '/404' . $this->getConfig('content_ext');
|
|
|
|
} while (!file_exists($this->getConfig('content_dir') . $errorFile) && ($errorFileDir !== '.'));
|
|
|
|
|
|
|
|
if (!file_exists($this->getConfig('content_dir') . $errorFile)) {
|
|
|
|
$errorFile = ($errorFileDir === '.') ? '404' . $this->getConfig('content_ext') : $errorFile;
|
2015-10-04 19:34:37 +00:00
|
|
|
throw new RuntimeException('Required "' . $errorFile . '" not found');
|
2015-10-04 13:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->loadFileContent($this->getConfig('content_dir') . $errorFile);
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-28 16:22:32 +00:00
|
|
|
* Returns the cached 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-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
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
/**
|
|
|
|
* Returns known meta headers and triggers the onMetaHeaders event
|
|
|
|
*
|
|
|
|
* Heads up! Calling this method triggers the `onMetaHeaders` event.
|
|
|
|
* Keep this in mind to prevent a infinite loop!
|
|
|
|
*
|
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()
|
|
|
|
{
|
2015-06-10 09:40:26 +00:00
|
|
|
$headers = array(
|
|
|
|
'title' => 'Title',
|
|
|
|
'description' => 'Description',
|
|
|
|
'author' => 'Author',
|
|
|
|
'date' => 'Date',
|
|
|
|
'robots' => 'Robots',
|
|
|
|
'template' => 'Template'
|
|
|
|
);
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->triggerEvent('onMetaHeaders', array(&$headers));
|
|
|
|
return $headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the file meta from raw file contents
|
|
|
|
*
|
|
|
|
* Meta data MUST start on the first line of the file, either opened and
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* @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-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
public function parseFileMeta($rawContent, array $headers)
|
|
|
|
{
|
|
|
|
$meta = array();
|
|
|
|
$pattern = "/^(\/(\*)|---)[[:blank:]]*(?:\r)?\n"
|
|
|
|
. "(.*?)(?:\r)?\n(?(2)\*\/|---)[[:blank:]]*(?:(?:\r)?\n|$)/s";
|
|
|
|
if (preg_match($pattern, $rawContent, $rawMetaMatches)) {
|
|
|
|
$yamlParser = new \Symfony\Component\Yaml\Parser();
|
2015-10-04 23:50:55 +00:00
|
|
|
$meta = $yamlParser->parse($rawMetaMatches[3]);
|
|
|
|
$meta = array_change_key_case($meta, CASE_LOWER);
|
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-08-28 16:22:32 +00:00
|
|
|
} else {
|
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'])) {
|
|
|
|
$meta['time'] = strtotime($meta['date']);
|
|
|
|
$meta['date_formatted'] = utf8_encode(strftime($this->getConfig('date_format'), $meta['time']));
|
2015-06-10 09:40:26 +00:00
|
|
|
} else {
|
2015-08-28 16:22:32 +00:00
|
|
|
$meta['time'] = $meta['date_formatted'] = '';
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-04 23:50:55 +00:00
|
|
|
// guarantee array key existance
|
2015-08-28 16:22:32 +00:00
|
|
|
foreach ($headers as $id => $field) {
|
|
|
|
$meta[$id] = '';
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
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-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%
|
|
|
|
*
|
|
|
|
* @param string $rawContent raw contents of a page
|
|
|
|
* @return string contents prepared for parsing
|
|
|
|
*/
|
|
|
|
public function prepareFileContent($rawContent)
|
|
|
|
{
|
|
|
|
// remove meta header
|
|
|
|
$metaHeaderPattern = "/^(\/(\*)|---)[[:blank:]]*(?:\r)?\n"
|
|
|
|
. "(.*?)(?:\r)?\n(?(2)\*\/|---)[[:blank:]]*(?:(?:\r)?\n|$)/s";
|
|
|
|
$content = preg_replace($metaHeaderPattern, '', $rawContent, 1);
|
|
|
|
|
|
|
|
// replace %site_title%
|
|
|
|
$content = str_replace('%site_title%', $this->getConfig('site_title'), $content);
|
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
|
|
|
|
$content = str_replace('%base_url%?', $this->getBaseUrl(), $content);
|
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
|
2015-08-28 16:22:32 +00:00
|
|
|
$content = str_replace('%base_url%?', $this->getBaseUrl() . '?', $content);
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
2015-08-28 16:22:32 +00:00
|
|
|
$content = str_replace('%base_url%', rtrim($this->getBaseUrl(), '/'), $content);
|
|
|
|
|
|
|
|
// replace %theme_url%
|
2015-10-01 13:05:50 +00:00
|
|
|
$themeUrl = $this->getBaseUrl() . basename($this->getThemesDir()) . '/' . $this->getConfig('theme');
|
2015-08-28 16:22:32 +00:00
|
|
|
$content = str_replace('%theme_url%', $themeUrl, $content);
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
// replace %meta.*%
|
2015-10-28 00:08:45 +00:00
|
|
|
$metaKeys = $metaValues = array();
|
|
|
|
foreach ($this->meta as $metaKey => $metaValue) {
|
|
|
|
if (is_scalar($metaValue) || ($metaValue === null)) {
|
|
|
|
$metaKeys[] = '%meta.' . $metaKey . '%';
|
|
|
|
$metaValues[] = strval($metaValue);
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 16:22:32 +00:00
|
|
|
$content = str_replace($metaKeys, $metaValues, $content);
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the contents of a page using ParsedownExtra
|
|
|
|
*
|
|
|
|
* @param string $content raw contents of a page (Markdown)
|
|
|
|
* @return string parsed contents (HTML)
|
|
|
|
*/
|
|
|
|
public function parseFileContent($content)
|
|
|
|
{
|
|
|
|
$parsedown = new ParsedownExtra();
|
|
|
|
return $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-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:
|
|
|
|
* +----------------+------------------------------------------+
|
|
|
|
* | Array key | Description |
|
|
|
|
* +----------------+------------------------------------------+
|
|
|
|
* | id | relative path to the content file |
|
|
|
|
* | url | URL to the page |
|
|
|
|
* | title | title of the page (YAML header) |
|
|
|
|
* | description | description of the page (YAML header) |
|
|
|
|
* | author | author of the page (YAML header) |
|
|
|
|
* | time | timestamp derived from the Date header |
|
|
|
|
* | date | date of the page (YAML header) |
|
|
|
|
* | date_formatted | formatted date of the page |
|
|
|
|
* | raw_content | raw, not yet parsed contents of the page |
|
|
|
|
* | meta | parsed meta data of the page) |
|
|
|
|
* +----------------+------------------------------------------+
|
|
|
|
*
|
2015-08-28 16:22:32 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function readPages()
|
|
|
|
{
|
2015-10-01 19:54:30 +00:00
|
|
|
$this->pages = array();
|
2015-10-04 12:15:11 +00:00
|
|
|
$files = $this->getFiles($this->getConfig('content_dir'), $this->getConfig('content_ext'), Pico::SORT_NONE);
|
2015-08-28 16:22:32 +00:00
|
|
|
foreach ($files as $i => $file) {
|
|
|
|
// skip 404 page
|
|
|
|
if (basename($file) == '404' . $this->getConfig('content_ext')) {
|
|
|
|
unset($files[$i]);
|
2015-06-10 09:40:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
$id = substr($file, strlen($this->getConfig('content_dir')), -strlen($this->getConfig('content_ext')));
|
2015-10-01 15:22:14 +00:00
|
|
|
|
|
|
|
// drop inaccessible pages (e.g. drop "sub.md" if "sub/index.md" exists)
|
|
|
|
$conflictFile = $this->getConfig('content_dir') . $id . '/index' . $this->getConfig('content_ext');
|
|
|
|
if (in_array($conflictFile, $files, true)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
$url = $this->getPageUrl($id);
|
|
|
|
if ($file != $this->requestFile) {
|
|
|
|
$rawContent = file_get_contents($file);
|
|
|
|
$meta = $this->parseFileMeta($rawContent, $this->getMetaHeaders());
|
|
|
|
} 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-08-28 16:22:32 +00:00
|
|
|
if ($file == $this->requestFile) {
|
|
|
|
$page['content'] = &$this->content;
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
2015-08-28 16:22:32 +00:00
|
|
|
|
|
|
|
unset($rawContent, $meta);
|
|
|
|
|
|
|
|
// trigger event
|
|
|
|
$this->triggerEvent('onSinglePageLoaded', array(&$page));
|
|
|
|
|
2015-10-01 19:54:30 +00:00
|
|
|
$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
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function sortPages()
|
|
|
|
{
|
2015-10-01 19:54:30 +00:00
|
|
|
// sort pages
|
|
|
|
$order = $this->getConfig('pages_order');
|
|
|
|
$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);
|
|
|
|
return $cmp * (($order == 'desc') ? -1 : 1);
|
|
|
|
};
|
2015-08-28 16:22:32 +00:00
|
|
|
|
2015-10-01 19:54:30 +00:00
|
|
|
if ($this->getConfig('pages_order_by') == 'date') {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
return $cmp * (($order == 'desc') ? 1 : -1);
|
|
|
|
});
|
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-27 00:39:28 +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-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');
|
|
|
|
$contentExt = $this->getConfig('content_ext');
|
|
|
|
$currentPageId = substr($this->requestFile, strlen($contentDir), -strlen($contentExt));
|
|
|
|
$currentPageIndex = array_search($currentPageId, $pageIds);
|
|
|
|
if ($currentPageIndex !== false) {
|
|
|
|
$this->currentPage = &$this->pages[$currentPageId];
|
|
|
|
|
|
|
|
if (($this->getConfig('order_by') == 'date') && ($this->getConfig('order') == 'desc')) {
|
|
|
|
$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
|
|
|
|
*
|
|
|
|
* @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());
|
|
|
|
$this->twig->addFilter(new Twig_SimpleFilter('link', array($this, 'getPageUrl')));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the twig template engine
|
|
|
|
*
|
2015-10-27 00:39:28 +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
|
|
|
|
*
|
|
|
|
* URLs and paths (namely base_dir, base_url, theme_dir and theme_url)
|
|
|
|
* don't add a trailing slash for historic reasons.
|
|
|
|
*
|
2015-10-27 00:48:58 +00:00
|
|
|
* @return mixed[] template variables
|
2015-08-28 16:22:32 +00:00
|
|
|
*/
|
|
|
|
protected function getTwigVariables()
|
|
|
|
{
|
|
|
|
$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'),
|
|
|
|
'theme_url' => $this->getBaseUrl() . basename($this->getThemesDir()) . '/' . $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,
|
|
|
|
'is_front_page' => ($this->requestFile == $frontPage),
|
|
|
|
);
|
|
|
|
}
|
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');
|
|
|
|
if (!empty($baseUrl)) {
|
|
|
|
return $baseUrl;
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
if (
|
|
|
|
(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off')
|
|
|
|
|| ($_SERVER['SERVER_PORT'] == 443)
|
|
|
|
|| (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
|
|
|
|
) {
|
|
|
|
$protocol = 'https';
|
|
|
|
} else {
|
|
|
|
$protocol = 'http';
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
$this->config['base_url'] =
|
|
|
|
$protocol . "://" . $_SERVER['HTTP_HOST']
|
|
|
|
. dirname($_SERVER['SCRIPT_NAME']) . '/';
|
2015-06-10 09:40:26 +00:00
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
return $this->getConfig('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-08-28 16:22:32 +00:00
|
|
|
if (($this->getConfig('rewrite_url') === null) && isset($_SERVER['PICO_URL_REWRITING'])) {
|
|
|
|
return (bool) $_SERVER['PICO_URL_REWRITING'];
|
|
|
|
} elseif ($this->getConfig('rewrite_url')) {
|
|
|
|
return true;
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 16:22:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the URL to a given page
|
|
|
|
*
|
|
|
|
* @param string $page identifier of the page to link to
|
|
|
|
* @return string URL
|
|
|
|
*/
|
|
|
|
public function getPageUrl($page)
|
|
|
|
{
|
|
|
|
return $this->getBaseUrl() . ((!$this->isUrlRewritingEnabled() && !empty($page)) ? '?' : '') . $page;
|
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
|
|
|
*/
|
2015-10-04 12:15:11 +00:00
|
|
|
protected 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)
|
|
|
|
if ((substr($file, 0, 1) === '.') || 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));
|
2015-09-13 18:46:09 +00:00
|
|
|
} elseif (empty($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
|
|
|
/**
|
|
|
|
* Makes a relative path absolute to Picos root dir
|
|
|
|
*
|
|
|
|
* This method also guarantees a trailing slash.
|
|
|
|
*
|
|
|
|
* @param string $path relative or absolute path
|
|
|
|
* @return string absolute path
|
|
|
|
*/
|
|
|
|
protected function getAbsolutePath($path)
|
|
|
|
{
|
|
|
|
if (substr($path, 0, 1) !== '/') {
|
|
|
|
$path = $this->getRootDir() . $path;
|
|
|
|
}
|
|
|
|
return rtrim($path, '/') . '/';
|
|
|
|
}
|
|
|
|
|
2015-06-10 09:40:26 +00:00
|
|
|
/**
|
2015-09-15 11:15:45 +00:00
|
|
|
* Triggers events on plugins which implement {@link 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
|
|
|
|
* {@link IPocPlugin}) are triggered by {@link PicoDeprecated}.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
*/
|
2015-08-28 16:22:32 +00:00
|
|
|
protected function triggerEvent($eventName, array $params = array())
|
2015-06-10 09:40:26 +00:00
|
|
|
{
|
2015-08-28 16:22:32 +00:00
|
|
|
foreach ($this->plugins as $plugin) {
|
2015-09-15 11:15:45 +00:00
|
|
|
// only trigger events for plugins that implement PicoPluginInterface
|
2015-08-28 16:22:32 +00:00
|
|
|
// deprecated events (plugins for Pico 0.9 and older) will be
|
|
|
|
// triggered by the `PicoPluginDeprecated` plugin
|
2015-09-15 11:15:45 +00:00
|
|
|
if (is_a($plugin, 'PicoPluginInterface')) {
|
2015-08-28 16:22:32 +00:00
|
|
|
$plugin->handleEvent($eventName, $params);
|
|
|
|
}
|
2015-06-10 09:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-04 13:45:09 +00:00
|
|
|
}
|