* @license PHP License * @package WB * @subpackage base */ /** * Generic request class * * * * @version 0.2.0 * @package WB * @subpackage base */ abstract class WBRequest { /** * request properties * @var array */ protected $_properties = array(); /** * meta information about the request, like additional headers * @var array */ protected $_meta = array(); /** * internal path to the request * @var string */ protected $_path = '/'; /** * protocol * @var string */ protected $protocol = ''; /** * encryption security * @var string */ protected $encryption = ''; /** * input filter * @var array */ protected $_filters = array(); /** * constructor * * */ public function __construct() { } /** * import new properties * * @param array * @param boolean whether to apply the request filters */ public function import($properties, $applyFilters = true) { $this->_properties = $properties; if ($applyFilters !== true) { return true; } for ($i = 0; $i < count($this->_filters); $i++) { $this->_filters[$i]->filter($this); } return true; } /** * export properties * * @return array $properties */ public function export() { return $this->_properties; } /** * receive raw request data * * @return string */ abstract public function getRaw(); /** * receive property * * @return string */ public function get($name, $default = null) { if (!isset($this->_properties[$name])) { return $default; } return $this->_properties[$name]; } /** * check whether the property is set * * @param string $name * @param bool $allowEmpty * @return bool */ public function has($name, $allowEmpty = true) { if (!isset($this->_properties[$name])) { return false; } if ($allowEmpty) { return true; } return !empty($this->_properties[$name]); } /** * set proerty value * * Overrider property or set to NULL * * @param string $name * @param mixed $value */ public function set($name, $value = null) { $this->_properties[$name] = $value; } /** * check whether the property is set * * @param string $name * @param bool $allowEmpty * @return bool */ public function getMeta($name, $default = null) { if (!isset($this->_meta[$name])) { return $default; } return $this->_meta[$name]; } /** * Get Range from Meta information * * Extract range request from request header (HTTP?) * * @return array */ public function getRange() { $range = $this->getMeta('HTTP_RANGE', ''); // extranct and normalize range if (empty($range) || (0 != strncmp('bytes=', $range, 6))) { $range = array(0, -1); return $range; } $range = explode('bytes=', $range); $range = explode('-', $range[1]); if (0 == strlen($range[1])) { $range[1] = -1; } // not supported if (0 == strlen($range[0])) { $range[1] = 0; } // range 0 is the offset, range 1 is the length if (0 < $range[1]) { $range[1] = $range[1] - $range[0] + 1; } return $range; } /** * Receive some special variables * * Grant acces to "path" * * @param string $name variable name * @return string */ public function __get($name) { switch ($name) { case 'path': return $this->_path; break; case 'protocol': return $this->protocol; break; case 'encryption': return $this->encryption; break; default: break; } } /** * Set some special variables * * Grant access to * - path * - protocol * - encryption * * @param string $name variable name * @param mnixed $value new value */ public function __set($name, $value) { switch ($name) { case 'path': $this->_path = $value; break; case 'protocol': $this->protocol = $value; break; case 'encryption': $this->encryption = $value; break; default: break; } } }