PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.5.1
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.5.1
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / app / Services / FileSystem / Drivers / Local / LocalDriver.php

LocalDriver.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.5.1, at app/Services/FileSystem/Drivers/Local/LocalDriver.php

254 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Services\FileSystem\Drivers\Local;
4
5 use FluentCart\App\App;
6 use FluentCart\App\Helpers\Helper;
7 use FluentCart\App\Modules\StorageDrivers\BaseStorageDriver;
8 use FluentCart\App\Modules\StorageDrivers\Local\Local as LocalStorageDriver;
9 use FluentCart\App\Services\FileSystem\Drivers\BaseDriver;
10 use FluentCart\Framework\Support\Arr;
11 use FluentCart\Framework\Support\Str;
12
13 class LocalDriver extends BaseDriver
14 {
15 public function getDirName(): string
16 {
17 return $this->dirName;
18 }
19
20
21 public function __construct(?string $dirPath = null, ?string $dirName = null)
22 {
23 parent::__construct($dirPath, $dirName);
24 $this->dirName = $dirName ?? 'fluent-cart';
25 $this->dirPath = $dirPath ?? $this->getDefaultDirPath();
26 $this->storageDriver = new LocalStorageDriver();
27
28 global $wp_filesystem;
29
30 // Initialize the WP_Filesystem API
31 if (empty($wp_filesystem)) {
32 require_once ABSPATH . 'wp-admin/includes/file.php';
33 WP_Filesystem();
34 }
35
36 $this->ensureDirectoryExist();
37
38 }
39
40 protected function getDefaultDirPath(): string
41 {
42 return wp_get_upload_dir()['basedir'] . DIRECTORY_SEPARATOR . $this->getDirName();
43 }
44
45 private function ensureDirectoryExist() {
46
47
48 $uploadDirectory = $this->getDefaultDirPath();
49 global $wp_filesystem;
50 // Check if directory exists
51 if (! $wp_filesystem->is_dir($uploadDirectory)) {
52 $wp_filesystem->mkdir($uploadDirectory, FS_CHMOD_DIR);
53 }
54 }
55
56
57 public function listFiles(array $params = []): array
58 {
59 $filesArray = [];
60 $searchTerm = $params['search'] ?? '';
61
62 $files = scandir($this->getDefaultDirPath());
63
64 $maxFile = App::request()->get('per_page', 10);
65
66 $fileCount = 1;
67
68 foreach ($files as $file) {
69 // Skip hidden files (starting with dot)
70 if (str_starts_with($file, '.')) {
71 continue;
72 }
73
74 if (str_ends_with($file, '.php')) {
75 continue;
76 }
77
78 // Apply search filter if provided
79 if (!empty($searchTerm) && stripos($file, $searchTerm) === false) {
80 continue;
81 }
82
83 $filePath = $this->getDefaultDirPath() . '/' . $file;
84 if (is_file($filePath)) {
85 $filesArray[] = [
86 'name' => $file,
87 'size' => filesize($filePath),
88 'driver' => 'local',
89 'bucket' => ''
90 ];
91 }
92
93 $fileCount++;
94 if($fileCount > $maxFile) {
95 break;
96 }
97 }
98
99 return $filesArray;
100 }
101
102 public function uploadFile($localFilePath, $uploadToFilePath, $file, $params = [])
103 {
104 global $wp_filesystem;
105
106 $uploadToFilePath = sanitize_file_name($uploadToFilePath);
107
108
109 $fileInfo = $file->toArray();
110 $fileSize =Arr::get($fileInfo, 'size_in_bytes', 0);
111
112 $originalName = $file->getClientOriginalName();
113 $sanitizedName = sanitize_file_name($originalName);
114 $checked = wp_check_filetype_and_ext(
115 $localFilePath,
116 $sanitizedName
117 );
118
119
120 if (empty($checked['ext']) || empty($checked['type'])) {
121 return new \WP_Error(
122 'invalid_file_type',
123 __('Invalid or unsupported file type.', 'fluent-cart')
124 );
125 }
126
127 $blockedExts = ['php','phtml','html','htm','svg','exe','sh','bat','cmd','dll'];
128
129 $blockedExts = apply_filters('fluent_cart/local_file_blocked_extensions',$blockedExts,[
130 'localFilePath' => $localFilePath,
131 'uploadToFilePath' => $uploadToFilePath,
132 'fileInfo' => $fileInfo,
133 'fileSize' => $fileSize,
134 'originalName' => $originalName,
135 'file' => $file,
136 'checked_data' => $checked,
137 ]);
138 if (in_array($checked['ext'], $blockedExts, true)) {
139 return new \WP_Error(
140 'forbidden_type',
141 __('This file type is not allowed.', 'fluent-cart')
142 );
143 }
144
145 // Generate unique filename
146 $uploadToFilePath = $uploadToFilePath . '__fluent-cart__.' . time() . '.' . $file->getClientOriginalExtension();
147
148 // Define destination
149 $destination = trailingslashit( $this->getDefaultDirPath() ) . $uploadToFilePath;
150
151 // Read file contents from temp path
152 $contents = @file_get_contents( $localFilePath );
153
154 if ( $contents === false ) {
155 return new \WP_Error( 'failed_to_read', __( 'Failed to read uploaded file', 'fluent-cart' ) );
156 }
157
158 // Write file using WP_Filesystem
159 $isUploaded = $wp_filesystem->put_contents( $destination, $contents, FS_CHMOD_FILE );
160
161 // Optionally delete temp file using WP_Filesystem for compliance
162 if ( $wp_filesystem->exists( $localFilePath ) ) {
163 $wp_filesystem->delete( $localFilePath );
164 }
165
166 if ( ! $isUploaded ) {
167 return new \WP_Error( 'failed_to_upload', __( 'Failed to upload file', 'fluent-cart' ) );
168 }
169
170 return [
171 'message' => __( 'File Uploaded Successfully', 'fluent-cart' ),
172 'path' => $uploadToFilePath,
173 'file' => [
174 'driver' => 'local',
175 'size' => $fileSize,
176 'name' => $uploadToFilePath,
177 'bucket' => '',
178 ],
179 ];
180 }
181
182
183 public function getSignedDownloadUrl(string $filePath, $bucket = null, $productDownload = null): string
184 {
185 return Helper::generateDownloadFileLink($productDownload);
186 }
187
188 public function downloadFile(string $filePath, $fileName = null)
189 {
190 $filePath = wp_normalize_path( $filePath );
191 $fileName = sanitize_file_name($fileName);
192
193 $file = "{$this->dirPath}/{$filePath}";
194 if (ob_get_level()) {
195 ob_end_clean();
196 }
197
198 if(!file_exists($file)) {
199 return new \WP_Error('file_not_found', __('File not found', 'fluent-cart'));
200 }
201 $fileSize = filesize($file);
202 $fileName = $fileName ?? basename($filePath);
203 $fileName = explode('_____fluent-cart_____', $fileName)[0];
204 $fileName = explode('__fluent-cart__', $fileName)[0];
205 $this->setDownloadHeader($fileName ?? basename($filePath), $file, $fileSize);
206 global $wp_filesystem;
207 $content = $wp_filesystem->get_contents( $file );
208 if ( $content !== false ) {
209 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
210 echo $content;
211 }
212 exit;
213 }
214
215 public function getFilePath(string $filePath, $fileName = null): string
216 {
217 return "{$this->dirPath}/{$filePath}";
218 }
219
220 protected function retrieveFileForDownload(string $downloadableFilePath)
221 {
222 // TODO: Implement retrieveFileForDownload() method.
223 }
224
225 public function deleteFile(string $filePath)
226 {
227
228 if ( !current_user_can('manage_options') ) {
229 return new \WP_Error('permission_error', __('You are not allowed to delete file', 'fluent-cart'));
230 }
231
232 $filePath = wp_normalize_path($filePath);
233 $fullPath = $this->getFilePath($filePath);
234
235 if (!file_exists($fullPath)) {
236 return new \WP_Error('file_not_found', __('File not found', 'fluent-cart'));
237 }
238
239 if (!is_file($fullPath)) {
240 return new \WP_Error('not_a_file', __('Path is not a file', 'fluent-cart'));
241 }
242
243 if (wp_delete_file($fullPath)) {
244 return [
245 'message' => __('File Deleted Successfully', 'fluent-cart'),
246 'driver' => 'local',
247 'path' => $filePath
248 ];
249 }
250
251 return new \WP_Error('failed_to_delete', __('Failed to delete file', 'fluent-cart'));
252 }
253 }
254