PluginProbe
Import WP – CSV & XML Import Export for WordPress / trunk
Import WP – CSV & XML Import Export for WordPress vtrunk
2.14.24 2.14.23 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 2.9.1 trunk 0.1.6 All 142 releases
jc-importer / class / Common / Http / Http.php

Http.php in Import WP – CSV & XML Import Export for WordPress trunk, at class/Common/Http/Http.php

150 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImportWP\Common\Http;
4
5 use ImportWP\Common\Properties\Properties;
6 use ImportWP\Common\Util\Logger;
7
8 class Http
9 {
10 /**
11 * @var Properties
12 */
13 protected $properties;
14
15 public function __construct(Properties $properties)
16 {
17 $this->properties = $properties;
18 }
19
20 public function end_rest_success($data)
21 {
22 return [
23 'status' => 'S',
24 'data' => $data
25 ];
26 }
27
28 public function end_rest_error($data)
29 {
30 return [
31 'status' => 'E',
32 'data' => $data
33 ];
34 }
35
36 public function download_file($source, $destination, $headers = [])
37 {
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);
46 $result = true;
47 if (!is_wp_error($response)) {
48 $response_code = wp_remote_retrieve_response_code($response);
49 Logger::write(__CLASS__ . '::download_file -response-code=' . $response_code . ' -url=' . esc_url($source));
50 if ($response_code !== 200) {
51 return new \WP_Error('IWP_HTTP_1', sprintf(__('Unable to download: %s, Response Code: %s', 'jc-importer'), esc_url($source), $response_code));
52 }
53
54 $filename = $this->get_response_filename($response);
55 if ($filename) {
56 $result = $filename;
57 }
58 }
59
60 if (is_wp_error($response)) {
61 return $response;
62 }
63
64
65
66 if (!file_put_contents($destination, $response['body'])) {
67 return false;
68 }
69
70 return $result;
71 }
72
73 public function download_file_stream($source, $destination, $headers = [])
74 {
75 $args = apply_filters('iwp/http/remote_get_args', [
76 'timeout' => 30,
77 'sslverify' => false,
78 'headers' => $headers,
79 'reject_unsafe_urls' => true
80 ]);
81
82 $args = array_merge($args, [
83 'stream' => true,
84 'filename' => $destination,
85 ]);
86
87 $response = wp_remote_get($source, $args);
88 if (is_wp_error($response)) {
89 return $response;
90 }
91
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));
97 }
98
99 $filename = $this->get_response_filename($response);
100 if ($filename) {
101 return $filename;
102 }
103
104 return true;
105 }
106
107 public function get_response_filename($response)
108 {
109 $content_disposition = wp_remote_retrieve_header($response, 'content-disposition');
110 $matches = [];
111 if (preg_match('~filename=(?|"([^"]*)"|\'([^\']*)\'|([^;]*))~', $content_disposition, $matches) !== false && isset($matches[1])) {
112 return $matches[1];
113 }
114
115 // If there is no extension on the file, use the response content-type to add th file extension
116 if (isset($response['filename'])) {
117
118 $filename = basename($response['filename']);
119 $current_ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
120
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']);
123
124 if (!$has_allowed_extension) {
125
126 $content_type = wp_remote_retrieve_header($response, 'content-type');
127
128 if ($content_type) {
129
130 $mime_types = wp_get_mime_types();
131 $possible_ext = array_search($content_type, $mime_types);
132 $allowed_extensions = explode('|', $possible_ext);
133
134 if ($possible_ext !== false && !$has_allowed_extension && !empty($allowed_extensions) && !in_array(strtolower($filename), $allowed_extensions)) {
135 return $filename . '.' . $allowed_extensions[0];
136 }
137 }
138 }
139 }
140
141 return false;
142 }
143
144 public function set_stream_headers()
145 {
146 send_nosniff_header();
147 nocache_headers();
148 }
149 }
150