PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.2.0
Fluent Support – Helpdesk & Customer Support Ticket System v2.2.0
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / app / Services / Includes / FileSystem.php

FileSystem.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.2.0, at app/Services/Includes/FileSystem.php

274 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\App\Services\Includes;
4
5 use FluentSupport\Framework\Support\Arr;
6
7 class FileSystem
8 {
9
10 protected $subDir = '';
11
12
13 public function _setSubDir($subDir)
14 {
15 $this->subDir = $subDir;
16 return $this;
17 }
18
19 /**
20 * Read file content from custom upload dir of this application
21 * @return string [path]
22 */
23 public function _get($file)
24 {
25 $arr = explode('/', $file);
26 $fileName = end($arr);
27 return file_get_contents(
28 $this->getDir() . '/' . $fileName
29 );
30 }
31
32 /**
33 * Get custom upload dir name of this application
34 * @return string [directory path]
35 */
36 public function _getDir()
37 {
38 $uploadDir = wp_upload_dir();
39
40 return $uploadDir['basedir'] . DIRECTORY_SEPARATOR . FLUENT_SUPPORT_UPLOAD_DIR;
41 }
42
43 /**
44 * Get absolute path of file using custom upload dir name of this application
45 * @return string [file path]
46 */
47 public function _getAbsolutePathOfFile($file)
48 {
49 return $this->_getDir() . '/' . $file;
50 }
51
52 /**
53 * Upload files into custom upload dir of this application
54 * @return array
55 */
56 public function _uploadFromRequest()
57 {
58 return $this->_put(\FluentSupport\App\Services\Helper::FluentSupport('request')->files());
59 }
60
61 /**
62 * Upload files into custom upload dir of this application
63 * @param array $files
64 * @return array
65 */
66 public function _put($files)
67 {
68 if (!function_exists('wp_handle_upload')) {
69 require_once(ABSPATH . 'wp-admin/includes/file.php');
70 }
71
72 $this->overrideUploadDir();
73
74 $uploadOverrides = ['test_form' => false];
75 $uploadedFiles = [];
76
77 $truncate_files = apply_filters('fluent_support/truncate_uploaded_file_names', false);
78
79 foreach ((array)$files as $file) {
80 $filesArray = $file->toArray();
81
82 if ($truncate_files) {
83 $filesArray['name'] = $this->truncateFileName($filesArray['name']);
84 }
85
86 $extraData = Arr::only($filesArray, ['name', 'size']);
87 $uploadsData = \wp_handle_upload($filesArray, $uploadOverrides);
88 $uploadedFiles[] = array_merge($extraData, $uploadsData);
89 }
90
91 return $uploadedFiles;
92 }
93
94 private function truncateFileName($fileName)
95 {
96 $file_extension = pathinfo($fileName, PATHINFO_EXTENSION);
97 $file_base_name = pathinfo($fileName, PATHINFO_FILENAME);
98
99 $max_base_length = 50 - strlen($file_extension) - 1;
100 if (strlen($file_base_name) > $max_base_length) {
101 $file_base_name = substr($file_base_name, 0, $max_base_length);
102 }
103
104 return $file_base_name . '.' . $file_extension;
105 }
106
107 public function _putAsContent($fileName, $fileContent)
108 {
109 add_filter('upload_dir', [$this, '_setCustomUploadDir']);
110 $item = wp_upload_bits($fileName, null, $fileContent);
111 remove_filter('upload_dir', [$this, '_setCustomUploadDir']);
112 return $item;
113 }
114
115 /**
116 * Copy a file from custom upload directory of this application
117 * @param string $source file path
118 * @param int $ticketId
119 * @return array | boolean
120 */
121 public function _copy($source)
122 {
123 if (!file_exists($source)) {
124 return false;
125 }
126
127 [$destDir, $folderName] = $this->_getCustomDir();
128 $fileName = basename($source);
129 $destFile = $destDir . DIRECTORY_SEPARATOR . $fileName;
130
131 $indexFile = $destDir.DIRECTORY_SEPARATOR.'index.html';
132 if(!is_file($indexFile)) {
133 @file_put_contents($indexFile, '');
134 }
135
136 if (@copy($source, $destFile)) {
137 return $this->_updateUploadDirForCopy($fileName, $folderName);
138 }
139
140 return false;
141 }
142
143 private function _getCustomDir()
144 {
145 $folderName = '';
146 $directory = $this->_getDir();
147
148 if ($this->subDir) {
149 $folderName .= DIRECTORY_SEPARATOR . $this->subDir;
150 $directory .= $folderName;
151 if (!is_dir($directory)) {
152 @mkdir($directory, 0755);
153 // we should add index.php file to prevent directory listing
154 @file_put_contents($directory . DIRECTORY_SEPARATOR . 'index.html', '');
155 }
156 }
157
158 return [$directory, $folderName];
159 }
160
161 private function _updateUploadDirForCopy($fileName, $folderName)
162 {
163 $uploadDir = wp_upload_dir();
164 $destDir = $this->_getDir() . $folderName;
165 $uploadDir['file_path'] = $destDir . DIRECTORY_SEPARATOR . $fileName;
166 $uploadDir['basedir'] = $destDir;
167 $uploadDir['subdir'] = $folderName;
168 $uploadDir['url'] = $uploadDir['baseurl'] . DIRECTORY_SEPARATOR . FLUENT_SUPPORT_UPLOAD_DIR . $folderName . DIRECTORY_SEPARATOR . $fileName;
169 $uploadDir['baseurl'] = $uploadDir['baseurl'] . DIRECTORY_SEPARATOR . FLUENT_SUPPORT_UPLOAD_DIR . $folderName;
170
171
172
173 return $uploadDir;
174 }
175
176 /**
177 * Delete a file from custom upload directory of this application
178 * @param array $files
179 * @return void
180 */
181 public function _delete($files)
182 {
183 $files = (array)$files;
184
185 foreach ($files as $file) {
186 $arr = explode('/', $file);
187 $fileName = end($arr);
188 @unlink($this->getDir() . '/' . $fileName);
189 }
190 }
191
192 /**
193 * Register filters for custom upload dir
194 */
195 public function _overrideUploadDir()
196 {
197 add_filter('wp_handle_upload_prefilter', function ($file) {
198 add_filter('upload_dir', [$this, '_setCustomUploadDir']);
199
200 add_filter('wp_handle_upload', function ($fileinfo) {
201 remove_filter('upload_dir', [$this, '_setCustomUploadDir']);
202 $filePath = $fileinfo['file'];
203 $fileinfo['file'] = basename($filePath);
204 $fileinfo['file_path'] = $filePath;
205 return $fileinfo;
206 });
207
208 return $this->_renameFileName($file);
209 });
210 }
211
212 /**
213 * Set plugin's custom upload dir
214 * @param array $param
215 * @return array $param
216 */
217 public function _setCustomUploadDir($param)
218 {
219 $folderName = '/' . FLUENT_SUPPORT_UPLOAD_DIR;
220
221 if ($this->subDir) {
222 $folderName .= '/' . $this->subDir;
223 $temFile = $param['basedir'] . $folderName . '/index.html';
224 if(!is_file($temFile)) {
225 if (!is_dir($param['basedir'] . $folderName)) {
226 @mkdir($param['basedir'] . $folderName, 0755);
227 }
228 @file_put_contents($temFile, '');
229 }
230 }
231
232 $param['url'] = $param['baseurl'] . $folderName;
233
234 $param['path'] = $param['basedir'] . $folderName;
235
236 if (!is_dir($param['path'])) {
237 @mkdir($param['path'], 0755);
238 // we should add index.php file to prevent directory listing
239 @file_put_contents($param['basedir'] . $folderName . '/index.html', '');
240 }
241
242 return $param;
243 }
244
245 /**
246 * Rename the uploaded file name before saving
247 * @param array $file
248 * @return array $file
249 */
250 public function _renameFileName($file)
251 {
252 $prefix = 'fluent_support-' . md5(uniqid(wp_rand())) . '___';
253 $prefix = apply_filters('fluent_support/uploaded_file_name_prefix', $prefix);
254 $file['name'] = $prefix . $file['name'];
255 return $file;
256 }
257
258 public static function __callStatic($method, $params)
259 {
260 $instance = new static;
261
262 return call_user_func_array([$instance, $method], $params);
263 }
264
265 public function __call($method, $params)
266 {
267 $hiddenMethod = "_" . $method;
268
269 $method = method_exists($this, $hiddenMethod) ? $hiddenMethod : $method;
270
271 return call_user_func_array([$this, $method], $params);
272 }
273 }
274