Pico.php 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  1. <?php
  2. /**
  3. * Pico
  4. *
  5. * Pico is a stupidly simple, blazing fast, flat file CMS.
  6. *
  7. * - Stupidly Simple: Pico makes creating and maintaining a
  8. * website as simple as editing text files.
  9. * - Blazing Fast: Pico is seriously lightweight and doesn't
  10. * use a database, making it super fast.
  11. * - No Database: Pico is a "flat file" CMS, meaning no
  12. * database woes, no MySQL queries, nothing.
  13. * - Markdown Formatting: Edit your website in your favourite
  14. * text editor using simple Markdown formatting.
  15. * - Twig Templates: Pico uses the Twig templating engine,
  16. * for powerful and flexible themes.
  17. * - Open Source: Pico is completely free and open source,
  18. * released under the MIT license.
  19. *
  20. * See <http://picocms.org/> for more info.
  21. *
  22. * @author Gilbert Pellegrom
  23. * @author Daniel Rudolf
  24. * @link http://picocms.org
  25. * @license http://opensource.org/licenses/MIT The MIT License
  26. * @version 1.1
  27. */
  28. class Pico
  29. {
  30. /**
  31. * Pico version
  32. *
  33. * @var string
  34. */
  35. const VERSION = '1.1.0-dev';
  36. /**
  37. * Pico version ID
  38. *
  39. * @var int
  40. */
  41. const VERSION_ID = 10100;
  42. /**
  43. * Sort files in alphabetical ascending order
  44. *
  45. * @see Pico::getFiles()
  46. * @var int
  47. */
  48. const SORT_ASC = 0;
  49. /**
  50. * Sort files in alphabetical descending order
  51. *
  52. * @see Pico::getFiles()
  53. * @var int
  54. */
  55. const SORT_DESC = 1;
  56. /**
  57. * Don't sort files
  58. *
  59. * @see Pico::getFiles()
  60. * @var int
  61. */
  62. const SORT_NONE = 2;
  63. /**
  64. * Root directory of this Pico instance
  65. *
  66. * @see Pico::getRootDir()
  67. * @var string
  68. */
  69. protected $rootDir;
  70. /**
  71. * Config directory of this Pico instance
  72. *
  73. * @see Pico::getConfigDir()
  74. * @var string
  75. */
  76. protected $configDir;
  77. /**
  78. * Plugins directory of this Pico instance
  79. *
  80. * @see Pico::getPluginsDir()
  81. * @var string
  82. */
  83. protected $pluginsDir;
  84. /**
  85. * Themes directory of this Pico instance
  86. *
  87. * @see Pico::getThemesDir()
  88. * @var string
  89. */
  90. protected $themesDir;
  91. /**
  92. * Boolean indicating whether Pico started processing yet
  93. *
  94. * @var boolean
  95. */
  96. protected $locked = false;
  97. /**
  98. * List of loaded plugins
  99. *
  100. * @see Pico::getPlugins()
  101. * @var object[]|null
  102. */
  103. protected $plugins;
  104. /**
  105. * Current configuration of this Pico instance
  106. *
  107. * @see Pico::getConfig()
  108. * @var array|null
  109. */
  110. protected $config;
  111. /**
  112. * Part of the URL describing the requested contents
  113. *
  114. * @see Pico::getRequestUrl()
  115. * @var string|null
  116. */
  117. protected $requestUrl;
  118. /**
  119. * Absolute path to the content file being served
  120. *
  121. * @see Pico::getRequestFile()
  122. * @var string|null
  123. */
  124. protected $requestFile;
  125. /**
  126. * Raw, not yet parsed contents to serve
  127. *
  128. * @see Pico::getRawContent()
  129. * @var string|null
  130. */
  131. protected $rawContent;
  132. /**
  133. * Meta data of the page to serve
  134. *
  135. * @see Pico::getFileMeta()
  136. * @var array|null
  137. */
  138. protected $meta;
  139. /**
  140. * Parsedown Extra instance used for markdown parsing
  141. *
  142. * @see Pico::getParsedown()
  143. * @var ParsedownExtra|null
  144. */
  145. protected $parsedown;
  146. /**
  147. * Parsed content being served
  148. *
  149. * @see Pico::getFileContent()
  150. * @var string|null
  151. */
  152. protected $content;
  153. /**
  154. * List of known pages
  155. *
  156. * @see Pico::getPages()
  157. * @var array[]|null
  158. */
  159. protected $pages;
  160. /**
  161. * Data of the page being served
  162. *
  163. * @see Pico::getCurrentPage()
  164. * @var array|null
  165. */
  166. protected $currentPage;
  167. /**
  168. * Data of the previous page relative to the page being served
  169. *
  170. * @see Pico::getPreviousPage()
  171. * @var array|null
  172. */
  173. protected $previousPage;
  174. /**
  175. * Data of the next page relative to the page being served
  176. *
  177. * @see Pico::getNextPage()
  178. * @var array|null
  179. */
  180. protected $nextPage;
  181. /**
  182. * Twig instance used for template parsing
  183. *
  184. * @see Pico::getTwig()
  185. * @var Twig_Environment|null
  186. */
  187. protected $twig;
  188. /**
  189. * Variables passed to the twig template
  190. *
  191. * @see Pico::getTwigVariables
  192. * @var array|null
  193. */
  194. protected $twigVariables;
  195. /**
  196. * Constructs a new Pico instance
  197. *
  198. * To carry out all the processing in Pico, call {@link Pico::run()}.
  199. *
  200. * @param string $rootDir root directory of this Pico instance
  201. * @param string $configDir config directory of this Pico instance
  202. * @param string $pluginsDir plugins directory of this Pico instance
  203. * @param string $themesDir themes directory of this Pico instance
  204. */
  205. public function __construct($rootDir, $configDir, $pluginsDir, $themesDir)
  206. {
  207. $this->rootDir = rtrim($rootDir, '/\\') . '/';
  208. $this->configDir = $this->getAbsolutePath($configDir);
  209. $this->pluginsDir = $this->getAbsolutePath($pluginsDir);
  210. $this->themesDir = $this->getAbsolutePath($themesDir);
  211. }
  212. /**
  213. * Returns the root directory of this Pico instance
  214. *
  215. * @return string root directory path
  216. */
  217. public function getRootDir()
  218. {
  219. return $this->rootDir;
  220. }
  221. /**
  222. * Returns the config directory of this Pico instance
  223. *
  224. * @return string config directory path
  225. */
  226. public function getConfigDir()
  227. {
  228. return $this->configDir;
  229. }
  230. /**
  231. * Returns the plugins directory of this Pico instance
  232. *
  233. * @return string plugins directory path
  234. */
  235. public function getPluginsDir()
  236. {
  237. return $this->pluginsDir;
  238. }
  239. /**
  240. * Returns the themes directory of this Pico instance
  241. *
  242. * @return string themes directory path
  243. */
  244. public function getThemesDir()
  245. {
  246. return $this->themesDir;
  247. }
  248. /**
  249. * Runs this Pico instance
  250. *
  251. * Loads plugins, evaluates the config file, does URL routing, parses
  252. * meta headers, processes Markdown, does Twig processing and returns
  253. * the rendered contents.
  254. *
  255. * @return string rendered Pico contents
  256. * @throws Exception thrown when a not recoverable error occurs
  257. */
  258. public function run()
  259. {
  260. // lock Pico
  261. $this->locked = true;
  262. // load plugins
  263. $this->loadPlugins();
  264. $this->triggerEvent('onPluginsLoaded', array(&$this->plugins));
  265. // load config
  266. $this->loadConfig();
  267. $this->triggerEvent('onConfigLoaded', array(&$this->config));
  268. // check content dir
  269. if (!is_dir($this->getConfig('content_dir'))) {
  270. throw new RuntimeException('Invalid content directory "' . $this->getConfig('content_dir') . '"');
  271. }
  272. // evaluate request url
  273. $this->evaluateRequestUrl();
  274. $this->triggerEvent('onRequestUrl', array(&$this->requestUrl));
  275. // discover requested file
  276. $this->requestFile = $this->resolveFilePath($this->requestUrl);
  277. $this->triggerEvent('onRequestFile', array(&$this->requestFile));
  278. // load raw file content
  279. $this->triggerEvent('onContentLoading', array(&$this->requestFile));
  280. $notFoundFile = '404' . $this->getConfig('content_ext');
  281. if (file_exists($this->requestFile) && (basename($this->requestFile) !== $notFoundFile)) {
  282. $this->rawContent = $this->loadFileContent($this->requestFile);
  283. } else {
  284. $this->triggerEvent('on404ContentLoading', array(&$this->requestFile));
  285. header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
  286. $this->rawContent = $this->load404Content($this->requestFile);
  287. $this->triggerEvent('on404ContentLoaded', array(&$this->rawContent));
  288. }
  289. $this->triggerEvent('onContentLoaded', array(&$this->rawContent));
  290. // parse file meta
  291. $headers = $this->getMetaHeaders();
  292. $this->triggerEvent('onMetaParsing', array(&$this->rawContent, &$headers));
  293. $this->meta = $this->parseFileMeta($this->rawContent, $headers);
  294. $this->triggerEvent('onMetaParsed', array(&$this->meta));
  295. // register parsedown
  296. $this->triggerEvent('onParsedownRegistration');
  297. $this->registerParsedown();
  298. // parse file content
  299. $this->triggerEvent('onContentParsing', array(&$this->rawContent));
  300. $this->content = $this->prepareFileContent($this->rawContent, $this->meta);
  301. $this->triggerEvent('onContentPrepared', array(&$this->content));
  302. $this->content = $this->parseFileContent($this->content);
  303. $this->triggerEvent('onContentParsed', array(&$this->content));
  304. // read pages
  305. $this->triggerEvent('onPagesLoading');
  306. $this->readPages();
  307. $this->sortPages();
  308. $this->discoverCurrentPage();
  309. $this->triggerEvent('onPagesLoaded', array(
  310. &$this->pages,
  311. &$this->currentPage,
  312. &$this->previousPage,
  313. &$this->nextPage
  314. ));
  315. // register twig
  316. $this->triggerEvent('onTwigRegistration');
  317. $this->registerTwig();
  318. // render template
  319. $this->twigVariables = $this->getTwigVariables();
  320. if (isset($this->meta['template']) && $this->meta['template']) {
  321. $templateName = $this->meta['template'];
  322. } else {
  323. $templateName = 'index';
  324. }
  325. if (file_exists($this->getThemesDir() . $this->getConfig('theme') . '/' . $templateName . '.twig')) {
  326. $templateName .= '.twig';
  327. } else {
  328. $templateName .= '.html';
  329. }
  330. $this->triggerEvent('onPageRendering', array(&$this->twig, &$this->twigVariables, &$templateName));
  331. $output = $this->twig->render($templateName, $this->twigVariables);
  332. $this->triggerEvent('onPageRendered', array(&$output));
  333. return $output;
  334. }
  335. /**
  336. * Loads plugins from Pico::$pluginsDir in alphabetical order
  337. *
  338. * Plugin files MAY be prefixed by a number (e.g. 00-PicoDeprecated.php)
  339. * to indicate their processing order. Plugins without a prefix will be
  340. * loaded last. If you want to use a prefix, you MUST consider the
  341. * following directives:
  342. * - 00 to 19: Reserved
  343. * - 20 to 39: Low level code helper plugins
  344. * - 40 to 59: Plugins manipulating routing or the pages array
  345. * - 60 to 79: Plugins hooking into template or markdown parsing
  346. * - 80 to 99: Plugins using the `onPageRendered` event
  347. *
  348. * @see Pico::loadPlugin()
  349. * @see Pico::getPlugin()
  350. * @see Pico::getPlugins()
  351. * @return void
  352. * @throws RuntimeException thrown when a plugin couldn't be loaded
  353. */
  354. protected function loadPlugins()
  355. {
  356. // scope isolated require_once()
  357. $includeClosure = function ($pluginFile) {
  358. require_once($pluginFile);
  359. };
  360. if (PHP_VERSION_ID >= 50400) {
  361. $includeClosure = $includeClosure->bindTo(null);
  362. }
  363. $this->plugins = array();
  364. $pluginFiles = $this->getFiles($this->getPluginsDir(), '.php');
  365. foreach ($pluginFiles as $pluginFile) {
  366. $includeClosure($pluginFile);
  367. $className = preg_replace('/^[0-9]+-/', '', basename($pluginFile, '.php'));
  368. if (class_exists($className)) {
  369. // class name and file name can differ regarding case sensitivity
  370. $plugin = new $className($this);
  371. $className = get_class($plugin);
  372. $this->plugins[$className] = $plugin;
  373. } else {
  374. // TODO: breaks backward compatibility
  375. /*
  376. $pluginFileName = substr($pluginFile, strlen($this->getPluginsDir()));
  377. throw new RuntimeException(
  378. "Unable to load plugin '" . $className . "' "
  379. . "from '" . $pluginFileName . "'"
  380. );
  381. */
  382. }
  383. }
  384. }
  385. /**
  386. * Manually loads a plugin
  387. *
  388. * Manually loaded plugins must implement {@see PicoPluginInterface}.
  389. *
  390. * @see Pico::loadPlugins()
  391. * @see Pico::getPlugin()
  392. * @see Pico::getPlugins()
  393. * @param PicoPluginInterface|string $plugin either the class name of a
  394. * plugin to instantiate or a plugin instance
  395. * @return PicoPluginInterface instance of the loaded plugin
  396. * @throws RuntimeException thrown when a plugin couldn't
  397. * be loaded
  398. */
  399. public function loadPlugin($plugin)
  400. {
  401. if (!is_object($plugin)) {
  402. $className = (string) $plugin;
  403. if (class_exists($className)) {
  404. $plugin = new $className($this);
  405. } else {
  406. throw new RuntimeException("Unable to load plugin '" . $className . "': Class not found");
  407. }
  408. }
  409. $className = get_class($plugin);
  410. if (!($plugin instanceof PicoPluginInterface)) {
  411. throw new RuntimeException(
  412. "Unable to load plugin '" . $className . "': "
  413. . "Manually loaded plugins must implement 'PicoPluginInterface'"
  414. );
  415. }
  416. if ($this->plugins === null) {
  417. $this->plugins = array();
  418. }
  419. $this->plugins[$className] = $plugin;
  420. return $plugin;
  421. }
  422. /**
  423. * Returns the instance of a named plugin
  424. *
  425. * Plugins SHOULD implement {@link PicoPluginInterface}, but you MUST NOT
  426. * rely on it. For more information see {@link PicoPluginInterface}.
  427. *
  428. * @see Pico::loadPlugins()
  429. * @see Pico::getPlugins()
  430. * @param string $pluginName name of the plugin
  431. * @return object instance of the plugin
  432. * @throws RuntimeException thrown when the plugin wasn't found
  433. */
  434. public function getPlugin($pluginName)
  435. {
  436. if (isset($this->plugins[$pluginName])) {
  437. return $this->plugins[$pluginName];
  438. }
  439. throw new RuntimeException("Missing plugin '" . $pluginName . "'");
  440. }
  441. /**
  442. * Returns all loaded plugins
  443. *
  444. * @see Pico::loadPlugins()
  445. * @see Pico::getPlugin()
  446. * @return object[]|null
  447. */
  448. public function getPlugins()
  449. {
  450. return $this->plugins;
  451. }
  452. /**
  453. * Loads the config.php and any *.config.php from Pico::$configDir
  454. *
  455. * After loading the {@path "config/config.php"}, Pico proceeds with any
  456. * existing `config/*.config.php` in alphabetical order. The file order is
  457. * crucial: Config values which has been set already, cannot be overwritten
  458. * by a succeeding file. This is also true for arrays, i.e. when specifying
  459. * `$config['test'] = array('foo' => 'bar')` in `config/a.config.php` and
  460. * `$config['test'] = array('baz' => 42)` in `config/b.config.php`,
  461. * `$config['test']['baz']` will be undefined!
  462. *
  463. * @see Pico::setConfig()
  464. * @see Pico::getConfig()
  465. * @return void
  466. */
  467. protected function loadConfig()
  468. {
  469. // scope isolated require()
  470. $includeClosure = function ($configFile) {
  471. require($configFile);
  472. return (isset($config) && is_array($config)) ? $config : array();
  473. };
  474. if (PHP_VERSION_ID >= 50400) {
  475. $includeClosure = $includeClosure->bindTo(null);
  476. }
  477. // load main config file (config/config.php)
  478. $this->config = is_array($this->config) ? $this->config : array();
  479. if (file_exists($this->getConfigDir() . 'config.php')) {
  480. $this->config += $includeClosure($this->getConfigDir() . 'config.php');
  481. }
  482. // merge $config of config/*.config.php files
  483. $configFiles = glob($this->getConfigDir() . '?*.config.php', GLOB_MARK);
  484. if ($configFiles) {
  485. foreach ($configFiles as $configFile) {
  486. if (substr($configFile, -1) !== '/') {
  487. $this->config += $includeClosure($configFile);
  488. }
  489. }
  490. }
  491. // merge default config
  492. $this->config += array(
  493. 'site_title' => 'Pico',
  494. 'base_url' => '',
  495. 'rewrite_url' => null,
  496. 'theme' => 'default',
  497. 'date_format' => '%D %T',
  498. 'twig_config' => array('cache' => false, 'autoescape' => false, 'debug' => false),
  499. 'pages_order_by' => 'alpha',
  500. 'pages_order' => 'asc',
  501. 'content_dir' => null,
  502. 'content_ext' => '.md',
  503. 'timezone' => ''
  504. );
  505. if (empty($this->config['base_url'])) {
  506. $this->config['base_url'] = $this->getBaseUrl();
  507. } else {
  508. $this->config['base_url'] = rtrim($this->config['base_url'], '/') . '/';
  509. }
  510. if ($this->config['rewrite_url'] === null) {
  511. $this->config['rewrite_url'] = $this->isUrlRewritingEnabled();
  512. }
  513. if (empty($this->config['content_dir'])) {
  514. // try to guess the content directory
  515. if (is_dir($this->getRootDir() . 'content')) {
  516. $this->config['content_dir'] = $this->getRootDir() . 'content/';
  517. } else {
  518. $this->config['content_dir'] = $this->getRootDir() . 'content-sample/';
  519. }
  520. } else {
  521. $this->config['content_dir'] = $this->getAbsolutePath($this->config['content_dir']);
  522. }
  523. if (empty($this->config['theme_url'])) {
  524. $this->config['theme_url'] = $this->getBaseThemeUrl();
  525. } elseif (preg_match('#^[A-Za-z][A-Za-z0-9+\-.]*://#', $this->config['theme_url'])) {
  526. $this->config['theme_url'] = rtrim($this->config['theme_url'], '/') . '/';
  527. } else {
  528. $this->config['theme_url'] = $this->getBaseUrl() . rtrim($this->config['theme_url'], '/') . '/';
  529. }
  530. if (empty($this->config['timezone'])) {
  531. // explicitly set a default timezone to prevent a E_NOTICE
  532. // when no timezone is set; the `date_default_timezone_get()`
  533. // function always returns a timezone, at least UTC
  534. $this->config['timezone'] = @date_default_timezone_get();
  535. }
  536. date_default_timezone_set($this->config['timezone']);
  537. }
  538. /**
  539. * Sets Pico's config before calling Pico::run()
  540. *
  541. * This method allows you to modify Pico's config without creating a
  542. * {@path "config/config.php"} or changing some of its variables before
  543. * Pico starts processing.
  544. *
  545. * You can call this method between {@link Pico::__construct()} and
  546. * {@link Pico::run()} only. Options set with this method cannot be
  547. * overwritten by {@path "config/config.php"}.
  548. *
  549. * @see Pico::loadConfig()
  550. * @see Pico::getConfig()
  551. * @param array $config array with config variables
  552. * @return void
  553. * @throws LogicException thrown if Pico already started processing
  554. */
  555. public function setConfig(array $config)
  556. {
  557. if ($this->locked) {
  558. throw new LogicException("You cannot modify Pico's config after processing has started");
  559. }
  560. $this->config = $config;
  561. }
  562. /**
  563. * Returns either the value of the specified config variable or
  564. * the config array
  565. *
  566. * @see Pico::setConfig()
  567. * @see Pico::loadConfig()
  568. * @param string $configName optional name of a config variable
  569. * @return mixed returns either the value of the named config
  570. * variable, null if the config variable doesn't exist or the config
  571. * array if no config name was supplied
  572. */
  573. public function getConfig($configName = null)
  574. {
  575. if ($configName !== null) {
  576. return isset($this->config[$configName]) ? $this->config[$configName] : null;
  577. } else {
  578. return $this->config;
  579. }
  580. }
  581. /**
  582. * Evaluates the requested URL
  583. *
  584. * Pico uses the `QUERY_STRING` routing method (e.g. `/pico/?sub/page`)
  585. * to support SEO-like URLs out-of-the-box with any webserver. You can
  586. * still setup URL rewriting to basically remove the `?` from URLs.
  587. * However, URL rewriting requires some special configuration of your
  588. * webserver, but this should be "basic work" for any webmaster...
  589. *
  590. * With Pico 1.0 you had to setup URL rewriting (e.g. using `mod_rewrite`
  591. * on Apache) in a way that rewritten URLs follow the `QUERY_STRING`
  592. * principles. Starting with version 1.1, Pico additionally supports the
  593. * `REQUEST_URI` routing method, what allows you to simply rewrite all
  594. * requests to just `index.php`. Pico then reads the requested page from
  595. * the `REQUEST_URI` environment variable provided by the webserver.
  596. * Please note that `QUERY_STRING` takes precedence over `REQUEST_URI`.
  597. *
  598. * Pico 0.9 and older required Apache with `mod_rewrite` enabled, thus old
  599. * plugins, templates and contents may require you to enable URL rewriting
  600. * to work. If you're upgrading from Pico 0.9, you will probably have to
  601. * update your rewriting rules.
  602. *
  603. * We recommend you to use the `link` filter in templates to create
  604. * internal links, e.g. `{{ "sub/page"|link }}` is equivalent to
  605. * `{{ base_url }}/sub/page` and `{{ base_url }}?sub/page`, depending on
  606. * enabled URL rewriting. In content files you can use the `%base_url%`
  607. * variable; e.g. `%base_url%?sub/page` will be replaced accordingly.
  608. *
  609. * Heads up! Pico always interprets the first parameter as name of the
  610. * requested page (provided that the parameter has no value). According to
  611. * that you MUST NOT call Pico with a parameter without value as first
  612. * parameter (e.g. http://example.com/pico/?someBooleanParam), otherwise
  613. * Pico interprets `someBooleanParam` as name of the requested page. Use
  614. * `/pico/?someBooleanParam=` or `/pico/?index&someBooleanParam` instead.
  615. *
  616. * @see Pico::getRequestUrl()
  617. * @return void
  618. */
  619. protected function evaluateRequestUrl()
  620. {
  621. // use QUERY_STRING; e.g. /pico/?sub/page
  622. $pathComponent = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '';
  623. if (!empty($pathComponent)) {
  624. if (($pathComponentLength = strpos($pathComponent, '&')) !== false) {
  625. $pathComponent = substr($pathComponent, 0, $pathComponentLength);
  626. }
  627. if (strpos($pathComponent, '=') === false) {
  628. $this->requestUrl = trim(rawurldecode($pathComponent), '/');
  629. }
  630. }
  631. // use REQUEST_URI (requires URL rewriting); e.g. /pico/sub/page
  632. if (($this->requestUrl === null) && $this->isUrlRewritingEnabled()) {
  633. $basePath = dirname($_SERVER['SCRIPT_NAME']) . '/';
  634. $basePathLength = strlen($basePath);
  635. $requestUri = $_SERVER['REQUEST_URI'];
  636. if (substr($requestUri, 0, $basePathLength) === $basePath) {
  637. $requestUri = substr($requestUri, $basePathLength);
  638. if (($requestUriLength = strpos($requestUri, '?')) !== false) {
  639. $requestUri = substr($requestUri, 0, $requestUriLength);
  640. }
  641. $this->requestUrl = rtrim(rawurldecode($requestUri), '/');
  642. }
  643. }
  644. }
  645. /**
  646. * Returns the URL where a user requested the page
  647. *
  648. * @see Pico::evaluateRequestUrl()
  649. * @return string|null request URL
  650. */
  651. public function getRequestUrl()
  652. {
  653. return $this->requestUrl;
  654. }
  655. /**
  656. * Resolves a given file path to its corresponding content file
  657. *
  658. * This method also prevents `content_dir` breakouts using malicious
  659. * request URLs. We don't use `realpath()`, because we neither want to
  660. * check for file existance, nor prohibit symlinks which intentionally
  661. * point to somewhere outside the `content_dir` folder. It is STRONGLY
  662. * RECOMMENDED to use PHP's `open_basedir` feature - always, not just
  663. * with Pico!
  664. *
  665. * @see Pico::getRequestFile()
  666. * @param string $requestUrl path name (likely from a URL) to resolve
  667. * @return string path to the resolved content file
  668. */
  669. public function resolveFilePath($requestUrl)
  670. {
  671. $contentDir = $this->getConfig('content_dir');
  672. $contentExt = $this->getConfig('content_ext');
  673. if (empty($requestUrl)) {
  674. return $contentDir . 'index' . $contentExt;
  675. } else {
  676. // prevent content_dir breakouts
  677. $requestUrl = str_replace('\\', '/', $requestUrl);
  678. $requestUrlParts = explode('/', $requestUrl);
  679. $requestFileParts = array();
  680. foreach ($requestUrlParts as $requestUrlPart) {
  681. if (($requestUrlPart === '') || ($requestUrlPart === '.')) {
  682. continue;
  683. } elseif ($requestUrlPart === '..') {
  684. array_pop($requestFileParts);
  685. continue;
  686. }
  687. $requestFileParts[] = $requestUrlPart;
  688. }
  689. if (empty($requestFileParts)) {
  690. return $contentDir . 'index' . $contentExt;
  691. }
  692. // discover the content file to serve
  693. // Note: $requestFileParts neither contains a trailing nor a leading slash
  694. $requestFile = $contentDir . implode('/', $requestFileParts);
  695. if (is_dir($requestFile)) {
  696. // if no index file is found, try a accordingly named file in the previous dir
  697. // if this file doesn't exist either, show the 404 page, but assume the index
  698. // file as being requested (maintains backward compatibility to Pico < 1.0)
  699. $indexFile = $requestFile . '/index' . $contentExt;
  700. if (file_exists($indexFile) || !file_exists($requestFile . $contentExt)) {
  701. return $indexFile;
  702. }
  703. }
  704. return $requestFile . $contentExt;
  705. }
  706. }
  707. /**
  708. * Returns the absolute path to the content file to serve
  709. *
  710. * @see Pico::resolveFilePath()
  711. * @return string|null file path
  712. */
  713. public function getRequestFile()
  714. {
  715. return $this->requestFile;
  716. }
  717. /**
  718. * Returns the raw contents of a file
  719. *
  720. * @see Pico::getRawContent()
  721. * @param string $file file path
  722. * @return string raw contents of the file
  723. */
  724. public function loadFileContent($file)
  725. {
  726. return file_get_contents($file);
  727. }
  728. /**
  729. * Returns the raw contents of the first found 404 file when traversing
  730. * up from the directory the requested file is in
  731. *
  732. * If no suitable `404.md` is found, fallback to a built-in error message.
  733. *
  734. * @see Pico::getRawContent()
  735. * @param string $file path to requested (but not existing) file
  736. * @return string raw contents of the 404 file
  737. */
  738. public function load404Content($file)
  739. {
  740. $contentDir = $this->getConfig('content_dir');
  741. $contentDirLength = strlen($contentDir);
  742. $contentExt = $this->getConfig('content_ext');
  743. if (substr($file, 0, $contentDirLength) === $contentDir) {
  744. $errorFileDir = substr($file, $contentDirLength);
  745. while ($errorFileDir !== '.') {
  746. $errorFileDir = dirname($errorFileDir);
  747. $errorFile = $errorFileDir . '/404' . $contentExt;
  748. if (file_exists($contentDir . $errorFile)) {
  749. return $this->loadFileContent($contentDir . $errorFile);
  750. }
  751. }
  752. } elseif (file_exists($contentDir . '404' . $contentExt)) {
  753. // provided that the requested file is not in the regular
  754. // content directory, fallback to Pico's global `404.md`
  755. return $this->loadFileContent($contentDir . '404' . $contentExt);
  756. }
  757. // fallback to built-in error message
  758. $rawErrorContent = "---\n"
  759. . "Title: Error 404\n"
  760. . "Robots: noindex,nofollow\n"
  761. . "---\n\n"
  762. . "# Error 404\n\n"
  763. . "Woops. Looks like this page doesn't exist.\n";
  764. return $rawErrorContent;
  765. }
  766. /**
  767. * Returns the raw contents, either of the requested or the 404 file
  768. *
  769. * @see Pico::loadFileContent()
  770. * @see Pico::load404Content()
  771. * @return string|null raw contents
  772. */
  773. public function getRawContent()
  774. {
  775. return $this->rawContent;
  776. }
  777. /**
  778. * Returns known meta headers and triggers the onMetaHeaders event
  779. *
  780. * Heads up! Calling this method triggers the `onMetaHeaders` event.
  781. * Keep this in mind to prevent a infinite loop!
  782. *
  783. * @return string[] known meta headers; the array value specifies the
  784. * YAML key to search for, the array key is later used to access the
  785. * found value
  786. */
  787. public function getMetaHeaders()
  788. {
  789. $headers = array(
  790. 'title' => 'Title',
  791. 'description' => 'Description',
  792. 'author' => 'Author',
  793. 'date' => 'Date',
  794. 'robots' => 'Robots',
  795. 'template' => 'Template'
  796. );
  797. $this->triggerEvent('onMetaHeaders', array(&$headers));
  798. return $headers;
  799. }
  800. /**
  801. * Parses the file meta from raw file contents
  802. *
  803. * Meta data MUST start on the first line of the file, either opened and
  804. * closed by `---` or C-style block comments (deprecated). The headers are
  805. * parsed by the YAML component of the Symfony project, keys are lowered.
  806. * If you're a plugin developer, you MUST register new headers during the
  807. * `onMetaHeaders` event first. The implicit availability of headers is
  808. * for users and pure (!) theme developers ONLY.
  809. *
  810. * @see Pico::getFileMeta()
  811. * @see http://symfony.com/doc/current/components/yaml/introduction.html
  812. * @param string $rawContent the raw file contents
  813. * @param string[] $headers known meta headers
  814. * @return array parsed meta data
  815. * @throws \Symfony\Component\Yaml\Exception\ParseException thrown when the
  816. * meta data is invalid
  817. */
  818. public function parseFileMeta($rawContent, array $headers)
  819. {
  820. $meta = array();
  821. $pattern = "/^(\/(\*)|---)[[:blank:]]*(?:\r)?\n"
  822. . "(?:(.*?)(?:\r)?\n)?(?(2)\*\/|---)[[:blank:]]*(?:(?:\r)?\n|$)/s";
  823. if (preg_match($pattern, $rawContent, $rawMetaMatches) && isset($rawMetaMatches[3])) {
  824. $yamlParser = new \Symfony\Component\Yaml\Parser();
  825. $meta = $yamlParser->parse($rawMetaMatches[3]);
  826. if ($meta !== null) {
  827. // the parser may return a string for non-YAML 1-liners
  828. // assume that this string is the page title
  829. $meta = is_array($meta) ? array_change_key_case($meta, CASE_LOWER) : array('title' => $meta);
  830. } else {
  831. $meta = array();
  832. }
  833. foreach ($headers as $fieldId => $fieldName) {
  834. $fieldName = strtolower($fieldName);
  835. if (isset($meta[$fieldName])) {
  836. // rename field (e.g. remove whitespaces)
  837. if ($fieldId != $fieldName) {
  838. $meta[$fieldId] = $meta[$fieldName];
  839. unset($meta[$fieldName]);
  840. }
  841. } elseif (!isset($meta[$fieldId])) {
  842. // guarantee array key existance
  843. $meta[$fieldId] = '';
  844. }
  845. }
  846. if (!empty($meta['date'])) {
  847. // workaround for issue #336
  848. // Symfony YAML interprets ISO-8601 datetime strings and returns timestamps instead of the string
  849. // this behavior conforms to the YAML standard, i.e. this is no bug of Symfony YAML
  850. if (is_int($meta['date'])) {
  851. $meta['time'] = $meta['date'];
  852. $rawDateFormat = (date('H:i:s', $meta['time']) === '00:00:00') ? 'Y-m-d' : 'Y-m-d H:i:s';
  853. $meta['date'] = date($rawDateFormat, $meta['time']);
  854. } else {
  855. $meta['time'] = strtotime($meta['date']);
  856. }
  857. $meta['date_formatted'] = utf8_encode(strftime($this->getConfig('date_format'), $meta['time']));
  858. } else {
  859. $meta['time'] = $meta['date_formatted'] = '';
  860. }
  861. } else {
  862. // guarantee array key existance
  863. $meta = array_fill_keys(array_keys($headers), '');
  864. $meta['time'] = $meta['date_formatted'] = '';
  865. }
  866. return $meta;
  867. }
  868. /**
  869. * Returns the parsed meta data of the requested page
  870. *
  871. * @see Pico::parseFileMeta()
  872. * @return array|null parsed meta data
  873. */
  874. public function getFileMeta()
  875. {
  876. return $this->meta;
  877. }
  878. /**
  879. * Registers the Parsedown Extra markdown parser
  880. *
  881. * @see Pico::getParsedown()
  882. * @return void
  883. */
  884. protected function registerParsedown()
  885. {
  886. $this->parsedown = new ParsedownExtra();
  887. }
  888. /**
  889. * Returns the Parsedown Extra markdown parser
  890. *
  891. * @see Pico::registerParsedown()
  892. * @return ParsedownExtra|null Parsedown Extra markdown parser
  893. */
  894. public function getParsedown()
  895. {
  896. return $this->parsedown;
  897. }
  898. /**
  899. * Applies some static preparations to the raw contents of a page,
  900. * e.g. removing the meta header and replacing %base_url%
  901. *
  902. * @see Pico::parseFileContent()
  903. * @see Pico::getFileContent()
  904. * @param string $rawContent raw contents of a page
  905. * @param array $meta meta data to use for %meta.*% replacement
  906. * @return string contents prepared for parsing
  907. */
  908. public function prepareFileContent($rawContent, array $meta)
  909. {
  910. $variables = array();
  911. // remove meta header
  912. $metaHeaderPattern = "/^(\/(\*)|---)[[:blank:]]*(?:\r)?\n"
  913. . "(?:(.*?)(?:\r)?\n)?(?(2)\*\/|---)[[:blank:]]*(?:(?:\r)?\n|$)/s";
  914. $content = preg_replace($metaHeaderPattern, '', $rawContent, 1);
  915. // replace %version%
  916. $variables['%version%'] = static::VERSION;
  917. // replace %site_title%
  918. $variables['%site_title%'] = $this->getConfig('site_title');
  919. // replace %base_url%
  920. if ($this->isUrlRewritingEnabled()) {
  921. // always use `%base_url%?sub/page` syntax for internal links
  922. // we'll replace the links accordingly, depending on enabled rewriting
  923. $variables['%base_url%?'] = $this->getBaseUrl();
  924. } else {
  925. // actually not necessary, but makes the URL look a little nicer
  926. $variables['%base_url%?'] = $this->getBaseUrl() . '?';
  927. }
  928. $variables['%base_url%'] = rtrim($this->getBaseUrl(), '/');
  929. // replace %theme_url%
  930. $variables['%theme_url%'] = $this->getBaseThemeUrl() . $this->getConfig('theme');
  931. // replace %meta.*%
  932. if (!empty($meta)) {
  933. foreach ($meta as $metaKey => $metaValue) {
  934. if (is_scalar($metaValue) || ($metaValue === null)) {
  935. $variables['%meta.' . $metaKey . '%'] = strval($metaValue);
  936. }
  937. }
  938. }
  939. $content = str_replace(array_keys($variables), $variables, $content);
  940. return $content;
  941. }
  942. /**
  943. * Parses the contents of a page using ParsedownExtra
  944. *
  945. * @see Pico::prepareFileContent()
  946. * @see Pico::getFileContent()
  947. * @param string $content raw contents of a page (Markdown)
  948. * @return string parsed contents (HTML)
  949. */
  950. public function parseFileContent($content)
  951. {
  952. if ($this->parsedown === null) {
  953. throw new LogicException("Unable to parse file contents: Parsedown instance wasn't registered yet");
  954. }
  955. return $this->parsedown->text($content);
  956. }
  957. /**
  958. * Returns the cached contents of the requested page
  959. *
  960. * @see Pico::prepareFileContent()
  961. * @see Pico::parseFileContent()
  962. * @return string|null parsed contents
  963. */
  964. public function getFileContent()
  965. {
  966. return $this->content;
  967. }
  968. /**
  969. * Reads the data of all pages known to Pico
  970. *
  971. * The page data will be an array containing the following values:
  972. *
  973. * | Array key | Type | Description |
  974. * | -------------- | ------ | ---------------------------------------- |
  975. * | id | string | relative path to the content file |
  976. * | url | string | URL to the page |
  977. * | title | string | title of the page (YAML header) |
  978. * | description | string | description of the page (YAML header) |
  979. * | author | string | author of the page (YAML header) |
  980. * | time | string | timestamp derived from the Date header |
  981. * | date | string | date of the page (YAML header) |
  982. * | date_formatted | string | formatted date of the page |
  983. * | raw_content | string | raw, not yet parsed contents of the page |
  984. * | meta | string | parsed meta data of the page |
  985. *
  986. * @see Pico::sortPages()
  987. * @see Pico::getPages()
  988. * @return void
  989. */
  990. protected function readPages()
  991. {
  992. $contentDir = $this->getConfig('content_dir');
  993. $contentDirLength = strlen($contentDir);
  994. $contentExt = $this->getConfig('content_ext');
  995. $contentExtLength = strlen($contentExt);
  996. $this->pages = array();
  997. $files = $this->getFiles($contentDir, $contentExt, Pico::SORT_NONE);
  998. foreach ($files as $i => $file) {
  999. // skip 404 page
  1000. if (basename($file) === '404' . $contentExt) {
  1001. unset($files[$i]);
  1002. continue;
  1003. }
  1004. $id = substr($file, $contentDirLength, -$contentExtLength);
  1005. // trigger onSinglePageLoading event
  1006. $this->triggerEvent('onSinglePageLoading', array(&$id));
  1007. // drop inaccessible pages (e.g. drop "sub.md" if "sub/index.md" exists)
  1008. $conflictFile = $contentDir . $id . '/index' . $contentExt;
  1009. if (in_array($conflictFile, $files, true)) {
  1010. continue;
  1011. }
  1012. $url = $this->getPageUrl($id);
  1013. if ($file !== $this->requestFile) {
  1014. $rawContent = file_get_contents($file);
  1015. $headers = $this->getMetaHeaders();
  1016. try {
  1017. $meta = $this->parseFileMeta($rawContent, $headers);
  1018. } catch (\Symfony\Component\Yaml\Exception\ParseException $e) {
  1019. $meta = $this->parseFileMeta('', $headers);
  1020. $meta['YAML_ParseError'] = $e->getMessage();
  1021. }
  1022. } else {
  1023. $rawContent = &$this->rawContent;
  1024. $meta = &$this->meta;
  1025. }
  1026. // build page data
  1027. // title, description, author and date are assumed to be pretty basic data
  1028. // everything else is accessible through $page['meta']
  1029. $page = array(
  1030. 'id' => $id,
  1031. 'url' => $url,
  1032. 'title' => &$meta['title'],
  1033. 'description' => &$meta['description'],
  1034. 'author' => &$meta['author'],
  1035. 'time' => &$meta['time'],
  1036. 'date' => &$meta['date'],
  1037. 'date_formatted' => &$meta['date_formatted'],
  1038. 'raw_content' => &$rawContent,
  1039. 'meta' => &$meta
  1040. );
  1041. if ($file === $this->requestFile) {
  1042. $page['content'] = &$this->content;
  1043. }
  1044. unset($rawContent, $meta);
  1045. // trigger onSinglePageLoaded event
  1046. $this->triggerEvent('onSinglePageLoaded', array(&$page));
  1047. if ($page !== null) {
  1048. $this->pages[$id] = $page;
  1049. }
  1050. }
  1051. }
  1052. /**
  1053. * Sorts all pages known to Pico
  1054. *
  1055. * @see Pico::readPages()
  1056. * @see Pico::getPages()
  1057. * @return void
  1058. */
  1059. protected function sortPages()
  1060. {
  1061. // sort pages
  1062. $order = $this->getConfig('pages_order');
  1063. $alphaSortClosure = function ($a, $b) use ($order) {
  1064. $aSortKey = (basename($a['id']) === 'index') ? dirname($a['id']) : $a['id'];
  1065. $bSortKey = (basename($b['id']) === 'index') ? dirname($b['id']) : $b['id'];
  1066. $cmp = strcmp($aSortKey, $bSortKey);
  1067. return $cmp * (($order === 'desc') ? -1 : 1);
  1068. };
  1069. if ($this->getConfig('pages_order_by') === 'date') {
  1070. // sort by date
  1071. uasort($this->pages, function ($a, $b) use ($alphaSortClosure, $order) {
  1072. if (empty($a['time']) || empty($b['time'])) {
  1073. $cmp = (empty($a['time']) - empty($b['time']));
  1074. } else {
  1075. $cmp = ($b['time'] - $a['time']);
  1076. }
  1077. if ($cmp === 0) {
  1078. // never assume equality; fallback to alphabetical order
  1079. return $alphaSortClosure($a, $b);
  1080. }
  1081. return $cmp * (($order === 'desc') ? 1 : -1);
  1082. });
  1083. } else {
  1084. // sort alphabetically
  1085. uasort($this->pages, $alphaSortClosure);
  1086. }
  1087. }
  1088. /**
  1089. * Returns the list of known pages
  1090. *
  1091. * @see Pico::readPages()
  1092. * @see Pico::sortPages()
  1093. * @return array[]|null the data of all pages
  1094. */
  1095. public function getPages()
  1096. {
  1097. return $this->pages;
  1098. }
  1099. /**
  1100. * Walks through the list of known pages and discovers the requested page
  1101. * as well as the previous and next page relative to it
  1102. *
  1103. * @see Pico::getCurrentPage()
  1104. * @see Pico::getPreviousPage()
  1105. * @see Pico::getNextPage()
  1106. * @return void
  1107. */
  1108. protected function discoverCurrentPage()
  1109. {
  1110. $pageIds = array_keys($this->pages);
  1111. $contentDir = $this->getConfig('content_dir');
  1112. $contentDirLength = strlen($contentDir);
  1113. // the requested file is not in the regular content directory, therefore its ID
  1114. // isn't specified and it's impossible to determine the current page automatically
  1115. if (substr($this->requestFile, 0, $contentDirLength) !== $contentDir) {
  1116. return;
  1117. }
  1118. $currentPageId = substr($this->requestFile, $contentDirLength, -strlen($this->getConfig('content_ext')));
  1119. $currentPageIndex = array_search($currentPageId, $pageIds);
  1120. if ($currentPageIndex !== false) {
  1121. $this->currentPage = &$this->pages[$currentPageId];
  1122. if (($this->getConfig('order_by') === 'date') && ($this->getConfig('order') === 'desc')) {
  1123. $previousPageOffset = 1;
  1124. $nextPageOffset = -1;
  1125. } else {
  1126. $previousPageOffset = -1;
  1127. $nextPageOffset = 1;
  1128. }
  1129. if (isset($pageIds[$currentPageIndex + $previousPageOffset])) {
  1130. $previousPageId = $pageIds[$currentPageIndex + $previousPageOffset];
  1131. $this->previousPage = &$this->pages[$previousPageId];
  1132. }
  1133. if (isset($pageIds[$currentPageIndex + $nextPageOffset])) {
  1134. $nextPageId = $pageIds[$currentPageIndex + $nextPageOffset];
  1135. $this->nextPage = &$this->pages[$nextPageId];
  1136. }
  1137. }
  1138. }
  1139. /**
  1140. * Returns the data of the requested page
  1141. *
  1142. * @see Pico::discoverCurrentPage()
  1143. * @return array|null page data
  1144. */
  1145. public function getCurrentPage()
  1146. {
  1147. return $this->currentPage;
  1148. }
  1149. /**
  1150. * Returns the data of the previous page relative to the page being served
  1151. *
  1152. * @see Pico::discoverCurrentPage()
  1153. * @return array|null page data
  1154. */
  1155. public function getPreviousPage()
  1156. {
  1157. return $this->previousPage;
  1158. }
  1159. /**
  1160. * Returns the data of the next page relative to the page being served
  1161. *
  1162. * @see Pico::discoverCurrentPage()
  1163. * @return array|null page data
  1164. */
  1165. public function getNextPage()
  1166. {
  1167. return $this->nextPage;
  1168. }
  1169. /**
  1170. * Registers the twig template engine
  1171. *
  1172. * This method also registers Pico's core Twig filters `link` and `content`
  1173. * as well as Pico's {@link PicoTwigExtension} Twig extension.
  1174. *
  1175. * @see Pico::getTwig()
  1176. * @return void
  1177. */
  1178. protected function registerTwig()
  1179. {
  1180. $twigLoader = new Twig_Loader_Filesystem($this->getThemesDir() . $this->getConfig('theme'));
  1181. $this->twig = new Twig_Environment($twigLoader, $this->getConfig('twig_config'));
  1182. $this->twig->addExtension(new Twig_Extension_Debug());
  1183. $this->twig->addExtension(new PicoTwigExtension($this));
  1184. // register link filter and the url_param and form_param functions
  1185. $this->twig->addFilter(new Twig_SimpleFilter('link', array($this, 'getPageUrl')));
  1186. $this->twig->addFunction(new Twig_SimpleFunction('url_param', array($this, 'getUrlParameter')));
  1187. $this->twig->addFunction(new Twig_SimpleFunction('form_param', array($this, 'getFormParameter')));
  1188. // register content filter
  1189. // we pass the $pages array by reference to prevent multiple parser runs for the same page
  1190. // this is the reason why we can't register this filter as part of PicoTwigExtension
  1191. $pico = $this;
  1192. $pages = &$this->pages;
  1193. $this->twig->addFilter(new Twig_SimpleFilter('content', function ($page) use ($pico, &$pages) {
  1194. if (isset($pages[$page])) {
  1195. $pageData = &$pages[$page];
  1196. if (!isset($pageData['content'])) {
  1197. $pageData['content'] = $pico->prepareFileContent($pageData['raw_content'], $pageData['meta']);
  1198. $pageData['content'] = $pico->parseFileContent($pageData['content']);
  1199. }
  1200. return $pageData['content'];
  1201. }
  1202. return null;
  1203. }));
  1204. }
  1205. /**
  1206. * Returns the twig template engine
  1207. *
  1208. * @see Pico::registerTwig()
  1209. * @return Twig_Environment|null Twig template engine
  1210. */
  1211. public function getTwig()
  1212. {
  1213. return $this->twig;
  1214. }
  1215. /**
  1216. * Returns the variables passed to the template
  1217. *
  1218. * URLs and paths (namely `base_dir`, `base_url`, `theme_dir` and
  1219. * `theme_url`) don't add a trailing slash for historic reasons.
  1220. *
  1221. * @return array template variables
  1222. */
  1223. protected function getTwigVariables()
  1224. {
  1225. $frontPage = $this->getConfig('content_dir') . 'index' . $this->getConfig('content_ext');
  1226. return array(
  1227. 'config' => $this->getConfig(),
  1228. 'base_dir' => rtrim($this->getRootDir(), '/'),
  1229. 'base_url' => rtrim($this->getBaseUrl(), '/'),
  1230. 'theme_dir' => $this->getThemesDir() . $this->getConfig('theme'),
  1231. 'theme_url' => $this->getBaseThemeUrl() . $this->getConfig('theme'),
  1232. 'rewrite_url' => $this->isUrlRewritingEnabled(),
  1233. 'site_title' => $this->getConfig('site_title'),
  1234. 'meta' => $this->meta,
  1235. 'content' => $this->content,
  1236. 'pages' => $this->pages,
  1237. 'prev_page' => $this->previousPage,
  1238. 'current_page' => $this->currentPage,
  1239. 'next_page' => $this->nextPage,
  1240. 'is_front_page' => ($this->requestFile === $frontPage),
  1241. 'version' => static::VERSION
  1242. );
  1243. }
  1244. /**
  1245. * Returns the base URL of this Pico instance
  1246. *
  1247. * @return string the base url
  1248. */
  1249. public function getBaseUrl()
  1250. {
  1251. $baseUrl = $this->getConfig('base_url');
  1252. if (!empty($baseUrl)) {
  1253. return $baseUrl;
  1254. }
  1255. $protocol = 'http';
  1256. if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
  1257. $secureProxyHeader = strtolower(current(explode(',', $_SERVER['HTTP_X_FORWARDED_PROTO'])));
  1258. $protocol = in_array($secureProxyHeader, array('https', 'on', 'ssl', '1')) ? 'https' : 'http';
  1259. } elseif (!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] !== 'off')) {
  1260. $protocol = 'https';
  1261. } elseif ($_SERVER['SERVER_PORT'] == 443) {
  1262. $protocol = 'https';
  1263. }
  1264. $this->config['base_url'] =
  1265. $protocol . "://" . $_SERVER['HTTP_HOST']
  1266. . rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\') . '/';
  1267. return $this->config['base_url'];
  1268. }
  1269. /**
  1270. * Returns true if URL rewriting is enabled
  1271. *
  1272. * @return boolean true if URL rewriting is enabled, false otherwise
  1273. */
  1274. public function isUrlRewritingEnabled()
  1275. {
  1276. $urlRewritingEnabled = $this->getConfig('rewrite_url');
  1277. if ($urlRewritingEnabled !== null) {
  1278. return $urlRewritingEnabled;
  1279. }
  1280. $this->config['rewrite_url'] = (isset($_SERVER['PICO_URL_REWRITING']) && $_SERVER['PICO_URL_REWRITING']);
  1281. return $this->config['rewrite_url'];
  1282. }
  1283. /**
  1284. * Returns the URL to a given page
  1285. *
  1286. * This method can be used in Twig templates by applying the `link` filter
  1287. * to a string representing a page identifier.
  1288. *
  1289. * @param string $page identifier of the page to link to
  1290. * @param array|string $queryData either an array containing properties to
  1291. * create a URL-encoded query string from, or a already encoded string
  1292. * @param boolean $dropIndex when the last path component is "index",
  1293. * then passing TRUE (default) leads to removing this path component
  1294. * @return string URL
  1295. */
  1296. public function getPageUrl($page, $queryData = null, $dropIndex = true)
  1297. {
  1298. if (is_array($queryData)) {
  1299. $queryData = http_build_query($queryData, '', '&');
  1300. } elseif (($queryData !== null) && !is_string($queryData)) {
  1301. throw new InvalidArgumentException(
  1302. 'Argument 2 passed to ' . get_called_class() . '::getPageUrl() must be of the type array or string, '
  1303. . (is_object($queryData) ? get_class($queryData) : gettype($queryData)) . ' given'
  1304. );
  1305. }
  1306. // drop "index"
  1307. if ($dropIndex) {
  1308. if ($page === 'index') {
  1309. $page = '';
  1310. } elseif (($pagePathLength = strrpos($page, '/')) !== false) {
  1311. if (substr($page, $pagePathLength + 1) === 'index') {
  1312. $page = substr($page, 0, $pagePathLength);
  1313. }
  1314. }
  1315. }
  1316. if (!empty($queryData)) {
  1317. $queryData = ($this->isUrlRewritingEnabled() || empty($page)) ? '?' . $queryData : '&' . $queryData;
  1318. }
  1319. if (empty($page)) {
  1320. return $this->getBaseUrl() . $queryData;
  1321. } elseif (!$this->isUrlRewritingEnabled()) {
  1322. return $this->getBaseUrl() . '?' . rawurlencode($page) . $queryData;
  1323. } else {
  1324. return $this->getBaseUrl() . implode('/', array_map('rawurlencode', explode('/', $page))) . $queryData;
  1325. }
  1326. }
  1327. /**
  1328. * Returns the URL of the themes folder of this Pico instance
  1329. *
  1330. * We assume that the themes folder is a arbitrary deep sub folder of the
  1331. * script's base path (i.e. the directory {@path "index.php"} is in resp.
  1332. * the `httpdocs` directory). Usually the script's base path is identical
  1333. * to {@link Pico::$rootDir}, but this may aberrate when Pico got installed
  1334. * as a composer dependency. However, ultimately it allows us to use
  1335. * {@link Pico::getBaseUrl()} as origin of the theme URL. Otherwise Pico
  1336. * falls back to the basename of {@link Pico::$themesDir} (i.e. assuming
  1337. * that `Pico::$themesDir` is `foo/bar/baz`, the base URL of the themes
  1338. * folder will be `baz/`; this ensures BC to Pico < 1.1). Pico's base URL
  1339. * always gets prepended appropriately.
  1340. *
  1341. * @return string the URL of the themes folder
  1342. */
  1343. public function getBaseThemeUrl()
  1344. {
  1345. $themeUrl = $this->getConfig('theme_url');
  1346. if (!empty($themeUrl)) {
  1347. return $themeUrl;
  1348. }
  1349. $basePath = dirname($_SERVER['SCRIPT_FILENAME']) . '/';
  1350. $basePathLength = strlen($basePath);
  1351. if (substr($this->getThemesDir(), 0, $basePathLength) === $basePath) {
  1352. $this->config['theme_url'] = $this->getBaseUrl() . substr($this->getThemesDir(), $basePathLength);
  1353. } else {
  1354. $this->config['theme_url'] = $this->getBaseUrl() . basename($this->getThemesDir()) . '/';
  1355. }
  1356. return $this->config['theme_url'];
  1357. }
  1358. /**
  1359. * Filters a URL GET parameter with a specified filter
  1360. *
  1361. * This method is just an alias for {@link Pico::filterVariable()}, see
  1362. * {@link Pico::filterVariable()} for a detailed description. It can be
  1363. * used in Twig templates by calling the `url_param` function.
  1364. *
  1365. * @see Pico::filterVariable()
  1366. * @param string $name name of the URL GET parameter
  1367. * to filter
  1368. * @param int|string $filter the filter to apply
  1369. * @param mixed|array $options either a associative options
  1370. * array to be used by the filter or a scalar default value
  1371. * @param int|string|int[]|string[] $flags flags and flag strings to
  1372. * be used by the filter
  1373. * @return mixed either the filtered data,
  1374. * FALSE if the filter fails, or NULL if the URL GET parameter doesn't
  1375. * exist and no default value is given
  1376. */
  1377. public function getUrlParameter($name, $filter = '', $options = null, $flags = null)
  1378. {
  1379. $variable = (isset($_GET[$name]) && is_scalar($_GET[$name])) ? $_GET[$name] : null;
  1380. return $this->filterVariable($variable, $filter, $options, $flags);
  1381. }
  1382. /**
  1383. * Filters a HTTP POST parameter with a specified filter
  1384. *
  1385. * This method is just an alias for {@link Pico::filterVariable()}, see
  1386. * {@link Pico::filterVariable()} for a detailed description. It can be
  1387. * used in Twig templates by calling the `form_param` function.
  1388. *
  1389. * @see Pico::filterVariable()
  1390. * @param string $name name of the HTTP POST
  1391. * parameter to filter
  1392. * @param int|string $filter the filter to apply
  1393. * @param mixed|array $options either a associative options
  1394. * array to be used by the filter or a scalar default value
  1395. * @param int|string|int[]|string[] $flags flags and flag strings to
  1396. * be used by the filter
  1397. * @return mixed either the filtered data,
  1398. * FALSE if the filter fails, or NULL if the HTTP POST parameter
  1399. * doesn't exist and no default value is given
  1400. */
  1401. public function getFormParameter($name, $filter = '', $options = null, $flags = null)
  1402. {
  1403. $variable = (isset($_POST[$name]) && is_scalar($_POST[$name])) ? $_POST[$name] : null;
  1404. return $this->filterVariable($variable, $filter, $options, $flags);
  1405. }
  1406. /**
  1407. * Filters a variable with a specified filter
  1408. *
  1409. * This method basically wraps around PHP's `filter_var()` function. It
  1410. * filters data by either validating or sanitizing it. This is especially
  1411. * useful when the data source contains unknown (or foreign) data, like
  1412. * user supplied input. Validation is used to validate or check if the data
  1413. * meets certain qualifications, but will not change the data itself.
  1414. * Sanitization will sanitize the data, so it may alter it by removing
  1415. * undesired characters. It doesn't actually validate the data! The
  1416. * behaviour of most filters can optionally be tweaked by flags.
  1417. *
  1418. * Heads up! Input validation is hard! Always validate your input data the
  1419. * most paranoid way you can imagine. Always prefer validation filters over
  1420. * sanitization filters; be very careful with sanitization filters, you
  1421. * might create cross-site scripting vulnerabilities!
  1422. *
  1423. * @see https://secure.php.net/manual/en/function.filter-var.php
  1424. * PHP's `filter_var()` function
  1425. * @see https://secure.php.net/manual/en/filter.filters.validate.php
  1426. * Validate filters
  1427. * @see https://secure.php.net/manual/en/filter.filters.sanitize.php
  1428. * Sanitize filters
  1429. * @param mixed $variable value to filter
  1430. * @param int|string $filter ID (int) or name (string) of
  1431. * the filter to apply; if omitted, the method will return FALSE
  1432. * @param mixed|array $options either a associative array
  1433. * of options to be used by the filter (e.g. `array('default' => 42)`),
  1434. * or a scalar default value that will be returned when the passed
  1435. * value is NULL (optional)
  1436. * @param int|string|int[]|string[] $flags either a bitwise disjunction
  1437. * of flags or a string with the significant part of a flag constant
  1438. * (the constant name is the result of "FILTER_FLAG_" and the given
  1439. * string in ASCII-only uppercase); you may also pass an array of flags
  1440. * and flag strings (optional)
  1441. * @return mixed with a validation filter,
  1442. * the method either returns the validated value or, provided that the
  1443. * value wasn't valid, the given default value or FALSE; with a
  1444. * sanitization filter, the method returns the sanitized value; if no
  1445. * value (i.e. NULL) was given, the method always returns either the
  1446. * provided default value or NULL
  1447. */
  1448. protected function filterVariable($variable, $filter = '', $options = null, $flags = null)
  1449. {
  1450. $defaultValue = null;
  1451. if (is_array($options)) {
  1452. $defaultValue = isset($options['default']) ? $options['default'] : null;
  1453. } elseif ($options !== null) {
  1454. $defaultValue = $options;
  1455. $options = array('default' => $defaultValue);
  1456. }
  1457. if ($variable === null) {
  1458. return $defaultValue;
  1459. }
  1460. $filter = !empty($filter) ? (is_string($filter) ? filter_id($filter) : (int) $filter) : false;
  1461. if (!$filter) {
  1462. return false;
  1463. }
  1464. $filterOptions = array('options' => $options, 'flags' => 0);
  1465. foreach ((array) $flags as $flag) {
  1466. if (is_numeric($flag)) {
  1467. $filterOptions['flags'] |= (int) $flag;
  1468. } elseif (is_string($flag)) {
  1469. $flag = strtoupper(preg_replace('/[^a-zA-Z0-9_]/', '', $flag));
  1470. if (($flag === 'NULL_ON_FAILURE') && ($filter === FILTER_VALIDATE_BOOLEAN)) {
  1471. $filterOptions['flags'] |= FILTER_NULL_ON_FAILURE;
  1472. } else {
  1473. $filterOptions['flags'] |= (int) constant('FILTER_FLAG_' . $flag);
  1474. }
  1475. }
  1476. }
  1477. return filter_var($variable, $filter, $filterOptions);
  1478. }
  1479. /**
  1480. * Recursively walks through a directory and returns all containing files
  1481. * matching the specified file extension
  1482. *
  1483. * @param string $directory start directory
  1484. * @param string $fileExtension return files with the given file extension
  1485. * only (optional)
  1486. * @param int $order specify whether and how files should be
  1487. * sorted; use Pico::SORT_ASC for a alphabetical ascending order (this
  1488. * is the default behaviour), Pico::SORT_DESC for a descending order
  1489. * or Pico::SORT_NONE to leave the result unsorted
  1490. * @return array list of found files
  1491. */
  1492. public function getFiles($directory, $fileExtension = '', $order = self::SORT_ASC)
  1493. {
  1494. $directory = rtrim($directory, '/');
  1495. $result = array();
  1496. // scandir() reads files in alphabetical order
  1497. $files = scandir($directory, $order);
  1498. $fileExtensionLength = strlen($fileExtension);
  1499. if ($files !== false) {
  1500. foreach ($files as $file) {
  1501. // exclude hidden files/dirs starting with a .; this also excludes the special dirs . and ..
  1502. // exclude files ending with a ~ (vim/nano backup) or # (emacs backup)
  1503. if (($file[0] === '.') || in_array(substr($file, -1), array('~', '#'))) {
  1504. continue;
  1505. }
  1506. if (is_dir($directory . '/' . $file)) {
  1507. // get files recursively
  1508. $result = array_merge($result, $this->getFiles($directory . '/' . $file, $fileExtension, $order));
  1509. } elseif (empty($fileExtension) || (substr($file, -$fileExtensionLength) === $fileExtension)) {
  1510. $result[] = $directory . '/' . $file;
  1511. }
  1512. }
  1513. }
  1514. return $result;
  1515. }
  1516. /**
  1517. * Makes a relative path absolute to Pico's root dir
  1518. *
  1519. * This method also guarantees a trailing slash.
  1520. *
  1521. * @param string $path relative or absolute path
  1522. * @return string absolute path
  1523. */
  1524. public function getAbsolutePath($path)
  1525. {
  1526. if (strncasecmp(PHP_OS, 'WIN', 3) === 0) {
  1527. if (preg_match('/^([a-zA-Z]:\\\\|\\\\\\\\)/', $path) !== 1) {
  1528. $path = $this->getRootDir() . $path;
  1529. }
  1530. } else {
  1531. if ($path[0] !== '/') {
  1532. $path = $this->getRootDir() . $path;
  1533. }
  1534. }
  1535. return rtrim($path, '/\\') . '/';
  1536. }
  1537. /**
  1538. * Triggers events on plugins which implement PicoPluginInterface
  1539. *
  1540. * Deprecated events (as used by plugins not implementing
  1541. * {@link PicoPluginInterface}) are triggered by {@link PicoDeprecated}.
  1542. * You MUST NOT trigger events of Pico's core with a plugin!
  1543. *
  1544. * @see PicoPluginInterface
  1545. * @see AbstractPicoPlugin
  1546. * @see DummyPlugin
  1547. * @param string $eventName name of the event to trigger
  1548. * @param array $params optional parameters to pass
  1549. * @return void
  1550. */
  1551. public function triggerEvent($eventName, array $params = array())
  1552. {
  1553. if (!empty($this->plugins)) {
  1554. foreach ($this->plugins as $plugin) {
  1555. // only trigger events for plugins that implement PicoPluginInterface
  1556. // deprecated events (plugins for Pico 0.9 and older) will be triggered by `PicoDeprecated`
  1557. if ($plugin instanceof PicoPluginInterface) {
  1558. $plugin->handleEvent($eventName, $params);
  1559. }
  1560. }
  1561. }
  1562. }
  1563. }