| @@ -3,9 +3,8 @@ | ||
| 3 | 3 | namespace ImportWP\Common\Http; |
| 4 | 4 | |
| 5 | 5 | use ImportWP\Common\Properties\Properties; |
| 6 | 6 | use ImportWP\Common\Util\Logger; |
| 7 | -use ImportWP\Container; | |
| 8 | 7 | |
| 9 | 8 | class Http |
| 10 | 9 | { |
| 11 | 10 | /** |
| @@ -35,15 +34,22 @@ | ||
| 35 | 34 | } |
| 36 | 35 | |
| 37 | 36 | public function download_file($source, $destination, $headers = []) |
| 38 | 37 | { |
| 39 | - $response = wp_remote_get($source, array('timeout' => 30, 'sslverify' => false, 'headers' => $headers)); | |
| 38 | + $args = apply_filters('iwp/http/remote_get_args', [ | |
| 39 | + 'timeout' => 30, | |
| 40 | + 'sslverify' => false, | |
| 41 | + 'headers' => $headers, | |
| 42 | + 'reject_unsafe_urls' => true | |
| 43 | + ]); | |
| 44 | + | |
| 45 | + $response = wp_remote_get($source, $args); | |
| 40 | 46 | $result = true; |
| 41 | 47 | if (!is_wp_error($response)) { |
| 42 | 48 | $response_code = wp_remote_retrieve_response_code($response); |
| 43 | 49 | Logger::write(__CLASS__ . '::download_file -response-code=' . $response_code . ' -url=' . esc_url($source)); |
| 44 | 50 | if ($response_code !== 200) { |
| 45 | - return new \WP_Error('IWP_HTTP_1', 'Unable to download: ' . esc_url($source) . ', Response Code: ' . $response_code); | |
| 51 | + return new \WP_Error('IWP_HTTP_1', sprintf(__('Unable to download: %s, Response Code: %s', 'jc-importer'), esc_url($source), $response_code)); | |
| 46 | 52 | } |
| 47 | 53 | |
| 48 | 54 | $filename = $this->get_response_filename($response); |
| 49 | 55 | if ($filename) { |
| @@ -65,29 +71,38 @@ | ||
| 65 | 71 | } |
| 66 | 72 | |
| 67 | 73 | public function download_file_stream($source, $destination, $headers = []) |
| 68 | 74 | { |
| 69 | - $response = wp_remote_get($source, ['stream' => true, 'filename' => $destination, 'timeout' => 30, 'sslverify' => false, 'headers' => $headers]); | |
| 70 | - $result = true; | |
| 75 | + $args = apply_filters('iwp/http/remote_get_args', [ | |
| 76 | + 'timeout' => 30, | |
| 77 | + 'sslverify' => false, | |
| 78 | + 'headers' => $headers, | |
| 79 | + 'reject_unsafe_urls' => true | |
| 80 | + ]); | |
| 71 | 81 | |
| 72 | - if (!is_wp_error($response)) { | |
| 73 | - $response_code = wp_remote_retrieve_response_code($response); | |
| 82 | + $args = array_merge($args, [ | |
| 83 | + 'stream' => true, | |
| 84 | + 'filename' => $destination, | |
| 85 | + ]); | |
| 74 | 86 | |
| 75 | - Logger::write(__CLASS__ . '::download_file_stream -response-code=' . $response_code . ' -url=' . esc_url($source) . ' -size=' . filesize($destination)); | |
| 76 | - if ($response_code !== 200) { | |
| 77 | - return new \WP_Error('IWP_HTTP_1', 'Unable to download: ' . esc_url($source) . ', Response Code: ' . $response_code); | |
| 78 | - } | |
| 87 | + $response = wp_remote_get($source, $args); | |
| 88 | + if (is_wp_error($response)) { | |
| 89 | + return $response; | |
| 90 | + } | |
| 79 | 91 | |
| 80 | - $filename = $this->get_response_filename($response); | |
| 81 | - if ($filename) { | |
| 82 | - $result = $filename; | |
| 83 | - } | |
| 92 | + $response_code = wp_remote_retrieve_response_code($response); | |
| 93 | + | |
| 94 | + Logger::write(__CLASS__ . '::download_file_stream -response-code=' . $response_code . ' -url=' . esc_url($source) . ' -size=' . filesize($destination)); | |
| 95 | + if ($response_code !== 200) { | |
| 96 | + return new \WP_Error('IWP_HTTP_1', sprintf(__('Unable to download: %s, Response Code: %s', 'jc-importer'), esc_url($source), $response_code)); | |
| 84 | 97 | } |
| 85 | 98 | |
| 86 | - if (is_wp_error($response)) { | |
| 87 | - return $this->download_file($source, $destination); | |
| 99 | + $filename = $this->get_response_filename($response); | |
| 100 | + if ($filename) { | |
| 101 | + return $filename; | |
| 88 | 102 | } |
| 89 | - return $result; | |
| 103 | + | |
| 104 | + return true; | |
| 90 | 105 | } |
| 91 | 106 | |
| 92 | 107 | public function get_response_filename($response) |
| 93 | 108 | { |
| @@ -102,10 +117,13 @@ | ||
| 102 | 117 | |
| 103 | 118 | $filename = basename($response['filename']); |
| 104 | 119 | $current_ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); |
| 105 | 120 | |
| 106 | - if (empty($current_ext)) { | |
| 121 | + $has_allowed_extension = !empty($current_ext); | |
| 122 | + $has_allowed_extension = apply_filters('iwp/regenerate_response_filename_ext', $has_allowed_extension, $current_ext, $response['filename']); | |
| 107 | 123 | |
| 124 | + if (!$has_allowed_extension) { | |
| 125 | + | |
| 108 | 126 | $content_type = wp_remote_retrieve_header($response, 'content-type'); |
| 109 | 127 | |
| 110 | 128 | if ($content_type) { |
| 111 | 129 | |
| @@ -112,9 +130,9 @@ | ||
| 112 | 130 | $mime_types = wp_get_mime_types(); |
| 113 | 131 | $possible_ext = array_search($content_type, $mime_types); |
| 114 | 132 | $allowed_extensions = explode('|', $possible_ext); |
| 115 | 133 | |
| 116 | - if ($possible_ext !== false && empty($current_ext) && !empty($allowed_extensions) && !in_array(strtolower($filename), $allowed_extensions)) { | |
| 134 | + if ($possible_ext !== false && !$has_allowed_extension && !empty($allowed_extensions) && !in_array(strtolower($filename), $allowed_extensions)) { | |
| 117 | 135 | return $filename . '.' . $allowed_extensions[0]; |
| 118 | 136 | } |
| 119 | 137 | } |
| 120 | 138 | } |
| @@ -124,18 +142,8 @@ | ||
| 124 | 142 | } |
| 125 | 143 | |
| 126 | 144 | public function set_stream_headers() |
| 127 | 145 | { |
| 128 | - $importer_manager = Container::getInstance()->get('importer_manager'); | |
| 129 | - if (false === $importer_manager->is_debug()) { | |
| 130 | - header('Content-Type: text/event-stream'); | |
| 131 | - } | |
| 132 | - | |
| 133 | - header("Content-Encoding: none"); | |
| 134 | - header('Cache-Control: no-cache'); | |
| 135 | - | |
| 136 | - // Allow for other requests to run at the same time | |
| 137 | - if (session_status() == PHP_SESSION_ACTIVE) { | |
| 138 | - session_write_close(); | |
| 139 | - } | |
| 146 | + send_nosniff_header(); | |
| 147 | + nocache_headers(); | |
| 140 | 148 | } |
| 141 | 149 | } |