@@ -4203,10 +4203,10 @@ discard block |
||
| 4203 | 4203 | { |
| 4204 | 4204 | $value=TPropertyValue::ensureEnum($value,'THttpSessionCookieMode'); |
| 4205 | 4205 | if($value===THttpSessionCookieMode::None) |
| 4206 | - { |
|
| 4206 | + { |
|
| 4207 | 4207 | ini_set('session.use_cookies','0'); |
| 4208 | 4208 | ini_set('session.use_only_cookies','0'); |
| 4209 | - } |
|
| 4209 | + } |
|
| 4210 | 4210 | else if($value===THttpSessionCookieMode::Allow) |
| 4211 | 4211 | { |
| 4212 | 4212 | ini_set('session.use_cookies','1'); |
@@ -6805,77 +6805,77 @@ discard block |
||
| 6805 | 6805 | throw new TConfigurationException('templatecontrol_mastercontrol_required',get_class($this)); |
| 6806 | 6806 | parent::initRecursive($namingContainer); |
| 6807 | 6807 | } |
| 6808 | - public function tryToUpdateView($arObj, $throwExceptions = false) |
|
| 6809 | - { |
|
| 6810 | - $objAttrs = get_class_vars(get_class($arObj)); |
|
| 6811 | - foreach (array_keys($objAttrs) as $key) |
|
| 6812 | - { |
|
| 6813 | - try |
|
| 6814 | - { |
|
| 6815 | - if ($key != "RELATIONS") |
|
| 6816 | - { |
|
| 6817 | - $control = $this->{$key}; |
|
| 6818 | - if ($control instanceof TTextBox) |
|
| 6819 | - $control->Text = $arObj->{$key}; |
|
| 6820 | - elseif ($control instanceof TCheckBox) |
|
| 6821 | - $control->Checked = (boolean) $arObj->{$key}; |
|
| 6822 | - elseif ($control instanceof TDatePicker) |
|
| 6823 | - $control->Date = $arObj->{$key}; |
|
| 6824 | - } |
|
| 6825 | - else |
|
| 6826 | - { |
|
| 6827 | - foreach ($objAttrs["RELATIONS"] as $relKey => $relValues) |
|
| 6828 | - { |
|
| 6829 | - $relControl = $this->{$relKey}; |
|
| 6830 | - switch ($relValues[0]) |
|
| 6831 | - { |
|
| 6832 | - case TActiveRecord::BELONGS_TO: |
|
| 6833 | - case TActiveRecord::HAS_ONE: |
|
| 6834 | - $relControl->Text = $arObj->{$relKey}; |
|
| 6835 | - break; |
|
| 6836 | - case TActiveRecord::HAS_MANY: |
|
| 6837 | - if ($relControl instanceof TListControl) |
|
| 6838 | - { |
|
| 6839 | - $relControl->DataSource = $arObj->{$relKey}; |
|
| 6840 | - $relControl->dataBind(); |
|
| 6841 | - } |
|
| 6842 | - break; |
|
| 6843 | - } |
|
| 6844 | - } |
|
| 6845 | - break; |
|
| 6846 | - } |
|
| 6847 | - } |
|
| 6848 | - catch (Exception $ex) |
|
| 6849 | - { |
|
| 6850 | - if ($throwExceptions) |
|
| 6851 | - throw $ex; |
|
| 6852 | - } |
|
| 6853 | - } |
|
| 6854 | - } |
|
| 6855 | - public function tryToUpdateAR($arObj, $throwExceptions = false) |
|
| 6856 | - { |
|
| 6857 | - $objAttrs = get_class_vars(get_class($arObj)); |
|
| 6858 | - foreach (array_keys($objAttrs) as $key) |
|
| 6859 | - { |
|
| 6860 | - try |
|
| 6861 | - { |
|
| 6862 | - if ($key == "RELATIONS") |
|
| 6863 | - break; |
|
| 6864 | - $control = $this->{$key}; |
|
| 6865 | - if ($control instanceof TTextBox) |
|
| 6866 | - $arObj->{$key} = $control->Text; |
|
| 6867 | - elseif ($control instanceof TCheckBox) |
|
| 6868 | - $arObj->{$key} = $control->Checked; |
|
| 6869 | - elseif ($control instanceof TDatePicker) |
|
| 6870 | - $arObj->{$key} = $control->Date; |
|
| 6871 | - } |
|
| 6872 | - catch (Exception $ex) |
|
| 6873 | - { |
|
| 6874 | - if ($throwExceptions) |
|
| 6875 | - throw $ex; |
|
| 6876 | - } |
|
| 6877 | - } |
|
| 6878 | - } |
|
| 6808 | + public function tryToUpdateView($arObj, $throwExceptions = false) |
|
| 6809 | + { |
|
| 6810 | + $objAttrs = get_class_vars(get_class($arObj)); |
|
| 6811 | + foreach (array_keys($objAttrs) as $key) |
|
| 6812 | + { |
|
| 6813 | + try |
|
| 6814 | + { |
|
| 6815 | + if ($key != "RELATIONS") |
|
| 6816 | + { |
|
| 6817 | + $control = $this->{$key}; |
|
| 6818 | + if ($control instanceof TTextBox) |
|
| 6819 | + $control->Text = $arObj->{$key}; |
|
| 6820 | + elseif ($control instanceof TCheckBox) |
|
| 6821 | + $control->Checked = (boolean) $arObj->{$key}; |
|
| 6822 | + elseif ($control instanceof TDatePicker) |
|
| 6823 | + $control->Date = $arObj->{$key}; |
|
| 6824 | + } |
|
| 6825 | + else |
|
| 6826 | + { |
|
| 6827 | + foreach ($objAttrs["RELATIONS"] as $relKey => $relValues) |
|
| 6828 | + { |
|
| 6829 | + $relControl = $this->{$relKey}; |
|
| 6830 | + switch ($relValues[0]) |
|
| 6831 | + { |
|
| 6832 | + case TActiveRecord::BELONGS_TO: |
|
| 6833 | + case TActiveRecord::HAS_ONE: |
|
| 6834 | + $relControl->Text = $arObj->{$relKey}; |
|
| 6835 | + break; |
|
| 6836 | + case TActiveRecord::HAS_MANY: |
|
| 6837 | + if ($relControl instanceof TListControl) |
|
| 6838 | + { |
|
| 6839 | + $relControl->DataSource = $arObj->{$relKey}; |
|
| 6840 | + $relControl->dataBind(); |
|
| 6841 | + } |
|
| 6842 | + break; |
|
| 6843 | + } |
|
| 6844 | + } |
|
| 6845 | + break; |
|
| 6846 | + } |
|
| 6847 | + } |
|
| 6848 | + catch (Exception $ex) |
|
| 6849 | + { |
|
| 6850 | + if ($throwExceptions) |
|
| 6851 | + throw $ex; |
|
| 6852 | + } |
|
| 6853 | + } |
|
| 6854 | + } |
|
| 6855 | + public function tryToUpdateAR($arObj, $throwExceptions = false) |
|
| 6856 | + { |
|
| 6857 | + $objAttrs = get_class_vars(get_class($arObj)); |
|
| 6858 | + foreach (array_keys($objAttrs) as $key) |
|
| 6859 | + { |
|
| 6860 | + try |
|
| 6861 | + { |
|
| 6862 | + if ($key == "RELATIONS") |
|
| 6863 | + break; |
|
| 6864 | + $control = $this->{$key}; |
|
| 6865 | + if ($control instanceof TTextBox) |
|
| 6866 | + $arObj->{$key} = $control->Text; |
|
| 6867 | + elseif ($control instanceof TCheckBox) |
|
| 6868 | + $arObj->{$key} = $control->Checked; |
|
| 6869 | + elseif ($control instanceof TDatePicker) |
|
| 6870 | + $arObj->{$key} = $control->Date; |
|
| 6871 | + } |
|
| 6872 | + catch (Exception $ex) |
|
| 6873 | + { |
|
| 6874 | + if ($throwExceptions) |
|
| 6875 | + throw $ex; |
|
| 6876 | + } |
|
| 6877 | + } |
|
| 6878 | + } |
|
| 6879 | 6879 | } |
| 6880 | 6880 | class TForm extends TControl |
| 6881 | 6881 | { |
@@ -7536,13 +7536,13 @@ discard block |
||
| 7536 | 7536 | { |
| 7537 | 7537 | Prado::using('System.Web.UI.ActiveControls.TActivePageAdapter'); |
| 7538 | 7538 | $this->setAdapter(new TActivePageAdapter($this)); |
| 7539 | - $callbackEventParameter = $this->getRequest()->itemAt(TPage::FIELD_CALLBACK_PARAMETER); |
|
| 7540 | - if(strlen($callbackEventParameter) > 0) |
|
| 7541 | - $this->_postData[TPage::FIELD_CALLBACK_PARAMETER]=TJavaScript::jsonDecode((string)$callbackEventParameter); |
|
| 7542 | - if (($g=$this->getApplication()->getGlobalization(false))!==null && |
|
| 7543 | - strtoupper($enc=$g->getCharset())!='UTF-8') |
|
| 7544 | - foreach ($this->_postData as $k=>$v) |
|
| 7545 | - $this->_postData[$k]=self::decodeUTF8($v, $enc); |
|
| 7539 | + $callbackEventParameter = $this->getRequest()->itemAt(TPage::FIELD_CALLBACK_PARAMETER); |
|
| 7540 | + if(strlen($callbackEventParameter) > 0) |
|
| 7541 | + $this->_postData[TPage::FIELD_CALLBACK_PARAMETER]=TJavaScript::jsonDecode((string)$callbackEventParameter); |
|
| 7542 | + if (($g=$this->getApplication()->getGlobalization(false))!==null && |
|
| 7543 | + strtoupper($enc=$g->getCharset())!='UTF-8') |
|
| 7544 | + foreach ($this->_postData as $k=>$v) |
|
| 7545 | + $this->_postData[$k]=self::decodeUTF8($v, $enc); |
|
| 7546 | 7546 | $this->onPreInit(null); |
| 7547 | 7547 | $this->initRecursive(); |
| 7548 | 7548 | $this->onInitComplete(null); |
@@ -9127,15 +9127,15 @@ discard block |
||
| 9127 | 9127 | $component=new ReflectionClass('TComponent'); |
| 9128 | 9128 | $behaviors=$component->getStaticProperties(); |
| 9129 | 9129 | if(!isset($behaviors['_um'])) |
| 9130 | - return false; |
|
| 9130 | + return false; |
|
| 9131 | 9131 | foreach($behaviors['_um'] as $name=>$list) |
| 9132 | 9132 | { |
| 9133 | - if(strtolower($class->getShortName())!==$name && !$class->isSubclassOf($name)) continue; |
|
| 9134 | - foreach($list as $param) |
|
| 9135 | - { |
|
| 9136 | - if(method_exists($param->getBehavior(),$method)) |
|
| 9137 | - return true; |
|
| 9138 | - } |
|
| 9133 | + if(strtolower($class->getShortName())!==$name && !$class->isSubclassOf($name)) continue; |
|
| 9134 | + foreach($list as $param) |
|
| 9135 | + { |
|
| 9136 | + if(method_exists($param->getBehavior(),$method)) |
|
| 9137 | + return true; |
|
| 9138 | + } |
|
| 9139 | 9139 | } |
| 9140 | 9140 | return false; |
| 9141 | 9141 | } |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | /** |
| 202 | - * @return boolean true to post the inputs of the form on callback, default |
|
| 202 | + * @return string true to post the inputs of the form on callback, default |
|
| 203 | 203 | * is post the inputs on callback. |
| 204 | 204 | */ |
| 205 | 205 | public function getPostState() |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | /** |
| 220 | - * @return integer callback request timeout. |
|
| 220 | + * @return string callback request timeout. |
|
| 221 | 221 | */ |
| 222 | 222 | public function getRequestTimeOut() |
| 223 | 223 | { |
@@ -261,6 +261,7 @@ discard block |
||
| 261 | 261 | * update. This will automatically set HasPrority to true. |
| 262 | 262 | * @param boolean true enables the callback response to update the |
| 263 | 263 | * viewstate. |
| 264 | + * @param boolean $value |
|
| 264 | 265 | */ |
| 265 | 266 | public function setEnablePageStateUpdate($value) |
| 266 | 267 | { |
@@ -269,7 +270,7 @@ discard block |
||
| 269 | 270 | } |
| 270 | 271 | |
| 271 | 272 | /** |
| 272 | - * @return boolean client-side viewstate will be updated on callback |
|
| 273 | + * @return boolean|string client-side viewstate will be updated on callback |
|
| 273 | 274 | * response if true. Default is true. |
| 274 | 275 | */ |
| 275 | 276 | public function getEnablePageStateUpdate() |
@@ -18,8 +18,8 @@ |
||
| 18 | 18 | if(!defined('PRADO_DIR')) |
| 19 | 19 | define('PRADO_DIR',dirname(__FILE__)); |
| 20 | 20 | /** |
| 21 | - * Defines the default permission for writable directories and files |
|
| 22 | - */ |
|
| 21 | + * Defines the default permission for writable directories and files |
|
| 22 | + */ |
|
| 23 | 23 | if(!defined('PRADO_CHMOD')) |
| 24 | 24 | define('PRADO_CHMOD',0777); |
| 25 | 25 | |
@@ -1,13 +1,13 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * TMap, TMapIterator classes |
|
| 4 | - * |
|
| 5 | - * @author Qiang Xue <[email protected]> |
|
| 6 | - * @link https://github.com/pradosoft/prado |
|
| 7 | - * @copyright Copyright © 2005-2016 The PRADO Group |
|
| 8 | - * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT |
|
| 9 | - * @package System.Collections |
|
| 10 | - */ |
|
| 3 | + * TMap, TMapIterator classes |
|
| 4 | + * |
|
| 5 | + * @author Qiang Xue <[email protected]> |
|
| 6 | + * @link https://github.com/pradosoft/prado |
|
| 7 | + * @copyright Copyright © 2005-2016 The PRADO Group |
|
| 8 | + * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT |
|
| 9 | + * @package System.Collections |
|
| 10 | + */ |
|
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * TMap class |
@@ -1,13 +1,13 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * TDataGridPagerStyle class file. |
|
| 4 | - * |
|
| 5 | - * @author Qiang Xue <[email protected]> |
|
| 6 | - * @link https://github.com/pradosoft/prado |
|
| 7 | - * @copyright Copyright © 2005-2016 The PRADO Group |
|
| 8 | - * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT |
|
| 9 | - * @package System.Web.UI.WebControls |
|
| 10 | - */ |
|
| 3 | + * TDataGridPagerStyle class file. |
|
| 4 | + * |
|
| 5 | + * @author Qiang Xue <[email protected]> |
|
| 6 | + * @link https://github.com/pradosoft/prado |
|
| 7 | + * @copyright Copyright © 2005-2016 The PRADO Group |
|
| 8 | + * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT |
|
| 9 | + * @package System.Web.UI.WebControls |
|
| 10 | + */ |
|
| 11 | 11 | |
| 12 | 12 | Prado::using('System.Web.UI.WebControls.TDataGrid'); |
| 13 | 13 | |
@@ -46,575 +46,575 @@ |
||
| 46 | 46 | class TCache_Lite |
| 47 | 47 | { |
| 48 | 48 | |
| 49 | - // --- Private properties --- |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Directory where to put the cache files |
|
| 53 | - * (make sure to add a trailing slash) |
|
| 54 | - * |
|
| 55 | - * @var string $_cacheDir |
|
| 56 | - */ |
|
| 57 | - protected $_cacheDir = '/tmp/'; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Enable / disable caching |
|
| 61 | - * |
|
| 62 | - * (can be very usefull for the debug of cached scripts) |
|
| 63 | - * |
|
| 64 | - * @var boolean $_caching |
|
| 65 | - */ |
|
| 66 | - protected $_caching = true; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Cache lifetime (in seconds) |
|
| 70 | - * |
|
| 71 | - * @var int $_lifeTime |
|
| 72 | - */ |
|
| 73 | - protected $_lifeTime = 3600; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Enable / disable fileLocking |
|
| 77 | - * |
|
| 78 | - * (can avoid cache corruption under bad circumstances) |
|
| 79 | - * |
|
| 80 | - * @var boolean $_fileLocking |
|
| 81 | - */ |
|
| 82 | - protected $_fileLocking = true; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Timestamp of the last valid cache |
|
| 86 | - * |
|
| 87 | - * @var int $_refreshTime |
|
| 88 | - */ |
|
| 89 | - protected $_refreshTime; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * File name (with path) |
|
| 93 | - * |
|
| 94 | - * @var string $_file |
|
| 95 | - */ |
|
| 96 | - protected $_file; |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Enable / disable write control (the cache is read just after writing |
|
| 100 | - * to detect corrupt entries) |
|
| 101 | - * |
|
| 102 | - * Enable write control will lightly slow the cache writing but not the |
|
| 103 | - * cache reading. Write control can detect some corrupt cache files but |
|
| 104 | - * maybe it's not a perfect control |
|
| 105 | - * |
|
| 106 | - * @var boolean $_writeControl |
|
| 107 | - */ |
|
| 108 | - protected $_writeControl = true; |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Enable / disable read control |
|
| 112 | - * |
|
| 113 | - * If enabled, a control key is embeded in cache file and this key is |
|
| 114 | - * compared with the one calculated after the reading. |
|
| 115 | - * |
|
| 116 | - * @var boolean $_writeControl |
|
| 117 | - */ |
|
| 118 | - protected $_readControl = true; |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Type of read control (only if read control is enabled) |
|
| 122 | - * |
|
| 123 | - * Available values are : |
|
| 124 | - * 'md5' for a md5 hash control (best but slowest) |
|
| 125 | - * 'crc32' for a crc32 hash control (lightly less safe but faster, |
|
| 126 | - * better choice) |
|
| 127 | - * 'strlen' for a length only test (fastest) |
|
| 128 | - * |
|
| 129 | - * @var boolean $_readControlType |
|
| 130 | - */ |
|
| 131 | - protected $_readControlType = 'crc32'; |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Current cache id |
|
| 135 | - * |
|
| 136 | - * @var string $_id |
|
| 137 | - */ |
|
| 138 | - protected $_id; |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Current cache group |
|
| 142 | - * |
|
| 143 | - * @var string $_group |
|
| 144 | - */ |
|
| 145 | - protected $_group; |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Enable / Disable "Memory Caching" |
|
| 149 | - * |
|
| 150 | - * NB : There is no lifetime for memory caching ! |
|
| 151 | - * |
|
| 152 | - * @var boolean $_memoryCaching |
|
| 153 | - */ |
|
| 154 | - protected $_memoryCaching = false; |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Enable / Disable "Only Memory Caching" |
|
| 158 | - * (be carefull, memory caching is "beta quality") |
|
| 159 | - * |
|
| 160 | - * @var boolean $_onlyMemoryCaching |
|
| 161 | - */ |
|
| 162 | - protected $_onlyMemoryCaching = false; |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Memory caching array |
|
| 166 | - * |
|
| 167 | - * @var array $_memoryCachingArray |
|
| 168 | - */ |
|
| 169 | - protected $_memoryCachingArray = array(); |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Memory caching counter |
|
| 173 | - * |
|
| 174 | - * @var int $memoryCachingCounter |
|
| 175 | - */ |
|
| 176 | - protected $_memoryCachingCounter = 0; |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Memory caching limit |
|
| 180 | - * |
|
| 181 | - * @var int $memoryCachingLimit |
|
| 182 | - */ |
|
| 183 | - protected $_memoryCachingLimit = 1000; |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * File Name protection |
|
| 187 | - * |
|
| 188 | - * if set to true, you can use any cache id or group name |
|
| 189 | - * if set to false, it can be faster but cache ids and group names |
|
| 190 | - * will be used directly in cache file names so be carefull with |
|
| 191 | - * special characters... |
|
| 192 | - * |
|
| 193 | - * @var boolean $fileNameProtection |
|
| 194 | - */ |
|
| 195 | - protected $_fileNameProtection = true; |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Enable / disable automatic serialization |
|
| 199 | - * |
|
| 200 | - * it can be used to save directly datas which aren't strings |
|
| 201 | - * (but it's slower) |
|
| 202 | - * |
|
| 203 | - * @var boolean $_serialize |
|
| 204 | - */ |
|
| 205 | - protected $_automaticSerialization = false; |
|
| 206 | - |
|
| 207 | - // --- Public methods --- |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Constructor |
|
| 211 | - * |
|
| 212 | - * $options is an assoc. Available options are : |
|
| 213 | - * $options = array( |
|
| 214 | - * 'cacheDir' => directory where to put the cache files (string), |
|
| 215 | - * 'caching' => enable / disable caching (boolean), |
|
| 216 | - * 'lifeTime' => cache lifetime in seconds (int), |
|
| 217 | - * 'fileLocking' => enable / disable fileLocking (boolean), |
|
| 218 | - * 'writeControl' => enable / disable write control (boolean), |
|
| 219 | - * 'readControl' => enable / disable read control (boolean), |
|
| 220 | - * 'readControlType' => type of read control 'crc32', 'md5', 'strlen', |
|
| 221 | - * 'memoryCaching' => enable / disable memory caching (boolean), |
|
| 222 | - * 'onlyMemoryCaching' => enable / disable only memory caching (boolean), |
|
| 223 | - * 'memoryCachingLimit' => max nbr of records in memory caching (int), |
|
| 224 | - * 'fileNameProtection' => enable / disable file name protection (boolean), |
|
| 225 | - * 'automaticSerialization' => enable / disable serialization (boolean) |
|
| 226 | - * ); |
|
| 227 | - * |
|
| 228 | - * @param array $options options |
|
| 229 | - * @access public |
|
| 230 | - */ |
|
| 231 | - function __construct($options = array(null)) |
|
| 232 | - { |
|
| 233 | - $availableOptions = array( 'automaticSerialization', |
|
| 234 | - 'fileNameProtection', |
|
| 235 | - 'memoryCaching', |
|
| 236 | - 'onlyMemoryCaching', |
|
| 237 | - 'memoryCachingLimit', |
|
| 238 | - 'cacheDir', |
|
| 239 | - 'caching', |
|
| 240 | - 'lifeTime', |
|
| 241 | - 'fileLocking', |
|
| 242 | - 'writeControl', |
|
| 243 | - 'readControl', |
|
| 244 | - 'readControlType'); |
|
| 245 | - foreach($options as $key => $value) { |
|
| 246 | - if(in_array($key, $availableOptions)) { |
|
| 247 | - $property = '_'.$key; |
|
| 248 | - $this->$property = $value; |
|
| 249 | - } |
|
| 250 | - } |
|
| 251 | - $this->_refreshTime = time() - $this->_lifeTime; |
|
| 49 | + // --- Private properties --- |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Directory where to put the cache files |
|
| 53 | + * (make sure to add a trailing slash) |
|
| 54 | + * |
|
| 55 | + * @var string $_cacheDir |
|
| 56 | + */ |
|
| 57 | + protected $_cacheDir = '/tmp/'; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Enable / disable caching |
|
| 61 | + * |
|
| 62 | + * (can be very usefull for the debug of cached scripts) |
|
| 63 | + * |
|
| 64 | + * @var boolean $_caching |
|
| 65 | + */ |
|
| 66 | + protected $_caching = true; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Cache lifetime (in seconds) |
|
| 70 | + * |
|
| 71 | + * @var int $_lifeTime |
|
| 72 | + */ |
|
| 73 | + protected $_lifeTime = 3600; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Enable / disable fileLocking |
|
| 77 | + * |
|
| 78 | + * (can avoid cache corruption under bad circumstances) |
|
| 79 | + * |
|
| 80 | + * @var boolean $_fileLocking |
|
| 81 | + */ |
|
| 82 | + protected $_fileLocking = true; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Timestamp of the last valid cache |
|
| 86 | + * |
|
| 87 | + * @var int $_refreshTime |
|
| 88 | + */ |
|
| 89 | + protected $_refreshTime; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * File name (with path) |
|
| 93 | + * |
|
| 94 | + * @var string $_file |
|
| 95 | + */ |
|
| 96 | + protected $_file; |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Enable / disable write control (the cache is read just after writing |
|
| 100 | + * to detect corrupt entries) |
|
| 101 | + * |
|
| 102 | + * Enable write control will lightly slow the cache writing but not the |
|
| 103 | + * cache reading. Write control can detect some corrupt cache files but |
|
| 104 | + * maybe it's not a perfect control |
|
| 105 | + * |
|
| 106 | + * @var boolean $_writeControl |
|
| 107 | + */ |
|
| 108 | + protected $_writeControl = true; |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Enable / disable read control |
|
| 112 | + * |
|
| 113 | + * If enabled, a control key is embeded in cache file and this key is |
|
| 114 | + * compared with the one calculated after the reading. |
|
| 115 | + * |
|
| 116 | + * @var boolean $_writeControl |
|
| 117 | + */ |
|
| 118 | + protected $_readControl = true; |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Type of read control (only if read control is enabled) |
|
| 122 | + * |
|
| 123 | + * Available values are : |
|
| 124 | + * 'md5' for a md5 hash control (best but slowest) |
|
| 125 | + * 'crc32' for a crc32 hash control (lightly less safe but faster, |
|
| 126 | + * better choice) |
|
| 127 | + * 'strlen' for a length only test (fastest) |
|
| 128 | + * |
|
| 129 | + * @var boolean $_readControlType |
|
| 130 | + */ |
|
| 131 | + protected $_readControlType = 'crc32'; |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Current cache id |
|
| 135 | + * |
|
| 136 | + * @var string $_id |
|
| 137 | + */ |
|
| 138 | + protected $_id; |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Current cache group |
|
| 142 | + * |
|
| 143 | + * @var string $_group |
|
| 144 | + */ |
|
| 145 | + protected $_group; |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Enable / Disable "Memory Caching" |
|
| 149 | + * |
|
| 150 | + * NB : There is no lifetime for memory caching ! |
|
| 151 | + * |
|
| 152 | + * @var boolean $_memoryCaching |
|
| 153 | + */ |
|
| 154 | + protected $_memoryCaching = false; |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Enable / Disable "Only Memory Caching" |
|
| 158 | + * (be carefull, memory caching is "beta quality") |
|
| 159 | + * |
|
| 160 | + * @var boolean $_onlyMemoryCaching |
|
| 161 | + */ |
|
| 162 | + protected $_onlyMemoryCaching = false; |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Memory caching array |
|
| 166 | + * |
|
| 167 | + * @var array $_memoryCachingArray |
|
| 168 | + */ |
|
| 169 | + protected $_memoryCachingArray = array(); |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Memory caching counter |
|
| 173 | + * |
|
| 174 | + * @var int $memoryCachingCounter |
|
| 175 | + */ |
|
| 176 | + protected $_memoryCachingCounter = 0; |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Memory caching limit |
|
| 180 | + * |
|
| 181 | + * @var int $memoryCachingLimit |
|
| 182 | + */ |
|
| 183 | + protected $_memoryCachingLimit = 1000; |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * File Name protection |
|
| 187 | + * |
|
| 188 | + * if set to true, you can use any cache id or group name |
|
| 189 | + * if set to false, it can be faster but cache ids and group names |
|
| 190 | + * will be used directly in cache file names so be carefull with |
|
| 191 | + * special characters... |
|
| 192 | + * |
|
| 193 | + * @var boolean $fileNameProtection |
|
| 194 | + */ |
|
| 195 | + protected $_fileNameProtection = true; |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Enable / disable automatic serialization |
|
| 199 | + * |
|
| 200 | + * it can be used to save directly datas which aren't strings |
|
| 201 | + * (but it's slower) |
|
| 202 | + * |
|
| 203 | + * @var boolean $_serialize |
|
| 204 | + */ |
|
| 205 | + protected $_automaticSerialization = false; |
|
| 206 | + |
|
| 207 | + // --- Public methods --- |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Constructor |
|
| 211 | + * |
|
| 212 | + * $options is an assoc. Available options are : |
|
| 213 | + * $options = array( |
|
| 214 | + * 'cacheDir' => directory where to put the cache files (string), |
|
| 215 | + * 'caching' => enable / disable caching (boolean), |
|
| 216 | + * 'lifeTime' => cache lifetime in seconds (int), |
|
| 217 | + * 'fileLocking' => enable / disable fileLocking (boolean), |
|
| 218 | + * 'writeControl' => enable / disable write control (boolean), |
|
| 219 | + * 'readControl' => enable / disable read control (boolean), |
|
| 220 | + * 'readControlType' => type of read control 'crc32', 'md5', 'strlen', |
|
| 221 | + * 'memoryCaching' => enable / disable memory caching (boolean), |
|
| 222 | + * 'onlyMemoryCaching' => enable / disable only memory caching (boolean), |
|
| 223 | + * 'memoryCachingLimit' => max nbr of records in memory caching (int), |
|
| 224 | + * 'fileNameProtection' => enable / disable file name protection (boolean), |
|
| 225 | + * 'automaticSerialization' => enable / disable serialization (boolean) |
|
| 226 | + * ); |
|
| 227 | + * |
|
| 228 | + * @param array $options options |
|
| 229 | + * @access public |
|
| 230 | + */ |
|
| 231 | + function __construct($options = array(null)) |
|
| 232 | + { |
|
| 233 | + $availableOptions = array( 'automaticSerialization', |
|
| 234 | + 'fileNameProtection', |
|
| 235 | + 'memoryCaching', |
|
| 236 | + 'onlyMemoryCaching', |
|
| 237 | + 'memoryCachingLimit', |
|
| 238 | + 'cacheDir', |
|
| 239 | + 'caching', |
|
| 240 | + 'lifeTime', |
|
| 241 | + 'fileLocking', |
|
| 242 | + 'writeControl', |
|
| 243 | + 'readControl', |
|
| 244 | + 'readControlType'); |
|
| 245 | + foreach($options as $key => $value) { |
|
| 246 | + if(in_array($key, $availableOptions)) { |
|
| 247 | + $property = '_'.$key; |
|
| 248 | + $this->$property = $value; |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | + $this->_refreshTime = time() - $this->_lifeTime; |
|
| 252 | 252 | } |
| 253 | 253 | |
| 254 | - /** |
|
| 255 | - * Test if a cache is available and (if yes) return it |
|
| 256 | - * |
|
| 257 | - * @param string $id cache id |
|
| 258 | - * @param string $group name of the cache group |
|
| 259 | - * @param boolean $doNotTestCacheValidity if set to true, the cache |
|
| 260 | - * validity won't be tested |
|
| 261 | - * @return string data of the cache (or false if no cache available) |
|
| 262 | - * @access public |
|
| 263 | - */ |
|
| 264 | - function get($id, $group = 'default', $doNotTestCacheValidity = false) |
|
| 265 | - { |
|
| 266 | - $this->_id = $id; |
|
| 267 | - $this->_group = $group; |
|
| 268 | - $data = false; |
|
| 269 | - if ($this->_caching) { |
|
| 270 | - $this->_setFileName($id, $group); |
|
| 271 | - if ($this->_memoryCaching) { |
|
| 272 | - if (isset($this->_memoryCachingArray[$this->_file])) { |
|
| 273 | - if ($this->_automaticSerialization) { |
|
| 274 | - return unserialize( |
|
| 275 | - $this->_memoryCachingArray[$this->_file]); |
|
| 276 | - } else { |
|
| 277 | - return $this->_memoryCachingArray[$this->_file]; |
|
| 278 | - } |
|
| 279 | - } else { |
|
| 280 | - if ($this->_onlyMemoryCaching) { |
|
| 281 | - return false; |
|
| 282 | - } |
|
| 283 | - } |
|
| 284 | - } |
|
| 285 | - if ($doNotTestCacheValidity) { |
|
| 286 | - if (file_exists($this->_file)) { |
|
| 287 | - $data = $this->_read(); |
|
| 288 | - } |
|
| 289 | - } else { |
|
| 290 | - if (@filemtime($this->_file) > $this->_refreshTime) { |
|
| 291 | - $data = $this->_read(); |
|
| 292 | - } |
|
| 293 | - } |
|
| 294 | - if (($data) and ($this->_memoryCaching)) { |
|
| 295 | - $this->_memoryCacheAdd($this->_file, $data); |
|
| 296 | - } |
|
| 297 | - if ($this->_automaticSerialization && is_string($data)) { |
|
| 298 | - $data = unserialize($data); |
|
| 299 | - } |
|
| 300 | - return $data; |
|
| 301 | - } |
|
| 302 | - return false; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Save some data in a cache file |
|
| 307 | - * |
|
| 308 | - * @param string $data data to put in cache (can be another type than strings |
|
| 309 | - * if automaticSerialization is on) |
|
| 310 | - * @param string $id cache id |
|
| 311 | - * @param string $group name of the cache group |
|
| 312 | - * @return boolean true if no problem |
|
| 313 | - * @access public |
|
| 314 | - */ |
|
| 315 | - function save($data, $id = null, $group = 'default') |
|
| 316 | - { |
|
| 317 | - if ($this->_caching) { |
|
| 318 | - if ($this->_automaticSerialization) { |
|
| 319 | - $data = serialize($data); |
|
| 320 | - } |
|
| 321 | - if (isset($id)) { |
|
| 322 | - $this->_setFileName($id, $group); |
|
| 323 | - } |
|
| 324 | - if ($this->_memoryCaching) { |
|
| 325 | - $this->_memoryCacheAdd($this->_file, $data); |
|
| 326 | - if ($this->_onlyMemoryCaching) { |
|
| 327 | - return true; |
|
| 328 | - } |
|
| 329 | - } |
|
| 330 | - if ($this->_writeControl) { |
|
| 331 | - if (!$this->_writeAndControl($data)) { |
|
| 332 | - @touch($this->_file, time() - 2*abs($this->_lifeTime)); |
|
| 333 | - return false; |
|
| 334 | - } else { |
|
| 335 | - return true; |
|
| 336 | - } |
|
| 337 | - } else { |
|
| 338 | - return $this->_write($data); |
|
| 339 | - } |
|
| 340 | - } |
|
| 341 | - return false; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Remove a cache file |
|
| 346 | - * |
|
| 347 | - * @param string $id cache id |
|
| 348 | - * @param string $group name of the cache group |
|
| 349 | - * @return boolean true if no problem |
|
| 350 | - * @access public |
|
| 351 | - */ |
|
| 352 | - function remove($id, $group = 'default') |
|
| 353 | - { |
|
| 354 | - $this->_setFileName($id, $group); |
|
| 355 | - if (!@unlink($this->_file)) { |
|
| 356 | - $this->raiseError('TCache_Lite : Unable to remove cache !', -3); |
|
| 357 | - return false; |
|
| 358 | - } |
|
| 359 | - return true; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Clean the cache |
|
| 364 | - * |
|
| 365 | - * if no group is specified all cache files will be destroyed |
|
| 366 | - * else only cache files of the specified group will be destroyed |
|
| 367 | - * |
|
| 368 | - * @param string $group name of the cache group |
|
| 369 | - * @return boolean true if no problem |
|
| 370 | - * @access public |
|
| 371 | - */ |
|
| 372 | - function clean($group = false) |
|
| 373 | - { |
|
| 374 | - if ($this->_fileNameProtection) { |
|
| 375 | - $motif = ($group) ? 'cache_'.md5($group).'_' : 'cache_'; |
|
| 376 | - } else { |
|
| 377 | - $motif = ($group) ? 'cache_'.$group.'_' : 'cache_'; |
|
| 378 | - } |
|
| 379 | - if ($this->_memoryCaching) { |
|
| 380 | - while (list($key, $value) = each($this->_memoryCaching)) { |
|
| 381 | - if (strpos($key, $motif, 0)) { |
|
| 382 | - unset($this->_memoryCaching[$key]); |
|
| 383 | - $this->_memoryCachingCounter = |
|
| 384 | - $this->_memoryCachingCounter - 1; |
|
| 385 | - } |
|
| 386 | - } |
|
| 387 | - if ($this->_onlyMemoryCaching) { |
|
| 388 | - return true; |
|
| 389 | - } |
|
| 390 | - } |
|
| 391 | - if (!($dh = opendir($this->_cacheDir))) { |
|
| 392 | - $this->raiseError('TCache_Lite : Unable to open cache directory !'); |
|
| 393 | - return false; |
|
| 394 | - } |
|
| 395 | - while ($file = readdir($dh)) { |
|
| 396 | - if (($file != '.') && ($file != '..')) { |
|
| 397 | - $file = $this->_cacheDir . $file; |
|
| 398 | - if (is_file($file)) { |
|
| 399 | - if (strpos($file, $motif, 0)) { |
|
| 400 | - if (!@unlink($file)) { |
|
| 401 | - $this->raiseError('Cache_Lite : Unable to remove cache !', -3); |
|
| 402 | - return false; |
|
| 403 | - } |
|
| 404 | - } |
|
| 405 | - } |
|
| 406 | - } |
|
| 407 | - } |
|
| 408 | - return true; |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - /** |
|
| 412 | - * Set a new life time |
|
| 413 | - * |
|
| 414 | - * @param int $newLifeTime new life time (in seconds) |
|
| 415 | - * @access public |
|
| 416 | - */ |
|
| 417 | - function setLifeTime($newLifeTime) |
|
| 418 | - { |
|
| 419 | - $this->_lifeTime = $newLifeTime; |
|
| 420 | - $this->_refreshTime = time() - $newLifeTime; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * |
|
| 425 | - * @access public |
|
| 426 | - */ |
|
| 427 | - function saveMemoryCachingState($id, $group = 'default') |
|
| 428 | - { |
|
| 429 | - if ($this->_caching) { |
|
| 430 | - $array = array( |
|
| 431 | - 'counter' => $this->_memoryCachingCounter, |
|
| 432 | - 'array' => $this->_memoryCachingState |
|
| 433 | - ); |
|
| 434 | - $data = serialize($array); |
|
| 435 | - $this->save($data, $id, $group); |
|
| 436 | - } |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /** |
|
| 440 | - * |
|
| 441 | - * @access public |
|
| 442 | - */ |
|
| 443 | - function getMemoryCachingState($id, $group = 'default', |
|
| 444 | - $doNotTestCacheValidity = false) |
|
| 445 | - { |
|
| 446 | - if ($this->_caching) { |
|
| 447 | - if ($data = $this->get($id, $group, $doNotTestCacheValidity)) |
|
| 448 | - { |
|
| 449 | - $array = unserialize($data); |
|
| 450 | - $this->_memoryCachingCounter = $array['counter']; |
|
| 451 | - $this->_memoryCachingArray = $array['array']; |
|
| 452 | - } |
|
| 453 | - } |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * Return the cache last modification time |
|
| 458 | - * |
|
| 459 | - * BE CAREFUL : THIS METHOD IS FOR HACKING ONLY ! |
|
| 460 | - * |
|
| 461 | - * @return int last modification time |
|
| 462 | - */ |
|
| 463 | - function lastModified() { |
|
| 464 | - return filemtime($this->cache->_file); |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * Trigger a PEAR error |
|
| 469 | - * |
|
| 470 | - * To improve performances, the PEAR.php file is included dynamically. |
|
| 471 | - * The file is so included only when an error is triggered. So, in most |
|
| 472 | - * cases, the file isn't included and perfs are much better. |
|
| 473 | - * |
|
| 474 | - * @param string $msg error message |
|
| 475 | - * @param int $code error code |
|
| 476 | - * @access public |
|
| 477 | - */ |
|
| 478 | - function raiseError($msg, $code) |
|
| 479 | - { |
|
| 480 | - throw new Exception($msg); |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - // --- Private methods --- |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * |
|
| 487 | - * @access private |
|
| 488 | - */ |
|
| 489 | - function _memoryCacheAdd($id, $data) |
|
| 490 | - { |
|
| 491 | - $this->_memoryCachingArray[$this->_file] = $data; |
|
| 492 | - if ($this->_memoryCachingCounter >= $this->_memoryCachingLimit) { |
|
| 493 | - list($key, $value) = each($this->_memoryCachingArray); |
|
| 494 | - unset($this->_memoryCachingArray[$key]); |
|
| 495 | - } else { |
|
| 496 | - $this->_memoryCachingCounter = $this->_memoryCachingCounter + 1; |
|
| 497 | - } |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - /** |
|
| 501 | - * Make a file name (with path) |
|
| 502 | - * |
|
| 503 | - * @param string $id cache id |
|
| 504 | - * @param string $group name of the group |
|
| 505 | - * @access private |
|
| 506 | - */ |
|
| 507 | - function _setFileName($id, $group) |
|
| 508 | - { |
|
| 509 | - if ($this->_fileNameProtection) { |
|
| 510 | - $this->_file = ($this->_cacheDir.'cache_'.md5($group).'_' |
|
| 511 | - .md5($id)); |
|
| 512 | - } else { |
|
| 513 | - $this->_file = $this->_cacheDir.'cache_'.$group.'_'.$id; |
|
| 514 | - } |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - function getCacheFile() |
|
| 518 | - { |
|
| 519 | - return $this->_file; |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - /** |
|
| 523 | - * Read the cache file and return the content |
|
| 524 | - * |
|
| 525 | - * @return string content of the cache file |
|
| 526 | - * @access private |
|
| 527 | - */ |
|
| 528 | - function _read() |
|
| 529 | - { |
|
| 530 | - $fp = @fopen($this->_file, "rb"); |
|
| 531 | - if ($this->_fileLocking) @flock($fp, LOCK_SH); |
|
| 532 | - if ($fp) { |
|
| 533 | - // because the filesize can be cached by PHP itself... |
|
| 534 | - clearstatcache(); |
|
| 535 | - $length = @filesize($this->_file); |
|
| 536 | - if ($this->_readControl) { |
|
| 537 | - $hashControl = @fread($fp, 32); |
|
| 538 | - $length = $length - 32; |
|
| 539 | - } |
|
| 540 | - $data = @fread($fp, $length); |
|
| 541 | - if ($this->_fileLocking) @flock($fp, LOCK_UN); |
|
| 542 | - @fclose($fp); |
|
| 543 | - if ($this->_readControl) { |
|
| 544 | - $hashData = $this->_hash($data, $this->_readControlType); |
|
| 545 | - if ($hashData != $hashControl) { |
|
| 546 | - @touch($this->_file, time() - 2*abs($this->_lifeTime)); |
|
| 547 | - return false; |
|
| 548 | - } |
|
| 549 | - } |
|
| 550 | - return $data; |
|
| 551 | - } |
|
| 552 | - $this->raiseError('Cache_Lite : Unable to read cache !', -2); |
|
| 553 | - return false; |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - /** |
|
| 557 | - * Write the given data in the cache file |
|
| 558 | - * |
|
| 559 | - * @param string $data data to put in cache |
|
| 560 | - * @return boolean true if ok |
|
| 561 | - * @access private |
|
| 562 | - */ |
|
| 563 | - function _write($data) |
|
| 564 | - { |
|
| 565 | - $fp = @fopen($this->_file, "wb"); |
|
| 566 | - if ($fp) { |
|
| 567 | - if ($this->_fileLocking) @flock($fp, LOCK_EX); |
|
| 568 | - if ($this->_readControl) { |
|
| 569 | - @fwrite($fp, $this->_hash($data, $this->_readControlType), 32); |
|
| 570 | - } |
|
| 571 | - $len = strlen($data); |
|
| 572 | - @fwrite($fp, $data, $len); |
|
| 573 | - if ($this->_fileLocking) @flock($fp, LOCK_UN); |
|
| 574 | - @fclose($fp); |
|
| 575 | - return true; |
|
| 576 | - } |
|
| 577 | - $this->raiseError('Cache_Lite : Unable to write cache !', -1); |
|
| 578 | - return false; |
|
| 579 | - } |
|
| 580 | - |
|
| 581 | - /** |
|
| 582 | - * Write the given data in the cache file and control it just after to avoid |
|
| 583 | - * corrupted cache entries |
|
| 584 | - * |
|
| 585 | - * @param string $data data to put in cache |
|
| 586 | - * @return boolean true if the test is ok |
|
| 587 | - * @access private |
|
| 588 | - */ |
|
| 589 | - function _writeAndControl($data) |
|
| 590 | - { |
|
| 591 | - $this->_write($data); |
|
| 592 | - $dataRead = $this->_read($data); |
|
| 593 | - return ($dataRead==$data); |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - /** |
|
| 597 | - * Make a control key with the string containing datas |
|
| 598 | - * |
|
| 599 | - * @param string $data data |
|
| 600 | - * @param string $controlType type of control 'md5', 'crc32' or 'strlen' |
|
| 601 | - * @return string control key |
|
| 602 | - * @access private |
|
| 603 | - */ |
|
| 604 | - function _hash($data, $controlType) |
|
| 605 | - { |
|
| 606 | - switch ($controlType) { |
|
| 607 | - case 'md5': |
|
| 608 | - return md5($data); |
|
| 609 | - case 'crc32': |
|
| 610 | - return sprintf('% 32d', crc32($data)); |
|
| 611 | - case 'strlen': |
|
| 612 | - return sprintf('% 32d', strlen($data)); |
|
| 613 | - default: |
|
| 614 | - $this->raiseError('Unknown controlType ! '. |
|
| 615 | - '(available values are only \'md5\', \'crc32\', \'strlen\')', -5); |
|
| 616 | - } |
|
| 617 | - } |
|
| 254 | + /** |
|
| 255 | + * Test if a cache is available and (if yes) return it |
|
| 256 | + * |
|
| 257 | + * @param string $id cache id |
|
| 258 | + * @param string $group name of the cache group |
|
| 259 | + * @param boolean $doNotTestCacheValidity if set to true, the cache |
|
| 260 | + * validity won't be tested |
|
| 261 | + * @return string data of the cache (or false if no cache available) |
|
| 262 | + * @access public |
|
| 263 | + */ |
|
| 264 | + function get($id, $group = 'default', $doNotTestCacheValidity = false) |
|
| 265 | + { |
|
| 266 | + $this->_id = $id; |
|
| 267 | + $this->_group = $group; |
|
| 268 | + $data = false; |
|
| 269 | + if ($this->_caching) { |
|
| 270 | + $this->_setFileName($id, $group); |
|
| 271 | + if ($this->_memoryCaching) { |
|
| 272 | + if (isset($this->_memoryCachingArray[$this->_file])) { |
|
| 273 | + if ($this->_automaticSerialization) { |
|
| 274 | + return unserialize( |
|
| 275 | + $this->_memoryCachingArray[$this->_file]); |
|
| 276 | + } else { |
|
| 277 | + return $this->_memoryCachingArray[$this->_file]; |
|
| 278 | + } |
|
| 279 | + } else { |
|
| 280 | + if ($this->_onlyMemoryCaching) { |
|
| 281 | + return false; |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | + if ($doNotTestCacheValidity) { |
|
| 286 | + if (file_exists($this->_file)) { |
|
| 287 | + $data = $this->_read(); |
|
| 288 | + } |
|
| 289 | + } else { |
|
| 290 | + if (@filemtime($this->_file) > $this->_refreshTime) { |
|
| 291 | + $data = $this->_read(); |
|
| 292 | + } |
|
| 293 | + } |
|
| 294 | + if (($data) and ($this->_memoryCaching)) { |
|
| 295 | + $this->_memoryCacheAdd($this->_file, $data); |
|
| 296 | + } |
|
| 297 | + if ($this->_automaticSerialization && is_string($data)) { |
|
| 298 | + $data = unserialize($data); |
|
| 299 | + } |
|
| 300 | + return $data; |
|
| 301 | + } |
|
| 302 | + return false; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Save some data in a cache file |
|
| 307 | + * |
|
| 308 | + * @param string $data data to put in cache (can be another type than strings |
|
| 309 | + * if automaticSerialization is on) |
|
| 310 | + * @param string $id cache id |
|
| 311 | + * @param string $group name of the cache group |
|
| 312 | + * @return boolean true if no problem |
|
| 313 | + * @access public |
|
| 314 | + */ |
|
| 315 | + function save($data, $id = null, $group = 'default') |
|
| 316 | + { |
|
| 317 | + if ($this->_caching) { |
|
| 318 | + if ($this->_automaticSerialization) { |
|
| 319 | + $data = serialize($data); |
|
| 320 | + } |
|
| 321 | + if (isset($id)) { |
|
| 322 | + $this->_setFileName($id, $group); |
|
| 323 | + } |
|
| 324 | + if ($this->_memoryCaching) { |
|
| 325 | + $this->_memoryCacheAdd($this->_file, $data); |
|
| 326 | + if ($this->_onlyMemoryCaching) { |
|
| 327 | + return true; |
|
| 328 | + } |
|
| 329 | + } |
|
| 330 | + if ($this->_writeControl) { |
|
| 331 | + if (!$this->_writeAndControl($data)) { |
|
| 332 | + @touch($this->_file, time() - 2*abs($this->_lifeTime)); |
|
| 333 | + return false; |
|
| 334 | + } else { |
|
| 335 | + return true; |
|
| 336 | + } |
|
| 337 | + } else { |
|
| 338 | + return $this->_write($data); |
|
| 339 | + } |
|
| 340 | + } |
|
| 341 | + return false; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Remove a cache file |
|
| 346 | + * |
|
| 347 | + * @param string $id cache id |
|
| 348 | + * @param string $group name of the cache group |
|
| 349 | + * @return boolean true if no problem |
|
| 350 | + * @access public |
|
| 351 | + */ |
|
| 352 | + function remove($id, $group = 'default') |
|
| 353 | + { |
|
| 354 | + $this->_setFileName($id, $group); |
|
| 355 | + if (!@unlink($this->_file)) { |
|
| 356 | + $this->raiseError('TCache_Lite : Unable to remove cache !', -3); |
|
| 357 | + return false; |
|
| 358 | + } |
|
| 359 | + return true; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Clean the cache |
|
| 364 | + * |
|
| 365 | + * if no group is specified all cache files will be destroyed |
|
| 366 | + * else only cache files of the specified group will be destroyed |
|
| 367 | + * |
|
| 368 | + * @param string $group name of the cache group |
|
| 369 | + * @return boolean true if no problem |
|
| 370 | + * @access public |
|
| 371 | + */ |
|
| 372 | + function clean($group = false) |
|
| 373 | + { |
|
| 374 | + if ($this->_fileNameProtection) { |
|
| 375 | + $motif = ($group) ? 'cache_'.md5($group).'_' : 'cache_'; |
|
| 376 | + } else { |
|
| 377 | + $motif = ($group) ? 'cache_'.$group.'_' : 'cache_'; |
|
| 378 | + } |
|
| 379 | + if ($this->_memoryCaching) { |
|
| 380 | + while (list($key, $value) = each($this->_memoryCaching)) { |
|
| 381 | + if (strpos($key, $motif, 0)) { |
|
| 382 | + unset($this->_memoryCaching[$key]); |
|
| 383 | + $this->_memoryCachingCounter = |
|
| 384 | + $this->_memoryCachingCounter - 1; |
|
| 385 | + } |
|
| 386 | + } |
|
| 387 | + if ($this->_onlyMemoryCaching) { |
|
| 388 | + return true; |
|
| 389 | + } |
|
| 390 | + } |
|
| 391 | + if (!($dh = opendir($this->_cacheDir))) { |
|
| 392 | + $this->raiseError('TCache_Lite : Unable to open cache directory !'); |
|
| 393 | + return false; |
|
| 394 | + } |
|
| 395 | + while ($file = readdir($dh)) { |
|
| 396 | + if (($file != '.') && ($file != '..')) { |
|
| 397 | + $file = $this->_cacheDir . $file; |
|
| 398 | + if (is_file($file)) { |
|
| 399 | + if (strpos($file, $motif, 0)) { |
|
| 400 | + if (!@unlink($file)) { |
|
| 401 | + $this->raiseError('Cache_Lite : Unable to remove cache !', -3); |
|
| 402 | + return false; |
|
| 403 | + } |
|
| 404 | + } |
|
| 405 | + } |
|
| 406 | + } |
|
| 407 | + } |
|
| 408 | + return true; |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + /** |
|
| 412 | + * Set a new life time |
|
| 413 | + * |
|
| 414 | + * @param int $newLifeTime new life time (in seconds) |
|
| 415 | + * @access public |
|
| 416 | + */ |
|
| 417 | + function setLifeTime($newLifeTime) |
|
| 418 | + { |
|
| 419 | + $this->_lifeTime = $newLifeTime; |
|
| 420 | + $this->_refreshTime = time() - $newLifeTime; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * |
|
| 425 | + * @access public |
|
| 426 | + */ |
|
| 427 | + function saveMemoryCachingState($id, $group = 'default') |
|
| 428 | + { |
|
| 429 | + if ($this->_caching) { |
|
| 430 | + $array = array( |
|
| 431 | + 'counter' => $this->_memoryCachingCounter, |
|
| 432 | + 'array' => $this->_memoryCachingState |
|
| 433 | + ); |
|
| 434 | + $data = serialize($array); |
|
| 435 | + $this->save($data, $id, $group); |
|
| 436 | + } |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /** |
|
| 440 | + * |
|
| 441 | + * @access public |
|
| 442 | + */ |
|
| 443 | + function getMemoryCachingState($id, $group = 'default', |
|
| 444 | + $doNotTestCacheValidity = false) |
|
| 445 | + { |
|
| 446 | + if ($this->_caching) { |
|
| 447 | + if ($data = $this->get($id, $group, $doNotTestCacheValidity)) |
|
| 448 | + { |
|
| 449 | + $array = unserialize($data); |
|
| 450 | + $this->_memoryCachingCounter = $array['counter']; |
|
| 451 | + $this->_memoryCachingArray = $array['array']; |
|
| 452 | + } |
|
| 453 | + } |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * Return the cache last modification time |
|
| 458 | + * |
|
| 459 | + * BE CAREFUL : THIS METHOD IS FOR HACKING ONLY ! |
|
| 460 | + * |
|
| 461 | + * @return int last modification time |
|
| 462 | + */ |
|
| 463 | + function lastModified() { |
|
| 464 | + return filemtime($this->cache->_file); |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * Trigger a PEAR error |
|
| 469 | + * |
|
| 470 | + * To improve performances, the PEAR.php file is included dynamically. |
|
| 471 | + * The file is so included only when an error is triggered. So, in most |
|
| 472 | + * cases, the file isn't included and perfs are much better. |
|
| 473 | + * |
|
| 474 | + * @param string $msg error message |
|
| 475 | + * @param int $code error code |
|
| 476 | + * @access public |
|
| 477 | + */ |
|
| 478 | + function raiseError($msg, $code) |
|
| 479 | + { |
|
| 480 | + throw new Exception($msg); |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + // --- Private methods --- |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * |
|
| 487 | + * @access private |
|
| 488 | + */ |
|
| 489 | + function _memoryCacheAdd($id, $data) |
|
| 490 | + { |
|
| 491 | + $this->_memoryCachingArray[$this->_file] = $data; |
|
| 492 | + if ($this->_memoryCachingCounter >= $this->_memoryCachingLimit) { |
|
| 493 | + list($key, $value) = each($this->_memoryCachingArray); |
|
| 494 | + unset($this->_memoryCachingArray[$key]); |
|
| 495 | + } else { |
|
| 496 | + $this->_memoryCachingCounter = $this->_memoryCachingCounter + 1; |
|
| 497 | + } |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + /** |
|
| 501 | + * Make a file name (with path) |
|
| 502 | + * |
|
| 503 | + * @param string $id cache id |
|
| 504 | + * @param string $group name of the group |
|
| 505 | + * @access private |
|
| 506 | + */ |
|
| 507 | + function _setFileName($id, $group) |
|
| 508 | + { |
|
| 509 | + if ($this->_fileNameProtection) { |
|
| 510 | + $this->_file = ($this->_cacheDir.'cache_'.md5($group).'_' |
|
| 511 | + .md5($id)); |
|
| 512 | + } else { |
|
| 513 | + $this->_file = $this->_cacheDir.'cache_'.$group.'_'.$id; |
|
| 514 | + } |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + function getCacheFile() |
|
| 518 | + { |
|
| 519 | + return $this->_file; |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + /** |
|
| 523 | + * Read the cache file and return the content |
|
| 524 | + * |
|
| 525 | + * @return string content of the cache file |
|
| 526 | + * @access private |
|
| 527 | + */ |
|
| 528 | + function _read() |
|
| 529 | + { |
|
| 530 | + $fp = @fopen($this->_file, "rb"); |
|
| 531 | + if ($this->_fileLocking) @flock($fp, LOCK_SH); |
|
| 532 | + if ($fp) { |
|
| 533 | + // because the filesize can be cached by PHP itself... |
|
| 534 | + clearstatcache(); |
|
| 535 | + $length = @filesize($this->_file); |
|
| 536 | + if ($this->_readControl) { |
|
| 537 | + $hashControl = @fread($fp, 32); |
|
| 538 | + $length = $length - 32; |
|
| 539 | + } |
|
| 540 | + $data = @fread($fp, $length); |
|
| 541 | + if ($this->_fileLocking) @flock($fp, LOCK_UN); |
|
| 542 | + @fclose($fp); |
|
| 543 | + if ($this->_readControl) { |
|
| 544 | + $hashData = $this->_hash($data, $this->_readControlType); |
|
| 545 | + if ($hashData != $hashControl) { |
|
| 546 | + @touch($this->_file, time() - 2*abs($this->_lifeTime)); |
|
| 547 | + return false; |
|
| 548 | + } |
|
| 549 | + } |
|
| 550 | + return $data; |
|
| 551 | + } |
|
| 552 | + $this->raiseError('Cache_Lite : Unable to read cache !', -2); |
|
| 553 | + return false; |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + /** |
|
| 557 | + * Write the given data in the cache file |
|
| 558 | + * |
|
| 559 | + * @param string $data data to put in cache |
|
| 560 | + * @return boolean true if ok |
|
| 561 | + * @access private |
|
| 562 | + */ |
|
| 563 | + function _write($data) |
|
| 564 | + { |
|
| 565 | + $fp = @fopen($this->_file, "wb"); |
|
| 566 | + if ($fp) { |
|
| 567 | + if ($this->_fileLocking) @flock($fp, LOCK_EX); |
|
| 568 | + if ($this->_readControl) { |
|
| 569 | + @fwrite($fp, $this->_hash($data, $this->_readControlType), 32); |
|
| 570 | + } |
|
| 571 | + $len = strlen($data); |
|
| 572 | + @fwrite($fp, $data, $len); |
|
| 573 | + if ($this->_fileLocking) @flock($fp, LOCK_UN); |
|
| 574 | + @fclose($fp); |
|
| 575 | + return true; |
|
| 576 | + } |
|
| 577 | + $this->raiseError('Cache_Lite : Unable to write cache !', -1); |
|
| 578 | + return false; |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + /** |
|
| 582 | + * Write the given data in the cache file and control it just after to avoid |
|
| 583 | + * corrupted cache entries |
|
| 584 | + * |
|
| 585 | + * @param string $data data to put in cache |
|
| 586 | + * @return boolean true if the test is ok |
|
| 587 | + * @access private |
|
| 588 | + */ |
|
| 589 | + function _writeAndControl($data) |
|
| 590 | + { |
|
| 591 | + $this->_write($data); |
|
| 592 | + $dataRead = $this->_read($data); |
|
| 593 | + return ($dataRead==$data); |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + /** |
|
| 597 | + * Make a control key with the string containing datas |
|
| 598 | + * |
|
| 599 | + * @param string $data data |
|
| 600 | + * @param string $controlType type of control 'md5', 'crc32' or 'strlen' |
|
| 601 | + * @return string control key |
|
| 602 | + * @access private |
|
| 603 | + */ |
|
| 604 | + function _hash($data, $controlType) |
|
| 605 | + { |
|
| 606 | + switch ($controlType) { |
|
| 607 | + case 'md5': |
|
| 608 | + return md5($data); |
|
| 609 | + case 'crc32': |
|
| 610 | + return sprintf('% 32d', crc32($data)); |
|
| 611 | + case 'strlen': |
|
| 612 | + return sprintf('% 32d', strlen($data)); |
|
| 613 | + default: |
|
| 614 | + $this->raiseError('Unknown controlType ! '. |
|
| 615 | + '(available values are only \'md5\', \'crc32\', \'strlen\')', -5); |
|
| 616 | + } |
|
| 617 | + } |
|
| 618 | 618 | |
| 619 | 619 | } |
| 620 | 620 | |
@@ -1,13 +1,13 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * TStyleSheet class file. |
|
| 4 | - * |
|
| 5 | - * @author Wei Zhuo <weizhuo[at]gmail[dot]com> |
|
| 6 | - * @link https://github.com/pradosoft/prado |
|
| 7 | - * @copyright Copyright © 2005-2016 The PRADO Group |
|
| 8 | - * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT |
|
| 9 | - * @package System.Web.UI.WebControls |
|
| 10 | - */ |
|
| 3 | + * TStyleSheet class file. |
|
| 4 | + * |
|
| 5 | + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> |
|
| 6 | + * @link https://github.com/pradosoft/prado |
|
| 7 | + * @copyright Copyright © 2005-2016 The PRADO Group |
|
| 8 | + * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT |
|
| 9 | + * @package System.Web.UI.WebControls |
|
| 10 | + */ |
|
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * TStyleSheet class. |