Permalink
Browse files

* Adding support windows ce (Lucas Nealan)

* Adding support for IE mobile (Lucas Nealan)
* Quirk detect for AJAX has been updated (Lucas Nealan)
* Added a subclass, Net_UserAgent_Detect_APC, which allows Net_UserAgent_Detect results to be cached which makes subsequent lookups for that useragent very fast


git-svn-id: http://svn.php.net/repository/pear/packages/Net_UserAgent/trunk@242708 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent 0985d11 commit 7e113a5c18bd82eb4c48b7d7ef59337e26191654 Jason Rust committed Sep 19, 2007
Showing with 126 additions and 6 deletions.
  1. +12 −6 Detect.php
  2. +114 −0 Detect/Apc.php
View
@@ -71,7 +71,7 @@ function Net_UserAgent_Detect($in_userAgent = null, $in_detect = null)
* @return object Net_UserAgent_Detect instance
*/
function &singleton($in_userAgent = null, $in_detect = null)
- {
+ {
static $instance;
if (!isset($instance)) {
@@ -113,8 +113,8 @@ function detect($in_userAgent = null, $in_detect = null)
$hasRun = true;
// {{{ set up static properties
- $in_userAgent = isset($options['userAgent']) && is_null($in_userAgent) ? $options['userAgent'] : null;
- $in_detect = isset($options['detectOptions']) && is_null($in_detect) ? $options['detectOptions'] : null;
+ $in_userAgent = isset($options['userAgent']) && is_null($in_userAgent) ? $options['userAgent'] : $in_userAgent;
+ $in_detect = isset($options['detectOptions']) && is_null($in_detect) ? $options['detectOptions'] : $in_detect;
// User agent string that is being analyzed
$userAgent = &Net_UserAgent_Detect::_getStaticProperty('userAgent');
@@ -127,7 +127,7 @@ function detect($in_userAgent = null, $in_detect = null)
// Array that stores all of the flags for the operating systems,
// and in some cases the versions of those operating systems (windows)
$os = &Net_UserAgent_Detect::_getStaticProperty('os');
- $os = array_flip(array('win', 'win95', 'win16', 'win31', 'win9x', 'win98', 'winme', 'win2k', 'winxp', 'winnt', 'win2003', 'os2', 'mac', 'mac68k', 'macppc', 'linux', 'unix', 'vms', 'sun', 'sun4', 'sun5', 'suni86', 'irix', 'irix5', 'irix6', 'hpux', 'hpux9', 'hpux10', 'aix', 'aix1', 'aix2', 'aix3', 'aix4', 'sco', 'unixware', 'mpras', 'reliant', 'dec', 'sinix', 'freebsd', 'bsd'));
+ $os = array_flip(array('win', 'win95', 'win16', 'win31', 'win9x', 'win98', 'wince', 'winme', 'win2k', 'winxp', 'winnt', 'win2003', 'os2', 'mac', 'mac68k', 'macppc', 'linux', 'unix', 'vms', 'sun', 'sun4', 'sun5', 'suni86', 'irix', 'irix5', 'irix6', 'hpux', 'hpux9', 'hpux10', 'aix', 'aix1', 'aix2', 'aix3', 'aix4', 'sco', 'unixware', 'mpras', 'reliant', 'dec', 'sinix', 'freebsd', 'bsd'));
// Array which stores known issues with the given client that can
// be used for on the fly tweaking so that the client may recieve
@@ -269,7 +269,7 @@ function detect($in_userAgent = null, $in_detect = null)
$browser['opera6'] = strpos($agt, 'opera 6') !== false || strpos($agt, 'opera/6') !== false;
$browser['opera7'] = strpos($agt, 'opera 7') !== false || strpos($agt, 'opera/7') !== false;
$browser['opera8'] = strpos($agt, 'opera 8') !== false || strpos($agt, 'opera/8') !== false;
- $browser['opera9'] = strpos($agt, 'opera 8') !== false || strpos($agt, 'opera/9') !== false;
+ $browser['opera9'] = strpos($agt, 'opera 9') !== false || strpos($agt, 'opera/9') !== false;
$browser['opera5up'] = $browser['opera'] && !$browser['opera2'] && !$browser['opera3'] && !$browser['opera4'];
$browser['opera6up'] = $browser['opera'] && !$browser['opera2'] && !$browser['opera3'] && !$browser['opera4'] && !$browser['opera5'];
$browser['opera7up'] = $browser['opera'] && !$browser['opera2'] && !$browser['opera3'] && !$browser['opera4'] && !$browser['opera5'] && !$browser['opera6'];
@@ -288,6 +288,7 @@ function detect($in_userAgent = null, $in_detect = null)
$browser['hotjava'] = strpos($agt, 'hotjava') !== false;
$browser['hotjava3'] = $browser['hotjava'] && $majorVersion == 3;
$browser['hotjava3up'] = $browser['hotjava'] && $majorVersion >= 3;
+ $browser['iemobile'] = strpos($agt, 'iemobile') !== false || strpos($agt, 'windows ce') !== false && (strpos($agt, 'ppc') !== false || strpos($agt, 'smartphone') !== false);
}
if ($detectFlags[NET_USERAGENT_DETECT_ALL] ||
@@ -296,6 +297,9 @@ function detect($in_userAgent = null, $in_detect = null)
if ($browser['ns2'] || $browser['ie3']) {
Net_UserAgent_Detect::setFeature('javascript', 1.0);
}
+ elseif ($browser['iemobile']) {
+ // no javascript
+ }
elseif ($browser['opera5up']) {
Net_UserAgent_Detect::setFeature('javascript', 1.3);
}
@@ -324,6 +328,7 @@ function detect($in_userAgent = null, $in_detect = null)
$os['win16'] = strpos($agt, 'win16') !== false || strpos($agt, '16bit') !== false || strpos($agt, 'windows 3.1') !== false || strpos($agt, 'windows 16-bit') !== false;
$os['win31'] = strpos($agt, 'windows 3.1') !== false || strpos($agt, 'win16') !== false || strpos($agt, 'windows 16-bit') !== false;
$os['winme'] = strpos($agt, 'win 9x 4.90') !== false;
+ $os['wince'] = strpos($agt, 'windows ce') !== false;
$os['win2k'] = strpos($agt, 'windows nt 5.0') !== false;
$os['winxp'] = strpos($agt, 'windows nt 5.1') !== false;
$os['win2003'] = strpos($agt, 'windows nt 5.2') !== false;
@@ -404,7 +409,7 @@ function detect($in_userAgent = null, $in_detect = null)
Net_UserAgent_Detect::setFeature('gecko', $matches[1]);
}
- if ($browser['gecko'] || $browser['ie5up'] || $browser['konq'] || $browser['opera8up']) {
+ if ($browser['gecko'] || ($browser['ie5up'] && !$browser['iemobile']) || $browser['konq'] || $browser['opera8up'] && !$os['wince']) {
Net_UserAgent_Detect::setFeature('ajax');
}
@@ -683,6 +688,7 @@ function getOSString($in_osStrings = null)
if (is_null($in_osStrings)) {
$in_osStrings = array(
'win' => 'Microsoft Windows',
+ 'wince' => 'Microsoft Windows CE',
'win9x' => 'Microsoft Windows 9x',
'winme' => 'Microsoft Windows Millenium',
'win2k' => 'Microsoft Windows 2000',
View
@@ -0,0 +1,114 @@
+<?php
+// +----------------------------------------------------------------------+
+// | PHP version 4.2 |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 1997-2003 The PHP Group |
+// +----------------------------------------------------------------------+
+// | This source file is subject to version 2.0 of the PHP license, |
+// | that is bundled with this package in the file LICENSE, and is |
+// | available at through the world-wide-web at |
+// | http://www.php.net/license/2_02.txt. |
+// | If you did not receive a copy of the PHP license and are unable to |
+// | obtain it through the world-wide-web, please send a note to |
+// | license@php.net so we can mail you a copy immediately. |
+// +----------------------------------------------------------------------+
+// | Authors: Lucas Nealan <lucas@facebook.com> |
+// +----------------------------------------------------------------------+
+
+// $Id$
+
+require_once 'Net_UserAgent/detect.php';
+
+class Net_UserAgent_Detect_APC extends Net_UserAgent_Detect
+{
+ var $key = '';
+
+ function Net_UserAgent_Detect_APC($in_userAgent = null, $in_detect = null, $ua_cache_window = 600)
+ {
+ $data = '';
+ $restored = false;
+ $ua_cache_timeout = apc_fetch('useragent:cache_timeout'); // don't cache after time period
+
+ if ($ua_cache_window > 0) {
+ if (!$ua_cache_timeout) {
+ // check apc uptime and disable after x mins
+ $apc_data = apc_cache_info('file', true);
+
+ if (isset($apc_data['start_time'])) {
+ $uptime = $apc_data['start_time'];
+
+ if (time() - $uptime > $ua_cache_window) { // timeout and disable after 10 minutes of uptime
+ apc_store('useragent:cache_timeout', true);
+ $ua_cache_timeout = true; // don't cache this one either
+ }
+ }
+ }
+
+ if (!$this->key) {
+ $key_flags = '';
+ if ($in_detect !== null) {
+ $key_flags = implode('-', $in_detect);
+ }
+ $this->key = 'useragent:'.md5($in_userAgent.$key_flags);
+ }
+
+ if ($data = apc_fetch($this->key)) {
+ $success = null;
+ $data = unserialize($data);
+ if ($data) {
+ $restored = $this->cache_restore($data);
+ }
+ }
+ }
+
+ if (!$data) {
+ $this->detect($in_userAgent, $in_detect);
+
+ if ($ua_cache_window > 0 && !$ua_cache_timeout) {
+ $this->cache_save();
+ }
+ }
+ }
+
+ function &singleton($in_userAgent = null, $in_detect = null)
+ {
+ static $instance;
+
+ if (!isset($instance)) {
+ $instance = new Net_UserAgent_Detect_APC($in_userAgent, $in_detect);
+ }
+
+ return $instance;
+ }
+
+ function cache_restore($cache)
+ {
+ if (is_array($cache)) {
+ foreach($cache as $prop => $value) {
+ $ptr = Net_UserAgent_Detect::_getStaticProperty($prop);
+ $ptr = $value;
+ }
+ return true;
+ }
+ return false;
+ }
+
+ function cache_save()
+ {
+ if ($this->key) {
+ $data = array('browser' => Net_UserAgent_Detect::_getStaticProperty('browser'),
+ 'features' => Net_UserAgent_Detect::_getStaticProperty('features'),
+ 'leadingIdentifier' => Net_UserAgent_Detect::_getStaticProperty('leadingIdentifier'),
+ 'majorVersion' => Net_UserAgent_Detect::_getStaticProperty('majorVersion'),
+ 'options' => Net_UserAgent_Detect::_getStaticProperty('options'),
+ 'os' => Net_UserAgent_Detect::_getStaticProperty('os'),
+ 'quirks' => Net_UserAgent_Detect::_getStaticProperty('quirks'),
+ 'subVersion' => Net_UserAgent_Detect::_getStaticProperty('subVersion'),
+ 'userAgent' => Net_UserAgent_Detect::_getStaticProperty('userAgent'),
+ 'version' => Net_UserAgent_Detect::_getStaticProperty('version'),
+ );
+ apc_store($this->key, serialize($data));
+ }
+ }
+}
+?>

0 comments on commit 7e113a5

Please sign in to comment.