PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.40
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.40
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.40, at app/Services/Libs/FileSystem.php

213 lines 5.9 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 return file_get_contents(
28 $this->getDir() . DIRECTORY_SEPARATOR . $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 $fbsUploadDir = apply_filters('fluent_boards/upload_folder_name', FLUENT_BOARDS_UPLOAD_DIR);
41
42 if ($this->subDir) {
43 return $uploadDir['basedir'] . DIRECTORY_SEPARATOR . $fbsUploadDir. DIRECTORY_SEPARATOR. $this->subDir;
44 }
45
46 return $uploadDir['basedir'] . DIRECTORY_SEPARATOR . $fbsUploadDir;
47 }
48
49 /**
50 * Get absolute path of file using custom upload dir name of this application
51 * @return string [file path]
52 */
53 public function _getAbsolutePathOfFile($file)
54 {
55 return $this->_getDir() . DIRECTORY_SEPARATOR . $file;
56 }
57
58 /**
59 * Upload files into custom upload dir of this application
60 * @return array
61 */
62 public function _uploadFromRequest()
63 {
64 return $this->_put(FluentBoards('request')->files());
65 }
66
67 /**
68 * Upload files into custom upload dir of this application
69 * @param array $files
70 * @return array
71 */
72 public function _put($files)
73 {
74 if (!function_exists('wp_handle_upload')) {
75 require_once(ABSPATH . 'wp-admin/includes/file.php');
76 }
77
78 $this->overrideUploadDir();
79
80 $uploadOverrides = ['test_form' => false];
81 $uploadedFiles = []; // Initialize the array
82
83 foreach ((array)$files as $file) {
84 $filesArray = $file->toArray();
85 $extraData = Arr::only($filesArray, ['name', 'size']);
86 $uploadsData = \wp_handle_upload($filesArray, $uploadOverrides);
87 // Add the full path to the file
88 $uploadsData['full_path'] = $this->_getAbsolutePathOfFile($uploadsData['file']);
89 $uploadedFiles[] = array_merge($extraData, $uploadsData);
90 }
91
92 return $uploadedFiles;
93 }
94
95 /**
96 * Delete a file from custom upload directory of this application
97 * @param array $files
98 * @return void
99 */
100 public function _delete($files)
101 {
102 $files = (array)$files;
103
104 foreach ($files as $file) {
105 $arr = explode('/', $file);
106 $fileName = end($arr);
107 @unlink($this->getDir() . '/' . $fileName);
108 }
109 }
110
111 /**
112 * Register filters for custom upload dir
113 */
114 public function _overrideUploadDir()
115 {
116 add_filter('wp_handle_upload_prefilter', function ($file) {
117 add_filter('upload_dir', [$this, '_setCustomUploadDir']);
118
119 add_filter('wp_handle_upload', function ($fileinfo) {
120 remove_filter('upload_dir', [$this, '_setCustomUploadDir']);
121 $fileinfo['file'] = basename($fileinfo['file']);
122 return $fileinfo;
123 });
124
125 return $this->_renameFileName($file);
126 });
127 }
128
129 /**
130 * Set plugin's custom upload dir
131 * @param array $param
132 * @return array $param
133 */
134 public function _setCustomUploadDir($param)
135 {
136 $fbsUploadDir = apply_filters('fluent_boards/upload_folder_name', FLUENT_BOARDS_UPLOAD_DIR);
137
138 if ($this->subDir) {
139 $fbsUploadDir .= DIRECTORY_SEPARATOR . $this->subDir;
140 if (!is_dir($param['basedir'] . $fbsUploadDir)) {
141 @mkdir($param['basedir'] . $fbsUploadDir, 0755);
142 }
143 }
144
145 $param['url'] = $param['baseurl'] . DIRECTORY_SEPARATOR . $fbsUploadDir;
146
147 $param['path'] = $param['basedir'] . DIRECTORY_SEPARATOR . $fbsUploadDir;
148
149 return $param;
150 }
151
152 /**
153 * Rename the uploaded file name before saving
154 * @param array $file
155 * @return array $file
156 */
157 public function _renameFileName($file)
158 {
159 $currentTimeStamp = (new \DateTimeImmutable())->getTimestamp();
160 $prefix = $currentTimeStamp . '-';
161 $prefix = apply_filters('fluent_boards/uploaded_file_name_prefix', $prefix);
162 $file['name'] = $prefix . $file['name'];
163
164 return $file;
165 }
166
167 public function _deleteDir($dir)
168 {
169 $dir = $this->getDir() . DIRECTORY_SEPARATOR . $dir;
170 if (is_dir($dir)) {
171 $this->deleteContents($dir);
172 }
173 }
174
175 /**
176 * Delete a directory and its content
177 * Lead Note: this method should be called via scheduler or queue
178 * @param string $dir
179 * @return bool
180 */
181 private function deleteContents($dir)
182 {
183 $files = array_diff(scandir($dir), ['.', '..']);
184 foreach ($files as $file) {
185 if(is_dir("$dir/$file")) {
186 // Recursively delete subdirectory
187 $this->deleteContents("$dir/$file");
188 } else {
189 // Delete file
190 unlink("$dir/$file");
191 }
192 }
193 // Delete the directory itself. If the directory is not empty, it will not be deleted.
194 return rmdir($dir);
195 }
196
197 public static function __callStatic($method, $params)
198 {
199 $instance = new static;
200
201 return call_user_func_array([$instance, $method], $params);
202 }
203
204 public function __call($method, $params)
205 {
206 $hiddenMethod = "_" . $method;
207
208 $method = method_exists($this, $hiddenMethod) ? $hiddenMethod : $method;
209
210 return call_user_func_array([$this, $method], $params);
211 }
212 }
213