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