/** * Singleton implementation. * * @static * @return Opc_Visit */ public static function getInstance() { if (self::$_instance == null) { self::$_instance = new Opc_Visit_UserAgent(); } return self::$_instance; }
/** * @dataProvider userAgentProvider */ public function testUserAgents($userAgent, $browser, $os) { $result = Opc_Visit_UserAgent::getInstance()->analyze($userAgent); foreach ($result as $group => $fields) { foreach ($fields as $key => $value) { $this->assertEquals(${$group}[$key], $value); } } }
/** * Gets the value of the specified parameter. * * @param string $key * @return mixed */ public function get($key) { // Filter out private fields.. if ($key[0] == '_') { throw new Opc_OptionNotExists_Exception($key, __CLASS__); } // At first, we check if the data is persisted in a variable. if ($this->{$key} != null) { return $this->{$key}; } // Now, we can fill empty variables. switch ($key) { case 'ip': $this->ip = $_SERVER['REMOTE_ADDR']; break; case 'numericIp': $this->numericIp = ip2long($_SERVER['REMOTE_ADDR']); break; case 'host': if (empty($_SERVER['REMOTE_HOST'])) { $this->host = @gethostbyaddr($_SERVER['REMOTE_ADDR']); } else { $this->host = $_SERVER['REMOTE_HOST']; } break; case 'protocol': $recognized = array('HTTPS' => 'https', 'HTTP' => 'http', 'WAP' => 'wap'); $this->protocol = 'unknown'; foreach ($recognized as $lookFor => $protocol) { if (strpos($_SERVER['SERVER_PROTOCOL'], $lookFor) !== false) { $this->protocol = $protocol; break; } } break; case 'referrer': $this->referrer = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; break; case 'requestMethod': $this->requestMethod = $_SERVER['REQUEST_METHOD']; break; case 'userAgentString': $this->userAgentString = $_SERVER['HTTP_USER_AGENT']; break; case 'userAgent': $this->userAgent = Opc_Visit_UserAgent::getInstance()->analyze($_SERVER['HTTP_USER_AGENT']); break; case 'port': $this->port = $_SERVER['SERVER_PORT']; break; case 'secure': $this->secure = $_SERVER['SERVER_PORT'] == 443; break; case 'languages': $this->languages = $this->_parseQualityString(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : null); break; case 'mimeTypes': $this->mimeTypes = $this->_parseQualityString(isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : null); break; case 'cookieServer': $this->cookieServer = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']; break; case 'cookiePath': $this->cookiePath = substr($this->get('currentPath'), strpos($this->get('currentPath'), $this->get('cookieServer')) + strlen($this->get('cookieServer')), strlen($this->get('currentPath'))); break; case 'currentAddress': case 'currentFile': case 'currentParams': case 'currentPath': case 'basePath': case 'pathInfo': case 'queryString': $this->currentPath = $this->currentAddress = $this->currentFile = $this->get('protocol') . '://'; $serverName = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']; $this->currentAddress .= $serverName . $_SERVER['REQUEST_URI']; $this->currentFile .= $serverName . $_SERVER['PHP_SELF']; $this->pathInfo = isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] != '' ? $_SERVER['PATH_INFO'] : ''; $this->queryString = isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '' ? $_SERVER['QUERY_STRING'] : ''; $this->fileName = basename($_SERVER['SCRIPT_NAME']); $this->currentPath = $this->currentFile; $pos = strpos($this->currentAddress, $this->currentFile); if (($pos = strpos($this->currentFile, $this->fileName)) !== false) { $this->currentPath = substr($this->currentFile, 0, $pos); // Trim everything after the filename in currentFile. This variable // must end at this. if ($pos + strlen($this->fileName) < strlen($this->currentFile)) { $this->currentFile = substr($this->currentFile, 0, $pos + strlen($this->fileName)); } } if ($pos === false || $pos > 0) { // Mod-rewrite used $this->currentParams = substr($this->currentAddress, strlen($this->currentPath)); if ($this->currentParams[0] != '/' && $this->currentParams[0] != '?') { $this->currentParams = '/' . $this->currentParams; } } else { // No mod-rewrite enter $this->currentParams = substr($this->currentAddress, strlen($this->currentFile)); } $this->basePath = substr($this->currentPath, strpos($this->currentPath, $serverName) + strlen($serverName)); break; default: throw new Opc_OptionNotExists_Exception($key, get_class($this)); break; } return $this->{$key}; }