PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.95
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.95
2.1.0 2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 All 42 releases
fluent-boards / app / Hooks / Handlers / FileHandler.php

FileHandler.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.95, at app/Hooks/Handlers/FileHandler.php

143 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 FluentBoards\App\Hooks\Handlers;
4
5 use DateTimeImmutable;
6 use Exception;
7 use FluentBoards\App\Models\Meta;
8 use FluentBoards\App\Services\Constant;
9 use FluentBoards\App\Services\Libs\FileSystem;
10 use FluentBoards\App\Models\Attachment;
11 use function Sodium\add;
12
13 class FileHandler
14 {
15 // private function validateFile($file)
16 // {
17 // if (!$file) {
18 // throw new Exception('File is empty.');
19 // }
20 // if (!$this->isFileTypeSupported($file)) {
21 // throw new Exception('File type not supported');
22 // }
23 // if ($file['size'] > $this->getFileUploadLimit()) {
24 // throw new Exception('File size is too large');
25 // }
26 // }
27
28 private function getFileUploadLimit() {
29 // Logic for calculating file upload limit as in your original code
30 return min(
31 wp_convert_hr_to_bytes(ini_get('upload_max_filesize')),
32 wp_convert_hr_to_bytes(ini_get('post_max_size')),
33 wp_max_upload_size()
34 );
35 }
36
37 /**
38 * Summary of deleteFileByUrl
39 * @param mixed $file_url
40 * @return bool
41 */
42 public function deleteFileByUrl($file_url)
43 {
44 $exists = Attachment::where('full_url', $file_url)->exists();
45 if($exists) {
46 return;
47 }
48 // Convert the URL to the local file path
49 $upload_dir = wp_upload_dir();
50 $file_path = str_replace($upload_dir['baseurl'], $upload_dir['basedir'], $file_url);
51
52 // Check if the file exists
53 if (file_exists($file_path)) {
54 // Delete the file
55 $deleted = wp_delete_file($file_path);
56
57 // Optionally, you can also remove the file from the media library
58 // Note: This won't delete the file physically, but it will remove it from the media library
59 $attachment_id = attachment_url_to_postid($file_url);
60 if ($attachment_id) {
61 wp_delete_attachment($attachment_id, true);
62 }
63
64 // Return true if the file was successfully deleted
65 return $deleted;
66 }
67 // Return false if the file does not exist
68 return false;
69 }
70
71
72 /**
73 * Summary of isFileTypeSupported checking file type it will allow only file which is readable by browser
74 * @param mixed $file
75 * TODO: Refactorable: This can be in a Helper class. and we may pass it to frontend via wp_localize_script appvars
76 * so that we can check similarly for better experience.
77 * @return bool
78 */
79 public function isFileTypeSupported($file)
80 {
81 // Define supported file types that are generally allowed by user
82 $allowedMimeTypes = get_allowed_mime_types();
83
84 // Check if the file type is supported
85 return in_array(strtolower($file['type']), $allowedMimeTypes);
86 }
87
88 /**
89 * @throws Exception
90 */
91 public function handleMediaFileUpload($data)
92 {
93 // Check if file was uploaded
94 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verification handled by REST API/controller layer
95 if (!isset($_FILES['file']['tmp_name']) || !isset($_FILES['file']['name'])) {
96 throw new Exception(esc_html__('No file was uploaded. Please try again.', 'fluent-boards'));
97 }
98
99 // Sanitize filename from request for validation
100 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verification handled by REST API/controller layer
101 $filename = sanitize_file_name(wp_unslash($_FILES['file']['name']));
102
103 // Validate and sanitize tmp_name before use
104 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verification handled by REST API/controller layer
105 $tmp_name = sanitize_text_field(wp_unslash($_FILES['file']['tmp_name']));
106
107 if (empty($tmp_name) || !file_exists($tmp_name) || !is_uploaded_file($tmp_name)) {
108 throw new Exception(esc_html__('Invalid upload. Please try again.', 'fluent-boards'));
109 }
110
111 // Check if the uploaded file is an image
112 $wp_filetype = wp_check_filetype_and_ext($tmp_name, $filename);
113
114 if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
115 throw new Exception(esc_html__('The uploaded file is not a valid image. Please try again.', 'fluent-boards'));
116 }
117 require_once(ABSPATH . 'wp-admin/includes/image.php');
118 require_once( ABSPATH . 'wp-admin/includes/file.php' );
119 require_once( ABSPATH . 'wp-admin/includes/media.php' );
120 $attachment_id = media_handle_upload( 'file', 0, [] );
121
122 $attachment = wp_prepare_attachment_for_js( $attachment_id);
123 if(!$attachment) {
124 throw new Exception(esc_html__('The uploaded file is not a valid image. Please try again.', 'fluent-boards'));
125 } else {
126 return $attachment;
127 }
128 }
129
130 /**
131 * This function will delete meta where default board default image is used.
132 * @param $id
133 * @return void
134 */
135 public function mediaFileDeleted($id)
136 {
137 $boardImage = Meta::where('object_id', $id)->where('object_type', Constant::BOARD_DEFAULT_IMAGE)->first();
138 if($boardImage) {
139 $boardImage->delete();
140 }
141 }
142 }
143