Net_UserAgent_Detect patch
| From: | KUBO Atsuhiro | Date: | Wed, 28 Aug 2002 16:16:49 +0000 |
| Subject: | Net_UserAgent_Detect patch | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-8716@lists.php.net to get a copy of this message | ||
Hi.
I have a patch for the Net_UserAgent_Detect.
The purpose is to support subclassing of the Net_UserAgent_Detect
class. (keep compatibility)
Please review this patch. If you please, apply this patch.
This patch includes the following:
* Changed singleton() method implementation.
* Changed detect() method in order to apply Template method pattern.
* Added a $detectedFlags property in order to carry out easy access
from a hook operation.
* Moved two statements containing array_flip() function into
constructor.
* Changed condition for $this object in each method.
Regards,
--
KUBO Atsuhiro e-mail: kubo@isite.co.jp
Index: Detect.php
===================================================================
RCS file: /repository/pear/Net_UserAgent/Detect.php,v
retrieving revision 1.2
diff -u -r1.2 Detect.php
--- Detect.php 15 May 2002 09:38:59 -0000 1.2
+++ Detect.php 28 Aug 2002 12:27:14 -0000
@@ -19,6 +19,8 @@
// $Id: Detect.php,v 1.2 2002/05/15 09:38:59 dallen Exp $
+require_once('PEAR.php');
+
// }}}
// {{{ description
@@ -149,6 +151,8 @@
var $encoding = array();
+ var $detectFlags = array();
+
// }}}
// {{{ singleton
@@ -160,18 +164,25 @@
* once. Therefore, all of the all of the properties and methods must map
* to a single state. Therefore, this function is used in place of the
* contructor to return any open instances of the client object, and if none
- * are open will create a new instance and cache it using a static
- * variable.
+ * are open will create a new instance and cache it using a PEAR::getStaticProperty method.
*
* @access public
- * @return object Net_UserAgent_Detect instance
+ * @return object Net_UserAgent_Detect instance or instance of a sub class of
Net_UserAgent_Detect
*/
function &singleton($in_userAgent = null, $in_detect = null)
{
- static $instance;
-
- if (!isset($instance)) {
- $instance = new Net_UserAgent_Detect($in_userAgent);
+ if (isset($_SERVER['Net_UserAgent_Detect'])
+ && class_exists($_SERVER['Net_UserAgent_Detect'])
+ ) {
+ $classname = $_SERVER['Net_UserAgent_Detect'];
+ }
+ else {
+ $classname = 'Net_UserAgent_Detect';
+ }
+
+ $instance = &PEAR::getStaticProperty($classname, 'instance');
+ if (is_null($instance)) {
+ $instance = new $classname($in_userAgent);
}
return $instance;
@@ -182,6 +193,8 @@
function Net_UserAgent_Detect($in_userAgent = null, $in_detect = null)
{
+ $this->browser = array_flip($this->browser);
+ $this->os = array_flip($this->os);
$this->detect($in_userAgent, $in_detect);
}
@@ -194,6 +207,9 @@
* This is the core of the Net_UserAgent_Detect class. It moves its way through the user agent
* string setting up the flags based on the vendors and versions of the browsers, determining
* the OS and setting up the features and quirks owned by each of the relevant clients.
+ * Finally this method calls a doDetect method. If you want to add operation to this method,
+ * you will be able to override the doDetect method in the extended class.
+ * See Template Method Pattern in "Design Patterns" GoF book.
*
* @param string (optional) user agent override
*
@@ -202,8 +218,8 @@
*/
function detect($in_userAgent = null, $in_detect = null)
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
// detemine what user agent we are using
@@ -231,22 +247,21 @@
settype($detect, 'array');
foreach($this->detectOptions as $option) {
if (in_array($option, $detect)) {
- $detectFlags[$option] = true;
+ $this->detectFlags[$option] = true;
}
else {
- $detectFlags[$option] = false;
+ $this->detectFlags[$option] = false;
}
}
// initialize the flag arrays
$brwsr =& $this->browser;
- $brwsr = array_flip($brwsr);
$os =& $this->os;
- $os = array_flip($os);
// Get the type and version of the client
- preg_match(";^([[:alpha:]]+)[ /\(]*[[:alpha:]]*([\d]*)\.([\d\.]*);", $agt,
$matches);
- list($null, $this->leadingIdentifier, $this->majorVersion, $this->subVersion) =
$matches;
+ if (preg_match(";^([[:alpha:]]+)[ /\(]*[[:alpha:]]*([\d]*)\.([\d\.]*);", $agt,
$matches)) {
+ list($null, $this->leadingIdentifier, $this->majorVersion, $this->subVersion)
= $matches;
+ }
if (empty($this->leadingIdentifier)) {
$this->leadingIdentifier = 'Unknown';
}
@@ -254,7 +269,7 @@
$this->version = $this->majorVersion . '.' . $this->subVersion;
// Browser type
- if ($detectFlags[NET_USERAGENT_DETECT_ALL] || $detectFlags[NET_USERAGENT_DETECT_BROWSER]) {
+ if ($this->detectFlags[NET_USERAGENT_DETECT_ALL] ||
$this->detectFlags[NET_USERAGENT_DETECT_BROWSER]) {
$brwsr['konq'] = (strpos($agt, 'konqueror') !== false);
$brwsr['text'] = (strpos($agt, 'links') !== false) ||
(strpos($agt, 'lynx') !== false) || (strpos($agt, 'w3m') !== false);
$brwsr['ns'] = (strpos($agt, 'mozilla') !== false) &&
!(strpos($agt, 'spoofer') !== false) && !(strpos($agt, 'compatible') !==
false) && !(strpos($agt, 'hotjava') !== false) && !(strpos($agt,
'opera') !== false) && !(strpos($agt, 'webtv') !== false) ? 1 : 0;
@@ -297,7 +312,7 @@
$brwsr['hotjava3up'] = $brwsr['hotjava'] &&
$this->majorVersion >= 3;
}
- if ($detectFlags[NET_USERAGENT_DETECT_ALL] || ($detectFlags[NET_USERAGENT_DETECT_BROWSER]
&& $detectFlags[NET_USERAGENT_DETECT_FEATURES])) {
+ if ($this->detectFlags[NET_USERAGENT_DETECT_ALL] ||
($this->detectFlags[NET_USERAGENT_DETECT_BROWSER] &&
$this->detectFlags[NET_USERAGENT_DETECT_FEATURES])) {
// Javascript Check
if ($brwsr['ns2'] || $brwsr['ie3']) {
$this->setFeature('javascript', 1.0);
@@ -324,7 +339,7 @@
}
/** OS Check **/
- if ($detectFlags[NET_USERAGENT_DETECT_ALL] || $detectFlags[NET_USERAGENT_DETECT_OS]) {
+ if ($this->detectFlags[NET_USERAGENT_DETECT_ALL] ||
$this->detectFlags[NET_USERAGENT_DETECT_OS]) {
$os['win'] = (strpos($agt, 'win') !== false) || (strpos($agt,
'16bit') !== false);
$os['win95'] = (strpos($agt, 'win95') !== false) || (strpos($agt,
'windows 95') !== false);
$os['win16'] = (strpos($agt, 'win16') !== false) || (strpos($agt,
'16bit') !== false) || (strpos($agt, 'windows 3.1') !== false) || (strpos($agt,
'windows 16-bit') !== false);
@@ -368,7 +383,7 @@
}
// Setup the quirks
- if ($detectFlags[NET_USERAGENT_DETECT_ALL] || ($detectFlags[NET_USERAGENT_DETECT_BROWSER]
&& $detectFlags[NET_USERAGENT_DETECT_QUIRKS])) {
+ if ($this->detectFlags[NET_USERAGENT_DETECT_ALL] ||
($this->detectFlags[NET_USERAGENT_DETECT_BROWSER] &&
$this->detectFlags[NET_USERAGENT_DETECT_QUIRKS])) {
if ($brwsr['konq']) {
$this->setQuirk('empty_file_input_value');
}
@@ -392,7 +407,7 @@
}
// Set features
- if ($detectFlags[NET_USERAGENT_DETECT_ALL] || ($detectFlags[NET_USERAGENT_DETECT_BROWSER]
&& $detectFlags[NET_USERAGENT_DETECT_FEATURES])) {
+ if ($this->detectFlags[NET_USERAGENT_DETECT_ALL] ||
($this->detectFlags[NET_USERAGENT_DETECT_BROWSER] &&
$this->detectFlags[NET_USERAGENT_DETECT_FEATURES])) {
if ($brwsr['gecko']) {
preg_match(';gecko/([\d]+)\b;i', $agt, $matches);
$this->setFeature('gecko', $matches[1]);
@@ -407,7 +422,7 @@
}
}
- if ($detectFlags[NET_USERAGENT_DETECT_ALL] || $detectFlags[NET_USERAGENT_DETECT_ACCEPT]) {
+ if ($this->detectFlags[NET_USERAGENT_DETECT_ALL] ||
$this->detectFlags[NET_USERAGENT_DETECT_ACCEPT]) {
$mimetypes = preg_split(';[\s,]+;', substr(getenv('HTTP_ACCEPT'),
0, strpos(getenv('HTTP_ACCEPT') . ';', ';')), -1,
PREG_SPLIT_NO_EMPTY);
$this->setAcceptType((array) $mimetypes, 'mimetype');
@@ -425,7 +440,21 @@
$this->setAcceptType((array) $charsets, 'charset');
}
+
+ // Call additional operation
+ $this->doDetect();
}
+
+ // }}}
+ // {{{ doDetect()
+
+ /**
+ * Primitive operation for another step of the detect method
+ *
+ * @access public
+ * @return void
+ */
+ function doDetect() {}
// }}}
// {{{ isBrowser()
@@ -443,8 +472,8 @@
*/
function isBrowser($in_match)
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
$match = strtolower($in_match);
@@ -467,8 +496,8 @@
*/
function getBrowser($in_expectList)
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
foreach((array) $in_expectList as $browser) {
@@ -508,8 +537,8 @@
'konq' => 'Konqueror',
))
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
foreach((array) $in_vendorStrings as $flag => $string) {
@@ -537,8 +566,8 @@
*/
function isIE()
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
return !empty($this->browser['ie']);
@@ -555,8 +584,8 @@
*/
function isNavigator()
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
return !empty($this->browser['nav']);
@@ -577,8 +606,8 @@
*/
function isNetscape()
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
return !empty($this->browser['ns4up']);
@@ -600,8 +629,8 @@
*/
function isOS($in_match)
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
$match = strtolower($in_match);
@@ -622,8 +651,8 @@
*/
function getOS($in_expectList)
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
foreach((array) $in_expectList as $os) {
@@ -660,8 +689,8 @@
'unix' => 'Linux/Unix',
))
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
$osString = 'Unknown';
@@ -690,8 +719,8 @@
*/
function setQuirk($in_quirk, $in_hasQuirk = true)
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
$hasQuirk = !empty($in_hasQuirk);
@@ -713,8 +742,8 @@
*/
function hasQuirk($in_quirk)
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
return !empty($this->quirks[strtolower($in_quirk)]);
@@ -735,8 +764,8 @@
*/
function getQuirk()
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
return isset($this->quirks[strtolower($in_quirks)]) ?
$this->quirks[strtolower($in_quirks)] : null;
@@ -757,8 +786,8 @@
*/
function setFeature($in_feature, $in_hasFeature = true)
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
$this->features[strtolower($in_feature)] = $in_hasFeature;
@@ -779,8 +808,8 @@
*/
function hasFeature($in_feature)
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
return !empty($this->features[strtolower($in_feature)]);
@@ -801,8 +830,8 @@
*/
function getFeature($in_feature)
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
return isset($this->features[strtolower($in_feature)]) ?
$this->features[strtolower($in_feature)] : null;
@@ -829,8 +858,8 @@
*/
function getAcceptType($in_expectList, $in_type)
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
$type = strtolower($in_type);
@@ -866,8 +895,8 @@
*/
function setAcceptType($in_values, $in_type)
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
$type = strtolower($in_type);
@@ -899,8 +928,8 @@
*/
function hasAcceptType($in_value, $in_type)
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
$type = strtolower($in_type);
@@ -925,8 +954,8 @@
*/
function getUserAgent()
{
- if (!isset($this) || get_class($this) != 'net_useragent_detect') {
- $this =& Net_UserAgent_Detect::singleton();
+ if (!isset($this)) {
+ $this = &Net_UserAgent_Detect::singleton();
}
return $this->userAgent;