1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2018 Volodymyr Chukh <[email protected]> |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
* |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace UaLibs; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class UaLib |
24
|
|
|
* @package UaLib |
25
|
|
|
*/ |
26
|
|
|
class UaLibs { |
27
|
|
|
|
28
|
|
|
public function __construct() { |
29
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function get(): array { |
36
|
|
|
return $this->getUserAgents(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return null|string |
41
|
|
|
*/ |
42
|
|
|
public function getRandom(): ?string { |
43
|
|
|
return $this->array_rand_value( $this->getUserAgents() ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function getAndroid(): array { |
50
|
|
|
return $this->getUserAgents( 'android' ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return null|string |
55
|
|
|
*/ |
56
|
|
|
public function getAndroidRandom(): ?string { |
57
|
|
|
return $this->array_rand_value( $this->getAndroid() ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function getChrome(): array { |
64
|
|
|
return $this->getUserAgents( 'chrome' ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return null|string |
69
|
|
|
*/ |
70
|
|
|
public function getChromeRandom(): ?string { |
71
|
|
|
return $this->array_rand_value( $this->getChrome() ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function getEdge(): array { |
78
|
|
|
return $this->getUserAgents( 'edge' ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return null|string |
83
|
|
|
*/ |
84
|
|
|
public function getEdgeRandom(): ?string { |
85
|
|
|
return $this->array_rand_value( $this->getEdge() ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function getFirefox(): array { |
92
|
|
|
return $this->getUserAgents( 'firefox' ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return null|string |
97
|
|
|
*/ |
98
|
|
|
public function getFirefoxRandom(): ?string { |
99
|
|
|
return $this->array_rand_value( $this->getFirefox() ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function getInternetExplorer(): array { |
106
|
|
|
return $this->getUserAgents( 'ie' ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return null|string |
111
|
|
|
*/ |
112
|
|
|
public function getInternetExplorerRandom(): ?string { |
113
|
|
|
return $this->array_rand_value( $this->getInternetExplorer() ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public function getOpera(): array { |
120
|
|
|
return $this->getUserAgents( 'opera' ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return null|string |
125
|
|
|
*/ |
126
|
|
|
public function getOperaRandom(): ?string { |
127
|
|
|
return $this->array_rand_value( $this->getOpera() ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
public function getSafari(): array { |
134
|
|
|
return $this->getUserAgents( 'safari' ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return null|string |
139
|
|
|
*/ |
140
|
|
|
public function getSafariRandom(): ?string { |
141
|
|
|
return $this->array_rand_value( $this->getSafari() ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param null|string $lib |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
protected function getUserAgents( ?string $lib = null ): array { |
150
|
|
|
if ( ! empty( $lib ) ) { |
151
|
|
|
$lib_path = __DIR__ . '/libs/' . $lib . '.txt'; |
152
|
|
|
$data = file( $lib_path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ); |
153
|
|
|
if ( ! $data ) { |
154
|
|
|
$data = []; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return file_exists( $lib_path ) ? $data : []; |
158
|
|
|
} else { |
159
|
|
|
$list = []; |
160
|
|
|
// combine all available libs and return it as array |
161
|
|
|
foreach ( new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( __DIR__ . '/libs' ) ) as $filename ) { |
162
|
|
|
// filter out "." and ".." |
163
|
|
|
if ( $filename->isDir() ) { |
164
|
|
|
continue; |
165
|
|
|
} |
166
|
|
|
$data = file( $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ); |
167
|
|
|
if ( ! $data ) { |
168
|
|
|
$data = []; |
169
|
|
|
} |
170
|
|
|
$list = array_merge( $list, $data ); |
171
|
|
|
} |
172
|
|
|
$list = array_values( array_unique( $list ) ); |
173
|
|
|
|
174
|
|
|
return $list; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param array $arr |
180
|
|
|
* |
181
|
|
|
* @return null|string |
182
|
|
|
*/ |
183
|
|
|
protected function array_rand_value( array $arr ): ?string { |
184
|
|
|
if ( empty( $arr ) ) { |
185
|
|
|
return null; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $arr[ array_rand( $arr ) ]; |
189
|
|
|
} |
190
|
|
|
} |