dufraissejeremy 2 years ago
parent 00b18b134d
commit 26db1a236d
  1. 194
      App.php

@ -140,65 +140,171 @@ class App
* @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,
{
$continue = true;
$requestUrl = $this->extractRequestUrlFormQueryString();
$data = [
'page' => '',
'rootPath' => '/',
'baseFolder' => $configDir,
'nbLevels' => 0
];
$nbLevels = 0;
$this->extractFromRequestUrl($data,$configDir,$requestUrl,$continue)
->extractFromScriptName($data,$configDir,$continue)
->setUrl($data, $continue, $configDir, $pico);
}
/**
* extract requestUrlFromQueryString
* @return string $requestUrl
*/
protected function extractRequestUrlFormQueryString(): string
{
// 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), '/');
return trim(rawurldecode($pathComponent), '/');
}
}
return '';
}
/**
* test if wanted requestUrl is found
* @param array &$data
* @param string $configDir
* @param string $requestUrl
* @param bool &$continue
* @return self
*
* set continue = false if found
*/
protected function extractFromRequestUrl(array &$data, string $configDir, string $requestUrl, bool &$continue): self
{
if ($continue && !empty($requestUrl)){
$supposedEndUrlForRewrite = $configDir.$requestUrl.'/index.php';
$supposedEndUrlForNotRewrite = $configDir.'index.php';
$matches = [];
$configDirForMatch = preg_quote($configDir,'/');
if ($this->isServerEndedBy($supposedEndUrlForRewrite,'SCRIPT_NAME')){
$data['page'] = $requestUrl;
$data['rootPath'] = substr($_SERVER['SCRIPT_NAME'],0,-strlen($supposedEndUrlForRewrite));
$data['nbLevels'] = count(explode('/',$configDir.$requestUrl));
$continue = false;
} elseif ($this->isServerEndedBy($supposedEndUrlForNotRewrite,'SCRIPT_NAME')){
$data['page'] = $requestUrl;
$data['nbLevels'] = count(explode('/',$configDir)) -1;
$data['rootPath'] = substr($_SERVER['SCRIPT_NAME'],0,-strlen($supposedEndUrlForNotRewrite));
$continue = false;
} elseif ($this->isServerEndedBy('index.php','SCRIPT_NAME') &&
!preg_match("/^(.*)$configDirForMatch(.*)index.php$/",$_SERVER['SCRIPT_NAME'],$matches) &&
!preg_match("/^(.*)\/sites\/(.*)index.php$/",$_SERVER['SCRIPT_NAME'],$matches)) {
$data['page'] = $requestUrl;
$data['nbLevels'] = 0;
$data['rootPath'] = substr($_SERVER['SCRIPT_NAME'],0,-strlen('index.php'));
$continue = false;
}
}
return $this;
}
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;
}
/**
* test if SCRIPT_NAME contain url rewrite
* @param array &$data
* @param string $configDir
* @param bool &$continue
* @return self
*
* set continue = false if found
*/
protected function extractFromScriptName(array &$data, string $configDir, bool &$continue): self
{
if ($continue && !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]) : '';
}
}
return $this;
}
/**
* set SERVER QUERY_STRING
* @param array $data
* @param bool $continue
* @param string $configDir
* @param Pico $pico
* @return $this
*/
protected function setUrl(array $data, bool $continue, string $configDir, Pico $pico): self
{
$config = [
'rewrite_url' => false,
'themes_url' => self::THEMES_PATH,
'plugins_url' => self::PLUGINS_PATH,
];
// SCRIPT_NAME
$rootPath = (empty($data['rootPath']) || !is_string($data['rootPath'])) ? '/' : (
(substr($data['rootPath'],-1) == '/')
? $data['rootPath']
: $data['rootPath'].'/'
);
$_SERVER['SCRIPT_NAME'] = $rootPath.$configDir.'index.php';
$_SERVER['PHP_SELF'] = $_SERVER['SCRIPT_NAME'].($_SERVER['PATH_INFO'] ?? '');
$_SERVER['DOCUMENT_URI'] = $_SERVER['PHP_SELF'];
// QUERY_STRING
$qString = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '';
if (substr($qString,0,strlen($data['page'])+1)==$data['page'].'&'){
$qString = substr($qString,strlen($data['page'])+1);
} elseif ($qString == $data['page']){
$qString = '';
}
if (!empty($data['page'])){
$qString = $data['page'].(empty($qString) ? '' : "&$qString");
}
$_SERVER['QUERY_STRING'] = $qString;
$_SERVER['REQUEST_URI'] = $_SERVER['DOCUMENT_URI'].(empty($_SERVER['QUERY_STRING'])?'':"?{$_SERVER['QUERY_STRING']}");
//FILENAME
$cwd = getcwd();
$fn = str_replace('\\','/',realpath($cwd.'/'.$configDir.'index.php'));
$_SERVER['SCRIPT_FILENAME'] = $fn;
// config
$config['rewrite_url'] = true;
if ($nbLevels == 0){
$nbLevels = 1;
}
$previous = implode('',array_fill(0,$nbLevels,'../'));
$config['themes_url'] = $previous.$config['themes_url'];
$config['plugins_url'] = $previous.$config['plugins_url'];
$pico->setConfig($config);
$dataJSON = json_encode(compact(['data','continue','configDir','pathComponent','qString','cwd','fn','config']));
echo <<<HTML
<script>
console.log($dataJSON)
</script>
HTML;
return $this;
}
/**
* test if $_SERVER[$key] end by $wantedValue
* @param string $wantedValue
* @param string $key
* @return bool
*/
protected function isServerEndedBy(string $wantedValue, string $key): bool
{
return (!empty($_SERVER[$key]) && is_string($_SERVER[$key]) && substr($_SERVER[$key],-strlen($wantedValue)) == $wantedValue);
}
}
Loading…
Cancel
Save