ILIAS  release_8 Revision v8.24
class.ilPDFGenerationRequest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25{
28
29 public function __construct(\ILIAS\Refinery\Factory $refinery, \ILIAS\HTTP\GlobalHttpState $http)
30 {
31 $this->refinery = $refinery;
32 $this->http = $http;
33 }
34
35 public function securedString(string $parameter, bool $cast_null_to_string = true): string
36 {
37 $as_sanizited_string = $this->refinery->custom()->transformation(static function (string $value): string {
38 return ilUtil::stripSlashes($value);
39 });
40
41 $null_to_empty_string = $this->refinery->custom()->transformation(static function ($value): string {
42 if ($value === null) {
43 return '';
44 }
45
46 throw new ilException('Expected null in transformation');
47 });
48
49 $sanizite_as_string = $this->refinery->in()->series([
50 $cast_null_to_string ?
51 $this->refinery->byTrying([$this->refinery->kindlyTo()->string(), $null_to_empty_string]) :
52 $this->refinery->kindlyTo()->string(),
53 $as_sanizited_string
54 ]);
55
56 $string = '';
57 if ($this->http->wrapper()->post()->has($parameter)) {
58 $string = $this->http->wrapper()->post()->retrieve(
59 $parameter,
60 $sanizite_as_string
61 );
62 } elseif ($this->http->wrapper()->query()->has($parameter)) {
63 $string = $this->http->wrapper()->query()->retrieve(
64 $parameter,
65 $sanizite_as_string
66 );
67 }
68
69 return $string;
70 }
71
72 protected function isPathOrUrlValid(string $url): bool
73 {
74 $is_valid = false;
75 $is_url = filter_var($url, FILTER_VALIDATE_URL);
76
77 if ($is_url) {
78 try {
80 $c->init();
81 $c->setOpt(CURLOPT_CUSTOMREQUEST, 'HEAD');
82 $c->setOpt(CURLOPT_SSL_VERIFYPEER, 0);
83 $c->setOpt(CURLOPT_SSL_VERIFYHOST, 0);
84 $c->setOpt(CURLOPT_RETURNTRANSFER, 1);
85 $c->setOpt(CURLOPT_FOLLOWLOCATION, 0);
86 $c->setOpt(CURLOPT_MAXREDIRS, 0);
87 $c->setOpt(CURLOPT_URL, $url);
88 $c->setOpt(CURLOPT_HEADER, true);
89 $c->setOpt(CURLOPT_NOBODY, true);
90
91 $result = $c->exec();
92 $is_valid = $c->getInfo(CURLINFO_HTTP_CODE) === 200;
93
95 return false;
96 }
97 } elseif (is_file($url)) {
98 $is_valid = true;
99 }
100
101 return $is_valid;
102 }
103
104 public function isNotValidSize(array $sizes): bool
105 {
106 foreach ($sizes as $size) {
107 if (is_int($size)) {
108 continue;
109 }
110 if (!preg_match('/(\d)+?(\W)*(cm|mm)$/', $size)) {
111 if ($size !== 0 && $size !== "0" && $size !== null && $size !== "") {
112 return true;
113 }
114 }
115 }
116
117 return false;
118 }
119
120 public function isNotValidText(array $texts): bool
121 {
122 foreach ($texts as $text) {
123 if (!preg_match('/[a-zA-Z\d ]+$/', $text)) {
124 if ($text !== '' && $text !== null) {
125 return true;
126 }
127 }
128 }
129
130 return false;
131 }
132
137 public function validatePathOrUrl(array $parameters): bool
138 {
139 $valid = false;
140 foreach ($parameters as $parameter) {
141 $value = $this->securedString($parameter);
142 if ($value === '') {
143 $valid = true;
144 } else {
145 $valid = $this->isPathOrUrlValid($value);
146 }
147 if ($valid === false) {
148 return false;
149 }
150 }
151 return true;
152 }
153
154 public function int(string $parameter, bool $cast_null_to_int = true): int
155 {
156 $null_to_zero = $this->refinery->custom()->transformation(static function ($value): int {
157 if ($value === null) {
158 return 0;
159 }
160
161 throw new ilException('Expected null in transformation');
162 });
163
164 $as_int = $cast_null_to_int ?
165 $this->refinery->byTrying([$this->refinery->kindlyTo()->int(), $null_to_zero]) :
166 $this->refinery->kindlyTo()->int();
167
168 $int = 0;
169 if ($this->http->wrapper()->post()->has($parameter)) {
170 $int = $this->http->wrapper()->post()->retrieve(
171 $parameter,
172 $as_int
173 );
174 } elseif ($this->http->wrapper()->query()->has($parameter)) {
175 $int = $this->http->wrapper()->query()->retrieve(
176 $parameter,
177 $as_int
178 );
179 }
180
181 return $int;
182 }
183
184 public function bool(string $parameter, bool $cast_null_to_false = true): bool
185 {
186 $null_to_false = $this->refinery->custom()->transformation(static function ($value): bool {
187 if ($value === null) {
188 return false;
189 }
190
191 throw new ilException('Expected null in transformation');
192 });
193
194 $as_bool = $cast_null_to_false ?
195 $this->refinery->byTrying([$this->refinery->kindlyTo()->bool(), $null_to_false]) :
196 $this->refinery->kindlyTo()->bool();
197
198 $bool = false;
199 if ($this->http->wrapper()->post()->has($parameter)) {
200 $bool = $this->http->wrapper()->post()->retrieve(
201 $parameter,
202 $as_bool
203 );
204 } elseif ($this->http->wrapper()->query()->has($parameter)) {
205 $bool = $this->http->wrapper()->query()->retrieve(
206 $parameter,
207 $as_bool
208 );
209 }
210
211 return $bool;
212 }
213
214 public function float(string $parameter, bool $cast_null_to_zero = true): float
215 {
216 $null_to_zero = $this->refinery->custom()->transformation(static function ($value): float {
217 if ($value === null) {
218 return 0.0;
219 }
220
221 throw new ilException('Expected null in transformation');
222 });
223
224 $as_float = $cast_null_to_zero ?
225 $this->refinery->byTrying([$this->refinery->kindlyTo()->float(), $null_to_zero]) :
226 $this->refinery->kindlyTo()->float();
227
228 $float = 0.0;
229 if ($this->http->wrapper()->post()->has($parameter)) {
230 $float = $this->http->wrapper()->post()->retrieve(
231 $parameter,
232 $as_float
233 );
234 } elseif ($this->http->wrapper()->query()->has($parameter)) {
235 $float = $this->http->wrapper()->query()->retrieve(
236 $parameter,
237 $as_float
238 );
239 }
240
241 return $float;
242 }
243}
Builds data types.
Definition: Factory.php:21
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
float(string $parameter, bool $cast_null_to_zero=true)
ILIAS HTTP GlobalHttpState $http
int(string $parameter, bool $cast_null_to_int=true)
securedString(string $parameter, bool $cast_null_to_string=true)
__construct(\ILIAS\Refinery\Factory $refinery, \ILIAS\HTTP\GlobalHttpState $http)
bool(string $parameter, bool $cast_null_to_false=true)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
$c
Definition: cli.php:38
$valid
Interface GlobalHttpState.
static http()
Fetches the global http state from ILIAS.
Class ChatMainBarProvider \MainMenu\Provider.
$url