From 6845c00a3f752abecd9ef763848329bceaf63df4 Mon Sep 17 00:00:00 2001 From: xue <> Date: Fri, 31 Mar 2006 12:42:22 +0000 Subject: Reorganized folders under framework. This may break existing applications. --- framework/Data/TAPCCache.php | 152 --------- framework/Data/TCache.php | 88 ----- framework/Data/TDataFieldAccessor.php | 117 ------- framework/Data/TMemCache.php | 256 -------------- framework/Data/TSimpleDateFormatter.php | 354 -------------------- framework/Data/TSqliteCache.php | 265 --------------- framework/Data/TTarFileExtractor.php | 574 -------------------------------- framework/Data/TXmlDocument.php | 455 ------------------------- 8 files changed, 2261 deletions(-) delete mode 100644 framework/Data/TAPCCache.php delete mode 100644 framework/Data/TCache.php delete mode 100644 framework/Data/TDataFieldAccessor.php delete mode 100644 framework/Data/TMemCache.php delete mode 100644 framework/Data/TSimpleDateFormatter.php delete mode 100644 framework/Data/TSqliteCache.php delete mode 100644 framework/Data/TTarFileExtractor.php delete mode 100644 framework/Data/TXmlDocument.php (limited to 'framework/Data') diff --git a/framework/Data/TAPCCache.php b/framework/Data/TAPCCache.php deleted file mode 100644 index 5c0a6eba..00000000 --- a/framework/Data/TAPCCache.php +++ /dev/null @@ -1,152 +0,0 @@ - - * @link http://www.pradosoft.com/ - * @copyright Copyright © 2005 PradoSoft - * @license http://www.pradosoft.com/license/ - * @version $Revision: $ $Date: $ - * @package System.Data - */ - -/** - * TAPCCache class - * - * TAPCCache implements a cache application module based on {@link http://www.php.net/apc APC}. - * - * By definition, cache does not ensure the existence of a value - * even if it never expires. Cache is not meant to be an persistent storage. - * - * To use this module, the APC PHP extension must be loaded and set in the php.ini file the following: - * - * apc.cache_by_default=0 - * - * - * Some usage examples of TAPCCache are as follows, - * - * $cache=new TAPCCache; // TAPCCache may also be loaded as a Prado application module - * $cache->init(null); - * $cache->add('object',$object); - * $object2=$cache->get('object'); - * - * - * If loaded, TAPCCache will register itself with {@link TApplication} as the - * cache module. It can be accessed via {@link TApplication::getCache()}. - * - * TAPCCache may be configured in application configuration file as follows - * - * - * @author Alban Hanry - * @version $Revision: $ $Date: $ - * @package System.Data - * @since 3.0b - */ -class TAPCCache extends TModule implements ICache -{ - /** - * @var boolean if the module is initialized - */ - private $_initialized=false; - - /** - * @var string a unique prefix used to identify this cache instance from the others - */ - protected $_prefix=null; - - /** - * Initializes this module. - * This method is required by the IModule interface. - * @param TXmlElement configuration for this module, can be null - * @throws TConfigurationException if apc extension is not installed or not started, check your php.ini - */ - public function init($config) - { - if(!extension_loaded('apc')) - throw new TConfigurationException('apccache_extension_required'); - $application=$this->getApplication(); - $this->_prefix=$application->getUniqueID(); - $application->setCache($this); - $this->_initialized=true; - } - - /** - * Retrieves a value from cache with a specified key. - * @return mixed the value stored in cache, false if the value is not in the cache or expired. - */ - public function get($key) - { - if(($value=apc_fetch($this->_prefix.$key))!==false) - return Prado::unserialize($value); - else - return $value; - } - - /** - * Stores a value identified by a key into cache. - * If the cache already contains such a key, the existing value and - * expiration time will be replaced with the new ones. - * - * @param string the key identifying the value to be cached - * @param mixed the value to be cached - * @param integer the expiration time of the value, - * 0 means never expire, - * @return boolean true if the value is successfully stored into cache, false otherwise - */ - public function set($key,$value,$expiry=0) - { - return apc_store($this->_prefix.$key,Prado::serialize($value),$expiry); - } - - /** - * Stores a value identified by a key into cache if the cache does not contain this key. - * APC does not support this mode. So calling this method will raise an exception. - * @param string the key identifying the value to be cached - * @param mixed the value to be cached - * @param integer the expiration time of the value, - * 0 means never expire, - * @return boolean true if the value is successfully stored into cache, false otherwise - * @throw new TNotSupportedException if this method is invoked - */ - public function add($key,$value,$expiry=0) - { - throw new TNotSupportedException('apccache_add_unsupported'); - } - - /** - * Stores a value identified by a key into cache only if the cache contains this key. - * The existing value and expiration time will be overwritten with the new ones. - * APC does not support this mode. So calling this method will raise an exception. - * @param string the key identifying the value to be cached - * @param mixed the value to be cached - * @param integer the expiration time of the value, - * 0 means never expire, - * @return boolean true if the value is successfully stored into cache, false otherwise - * @throw new TNotSupportedException if this method is invoked - */ - public function replace($key,$value,$expiry=0) - { - throw new TNotSupportedException('apccache_replace_unsupported'); - } - - /** - * Deletes a value with the specified key from cache - * @param string the key of the value to be deleted - * @return boolean if no error happens during deletion - */ - public function delete($key) - { - return apc_delete($this->_prefix.$key); - } - - /** - * Deletes all values from cache. - */ - public function flush() - { - return apc_clear_cache('user'); - } - -} - -?> \ No newline at end of file diff --git a/framework/Data/TCache.php b/framework/Data/TCache.php deleted file mode 100644 index b598801d..00000000 --- a/framework/Data/TCache.php +++ /dev/null @@ -1,88 +0,0 @@ - - * @version $Revision: $ $Date: $ - * @package System.Data - * @since 3.0 - */ -interface ICache -{ - /** - * Retrieves a value from cache with a specified key. - * @return mixed the value stored in cache, false if the value is not in the cache or expired. - */ - public function get($id); - /** - * Stores a value identified by a key into cache. - * If the cache already contains such a key, the existing value and - * expiration time will be replaced with the new ones. - * - * @param string the key identifying the value to be cached - * @param mixed the value to be cached - * @param integer the expiration time of the value, - * 0 means never expire, - * a number less or equal than 60*60*24*30 means the number of seconds that the value will remain valid. - * a number greater than 60*60*24*30 means a UNIX timestamp after which the value will expire. - * @return boolean true if the value is successfully stored into cache, false otherwise - */ - public function set($id,$value,$expire=0); - /** - * Stores a value identified by a key into cache if the cache does not contain this key. - * Nothing will be done if the cache already contains the key. - * @param string the key identifying the value to be cached - * @param mixed the value to be cached - * @param integer the expiration time of the value, - * 0 means never expire, - * a number less or equal than 60*60*24*30 means the number of seconds that the value will remain valid. - * a number greater than 60*60*24*30 means a UNIX timestamp after which the value will expire. - * @return boolean true if the value is successfully stored into cache, false otherwise - */ - public function add($id,$value,$expire=0); - /** - * Stores a value identified by a key into cache only if the cache contains this key. - * The existing value and expiration time will be overwritten with the new ones. - * @param string the key identifying the value to be cached - * @param mixed the value to be cached - * @param integer the expiration time of the value, - * 0 means never expire, - * a number less or equal than 60*60*24*30 means the number of seconds that the value will remain valid. - * a number greater than 60*60*24*30 means a UNIX timestamp after which the value will expire. - * @return boolean true if the value is successfully stored into cache, false otherwise - */ - public function replace($id,$value,$expire=0); - /** - * Deletes a value with the specified key from cache - * @param string the key of the value to be deleted - * @return boolean if no error happens during deletion - */ - public function delete($id); - /** - * Deletes all values from cache. - * Be careful of performing this operation if the cache is shared by multiple applications. - */ - public function flush(); -} - -interface IDependency -{ - -} - -class TTimeDependency -{ -} - -class TFileDependency -{ -} - -class TDirectoryDependency -{ -} - -?> \ No newline at end of file diff --git a/framework/Data/TDataFieldAccessor.php b/framework/Data/TDataFieldAccessor.php deleted file mode 100644 index aa6d7be7..00000000 --- a/framework/Data/TDataFieldAccessor.php +++ /dev/null @@ -1,117 +0,0 @@ - - * @link http://www.pradosoft.com/ - * @copyright Copyright © 2005 PradoSoft - * @license http://www.pradosoft.com/license/ - * @version $Revision: $ $Date: $ - * @package System.Data - */ - -/** - * TDataFieldAccessor class - * - * TDataFieldAccessor is a utility class that provides access to a field of some data. - * The accessor attempts to obtain the field value in the following order: - * - If the data is an array, then the field is treated as an array index - * and the corresponding element value is returned; - * - If the data is a TMap or TList object, then the field is treated as a key - * into the map or list, and the corresponding value is returned. - * - If the data is an object, the field is treated as a property or subproperty - * defined with getter methods. For example, if the object has a method called - * getMyValue(), then field 'MyValue' will retrive the result of this method call. - * If getMyValue() returns an object which contains a method getMySubValue(), - * then field 'MyValue.MySubValue' will return that method call result. - * - * @author Qiang Xue - * @version $Revision: $ $Date: $ - * @package System.Data - * @since 3.0 - */ -class TDataFieldAccessor -{ - /** - * Evaluates the data value at the specified field. - * - If the data is an array, then the field is treated as an array index - * and the corresponding element value is returned; - * - If the data is a TMap or TList object, then the field is treated as a key - * into the map or list, and the corresponding value is returned. - * - If the data is an object, the field is treated as a property or subproperty - * defined with getter methods. For example, if the object has a method called - * getMyValue(), then field 'MyValue' will retrive the result of this method call. - * If getMyValue() returns an object which contains a method getMySubValue(), - * then field 'MyValue.MySubValue' will return that method call result. - * @param mixed data containing the field value, can be an array, TMap, TList or object. - * @param mixed field value - * @return mixed value at the specified field - * @throw TInvalidDataValueException if field or data is invalid - */ - public static function getDataFieldValue($data,$field) - { - if(Prado::getApplication()->getMode()===TApplication::STATE_PERFORMANCE) - { - if(is_array($data) || ($data instanceof ArrayAccess)) - return $data[$field]; - else if(is_object($data)) - { - if(strpos($field,'.')===false) // simple field - { - if(property_exists($data,$field)) - return $data->{$field}; - else - return call_user_func(array($data,'get'.$field)); - } - else // field in the format of xxx.yyy.zzz - { - $object=$data; - foreach(explode('.',$field) as $f) - $object=call_user_func(array($object,'get'.$f)); - return $object; - } - } - else - throw new TInvalidDataValueException('datafieldaccessor_data_invalid',$field); - } - else - { - if(is_array($data) || ($data instanceof ArrayAccess)) - { - if(isset($data[$field]) || $data[$field]===null) - return $data[$field]; - else - throw new TInvalidDataValueException('datafieldaccessor_datafield_invalid',$field); - } - else if(is_object($data)) - { - if(strpos($field,'.')===false) // simple field - { - if(property_exists($data,$field)) - return $data->{$field}; - else if(is_callable(array($data,'get'.$field))) - return call_user_func(array($data,'get'.$field)); - else - throw new TInvalidDataValueException('datafieldaccessor_datafield_invalid',$field); - } - else // field in the format of xxx.yyy.zzz - { - $object=$data; - foreach(explode('.',$field) as $f) - { - $getter='get'.$f; - if(is_callable(array($object,$getter))) - $object=call_user_func(array($object,$getter)); - else - throw new TInvalidDataValueException('datafieldaccessor_datafield_invalid',$field); - } - return $object; - } - } - else - throw new TInvalidDataValueException('datafieldaccessor_data_invalid',$field); - } - } -} - -?> \ No newline at end of file diff --git a/framework/Data/TMemCache.php b/framework/Data/TMemCache.php deleted file mode 100644 index c71de1de..00000000 --- a/framework/Data/TMemCache.php +++ /dev/null @@ -1,256 +0,0 @@ - - * @link http://www.pradosoft.com/ - * @copyright Copyright © 2005 PradoSoft - * @license http://www.pradosoft.com/license/ - * @version $Revision: $ $Date: $ - * @package System.Data - */ - -/** - * TMemCache class - * - * TMemCache implements a cache application module based on {@link http://www.danga.com/memcached/ memcached}. - * - * TMemCache can be configured with the Host and Port properties, which - * specify the host and port of the memcache server to be used. - * By default, they take the value 'localhost' and 11211, respectively. - * These properties must be set before {@link init} is invoked. - * - * The following basic cache operations are implemented: - * - {@link get} : retrieve the value with a key (if any) from cache - * - {@link set} : store the value with a key into cache - * - {@link add} : store the value only if cache does not have this key - * - {@link replace} : store the value only if cache has this key - * - {@link delete} : delete the value with the specified key from cache - * - {@link flush} : delete all values from cache - * - * Each value is associated with an expiration time. The {@link get} operation - * ensures that any expired value will not be returned. The expiration time can - * be specified by the number of seconds (maximum 60*60*24*30) - * or a UNIX timestamp. A expiration time 0 represents never expire. - * - * By definition, cache does not ensure the existence of a value - * even if it never expires. Cache is not meant to be an persistent storage. - * - * Also note, there is no security measure to protected data in memcache. - * All data in memcache can be accessed by any process running in the system. - * - * To use this module, the memcache PHP extension must be loaded. - * - * Some usage examples of TMemCache are as follows, - * - * $cache=new TMemCache; // TMemCache may also be loaded as a Prado application module - * $cache->init(null); - * $cache->add('object',$object); - * $object2=$cache->get('object'); - * - * - * If loaded, TMemCache will register itself with {@link TApplication} as the - * cache module. It can be accessed via {@link TApplication::getCache()}. - * - * TMemCache may be configured in application configuration file as follows - * - * where {@link getHost Host} and {@link getPort Port} are configurable properties - * of TMemCache. - * - * @author Qiang Xue - * @version $Revision: $ $Date: $ - * @package System.Data - * @since 3.0 - */ -class TMemCache extends TModule implements ICache -{ - /** - * @var boolean if the module is initialized - */ - private $_initialized=false; - /** - * @var Memcache the Memcache instance - */ - private $_cache=null; - /** - * @var string a unique prefix used to identify this cache instance from the others - */ - private $_prefix=null; - /** - * @var string host name of the memcache server - */ - private $_host='localhost'; - /** - * @var integer the port number of the memcache server - */ - private $_port=11211; - - /** - * Destructor. - * Disconnect the memcache server. - */ - public function __destruct() - { - if($this->_cache!==null) - $this->_cache->close(); - } - - /** - * Initializes this module. - * This method is required by the IModule interface. It makes sure that - * UniquePrefix has been set, creates a Memcache instance and connects - * to the memcache server. - * @param TApplication Prado application, can be null - * @param TXmlElement configuration for this module, can be null - * @throws TConfigurationException if memcache extension is not installed or memcache sever connection fails - */ - public function init($config) - { - if(!extension_loaded('memcache')) - throw new TConfigurationException('memcache_extension_required'); - $this->_cache=new Memcache; - if($this->_cache->connect($this->_host,$this->_port)===false) - throw new TConfigurationException('memcache_connection_failed',$this->_host,$this->_port); - $application=$this->getApplication(); - $this->_prefix=$application->getUniqueID(); - $application->setCache($this); - $this->_initialized=true; - } - - /** - * @return string host name of the memcache server - */ - public function getHost() - { - return $this->_host; - } - - /** - * @param string host name of the memcache server - * @throws TInvalidOperationException if the module is already initialized - */ - public function setHost($value) - { - if($this->_initialized) - throw new TInvalidOperationException('memcache_host_unchangeable'); - else - $this->_host=$value; - } - - /** - * @return integer port number of the memcache server - */ - public function getPort() - { - return $this->_port; - } - - /** - * @param integer port number of the memcache server - * @throws TInvalidOperationException if the module is already initialized - */ - public function setPort($value) - { - if($this->_initialized) - throw new TInvalidOperationException('memcache_port_unchangeable'); - else - $this->_port=TPropertyValue::ensureInteger($value); - } - - /** - * Retrieves a value from cache with a specified key. - * @return mixed the value stored in cache, false if the value is not in the cache or expired. - */ - public function get($key) - { - return $this->_cache->get($this->generateUniqueKey($key)); - } - - /** - * Stores a value identified by a key into cache. - * If the cache already contains such a key, the existing value and - * expiration time will be replaced with the new ones. - * - * Note, avoid using this method whenever possible. Database insertion is - * very expensive. Try using {@link add} instead, which will not store the value - * if the key is already in cache. - * - * @param string the key identifying the value to be cached - * @param mixed the value to be cached - * @param integer the expiration time of the value, - * 0 means never expire, - * a number less or equal than 60*60*24*30 means the number of seconds that the value will remain valid. - * a number greater than 60*60*24*30 means a UNIX timestamp after which the value will expire. - * @return boolean true if the value is successfully stored into cache, false otherwise - */ - public function set($key,$value,$expire=0) - { - return $this->_cache->set($this->generateUniqueKey($key),$value,0,$expire); - } - - /** - * Stores a value identified by a key into cache if the cache does not contain this key. - * Nothing will be done if the cache already contains the key. - * @param string the key identifying the value to be cached - * @param mixed the value to be cached - * @param integer the expiration time of the value, - * 0 means never expire, - * a number less or equal than 60*60*24*30 means the number of seconds that the value will remain valid. - * a number greater than 60*60*24*30 means a UNIX timestamp after which the value will expire. - * @return boolean true if the value is successfully stored into cache, false otherwise - */ - public function add($key,$value,$expiry=0) - { - return $this->_cache->add($this->generateUniqueKey($key),$value,0,$expiry); - } - - /** - * Stores a value identified by a key into cache only if the cache contains this key. - * The existing value and expiration time will be overwritten with the new ones. - * @param string the key identifying the value to be cached - * @param mixed the value to be cached - * @param integer the expiration time of the value, - * 0 means never expire, - * a number less or equal than 60*60*24*30 means the number of seconds that the value will remain valid. - * a number greater than 60*60*24*30 means a UNIX timestamp after which the value will expire. - * @return boolean true if the value is successfully stored into cache, false otherwise - */ - public function replace($key,$value,$expiry=0) - { - return $this->_cache->replace($this->generateUniqueKey($key),$value,0,$expiry); - } - - /** - * Deletes a value with the specified key from cache - * @param string the key of the value to be deleted - * @return boolean if no error happens during deletion - */ - public function delete($key) - { - return $this->_cache->delete($this->generateUniqueKey($key)); - } - - /** - * Deletes all values from cache. - * Be careful of performing this operation if the cache is shared by multiple applications. - */ - public function flush() - { - return $this->_cache->flush(); - } - - /** - * Generates a unique key based on a given user key. - * This method generates a unique key with the memcache. - * The key is made unique by prefixing with a unique string that is supposed - * to be unique among applications using the same memcache. - * @param string user key - * @param string a unique key - */ - protected function generateUniqueKey($key) - { - return md5($this->_prefix.$key); - } -} - -?> \ No newline at end of file diff --git a/framework/Data/TSimpleDateFormatter.php b/framework/Data/TSimpleDateFormatter.php deleted file mode 100644 index ec69a045..00000000 --- a/framework/Data/TSimpleDateFormatter.php +++ /dev/null @@ -1,354 +0,0 @@ - - * @link http://www.pradosoft.com/ - * @copyright Copyright © 2006 PradoSoft - * @license http://www.pradosoft.com/license/ - * @version $Revision: $ $Date: $ - * @package System.Data - */ - -/** - * TSimpleDateFormatter class. - * - * Formats and parses dates using the SimpleDateFormat pattern. - * This pattern is compatible with the I18N and java's SimpleDateFormatter. - * - * Pattern | Description - * ---------------------------------------------------- - * d | Day of month 1 to 31, no padding - * dd | Day of monath 01 to 31, zero leading - * M | Month digit 1 to 12, no padding - * MM | Month digit 01 to 12, zero leading - * yy | 2 year digit, e.g., 96, 05 - * yyyy | 4 year digit, e.g., 2005 - * ---------------------------------------------------- - * - * - * Usage example, to format a date - * - * $formatter = new TSimpleDateFormatter("dd/MM/yyy"); - * echo $formatter->format(time()); - * - * - * To parse the date string into a date timestamp. - * - * $formatter = new TSimpleDateFormatter("d-M-yyy"); - * echo $formatter->parse("24-6-2005"); - * - * - * @author Wei Zhuo - * @version $Revision: $ $Date: $ - * @package System.Data - * @since 3.0 - */ -class TSimpleDateFormatter -{ - /** - * Formatting pattern. - * @var string - */ - private $pattern; - - /** - * Charset, default is 'UTF-8' - * @var string - */ - private $charset = 'UTF-8'; - - /** - * Constructor, create a new date time formatter. - * @param string formatting pattern. - * @param string pattern and value charset - */ - public function __construct($pattern, $charset='UTF-8') - { - $this->setPattern($pattern); - $this->setCharset($charset); - } - - /** - * @return string formatting pattern. - */ - public function getPattern() - { - return $this->pattern; - } - - /** - * @param string formatting pattern. - */ - public function setPattern($pattern) - { - $this->pattern = $pattern; - } - - /** - * @return string formatting charset. - */ - public function getCharset() - { - return $this->charset; - } - - /** - * @param string formatting charset. - */ - public function setCharset($charset) - { - $this->charset = $charset; - } - - /** - * Format the date according to the pattern. - * @param string|int the date to format, either integer or a string readable by strtotime. - * @return string formatted date. - */ - public function format($value) - { - $date = $this->getDate($value); - $bits['yyyy'] = $date['year']; - $bits['yy'] = substr("{$date['year']}", -2); - - $bits['MM'] = str_pad("{$date['mon']}", 2, '0', STR_PAD_LEFT); - $bits['M'] = $date['mon']; - - $bits['dd'] = str_pad("{$date['mday']}", 2, '0', STR_PAD_LEFT); - $bits['d'] = $date['mday']; - - return str_replace(array_keys($bits), $bits, $this->pattern); - } - - public function getMonthPattern() - { - if(is_int(strpos($this->pattern, 'MMMM'))) - return 'MMMM'; - if(is_int(strpos($this->pattern, 'MMM'))) - return 'MMM'; - if(is_int(strpos($this->pattern, 'MM'))) - return 'MM'; - if(is_int(strpos($this->pattern, 'M'))) - return 'M'; - return false; - } - - public function getDayPattern() - { - if(is_int(strpos($this->pattern, 'dd'))) - return 'dd'; - if(is_int(strpos($this->pattern, 'd'))) - return 'd'; - return false; - } - - public function getYearPattern() - { - if(is_int(strpos($this->pattern, 'yyyy'))) - return 'yyyy'; - if(is_int(strpos($this->pattern, 'yy'))) - return 'yy'; - return false; - } - - public function getDayMonthYearOrdering() - { - $ordering = array(); - if(is_int($day= strpos($this->pattern, 'd'))) - $ordering['day'] = $day; - if(is_int($month= strpos($this->pattern, 'M'))) - $ordering['month'] = $month; - if(is_int($year= strpos($this->pattern, 'yy'))) - $ordering['year'] = $year; - asort($ordering); - return array_keys($ordering); - } - - /** - * Gets the time stamp from string or integer. - * @param string|int date to parse - * @return int parsed date time stamp - */ - private function getDate($value) - { - if(is_int($value)) - return @getdate($value); - $date = @strtotime($value); - if($date < 0) - throw new TInvalidDataValueException('invalid_date', $value); - return @getdate($date); - } - - /** - * @return boolean true if the given value matches with the date pattern. - */ - public function isValidDate($value) - { - return !is_null($this->parse($value, false)); - } - - /** - * Parse the string according to the pattern. - * @param string date string to parse - * @return int date time stamp - * @throws TInvalidDataValueException if date string is malformed. - */ - public function parse($value,$defaultToCurrentTime=true) - { - if(!is_string($value)) - throw new TInvalidDataValueException('date_to_parse_must_be_string', $value); - - if(empty($this->pattern)) return time(); - - $date = $this->getDate(time()); - - if($this->length(trim($value)) < 1) - return $defaultToCurrentTime ? $date : null; - - $pattern = $this->pattern; - - $i_val = 0; - $i_format = 0; - $pattern_length = $this->length($pattern); - $c = ''; - $token=''; - $x=null; $y=null; - - - if($defaultToCurrentTime) - { - $year = "{$date['year']}"; - $month = $date['mon']; - $day = $date['mday']; - } - else - { - $year = null; - $month = null; - $day = null; - } - - while ($i_format < $pattern_length) - { - $c = $this->charAt($pattern,$i_format); - $token=''; - while ($this->charEqual($pattern, $i_format, $c) - && ($i_format < $pattern_length)) - { - $token .= $this->charAt($pattern, $i_format++); - } - - if ($token=='yyyy' || $token=='yy' || $token=='y') - { - if ($token=='yyyy') { $x=4;$y=4; } - if ($token=='yy') { $x=2;$y=2; } - if ($token=='y') { $x=2;$y=4; } - $year = $this->getInteger($value,$i_val,$x,$y); - if(is_null($year)) - throw new TInvalidDataValueException('Invalid year', $value); - $i_val += strlen($year); - if(strlen($year) == 2) - { - $iYear = intval($year); - if($iYear > 70) - $year = $iYear + 1900; - else - $year = $iYear + 2000; - } - $year = intval($year); - } - elseif($token=='MM' || $token=='M') - { - $month=$this->getInteger($value,$i_val, - $this->length($token),2); - $iMonth = intval($month); - if(is_null($month) || $iMonth < 1 || $iMonth > 12 ) - throw new TInvalidDataValueException('Invalid month', $value); - $i_val += strlen($month); - $month = $iMonth; - } - elseif ($token=='dd' || $token=='d') - { - $day = $this->getInteger($value,$i_val, - $this->length($token), 2); - $iDay = intval($day); - if(is_null($day) || $iDay < 1 || $iDay >31) - throw new TInvalidDataValueException('Invalid day', $value); - $i_val += strlen($day); - $day = $iDay; - } - else - { - if($this->substring($value, $i_val, $this->length($token)) != $token) - throw new TInvalidDataValueException("Subpattern '{$this->pattern}' mismatch", $value); - else - $i_val += $this->length($token); - } - } - if ($i_val != $this->length($value)) - throw new TInvalidDataValueException("Pattern '{$this->pattern}' mismatch", $value); - - if(!$defaultToCurrentTime && (is_null($month) || is_null($day) || is_null($year))) - return null; - else - return $this->getDate(@mktime(0, 0, 0, $month, $day, $year)); - } - - /** - * Calculate the length of a string, may be consider iconv_strlen? - */ - private function length($string) - { - //use iconv_strlen or just strlen? - return strlen($string); - } - - /** - * Get the char at a position. - */ - private function charAt($string, $pos) - { - return $this->substring($string, $pos, 1); - } - - /** - * Gets a portion of a string, uses iconv_substr. - */ - private function substring($string, $start, $length) - { - return iconv_substr($string, $start, $length); - } - - /** - * Returns true if char at position equals a particular char. - */ - private function charEqual($string, $pos, $char) - { - return $this->charAt($string, $pos) == $char; - } - - /** - * Gets integer from part of a string, allows integers of any length. - * @param string string to retrieve the integer from. - * @param int starting position - * @param int minimum integer length - * @param int maximum integer length - * @return string integer portition of the string, null otherwise - */ - private function getInteger($str,$i,$minlength,$maxlength) - { - //match for digits backwards - for ($x = $maxlength; $x >= $minlength; $x--) - { - $token= $this->substring($str, $i,$x); - if ($this->length($token) < $minlength) - return null; - if (preg_match('/^\d+$/', $token)) - return $token; - } - return null; - } -} - -?> \ No newline at end of file diff --git a/framework/Data/TSqliteCache.php b/framework/Data/TSqliteCache.php deleted file mode 100644 index 1654d577..00000000 --- a/framework/Data/TSqliteCache.php +++ /dev/null @@ -1,265 +0,0 @@ - - * @link http://www.pradosoft.com/ - * @copyright Copyright © 2005 PradoSoft - * @license http://www.pradosoft.com/license/ - * @version $Revision: $ $Date: $ - * @package System.Data - */ - -/** - * TSqliteCache class - * - * TSqliteCache implements a cache application module based on SQLite database. - * - * The database file is specified by the {@link setDbFile DbFile} property. - * If not set, the database file will be created under the system state path. - * If the specified database file does not exist, it will be created automatically. - * Make sure the directory containing the specified DB file and the file itself is - * writable by the Web server process. - * - * The following basic cache operations are implemented: - * - {@link get} : retrieve the value with a key (if any) from cache - * - {@link set} : store the value with a key into cache - * - {@link add} : store the value only if cache does not have this key - * - {@link replace} : store the value only if cache has this key - * - {@link delete} : delete the value with the specified key from cache - * - {@link flush} : delete all values from cache - * - * Each value is associated with an expiration time. The {@link get} operation - * ensures that any expired value will not be returned. The expiration time can - * be specified by the number of seconds (maximum 60*60*24*30) - * or a UNIX timestamp. A expiration time 0 represents never expire. - * - * By definition, cache does not ensure the existence of a value - * even if it never expires. Cache is not meant to be an persistent storage. - * - * Do not use the same database file for multiple applications using TSqliteCache. - * Also note, cache is shared by all user sessions of an application. - * - * To use this module, the sqlite PHP extension must be loaded. Sqlite extension - * is no longer loaded by default since PHP 5.1. - * - * Some usage examples of TSqliteCache are as follows, - * - * $cache=new TSqliteCache; // TSqliteCache may also be loaded as a Prado application module - * $cache->setDbFile($dbFilePath); - * $cache->init(null); - * $cache->add('object',$object); - * $object2=$cache->get('object'); - * - * - * If loaded, TSqliteCache will register itself with {@link TApplication} as the - * cache module. It can be accessed via {@link TApplication::getCache()}. - * - * TMemCache may be configured in application configuration file as follows - * - * where {@link getDbFile DbFile} is a property specifying the location of the - * SQLite DB file (in the namespace format). - * - * @author Qiang Xue - * @version $Revision: $ $Date: $ - * @package System.Data - * @since 3.0 - */ -class TSqliteCache extends TModule implements ICache -{ - /** - * name of the table storing cache data - */ - const CACHE_TABLE='cache'; - /** - * extension of the db file name - */ - const DB_FILE_EXT='.db'; - /** - * maximum number of seconds specified as expire - */ - const EXPIRE_LIMIT=2592000; // 30 days - - /** - * @var boolean if the module has been initialized - */ - private $_initialized=false; - /** - * @var SQLiteDatabase the sqlite database instance - */ - private $_db=null; - /** - * @var string the database file name - */ - private $_file=null; - - /** - * Destructor. - * Disconnect the db connection. - */ - public function __destruct() - { - $this->_db=null; - } - - /** - * Initializes this module. - * This method is required by the IModule interface. It checks if the DbFile - * property is set, and creates a SQLiteDatabase instance for it. - * The database or the cache table does not exist, they will be created. - * Expired values are also deleted. - * @param TXmlElement configuration for this module, can be null - * @throws TConfigurationException if sqlite extension is not installed, - * DbFile is set invalid, or any error happens during creating database or cache table. - */ - public function init($config) - { - if(!function_exists('sqlite_open')) - throw new TConfigurationException('sqlitecache_extension_required'); - if($this->_file===null) - $this->_file=$this->getApplication()->getRuntimePath().'/sqlite.cache'; - $error=''; - if(($this->_db=new SQLiteDatabase($this->_file,0666,$error))===false) - throw new TConfigurationException('sqlitecache_connection_failed',$error); - if(($res=$this->_db->query('SELECT * FROM sqlite_master WHERE tbl_name=\''.self::CACHE_TABLE.'\' AND type=\'table\''))!=false) - { - if($res->numRows()===0) - { - if($this->_db->query('CREATE TABLE '.self::CACHE_TABLE.' (key CHAR(128) PRIMARY KEY, value BLOB, serialized INT, expire INT)')===false) - throw new TConfigurationException('sqlitecache_table_creation_failed',sqlite_error_string(sqlite_last_error())); - } - } - else - throw new TConfigurationException('sqlitecache_table_creation_failed',sqlite_error_string(sqlite_last_error())); - $this->_db->query('DELETE FROM '.self::CACHE_TABLE.' WHERE expire<>0 AND expire<'.time()); - $this->_initialized=true; - $this->getApplication()->setCache($this); - } - - /** - * @return string database file path (in namespace form) - */ - public function getDbFile() - { - return $this->_file; - } - - /** - * @param string database file path (in namespace form) - * @throws TInvalidOperationException if the module is already initialized - * @throws TConfigurationException if the file is not in proper namespace format - */ - public function setDbFile($value) - { - if($this->_initialized) - throw new TInvalidOperationException('sqlitecache_dbfile_unchangeable'); - else if(($this->_file=Prado::getPathOfNamespace($value,self::DB_FILE_EXT))===null) - throw new TConfigurationException('sqlitecache_dbfile_invalid',$value); - } - - /** - * Retrieves a value from cache with a specified key. - * @return mixed the value stored in cache, false if the value is not in the cache or expired. - */ - public function get($key) - { - $sql='SELECT serialized,value FROM '.self::CACHE_TABLE.' WHERE key=\''.md5($key).'\' AND (expire=0 OR expire>'.time().')'; - if(($ret=$this->_db->query($sql))!=false && ($row=$ret->fetch(SQLITE_ASSOC))!==false) - return $row['serialized']?Prado::unserialize($row['value']):$row['value']; - else - return false; - } - - /** - * Stores a value identified by a key into cache. - * If the cache already contains such a key, the existing value and - * expiration time will be replaced with the new ones. - * - * Note, avoid using this method whenever possible. Database insertion is - * very expensive. Try using {@link add} instead, which will not store the value - * if the key is already in cache. - * - * @param string the key identifying the value to be cached - * @param mixed the value to be cached - * @param integer the expiration time of the value, - * 0 means never expire, - * a number less or equal than 60*60*24*30 means the number of seconds that the value will remain valid. - * a number greater than 60*60*24*30 means a UNIX timestamp after which the value will expire. - * @return boolean true if the value is successfully stored into cache, false otherwise - */ - public function set($key,$value,$expire=0) - { - $serialized=is_string($value)?0:1; - $value1=sqlite_escape_string($serialized?Prado::serialize($value):$value); - if($expire && $expire<=self::EXPIRE_LIMIT) - $expire=time()+$expire; - $sql='REPLACE INTO '.self::CACHE_TABLE.' VALUES(\''.md5($key).'\',\''.$value1.'\','.$serialized.','.$expire.')'; - return $this->_db->query($sql)!==false; - } - - /** - * Stores a value identified by a key into cache if the cache does not contain this key. - * Nothing will be done if the cache already contains the key. - * @param string the key identifying the value to be cached - * @param mixed the value to be cached - * @param integer the expiration time of the value, - * 0 means never expire, - * a number less or equal than 60*60*24*30 means the number of seconds that the value will remain valid. - * a number greater than 60*60*24*30 means a UNIX timestamp after which the value will expire. - * @return boolean true if the value is successfully stored into cache, false otherwise - */ - public function add($key,$value,$expire=0) - { - $serialized=is_string($value)?0:1; - $value1=sqlite_escape_string($serialized?Prado::serialize($value):$value); - if($expire && $expire<=self::EXPIRE_LIMIT) - $expire=time()+$expire; - $sql='INSERT INTO '.self::CACHE_TABLE.' VALUES(\''.md5($key).'\',\''.$value1.'\','.$serialized.','.$expire.')'; - return @$this->_db->query($sql)!==false; - } - - /** - * Stores a value identified by a key into cache only if the cache contains this key. - * The existing value and expiration time will be overwritten with the new ones. - * @param string the key identifying the value to be cached - * @param mixed the value to be cached - * @param integer the expiration time of the value, - * 0 means never expire, - * a number less or equal than 60*60*24*30 means the number of seconds that the value will remain valid. - * a number greater than 60*60*24*30 means a UNIX timestamp after which the value will expire. - * @return boolean true if the value is successfully stored into cache, false otherwise - */ - public function replace($key,$value,$expire=0) - { - $serialized=is_string($value)?0:1; - $value1=sqlite_escape_string($serialized?Prado::serialize($value):$value); - if($expire && $expire<=self::EXPIRE_LIMIT) - $expire=time()+$expire; - $sql='UPDATE '.self::CACHE_TABLE.' SET value=\''.$value1.'\', serialized='.$serialized.',expire='.$expire.' WHERE key=\''.md5($key).'\''; - $this->_db->query($sql); - $ret=$this->_db->query('SELECT serialized FROM '.self::CACHE_TABLE.' WHERE key=\''.md5($key).'\''); - return ($ret!=false && $ret->numRows()>0); - } - - /** - * Deletes a value with the specified key from cache - * @param string the key of the value to be deleted - * @return boolean if no error happens during deletion - */ - public function delete($key) - { - $sql='DELETE FROM '.self::CACHE_TABLE.' WHERE key=\''.md5($key).'\''; - return $this->_db->query($sql)!==false; - } - - /** - * Deletes all values from cache. - * Be careful of performing this operation if the cache is shared by multiple applications. - */ - public function flush() - { - return $this->_db->query('DELETE FROM '.self::CACHE_TABLE)!==false; - } -} - -?> \ No newline at end of file diff --git a/framework/Data/TTarFileExtractor.php b/framework/Data/TTarFileExtractor.php deleted file mode 100644 index 12749d16..00000000 --- a/framework/Data/TTarFileExtractor.php +++ /dev/null @@ -1,574 +0,0 @@ - - * @link http://www.pradosoft.com/ - * @copyright Copyright © 2005 PradoSoft - * @license http://www.pradosoft.com/license/ - * @version $Revision: $ $Date: $ - * @package System.Data - */ - -/* vim: set ts=4 sw=4: */ -// +----------------------------------------------------------------------+ -// | PHP Version 4 | -// +----------------------------------------------------------------------+ -// | Copyright (c) 1997-2003 The PHP Group | -// +----------------------------------------------------------------------+ -// | This source file is subject to version 3.0 of the PHP license, | -// | that is bundled with this package in the file LICENSE, and is | -// | available through the world-wide-web at the following url: | -// | http://www.php.net/license/3_0.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. | -// +----------------------------------------------------------------------+ -// | Author: Vincent Blavet | -// +----------------------------------------------------------------------+ -// -// $Id: Tar.php,v 1.29 2005/03/17 21:02:31 vblavet Exp $ - -/** - * TTarFileExtractor class - * - * @author Vincent Blavet - * @version $Revision: $ $Date: $ - * @package System.Data - * @since 3.0 - */ -class TTarFileExtractor -{ - /** - * @var string Name of the Tar - */ - private $_tarname=''; - - /** - * @var file descriptor - */ - private $_file=0; - - /** - * @var string Local Tar name of a remote Tar (http:// or ftp://) - */ - private $_temp_tarname=''; - - /** - * Archive_Tar Class constructor. This flavour of the constructor only - * declare a new Archive_Tar object, identifying it by the name of the - * tar file. - * - * @param string $p_tarname The name of the tar archive to create - * @access public - */ - public function __construct($p_tarname) - { - $this->_tarname = $p_tarname; - } - - public function __destruct() - { - $this->_close(); - // ----- Look for a local copy to delete - if ($this->_temp_tarname != '') - @unlink($this->_temp_tarname); - } - - public function extract($p_path='') - { - return $this->extractModify($p_path, ''); - } - - /** - * This method extract all the content of the archive in the directory - * indicated by $p_path. When relevant the memorized path of the - * files/dir can be modified by removing the $p_remove_path path at the - * beginning of the file/dir path. - * While extracting a file, if the directory path does not exists it is - * created. - * While extracting a file, if the file already exists it is replaced - * without looking for last modification date. - * While extracting a file, if the file already exists and is write - * protected, the extraction is aborted. - * While extracting a file, if a directory with the same name already - * exists, the extraction is aborted. - * While extracting a directory, if a file with the same name already - * exists, the extraction is aborted. - * While extracting a file/directory if the destination directory exist - * and is write protected, or does not exist but can not be created, - * the extraction is aborted. - * If after extraction an extracted file does not show the correct - * stored file size, the extraction is aborted. - * When the extraction is aborted, a PEAR error text is set and false - * is returned. However the result can be a partial extraction that may - * need to be manually cleaned. - * - * @param string $p_path The path of the directory where the - * files/dir need to by extracted. - * @param string $p_remove_path Part of the memorized path that can be - * removed if present at the beginning of - * the file/dir path. - * @return boolean true on success, false on error. - * @access public - */ - protected function extractModify($p_path, $p_remove_path) - { - $v_result = true; - $v_list_detail = array(); - - if ($v_result = $this->_openRead()) { - $v_result = $this->_extractList($p_path, $v_list_detail, - "complete", 0, $p_remove_path); - $this->_close(); - } - - return $v_result; - } - - protected function _error($p_message) - { - throw new Exception($p_message); - } - - private function _isArchive($p_filename=null) - { - if ($p_filename == null) { - $p_filename = $this->_tarname; - } - clearstatcache(); - return @is_file($p_filename); - } - - private function _openRead() - { - if (strtolower(substr($this->_tarname, 0, 7)) == 'http://') { - - // ----- Look if a local copy need to be done - if ($this->_temp_tarname == '') { - $this->_temp_tarname = uniqid('tar').'.tmp'; - if (!$v_file_from = @fopen($this->_tarname, 'rb')) { - $this->_error('Unable to open in read mode \'' - .$this->_tarname.'\''); - $this->_temp_tarname = ''; - return false; - } - if (!$v_file_to = @fopen($this->_temp_tarname, 'wb')) { - $this->_error('Unable to open in write mode \'' - .$this->_temp_tarname.'\''); - $this->_temp_tarname = ''; - return false; - } - while ($v_data = @fread($v_file_from, 1024)) - @fwrite($v_file_to, $v_data); - @fclose($v_file_from); - @fclose($v_file_to); - } - - // ----- File to open if the local copy - $v_filename = $this->_temp_tarname; - - } else - // ----- File to open if the normal Tar file - $v_filename = $this->_tarname; - - $this->_file = @fopen($v_filename, "rb"); - - if ($this->_file == 0) { - $this->_error('Unable to open in read mode \''.$v_filename.'\''); - return false; - } - - return true; - } - - private function _close() - { - //if (isset($this->_file)) { - if (is_resource($this->_file)) - { - @fclose($this->_file); - $this->_file = 0; - } - - // ----- Look if a local copy need to be erase - // Note that it might be interesting to keep the url for a time : ToDo - if ($this->_temp_tarname != '') { - @unlink($this->_temp_tarname); - $this->_temp_tarname = ''; - } - - return true; - } - - private function _cleanFile() - { - $this->_close(); - - // ----- Look for a local copy - if ($this->_temp_tarname != '') { - // ----- Remove the local copy but not the remote tarname - @unlink($this->_temp_tarname); - $this->_temp_tarname = ''; - } else { - // ----- Remove the local tarname file - @unlink($this->_tarname); - } - $this->_tarname = ''; - - return true; - } - - private function _readBlock() - { - $v_block = null; - if (is_resource($this->_file)) { - $v_block = @fread($this->_file, 512); - } - return $v_block; - } - - private function _jumpBlock($p_len=null) - { - if (is_resource($this->_file)) { - if ($p_len === null) - $p_len = 1; - - @fseek($this->_file, @ftell($this->_file)+($p_len*512)); - } - return true; - } - - private function _readHeader($v_binary_data, &$v_header) - { - if (strlen($v_binary_data)==0) { - $v_header['filename'] = ''; - return true; - } - - if (strlen($v_binary_data) != 512) { - $v_header['filename'] = ''; - $this->_error('Invalid block size : '.strlen($v_binary_data)); - return false; - } - - // ----- Calculate the checksum - $v_checksum = 0; - // ..... First part of the header - for ($i=0; $i<148; $i++) - $v_checksum+=ord(substr($v_binary_data,$i,1)); - // ..... Ignore the checksum value and replace it by ' ' (space) - for ($i=148; $i<156; $i++) - $v_checksum += ord(' '); - // ..... Last part of the header - for ($i=156; $i<512; $i++) - $v_checksum+=ord(substr($v_binary_data,$i,1)); - - $v_data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/" - ."a8checksum/a1typeflag/a100link/a6magic/a2version/" - ."a32uname/a32gname/a8devmajor/a8devminor", - $v_binary_data); - - // ----- Extract the checksum - $v_header['checksum'] = OctDec(trim($v_data['checksum'])); - if ($v_header['checksum'] != $v_checksum) { - $v_header['filename'] = ''; - - // ----- Look for last block (empty block) - if (($v_checksum == 256) && ($v_header['checksum'] == 0)) - return true; - - $this->_error('Invalid checksum for file "'.$v_data['filename'] - .'" : '.$v_checksum.' calculated, ' - .$v_header['checksum'].' expected'); - return false; - } - - // ----- Extract the properties - $v_header['filename'] = trim($v_data['filename']); - $v_header['mode'] = OctDec(trim($v_data['mode'])); - $v_header['uid'] = OctDec(trim($v_data['uid'])); - $v_header['gid'] = OctDec(trim($v_data['gid'])); - $v_header['size'] = OctDec(trim($v_data['size'])); - $v_header['mtime'] = OctDec(trim($v_data['mtime'])); - if (($v_header['typeflag'] = $v_data['typeflag']) == "5") { - $v_header['size'] = 0; - } - return true; - } - - private function _readLongHeader(&$v_header) - { - $v_filename = ''; - $n = floor($v_header['size']/512); - for ($i=0; $i<$n; $i++) { - $v_content = $this->_readBlock(); - $v_filename .= $v_content; - } - if (($v_header['size'] % 512) != 0) { - $v_content = $this->_readBlock(); - $v_filename .= $v_content; - } - - // ----- Read the next header - $v_binary_data = $this->_readBlock(); - - if (!$this->_readHeader($v_binary_data, $v_header)) - return false; - - $v_header['filename'] = $v_filename; - - return true; - } - - protected function _extractList($p_path, &$p_list_detail, $p_mode, - $p_file_list, $p_remove_path) - { - $v_result=true; - $v_nb = 0; - $v_extract_all = true; - $v_listing = false; - - $p_path = $this->_translateWinPath($p_path, false); - if ($p_path == '' || (substr($p_path, 0, 1) != '/' - && substr($p_path, 0, 3) != "../" && !strpos($p_path, ':'))) { - $p_path = "./".$p_path; - } - $p_remove_path = $this->_translateWinPath($p_remove_path); - - // ----- Look for path to remove format (should end by /) - if (($p_remove_path != '') && (substr($p_remove_path, -1) != '/')) - $p_remove_path .= '/'; - $p_remove_path_size = strlen($p_remove_path); - - switch ($p_mode) { - case "complete" : - $v_extract_all = true; - $v_listing = false; - break; - case "partial" : - $v_extract_all = false; - $v_listing = false; - break; - case "list" : - $v_extract_all = false; - $v_listing = true; - break; - default : - $this->_error('Invalid extract mode ('.$p_mode.')'); - return false; - } - - clearstatcache(); - - while (strlen($v_binary_data = $this->_readBlock()) != 0) - { - $v_extract_file = false; - $v_extraction_stopped = 0; - - if (!$this->_readHeader($v_binary_data, $v_header)) - return false; - - if ($v_header['filename'] == '') { - continue; - } - - // ----- Look for long filename - if ($v_header['typeflag'] == 'L') { - if (!$this->_readLongHeader($v_header)) - return false; - } - - if ((!$v_extract_all) && (is_array($p_file_list))) { - // ----- By default no unzip if the file is not found - $v_extract_file = false; - - for ($i=0; $i strlen($p_file_list[$i])) - && (substr($v_header['filename'], 0, strlen($p_file_list[$i])) - == $p_file_list[$i])) { - $v_extract_file = true; - break; - } - } - - // ----- It is a file, so compare the file names - elseif ($p_file_list[$i] == $v_header['filename']) { - $v_extract_file = true; - break; - } - } - } else { - $v_extract_file = true; - } - - // ----- Look if this file need to be extracted - if (($v_extract_file) && (!$v_listing)) - { - if (($p_remove_path != '') - && (substr($v_header['filename'], 0, $p_remove_path_size) - == $p_remove_path)) - $v_header['filename'] = substr($v_header['filename'], - $p_remove_path_size); - if (($p_path != './') && ($p_path != '/')) { - while (substr($p_path, -1) == '/') - $p_path = substr($p_path, 0, strlen($p_path)-1); - - if (substr($v_header['filename'], 0, 1) == '/') - $v_header['filename'] = $p_path.$v_header['filename']; - else - $v_header['filename'] = $p_path.'/'.$v_header['filename']; - } - if (file_exists($v_header['filename'])) { - if ( (@is_dir($v_header['filename'])) - && ($v_header['typeflag'] == '')) { - $this->_error('File '.$v_header['filename'] - .' already exists as a directory'); - return false; - } - if ( ($this->_isArchive($v_header['filename'])) - && ($v_header['typeflag'] == "5")) { - $this->_error('Directory '.$v_header['filename'] - .' already exists as a file'); - return false; - } - if (!is_writeable($v_header['filename'])) { - $this->_error('File '.$v_header['filename'] - .' already exists and is write protected'); - return false; - } - if (filemtime($v_header['filename']) > $v_header['mtime']) { - // To be completed : An error or silent no replace ? - } - } - - // ----- Check the directory availability and create it if necessary - elseif (($v_result - = $this->_dirCheck(($v_header['typeflag'] == "5" - ?$v_header['filename'] - :dirname($v_header['filename'])))) != 1) { - $this->_error('Unable to create path for '.$v_header['filename']); - return false; - } - - if ($v_extract_file) { - if ($v_header['typeflag'] == "5") { - if (!@file_exists($v_header['filename'])) { - if (!@mkdir($v_header['filename'], 0777)) { - $this->_error('Unable to create directory {' - .$v_header['filename'].'}'); - return false; - } - } - } else { - if (($v_dest_file = @fopen($v_header['filename'], "wb")) == 0) { - $this->_error('Error while opening {'.$v_header['filename'] - .'} in write binary mode'); - return false; - } else { - $n = floor($v_header['size']/512); - for ($i=0; $i<$n; $i++) { - $v_content = $this->_readBlock(); - fwrite($v_dest_file, $v_content, 512); - } - if (($v_header['size'] % 512) != 0) { - $v_content = $this->_readBlock(); - fwrite($v_dest_file, $v_content, ($v_header['size'] % 512)); - } - - @fclose($v_dest_file); - - // ----- Change the file mode, mtime - @touch($v_header['filename'], $v_header['mtime']); - // To be completed - //chmod($v_header[filename], DecOct($v_header[mode])); - } - - // ----- Check the file size - clearstatcache(); - if (filesize($v_header['filename']) != $v_header['size']) { - $this->_error('Extracted file '.$v_header['filename'] - .' does not have the correct file size \'' - .filesize($v_header['filename']) - .'\' ('.$v_header['size'] - .' expected). Archive may be corrupted.'); - return false; - } - } - } else { - $this->_jumpBlock(ceil(($v_header['size']/512))); - } - } else { - $this->_jumpBlock(ceil(($v_header['size']/512))); - } - - /* TBC : Seems to be unused ... - if ($this->_compress) - $v_end_of_file = @gzeof($this->_file); - else - $v_end_of_file = @feof($this->_file); - */ - - if ($v_listing || $v_extract_file || $v_extraction_stopped) { - // ----- Log extracted files - if (($v_file_dir = dirname($v_header['filename'])) - == $v_header['filename']) - $v_file_dir = ''; - if ((substr($v_header['filename'], 0, 1) == '/') && ($v_file_dir == '')) - $v_file_dir = '/'; - - $p_list_detail[$v_nb++] = $v_header; - } - } - - return true; - } - - /** - * Check if a directory exists and create it (including parent - * dirs) if not. - * - * @param string $p_dir directory to check - * - * @return bool true if the directory exists or was created - */ - protected function _dirCheck($p_dir) - { - if ((@is_dir($p_dir)) || ($p_dir == '')) - return true; - - $p_parent_dir = dirname($p_dir); - - if (($p_parent_dir != $p_dir) && - ($p_parent_dir != '') && - (!$this->_dirCheck($p_parent_dir))) - return false; - - if (!@mkdir($p_dir, 0777)) { - $this->_error("Unable to create directory '$p_dir'"); - return false; - } - - return true; - } - - protected function _translateWinPath($p_path, $p_remove_disk_letter=true) - { - if (substr(PHP_OS, 0, 3) == 'WIN') { - // ----- Look for potential disk letter - if ( ($p_remove_disk_letter) - && (($v_position = strpos($p_path, ':')) != false)) { - $p_path = substr($p_path, $v_position+1); - } - // ----- Change potential windows directory separator - if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0,1) == '\\')) { - $p_path = strtr($p_path, '\\', '/'); - } - } - return $p_path; - } -} -?> diff --git a/framework/Data/TXmlDocument.php b/framework/Data/TXmlDocument.php deleted file mode 100644 index dfd65ebe..00000000 --- a/framework/Data/TXmlDocument.php +++ /dev/null @@ -1,455 +0,0 @@ - - * @link http://www.pradosoft.com/ - * @copyright Copyright © 2005 PradoSoft - * @license http://www.pradosoft.com/license/ - * @version $Revision: $ $Date: $ - * @package System.Data - */ - -/** - * TXmlElement class. - * - * TXmlElement represents an XML element node. - * You can obtain its tagname, attributes, text between the openning and closing - * tags via the TagName, Attributes, and Value properties, respectively. - * You can also retrieve its parent and child elements by Parent and Elements - * properties, respectively. - * - * TBD: xpath - * - * @author Qiang Xue - * @version $Revision: $ $Date: $ - * @package System - * @since 3.0 - */ -class TXmlElement extends TComponent -{ - /** - * @var TXmlElement parent of this element - */ - private $_parent=null; - /** - * @var string tagname of this element - */ - private $_tagName; - /** - * @var string text enclosed between openning and closing tags of this element - */ - private $_value; - /** - * @var TXmlElementList list of child elements of this element - */ - private $_elements=null; - /** - * @var TMap attributes of this element - */ - private $_attributes=null; - - /** - * Constructor. - * @param string tagname for this element - */ - public function __construct($tagName) - { - $this->setTagName($tagName); - } - - /** - * @return TXmlElement parent element of this element - */ - public function getParent() - { - return $this->_parent; - } - - /** - * @param TXmlElement parent element of this element - */ - public function setParent($parent) - { - $this->_parent=$parent; - } - - /** - * @return string tagname of this element - */ - public function getTagName() - { - return $this->_tagName; - } - - /** - * @param string tagname of this element - */ - public function setTagName($tagName) - { - $this->_tagName=$tagName; - } - - /** - * @return string text enclosed between opening and closing tag of this element - */ - public function getValue() - { - return $this->_value; - } - - /** - * @param string text enclosed between opening and closing tag of this element - */ - public function setValue($value) - { - $this->_value=$value; - } - - /** - * @return boolean true if this element has child elements - */ - public function getHasElement() - { - return $this->_elements!==null && $this->_elements->getCount()>0; - } - - /** - * @return boolean true if this element has attributes - */ - public function getHasAttribute() - { - return $this->_attributes!==null && $this->_attributes->getCount()>0; - } - - /** - * @return string the attribute specified by the name, null if no such attribute - */ - public function getAttribute($name) - { - if($this->_attributes!==null) - return $this->_attributes->itemAt($name); - else - return null; - } - - /** - * @return TXmlElementList list of child elements - */ - public function getElements() - { - if(!$this->_elements) - $this->_elements=new TXmlElementList($this); - return $this->_elements; - } - - /** - * @return TMap list of attributes - */ - public function getAttributes() - { - if(!$this->_attributes) - $this->_attributes=new TMap; - return $this->_attributes; - } - - /** - * @return TXmlElement the first child element that has the specified tagname, null if not found - */ - public function getElementByTagName($tagName) - { - if($this->_elements) - { - foreach($this->_elements as $element) - if($element->_tagName===$tagName) - return $element; - } - return null; - } - - /** - * @return TList list of all child elements that have the specified tagname - */ - public function getElementsByTagName($tagName) - { - $list=new TList; - if($this->_elements) - { - foreach($this->_elements as $element) - if($element->_tagName===$tagName) - $list->add($element); - } - return $list; - } - - /** - * @return string string representation of this element - */ - public function toString($indent=0) - { - $attr=''; - if($this->_attributes!==null) - { - foreach($this->_attributes as $name=>$value) - $attr.=" $name=\"$value\""; - } - $prefix=str_repeat(' ',$indent*4); - if($this->getHasElement()) - { - $str=$prefix."<{$this->_tagName}$attr>\n"; - foreach($this->getElements() as $element) - $str.=$element->toString($indent+1)."\n"; - $str.=$prefix."_tagName}>"; - return $str; - } - else if($this->getValue()!=='') - { - return $prefix."<{$this->_tagName}$attr>{$this->_value}_tagName}>"; - } - else - return $prefix."<{$this->_tagName}$attr />"; - } -} - -/** - * TXmlDocument class. - * - * TXmlDocument represents a DOM representation of an XML file. - * Besides all properties and methods inherited from {@link TXmlElement}, - * you can load an XML file or string by {@link loadFromFile} or {@link loadFromString}. - * You can also get the version and encoding of the XML document by - * the Version and Encoding properties. - * - * @author Qiang Xue - * @version $Revision: $ $Date: $ - * @package System - * @since 3.0 - */ -class TXmlDocument extends TXmlElement -{ - /** - * @var string version of this XML document - */ - private $_version; - /** - * @var string encoding of this XML document - */ - private $_encoding; - - /** - * Constructor. - * @param string version of this XML document - * @param string encoding of this XML document - */ - public function __construct($version='1.0',$encoding='') - { - parent::__construct(''); - $this->setversion($version); - $this->setEncoding($encoding); - } - - /** - * @return string version of this XML document - */ - public function getVersion() - { - return $this->_version; - } - - /** - * @param string version of this XML document - */ - public function setVersion($version) - { - $this->_version=$version; - } - - /** - * @return string encoding of this XML document - */ - public function getEncoding() - { - return $this->_encoding; - } - - /** - * @param string encoding of this XML document - */ - public function setEncoding($encoding) - { - $this->_encoding=$encoding; - } - - /** - * Loads and parses an XML document. - * @param string the XML file path - * @return boolean whether the XML file is parsed successfully - * @throws TIOException if the file fails to be opened. - */ - public function loadFromFile($file) - { - if(($str=@file_get_contents($file))!==false) - return $this->loadFromString($str); - else - throw new TIOException('xmldocument_file_read_failed',$file); - } - - /** - * Loads and parses an XML string. - * The version and encoding will be determined based on the parsing result. - * @param string the XML string - * @return boolean whether the XML string is parsed successfully - */ - public function loadFromString($string) - { - // TODO: since PHP 5.1, we can get parsing errors and throw them as exception - $doc=new DOMDocument(); - if($doc->loadXML($string)===false) - return false; - - $this->setEncoding($doc->encoding); - $this->setVersion($doc->version); - - $element=$doc->documentElement; - $this->setTagName($element->tagName); - $this->setValue($element->nodeValue); - $elements=$this->getElements(); - $attributes=$this->getAttributes(); - $elements->clear(); - $attributes->clear(); - foreach($element->attributes as $name=>$attr) - $attributes->add($name,$attr->value); - foreach($element->childNodes as $child) - { - if($child instanceof DOMElement) - $elements->add($this->buildElement($child)); - } - - return true; - } - - /** - * Saves this XML document as an XML file. - * @param string the name of the file to be stored with XML output - * @throws TIOException if the file cannot be written - */ - public function saveToFile($file) - { - if(($fw=fopen($file,'w'))!==false) - { - fwrite($fw,$this->saveToString()); - fclose($fw); - } - else - throw new TIOException('xmldocument_file_write_failed',$file); - } - - /** - * Saves this XML document as an XML string - * @return string the XML string of this XML document - */ - public function saveToString() - { - $version=empty($this->_version)?' version="1.0"':' version="'.$this->_version.'"'; - $encoding=empty($this->_encoding)?'':' encoding="'.$this->_encoding.'"'; - return "\n".$this->toString(0); - } - - /** - * Recursively converts DOM XML nodes into TXmlElement - * @param DOMXmlNode the node to be converted - * @return TXmlElement the converted TXmlElement - */ - private function buildElement($node) - { - $element=new TXmlElement($node->tagName); - $element->setValue($node->nodeValue); - foreach($node->attributes as $name=>$attr) - $element->getAttributes()->add($name,$attr->value); - foreach($node->childNodes as $child) - { - if($child instanceof DOMElement) - $element->getElements()->add($this->buildElement($child)); - } - return $element; - } -} - - -/** - * TXmlElement class. - * - * TXmlElement represents an XML element node. - * You can obtain its tagname, attributes, text between the openning and closing - * tags via the TagName, Attributes, and Value properties, respectively. - * You can also retrieve its parent and child elements by Parent and Elements - * properties, respectively. - * - * @author Qiang Xue - * @version $Revision: $ $Date: $ - * @package System - * @since 3.0 - */ -class TXmlElementList extends TList -{ - /** - * @var TXmlElement owner of this list - */ - private $_o; - - /** - * Constructor. - * @param TXmlElement owner of this list - */ - public function __construct(TXmlElement $owner) - { - $this->_o=$owner; - } - - /** - * @return TXmlElement owner of this list - */ - protected function getOwner() - { - return $this->_o; - } - - - /** - * Inserts an item at the specified position. - * This overrides the parent implementation by performing additional - * operations for each newly added TXmlElement object. - * @param integer the speicified position. - * @param mixed new item - * @throws TInvalidDataTypeException if the item to be inserted is not a TXmlElement object. - */ - public function insertAt($index,$item) - { - if($item instanceof TXmlElement) - { - parent::insertAt($index,$item); - if($item->getParent()!==null) - $item->getParent()->getElements()->remove($item); - $item->setParent($this->_o); - } - else - throw new TInvalidDataTypeException('xmlelementlist_xmlelement_required'); - } - - /** - * Removes an item at the specified position. - * This overrides the parent implementation by performing additional - * cleanup work when removing a TXmlElement object. - * @param integer the index of the item to be removed. - * @return mixed the removed item. - */ - public function removeAt($index) - { - $item=parent::removeAt($index); - if($item instanceof TXmlElement) - $item->setParent(null); - return $item; - } -} - -?> \ No newline at end of file -- cgit v1.2.3