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-2006 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.25 2006/11/07 09:25:14 kuboa Exp $ |
---|
19 | * @since File available since Release 0.1 |
---|
20 | */ |
---|
21 | |
---|
22 | require_once dirname(__FILE__) . '/../../PEAR.php'; |
---|
23 | require_once dirname(__FILE__) . '/Mobile/Request.php'; |
---|
24 | |
---|
25 | // {{{ constants |
---|
26 | |
---|
27 | /** |
---|
28 | * Constants for error handling. |
---|
29 | */ |
---|
30 | define('NET_USERAGENT_MOBILE_OK', 1); |
---|
31 | define('NET_USERAGENT_MOBILE_ERROR', -1); |
---|
32 | define('NET_USERAGENT_MOBILE_ERROR_NOMATCH', -2); |
---|
33 | define('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-2006 KUBO Atsuhiro <iteman@users.sourceforge.net> |
---|
92 | * @license http://www.php.net/license/3_0.txt PHP License 3.0 |
---|
93 | * @version Release: @package_version@ |
---|
94 | * @since Class available since Release 0.1 |
---|
95 | */ |
---|
96 | class 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 | $include = dirname(__FILE__) . "/Mobile/{$sub}.php"; |
---|
158 | |
---|
159 | if (!class_exists($className)) { |
---|
160 | if (!is_readable($include)) { |
---|
161 | return PEAR::raiseError(null, |
---|
162 | NET_USERAGENT_MOBILE_ERROR_NOT_FOUND, |
---|
163 | null, null, |
---|
164 | "Unable to read the $include file", |
---|
165 | 'Net_UserAgent_Mobile_Error', true |
---|
166 | ); |
---|
167 | } |
---|
168 | if (!include_once $include) { |
---|
169 | return PEAR::raiseError(null, |
---|
170 | NET_USERAGENT_MOBILE_ERROR_NOT_FOUND, |
---|
171 | null, null, |
---|
172 | "Unable to include the $include file", |
---|
173 | 'Net_UserAgent_Mobile_Error', true |
---|
174 | ); |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | $instance = &new $className($request); |
---|
179 | $error = &$instance->isError(); |
---|
180 | if (Net_UserAgent_Mobile::isError($error)) { |
---|
181 | if ($GLOBALS['_NET_USERAGENT_MOBILE_FALLBACK_ON_NOMATCH'] |
---|
182 | && $error->getCode() == NET_USERAGENT_MOBILE_ERROR_NOMATCH |
---|
183 | ) { |
---|
184 | $instance = &Net_UserAgent_Mobile::factory('Net_UserAgent_Mobile_Fallback_On_NoMatch'); |
---|
185 | return $instance; |
---|
186 | } |
---|
187 | |
---|
188 | $instance = &$error; |
---|
189 | } |
---|
190 | |
---|
191 | return $instance; |
---|
192 | } |
---|
193 | |
---|
194 | // }}} |
---|
195 | // {{{ singleton() |
---|
196 | |
---|
197 | /** |
---|
198 | * creates a new {@link Net_UserAgent_Mobile_Common} subclass instance or |
---|
199 | * returns a instance from existent ones |
---|
200 | * |
---|
201 | * @param mixed $stuff User-Agent string or object that works with |
---|
202 | * HTTP_Request (not implemented) |
---|
203 | * @return mixed a newly created or a existent Net_UserAgent_Mobile |
---|
204 | * object, or a PEAR error object on error |
---|
205 | * @see Net_UserAgent_Mobile::factory() |
---|
206 | */ |
---|
207 | function &singleton($stuff = null) |
---|
208 | { |
---|
209 | static $instance; |
---|
210 | if (!isset($instance)) { |
---|
211 | $instance = Net_UserAgent_Mobile::factory($stuff); |
---|
212 | } |
---|
213 | |
---|
214 | return $instance; |
---|
215 | } |
---|
216 | |
---|
217 | // }}} |
---|
218 | // {{{ isError() |
---|
219 | |
---|
220 | /** |
---|
221 | * tell whether a result code from a Net_UserAgent_Mobile method |
---|
222 | * is an error |
---|
223 | * |
---|
224 | * @param integer $value result code |
---|
225 | * @return boolean whether $value is an {@link Net_UserAgent_Mobile_Error} |
---|
226 | */ |
---|
227 | function isError($value) |
---|
228 | { |
---|
229 | return is_a($value, 'Net_UserAgent_Mobile_Error'); |
---|
230 | } |
---|
231 | |
---|
232 | // }}} |
---|
233 | // {{{ errorMessage() |
---|
234 | |
---|
235 | /** |
---|
236 | * return a textual error message for a Net_UserAgent_Mobile error code |
---|
237 | * |
---|
238 | * @param integer $value error code |
---|
239 | * @return string error message, or false if the error code was not |
---|
240 | * recognized |
---|
241 | */ |
---|
242 | function errorMessage($value) |
---|
243 | { |
---|
244 | static $errorMessages; |
---|
245 | if (!isset($errorMessages)) { |
---|
246 | $errorMessages = array( |
---|
247 | NET_USERAGENT_MOBILE_ERROR => 'unknown error', |
---|
248 | NET_USERAGENT_MOBILE_ERROR_NOMATCH => 'no match', |
---|
249 | NET_USERAGENT_MOBILE_ERROR_NOT_FOUND => 'not found', |
---|
250 | NET_USERAGENT_MOBILE_OK => 'no error' |
---|
251 | ); |
---|
252 | } |
---|
253 | |
---|
254 | if (Net_UserAgent_Mobile::isError($value)) { |
---|
255 | $value = $value->getCode(); |
---|
256 | } |
---|
257 | |
---|
258 | return isset($errorMessages[$value]) ? |
---|
259 | $errorMessages[$value] : |
---|
260 | $errorMessages[NET_USERAGENT_MOBILE_ERROR]; |
---|
261 | } |
---|
262 | |
---|
263 | /**#@-*/ |
---|
264 | |
---|
265 | /**#@+ |
---|
266 | * @access private |
---|
267 | */ |
---|
268 | |
---|
269 | /**#@-*/ |
---|
270 | |
---|
271 | // }}} |
---|
272 | } |
---|
273 | |
---|
274 | // }}} |
---|
275 | // {{{ Net_UserAgent_Mobile_Error |
---|
276 | |
---|
277 | /** |
---|
278 | * Net_UserAgent_Mobile_Error implements a class for reporting user |
---|
279 | * agent error messages |
---|
280 | * |
---|
281 | * @category Networking |
---|
282 | * @package Net_UserAgent_Mobile |
---|
283 | * @author KUBO Atsuhiro <iteman@users.sourceforge.net> |
---|
284 | * @copyright 2003-2006 KUBO Atsuhiro <iteman@users.sourceforge.net> |
---|
285 | * @license http://www.php.net/license/3_0.txt PHP License 3.0 |
---|
286 | * @version Release: @package_version@ |
---|
287 | * @since Class available since Release 0.1 |
---|
288 | */ |
---|
289 | class Net_UserAgent_Mobile_Error extends PEAR_Error |
---|
290 | { |
---|
291 | |
---|
292 | // {{{ properties |
---|
293 | |
---|
294 | /**#@+ |
---|
295 | * @access public |
---|
296 | */ |
---|
297 | |
---|
298 | /**#@-*/ |
---|
299 | |
---|
300 | /**#@+ |
---|
301 | * @access private |
---|
302 | */ |
---|
303 | |
---|
304 | /**#@-*/ |
---|
305 | |
---|
306 | /**#@+ |
---|
307 | * @access public |
---|
308 | */ |
---|
309 | |
---|
310 | // }}} |
---|
311 | // {{{ constructor |
---|
312 | |
---|
313 | /** |
---|
314 | * constructor |
---|
315 | * |
---|
316 | * @param mixed $code Net_UserAgent_Mobile error code, or string |
---|
317 | * with error message. |
---|
318 | * @param integer $mode what 'error mode' to operate in |
---|
319 | * @param integer $level what error level to use for $mode and |
---|
320 | * PEAR_ERROR_TRIGGER |
---|
321 | * @param mixed $userinfo additional user/debug info |
---|
322 | * @access public |
---|
323 | */ |
---|
324 | function Net_UserAgent_Mobile_Error($code = NET_USERAGENT_MOBILE_ERROR, |
---|
325 | $mode = PEAR_ERROR_RETURN, |
---|
326 | $level = E_USER_NOTICE, |
---|
327 | $userinfo = null |
---|
328 | ) |
---|
329 | { |
---|
330 | if (is_int($code)) { |
---|
331 | $this->PEAR_Error('Net_UserAgent_Mobile Error: ' . |
---|
332 | Net_UserAgent_Mobile::errorMessage($code), |
---|
333 | $code, $mode, $level, $userinfo |
---|
334 | ); |
---|
335 | } else { |
---|
336 | $this->PEAR_Error("Net_UserAgent_Mobile Error: $code", |
---|
337 | NET_USERAGENT_MOBILE_ERROR, $mode, $level, |
---|
338 | $userinfo |
---|
339 | ); |
---|
340 | } |
---|
341 | } |
---|
342 | |
---|
343 | /**#@-*/ |
---|
344 | |
---|
345 | /**#@+ |
---|
346 | * @access private |
---|
347 | */ |
---|
348 | |
---|
349 | /**#@-*/ |
---|
350 | |
---|
351 | // }}} |
---|
352 | } |
---|
353 | |
---|
354 | // }}} |
---|
355 | |
---|
356 | /* |
---|
357 | * Local Variables: |
---|
358 | * mode: php |
---|
359 | * coding: iso-8859-1 |
---|
360 | * tab-width: 4 |
---|
361 | * c-basic-offset: 4 |
---|
362 | * c-hanging-comment-ender-p: nil |
---|
363 | * indent-tabs-mode: nil |
---|
364 | * End: |
---|
365 | */ |
---|
366 | ?> |
---|