PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.95
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.95
2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 1.45 All 41 releases
fluent-boards / app / Services / Libs / FileSystem.php

FileSystem.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.95, at app/Services/Libs/FileSystem.php

237 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBoards\App\Services\Libs;
4
5 use FluentBoards\Framework\Support\Arr;
6
7 class FileSystem
8 {
9 protected $subDir = '';
10
11 public function _setSubDir($subDir)
12 {
13 $this->subDir = $subDir;
14 return $this;
15 }
16 /**
17 * Read file content from custom upload dir of this application
18 * @return string [path]
19 */
20 public function _get($file)
21 {
22 $arr = explode('/', $file);
23 $fileName = end($arr);
24 if ($this->subDir) {
25 $fileName = $this->subDir . DIRECTORY_SEPARATOR . $fileName;
26 }
27
28 $filePath = $this->getDir() . DIRECTORY_SEPARATOR . $fileName;
29
30 // Use WordPress Filesystem API
31 global $wp_filesystem;
32 if (!function_exists('WP_Filesystem')) {
33 require_once(ABSPATH . 'wp-admin/includes/file.php');
34 }
35 WP_Filesystem();
36
37 return $wp_filesystem->get_contents($filePath);
38 }
39
40 /**
41 * Get custom upload dir name of this application
42 * @return string [directory path]
43 */
44 public function _getDir()
45 {
46 $uploadDir = wp_upload_dir();
47
48 $fbsUploadDir = apply_filters('fluent_boards/upload_folder_name', FLUENT_BOARDS_UPLOAD_DIR);
49
50 if ($this->subDir) {
51 return $uploadDir['basedir'] . DIRECTORY_SEPARATOR . $fbsUploadDir. DIRECTORY_SEPARATOR. $this->subDir;
52 }
53
54 return $uploadDir['basedir'] . DIRECTORY_SEPARATOR . $fbsUploadDir;
55 }
56
57 /**
58 * Get absolute path of file using custom upload dir name of this application
59 * @return string [file path]
60 */
61 public function _getAbsolutePathOfFile($file)
62 {
63 return $this->_getDir() . DIRECTORY_SEPARATOR . $file;
64 }
65
66 /**
67 * Upload files into custom upload dir of this application
68 * @return array
69 */
70 public function _uploadFromRequest()
71 {
72 return $this->_put(FluentBoards('request')->files());
73 }
74
75 /**
76 * Upload files into custom upload dir of this application
77 * @param array $files
78 * @return array
79 */
80 public function _put($files)
81 {
82 if (!function_exists('wp_handle_upload')) {
83 require_once(ABSPATH . 'wp-admin/includes/file.php');
84 }
85
86 $this->overrideUploadDir();
87
88 $uploadOverrides = ['test_form' => false];
89 $uploadedFiles = []; // Initialize the array
90
91 if(is_object($files)) {
92 $files = [$files];
93 }
94
95 foreach ((array)$files as $file) {
96
97 $filesArray = $file->toArray();
98
99 // tmp_name is the path to the uploaded file which is required for wp_handle_upload, new framework update
100 // changed the way to get the tmp_name Reference to line # 448 in File.php
101 $filesArray['tmp_name'] = $file->getRealPath();
102
103 $extraData = Arr::only($filesArray, ['name', 'size']);
104 $uploadsData = \wp_handle_upload($filesArray, $uploadOverrides);
105 // Add the full path to the file
106 $uploadsData['full_path'] = $this->_getAbsolutePathOfFile($uploadsData['file']);
107 $uploadedFiles[] = array_merge($extraData, $uploadsData);
108 }
109
110 return $uploadedFiles;
111 }
112
113 /**
114 * Delete a file from custom upload directory of this application
115 * @param array $files
116 * @return void
117 */
118 public function _delete($files)
119 {
120 $files = (array)$files;
121
122 foreach ($files as $file) {
123 $arr = explode('/', $file);
124 $fileName = end($arr);
125 $filePath = $this->getDir() . '/' . $fileName;
126 if (file_exists($filePath)) {
127 wp_delete_file($filePath);
128 }
129 }
130 }
131
132 /**
133 * Register filters for custom upload dir
134 */
135 public function _overrideUploadDir()
136 {
137 add_filter('wp_handle_upload_prefilter', function ($file) {
138 add_filter('upload_dir', [$this, '_setCustomUploadDir']);
139
140 add_filter('wp_handle_upload', function ($fileinfo) {
141 remove_filter('upload_dir', [$this, '_setCustomUploadDir']);
142 $fileinfo['file'] = basename($fileinfo['file']);
143 return $fileinfo;
144 });
145
146 return $this->_renameFileName($file);
147 });
148 }
149
150 /**
151 * Set plugin's custom upload dir
152 * @param array $param
153 * @return array $param
154 */
155 public function _setCustomUploadDir($param)
156 {
157 $fbsUploadDir = apply_filters('fluent_boards/upload_folder_name', FLUENT_BOARDS_UPLOAD_DIR);
158
159 if ($this->subDir) {
160 $fbsUploadDir .= DIRECTORY_SEPARATOR . $this->subDir;
161 if (!is_dir($param['basedir'] . $fbsUploadDir)) {
162 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir -- Creating custom upload subdirectory during upload process, WP_Filesystem not initialized
163 @mkdir($param['basedir'] . $fbsUploadDir, 0755);
164 }
165 }
166
167 $param['url'] = $param['baseurl'] . DIRECTORY_SEPARATOR . $fbsUploadDir;
168
169 $param['path'] = $param['basedir'] . DIRECTORY_SEPARATOR . $fbsUploadDir;
170
171 return $param;
172 }
173
174 /**
175 * Rename the uploaded file name before saving
176 * @param array $file
177 * @return array $file
178 */
179 public function _renameFileName($file)
180 {
181 $currentTimeStamp = (new \DateTimeImmutable())->getTimestamp();
182 $prefix = $currentTimeStamp . '-';
183 $prefix = apply_filters('fluent_boards/uploaded_file_name_prefix', $prefix);
184 $file['name'] = $prefix . $file['name'];
185
186 return $file;
187 }
188
189 public function _deleteDir($dir)
190 {
191 $dir = $this->getDir() . DIRECTORY_SEPARATOR . $dir;
192 if (is_dir($dir)) {
193 $this->deleteContents($dir);
194 }
195 }
196
197 /**
198 * Delete a directory and its content
199 * Lead Note: this method should be called via scheduler or queue
200 * @param string $dir
201 * @return bool
202 */
203 private function deleteContents($dir)
204 {
205 $files = array_diff(scandir($dir), ['.', '..']);
206 foreach ($files as $file) {
207 if(is_dir("$dir/$file")) {
208 // Recursively delete subdirectory
209 $this->deleteContents("$dir/$file");
210 } else {
211 // Delete file
212 // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink -- Recursive directory cleanup, direct file operations required
213 unlink("$dir/$file");
214 }
215 }
216 // Delete the directory itself. If the directory is not empty, it will not be deleted.
217 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- Recursive directory removal, WP_Filesystem not suitable for this operation
218 return rmdir($dir);
219 }
220
221 public static function __callStatic($method, $params)
222 {
223 $instance = new static;
224
225 return call_user_func_array([$instance, $method], $params);
226 }
227
228 public function __call($method, $params)
229 {
230 $hiddenMethod = "_" . $method;
231
232 $method = method_exists($this, $hiddenMethod) ? $hiddenMethod : $method;
233
234 return call_user_func_array([$this, $method], $params);
235 }
236 }
237