Skip to content

Commit

Permalink
Merge pull request #9608 from jjjb03/patch-1
Browse files Browse the repository at this point in the history
fix: treat includes starting with '.' and '..' correct
  • Loading branch information
orklah committed Apr 4, 2023
2 parents bf0740a + 865e183 commit 82b3a7c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Expand Up @@ -395,6 +395,18 @@ public static function resolveIncludePath(string $file_name, string $current_dir
return $file_name;
}

if ((substr($file_name, 0, 2) === '.' . DIRECTORY_SEPARATOR)
|| (substr($file_name, 0, 3) === '..' . DIRECTORY_SEPARATOR)
) {
$file = $current_directory . DIRECTORY_SEPARATOR . $file_name;

if (file_exists($file)) {
return $file;
}

return null;
}

$paths = PATH_SEPARATOR === ':'
? preg_split('#(?<!phar):#', get_include_path())
: explode(PATH_SEPARATOR, get_include_path());
Expand Down
44 changes: 44 additions & 0 deletions tests/IncludeTest.php
Expand Up @@ -619,6 +619,33 @@ class Two {}',
getcwd() . DIRECTORY_SEPARATOR . 'user.php',
],
],
'pathStartingWithDot' => [
'files' => [
getcwd() . DIRECTORY_SEPARATOR . 'test_1.php' => '<?php
// direct usage
require "./include_1.php";
require "./a/include_2.php";
Class_1::foo();
Class_2::bar();
',
getcwd() . DIRECTORY_SEPARATOR . 'include_1.php' => '<?php
class Class_1 {
public static function foo(): void {
// empty;
}
}',
getcwd() . DIRECTORY_SEPARATOR . 'a' . DIRECTORY_SEPARATOR . 'include_2.php' => '<?php
class Class_2 {
public static function bar(): void {
// empty;
}
}',
],
'files_to_check' => [
getcwd() . DIRECTORY_SEPARATOR . 'test_1.php',
],
],
];
}

Expand Down Expand Up @@ -867,6 +894,23 @@ function bar(): void {}
],
'error_message' => 'UndefinedFunction',
],
'pathStartingWithDot' => [
'files' => [
getcwd() . DIRECTORY_SEPARATOR . 'test_1.php' => '<?php
// start with single dot
require "./doesnotexist.php";
',
getcwd() . DIRECTORY_SEPARATOR . 'a' . DIRECTORY_SEPARATOR . 'test_2.php' => '<?php
// start with 2 dots
require "../doesnotexist.php";
',
],
'files_to_check' => [
getcwd() . DIRECTORY_SEPARATOR . 'test_1.php',
getcwd() . DIRECTORY_SEPARATOR . 'a' . DIRECTORY_SEPARATOR . 'test_2.php',
],
'error_message' => 'MissingFile',
],
];
}
}

0 comments on commit 82b3a7c

Please sign in to comment.