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

166 lines 4.9 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 foreach ((array)$files as $file) {
65 $filesArray = $file->toArray();
66 $uploadedFiles[] = \wp_handle_upload($filesArray, $uploadOverrides);
67 }
68
69 return $uploadedFiles;
70 }
71
72 /**
73 * Delete a file from custom upload directory of this application
74 * @param array $files
75 * @return void
76 */
77 public function _delete($files)
78 {
79 $files = (array)$files;
80
81 foreach ($files as $file) {
82 $arr = explode('/', $file);
83 $fileName = end($arr);
84 wp_delete_file($this->getDir() . '/' . $fileName);
85 }
86 }
87
88 /**
89 * Register filters for custom upload dir
90 */
91 public function _overrideUploadDir()
92 {
93 add_filter('wp_handle_upload_prefilter', function ($file) {
94 add_filter('upload_dir', [$this, '_setCustomUploadDir']);
95
96 add_filter('wp_handle_upload', function ($fileinfo) {
97 remove_filter('upload_dir', [$this, '_setCustomUploadDir']);
98 $fileinfo['file'] = basename($fileinfo['file']);
99 return $fileinfo;
100 });
101
102 return $this->_renameFileName($file);
103 });
104 }
105
106 /**
107 * Set plugin's custom upload dir
108 * @param array $param
109 * @return array $param
110 */
111 public function _setCustomUploadDir($param)
112 {
113
114 $folderName = apply_filters('fluent_community/upload_folder_name', FLUENT_COMMUNITY_UPLOAD_DIR);
115
116 $param['url'] = $param['baseurl'] .'/'. $folderName;
117
118 $param['path'] = $param['basedir'] .'/'. $folderName;
119
120 if (!is_dir($param['path'])) {
121 mkdir($param['path'], 0755); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir
122 $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';
123
124 file_put_contents( $param['basedir'].'/'.$folderName.'/.htaccess', $htaccessContent); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
125 }
126
127 if(!file_exists($param['basedir'].'/'.$folderName.'/index.php')) {
128
129 $indexStubContent = "<?php\n// Silence is golden.\n";
130
131 file_put_contents( $param['basedir'].'/'.$folderName.'/index.php', $indexStubContent ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
132 }
133
134 return $param;
135 }
136
137 /**
138 * Rename the uploaded file name before saving
139 * @param array $file
140 * @return array $file
141 */
142 public function _renameFileName($file)
143 {
144 $prefix = 'fluentcom-' . md5(wp_generate_uuid4()) . '-fluentcom-';
145 $file['name'] = $prefix . $file['name'];
146
147 return $file;
148 }
149
150 public static function __callStatic($method, $params)
151 {
152 $instance = new static;
153
154 return call_user_func_array([$instance, $method], $params);
155 }
156
157 public function __call($method, $params)
158 {
159 $hiddenMethod = "_" . $method;
160
161 $method = method_exists($this, $hiddenMethod) ? $hiddenMethod : $method;
162
163 return call_user_func_array([$this, $method], $params);
164 }
165 }
166