summaryrefslogtreecommitdiff
path: root/app/Core/Http/Client.php
blob: 12b0a1cb61f646cc72f47d03531a52761fba09fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php

namespace Kanboard\Core\Http;

use Kanboard\Core\Base;

/**
 * HTTP client
 *
 * @package  http
 * @author   Frederic Guillot
 */
class Client extends Base
{
    /**
     * HTTP connection timeout in seconds
     *
     * @var integer
     */
    const HTTP_TIMEOUT = 5;

    /**
     * Number of maximum redirections for the HTTP client
     *
     * @var integer
     */
    const HTTP_MAX_REDIRECTS = 2;

    /**
     * HTTP client user agent
     *
     * @var string
     */
    const HTTP_USER_AGENT = 'Kanboard';

    /**
     * Send a GET HTTP request
     *
     * @access public
     * @param  string     $url
     * @param  string[]   $headers
     * @return string
     */
    public function get($url, array $headers = array())
    {
        return $this->doRequest('GET', $url, '', $headers);
    }

    /**
     * Send a GET HTTP request and parse JSON response
     *
     * @access public
     * @param  string     $url
     * @param  string[]   $headers
     * @return array
     */
    public function getJson($url, array $headers = array())
    {
        $response = $this->doRequest('GET', $url, '', array_merge(array('Accept: application/json'), $headers));
        return json_decode($response, true) ?: array();
    }

    /**
     * Send a POST HTTP request encoded in JSON
     *
     * @access public
     * @param  string     $url
     * @param  array      $data
     * @param  string[]   $headers
     * @return string
     */
    public function postJson($url, array $data, array $headers = array())
    {
        return $this->doRequest(
            'POST',
            $url,
            json_encode($data),
            array_merge(array('Content-type: application/json'), $headers)
        );
    }

    /**
     * Send a POST HTTP request encoded in www-form-urlencoded
     *
     * @access public
     * @param  string     $url
     * @param  array      $data
     * @param  string[]   $headers
     * @return string
     */
    public function postForm($url, array $data, array $headers = array())
    {
        return $this->doRequest(
            'POST',
            $url,
            http_build_query($data),
            array_merge(array('Content-type: application/x-www-form-urlencoded'), $headers)
        );
    }

    /**
     * Make the HTTP request
     *
     * @access private
     * @param  string     $method
     * @param  string     $url
     * @param  string     $content
     * @param  string[]   $headers
     * @return string
     */
    private function doRequest($method, $url, $content, array $headers)
    {
        if (empty($url)) {
            return '';
        }

        $stream = @fopen(trim($url), 'r', false, stream_context_create($this->getContext($method, $content, $headers)));
        $response = '';

        if (is_resource($stream)) {
            $response = stream_get_contents($stream);
        } else {
            $this->logger->error('HttpClient: request failed');
        }

        if (DEBUG) {
            $this->logger->debug('HttpClient: url='.$url);
            $this->logger->debug('HttpClient: payload='.$content);
            $this->logger->debug('HttpClient: metadata='.var_export(@stream_get_meta_data($stream), true));
            $this->logger->debug('HttpClient: response='.$response);
        }

        return $response;
    }

    /**
     * Get stream context
     *
     * @access private
     * @param  string     $method
     * @param  string     $content
     * @param  string[]   $headers
     * @return array
     */
    private function getContext($method, $content, array $headers)
    {
        $default_headers = array(
            'User-Agent: '.self::HTTP_USER_AGENT,
            'Connection: close',
        );

        if (HTTP_PROXY_USERNAME) {
            $default_headers[] = 'Proxy-Authorization: Basic '.base64_encode(HTTP_PROXY_USERNAME.':'.HTTP_PROXY_PASSWORD);
        }

        $headers = array_merge($default_headers, $headers);

        $context = array(
            'http' => array(
                'method' => $method,
                'protocol_version' => 1.1,
                'timeout' => self::HTTP_TIMEOUT,
                'max_redirects' => self::HTTP_MAX_REDIRECTS,
                'header' => implode("\r\n", $headers),
                'content' => $content
            )
        );

        if (HTTP_PROXY_HOSTNAME) {
            $context['http']['proxy'] = 'tcp://'.HTTP_PROXY_HOSTNAME.':'.HTTP_PROXY_PORT;
            $context['http']['request_fulluri'] = true;
        }

        return $context;
    }
}