22
33namespace App \Controller ;
44
5+ use App \FlashMessage ;
56use Twig \Environment as TwigEnvironment ;
7+
68use Psr \Http \Message \ResponseInterface as Response ;
79use Psr \Http \Message \ServerRequestInterface as Request ;
10+ use Slim \Exception \HttpInternalServerErrorException ;
811use Slim \Exception \HttpNotFoundException ;
12+
913use Xenokore \Utility \Helper \StringHelper ;
1014
1115class WikiController {
1216
13- private const WIKI_ROOT = APP_ROOT . '/var/wiki ' ;
14-
1517 private function fixMarkdownHeaderTagsSEO (string $ content ): string
1618 {
1719 return \preg_replace (
@@ -34,8 +36,15 @@ public function wikiPage(
3436 Request $ request ,
3537 Response $ response ,
3638 TwigEnvironment $ twig ,
39+ FlashMessage $ flash ,
3740 ?string $ page = null ,
3841 ){
42+ // Get wiki dir
43+ $ wiki_dir = $ _ENV ['APP_WIKI_STORAGE ' ];
44+ if (empty ($ wiki_dir ) || !\is_dir ($ wiki_dir ) || !\is_readable ($ wiki_dir )){
45+ throw new HttpInternalServerErrorException ($ request , "wiki dir is not accessible " );
46+ }
47+
3948 // Redirect to the "/wiki/home" if no page is given
4049 if ($ page === null ){
4150 $ response = $ response ->withHeader ('Location ' , '/wiki/home ' )->withStatus (302 );
@@ -51,7 +60,7 @@ public function wikiPage(
5160
5261 // Get the markdown file
5362 $ file = null ;
54- foreach (\glob (self :: WIKI_ROOT . '/*.md ' ) as $ file_path ){
63+ foreach (\glob ($ wiki_dir . '/*.md ' ) as $ file_path ){
5564 if (strtolower (\basename ($ file_path )) === strtolower ($ page ) . '.md ' ){
5665 $ file = $ file_path ;
5766 break ;
@@ -74,7 +83,7 @@ public function wikiPage(
7483 // TODO: markdown titles should be #hash-bang linkable
7584
7685 // Get 'sidebar' contents
77- $ sidebar_contents = \file_get_contents (self :: WIKI_ROOT . '/_Sidebar.md ' );
86+ $ sidebar_contents = \file_get_contents ($ wiki_dir . '/_Sidebar.md ' );
7887 if ($ sidebar_contents === false ){
7988 throw new \Exception ("Sidebar markdown file not found " );
8089 }
0 commit comments