PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.1
Import WP – CSV & XML Import Export for WordPress v2.7.1
2.15.1 2.15.0 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 All 144 releases
jc-importer / class / Common / Http / Http.php

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

142 lines 4.4 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 use ImportWP\Container;
8
9 class Http
10 {
11 /**
12 * @var Properties
13 */
14 protected $properties;
15
16 public function __construct(Properties $properties)
17 {
18 $this->properties = $properties;
19 }
20
21 public function end_rest_success($data)
22 {
23 return [
24 'status' => 'S',
25 'data' => $data
26 ];
27 }
28
29 public function end_rest_error($data)
30 {
31 return [
32 'status' => 'E',
33 'data' => $data
34 ];
35 }
36
37 public function download_file($source, $destination, $headers = [])
38 {
39 $response = wp_remote_get($source, array('timeout' => 30, 'sslverify' => false, 'headers' => $headers));
40 $result = true;
41 if (!is_wp_error($response)) {
42 $response_code = wp_remote_retrieve_response_code($response);
43 Logger::write(__CLASS__ . '::download_file -response-code=' . $response_code . ' -url=' . esc_url($source));
44 if ($response_code !== 200) {
45 return new \WP_Error('IWP_HTTP_1', 'Unable to download: ' . esc_url($source) . ', Response Code: ' . $response_code);
46 }
47
48 $filename = $this->get_response_filename($response);
49 if ($filename) {
50 $result = $filename;
51 }
52 }
53
54 if (is_wp_error($response)) {
55 return $response;
56 }
57
58
59
60 if (!file_put_contents($destination, $response['body'])) {
61 return false;
62 }
63
64 return $result;
65 }
66
67 public function download_file_stream($source, $destination, $headers = [])
68 {
69 $response = wp_remote_get($source, ['stream' => true, 'filename' => $destination, 'timeout' => 30, 'sslverify' => false, 'headers' => $headers]);
70 $result = true;
71
72 if (!is_wp_error($response)) {
73 $response_code = wp_remote_retrieve_response_code($response);
74
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 }
79
80 $filename = $this->get_response_filename($response);
81 if ($filename) {
82 $result = $filename;
83 }
84 }
85
86 if (is_wp_error($response)) {
87 return $this->download_file($source, $destination);
88 }
89 return $result;
90 }
91
92 public function get_response_filename($response)
93 {
94 $content_disposition = wp_remote_retrieve_header($response, 'content-disposition');
95 $matches = [];
96 if (preg_match('~filename=(?|"([^"]*)"|\'([^\']*)\'|([^;]*))~', $content_disposition, $matches) !== false && isset($matches[1])) {
97 return $matches[1];
98 }
99
100 // If there is no extension on the file, use the response content-type to add th file extension
101 if (isset($response['filename'])) {
102
103 $filename = basename($response['filename']);
104 $current_ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
105
106 if (empty($current_ext)) {
107
108 $content_type = wp_remote_retrieve_header($response, 'content-type');
109
110 if ($content_type) {
111
112 $mime_types = wp_get_mime_types();
113 $possible_ext = array_search($content_type, $mime_types);
114 $allowed_extensions = explode('|', $possible_ext);
115
116 if ($possible_ext !== false && empty($current_ext) && !empty($allowed_extensions) && !in_array(strtolower($filename), $allowed_extensions)) {
117 return $filename . '.' . $allowed_extensions[0];
118 }
119 }
120 }
121 }
122
123 return false;
124 }
125
126 public function set_stream_headers()
127 {
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 }
140 }
141 }
142