First commit

This commit is contained in:
Theodotos Andreou 2018-01-14 13:10:16 +00:00
commit c6e2478c40
13918 changed files with 2303184 additions and 0 deletions

View file

@ -0,0 +1,876 @@
<?php
/**
* An Object Oriented interface to PHP's cURL extension
*
* PHP version 5.1.0+
*
* Copyright (c) 2007, The PEAR Group
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the The PEAR Group nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Net
* @package Net_Curl
* @author David Costa <gurugeek@php.net>
* @author Sterling Hughes <sterling@php.net>
* @author Joe Stump <joe@joestump.net>
* @author Philippe Jausions <jausions@php.net>
* @copyright 1997-2008 The PHP Group
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Revision: 1.15 $
* @link http://pear.php.net/package/Net_Curl
*/
/**
* Include PEAR package for error handling
*/
require_once 'PEAR.php';
/**
* Object-oriented implementation of the Curl extension
*
* @category Net
* @package Net_Curl
* @author David Costa <gurugeek@php.net>
* @author Sterling Hughes <sterling@php.net>
* @author Joe Stump <joe@joestump.net>
* @author Philippe Jausions <jausions@php.net>
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @link http://pear.php.net/package/Net_Curl
*/
class Net_Curl
{
// {{{ Public Properties
/**
* The URL for cURL to work with
*
* @var string $url
* @access public
*/
var $url;
/**
* The Username for standard HTTP Authentication
*
* @var string $username
* @access public
*/
var $username = '';
/**
* The Password for standard HTTP Authentication
*
* @var string $password
* @access public
*/
var $password = '';
/**
* The SSL version for the transfer
*
* @var integer $sslVersion
* @access public
*/
var $sslVersion;
/**
* The filename of the SSL certificate
*
* @var string $sslCert
* @access public
*/
var $sslCert;
/**
* The password corresponding to the certificate
* in the $sslCert property
*
* @var string $sslCertPasswd
* @access public
*/
var $sslCertPasswd;
/**
* User Agent string when making an HTTP request
*
* @var string $userAgent
* @access public
*/
var $userAgent;
/**
* Whether or not to include the header in the results
* of the CURL transfer
*
* @var boolean $header
*/
var $header = false;
/**
* Whether or not to output debug information while executing a
* curl transfer
*
* @var boolean $verbose
* @access public
*/
var $verbose = false;
/**
* Whether or not to display a progress meter for the current transfer
*
* @var boolean $progress
* @access public
*/
var $progress = false;
/**
* Whether or not to suppress error messages
*
* @var boolean $mute
* @access public
*/
var $mute = false;
/**
* Whether or not to follow HTTP Location headers.
*
* @var boolean $followLocation
* @access public
*/
var $followLocation = true;
/**
* Whether or not to follow HTTP Location headers.
*
* @var boolean $follow_location
* @access public
* @deprecated
*/
var $follow_location = false;
/**
* Time allowed for current transfer, in seconds. 0 means no limit
*
* @var int $timeout
* @access public
*/
var $timeout = 0;
/**
* Whether or not to return the results of the
* current transfer
*
* @var boolean $returnTransfer
* @access public
*/
var $returnTransfer = true;
/**
* Whether or not to return the results of the
* current transfer
*
* @var boolean $return_transfer
* @access public
* @deprecated
*/
var $return_transfer = false;
/**
* The type of transfer to perform (ie. 'POST', 'GET', 'PUT', etc)
*
* @var string $type
* @access public
*/
var $type;
/**
* The file to upload (PUT, or FTP methods)
*
* @var string $file
* @access public
*/
var $file;
/**
* The file size of the file pointed to by the $file
* property
*
* @var integer $fileSize
* @access public
*/
var $fileSize;
/**
* The file size of the file pointed to by the $file
* property
*
* @var integer $file_size
* @access public
* @deprecated
*/
var $file_size = false;
/**
* The cookies to send to the remote site
*
* @var array $cookies
* @access public
*/
var $cookies = array();
/**
* Additional HTTP headers to send to the remote site
*
* @var array $httpHeaders
* @access public
*/
var $httpHeaders = null;
/**
* Additional HTTP headers to send to the remote site
*
* @var array $http_headers
* @access public
* @deprecated
*/
var $http_headers = false;
/**
* The fields to send in a 'POST' request
*
* @var array $fields
* @access public
*/
var $fields;
/**
* The proxy server to go through
*
* @var string $proxy
* @access public
*/
var $proxy;
/**
* The username for the Proxy server
*
* @var string $proxyUser
* @access public
*/
var $proxyUser;
/**
* The password for the Proxy server
*
* @var string $proxyPassword
* @access public
*/
var $proxyPassword;
/**
* $verifyPeer
*
* FALSE to stop CURL from verifying the peer's certificate.
* Alternate certificates to verify against can be specified
* with the CURLOPT_CAINFO option or a certificate directory
* can be specified with the CURLOPT_CAPATH option.
* CURLOPT_SSL_VERIFYHOST may also need to be TRUE or FALSE
* if CURLOPT_SSL_VERIFYPEER is disabled (it defaults to 2).
*
* @var boolean $verifyPeer
* @access public
*/
var $verifyPeer = true;
/**
* $verifyHost
*
* 0 : to stop CURL from verifying the host's certificate.
* 1 : to check the existence of a common name in the SSL peer certificate.
* 2 : to check the existence of a common name and also verify that it
* matches the hostname provided.
*
* @var bool $verifyHost
* @access public
*/
var $verifyHost = 2;
/**
* $caInfo
*
* Set value for CURLOPT_CAINFO. The name of a file holding one or more
* certificates to verify the peer with. This only makes sense when used
* in combination with CURLOPT_SSL_VERIFYPEER. curl-ca-bundle.crt is
* avaible on the Curl website http://curl.haxx.se/ for download inside
* the packages.
*
* @var string $caInfo
* @access public
*/
var $caInfo = '';
/**
* $caPath
*
* Set value for CURLOPT_CAPATH. A directory that holds multiple CA
* certificates. Use this option alongside CURLOPT_SSL_VERIFYPEER.
*
* @var string $caPath
* @access public
*/
var $caPath;
// }}}
// {{{ Private Properties
/**
* The current curl handle
*
* @var resource $_ch
* @access private
* @see Net_Curl::create()
*/
var $_ch = null;
/**
* The file upload resource
*
* The CURLOPT_INFILE requires a file resource and not just a file name.
* This is used by execute to open the file.
*
* @var resource $_fp
* @access private
* @see Net_Curl::execute()
*/
var $_fp = null;
// }}}
// {{{ __construct($url = '', $userAgent = '')
/**
* The Net_Curl PHP 5.x constructor, called when a new Net_Curl object
* is initialized (also called via 4.x constructor)
*
* @param string $url The URL to fetch (can be set using the $url
* property as well)
* @param string $userAgent The userAgent string (can be set using the
* $userAgent property as well)
*
* @access public
* @author Joe Stump <joe@joestump.net>
* @return void
*/
function __construct($url = '', $userAgent = '')
{
if (is_string($url) && strlen($url)) {
$this->url = $url;
}
if (is_string($userAgent) && strlen($userAgent)) {
$this->userAgent = $userAgent;
}
}
// }}}
// {{{ Net_Curl($url = '', $userAgent = '')
/**
* Net_Curl
*
* PHP 4.x constructor.
*
* @param string $url The URL to fetch (can be set using the $url
* property as well)
* @param string $userAgent The userAgent string (can be set using the
* $userAgent property as well)
*
* @access public
* @return void
*
function Net_Curl($url = '', $userAgent = '')
{
$this->__construct($url, $userAgent);
}
// }}} */
// {{{ execute()
/**
* Executes a prepared CURL transfer
*
* Run this function to execute your cURL request. If all goes well you
* should get a string (the output from the remote host regarding your
* request) or true (if you choose to output directly to the browser). If
* something fails then PEAR_Error is returned.
*
* <code>
* <?php
* require_once 'Net/Curl.php';
*
* $curl = new Net_Curl('http://www.example.com');
* $curl->fields = array('foo' => '1', 'bar' => 'apple');
* $result = $curl->execute();
* if (!PEAR::isError($result)) {
* echo $result;
* }
* ?>
* </code>
*
* @access public
* @author Sterling Hughes <sterling@php.net>
* @author Joe Stump <joe@joestump.net>
* @return PEAR_Error on failure, true/result on success
* @since PHP 4.0.5
*/
function execute()
{
// Create cURL handle if it hasn't already been created
if (!is_resource($this->_ch)) {
$result = $this->create();
if (PEAR::isError($result)) {
return $result;
}
}
// Map the deprecated variables and throw a bunch of errors
$this->_mapDeprecatedVariables();
// Default return value is true.
$ret = true;
// Basic stuff
$ret = curl_setopt($this->_ch, CURLOPT_URL, $this->url);
$ret = curl_setopt($this->_ch, CURLOPT_HEADER, $this->header);
// Whether or not to return the transfer contents
if ($this->returnTransfer === true || $this->mute === true) {
$ret = curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, true);
}
// HTTP Authentication
if ($this->username != '') {
$ret = curl_setopt($this->_ch,
CURLOPT_USERPWD,
$this->username . ':' . $this->password);
}
// SSL Checks
if (isset($this->sslVersion)) {
$ret = curl_setopt($this->_ch,
CURLOPT_SSLVERSION,
$this->sslVersion);
}
if (isset($this->sslCert)) {
$ret = curl_setopt($this->_ch, CURLOPT_SSLCERT, $this->sslCert);
}
if (isset($this->sslCertPasswd)) {
$ret = curl_setopt($this->_ch,
CURLOPT_SSLCERTPASSWD,
$this->sslCertPasswd);
}
// Proxy Related checks
if (isset($this->proxy)) {
$ret = curl_setopt($this->_ch, CURLOPT_PROXY, $this->proxy);
}
if (isset($this->proxyUser) || isset($this->proxyPassword)) {
$ret = curl_setopt($this->_ch,
CURLOPT_PROXYUSERPWD,
$this->proxyUser . ':' . $this->proxyPassword);
}
if (is_bool($this->verifyPeer)) {
if (!$this->setOption(CURLOPT_SSL_VERIFYPEER, $this->verifyPeer)) {
return PEAR::raiseError('Error setting CURLOPT_SSL_VERIFYPEER');
}
}
if (is_numeric($this->verifyHost) && $this->verifyHost >= 0 &&
$this->verifyHost <= 2) {
if (!$this->setOption(CURLOPT_SSL_VERIFYHOST, $this->verifyHost)) {
return PEAR::raiseError('Error setting CURLOPT_SSL_VERIFYPEER');
}
}
if (is_bool($this->verifyPeer) && $this->verifyPeer == true) {
if (isset($this->caInfo) && strlen($this->caInfo)) {
if (file_exists($this->caInfo)) {
if (!$this->setOption(CURLOPT_CAINFO, $this->caInfo)) {
return PEAR::raiseError('Error setting CURLOPT_CAINFO');
}
} else {
return PEAR::raiseError('Could not find CA info: '.
$this->caInfo);
}
}
if (isset($this->caPath) && is_string($this->caPath)) {
if (!$this->setOption(CURLOPT_CAPATH, $this->caPath)) {
return PEAR::raiseError('Error setting CURLOPT_CAPATH');
}
}
}
// Transfer type
if (isset($this->type)) {
switch (strtolower($this->type)) {
case 'post':
$ret = curl_setopt($this->_ch, CURLOPT_POST, true);
break;
case 'put':
$ret = curl_setopt($this->_ch, CURLOPT_PUT, true);
break;
}
}
// Transfer upload, etc. related
if (isset($this->file)) {
if (!file_exists($this->file)) {
return PEAR::raiseError('File does not exist: '.$this->file);
}
$this->_fp = fopen($this->file, 'r');
if (!is_resource($this->_fp)) {
return PEAR::raiseError('Could not open file: '.$this->file);
}
if (!isset($this->fileSize)) {
$this->fileSize = filesize($this->file);
}
$ret = curl_setopt($this->_ch, CURLOPT_INFILE, $this->_fp);
$ret = curl_setopt($this->_ch, CURLOPT_INFILESIZE, $this->fileSize);
$ret = curl_setopt($this->_ch, CURLOPT_UPLOAD, true);
}
if (isset($this->fields)) {
$sets = null;
if (!isset($this->type)) {
$this->type = 'post';
$ret = curl_setopt($this->_ch, CURLOPT_POST, true);
}
// If fields is an array then turn it into a string. Sometimes
// cURL doesn't like fields as an array.
// Exception: if a value is prefixed with "@" and the rest of the
// value resolves to an existing file, then pass
// the values as the original array.
if (is_array($this->fields)) {
$sets = array();
foreach ($this->fields as $key => $val) {
if (strlen($val) > 1 && $val{0} == '@') {
$file = substr($val, 1);
if (is_file($file) && is_readable($file)) {
$sets = null;
break;
}
}
$sets[] = urlencode($key) . '=' . urlencode($val);
}
}
if (!is_null($sets)) {
$fields = implode('&', $sets);
} else {
$fields = $this->fields;
}
$ret = curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $fields);
}
// Error related
if ($this->progress === true) {
$ret = curl_setopt($this->_ch, CURLOPT_PROGRESS, true);
}
if ($this->verbose === true) {
$ret = curl_setopt($this->_ch, CURLOPT_VERBOSE, true);
}
// If a Location: header is passed then follow it
$ret = curl_setopt($this->_ch,
CURLOPT_FOLLOWLOCATION,
$this->followLocation);
// If a timeout is set and is greater then zero then set it
if (is_numeric($this->timeout) && $this->timeout > 0) {
$ret = curl_setopt($this->_ch, CURLOPT_TIMEOUT, $this->timeout);
}
if (isset($this->userAgent)) {
$ret = curl_setopt($this->_ch, CURLOPT_USERAGENT, $this->userAgent);
}
// Cookies
if (is_array($this->cookies) && count($this->cookies)) {
$cookieData = '';
foreach ($this->cookies as $name => $value) {
$cookieData .= $name . '=' . $value . ';';
}
$ret = curl_setopt($this->_ch, CURLOPT_COOKIE, $cookieData);
}
// Other HTTP headers
if ($this->httpHeaders !== null) {
if (is_array($this->httpHeaders)) {
$ret = curl_setopt($this->_ch,
CURLOPT_HTTPHEADER,
$this->httpHeaders);
} else {
return PEAR::raiseError('Net_Curl::$httpHeaders must be an array');
}
}
$ret = curl_exec($this->_ch);
// Close the file before we return anything
if (is_resource($this->_fp)) {
fclose($this->_fp);
}
if (curl_errno($this->_ch)) {
return PEAR::raiseError(curl_error($this->_ch), curl_errno($this->_ch));
}
// Check to make sure we get a 2XX/3XX code and not a 404 or something.
$info = $this->getInfo();
if (!isset($info['http_code'])) {
return PEAR::raiseError('Unknown or invalid HTTP response');
} else {
$type = substr($info['http_code'], 0, 1);
if ($type != 2 && $type != 3) {
return PEAR::raiseError('Unexpected HTTP code: ' .
$info['http_code']);
}
}
return $ret;
}
// }}}
// {{{ setOption($option, $value)
/**
* Sets an option for your cURL session. Please note that the cURL handler
* is NOT created before execute(). This is for error checking purposes.
* You should use setOption() in the following manner:
*
* <code>
* <?php
*
* require_once 'Net/Curl.php';
* $curl = new Net_Curl('http://www.example.com');
* $check = $curl->create();
* if (!PEAR::isError($check)) {
* $curl->setOption(CURLOPT_FOO, 'bar');
* $result = $curl->execute();
* if (!PEAR::isError($result)) {
* echo $result;
* }
* }
*
* ?>
* </code>
*
* @param int $option cURL constant (ie. CURLOPT_URL)
* @param mixed $value The option's value
*
* @author Joe Stump <joe@joestump.net>
* @access public
* @return boolean
*/
function setOption($option, $value)
{
if (is_resource($this->_ch)) {
return curl_setopt($this->_ch, $option, $value);
}
return false;
}
// }}}
// {{{ getInfo()
/**
* Returns the info from the cURL session. PEAR_Error if you try and run
* this before you execute the session.
*
* @author Joe Stump <joe@joestump.net>
* @access public
* @return mixed PEAR_Error if there is no resource, info on success
*/
function getInfo()
{
if (is_resource($this->_ch)) {
return curl_getinfo($this->_ch);
}
return PEAR::isError('cURL handler does not exist!');
}
// }}}
// {{{ create()
/**
* Creates a cURL resource. If curl_init() doesn't exist or we could not
* create a resource it will error out.
*
* @author Joe Stump <joe@joestump.net>
* @return boolean TRUE on success, PEAR_Error on failure
*/
function create()
{
if (!PEAR::loadExtension('curl')) {
return PEAR::raiseError('CURL extension is not available');
}
if (!function_exists('curl_init')) {
return PEAR::raiseError('Function curl_init() not found');
}
$this->_ch = curl_init();
if (!is_resource($this->_ch)) {
return PEAR::raiseError('Could not initialize cURL handler');
}
return true;
}
// }}}
// {{{ verboseAll()
/**
* Sets verbose output
*
* Turns on super debugging mode by not suppressing errors, turning on
* verbose mode, showing headers and displaying progress.
*
* @access public
* @author David Costa <gurugeek@php.net>
* @return void
*/
function verboseAll()
{
$this->verbose = true;
$this->mute = false;
$this->header = true;
$this->progress = true;
}
// }}}
// {{{ verbose_all()
/**
* Sets verbose output
*
* @access public
* @author David Costa <gurugeek@php.net>
* @return void
* @deprecated
*/
function verbose_all()
{
$this->verboseAll();
PEAR::raiseError('Net_Curl::verbose_all() is deprecated! Please use Net_Curl::verboseAll()'." <br />\n", null, PEAR_ERROR_PRINT);
}
// }}}
// {{{ close()
/**
* Closes the curl transfer and finishes the object (kinda ;)
*
* @access public
* @author Sterling Hughes <sterling@php.net>
* @return void
* @since PHP 4.0.5
*/
function close()
{
if (is_resource($this->_ch)) {
curl_close($this->_ch);
}
}
// }}}
// {{{ _mapDeprecatedVariables()
/**
* Maps deprecated variables into the appropriate places. It also throws
* the necessary notices.
*
* @author Joe Stump <joe@joestump.net>
* @access private
* @return void
*/
function _mapDeprecatedVariables()
{
$bad = array();
if ($this->follow_location !== false) {
if ($this->follow_location > 0) {
$this->followLocation = true;
} else {
$this->followLocation = false;
}
$bad[] = array('follow_location', 'followLocation');
}
if ($this->return_transfer !== false) {
if ($this->return_transfer > 0) {
$this->returnTransfer = true;
} else {
$this->returnTransfer = false;
}
$bad[] = array('return_transfer', 'returnTransfer');
}
if ($this->file_size !== false) {
$this->fileSize = $this->file_size;
$bad[] = array('file_size', 'fileSize');
}
if ($this->http_headers !== false) {
$this->httpHeaders = $this->http_headers;
$bad[] = array('http_headers', 'httpHeaders');
}
foreach ($bad as $map) {
PEAR::raiseError('Net_Curl::$'. $map[0]. ' is deprecated! Please use Net_Curl::$'.$map[1]." instead! <br />\n", null, PEAR_ERROR_PRINT);
}
}
// }}}
// {{{ __destruct()
/**
* PHP 5.x destructor.
*
* Runs Net_Curl::close() to make sure we close our cURL connection.
*
* @author Joe Stump <joe@joestump.net>
* @see Net_Curl::close()
*/
function __destruct()
{
$this->close();
}
// }}}
}
?>

