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