This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
1 | <?php |
||
2 | |||
3 | namespace App; |
||
4 | |||
5 | /** |
||
6 | * Utils class. |
||
7 | * |
||
8 | * @package App |
||
9 | * |
||
10 | * @copyright YetiForce S.A. |
||
11 | * @license YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com) |
||
12 | * @author Mariusz Krzaczkowski <[email protected]> |
||
13 | */ |
||
14 | class Utils |
||
15 | { |
||
16 | /** |
||
17 | * Function to capture the initial letters of words. |
||
18 | * |
||
19 | * @param string $name |
||
20 | * |
||
21 | * @return string |
||
22 | */ |
||
23 | public static function getInitials(string $name): string |
||
24 | { |
||
25 | preg_match_all('#(?<=\s|\b)\pL|[()]#u', $name, $initial); |
||
26 | return isset($initial[0]) ? implode('', $initial[0]) : ''; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Outputs or returns a parsable string representation of a variable. |
||
31 | * |
||
32 | * @see https://php.net/manual/en/function.var-export.php |
||
33 | * |
||
34 | * @param mixed $variable |
||
35 | * |
||
36 | 5794 | * @return mixed the variable representation when the <i>return</i> |
|
37 | */ |
||
38 | 5794 | public static function varExport($variable) |
|
39 | 5794 | { |
|
40 | 5794 | if (\is_array($variable)) { |
|
41 | 5788 | $toImplode = []; |
|
42 | 5788 | if (static::isAssoc($variable)) { |
|
43 | foreach ($variable as $key => $value) { |
||
44 | $toImplode[] = var_export($key, true) . '=>' . static::varExport($value); |
||
45 | 5786 | } |
|
46 | 5780 | } else { |
|
47 | foreach ($variable as $value) { |
||
48 | $toImplode[] = static::varExport($value); |
||
49 | } |
||
50 | 5794 | } |
|
51 | return '[' . implode(',', $toImplode) . ']'; |
||
52 | 5788 | } |
|
53 | return var_export($variable, true); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Check if array is associative. |
||
58 | * |
||
59 | * @param array $arr |
||
60 | * |
||
61 | * @return bool |
||
62 | 5794 | */ |
|
63 | public static function isAssoc(array $arr) |
||
64 | 5794 | { |
|
65 | 5785 | if (empty($arr)) { |
|
66 | return false; |
||
67 | 5788 | } |
|
68 | return array_keys($arr) !== range(0, \count($arr) - 1); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Flatten a multi-dimensional array into a single level. |
||
73 | * |
||
74 | * @param array $array |
||
75 | * @param float $depth |
||
76 | * |
||
77 | * @return array |
||
78 | */ |
||
79 | 1 | public static function flatten(array $array, float $depth = INF): array |
|
80 | { |
||
81 | 1 | $result = []; |
|
82 | 1 | foreach ($array as $item) { |
|
83 | if (\is_array($item)) { |
||
84 | $values = 1 === $depth ? array_values($item) : static::flatten($item, $depth - 1); |
||
85 | foreach ($values as $value) { |
||
86 | 1 | $result[] = $value; |
|
87 | } |
||
88 | } else { |
||
89 | $result[] = $item; |
||
90 | } |
||
91 | } |
||
92 | return $result; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | 1 | * Flatten the multidimensional array on one level, keeping the key names unique. |
|
97 | * |
||
98 | 1 | * @param array $array |
|
99 | 1 | * @param string $type |
|
100 | * @param float $depth |
||
101 | * |
||
102 | * @return array |
||
103 | */ |
||
104 | public static function flattenKeys(array $array, string $type = '_', float $depth = INF): array |
||
105 | { |
||
106 | $result = []; |
||
107 | foreach ($array as $key => $item) { |
||
108 | if (\is_array($item)) { |
||
109 | if (1 === $depth) { |
||
110 | $values = array_values($item); |
||
111 | } else { |
||
112 | $values = static::flattenKeys($item, $type, $depth - 1); |
||
113 | 19 | } |
|
114 | foreach ($values as $keySec => $value) { |
||
115 | 19 | switch ($type) { |
|
116 | 1 | case 'ucfirst': |
|
117 | $keySec = \ucfirst($keySec); |
||
118 | 19 | $newKey = "{$key}{$keySec}"; |
|
119 | 10 | break; |
|
120 | default: |
||
121 | 19 | $newKey = "{$key}{$type}{$keySec}"; |
|
122 | 1 | break; |
|
123 | } |
||
124 | 19 | $result[$newKey] = $value; |
|
125 | } |
||
126 | 19 | } else { |
|
127 | 19 | $result[$key] = $item; |
|
128 | } |
||
129 | 19 | } |
|
130 | return $result; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Merge two arrays. |
||
135 | * |
||
136 | * @param array $array1 |
||
137 | * @param array $array2 |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | public static function merge(array $array1, array $array2): array |
||
142 | { |
||
143 | foreach ($array2 as $key => $value) { |
||
144 | if (isset($array1[$key])) { |
||
145 | if (\is_array($array1[$key]) && \is_array($value)) { |
||
146 | $array1[$key] = self::merge($array1[$key], $value); |
||
147 | } else { |
||
148 | $array1[$key] = $value; |
||
149 | } |
||
150 | } else { |
||
151 | $array1[$key] = $value; |
||
152 | } |
||
153 | } |
||
154 | return $array1; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Convert string from encoding to encoding. |
||
159 | * |
||
160 | * @param string $value |
||
161 | * @param string $fromCharset |
||
162 | * @param string $toCharset |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | public static function convertCharacterEncoding($value, $fromCharset, $toCharset) |
||
167 | { |
||
168 | if (\function_exists('mb_convert_encoding') && \function_exists('mb_list_encodings') && \in_array($fromCharset, mb_list_encodings()) && \in_array($toCharset, mb_list_encodings())) { |
||
169 | $value = mb_convert_encoding($value, $toCharset, $fromCharset); |
||
170 | } else { |
||
171 | $value = iconv($fromCharset, $toCharset, $value); |
||
172 | } |
||
173 | return $value; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Function to check is a html message. |
||
178 | * |
||
179 | * @param string $content |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | public static function isHtml(string $content): bool |
||
184 | { |
||
185 | $content = trim($content); |
||
186 | if ('<' === substr($content, 0, 1) && '>' === substr($content, -1)) { |
||
187 | return true; |
||
188 | } |
||
189 | return $content != strip_tags($content); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Strip tags content. |
||
194 | * |
||
195 | * @param string $content |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | public static function htmlToText(string $content): string |
||
200 | { |
||
201 | return trim(preg_replace('/[ \t\n]+/', ' ', strip_tags($content))); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Function to save php file with cleaning file cache. |
||
206 | * |
||
207 | * @param string $pathDirectory |
||
208 | * @param array|string $content |
||
209 | * @param string $comment |
||
210 | * @param int $flag |
||
211 | * @param bool $return |
||
212 | * |
||
213 | * @return bool $value |
||
214 | */ |
||
215 | public static function saveToFile(string $pathDirectory, $content, string $comment = '', int $flag = LOCK_EX, bool $return = false): bool |
||
216 | { |
||
217 | if (\is_array($content)) { |
||
218 | $content = self::varExport($content); |
||
219 | } |
||
220 | if ($return) { |
||
221 | $content = "return $content;"; |
||
222 | } |
||
223 | if ($comment) { |
||
224 | $content = "<?php \n/** {$comment} */\n{$content}\n"; |
||
225 | } else { |
||
226 | $content = "<?php $content" . PHP_EOL; |
||
227 | } |
||
228 | if (false !== $value = file_put_contents($pathDirectory, $content, $flag)) { |
||
229 | Cache::resetFileCache($pathDirectory); |
||
230 | } |
||
231 | return (bool) $value; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Replacement for the ucfirst function for proper Multibyte String operation. |
||
236 | * Delete function will exist as mb_ucfirst. |
||
237 | * |
||
238 | * @param string $string |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | public static function mbUcfirst($string) |
||
243 | { |
||
244 | return mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Sanitize special chars from given string. |
||
249 | * |
||
250 | * @param string $string |
||
251 | * @param string $delimiter |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | public static function sanitizeSpecialChars(string $string, string $delimiter = '_'): string |
||
256 | { |
||
257 | $string = mb_convert_encoding((string) $string, 'UTF-8', mb_list_encodings()); |
||
258 | $replace = [ |
||
259 | 'ъ' => '-', 'Ь' => '-', 'Ъ' => '-', 'ь' => '-', |
||
260 | 'Ă' => 'A', 'Ą' => 'A', 'À' => 'A', 'Ã' => 'A', 'Á' => 'A', 'Æ' => 'A', 'Â' => 'A', 'Å' => 'A', 'Ä' => 'Ae', |
||
261 | 'Þ' => 'B', 'Ć' => 'C', 'ץ' => 'C', 'Ç' => 'C', 'È' => 'E', 'Ę' => 'E', 'É' => 'E', 'Ë' => 'E', 'Ê' => 'E', |
||
262 | 'Ğ' => 'G', 'İ' => 'I', 'Ï' => 'I', 'Î' => 'I', 'Í' => 'I', 'Ì' => 'I', 'Ł' => 'L', 'Ñ' => 'N', 'Ń' => 'N', |
||
263 | 'Ø' => 'O', 'Ó' => 'O', 'Ò' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'Oe', 'Ş' => 'S', 'Ś' => 'S', 'Ș' => 'S', |
||
264 | 'Š' => 'S', 'Ț' => 'T', 'Ù' => 'U', 'Û' => 'U', 'Ú' => 'U', 'Ü' => 'Ue', 'Ý' => 'Y', 'Ź' => 'Z', 'Ž' => 'Z', |
||
265 | 'Ż' => 'Z', 'â' => 'a', 'ǎ' => 'a', 'ą' => 'a', 'á' => 'a', 'ă' => 'a', 'ã' => 'a', 'Ǎ' => 'a', 'а' => 'a', |
||
266 | 'А' => 'a', 'å' => 'a', 'à' => 'a', 'א' => 'a', 'Ǻ' => 'a', 'Ā' => 'a', 'ǻ' => 'a', 'ā' => 'a', 'ä' => 'ae', |
||
267 | 'æ' => 'ae', 'Ǽ' => 'ae', 'ǽ' => 'ae', 'б' => 'b', 'ב' => 'b', 'Б' => 'b', 'þ' => 'b', 'ĉ' => 'c', 'Ĉ' => 'c', |
||
268 | 'Ċ' => 'c', 'ć' => 'c', 'ç' => 'c', 'ц' => 'c', 'צ' => 'c', 'ċ' => 'c', 'Ц' => 'c', 'Č' => 'c', 'č' => 'c', |
||
269 | 'Ч' => 'ch', 'ч' => 'ch', 'ד' => 'd', 'ď' => 'd', 'Đ' => 'd', 'Ď' => 'd', 'đ' => 'd', 'д' => 'd', 'Д' => 'D', |
||
270 | 'ð' => 'd', 'є' => 'e', 'ע' => 'e', 'е' => 'e', 'Е' => 'e', 'Ə' => 'e', 'ę' => 'e', 'ĕ' => 'e', 'ē' => 'e', |
||
271 | 'Ē' => 'e', 'Ė' => 'e', 'ė' => 'e', 'ě' => 'e', 'Ě' => 'e', 'Є' => 'e', 'Ĕ' => 'e', 'ê' => 'e', 'ə' => 'e', |
||
272 | 'è' => 'e', 'ë' => 'e', 'é' => 'e', 'ф' => 'f', 'ƒ' => 'f', 'Ф' => 'f', 'ġ' => 'g', 'Ģ' => 'g', 'Ġ' => 'g', |
||
273 | 'Ĝ' => 'g', 'Г' => 'g', 'г' => 'g', 'ĝ' => 'g', 'ğ' => 'g', 'ג' => 'g', 'Ґ' => 'g', 'ґ' => 'g', 'ģ' => 'g', |
||
274 | 'ח' => 'h', 'ħ' => 'h', 'Х' => 'h', 'Ħ' => 'h', 'Ĥ' => 'h', 'ĥ' => 'h', 'х' => 'h', 'ה' => 'h', 'î' => 'i', |
||
275 | 'ï' => 'i', 'í' => 'i', 'ì' => 'i', 'į' => 'i', 'ĭ' => 'i', 'ı' => 'i', 'Ĭ' => 'i', 'И' => 'i', 'ĩ' => 'i', |
||
276 | 'ǐ' => 'i', 'Ĩ' => 'i', 'Ǐ' => 'i', 'и' => 'i', 'Į' => 'i', 'י' => 'i', 'Ї' => 'i', 'Ī' => 'i', 'І' => 'i', |
||
277 | 'ї' => 'i', 'і' => 'i', 'ī' => 'i', 'ij' => 'ij', 'IJ' => 'ij', 'й' => 'j', 'Й' => 'j', 'Ĵ' => 'j', 'ĵ' => 'j', |
||
278 | 'я' => 'ja', 'Я' => 'ja', 'Э' => 'je', 'э' => 'je', 'ё' => 'jo', 'Ё' => 'jo', 'ю' => 'ju', 'Ю' => 'ju', |
||
279 | 'ĸ' => 'k', 'כ' => 'k', 'Ķ' => 'k', 'К' => 'k', 'к' => 'k', 'ķ' => 'k', 'ך' => 'k', 'Ŀ' => 'l', 'ŀ' => 'l', |
||
280 | 'Л' => 'l', 'ł' => 'l', 'ļ' => 'l', 'ĺ' => 'l', 'Ĺ' => 'l', 'Ļ' => 'l', 'л' => 'l', 'Ľ' => 'l', 'ľ' => 'l', |
||
281 | 'ל' => 'l', 'מ' => 'm', 'М' => 'm', 'ם' => 'm', 'м' => 'm', 'ñ' => 'n', 'н' => 'n', 'Ņ' => 'n', 'ן' => 'n', |
||
282 | 'ŋ' => 'n', 'נ' => 'n', 'Н' => 'n', 'ń' => 'n', 'Ŋ' => 'n', 'ņ' => 'n', 'ʼn' => 'n', 'Ň' => 'n', 'ň' => 'n', |
||
283 | 'о' => 'o', 'О' => 'o', 'ő' => 'o', 'õ' => 'o', 'ô' => 'o', 'Ő' => 'o', 'ŏ' => 'o', 'Ŏ' => 'o', 'Ō' => 'o', |
||
284 | 'ō' => 'o', 'ø' => 'o', 'ǿ' => 'o', 'ǒ' => 'o', 'ò' => 'o', 'Ǿ' => 'o', 'Ǒ' => 'o', 'ơ' => 'o', 'ó' => 'o', |
||
285 | 'Ơ' => 'o', 'œ' => 'oe', 'Œ' => 'oe', 'ö' => 'oe', 'פ' => 'p', 'ף' => 'p', 'п' => 'p', 'П' => 'p', 'ק' => 'q', |
||
286 | 'ŕ' => 'r', 'ř' => 'r', 'Ř' => 'r', 'ŗ' => 'r', 'Ŗ' => 'r', 'ר' => 'r', 'Ŕ' => 'r', 'Р' => 'r', 'р' => 'r', |
||
287 | 'ș' => 's', 'с' => 's', 'Ŝ' => 's', 'š' => 's', 'ś' => 's', 'ס' => 's', 'ş' => 's', 'С' => 's', 'ŝ' => 's', |
||
288 | 'Щ' => 'sch', 'щ' => 'sch', 'ш' => 'sh', 'Ш' => 'sh', 'ß' => 'ss', 'т' => 't', 'ט' => 't', 'ŧ' => 't', |
||
289 | 'ת' => 't', 'ť' => 't', 'ţ' => 't', 'Ţ' => 't', 'Т' => 't', 'ț' => 't', 'Ŧ' => 't', 'Ť' => 't', '™' => 'tm', |
||
290 | 'ū' => 'u', 'у' => 'u', 'Ũ' => 'u', 'ũ' => 'u', 'Ư' => 'u', 'ư' => 'u', 'Ū' => 'u', 'Ǔ' => 'u', 'ų' => 'u', |
||
291 | 'Ų' => 'u', 'ŭ' => 'u', 'Ŭ' => 'u', 'Ů' => 'u', 'ů' => 'u', 'ű' => 'u', 'Ű' => 'u', 'Ǖ' => 'u', 'ǔ' => 'u', |
||
292 | 'Ǜ' => 'u', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'У' => 'u', 'ǚ' => 'u', 'ǜ' => 'u', 'Ǚ' => 'u', 'Ǘ' => 'u', |
||
293 | 'ǖ' => 'u', 'ǘ' => 'u', 'ü' => 'ue', 'в' => 'v', 'ו' => 'v', 'В' => 'v', 'ש' => 'w', 'ŵ' => 'w', 'Ŵ' => 'w', |
||
294 | 'ы' => 'y', 'ŷ' => 'y', 'ý' => 'y', 'ÿ' => 'y', 'Ÿ' => 'y', 'Ŷ' => 'y', 'Ы' => 'y', 'ž' => 'z', 'З' => 'z', |
||
295 | 'з' => 'z', 'ź' => 'z', 'ז' => 'z', 'ż' => 'z', 'ſ' => 'z', 'Ж' => 'zh', 'ж' => 'zh', 'Ð' => 'D', 'Θ' => '8', |
||
296 | '©' => '(c)', 'Α' => 'A', 'Β' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'H', 'Ι' => 'I', |
||
297 | 'Κ' => 'K', 'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => '3', 'Ο' => 'O', 'Π' => 'P', 'Ρ' => 'R', 'Σ' => 'S', |
||
298 | 'Τ' => 'T', 'Υ' => 'Y', 'Φ' => 'F', 'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W', 'Ά' => 'A', 'Έ' => 'E', 'Ί' => 'I', |
||
299 | 'Ό' => 'O', 'Ύ' => 'Y', 'Ή' => 'H', 'Ώ' => 'W', 'Ϊ' => 'I', 'Ϋ' => 'Y', 'α' => 'a', 'β' => 'b', 'γ' => 'g', |
||
300 | 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'h', 'θ' => '8', 'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', |
||
301 | 'ν' => 'n', 'ξ' => '3', 'ο' => 'o', 'π' => 'p', 'ρ' => 'r', 'σ' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', |
||
302 | 'χ' => 'x', 'ψ' => 'ps', 'ω' => 'w', 'ά' => 'a', 'έ' => 'e', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'y', 'ή' => 'h', |
||
303 | 'ώ' => 'w', 'ς' => 's', 'ϊ' => 'i', 'ΰ' => 'y', 'ϋ' => 'y', 'ΐ' => 'i', |
||
304 | ]; |
||
305 | $string = strtr($string, $replace); |
||
306 | $string = preg_replace('/[^\p{L}\p{Nd}\.]+/u', $delimiter, $string); |
||
307 | return trim($string, $delimiter); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Change the order of associative array. |
||
312 | * |
||
313 | * @param array $array |
||
314 | * @param array $order |
||
315 | * |
||
316 | * @return array |
||
317 | */ |
||
318 | public static function changeSequence(array $array, array $order): array |
||
319 | { |
||
320 | if (!$order) { |
||
0 ignored issues
–
show
The expression
$order of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||
321 | return $array; |
||
322 | } |
||
323 | $returnLinks = []; |
||
324 | foreach ($order as $value) { |
||
325 | if ($array[$value]) { |
||
326 | $returnLinks[$value] = $array[$value]; |
||
327 | } |
||
328 | unset($array[$value]); |
||
329 | } |
||
330 | return array_merge($returnLinks, $array); |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Get locks content by events. |
||
335 | * |
||
336 | * @param array $locks |
||
337 | * |
||
338 | * @return string |
||
339 | */ |
||
340 | public static function getLocksContent(array $locks): string |
||
341 | { |
||
342 | $return = ''; |
||
343 | foreach ($locks as $lock) { |
||
344 | switch ($lock) { |
||
345 | case 'copy': |
||
346 | $return .= ' oncopy = "return false"'; |
||
347 | break; |
||
348 | case 'cut': |
||
349 | $return .= ' oncut = "return false"'; |
||
350 | break; |
||
351 | case 'paste': |
||
352 | $return .= ' onpaste = "return false"'; |
||
353 | break; |
||
354 | case 'contextmenu': |
||
355 | $return .= ' oncontextmenu = "return false"'; |
||
356 | break; |
||
357 | case 'selectstart': |
||
358 | $return .= ' onselectstart = "return false" onselect = "return false"'; |
||
359 | break; |
||
360 | case 'drag': |
||
361 | $return .= ' ondragstart = "return false" ondrag = "return false"'; |
||
362 | break; |
||
363 | } |
||
364 | } |
||
365 | return $return; |
||
366 | } |
||
367 | } |
||
368 |