View file

@ -0,0 +1,693 @@
<?php
/**
* This file holds the Net_DIME_Message and Net_DIME_Record classes and all
* constants defined for the Net_DIME package.
*
* PHP versions 4 and 5
*
* Copyright (c) 2002-2007 The PHP Group
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DIME
* @author Shane Caraveo <shane@caraveo.com>
* @author Ralf Hofmann <ralf.hofmann@verdisoft.com>
* @author Jan Schneider <jan@horde.org>
* @copyright 2002-2007 The PHP Group
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DIME
* @example test/dime_record_test.php For example of usage.
*/
/** PEAR */
require_once 'PEAR.php';
define('NET_DIME_TYPE_UNCHANGED', 0x00);
define('NET_DIME_TYPE_MEDIA', 0x01);
define('NET_DIME_TYPE_URI', 0x02);
define('NET_DIME_TYPE_UNKNOWN', 0x03);
define('NET_DIME_TYPE_NONE', 0x04);
define('NET_DIME_VERSION', 0x0001);
define('NET_DIME_RECORD_HEADER', 12);
define('NET_DIME_FLAGS', 0);
define('NET_DIME_OPTS_LEN', 1);
define('NET_DIME_ID_LEN', 2);
define('NET_DIME_TYPE_LEN', 3);
define('NET_DIME_DATA_LEN', 4);
define('NET_DIME_OPTS', 5);
define('NET_DIME_ID', 6);
define('NET_DIME_TYPE', 7);
define('NET_DIME_DATA', 8);
/**
* Net_DIME_Record encodes and decodes single DIME records.
*
* @category Networking
* @package Net_DIME
* @author Shane Caraveo <shane@caraveo.com>
* @author Ralf Hofmann <ralf.hofmann@verdisoft.com>
* @author Jan Schneider <jan@horde.org>
* @copyright 2002-2007 The PHP Group
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @see Net_DIME_Message
* @link http://pear.php.net/package/Net_DIME
* @link http://www.ietf.org/internet-drafts/draft-nielsen-dime-02.txt
*/
class Net_DIME_Record {
// these are used to hold the padded length
var $OPTS_LENGTH = 0;
var $ID_LENGTH = 0;
var $TYPE_LENGTH = 0;
var $DATA_LENGTH = 0;
var $_haveOpts = false;
var $_haveID = false;
var $_haveType = false;
var $_haveData = false;
var $debug = false;
var $padstr = "\0";
/**
* Elements
* [NET_DIME_FLAGS], 16 bits: VERSION:MB:ME:CF:TYPE_T
* [NET_DIME_OPTS_LEN], 16 bits: OPTIONS_LENGTH
* [NET_DIME_ID_LEN], 16 bits: ID_LENGTH
* [NET_DIME_TYPE_LEN], 16 bits: TYPE_LENGTH
* [NET_DIME_DATA_LEN], 32 bits: DATA_LENGTH
* [NET_DIME_OPTS] : OPTIONS
* [NET_DIME_ID] : ID
* [NET_DIME_TYPE] : TYPE
* [NET_DIME_DATA] : DATA
*/
var $Elements = array(NET_DIME_FLAGS => 0, NET_DIME_OPTS_LEN => 0,
NET_DIME_ID_LEN => 0, NET_DIME_TYPE_LEN => 0,
NET_DIME_DATA_LEN => 0,
NET_DIME_OPTS => '',
NET_DIME_ID => '',
NET_DIME_TYPE => '',
NET_DIME_DATA => '');
function __construct($debug = false)
{
$this->debug = $debug;
if ($debug) {
$this->padstr = '*';
}
}
function setMB()
{
$this->Elements[NET_DIME_FLAGS] |= 0x0400;
}
function setME()
{
$this->Elements[NET_DIME_FLAGS] |= 0x0200;
}
function setCF()
{
$this->Elements[NET_DIME_FLAGS] |= 0x0100;
}
function isChunk()
{
return $this->Elements[NET_DIME_FLAGS] & 0x0100;
}
function isEnd()
{
return $this->Elements[NET_DIME_FLAGS] & 0x0200;
}
function isStart()
{
return $this->Elements[NET_DIME_FLAGS] & 0x0400;
}
function getID()
{
return $this->Elements[NET_DIME_ID];
}
function getType()
{
return $this->Elements[NET_DIME_TYPE];
}
function getData()
{
return $this->Elements[NET_DIME_DATA];
}
function getDataLength()
{
return $this->Elements[NET_DIME_DATA_LEN];
}
function setType($typestring, $type = NET_DIME_TYPE_UNKNOWN)
{
$typelen = strlen($typestring) & 0xFFFF;
$type = $type << 4;
$this->Elements[NET_DIME_FLAGS] = ($this->Elements[NET_DIME_FLAGS] & 0xFF0F) | $type;
$this->Elements[NET_DIME_TYPE_LEN] = $typelen;
$this->TYPE_LENGTH = $this->_getPadLength($typelen);
$this->Elements[NET_DIME_TYPE] = $typestring;
}
function generateID()
{
$id = md5(time());
$this->setID($id);
return $id;
}
function setID($id)
{
$idlen = strlen($id) & 0xFFFF;
$this->Elements[NET_DIME_ID_LEN] = $idlen;
$this->ID_LENGTH = $this->_getPadLength($idlen);
$this->Elements[NET_DIME_ID] = $id;
}
function setData($data, $size = 0)
{
$datalen = $size ? $size : strlen($data);
$this->Elements[NET_DIME_DATA_LEN] = $datalen;
$this->DATA_LENGTH = $this->_getPadLength($datalen);
$this->Elements[NET_DIME_DATA] = $data;
}
function encode()
{
// Insert version.
$this->Elements[NET_DIME_FLAGS] = ($this->Elements[NET_DIME_FLAGS] & 0x07FF) | (NET_DIME_VERSION << 11);
// The real DIME encoding.
$format = '%c%c%c%c%c%c%c%c%c%c%c%c'
. '%' . $this->OPTS_LENGTH . 's'
. '%' . $this->ID_LENGTH . 's'
. '%' . $this->TYPE_LENGTH . 's'
. '%' . $this->DATA_LENGTH . 's';
return sprintf($format,
($this->Elements[NET_DIME_FLAGS] & 0x0000FF00) >> 8,
($this->Elements[NET_DIME_FLAGS] & 0x000000FF),
($this->Elements[NET_DIME_OPTS_LEN] & 0x0000FF00) >> 8,
($this->Elements[NET_DIME_OPTS_LEN] & 0x000000FF),
($this->Elements[NET_DIME_ID_LEN] & 0x0000FF00) >> 8,
($this->Elements[NET_DIME_ID_LEN] & 0x000000FF),
($this->Elements[NET_DIME_TYPE_LEN] & 0x0000FF00) >> 8,
($this->Elements[NET_DIME_TYPE_LEN] & 0x000000FF),
($this->Elements[NET_DIME_DATA_LEN] & 0xFF000000) >> 24,
($this->Elements[NET_DIME_DATA_LEN] & 0x00FF0000) >> 16,
($this->Elements[NET_DIME_DATA_LEN] & 0x0000FF00) >> 8,
($this->Elements[NET_DIME_DATA_LEN] & 0x000000FF),
str_pad($this->Elements[NET_DIME_OPTS], $this->OPTS_LENGTH, $this->padstr),
str_pad($this->Elements[NET_DIME_ID], $this->ID_LENGTH, $this->padstr),
str_pad($this->Elements[NET_DIME_TYPE], $this->TYPE_LENGTH, $this->padstr),
str_pad($this->Elements[NET_DIME_DATA], $this->DATA_LENGTH, $this->padstr));
}
function _getPadLength($len)
{
$pad = 0;
if ($len) {
$pad = $len % 4;
if ($pad) $pad = 4 - $pad;
}
return $len + $pad;
}
function decode($data)
{
// Real DIME decoding.
$this->Elements[NET_DIME_FLAGS] = (hexdec(bin2hex($data[0])) << 8)
+ hexdec(bin2hex($data[1]));
$this->Elements[NET_DIME_OPTS_LEN] = (hexdec(bin2hex($data[2])) << 8)
+ hexdec(bin2hex($data[3]));
$this->Elements[NET_DIME_ID_LEN] = (hexdec(bin2hex($data[4])) << 8)
+ hexdec(bin2hex($data[5]));
$this->Elements[NET_DIME_TYPE_LEN] = (hexdec(bin2hex($data[6])) << 8)
+ hexdec(bin2hex($data[7]));
$this->Elements[NET_DIME_DATA_LEN] = (hexdec(bin2hex($data[8])) << 24)
+ (hexdec(bin2hex($data[9])) << 16)
+ (hexdec(bin2hex($data[10])) << 8)
+ hexdec(bin2hex($data[11]));
$p = 12;
$version = (($this->Elements[NET_DIME_FLAGS] >> 11) & 0x001F);
if ($version == NET_DIME_VERSION) {
$this->OPTS_LENGTH = $this->_getPadLength($this->Elements[NET_DIME_OPTS_LEN]);
$this->ID_LENGTH = $this->_getPadLength($this->Elements[NET_DIME_ID_LEN]);
$this->TYPE_LENGTH = $this->_getPadLength($this->Elements[NET_DIME_TYPE_LEN]);
$this->DATA_LENGTH = $this->_getPadLength($this->Elements[NET_DIME_DATA_LEN]);
$datalen = strlen($data);
$this->Elements[NET_DIME_OPTS] = substr($data, $p, $this->Elements[NET_DIME_OPTS_LEN]);
$this->_haveOpts = (strlen($this->Elements[NET_DIME_OPTS]) == $this->Elements[NET_DIME_OPTS_LEN]);
if ($this->_haveOpts) {
$p += $this->OPTS_LENGTH;
$this->Elements[NET_DIME_ID] = substr($data, $p, $this->Elements[NET_DIME_ID_LEN]);
$this->_haveID = (strlen($this->Elements[NET_DIME_ID]) == $this->Elements[NET_DIME_ID_LEN]);
if ($this->_haveID) {
$p += $this->ID_LENGTH;
$this->Elements[NET_DIME_TYPE] = substr($data, $p, $this->Elements[NET_DIME_TYPE_LEN]);
$this->_haveType = (strlen($this->Elements[NET_DIME_TYPE]) == $this->Elements[NET_DIME_TYPE_LEN]);
if ($this->_haveType) {
$p += $this->TYPE_LENGTH;
$this->Elements[NET_DIME_DATA] = substr($data, $p, $this->Elements[NET_DIME_DATA_LEN]);
$this->_haveData = (strlen($this->Elements[NET_DIME_DATA]) == $this->Elements[NET_DIME_DATA_LEN]);
if ($this->_haveData) {
$p += $this->DATA_LENGTH;
} else {
$p += strlen($this->Elements[NET_DIME_DATA]);
}
} else {
$p += strlen($this->Elements[NET_DIME_TYPE]);
}
} else {
$p += strlen($this->Elements[NET_DIME_ID]);
}
} else {
$p += strlen($this->Elements[NET_DIME_OPTS]);
}
}
return substr($data, $p);
}
function addData($data)
{
$datalen = strlen($data);
$p = 0;
if (!$this->_haveOpts) {
$have = strlen($this->Elements[NET_DIME_OPTS]);
$this->Elements[NET_DIME_OPTS] .= substr($data, $p, $this->Elements[NET_DIME_OPTS_LEN] - $have);
$this->_haveOpts = strlen($this->Elements[NET_DIME_OPTS]) == $this->Elements[NET_DIME_OPTS_LEN];
if (!$this->_haveOpts) {
return null;
}
$p += $this->OPTS_LENGTH - $have;
}
if (!$this->_haveID) {
$have = strlen($this->Elements[NET_DIME_ID]);
$this->Elements[NET_DIME_ID] .= substr($data, $p, $this->Elements[NET_DIME_ID_LEN] - $have);
$this->_haveID = strlen($this->Elements[NET_DIME_ID]) == $this->Elements[NET_DIME_ID_LEN];
if (!$this->_haveID) {
return null;
}
$p += $this->ID_LENGTH - $have;
}
if (!$this->_haveType && $p < $datalen) {
$have = strlen($this->Elements[NET_DIME_TYPE]);
$this->Elements[NET_DIME_TYPE] .= substr($data, $p, $this->Elements[NET_DIME_TYPE_LEN] - $have);
$this->_haveType = strlen($this->Elements[NET_DIME_TYPE]) == $this->Elements[NET_DIME_TYPE_LEN];
if (!$this->_haveType) {
return null;
}
$p += $this->TYPE_LENGTH - $have;
}
if (!$this->_haveData && $p < $datalen) {
$have = strlen($this->Elements[NET_DIME_DATA]);
$this->Elements[NET_DIME_DATA] .= substr($data, $p, $this->Elements[NET_DIME_DATA_LEN] - $have);
$this->_haveData = strlen($this->Elements[NET_DIME_DATA]) == $this->Elements[NET_DIME_DATA_LEN];
if (!$this->_haveData) {
return null;
}
$p += $this->DATA_LENGTH - $have;
}
return substr($data, $p);
}
}
/**
* Net_DIME_Message enables you to manipulate and build a DIME encapsulated
* message.
*
* @category Networking
* @package Net_DIME
* @author Shane Caraveo <shane@caraveo.com>
* @author Ralf Hofmann <ralf.hofmann@verdisoft.com>
* @author Jan Schneider <jan@horde.org>
* @copyright 2002-2007 The PHP Group
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @see Net_DIME_Message
* @link http://pear.php.net/package/Net_DIME
* @link http://www.ietf.org/internet-drafts/draft-nielsen-dime-02.txt
* @example test/dime_message_test.php For example of usage.
* @todo - Lots of stuff needs to be tested.
* - Definitely have to go through DIME spec and make things work
* right, most importantly, sec 3.3.
* - Make examples, document.
*/
class Net_DIME_Message {
var $record_size = 4096;
var $parts = array();
var $currentPart = -1;
var $stream = null;
var $_currentRecord;
var $_proc = array();
var $type;
var $typestr;
var $mb = 1;
var $me = 0;
var $cf = 0;
var $id = null;
var $debug = false;
/**
* Constructor.
*
* @todo Integrate with PHP streams.
*
* @param resource $stream A file pointer.
* @param integer $record_size
* @param boolean $debug
*/
function __construct($stream = null, $record_size = 4096,
$debug = false)
{
$this->stream = $stream;
$this->record_size = $record_size;
$this->debug = $debug;
}
function _makeRecord($data, $typestr = '', $id = null,
$type = NET_DIME_TYPE_UNKNOWN)
{
$record = new Net_DIME_Record($this->debug);
if ($this->mb) {
$record->setMB();
// all subsequent records are not message begin!
$this->mb = 0;
}
if ($this->me) {
$record->setME();
}
if ($this->cf) {
$record->setCF();
}
$record->setData($data);
$record->setType($typestr,$type);
if ($id) {
$record->setID($id);
}
return $record->encode();
}
function startChunk($data, $typestr = '', $id = null,
$type = NET_DIME_TYPE_UNKNOWN)
{
$this->me = 0;
$this->cf = 1;
$this->type = $type;
$this->typestr = $typestr;
if ($id) {
$this->id = $id;
} else {
$this->id = md5(time());
}
return $this->_makeRecord($data, $this->typestr, $this->id, $this->type);
}
function doChunk($data)
{
$this->me = 0;
$this->cf = 1;
return $this->_makeRecord($data, null, null, NET_DIME_TYPE_UNCHANGED);
}
function endChunk()
{
$this->cf = 0;
$data = null;
$rec = $this->_makeRecord($data, null, null, NET_DIME_TYPE_UNCHANGED);
$this->id = 0;
$this->cf = 0;
$this->id = 0;
$this->type = NET_DIME_TYPE_UNKNOWN;
$this->typestr = null;
return $rec;
}
function endMessage()
{
$this->me = 1;
$data = null;
$rec = $this->_makeRecord($data, null, null, NET_DIME_TYPE_NONE);
$this->me = 0;
$this->mb = 1;
$this->id = 0;
return $rec;
}
/**
* Creates DIME records from a chunk of data and writes them to the stream
* provided in the constructor.
*
* @param string $data
* @param string $typestr
* @param string $id
* @param integer $type One of the NET_DIME_TYPE_* constants.
*/
function sendData($data, $typestr='', $id = null,
$type = NET_DIME_TYPE_UNKNOWN)
{
$len = strlen($data);
if ($len > $this->record_size) {
$chunk = substr($data, 0, $this->record_size);
$p = $this->record_size;
$rec = $this->startChunk($chunk, $typestr, $id, $type);
fwrite($this->stream, $rec);
while ($p < $len) {
$chunk = substr($data, $p, $this->record_size);
$p += $this->record_size;
$rec = $this->doChunk($chunk);
fwrite($this->stream, $rec);
}
$rec = $this->endChunk();
fwrite($this->stream, $rec);
return;
}
$rec = $this->_makeRecord($data, $typestr, $id, $type);
fwrite($this->stream, $rec);
}
function sendEndMessage()
{
$rec = $this->endMessage();
fwrite($this->stream, $rec);
}
/**
* Reads a file, creates records and writes them to the stream provided in
* the constructor.
*
* @param string $filename A file name.
* @param string $typestr
* @param string $id
* @param integer $type One of the NET_DIME_TYPE_* constants.
*/
function sendFile($filename, $typestr='', $id = null,
$type = NET_DIME_TYPE_UNKNOWN)
{
$f = fopen($filename, 'rb');
if ($f) {
if ($data = fread($f, $this->record_size)) {
$this->startChunk($data, $typestr, $id, $type);
}
while ($data = fread($f, $this->record_size)) {
$this->doChunk($data, $typestr, $id, $type);
}
$this->endChunk();
fclose($f);
}
}
/**
* Encodes data in DIME.
*
* @param string $data
* @param string $typestr
* @param string $id
* @param integer $type One of the NET_DIME_TYPE_* constants.
*/
function encodeData($data, $typestr = '', $id = null,
$type = NET_DIME_TYPE_UNKNOWN)
{
$len = strlen($data);
$resp = '';
if ($len > $this->record_size) {
$chunk = substr($data, 0, $this->record_size);
$p = $this->record_size;
$resp .= $this->startChunk($chunk, $typestr, $id, $type);
while ($p < $len) {
$chunk = substr($data, $p, $this->record_size);
$p += $this->record_size;
$resp .= $this->doChunk($chunk);
}
$resp .= $this->endChunk();
} else {
$resp .= $this->_makeRecord($data, $typestr, $id, $type);
}
return $resp;
}
/**
* Reads a file, creates records and writes them to the stream provided in
* the constructor.
*
*/
function encodeFile($filename, $typestr = '', $id = null,
$type = NET_DIME_TYPE_UNKNOWN)
{
$f = fopen($filename, 'rb');
if ($f) {
if ($data = fread($f, $this->record_size)) {
$resp = $this->startChunk($data, $typestr, $id, $type);
}
while ($data = fread($f, $this->record_size)) {
$resp = $this->doChunk($data, $typestr, $id, $type);
}
$resp = $this->endChunk();
fclose($f);
}
return $resp;
}
/**
* Creates Net_DIME_Records from provided data.
*/
function _processData(&$data)
{
$leftover = null;
if (!$this->_currentRecord) {
$this->_currentRecord = new Net_DIME_Record($this->debug);
$data = $this->_currentRecord->decode($data);
} else {
$data = $this->_currentRecord->addData($data);
}
if ($this->_currentRecord->_haveData) {
if (count($this->parts) == 0 &&
!$this->_currentRecord->isStart()) {
return PEAR::raiseError('First Message is not a DIME begin record!');
}
if ($this->_currentRecord->isEnd() &&
$this->_currentRecord->getDataLength() == 0) {
return;
}
if ($this->currentPart < 0 && !$this->_currentRecord->isChunk()) {
$this->parts[] = array();
$this->currentPart = count($this->parts) - 1;
$this->parts[$this->currentPart]['id'] = $this->_currentRecord->getID();
$this->parts[$this->currentPart]['type'] = $this->_currentRecord->getType();
$this->parts[$this->currentPart]['data'] = $this->_currentRecord->getData();
$this->currentPart = -1;
} else {
if ($this->currentPart < 0) {
$this->parts[] = array();
$this->currentPart = count($this->parts) - 1;
$this->parts[$this->currentPart]['id'] = $this->_currentRecord->getID();
$this->parts[$this->currentPart]['type'] = $this->_currentRecord->getType();
$this->parts[$this->currentPart]['data'] = $this->_currentRecord->getData();
} else {
$this->parts[$this->currentPart]['data'] .= $this->_currentRecord->getData();
if (!$this->_currentRecord->isChunk()) {
// We reached the end of the chunk.
$this->currentPart = -1;
}
}
}
if (!$this->_currentRecord->isEnd()) {
$this->_currentRecord = null;
}
}
return;
}
/**
* Decodes a DIME encode string of data.
*/
function decodeData(&$data)
{
while (strlen($data) >= NET_DIME_RECORD_HEADER) {
$err = $this->_processData($data);
if (PEAR::isError($err)) {
return $err;
}
}
}
/**
* Reads the stream and creates an array of records.
*
* The method can accept the start of a previously read buffer. This is
* useful in situations where you need to read headers before discovering
* that the data is DIME encoded, such as in the case of reading an HTTP
* response.
*/
function read($buf = null)
{
while ($data = fread($this->stream, 8192)) {
if ($buf) {
$data = $buf . $data;
$buf = null;
}
if ($this->debug) {
echo 'read: ' . strlen($data) . " bytes\n";
}
$err = $this->decodeData($data);
if (PEAR::isError($err)) {
return $err;
}
// Store any leftover data to be used again.
// Should be < NET_DIME_RECORD_HEADER bytes.
$buf = $data;
}
if (!$this->_currentRecord || !$this->_currentRecord->isEnd()) {
return PEAR::raiseError('reached stream end without end record');
}
}
}

