source: trunk/data/module/Net/UserAgent/Mobile.php @ 18562

Revision 18562, 13.3 KB checked in by kajiwara, 14 years ago (diff)

EC-CUBE Ver2.4.3 分コミット。詳細はこちら( http://www.ec-cube.net/release/detail.php?release_id=210

  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
4/**
5 * PHP versions 4 and 5
6 *
7 * LICENSE: This source file is subject to version 3.0 of the PHP license
8 * that is available through the world-wide-web at the following URI:
9 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
10 * the PHP License and are unable to obtain it through the web, please
11 * send a note to license@php.net so we can mail you a copy immediately.
12 *
13 * @category   Networking
14 * @package    Net_UserAgent_Mobile
15 * @author     KUBO Atsuhiro <iteman@users.sourceforge.net>
16 * @copyright  2003-2008 KUBO Atsuhiro <iteman@users.sourceforge.net>
17 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
18 * @version    CVS: $Id$
19 * @since      File available since Release 0.1
20 */
21
22require_once dirname(__FILE__) . '/../../PEAR.php';
23
24// {{{ constants
25
26/**
27 * Constants for error handling.
28 */
29define('NET_USERAGENT_MOBILE_OK',               1);
30define('NET_USERAGENT_MOBILE_ERROR',           -1);
31define('NET_USERAGENT_MOBILE_ERROR_NOMATCH',   -2);
32define('NET_USERAGENT_MOBILE_ERROR_NOT_FOUND', -3);
33
34// }}}
35// {{{ GLOBALS
36
37/**
38 * globals for fallback on no match
39 *
40 * @global boolean $GLOBALS['_NET_USERAGENT_MOBILE_FALLBACK_ON_NOMATCH']
41 */
42$GLOBALS['_NET_USERAGENT_MOBILE_FALLBACK_ON_NOMATCH'] = false;
43
44// }}}
45// {{{ Net_UserAgent_Mobile
46
47/**
48 * HTTP mobile user agent string parser
49 *
50 * Net_UserAgent_Mobile parses HTTP_USER_AGENT strings of (mainly Japanese)
51 * mobile HTTP user agents. It'll be useful in page dispatching by user
52 * agents.
53 * This package was ported from Perl's HTTP::MobileAgent.
54 * See {@link http://search.cpan.org/search?mode=module&query=HTTP-MobileAgent}
55 * The author of the HTTP::MobileAgent module is Tatsuhiko Miyagawa
56 * <miyagawa@bulknews.net>
57 *
58 * SYNOPSIS:
59 * <code>
60 * require_once 'Net/UserAgent/Mobile.php';
61 *
62 * $agent = &Net_UserAgent_Mobile::factory($agent_string);
63 * // or $agent = &Net_UserAgent_Mobile::factory(); // to get from $_SERVER
64 *
65 * if ($agent->isDoCoMo()) {
66 *     // or if ($agent->getName() == 'DoCoMo')
67 *     // or if (strtolower(get_class($agent)) == 'http_mobileagent_docomo')
68 *     // it's NTT DoCoMo i-mode
69 *     // see what's available in Net_UserAgent_Mobile_DoCoMo
70 * } elseif ($agent->isVodafone()) {
71 *     // it's Vodafone(J-PHONE)
72 *     // see what's available in Net_UserAgent_Mobile_Vodafone
73 * } elseif ($agent->isEZweb()) {
74 *     // it's KDDI/EZWeb
75 *     // see what's available in Net_UserAgent_Mobile_EZweb
76 * } else {
77 *     // may be PC
78 *     // $agent is Net_UserAgent_Mobile_NonMobile
79 * }
80 *
81 * $display = $agent->getDisplay();    // Net_UserAgent_Mobile_Display
82 * if ($display->isColor()) {
83 *    ...
84 * }
85 * </code>
86 *
87 * @category   Networking
88 * @package    Net_UserAgent_Mobile
89 * @author     KUBO Atsuhiro <iteman@users.sourceforge.net>
90 * @copyright  2003-2008 KUBO Atsuhiro <iteman@users.sourceforge.net>
91 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
92 * @version    Release: 0.31.0
93 * @since      Class available since Release 0.1
94 */
95class Net_UserAgent_Mobile
96{
97
98    // {{{ properties
99
100    /**#@+
101     * @access public
102     */
103
104    /**#@-*/
105
106    /**#@+
107     * @access private
108     */
109
110    /**#@-*/
111
112    /**#@+
113     * @access public
114     * @static
115     */
116
117    // }}}
118    // {{{ factory()
119
120    /**
121     * create a new {@link Net_UserAgent_Mobile_Common} subclass instance
122     *
123     * parses HTTP headers and constructs {@link Net_UserAgent_Mobile_Common}
124     * subclass instance.
125     * If no argument is supplied, $_SERVER{'HTTP_*'} is used.
126     *
127     * @param string $userAgent User-Agent string
128     * @return mixed a newly created Net_UserAgent_Mobile object, or a PEAR
129     *     error object on error
130     */
131    function &factory($userAgent = null)
132    {
133        if (is_null($userAgent)) {
134            $userAgent = $_SERVER['HTTP_USER_AGENT'];
135        }
136
137        // parse User-Agent string
138        if (Net_UserAgent_Mobile::isDoCoMo($userAgent)) {
139            $driver = 'DoCoMo';
140        } elseif (Net_UserAgent_Mobile::isEZweb($userAgent)) {
141            $driver = 'EZweb';
142        } elseif (Net_UserAgent_Mobile::isSoftBank($userAgent)) {
143            $driver = 'SoftBank';
144        } elseif (Net_UserAgent_Mobile::isWillcom($userAgent)) {
145            $driver = 'Willcom';
146        } else {
147            $driver = 'NonMobile';
148        }
149
150        $class = "Net_UserAgent_Mobile_$driver";
151
152        if (!class_exists($class)) {
153           
154            $file = dirname(__FILE__) . "/Mobile/{$driver}.php";
155            if (!include_once $file) {
156                return PEAR::raiseError(null,
157                                        NET_USERAGENT_MOBILE_ERROR_NOT_FOUND,
158                                        null, null,
159                                        "Unable to include the $file file",
160                                        'Net_UserAgent_Mobile_Error', true
161                                        );
162            }
163        }
164
165        $instance = &new $class($userAgent);
166        $error = &$instance->isError();
167        if (Net_UserAgent_Mobile::isError($error)) {
168            if ($GLOBALS['_NET_USERAGENT_MOBILE_FALLBACK_ON_NOMATCH']
169                && $error->getCode() == NET_USERAGENT_MOBILE_ERROR_NOMATCH
170                ) {
171                $instance = &Net_UserAgent_Mobile::factory('Net_UserAgent_Mobile_Fallback_On_NoMatch');
172                return $instance;
173            }
174
175            $instance = &$error;
176        }
177
178        return $instance;
179    }
180
181    // }}}
182    // {{{ singleton()
183
184    /**
185     * creates a new {@link Net_UserAgent_Mobile_Common} subclass instance or
186     * returns a instance from existent ones
187     *
188     * @param string $userAgent User-Agent string
189     * @return mixed a newly created or a existent Net_UserAgent_Mobile
190     *     object, or a PEAR error object on error
191     * @see Net_UserAgent_Mobile::factory()
192     */
193    function &singleton($userAgent = null)
194    {
195        static $instances;
196
197        if (!isset($instances)) {
198            $instances = array();
199        }
200
201        if (is_null($userAgent)) {
202            $userAgent = $_SERVER['HTTP_USER_AGENT'];
203        }
204
205        if (!array_key_exists($userAgent, $instances)) {
206            $instances[$userAgent] = Net_UserAgent_Mobile::factory($userAgent);
207        }
208
209        return $instances[$userAgent];
210    }
211
212    // }}}
213    // {{{ isError()
214
215    /**
216     * tell whether a result code from a Net_UserAgent_Mobile method
217     * is an error
218     *
219     * @param integer $value result code
220     * @return boolean whether $value is an {@link Net_UserAgent_Mobile_Error}
221     */
222    function isError($value)
223    {
224        return is_a($value, 'Net_UserAgent_Mobile_Error');
225    }
226
227    // }}}
228    // {{{ errorMessage()
229
230    /**
231     * return a textual error message for a Net_UserAgent_Mobile error code
232     *
233     * @param integer $value error code
234     * @return string error message, or false if the error code was not
235     *     recognized
236     */
237    function errorMessage($value)
238    {
239        static $errorMessages;
240        if (!isset($errorMessages)) {
241            $errorMessages = array(
242                                   NET_USERAGENT_MOBILE_ERROR           => 'unknown error',
243                                   NET_USERAGENT_MOBILE_ERROR_NOMATCH   => 'no match',
244                                   NET_USERAGENT_MOBILE_ERROR_NOT_FOUND => 'not found',
245                                   NET_USERAGENT_MOBILE_OK              => 'no error'
246                                   );
247        }
248
249        if (Net_UserAgent_Mobile::isError($value)) {
250            $value = $value->getCode();
251        }
252
253        return isset($errorMessages[$value]) ?
254            $errorMessages[$value] :
255            $errorMessages[NET_USERAGENT_MOBILE_ERROR];
256    }
257
258    // }}}
259    // {{{ isMobile()
260
261    /**
262     * Checks whether or not the user agent is mobile by a given user agent
263     * string.
264     *
265     * @param string $userAgent
266     * @return boolean
267     * @since Method available since Release 0.31.0
268     */
269    function isMobile($userAgent = null)
270    {
271        if (Net_UserAgent_Mobile::isDoCoMo($userAgent)) {
272            return true;
273        } elseif (Net_UserAgent_Mobile::isEZweb($userAgent)) {
274            return true;
275        } elseif (Net_UserAgent_Mobile::isSoftBank($userAgent)) {
276            return true;
277        } elseif (Net_UserAgent_Mobile::isWillcom($userAgent)) {
278            return true;
279        }
280
281        return false;
282    }
283
284    // }}}
285    // {{{ isDoCoMo()
286
287    /**
288     * Checks whether or not the user agent is DoCoMo by a given user agent
289     * string.
290     *
291     * @param string $userAgent
292     * @return boolean
293     * @since Method available since Release 0.31.0
294     */
295    function isDoCoMo($userAgent = null)
296    {
297        if (is_null($userAgent)) {
298            $userAgent = $_SERVER['HTTP_USER_AGENT'];
299        }
300
301        if (preg_match('!^DoCoMo!', $userAgent)) {
302            return true;
303        }
304
305        return false;
306    }
307
308    // }}}
309    // {{{ isEZweb()
310
311    /**
312     * Checks whether or not the user agent is EZweb by a given user agent
313     * string.
314     *
315     * @param string $userAgent
316     * @return boolean
317     * @since Method available since Release 0.31.0
318     */
319    function isEZweb($userAgent = null)
320    {
321        if (is_null($userAgent)) {
322            $userAgent = $_SERVER['HTTP_USER_AGENT'];
323        }
324
325        if (preg_match('!^KDDI-!', $userAgent)) {
326            return true;
327        } elseif (preg_match('!^UP\.Browser!', $userAgent)) {
328            return true;
329        }
330
331        return false;
332    }
333
334    // }}}
335    // {{{ isSoftBank()
336
337    /**
338     * Checks whether or not the user agent is SoftBank by a given user agent
339     * string.
340     *
341     * @param string $userAgent
342     * @return boolean
343     * @since Method available since Release 0.31.0
344     */
345    function isSoftBank($userAgent = null)
346    {
347        if (is_null($userAgent)) {
348            $userAgent = $_SERVER['HTTP_USER_AGENT'];
349        }
350
351        if (preg_match('!^SoftBank!', $userAgent)) {
352            return true;
353        } elseif (preg_match('!^Semulator!', $userAgent)) {
354            return true;
355        } elseif (preg_match('!^Vodafone!', $userAgent)) {
356            return true;
357        } elseif (preg_match('!^Vemulator!', $userAgent)) {
358            return true;
359        } elseif (preg_match('!^MOT-!', $userAgent)) {
360            return true;
361        } elseif (preg_match('!^MOTEMULATOR!', $userAgent)) {
362            return true;
363        } elseif (preg_match('!^J-PHONE!', $userAgent)) {
364            return true;
365        } elseif (preg_match('!^J-EMULATOR!', $userAgent)) {
366            return true;
367        }
368
369        return false;
370    }
371
372    // }}}
373    // {{{ isWillcom()
374
375    /**
376     * Checks whether or not the user agent is Willcom by a given user agent
377     * string.
378     *
379     * @param string $userAgent
380     * @return boolean
381     * @since Method available since Release 0.31.0
382     */
383    function isWillcom($userAgent = null)
384    {
385        if (is_null($userAgent)) {
386            $userAgent = $_SERVER['HTTP_USER_AGENT'];
387        }
388
389        if (preg_match('!^Mozilla/3\.0\((?:DDIPOCKET|WILLCOM);!', $userAgent)) {
390            return true;
391        }
392
393        return false;
394    }
395
396    /**#@-*/
397
398    /**#@+
399     * @access private
400     */
401
402    /**#@-*/
403
404    // }}}
405}
406
407// }}}
408// {{{ Net_UserAgent_Mobile_Error
409
410/**
411 * Net_UserAgent_Mobile_Error implements a class for reporting user
412 * agent error messages
413 *
414 * @category   Networking
415 * @package    Net_UserAgent_Mobile
416 * @author     KUBO Atsuhiro <iteman@users.sourceforge.net>
417 * @copyright  2003-2007 KUBO Atsuhiro <iteman@users.sourceforge.net>
418 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
419 * @version    Release: 0.31.0
420 * @since      Class available since Release 0.1
421 */
422class Net_UserAgent_Mobile_Error extends PEAR_Error
423{
424
425    // {{{ properties
426
427    /**#@+
428     * @access public
429     */
430
431    /**#@-*/
432
433    /**#@+
434     * @access private
435     */
436
437    /**#@-*/
438
439    /**#@+
440     * @access public
441     */
442
443    // }}}
444    // {{{ constructor
445
446    /**
447     * constructor
448     *
449     * @param mixed   $code     Net_UserAgent_Mobile error code, or string
450     *     with error message.
451     * @param integer $mode     what 'error mode' to operate in
452     * @param integer $level    what error level to use for $mode and
453     *     PEAR_ERROR_TRIGGER
454     * @param mixed   $userinfo additional user/debug info
455     * @access public
456     */
457    function Net_UserAgent_Mobile_Error($code = NET_USERAGENT_MOBILE_ERROR,
458                                        $mode = PEAR_ERROR_RETURN,
459                                        $level = E_USER_NOTICE,
460                                        $userinfo = null
461                                        )
462    {
463        if (is_int($code)) {
464            $this->PEAR_Error('Net_UserAgent_Mobile Error: ' .
465                              Net_UserAgent_Mobile::errorMessage($code),
466                              $code, $mode, $level, $userinfo
467                              );
468        } else {
469            $this->PEAR_Error("Net_UserAgent_Mobile Error: $code",
470                              NET_USERAGENT_MOBILE_ERROR, $mode, $level,
471                              $userinfo
472                              );
473        }
474    }
475
476    /**#@-*/
477
478    /**#@+
479     * @access private
480     */
481
482    /**#@-*/
483
484    // }}}
485}
486
487// }}}
488
489/*
490 * Local Variables:
491 * mode: php
492 * coding: iso-8859-1
493 * tab-width: 4
494 * c-basic-offset: 4
495 * c-hanging-comment-ender-p: nil
496 * indent-tabs-mode: nil
497 * End:
498 */
Note: See TracBrowser for help on using the repository browser.