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

178 lines 5.5 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 class FileSystem
6 {
7 /**
8 * Read file content from custom upload dir of this application
9 * @return string [path]
10 */
11 public function _get($file)
12 {
13 $arr = explode('/', $file);
14 $fileName = end($arr);
15 return file_get_contents($this->getDir() . '/' . $fileName); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
16 }
17
18 /**
19 * Get custom upload dir name of this application
20 * @return string [directory path]
21 */
22 public function _getDir()
23 {
24 $uploadDir = wp_upload_dir();
25
26 $folderName = apply_filters('fluent_community/upload_folder_name', FLUENT_COMMUNITY_UPLOAD_DIR);
27
28 return $uploadDir['basedir'] . $folderName;
29 }
30
31 /**
32 * Get absolute path of file using custom upload dir name of this application
33 * @return string [file path]
34 */
35 public function _getAbsolutePathOfFile($file)
36 {
37 return $this->_getDir() . '/' . $file;
38 }
39
40 /**
41 * Upload files into custom upload dir of this application
42 * @return array
43 */
44 public function _uploadFromRequest()
45 {
46 return $this->_put(fluentCommunityApp('request')->files());
47 }
48
49 /**
50 * Upload files into custom upload dir of this application
51 * @param array $files
52 * @return array
53 */
54 public function _put($files)
55 {
56 if (!function_exists('wp_handle_upload')) {
57 require_once(ABSPATH . 'wp-admin/includes/file.php');
58 }
59
60 $this->overrideUploadDir();
61
62 $uploadOverrides = ['test_form' => false];
63
64 $uploadedFiles = [];
65
66 foreach ((array)$files as $file) {
67 $filesArray = $file->toArray();
68 $originalName = $filesArray['name'];
69 $uploadedFile = \wp_handle_upload($filesArray, $uploadOverrides);
70
71 if (isset($uploadedFile['error'])) {
72 $uploadedFiles[] = new \WP_Error('upload_error', $uploadedFile['error']);
73 }
74
75 $uploadedFile['original_name'] = $originalName;
76
77 $uploadedFiles[] = $uploadedFile;
78 }
79
80 return $uploadedFiles;
81 }
82
83 /**
84 * Delete a file from custom upload directory of this application
85 * @param array $files
86 * @return void
87 */
88 public function _delete($files)
89 {
90 $files = (array)$files;
91
92 foreach ($files as $file) {
93 $arr = explode('/', $file);
94 $fileName = end($arr);
95 wp_delete_file($this->getDir() . '/' . $fileName);
96 }
97 }
98
99 /**
100 * Register filters for custom upload dir
101 */
102 public function _overrideUploadDir()
103 {
104 add_filter('wp_handle_upload_prefilter', function ($file) {
105 add_filter('upload_dir', [$this, '_setCustomUploadDir']);
106
107 add_filter('wp_handle_upload', function ($fileinfo) {
108 remove_filter('upload_dir', [$this, '_setCustomUploadDir']);
109 $fileinfo['file'] = basename($fileinfo['file']);
110 return $fileinfo;
111 });
112
113 return $this->_renameFileName($file);
114 });
115 }
116
117 /**
118 * Set plugin's custom upload dir
119 * @param array $param
120 * @return array $param
121 */
122 public function _setCustomUploadDir($param)
123 {
124
125 $folderName = apply_filters('fluent_community/upload_folder_name', FLUENT_COMMUNITY_UPLOAD_DIR);
126
127 $param['url'] = $param['baseurl'] . '/' . $folderName;
128
129 $param['path'] = $param['basedir'] . '/' . $folderName;
130
131 if (!is_dir($param['path'])) {
132 mkdir($param['path'], 0755); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir
133 $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';
134
135 file_put_contents($param['basedir'] . '/' . $folderName . '/.htaccess', $htaccessContent); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
136 }
137
138 if (!file_exists($param['basedir'] . '/' . $folderName . '/index.php')) {
139
140 $indexStubContent = "<?php\n// Silence is golden.\n";
141
142 file_put_contents($param['basedir'] . '/' . $folderName . '/index.php', $indexStubContent); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
143 }
144
145 return $param;
146 }
147
148 /**
149 * Rename the uploaded file name before saving
150 * @param array $file
151 * @return array $file
152 */
153 public function _renameFileName($file)
154 {
155 $prefix = 'fluentcom-' . wp_generate_password(32, false) . '-fluentcom-';
156 $originalName = $file['name'];
157 $file['name'] = $prefix . $file['name'];
158 $file['name'] = apply_filters('fluent_community/generated_upload_file_name', $file['name'], $originalName, $file);
159 return $file;
160 }
161
162 public static function __callStatic($method, $params)
163 {
164 $instance = new static;
165
166 return call_user_func_array([$instance, $method], $params);
167 }
168
169 public function __call($method, $params)
170 {
171 $hiddenMethod = "_" . $method;
172
173 $method = method_exists($this, $hiddenMethod) ? $hiddenMethod : $method;
174
175 return call_user_func_array([$this, $method], $params);
176 }
177 }
178