PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.10.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.10.0
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / app / Services / Libs / FileSystem.php

FileSystem.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.10.0, at app/Services/Libs/FileSystem.php

190 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\App\Services\Libs;
4
5 /**
6 * @method string getDir()
7 * @method mixed get(string $file)
8 * @method string getAbsolutePathOfFile(string $file)
9 * @method array uploadFromRequest()
10 * @method array put(array $files)
11 * @method void delete(array|string $files)
12 * @method void overrideUploadDir()
13 * @method array setCustomUploadDir(array $param)
14 * @method array renameFileName(array $file)
15 */
16 class FileSystem
17 {
18 /**
19 * Read file content from custom upload dir of this application
20 * @return string [path]
21 */
22 public function _get($file)
23 {
24 $arr = explode('/', $file);
25 $fileName = end($arr);
26 return file_get_contents($this->getDir() . '/' . $fileName); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
27 }
28
29 /**
30 * Get custom upload dir name of this application
31 * @return string [directory path]
32 */
33 public function _getDir()
34 {
35 $uploadDir = wp_upload_dir();
36
37 $folderName = apply_filters('fluent_community/upload_folder_name', FLUENT_COMMUNITY_UPLOAD_DIR);
38
39 return $uploadDir['basedir'] . $folderName;
40 }
41
42 /**
43 * Get absolute path of file using custom upload dir name of this application
44 * @return string [file path]
45 */
46 public function _getAbsolutePathOfFile($file)
47 {
48 return $this->_getDir() . '/' . $file;
49 }
50
51 /**
52 * Upload files into custom upload dir of this application
53 * @return array
54 */
55 public function _uploadFromRequest()
56 {
57 return $this->_put(fluentCommunityApp('request')->files());
58 }
59
60 /**
61 * Upload files into custom upload dir of this application
62 * @param array $files
63 * @return array
64 */
65 public function _put($files)
66 {
67 if (!function_exists('wp_handle_upload')) {
68 require_once(ABSPATH . 'wp-admin/includes/file.php');
69 }
70
71 $this->overrideUploadDir();
72
73 $uploadOverrides = ['test_form' => false];
74
75 $uploadedFiles = [];
76
77 foreach ((array)$files as $file) {
78 $filesArray = $file->toArray();
79 $originalName = $filesArray['name'];
80 $uploadedFile = \wp_handle_upload($filesArray, $uploadOverrides);
81
82 if (isset($uploadedFile['error'])) {
83 $uploadedFiles[] = new \WP_Error('upload_error', $uploadedFile['error']);
84 continue;
85 }
86
87 $uploadedFile['original_name'] = $originalName;
88
89 $uploadedFiles[] = $uploadedFile;
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 wp_delete_file($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
137 $folderName = apply_filters('fluent_community/upload_folder_name', FLUENT_COMMUNITY_UPLOAD_DIR);
138
139 $param['url'] = $param['baseurl'] . '/' . $folderName;
140
141 $param['path'] = $param['basedir'] . '/' . $folderName;
142
143 if (!is_dir($param['path'])) {
144 mkdir($param['path'], 0755); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir
145 $htaccessContent = '# END FluentCommunity\n# Disable parsing of PHP for some server configurations.\n<Files *>\nSetHandler none\nSetHandler default-handler\nOptions -ExecCGI\nRemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo\n</Files>\n<IfModule mod_php5.c>\nphp_flag engine off\n</IfModule>\n# END FluentCommunity';
146
147 file_put_contents($param['basedir'] . '/' . $folderName . '/.htaccess', $htaccessContent); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
148 }
149
150 if (!file_exists($param['basedir'] . '/' . $folderName . '/index.php')) {
151
152 $indexStubContent = "<?php\n// Silence is golden.\n";
153
154 file_put_contents($param['basedir'] . '/' . $folderName . '/index.php', $indexStubContent); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
155 }
156
157 return $param;
158 }
159
160 /**
161 * Rename the uploaded file name before saving
162 * @param array $file
163 * @return array $file
164 */
165 public function _renameFileName($file)
166 {
167 $prefix = 'fluentcom-' . wp_generate_password(32, false) . '-fluentcom-';
168 $originalName = $file['name'];
169 $file['name'] = $prefix . $file['name'];
170 $file['name'] = apply_filters('fluent_community/generated_upload_file_name', $file['name'], $originalName, $file);
171 return $file;
172 }
173
174 public static function __callStatic($method, $params)
175 {
176 $instance = new static;
177
178 return call_user_func_array([$instance, $method], $params);
179 }
180
181 public function __call($method, $params)
182 {
183 $hiddenMethod = "_" . $method;
184
185 $method = method_exists($this, $hiddenMethod) ? $hiddenMethod : $method;
186
187 return call_user_func_array([$this, $method], $params);
188 }
189 }
190