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