From 1c89b6cb4f74af4e3922d630ced93e779fce373f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Dufraisse?= Date: Wed, 15 Mar 2023 14:53:43 +0100 Subject: [PATCH] fix(App/rewrite): detect when at root --- App.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/App.php b/App.php index f6dedbe..b3dba3f 100644 --- a/App.php +++ b/App.php @@ -173,7 +173,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 = []; @@ -193,6 +196,8 @@ class App } $nbLevels = count(explode('/',$configDir)) -1; $config['rewrite_url'] = true; + } else { + $this->appendContentToServerfNeeded($nbLevels,$config); } } } @@ -201,4 +206,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; + } } \ No newline at end of file