Completed
Push — master ( 5a53e5...bcb105 )
by Sergey
03:09
created

CurlHttpClient::initCookieJar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api;
4
5
use seregazhuk\PinterestBot\Helpers\Cookies;
6
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
7
use seregazhuk\PinterestBot\Api\Contracts\HttpClient;
8
9
/**
10
 * Class CurlAdapter.
11
 */
12
class CurlHttpClient implements HttpClient
13
{
14
    /**
15
     * Custom CURL options for requests.
16
     *
17
     * @var array
18
     */
19
    protected $options = [
20
        CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0'
21
    ];
22
23
    /**
24
     * @var array
25
     */
26
    protected $headers = [];
27
28
    /**
29
     * Contains the curl instance.
30
     *
31
     * @var resource
32
     */
33
    protected $curl;
34
35
    /**
36
     * @var string
37
     */
38
    protected $cookieJar;
39
40
    /**
41
     * Cookies container
42
     *
43
     * @var Cookies
44
     */
45
    protected $cookies;
46
47
    /**
48
     * @var string
49
     */
50
    protected $currentUrl;
51
52
    /**
53
     * @var string
54
     */
55
    protected $cookiesPath;
56
57
    public function __construct(Cookies $cookies)
58
    {
59
        $this->cookies = $cookies;
60
    }
61
62
    /**
63
     * Load cookies for specified username
64
     *
65
     * @param string $username
66
     * @return HttpClient
67
     */
68
    public function loadCookies($username = '')
69
    {
70
        return $this->initCookieJar($username)
71
            ->fillCookies();
72
    }
73
74
    /**
75
     * Executes curl request.
76
     *
77
     * @param string $url
78
     * @param string $postString
79
     * @param array $headers
80
     * @return string
81
     */
82
    public function execute($url, $postString = '', array $headers = [])
83
    {
84
        return $this
85
            ->init($url, $postString, $headers)
86
            ->callCurl();
87
    }
88
89
    /**
90
     * @return mixed
91
     */
92
    protected function callCurl()
93
    {
94
        $res = curl_exec($this->curl);
95
96
        $this->currentUrl = curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL);
97
98
        curl_close($this->curl);
99
100
        $this->fillCookies();
101
102
        return $res;
103
    }
104
105
    /**
106
     * Initializes curl resource with options.
107
     *
108
     * @param string $url
109
     * @param string $postString
110
     * @param array $headers
111
     * @return $this
112
     */
113
    protected function init($url, $postString, $headers)
114
    {
115
        $this->headers = $headers;
116
117
        $this->curl = curl_init($url);
118
119
        if (empty($this->cookieJar)) {
120
            $this->loadCookies();
121
        }
122
123
        curl_setopt_array($this->curl, $this->makeHttpOptions($postString));
124
125
        return $this;
126
    }
127
128
    /**
129
     * @return array
130
     */
131
    protected function getDefaultHttpOptions()
132
    {
133
        return [
134
            CURLOPT_RETURNTRANSFER => true,
135
            CURLOPT_SSL_VERIFYPEER => false,
136
            CURLOPT_FOLLOWLOCATION => true,
137
            CURLOPT_ENCODING       => 'gzip,deflate',
138
            CURLOPT_HTTPHEADER     => $this->headers,
139
            CURLOPT_REFERER        => UrlBuilder::URL_BASE,
140
            CURLOPT_COOKIEFILE     => $this->cookieJar,
141
            CURLOPT_COOKIEJAR      => $this->cookieJar,
142
        ];
143
    }
144
145
    /**
146
     * Adds necessary curl options for query.
147
     *
148
     * @param string $postString POST query string
149
     *
150
     * @return array
151
     */
152
    protected function makeHttpOptions($postString = '')
153
    {
154
        // Union custom Curl options and default.
155
        $options = array_replace(
156
            $this->options,
157
            $this->getDefaultHttpOptions()
158
        );
159
160
        if (!empty($postString)) {
161
            $options[CURLOPT_POST] = true;
162
            $options[CURLOPT_POSTFIELDS] = $postString;
163
        }
164
165
        return $options;
166
    }
167
168
    /**
169
     * Set custom Curl options to override default
170
     *
171
     * @param array $options
172
     * @return CurlHttpClient
173
     */
174
    public function setOptions(array $options)
175
    {
176
        $this->options = $options;
177
178
        return $this;
179
    }
180
181
    /**
182
     * Get a cookie value by name
183
     *
184
     * @param $name
185
     * @return mixed
186
     */
187
    public function cookie($name)
188
    {
189
        return $this->cookies->get($name);
190
    }
191
192
    /**
193
     * Get all cookies
194
     *
195
     * @return array
196
     */
197
    public function cookies()
198
    {
199
        return $this->cookies->all();
200
    }
201
202
    /**
203
     * Set directory to store all cookie files.
204
     * @param string $path
205
     * @return $this
206
     */
207
    public function setCookiesPath($path)
208
    {
209
        $this->cookiesPath = $path;
210
211
        return $this;
212
    }
213
214
    /**
215
     * Init cookie file for a specified username. If username is empty we use
216
     * common cookie file for all sessions. If file does not exist it will
217
     * be created in system temp directory.
218
     *
219
     * @param $username
220
     * @return $this
221
     */
222
    protected function initCookieJar($username = '')
223
    {
224
        $this->cookieJar = $this->initCookieFile($username);
225
226
        return $this;
227
    }
228
229
    /**
230
     * Returns cookie file name by username. If username is empty we use a
231
     * random cookie name, to be sure we have different cookies
232
     * in parallel sessions.
233
     *
234
     * @param string $username
235
     * @return string
236
     */
237
    protected function initCookieFile($username)
238
    {
239
        if(empty($username)) {
240
            return tempnam($this->getCookiesPath(), self::COOKIE_PREFIX);
241
        }
242
243
        $cookieName = self::COOKIE_PREFIX . $username;
244
        $cookieFilePath = $this->getCookiesPath() . DIRECTORY_SEPARATOR . $cookieName;
245
246
        if (!file_exists($cookieFilePath)) {
247
            touch($cookieFilePath);
248
        }
249
250
        return $cookieFilePath;
251
    }
252
253
    /**
254
     * @return string
255
     */
256
    public function getCookiesPath()
257
    {
258
        return $this->cookiesPath ? : sys_get_temp_dir();
259
    }
260
261
    /**
262
     * @return $this
263
     */
264
    protected function fillCookies()
265
    {
266
        $this->cookies->fill($this->cookieJar);
267
268
        return $this;
269
    }
270
271
    /**
272
     * @return string
273
     */
274
    public function getCurrentUrl()
275
    {
276
        return $this->currentUrl;
277
    }
278
}