| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Faker\Provider;
|
|
|
4 |
|
|
|
5 |
class UserAgent extends Base
|
|
|
6 |
{
|
|
|
7 |
protected static $userAgents = ['firefox', 'chrome', 'internetExplorer', 'opera', 'safari', 'msedge'];
|
|
|
8 |
|
|
|
9 |
protected static $windowsPlatformTokens = [
|
|
|
10 |
'Windows NT 6.2', 'Windows NT 6.1', 'Windows NT 6.0', 'Windows NT 5.2', 'Windows NT 5.1',
|
|
|
11 |
'Windows NT 5.01', 'Windows NT 5.0', 'Windows NT 4.0', 'Windows 98; Win 9x 4.90', 'Windows 98',
|
|
|
12 |
'Windows 95', 'Windows CE',
|
|
|
13 |
];
|
|
|
14 |
|
|
|
15 |
/**
|
|
|
16 |
* Possible processors on Linux
|
|
|
17 |
*/
|
|
|
18 |
protected static $linuxProcessor = ['i686', 'x86_64'];
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Mac processors (it also added U;)
|
|
|
22 |
*/
|
|
|
23 |
protected static $macProcessor = ['Intel', 'PPC', 'U; Intel', 'U; PPC'];
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Add as many languages as you like.
|
|
|
27 |
*/
|
|
|
28 |
protected static $lang = ['en-US', 'sl-SI', 'nl-NL'];
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Generate mac processor
|
|
|
32 |
*
|
|
|
33 |
* @return string
|
|
|
34 |
*/
|
|
|
35 |
public static function macProcessor()
|
|
|
36 |
{
|
|
|
37 |
return static::randomElement(static::$macProcessor);
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Generate linux processor
|
|
|
42 |
*
|
|
|
43 |
* @return string
|
|
|
44 |
*/
|
|
|
45 |
public static function linuxProcessor()
|
|
|
46 |
{
|
|
|
47 |
return static::randomElement(static::$linuxProcessor);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Generate a random user agent
|
|
|
52 |
*
|
|
|
53 |
* @example 'Mozilla/5.0 (Windows CE) AppleWebKit/5350 (KHTML, like Gecko) Chrome/13.0.888.0 Safari/5350'
|
|
|
54 |
*
|
|
|
55 |
* @return string
|
|
|
56 |
*/
|
|
|
57 |
public static function userAgent()
|
|
|
58 |
{
|
|
|
59 |
$userAgentName = static::randomElement(static::$userAgents);
|
|
|
60 |
|
|
|
61 |
return static::$userAgentName();
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Generate Chrome user agent
|
|
|
66 |
*
|
|
|
67 |
* @example 'Mozilla/5.0 (Macintosh; PPC Mac OS X 10_6_5) AppleWebKit/5312 (KHTML, like Gecko) Chrome/14.0.894.0 Safari/5312'
|
|
|
68 |
*
|
|
|
69 |
* @return string
|
|
|
70 |
*/
|
|
|
71 |
public static function chrome()
|
|
|
72 |
{
|
|
|
73 |
$saf = self::numberBetween(531, 536) . self::numberBetween(0, 2);
|
|
|
74 |
|
|
|
75 |
$platforms = [
|
|
|
76 |
'(' . static::linuxPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/" . self::numberBetween(36, 40) . '.0.' . self::numberBetween(800, 899) . ".0 Mobile Safari/$saf",
|
|
|
77 |
'(' . static::windowsPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/" . self::numberBetween(36, 40) . '.0.' . self::numberBetween(800, 899) . ".0 Mobile Safari/$saf",
|
|
|
78 |
'(' . static::macPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/" . self::numberBetween(36, 40) . '.0.' . self::numberBetween(800, 899) . ".0 Mobile Safari/$saf",
|
|
|
79 |
];
|
|
|
80 |
|
|
|
81 |
return 'Mozilla/5.0 ' . static::randomElement($platforms);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Generate Edge user agent
|
|
|
86 |
*
|
|
|
87 |
* @example 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36 Edg/99.0.1150.36'
|
|
|
88 |
*
|
|
|
89 |
* @return string
|
|
|
90 |
*/
|
|
|
91 |
public static function msedge()
|
|
|
92 |
{
|
|
|
93 |
$saf = self::numberBetween(531, 537) . '.' . self::numberBetween(0, 2);
|
|
|
94 |
$chrv = self::numberBetween(79, 99) . '.0';
|
|
|
95 |
|
|
|
96 |
$platforms = [
|
|
|
97 |
'(' . static::windowsPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/$chrv" . '.' . self::numberBetween(4000, 4844) . '.' . self::numberBetween(10, 99) . " Safari/$saf Edg/$chrv" . self::numberBetween(1000, 1146) . '.' . self::numberBetween(0, 99),
|
|
|
98 |
'(' . static::macPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/$chrv" . '.' . self::numberBetween(4000, 4844) . '.' . self::numberBetween(10, 99) . " Safari/$saf Edg/$chrv" . self::numberBetween(1000, 1146) . '.' . self::numberBetween(0, 99),
|
|
|
99 |
'(' . static::linuxPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/$chrv" . '.' . self::numberBetween(4000, 4844) . '.' . self::numberBetween(10, 99) . " Safari/$saf EdgA/$chrv" . self::numberBetween(1000, 1146) . '.' . self::numberBetween(0, 99),
|
|
|
100 |
'(' . static::iosMobileToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Version/15.0 EdgiOS/$chrv" . self::numberBetween(1000, 1146) . '.' . self::numberBetween(0, 99) . " Mobile/15E148 Safari/$saf",
|
|
|
101 |
];
|
|
|
102 |
|
|
|
103 |
return 'Mozilla/5.0 ' . static::randomElement($platforms);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Generate Firefox user agent
|
|
|
108 |
*
|
|
|
109 |
* @example 'Mozilla/5.0 (X11; Linuxi686; rv:7.0) Gecko/20101231 Firefox/3.6'
|
|
|
110 |
*
|
|
|
111 |
* @return string
|
|
|
112 |
*/
|
|
|
113 |
public static function firefox()
|
|
|
114 |
{
|
|
|
115 |
$ver = 'Gecko/' . date('Ymd', self::numberBetween(strtotime('2010-1-1'), time())) . ' Firefox/' . self::numberBetween(35, 37) . '.0';
|
|
|
116 |
|
|
|
117 |
$platforms = [
|
|
|
118 |
'(' . static::windowsPlatformToken() . '; ' . static::randomElement(static::$lang) . '; rv:1.9.' . self::numberBetween(0, 2) . '.20) ' . $ver,
|
|
|
119 |
'(' . static::linuxPlatformToken() . '; rv:' . self::numberBetween(5, 7) . '.0) ' . $ver,
|
|
|
120 |
'(' . static::macPlatformToken() . ' rv:' . self::numberBetween(2, 6) . '.0) ' . $ver,
|
|
|
121 |
];
|
|
|
122 |
|
|
|
123 |
return 'Mozilla/5.0 ' . static::randomElement($platforms);
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Generate Safari user agent
|
|
|
128 |
*
|
|
|
129 |
* @example 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_7_1 rv:3.0; en-US) AppleWebKit/534.11.3 (KHTML, like Gecko) Version/4.0 Safari/534.11.3'
|
|
|
130 |
*
|
|
|
131 |
* @return string
|
|
|
132 |
*/
|
|
|
133 |
public static function safari()
|
|
|
134 |
{
|
|
|
135 |
$saf = self::numberBetween(531, 535) . '.' . self::numberBetween(1, 50) . '.' . self::numberBetween(1, 7);
|
|
|
136 |
|
|
|
137 |
if (Miscellaneous::boolean()) {
|
|
|
138 |
$ver = self::numberBetween(4, 5) . '.' . self::numberBetween(0, 1);
|
|
|
139 |
} else {
|
|
|
140 |
$ver = self::numberBetween(4, 5) . '.0.' . self::numberBetween(1, 5);
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
$mobileDevices = [
|
|
|
144 |
'iPhone; CPU iPhone OS',
|
|
|
145 |
'iPad; CPU OS',
|
|
|
146 |
];
|
|
|
147 |
|
|
|
148 |
$platforms = [
|
|
|
149 |
'(Windows; U; ' . static::windowsPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Version/$ver Safari/$saf",
|
|
|
150 |
'(' . static::macPlatformToken() . ' rv:' . self::numberBetween(2, 6) . '.0; ' . static::randomElement(static::$lang) . ") AppleWebKit/$saf (KHTML, like Gecko) Version/$ver Safari/$saf",
|
|
|
151 |
'(' . static::randomElement($mobileDevices) . ' ' . self::numberBetween(7, 8) . '_' . self::numberBetween(0, 2) . '_' . self::numberBetween(1, 2) . ' like Mac OS X; ' . static::randomElement(static::$lang) . ") AppleWebKit/$saf (KHTML, like Gecko) Version/" . self::numberBetween(3, 4) . '.0.5 Mobile/8B' . self::numberBetween(111, 119) . " Safari/6$saf",
|
|
|
152 |
];
|
|
|
153 |
|
|
|
154 |
return 'Mozilla/5.0 ' . static::randomElement($platforms);
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* Generate Opera user agent
|
|
|
159 |
*
|
|
|
160 |
* @example 'Opera/8.25 (Windows NT 5.1; en-US) Presto/2.9.188 Version/10.00'
|
|
|
161 |
*
|
|
|
162 |
* @return string
|
|
|
163 |
*/
|
|
|
164 |
public static function opera()
|
|
|
165 |
{
|
|
|
166 |
$platforms = [
|
|
|
167 |
'(' . static::linuxPlatformToken() . '; ' . static::randomElement(static::$lang) . ') Presto/2.' . self::numberBetween(8, 12) . '.' . self::numberBetween(160, 355) . ' Version/' . self::numberBetween(10, 12) . '.00',
|
|
|
168 |
'(' . static::windowsPlatformToken() . '; ' . static::randomElement(static::$lang) . ') Presto/2.' . self::numberBetween(8, 12) . '.' . self::numberBetween(160, 355) . ' Version/' . self::numberBetween(10, 12) . '.00',
|
|
|
169 |
];
|
|
|
170 |
|
|
|
171 |
return 'Opera/' . self::numberBetween(8, 9) . '.' . self::numberBetween(10, 99) . ' ' . static::randomElement($platforms);
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
/**
|
|
|
175 |
* Generate Internet Explorer user agent
|
|
|
176 |
*
|
|
|
177 |
* @example 'Mozilla/5.0 (compatible; MSIE 7.0; Windows 98; Win 9x 4.90; Trident/3.0)'
|
|
|
178 |
*
|
|
|
179 |
* @return string
|
|
|
180 |
*/
|
|
|
181 |
public static function internetExplorer()
|
|
|
182 |
{
|
|
|
183 |
return 'Mozilla/5.0 (compatible; MSIE ' . self::numberBetween(5, 11) . '.0; ' . static::windowsPlatformToken() . '; Trident/' . self::numberBetween(3, 5) . '.' . self::numberBetween(0, 1) . ')';
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
/**
|
|
|
187 |
* @return string
|
|
|
188 |
*/
|
|
|
189 |
public static function windowsPlatformToken()
|
|
|
190 |
{
|
|
|
191 |
return static::randomElement(static::$windowsPlatformTokens);
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
/**
|
|
|
195 |
* @return string
|
|
|
196 |
*/
|
|
|
197 |
public static function macPlatformToken()
|
|
|
198 |
{
|
|
|
199 |
return 'Macintosh; ' . static::randomElement(static::$macProcessor) . ' Mac OS X 10_' . self::numberBetween(5, 8) . '_' . self::numberBetween(0, 9);
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
/**
|
|
|
203 |
* @return string
|
|
|
204 |
*/
|
|
|
205 |
public static function iosMobileToken()
|
|
|
206 |
{
|
|
|
207 |
$iosVer = self::numberBetween(13, 15) . '_' . self::numberBetween(0, 2);
|
|
|
208 |
|
|
|
209 |
return 'iPhone; CPU iPhone OS ' . $iosVer . ' like Mac OS X';
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
/**
|
|
|
213 |
* @return string
|
|
|
214 |
*/
|
|
|
215 |
public static function linuxPlatformToken()
|
|
|
216 |
{
|
|
|
217 |
return 'X11; Linux ' . static::randomElement(static::$linuxProcessor);
|
|
|
218 |
}
|
|
|
219 |
}
|