Re: Net_UserAgent_Detect patch

From: Date: Thu, 29 Aug 2002 07:16:23 +0000
Subject: Re: Net_UserAgent_Detect patch
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-8727@lists.php.net to get a copy of this message
Hi. > * Changed condition for $this object in each method. Sorry, I didn't really understand $this behavior. I fixed this item. 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 29 Aug 2002 07:00:29 -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,10 @@ */ 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) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } // detemine what user agent we are using @@ -231,22 +249,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 +271,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 +314,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 +341,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 +385,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 +409,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 +424,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 +442,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 +474,10 @@ */ function isBrowser($in_match) { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } $match = strtolower($in_match); @@ -467,8 +500,10 @@ */ function getBrowser($in_expectList) { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } foreach((array) $in_expectList as $browser) { @@ -508,8 +543,10 @@ 'konq' => 'Konqueror', )) { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } foreach((array) $in_vendorStrings as $flag => $string) { @@ -537,8 +574,10 @@ */ function isIE() { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } return !empty($this->browser['ie']); @@ -555,8 +594,10 @@ */ function isNavigator() { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } return !empty($this->browser['nav']); @@ -577,8 +618,10 @@ */ function isNetscape() { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } return !empty($this->browser['ns4up']); @@ -600,8 +643,10 @@ */ function isOS($in_match) { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } $match = strtolower($in_match); @@ -622,8 +667,10 @@ */ function getOS($in_expectList) { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } foreach((array) $in_expectList as $os) { @@ -660,8 +707,10 @@ 'unix' => 'Linux/Unix', )) { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } $osString = 'Unknown'; @@ -690,8 +739,10 @@ */ function setQuirk($in_quirk, $in_hasQuirk = true) { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } $hasQuirk = !empty($in_hasQuirk); @@ -713,8 +764,10 @@ */ function hasQuirk($in_quirk) { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } return !empty($this->quirks[strtolower($in_quirk)]); @@ -735,8 +788,10 @@ */ function getQuirk() { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } return isset($this->quirks[strtolower($in_quirks)]) ? $this->quirks[strtolower($in_quirks)] : null; @@ -757,8 +812,10 @@ */ function setFeature($in_feature, $in_hasFeature = true) { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } $this->features[strtolower($in_feature)] = $in_hasFeature; @@ -779,8 +836,10 @@ */ function hasFeature($in_feature) { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } return !empty($this->features[strtolower($in_feature)]); @@ -801,8 +860,10 @@ */ function getFeature($in_feature) { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } return isset($this->features[strtolower($in_feature)]) ? $this->features[strtolower($in_feature)] : null; @@ -829,8 +890,10 @@ */ function getAcceptType($in_expectList, $in_type) { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } $type = strtolower($in_type); @@ -866,8 +929,10 @@ */ function setAcceptType($in_values, $in_type) { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } $type = strtolower($in_type); @@ -899,8 +964,10 @@ */ function hasAcceptType($in_value, $in_type) { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } $type = strtolower($in_type); @@ -925,8 +992,10 @@ */ function getUserAgent() { - if (!isset($this) || get_class($this) != 'net_useragent_detect') { - $this =& Net_UserAgent_Detect::singleton(); + if (!isset($this) || !is_subclass_of($this, 'Net_UserAgent_Detect') + || get_class($this) !== 'Net_UserAgent_Detect' + ) { + $this = &Net_UserAgent_Detect::singleton(); } return $this->userAgent;

« previous php.pear.dev (#8727) next »