PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.7.7
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.7.7
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.7.7, at app/Services/Libs/FileSystem.php

189 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 }
85
86 $uploadedFile['original_name'] = $originalName;
87
88 $uploadedFiles[] = $uploadedFile;
89 }
90
91 return $uploadedFiles;
92 }
93
94 /**
95 * Delete a file from custom upload directory of this application
96 * @param array $files
97 * @return void
98 */
99 public function _delete($files)
100 {
101 $files = (array)$files;
102
103 foreach ($files as $file) {
104 $arr = explode('/', $file);
105 $fileName = end($arr);
106 wp_delete_file($this->getDir() . '/' . $fileName);
107 }
108 }
109
110 /**
111 * Register filters for custom upload dir
112 */
113 public function _overrideUploadDir()
114 {
115 add_filter('wp_handle_upload_prefilter', function ($file) {
116 add_filter('upload_dir', [$this, '_setCustomUploadDir']);
117
118 add_filter('wp_handle_upload', function ($fileinfo) {
119 remove_filter('upload_dir', [$this, '_setCustomUploadDir']);
120 $fileinfo['file'] = basename($fileinfo['file']);
121 return $fileinfo;
122 });
123
124 return $this->_renameFileName($file);
125 });
126 }
127
128 /**
129 * Set plugin's custom upload dir
130 * @param array $param
131 * @return array $param
132 */
133 public function _setCustomUploadDir($param)
134 {
135
136 $folderName = apply_filters('fluent_community/upload_folder_name', FLUENT_COMMUNITY_UPLOAD_DIR);
137
138 $param['url'] = $param['baseurl'] . '/' . $folderName;
139
140 $param['path'] = $param['basedir'] . '/' . $folderName;
141
142 if (!is_dir($param['path'])) {
143 mkdir($param['path'], 0755); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir
144 $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';
145
146 file_put_contents($param['basedir'] . '/' . $folderName . '/.htaccess', $htaccessContent); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
147 }
148
149 if (!file_exists($param['basedir'] . '/' . $folderName . '/index.php')) {
150
151 $indexStubContent = "<?php\n// Silence is golden.\n";
152
153 file_put_contents($param['basedir'] . '/' . $folderName . '/index.php', $indexStubContent); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
154 }
155
156 return $param;
157 }
158
159 /**
160 * Rename the uploaded file name before saving
161 * @param array $file
162 * @return array $file
163 */
164 public function _renameFileName($file)
165 {
166 $prefix = 'fluentcom-' . wp_generate_password(32, false) . '-fluentcom-';
167 $originalName = $file['name'];
168 $file['name'] = $prefix . $file['name'];
169 $file['name'] = apply_filters('fluent_community/generated_upload_file_name', $file['name'], $originalName, $file);
170 return $file;
171 }
172
173 public static function __callStatic($method, $params)
174 {
175 $instance = new static;
176
177 return call_user_func_array([$instance, $method], $params);
178 }
179
180 public function __call($method, $params)
181 {
182 $hiddenMethod = "_" . $method;
183
184 $method = method_exists($this, $hiddenMethod) ? $hiddenMethod : $method;
185
186 return call_user_func_array([$this, $method], $params);
187 }
188 }
189