1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12: namespace Pry\Util;
13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28:
29: class UserAgent
30: {
31:
32: 33: 34: 35: 36:
37: private $user_agent;
38:
39: 40: 41: 42: 43:
44: private $tabOfMobile;
45:
46: 47: 48: 49: 50: 51:
52: private $tabOfSmart;
53:
54: 55: 56: 57: 58:
59: private $tabDesktopBrowser;
60:
61: 62: 63: 64: 65:
66: private $tabSize;
67:
68: 69: 70: 71: 72:
73: private $tabSizeDesktopBrowser;
74:
75: 76: 77: 78: 79:
80: private $mobile;
81:
82: 83: 84: 85: 86: 87:
88: private $smartphone;
89:
90: 91: 92: 93: 94:
95: private $desktop;
96:
97: 98: 99: 100: 101:
102: private $usedDesktop;
103:
104: 105: 106: 107:
108: private $usedMobile;
109:
110: 111: 112: 113:
114: public function __construct($UA)
115: {
116: $this->user_agent = $UA;
117: $this->tabOfMobile = array("Android",
118: "Blazer",
119: "Palm",
120: "Handspring",
121: "Nokia",
122: "Kyocera",
123: "Samsung",
124: "Motorola",
125: "Smartphone",
126: "Windows CE",
127: "Blackberry",
128: "WAP",
129: "SonyEricsson",
130: "Pda",
131: "Cldc",
132: "Midp",
133: "Symbian",
134: "Ericsson",
135: "Portalmmm",
136: "PANTECH",
137: "Bcdm",
138: "Bvirtual",
139: "Klondike",
140: "Pocketpc",
141: "Vodafone",
142: "AvantGo",
143: "Minimo",
144: "iPhone",
145: "iPod",
146: "iPad",
147: "HP",
148: "LGE",
149: "mmp",
150: "Xda",
151: "PSP",
152: "phone",
153: "mobile");
154: $this->tabSize = count($this->tabOfMobile);
155: $this->tabDesktopBrowser = array(
156: "Chrome",
157: "Netscape",
158: "Safari",
159: "Firefox",
160: "Konqueror",
161: "Epiphany",
162: "Lynx",
163: "MSIE",
164: "Opera");
165: $this->tabOfSmart = array("wap", "smartphone", "phone", "mmp", "symbian", "midp");
166: $this->tabSizeDesktopBrowser = count($this->tabDesktopBrowser);
167: $this->mobile = false;
168: $this->desktop = false;
169: $this->smartphone = false;
170: $this->usedDesktop = 'Inconnu';
171: }
172:
173: 174: 175: 176: 177:
178: public function isMobile()
179: {
180: for ($i = 0; $i < $this->tabSize; $i++) {
181: if (!strripos($this->user_agent, $this->tabOfMobile[$i]) === FALSE)
182: {
183: $this->mobile = true;
184: $this->usedMobile = $this->tabOfMobile[$i];
185: break;
186: }
187: }
188: if ($this->mobile == false)
189: {
190: $this->desktop = true;
191: $this->quelDesktop();
192: }
193: return $this->mobile;
194: }
195:
196: 197: 198: 199:
200: public function isIphone()
201: {
202: if ($this->isMobile())
203: {
204: if (strripos($this->user_agent, 'iPhone') || strripos($this->user_agent, 'iPod'))
205: return true;
206: }
207: return false;
208: }
209:
210: 211: 212: 213:
214: public function isIpad()
215: {
216: if ($this->isMobile())
217: {
218: if (strripos($this->user_agent, 'iPad'))
219: return true;
220: }
221: return false;
222: }
223:
224: 225: 226: 227:
228: public function isAndroid()
229: {
230: if ($this->isMobile())
231: if (strripos($this->user_agent, 'Android'))
232: return true;
233:
234: return false;
235: }
236:
237: 238: 239: 240: 241: 242:
243: public function isSmartphone()
244: {
245: $tailleSmart = count($this->tabOfSmart);
246: for ($i = 0; $i < $tailleSmart; $i++) {
247: if (!strripos($this->user_agent, $this->tabOfSmart[$i]) === FALSE)
248: {
249: $this->smartphone = true;
250: break;
251: }
252: }
253: return $this->smartphone;
254: }
255:
256: 257: 258: 259: 260:
261: public function isDesktop()
262: {
263: if ($this->isMobile() === FALSE)
264: {
265: $this->desktop = true;
266: $this->quelDesktop();
267: }
268: else
269: $this->desktop = false;
270:
271: return $this->desktop;
272: }
273:
274: 275: 276: 277: 278:
279: public function showDesktop()
280: {
281: if ($this->isDesktop() === true)
282: {
283: $nav = $this->usedDesktop;
284: }
285: else
286: {
287: $nav = 'Navigateur mobile';
288: }
289: return $nav;
290: }
291:
292: 293: 294: 295: 296:
297: public function showMobile()
298: {
299: if ($this->isMobile())
300: $nav = $this->usedMobile;
301: else
302: $nav = 'Desktop';
303:
304: return $nav;
305: }
306:
307: 308: 309: 310: 311:
312: private function quelDesktop()
313: {
314: for ($i = 0; $i < $this->tabSizeDesktopBrowser; $i++) {
315: if (!strripos($this->user_agent, $this->tabDesktopBrowser[$i]) === FALSE)
316: {
317: $this->usedDesktop = $this->tabDesktopBrowser[$i];
318: break;
319: }
320: }
321: }
322:
323: 324: 325: 326: 327:
328: public function addMobile($input)
329: {
330: if (is_array($input))
331: {
332: foreach ($input as $value) {
333: array_push($this->tabOfMobile, $value);
334: }
335: }
336: else
337: {
338: array_push($this->tabOfMobile, $input);
339: }
340: $this->tabSize = count($this->tabOfMobile);
341: return $this->tabOfMobile;
342: }
343:
344: 345: 346: 347: 348:
349: public function removeMobile($valeur)
350: {
351: if (in_array($valeur, $this->tabOfMobile))
352: {
353: $key = array_search($valeur, $this->tabOfMobile);
354: unset($this->tabOfMobile[$key]);
355: $this->tabSize = count($this->tabOfMobile);
356: }
357: }
358:
359: 360: 361: 362: 363:
364: public function get()
365: {
366: return $this->user_agent;
367: }
368:
369: }
370:
371: ?>