PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.21.4
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.21.4
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Util / FileDownloadProvider.php

FileDownloadProvider.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.21.4, at includes/Core/Util/FileDownloadProvider.php

152 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Core\Util;
4
5 final class FileDownloadProvider
6 {
7 public function register()
8 {
9 add_action('template_redirect', [$this, 'authCheckandFrceDownloadHelper']);
10 add_shortcode('bitforms-frontend-file', [$this, 'handleFileDownload']);
11 }
12
13 public function handleFileDownload()
14 {
15 if (!isset($_GET['formID']) || !isset($_GET['entryID']) || !isset($_GET['fileID'])) {
16 global $wp_query;
17 $wp_query->set_404();
18 status_header(404);
19 get_template_part(404);
20 exit();
21 }
22 $formID = intval(sanitize_text_field($_GET['formID']));
23 $entryID = intval(sanitize_text_field($_GET['entryID']));
24 $fileID = sanitize_file_name($_GET['fileID']);
25 $filePath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR . $fileID;
26
27 if (is_readable($filePath)) {
28 $this->fileDownloadORView($filePath, true);
29 }
30 }
31
32 public static function getBaseDownloadURL()
33 {
34 $routes = get_option('bitforms_routes');
35 if (isset($routes['file'])) {
36 $file_page = get_post($routes['file']);
37 if (empty($file_page)) {
38 $file_route_id = wp_insert_post(
39 [
40 'post_name' => 'bitforms-file',
41 'comment_status' => 'closed',
42 'ping_status' => 'closed',
43 'post_content' => '<!-- wp:shortcode -->[bitforms-frontend-file /]<!-- /wp:shortcode -->',
44 'post_status' => 'publish',
45 'post_type' => 'bitforms'
46 ]
47 );
48 $routes['file'] = $file_route_id;
49 update_option('bitforms_routes', $routes);
50 $file_page_slug = get_post_permalink($file_route_id);
51 } else {
52 $file_page_slug = get_post_permalink($file_page->ID);
53 }
54 } else {
55 $file_route_id = wp_insert_post(
56 [
57 'post_name' => 'bitforms-file',
58 'comment_status' => 'closed',
59 'ping_status' => 'closed',
60 'post_content' => '<!-- wp:shortcode -->[bitforms-frontend-file /]<!-- /wp:shortcode -->',
61 'post_status' => 'publish',
62 'post_type' => 'bitforms'
63 ]
64 );
65 $route_value = [];
66 $route_value['file'] = $file_route_id;
67 update_option('bitforms_routes', $route_value);
68 $file_page_slug = get_post_permalink($file_route_id);
69 }
70
71 return $file_page_slug;
72 }
73
74 public function authCheckandFrceDownloadHelper()
75 {
76 if (!is_singular('bitforms')) {
77 return;
78 }
79 global $post;
80 if (!empty($post->post_content)) {
81 $shortCodeRegex = get_shortcode_regex();
82 preg_match_all('/' . $shortCodeRegex . '/', $post->post_content, $regexMatchGroups);
83 if (!empty($regexMatchGroups[2]) && in_array('bitforms-frontend-file', $regexMatchGroups[2]) && is_user_logged_in()) {
84 $file = $this->isRequestedFileExists();
85 if ($file) {
86 $this->fileDownloadORView($file, isset($_GET['download']));
87 } else {
88 $this->show404();
89 }
90 } else {
91 auth_redirect();
92 }
93 }
94 }
95
96 private function show404()
97 {
98 global $wp_query;
99 $wp_query->set_404();
100 status_header(404);
101 get_template_part(404);
102 exit();
103 }
104
105 private function isRequestedFileExists()
106 {
107 if (!isset($_GET['formID']) || !isset($_GET['entryID']) || !isset($_GET['fileID'])) {
108 return false;
109 }
110 $formID = intval(sanitize_text_field($_GET['formID']));
111 $entryID = intval(sanitize_text_field($_GET['entryID']));
112 $fileID = sanitize_file_name($_GET['fileID']);
113 $filePath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR . $fileID;
114 if (is_readable($filePath)) {
115 return $filePath;
116 }
117
118 return false;
119 }
120
121 private function fileDownloadORView($filePath, $forceDownload = false)
122 {
123 if ($forceDownload) {
124 header('Content-Type: application/force-download');
125 header('Content-Type: application/octet-stream');
126 header('Content-Type: application/download');
127 header('Content-Disposition: attachment; filename="' . basename($filePath) . '"');
128 } else {
129 $fileInfo = wp_check_filetype($filePath);
130 $content_types = 'text/plain';
131 if ($fileInfo['type'] && $fileInfo['ext']) {
132 $content_types = $fileInfo['type'];
133 $ext = $fileInfo['ext'];
134 if (in_array($ext[1], ['txt', 'php', 'html', 'xhtml', 'json'])) {
135 $content_types = 'text/plain';
136 }
137 }
138 header('Content-Disposition:filename="' . basename($filePath) . '"');
139 header("Content-Type: $content_types");
140 }
141 header('Content-Description: File Transfer');
142 header('Expires: 0');
143 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
144 header('Pragma: public');
145 header('Content-Length: ' . filesize($filePath));
146 header('Content-Transfer-Encoding: binary ');
147 flush();
148 readfile($filePath);
149 die();
150 }
151 }
152