View file

@ -0,0 +1,474 @@
<?php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2002-2004, Richard Heyes |
// | All rights reserved. |
// | |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | are met: |
// | |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | permission. |
// | |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// | |
// +-----------------------------------------------------------------------+
// | Author: Richard Heyes <richard at php net> |
// +-----------------------------------------------------------------------+
//
// $Id: URL.php,v 1.49 2007/06/28 14:43:07 davidc Exp $
//
// Net_URL Class
class Net_URL
{
var $options = array('encode_query_keys' => false);
/**
* Full url
* @var string
*/
var $url;
/**
* Protocol
* @var string
*/
var $protocol;
/**
* Username
* @var string
*/
var $username;
/**
* Password
* @var string
*/
var $password;
/**
* Host
* @var string
*/
var $host;
/**
* Port
* @var integer
*/
var $port;
/**
* Path
* @var string
*/
var $path;
/**
* Query string
* @var array
*/
var $querystring;
/**
* Anchor
* @var string
*/
var $anchor;
/**
* Whether to use []
* @var bool
*/
var $useBrackets;
/**
* PHP5 Constructor
*
* Parses the given url and stores the various parts
* Defaults are used in certain cases
*
* @param string $url Optional URL
* @param bool $useBrackets Whether to use square brackets when
* multiple querystrings with the same name
* exist
*/
function __construct($url = null, $useBrackets = true)
{
$this->url = $url;
$this->useBrackets = $useBrackets;
$this->initialize();
}
function initialize()
{
$HTTP_SERVER_VARS = !empty($_SERVER) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
$this->user = '';
$this->pass = '';
$this->host = '';
$this->port = 80;
$this->path = '';
$this->querystring = array();
$this->anchor = '';
// Only use defaults if not an absolute URL given
if (!preg_match('/^[a-z0-9]+:\/\//i', $this->url)) {
$this->protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http');
/**
* Figure out host/port
*/
if (!empty($HTTP_SERVER_VARS['HTTP_HOST']) &&
preg_match('/^(.*)(:([0-9]+))?$/U', $HTTP_SERVER_VARS['HTTP_HOST'], $matches))
{
$host = $matches[1];
if (!empty($matches[3])) {
$port = $matches[3];
} else {
$port = $this->getStandardPort($this->protocol);
}
}
$this->user = '';
$this->pass = '';
$this->host = !empty($host) ? $host : (isset($HTTP_SERVER_VARS['SERVER_NAME']) ? $HTTP_SERVER_VARS['SERVER_NAME'] : 'localhost');
$this->port = !empty($port) ? $port : (isset($HTTP_SERVER_VARS['SERVER_PORT']) ? $HTTP_SERVER_VARS['SERVER_PORT'] : $this->getStandardPort($this->protocol));
$this->path = !empty($HTTP_SERVER_VARS['PHP_SELF']) ? $HTTP_SERVER_VARS['PHP_SELF'] : '/';
$this->querystring = isset($HTTP_SERVER_VARS['QUERY_STRING']) ? $this->_parseRawQuerystring($HTTP_SERVER_VARS['QUERY_STRING']) : null;
$this->anchor = '';
}
// Parse the url and store the various parts
if (!empty($this->url)) {
$urlinfo = parse_url($this->url);
// Default querystring
$this->querystring = array();
foreach ($urlinfo as $key => $value) {
switch ($key) {
case 'scheme':
$this->protocol = $value;
$this->port = $this->getStandardPort($value);
break;
case 'user':
case 'pass':
case 'host':
case 'port':
$this->$key = $value;
break;
case 'path':
if ($value{0} == '/') {
$this->path = $value;
} else {
$path = dirname($this->path) == DIRECTORY_SEPARATOR ? '' : dirname($this->path);
$this->path = sprintf('%s/%s', $path, $value);
}
break;
case 'query':
$this->querystring = $this->_parseRawQueryString($value);
break;
case 'fragment':
$this->anchor = $value;
break;
}
}
}
}
/**
* Returns full url
*
* @return string Full url
* @access public
*/
function getURL()
{
$querystring = $this->getQueryString();
$this->url = $this->protocol . '://'
. $this->user . (!empty($this->pass) ? ':' : '')
. $this->pass . (!empty($this->user) ? '@' : '')
. $this->host . ($this->port == $this->getStandardPort($this->protocol) ? '' : ':' . $this->port)
. $this->path
. (!empty($querystring) ? '?' . $querystring : '')
. (!empty($this->anchor) ? '#' . $this->anchor : '');
return $this->url;
}
/**
* Adds or updates a querystring item (URL parameter).
* Automatically encodes parameters with rawurlencode() if $preencoded
* is false.
* You can pass an array to $value, it gets mapped via [] in the URL if
* $this->useBrackets is activated.
*
* @param string $name Name of item
* @param string $value Value of item
* @param bool $preencoded Whether value is urlencoded or not, default = not
* @access public
*/
function addQueryString($name, $value, $preencoded = false)
{
if ($this->getOption('encode_query_keys')) {
$name = rawurlencode($name);
}
if ($preencoded) {
$this->querystring[$name] = $value;
} else {
$this->querystring[$name] = is_array($value) ? array_map('rawurlencode', $value): rawurlencode($value);
}
}
/**
* Removes a querystring item
*
* @param string $name Name of item
* @access public
*/
function removeQueryString($name)
{
if ($this->getOption('encode_query_keys')) {
$name = rawurlencode($name);
}
if (isset($this->querystring[$name])) {
unset($this->querystring[$name]);
}
}
/**
* Sets the querystring to literally what you supply
*
* @param string $querystring The querystring data. Should be of the format foo=bar&x=y etc
* @access public
*/
function addRawQueryString($querystring)
{
$this->querystring = $this->_parseRawQueryString($querystring);
}
/**
* Returns flat querystring
*
* @return string Querystring
* @access public
*/
function getQueryString()
{
if (!empty($this->querystring)) {
foreach ($this->querystring as $name => $value) {
// Encode var name
$name = rawurlencode($name);
if (is_array($value)) {
foreach ($value as $k => $v) {
$querystring[] = $this->useBrackets ? sprintf('%s[%s]=%s', $name, $k, $v) : ($name . '=' . $v);
}
} elseif (!is_null($value)) {
$querystring[] = $name . '=' . $value;
} else {
$querystring[] = $name;
}
}
$querystring = implode(ini_get('arg_separator.output'), $querystring);
} else {
$querystring = '';
}
return $querystring;
}
/**
* Parses raw querystring and returns an array of it
*
* @param string $querystring The querystring to parse
* @return array An array of the querystring data
* @access private
*/
function _parseRawQuerystring($querystring)
{
$parts = preg_split('/[' . preg_quote(ini_get('arg_separator.input'), '/') . ']/', $querystring, -1, PREG_SPLIT_NO_EMPTY);
$return = array();
foreach ($parts as $part) {
if (strpos($part, '=') !== false) {
$value = substr($part, strpos($part, '=') + 1);
$key = substr($part, 0, strpos($part, '='));
} else {
$value = null;
$key = $part;
}
if (!$this->getOption('encode_query_keys')) {
$key = rawurldecode($key);
}
if (preg_match('#^(.*)\[([0-9a-z_-]*)\]#i', $key, $matches)) {
$key = $matches[1];
$idx = $matches[2];
// Ensure is an array
if (empty($return[$key]) || !is_array($return[$key])) {
$return[$key] = array();
}
// Add data
if ($idx === '') {
$return[$key][] = $value;
} else {
$return[$key][$idx] = $value;
}
} elseif (!$this->useBrackets AND !empty($return[$key])) {
$return[$key] = (array)$return[$key];
$return[$key][] = $value;
} else {
$return[$key] = $value;
}
}
return $return;
}
/**
* Resolves //, ../ and ./ from a path and returns
* the result. Eg:
*
* /foo/bar/../boo.php => /foo/boo.php
* /foo/bar/../../boo.php => /boo.php
* /foo/bar/.././/boo.php => /foo/boo.php
*
* This method can also be called statically.
*
* @param string $path URL path to resolve
* @return string The result
*/
function resolvePath($path)
{
$path = explode('/', str_replace('//', '/', $path));
for ($i=0; $i<count($path); $i++) {
if ($path[$i] == '.') {
unset($path[$i]);
$path = array_values($path);
$i--;
} elseif ($path[$i] == '..' AND ($i > 1 OR ($i == 1 AND $path[0] != '') ) ) {
unset($path[$i]);
unset($path[$i-1]);
$path = array_values($path);
$i -= 2;
} elseif ($path[$i] == '..' AND $i == 1 AND $path[0] == '') {
unset($path[$i]);
$path = array_values($path);
$i--;
} else {
continue;
}
}
return implode('/', $path);
}
/**
* Returns the standard port number for a protocol
*
* @param string $scheme The protocol to lookup
* @return integer Port number or NULL if no scheme matches
*
* @author Philippe Jausions <Philippe.Jausions@11abacus.com>
*/
function getStandardPort($scheme)
{
switch (strtolower($scheme)) {
case 'http': return 80;
case 'https': return 443;
case 'ftp': return 21;
case 'imap': return 143;
case 'imaps': return 993;
case 'pop3': return 110;
case 'pop3s': return 995;
default: return null;
}
}
/**
* Forces the URL to a particular protocol
*
* @param string $protocol Protocol to force the URL to
* @param integer $port Optional port (standard port is used by default)
*/
function setProtocol($protocol, $port = null)
{
$this->protocol = $protocol;
$this->port = is_null($port) ? $this->getStandardPort($protocol) : $port;
}
/**
* Set an option
*
* This function set an option
* to be used thorough the script.
*
* @access public
* @param string $optionName The optionname to set
* @param string $value The value of this option.
*/
function setOption($optionName, $value)
{
if (!array_key_exists($optionName, $this->options)) {
return false;
}
$this->options[$optionName] = $value;
$this->initialize();
}
/**
* Get an option
*
* This function gets an option
* from the $this->options array
* and return it's value.
*
* @access public
* @param string $opionName The name of the option to retrieve
* @see $this->options
*/
function getOption($optionName)
{
if (!isset($this->options[$optionName])) {
return false;
}
return $this->options[$optionName];
}
}
?>

