|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace BrowscapPHP\Parser\Helper; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* includes general functions for the work with patterns |
|
8
|
|
|
*/ |
|
9
|
|
|
final class Pattern |
|
10
|
|
|
{ |
|
11
|
|
|
private function __construct() |
|
12
|
|
|
{ |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Gets a hash or an array of hashes from the first characters of a pattern/user agent, that can |
|
17
|
|
|
* be used for a fast comparison, by comparing only the hashes, without having to match the |
|
18
|
|
|
* complete pattern against the user agent. |
|
19
|
|
|
* |
|
20
|
|
|
* With the variants options, all variants from the maximum number of pattern characters to one |
|
21
|
|
|
* character will be returned. This is required in some cases, the a placeholder is used very |
|
22
|
|
|
* early in the pattern. |
|
23
|
|
|
* |
|
24
|
|
|
* Example: |
|
25
|
|
|
* |
|
26
|
|
|
* Pattern: "Mozilla/* (Nintendo 3DS; *) Version/*" |
|
27
|
|
|
* User agent: "Mozilla/5.0 (Nintendo 3DS; U; ; en) Version/1.7567.US" |
|
28
|
|
|
* |
|
29
|
|
|
* In this case the has for the pattern is created for "Mozilla/" while the pattern |
|
30
|
|
|
* for the hash for user agent is created for "Mozilla/5.0". The variants option |
|
31
|
|
|
* results in an array with hashes for "Mozilla/5.0", "Mozilla/5.", "Mozilla/5", |
|
32
|
|
|
* "Mozilla/" ... "M", so that the pattern hash is included. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $pattern |
|
35
|
|
|
* @param bool $variants |
|
36
|
|
|
* @return string|array |
|
37
|
|
|
*/ |
|
38
|
3 |
|
public static function getHashForPattern(string $pattern, bool $variants = false) |
|
39
|
|
|
{ |
|
40
|
3 |
|
$regex = '/^([^\.\*\?\s\r\n\\\\]+).*$/'; |
|
41
|
3 |
|
$pattern = substr($pattern, 0, 32); |
|
42
|
3 |
|
$matches = []; |
|
43
|
|
|
|
|
44
|
3 |
|
if (!preg_match($regex, $pattern, $matches) || !isset($matches[1])) { |
|
45
|
1 |
|
return ($variants ? [md5('')] : md5('')); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
$string = $matches[1]; |
|
49
|
|
|
|
|
50
|
2 |
|
if (true === $variants) { |
|
51
|
1 |
|
$patternStarts = []; |
|
52
|
|
|
|
|
53
|
1 |
|
for ($i = strlen($string); $i >= 1; --$i) { |
|
54
|
1 |
|
$string = substr($string, 0, $i); |
|
55
|
1 |
|
$patternStarts[] = md5($string); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// Add empty pattern start to include patterns that start with "*", |
|
59
|
|
|
// e.g. "*FAST Enterprise Crawler*" |
|
60
|
1 |
|
$patternStarts[] = md5(''); |
|
61
|
|
|
|
|
62
|
1 |
|
return $patternStarts; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
return md5($string); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* returns a hash for one pattern |
|
70
|
|
|
* |
|
71
|
|
|
* @param $pattern |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public static function getHashForParts(string $pattern) : string |
|
76
|
|
|
{ |
|
77
|
1 |
|
return md5($pattern); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Gets the minimum length of the patern (used in the getPatterns() method to |
|
82
|
|
|
* check against the user agent length) |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $pattern |
|
85
|
|
|
* @return int |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public static function getPatternLength(string $pattern) : int |
|
88
|
|
|
{ |
|
89
|
1 |
|
return strlen(str_replace('*', '', $pattern)); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|