|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Jasny; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Check if the file contains the specified string |
|
9
|
|
|
* |
|
10
|
|
|
* @param string $filename |
|
11
|
|
|
* @param string $str |
|
12
|
|
|
* @return bool |
|
13
|
|
|
*/ |
|
14
|
|
|
function file_contains(string $filename, string $str): bool |
|
15
|
|
|
{ |
|
16
|
2 |
|
$handle = fopen($filename, 'r'); |
|
17
|
|
|
|
|
18
|
2 |
|
if ($handle === false) { |
|
19
|
1 |
|
return false; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
1 |
|
$valid = false; |
|
23
|
|
|
|
|
24
|
1 |
|
$len = max(2 * strlen($str), 256); |
|
25
|
1 |
|
$prev = ''; |
|
26
|
|
|
|
|
27
|
1 |
|
while (!feof($handle)) { |
|
28
|
1 |
|
$cur = fread($handle, $len); |
|
29
|
|
|
|
|
30
|
1 |
|
if (strpos($prev . $cur, $str) !== false) { |
|
31
|
1 |
|
$valid = true; |
|
32
|
1 |
|
break; |
|
33
|
|
|
} |
|
34
|
1 |
|
$prev = $cur; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
fclose($handle); |
|
38
|
|
|
|
|
39
|
1 |
|
return $valid; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Match path against an extended wildcard pattern. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $pattern |
|
46
|
|
|
* @param string $path |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
|
|
function fnmatch_extended(string $pattern, string $path): bool |
|
50
|
|
|
{ |
|
51
|
24 |
|
$quoted = preg_quote($pattern, '~'); |
|
52
|
|
|
|
|
53
|
24 |
|
$step1 = strtr($quoted, ['\?' => '[^/]', '\*' => '[^/]*', '/\*\*' => '(?:/.*)?', '#' => '\d+', '\[' => '[', |
|
54
|
|
|
'\]' => ']', '\-' => '-', '\{' => '{', '\}' => '}']); |
|
55
|
|
|
|
|
56
|
|
|
$step2 = preg_replace_callback('~{[^}]+}~', function ($part) { |
|
57
|
4 |
|
return '(?:' . substr(strtr($part[0], ',', '|'), 1, -1) . ')'; |
|
58
|
24 |
|
}, $step1); |
|
59
|
|
|
|
|
60
|
24 |
|
$regex = rawurldecode($step2); |
|
61
|
|
|
|
|
62
|
24 |
|
return (bool)preg_match("~^{$regex}$~", $path); |
|
63
|
|
|
} |
|
64
|
|
|
|