View file

@ -0,0 +1,984 @@
<?php
// {{{ license
// +----------------------------------------------------------------------+
// | PHP version 4.2 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2007 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: Dan Allen <dan@mojavelinux.com> |
// | Jason Rust <jrust@php.net> |
// +----------------------------------------------------------------------+
// $Id: Detect.php,v 1.28 2009/06/09 04:07:00 clockwerx Exp $
// }}}
// {{{ constants
define('NET_USERAGENT_DETECT_BROWSER', 'browser');
define('NET_USERAGENT_DETECT_OS', 'os');
define('NET_USERAGENT_DETECT_FEATURES', 'features');
define('NET_USERAGENT_DETECT_QUIRKS', 'quirks');
define('NET_USERAGENT_DETECT_ACCEPT', 'accept');
define('NET_USERAGENT_DETECT_ALL', 'all');
// }}}
// {{{ class Net_UserAgent_Detect
/**
* The Net_UserAgent_Detect object does a number of tests on an HTTP user
* agent string. The results of these tests are available via methods of
* the object. Note that all methods in this class can be called
* statically. The constructor and singleton methods are only retained
* for BC.
*
* This module is based upon the JavaScript browser detection code
* available at http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html.
* This module had many influences from the lib/Browser.php code in
* version 1.3 of Horde.
*
* @author Jason Rust <jrust@php.net>
* @author Dan Allen <dan@mojavelinux.com>
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Jon Parise <jon@horde.org>
* @package Net_UserAgent
*/
// }}}
class Net_UserAgent_Detect {
// {{{ constructor
function __construct($in_userAgent = null, $in_detect = null)
{
$this->detect($in_userAgent, $in_detect);
}
// }}}
// {{{ singleton
/**
* To be used in place of the contructor to return only open instance.
*
* @access public
* @return object Net_UserAgent_Detect instance
*/
function &singleton($in_userAgent = null, $in_detect = null)
{
static $instance;
if (!isset($instance)) {
$instance = new Net_UserAgent_Detect($in_userAgent, $in_detect);
}
return $instance;
}
// }}}
// {{{ detect()
/**
* Detect the user agent and prepare flags, features and quirks
* based on what is found
*
* 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. Note that if you are going to be calling methods of
* this class statically then set all the parameters using th
* setOption()
*
* @param string $in_userAgent (optional) User agent override.
* @param mixed $in_detect (optional) The level of checking to do.
*
* @access public
* @return void
*/
function detect($in_userAgent = null, $in_detect = null)
{
static $hasRun;
$options = Net_UserAgent_Detect::_getStaticProperty('options');
if (!empty($hasRun) && empty($options['re-evaluate'])) {
return;
}
$hasRun = true;
// {{{ set up static properties
$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');
// Array that stores all of the flags for the vendor and version
// of the different browsers
$browser = Net_UserAgent_Detect::_getStaticProperty('browser');
$browser = array_flip(array('ns', 'ns2', 'ns3', 'ns4', 'ns4up', 'nav', 'ns6', 'belowns6', 'ns6up', 'firefox', 'firefox0.x', 'firefox1.x', 'firefox1.5', 'firefox2.x', 'firefox3.x', 'gecko', 'ie', 'ie3', 'ie4', 'ie4up', 'ie5', 'ie5_5', 'ie5up', 'ie6', 'belowie6', 'ie6up', 'ie7', 'ie7up', 'ie8', 'ie8tr', 'ie8up', 'opera', 'opera2', 'opera3', 'opera4', 'opera5', 'opera6', 'opera7', 'opera8', 'opera9', 'opera5up', 'opera6up', 'opera7up', 'belowopera8', 'opera8up', 'opera9up', 'aol', 'aol3', 'aol4', 'aol5', 'aol6', 'aol7', 'aol8', 'webtv', 'aoltv', 'tvnavigator', 'hotjava', 'hotjava3', 'hotjava3up', 'konq', 'safari', 'safari_mobile', 'chrome', 'netgem', 'webdav', 'icab'));
// 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', 'wince', 'winme', 'win2k', 'winxp', 'winnt', 'win2003', 'vista', 'win7', '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
// the proper handling of this quirk.
$quirks = Net_UserAgent_Detect::_getStaticProperty('quirks');
$quirks = array(
'must_cache_forms' => false,
'popups_disabled' => false,
'empty_file_input_value' => false,
'cache_ssl_downloads' => false,
'scrollbar_in_way' => false,
'break_disposition_header' => false,
'nested_table_render_bug' => false);
// Array that stores credentials for each of the browser/os
// combinations. These allow quick access to determine if the
// current client has a feature that is going to be implemented
// in the script.
$features = Net_UserAgent_Detect::_getStaticProperty('features');
$features = array(
'javascript' => false,
'dhtml' => false,
'dom' => false,
'sidebar' => false,
'gecko' => false,
'svg' => false,
'css2' => false,
'ajax' => false);
// The leading identifier is the very first term in the user
// agent string, which is used to identify clients which are not
// Mosaic-based browsers.
$leadingIdentifier = Net_UserAgent_Detect::_getStaticProperty('leadingIdentifier');
// The full version of the client as supplied by the very first
// numbers in the user agent
$version = Net_UserAgent_Detect::_getStaticProperty('version');
$version = 0;
// The major part of the client version, which is the integer
// value of the version.
$majorVersion = Net_UserAgent_Detect::_getStaticProperty('majorVersion');
$majorVersion = 0;
// The minor part of the client version, which is the decimal
// parts of the version
$subVersion = Net_UserAgent_Detect::_getStaticProperty('subVersion');
$subVersion = 0;
// }}}
// detemine what user agent we are using
if (is_null($in_userAgent)) {
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$userAgent = $_SERVER['HTTP_USER_AGENT'];
}
elseif (isset($GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT'])) {
$userAgent = $GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT'];
}
else {
$userAgent = '';
}
}
else {
$userAgent = $in_userAgent;
}
// get the lowercase version for case-insensitive searching
$agt = strtolower($userAgent);
// figure out what we need to look for
$detectOptions = array(NET_USERAGENT_DETECT_BROWSER,
NET_USERAGENT_DETECT_OS, NET_USERAGENT_DETECT_FEATURES,
NET_USERAGENT_DETECT_QUIRKS, NET_USERAGENT_DETECT_ACCEPT,
NET_USERAGENT_DETECT_ALL);
$detect = is_null($in_detect) ? NET_USERAGENT_DETECT_ALL : $in_detect;
settype($detect, 'array');
foreach($detectOptions as $option) {
if (in_array($option, $detect)) {
$detectFlags[$option] = true;
}
else {
$detectFlags[$option] = false;
}
}
// initialize the arrays of browsers and operating systems
// Get the type and version of the client
if (preg_match(";^([[:alnum:]]+)[ /\(]*[[:alpha:]]*([\d]*)(\.[\d\.]*);", $agt, $matches)) {
list(, $leadingIdentifier, $majorVersion, $subVersion) = $matches;
}
if (empty($leadingIdentifier)) {
$leadingIdentifier = 'Unknown';
}
$version = $majorVersion . $subVersion;
// Browser type
if ($detectFlags[NET_USERAGENT_DETECT_ALL] || $detectFlags[NET_USERAGENT_DETECT_BROWSER]) {
$browser['webdav'] = ($agt == 'microsoft data access internet publishing provider dav' || $agt == 'microsoft data access internet publishing provider protocol discovery');
$browser['konq'] = (strpos($agt, 'konqueror') !== false || strpos($agt, 'safari') !== false );
$browser['safari'] = (strpos($agt, 'safari') !== false);
$browser['chrome'] = (strpos($agt, 'chrome') !== false);
$browser['safari_mobile'] = (strpos($agt, 'safari') !== false && strpos($agt, 'mobile') !== false );
$browser['text'] = strpos($agt, 'links') !== false || strpos($agt, 'lynx') !== false || strpos($agt, 'w3m') !== false;
$browser['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;
$browser['netgem'] = strpos($agt, 'netgem') !== false;
$browser['icab'] = strpos($agt, 'icab') !== false;
$browser['ns2'] = $browser['ns'] && $majorVersion == 2;
$browser['ns3'] = $browser['ns'] && $majorVersion == 3;
$browser['ns4'] = $browser['ns'] && $majorVersion == 4;
$browser['ns4up'] = $browser['ns'] && $majorVersion >= 4;
// determine if this is a Netscape Navigator
$browser['nav'] = $browser['belowns6'] = $browser['ns'] && $majorVersion < 5;
$browser['ns6'] = !$browser['konq'] && $browser['ns'] && $majorVersion == 5;
$browser['ns6up'] = $browser['ns6'] && $majorVersion >= 5;
$browser['gecko'] = strpos($agt, 'gecko') !== false && !$browser['konq'];
$browser['firefox'] = $browser['gecko'] && strpos($agt, 'firefox') !== false;
$browser['firefox0.x'] = $browser['firefox'] && strpos($agt, 'firefox/0.') !== false;
$browser['firefox1.x'] = $browser['firefox'] && strpos($agt, 'firefox/1.') !== false;
$browser['firefox1.5'] = $browser['firefox'] && strpos($agt, 'firefox/1.5') !== false;
$browser['firefox2.x'] = $browser['firefox'] && strpos($agt, 'firefox/2.') !== false;
$browser['firefox3.x'] = $browser['firefox'] && strpos($agt, 'firefox/3.') !== false;
$browser['ie'] = strpos($agt, 'msie') !== false && !(strpos($agt, 'opera') !== false);
$browser['ie3'] = $browser['ie'] && $majorVersion < 4;
$browser['ie4'] = $browser['ie'] && $majorVersion == 4 && (strpos($agt, 'msie 4') !== false);
$browser['ie4up'] = $browser['ie'] && !$browser['ie3'];
$browser['ie5'] = $browser['ie4up'] && (strpos($agt, 'msie 5') !== false);
$browser['ie5_5'] = $browser['ie4up'] && (strpos($agt, 'msie 5.5') !== false);
$browser['ie5up'] = $browser['ie4up'] && !$browser['ie3'] && !$browser['ie4'];
$browser['ie5_5up'] = $browser['ie5up'] && !$browser['ie5'];
$browser['ie6'] = strpos($agt, 'msie 6') !== false;
$browser['ie6up'] = $browser['ie5up'] && !$browser['ie5'] && !$browser['ie5_5'];
$browser['ie7'] = strpos($agt, 'msie 7') && !strpos($agt,'trident/4');
$browser['ie7up'] = $browser['ie6up'] && !$browser['ie6'];
$browser['ie8tr'] = strpos($agt, 'msie 7') && strpos($agt,'trident/4') !== false;
$browser['ie8'] = strpos($agt, 'msie 8') !== false;
$browser['ie8up'] = $browser['ie7up'] && !$browser['ie7'];
$browser['belowie6']= $browser['ie'] && !$browser['ie6up'];
$browser['opera'] = strpos($agt, 'opera') !== false;
$browser['opera2'] = strpos($agt, 'opera 2') !== false || strpos($agt, 'opera/2') !== false;
$browser['opera3'] = strpos($agt, 'opera 3') !== false || strpos($agt, 'opera/3') !== false;
$browser['opera4'] = strpos($agt, 'opera 4') !== false || strpos($agt, 'opera/4') !== false;
$browser['opera5'] = strpos($agt, 'opera 5') !== false || strpos($agt, 'opera/5') !== false;
$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 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'];
$browser['opera8up'] = $browser['opera'] && !$browser['opera2'] && !$browser['opera3'] && !$browser['opera4'] && !$browser['opera5'] && !$browser['opera6'] && !$browser['opera7'];
$browser['opera9up'] = $browser['opera'] && !$browser['opera2'] && !$browser['opera3'] && !$browser['opera4'] && !$browser['opera5'] && !$browser['opera6'] && !$browser['opera7'] && !$browser['opera8'];
$browser['belowopera8'] = $browser['opera'] && !$browser['opera8up'];
$browser['aol'] = strpos($agt, 'aol') !== false;
$browser['aol3'] = $browser['aol'] && $browser['ie3'];
$browser['aol4'] = $browser['aol'] && $browser['ie4'];
$browser['aol5'] = strpos($agt, 'aol 5') !== false;
$browser['aol6'] = strpos($agt, 'aol 6') !== false;
$browser['aol7'] = strpos($agt, 'aol 7') !== false || strpos($agt, 'aol7') !== false;
$browser['aol8'] = strpos($agt, 'aol 8') !== false || strpos($agt, 'aol8') !== false;
$browser['webtv'] = strpos($agt, 'webtv') !== false;
$browser['aoltv'] = $browser['tvnavigator'] = strpos($agt, 'navio') !== false || strpos($agt, 'navio_aoltv') !== false;
$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] ||
($detectFlags[NET_USERAGENT_DETECT_BROWSER] && $detectFlags[NET_USERAGENT_DETECT_FEATURES])) {
// Javascript Check
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);
}
elseif ($browser['opera'] || $browser['ns3']) {
Net_UserAgent_Detect::setFeature('javascript', 1.1);
}
elseif (($browser['ns4'] && ($version <= 4.05)) || $browser['ie4']) {
Net_UserAgent_Detect::setFeature('javascript', 1.2);
}
elseif (($browser['ie5up'] && strpos($agt, 'mac') !== false) || $browser['konq']) {
Net_UserAgent_Detect::setFeature('javascript', 1.4);
}
// I can't believe IE6 still has javascript 1.3, what a shitty browser
elseif (($browser['ns4'] && ($version > 4.05)) || $browser['ie5up'] || $browser['hotjava3up']) {
Net_UserAgent_Detect::setFeature('javascript', 1.3);
}
elseif ($browser['ns6up'] || $browser['gecko'] || $browser['netgem']) {
Net_UserAgent_Detect::setFeature('javascript', 1.5);
}
}
/** OS Check **/
if ($detectFlags[NET_USERAGENT_DETECT_ALL] || $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;
$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;
$os['win98'] = strpos($agt, 'win98') !== false || strpos($agt, 'windows 98') !== false;
$os['win9x'] = $os['win95'] || $os['win98'];
$os['winnt'] = (strpos($agt, 'winnt') !== false || strpos($agt, 'windows nt') !== false) && strpos($agt, 'windows nt 5') === false;
$os['win32'] = $os['win95'] || $os['winnt'] || $os['win98'] || $majorVersion >= 4 && strpos($agt, 'win32') !== false || strpos($agt, '32bit') !== false;
$os['vista'] = strpos($agt, 'windows nt 6.0') !== false;
$os['win7'] = strpos($agt, 'windows nt 6.1') !== false;
$os['os2'] = strpos($agt, 'os/2') !== false || strpos($agt, 'ibm-webexplorer') !== false;
$os['mac'] = strpos($agt, 'mac') !== false;
$os['mac68k'] = $os['mac'] && (strpos($agt, '68k') !== false || strpos($agt, '68000') !== false);
$os['macppc'] = $os['mac'] && (strpos($agt, 'ppc') !== false || strpos($agt, 'powerpc') !== false);
$os['sun'] = strpos($agt, 'sunos') !== false;
$os['sun4'] = strpos($agt, 'sunos 4') !== false;
$os['sun5'] = strpos($agt, 'sunos 5') !== false;
$os['suni86'] = $os['sun'] && strpos($agt, 'i86') !== false;
$os['irix'] = strpos($agt, 'irix') !== false;
$os['irix5'] = strpos($agt, 'irix 5') !== false;
$os['irix6'] = strpos($agt, 'irix 6') !== false || strpos($agt, 'irix6') !== false;
$os['hpux'] = strpos($agt, 'hp-ux') !== false;
$os['hpux9'] = $os['hpux'] && strpos($agt, '09.') !== false;
$os['hpux10'] = $os['hpux'] && strpos($agt, '10.') !== false;
$os['aix'] = strpos($agt, 'aix') !== false;
$os['aix1'] = strpos($agt, 'aix 1') !== false;
$os['aix2'] = strpos($agt, 'aix 2') !== false;
$os['aix3'] = strpos($agt, 'aix 3') !== false;
$os['aix4'] = strpos($agt, 'aix 4') !== false;
$os['linux'] = strpos($agt, 'inux') !== false;
$os['sco'] = strpos($agt, 'sco') !== false || strpos($agt, 'unix_sv') !== false;
$os['unixware'] = strpos($agt, 'unix_system_v') !== false;
$os['mpras'] = strpos($agt, 'ncr') !== false;
$os['reliant'] = strpos($agt, 'reliant') !== false;
$os['dec'] = strpos($agt, 'dec') !== false || strpos($agt, 'osf1') !== false || strpos($agt, 'dec_alpha') !== false || strpos($agt, 'alphaserver') !== false || strpos($agt, 'ultrix') !== false || strpos($agt, 'alphastation') !== false;
$os['sinix'] = strpos($agt, 'sinix') !== false;
$os['freebsd'] = strpos($agt, 'freebsd') !== false;
$os['bsd'] = strpos($agt, 'bsd') !== false;
$os['unix'] = strpos($agt, 'x11') !== false || strpos($agt, 'unix') !== false || $os['sun'] || $os['irix'] || $os['hpux'] || $os['sco'] || $os['unixware'] || $os['mpras'] || $os['reliant'] || $os['dec'] || $os['sinix'] || $os['aix'] || $os['linux'] || $os['bsd'] || $os['freebsd'];
$os['vms'] = strpos($agt, 'vax') !== false || strpos($agt, 'openvms') !== false;
}
// Setup the quirks
if ($detectFlags[NET_USERAGENT_DETECT_ALL] ||
($detectFlags[NET_USERAGENT_DETECT_BROWSER] && $detectFlags[NET_USERAGENT_DETECT_QUIRKS])) {
if ($browser['konq']) {
Net_UserAgent_Detect::setQuirk('empty_file_input_value');
}
if ($browser['ie']) {
Net_UserAgent_Detect::setQuirk('cache_ssl_downloads');
}
if ($browser['ie6']) {
Net_UserAgent_Detect::setQuirk('scrollbar_in_way');
}
if ($browser['ie5']) {
Net_UserAgent_Detect::setQuirk('break_disposition_header');
}
if ($browser['ie7']) {
Net_UserAgent_Detect::setQuirk('popups_disabled');
}
if ($browser['ns6']) {
Net_UserAgent_Detect::setQuirk('popups_disabled');
Net_UserAgent_Detect::setQuirk('must_cache_forms');
}
if ($browser['nav'] && $subVersion < .79) {
Net_UserAgent_Detect::setQuirk('nested_table_render_bug');
}
}
// Set features
if ($detectFlags[NET_USERAGENT_DETECT_ALL] ||
($detectFlags[NET_USERAGENT_DETECT_BROWSER] && $detectFlags[NET_USERAGENT_DETECT_FEATURES])) {
if ($browser['gecko']) {
preg_match(';gecko/([\d]+)\b;i', $agt, $matches);
Net_UserAgent_Detect::setFeature('gecko', $matches[1]);
}
if ($browser['gecko'] || ($browser['ie5up'] && !$browser['iemobile']) || $browser['konq'] || $browser['opera8up'] && !$os['wince']) {
Net_UserAgent_Detect::setFeature('ajax');
}
if ($browser['ns6up'] || $browser['opera5up'] || $browser['konq'] || $browser['netgem']) {
Net_UserAgent_Detect::setFeature('dom');
}
if ($browser['ie4up'] || $browser['ns4up'] || $browser['opera5up'] || $browser['konq'] || $browser['netgem']) {
Net_UserAgent_Detect::setFeature('dhtml');
}
if ($browser['firefox1.5'] || $browser['firefox2.x'] || $browser['opera9up']) {
Net_UserAgent_Detect::setFeature('svg');
}
if ($browser['gecko'] || $browser['ns6up'] || $browser['ie5up'] || $browser['konq'] || $browser['opera7up']) {
Net_UserAgent_Detect::setFeature('css2');
}
}
if ($detectFlags[NET_USERAGENT_DETECT_ALL] || $detectFlags[NET_USERAGENT_DETECT_ACCEPT]) {
$mimetypes = preg_split(';[\s,]+;', substr(getenv('HTTP_ACCEPT'), 0, strpos(getenv('HTTP_ACCEPT') . ';', ';')), -1, PREG_SPLIT_NO_EMPTY);
Net_UserAgent_Detect::setAcceptType((array) $mimetypes, 'mimetype');
$languages = preg_split(';[\s,]+;', substr(getenv('HTTP_ACCEPT_LANGUAGE'), 0, strpos(getenv('HTTP_ACCEPT_LANGUAGE') . ';', ';')), -1, PREG_SPLIT_NO_EMPTY);
if (empty($languages)) {
$languages = 'en';
}
Net_UserAgent_Detect::setAcceptType((array) $languages, 'language');
$encodings = preg_split(';[\s,]+;', substr(getenv('HTTP_ACCEPT_ENCODING'), 0, strpos(getenv('HTTP_ACCEPT_ENCODING') . ';', ';')), -1, PREG_SPLIT_NO_EMPTY);
Net_UserAgent_Detect::setAcceptType((array) $encodings, 'encoding');
$charsets = preg_split(';[\s,]+;', substr(getenv('HTTP_ACCEPT_CHARSET'), 0, strpos(getenv('HTTP_ACCEPT_CHARSET') . ';', ';')), -1, PREG_SPLIT_NO_EMPTY);
Net_UserAgent_Detect::setAcceptType((array) $charsets, 'charset');
}
}
// }}}
// {{{ setOption()
/**
* Sets a class option. The available settings are:
* o 'userAgent' => The user agent string to detect (useful for
* checking a string manually).
* o 'detectOptions' => The level of checking to do. A single level
* or an array of options. Default is NET_USERAGENT_DETECT_ALL.
*
* @param string $in_field The option field (userAgent or detectOptions)
* @param mixed $in_value The value for the field
*/
function setOption($in_field, $in_value)
{
$options = Net_UserAgent_Detect::_getStaticProperty('options');
$options[$in_field] = $in_value;
}
// }}}
// {{{ isBrowser()
/**
* Look up the provide browser flag and return a boolean value
*
* Given one of the flags listed in the properties, this function will return
* the value associated with that flag.
*
* @param string $in_match flag to lookup
*
* @access public
* @return boolean whether or not the browser satisfies this flag
*/
function isBrowser($in_match)
{
Net_UserAgent_Detect::detect();
$browser = Net_UserAgent_Detect::_getStaticProperty('browser');
return isset($browser[strtolower($in_match)]) ? $browser[strtolower($in_match)] : false;
}
// }}}
// {{{ getBrowser()
/**
* Since simply returning the "browser" is somewhat ambiguous since there
* are different ways to classify the browser, this function works by taking
* an expect list and returning the string of the first match, so put the important
* ones first in the array.
*
* @param array $in_expectList the browser flags to search for
*
* @access public
* @return string first flag that matches
*/
function getBrowser($in_expectList)
{
Net_UserAgent_Detect::detect();
$browser = Net_UserAgent_Detect::_getStaticProperty('browser');
foreach((array) $in_expectList as $brwsr) {
if (!empty($browser[strtolower($brwsr)])) {
return $brwsr;
}
}
}
// }}}
// {{{ getBrowserString()
/**
* This function returns the vendor string corresponding to the flag.
*
* Either use the default matches or pass in an associative array of
* flags and corresponding vendor strings. This function will find
* the highest version flag and return the vendor string corresponding
* to the appropriate flag. Be sure to pass in the flags in ascending order
* if you want a basic matches first, followed by more detailed matches.
*
* @param array $in_vendorStrings (optional) array of flags matched with vendor strings
*
* @access public
* @return string vendor string matches appropriate flag
*/
function getBrowserString($in_vendorStrings = null)
{
if (is_null($in_vendorStrings)) {
$in_vendorStrings = array (
'ie' => 'Microsoft Internet Explorer',
'ie4up' => 'Microsoft Internet Explorer 4.x',
'ie5up' => 'Microsoft Internet Explorer 5.x',
'ie6up' => 'Microsoft Internet Explorer 6.x',
'ie7up' => 'Microsoft Internet Explorer 7.x',
'ie8up' => 'Microsoft Internet Explorer 8.x',
'ie8tr' => 'Microsoft Internet Explorer 8.x (Compatibility View)',
'opera4' => 'Opera 4.x',
'opera5up' => 'Opera 5.x',
'nav' => 'Netscape Navigator',
'ns4' => 'Netscape 4.x',
'ns6up' => 'Mozilla/Netscape 6.x',
'firefox0.x' => 'Firefox 0.x',
'firefox1.x' => 'Firefox 1.x',
'firefox1.5' => 'Firefox 1.5',
'firefox2.x' => 'Firefox 2.x',
'firefox3.x' => 'Firefox 3.x',
'konq' => 'Konqueror',
'safari' => 'Safari',
'safari_mobile' => 'Safari Mobile',
'chrome' => 'Google Chrome',
'netgem' => 'Netgem/iPlayer');
}
Net_UserAgent_Detect::detect();
$browser = Net_UserAgent_Detect::_getStaticProperty('browser');
foreach((array) $in_vendorStrings as $flag => $string) {
if (!empty($browser[$flag])) {
$vendorString = $string;
}
}
// if there are no matches just use the user agent leading idendifier (usually Mozilla)
if (!isset($vendorString)) {
$leadingIdentifier = Net_UserAgent_Detect::_getStaticProperty('leadingIdentifier');
$vendorString = $leadingIdentifier;
}
return $vendorString;
}
// }}}
// {{{ isIE()
/**
* Determine if the browser is an Internet Explorer browser
*
* @access public
* @return bool whether or not this browser is an ie browser
*/
function isIE()
{
Net_UserAgent_Detect::detect();
$browser = Net_UserAgent_Detect::_getStaticProperty('browser');
return !empty($browser['ie']);
}
// }}}
// {{{ isNavigator()
/**
* Determine if the browser is a Netscape Navigator browser
*
* @access public
* @return bool whether or not this browser is a Netscape Navigator browser
*/
function isNavigator()
{
Net_UserAgent_Detect::detect();
$browser = Net_UserAgent_Detect::_getStaticProperty('browser');
return !empty($browser['nav']);
}
// }}}
// {{{ isNetscape()
/**
* Determine if the browser is a Netscape or Mozilla browser
*
* Note that this function is not the same as isNavigator, since the
* new Mozilla browsers are still sponsered by Netscape, and hence are
* Netscape products, but not the original Navigators
*
* @access public
* @return bool whether or not this browser is a Netscape product
*/
function isNetscape()
{
Net_UserAgent_Detect::detect();
$browser = Net_UserAgent_Detect::_getStaticProperty('browser');
return !empty($browser['ns4up']);
}
// }}}
// {{{ isOS()
/**
* Look up the provide OS flag and return a boolean value
*
* Given one of the flags listed in the properties, this function will return
* the value associated with that flag for the operating system.
*
* @param string $in_match flag to lookup
*
* @access public
* @return boolean whether or not the OS satisfies this flag
*/
function isOS($in_match)
{
Net_UserAgent_Detect::detect();
$os = Net_UserAgent_Detect::_getStaticProperty('os');
return isset($os[strtolower($in_match)]) ? $os[strtolower($in_match)] : false;
}
// }}}
// {{{ getOS()
/**
* Since simply returning the "os" is somewhat ambiguous since there
* are different ways to classify the browser, this function works by taking
* an expect list and returning the string of the first match, so put the important
* ones first in the array.
*
* @access public
* @return string first flag that matches
*/
function getOS($in_expectList)
{
Net_UserAgent_Detect::detect();
$os = Net_UserAgent_Detect::_getStaticProperty('os');
foreach((array) $in_expectList as $expectOs) {
if (!empty($os[strtolower($expectOs)])) {
return $expectOs;
}
}
}
// }}}
// {{{ getOSString()
/**
* This function returns the os string corresponding to the flag.
*
* Either use the default matches or pass in an associative array of
* flags and corresponding os strings. This function will find
* the highest version flag and return the os string corresponding
* to the appropriate flag. Be sure to pass in the flags in ascending order
* if you want a basic matches first, followed by more detailed matches.
*
* @param array $in_osStrings (optional) array of flags matched with os strings
*
* @access public
* @return string os string matches appropriate flag
*/
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',
'winnt' => 'Microsoft Windows NT',
'winxp' => 'Microsoft Windows XP',
'win2003' => 'Microsoft Windows 2003',
'vista' => 'Microsoft Windows Vista',
'win7' => 'Microsoft Windows 7',
'mac' => 'Macintosh',
'unix' => 'Linux/Unix');
}
Net_UserAgent_Detect::detect();
$osString = 'Unknown';
$os = Net_UserAgent_Detect::_getStaticProperty('os');
foreach((array) $in_osStrings as $flag => $string) {
if (!empty($os[$flag])) {
$osString = $string;
}
}
return $osString;
}
// }}}
// {{{ setQuirk()
/**
* Set a unique behavior for the current browser.
*
* Many client browsers do some really funky things, and this
* mechanism allows the coder to determine if an excepetion must
* be made with the current client.
*
* @param string $in_quirk The quirk to set
* @param string $in_hasQuirk (optional) Does the browser have the quirk?
*
* @access public
* @return void
*/
function setQuirk($in_quirk, $in_hasQuirk = true)
{
$quirks = Net_UserAgent_Detect::_getStaticProperty('quirks');
$hasQuirk = !empty($in_hasQuirk);
$quirks[strtolower($in_quirk)] = $hasQuirk;
}
// }}}
// {{{ hasQuirk()
/**
* Check a unique behavior for the current browser.
*
* Many client browsers do some really funky things, and this
* mechanism allows the coder to determine if an excepetion must
* be made with the current client.
*
* @param string $in_quirk The quirk to detect
*
* @access public
* @return bool whether or not browser has this quirk
*/
function hasQuirk($in_quirk)
{
return (bool) Net_UserAgent_Detect::getQuirk($in_quirk);
}
// }}}
// {{{ getQuirk()
/**
* Get the unique behavior for the current browser.
*
* Many client browsers do some really funky things, and this
* mechanism allows the coder to determine if an excepetion must
* be made with the current client.
*
* @param string $in_quirk The quirk to detect
*
* @access public
* @return string value of the quirk, in this case usually a boolean
*/
function getQuirk($in_quirk)
{
Net_UserAgent_Detect::detect();
$quirks = Net_UserAgent_Detect::_getStaticProperty('quirks');
return isset($quirks[strtolower($in_quirk)]) ? $quirks[strtolower($in_quirk)] : null;
}
// }}}
// {{{ setFeature()
/**
* Set capabilities for the current browser.
*
* Since the capabilities of client browsers vary widly, this interface
* helps keep track of the core features of a client, such as if the client
* supports dhtml, dom, javascript, etc.
*
* @param string $in_feature The feature to set
* @param string $in_hasFeature (optional) Does the browser have the feature?
*
* @access public
* @return void
*/
function setFeature($in_feature, $in_hasFeature = true)
{
$features = Net_UserAgent_Detect::_getStaticProperty('features');
$features[strtolower($in_feature)] = $in_hasFeature;
}
// }}}
// {{{ hasFeature()
/**
* Check the capabilities for the current browser.
*
* Since the capabilities of client browsers vary widly, this interface
* helps keep track of the core features of a client, such as if the client
* supports dhtml, dom, javascript, etc.
*
* @param string $in_feature The feature to detect
*
* @access public
* @return bool whether or not the current client has this feature
*/
function hasFeature($in_feature)
{
return (bool) Net_UserAgent_Detect::getFeature($in_feature);
}
// }}}
// {{{ getFeature()
/**
* Get the capabilities for the current browser.
*
* Since the capabilities of client browsers vary widly, this interface
* helps keep track of the core features of a client, such as if the client
* supports dhtml, dom, javascript, etc.
*
* @param string $in_feature The feature to detect
*
* @access public
* @return string value of the feature requested
*/
function getFeature($in_feature)
{
Net_UserAgent_Detect::detect();
$features = Net_UserAgent_Detect::_getStaticProperty('features');
return isset($features[strtolower($in_feature)]) ? $features[strtolower($in_feature)] : null;
}
// }}}
// {{{ getAcceptType()
/**
* Retrive the accept type for the current browser.
*
* To keep track of the mime-types, languages, charsets and encodings
* that each browser accepts we use associative arrays for each type.
* This function works like getBrowser() as it takes an expect list
* and returns the first match. For instance, to find the language
* you would pass in your allowed languages and see if any of the
* languages set in the browser match.
*
* @param string $in_expectList values to check
* @param string $in_type type of accept
*
* @access public
* @return string the first matched value
*/
function getAcceptType($in_expectList, $in_type)
{
Net_UserAgent_Detect::detect();
$type = strtolower($in_type);
if ($type == 'mimetype' || $type == 'language' || $type == 'charset' || $type == 'encoding') {
$typeArray = Net_UserAgent_Detect::_getStaticProperty($type);
foreach((array) $in_expectList as $match) {
if (!empty($typeArray[$match])) {
return $match;
}
}
}
return null;
}
// }}}
// {{{ setAcceptType()
/**
* Set the accept types for the current browser.
*
* To keep track of the mime-types, languages, charsets and encodings
* that each browser accepts we use associative arrays for each type.
* This function takes and array of accepted values for the type and
* records them for retrieval.
*
* @param array $in_values values of the accept type
* @param string $in_type type of accept
*
* @access public
* @return void
*/
function setAcceptType($in_values, $in_type)
{
$type = strtolower($in_type);
if ($type == 'mimetype' || $type == 'language' || $type == 'charset' || $type == 'encoding') {
$typeArray = Net_UserAgent_Detect::_getStaticProperty($type);
foreach((array) $in_values as $value) {
$typeArray[$value] = true;
}
}
}
// }}}
// {{{ hasAcceptType()
/**
* Check the accept types for the current browser.
*
* To keep track of the mime-types, languages, charsets and encodings
* that each browser accepts we use associative arrays for each type.
* This function checks the array for the given type and determines if
* the browser accepts it.
*
* @param string $in_value values to check
* @param string $in_type type of accept
*
* @access public
* @return bool whether or not the value is accept for this type
*/
function hasAcceptType($in_value, $in_type)
{
return (bool) Net_UserAgent_Detect::getAcceptType((array) $in_value, $in_type);
}
// }}}
// {{{ getUserAgent()
/**
* Return the user agent string that is being worked on
*
* @access public
* @return string user agent
*/
function getUserAgent()
{
Net_UserAgent_Detect::detect();
$userAgent = Net_UserAgent_Detect::_getStaticProperty('userAgent');
return $userAgent;
}
// }}}
// {{{ _getStaticProperty()
/**
* Copy of getStaticProperty() from PEAR.php to avoid having to
* include PEAR.php
*
* @access private
* @param string $var The variable to retrieve.
* @return mixed A reference to the variable. If not set it will be
* auto initialised to NULL.
*/
function &_getStaticProperty($var)
{
static $properties;
return $properties[$var];
}
// }}}
}
?>

