2018-01-14 18:09:40 +01:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
use PhpParser;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2020-10-13 22:49:03 +02:00
|
|
|
use Psalm\Internal\DataFlow\TaintSink;
|
2020-09-30 18:28:13 +02:00
|
|
|
use Psalm\Internal\Codebase\TaintFlowGraph;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Exception\FileIncludeException;
|
|
|
|
use Psalm\Issue\MissingFile;
|
|
|
|
use Psalm\Issue\UnresolvableInclude;
|
|
|
|
use Psalm\IssueBuffer;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function str_replace;
|
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
use function dirname;
|
|
|
|
use function preg_match;
|
|
|
|
use function in_array;
|
|
|
|
use function realpath;
|
|
|
|
use function get_included_files;
|
|
|
|
use function str_repeat;
|
|
|
|
use const PHP_EOL;
|
|
|
|
use function is_string;
|
|
|
|
use function implode;
|
|
|
|
use function defined;
|
|
|
|
use function constant;
|
|
|
|
use const PATH_SEPARATOR;
|
|
|
|
use function preg_split;
|
|
|
|
use function get_include_path;
|
|
|
|
use function explode;
|
|
|
|
use function substr;
|
|
|
|
use function file_exists;
|
|
|
|
use function preg_replace;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
class IncludeAnalyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
{
|
|
|
|
public static function analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2018-01-14 18:09:40 +01:00
|
|
|
PhpParser\Node\Expr\Include_ $stmt,
|
2018-05-23 05:38:27 +02:00
|
|
|
Context $context,
|
2020-09-07 01:36:47 +02:00
|
|
|
?Context $global_context = null
|
2020-05-18 21:13:27 +02:00
|
|
|
) : bool {
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-11-06 03:57:36 +01:00
|
|
|
$config = $codebase->config;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
if (!$config->allow_includes) {
|
|
|
|
throw new FileIncludeException(
|
|
|
|
'File includes are not allowed per your Psalm config - check the allowFileIncludes flag.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-13 19:15:23 +02:00
|
|
|
$was_inside_call = $context->inside_call;
|
|
|
|
|
|
|
|
$context->inside_call = true;
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-13 19:15:23 +02:00
|
|
|
if (!$was_inside_call) {
|
|
|
|
$context->inside_call = false;
|
|
|
|
}
|
|
|
|
|
2020-06-19 00:48:19 +02:00
|
|
|
$stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr);
|
2019-11-25 17:44:54 +01:00
|
|
|
|
2018-08-07 19:17:23 +02:00
|
|
|
if ($stmt->expr instanceof PhpParser\Node\Scalar\String_
|
2020-06-19 00:48:19 +02:00
|
|
|
|| ($stmt_expr_type && $stmt_expr_type->isSingleStringLiteral())
|
2018-08-07 19:17:23 +02:00
|
|
|
) {
|
|
|
|
if ($stmt->expr instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$path_to_file = $stmt->expr->value;
|
|
|
|
} else {
|
2019-11-25 17:44:54 +01:00
|
|
|
$path_to_file = $stmt_expr_type->getSingleStringLiteral()->value;
|
2018-08-07 19:17:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$path_to_file = str_replace('/', DIRECTORY_SEPARATOR, $path_to_file);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
// attempts to resolve using get_include_path dirs
|
2018-11-18 22:53:46 +01:00
|
|
|
$include_path = self::resolveIncludePath($path_to_file, dirname($statements_analyzer->getFilePath()));
|
2018-01-14 18:09:40 +01:00
|
|
|
$path_to_file = $include_path ? $include_path : $path_to_file;
|
|
|
|
|
2018-04-12 21:15:57 +02:00
|
|
|
if (DIRECTORY_SEPARATOR === '/') {
|
|
|
|
$is_path_relative = $path_to_file[0] !== DIRECTORY_SEPARATOR;
|
|
|
|
} else {
|
|
|
|
$is_path_relative = !preg_match('~^[A-Z]:\\\\~i', $path_to_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($is_path_relative) {
|
2018-11-18 22:53:46 +01:00
|
|
|
$path_to_file = $config->base_dir . DIRECTORY_SEPARATOR . $path_to_file;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
} else {
|
2019-11-25 17:44:54 +01:00
|
|
|
$path_to_file = self::getPathTo(
|
|
|
|
$stmt->expr,
|
|
|
|
$statements_analyzer->node_data,
|
2020-05-23 05:53:37 +02:00
|
|
|
$statements_analyzer,
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->getFileName(),
|
|
|
|
$config
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2020-06-19 00:48:19 +02:00
|
|
|
if ($stmt_expr_type
|
2020-10-13 23:28:12 +02:00
|
|
|
&& $statements_analyzer->data_flow_graph instanceof TaintFlowGraph
|
2020-06-19 00:48:19 +02:00
|
|
|
&& $stmt_expr_type->parent_nodes
|
2020-07-02 05:23:38 +02:00
|
|
|
&& !\in_array('TaintedInput', $statements_analyzer->getSuppressedIssues())
|
2020-06-19 00:48:19 +02:00
|
|
|
) {
|
|
|
|
$arg_location = new CodeLocation($statements_analyzer->getSource(), $stmt->expr);
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$include_param_sink = TaintSink::getForMethodArgument(
|
2020-06-19 00:48:19 +02:00
|
|
|
'include',
|
|
|
|
'include',
|
|
|
|
0,
|
|
|
|
$arg_location
|
|
|
|
);
|
|
|
|
|
2020-11-17 18:44:31 +01:00
|
|
|
$include_param_sink->taints = [\Psalm\Type\TaintKind::INPUT_INCLUDE];
|
2020-06-19 00:48:19 +02:00
|
|
|
|
2020-10-13 23:28:12 +02:00
|
|
|
$statements_analyzer->data_flow_graph->addSink($include_param_sink);
|
2020-06-19 00:48:19 +02:00
|
|
|
|
|
|
|
foreach ($stmt_expr_type->parent_nodes as $parent_node) {
|
2020-10-13 23:28:12 +02:00
|
|
|
$statements_analyzer->data_flow_graph->addPath($parent_node, $include_param_sink, 'arg');
|
2020-06-19 00:48:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($path_to_file) {
|
2018-11-18 23:18:05 +01:00
|
|
|
$path_to_file = self::normalizeFilePath($path_to_file);
|
2018-11-06 00:22:12 +01:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
// if the file is already included, we can't check much more
|
2018-04-12 21:15:57 +02:00
|
|
|
if (in_array(realpath($path_to_file), get_included_files(), true)) {
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$current_file_analyzer = $statements_analyzer->getFileAnalyzer();
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if ($current_file_analyzer->project_analyzer->fileExists($path_to_file)) {
|
|
|
|
if ($statements_analyzer->hasParentFilePath($path_to_file)
|
2019-04-03 16:43:41 +02:00
|
|
|
|| !$codebase->file_storage_provider->has($path_to_file)
|
2018-11-11 18:01:14 +01:00
|
|
|
|| ($statements_analyzer->hasAlreadyRequiredFilePath($path_to_file)
|
2018-05-23 05:38:27 +02:00
|
|
|
&& !$codebase->file_storage_provider->get($path_to_file)->has_extra_statements)
|
|
|
|
) {
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-05-23 05:38:27 +02:00
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$current_file_analyzer->addRequiredFilePath($path_to_file);
|
2018-05-30 22:19:18 +02:00
|
|
|
|
2018-05-23 05:38:27 +02:00
|
|
|
$file_name = $config->shortenFileName($path_to_file);
|
|
|
|
|
2019-05-30 16:30:41 +02:00
|
|
|
$nesting = $statements_analyzer->getRequireNesting() + 1;
|
|
|
|
$current_file_analyzer->project_analyzer->progress->debug(
|
|
|
|
str_repeat(' ', $nesting) . 'checking ' . $file_name . PHP_EOL
|
|
|
|
);
|
2018-05-30 22:19:18 +02:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$include_file_analyzer = new \Psalm\Internal\Analyzer\FileAnalyzer(
|
|
|
|
$current_file_analyzer->project_analyzer,
|
2018-05-30 22:19:18 +02:00
|
|
|
$path_to_file,
|
|
|
|
$file_name
|
|
|
|
);
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$include_file_analyzer->setRootFilePath(
|
|
|
|
$current_file_analyzer->getRootFilePath(),
|
|
|
|
$current_file_analyzer->getRootFileName()
|
2018-05-30 22:19:18 +02:00
|
|
|
);
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$include_file_analyzer->addParentFilePath($current_file_analyzer->getFilePath());
|
|
|
|
$include_file_analyzer->addRequiredFilePath($current_file_analyzer->getFilePath());
|
2018-05-30 22:19:18 +02:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
foreach ($current_file_analyzer->getRequiredFilePaths() as $required_file_path) {
|
|
|
|
$include_file_analyzer->addRequiredFilePath($required_file_path);
|
2018-05-30 22:19:18 +02:00
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
foreach ($current_file_analyzer->getParentFilePaths() as $parent_file_path) {
|
|
|
|
$include_file_analyzer->addParentFilePath($parent_file_path);
|
2018-05-30 22:19:18 +02:00
|
|
|
}
|
|
|
|
|
2018-08-07 19:17:23 +02:00
|
|
|
try {
|
2018-11-11 18:01:14 +01:00
|
|
|
$include_file_analyzer->analyze(
|
2018-08-07 19:17:23 +02:00
|
|
|
$context,
|
|
|
|
false,
|
|
|
|
$global_context
|
|
|
|
);
|
|
|
|
} catch (\Psalm\Exception\UnpreparedAnalysisException $e) {
|
2020-02-25 13:40:46 +01:00
|
|
|
if ($config->skip_checks_on_unresolvable_includes) {
|
|
|
|
$context->check_classes = false;
|
|
|
|
$context->check_variables = false;
|
|
|
|
$context->check_functions = false;
|
|
|
|
}
|
2018-08-07 19:17:23 +02:00
|
|
|
}
|
2018-05-30 22:19:18 +02:00
|
|
|
|
2020-06-13 21:48:12 +02:00
|
|
|
$included_return_type = $include_file_analyzer->getReturnType();
|
|
|
|
|
|
|
|
if ($included_return_type) {
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $included_return_type);
|
|
|
|
}
|
|
|
|
|
2020-03-17 14:19:39 +01:00
|
|
|
$context->has_returned = false;
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
foreach ($include_file_analyzer->getRequiredFilePaths() as $required_file_path) {
|
|
|
|
$current_file_analyzer->addRequiredFilePath($required_file_path);
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2019-08-02 07:55:24 +02:00
|
|
|
$include_file_analyzer->clearSourceBeforeDestruction();
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$source = $statements_analyzer->getSource();
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingFile(
|
|
|
|
'Cannot find file ' . $path_to_file . ' to include',
|
|
|
|
new CodeLocation($source, $stmt)
|
|
|
|
),
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-18 21:13:27 +02:00
|
|
|
$var_id = ExpressionIdentifier::getArrayVarId($stmt->expr, null);
|
2018-08-10 05:29:30 +02:00
|
|
|
|
|
|
|
if (!$var_id || !isset($context->phantom_files[$var_id])) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$source = $statements_analyzer->getSource();
|
2018-08-10 05:29:30 +02:00
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnresolvableInclude(
|
|
|
|
'Cannot resolve the given expression to a file path',
|
|
|
|
new CodeLocation($source, $stmt)
|
|
|
|
),
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 13:40:46 +01:00
|
|
|
if ($config->skip_checks_on_unresolvable_includes) {
|
|
|
|
$context->check_classes = false;
|
|
|
|
$context->check_variables = false;
|
|
|
|
$context->check_functions = false;
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedAssignment
|
|
|
|
*/
|
2019-11-25 17:44:54 +01:00
|
|
|
public static function getPathTo(
|
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
?\Psalm\Internal\Provider\NodeDataProvider $type_provider,
|
2020-05-23 05:53:37 +02:00
|
|
|
?StatementsAnalyzer $statements_analyzer,
|
2020-09-07 01:36:47 +02:00
|
|
|
string $file_name,
|
2019-11-25 17:44:54 +01:00
|
|
|
Config $config
|
2020-09-04 22:26:33 +02:00
|
|
|
): ?string {
|
2018-04-12 21:15:57 +02:00
|
|
|
if (DIRECTORY_SEPARATOR === '/') {
|
|
|
|
$is_path_relative = $file_name[0] !== DIRECTORY_SEPARATOR;
|
|
|
|
} else {
|
|
|
|
$is_path_relative = !preg_match('~^[A-Z]:\\\\~i', $file_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($is_path_relative) {
|
2018-11-18 22:53:46 +01:00
|
|
|
$file_name = $config->base_dir . DIRECTORY_SEPARATOR . $file_name;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Scalar\String_) {
|
2018-11-18 23:06:11 +01:00
|
|
|
if (DIRECTORY_SEPARATOR !== '/') {
|
|
|
|
return str_replace('/', DIRECTORY_SEPARATOR, $stmt->value);
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
return $stmt->value;
|
2018-08-07 19:17:23 +02:00
|
|
|
}
|
|
|
|
|
2020-05-23 05:53:37 +02:00
|
|
|
$stmt_type = $type_provider ? $type_provider->getType($stmt) : null;
|
|
|
|
|
|
|
|
if ($stmt_type && $stmt_type->isSingleStringLiteral()) {
|
2018-11-18 23:06:11 +01:00
|
|
|
if (DIRECTORY_SEPARATOR !== '/') {
|
|
|
|
return str_replace(
|
|
|
|
'/',
|
|
|
|
DIRECTORY_SEPARATOR,
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type->getSingleStringLiteral()->value
|
2018-11-18 23:06:11 +01:00
|
|
|
);
|
|
|
|
}
|
2019-11-25 17:44:54 +01:00
|
|
|
|
|
|
|
return $stmt_type->getSingleStringLiteral()->value;
|
2018-08-07 19:17:23 +02:00
|
|
|
}
|
|
|
|
|
2018-10-01 19:50:55 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\Variable
|
|
|
|
&& $stmt->var->name === 'GLOBALS'
|
|
|
|
&& $stmt->dim instanceof PhpParser\Node\Scalar\String_
|
|
|
|
) {
|
2018-10-01 21:14:39 +02:00
|
|
|
if (isset($GLOBALS[$stmt->dim->value]) && is_string($GLOBALS[$stmt->dim->value])) {
|
|
|
|
/** @var string */
|
2018-10-01 19:50:55 +02:00
|
|
|
return $GLOBALS[$stmt->dim->value];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Concat) {
|
2020-05-23 05:53:37 +02:00
|
|
|
$left_string = self::getPathTo($stmt->left, $type_provider, $statements_analyzer, $file_name, $config);
|
|
|
|
$right_string = self::getPathTo($stmt->right, $type_provider, $statements_analyzer, $file_name, $config);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
if ($left_string && $right_string) {
|
|
|
|
return $left_string . $right_string;
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\FuncCall &&
|
|
|
|
$stmt->name instanceof PhpParser\Node\Name &&
|
|
|
|
$stmt->name->parts === ['dirname']
|
|
|
|
) {
|
|
|
|
if ($stmt->args) {
|
2018-07-22 02:22:42 +02:00
|
|
|
$dir_level = 1;
|
|
|
|
|
|
|
|
if (isset($stmt->args[1])) {
|
|
|
|
if ($stmt->args[1]->value instanceof PhpParser\Node\Scalar\LNumber) {
|
|
|
|
$dir_level = $stmt->args[1]->value->value;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 05:53:37 +02:00
|
|
|
$evaled_path = self::getPathTo(
|
|
|
|
$stmt->args[0]->value,
|
|
|
|
$type_provider,
|
|
|
|
$statements_analyzer,
|
|
|
|
$file_name,
|
|
|
|
$config
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
if (!$evaled_path) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-22 02:22:42 +02:00
|
|
|
return dirname($evaled_path, $dir_level);
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
2019-02-26 07:03:33 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\ConstFetch) {
|
2018-01-14 18:09:40 +01:00
|
|
|
$const_name = implode('', $stmt->name->parts);
|
|
|
|
|
|
|
|
if (defined($const_name)) {
|
|
|
|
$constant_value = constant($const_name);
|
|
|
|
|
|
|
|
if (is_string($constant_value)) {
|
|
|
|
return $constant_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst\Dir) {
|
|
|
|
return dirname($file_name);
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst\File) {
|
|
|
|
return $file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public static function resolveIncludePath(string $file_name, string $current_directory): ?string
|
2018-01-14 18:09:40 +01:00
|
|
|
{
|
|
|
|
if (!$current_directory) {
|
|
|
|
return $file_name;
|
|
|
|
}
|
|
|
|
|
2020-09-20 00:26:51 +02:00
|
|
|
$paths = PATH_SEPARATOR === ':'
|
2018-01-14 18:09:40 +01:00
|
|
|
? preg_split('#(?<!phar):#', get_include_path())
|
|
|
|
: explode(PATH_SEPARATOR, get_include_path());
|
|
|
|
|
|
|
|
foreach ($paths as $prefix) {
|
2020-09-20 00:26:51 +02:00
|
|
|
$ds = substr($prefix, -1) === DIRECTORY_SEPARATOR ? '' : DIRECTORY_SEPARATOR;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
if ($prefix === '.') {
|
|
|
|
$prefix = $current_directory;
|
|
|
|
}
|
|
|
|
|
|
|
|
$file = $prefix . $ds . $file_name;
|
|
|
|
|
|
|
|
if (file_exists($file)) {
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2018-11-18 23:18:05 +01:00
|
|
|
|
2020-08-23 19:52:31 +02:00
|
|
|
/**
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
2018-11-18 23:18:05 +01:00
|
|
|
public static function normalizeFilePath(string $path_to_file) : string
|
|
|
|
{
|
|
|
|
// replace all \ with / for normalization
|
|
|
|
$path_to_file = str_replace('\\', '/', $path_to_file);
|
|
|
|
$path_to_file = str_replace('/./', '/', $path_to_file);
|
|
|
|
|
|
|
|
// first remove unnecessary / duplicates
|
|
|
|
$path_to_file = preg_replace('/\/[\/]+/', '/', $path_to_file);
|
|
|
|
|
|
|
|
$reduce_pattern = '/\/[^\/]+\/\.\.\//';
|
|
|
|
|
|
|
|
while (preg_match($reduce_pattern, $path_to_file)) {
|
2019-07-21 17:03:02 +02:00
|
|
|
$path_to_file = preg_replace($reduce_pattern, '/', $path_to_file, 1);
|
2018-11-18 23:18:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (DIRECTORY_SEPARATOR !== '/') {
|
|
|
|
$path_to_file = str_replace('/', DIRECTORY_SEPARATOR, $path_to_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $path_to_file;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|