PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
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.0, at includes/Core/Util/FileDownloadProvider.php

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