feat(App): manage rewrite

This commit is contained in:
Jérémy Dufraisse 2023-02-27 10:47:31 +01:00
parent 398e9406a4
commit e321e02c7f

80
App.php
View File

@ -76,19 +76,11 @@ class App
self::THEMES_PATH // themes dir self::THEMES_PATH // themes dir
); );
$this->pico->loadPlugin(new SeacmsAppPlugin($this->pico)); $this->pico->loadPlugin(new SeacmsAppPlugin($this->pico));
$this->update_SERVERIfNeeded($this->pico, $contentFolderFromRoot);
} }
public function runPico(): string 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(); return $this->pico->run();
} }
@ -134,4 +126,74 @@ class App
} }
} }
} }
/**
* detect if rewrite mode then update $_SERVER in consequence
* @see Pico:evaluateRequestUrl()
*
* @param Pico $pico
* @param string $configDir
*/
protected function update_SERVERIfNeeded(Pico $pico, string $configDir)
{
$requestUrl = null;
$config = [
'rewrite_url' => false,
'themes_url' => self::THEMES_PATH,
'plugins_url' => self::PLUGINS_PATH,
];
$nbLevels = 0;
// use QUERY_STRING; e.g. /pico/?sub/page
$pathComponent = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '';
if ($pathComponent) {
$pathComponent = strstr($pathComponent, '&', true) ?: $pathComponent;
if (strpos($pathComponent, '=') === false) {
$requestUrl = trim(rawurldecode($pathComponent), '/');
}
}
if (isset($_SERVER)){
if (!empty($requestUrl)){
$supposedEndUrlForRewrite = $configDir.$requestUrl.'/index.php';
$supposedEndUrlForNotRewrite = $configDir.'index.php';
if (!empty($_SERVER['SCRIPT_NAME']) && substr($_SERVER['SCRIPT_NAME'],-strlen($supposedEndUrlForRewrite)) == $supposedEndUrlForRewrite){
$_SERVER['SCRIPT_NAME'] = str_replace($supposedEndUrlForRewrite,$configDir.'index.php',$_SERVER['SCRIPT_NAME']);
if (!empty($_SERVER['SCRIPT_FILENAME'])){
$_SERVER['SCRIPT_FILENAME'] = str_replace($supposedEndUrlForRewrite,$configDir.'index.php',$_SERVER['SCRIPT_FILENAME']);
}
if (!empty($_SERVER['REQUEST_URI'])){
$_SERVER['REQUEST_URI'] = str_replace([$configDir.$requestUrl.'/',$configDir.$requestUrl],$configDir,$_SERVER['REQUEST_URI']);
}
$config['rewrite_url'] = true;
$nbLevels = count(explode('/',$configDir.$requestUrl));
} elseif (!empty($_SERVER['SCRIPT_NAME']) && substr($_SERVER['SCRIPT_NAME'],-strlen($supposedEndUrlForNotRewrite)) == $supposedEndUrlForNotRewrite) {
$nbLevels = count(explode('/',$configDir)) -1;
}
} elseif (!empty($_SERVER['SCRIPT_NAME']) && is_string($_SERVER['SCRIPT_NAME'])) {
$matches = [];
$configDirForMatch = preg_quote($configDir,'/');
if (preg_match("/^(.*)$configDirForMatch(.*)index.php$/",$_SERVER['SCRIPT_NAME'],$matches)){
$rootPath = $matches[1];
$requestUrl = !empty($matches[2]) ? (substr($matches[2],-1) == '/' ? substr($matches[2],0,-1) : $matches[2]) : '';
if (!empty($requestUrl)){
$supposedEndUrlForRewrite = $configDir.$requestUrl.'/index.php';
if (!empty($_SERVER['SCRIPT_FILENAME'])){
$_SERVER['SCRIPT_FILENAME'] = str_replace($supposedEndUrlForRewrite,$configDir.'index.php',$_SERVER['SCRIPT_FILENAME']);
}
if (!empty($_SERVER['SCRIPT_NAME'])){
$_SERVER['SCRIPT_NAME'] = str_replace($supposedEndUrlForRewrite,$configDir.'index.php',$_SERVER['SCRIPT_NAME']);
}
}
$nbLevels = count(explode('/',$configDir)) -1;
$config['rewrite_url'] = true;
}
}
}
$previous = implode('',array_fill(0,$nbLevels,'../'));
$config['themes_url'] = $previous.$config['themes_url'];
$config['plugins_url'] = $previous.$config['plugins_url'];
$pico->setConfig($config);
}
} }