1212
1313const { format } = require ( '../../helpers/format' )
1414const CodeBuilder = require ( '../../helpers/code-builder' )
15+ const { phpSqEscape } = require ( './helpers' )
1516
1617module . 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 {' )
0 commit comments