1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. |
4
|
|
|
* |
5
|
|
|
* You are hereby granted a non-exclusive, worldwide, royalty-free license to |
6
|
|
|
* use, copy, modify, and distribute this software in source code or binary |
7
|
|
|
* form for use in connection with the web services and APIs provided by |
8
|
|
|
* Facebook. |
9
|
|
|
* |
10
|
|
|
* As with any software that integrates with the Facebook platform, your use |
11
|
|
|
* of this software is subject to the Facebook Developer Principles and |
12
|
|
|
* Policies [http://developers.facebook.com/policy/]. This copyright notice |
13
|
|
|
* shall be included in all copies or substantial portions of the software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
20
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
21
|
|
|
* DEALINGS IN THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace FacebookAds\Logger; |
26
|
|
|
|
27
|
|
|
use FacebookAds\Http\Parameters; |
28
|
|
|
use FacebookAds\Http\RequestInterface; |
29
|
|
|
use FacebookAds\Http\ResponseInterface; |
30
|
|
|
use FacebookAds\Logger\CurlLogger\JsonAwareParameters; |
31
|
|
|
|
32
|
|
|
class CurlLogger implements LoggerInterface { |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
const PARAM_DEFAULT_FLAG = 'd'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
const PARAM_URLENCODE_FLAG = '-data-urlencode'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
const PARAM_POST_FLAG = 'F'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
const METHOD_DEFAULT_FLAG = ''; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
const METHOD_GET_FLAG = 'G'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
const METHOD_PUT_FLAG = 'X PUT'; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
const METHOD_DELETE_FLAG = 'X DELETE'; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var resource |
71
|
|
|
*/ |
72
|
|
|
protected $handle; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var bool |
76
|
|
|
*/ |
77
|
|
|
protected $jsonPrettyPrint = false; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param resource $handle |
81
|
|
|
*/ |
82
|
7 |
|
public function __construct($handle = null) { |
83
|
7 |
|
$this->handle = is_resource($handle) ? $handle : STDOUT; |
84
|
7 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
5 |
|
public function isJsonPrettyPrint() { |
90
|
5 |
|
return $this->jsonPrettyPrint; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param bool $json_pretty_print |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
1 |
|
public function setJsonPrettyPrint($json_pretty_print) { |
98
|
1 |
|
$this->jsonPrettyPrint = $json_pretty_print; |
99
|
1 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $method |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
5 |
|
public static function getMethodFlag($method) { |
107
|
|
|
switch ($method) { |
108
|
5 |
|
case RequestInterface::METHOD_GET: |
109
|
1 |
|
return static::METHOD_GET_FLAG; |
110
|
4 |
|
case RequestInterface::METHOD_PUT: |
111
|
1 |
|
return static::METHOD_PUT_FLAG; |
112
|
3 |
|
case RequestInterface::METHOD_DELETE: |
113
|
1 |
|
return static::METHOD_DELETE_FLAG; |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
return static::METHOD_DEFAULT_FLAG; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $method |
121
|
|
|
* @param string $value |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
5 |
|
public static function getParamFlag($method, $value) { |
125
|
5 |
|
return $method === RequestInterface::METHOD_POST |
126
|
1 |
|
? static::PARAM_POST_FLAG |
127
|
4 |
|
: (strstr($value, "\n") |
128
|
1 |
|
? static::PARAM_URLENCODE_FLAG |
129
|
5 |
|
: static::PARAM_DEFAULT_FLAG); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $string |
134
|
|
|
* @param int $indent |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
1 |
|
protected function indent($string, $indent) { |
138
|
1 |
|
return str_replace("\n", " \n".str_repeat(' ', $indent), $string); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param Parameters $params |
143
|
|
|
* @param string $method |
144
|
|
|
* @param bool $is_file |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
5 |
|
protected function processParams(Parameters $params, $method, $is_file) { |
148
|
5 |
|
$chunks = array(); |
149
|
5 |
|
if ($this->isJsonPrettyPrint()) { |
150
|
1 |
|
$params = new JsonAwareParameters($params); |
151
|
|
|
} |
152
|
5 |
|
foreach ($params->export() as $name => $value) { |
153
|
5 |
|
$value = addcslashes( |
154
|
5 |
|
strpos($value, "\n") !== false |
155
|
1 |
|
? $this->indent($value, 2) |
156
|
5 |
|
: $value, |
157
|
5 |
|
'\''); |
158
|
5 |
|
$chunks[$name] = sprintf( |
159
|
5 |
|
'-%s \'%s=%s%s\'', |
160
|
5 |
|
$this->getParamFlag($method, $value), |
161
|
|
|
$name, |
162
|
5 |
|
$is_file ? '@' : '', |
163
|
5 |
|
$value); |
164
|
|
|
} |
165
|
|
|
|
166
|
5 |
|
return $chunks; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param RequestInterface $request |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
5 |
|
protected function processUrl(RequestInterface $request) { |
174
|
5 |
|
return $request->getProtocol().$request->getDomain() |
175
|
5 |
|
.'/v'.$request->getGraphVersion().$request->getPath(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string $buffer |
180
|
|
|
*/ |
181
|
5 |
|
protected function flush($buffer) { |
182
|
5 |
|
fwrite($this->handle, $buffer.PHP_EOL.PHP_EOL); |
183
|
5 |
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param mixed $level |
187
|
|
|
* @param string $message |
188
|
|
|
* @param array $context |
189
|
|
|
*/ |
190
|
1 |
|
public function log($level, $message, array $context = array()) { |
191
|
|
|
// We only care about requests |
192
|
1 |
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param array $array |
196
|
|
|
* @param mixed $key |
197
|
|
|
* @return mixed |
198
|
|
|
*/ |
199
|
5 |
|
protected function removeArrayKey(array &$array, $key) { |
200
|
5 |
|
if (array_key_exists($key, $array)) { |
201
|
4 |
|
$value = $array[$key]; |
202
|
4 |
|
unset($array[$key]); |
203
|
4 |
|
return $value; |
204
|
|
|
} else { |
205
|
1 |
|
return null; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param array $params |
211
|
|
|
* @return array |
212
|
|
|
*/ |
213
|
5 |
|
protected function sortParams(array $params) { |
214
|
5 |
|
$access_token = $this->removeArrayKey($params, 'access_token'); |
215
|
5 |
|
$appsecret_proof = $this->removeArrayKey($params, 'appsecret_proof'); |
216
|
5 |
|
$access_token !== null && $params['access_token'] = $access_token; |
217
|
5 |
|
$appsecret_proof !== null && $params['appsecret_proof'] = $appsecret_proof; |
218
|
|
|
|
219
|
5 |
|
return $params; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param string $level |
224
|
|
|
* @param RequestInterface $request |
225
|
|
|
* @param array $context |
226
|
|
|
*/ |
227
|
5 |
|
public function logRequest( |
228
|
|
|
$level, RequestInterface $request, array $context = array()) { |
229
|
|
|
|
230
|
5 |
|
$new_line = ' \\'.PHP_EOL.' '; |
231
|
5 |
|
$method = $request->getMethod(); |
232
|
5 |
|
$method_flag = static::getMethodFlag($method); |
233
|
5 |
|
$params = $this->sortParams(array_merge( |
234
|
5 |
|
$this->processParams($request->getQueryParams(), $method, false), |
235
|
5 |
|
$this->processParams($request->getBodyParams(), $method, false), |
236
|
5 |
|
$this->processParams($request->getFileParams(), $method, true))); |
237
|
|
|
|
238
|
5 |
|
$buffer = 'curl'.($method_flag ? ' -'.$method_flag : ''); |
239
|
5 |
|
foreach ($params as $param) { |
240
|
5 |
|
$buffer .= $new_line.$param; |
241
|
|
|
} |
242
|
5 |
|
$buffer .= $new_line.$this->processUrl($request); |
243
|
|
|
|
244
|
5 |
|
$this->flush($buffer); |
245
|
5 |
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param string $level |
249
|
|
|
* @param ResponseInterface $response |
250
|
|
|
* @param array $context |
251
|
|
|
*/ |
252
|
1 |
|
public function logResponse( |
253
|
|
|
$level, ResponseInterface $response, array $context = array()) { |
254
|
|
|
// We only care about requests |
255
|
1 |
|
} |
256
|
|
|
} |
257
|
|
|
|