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