55 * This file is part of the TechDivision.DocViewer package.
66 */
77use TechDivision \DocViewer \Exceptions \ParsingNotAllowedException ;
8+ use TechDivision \DocViewer \Util ;
89use TYPO3 \Flow \Annotations as Flow ;
10+ use TYPO3 \Flow \Mvc \Controller \ControllerContext ;
11+ use TYPO3 \Flow \Object \ObjectManagerInterface ;
912
1013class Parser {
1114
1215 /**
13- * Suffix for all resources to avid webserver's file delivering
1416 * @var string
1517 */
16- protected static $ resourceSuffix = ' __docviwer ' ;
18+ protected $ baseUri ;
1719
1820 /**
19- * @var string
21+ * @var ObjectManagerInterface
22+ * @Flow\Inject
2023 */
21- protected $ baseUri ;
22-
23- public function __construct ($ baseUri ) {
24- $ this ->baseUri = $ baseUri ;
25- }
24+ protected $ objectManager ;
2625
2726 /**
28- * @param Node $node
29- * @return bool
27+ * @var ControllerContext
3028 */
31- public function isAllowed ($ node ) {
32-
33- $ nodeInfo = $ node ->getInfo ();
34-
35- if (is_array ($ nodeInfo ) && isset ($ nodeInfo ['extension ' ])) {
36- return in_array ($ node ->getInfo ()['extension ' ], $ this ->markdownFileExtensions );
37- }
38- return false ;
39- }
29+ protected $ controllerContext ;
4030
4131 /**
4232 * Files which are allowed for parsing as markdown
@@ -46,41 +36,27 @@ public function isAllowed($node) {
4636 protected $ markdownFileExtensions ;
4737
4838 /**
49- * Encode a file path so ensure webserver configuration won't try to deliver a file itself
50- * @param string $path
51- * @return string
52- */
53- public static function urlEncodeFilePath ($ path ) {
54- return urlencode ($ path ) . self ::$ resourceSuffix ;
55- }
56-
57- /**
58- * Decode a file path to ensure webserver configuration won't try to deliver a file itself
59- * @param string $path
60- * @return mixed
39+ * Parser constructor.
40+ * @param string $baseUri
41+ * @param ControllerContext $controllerContext
6142 */
62- public static function urlDecodeFilePath ($ path ) {
63- return preg_replace ('/ ' . self ::$ resourceSuffix .'$/ ' , '' , urldecode ($ path ));
43+ public function __construct ($ baseUri , $ controllerContext ) {
44+ $ this ->baseUri = $ baseUri ;
45+ $ this ->controllerContext = $ controllerContext ;
6446 }
6547
6648 /**
6749 * @param Node $node
68- * @param string $path
69- * @return string
50+ * @return bool
7051 */
71- public static function buildResourceUrl ($ node , $ path = null , $ baseUri = '' ) {
72- if (!$ path ) {
73- // if no path given the node is the resource url itself
74- $ path = $ node ->getPath ();
75- } else {
76- // build paths for relative resources
77- $ sourcePathElements = explode ("/ " , $ node ->getPath ());
78- array_pop ($ sourcePathElements );
79- array_push ($ sourcePathElements , $ path );
80- $ path = join ("/ " , $ sourcePathElements );
81- }
52+ public function isAllowed ($ node ) {
8253
83- return $ baseUri . 'techdivision-docviewer/ ' . $ node ->getPackageKey () . "/ " . self ::urlEncodeFilePath ($ path );
54+ $ nodeInfo = $ node ->getInfo ();
55+
56+ if (is_array ($ nodeInfo ) && isset ($ nodeInfo ['extension ' ])) {
57+ return in_array ($ node ->getInfo ()['extension ' ], $ this ->markdownFileExtensions );
58+ }
59+ return false ;
8460 }
8561
8662 /**
@@ -95,7 +71,7 @@ protected function replaceSrcValues($dom, $node) {
9571 function ($ matches ) use ($ node ) {
9672 $ src = $ matches [1 ];
9773 if (strpos ($ src , 'http ' ) !== 0 ) {
98- $ src = self ::buildResourceUrl ($ node , $ src , $ this ->baseUri );
74+ $ src = Util ::buildResourceUrl ($ node , $ src , $ this ->baseUri );
9975 }
10076 return 'src=" ' . $ src . '" ' ;
10177 },
@@ -109,13 +85,15 @@ function ($matches) use ($node) {
10985 * @return mixed
11086 */
11187 protected function replaceHrefValues ($ dom , $ node ) {
88+ $ uriBuilder = $ uriBuilder = $ this ->controllerContext ->getUriBuilder ();
11289 return preg_replace_callback (
11390 '/href\s*=\s*\"(.+?)\"/ ' ,
114- function ($ matches ) use ($ node ) {
91+ function ($ matches ) use ($ node, $ uriBuilder ) {
11592 $ href = $ matches [1 ];
11693 if (strpos ($ href , 'http ' ) !== 0 ) {
11794 $ href = trim ($ href , "./ " );
118- $ href = 'show?moduleArguments%5Bpackage%5D= ' . $ node ->getPackageKey () . '&moduleArguments%5BfilePath%5D= ' . $ href ;
95+ $ uriBuilder ->reset ();
96+ $ href = $ uriBuilder ->uriFor ('show ' , array ('package ' => $ node ->getPackageKey (), 'filePath ' => $ href ), null , null , null );
11997 }
12098 return 'href=" ' . $ href . '" ' ;
12199 },
0 commit comments