View file

@ -0,0 +1,180 @@
<?php
/**
* Net_UserAgent_Detect_APC.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.
*
* @category Net
* @package Net_UserAgent
* @author Lucas Nealan <lucas@facebook.com>
* @license http://www.php.net/license/2_02.txt PHP 2.0 Licence
* @version CVS: $Id: APC.php,v 1.3 2009/06/08 04:49:23 clockwerx Exp $
* @link http://pear.php.net/package/Net_UserAgent_Detect
*/
require_once 'Net/UserAgent/Detect.php';
/**
* Net_UserAgent_Detect_APC
*
* 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.
*
* @category Net
* @package Net_UserAgent
* @author Lucas Nealan <lucas@facebook.com>
* @license http://www.php.net/license/2_02.txt PHP 2.0 Licence
* @link http://pear.php.net/package/Net_UserAgent_Detect
*/
class Net_UserAgent_Detect_APC extends Net_UserAgent_Detect
{
var $key = '';
/**
* Class constructor
*
* @param string $in_userAgent (optional) User agent override.
* @param mixed $in_detect (optional) The level of checking to do.
* @param mixed $ua_cache_window Unknown
*/
function __construct($in_userAgent = null, $in_detect = null,
$ua_cache_window = 600)
{
$data = '';
$restored = false;
// don't cache after time period
$ua_cache_timeout = apc_fetch('useragent:cache_timeout');
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'];
// timeout and disable after 10 minutes of uptime
if (time() - $uptime > $ua_cache_window) {
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->cacheRestore($data);
}
}
}
if (!$data) {
$this->detect($in_userAgent, $in_detect);
if ($ua_cache_window > 0 && !$ua_cache_timeout) {
$this->cacheSave();
}
}
}
/**
* To be used in place of the contructor to return only open instance.
*
* @param string $in_userAgent (optional) User agent override.
* @param mixed $in_detect (optional) The level of checking to do.
*
* @access public
* @return object Net_UserAgent_Detect instance
*/
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;
}
/**
* Restore cached items
*
* @param mixed[] $cache An array of items to restore
*
* @return bool
*/
function cacheRestore($cache)
{
if (is_array($cache)) {
foreach ($cache as $prop => $value) {
$ptr = Net_UserAgent_Detect::_getStaticProperty($prop);
$ptr = $value;
}
return true;
}
return false;
}
/**
* Store items in APC
*
* @return void
*/
function cacheSave()
{
if ($this->key) {
$items = array('browser',
'features',
'leadingIdentifier',
'majorVersion',
'options',
'os',
'quirks',
'subVersion',
'userAgent',
'version');
$data = array();
foreach ($items as $item) {
$data[$item] = Net_UserAgent_Detect::_getStaticProperty($item);
}
apc_store($this->key, serialize($data));
}
}
}
?>