Removing constant REQUEST_MOBILE_UA, and added variable $mobileUA instead for mobile browser detection. Also added new user agent string 'webOS', closes #457

This commit is contained in:
ADmad 2010-03-15 01:58:38 +05:30
parent 4c7b62bf64
commit 70d0c64283
2 changed files with 45 additions and 6 deletions

View file

@ -20,10 +20,6 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
if (!defined('REQUEST_MOBILE_UA')) {
define('REQUEST_MOBILE_UA', '(Android|iPod|iPhone|MIDP|AvantGo|BlackBerry|J2ME|Opera Mini|DoCoMo|NetFront|Nokia|PalmOS|PalmSource|portalmmm|Plucker|ReqwirelessWeb|SonyEricsson|Symbian|UP\.Browser|Windows CE|Xiino)');
}
/**
* Request object for handling HTTP requests
*
@ -105,6 +101,37 @@ class RequestHandlerComponent extends Object {
'tar' => 'application/x-tar'
);
/**
* List of regular expressions for matching mobile device's user agent string
*
* @var array
* @access public
*/
var $mobileUA = array(
'Android',
'AvantGo',
'BlackBerry',
'DoCoMo',
'iPod',
'iPhone',
'J2ME',
'MIDP',
'NetFront',
'Nokia',
'Opera Mini',
'PalmOS',
'PalmSource',
'portalmmm',
'Plucker',
'ReqwirelessWeb',
'SonyEricsson',
'Symbian',
'UP\.Browser',
'webOS',
'Windows CE',
'Xiino'
);
/**
* Content-types accepted by the client. If extension parsing is enabled in the
* Router, and an extension is detected, the corresponding content-type will be
@ -314,10 +341,16 @@ class RequestHandlerComponent extends Object {
*
* @return boolean True if user agent is a mobile web browser
* @access public
* @deprecated Use of constant REQUEST_MOBILE_UA is deprecated and will be removed in future versions
*/
function isMobile() {
preg_match('/' . REQUEST_MOBILE_UA . '/i', env('HTTP_USER_AGENT'), $match);
if (!empty($match) || $this->accepts('wap')) {
if (defined('REQUEST_MOBILE_UA')) {
$regex = '/' . REQUEST_MOBILE_UA . '/i';
} else {
$regex = '/' . implode('|', $this->mobileUA) . '/i';
}
if (preg_match($regex, env('HTTP_USER_AGENT')) || $this->accepts('wap')) {
return true;
}
return false;

View file

@ -427,8 +427,14 @@ class RequestHandlerComponentTest extends CakeTestCase {
*/
function testMobileDeviceDetection() {
$this->assertFalse($this->RequestHandler->isMobile());
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3';
$this->assertTrue($this->RequestHandler->isMobile());
$_SERVER['HTTP_USER_AGENT'] = 'Some imaginary UA';
$this->RequestHandler->mobileUA []= 'imaginary';
$this->assertTrue($this->RequestHandler->isMobile());
array_pop($this->RequestHandler->mobileUA);
}
/**