Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

  • Bench
  • CommandLineBuilder
  • Pagination
  • Registry
  • Strings
  • Token
  • UserAgent

Exceptions

  • ExceptionHandler
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Pry Framework
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file LICENSE.txt.
 10:  * 
 11:  */
 12: namespace Pry\Util;
 13: /**
 14:  * Classe d"identification de navigateur internet.
 15:  * <code>
 16:  * $ua = new UserAgent();
 17:  * if($ua->isMobile()){echo 'Navigateur mobile';}
 18:  * $ua->addMobile('Mon Mobile');
 19:  * $mesMobiles = array('mobile 1', 'mobile2');
 20:  * $ua->addMobile($mesMobiles);
 21:  * $ua->removeMobile('Nokia');
 22:  * </code>
 23:  * @category Pry
 24:  * @package Util
 25:  * @author          Olivier ROGER <oroger@prynel.com>
 26:  * @version         1.1.6
 27:  *
 28:  */
 29: class UserAgent
 30: {
 31: 
 32:     /**
 33:      * User agent du navigateur
 34:      * @var string
 35:      * @access private
 36:      */
 37:     private $user_agent;
 38: 
 39:     /**
 40:      * Tableau des entête mobile
 41:      * @var array
 42:      * @access private
 43:      */
 44:     private $tabOfMobile;
 45: 
 46:     /**
 47:      * Tableau des mot clé pour smartphone
 48:      *
 49:      * @var array
 50:      * @access private
 51:      */
 52:     private $tabOfSmart;
 53: 
 54:     /**
 55:      * Tableau des navigateur de bureau
 56:      * @var array
 57:      * @access private
 58:      */
 59:     private $tabDesktopBrowser;
 60: 
 61:     /**
 62:      * Taille du tableau des entêtes
 63:      * @var int
 64:      * @access private
 65:      */
 66:     private $tabSize;
 67: 
 68:     /**
 69:      * Taille du tableau des navigateurs
 70:      * @var int
 71:      * @access private
 72:      */
 73:     private $tabSizeDesktopBrowser;
 74: 
 75:     /**
 76:      * Nav Mobile ou non
 77:      * @var boolean
 78:      * @access private
 79:      */
 80:     private $mobile;
 81: 
 82:     /**
 83:      * Navigateur sur smartphone ou non
 84:      *
 85:      * @var bool
 86:      * @access private
 87:      */
 88:     private $smartphone;
 89: 
 90:     /**
 91:      * Nav "desktop" ou non
 92:      * @var boolean
 93:      * @access private
 94:      */
 95:     private $desktop;
 96: 
 97:     /**
 98:      * Le bavigateur de bureau utilisé
 99:      * @var string
100:      * @access private
101:      */
102:     private $usedDesktop;
103: 
104:     /**
105:      * Le navigateur mobile détecté
106:      * @var string
107:      */
108:     private $usedMobile;
109: 
110:     /**
111:      * Constructeur
112:      * @param string $UA HTTP_USER_AGENT du navigateur
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:      * Détécte si le navigateur est un navigateur d'appareil mobile;
175:      * @access public
176:      * @return boolean
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:      * Détecte si l'appareil est un Iphone//Ipod touch
198:      * return boolean
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:      * Détecte si l'appareil est un iPad
212:      * @return boolean
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:      * Détecte si le device est un device android
226:      * @return boolean 
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:      * Détecte si l'appareil mobile est un smartphone. Si non c'est un pda
239:      * 
240:      * @access public
241:      * @return bool
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:      * Détecte si le navigateur est un navigateur de bureau;
258:      * @access public
259:      * @return boolean
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:      * Retourne le navigateur de bureau utilisé
276:      * @access public
277:      * @return string
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:      * Retourne le navigateur mobile utilisé
294:      * @since 1.1.4
295:      * @return string
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:      * Retourne le navigateur de bureau utilisé
309:      * @access private
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:      * Ajoute un ou plusieur mobile à la liste des chaines de détection
325:      * @access public
326:      * @param string/array $input Chaine ou tableau de chaine représentant un mobile potentiel.
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:      * Retire un mobile au tableau des chaines de détection
346:      * @access public
347:      * @param string $valeur Chaine ou tableau de chaine représentant un mobile potentiel.
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:      * Retour le user agent
361:      * @since 1.1.4
362:      * @return string
363:      */
364:     public function get()
365:     {
366:         return $this->user_agent;
367:     }
368: 
369: }
370: 
371: ?>
Pry API documentation generated by ApiGen 2.8.0