PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.5.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.5.0
2.5.0 2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 All 34 releases
fluent-booking / app / Services / Libs / FileSystem.php

FileSystem.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 2.5.0, at app/Services/Libs/FileSystem.php

188 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBooking\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(
16 $this->getDir() . '/' . $fileName
17 );
18 }
19
20 /**
21 * Get custom upload dir name of this application
22 * @return string [directory path]
23 */
24 public function _getDir()
25 {
26 $uploadDir = wp_upload_dir();
27
28 $folderName = apply_filters('fluent_booking/upload_folder_name', 'fluent-booking');
29
30 return $uploadDir['basedir'] . '/' . $folderName;
31 }
32
33 /**
34 * Whether a URL points at a file this application uploaded (renamed by _renameFileName)
35 * @return bool
36 */
37 public function _isUploadedFileUrl($url)
38 {
39 $name = is_string($url) ? basename((string) wp_parse_url($url, PHP_URL_PATH)) : '';
40
41 // The prefix also rules out the folder's own .htaccess and index.php.
42 if (!preg_match('/^fluentbooking-[a-f0-9]{32}-fluentbooking-/', $name)) {
43 return false;
44 }
45
46 $folderName = apply_filters('fluent_booking/upload_folder_name', 'fluent-booking');
47 $expected = wp_upload_dir()['baseurl'] . '/' . $folderName . '/' . $name;
48
49 return set_url_scheme($url) === set_url_scheme($expected) && file_exists($this->_getDir() . '/' . $name);
50 }
51
52 /**
53 * Get absolute path of file using custom upload dir name of this application
54 * @return string [file path]
55 */
56 public function _getAbsolutePathOfFile($file)
57 {
58 return $this->_getDir() . '/' . $file;
59 }
60
61 /**
62 * Upload files into custom upload dir of this application
63 * @return array
64 */
65 public function _uploadFromRequest()
66 {
67 return $this->_put(FluentCrm('request')->files());
68 }
69
70 /**
71 * Upload files into custom upload dir of this application
72 * @param array $files
73 * @return array
74 */
75 public function _put($files)
76 {
77 if (!function_exists('wp_handle_upload')) {
78 require_once(ABSPATH . 'wp-admin/includes/file.php');
79 }
80
81 $this->overrideUploadDir();
82
83 $uploadOverrides = ['test_form' => false];
84
85 foreach ((array)$files as $file) {
86 $filesArray = $file->toArray();
87 $uploadedFiles[] = \wp_handle_upload($filesArray, $uploadOverrides);
88 }
89
90 return $uploadedFiles;
91 }
92
93 /**
94 * Delete a file from custom upload directory of this application
95 * @param array $files
96 * @return void
97 */
98 public function _delete($files)
99 {
100 $files = (array)$files;
101
102 foreach ($files as $file) {
103 $arr = explode('/', $file);
104 $fileName = end($arr);
105 wp_delete_file($this->getDir() . '/' . $fileName);
106 }
107 }
108
109 /**
110 * Register filters for custom upload dir
111 */
112 public function _overrideUploadDir()
113 {
114 add_filter('wp_handle_upload_prefilter', function ($file) {
115 add_filter('upload_dir', [$this, '_setCustomUploadDir']);
116
117 add_filter('wp_handle_upload', function ($fileinfo) {
118 remove_filter('upload_dir', [$this, '_setCustomUploadDir']);
119 $fileinfo['file'] = basename($fileinfo['file']);
120 return $fileinfo;
121 });
122
123 return $this->_renameFileName($file);
124 });
125 }
126
127 /**
128 * Set plugin's custom upload dir
129 * @param array $param
130 * @return array $param
131 */
132 public function _setCustomUploadDir($param)
133 {
134
135 $folderName = apply_filters('fluent_booking/upload_folder_name', 'fluent-booking');
136
137 $param['url'] = $param['baseurl'] .'/'. $folderName;
138
139 $param['path'] = $param['basedir'] .'/'. $folderName;
140
141 if (!is_dir($param['path'])) {
142 mkdir($param['path'], 0755); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir
143 file_put_contents(
144 $param['basedir'].'/'.$folderName.'/.htaccess',
145 file_get_contents(__DIR__.'/Stubs/htaccess.stub')
146 );
147 }
148
149 if(!file_exists($param['basedir'].'/'.$folderName.'/index.php')) {
150 file_put_contents(
151 $param['basedir'].'/'.$folderName.'/index.php',
152 file_get_contents(__DIR__.'/Stubs/index.stub')
153 );
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 = 'fluentbooking-' . md5(wp_generate_uuid4()) . '-fluentbooking-';
167 $file['name'] = $prefix . $file['name'];
168
169 return $file;
170 }
171
172 public static function __callStatic($method, $params)
173 {
174 $instance = new static;
175
176 return call_user_func_array([$instance, $method], $params);
177 }
178
179 public function __call($method, $params)
180 {
181 $hiddenMethod = "_" . $method;
182
183 $method = method_exists($this, $hiddenMethod) ? $hiddenMethod : $method;
184
185 return call_user_func_array([$this, $method], $params);
186 }
187 }
188