# jc-importer/2.7.12/class/Common/Http/Http.php

Import WP – CSV &amp; XML Import Export for WordPress, version 2.7.12. 142 lines.

- Page: https://pluginprobe.com/plugins/jc-importer/2.7.12/code/class/Common/Http/Http.php
- Raw: https://pluginprobe.com/plugins/jc-importer/2.7.12/raw/class/Common/Http/Http.php
- Modified: 2022-07-30T10:15:10+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/jc-importer/2.7.12/code/class/Common/Http/Http.php#L10-L20`.

```php
<?php

namespace ImportWP\Common\Http;

use ImportWP\Common\Properties\Properties;
use ImportWP\Common\Util\Logger;
use ImportWP\Container;

class Http
{
    /**
     * @var Properties
     */
    protected $properties;

    public function __construct(Properties $properties)
    {
        $this->properties = $properties;
    }

    public function end_rest_success($data)
    {
        return [
            'status' => 'S',
            'data' => $data
        ];
    }

    public function end_rest_error($data)
    {
        return [
            'status' => 'E',
            'data' => $data
        ];
    }

    public function download_file($source, $destination, $headers = [])
    {
        $response = wp_remote_get($source, array('timeout' => 30, 'sslverify' => false, 'headers' => $headers));
        $result = true;
        if (!is_wp_error($response)) {
            $response_code = wp_remote_retrieve_response_code($response);
            Logger::write(__CLASS__ . '::download_file -response-code=' . $response_code . ' -url=' . esc_url($source));
            if ($response_code !== 200) {
                return new \WP_Error('IWP_HTTP_1', 'Unable to download: ' . esc_url($source) . ', Response Code: ' . $response_code);
            }

            $filename = $this->get_response_filename($response);
            if ($filename) {
                $result = $filename;
            }
        }

        if (is_wp_error($response)) {
            return $response;
        }



        if (!file_put_contents($destination, $response['body'])) {
            return false;
        }

        return $result;
    }

    public function download_file_stream($source, $destination, $headers = [])
    {
        $response = wp_remote_get($source, ['stream' => true, 'filename' => $destination, 'timeout' => 30, 'sslverify' => false, 'headers' => $headers]);
        $result = true;

        if (!is_wp_error($response)) {
            $response_code = wp_remote_retrieve_response_code($response);

            Logger::write(__CLASS__ . '::download_file_stream -response-code=' . $response_code . ' -url=' . esc_url($source) . ' -size=' . filesize($destination));
            if ($response_code !== 200) {
                return new \WP_Error('IWP_HTTP_1', 'Unable to download: ' . esc_url($source) . ', Response Code: ' . $response_code);
            }

            $filename = $this->get_response_filename($response);
            if ($filename) {
                $result = $filename;
            }
        }

        if (is_wp_error($response)) {
            return $this->download_file($source, $destination);
        }
        return $result;
    }

    public function get_response_filename($response)
    {
        $content_disposition = wp_remote_retrieve_header($response, 'content-disposition');
        $matches = [];
        if (preg_match('~filename=(?|"([^"]*)"|\'([^\']*)\'|([^;]*))~', $content_disposition, $matches) !== false && isset($matches[1])) {
            return $matches[1];
        }

        // If there is no extension on the file, use the response content-type to add th file extension
        if (isset($response['filename'])) {

            $filename = basename($response['filename']);
            $current_ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

            if (empty($current_ext)) {

                $content_type = wp_remote_retrieve_header($response, 'content-type');

                if ($content_type) {

                    $mime_types = wp_get_mime_types();
                    $possible_ext = array_search($content_type, $mime_types);
                    $allowed_extensions = explode('|', $possible_ext);

                    if ($possible_ext !== false && empty($current_ext) && !empty($allowed_extensions) && !in_array(strtolower($filename), $allowed_extensions)) {
                        return $filename . '.' . $allowed_extensions[0];
                    }
                }
            }
        }

        return false;
    }

    public function set_stream_headers()
    {
        $importer_manager = Container::getInstance()->get('importer_manager');
        if (false === $importer_manager->is_debug()) {
            header('Content-Type: text/event-stream');
        }

        header("Content-Encoding: none");
        header('Cache-Control: no-cache');

        // Allow for other requests to run at the same time
        if (session_status() == PHP_SESSION_ACTIVE) {
            session_write_close();
        }
    }
}

```
