You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
3.8 KiB
137 lines
3.8 KiB
<?php
|
|
|
|
/**
|
|
* SPDX-License-Identifier: EUPL-1.2
|
|
* Authors: see /README.md
|
|
*/
|
|
|
|
namespace SeaCMS;
|
|
|
|
use Exception;
|
|
use Pico;
|
|
use SeacmsAppPlugin;
|
|
use Throwable;
|
|
|
|
set_error_handler(function (
|
|
int $errno,
|
|
string $errstr,
|
|
string $errfile = '',
|
|
int $errline = -1
|
|
) {
|
|
if (!isset($GLOBALS['errorMessages'])){
|
|
$GLOBALS['errorMessages']= [];
|
|
}
|
|
$GLOBALS['errorMessages'][] = <<<HTML
|
|
<div>
|
|
Error : $errstr<br>
|
|
in file '$errfile' (line '$errline').<br/>
|
|
ErrCode : $errno
|
|
</div>
|
|
|
|
HTML;
|
|
return true;
|
|
});
|
|
|
|
class App
|
|
{
|
|
/**
|
|
* plugins path in vendor
|
|
* @var string
|
|
*/
|
|
public const PLUGINS_PATH = 'vendor/picocms/plugins/';
|
|
/**
|
|
* themes path in vendor
|
|
* @var string
|
|
*/
|
|
public const THEMES_PATH = 'vendor/picocms/themes/';
|
|
/**
|
|
* Pico instance.
|
|
* @var Pico
|
|
*/
|
|
protected $pico;
|
|
|
|
public function __construct(string $contentFolderFromRoot)
|
|
{
|
|
// sanitize content folder
|
|
$cwd = getcwd();
|
|
if (empty($contentFolderFromRoot)){
|
|
$contentFolderFromRoot = 'content';
|
|
} else {
|
|
$contentFolderFromRoot = str_replace('\\','/',$contentFolderFromRoot);
|
|
}
|
|
if (!is_dir($cwd)){
|
|
throw new Exception("getcwd returned a path that is not a directory !");
|
|
}
|
|
if (!is_dir("$cwd/$contentFolderFromRoot")){
|
|
$contentFolderFromRoot = 'vendor/picocms/pico/content-sample';
|
|
}
|
|
if (substr($contentFolderFromRoot,-1) !== '/'){
|
|
$contentFolderFromRoot .= '/';
|
|
}
|
|
// instance Pico
|
|
$this->pico = new Pico(
|
|
$cwd, // root dir
|
|
$contentFolderFromRoot, // config dir
|
|
self::PLUGINS_PATH, // plugins dir
|
|
self::THEMES_PATH // themes dir
|
|
);
|
|
$this->pico->loadPlugin(new SeacmsAppPlugin($this->pico));
|
|
}
|
|
|
|
public function runPico(): string
|
|
{
|
|
if (!empty($GLOBALS['PicoVendorsDirectoryRelativeLevels']) &&
|
|
intval($GLOBALS['PicoVendorsDirectoryRelativeLevels']) > 0){
|
|
$previous = implode('',array_fill(0,intval($GLOBALS['PicoVendorsDirectoryRelativeLevels']),'../'));
|
|
$this->pico->setConfig([
|
|
'themes_url' => $previous.self::THEMES_PATH,
|
|
'plugins_url' => $previous.self::PLUGINS_PATH
|
|
]);
|
|
|
|
}
|
|
return $this->pico->run();
|
|
}
|
|
|
|
/**
|
|
* instanciate Pico and run it then echo output
|
|
* @param string $contentFolderFromRoot where is the root folder
|
|
*/
|
|
public static function run(string $contentFolderFromRoot)
|
|
{
|
|
try {
|
|
$app = new App($contentFolderFromRoot);
|
|
$output = $app->runPico();
|
|
self::appendErrorMessagesIfNeeded($output);
|
|
} catch (Throwable $th) {
|
|
$output = <<<HTML
|
|
<div style="color:red;">
|
|
Exception : {$th->__toString()}
|
|
</div>
|
|
HTML;
|
|
} finally {
|
|
echo $output;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* append error messages from $GLOBALS['errorMessages']
|
|
* only if $_GET['debug'] === 'yes'
|
|
* @param string &$output
|
|
*/
|
|
protected static function appendErrorMessagesIfNeeded(string &$output)
|
|
{
|
|
if (!empty($_GET['debug']) && $_GET['debug'] === 'yes' && !empty($GLOBALS['errorMessages'])){
|
|
$formattedMessages = implode("\n", $GLOBALS['errorMessages']);
|
|
$formattedMessages = <<<HTML
|
|
<div style="background-color:navajowhite;">
|
|
$formattedMessages
|
|
</div>
|
|
HTML;
|
|
if (preg_match('/<\/body>/i', $output, $match)) {
|
|
$output = str_replace($match[0], $formattedMessages.$match[0], $output);
|
|
} else {
|
|
$output = $output.$formattedMessages;
|
|
}
|
|
}
|
|
}
|
|
} |