Changeset 17127 for branches/version-2/data/module/Net/UserAgent/Mobile.php
- Timestamp:
- 2008/03/12 16:43:11 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2/data/module/Net/UserAgent/Mobile.php
r15532 r17127 14 14 * @package Net_UserAgent_Mobile 15 15 * @author KUBO Atsuhiro <[email protected]> 16 * @copyright 2003-200 7KUBO Atsuhiro <[email protected]>16 * @copyright 2003-2008 KUBO Atsuhiro <[email protected]> 17 17 * @license http://www.php.net/license/3_0.txt PHP License 3.0 18 18 * @version CVS: $Id$ … … 20 20 */ 21 21 22 require_once dirname(__FILE__) . '/../../PEAR.php'; 23 require_once dirname(__FILE__) . '/Mobile/Request.php'; 22 require_once 'PEAR.php'; 24 23 25 24 // {{{ constants … … 89 88 * @package Net_UserAgent_Mobile 90 89 * @author KUBO Atsuhiro <[email protected]> 91 * @copyright 2003-200 7KUBO Atsuhiro <[email protected]>90 * @copyright 2003-2008 KUBO Atsuhiro <[email protected]> 92 91 * @license http://www.php.net/license/3_0.txt PHP License 3.0 93 * @version Release: 0.3 0.092 * @version Release: 0.31.0 94 93 * @since Class available since Release 0.1 95 94 */ … … 126 125 * If no argument is supplied, $_SERVER{'HTTP_*'} is used. 127 126 * 128 * @param mixed $stuff User-Agent string or object that works with 129 * HTTP_Request (not implemented) 127 * @param string $userAgent User-Agent string 130 128 * @return mixed a newly created Net_UserAgent_Mobile object, or a PEAR 131 129 * 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); 130 */ 131 function &factory($userAgent = null) 132 { 133 if (is_null($userAgent)) { 134 $userAgent = $_SERVER['HTTP_USER_AGENT']; 135 } 147 136 148 137 // 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"; 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 $file = str_replace('_', '/', $class) . '.php'; 160 154 if (!include_once $file) { 161 155 return PEAR::raiseError(null, … … 168 162 } 169 163 170 $instance = &new $class Name($request);164 $instance = &new $class($userAgent); 171 165 $error = &$instance->isError(); 172 166 if (Net_UserAgent_Mobile::isError($error)) { … … 191 185 * returns a instance from existent ones 192 186 * 193 * @param mixed $stuff User-Agent string or object that works with 194 * HTTP_Request (not implemented) 187 * @param string $userAgent User-Agent string 195 188 * @return mixed a newly created or a existent Net_UserAgent_Mobile 196 189 * object, or a PEAR error object on error 197 190 * @see Net_UserAgent_Mobile::factory() 198 191 */ 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 } 192 function &singleton($userAgent = null) 193 { 194 static $instances; 195 196 if (!isset($instances)) { 197 $instances = array(); 198 } 199 200 if (is_null($userAgent)) { 201 $userAgent = $_SERVER['HTTP_USER_AGENT']; 202 } 203 204 if (!array_key_exists($userAgent, $instances)) { 205 $instances[$userAgent] = Net_UserAgent_Mobile::factory($userAgent); 206 } 207 208 return $instances[$userAgent]; 209 } 208 210 209 211 // }}} … … 253 255 } 254 256 257 // }}} 258 // {{{ isMobile() 259 260 /** 261 * Checks whether or not the user agent is mobile by a given user agent 262 * string. 263 * 264 * @param string $userAgent 265 * @return boolean 266 * @since Method available since Release 0.31.0 267 */ 268 function isMobile($userAgent = null) 269 { 270 if (Net_UserAgent_Mobile::isDoCoMo($userAgent)) { 271 return true; 272 } elseif (Net_UserAgent_Mobile::isEZweb($userAgent)) { 273 return true; 274 } elseif (Net_UserAgent_Mobile::isSoftBank($userAgent)) { 275 return true; 276 } elseif (Net_UserAgent_Mobile::isWillcom($userAgent)) { 277 return true; 278 } 279 280 return false; 281 } 282 283 // }}} 284 // {{{ isDoCoMo() 285 286 /** 287 * Checks whether or not the user agent is DoCoMo by a given user agent 288 * string. 289 * 290 * @param string $userAgent 291 * @return boolean 292 * @since Method available since Release 0.31.0 293 */ 294 function isDoCoMo($userAgent = null) 295 { 296 if (is_null($userAgent)) { 297 $userAgent = $_SERVER['HTTP_USER_AGENT']; 298 } 299 300 if (preg_match('!^DoCoMo!', $userAgent)) { 301 return true; 302 } 303 304 return false; 305 } 306 307 // }}} 308 // {{{ isEZweb() 309 310 /** 311 * Checks whether or not the user agent is EZweb by a given user agent 312 * string. 313 * 314 * @param string $userAgent 315 * @return boolean 316 * @since Method available since Release 0.31.0 317 */ 318 function isEZweb($userAgent = null) 319 { 320 if (is_null($userAgent)) { 321 $userAgent = $_SERVER['HTTP_USER_AGENT']; 322 } 323 324 if (preg_match('!^KDDI-!', $userAgent)) { 325 return true; 326 } elseif (preg_match('!^UP\.Browser!', $userAgent)) { 327 return true; 328 } 329 330 return false; 331 } 332 333 // }}} 334 // {{{ isSoftBank() 335 336 /** 337 * Checks whether or not the user agent is SoftBank by a given user agent 338 * string. 339 * 340 * @param string $userAgent 341 * @return boolean 342 * @since Method available since Release 0.31.0 343 */ 344 function isSoftBank($userAgent = null) 345 { 346 if (is_null($userAgent)) { 347 $userAgent = $_SERVER['HTTP_USER_AGENT']; 348 } 349 350 if (preg_match('!^SoftBank!', $userAgent)) { 351 return true; 352 } elseif (preg_match('!^Semulator!', $userAgent)) { 353 return true; 354 } elseif (preg_match('!^Vodafone!', $userAgent)) { 355 return true; 356 } elseif (preg_match('!^Vemulator!', $userAgent)) { 357 return true; 358 } elseif (preg_match('!^MOT-!', $userAgent)) { 359 return true; 360 } elseif (preg_match('!^MOTEMULATOR!', $userAgent)) { 361 return true; 362 } elseif (preg_match('!^J-PHONE!', $userAgent)) { 363 return true; 364 } elseif (preg_match('!^J-EMULATOR!', $userAgent)) { 365 return true; 366 } 367 368 return false; 369 } 370 371 // }}} 372 // {{{ isWillcom() 373 374 /** 375 * Checks whether or not the user agent is Willcom by a given user agent 376 * string. 377 * 378 * @param string $userAgent 379 * @return boolean 380 * @since Method available since Release 0.31.0 381 */ 382 function isWillcom($userAgent = null) 383 { 384 if (is_null($userAgent)) { 385 $userAgent = $_SERVER['HTTP_USER_AGENT']; 386 } 387 388 if (preg_match('!^Mozilla/3\.0\((?:DDIPOCKET|WILLCOM);!', $userAgent)) { 389 return true; 390 } 391 392 return false; 393 } 394 255 395 /**#@-*/ 256 396 … … 276 416 * @copyright 2003-2007 KUBO Atsuhiro <[email protected]> 277 417 * @license http://www.php.net/license/3_0.txt PHP License 3.0 278 * @version Release: 0.3 0.0418 * @version Release: 0.31.0 279 419 * @since Class available since Release 0.1 280 420 */ … … 356 496 * End: 357 497 */ 358 ?>
Note: See TracChangeset
for help on using the changeset viewer.
