Skip to content

Commit bf61e2e

Browse files
committed
Fix issue with $ interpolation in PHP curl snippets
1 parent 22e5a6e commit bf61e2e

20 files changed

+152
-121
lines changed

src/targets/php/curl.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
const { format } = require('../../helpers/format')
1414
const CodeBuilder = require('../../helpers/code-builder')
15+
const { phpSqEscape } = require('./helpers')
1516

1617
module.exports = function (source, options) {
1718
const opts = Object.assign({
@@ -78,7 +79,16 @@ module.exports = function (source, options) {
7879

7980
curlOptions.forEach(function (option) {
8081
if (!~[null, undefined].indexOf(option.value)) {
81-
curlopts.push(format('%s => %s,', option.name, option.escape ? JSON.stringify(option.value) : option.value))
82+
curlopts.push(
83+
format('%s => %s,',
84+
option.name,
85+
option.escape && typeof option.value === 'string'
86+
? `'${phpSqEscape(option.value)}'`
87+
: option.escape
88+
? JSON.stringify(option.value)
89+
: option.value
90+
)
91+
)
8292
}
8393
})
8494

@@ -88,12 +98,12 @@ module.exports = function (source, options) {
8898
})
8999

90100
if (cookies.length) {
91-
curlopts.push(format('CURLOPT_COOKIE => "%s",', cookies.join('; ')))
101+
curlopts.push(format("CURLOPT_COOKIE => '%s'", phpSqEscape(cookies.join('; '))))
92102
}
93103

94104
// construct cookies
95105
const headers = Object.keys(source.headersObj).sort().map(function (key) {
96-
return format('"%s: %qd"', key, source.headersObj[key])
106+
return format("'%s: %s'", phpSqEscape(key), phpSqEscape(source.headersObj[key]))
97107
})
98108

99109
if (headers.length) {
@@ -113,9 +123,9 @@ module.exports = function (source, options) {
113123
.push('if ($err) {')
114124

115125
if (opts.namedErrors) {
116-
code.push(1, 'echo array_flip(get_defined_constants(true)["curl"])[$err];')
126+
code.push(1, "echo array_flip(get_defined_constants(true)['curl'])[$err];")
117127
} else {
118-
code.push(1, 'echo "cURL Error #:" . $err;')
128+
code.push(1, "echo 'cURL Error #:' . $err;")
119129
}
120130

121131
code.push('} else {')

src/targets/php/helpers.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

3-
const { escape } = require('../../helpers/format')
3+
// PHP single quotes are super simple - all escapes ignored except sq & slash
4+
const phpSqEscape = val => val.replace(/\\/g, '\\\\').replace(/'/g, "\\'")
45

56
const convert = function (obj, indent, lastIndent) {
67
let i, result
@@ -19,7 +20,7 @@ const convert = function (obj, indent, lastIndent) {
1920
break
2021

2122
case '[object String]':
22-
result = "'" + escape(obj, { delimiter: "'", escapeNewlines: false }) + "'"
23+
result = "'" + phpSqEscape(obj) + "'"
2324
break
2425

2526
case '[object Number]':
@@ -55,6 +56,7 @@ const convert = function (obj, indent, lastIndent) {
5556
}
5657

5758
module.exports = {
59+
phpSqEscape: phpSqEscape,
5860
convert: convert,
5961
methods: [
6062
'ACL',

test/fixtures/output/php/curl/application-form-encoded.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
$curl = curl_init();
44

55
curl_setopt_array($curl, [
6-
CURLOPT_URL => "http://mockbin.com/har",
6+
CURLOPT_URL => 'http://mockbin.com/har',
77
CURLOPT_RETURNTRANSFER => true,
8-
CURLOPT_ENCODING => "",
8+
CURLOPT_ENCODING => '',
99
CURLOPT_MAXREDIRS => 10,
1010
CURLOPT_TIMEOUT => 30,
1111
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
12-
CURLOPT_CUSTOMREQUEST => "POST",
13-
CURLOPT_POSTFIELDS => "foo=bar&hello=world",
12+
CURLOPT_CUSTOMREQUEST => 'POST',
13+
CURLOPT_POSTFIELDS => 'foo=bar&hello=world',
1414
CURLOPT_HTTPHEADER => [
15-
"content-type: application/x-www-form-urlencoded"
15+
'content-type: application/x-www-form-urlencoded'
1616
],
1717
]);
1818

@@ -22,7 +22,7 @@
2222
curl_close($curl);
2323

2424
if ($err) {
25-
echo "cURL Error #:" . $err;
25+
echo 'cURL Error #:' . $err;
2626
} else {
2727
echo $response;
2828
}

test/fixtures/output/php/curl/application-json.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
$curl = curl_init();
44

55
curl_setopt_array($curl, [
6-
CURLOPT_URL => "http://mockbin.com/har",
6+
CURLOPT_URL => 'http://mockbin.com/har',
77
CURLOPT_RETURNTRANSFER => true,
8-
CURLOPT_ENCODING => "",
8+
CURLOPT_ENCODING => '',
99
CURLOPT_MAXREDIRS => 10,
1010
CURLOPT_TIMEOUT => 30,
1111
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
12-
CURLOPT_CUSTOMREQUEST => "POST",
13-
CURLOPT_POSTFIELDS => "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}",
12+
CURLOPT_CUSTOMREQUEST => 'POST',
13+
CURLOPT_POSTFIELDS => '{"number":1,"string":"f\\"oo","arr":[1,2,3],"nested":{"a":"b"},"arr_mix":[1,"a",{"arr_mix_nested":{}}],"boolean":false}',
1414
CURLOPT_HTTPHEADER => [
15-
"content-type: application/json"
15+
'content-type: application/json'
1616
],
1717
]);
1818

@@ -22,7 +22,7 @@
2222
curl_close($curl);
2323

2424
if ($err) {
25-
echo "cURL Error #:" . $err;
25+
echo 'cURL Error #:' . $err;
2626
} else {
2727
echo $response;
2828
}

test/fixtures/output/php/curl/compression.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
$curl = curl_init();
44

55
curl_setopt_array($curl, [
6-
CURLOPT_URL => "http://mockbin.com/har",
6+
CURLOPT_URL => 'http://mockbin.com/har',
77
CURLOPT_RETURNTRANSFER => true,
8-
CURLOPT_ENCODING => "",
8+
CURLOPT_ENCODING => '',
99
CURLOPT_MAXREDIRS => 10,
1010
CURLOPT_TIMEOUT => 30,
1111
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
12-
CURLOPT_CUSTOMREQUEST => "GET",
12+
CURLOPT_CUSTOMREQUEST => 'GET',
1313
CURLOPT_HTTPHEADER => [
14-
"accept-encoding: deflate, gzip, br"
14+
'accept-encoding: deflate, gzip, br'
1515
],
1616
]);
1717

@@ -21,7 +21,7 @@
2121
curl_close($curl);
2222

2323
if ($err) {
24-
echo "cURL Error #:" . $err;
24+
echo 'cURL Error #:' . $err;
2525
} else {
2626
echo $response;
2727
}

test/fixtures/output/php/curl/cookies.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
$curl = curl_init();
44

55
curl_setopt_array($curl, [
6-
CURLOPT_URL => "http://mockbin.com/har",
6+
CURLOPT_URL => 'http://mockbin.com/har',
77
CURLOPT_RETURNTRANSFER => true,
8-
CURLOPT_ENCODING => "",
8+
CURLOPT_ENCODING => '',
99
CURLOPT_MAXREDIRS => 10,
1010
CURLOPT_TIMEOUT => 30,
1111
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
12-
CURLOPT_CUSTOMREQUEST => "POST",
13-
CURLOPT_COOKIE => "foo=bar; bar=baz",
12+
CURLOPT_CUSTOMREQUEST => 'POST',
13+
CURLOPT_COOKIE => 'foo=bar; bar=baz'
1414
]);
1515

1616
$response = curl_exec($curl);
@@ -19,7 +19,7 @@
1919
curl_close($curl);
2020

2121
if ($err) {
22-
echo "cURL Error #:" . $err;
22+
echo 'cURL Error #:' . $err;
2323
} else {
2424
echo $response;
2525
}

test/fixtures/output/php/curl/custom-method.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
$curl = curl_init();
44

55
curl_setopt_array($curl, [
6-
CURLOPT_URL => "http://mockbin.com/har",
6+
CURLOPT_URL => 'http://mockbin.com/har',
77
CURLOPT_RETURNTRANSFER => true,
8-
CURLOPT_ENCODING => "",
8+
CURLOPT_ENCODING => '',
99
CURLOPT_MAXREDIRS => 10,
1010
CURLOPT_TIMEOUT => 30,
1111
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
12-
CURLOPT_CUSTOMREQUEST => "PROPFIND",
12+
CURLOPT_CUSTOMREQUEST => 'PROPFIND',
1313
]);
1414

1515
$response = curl_exec($curl);
@@ -18,7 +18,7 @@
1818
curl_close($curl);
1919

2020
if ($err) {
21-
echo "cURL Error #:" . $err;
21+
echo 'cURL Error #:' . $err;
2222
} else {
2323
echo $response;
2424
}

test/fixtures/output/php/curl/full.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
$curl = curl_init();
44

55
curl_setopt_array($curl, [
6-
CURLOPT_URL => "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value",
6+
CURLOPT_URL => 'http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value',
77
CURLOPT_RETURNTRANSFER => true,
8-
CURLOPT_ENCODING => "",
8+
CURLOPT_ENCODING => '',
99
CURLOPT_MAXREDIRS => 10,
1010
CURLOPT_TIMEOUT => 30,
1111
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
12-
CURLOPT_CUSTOMREQUEST => "POST",
13-
CURLOPT_POSTFIELDS => "foo=bar",
14-
CURLOPT_COOKIE => "foo=bar; bar=baz",
12+
CURLOPT_CUSTOMREQUEST => 'POST',
13+
CURLOPT_POSTFIELDS => 'foo=bar',
14+
CURLOPT_COOKIE => 'foo=bar; bar=baz'
1515
CURLOPT_HTTPHEADER => [
16-
"accept: application/json",
17-
"content-type: application/x-www-form-urlencoded"
16+
'accept: application/json',
17+
'content-type: application/x-www-form-urlencoded'
1818
],
1919
]);
2020

@@ -24,7 +24,7 @@
2424
curl_close($curl);
2525

2626
if ($err) {
27-
echo "cURL Error #:" . $err;
27+
echo 'cURL Error #:' . $err;
2828
} else {
2929
echo $response;
3030
}

test/fixtures/output/php/curl/headers.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
$curl = curl_init();
44

55
curl_setopt_array($curl, [
6-
CURLOPT_URL => "http://mockbin.com/har",
6+
CURLOPT_URL => 'http://mockbin.com/har',
77
CURLOPT_RETURNTRANSFER => true,
8-
CURLOPT_ENCODING => "",
8+
CURLOPT_ENCODING => '',
99
CURLOPT_MAXREDIRS => 10,
1010
CURLOPT_TIMEOUT => 30,
1111
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
12-
CURLOPT_CUSTOMREQUEST => "GET",
12+
CURLOPT_CUSTOMREQUEST => 'GET',
1313
CURLOPT_HTTPHEADER => [
14-
"accept: application/json",
15-
"quoted-value: \"quoted\" 'string'",
16-
"x-foo: Bar"
14+
'accept: application/json',
15+
'quoted-value: "quoted" \'string\'',
16+
'x-foo: Bar'
1717
],
1818
]);
1919

@@ -23,7 +23,7 @@
2323
curl_close($curl);
2424

2525
if ($err) {
26-
echo "cURL Error #:" . $err;
26+
echo 'cURL Error #:' . $err;
2727
} else {
2828
echo $response;
2929
}

test/fixtures/output/php/curl/https.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
$curl = curl_init();
44

55
curl_setopt_array($curl, [
6-
CURLOPT_URL => "https://mockbin.com/har",
6+
CURLOPT_URL => 'https://mockbin.com/har',
77
CURLOPT_RETURNTRANSFER => true,
8-
CURLOPT_ENCODING => "",
8+
CURLOPT_ENCODING => '',
99
CURLOPT_MAXREDIRS => 10,
1010
CURLOPT_TIMEOUT => 30,
1111
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
12-
CURLOPT_CUSTOMREQUEST => "GET",
12+
CURLOPT_CUSTOMREQUEST => 'GET',
1313
]);
1414

1515
$response = curl_exec($curl);
@@ -18,7 +18,7 @@
1818
curl_close($curl);
1919

2020
if ($err) {
21-
echo "cURL Error #:" . $err;
21+
echo 'cURL Error #:' . $err;
2222
} else {
2323
echo $response;
2424
}

test/fixtures/output/php/curl/jsonObj-multiline.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
$curl = curl_init();
44

55
curl_setopt_array($curl, [
6-
CURLOPT_URL => "http://mockbin.com/har",
6+
CURLOPT_URL => 'http://mockbin.com/har',
77
CURLOPT_RETURNTRANSFER => true,
8-
CURLOPT_ENCODING => "",
8+
CURLOPT_ENCODING => '',
99
CURLOPT_MAXREDIRS => 10,
1010
CURLOPT_TIMEOUT => 30,
1111
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
12-
CURLOPT_CUSTOMREQUEST => "POST",
13-
CURLOPT_POSTFIELDS => "{\n \"foo\": \"bar\"\n}",
12+
CURLOPT_CUSTOMREQUEST => 'POST',
13+
CURLOPT_POSTFIELDS => '{
14+
"foo": "bar"
15+
}',
1416
CURLOPT_HTTPHEADER => [
15-
"content-type: application/json"
17+
'content-type: application/json'
1618
],
1719
]);
1820

@@ -22,7 +24,7 @@
2224
curl_close($curl);
2325

2426
if ($err) {
25-
echo "cURL Error #:" . $err;
27+
echo 'cURL Error #:' . $err;
2628
} else {
2729
echo $response;
2830
}

test/fixtures/output/php/curl/jsonObj-null-value.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
$curl = curl_init();
44

55
curl_setopt_array($curl, [
6-
CURLOPT_URL => "http://mockbin.com/har",
6+
CURLOPT_URL => 'http://mockbin.com/har',
77
CURLOPT_RETURNTRANSFER => true,
8-
CURLOPT_ENCODING => "",
8+
CURLOPT_ENCODING => '',
99
CURLOPT_MAXREDIRS => 10,
1010
CURLOPT_TIMEOUT => 30,
1111
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
12-
CURLOPT_CUSTOMREQUEST => "POST",
13-
CURLOPT_POSTFIELDS => "{\"foo\":null}",
12+
CURLOPT_CUSTOMREQUEST => 'POST',
13+
CURLOPT_POSTFIELDS => '{"foo":null}',
1414
CURLOPT_HTTPHEADER => [
15-
"content-type: application/json"
15+
'content-type: application/json'
1616
],
1717
]);
1818

@@ -22,7 +22,7 @@
2222
curl_close($curl);
2323

2424
if ($err) {
25-
echo "cURL Error #:" . $err;
25+
echo 'cURL Error #:' . $err;
2626
} else {
2727
echo $response;
2828
}

0 commit comments

Comments
 (0)