1 | <?php |
||
13 | class Request |
||
14 | { |
||
15 | const DEFAULT_TOKEN = '1234'; |
||
16 | |||
17 | /** |
||
18 | * @var HttpClient |
||
19 | */ |
||
20 | protected $httpClient; |
||
21 | |||
22 | /** |
||
23 | * @var bool |
||
24 | */ |
||
25 | protected $loggedIn; |
||
26 | |||
27 | /** |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $filePathToUpload; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $csrfToken = ''; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $postFileData; |
||
42 | |||
43 | /** |
||
44 | * Common headers needed for every query. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $requestHeaders = [ |
||
49 | 'Accept: application/json, text/javascript, */*; q=0.01', |
||
50 | 'Accept-Language: en-US,en;q=0.5', |
||
51 | 'DNT: 1', |
||
52 | 'X-Pinterest-AppState: active', |
||
53 | 'X-NEW-APP: 1', |
||
54 | 'X-APP-VERSION: 831c928', |
||
55 | 'X-Requested-With: XMLHttpRequest', |
||
56 | 'X-Pinterest-AppState:active', |
||
57 | ]; |
||
58 | |||
59 | /** |
||
60 | * @param HttpClient $http |
||
61 | */ |
||
62 | public function __construct(HttpClient $http) |
||
63 | { |
||
64 | $this->httpClient = $http; |
||
65 | $this->loggedIn = false; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param string $pathToFile |
||
70 | * @param string $url |
||
71 | * @return string |
||
72 | * @throws InvalidRequest |
||
73 | */ |
||
74 | public function upload($pathToFile, $url) |
||
75 | { |
||
76 | $this->filePathToUpload = $pathToFile; |
||
77 | |||
78 | return $this->exec($url); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Executes request to Pinterest API. |
||
83 | * |
||
84 | * @param string $resourceUrl |
||
85 | * @param string $postString |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | public function exec($resourceUrl, $postString = '') |
||
90 | { |
||
91 | $url = UrlBuilder::buildApiUrl($resourceUrl); |
||
92 | $headers = $this->getHttpHeaders(); |
||
93 | $postString = $this->filePathToUpload ? $this->postFileData : $postString; |
||
94 | |||
95 | $result = $this |
||
96 | ->httpClient |
||
97 | ->execute($url, $postString, $headers); |
||
98 | |||
99 | $this->setTokenFromCookies(); |
||
100 | |||
101 | $this->filePathToUpload = null; |
||
102 | |||
103 | return $result; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return array |
||
108 | */ |
||
109 | protected function getHttpHeaders() |
||
110 | { |
||
111 | $headers = $this->getDefaultHttpHeaders(); |
||
112 | if ($this->csrfToken == self::DEFAULT_TOKEN) { |
||
113 | $headers[] = 'Cookie: csrftoken=' . self::DEFAULT_TOKEN . ';'; |
||
114 | } |
||
115 | |||
116 | return $headers; |
||
117 | } |
||
118 | |||
119 | |||
120 | /** |
||
121 | * Clear token information. |
||
122 | * |
||
123 | * @return $this |
||
124 | */ |
||
125 | protected function clearToken() |
||
126 | { |
||
127 | $this->csrfToken = self::DEFAULT_TOKEN; |
||
128 | |||
129 | return $this; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Load cookies for this username and check if it was logged in. |
||
134 | * @param string $username |
||
135 | * @return bool |
||
136 | */ |
||
137 | public function autoLogin($username) |
||
138 | { |
||
139 | $this->httpClient->loadCookies($username); |
||
140 | |||
141 | if (!$this->httpClient->cookie('_auth')) { |
||
142 | return false; |
||
143 | } |
||
144 | |||
145 | $this->login(); |
||
146 | |||
147 | return true; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Mark client as logged. |
||
152 | */ |
||
153 | public function login() |
||
154 | { |
||
155 | $this->setTokenFromCookies(); |
||
156 | |||
157 | if(!empty($this->csrfToken)) { |
||
158 | $this->loggedIn = true; |
||
159 | } |
||
160 | |||
161 | return $this; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Mark client as logged out. |
||
166 | */ |
||
167 | public function logout() |
||
172 | |||
173 | /** |
||
174 | * Get current auth status. |
||
175 | * |
||
176 | * @return bool |
||
177 | */ |
||
178 | public function isLoggedIn() |
||
182 | |||
183 | /** |
||
184 | * Create request string. |
||
185 | * |
||
186 | * @param array $data |
||
187 | * @param array|null $bookmarks |
||
188 | * @return string |
||
189 | */ |
||
190 | public static function createQuery(array $data = [], $bookmarks = null) |
||
202 | |||
203 | /** |
||
204 | * @param array|object $data |
||
205 | * @param array $bookmarks |
||
206 | * @return array |
||
207 | */ |
||
208 | public static function createRequestData(array $data = [], $bookmarks = []) |
||
227 | |||
228 | /** |
||
229 | * Cast all non-array values to strings |
||
230 | * |
||
231 | * @param $array |
||
232 | * @return array |
||
233 | */ |
||
234 | protected static function prepareArrayToJson(array $array) |
||
245 | |||
246 | /** |
||
247 | * Trying to execGet csrf token from cookies. |
||
248 | * |
||
249 | * @return $this |
||
250 | */ |
||
251 | protected function setTokenFromCookies() |
||
259 | |||
260 | /** |
||
261 | * @return array |
||
262 | */ |
||
263 | protected function getDefaultHttpHeaders() |
||
275 | |||
276 | /** |
||
277 | * If we are uploading file, we should build boundary form data. Otherwise |
||
278 | * it is simple urlencoded form. |
||
279 | * |
||
280 | * @return array |
||
281 | */ |
||
282 | protected function getContentTypeHeader() |
||
288 | |||
289 | /** |
||
290 | * @param string $delimiter |
||
291 | * @return $this |
||
292 | */ |
||
293 | protected function buildFilePostData($delimiter) |
||
305 | |||
306 | /** |
||
307 | * @return array |
||
308 | */ |
||
309 | protected function makeHeadersForUpload() |
||
319 | |||
320 | /** |
||
321 | * @return HttpClient |
||
322 | */ |
||
323 | public function getHttpClient() |
||
327 | |||
328 | /** |
||
329 | * @return string |
||
330 | */ |
||
331 | public function getCurrentUrl() |
||
335 | |||
336 | /** |
||
337 | * @return $this |
||
338 | */ |
||
339 | public function dropCookies() |
||
346 | } |
||
347 |