Skip to content

Commit 93fc0c8

Browse files
committed
Fixed tree url static resource bug, some refactoring
1 parent bef1dfc commit 93fc0c8

File tree

6 files changed

+79
-56
lines changed

6 files changed

+79
-56
lines changed

Classes/TechDivision/DocViewer/Controller/ModuleController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function showAction($package, $filePath = null) {
124124
}
125125

126126
if($file) {
127-
$parser = new Parser($baseUri);
127+
$parser = new Parser($baseUri, $this->getControllerContext());
128128
$this->view->assign('currentFile', $file);
129129
try {
130130
$documentContent = $parser->parseFile($file);

Classes/TechDivision/DocViewer/Controller/ResourceController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ public function rawAction($package, $filePath) {
4747
}
4848

4949
$docDir = Util::getDocumentPath($this->packageManager->getPackage($package));
50-
$filePath = realpath($docDir . DIRECTORY_SEPARATOR . Parser::urlDecodeFilePath($filePath));
50+
$filePath = realpath($docDir . DIRECTORY_SEPARATOR . Util::urlDecodeFilePath($filePath));
5151

5252
// take care given file path is sub path of the doc dir of the package
53-
if(strpos($filePath, $docDir) === false) {
53+
if(strpos($filePath, $docDir) < 0 || strpos($filePath, $docDir) === false) {
5454
throw new FileNotInsideDocumentationException("You are not allowed to access files outside the documentation folder");
5555
}
5656

Classes/TechDivision/DocViewer/File/Parser.php

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,28 @@
55
* This file is part of the TechDivision.DocViewer package.
66
*/
77
use TechDivision\DocViewer\Exceptions\ParsingNotAllowedException;
8+
use TechDivision\DocViewer\Util;
89
use TYPO3\Flow\Annotations as Flow;
10+
use TYPO3\Flow\Mvc\Controller\ControllerContext;
11+
use TYPO3\Flow\Object\ObjectManagerInterface;
912

1013
class 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
},

Classes/TechDivision/DocViewer/File/Tree.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Tree {
3434
*/
3535
public function __construct(\TYPO3\Flow\Package\PackageInterface $package, $baseUri)
3636
{
37-
$this->parser = new Parser($baseUri);
37+
$this->parser = new Parser($baseUri, null);
3838
$this->rootNode = $this->buildFsNode($package);
3939
}
4040

Classes/TechDivision/DocViewer/Util.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/*
55
* This file is part of the TechDivision.DocViewer package.
66
*/
7+
use TechDivision\DocViewer\File\Node;
78
use TYPO3\Flow\Annotations as Flow;
89

910
class Util {
@@ -15,6 +16,12 @@ class Util {
1516
*/
1617
protected static $docDir = 'Documentation';
1718

19+
/**
20+
* Suffix for all resources to avid webserver's file delivering
21+
* @var string
22+
*/
23+
public static $resourceSuffix = '__docviwer';
24+
1825
/**
1926
* Get the documentation path
2027
*
@@ -28,4 +35,42 @@ public static function getDocumentPath($package) {
2835
}
2936
return $path;
3037
}
38+
39+
/**
40+
* @param Node $node
41+
* @param string $path
42+
* @return string
43+
*/
44+
public static function buildResourceUrl($node, $path = null, $baseUri = '') {
45+
if(!$path) {
46+
// if no path given the node is the resource url itself
47+
$path = $node->getPath();
48+
} else {
49+
// build paths for relative resources
50+
$sourcePathElements = explode("/", $node->getPath());
51+
array_pop($sourcePathElements);
52+
array_push($sourcePathElements, $path);
53+
$path = join("/", $sourcePathElements);
54+
}
55+
56+
return $baseUri . 'techdivision-docviewer/' . $node->getPackageKey() . "/" . self::urlEncodeFilePath($path);
57+
}
58+
59+
/**
60+
* Encode a file path so ensure webserver configuration won't try to deliver a file itself
61+
* @param string $path
62+
* @return string
63+
*/
64+
protected static function urlEncodeFilePath($path) {
65+
return urlencode($path) . self::$resourceSuffix;
66+
}
67+
68+
/**
69+
* Decode a file path to ensure webserver configuration won't try to deliver a file itself
70+
* @param string $path
71+
* @return mixed
72+
*/
73+
public static function urlDecodeFilePath($path) {
74+
return preg_replace('/'. self::$resourceSuffix .'$/', '', urldecode($path));
75+
}
3176
}

Classes/TechDivision/DocViewer/ViewHelpers/ResourceUrlViewHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace TechDivision\DocViewer\ViewHelpers;
33

44
use TechDivision\DocViewer\File\Node;
5-
use TechDivision\DocViewer\File\Parser;
5+
use TechDivision\DocViewer\Util;
66
use TYPO3\Flow\Annotations as Flow;
77
use TYPO3\Fluid\Core\ViewHelper\AbstractViewHelper;
88

@@ -24,6 +24,6 @@ class ResourceUrlViewHelper extends AbstractViewHelper
2424
*/
2525
public function render($package, $filePath)
2626
{
27-
return Parser::buildResourceUrl(new Node($this->packageManager->getPackage($package), $filePath), null, $this->controllerContext->getRequest()->getHttpRequest()->getBaseUri());
27+
return Util::buildResourceUrl(new Node($this->packageManager->getPackage($package), $filePath), null, $this->controllerContext->getRequest()->getHttpRequest()->getBaseUri());
2828
}
2929
}

0 commit comments

Comments
 (0)