1717class Directory extends AbstractResolver
1818{
1919 /**
20- * { @inheritdoc}
20+ * @inheritdoc
2121 */
2222 public function getIndicator (): string
2323 {
2424 return 'directory ' ;
2525 }
2626
2727 /**
28- * { @inheritdoc}
28+ * @inheritdoc
2929 */
3030 public function isValid (Definition $ definition ): bool
3131 {
@@ -36,8 +36,10 @@ public function isValid(Definition $definition): bool
3636 $ directory = $ this ->getIterator ()->get ('directory ' , $ definition );
3737 $ upwardRoot = $ definition ->getBasepath ();
3838
39+ // phpcs:ignore
3940 $ root = realpath ($ upwardRoot . \DIRECTORY_SEPARATOR . $ directory );
4041
42+ // phpcs:ignore
4143 if (!$ root || !is_dir ($ root )) {
4244 return false ;
4345 }
@@ -46,7 +48,7 @@ public function isValid(Definition $definition): bool
4648 }
4749
4850 /**
49- * { @inheritdoc}
51+ * @inheritdoc
5052 */
5153 public function resolve ($ definition )
5254 {
@@ -57,15 +59,30 @@ public function resolve($definition)
5759 $ directory = $ this ->getIterator ()->get ('directory ' , $ definition );
5860 $ response = new Stream ();
5961 $ upwardRoot = $ this ->getIterator ()->getRootDefinition ()->getBasepath ();
62+ // phpcs:ignore
6063 $ root = realpath ($ upwardRoot . \DIRECTORY_SEPARATOR . $ directory );
6164 $ filename = $ this ->getIterator ()->get ('request.url.pathname ' );
65+ // phpcs:ignore
6266 $ path = realpath ($ root . $ filename );
67+ if (!$ path ) {
68+ // phpcs:ignore
69+ $ path = realpath (
70+ rtrim ($ root , '/ ' ) . (str_ends_with (
71+ $ root ,
72+ ($ p = strtok (ltrim ($ filename , '/ ' ), '/ ' ))
73+ ) ? substr ($ filename , strlen ($ p ) + 1 ) : $ filename
74+ )
75+ );
76+ }
6377
78+ // phpcs:ignore
6479 if (!$ path || strpos ($ path , $ root ) !== 0 || strpos ($ path , $ upwardRoot ) !== 0 || !is_file ($ path )) {
6580 $ response ->setStatusCode (Response::STATUS_CODE_404 );
6681 } else {
82+ // phpcs:ignore
6783 $ mimeType = (new MimeTypes ())->getMimeType (pathinfo ($ path , \PATHINFO_EXTENSION ));
6884
85+ // phpcs:ignore
6986 $ response ->setStream (fopen ($ path , 'r ' ));
7087 $ response ->getHeaders ()->addHeader (new ContentType ($ mimeType ));
7188 // Enforce best practice and make sure static assets are cacheable
0 commit comments