1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of phpFastCache. |
5
|
|
|
* |
6
|
|
|
* @license MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
9
|
|
|
* |
10
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace phpFastCache\Core; |
16
|
|
|
|
17
|
|
|
use phpFastCache\Exceptions\phpFastCacheCoreException; |
18
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverException; |
19
|
|
|
use phpFastCache\Util\Directory; |
20
|
|
|
|
21
|
|
|
trait PathSeekerTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
public $tmp = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param bool $readonly |
30
|
|
|
* @return string |
31
|
|
|
* @throws phpFastCacheDriverException |
32
|
|
|
*/ |
33
|
|
|
public function getPath($readonly = false) |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Get the base system temporary directory |
37
|
|
|
*/ |
38
|
|
|
$tmp_dir = rtrim(ini_get('upload_tmp_dir') ?: sys_get_temp_dir(), '\\/') . DIRECTORY_SEPARATOR . 'phpfastcache'; |
39
|
|
|
/** |
40
|
|
|
* Calculate the security key |
41
|
|
|
*/ |
42
|
|
|
{ |
43
|
|
|
$securityKey = array_key_exists('securityKey', $this->config) ? $this->config[ 'securityKey' ] : ''; |
44
|
|
|
if (!$securityKey || $securityKey === 'auto') { |
45
|
|
|
if (isset($_SERVER[ 'HTTP_HOST' ])) { |
46
|
|
|
$securityKey = preg_replace('/^www./', '', strtolower(str_replace(':', '_', $_SERVER[ 'HTTP_HOST' ]))); |
47
|
|
|
} else { |
48
|
|
|
$securityKey = ($this->isPHPModule() ? 'web' : 'cli'); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
if ($securityKey !== '') { |
52
|
|
|
$securityKey .= '/'; |
53
|
|
|
} |
54
|
|
|
$securityKey = static::cleanFileName($securityKey); |
55
|
|
|
} |
56
|
|
|
/** |
57
|
|
|
* Extends the temporary directory |
58
|
|
|
* with the security key and the driver name |
59
|
|
|
*/ |
60
|
|
|
$tmp_dir = rtrim($tmp_dir, '/') . DIRECTORY_SEPARATOR; |
61
|
|
|
if (empty($this->config[ 'path' ]) || !is_string($this->config[ 'path' ])) { |
62
|
|
|
$path = $tmp_dir; |
63
|
|
|
} else { |
64
|
|
|
$path = rtrim($this->config[ 'path' ], '/') . DIRECTORY_SEPARATOR; |
65
|
|
|
} |
66
|
|
|
$path_suffix = $securityKey . DIRECTORY_SEPARATOR . $this->getDriverName(); |
67
|
|
|
$full_path = Directory::getAbsolutePath($path . $path_suffix); |
68
|
|
|
$full_path_tmp = Directory::getAbsolutePath($tmp_dir . $path_suffix); |
69
|
|
|
$full_path_hash = md5($full_path); |
70
|
|
|
/** |
71
|
|
|
* In readonly mode we only attempt |
72
|
|
|
* to verify if the directory exists |
73
|
|
|
* or not, if it does not then we |
74
|
|
|
* return the temp dir |
75
|
|
|
*/ |
76
|
|
|
if ($readonly === true) { |
77
|
|
|
if(!@file_exists($full_path) || !@is_writable($full_path)){ |
78
|
|
|
return $full_path_tmp; |
79
|
|
|
} |
80
|
|
|
return $full_path; |
81
|
|
|
}else{ |
82
|
|
|
if (!isset($this->tmp[ $full_path_hash ]) || (!@file_exists($full_path) || !@is_writable($full_path))) { |
83
|
|
|
if (!@file_exists($full_path)) { |
84
|
|
|
@mkdir($full_path, $this->setChmodAuto(), true); |
85
|
|
|
} elseif (!@is_writable($full_path)) { |
86
|
|
|
if (!@chmod($full_path, $this->setChmodAuto())) |
87
|
|
|
{ |
88
|
|
|
/** |
89
|
|
|
* Switch back to tmp dir |
90
|
|
|
* again if the path is not writable |
91
|
|
|
*/ |
92
|
|
|
$full_path = $full_path_tmp; |
93
|
|
|
if (!@file_exists($full_path)) { |
94
|
|
|
if (!@mkdir($full_path, $this->setChmodAuto(), true)) |
95
|
|
|
{ |
96
|
|
|
throw new phpFastCacheDriverException('PLEASE CREATE OR CHMOD ' . $full_path . ' - 0777 OR ANY WRITABLE PERMISSION!'); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->tmp[ $full_path_hash ] = $full_path; |
103
|
|
|
$this->htaccessGen($full_path, array_key_exists('htaccess', $this->config) ? $this->config[ 'htaccess' ] : false); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
return realpath($full_path); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param $keyword |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
protected function encodeFilename($keyword) |
115
|
|
|
{ |
116
|
|
|
return md5($keyword); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function isExpired() |
123
|
|
|
{ |
124
|
|
|
trigger_error(__FUNCTION__ . '() is deprecated, use ExtendedCacheItemInterface::isExpired() instead.', E_USER_DEPRECATED); |
125
|
|
|
|
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
* @throws \phpFastCache\Exceptions\phpFastCacheCoreException |
133
|
|
|
*/ |
134
|
|
|
public function getFileDir() |
135
|
|
|
{ |
136
|
|
|
return $this->getPath() . DIRECTORY_SEPARATOR . self::FILE_DIR; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param $keyword |
141
|
|
|
* @param bool $skip |
142
|
|
|
* @return string |
143
|
|
|
* @throws phpFastCacheDriverException |
144
|
|
|
*/ |
145
|
|
|
private function getFilePath($keyword, $skip = false) |
146
|
|
|
{ |
147
|
|
|
$path = $this->getFileDir(); |
148
|
|
|
|
149
|
|
|
if ($keyword === false) { |
150
|
|
|
return $path; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$filename = $this->encodeFilename($keyword); |
154
|
|
|
$folder = substr($filename, 0, 2); |
155
|
|
|
$path = rtrim($path, '/') . '/' . $folder; |
156
|
|
|
/** |
157
|
|
|
* Skip Create Sub Folders; |
158
|
|
|
*/ |
159
|
|
|
if ($skip == false) { |
|
|
|
|
160
|
|
|
if (!file_exists($path)) { |
161
|
|
|
if (@!mkdir($path, $this->setChmodAuto(), true)) { |
162
|
|
|
throw new phpFastCacheDriverException('PLEASE CHMOD ' . $this->getPath() . ' - ' . $this->setChmodAuto() . ' OR ANY WRITABLE PERMISSION!'); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $path . '/' . $filename . '.txt'; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param $this ->config |
173
|
|
|
* @return int |
174
|
|
|
*/ |
175
|
|
|
public function setChmodAuto() |
176
|
|
|
{ |
177
|
|
|
if (!isset($this->config[ 'default_chmod' ]) || $this->config[ 'default_chmod' ] == '' || is_null($this->config[ 'default_chmod' ])) { |
178
|
|
|
return 0777; |
179
|
|
|
} else { |
180
|
|
|
return $this->config[ 'default_chmod' ]; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param $filename |
186
|
|
|
* @return mixed |
187
|
|
|
*/ |
188
|
|
|
protected static function cleanFileName($filename) |
189
|
|
|
{ |
190
|
|
|
$regex = [ |
191
|
|
|
'/[\?\[\]\/\\\=\<\>\:\;\,\'\"\&\$\#\*\(\)\|\~\`\!\{\}]/', |
192
|
|
|
'/\.$/', |
193
|
|
|
'/^\./', |
194
|
|
|
]; |
195
|
|
|
$replace = ['-', '', '']; |
196
|
|
|
|
197
|
|
|
return trim(preg_replace($regex, $replace, trim($filename)), '-'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param $path |
202
|
|
|
* @param bool $create |
203
|
|
|
* @throws \Exception |
204
|
|
|
*/ |
205
|
|
|
protected function htaccessGen($path, $create = true) |
206
|
|
|
{ |
207
|
|
|
if ($create === true) { |
208
|
|
|
if (!is_writable($path)) { |
209
|
|
|
try { |
210
|
|
|
if(!chmod($path, 0777)){ |
211
|
|
|
throw new phpFastCacheDriverException('Chmod failed on : ' . $path); |
212
|
|
|
} |
213
|
|
|
} catch (phpFastCacheDriverException $e) { |
214
|
|
|
throw new phpFastCacheDriverException('PLEASE CHMOD ' . $path . ' - 0777 OR ANY WRITABLE PERMISSION!', 0, $e); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
if (!file_exists($path . "/.htaccess")) { |
219
|
|
|
$html = "order deny, allow \r\n |
220
|
|
|
deny from all \r\n |
221
|
|
|
allow from 127.0.0.1"; |
222
|
|
|
|
223
|
|
|
$file = @fopen($path . '/.htaccess', 'w+'); |
224
|
|
|
if (!$file) { |
225
|
|
|
throw new phpFastCacheDriverException('PLEASE CHMOD ' . $path . ' - 0777 OR ANY WRITABLE PERMISSION!'); |
226
|
|
|
} |
227
|
|
|
fwrite($file, $html); |
228
|
|
|
fclose($file); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
} |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.