fix(App/rewrite): detect when at root

This commit is contained in:
Jérémy Dufraisse 2023-03-15 14:53:43 +01:00
parent 262012d5d4
commit 19b51b382b

30
App.php
View File

@ -172,7 +172,10 @@ class App
$config['rewrite_url'] = true;
$nbLevels = count(explode('/',$configDir.$requestUrl));
} elseif (!empty($_SERVER['SCRIPT_NAME']) && substr($_SERVER['SCRIPT_NAME'],-strlen($supposedEndUrlForNotRewrite)) == $supposedEndUrlForNotRewrite) {
$config['rewrite_url'] = true;
$nbLevels = count(explode('/',$configDir)) -1;
} else {
$this->appendContentToServerfNeeded($nbLevels,$config);
}
} elseif (!empty($_SERVER['SCRIPT_NAME']) && is_string($_SERVER['SCRIPT_NAME'])) {
$matches = [];
@ -192,6 +195,8 @@ class App
}
$nbLevels = count(explode('/',$configDir)) -1;
$config['rewrite_url'] = true;
} else {
$this->appendContentToServerfNeeded($nbLevels,$config);
}
}
}
@ -200,4 +205,29 @@ class App
$config['plugins_url'] = $previous.$config['plugins_url'];
$pico->setConfig($config);
}
/**
* append 'content' to $_SERVER var
* @param int &$nbLevels
* @param array &$config
* @return bool
*/
protected function appendContentToServerfNeeded(int &$nbLevels, array &$config): bool
{
if (!empty($_SERVER['SCRIPT_NAME']) && substr($_SERVER['SCRIPT_NAME'],-strlen('index.php')) == 'index.php' &&
!empty($_SERVER['SCRIPT_FILENAME']) &&
is_file(dirname($_SERVER['SCRIPT_FILENAME']).'/content/index.php')) {
if (!empty($_SERVER['SCRIPT_FILENAME'])){
$_SERVER['SCRIPT_FILENAME'] = str_replace('index.php','content/index.php',$_SERVER['SCRIPT_FILENAME']);
}
if (!empty($_SERVER['REQUEST_URI'])){
$_SERVER['REQUEST_URI'] = str_replace(['?','//content/'],['/content/?','/content/'],$_SERVER['REQUEST_URI']);
}
$_SERVER['SCRIPT_NAME'] = dirname($_SERVER['SCRIPT_NAME']).'/content/index.php';
$nbLevels = 1;
$config['rewrite_url'] = true;
return true;
}
return false;
}
}