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

210 lines 5.7 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
82 foreach ((array)$files as $file) {
83 $filesArray = $file->toArray();
84 $extraData = Arr::only($filesArray, ['name', 'size']);
85 $uploadsData = \wp_handle_upload($filesArray, $uploadOverrides);
86 $uploadedFiles[] = array_merge($extraData, $uploadsData);
87 }
88
89 return $uploadedFiles;
90 }
91
92 /**
93 * Delete a file from custom upload directory of this application
94 * @param array $files
95 * @return void
96 */
97 public function _delete($files)
98 {
99 $files = (array)$files;
100
101 foreach ($files as $file) {
102 $arr = explode('/', $file);
103 $fileName = end($arr);
104 @unlink($this->getDir() . '/' . $fileName);
105 }
106 }
107
108 /**
109 * Register filters for custom upload dir
110 */
111 public function _overrideUploadDir()
112 {
113 add_filter('wp_handle_upload_prefilter', function ($file) {
114 add_filter('upload_dir', [$this, '_setCustomUploadDir']);
115
116 add_filter('wp_handle_upload', function ($fileinfo) {
117 remove_filter('upload_dir', [$this, '_setCustomUploadDir']);
118 $fileinfo['file'] = basename($fileinfo['file']);
119 return $fileinfo;
120 });
121
122 return $this->_renameFileName($file);
123 });
124 }
125
126 /**
127 * Set plugin's custom upload dir
128 * @param array $param
129 * @return array $param
130 */
131 public function _setCustomUploadDir($param)
132 {
133 $fbsUploadDir = apply_filters('fluent_boards/upload_folder_name', FLUENT_BOARDS_UPLOAD_DIR);
134
135 if ($this->subDir) {
136 $fbsUploadDir .= DIRECTORY_SEPARATOR . $this->subDir;
137 if (!is_dir($param['basedir'] . $fbsUploadDir)) {
138 @mkdir($param['basedir'] . $fbsUploadDir, 0755);
139 }
140 }
141
142 $param['url'] = $param['baseurl'] . DIRECTORY_SEPARATOR . $fbsUploadDir;
143
144 $param['path'] = $param['basedir'] . DIRECTORY_SEPARATOR . $fbsUploadDir;
145
146 return $param;
147 }
148
149 /**
150 * Rename the uploaded file name before saving
151 * @param array $file
152 * @return array $file
153 */
154 public function _renameFileName($file)
155 {
156 $currentTimeStamp = (new \DateTimeImmutable())->getTimestamp();
157 $prefix = $currentTimeStamp . '-';
158 $prefix = apply_filters('fluent_boards/uploaded_file_name_prefix', $prefix);
159 $file['name'] = $prefix . $file['name'];
160
161 return $file;
162 }
163
164 public function _deleteDir($dir)
165 {
166 $dir = $this->getDir() . DIRECTORY_SEPARATOR . $dir;
167 if (is_dir($dir)) {
168 $this->deleteContents($dir);
169 }
170 }
171
172 /**
173 * Delete a directory and its content
174 * Lead Note: this method should be called via scheduler or queue
175 * @param string $dir
176 * @return bool
177 */
178 private function deleteContents($dir)
179 {
180 $files = array_diff(scandir($dir), ['.', '..']);
181 foreach ($files as $file) {
182 if(is_dir("$dir/$file")) {
183 // Recursively delete subdirectory
184 $this->deleteContents("$dir/$file");
185 } else {
186 // Delete file
187 unlink("$dir/$file");
188 }
189 }
190 // Delete the directory itself. If the directory is not empty, it will not be deleted.
191 return rmdir($dir);
192 }
193
194 public static function __callStatic($method, $params)
195 {
196 $instance = new static;
197
198 return call_user_func_array([$instance, $method], $params);
199 }
200
201 public function __call($method, $params)
202 {
203 $hiddenMethod = "_" . $method;
204
205 $method = method_exists($this, $hiddenMethod) ? $hiddenMethod : $method;
206
207 return call_user_func_array([$this, $method], $params);
208 }
209 }
210