1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 1998-2015 Browser Capabilities Project |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a |
6
|
|
|
* copy of this software and associated documentation files (the "Software"), |
7
|
|
|
* to deal in the Software without restriction, including without limitation |
8
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
9
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the |
10
|
|
|
* Software is furnished to do so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included |
13
|
|
|
* in all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
16
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21
|
|
|
* THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
* @category Browscap-PHP |
24
|
|
|
* @copyright 1998-2015 Browser Capabilities Project |
25
|
|
|
* @license http://www.opensource.org/licenses/MIT MIT License |
26
|
|
|
* @link https://github.com/browscap/browscap-php/ |
27
|
|
|
* @since added with version 3.0 |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
namespace BrowscapPHP\Helper; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* class to help quoting strings for using a regex |
34
|
|
|
* |
35
|
|
|
* @category Browscap-PHP |
36
|
|
|
* @author Thomas Müller <[email protected]> |
37
|
|
|
* @copyright Copyright (c) 1998-2015 Browser Capabilities Project |
38
|
|
|
* @version 3.0 |
39
|
|
|
* @license http://www.opensource.org/licenses/MIT MIT License |
40
|
|
|
* @link https://github.com/browscap/browscap-php/ |
41
|
|
|
*/ |
42
|
|
|
class Quoter |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* Converts browscap match patterns into preg match patterns. |
46
|
|
|
* |
47
|
|
|
* @param string $user_agent |
48
|
|
|
* @param string $delimiter |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
9 |
|
public function pregQuote($user_agent, $delimiter = '/') |
53
|
|
|
{ |
54
|
9 |
|
$pattern = preg_quote($user_agent, $delimiter); |
55
|
|
|
|
56
|
|
|
// the \\x replacement is a fix for "Der gro\xdfe BilderSauger 2.00u" user agent match |
57
|
9 |
|
return str_replace(['\*', '\?', '\\x'], ['.*', '.', '\\\\x'], $pattern); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Reverts the quoting of a pattern. |
62
|
|
|
* |
63
|
|
|
* @param string $pattern |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function pregUnQuote($pattern) |
67
|
|
|
{ |
68
|
|
|
// Fast check, because most parent pattern like 'DefaultProperties' don't need a replacement |
69
|
|
|
if (preg_match('/[^a-z\s]/i', $pattern)) { |
70
|
|
|
// Undo the \\x replacement, that is a fix for "Der gro\xdfe BilderSauger 2.00u" user agent match |
71
|
|
|
// @source https://github.com/browscap/browscap-php |
72
|
|
|
$pattern = preg_replace( |
73
|
|
|
['/(?<!\\\\)\\.\\*/', '/(?<!\\\\)\\./', '/(?<!\\\\)\\\\x/'], |
74
|
|
|
['\\*', '\\?', '\\x'], |
75
|
|
|
$pattern |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
// Undo preg_quote |
79
|
|
|
$pattern = str_replace( |
80
|
|
|
[ |
81
|
|
|
'\\\\', '\\+', '\\*', '\\?', '\\[', '\\^', '\\]', '\\$', '\\(', '\\)', '\\{', '\\}', '\\=', |
82
|
|
|
'\\!', '\\<', '\\>', '\\|', '\\:', '\\-', '\\.', '\\/', |
83
|
|
|
], |
84
|
|
|
[ |
85
|
|
|
'\\', '+', '*', '?', '[', '^', ']', '$', '(', ')', '{', '}', '=', '!', '<', '>', '|', ':', |
86
|
|
|
'-', '.', '/', |
87
|
|
|
], |
88
|
|
|
$pattern |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $pattern; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|