source: branches/comu-utf8/data/module/Net/UserAgent/Mobile.php @ 15099

Revision 15099, 10.6 KB checked in by Yammy, 17 years ago (diff)

UTF-8変換済みファイルインポート
1.3.4ベース

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-2007 KUBO Atsuhiro <iteman@users.sourceforge.net>
17 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
18 * @version    CVS: $Id: Mobile.php,v 1.27 2007/02/20 15:17:16 kuboa Exp $
19 * @since      File available since Release 0.1
20 */
21
22require_once dirname(__FILE__) . '/../../PEAR.php';
23require_once dirname(__FILE__) . '/Mobile/Request.php';
24
25// {{{ constants
26
27/**
28 * Constants for error handling.
29 */
30define('NET_USERAGENT_MOBILE_OK',               1);
31define('NET_USERAGENT_MOBILE_ERROR',           -1);
32define('NET_USERAGENT_MOBILE_ERROR_NOMATCH',   -2);
33define('NET_USERAGENT_MOBILE_ERROR_NOT_FOUND', -3);
34
35// }}}
36// {{{ GLOBALS
37
38/**
39 * globals for fallback on no match
40 *
41 * @global boolean $GLOBALS['_NET_USERAGENT_MOBILE_FALLBACK_ON_NOMATCH']
42 */
43$GLOBALS['_NET_USERAGENT_MOBILE_FALLBACK_ON_NOMATCH'] = false;
44
45// }}}
46// {{{ Net_UserAgent_Mobile
47
48/**
49 * HTTP mobile user agent string parser
50 *
51 * Net_UserAgent_Mobile parses HTTP_USER_AGENT strings of (mainly Japanese)
52 * mobile HTTP user agents. It'll be useful in page dispatching by user
53 * agents.
54 * This package was ported from Perl's HTTP::MobileAgent.
55 * See {@link http://search.cpan.org/search?mode=module&query=HTTP-MobileAgent}
56 * The author of the HTTP::MobileAgent module is Tatsuhiko Miyagawa
57 * <miyagawa@bulknews.net>
58 *
59 * SYNOPSIS:
60 * <code>
61 * require_once 'Net/UserAgent/Mobile.php';
62 *
63 * $agent = &Net_UserAgent_Mobile::factory($agent_string);
64 * // or $agent = &Net_UserAgent_Mobile::factory(); // to get from $_SERVER
65 *
66 * if ($agent->isDoCoMo()) {
67 *     // or if ($agent->getName() == 'DoCoMo')
68 *     // or if (strtolower(get_class($agent)) == 'http_mobileagent_docomo')
69 *     // it's NTT DoCoMo i-mode
70 *     // see what's available in Net_UserAgent_Mobile_DoCoMo
71 * } elseif ($agent->isVodafone()) {
72 *     // it's Vodafone(J-PHONE)
73 *     // see what's available in Net_UserAgent_Mobile_Vodafone
74 * } elseif ($agent->isEZweb()) {
75 *     // it's KDDI/EZWeb
76 *     // see what's available in Net_UserAgent_Mobile_EZweb
77 * } else {
78 *     // may be PC
79 *     // $agent is Net_UserAgent_Mobile_NonMobile
80 * }
81 *
82 * $display = $agent->getDisplay();    // Net_UserAgent_Mobile_Display
83 * if ($display->isColor()) {
84 *    ...
85 * }
86 * </code>
87 *
88 * @category   Networking
89 * @package    Net_UserAgent_Mobile
90 * @author     KUBO Atsuhiro <iteman@users.sourceforge.net>
91 * @copyright  2003-2007 KUBO Atsuhiro <iteman@users.sourceforge.net>
92 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
93 * @version    Release: 0.30.0
94 * @since      Class available since Release 0.1
95 */
96class Net_UserAgent_Mobile
97{
98
99    // {{{ properties
100
101    /**#@+
102     * @access public
103     */
104
105    /**#@-*/
106
107    /**#@+
108     * @access private
109     */
110
111    /**#@-*/
112
113    /**#@+
114     * @access public
115     * @static
116     */
117
118    // }}}
119    // {{{ factory()
120
121    /**
122     * create a new {@link Net_UserAgent_Mobile_Common} subclass instance
123     *
124     * parses HTTP headers and constructs {@link Net_UserAgent_Mobile_Common}
125     * subclass instance.
126     * If no argument is supplied, $_SERVER{'HTTP_*'} is used.
127     *
128     * @param mixed $stuff User-Agent string or object that works with
129     *     HTTP_Request (not implemented)
130     * @return mixed a newly created Net_UserAgent_Mobile object, or a PEAR
131     *     error object on error
132     * @see Net_UserAgent_Mobile_Request::factory()
133     */
134    function &factory($stuff = null)
135    {
136        static $mobileRegex;
137        if (!isset($mobileRegex)) {
138            $docomoRegex    = '^DoCoMo/\d\.\d[ /]';
139            $vodafoneRegex  = '^(?:(?:SoftBank|Vodafone|J-PHONE|Vemulator|J-EMULATOR)/\d\.\d|(?:MOT|MOTEMULATOR)-)';
140            $ezwebRegex     = '^(?:KDDI-[A-Z]+\d+[A-Z]? )?UP\.Browser\/';
141            $airhphoneRegex = '^Mozilla/3\.0\((?:DDIPOCKET|WILLCOM);';
142            $mobileRegex =
143                "(?:($docomoRegex)|($vodafoneRegex)|($ezwebRegex)|($airhphoneRegex))";
144        }
145
146        $request = &Net_UserAgent_Mobile_Request::factory($stuff);
147
148        // parse User-Agent string
149        $ua = $request->get('User-Agent');
150        $sub = 'NonMobile';
151        if (preg_match("!$mobileRegex!", $ua, $matches)) {
152            $sub = @$matches[1] ? 'DoCoMo' :
153                (@$matches[2] ? 'Vodafone' :
154                 (@$matches[3] ? 'EZweb' : 'AirHPhone'));
155        }
156        $className = "Net_UserAgent_Mobile_{$sub}";
157
158        if (!class_exists($className)) {
159            $file = dirname(__FILE__) . "/Mobile/{$sub}.php";
160            if (!include_once $file) {
161                return PEAR::raiseError(null,
162                                        NET_USERAGENT_MOBILE_ERROR_NOT_FOUND,
163                                        null, null,
164                                        "Unable to include the $file file",
165                                        'Net_UserAgent_Mobile_Error', true
166                                        );
167            }
168        }
169
170        $instance = &new $className($request);
171        $error = &$instance->isError();
172        if (Net_UserAgent_Mobile::isError($error)) {
173            if ($GLOBALS['_NET_USERAGENT_MOBILE_FALLBACK_ON_NOMATCH']
174                && $error->getCode() == NET_USERAGENT_MOBILE_ERROR_NOMATCH
175                ) {
176                $instance = &Net_UserAgent_Mobile::factory('Net_UserAgent_Mobile_Fallback_On_NoMatch');
177                return $instance;
178            }
179
180            $instance = &$error;
181        }
182
183        return $instance;
184    }
185
186    // }}}
187    // {{{ singleton()
188
189    /**
190     * creates a new {@link Net_UserAgent_Mobile_Common} subclass instance or
191     * returns a instance from existent ones
192     *
193     * @param mixed $stuff User-Agent string or object that works with
194     *     HTTP_Request (not implemented)
195     * @return mixed a newly created or a existent Net_UserAgent_Mobile
196     *     object, or a PEAR error object on error
197     * @see Net_UserAgent_Mobile::factory()
198     */
199     function &singleton($stuff = null)
200     {
201         static $instance;
202         if (!isset($instance)) {
203             $instance = Net_UserAgent_Mobile::factory($stuff);
204         }
205
206         return $instance;
207     }
208
209    // }}}
210    // {{{ isError()
211
212    /**
213     * tell whether a result code from a Net_UserAgent_Mobile method
214     * is an error
215     *
216     * @param integer $value result code
217     * @return boolean whether $value is an {@link Net_UserAgent_Mobile_Error}
218     */
219    function isError($value)
220    {
221        return is_a($value, 'Net_UserAgent_Mobile_Error');
222    }
223
224    // }}}
225    // {{{ errorMessage()
226
227    /**
228     * return a textual error message for a Net_UserAgent_Mobile error code
229     *
230     * @param integer $value error code
231     * @return string error message, or false if the error code was not
232     *     recognized
233     */
234    function errorMessage($value)
235    {
236        static $errorMessages;
237        if (!isset($errorMessages)) {
238            $errorMessages = array(
239                                   NET_USERAGENT_MOBILE_ERROR           => 'unknown error',
240                                   NET_USERAGENT_MOBILE_ERROR_NOMATCH   => 'no match',
241                                   NET_USERAGENT_MOBILE_ERROR_NOT_FOUND => 'not found',
242                                   NET_USERAGENT_MOBILE_OK              => 'no error'
243                                   );
244        }
245
246        if (Net_UserAgent_Mobile::isError($value)) {
247            $value = $value->getCode();
248        }
249
250        return isset($errorMessages[$value]) ?
251            $errorMessages[$value] :
252            $errorMessages[NET_USERAGENT_MOBILE_ERROR];
253    }
254
255    /**#@-*/
256
257    /**#@+
258     * @access private
259     */
260
261    /**#@-*/
262
263    // }}}
264}
265
266// }}}
267// {{{ Net_UserAgent_Mobile_Error
268
269/**
270 * Net_UserAgent_Mobile_Error implements a class for reporting user
271 * agent error messages
272 *
273 * @category   Networking
274 * @package    Net_UserAgent_Mobile
275 * @author     KUBO Atsuhiro <iteman@users.sourceforge.net>
276 * @copyright  2003-2007 KUBO Atsuhiro <iteman@users.sourceforge.net>
277 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
278 * @version    Release: 0.30.0
279 * @since      Class available since Release 0.1
280 */
281class Net_UserAgent_Mobile_Error extends PEAR_Error
282{
283
284    // {{{ properties
285
286    /**#@+
287     * @access public
288     */
289
290    /**#@-*/
291
292    /**#@+
293     * @access private
294     */
295
296    /**#@-*/
297
298    /**#@+
299     * @access public
300     */
301
302    // }}}
303    // {{{ constructor
304
305    /**
306     * constructor
307     *
308     * @param mixed   $code     Net_UserAgent_Mobile error code, or string
309     *     with error message.
310     * @param integer $mode     what 'error mode' to operate in
311     * @param integer $level    what error level to use for $mode and
312     *     PEAR_ERROR_TRIGGER
313     * @param mixed   $userinfo additional user/debug info
314     * @access public
315     */
316    function Net_UserAgent_Mobile_Error($code = NET_USERAGENT_MOBILE_ERROR,
317                                        $mode = PEAR_ERROR_RETURN,
318                                        $level = E_USER_NOTICE,
319                                        $userinfo = null
320                                        )
321    {
322        if (is_int($code)) {
323            $this->PEAR_Error('Net_UserAgent_Mobile Error: ' .
324                              Net_UserAgent_Mobile::errorMessage($code),
325                              $code, $mode, $level, $userinfo
326                              );
327        } else {
328            $this->PEAR_Error("Net_UserAgent_Mobile Error: $code",
329                              NET_USERAGENT_MOBILE_ERROR, $mode, $level,
330                              $userinfo
331                              );
332        }
333    }
334
335    /**#@-*/
336
337    /**#@+
338     * @access private
339     */
340
341    /**#@-*/
342
343    // }}}
344}
345
346// }}}
347
348/*
349 * Local Variables:
350 * mode: php
351 * coding: iso-8859-1
352 * tab-width: 4
353 * c-basic-offset: 4
354 * c-hanging-comment-ender-p: nil
355 * indent-tabs-mode: nil
356 * End:
357 */
358?>
Note: See TracBrowser for help on using the repository browser.