1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Zikula package. |
7
|
|
|
* |
8
|
|
|
* Copyright Zikula Foundation - https://ziku.la/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Zikula\ExtensionsModule\Controller; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
20
|
|
|
use Symfony\Component\Routing\RouterInterface; |
21
|
|
|
use Zikula\Bundle\CoreBundle\Controller\AbstractController; |
22
|
|
|
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface; |
23
|
|
|
use Zikula\Bundle\CoreBundle\Response\PlainResponse; |
24
|
|
|
use Zikula\ThemeModule\Engine\Annotation\Theme; |
25
|
|
|
use Zikula\ThemeModule\Engine\AssetFilter; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @Route("/help") |
29
|
|
|
*/ |
30
|
|
|
class HelpController extends AbstractController |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @Route("/{moduleName}") |
34
|
|
|
* @Theme("admin") |
35
|
|
|
* |
36
|
|
|
* Display a module's help page. |
37
|
|
|
*/ |
38
|
|
|
public function indexAction( |
39
|
|
|
ZikulaHttpKernelInterface $kernel, |
40
|
|
|
Request $request, |
41
|
|
|
Filesystem $fileSystem, |
42
|
|
|
RouterInterface $router, |
43
|
|
|
AssetFilter $assetFilter, |
44
|
|
|
string $moduleName |
45
|
|
|
): Response |
46
|
|
|
{ |
47
|
|
|
$page = $request->query->get('page', 'README'); |
48
|
|
|
if (false !== mb_strpos($page, '..')) { |
49
|
|
|
throw new \Exception('Invalid page "' . $page . '".'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$locale = $request->getLocale(); |
53
|
|
|
$extension = $kernel->getBundle($moduleName); |
54
|
|
|
if (null === $extension) { |
55
|
|
|
throw new \Exception('Invalid extension "' . $moduleName . '".'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$helpPath = $extension->getPath() . '/Resources/docs/help/'; |
59
|
|
|
|
60
|
|
|
// check if requested page exists |
61
|
|
|
if (!$fileSystem->exists($helpPath . $locale . '/' . $page . '.md')) { |
62
|
|
|
if ('en' === $locale) { |
63
|
|
|
throw new \Exception('Invalid page "' . $page . '".'); |
64
|
|
|
} |
65
|
|
|
// fallback to English |
66
|
|
|
$locale = 'en'; |
67
|
|
|
} |
68
|
|
|
if (!$fileSystem->exists($helpPath . $locale . '/' . $page . '.md')) { |
69
|
|
|
throw new \Exception('Invalid page "' . $page . '".'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$raw = $request->query->getInt('raw', 0); |
73
|
|
|
|
74
|
|
|
$content = file_get_contents($helpPath . $locale . '/' . $page . '.md'); |
75
|
|
|
|
76
|
|
|
// rewrite local links |
77
|
|
|
$content = preg_replace_callback( |
78
|
|
|
'/\[(.*?)\]\((.*?)\)/', |
79
|
|
|
function ($match) use($router, $moduleName, $raw) { |
80
|
|
|
if (false === mb_strpos($match[2], '.md')) { |
81
|
|
|
return $match[0]; |
82
|
|
|
} |
83
|
|
|
if ('http' === mb_substr($match[2], 0, 4)) { |
84
|
|
|
return $match[0]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// local link - rewrite |
88
|
|
|
$urlArgs = [ |
89
|
|
|
'moduleName' => $moduleName, |
90
|
|
|
'page' => trim($match[2], '.md') |
91
|
|
|
]; |
92
|
|
|
if (1 === $raw) { |
93
|
|
|
$urlArgs['raw'] = 1; |
94
|
|
|
} |
95
|
|
|
$url = $router->generate('zikulaextensionsmodule_help_index', $urlArgs); |
96
|
|
|
|
97
|
|
|
return '[' . $match[1] . '](' . $url . ')'; |
98
|
|
|
}, |
99
|
|
|
$content |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$output = $this->renderView('@ZikulaExtensionsModule/Help/page.html.twig', [ |
103
|
|
|
'moduleName' => $moduleName, |
104
|
|
|
'content' => $content, |
105
|
|
|
'raw' => $raw |
106
|
|
|
]); |
107
|
|
|
|
108
|
|
|
if (1 === $raw) { |
109
|
|
|
$output = $assetFilter->filter($output); |
110
|
|
|
$output = new PlainResponse($output); |
111
|
|
|
|
112
|
|
|
return $output; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return new Response($output); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|