Cron.php
4 years ago
Ecommerce.php
4 years ago
Language.php
4 years ago
Logger.php
4 years ago
MobileDetect.php
4 years ago
Options.php
4 years ago
Plugin.php
4 years ago
Properties.php
4 years ago
Tracking.php
4 years ago
Utils.php
4 years ago
Properties.php
206 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | |
| 4 | class TCMP_Properties { |
| 5 | var $data; |
| 6 | var $autoPush; |
| 7 | |
| 8 | function __construct() { |
| 9 | $this->data=array(); |
| 10 | $this->autoPush=TRUE; |
| 11 | } |
| 12 | |
| 13 | public function hasKeys() { |
| 14 | return (count($this->data)>0); |
| 15 | } |
| 16 | public function existsKey($key) { |
| 17 | return (isset($this->data[$key]) && $this->data[$key]!=''); |
| 18 | } |
| 19 | |
| 20 | public function load($file) { |
| 21 | $bundle=array(); |
| 22 | if(!file_exists($file)) { |
| 23 | return $bundle; |
| 24 | } |
| 25 | $file=file_get_contents($file); |
| 26 | if($file!=NULL && strlen($file)>0) { |
| 27 | $file=str_replace("\r\n", "\n", $file); |
| 28 | $file=str_replace("\n\n", "\n", $file); |
| 29 | $file=explode("\n", $file); |
| 30 | |
| 31 | foreach($file as $row) { |
| 32 | $index=strpos($row, "="); |
| 33 | if($index===FALSE) continue; |
| 34 | |
| 35 | $k=trim(substr($row, 0, $index)); |
| 36 | $v=trim(substr($row, $index+1)); |
| 37 | if($v!='') { |
| 38 | $bundle[$k]=$v; |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | $this->data=$bundle; |
| 43 | return $bundle; |
| 44 | } |
| 45 | public function store($file) { |
| 46 | ksort($this->data); |
| 47 | $buffer=''; |
| 48 | foreach($this->data as $k=>$v) { |
| 49 | if($buffer!='') { |
| 50 | $buffer.="\r\n"; |
| 51 | } |
| 52 | $buffer.=$k.'='.$v; |
| 53 | } |
| 54 | $bytes=file_put_contents($file, $buffer); |
| 55 | return ($bytes>0); |
| 56 | } |
| 57 | |
| 58 | public function pushValue($t, $k, $v) { |
| 59 | $v=$this->encode($t, $v); |
| 60 | $this->data[$k]=$v; |
| 61 | } |
| 62 | public function pushString($k, $v) { |
| 63 | $this->pushValue('s', $k, $v); |
| 64 | } |
| 65 | public function pushBoolean($k, $v) { |
| 66 | $this->pushValue('b', $k, $v); |
| 67 | } |
| 68 | public function pushInt($k, $v) { |
| 69 | $this->pushValue('i', $k, $v); |
| 70 | } |
| 71 | public function pushFloat($k, $v) { |
| 72 | $this->pushValue('f', $k, $v); |
| 73 | } |
| 74 | public function pushDate($k, $v) { |
| 75 | $this->pushValue('d', $k, $v); |
| 76 | } |
| 77 | public function pushArray($k, $v) { |
| 78 | $this->pushValue('a', $k, $v); |
| 79 | } |
| 80 | public function pushAssocArray($k, $v) { |
| 81 | $this->pushValue('aa', $k, $v); |
| 82 | } |
| 83 | |
| 84 | private function encode($t, $v) { |
| 85 | global $tcmp; |
| 86 | switch (strtolower($t)) { |
| 87 | case 's': |
| 88 | break; |
| 89 | case 'i': |
| 90 | $v=intval($v); |
| 91 | break; |
| 92 | case 'f': |
| 93 | $v=round(floatval($v), 2); |
| 94 | break; |
| 95 | case 'd': |
| 96 | $v=$tcmp->Utils->formatDatetime($v); |
| 97 | break; |
| 98 | case 'b': |
| 99 | $v=($tcmp->Utils->isTrue($v) ? 'true' : 'false'); |
| 100 | break; |
| 101 | case 'a': |
| 102 | $v=$tcmp->Utils->toArray($v); |
| 103 | $v=implode('|', $v); |
| 104 | break; |
| 105 | case 'aa': |
| 106 | $array=$tcmp->Utils->toArray($v); |
| 107 | $buffer=''; |
| 108 | foreach($array as $k=>$v) { |
| 109 | if($buffer!='') { |
| 110 | $buffer.='|'; |
| 111 | } |
| 112 | $buffer.=$k.'='.$v; |
| 113 | } |
| 114 | $v=$array; |
| 115 | break; |
| 116 | } |
| 117 | return $v; |
| 118 | } |
| 119 | private function decode($t, $v) { |
| 120 | global $tcmp; |
| 121 | $v=trim($v); |
| 122 | switch (strtolower($t)) { |
| 123 | case 's': |
| 124 | break; |
| 125 | case 'i': |
| 126 | $v=intval($v); |
| 127 | break; |
| 128 | case 'f': |
| 129 | $v=round(floatval($v), 2); |
| 130 | break; |
| 131 | case 'd': |
| 132 | $v=$tcmp->Utils->parseDateToTime($v); |
| 133 | break; |
| 134 | case 'b': |
| 135 | $v=$tcmp->Utils->isTrue($v); |
| 136 | break; |
| 137 | case 'a': |
| 138 | $v=$tcmp->Utils->toArray($v); |
| 139 | break; |
| 140 | case 'aa': |
| 141 | $v=$tcmp->Utils->toArray($v); |
| 142 | $array=implode('|', $v); |
| 143 | $t=array(); |
| 144 | foreach($array as $v) { |
| 145 | $v=explode('=', $v); |
| 146 | if(count($v)==1) { |
| 147 | $v[]=''; |
| 148 | } |
| 149 | $t[trim($v[0])]=trim($v[1]); |
| 150 | } |
| 151 | $v=$t; |
| 152 | break; |
| 153 | } |
| 154 | return $v; |
| 155 | } |
| 156 | |
| 157 | protected function getValue($t, $key, $default) { |
| 158 | $v=$default; |
| 159 | if(isset($this->data[$key])) { |
| 160 | $v=$this->data[$key]; |
| 161 | } elseif($this->autoPush) { |
| 162 | $this->pushValue($t, $key, $default); |
| 163 | } |
| 164 | $v=$this->decode($t, $v); |
| 165 | return $v; |
| 166 | } |
| 167 | public function getString($key, $default='') { |
| 168 | return $this->getValue('s', $key, $default); |
| 169 | } |
| 170 | public function getFile($key, $default=FALSE) { |
| 171 | global $tcmp; |
| 172 | $file=$this->getString($key, $default); |
| 173 | if($file!==$default && $file!=='' && $file!==FALSE) { |
| 174 | if(!file_exists($file)) { |
| 175 | $file=$default; |
| 176 | } |
| 177 | } |
| 178 | if(is_dir($file)) { |
| 179 | $file=str_replace("\\", DIRECTORY_SEPARATOR, $file); |
| 180 | $file=str_replace("/", DIRECTORY_SEPARATOR, $file); |
| 181 | if(!$tcmp->Utils->endsWith($file, DIRECTORY_SEPARATOR)) { |
| 182 | $file.=DIRECTORY_SEPARATOR; |
| 183 | } |
| 184 | } |
| 185 | return $file; |
| 186 | } |
| 187 | public function getInt($key, $default=0) { |
| 188 | return $this->getValue('i', $key, $default); |
| 189 | } |
| 190 | public function getFloat($key, $default=0) { |
| 191 | return $this->getValue('f', $key, $default); |
| 192 | } |
| 193 | public function getBoolean($key, $default=FALSE) { |
| 194 | return $this->getValue('b', $key, $default); |
| 195 | } |
| 196 | public function getDate($key, $default=FALSE) { |
| 197 | return $this->getValue('d', $key, $default); |
| 198 | } |
| 199 | public function getArray($key, $default=FALSE) { |
| 200 | return $this->getValue('a', $key, $default); |
| 201 | } |
| 202 | public function getAssocArray($key, $default=FALSE) { |
| 203 | return $this->getValue('aa', $key, $default); |
| 204 | } |
| 205 | } |
| 206 |