ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
CustomIconTempUploadPath.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28{
29 private string $absolute_path;
30
34 public function __construct(
35 string $temp_file_name,
36 string $ilias_data_dir
37 ) {
38 $this->absolute_path = $this->buildAndCheckSource(
39 $this->buildAndCheckBaseName($temp_file_name),
40 $ilias_data_dir
41 );
42 }
43
44 public function getAbsolutePath(): string
45 {
47 }
48
49 private function buildAndCheckBaseName(
50 string $temp_file_name
51 ): string {
52 $base_name = basename(
53 str_replace('\\', '/', $temp_file_name)
54 );
55 if ($base_name === '' || $base_name === '.' || $base_name === '..') {
56 throw new \InvalidArgumentException(
57 'Invalid temporary upload file name.'
58 );
59 }
60
61 return $base_name;
62 }
63
64 private function buildAndCheckSource(
65 string $base_name,
66 string $ilias_data_dir
67 ): string {
68 $data_dir = rtrim($ilias_data_dir, '/\\');
69 $temp_dir = "{$data_dir}/temp";
70 $real_temp = $this->getRealPath($temp_dir);
71 if ($real_temp === false) {
72 throw new \InvalidArgumentException(
73 'Temporary directory is not accessible.'
74 );
75 }
76
77 $real_source = $this->getRealPath("{$temp_dir}/{$base_name}");
78 if ($real_source === false || !is_file($real_source)) {
79 throw new \InvalidArgumentException(
80 'Temporary upload file not found.'
81 );
82 }
83
84 if (!str_starts_with($real_source, "{$real_temp}/")) {
85 throw new \InvalidArgumentException(
86 'Temporary upload file is outside the temp directory.'
87 );
88 }
89
90 return $real_source;
91 }
92
93 protected function getRealPath(
94 string $path
95 ): string|false {
96 return realpath($path);
97 }
98}
Resolves a user-supplied temp file identifier to an absolute path that is guaranteed to refer to a re...
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$path
Definition: ltiservices.php:30