xref: /PHP-7.4/ext/curl/interface.c (revision 8c292a2f)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Sterling Hughes <sterling@php.net>                           |
16    +----------------------------------------------------------------------+
17 */
18 
19 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "php.h"
26 
27 #if HAVE_CURL
28 
29 #include <stdio.h>
30 #include <string.h>
31 
32 #ifdef PHP_WIN32
33 #include <winsock2.h>
34 #include <sys/types.h>
35 #endif
36 
37 #include <curl/curl.h>
38 #include <curl/easy.h>
39 
40 /* As of curl 7.11.1 this is no longer defined inside curl.h */
41 #ifndef HttpPost
42 #define HttpPost curl_httppost
43 #endif
44 
45 /* {{{ cruft for thread safe SSL crypto locks */
46 #if defined(ZTS) && defined(HAVE_CURL_OLD_OPENSSL)
47 # if defined(HAVE_OPENSSL_CRYPTO_H)
48 #  define PHP_CURL_NEED_OPENSSL_TSL
49 #  include <openssl/crypto.h>
50 # else
51 #  warning \
52 	"libcurl was compiled with OpenSSL support, but configure could not find " \
53 	"openssl/crypto.h; thus no SSL crypto locking callbacks will be set, which may " \
54 	"cause random crashes on SSL requests"
55 # endif
56 #endif /* ZTS && HAVE_CURL_OLD_OPENSSL */
57 /* }}} */
58 
59 #define SMART_STR_PREALLOC 4096
60 
61 #include "zend_smart_str.h"
62 #include "ext/standard/info.h"
63 #include "ext/standard/file.h"
64 #include "ext/standard/url.h"
65 #include "php_curl.h"
66 
67 int  le_curl;
68 int  le_curl_multi_handle;
69 int  le_curl_share_handle;
70 
71 #ifdef PHP_CURL_NEED_OPENSSL_TSL /* {{{ */
72 static MUTEX_T *php_curl_openssl_tsl = NULL;
73 
74 /* Locking callbacks are no longer used since OpenSSL 1.1. Mark the functions as unused to
75  * avoid warnings due to this. */
php_curl_ssl_lock(int mode,int n,const char * file,int line)76 static ZEND_ATTRIBUTE_UNUSED void php_curl_ssl_lock(int mode, int n, const char * file, int line)
77 {
78 	if (mode & CRYPTO_LOCK) {
79 		tsrm_mutex_lock(php_curl_openssl_tsl[n]);
80 	} else {
81 		tsrm_mutex_unlock(php_curl_openssl_tsl[n]);
82 	}
83 }
84 
php_curl_ssl_id(void)85 static ZEND_ATTRIBUTE_UNUSED unsigned long php_curl_ssl_id(void)
86 {
87 	return (unsigned long) tsrm_thread_id();
88 }
89 #endif
90 /* }}} */
91 
92 static void _php_curl_close_ex(php_curl *ch);
93 static void _php_curl_close(zend_resource *rsrc);
94 
95 #define CAAL(s, v) add_assoc_long_ex(return_value, s, sizeof(s) - 1, (zend_long) v);
96 #define CAAD(s, v) add_assoc_double_ex(return_value, s, sizeof(s) - 1, (double) v);
97 #define CAAS(s, v) add_assoc_string_ex(return_value, s, sizeof(s) - 1, (char *) (v ? v : ""));
98 #define CAASTR(s, v) add_assoc_str_ex(return_value, s, sizeof(s) - 1, \
99 		v ? zend_string_copy(v) : ZSTR_EMPTY_ALLOC());
100 #define CAAZ(s, v) add_assoc_zval_ex(return_value, s, sizeof(s) -1 , (zval *) v);
101 
102 #if defined(PHP_WIN32) || defined(__GNUC__)
103 # define php_curl_ret(__ret) RETVAL_FALSE; return __ret;
104 #else
105 # define php_curl_ret(__ret) RETVAL_FALSE; return;
106 #endif
107 
php_curl_option_str(php_curl * ch,zend_long option,const char * str,const size_t len,zend_bool make_copy)108 static int php_curl_option_str(php_curl *ch, zend_long option, const char *str, const size_t len, zend_bool make_copy)
109 {
110 	CURLcode error = CURLE_OK;
111 
112 	if (strlen(str) != len) {
113 		php_error_docref(NULL, E_WARNING, "Curl option contains invalid characters (\\0)");
114 		return FAILURE;
115 	}
116 
117 #if LIBCURL_VERSION_NUM >= 0x071100
118 	if (make_copy) {
119 #endif
120 		char *copystr;
121 
122 		/* Strings passed to libcurl as 'char *' arguments, are copied by the library since 7.17.0 */
123 		copystr = estrndup(str, len);
124 		error = curl_easy_setopt(ch->cp, option, copystr);
125 		zend_llist_add_element(&ch->to_free->str, &copystr);
126 #if LIBCURL_VERSION_NUM >= 0x071100
127 	} else {
128 		error = curl_easy_setopt(ch->cp, option, str);
129 	}
130 #endif
131 
132 	SAVE_CURL_ERROR(ch, error);
133 
134 	return error == CURLE_OK ? SUCCESS : FAILURE;
135 }
136 
php_curl_option_url(php_curl * ch,const char * url,const size_t len)137 static int php_curl_option_url(php_curl *ch, const char *url, const size_t len) /* {{{ */
138 {
139 	/* Disable file:// if open_basedir are used */
140 	if (PG(open_basedir) && *PG(open_basedir)) {
141 #if LIBCURL_VERSION_NUM >= 0x071304
142 		curl_easy_setopt(ch->cp, CURLOPT_PROTOCOLS, CURLPROTO_ALL & ~CURLPROTO_FILE);
143 #else
144 		php_url *uri;
145 
146 		if (!(uri = php_url_parse_ex(url, len))) {
147 			php_error_docref(NULL, E_WARNING, "Invalid URL '%s'", url);
148 			return FAILURE;
149 		}
150 
151 		if (uri->scheme && zend_string_equals_literal_ci(uri->scheme, "file")) {
152 			php_error_docref(NULL, E_WARNING, "Protocol 'file' disabled in cURL");
153 			php_url_free(uri);
154 			return FAILURE;
155 		}
156 		php_url_free(uri);
157 #endif
158 	}
159 
160 #if LIBCURL_VERSION_NUM > 0x073800 && defined(PHP_WIN32)
161 	if (len > sizeof("file://") - 1 && '/' != url[sizeof("file://") - 1] && !strncmp("file://", url, sizeof("file://") - 1) && len < MAXPATHLEN - 2) {
162 		char _tmp[MAXPATHLEN] = {0};
163 
164 		memmove(_tmp, "file:///", sizeof("file:///") - 1);
165 		memmove(_tmp + sizeof("file:///") - 1, url + sizeof("file://") - 1, len - sizeof("file://") + 1);
166 
167 		return php_curl_option_str(ch, CURLOPT_URL, _tmp, len + 1, 0);
168 	}
169 #endif
170 
171 	return php_curl_option_str(ch, CURLOPT_URL, url, len, 0);
172 }
173 /* }}} */
174 
_php_curl_verify_handlers(php_curl * ch,int reporterror)175 void _php_curl_verify_handlers(php_curl *ch, int reporterror) /* {{{ */
176 {
177 	php_stream *stream;
178 
179 	ZEND_ASSERT(ch && ch->handlers);
180 
181 	if (!Z_ISUNDEF(ch->handlers->std_err)) {
182 		stream = (php_stream *)zend_fetch_resource2_ex(&ch->handlers->std_err, NULL, php_file_le_stream(), php_file_le_pstream());
183 		if (stream == NULL) {
184 			if (reporterror) {
185 				php_error_docref(NULL, E_WARNING, "CURLOPT_STDERR resource has gone away, resetting to stderr");
186 			}
187 			zval_ptr_dtor(&ch->handlers->std_err);
188 			ZVAL_UNDEF(&ch->handlers->std_err);
189 
190 			curl_easy_setopt(ch->cp, CURLOPT_STDERR, stderr);
191 		}
192 	}
193 	if (ch->handlers->read && !Z_ISUNDEF(ch->handlers->read->stream)) {
194 		stream = (php_stream *)zend_fetch_resource2_ex(&ch->handlers->read->stream, NULL, php_file_le_stream(), php_file_le_pstream());
195 		if (stream == NULL) {
196 			if (reporterror) {
197 				php_error_docref(NULL, E_WARNING, "CURLOPT_INFILE resource has gone away, resetting to default");
198 			}
199 			zval_ptr_dtor(&ch->handlers->read->stream);
200 			ZVAL_UNDEF(&ch->handlers->read->stream);
201 			ch->handlers->read->res = NULL;
202 			ch->handlers->read->fp = 0;
203 
204 			curl_easy_setopt(ch->cp, CURLOPT_INFILE, (void *) ch);
205 		}
206 	}
207 	if (ch->handlers->write_header && !Z_ISUNDEF(ch->handlers->write_header->stream)) {
208 		stream = (php_stream *)zend_fetch_resource2_ex(&ch->handlers->write_header->stream, NULL, php_file_le_stream(), php_file_le_pstream());
209 		if (stream == NULL) {
210 			if (reporterror) {
211 				php_error_docref(NULL, E_WARNING, "CURLOPT_WRITEHEADER resource has gone away, resetting to default");
212 			}
213 			zval_ptr_dtor(&ch->handlers->write_header->stream);
214 			ZVAL_UNDEF(&ch->handlers->write_header->stream);
215 			ch->handlers->write_header->fp = 0;
216 
217 			ch->handlers->write_header->method = PHP_CURL_IGNORE;
218 			curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER, (void *) ch);
219 		}
220 	}
221 	if (ch->handlers->write && !Z_ISUNDEF(ch->handlers->write->stream)) {
222 		stream = (php_stream *)zend_fetch_resource2_ex(&ch->handlers->write->stream, NULL, php_file_le_stream(), php_file_le_pstream());
223 		if (stream == NULL) {
224 			if (reporterror) {
225 				php_error_docref(NULL, E_WARNING, "CURLOPT_FILE resource has gone away, resetting to default");
226 			}
227 			zval_ptr_dtor(&ch->handlers->write->stream);
228 			ZVAL_UNDEF(&ch->handlers->write->stream);
229 			ch->handlers->write->fp = 0;
230 
231 			ch->handlers->write->method = PHP_CURL_STDOUT;
232 			curl_easy_setopt(ch->cp, CURLOPT_FILE, (void *) ch);
233 		}
234 	}
235 	return;
236 }
237 /* }}} */
238 
239 /* {{{ arginfo */
240 ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_version, 0, 0, 0)
241 	ZEND_ARG_INFO(0, version)
242 ZEND_END_ARG_INFO()
243 
244 ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_init, 0, 0, 0)
245 	ZEND_ARG_INFO(0, url)
246 ZEND_END_ARG_INFO()
247 
248 ZEND_BEGIN_ARG_INFO(arginfo_curl_copy_handle, 0)
249 	ZEND_ARG_INFO(0, ch)
250 ZEND_END_ARG_INFO()
251 
252 ZEND_BEGIN_ARG_INFO(arginfo_curl_setopt, 0)
253 	ZEND_ARG_INFO(0, ch)
254 	ZEND_ARG_INFO(0, option)
255 	ZEND_ARG_INFO(0, value)
256 ZEND_END_ARG_INFO()
257 
258 ZEND_BEGIN_ARG_INFO(arginfo_curl_setopt_array, 0)
259 	ZEND_ARG_INFO(0, ch)
260 	ZEND_ARG_ARRAY_INFO(0, options, 0)
261 ZEND_END_ARG_INFO()
262 
263 ZEND_BEGIN_ARG_INFO(arginfo_curl_exec, 0)
264 	ZEND_ARG_INFO(0, ch)
265 ZEND_END_ARG_INFO()
266 
267 ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_getinfo, 0, 0, 1)
268 	ZEND_ARG_INFO(0, ch)
269 	ZEND_ARG_INFO(0, option)
270 ZEND_END_ARG_INFO()
271 
272 ZEND_BEGIN_ARG_INFO(arginfo_curl_error, 0)
273 	ZEND_ARG_INFO(0, ch)
274 ZEND_END_ARG_INFO()
275 
276 ZEND_BEGIN_ARG_INFO(arginfo_curl_errno, 0)
277 	ZEND_ARG_INFO(0, ch)
278 ZEND_END_ARG_INFO()
279 
280 ZEND_BEGIN_ARG_INFO(arginfo_curl_close, 0)
281 	ZEND_ARG_INFO(0, ch)
282 ZEND_END_ARG_INFO()
283 
284 ZEND_BEGIN_ARG_INFO(arginfo_curl_reset, 0)
285 	ZEND_ARG_INFO(0, ch)
286 ZEND_END_ARG_INFO()
287 
288 ZEND_BEGIN_ARG_INFO(arginfo_curl_escape, 0)
289 	ZEND_ARG_INFO(0, ch)
290 	ZEND_ARG_INFO(0, str)
291 ZEND_END_ARG_INFO()
292 
293 ZEND_BEGIN_ARG_INFO(arginfo_curl_unescape, 0)
294 	ZEND_ARG_INFO(0, ch)
295 	ZEND_ARG_INFO(0, str)
296 ZEND_END_ARG_INFO()
297 
298 ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_setopt, 0)
299 	ZEND_ARG_INFO(0, sh)
300 	ZEND_ARG_INFO(0, option)
301 	ZEND_ARG_INFO(0, value)
302 ZEND_END_ARG_INFO()
303 
304 ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_init, 0)
305 ZEND_END_ARG_INFO()
306 
307 ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_add_handle, 0)
308 	ZEND_ARG_INFO(0, mh)
309 	ZEND_ARG_INFO(0, ch)
310 ZEND_END_ARG_INFO()
311 
312 ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_remove_handle, 0)
313 	ZEND_ARG_INFO(0, mh)
314 	ZEND_ARG_INFO(0, ch)
315 ZEND_END_ARG_INFO()
316 
317 ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_multi_select, 0, 0, 1)
318 	ZEND_ARG_INFO(0, mh)
319 	ZEND_ARG_INFO(0, timeout)
320 ZEND_END_ARG_INFO()
321 
322 ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_multi_exec, 0, 0, 1)
323 	ZEND_ARG_INFO(0, mh)
324 	ZEND_ARG_INFO(1, still_running)
325 ZEND_END_ARG_INFO()
326 
327 ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_getcontent, 0)
328 	ZEND_ARG_INFO(0, ch)
329 ZEND_END_ARG_INFO()
330 
331 ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_multi_info_read, 0, 0, 1)
332 	ZEND_ARG_INFO(0, mh)
333 	ZEND_ARG_INFO(1, msgs_in_queue)
334 ZEND_END_ARG_INFO()
335 
336 ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_close, 0)
337 	ZEND_ARG_INFO(0, mh)
338 ZEND_END_ARG_INFO()
339 
340 ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_errno, 0)
341 	ZEND_ARG_INFO(0, mh)
342 ZEND_END_ARG_INFO()
343 
344 ZEND_BEGIN_ARG_INFO(arginfo_curl_strerror, 0)
345 	ZEND_ARG_INFO(0, errornum)
346 ZEND_END_ARG_INFO()
347 
348 ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_strerror, 0)
349 	ZEND_ARG_INFO(0, errornum)
350 ZEND_END_ARG_INFO()
351 
352 ZEND_BEGIN_ARG_INFO(arginfo_curl_share_strerror, 0)
353 	ZEND_ARG_INFO(0, errornum)
354 ZEND_END_ARG_INFO()
355 
356 ZEND_BEGIN_ARG_INFO(arginfo_curl_share_init, 0)
357 ZEND_END_ARG_INFO()
358 
359 ZEND_BEGIN_ARG_INFO(arginfo_curl_share_close, 0)
360 	ZEND_ARG_INFO(0, sh)
361 ZEND_END_ARG_INFO()
362 
363 ZEND_BEGIN_ARG_INFO(arginfo_curl_share_setopt, 0)
364 	ZEND_ARG_INFO(0, sh)
365 	ZEND_ARG_INFO(0, option)
366 	ZEND_ARG_INFO(0, value)
367 ZEND_END_ARG_INFO()
368 
369 ZEND_BEGIN_ARG_INFO(arginfo_curl_share_errno, 0)
370 	ZEND_ARG_INFO(0, sh)
371 ZEND_END_ARG_INFO()
372 
373 #if LIBCURL_VERSION_NUM >= 0x071200 /* Available since 7.18.0 */
374 ZEND_BEGIN_ARG_INFO(arginfo_curl_pause, 0)
375 	ZEND_ARG_INFO(0, ch)
376 	ZEND_ARG_INFO(0, bitmask)
377 ZEND_END_ARG_INFO()
378 #endif
379 
380 ZEND_BEGIN_ARG_INFO_EX(arginfo_curlfile_create, 0, 0, 1)
381 	ZEND_ARG_INFO(0, filename)
382 	ZEND_ARG_INFO(0, mimetype)
383 	ZEND_ARG_INFO(0, postname)
384 ZEND_END_ARG_INFO()
385 /* }}} */
386 
387 /* {{{ curl_functions[]
388  */
389 static const zend_function_entry curl_functions[] = {
390 	PHP_FE(curl_init,                arginfo_curl_init)
391 	PHP_FE(curl_copy_handle,         arginfo_curl_copy_handle)
392 	PHP_FE(curl_version,             arginfo_curl_version)
393 	PHP_FE(curl_setopt,              arginfo_curl_setopt)
394 	PHP_FE(curl_setopt_array,        arginfo_curl_setopt_array)
395 	PHP_FE(curl_exec,                arginfo_curl_exec)
396 	PHP_FE(curl_getinfo,             arginfo_curl_getinfo)
397 	PHP_FE(curl_error,               arginfo_curl_error)
398 	PHP_FE(curl_errno,               arginfo_curl_errno)
399 	PHP_FE(curl_close,               arginfo_curl_close)
400 	PHP_FE(curl_strerror,            arginfo_curl_strerror)
401 	PHP_FE(curl_multi_strerror,      arginfo_curl_multi_strerror)
402 	PHP_FE(curl_share_strerror,      arginfo_curl_share_strerror)
403 	PHP_FE(curl_reset,               arginfo_curl_reset)
404 	PHP_FE(curl_escape,              arginfo_curl_escape)
405 	PHP_FE(curl_unescape,            arginfo_curl_unescape)
406 #if LIBCURL_VERSION_NUM >= 0x071200 /* 7.18.0 */
407 	PHP_FE(curl_pause,               arginfo_curl_pause)
408 #endif
409 	PHP_FE(curl_multi_init,          arginfo_curl_multi_init)
410 	PHP_FE(curl_multi_add_handle,    arginfo_curl_multi_add_handle)
411 	PHP_FE(curl_multi_remove_handle, arginfo_curl_multi_remove_handle)
412 	PHP_FE(curl_multi_select,        arginfo_curl_multi_select)
413 	PHP_FE(curl_multi_exec,          arginfo_curl_multi_exec)
414 	PHP_FE(curl_multi_getcontent,    arginfo_curl_multi_getcontent)
415 	PHP_FE(curl_multi_info_read,     arginfo_curl_multi_info_read)
416 	PHP_FE(curl_multi_close,         arginfo_curl_multi_close)
417 	PHP_FE(curl_multi_errno,         arginfo_curl_multi_errno)
418 	PHP_FE(curl_multi_setopt,        arginfo_curl_multi_setopt)
419 	PHP_FE(curl_share_init,          arginfo_curl_share_init)
420 	PHP_FE(curl_share_close,         arginfo_curl_share_close)
421 	PHP_FE(curl_share_setopt,        arginfo_curl_share_setopt)
422 	PHP_FE(curl_share_errno,         arginfo_curl_share_errno)
423 	PHP_FE(curl_file_create,         arginfo_curlfile_create)
424 	PHP_FE_END
425 };
426 /* }}} */
427 
428 /* {{{ curl_module_entry
429  */
430 zend_module_entry curl_module_entry = {
431 	STANDARD_MODULE_HEADER,
432 	"curl",
433 	curl_functions,
434 	PHP_MINIT(curl),
435 	PHP_MSHUTDOWN(curl),
436 	NULL,
437 	NULL,
438 	PHP_MINFO(curl),
439 	PHP_CURL_VERSION,
440 	STANDARD_MODULE_PROPERTIES
441 };
442 /* }}} */
443 
444 #ifdef COMPILE_DL_CURL
445 ZEND_GET_MODULE (curl)
446 #endif
447 
448 /* {{{ PHP_INI_BEGIN */
PHP_INI_BEGIN()449 PHP_INI_BEGIN()
450 	PHP_INI_ENTRY("curl.cainfo", "", PHP_INI_SYSTEM, NULL)
451 PHP_INI_END()
452 /* }}} */
453 
454 /* {{{ PHP_MINFO_FUNCTION
455  */
456 PHP_MINFO_FUNCTION(curl)
457 {
458 	curl_version_info_data *d;
459 	char **p;
460 	char str[1024];
461 	size_t n = 0;
462 
463 	d = curl_version_info(CURLVERSION_NOW);
464 	php_info_print_table_start();
465 	php_info_print_table_row(2, "cURL support",    "enabled");
466 	php_info_print_table_row(2, "cURL Information", d->version);
467 	sprintf(str, "%d", d->age);
468 	php_info_print_table_row(2, "Age", str);
469 
470 	/* To update on each new cURL release using src/main.c in cURL sources */
471 	if (d->features) {
472 		struct feat {
473 			const char *name;
474 			int bitmask;
475 		};
476 
477 		unsigned int i;
478 
479 		static const struct feat feats[] = {
480 			{"AsynchDNS", CURL_VERSION_ASYNCHDNS},
481 			{"CharConv", CURL_VERSION_CONV},
482 			{"Debug", CURL_VERSION_DEBUG},
483 			{"GSS-Negotiate", CURL_VERSION_GSSNEGOTIATE},
484 			{"IDN", CURL_VERSION_IDN},
485 			{"IPv6", CURL_VERSION_IPV6},
486 			{"krb4", CURL_VERSION_KERBEROS4},
487 			{"Largefile", CURL_VERSION_LARGEFILE},
488 			{"libz", CURL_VERSION_LIBZ},
489 			{"NTLM", CURL_VERSION_NTLM},
490 #if LIBCURL_VERSION_NUM >= 0x071600 /* 7.22.0 */
491 			{"NTLMWB", CURL_VERSION_NTLM_WB},
492 #endif
493 			{"SPNEGO", CURL_VERSION_SPNEGO},
494 			{"SSL",  CURL_VERSION_SSL},
495 			{"SSPI",  CURL_VERSION_SSPI},
496 #if LIBCURL_VERSION_NUM >= 0x071504 /* 7.21.4 */
497 			{"TLS-SRP", CURL_VERSION_TLSAUTH_SRP},
498 #endif
499 #if LIBCURL_VERSION_NUM >= 0x072100 /* 7.33.0 */
500 			{"HTTP2", CURL_VERSION_HTTP2},
501 #endif
502 #if LIBCURL_VERSION_NUM >= 0x072600 /* 7.38.0 */
503 			{"GSSAPI", CURL_VERSION_GSSAPI},
504 #endif
505 #if LIBCURL_VERSION_NUM >= 0x072800 /* 7.40.0 */
506 			{"KERBEROS5", CURL_VERSION_KERBEROS5},
507 			{"UNIX_SOCKETS", CURL_VERSION_UNIX_SOCKETS},
508 #endif
509 #if LIBCURL_VERSION_NUM >= 0x072f00 /* 7.47.0 */
510 			{"PSL", CURL_VERSION_PSL},
511 #endif
512 #if LIBCURL_VERSION_NUM >= 0x073400 /* 7.52.0 */
513 			{"HTTPS_PROXY", CURL_VERSION_HTTPS_PROXY},
514 #endif
515 #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
516 			{"MULTI_SSL", CURL_VERSION_MULTI_SSL},
517 #endif
518 #if LIBCURL_VERSION_NUM >= 0x073900 /* 7.57.0 */
519 			{"BROTLI", CURL_VERSION_BROTLI},
520 #endif
521 			{NULL, 0}
522 		};
523 
524 		php_info_print_table_row(1, "Features");
525 		for(i=0; i<sizeof(feats)/sizeof(feats[0]); i++) {
526 			if (feats[i].name) {
527 				php_info_print_table_row(2, feats[i].name, d->features & feats[i].bitmask ? "Yes" : "No");
528 			}
529 		}
530 	}
531 
532 	n = 0;
533 	p = (char **) d->protocols;
534 	while (*p != NULL) {
535 			n += sprintf(str + n, "%s%s", *p, *(p + 1) != NULL ? ", " : "");
536 			p++;
537 	}
538 	php_info_print_table_row(2, "Protocols", str);
539 
540 	php_info_print_table_row(2, "Host", d->host);
541 
542 	if (d->ssl_version) {
543 		php_info_print_table_row(2, "SSL Version", d->ssl_version);
544 	}
545 
546 	if (d->libz_version) {
547 		php_info_print_table_row(2, "ZLib Version", d->libz_version);
548 	}
549 
550 #if defined(CURLVERSION_SECOND) && CURLVERSION_NOW >= CURLVERSION_SECOND
551 	if (d->ares) {
552 		php_info_print_table_row(2, "ZLib Version", d->ares);
553 	}
554 #endif
555 
556 #if defined(CURLVERSION_THIRD) && CURLVERSION_NOW >= CURLVERSION_THIRD
557 	if (d->libidn) {
558 		php_info_print_table_row(2, "libIDN Version", d->libidn);
559 	}
560 #endif
561 
562 #if LIBCURL_VERSION_NUM >= 0x071300
563 
564 	if (d->iconv_ver_num) {
565 		php_info_print_table_row(2, "IconV Version", d->iconv_ver_num);
566 	}
567 
568 	if (d->libssh_version) {
569 		php_info_print_table_row(2, "libSSH Version", d->libssh_version);
570 	}
571 #endif
572 	php_info_print_table_end();
573 
574 	DISPLAY_INI_ENTRIES();
575 }
576 /* }}} */
577 
578 #define REGISTER_CURL_CONSTANT(__c) REGISTER_LONG_CONSTANT(#__c, __c, CONST_CS | CONST_PERSISTENT)
579 
580 /* {{{ PHP_MINIT_FUNCTION
581  */
PHP_MINIT_FUNCTION(curl)582 PHP_MINIT_FUNCTION(curl)
583 {
584 	le_curl = zend_register_list_destructors_ex(_php_curl_close, NULL, "curl", module_number);
585 	le_curl_multi_handle = zend_register_list_destructors_ex(_php_curl_multi_close, NULL, "curl_multi", module_number);
586 	le_curl_share_handle = zend_register_list_destructors_ex(_php_curl_share_close, NULL, "curl_share", module_number);
587 
588 	REGISTER_INI_ENTRIES();
589 
590 	/* See http://curl.haxx.se/lxr/source/docs/libcurl/symbols-in-versions
591 	   or curl src/docs/libcurl/symbols-in-versions for a (almost) complete list
592 	   of options and which version they were introduced */
593 
594 	/* Constants for curl_setopt() */
595 	REGISTER_CURL_CONSTANT(CURLOPT_AUTOREFERER);
596 	REGISTER_CURL_CONSTANT(CURLOPT_BINARYTRANSFER);
597 	REGISTER_CURL_CONSTANT(CURLOPT_BUFFERSIZE);
598 	REGISTER_CURL_CONSTANT(CURLOPT_CAINFO);
599 	REGISTER_CURL_CONSTANT(CURLOPT_CAPATH);
600 	REGISTER_CURL_CONSTANT(CURLOPT_CONNECTTIMEOUT);
601 	REGISTER_CURL_CONSTANT(CURLOPT_COOKIE);
602 	REGISTER_CURL_CONSTANT(CURLOPT_COOKIEFILE);
603 	REGISTER_CURL_CONSTANT(CURLOPT_COOKIEJAR);
604 	REGISTER_CURL_CONSTANT(CURLOPT_COOKIESESSION);
605 	REGISTER_CURL_CONSTANT(CURLOPT_CRLF);
606 	REGISTER_CURL_CONSTANT(CURLOPT_CUSTOMREQUEST);
607 	REGISTER_CURL_CONSTANT(CURLOPT_DNS_CACHE_TIMEOUT);
608 	REGISTER_CURL_CONSTANT(CURLOPT_DNS_USE_GLOBAL_CACHE);
609 	REGISTER_CURL_CONSTANT(CURLOPT_EGDSOCKET);
610 	REGISTER_CURL_CONSTANT(CURLOPT_ENCODING);
611 	REGISTER_CURL_CONSTANT(CURLOPT_FAILONERROR);
612 	REGISTER_CURL_CONSTANT(CURLOPT_FILE);
613 	REGISTER_CURL_CONSTANT(CURLOPT_FILETIME);
614 	REGISTER_CURL_CONSTANT(CURLOPT_FOLLOWLOCATION);
615 	REGISTER_CURL_CONSTANT(CURLOPT_FORBID_REUSE);
616 	REGISTER_CURL_CONSTANT(CURLOPT_FRESH_CONNECT);
617 	REGISTER_CURL_CONSTANT(CURLOPT_FTPAPPEND);
618 	REGISTER_CURL_CONSTANT(CURLOPT_FTPLISTONLY);
619 	REGISTER_CURL_CONSTANT(CURLOPT_FTPPORT);
620 	REGISTER_CURL_CONSTANT(CURLOPT_FTP_USE_EPRT);
621 	REGISTER_CURL_CONSTANT(CURLOPT_FTP_USE_EPSV);
622 	REGISTER_CURL_CONSTANT(CURLOPT_HEADER);
623 	REGISTER_CURL_CONSTANT(CURLOPT_HEADERFUNCTION);
624 	REGISTER_CURL_CONSTANT(CURLOPT_HTTP200ALIASES);
625 	REGISTER_CURL_CONSTANT(CURLOPT_HTTPGET);
626 	REGISTER_CURL_CONSTANT(CURLOPT_HTTPHEADER);
627 	REGISTER_CURL_CONSTANT(CURLOPT_HTTPPROXYTUNNEL);
628 	REGISTER_CURL_CONSTANT(CURLOPT_HTTP_VERSION);
629 	REGISTER_CURL_CONSTANT(CURLOPT_INFILE);
630 	REGISTER_CURL_CONSTANT(CURLOPT_INFILESIZE);
631 	REGISTER_CURL_CONSTANT(CURLOPT_INTERFACE);
632 	REGISTER_CURL_CONSTANT(CURLOPT_KRB4LEVEL);
633 	REGISTER_CURL_CONSTANT(CURLOPT_LOW_SPEED_LIMIT);
634 	REGISTER_CURL_CONSTANT(CURLOPT_LOW_SPEED_TIME);
635 	REGISTER_CURL_CONSTANT(CURLOPT_MAXCONNECTS);
636 	REGISTER_CURL_CONSTANT(CURLOPT_MAXREDIRS);
637 	REGISTER_CURL_CONSTANT(CURLOPT_NETRC);
638 	REGISTER_CURL_CONSTANT(CURLOPT_NOBODY);
639 	REGISTER_CURL_CONSTANT(CURLOPT_NOPROGRESS);
640 	REGISTER_CURL_CONSTANT(CURLOPT_NOSIGNAL);
641 	REGISTER_CURL_CONSTANT(CURLOPT_PORT);
642 	REGISTER_CURL_CONSTANT(CURLOPT_POST);
643 	REGISTER_CURL_CONSTANT(CURLOPT_POSTFIELDS);
644 	REGISTER_CURL_CONSTANT(CURLOPT_POSTQUOTE);
645 	REGISTER_CURL_CONSTANT(CURLOPT_PREQUOTE);
646 	REGISTER_CURL_CONSTANT(CURLOPT_PRIVATE);
647 	REGISTER_CURL_CONSTANT(CURLOPT_PROGRESSFUNCTION);
648 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY);
649 	REGISTER_CURL_CONSTANT(CURLOPT_PROXYPORT);
650 	REGISTER_CURL_CONSTANT(CURLOPT_PROXYTYPE);
651 	REGISTER_CURL_CONSTANT(CURLOPT_PROXYUSERPWD);
652 	REGISTER_CURL_CONSTANT(CURLOPT_PUT);
653 	REGISTER_CURL_CONSTANT(CURLOPT_QUOTE);
654 	REGISTER_CURL_CONSTANT(CURLOPT_RANDOM_FILE);
655 	REGISTER_CURL_CONSTANT(CURLOPT_RANGE);
656 	REGISTER_CURL_CONSTANT(CURLOPT_READDATA);
657 	REGISTER_CURL_CONSTANT(CURLOPT_READFUNCTION);
658 	REGISTER_CURL_CONSTANT(CURLOPT_REFERER);
659 	REGISTER_CURL_CONSTANT(CURLOPT_RESUME_FROM);
660 	REGISTER_CURL_CONSTANT(CURLOPT_RETURNTRANSFER);
661 	REGISTER_CURL_CONSTANT(CURLOPT_SHARE);
662 	REGISTER_CURL_CONSTANT(CURLOPT_SSLCERT);
663 	REGISTER_CURL_CONSTANT(CURLOPT_SSLCERTPASSWD);
664 	REGISTER_CURL_CONSTANT(CURLOPT_SSLCERTTYPE);
665 	REGISTER_CURL_CONSTANT(CURLOPT_SSLENGINE);
666 	REGISTER_CURL_CONSTANT(CURLOPT_SSLENGINE_DEFAULT);
667 	REGISTER_CURL_CONSTANT(CURLOPT_SSLKEY);
668 	REGISTER_CURL_CONSTANT(CURLOPT_SSLKEYPASSWD);
669 	REGISTER_CURL_CONSTANT(CURLOPT_SSLKEYTYPE);
670 	REGISTER_CURL_CONSTANT(CURLOPT_SSLVERSION);
671 	REGISTER_CURL_CONSTANT(CURLOPT_SSL_CIPHER_LIST);
672 	REGISTER_CURL_CONSTANT(CURLOPT_SSL_VERIFYHOST);
673 	REGISTER_CURL_CONSTANT(CURLOPT_SSL_VERIFYPEER);
674 	REGISTER_CURL_CONSTANT(CURLOPT_STDERR);
675 	REGISTER_CURL_CONSTANT(CURLOPT_TELNETOPTIONS);
676 	REGISTER_CURL_CONSTANT(CURLOPT_TIMECONDITION);
677 	REGISTER_CURL_CONSTANT(CURLOPT_TIMEOUT);
678 	REGISTER_CURL_CONSTANT(CURLOPT_TIMEVALUE);
679 	REGISTER_CURL_CONSTANT(CURLOPT_TRANSFERTEXT);
680 	REGISTER_CURL_CONSTANT(CURLOPT_UNRESTRICTED_AUTH);
681 	REGISTER_CURL_CONSTANT(CURLOPT_UPLOAD);
682 	REGISTER_CURL_CONSTANT(CURLOPT_URL);
683 	REGISTER_CURL_CONSTANT(CURLOPT_USERAGENT);
684 	REGISTER_CURL_CONSTANT(CURLOPT_USERPWD);
685 	REGISTER_CURL_CONSTANT(CURLOPT_VERBOSE);
686 	REGISTER_CURL_CONSTANT(CURLOPT_WRITEFUNCTION);
687 	REGISTER_CURL_CONSTANT(CURLOPT_WRITEHEADER);
688 
689 	/* */
690 	REGISTER_CURL_CONSTANT(CURLE_ABORTED_BY_CALLBACK);
691 	REGISTER_CURL_CONSTANT(CURLE_BAD_CALLING_ORDER);
692 	REGISTER_CURL_CONSTANT(CURLE_BAD_CONTENT_ENCODING);
693 	REGISTER_CURL_CONSTANT(CURLE_BAD_DOWNLOAD_RESUME);
694 	REGISTER_CURL_CONSTANT(CURLE_BAD_FUNCTION_ARGUMENT);
695 	REGISTER_CURL_CONSTANT(CURLE_BAD_PASSWORD_ENTERED);
696 	REGISTER_CURL_CONSTANT(CURLE_COULDNT_CONNECT);
697 	REGISTER_CURL_CONSTANT(CURLE_COULDNT_RESOLVE_HOST);
698 	REGISTER_CURL_CONSTANT(CURLE_COULDNT_RESOLVE_PROXY);
699 	REGISTER_CURL_CONSTANT(CURLE_FAILED_INIT);
700 	REGISTER_CURL_CONSTANT(CURLE_FILE_COULDNT_READ_FILE);
701 	REGISTER_CURL_CONSTANT(CURLE_FTP_ACCESS_DENIED);
702 	REGISTER_CURL_CONSTANT(CURLE_FTP_BAD_DOWNLOAD_RESUME);
703 	REGISTER_CURL_CONSTANT(CURLE_FTP_CANT_GET_HOST);
704 	REGISTER_CURL_CONSTANT(CURLE_FTP_CANT_RECONNECT);
705 	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_GET_SIZE);
706 	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_RETR_FILE);
707 	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_SET_ASCII);
708 	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_SET_BINARY);
709 	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_STOR_FILE);
710 	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_USE_REST);
711 	REGISTER_CURL_CONSTANT(CURLE_FTP_PARTIAL_FILE);
712 	REGISTER_CURL_CONSTANT(CURLE_FTP_PORT_FAILED);
713 	REGISTER_CURL_CONSTANT(CURLE_FTP_QUOTE_ERROR);
714 	REGISTER_CURL_CONSTANT(CURLE_FTP_USER_PASSWORD_INCORRECT);
715 	REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_227_FORMAT);
716 	REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_PASS_REPLY);
717 	REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_PASV_REPLY);
718 	REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_SERVER_REPLY);
719 	REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_USER_REPLY);
720 	REGISTER_CURL_CONSTANT(CURLE_FTP_WRITE_ERROR);
721 	REGISTER_CURL_CONSTANT(CURLE_FUNCTION_NOT_FOUND);
722 	REGISTER_CURL_CONSTANT(CURLE_GOT_NOTHING);
723 	REGISTER_CURL_CONSTANT(CURLE_HTTP_NOT_FOUND);
724 	REGISTER_CURL_CONSTANT(CURLE_HTTP_PORT_FAILED);
725 	REGISTER_CURL_CONSTANT(CURLE_HTTP_POST_ERROR);
726 	REGISTER_CURL_CONSTANT(CURLE_HTTP_RANGE_ERROR);
727 	REGISTER_CURL_CONSTANT(CURLE_HTTP_RETURNED_ERROR);
728 	REGISTER_CURL_CONSTANT(CURLE_LDAP_CANNOT_BIND);
729 	REGISTER_CURL_CONSTANT(CURLE_LDAP_SEARCH_FAILED);
730 	REGISTER_CURL_CONSTANT(CURLE_LIBRARY_NOT_FOUND);
731 	REGISTER_CURL_CONSTANT(CURLE_MALFORMAT_USER);
732 	REGISTER_CURL_CONSTANT(CURLE_OBSOLETE);
733 	REGISTER_CURL_CONSTANT(CURLE_OK);
734 	REGISTER_CURL_CONSTANT(CURLE_OPERATION_TIMEDOUT);
735 	REGISTER_CURL_CONSTANT(CURLE_OPERATION_TIMEOUTED);
736 	REGISTER_CURL_CONSTANT(CURLE_OUT_OF_MEMORY);
737 	REGISTER_CURL_CONSTANT(CURLE_PARTIAL_FILE);
738 	REGISTER_CURL_CONSTANT(CURLE_READ_ERROR);
739 	REGISTER_CURL_CONSTANT(CURLE_RECV_ERROR);
740 	REGISTER_CURL_CONSTANT(CURLE_SEND_ERROR);
741 	REGISTER_CURL_CONSTANT(CURLE_SHARE_IN_USE);
742 	REGISTER_CURL_CONSTANT(CURLE_SSL_CACERT);
743 	REGISTER_CURL_CONSTANT(CURLE_SSL_CERTPROBLEM);
744 	REGISTER_CURL_CONSTANT(CURLE_SSL_CIPHER);
745 	REGISTER_CURL_CONSTANT(CURLE_SSL_CONNECT_ERROR);
746 	REGISTER_CURL_CONSTANT(CURLE_SSL_ENGINE_NOTFOUND);
747 	REGISTER_CURL_CONSTANT(CURLE_SSL_ENGINE_SETFAILED);
748 	REGISTER_CURL_CONSTANT(CURLE_SSL_PEER_CERTIFICATE);
749 #if LIBCURL_VERSION_NUM >= 0x072700 /* Available since 7.39.0 */
750 	REGISTER_CURL_CONSTANT(CURLE_SSL_PINNEDPUBKEYNOTMATCH);
751 #endif
752 	REGISTER_CURL_CONSTANT(CURLE_TELNET_OPTION_SYNTAX);
753 	REGISTER_CURL_CONSTANT(CURLE_TOO_MANY_REDIRECTS);
754 	REGISTER_CURL_CONSTANT(CURLE_UNKNOWN_TELNET_OPTION);
755 	REGISTER_CURL_CONSTANT(CURLE_UNSUPPORTED_PROTOCOL);
756 	REGISTER_CURL_CONSTANT(CURLE_URL_MALFORMAT);
757 	REGISTER_CURL_CONSTANT(CURLE_URL_MALFORMAT_USER);
758 	REGISTER_CURL_CONSTANT(CURLE_WRITE_ERROR);
759 
760 	/* cURL info constants */
761 	REGISTER_CURL_CONSTANT(CURLINFO_CONNECT_TIME);
762 	REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_LENGTH_DOWNLOAD);
763 	REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_LENGTH_UPLOAD);
764 	REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_TYPE);
765 	REGISTER_CURL_CONSTANT(CURLINFO_EFFECTIVE_URL);
766 	REGISTER_CURL_CONSTANT(CURLINFO_FILETIME);
767 	REGISTER_CURL_CONSTANT(CURLINFO_HEADER_OUT);
768 	REGISTER_CURL_CONSTANT(CURLINFO_HEADER_SIZE);
769 	REGISTER_CURL_CONSTANT(CURLINFO_HTTP_CODE);
770 	REGISTER_CURL_CONSTANT(CURLINFO_LASTONE);
771 	REGISTER_CURL_CONSTANT(CURLINFO_NAMELOOKUP_TIME);
772 	REGISTER_CURL_CONSTANT(CURLINFO_PRETRANSFER_TIME);
773 	REGISTER_CURL_CONSTANT(CURLINFO_PRIVATE);
774 	REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_COUNT);
775 	REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_TIME);
776 	REGISTER_CURL_CONSTANT(CURLINFO_REQUEST_SIZE);
777 	REGISTER_CURL_CONSTANT(CURLINFO_SIZE_DOWNLOAD);
778 	REGISTER_CURL_CONSTANT(CURLINFO_SIZE_UPLOAD);
779 	REGISTER_CURL_CONSTANT(CURLINFO_SPEED_DOWNLOAD);
780 	REGISTER_CURL_CONSTANT(CURLINFO_SPEED_UPLOAD);
781 	REGISTER_CURL_CONSTANT(CURLINFO_SSL_VERIFYRESULT);
782 	REGISTER_CURL_CONSTANT(CURLINFO_STARTTRANSFER_TIME);
783 	REGISTER_CURL_CONSTANT(CURLINFO_TOTAL_TIME);
784 
785 	/* Other */
786 	REGISTER_CURL_CONSTANT(CURLMSG_DONE);
787 	REGISTER_CURL_CONSTANT(CURLVERSION_NOW);
788 
789 	/* Curl Multi Constants */
790 	REGISTER_CURL_CONSTANT(CURLM_BAD_EASY_HANDLE);
791 	REGISTER_CURL_CONSTANT(CURLM_BAD_HANDLE);
792 	REGISTER_CURL_CONSTANT(CURLM_CALL_MULTI_PERFORM);
793 	REGISTER_CURL_CONSTANT(CURLM_INTERNAL_ERROR);
794 	REGISTER_CURL_CONSTANT(CURLM_OK);
795 	REGISTER_CURL_CONSTANT(CURLM_OUT_OF_MEMORY);
796 #if LIBCURL_VERSION_NUM >= 0x072001 /* Available since 7.32.1 */
797 	REGISTER_CURL_CONSTANT(CURLM_ADDED_ALREADY);
798 #endif
799 
800 	/* Curl proxy constants */
801 	REGISTER_CURL_CONSTANT(CURLPROXY_HTTP);
802 	REGISTER_CURL_CONSTANT(CURLPROXY_SOCKS4);
803 	REGISTER_CURL_CONSTANT(CURLPROXY_SOCKS5);
804 
805 	/* Curl Share constants */
806 	REGISTER_CURL_CONSTANT(CURLSHOPT_NONE);
807 	REGISTER_CURL_CONSTANT(CURLSHOPT_SHARE);
808 	REGISTER_CURL_CONSTANT(CURLSHOPT_UNSHARE);
809 
810 	/* Curl Http Version constants (CURLOPT_HTTP_VERSION) */
811 	REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_1_0);
812 	REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_1_1);
813 	REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_NONE);
814 
815 	/* Curl Lock constants */
816 	REGISTER_CURL_CONSTANT(CURL_LOCK_DATA_COOKIE);
817 	REGISTER_CURL_CONSTANT(CURL_LOCK_DATA_DNS);
818 	REGISTER_CURL_CONSTANT(CURL_LOCK_DATA_SSL_SESSION);
819 
820 	/* Curl NETRC constants (CURLOPT_NETRC) */
821 	REGISTER_CURL_CONSTANT(CURL_NETRC_IGNORED);
822 	REGISTER_CURL_CONSTANT(CURL_NETRC_OPTIONAL);
823 	REGISTER_CURL_CONSTANT(CURL_NETRC_REQUIRED);
824 
825 	/* Curl SSL Version constants (CURLOPT_SSLVERSION) */
826 	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_DEFAULT);
827 	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_SSLv2);
828 	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_SSLv3);
829 	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_TLSv1);
830 
831 	/* Curl TIMECOND constants (CURLOPT_TIMECONDITION) */
832 	REGISTER_CURL_CONSTANT(CURL_TIMECOND_IFMODSINCE);
833 	REGISTER_CURL_CONSTANT(CURL_TIMECOND_IFUNMODSINCE);
834 	REGISTER_CURL_CONSTANT(CURL_TIMECOND_LASTMOD);
835 	REGISTER_CURL_CONSTANT(CURL_TIMECOND_NONE);
836 
837 	/* Curl version constants */
838 	REGISTER_CURL_CONSTANT(CURL_VERSION_ASYNCHDNS);
839 	REGISTER_CURL_CONSTANT(CURL_VERSION_CONV);
840 	REGISTER_CURL_CONSTANT(CURL_VERSION_DEBUG);
841 	REGISTER_CURL_CONSTANT(CURL_VERSION_GSSNEGOTIATE);
842 	REGISTER_CURL_CONSTANT(CURL_VERSION_IDN);
843 	REGISTER_CURL_CONSTANT(CURL_VERSION_IPV6);
844 	REGISTER_CURL_CONSTANT(CURL_VERSION_KERBEROS4);
845 	REGISTER_CURL_CONSTANT(CURL_VERSION_LARGEFILE);
846 	REGISTER_CURL_CONSTANT(CURL_VERSION_LIBZ);
847 	REGISTER_CURL_CONSTANT(CURL_VERSION_NTLM);
848 	REGISTER_CURL_CONSTANT(CURL_VERSION_SPNEGO);
849 	REGISTER_CURL_CONSTANT(CURL_VERSION_SSL);
850 	REGISTER_CURL_CONSTANT(CURL_VERSION_SSPI);
851 
852 	/* Available since 7.10.6 */
853 	REGISTER_CURL_CONSTANT(CURLOPT_HTTPAUTH);
854 	/* http authentication options */
855 	REGISTER_CURL_CONSTANT(CURLAUTH_ANY);
856 	REGISTER_CURL_CONSTANT(CURLAUTH_ANYSAFE);
857 	REGISTER_CURL_CONSTANT(CURLAUTH_BASIC);
858 	REGISTER_CURL_CONSTANT(CURLAUTH_DIGEST);
859 	REGISTER_CURL_CONSTANT(CURLAUTH_GSSNEGOTIATE);
860 	REGISTER_CURL_CONSTANT(CURLAUTH_NONE);
861 	REGISTER_CURL_CONSTANT(CURLAUTH_NTLM);
862 
863 	/* Available since 7.10.7 */
864 	REGISTER_CURL_CONSTANT(CURLINFO_HTTP_CONNECTCODE);
865 	REGISTER_CURL_CONSTANT(CURLOPT_FTP_CREATE_MISSING_DIRS);
866 	REGISTER_CURL_CONSTANT(CURLOPT_PROXYAUTH);
867 
868 	/* Available since 7.10.8 */
869 	REGISTER_CURL_CONSTANT(CURLE_FILESIZE_EXCEEDED);
870 	REGISTER_CURL_CONSTANT(CURLE_LDAP_INVALID_URL);
871 	REGISTER_CURL_CONSTANT(CURLINFO_HTTPAUTH_AVAIL);
872 	REGISTER_CURL_CONSTANT(CURLINFO_RESPONSE_CODE);
873 	REGISTER_CURL_CONSTANT(CURLINFO_PROXYAUTH_AVAIL);
874 	REGISTER_CURL_CONSTANT(CURLOPT_FTP_RESPONSE_TIMEOUT);
875 	REGISTER_CURL_CONSTANT(CURLOPT_IPRESOLVE);
876 	REGISTER_CURL_CONSTANT(CURLOPT_MAXFILESIZE);
877 	REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_V4);
878 	REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_V6);
879 	REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_WHATEVER);
880 
881 	/* Available since 7.11.0 */
882 	REGISTER_CURL_CONSTANT(CURLE_FTP_SSL_FAILED);
883 	REGISTER_CURL_CONSTANT(CURLFTPSSL_ALL);
884 	REGISTER_CURL_CONSTANT(CURLFTPSSL_CONTROL);
885 	REGISTER_CURL_CONSTANT(CURLFTPSSL_NONE);
886 	REGISTER_CURL_CONSTANT(CURLFTPSSL_TRY);
887 	REGISTER_CURL_CONSTANT(CURLOPT_FTP_SSL);
888 	REGISTER_CURL_CONSTANT(CURLOPT_NETRC_FILE);
889 
890 	/* Available since 7.11.2 */
891 	REGISTER_CURL_CONSTANT(CURLOPT_TCP_NODELAY);
892 
893 	/* Available since 7.12.2 */
894 	REGISTER_CURL_CONSTANT(CURLFTPAUTH_DEFAULT);
895 	REGISTER_CURL_CONSTANT(CURLFTPAUTH_SSL);
896 	REGISTER_CURL_CONSTANT(CURLFTPAUTH_TLS);
897 	REGISTER_CURL_CONSTANT(CURLOPT_FTPSSLAUTH);
898 
899 	/* Available since 7.13.0 */
900 	REGISTER_CURL_CONSTANT(CURLOPT_FTP_ACCOUNT);
901 
902 	/* Available since 7.12.2 */
903 	REGISTER_CURL_CONSTANT(CURLINFO_OS_ERRNO);
904 
905 	/* Available since 7.12.3 */
906 	REGISTER_CURL_CONSTANT(CURLINFO_NUM_CONNECTS);
907 	REGISTER_CURL_CONSTANT(CURLINFO_SSL_ENGINES);
908 
909 	/* Available since 7.14.1 */
910 	REGISTER_CURL_CONSTANT(CURLINFO_COOKIELIST);
911 	REGISTER_CURL_CONSTANT(CURLOPT_COOKIELIST);
912 	REGISTER_CURL_CONSTANT(CURLOPT_IGNORE_CONTENT_LENGTH);
913 
914 	/* Available since 7.15.0 */
915 	REGISTER_CURL_CONSTANT(CURLOPT_FTP_SKIP_PASV_IP);
916 
917 	/* Available since 7.15.1 */
918 	REGISTER_CURL_CONSTANT(CURLOPT_FTP_FILEMETHOD);
919 
920 	/* Available since 7.15.2 */
921 	REGISTER_CURL_CONSTANT(CURLOPT_CONNECT_ONLY);
922 	REGISTER_CURL_CONSTANT(CURLOPT_LOCALPORT);
923 	REGISTER_CURL_CONSTANT(CURLOPT_LOCALPORTRANGE);
924 
925 	/* Available since 7.15.3 */
926 	REGISTER_CURL_CONSTANT(CURLFTPMETHOD_MULTICWD);
927 	REGISTER_CURL_CONSTANT(CURLFTPMETHOD_NOCWD);
928 	REGISTER_CURL_CONSTANT(CURLFTPMETHOD_SINGLECWD);
929 
930 	/* Available since 7.15.4 */
931 	REGISTER_CURL_CONSTANT(CURLINFO_FTP_ENTRY_PATH);
932 
933 	/* Available since 7.15.5 */
934 	REGISTER_CURL_CONSTANT(CURLOPT_FTP_ALTERNATIVE_TO_USER);
935 	REGISTER_CURL_CONSTANT(CURLOPT_MAX_RECV_SPEED_LARGE);
936 	REGISTER_CURL_CONSTANT(CURLOPT_MAX_SEND_SPEED_LARGE);
937 
938 #if LIBCURL_VERSION_NUM >= 0x071000 /* Available since 7.16.0 */
939 	REGISTER_CURL_CONSTANT(CURLE_SSL_CACERT_BADFILE);
940 	REGISTER_CURL_CONSTANT(CURLOPT_SSL_SESSIONID_CACHE);
941 	REGISTER_CURL_CONSTANT(CURLMOPT_PIPELINING);
942 #endif
943 
944 #if LIBCURL_VERSION_NUM >= 0x071001 /* Available since 7.16.1 */
945 	REGISTER_CURL_CONSTANT(CURLE_SSH);
946 	REGISTER_CURL_CONSTANT(CURLOPT_FTP_SSL_CCC);
947 	REGISTER_CURL_CONSTANT(CURLOPT_SSH_AUTH_TYPES);
948 	REGISTER_CURL_CONSTANT(CURLOPT_SSH_PRIVATE_KEYFILE);
949 	REGISTER_CURL_CONSTANT(CURLOPT_SSH_PUBLIC_KEYFILE);
950 	REGISTER_CURL_CONSTANT(CURLFTPSSL_CCC_ACTIVE);
951 	REGISTER_CURL_CONSTANT(CURLFTPSSL_CCC_NONE);
952 	REGISTER_CURL_CONSTANT(CURLFTPSSL_CCC_PASSIVE);
953 #endif
954 
955 #if LIBCURL_VERSION_NUM >= 0x071002 /* Available since 7.16.2 */
956 	REGISTER_CURL_CONSTANT(CURLOPT_CONNECTTIMEOUT_MS);
957 	REGISTER_CURL_CONSTANT(CURLOPT_HTTP_CONTENT_DECODING);
958 	REGISTER_CURL_CONSTANT(CURLOPT_HTTP_TRANSFER_DECODING);
959 	REGISTER_CURL_CONSTANT(CURLOPT_TIMEOUT_MS);
960 #endif
961 
962 #if LIBCURL_VERSION_NUM >= 0x071003 /* Available since 7.16.3 */
963 	REGISTER_CURL_CONSTANT(CURLMOPT_MAXCONNECTS);
964 #endif
965 
966 #if LIBCURL_VERSION_NUM >= 0x071004 /* Available since 7.16.4 */
967 	REGISTER_CURL_CONSTANT(CURLOPT_KRBLEVEL);
968 	REGISTER_CURL_CONSTANT(CURLOPT_NEW_DIRECTORY_PERMS);
969 	REGISTER_CURL_CONSTANT(CURLOPT_NEW_FILE_PERMS);
970 #endif
971 
972 #if LIBCURL_VERSION_NUM >= 0x071100 /* Available since 7.17.0 */
973 	REGISTER_CURL_CONSTANT(CURLOPT_APPEND);
974 	REGISTER_CURL_CONSTANT(CURLOPT_DIRLISTONLY);
975 	REGISTER_CURL_CONSTANT(CURLOPT_USE_SSL);
976 	/* Curl SSL Constants */
977 	REGISTER_CURL_CONSTANT(CURLUSESSL_ALL);
978 	REGISTER_CURL_CONSTANT(CURLUSESSL_CONTROL);
979 	REGISTER_CURL_CONSTANT(CURLUSESSL_NONE);
980 	REGISTER_CURL_CONSTANT(CURLUSESSL_TRY);
981 #endif
982 
983 #if LIBCURL_VERSION_NUM >= 0x071101 /* Available since 7.17.1 */
984 	REGISTER_CURL_CONSTANT(CURLOPT_SSH_HOST_PUBLIC_KEY_MD5);
985 #endif
986 
987 #if LIBCURL_VERSION_NUM >= 0x071200 /* Available since 7.18.0 */
988 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_TRANSFER_MODE);
989 	REGISTER_CURL_CONSTANT(CURLPAUSE_ALL);
990 	REGISTER_CURL_CONSTANT(CURLPAUSE_CONT);
991 	REGISTER_CURL_CONSTANT(CURLPAUSE_RECV);
992 	REGISTER_CURL_CONSTANT(CURLPAUSE_RECV_CONT);
993 	REGISTER_CURL_CONSTANT(CURLPAUSE_SEND);
994 	REGISTER_CURL_CONSTANT(CURLPAUSE_SEND_CONT);
995 	REGISTER_CURL_CONSTANT(CURL_READFUNC_PAUSE);
996 	REGISTER_CURL_CONSTANT(CURL_WRITEFUNC_PAUSE);
997 
998 	REGISTER_CURL_CONSTANT(CURLPROXY_SOCKS4A);
999 	REGISTER_CURL_CONSTANT(CURLPROXY_SOCKS5_HOSTNAME);
1000 #endif
1001 
1002 #if LIBCURL_VERSION_NUM >= 0x071202 /* Available since 7.18.2 */
1003 	REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_URL);
1004 #endif
1005 
1006 #if LIBCURL_VERSION_NUM >= 0x071300 /* Available since 7.19.0 */
1007 	REGISTER_CURL_CONSTANT(CURLINFO_APPCONNECT_TIME);
1008 	REGISTER_CURL_CONSTANT(CURLINFO_PRIMARY_IP);
1009 
1010 	REGISTER_CURL_CONSTANT(CURLOPT_ADDRESS_SCOPE);
1011 	REGISTER_CURL_CONSTANT(CURLOPT_CRLFILE);
1012 	REGISTER_CURL_CONSTANT(CURLOPT_ISSUERCERT);
1013 	REGISTER_CURL_CONSTANT(CURLOPT_KEYPASSWD);
1014 
1015 	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_ANY);
1016 	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_DEFAULT);
1017 	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_HOST);
1018 	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_KEYBOARD);
1019 	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_NONE);
1020 	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_PASSWORD);
1021 	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_PUBLICKEY);
1022 #endif
1023 
1024 #if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
1025 	REGISTER_CURL_CONSTANT(CURLINFO_CERTINFO);
1026 	REGISTER_CURL_CONSTANT(CURLOPT_CERTINFO);
1027 	REGISTER_CURL_CONSTANT(CURLOPT_PASSWORD);
1028 	REGISTER_CURL_CONSTANT(CURLOPT_POSTREDIR);
1029 	REGISTER_CURL_CONSTANT(CURLOPT_PROXYPASSWORD);
1030 	REGISTER_CURL_CONSTANT(CURLOPT_PROXYUSERNAME);
1031 	REGISTER_CURL_CONSTANT(CURLOPT_USERNAME);
1032 	REGISTER_CURL_CONSTANT(CURL_REDIR_POST_301);
1033 	REGISTER_CURL_CONSTANT(CURL_REDIR_POST_302);
1034 	REGISTER_CURL_CONSTANT(CURL_REDIR_POST_ALL);
1035 #endif
1036 
1037 #if LIBCURL_VERSION_NUM >= 0x071303 /* Available since 7.19.3 */
1038 	REGISTER_CURL_CONSTANT(CURLAUTH_DIGEST_IE);
1039 #endif
1040 
1041 #if LIBCURL_VERSION_NUM >= 0x071304 /* Available since 7.19.4 */
1042 	REGISTER_CURL_CONSTANT(CURLINFO_CONDITION_UNMET);
1043 
1044 	REGISTER_CURL_CONSTANT(CURLOPT_NOPROXY);
1045 	REGISTER_CURL_CONSTANT(CURLOPT_PROTOCOLS);
1046 	REGISTER_CURL_CONSTANT(CURLOPT_REDIR_PROTOCOLS);
1047 	REGISTER_CURL_CONSTANT(CURLOPT_SOCKS5_GSSAPI_NEC);
1048 	REGISTER_CURL_CONSTANT(CURLOPT_SOCKS5_GSSAPI_SERVICE);
1049 	REGISTER_CURL_CONSTANT(CURLOPT_TFTP_BLKSIZE);
1050 
1051 	REGISTER_CURL_CONSTANT(CURLPROTO_ALL);
1052 	REGISTER_CURL_CONSTANT(CURLPROTO_DICT);
1053 	REGISTER_CURL_CONSTANT(CURLPROTO_FILE);
1054 	REGISTER_CURL_CONSTANT(CURLPROTO_FTP);
1055 	REGISTER_CURL_CONSTANT(CURLPROTO_FTPS);
1056 	REGISTER_CURL_CONSTANT(CURLPROTO_HTTP);
1057 	REGISTER_CURL_CONSTANT(CURLPROTO_HTTPS);
1058 	REGISTER_CURL_CONSTANT(CURLPROTO_LDAP);
1059 	REGISTER_CURL_CONSTANT(CURLPROTO_LDAPS);
1060 	REGISTER_CURL_CONSTANT(CURLPROTO_SCP);
1061 	REGISTER_CURL_CONSTANT(CURLPROTO_SFTP);
1062 	REGISTER_CURL_CONSTANT(CURLPROTO_TELNET);
1063 	REGISTER_CURL_CONSTANT(CURLPROTO_TFTP);
1064 
1065 	REGISTER_CURL_CONSTANT(CURLPROXY_HTTP_1_0);
1066 
1067 	REGISTER_CURL_CONSTANT(CURLFTP_CREATE_DIR);
1068 	REGISTER_CURL_CONSTANT(CURLFTP_CREATE_DIR_NONE);
1069 	REGISTER_CURL_CONSTANT(CURLFTP_CREATE_DIR_RETRY);
1070 #endif
1071 
1072 #if LIBCURL_VERSION_NUM >= 0x071306 /* Available since 7.19.6 */
1073 	REGISTER_CURL_CONSTANT(CURL_VERSION_CURLDEBUG);
1074 	REGISTER_CURL_CONSTANT(CURLOPT_SSH_KNOWNHOSTS);
1075 #endif
1076 
1077 #if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
1078 	REGISTER_CURL_CONSTANT(CURLINFO_RTSP_CLIENT_CSEQ);
1079 	REGISTER_CURL_CONSTANT(CURLINFO_RTSP_CSEQ_RECV);
1080 	REGISTER_CURL_CONSTANT(CURLINFO_RTSP_SERVER_CSEQ);
1081 	REGISTER_CURL_CONSTANT(CURLINFO_RTSP_SESSION_ID);
1082 	REGISTER_CURL_CONSTANT(CURLOPT_FTP_USE_PRET);
1083 	REGISTER_CURL_CONSTANT(CURLOPT_MAIL_FROM);
1084 	REGISTER_CURL_CONSTANT(CURLOPT_MAIL_RCPT);
1085 	REGISTER_CURL_CONSTANT(CURLOPT_RTSP_CLIENT_CSEQ);
1086 	REGISTER_CURL_CONSTANT(CURLOPT_RTSP_REQUEST);
1087 	REGISTER_CURL_CONSTANT(CURLOPT_RTSP_SERVER_CSEQ);
1088 	REGISTER_CURL_CONSTANT(CURLOPT_RTSP_SESSION_ID);
1089 	REGISTER_CURL_CONSTANT(CURLOPT_RTSP_STREAM_URI);
1090 	REGISTER_CURL_CONSTANT(CURLOPT_RTSP_TRANSPORT);
1091 	REGISTER_CURL_CONSTANT(CURLPROTO_IMAP);
1092 	REGISTER_CURL_CONSTANT(CURLPROTO_IMAPS);
1093 	REGISTER_CURL_CONSTANT(CURLPROTO_POP3);
1094 	REGISTER_CURL_CONSTANT(CURLPROTO_POP3S);
1095 	REGISTER_CURL_CONSTANT(CURLPROTO_RTSP);
1096 	REGISTER_CURL_CONSTANT(CURLPROTO_SMTP);
1097 	REGISTER_CURL_CONSTANT(CURLPROTO_SMTPS);
1098 	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_ANNOUNCE);
1099 	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_DESCRIBE);
1100 	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_GET_PARAMETER);
1101 	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_OPTIONS);
1102 	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_PAUSE);
1103 	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_PLAY);
1104 	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_RECEIVE);
1105 	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_RECORD);
1106 	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_SET_PARAMETER);
1107 	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_SETUP);
1108 	REGISTER_CURL_CONSTANT(CURL_RTSPREQ_TEARDOWN);
1109 #endif
1110 
1111 #if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
1112 	REGISTER_CURL_CONSTANT(CURLINFO_LOCAL_IP);
1113 	REGISTER_CURL_CONSTANT(CURLINFO_LOCAL_PORT);
1114 	REGISTER_CURL_CONSTANT(CURLINFO_PRIMARY_PORT);
1115 	REGISTER_CURL_CONSTANT(CURLOPT_FNMATCH_FUNCTION);
1116 	REGISTER_CURL_CONSTANT(CURLOPT_WILDCARDMATCH);
1117 	REGISTER_CURL_CONSTANT(CURLPROTO_RTMP);
1118 	REGISTER_CURL_CONSTANT(CURLPROTO_RTMPE);
1119 	REGISTER_CURL_CONSTANT(CURLPROTO_RTMPS);
1120 	REGISTER_CURL_CONSTANT(CURLPROTO_RTMPT);
1121 	REGISTER_CURL_CONSTANT(CURLPROTO_RTMPTE);
1122 	REGISTER_CURL_CONSTANT(CURLPROTO_RTMPTS);
1123 	REGISTER_CURL_CONSTANT(CURL_FNMATCHFUNC_FAIL);
1124 	REGISTER_CURL_CONSTANT(CURL_FNMATCHFUNC_MATCH);
1125 	REGISTER_CURL_CONSTANT(CURL_FNMATCHFUNC_NOMATCH);
1126 #endif
1127 
1128 #if LIBCURL_VERSION_NUM >= 0x071502 /* Available since 7.21.2 */
1129 	REGISTER_CURL_CONSTANT(CURLPROTO_GOPHER);
1130 #endif
1131 
1132 #if LIBCURL_VERSION_NUM >= 0x071503 /* Available since 7.21.3 */
1133 	REGISTER_CURL_CONSTANT(CURLAUTH_ONLY);
1134 	REGISTER_CURL_CONSTANT(CURLOPT_RESOLVE);
1135 #endif
1136 
1137 #if LIBCURL_VERSION_NUM >= 0x071504 /* Available since 7.21.4 */
1138 	REGISTER_CURL_CONSTANT(CURLOPT_TLSAUTH_PASSWORD);
1139 	REGISTER_CURL_CONSTANT(CURLOPT_TLSAUTH_TYPE);
1140 	REGISTER_CURL_CONSTANT(CURLOPT_TLSAUTH_USERNAME);
1141 	REGISTER_CURL_CONSTANT(CURL_TLSAUTH_SRP);
1142 	REGISTER_CURL_CONSTANT(CURL_VERSION_TLSAUTH_SRP);
1143 #endif
1144 
1145 #if LIBCURL_VERSION_NUM >= 0x071506 /* Available since 7.21.6 */
1146 	REGISTER_CURL_CONSTANT(CURLOPT_ACCEPT_ENCODING);
1147 	REGISTER_CURL_CONSTANT(CURLOPT_TRANSFER_ENCODING);
1148 #endif
1149 
1150 #if LIBCURL_VERSION_NUM >= 0x071600 /* Available since 7.22.0 */
1151 	REGISTER_CURL_CONSTANT(CURLAUTH_NTLM_WB);
1152 	REGISTER_CURL_CONSTANT(CURLGSSAPI_DELEGATION_FLAG);
1153 	REGISTER_CURL_CONSTANT(CURLGSSAPI_DELEGATION_POLICY_FLAG);
1154 	REGISTER_CURL_CONSTANT(CURLOPT_GSSAPI_DELEGATION);
1155 	REGISTER_CURL_CONSTANT(CURL_VERSION_NTLM_WB);
1156 #endif
1157 
1158 #if LIBCURL_VERSION_NUM >= 0x071800 /* Available since 7.24.0 */
1159 	REGISTER_CURL_CONSTANT(CURLOPT_ACCEPTTIMEOUT_MS);
1160 	REGISTER_CURL_CONSTANT(CURLOPT_DNS_SERVERS);
1161 #endif
1162 
1163 #if LIBCURL_VERSION_NUM >= 0x071900 /* Available since 7.25.0 */
1164 	REGISTER_CURL_CONSTANT(CURLOPT_MAIL_AUTH);
1165 	REGISTER_CURL_CONSTANT(CURLOPT_SSL_OPTIONS);
1166 	REGISTER_CURL_CONSTANT(CURLOPT_TCP_KEEPALIVE);
1167 	REGISTER_CURL_CONSTANT(CURLOPT_TCP_KEEPIDLE);
1168 	REGISTER_CURL_CONSTANT(CURLOPT_TCP_KEEPINTVL);
1169 	REGISTER_CURL_CONSTANT(CURLSSLOPT_ALLOW_BEAST);
1170 #endif
1171 
1172 #if LIBCURL_VERSION_NUM >= 0x071901 /* Available since 7.25.1 */
1173 	REGISTER_CURL_CONSTANT(CURL_REDIR_POST_303);
1174 #endif
1175 
1176 #if LIBCURL_VERSION_NUM >= 0x071c00 /* Available since 7.28.0 */
1177 	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_AGENT);
1178 #endif
1179 
1180 #if LIBCURL_VERSION_NUM >= 0x071e00 /* Available since 7.30.0 */
1181 	REGISTER_CURL_CONSTANT(CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE);
1182 	REGISTER_CURL_CONSTANT(CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE);
1183 	REGISTER_CURL_CONSTANT(CURLMOPT_MAX_HOST_CONNECTIONS);
1184 	REGISTER_CURL_CONSTANT(CURLMOPT_MAX_PIPELINE_LENGTH);
1185 	REGISTER_CURL_CONSTANT(CURLMOPT_MAX_TOTAL_CONNECTIONS);
1186 #endif
1187 
1188 #if LIBCURL_VERSION_NUM >= 0x071f00 /* Available since 7.31.0 */
1189 	REGISTER_CURL_CONSTANT(CURLOPT_SASL_IR);
1190 #endif
1191 
1192 #if LIBCURL_VERSION_NUM >= 0x072100 /* Available since 7.33.0 */
1193 	REGISTER_CURL_CONSTANT(CURLOPT_DNS_INTERFACE);
1194 	REGISTER_CURL_CONSTANT(CURLOPT_DNS_LOCAL_IP4);
1195 	REGISTER_CURL_CONSTANT(CURLOPT_DNS_LOCAL_IP6);
1196 	REGISTER_CURL_CONSTANT(CURLOPT_XOAUTH2_BEARER);
1197 
1198 	REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_2_0);
1199 	REGISTER_CURL_CONSTANT(CURL_VERSION_HTTP2);
1200 #endif
1201 
1202 #if LIBCURL_VERSION_NUM >= 0x072200 /* Available since 7.34.0 */
1203 	REGISTER_CURL_CONSTANT(CURLOPT_LOGIN_OPTIONS);
1204 
1205 	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_TLSv1_0);
1206 	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_TLSv1_1);
1207 	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_TLSv1_2);
1208 #endif
1209 
1210 #if LIBCURL_VERSION_NUM >= 0x072400 /* Available since 7.36.0 */
1211 	REGISTER_CURL_CONSTANT(CURLOPT_EXPECT_100_TIMEOUT_MS);
1212 	REGISTER_CURL_CONSTANT(CURLOPT_SSL_ENABLE_ALPN);
1213 	REGISTER_CURL_CONSTANT(CURLOPT_SSL_ENABLE_NPN);
1214 #endif
1215 
1216 #if LIBCURL_VERSION_NUM >= 0x072500 /* Available since 7.37.0 */
1217 	REGISTER_CURL_CONSTANT(CURLHEADER_SEPARATE);
1218 	REGISTER_CURL_CONSTANT(CURLHEADER_UNIFIED);
1219 	REGISTER_CURL_CONSTANT(CURLOPT_HEADEROPT);
1220 	REGISTER_CURL_CONSTANT(CURLOPT_PROXYHEADER);
1221 #endif
1222 
1223 #if LIBCURL_VERSION_NUM >= 0x072600 /* Available since 7.38.0 */
1224 	REGISTER_CURL_CONSTANT(CURLAUTH_NEGOTIATE);
1225 	REGISTER_CURL_CONSTANT(CURL_VERSION_GSSAPI);
1226 #endif
1227 
1228 #if LIBCURL_VERSION_NUM >= 0x072700 /* Available since 7.39.0 */
1229 	REGISTER_CURL_CONSTANT(CURLOPT_PINNEDPUBLICKEY);
1230 #endif
1231 
1232 #if LIBCURL_VERSION_NUM >= 0x072800 /* Available since 7.40.0 */
1233 	REGISTER_CURL_CONSTANT(CURLOPT_UNIX_SOCKET_PATH);
1234 	REGISTER_CURL_CONSTANT(CURLPROTO_SMB);
1235 	REGISTER_CURL_CONSTANT(CURLPROTO_SMBS);
1236 	REGISTER_CURL_CONSTANT(CURL_VERSION_KERBEROS5);
1237 	REGISTER_CURL_CONSTANT(CURL_VERSION_UNIX_SOCKETS);
1238 #endif
1239 
1240 #if LIBCURL_VERSION_NUM >= 0x072900 /* Available since 7.41.0 */
1241 	REGISTER_CURL_CONSTANT(CURLOPT_SSL_VERIFYSTATUS);
1242 #endif
1243 
1244 #if LIBCURL_VERSION_NUM >= 0x072a00 /* Available since 7.42.0 */
1245 	REGISTER_CURL_CONSTANT(CURLOPT_PATH_AS_IS);
1246 	REGISTER_CURL_CONSTANT(CURLOPT_SSL_FALSESTART);
1247 #endif
1248 
1249 #if LIBCURL_VERSION_NUM >= 0x072b00 /* Available since 7.43.0 */
1250 	REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_2);
1251 
1252 	REGISTER_CURL_CONSTANT(CURLOPT_PIPEWAIT);
1253 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_SERVICE_NAME);
1254 	REGISTER_CURL_CONSTANT(CURLOPT_SERVICE_NAME);
1255 
1256 	REGISTER_CURL_CONSTANT(CURLPIPE_NOTHING);
1257 	REGISTER_CURL_CONSTANT(CURLPIPE_HTTP1);
1258 	REGISTER_CURL_CONSTANT(CURLPIPE_MULTIPLEX);
1259 #endif
1260 
1261 #if LIBCURL_VERSION_NUM >= 0x072c00 /* Available since 7.44.0 */
1262 	REGISTER_CURL_CONSTANT(CURLSSLOPT_NO_REVOKE);
1263 #endif
1264 
1265 #if LIBCURL_VERSION_NUM >= 0x072d00 /* Available since 7.45.0 */
1266 	REGISTER_CURL_CONSTANT(CURLOPT_DEFAULT_PROTOCOL);
1267 #endif
1268 
1269 #if LIBCURL_VERSION_NUM >= 0x072e00 /* Available since 7.46.0 */
1270 	REGISTER_CURL_CONSTANT(CURLOPT_STREAM_WEIGHT);
1271 	REGISTER_CURL_CONSTANT(CURLMOPT_PUSHFUNCTION);
1272 	REGISTER_CURL_CONSTANT(CURL_PUSH_OK);
1273 	REGISTER_CURL_CONSTANT(CURL_PUSH_DENY);
1274 #endif
1275 
1276 #if LIBCURL_VERSION_NUM >= 0x072f00 /* Available since 7.47.0 */
1277 	REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_2TLS);
1278 	REGISTER_CURL_CONSTANT(CURL_VERSION_PSL);
1279 #endif
1280 
1281 #if LIBCURL_VERSION_NUM >= 0x073000 /* Available since 7.48.0 */
1282 	REGISTER_CURL_CONSTANT(CURLOPT_TFTP_NO_OPTIONS);
1283 #endif
1284 
1285 #if LIBCURL_VERSION_NUM >= 0x073100 /* Available since 7.49.0 */
1286 	REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE);
1287 	REGISTER_CURL_CONSTANT(CURLOPT_CONNECT_TO);
1288 	REGISTER_CURL_CONSTANT(CURLOPT_TCP_FASTOPEN);
1289 #endif
1290 
1291 #if LIBCURL_VERSION_NUM >= 0x073200 /* Available since 7.50.0 */
1292 	REGISTER_CURL_CONSTANT(CURLINFO_HTTP_VERSION);
1293 #endif
1294 
1295 #if LIBCURL_VERSION_NUM >= 0x073300 /* Available since 7.51.0 */
1296 	REGISTER_CURL_CONSTANT(CURLE_WEIRD_SERVER_REPLY);
1297 	REGISTER_CURL_CONSTANT(CURLOPT_KEEP_SENDING_ON_ERROR);
1298 #endif
1299 
1300 #if LIBCURL_VERSION_NUM >= 0x073400 /* Available since 7.52.0 */
1301 	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_TLSv1_3);
1302 	REGISTER_CURL_CONSTANT(CURL_VERSION_HTTPS_PROXY);
1303 	REGISTER_CURL_CONSTANT(CURLINFO_PROTOCOL);
1304 	REGISTER_CURL_CONSTANT(CURLINFO_PROXY_SSL_VERIFYRESULT);
1305 	REGISTER_CURL_CONSTANT(CURLINFO_SCHEME);
1306 	REGISTER_CURL_CONSTANT(CURLOPT_PRE_PROXY);
1307 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_CAINFO);
1308 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_CAPATH);
1309 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_CRLFILE);
1310 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_KEYPASSWD);
1311 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_PINNEDPUBLICKEY);
1312 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_SSL_CIPHER_LIST);
1313 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_SSL_OPTIONS);
1314 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_SSL_VERIFYHOST);
1315 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_SSL_VERIFYPEER);
1316 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_SSLCERT);
1317 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_SSLCERTTYPE);
1318 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_SSLKEY);
1319 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_SSLKEYTYPE);
1320 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_SSLVERSION);
1321 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_TLSAUTH_PASSWORD);
1322 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_TLSAUTH_TYPE);
1323 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_TLSAUTH_USERNAME);
1324 	REGISTER_CURL_CONSTANT(CURLPROXY_HTTPS);
1325 #endif
1326 
1327 #if LIBCURL_VERSION_NUM >= 0x073500 /* Available since 7.53.0 */
1328 	REGISTER_CURL_CONSTANT(CURL_MAX_READ_SIZE);
1329 	REGISTER_CURL_CONSTANT(CURLOPT_ABSTRACT_UNIX_SOCKET);
1330 #endif
1331 
1332 #if LIBCURL_VERSION_NUM >= 0x073600 /* Available since 7.54.0 */
1333 	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_MAX_DEFAULT);
1334 	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_MAX_NONE);
1335 	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_MAX_TLSv1_0);
1336 	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_MAX_TLSv1_1);
1337 	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_MAX_TLSv1_2);
1338 	REGISTER_CURL_CONSTANT(CURL_SSLVERSION_MAX_TLSv1_3);
1339 	REGISTER_CURL_CONSTANT(CURLOPT_SUPPRESS_CONNECT_HEADERS);
1340 #endif
1341 
1342 #if LIBCURL_VERSION_NUM >= 0x073601 /* Available since 7.54.1 */
1343 	REGISTER_CURL_CONSTANT(CURLAUTH_GSSAPI);
1344 #endif
1345 
1346 #if LIBCURL_VERSION_NUM >= 0x073700 /* Available since 7.55.0 */
1347 	REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_LENGTH_DOWNLOAD_T);
1348 	REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_LENGTH_UPLOAD_T);
1349 	REGISTER_CURL_CONSTANT(CURLINFO_SIZE_DOWNLOAD_T);
1350 	REGISTER_CURL_CONSTANT(CURLINFO_SIZE_UPLOAD_T);
1351 	REGISTER_CURL_CONSTANT(CURLINFO_SPEED_DOWNLOAD_T);
1352 	REGISTER_CURL_CONSTANT(CURLINFO_SPEED_UPLOAD_T);
1353 	REGISTER_CURL_CONSTANT(CURLOPT_REQUEST_TARGET);
1354 	REGISTER_CURL_CONSTANT(CURLOPT_SOCKS5_AUTH);
1355 #endif
1356 
1357 #if LIBCURL_VERSION_NUM >= 0x073800 /* Available since 7.56.0 */
1358 	REGISTER_CURL_CONSTANT(CURLOPT_SSH_COMPRESSION);
1359 	REGISTER_CURL_CONSTANT(CURL_VERSION_MULTI_SSL);
1360 #endif
1361 
1362 #if LIBCURL_VERSION_NUM >= 0x073900 /* Available since 7.57.0 */
1363 	REGISTER_CURL_CONSTANT(CURL_VERSION_BROTLI);
1364 	REGISTER_CURL_CONSTANT(CURL_LOCK_DATA_CONNECT);
1365 #endif
1366 
1367 #if LIBCURL_VERSION_NUM >= 0x073a00 /* Available since 7.58.0 */
1368 	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_GSSAPI);
1369 #endif
1370 
1371 #if LIBCURL_VERSION_NUM >= 0x073b00 /* Available since 7.59.0 */
1372 	REGISTER_CURL_CONSTANT(CURLINFO_FILETIME_T);
1373 	REGISTER_CURL_CONSTANT(CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS);
1374 	REGISTER_CURL_CONSTANT(CURLOPT_TIMEVALUE_LARGE);
1375 #endif
1376 
1377 #if LIBCURL_VERSION_NUM >= 0x073c00 /* Available since 7.60.0 */
1378 	REGISTER_CURL_CONSTANT(CURLOPT_DNS_SHUFFLE_ADDRESSES);
1379 	REGISTER_CURL_CONSTANT(CURLOPT_HAPROXYPROTOCOL);
1380 #endif
1381 
1382 #if LIBCURL_VERSION_NUM >= 0x073d00 /* Available since 7.61.0 */
1383 	REGISTER_CURL_CONSTANT(CURL_LOCK_DATA_PSL);
1384 	REGISTER_CURL_CONSTANT(CURLAUTH_BEARER);
1385 	REGISTER_CURL_CONSTANT(CURLINFO_APPCONNECT_TIME_T);
1386 	REGISTER_CURL_CONSTANT(CURLINFO_CONNECT_TIME_T);
1387 	REGISTER_CURL_CONSTANT(CURLINFO_NAMELOOKUP_TIME_T);
1388 	REGISTER_CURL_CONSTANT(CURLINFO_PRETRANSFER_TIME_T);
1389 	REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_TIME_T);
1390 	REGISTER_CURL_CONSTANT(CURLINFO_STARTTRANSFER_TIME_T);
1391 	REGISTER_CURL_CONSTANT(CURLINFO_TOTAL_TIME_T);
1392 	REGISTER_CURL_CONSTANT(CURLOPT_DISALLOW_USERNAME_IN_URL);
1393 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY_TLS13_CIPHERS);
1394 	REGISTER_CURL_CONSTANT(CURLOPT_TLS13_CIPHERS);
1395 #endif
1396 
1397 #if LIBCURL_VERSION_NUM >= 0x074000 /* Available since 7.64.0 */
1398 	REGISTER_CURL_CONSTANT(CURLOPT_HTTP09_ALLOWED);
1399 #endif
1400 
1401 #if LIBCURL_VERSION_NUM >= 0x074001 /* Available since 7.64.1 */
1402 	REGISTER_CURL_CONSTANT(CURL_VERSION_ALTSVC);
1403 #endif
1404 
1405 	REGISTER_CURL_CONSTANT(CURLOPT_SAFE_UPLOAD);
1406 
1407 #ifdef PHP_CURL_NEED_OPENSSL_TSL
1408 	if (!CRYPTO_get_id_callback()) {
1409 		int i, c = CRYPTO_num_locks();
1410 
1411 		php_curl_openssl_tsl = malloc(c * sizeof(MUTEX_T));
1412 		if (!php_curl_openssl_tsl) {
1413 			return FAILURE;
1414 		}
1415 
1416 		for (i = 0; i < c; ++i) {
1417 			php_curl_openssl_tsl[i] = tsrm_mutex_alloc();
1418 		}
1419 
1420 		CRYPTO_set_id_callback(php_curl_ssl_id);
1421 		CRYPTO_set_locking_callback(php_curl_ssl_lock);
1422 	}
1423 #endif
1424 
1425 	if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) {
1426 		return FAILURE;
1427 	}
1428 
1429 	curlfile_register_class();
1430 
1431 	return SUCCESS;
1432 }
1433 /* }}} */
1434 
1435 /* {{{ PHP_MSHUTDOWN_FUNCTION
1436  */
PHP_MSHUTDOWN_FUNCTION(curl)1437 PHP_MSHUTDOWN_FUNCTION(curl)
1438 {
1439 	curl_global_cleanup();
1440 #ifdef PHP_CURL_NEED_OPENSSL_TSL
1441 	if (php_curl_openssl_tsl) {
1442 		int i, c = CRYPTO_num_locks();
1443 
1444 		CRYPTO_set_id_callback(NULL);
1445 		CRYPTO_set_locking_callback(NULL);
1446 
1447 		for (i = 0; i < c; ++i) {
1448 			tsrm_mutex_free(php_curl_openssl_tsl[i]);
1449 		}
1450 
1451 		free(php_curl_openssl_tsl);
1452 		php_curl_openssl_tsl = NULL;
1453 	}
1454 #endif
1455 	UNREGISTER_INI_ENTRIES();
1456 	return SUCCESS;
1457 }
1458 /* }}} */
1459 
1460 /* {{{ curl_write_nothing
1461  * Used as a work around. See _php_curl_close_ex
1462  */
curl_write_nothing(char * data,size_t size,size_t nmemb,void * ctx)1463 static size_t curl_write_nothing(char *data, size_t size, size_t nmemb, void *ctx)
1464 {
1465 	return size * nmemb;
1466 }
1467 /* }}} */
1468 
1469 /* {{{ curl_write
1470  */
curl_write(char * data,size_t size,size_t nmemb,void * ctx)1471 static size_t curl_write(char *data, size_t size, size_t nmemb, void *ctx)
1472 {
1473 	php_curl *ch = (php_curl *) ctx;
1474 	php_curl_write *t = ch->handlers->write;
1475 	size_t length = size * nmemb;
1476 
1477 #if PHP_CURL_DEBUG
1478 	fprintf(stderr, "curl_write() called\n");
1479 	fprintf(stderr, "data = %s, size = %d, nmemb = %d, ctx = %x\n", data, size, nmemb, ctx);
1480 #endif
1481 
1482 	switch (t->method) {
1483 		case PHP_CURL_STDOUT:
1484 			PHPWRITE(data, length);
1485 			break;
1486 		case PHP_CURL_FILE:
1487 			return fwrite(data, size, nmemb, t->fp);
1488 		case PHP_CURL_RETURN:
1489 			if (length > 0) {
1490 				smart_str_appendl(&t->buf, data, (int) length);
1491 			}
1492 			break;
1493 		case PHP_CURL_USER: {
1494 			zval argv[2];
1495 			zval retval;
1496 			int  error;
1497 			zend_fcall_info fci;
1498 
1499 			GC_ADDREF(ch->res);
1500 			ZVAL_RES(&argv[0], ch->res);
1501 			ZVAL_STRINGL(&argv[1], data, length);
1502 
1503 			fci.size = sizeof(fci);
1504 			fci.object = NULL;
1505 			ZVAL_COPY_VALUE(&fci.function_name, &t->func_name);
1506 			fci.retval = &retval;
1507 			fci.param_count = 2;
1508 			fci.params = argv;
1509 			fci.no_separation = 0;
1510 
1511 			ch->in_callback = 1;
1512 			error = zend_call_function(&fci, &t->fci_cache);
1513 			ch->in_callback = 0;
1514 			if (error == FAILURE) {
1515 				php_error_docref(NULL, E_WARNING, "Could not call the CURLOPT_WRITEFUNCTION");
1516 				length = -1;
1517 			} else if (!Z_ISUNDEF(retval)) {
1518 				_php_curl_verify_handlers(ch, 1);
1519 				length = zval_get_long(&retval);
1520 			}
1521 
1522 			zval_ptr_dtor(&argv[0]);
1523 			zval_ptr_dtor(&argv[1]);
1524 			break;
1525 		}
1526 	}
1527 
1528 	return length;
1529 }
1530 /* }}} */
1531 
1532 #if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
1533 /* {{{ curl_fnmatch
1534  */
curl_fnmatch(void * ctx,const char * pattern,const char * string)1535 static int curl_fnmatch(void *ctx, const char *pattern, const char *string)
1536 {
1537 	php_curl *ch = (php_curl *) ctx;
1538 	php_curl_fnmatch *t = ch->handlers->fnmatch;
1539 	int rval = CURL_FNMATCHFUNC_FAIL;
1540 	switch (t->method) {
1541 		case PHP_CURL_USER: {
1542 			zval argv[3];
1543 			zval retval;
1544 			int  error;
1545 			zend_fcall_info fci;
1546 
1547 			GC_ADDREF(ch->res);
1548 			ZVAL_RES(&argv[0], ch->res);
1549 			ZVAL_STRING(&argv[1], pattern);
1550 			ZVAL_STRING(&argv[2], string);
1551 
1552 			fci.size = sizeof(fci);
1553 			ZVAL_COPY_VALUE(&fci.function_name, &t->func_name);
1554 			fci.object = NULL;
1555 			fci.retval = &retval;
1556 			fci.param_count = 3;
1557 			fci.params = argv;
1558 			fci.no_separation = 0;
1559 
1560 			ch->in_callback = 1;
1561 			error = zend_call_function(&fci, &t->fci_cache);
1562 			ch->in_callback = 0;
1563 			if (error == FAILURE) {
1564 				php_error_docref(NULL, E_WARNING, "Cannot call the CURLOPT_FNMATCH_FUNCTION");
1565 			} else if (!Z_ISUNDEF(retval)) {
1566 				_php_curl_verify_handlers(ch, 1);
1567 				rval = zval_get_long(&retval);
1568 			}
1569 			zval_ptr_dtor(&argv[0]);
1570 			zval_ptr_dtor(&argv[1]);
1571 			zval_ptr_dtor(&argv[2]);
1572 			break;
1573 		}
1574 	}
1575 	return rval;
1576 }
1577 /* }}} */
1578 #endif
1579 
1580 /* {{{ curl_progress
1581  */
curl_progress(void * clientp,double dltotal,double dlnow,double ultotal,double ulnow)1582 static size_t curl_progress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
1583 {
1584 	php_curl *ch = (php_curl *)clientp;
1585 	php_curl_progress *t = ch->handlers->progress;
1586 	size_t	rval = 0;
1587 
1588 #if PHP_CURL_DEBUG
1589 	fprintf(stderr, "curl_progress() called\n");
1590 	fprintf(stderr, "clientp = %x, dltotal = %f, dlnow = %f, ultotal = %f, ulnow = %f\n", clientp, dltotal, dlnow, ultotal, ulnow);
1591 #endif
1592 
1593 	switch (t->method) {
1594 		case PHP_CURL_USER: {
1595 			zval argv[5];
1596 			zval retval;
1597 			int  error;
1598 			zend_fcall_info fci;
1599 
1600 			GC_ADDREF(ch->res);
1601 			ZVAL_RES(&argv[0], ch->res);
1602 			ZVAL_LONG(&argv[1], (zend_long)dltotal);
1603 			ZVAL_LONG(&argv[2], (zend_long)dlnow);
1604 			ZVAL_LONG(&argv[3], (zend_long)ultotal);
1605 			ZVAL_LONG(&argv[4], (zend_long)ulnow);
1606 
1607 			fci.size = sizeof(fci);
1608 			ZVAL_COPY_VALUE(&fci.function_name, &t->func_name);
1609 			fci.object = NULL;
1610 			fci.retval = &retval;
1611 			fci.param_count = 5;
1612 			fci.params = argv;
1613 			fci.no_separation = 0;
1614 
1615 			ch->in_callback = 1;
1616 			error = zend_call_function(&fci, &t->fci_cache);
1617 			ch->in_callback = 0;
1618 			if (error == FAILURE) {
1619 				php_error_docref(NULL, E_WARNING, "Cannot call the CURLOPT_PROGRESSFUNCTION");
1620 			} else if (!Z_ISUNDEF(retval)) {
1621 				_php_curl_verify_handlers(ch, 1);
1622 				if (0 != zval_get_long(&retval)) {
1623 					rval = 1;
1624 				}
1625 			}
1626 			zval_ptr_dtor(&argv[0]);
1627 			break;
1628 		}
1629 	}
1630 	return rval;
1631 }
1632 /* }}} */
1633 
1634 /* {{{ curl_read
1635  */
curl_read(char * data,size_t size,size_t nmemb,void * ctx)1636 static size_t curl_read(char *data, size_t size, size_t nmemb, void *ctx)
1637 {
1638 	php_curl *ch = (php_curl *)ctx;
1639 	php_curl_read *t = ch->handlers->read;
1640 	int length = 0;
1641 
1642 	switch (t->method) {
1643 		case PHP_CURL_DIRECT:
1644 			if (t->fp) {
1645 				length = fread(data, size, nmemb, t->fp);
1646 			}
1647 			break;
1648 		case PHP_CURL_USER: {
1649 			zval argv[3];
1650 			zval retval;
1651 			int  error;
1652 			zend_fcall_info fci;
1653 
1654 			GC_ADDREF(ch->res);
1655 			ZVAL_RES(&argv[0], ch->res);
1656 			if (t->res) {
1657 				GC_ADDREF(t->res);
1658 				ZVAL_RES(&argv[1], t->res);
1659 			} else {
1660 				ZVAL_NULL(&argv[1]);
1661 			}
1662 			ZVAL_LONG(&argv[2], (int)size * nmemb);
1663 
1664 			fci.size = sizeof(fci);
1665 			ZVAL_COPY_VALUE(&fci.function_name, &t->func_name);
1666 			fci.object = NULL;
1667 			fci.retval = &retval;
1668 			fci.param_count = 3;
1669 			fci.params = argv;
1670 			fci.no_separation = 0;
1671 
1672 			ch->in_callback = 1;
1673 			error = zend_call_function(&fci, &t->fci_cache);
1674 			ch->in_callback = 0;
1675 			if (error == FAILURE) {
1676 				php_error_docref(NULL, E_WARNING, "Cannot call the CURLOPT_READFUNCTION");
1677 				length = CURL_READFUNC_ABORT;
1678 			} else if (!Z_ISUNDEF(retval)) {
1679 				_php_curl_verify_handlers(ch, 1);
1680 				if (Z_TYPE(retval) == IS_STRING) {
1681 					length = MIN((int) (size * nmemb), Z_STRLEN(retval));
1682 					memcpy(data, Z_STRVAL(retval), length);
1683 				}
1684 				zval_ptr_dtor(&retval);
1685 			}
1686 
1687 			zval_ptr_dtor(&argv[0]);
1688 			zval_ptr_dtor(&argv[1]);
1689 			break;
1690 		}
1691 	}
1692 
1693 	return length;
1694 }
1695 /* }}} */
1696 
1697 /* {{{ curl_write_header
1698  */
curl_write_header(char * data,size_t size,size_t nmemb,void * ctx)1699 static size_t curl_write_header(char *data, size_t size, size_t nmemb, void *ctx)
1700 {
1701 	php_curl *ch = (php_curl *) ctx;
1702 	php_curl_write *t = ch->handlers->write_header;
1703 	size_t length = size * nmemb;
1704 
1705 	switch (t->method) {
1706 		case PHP_CURL_STDOUT:
1707 			/* Handle special case write when we're returning the entire transfer
1708 			 */
1709 			if (ch->handlers->write->method == PHP_CURL_RETURN && length > 0) {
1710 				smart_str_appendl(&ch->handlers->write->buf, data, (int) length);
1711 			} else {
1712 				PHPWRITE(data, length);
1713 			}
1714 			break;
1715 		case PHP_CURL_FILE:
1716 			return fwrite(data, size, nmemb, t->fp);
1717 		case PHP_CURL_USER: {
1718 			zval argv[2];
1719 			zval retval;
1720 			int  error;
1721 			zend_fcall_info fci;
1722 
1723 			GC_ADDREF(ch->res);
1724 			ZVAL_RES(&argv[0], ch->res);
1725 			ZVAL_STRINGL(&argv[1], data, length);
1726 
1727 			fci.size = sizeof(fci);
1728 			ZVAL_COPY_VALUE(&fci.function_name, &t->func_name);
1729 			fci.object = NULL;
1730 			fci.retval = &retval;
1731 			fci.param_count = 2;
1732 			fci.params = argv;
1733 			fci.no_separation = 0;
1734 
1735 			ch->in_callback = 1;
1736 			error = zend_call_function(&fci, &t->fci_cache);
1737 			ch->in_callback = 0;
1738 			if (error == FAILURE) {
1739 				php_error_docref(NULL, E_WARNING, "Could not call the CURLOPT_HEADERFUNCTION");
1740 				length = -1;
1741 			} else if (!Z_ISUNDEF(retval)) {
1742 				_php_curl_verify_handlers(ch, 1);
1743 				length = zval_get_long(&retval);
1744 			}
1745 			zval_ptr_dtor(&argv[0]);
1746 			zval_ptr_dtor(&argv[1]);
1747 			break;
1748 		}
1749 
1750 		case PHP_CURL_IGNORE:
1751 			return length;
1752 
1753 		default:
1754 			return -1;
1755 	}
1756 
1757 	return length;
1758 }
1759 /* }}} */
1760 
curl_debug(CURL * cp,curl_infotype type,char * buf,size_t buf_len,void * ctx)1761 static int curl_debug(CURL *cp, curl_infotype type, char *buf, size_t buf_len, void *ctx) /* {{{ */
1762 {
1763 	php_curl *ch = (php_curl *)ctx;
1764 
1765 	if (type == CURLINFO_HEADER_OUT) {
1766 		if (ch->header.str) {
1767 			zend_string_release_ex(ch->header.str, 0);
1768 		}
1769 		ch->header.str = zend_string_init(buf, buf_len, 0);
1770 	}
1771 
1772 	return 0;
1773 }
1774 /* }}} */
1775 
1776 /* {{{ curl_free_string
1777  */
curl_free_string(void ** string)1778 static void curl_free_string(void **string)
1779 {
1780 	efree((char *)*string);
1781 }
1782 /* }}} */
1783 
1784 /* {{{ curl_free_post
1785  */
curl_free_post(void ** post)1786 static void curl_free_post(void **post)
1787 {
1788 #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
1789 	curl_mime_free((curl_mime *)*post);
1790 #else
1791 	curl_formfree((struct HttpPost *)*post);
1792 #endif
1793 }
1794 /* }}} */
1795 
1796 struct mime_data_cb_arg {
1797 	zend_string *filename;
1798 	php_stream *stream;
1799 };
1800 
1801 /* {{{ curl_free_cb_arg
1802  */
curl_free_cb_arg(void ** cb_arg_p)1803 static void curl_free_cb_arg(void **cb_arg_p)
1804 {
1805 	struct mime_data_cb_arg *cb_arg = (struct mime_data_cb_arg *) *cb_arg_p;
1806 
1807 	ZEND_ASSERT(cb_arg->stream == NULL);
1808 	zend_string_release(cb_arg->filename);
1809 	efree(cb_arg);
1810 }
1811 /* }}} */
1812 
1813 /* {{{ curl_free_slist
1814  */
curl_free_slist(zval * el)1815 static void curl_free_slist(zval *el)
1816 {
1817 	curl_slist_free_all(((struct curl_slist *)Z_PTR_P(el)));
1818 }
1819 /* }}} */
1820 
1821 /* {{{ proto array curl_version([int version])
1822    Return cURL version information. */
PHP_FUNCTION(curl_version)1823 PHP_FUNCTION(curl_version)
1824 {
1825 	curl_version_info_data *d;
1826 	zend_long uversion = -1;
1827 
1828 	ZEND_PARSE_PARAMETERS_START(0, 1)
1829 		Z_PARAM_OPTIONAL
1830 		Z_PARAM_LONG(uversion)
1831 	ZEND_PARSE_PARAMETERS_END();
1832 
1833 	if (uversion == CURLVERSION_NOW) {
1834 		php_error_docref(NULL, E_DEPRECATED, "the $version parameter is deprecated");
1835 	} else if (ZEND_NUM_ARGS() > 0) {
1836 		php_error_docref(NULL, E_WARNING, "$version argument ignored");
1837 	}
1838 
1839 	d = curl_version_info(CURLVERSION_NOW);
1840 	if (d == NULL) {
1841 		RETURN_FALSE;
1842 	}
1843 
1844 	array_init(return_value);
1845 
1846 	CAAL("version_number", d->version_num);
1847 	CAAL("age", d->age);
1848 	CAAL("features", d->features);
1849 	CAAL("ssl_version_number", d->ssl_version_num);
1850 	CAAS("version", d->version);
1851 	CAAS("host", d->host);
1852 	CAAS("ssl_version", d->ssl_version);
1853 	CAAS("libz_version", d->libz_version);
1854 	/* Add an array of protocols */
1855 	{
1856 		char **p = (char **) d->protocols;
1857 		zval protocol_list;
1858 
1859 		array_init(&protocol_list);
1860 
1861 		while (*p != NULL) {
1862 			add_next_index_string(&protocol_list, *p);
1863 			p++;
1864 		}
1865 		CAAZ("protocols", &protocol_list);
1866 	}
1867 	if (d->age >= 1) {
1868 		CAAS("ares", d->ares);
1869 		CAAL("ares_num", d->ares_num);
1870 	}
1871 	if (d->age >= 2) {
1872 		CAAS("libidn", d->libidn);
1873 	}
1874 #if LIBCURL_VERSION_NUM >= 0x071001 /* Available since 7.16.1 */
1875 	if (d->age >= 3) {
1876 		CAAL("iconv_ver_num", d->iconv_ver_num);
1877 		CAAS("libssh_version", d->libssh_version);
1878 	}
1879 #endif
1880 #if LIBCURL_VERSION_NUM >= 0x073900 /* Available since 7.57.0 */
1881 	if (d->age >= 4) {
1882 		CAAL("brotli_ver_num", d->brotli_ver_num);
1883 		CAAS("brotli_version", d->brotli_version);
1884 	}
1885 #endif
1886 }
1887 /* }}} */
1888 
1889 /* {{{ alloc_curl_handle
1890  */
alloc_curl_handle()1891 php_curl *alloc_curl_handle()
1892 {
1893 	php_curl *ch               = ecalloc(1, sizeof(php_curl));
1894 	ch->to_free                = ecalloc(1, sizeof(struct _php_curl_free));
1895 	ch->handlers               = ecalloc(1, sizeof(php_curl_handlers));
1896 	ch->handlers->write        = ecalloc(1, sizeof(php_curl_write));
1897 	ch->handlers->write_header = ecalloc(1, sizeof(php_curl_write));
1898 	ch->handlers->read         = ecalloc(1, sizeof(php_curl_read));
1899 	ch->handlers->progress     = NULL;
1900 #if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
1901 	ch->handlers->fnmatch      = NULL;
1902 #endif
1903 	ch->clone 				   = emalloc(sizeof(uint32_t));
1904 	*ch->clone                 = 1;
1905 
1906 	memset(&ch->err, 0, sizeof(struct _php_curl_error));
1907 
1908 	zend_llist_init(&ch->to_free->str,   sizeof(char *),          (llist_dtor_func_t)curl_free_string, 0);
1909 	zend_llist_init(&ch->to_free->post,  sizeof(struct HttpPost *), (llist_dtor_func_t)curl_free_post,   0);
1910 	zend_llist_init(&ch->to_free->stream, sizeof(struct mime_data_cb_arg *), (llist_dtor_func_t)curl_free_cb_arg, 0);
1911 
1912 	ch->to_free->slist = emalloc(sizeof(HashTable));
1913 	zend_hash_init(ch->to_free->slist, 4, NULL, curl_free_slist, 0);
1914 #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
1915 	ZVAL_UNDEF(&ch->postfields);
1916 #endif
1917 	return ch;
1918 }
1919 /* }}} */
1920 
1921 #if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
1922 /* {{{ create_certinfo
1923  */
create_certinfo(struct curl_certinfo * ci,zval * listcode)1924 static void create_certinfo(struct curl_certinfo *ci, zval *listcode)
1925 {
1926 	int i;
1927 
1928 	if (ci) {
1929 		zval certhash;
1930 
1931 		for (i=0; i<ci->num_of_certs; i++) {
1932 			struct curl_slist *slist;
1933 
1934 			array_init(&certhash);
1935 			for (slist = ci->certinfo[i]; slist; slist = slist->next) {
1936 				int len;
1937 				char s[64];
1938 				char *tmp;
1939 				strncpy(s, slist->data, sizeof(s));
1940 				s[sizeof(s)-1] = '\0';
1941 				tmp = memchr(s, ':', sizeof(s));
1942 				if(tmp) {
1943 					*tmp = '\0';
1944 					len = strlen(s);
1945 					add_assoc_string(&certhash, s, &slist->data[len+1]);
1946 				} else {
1947 					php_error_docref(NULL, E_WARNING, "Could not extract hash key from certificate info");
1948 				}
1949 			}
1950 			add_next_index_zval(listcode, &certhash);
1951 		}
1952 	}
1953 }
1954 /* }}} */
1955 #endif
1956 
1957 /* {{{ _php_curl_set_default_options()
1958    Set default options for a handle */
_php_curl_set_default_options(php_curl * ch)1959 static void _php_curl_set_default_options(php_curl *ch)
1960 {
1961 	char *cainfo;
1962 
1963 	curl_easy_setopt(ch->cp, CURLOPT_NOPROGRESS,        1);
1964 	curl_easy_setopt(ch->cp, CURLOPT_VERBOSE,           0);
1965 	curl_easy_setopt(ch->cp, CURLOPT_ERRORBUFFER,       ch->err.str);
1966 	curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION,     curl_write);
1967 	curl_easy_setopt(ch->cp, CURLOPT_FILE,              (void *) ch);
1968 	curl_easy_setopt(ch->cp, CURLOPT_READFUNCTION,      curl_read);
1969 	curl_easy_setopt(ch->cp, CURLOPT_INFILE,            (void *) ch);
1970 	curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION,    curl_write_header);
1971 	curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER,       (void *) ch);
1972 #if !defined(ZTS)
1973 	curl_easy_setopt(ch->cp, CURLOPT_DNS_USE_GLOBAL_CACHE, 1);
1974 #endif
1975 	curl_easy_setopt(ch->cp, CURLOPT_DNS_CACHE_TIMEOUT, 120);
1976 	curl_easy_setopt(ch->cp, CURLOPT_MAXREDIRS, 20); /* prevent infinite redirects */
1977 
1978 	cainfo = INI_STR("openssl.cafile");
1979 	if (!(cainfo && cainfo[0] != '\0')) {
1980 		cainfo = INI_STR("curl.cainfo");
1981 	}
1982 	if (cainfo && cainfo[0] != '\0') {
1983 		curl_easy_setopt(ch->cp, CURLOPT_CAINFO, cainfo);
1984 	}
1985 
1986 #if defined(ZTS)
1987 	curl_easy_setopt(ch->cp, CURLOPT_NOSIGNAL, 1);
1988 #endif
1989 }
1990 /* }}} */
1991 
1992 /* {{{ proto resource curl_init([string url])
1993    Initialize a cURL session */
PHP_FUNCTION(curl_init)1994 PHP_FUNCTION(curl_init)
1995 {
1996 	php_curl *ch;
1997 	CURL 	 *cp;
1998 	zend_string *url = NULL;
1999 
2000 	ZEND_PARSE_PARAMETERS_START(0,1)
2001 		Z_PARAM_OPTIONAL
2002 		Z_PARAM_STR(url)
2003 	ZEND_PARSE_PARAMETERS_END();
2004 
2005 	cp = curl_easy_init();
2006 	if (!cp) {
2007 		php_error_docref(NULL, E_WARNING, "Could not initialize a new cURL handle");
2008 		RETURN_FALSE;
2009 	}
2010 
2011 	ch = alloc_curl_handle();
2012 
2013 	ch->cp = cp;
2014 
2015 	ch->handlers->write->method = PHP_CURL_STDOUT;
2016 	ch->handlers->read->method  = PHP_CURL_DIRECT;
2017 	ch->handlers->write_header->method = PHP_CURL_IGNORE;
2018 
2019 	_php_curl_set_default_options(ch);
2020 
2021 	if (url) {
2022 		if (php_curl_option_url(ch, ZSTR_VAL(url), ZSTR_LEN(url)) == FAILURE) {
2023 			_php_curl_close_ex(ch);
2024 			RETURN_FALSE;
2025 		}
2026 	}
2027 
2028 	ZVAL_RES(return_value, zend_register_resource(ch, le_curl));
2029 	ch->res = Z_RES_P(return_value);
2030 }
2031 /* }}} */
2032 
_php_setup_easy_copy_handlers(php_curl * ch,php_curl * source)2033 void _php_setup_easy_copy_handlers(php_curl *ch, php_curl *source)
2034 {
2035 	if (!Z_ISUNDEF(source->handlers->write->stream)) {
2036 		Z_ADDREF(source->handlers->write->stream);
2037 	}
2038 	ch->handlers->write->stream = source->handlers->write->stream;
2039 	ch->handlers->write->method = source->handlers->write->method;
2040 	if (!Z_ISUNDEF(source->handlers->read->stream)) {
2041 		Z_ADDREF(source->handlers->read->stream);
2042 	}
2043 	ch->handlers->read->stream  = source->handlers->read->stream;
2044 	ch->handlers->read->method  = source->handlers->read->method;
2045 	ch->handlers->write_header->method = source->handlers->write_header->method;
2046 	if (!Z_ISUNDEF(source->handlers->write_header->stream)) {
2047 		Z_ADDREF(source->handlers->write_header->stream);
2048 	}
2049 	ch->handlers->write_header->stream = source->handlers->write_header->stream;
2050 
2051 	ch->handlers->write->fp = source->handlers->write->fp;
2052 	ch->handlers->write_header->fp = source->handlers->write_header->fp;
2053 	ch->handlers->read->fp = source->handlers->read->fp;
2054 	ch->handlers->read->res = source->handlers->read->res;
2055 #if CURLOPT_PASSWDDATA != 0
2056 	if (!Z_ISUNDEF(source->handlers->passwd)) {
2057 		ZVAL_COPY(&ch->handlers->passwd, &source->handlers->passwd);
2058 		curl_easy_setopt(source->cp, CURLOPT_PASSWDDATA, (void *) ch);
2059 	}
2060 #endif
2061 	if (!Z_ISUNDEF(source->handlers->write->func_name)) {
2062 		ZVAL_COPY(&ch->handlers->write->func_name, &source->handlers->write->func_name);
2063 	}
2064 	if (!Z_ISUNDEF(source->handlers->read->func_name)) {
2065 		ZVAL_COPY(&ch->handlers->read->func_name, &source->handlers->read->func_name);
2066 	}
2067 	if (!Z_ISUNDEF(source->handlers->write_header->func_name)) {
2068 		ZVAL_COPY(&ch->handlers->write_header->func_name, &source->handlers->write_header->func_name);
2069 	}
2070 
2071 	curl_easy_setopt(ch->cp, CURLOPT_ERRORBUFFER,       ch->err.str);
2072 	curl_easy_setopt(ch->cp, CURLOPT_FILE,              (void *) ch);
2073 	curl_easy_setopt(ch->cp, CURLOPT_INFILE,            (void *) ch);
2074 	curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER,       (void *) ch);
2075 	curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA,         (void *) ch);
2076 
2077 	if (source->handlers->progress) {
2078 		ch->handlers->progress = ecalloc(1, sizeof(php_curl_progress));
2079 		if (!Z_ISUNDEF(source->handlers->progress->func_name)) {
2080 			ZVAL_COPY(&ch->handlers->progress->func_name, &source->handlers->progress->func_name);
2081 		}
2082 		ch->handlers->progress->method = source->handlers->progress->method;
2083 		curl_easy_setopt(ch->cp, CURLOPT_PROGRESSDATA, (void *) ch);
2084 	}
2085 
2086 #if LIBCURL_VERSION_NUM >= 0x071500
2087 	if (source->handlers->fnmatch) {
2088 		ch->handlers->fnmatch = ecalloc(1, sizeof(php_curl_fnmatch));
2089 		if (!Z_ISUNDEF(source->handlers->fnmatch->func_name)) {
2090 			ZVAL_COPY(&ch->handlers->fnmatch->func_name, &source->handlers->fnmatch->func_name);
2091 		}
2092 		ch->handlers->fnmatch->method = source->handlers->fnmatch->method;
2093 		curl_easy_setopt(ch->cp, CURLOPT_FNMATCH_DATA, (void *) ch);
2094 	}
2095 #endif
2096 
2097 	efree(ch->to_free->slist);
2098 	efree(ch->to_free);
2099 	ch->to_free = source->to_free;
2100 	efree(ch->clone);
2101 	ch->clone = source->clone;
2102 
2103 	/* Keep track of cloned copies to avoid invoking curl destructors for every clone */
2104 	(*source->clone)++;
2105 }
2106 
2107 #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
read_cb(char * buffer,size_t size,size_t nitems,void * arg)2108 static size_t read_cb(char *buffer, size_t size, size_t nitems, void *arg) /* {{{ */
2109 {
2110 	struct mime_data_cb_arg *cb_arg = (struct mime_data_cb_arg *) arg;
2111 	ssize_t numread;
2112 
2113 	if (cb_arg->stream == NULL) {
2114 		if (!(cb_arg->stream = php_stream_open_wrapper(ZSTR_VAL(cb_arg->filename), "rb", IGNORE_PATH, NULL))) {
2115 			return CURL_READFUNC_ABORT;
2116 		}
2117 	}
2118 	numread = php_stream_read(cb_arg->stream, buffer, nitems * size);
2119 	if (numread < 0) {
2120 		php_stream_close(cb_arg->stream);
2121 		cb_arg->stream = NULL;
2122 		return CURL_READFUNC_ABORT;
2123 	}
2124 	return numread;
2125 }
2126 /* }}} */
2127 
seek_cb(void * arg,curl_off_t offset,int origin)2128 static int seek_cb(void *arg, curl_off_t offset, int origin) /* {{{ */
2129 {
2130 	struct mime_data_cb_arg *cb_arg = (struct mime_data_cb_arg *) arg;
2131 	int res;
2132 
2133 	if (cb_arg->stream == NULL) {
2134 		return CURL_SEEKFUNC_CANTSEEK;
2135 	}
2136 	res = php_stream_seek(cb_arg->stream, offset, origin);
2137 	return res == SUCCESS ? CURL_SEEKFUNC_OK : CURL_SEEKFUNC_CANTSEEK;
2138 }
2139 /* }}} */
2140 
free_cb(void * arg)2141 static void free_cb(void *arg) /* {{{ */
2142 {
2143 	struct mime_data_cb_arg *cb_arg = (struct mime_data_cb_arg *) arg;
2144 
2145 	if (cb_arg->stream != NULL) {
2146 		php_stream_close(cb_arg->stream);
2147 		cb_arg->stream = NULL;
2148 	}
2149 }
2150 /* }}} */
2151 #endif
2152 
build_mime_structure_from_hash(php_curl * ch,zval * zpostfields)2153 static inline int build_mime_structure_from_hash(php_curl *ch, zval *zpostfields) /* {{{ */
2154 {
2155 	CURLcode error = CURLE_OK;
2156 	zval *current;
2157 	HashTable *postfields;
2158 	zend_string *string_key;
2159 	zend_ulong num_key;
2160 #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2161 	curl_mime *mime = NULL;
2162 	curl_mimepart *part;
2163 	CURLcode form_error;
2164 #else
2165 	struct HttpPost *first = NULL;
2166 	struct HttpPost *last  = NULL;
2167 	CURLFORMcode form_error;
2168 #endif
2169 
2170 	postfields = HASH_OF(zpostfields);
2171 	if (!postfields) {
2172 		php_error_docref(NULL, E_WARNING, "Couldn't get HashTable in CURLOPT_POSTFIELDS");
2173 		return FAILURE;
2174 	}
2175 
2176 #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2177 	if (zend_hash_num_elements(postfields) > 0) {
2178 		mime = curl_mime_init(ch->cp);
2179 		if (mime == NULL) {
2180 			return FAILURE;
2181 		}
2182 	}
2183 #endif
2184 
2185 	ZEND_HASH_FOREACH_KEY_VAL_IND(postfields, num_key, string_key, current) {
2186 		zend_string *postval, *tmp_postval;
2187 		/* Pretend we have a string_key here */
2188 		if (!string_key) {
2189 			string_key = zend_long_to_str(num_key);
2190 		} else {
2191 			zend_string_addref(string_key);
2192 		}
2193 
2194 		ZVAL_DEREF(current);
2195 		if (Z_TYPE_P(current) == IS_OBJECT &&
2196 				instanceof_function(Z_OBJCE_P(current), curl_CURLFile_class)) {
2197 			/* new-style file upload */
2198 			zval *prop, rv;
2199 			char *type = NULL, *filename = NULL;
2200 #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2201 			struct mime_data_cb_arg *cb_arg;
2202 			php_stream *stream;
2203 			php_stream_statbuf ssb;
2204 			size_t filesize = -1;
2205 			curl_seek_callback seekfunc = seek_cb;
2206 #endif
2207 
2208 			prop = zend_read_property(curl_CURLFile_class, current, "name", sizeof("name")-1, 0, &rv);
2209 			ZVAL_DEREF(prop);
2210 			if (Z_TYPE_P(prop) != IS_STRING) {
2211 				php_error_docref(NULL, E_WARNING, "Invalid filename for key %s", ZSTR_VAL(string_key));
2212 			} else {
2213 				postval = Z_STR_P(prop);
2214 
2215 				if (php_check_open_basedir(ZSTR_VAL(postval))) {
2216 					return 1;
2217 				}
2218 
2219 				prop = zend_read_property(curl_CURLFile_class, current, "mime", sizeof("mime")-1, 0, &rv);
2220 				ZVAL_DEREF(prop);
2221 				if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
2222 					type = Z_STRVAL_P(prop);
2223 				}
2224 				prop = zend_read_property(curl_CURLFile_class, current, "postname", sizeof("postname")-1, 0, &rv);
2225 				ZVAL_DEREF(prop);
2226 				if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
2227 					filename = Z_STRVAL_P(prop);
2228 				}
2229 
2230 #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2231 				zval_ptr_dtor(&ch->postfields);
2232 				ZVAL_COPY(&ch->postfields, zpostfields);
2233 
2234 				if ((stream = php_stream_open_wrapper(ZSTR_VAL(postval), "rb", STREAM_MUST_SEEK, NULL))) {
2235 					if (!stream->readfilters.head && !php_stream_stat(stream, &ssb)) {
2236 						filesize = ssb.sb.st_size;
2237 					}
2238 				} else {
2239 					seekfunc = NULL;
2240 				}
2241 
2242 				cb_arg = emalloc(sizeof *cb_arg);
2243 				cb_arg->filename = zend_string_copy(postval);
2244 				cb_arg->stream = stream;
2245 
2246 				part = curl_mime_addpart(mime);
2247 				if (part == NULL) {
2248 					zend_string_release_ex(string_key, 0);
2249 					return FAILURE;
2250 				}
2251 				if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
2252 					|| (form_error = curl_mime_data_cb(part, filesize, read_cb, seekfunc, free_cb, cb_arg)) != CURLE_OK
2253 					|| (form_error = curl_mime_filename(part, filename ? filename : ZSTR_VAL(postval))) != CURLE_OK
2254 					|| (form_error = curl_mime_type(part, type ? type : "application/octet-stream")) != CURLE_OK) {
2255 					error = form_error;
2256 				}
2257 				zend_llist_add_element(&ch->to_free->stream, &cb_arg);
2258 #else
2259 				form_error = curl_formadd(&first, &last,
2260 								CURLFORM_COPYNAME, ZSTR_VAL(string_key),
2261 								CURLFORM_NAMELENGTH, ZSTR_LEN(string_key),
2262 								CURLFORM_FILENAME, filename ? filename : ZSTR_VAL(postval),
2263 								CURLFORM_CONTENTTYPE, type ? type : "application/octet-stream",
2264 								CURLFORM_FILE, ZSTR_VAL(postval),
2265 								CURLFORM_END);
2266 				if (form_error != CURL_FORMADD_OK) {
2267 					/* Not nice to convert between enums but we only have place for one error type */
2268 					error = (CURLcode)form_error;
2269 				}
2270 #endif
2271 			}
2272 
2273 			zend_string_release_ex(string_key, 0);
2274 			continue;
2275 		}
2276 
2277 		postval = zval_get_tmp_string(current, &tmp_postval);
2278 
2279 #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2280 		part = curl_mime_addpart(mime);
2281 		if (part == NULL) {
2282 			zend_tmp_string_release(tmp_postval);
2283 			zend_string_release_ex(string_key, 0);
2284 			return FAILURE;
2285 		}
2286 		if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
2287 			|| (form_error = curl_mime_data(part, ZSTR_VAL(postval), ZSTR_LEN(postval))) != CURLE_OK) {
2288 			error = form_error;
2289 		}
2290 #else
2291 		/* The arguments after _NAMELENGTH and _CONTENTSLENGTH
2292 			* must be explicitly cast to long in curl_formadd
2293 			* use since curl needs a long not an int. */
2294 		form_error = curl_formadd(&first, &last,
2295 								CURLFORM_COPYNAME, ZSTR_VAL(string_key),
2296 								CURLFORM_NAMELENGTH, ZSTR_LEN(string_key),
2297 								CURLFORM_COPYCONTENTS, ZSTR_VAL(postval),
2298 								CURLFORM_CONTENTSLENGTH, ZSTR_LEN(postval),
2299 								CURLFORM_END);
2300 
2301 		if (form_error != CURL_FORMADD_OK) {
2302 			/* Not nice to convert between enums but we only have place for one error type */
2303 			error = (CURLcode)form_error;
2304 		}
2305 #endif
2306 		zend_tmp_string_release(tmp_postval);
2307 		zend_string_release_ex(string_key, 0);
2308 	} ZEND_HASH_FOREACH_END();
2309 
2310 	SAVE_CURL_ERROR(ch, error);
2311 	if (error != CURLE_OK) {
2312 		return FAILURE;
2313 	}
2314 
2315 	if ((*ch->clone) == 1) {
2316 		zend_llist_clean(&ch->to_free->post);
2317 	}
2318 #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2319 	zend_llist_add_element(&ch->to_free->post, &mime);
2320 	error = curl_easy_setopt(ch->cp, CURLOPT_MIMEPOST, mime);
2321 #else
2322 	zend_llist_add_element(&ch->to_free->post, &first);
2323 	error = curl_easy_setopt(ch->cp, CURLOPT_HTTPPOST, first);
2324 #endif
2325 
2326 	SAVE_CURL_ERROR(ch, error);
2327 	return error == CURLE_OK ? SUCCESS : FAILURE;
2328 }
2329 /* }}} */
2330 
2331 /* {{{ proto resource curl_copy_handle(resource ch)
2332    Copy a cURL handle along with all of it's preferences */
PHP_FUNCTION(curl_copy_handle)2333 PHP_FUNCTION(curl_copy_handle)
2334 {
2335 	CURL		*cp;
2336 	zval		*zid;
2337 	php_curl	*ch, *dupch;
2338 #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2339 	zval		*postfields;
2340 #endif
2341 
2342 	ZEND_PARSE_PARAMETERS_START(1,1)
2343 		Z_PARAM_RESOURCE(zid)
2344 	ZEND_PARSE_PARAMETERS_END();
2345 
2346 	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
2347 		RETURN_FALSE;
2348 	}
2349 
2350 	cp = curl_easy_duphandle(ch->cp);
2351 	if (!cp) {
2352 		php_error_docref(NULL, E_WARNING, "Cannot duplicate cURL handle");
2353 		RETURN_FALSE;
2354 	}
2355 
2356 	dupch = alloc_curl_handle();
2357 	dupch->cp = cp;
2358 
2359 	_php_setup_easy_copy_handlers(dupch, ch);
2360 
2361 #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2362 	postfields = &ch->postfields;
2363 	if (Z_TYPE_P(postfields) != IS_UNDEF) {
2364 		if (build_mime_structure_from_hash(dupch, postfields) != SUCCESS) {
2365 			_php_curl_close_ex(dupch);
2366 			php_error_docref(NULL, E_WARNING, "Cannot rebuild mime structure");
2367 			RETURN_FALSE;
2368 		}
2369 	}
2370 #endif
2371 
2372 	ZVAL_RES(return_value, zend_register_resource(dupch, le_curl));
2373 	dupch->res = Z_RES_P(return_value);
2374 }
2375 /* }}} */
2376 
_php_curl_setopt(php_curl * ch,zend_long option,zval * zvalue)2377 static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{ */
2378 {
2379 	CURLcode error = CURLE_OK;
2380 	zend_long lval;
2381 
2382 	switch (option) {
2383 		/* Long options */
2384 		case CURLOPT_SSL_VERIFYHOST:
2385 			lval = zval_get_long(zvalue);
2386 			if (lval == 1) {
2387 #if LIBCURL_VERSION_NUM <= 0x071c00 /* 7.28.0 */
2388 				php_error_docref(NULL, E_NOTICE, "CURLOPT_SSL_VERIFYHOST with value 1 is deprecated and will be removed as of libcurl 7.28.1. It is recommended to use value 2 instead");
2389 #else
2390 				php_error_docref(NULL, E_NOTICE, "CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead");
2391 				error = curl_easy_setopt(ch->cp, option, 2);
2392 				break;
2393 #endif
2394 			}
2395 		case CURLOPT_AUTOREFERER:
2396 		case CURLOPT_BUFFERSIZE:
2397 		case CURLOPT_CONNECTTIMEOUT:
2398 		case CURLOPT_COOKIESESSION:
2399 		case CURLOPT_CRLF:
2400 		case CURLOPT_DNS_CACHE_TIMEOUT:
2401 		case CURLOPT_DNS_USE_GLOBAL_CACHE:
2402 		case CURLOPT_FAILONERROR:
2403 		case CURLOPT_FILETIME:
2404 		case CURLOPT_FORBID_REUSE:
2405 		case CURLOPT_FRESH_CONNECT:
2406 		case CURLOPT_FTP_USE_EPRT:
2407 		case CURLOPT_FTP_USE_EPSV:
2408 		case CURLOPT_HEADER:
2409 		case CURLOPT_HTTPGET:
2410 		case CURLOPT_HTTPPROXYTUNNEL:
2411 		case CURLOPT_HTTP_VERSION:
2412 		case CURLOPT_INFILESIZE:
2413 		case CURLOPT_LOW_SPEED_LIMIT:
2414 		case CURLOPT_LOW_SPEED_TIME:
2415 		case CURLOPT_MAXCONNECTS:
2416 		case CURLOPT_MAXREDIRS:
2417 		case CURLOPT_NETRC:
2418 		case CURLOPT_NOBODY:
2419 		case CURLOPT_NOPROGRESS:
2420 		case CURLOPT_NOSIGNAL:
2421 		case CURLOPT_PORT:
2422 		case CURLOPT_POST:
2423 		case CURLOPT_PROXYPORT:
2424 		case CURLOPT_PROXYTYPE:
2425 		case CURLOPT_PUT:
2426 		case CURLOPT_RESUME_FROM:
2427 		case CURLOPT_SSLVERSION:
2428 		case CURLOPT_SSL_VERIFYPEER:
2429 		case CURLOPT_TIMECONDITION:
2430 		case CURLOPT_TIMEOUT:
2431 		case CURLOPT_TIMEVALUE:
2432 		case CURLOPT_TRANSFERTEXT:
2433 		case CURLOPT_UNRESTRICTED_AUTH:
2434 		case CURLOPT_UPLOAD:
2435 		case CURLOPT_VERBOSE:
2436 		case CURLOPT_HTTPAUTH:
2437 		case CURLOPT_FTP_CREATE_MISSING_DIRS:
2438 		case CURLOPT_PROXYAUTH:
2439 		case CURLOPT_FTP_RESPONSE_TIMEOUT:
2440 		case CURLOPT_IPRESOLVE:
2441 		case CURLOPT_MAXFILESIZE:
2442 		case CURLOPT_TCP_NODELAY:
2443 		case CURLOPT_FTPSSLAUTH:
2444 		case CURLOPT_IGNORE_CONTENT_LENGTH:
2445 		case CURLOPT_FTP_SKIP_PASV_IP:
2446 		case CURLOPT_FTP_FILEMETHOD:
2447 		case CURLOPT_CONNECT_ONLY:
2448 		case CURLOPT_LOCALPORT:
2449 		case CURLOPT_LOCALPORTRANGE:
2450 #if LIBCURL_VERSION_NUM >= 0x071000 /* Available since 7.16.0 */
2451 		case CURLOPT_SSL_SESSIONID_CACHE:
2452 #endif
2453 #if LIBCURL_VERSION_NUM >= 0x071001 /* Available since 7.16.1 */
2454 		case CURLOPT_FTP_SSL_CCC:
2455 		case CURLOPT_SSH_AUTH_TYPES:
2456 #endif
2457 #if LIBCURL_VERSION_NUM >= 0x071002 /* Available since 7.16.2 */
2458 		case CURLOPT_CONNECTTIMEOUT_MS:
2459 		case CURLOPT_HTTP_CONTENT_DECODING:
2460 		case CURLOPT_HTTP_TRANSFER_DECODING:
2461 		case CURLOPT_TIMEOUT_MS:
2462 #endif
2463 #if LIBCURL_VERSION_NUM >= 0x071004 /* Available since 7.16.4 */
2464 		case CURLOPT_NEW_DIRECTORY_PERMS:
2465 		case CURLOPT_NEW_FILE_PERMS:
2466 #endif
2467 #if LIBCURL_VERSION_NUM >= 0x071100 /* Available since 7.17.0 */
2468 		case CURLOPT_USE_SSL:
2469 		case CURLOPT_APPEND:
2470 		case CURLOPT_DIRLISTONLY:
2471 #else
2472 		case CURLOPT_FTP_SSL:
2473 		case CURLOPT_FTPAPPEND:
2474 		case CURLOPT_FTPLISTONLY:
2475 #endif
2476 #if LIBCURL_VERSION_NUM >= 0x071200 /* Available since 7.18.0 */
2477 		case CURLOPT_PROXY_TRANSFER_MODE:
2478 #endif
2479 #if LIBCURL_VERSION_NUM >= 0x071300 /* Available since 7.19.0 */
2480 		case CURLOPT_ADDRESS_SCOPE:
2481 #endif
2482 #if LIBCURL_VERSION_NUM >  0x071301 /* Available since 7.19.1 */
2483 		case CURLOPT_CERTINFO:
2484 #endif
2485 #if LIBCURL_VERSION_NUM >= 0x071304 /* Available since 7.19.4 */
2486 		case CURLOPT_PROTOCOLS:
2487 		case CURLOPT_REDIR_PROTOCOLS:
2488 		case CURLOPT_SOCKS5_GSSAPI_NEC:
2489 		case CURLOPT_TFTP_BLKSIZE:
2490 #endif
2491 #if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2492 		case CURLOPT_FTP_USE_PRET:
2493 		case CURLOPT_RTSP_CLIENT_CSEQ:
2494 		case CURLOPT_RTSP_REQUEST:
2495 		case CURLOPT_RTSP_SERVER_CSEQ:
2496 #endif
2497 #if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
2498 		case CURLOPT_WILDCARDMATCH:
2499 #endif
2500 #if LIBCURL_VERSION_NUM >= 0x071504 /* Available since 7.21.4 */
2501 		case CURLOPT_TLSAUTH_TYPE:
2502 #endif
2503 #if LIBCURL_VERSION_NUM >= 0x071600 /* Available since 7.22.0 */
2504 		case CURLOPT_GSSAPI_DELEGATION:
2505 #endif
2506 #if LIBCURL_VERSION_NUM >= 0x071800 /* Available since 7.24.0 */
2507 		case CURLOPT_ACCEPTTIMEOUT_MS:
2508 #endif
2509 #if LIBCURL_VERSION_NUM >= 0x071900 /* Available since 7.25.0 */
2510 		case CURLOPT_SSL_OPTIONS:
2511 		case CURLOPT_TCP_KEEPALIVE:
2512 		case CURLOPT_TCP_KEEPIDLE:
2513 		case CURLOPT_TCP_KEEPINTVL:
2514 #endif
2515 #if LIBCURL_VERSION_NUM >= 0x071f00 /* Available since 7.31.0 */
2516 		case CURLOPT_SASL_IR:
2517 #endif
2518 #if LIBCURL_VERSION_NUM >= 0x072400 /* Available since 7.36.0 */
2519 		case CURLOPT_EXPECT_100_TIMEOUT_MS:
2520 		case CURLOPT_SSL_ENABLE_ALPN:
2521 		case CURLOPT_SSL_ENABLE_NPN:
2522 #endif
2523 #if LIBCURL_VERSION_NUM >= 0x072500 /* Available since 7.37.0 */
2524 		case CURLOPT_HEADEROPT:
2525 #endif
2526 #if LIBCURL_VERSION_NUM >= 0x072900 /* Available since 7.41.0 */
2527 		case CURLOPT_SSL_VERIFYSTATUS:
2528 #endif
2529 #if LIBCURL_VERSION_NUM >= 0x072a00 /* Available since 7.42.0 */
2530 		case CURLOPT_PATH_AS_IS:
2531 		case CURLOPT_SSL_FALSESTART:
2532 #endif
2533 #if LIBCURL_VERSION_NUM >= 0x072b00 /* Available since 7.43.0 */
2534 		case CURLOPT_PIPEWAIT:
2535 #endif
2536 #if LIBCURL_VERSION_NUM >= 0x072e00 /* Available since 7.46.0 */
2537 		case CURLOPT_STREAM_WEIGHT:
2538 #endif
2539 #if LIBCURL_VERSION_NUM >= 0x073000 /* Available since 7.48.0 */
2540 		case CURLOPT_TFTP_NO_OPTIONS:
2541 #endif
2542 #if LIBCURL_VERSION_NUM >= 0x073100 /* Available since 7.49.0 */
2543 		case CURLOPT_TCP_FASTOPEN:
2544 #endif
2545 #if LIBCURL_VERSION_NUM >= 0x073300 /* Available since 7.51.0 */
2546 		case CURLOPT_KEEP_SENDING_ON_ERROR:
2547 #endif
2548 #if LIBCURL_VERSION_NUM >= 0x073400 /* Available since 7.52.0 */
2549 		case CURLOPT_PROXY_SSL_OPTIONS:
2550 		case CURLOPT_PROXY_SSL_VERIFYHOST:
2551 		case CURLOPT_PROXY_SSL_VERIFYPEER:
2552 		case CURLOPT_PROXY_SSLVERSION:
2553 #endif
2554 #if LIBCURL_VERSION_NUM >= 0x073600 /* Available since 7.54.0 */
2555 		case CURLOPT_SUPPRESS_CONNECT_HEADERS:
2556 #endif
2557 #if LIBCURL_VERSION_NUM >= 0x073700 /* Available since 7.55.0 */
2558 		case CURLOPT_SOCKS5_AUTH:
2559 #endif
2560 #if LIBCURL_VERSION_NUM >= 0x073800 /* Available since 7.56.0 */
2561 		case CURLOPT_SSH_COMPRESSION:
2562 #endif
2563 #if LIBCURL_VERSION_NUM >= 0x073b00 /* Available since 7.59.0 */
2564 		case CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS:
2565 #endif
2566 #if LIBCURL_VERSION_NUM >= 0x073c00 /* Available since 7.60.0 */
2567 		case CURLOPT_DNS_SHUFFLE_ADDRESSES:
2568 		case CURLOPT_HAPROXYPROTOCOL:
2569 #endif
2570 #if LIBCURL_VERSION_NUM >= 0x073d00 /* Available since 7.61.0 */
2571 		case CURLOPT_DISALLOW_USERNAME_IN_URL:
2572 #endif
2573 #if LIBCURL_VERSION_NUM >= 0x074000 /* Available since 7.64.0 */
2574 		case CURLOPT_HTTP09_ALLOWED:
2575 #endif
2576 			lval = zval_get_long(zvalue);
2577 #if LIBCURL_VERSION_NUM >= 0x071304
2578 			if ((option == CURLOPT_PROTOCOLS || option == CURLOPT_REDIR_PROTOCOLS) &&
2579 				(PG(open_basedir) && *PG(open_basedir)) && (lval & CURLPROTO_FILE)) {
2580 					php_error_docref(NULL, E_WARNING, "CURLPROTO_FILE cannot be activated when an open_basedir is set");
2581 					return 1;
2582 			}
2583 #endif
2584 # if defined(ZTS)
2585 			if (option == CURLOPT_DNS_USE_GLOBAL_CACHE && lval) {
2586 				php_error_docref(NULL, E_WARNING, "CURLOPT_DNS_USE_GLOBAL_CACHE cannot be activated when thread safety is enabled");
2587 				return 1;
2588 			}
2589 # endif
2590 			error = curl_easy_setopt(ch->cp, option, lval);
2591 			break;
2592 		case CURLOPT_SAFE_UPLOAD:
2593 			if (!zend_is_true(zvalue)) {
2594 				php_error_docref(NULL, E_WARNING, "Disabling safe uploads is no longer supported");
2595 				return FAILURE;
2596 			}
2597 			break;
2598 
2599 		/* String options */
2600 		case CURLOPT_CAINFO:
2601 		case CURLOPT_CAPATH:
2602 		case CURLOPT_COOKIE:
2603 		case CURLOPT_EGDSOCKET:
2604 		case CURLOPT_INTERFACE:
2605 		case CURLOPT_PROXY:
2606 		case CURLOPT_PROXYUSERPWD:
2607 		case CURLOPT_REFERER:
2608 		case CURLOPT_SSLCERTTYPE:
2609 		case CURLOPT_SSLENGINE:
2610 		case CURLOPT_SSLENGINE_DEFAULT:
2611 		case CURLOPT_SSLKEY:
2612 		case CURLOPT_SSLKEYPASSWD:
2613 		case CURLOPT_SSLKEYTYPE:
2614 		case CURLOPT_SSL_CIPHER_LIST:
2615 		case CURLOPT_USERAGENT:
2616 		case CURLOPT_USERPWD:
2617 		case CURLOPT_COOKIELIST:
2618 		case CURLOPT_FTP_ALTERNATIVE_TO_USER:
2619 #if LIBCURL_VERSION_NUM >= 0x071101 /* Available since 7.17.1 */
2620 		case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
2621 #endif
2622 #if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
2623 		case CURLOPT_PASSWORD:
2624 		case CURLOPT_PROXYPASSWORD:
2625 		case CURLOPT_PROXYUSERNAME:
2626 		case CURLOPT_USERNAME:
2627 #endif
2628 #if LIBCURL_VERSION_NUM >= 0x071304 /* Available since 7.19.4 */
2629 		case CURLOPT_NOPROXY:
2630 		case CURLOPT_SOCKS5_GSSAPI_SERVICE:
2631 #endif
2632 #if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2633 		case CURLOPT_MAIL_FROM:
2634 		case CURLOPT_RTSP_STREAM_URI:
2635 		case CURLOPT_RTSP_TRANSPORT:
2636 #endif
2637 #if LIBCURL_VERSION_NUM >= 0x071504 /* Available since 7.21.4 */
2638 		case CURLOPT_TLSAUTH_PASSWORD:
2639 		case CURLOPT_TLSAUTH_USERNAME:
2640 #endif
2641 #if LIBCURL_VERSION_NUM >= 0x071506 /* Available since 7.21.6 */
2642 		case CURLOPT_ACCEPT_ENCODING:
2643 		case CURLOPT_TRANSFER_ENCODING:
2644 #else
2645 		case CURLOPT_ENCODING:
2646 #endif
2647 #if LIBCURL_VERSION_NUM >= 0x071800 /* Available since 7.24.0 */
2648 		case CURLOPT_DNS_SERVERS:
2649 #endif
2650 #if LIBCURL_VERSION_NUM >= 0x071900 /* Available since 7.25.0 */
2651 		case CURLOPT_MAIL_AUTH:
2652 #endif
2653 #if LIBCURL_VERSION_NUM >= 0x072200 /* Available since 7.34.0 */
2654 		case CURLOPT_LOGIN_OPTIONS:
2655 #endif
2656 #if LIBCURL_VERSION_NUM >= 0x072700 /* Available since 7.39.0 */
2657 		case CURLOPT_PINNEDPUBLICKEY:
2658 #endif
2659 #if LIBCURL_VERSION_NUM >= 0x072b00 /* Available since 7.43.0 */
2660 		case CURLOPT_PROXY_SERVICE_NAME:
2661 		case CURLOPT_SERVICE_NAME:
2662 #endif
2663 #if LIBCURL_VERSION_NUM >= 0x072d00 /* Available since 7.45.0 */
2664 		case CURLOPT_DEFAULT_PROTOCOL:
2665 #endif
2666 #if LIBCURL_VERSION_NUM >= 0x073400 /* Available since 7.52.0 */
2667 		case CURLOPT_PRE_PROXY:
2668 		case CURLOPT_PROXY_CAINFO:
2669 		case CURLOPT_PROXY_CAPATH:
2670 		case CURLOPT_PROXY_CRLFILE:
2671 		case CURLOPT_PROXY_KEYPASSWD:
2672 		case CURLOPT_PROXY_PINNEDPUBLICKEY:
2673 		case CURLOPT_PROXY_SSL_CIPHER_LIST:
2674 		case CURLOPT_PROXY_SSLCERT:
2675 		case CURLOPT_PROXY_SSLCERTTYPE:
2676 		case CURLOPT_PROXY_SSLKEY:
2677 		case CURLOPT_PROXY_SSLKEYTYPE:
2678 		case CURLOPT_PROXY_TLSAUTH_PASSWORD:
2679 		case CURLOPT_PROXY_TLSAUTH_TYPE:
2680 		case CURLOPT_PROXY_TLSAUTH_USERNAME:
2681 #endif
2682 #if LIBCURL_VERSION_NUM >= 0x073500 /* Available since 7.53.0 */
2683 		case CURLOPT_ABSTRACT_UNIX_SOCKET:
2684 #endif
2685 #if LIBCURL_VERSION_NUM >= 0x073700 /* Available since 7.55.0 */
2686 		case CURLOPT_REQUEST_TARGET:
2687 #endif
2688 #if LIBCURL_VERSION_NUM >= 0x073d00 /* Available since 7.61.0 */
2689 		case CURLOPT_PROXY_TLS13_CIPHERS:
2690 		case CURLOPT_TLS13_CIPHERS:
2691 #endif
2692 		{
2693 			zend_string *tmp_str;
2694 			zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
2695 			int ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str), 0);
2696 			zend_tmp_string_release(tmp_str);
2697 			return ret;
2698 		}
2699 
2700 		/* Curl nullable string options */
2701 		case CURLOPT_CUSTOMREQUEST:
2702 		case CURLOPT_FTPPORT:
2703 		case CURLOPT_RANGE:
2704 		case CURLOPT_FTP_ACCOUNT:
2705 #if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2706 		case CURLOPT_RTSP_SESSION_ID:
2707 #endif
2708 #if LIBCURL_VERSION_NUM >= 0x072100 /* Available since 7.33.0 */
2709 		case CURLOPT_DNS_INTERFACE:
2710 		case CURLOPT_DNS_LOCAL_IP4:
2711 		case CURLOPT_DNS_LOCAL_IP6:
2712 		case CURLOPT_XOAUTH2_BEARER:
2713 #endif
2714 #if LIBCURL_VERSION_NUM >= 0x072800 /* Available since 7.40.0 */
2715 		case CURLOPT_UNIX_SOCKET_PATH:
2716 #endif
2717 #if LIBCURL_VERSION_NUM >= 0x071004 /* Available since 7.16.4 */
2718 		case CURLOPT_KRBLEVEL:
2719 #else
2720 		case CURLOPT_KRB4LEVEL:
2721 #endif
2722 		{
2723 			if (Z_ISNULL_P(zvalue)) {
2724 				error = curl_easy_setopt(ch->cp, option, NULL);
2725 			} else {
2726 				zend_string *tmp_str;
2727 				zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
2728 				int ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str), 0);
2729 				zend_tmp_string_release(tmp_str);
2730 				return ret;
2731 			}
2732 			break;
2733 		}
2734 
2735 		/* Curl private option */
2736 		case CURLOPT_PRIVATE:
2737 		{
2738 			zend_string *tmp_str;
2739 			zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
2740 			int ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str), 1);
2741 			zend_tmp_string_release(tmp_str);
2742 			return ret;
2743 		}
2744 
2745 		/* Curl url option */
2746 		case CURLOPT_URL:
2747 		{
2748 			zend_string *tmp_str;
2749 			zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
2750 			int ret = php_curl_option_url(ch, ZSTR_VAL(str), ZSTR_LEN(str));
2751 			zend_tmp_string_release(tmp_str);
2752 			return ret;
2753 		}
2754 
2755 		/* Curl file handle options */
2756 		case CURLOPT_FILE:
2757 		case CURLOPT_INFILE:
2758 		case CURLOPT_STDERR:
2759 		case CURLOPT_WRITEHEADER: {
2760 			FILE *fp = NULL;
2761 			php_stream *what = NULL;
2762 
2763 			if (Z_TYPE_P(zvalue) != IS_NULL) {
2764 				what = (php_stream *)zend_fetch_resource2_ex(zvalue, "File-Handle", php_file_le_stream(), php_file_le_pstream());
2765 				if (!what) {
2766 					return FAILURE;
2767 				}
2768 
2769 				if (FAILURE == php_stream_cast(what, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) {
2770 					return FAILURE;
2771 				}
2772 
2773 				if (!fp) {
2774 					return FAILURE;
2775 				}
2776 			}
2777 
2778 			error = CURLE_OK;
2779 			switch (option) {
2780 				case CURLOPT_FILE:
2781 					if (!what) {
2782 						if (!Z_ISUNDEF(ch->handlers->write->stream)) {
2783 							zval_ptr_dtor(&ch->handlers->write->stream);
2784 							ZVAL_UNDEF(&ch->handlers->write->stream);
2785 						}
2786 						ch->handlers->write->fp = NULL;
2787 						ch->handlers->write->method = PHP_CURL_STDOUT;
2788 					} else if (what->mode[0] != 'r' || what->mode[1] == '+') {
2789 						zval_ptr_dtor(&ch->handlers->write->stream);
2790 						ch->handlers->write->fp = fp;
2791 						ch->handlers->write->method = PHP_CURL_FILE;
2792 						ZVAL_COPY(&ch->handlers->write->stream, zvalue);
2793 					} else {
2794 						php_error_docref(NULL, E_WARNING, "the provided file handle is not writable");
2795 						return FAILURE;
2796 					}
2797 					break;
2798 				case CURLOPT_WRITEHEADER:
2799 					if (!what) {
2800 						if (!Z_ISUNDEF(ch->handlers->write_header->stream)) {
2801 							zval_ptr_dtor(&ch->handlers->write_header->stream);
2802 							ZVAL_UNDEF(&ch->handlers->write_header->stream);
2803 						}
2804 						ch->handlers->write_header->fp = NULL;
2805 						ch->handlers->write_header->method = PHP_CURL_IGNORE;
2806 					} else if (what->mode[0] != 'r' || what->mode[1] == '+') {
2807 						zval_ptr_dtor(&ch->handlers->write_header->stream);
2808 						ch->handlers->write_header->fp = fp;
2809 						ch->handlers->write_header->method = PHP_CURL_FILE;
2810 						ZVAL_COPY(&ch->handlers->write_header->stream, zvalue);
2811 					} else {
2812 						php_error_docref(NULL, E_WARNING, "the provided file handle is not writable");
2813 						return FAILURE;
2814 					}
2815 					break;
2816 				case CURLOPT_INFILE:
2817 					if (!what) {
2818 						if (!Z_ISUNDEF(ch->handlers->read->stream)) {
2819 							zval_ptr_dtor(&ch->handlers->read->stream);
2820 							ZVAL_UNDEF(&ch->handlers->read->stream);
2821 						}
2822 						ch->handlers->read->fp = NULL;
2823 						ch->handlers->read->res = NULL;
2824 					} else {
2825 						zval_ptr_dtor(&ch->handlers->read->stream);
2826 						ch->handlers->read->fp = fp;
2827 						ch->handlers->read->res = Z_RES_P(zvalue);
2828 						ZVAL_COPY(&ch->handlers->read->stream, zvalue);
2829 					}
2830 					break;
2831 				case CURLOPT_STDERR:
2832 					if (!what) {
2833 						if (!Z_ISUNDEF(ch->handlers->std_err)) {
2834 							zval_ptr_dtor(&ch->handlers->std_err);
2835 							ZVAL_UNDEF(&ch->handlers->std_err);
2836 						}
2837 					} else if (what->mode[0] != 'r' || what->mode[1] == '+') {
2838 						zval_ptr_dtor(&ch->handlers->std_err);
2839 						ZVAL_COPY(&ch->handlers->std_err, zvalue);
2840 					} else {
2841 						php_error_docref(NULL, E_WARNING, "the provided file handle is not writable");
2842 						return FAILURE;
2843 					}
2844 					/* break omitted intentionally */
2845 				default:
2846 					error = curl_easy_setopt(ch->cp, option, fp);
2847 					break;
2848 			}
2849 			break;
2850 		}
2851 
2852 		/* Curl linked list options */
2853 		case CURLOPT_HTTP200ALIASES:
2854 		case CURLOPT_HTTPHEADER:
2855 		case CURLOPT_POSTQUOTE:
2856 		case CURLOPT_PREQUOTE:
2857 		case CURLOPT_QUOTE:
2858 		case CURLOPT_TELNETOPTIONS:
2859 #if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2860 		case CURLOPT_MAIL_RCPT:
2861 #endif
2862 #if LIBCURL_VERSION_NUM >= 0x071503 /* Available since 7.21.3 */
2863 		case CURLOPT_RESOLVE:
2864 #endif
2865 #if LIBCURL_VERSION_NUM >= 0x072500 /* Available since 7.37.0 */
2866 		case CURLOPT_PROXYHEADER:
2867 #endif
2868 #if LIBCURL_VERSION_NUM >= 0x073100 /* Available since 7.49.0 */
2869 		case CURLOPT_CONNECT_TO:
2870 #endif
2871 		{
2872 			zval *current;
2873 			HashTable *ph;
2874 			zend_string *val, *tmp_val;
2875 			struct curl_slist *slist = NULL;
2876 
2877 			ph = HASH_OF(zvalue);
2878 			if (!ph) {
2879 				char *name = NULL;
2880 				switch (option) {
2881 					case CURLOPT_HTTPHEADER:
2882 						name = "CURLOPT_HTTPHEADER";
2883 						break;
2884 					case CURLOPT_QUOTE:
2885 						name = "CURLOPT_QUOTE";
2886 						break;
2887 					case CURLOPT_HTTP200ALIASES:
2888 						name = "CURLOPT_HTTP200ALIASES";
2889 						break;
2890 					case CURLOPT_POSTQUOTE:
2891 						name = "CURLOPT_POSTQUOTE";
2892 						break;
2893 					case CURLOPT_PREQUOTE:
2894 						name = "CURLOPT_PREQUOTE";
2895 						break;
2896 					case CURLOPT_TELNETOPTIONS:
2897 						name = "CURLOPT_TELNETOPTIONS";
2898 						break;
2899 #if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2900 					case CURLOPT_MAIL_RCPT:
2901 						name = "CURLOPT_MAIL_RCPT";
2902 						break;
2903 #endif
2904 #if LIBCURL_VERSION_NUM >= 0x071503 /* Available since 7.21.3 */
2905 					case CURLOPT_RESOLVE:
2906 						name = "CURLOPT_RESOLVE";
2907 						break;
2908 #endif
2909 #if LIBCURL_VERSION_NUM >= 0x072500 /* Available since 7.37.0 */
2910 					case CURLOPT_PROXYHEADER:
2911 						name = "CURLOPT_PROXYHEADER";
2912 						break;
2913 #endif
2914 #if LIBCURL_VERSION_NUM >= 0x073100 /* Available since 7.49.0 */
2915 					case CURLOPT_CONNECT_TO:
2916 						name = "CURLOPT_CONNECT_TO";
2917 						break;
2918 #endif
2919 				}
2920 				php_error_docref(NULL, E_WARNING, "You must pass either an object or an array with the %s argument", name);
2921 				return FAILURE;
2922 			}
2923 
2924 			ZEND_HASH_FOREACH_VAL_IND(ph, current) {
2925 				ZVAL_DEREF(current);
2926 				val = zval_get_tmp_string(current, &tmp_val);
2927 				slist = curl_slist_append(slist, ZSTR_VAL(val));
2928 				zend_tmp_string_release(tmp_val);
2929 				if (!slist) {
2930 					php_error_docref(NULL, E_WARNING, "Could not build curl_slist");
2931 					return 1;
2932 				}
2933 			} ZEND_HASH_FOREACH_END();
2934 
2935 			if (slist) {
2936 				if ((*ch->clone) == 1) {
2937 					zend_hash_index_update_ptr(ch->to_free->slist, option, slist);
2938 				} else {
2939 					zend_hash_next_index_insert_ptr(ch->to_free->slist, slist);
2940 				}
2941 			}
2942 
2943 			error = curl_easy_setopt(ch->cp, option, slist);
2944 
2945 			break;
2946 		}
2947 
2948 		case CURLOPT_BINARYTRANSFER:
2949 			/* Do nothing, just backward compatibility */
2950 			break;
2951 
2952 		case CURLOPT_FOLLOWLOCATION:
2953 			lval = zend_is_true(zvalue);
2954 #if LIBCURL_VERSION_NUM < 0x071304
2955 			if (lval && PG(open_basedir) && *PG(open_basedir)) {
2956 				php_error_docref(NULL, E_WARNING, "CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set");
2957 				return FAILURE;
2958 			}
2959 #endif
2960 			error = curl_easy_setopt(ch->cp, option, lval);
2961 			break;
2962 
2963 		case CURLOPT_HEADERFUNCTION:
2964 			if (!Z_ISUNDEF(ch->handlers->write_header->func_name)) {
2965 				zval_ptr_dtor(&ch->handlers->write_header->func_name);
2966 				ch->handlers->write_header->fci_cache = empty_fcall_info_cache;
2967 			}
2968 			ZVAL_COPY(&ch->handlers->write_header->func_name, zvalue);
2969 			ch->handlers->write_header->method = PHP_CURL_USER;
2970 			break;
2971 
2972 		case CURLOPT_POSTFIELDS:
2973 			if (Z_TYPE_P(zvalue) == IS_ARRAY || Z_TYPE_P(zvalue) == IS_OBJECT) {
2974 				if (zend_hash_num_elements(HASH_OF(zvalue)) == 0) {
2975 					/* no need to build the mime structure for empty hashtables;
2976 					   also works around https://github.com/curl/curl/issues/6455 */
2977 					curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDS, "");
2978 					error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, 0);
2979 				} else {
2980 					return build_mime_structure_from_hash(ch, zvalue);
2981 				}
2982 			} else {
2983 #if LIBCURL_VERSION_NUM >= 0x071101
2984 				zend_string *tmp_str;
2985 				zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
2986 				/* with curl 7.17.0 and later, we can use COPYPOSTFIELDS, but we have to provide size before */
2987 				error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, ZSTR_LEN(str));
2988 				error = curl_easy_setopt(ch->cp, CURLOPT_COPYPOSTFIELDS, ZSTR_VAL(str));
2989 				zend_tmp_string_release(tmp_str);
2990 #else
2991 				char *post = NULL;
2992 				zend_string *tmp_str;
2993 				zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
2994 
2995 				post = estrndup(ZSTR_VAL(str), ZSTR_LEN(str));
2996 				zend_llist_add_element(&ch->to_free->str, &post);
2997 
2998 				curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDS, post);
2999 				error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, ZSTR_LEN(str));
3000 				zend_tmp_string_release(tmp_str);
3001 #endif
3002 			}
3003 			break;
3004 
3005 		case CURLOPT_PROGRESSFUNCTION:
3006 			curl_easy_setopt(ch->cp, CURLOPT_PROGRESSFUNCTION,	curl_progress);
3007 			curl_easy_setopt(ch->cp, CURLOPT_PROGRESSDATA, ch);
3008 			if (ch->handlers->progress == NULL) {
3009 				ch->handlers->progress = ecalloc(1, sizeof(php_curl_progress));
3010 			} else if (!Z_ISUNDEF(ch->handlers->progress->func_name)) {
3011 				zval_ptr_dtor(&ch->handlers->progress->func_name);
3012 				ch->handlers->progress->fci_cache = empty_fcall_info_cache;
3013 			}
3014 			ZVAL_COPY(&ch->handlers->progress->func_name, zvalue);
3015 			ch->handlers->progress->method = PHP_CURL_USER;
3016 			break;
3017 
3018 		case CURLOPT_READFUNCTION:
3019 			if (!Z_ISUNDEF(ch->handlers->read->func_name)) {
3020 				zval_ptr_dtor(&ch->handlers->read->func_name);
3021 				ch->handlers->read->fci_cache = empty_fcall_info_cache;
3022 			}
3023 			ZVAL_COPY(&ch->handlers->read->func_name, zvalue);
3024 			ch->handlers->read->method = PHP_CURL_USER;
3025 			break;
3026 
3027 		case CURLOPT_RETURNTRANSFER:
3028 			if (zend_is_true(zvalue)) {
3029 				ch->handlers->write->method = PHP_CURL_RETURN;
3030 			} else {
3031 				ch->handlers->write->method = PHP_CURL_STDOUT;
3032 			}
3033 			break;
3034 
3035 		case CURLOPT_WRITEFUNCTION:
3036 			if (!Z_ISUNDEF(ch->handlers->write->func_name)) {
3037 				zval_ptr_dtor(&ch->handlers->write->func_name);
3038 				ch->handlers->write->fci_cache = empty_fcall_info_cache;
3039 			}
3040 			ZVAL_COPY(&ch->handlers->write->func_name, zvalue);
3041 			ch->handlers->write->method = PHP_CURL_USER;
3042 			break;
3043 
3044 		/* Curl off_t options */
3045 		case CURLOPT_MAX_RECV_SPEED_LARGE:
3046 		case CURLOPT_MAX_SEND_SPEED_LARGE:
3047 #if LIBCURL_VERSION_NUM >= 0x073b00 /* Available since 7.59.0 */
3048 		case CURLOPT_TIMEVALUE_LARGE:
3049 #endif
3050 			lval = zval_get_long(zvalue);
3051 			error = curl_easy_setopt(ch->cp, option, (curl_off_t)lval);
3052 			break;
3053 
3054 #if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
3055 		case CURLOPT_POSTREDIR:
3056 			lval = zval_get_long(zvalue);
3057 			error = curl_easy_setopt(ch->cp, CURLOPT_POSTREDIR, lval & CURL_REDIR_POST_ALL);
3058 			break;
3059 #endif
3060 
3061 		/* the following options deal with files, therefore the open_basedir check
3062 		 * is required.
3063 		 */
3064 		case CURLOPT_COOKIEFILE:
3065 		case CURLOPT_COOKIEJAR:
3066 		case CURLOPT_RANDOM_FILE:
3067 		case CURLOPT_SSLCERT:
3068 		case CURLOPT_NETRC_FILE:
3069 #if LIBCURL_VERSION_NUM >= 0x071001 /* Available since 7.16.1 */
3070 		case CURLOPT_SSH_PRIVATE_KEYFILE:
3071 		case CURLOPT_SSH_PUBLIC_KEYFILE:
3072 #endif
3073 #if LIBCURL_VERSION_NUM >= 0x071300 /* Available since 7.19.0 */
3074 		case CURLOPT_CRLFILE:
3075 		case CURLOPT_ISSUERCERT:
3076 #endif
3077 #if LIBCURL_VERSION_NUM >= 0x071306 /* Available since 7.19.6 */
3078 		case CURLOPT_SSH_KNOWNHOSTS:
3079 #endif
3080 		{
3081 		    zend_string *tmp_str;
3082 			zend_string *str = zval_get_tmp_string(zvalue, &tmp_str);
3083 			int ret;
3084 
3085 			if (ZSTR_LEN(str) && php_check_open_basedir(ZSTR_VAL(str))) {
3086 				zend_tmp_string_release(tmp_str);
3087 				return FAILURE;
3088 			}
3089 
3090 			ret = php_curl_option_str(ch, option, ZSTR_VAL(str), ZSTR_LEN(str), 0);
3091 			zend_tmp_string_release(tmp_str);
3092 			return ret;
3093 		}
3094 
3095 		case CURLINFO_HEADER_OUT:
3096 			if (zend_is_true(zvalue)) {
3097 				curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, curl_debug);
3098 				curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, (void *)ch);
3099 				curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 1);
3100 			} else {
3101 				curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, NULL);
3102 				curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, NULL);
3103 				curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 0);
3104 			}
3105 			break;
3106 
3107 		case CURLOPT_SHARE:
3108 			{
3109 				php_curlsh *sh;
3110 				if ((sh = (php_curlsh *)zend_fetch_resource_ex(zvalue, le_curl_share_handle_name, le_curl_share_handle))) {
3111 					curl_easy_setopt(ch->cp, CURLOPT_SHARE, sh->share);
3112 				}
3113 			}
3114 			break;
3115 
3116 #if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
3117 		case CURLOPT_FNMATCH_FUNCTION:
3118 			curl_easy_setopt(ch->cp, CURLOPT_FNMATCH_FUNCTION, curl_fnmatch);
3119 			curl_easy_setopt(ch->cp, CURLOPT_FNMATCH_DATA, ch);
3120 			if (ch->handlers->fnmatch == NULL) {
3121 				ch->handlers->fnmatch = ecalloc(1, sizeof(php_curl_fnmatch));
3122 			} else if (!Z_ISUNDEF(ch->handlers->fnmatch->func_name)) {
3123 				zval_ptr_dtor(&ch->handlers->fnmatch->func_name);
3124 				ch->handlers->fnmatch->fci_cache = empty_fcall_info_cache;
3125 			}
3126 			ZVAL_COPY(&ch->handlers->fnmatch->func_name, zvalue);
3127 			ch->handlers->fnmatch->method = PHP_CURL_USER;
3128 			break;
3129 #endif
3130 
3131 	}
3132 
3133 	SAVE_CURL_ERROR(ch, error);
3134 	if (error != CURLE_OK) {
3135 		return FAILURE;
3136 	} else {
3137 		return SUCCESS;
3138 	}
3139 }
3140 /* }}} */
3141 
3142 /* {{{ proto bool curl_setopt(resource ch, int option, mixed value)
3143    Set an option for a cURL transfer */
PHP_FUNCTION(curl_setopt)3144 PHP_FUNCTION(curl_setopt)
3145 {
3146 	zval       *zid, *zvalue;
3147 	zend_long        options;
3148 	php_curl   *ch;
3149 
3150 	ZEND_PARSE_PARAMETERS_START(3, 3)
3151 		Z_PARAM_RESOURCE(zid)
3152 		Z_PARAM_LONG(options)
3153 		Z_PARAM_ZVAL(zvalue)
3154 	ZEND_PARSE_PARAMETERS_END();
3155 
3156 	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3157 		RETURN_FALSE;
3158 	}
3159 
3160 	if (options <= 0 && options != CURLOPT_SAFE_UPLOAD) {
3161 		php_error_docref(NULL, E_WARNING, "Invalid curl configuration option");
3162 		RETURN_FALSE;
3163 	}
3164 
3165 	if (_php_curl_setopt(ch, options, zvalue) == SUCCESS) {
3166 		RETURN_TRUE;
3167 	} else {
3168 		RETURN_FALSE;
3169 	}
3170 }
3171 /* }}} */
3172 
3173 /* {{{ proto bool curl_setopt_array(resource ch, array options)
3174    Set an array of option for a cURL transfer */
PHP_FUNCTION(curl_setopt_array)3175 PHP_FUNCTION(curl_setopt_array)
3176 {
3177 	zval		*zid, *arr, *entry;
3178 	php_curl	*ch;
3179 	zend_ulong	option;
3180 	zend_string	*string_key;
3181 
3182 	ZEND_PARSE_PARAMETERS_START(2, 2)
3183 		Z_PARAM_RESOURCE(zid)
3184 		Z_PARAM_ARRAY(arr)
3185 	ZEND_PARSE_PARAMETERS_END();
3186 
3187 	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3188 		RETURN_FALSE;
3189 	}
3190 
3191 	ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arr), option, string_key, entry) {
3192 		if (string_key) {
3193 			php_error_docref(NULL, E_WARNING,
3194 					"Array keys must be CURLOPT constants or equivalent integer values");
3195 			RETURN_FALSE;
3196 		}
3197 		ZVAL_DEREF(entry);
3198 		if (_php_curl_setopt(ch, (zend_long) option, entry) == FAILURE) {
3199 			RETURN_FALSE;
3200 		}
3201 	} ZEND_HASH_FOREACH_END();
3202 
3203 	RETURN_TRUE;
3204 }
3205 /* }}} */
3206 
3207 /* {{{ _php_curl_cleanup_handle(ch)
3208    Cleanup an execution phase */
_php_curl_cleanup_handle(php_curl * ch)3209 void _php_curl_cleanup_handle(php_curl *ch)
3210 {
3211 	smart_str_free(&ch->handlers->write->buf);
3212 	if (ch->header.str) {
3213 		zend_string_release_ex(ch->header.str, 0);
3214 		ch->header.str = NULL;
3215 	}
3216 
3217 	memset(ch->err.str, 0, CURL_ERROR_SIZE + 1);
3218 	ch->err.no = 0;
3219 }
3220 /* }}} */
3221 
3222 /* {{{ proto bool curl_exec(resource ch)
3223    Perform a cURL session */
PHP_FUNCTION(curl_exec)3224 PHP_FUNCTION(curl_exec)
3225 {
3226 	CURLcode	error;
3227 	zval		*zid;
3228 	php_curl	*ch;
3229 
3230 	ZEND_PARSE_PARAMETERS_START(1, 1)
3231 		Z_PARAM_RESOURCE(zid)
3232 	ZEND_PARSE_PARAMETERS_END();
3233 
3234 	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3235 		RETURN_FALSE;
3236 	}
3237 
3238 	_php_curl_verify_handlers(ch, 1);
3239 
3240 	_php_curl_cleanup_handle(ch);
3241 
3242 	error = curl_easy_perform(ch->cp);
3243 	SAVE_CURL_ERROR(ch, error);
3244 
3245 	if (error != CURLE_OK) {
3246 		smart_str_free(&ch->handlers->write->buf);
3247 		RETURN_FALSE;
3248 	}
3249 
3250 	if (!Z_ISUNDEF(ch->handlers->std_err)) {
3251 		php_stream  *stream;
3252 		stream = (php_stream*)zend_fetch_resource2_ex(&ch->handlers->std_err, NULL, php_file_le_stream(), php_file_le_pstream());
3253 		if (stream) {
3254 			php_stream_flush(stream);
3255 		}
3256 	}
3257 
3258 	if (ch->handlers->write->method == PHP_CURL_RETURN && ch->handlers->write->buf.s) {
3259 		smart_str_0(&ch->handlers->write->buf);
3260 		RETURN_STR_COPY(ch->handlers->write->buf.s);
3261 	}
3262 
3263 	/* flush the file handle, so any remaining data is synched to disk */
3264 	if (ch->handlers->write->method == PHP_CURL_FILE && ch->handlers->write->fp) {
3265 		fflush(ch->handlers->write->fp);
3266 	}
3267 	if (ch->handlers->write_header->method == PHP_CURL_FILE && ch->handlers->write_header->fp) {
3268 		fflush(ch->handlers->write_header->fp);
3269 	}
3270 
3271 	if (ch->handlers->write->method == PHP_CURL_RETURN) {
3272 		RETURN_EMPTY_STRING();
3273 	} else {
3274 		RETURN_TRUE;
3275 	}
3276 }
3277 /* }}} */
3278 
3279 /* {{{ proto mixed curl_getinfo(resource ch [, int option])
3280    Get information regarding a specific transfer */
PHP_FUNCTION(curl_getinfo)3281 PHP_FUNCTION(curl_getinfo)
3282 {
3283 	zval		*zid;
3284 	php_curl	*ch;
3285 	zend_long	option = 0;
3286 
3287 	ZEND_PARSE_PARAMETERS_START(1, 2)
3288 		Z_PARAM_RESOURCE(zid)
3289 		Z_PARAM_OPTIONAL
3290 		Z_PARAM_LONG(option)
3291 	ZEND_PARSE_PARAMETERS_END();
3292 
3293 	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3294 		RETURN_FALSE;
3295 	}
3296 
3297 	if (ZEND_NUM_ARGS() < 2) {
3298 		char *s_code;
3299 		/* libcurl expects long datatype. So far no cases are known where
3300 		   it would be an issue. Using zend_long would truncate a 64-bit
3301 		   var on Win64, so the exact long datatype fits everywhere, as
3302 		   long as there's no 32-bit int overflow. */
3303 		long l_code;
3304 		double d_code;
3305 #if LIBCURL_VERSION_NUM > 0x071301 /* 7.19.1 */
3306 		struct curl_certinfo *ci = NULL;
3307 		zval listcode;
3308 #endif
3309 #if LIBCURL_VERSION_NUM >= 0x073d00 /* 7.61.0 */
3310 		curl_off_t co;
3311 #endif
3312 
3313 		array_init(return_value);
3314 
3315 		if (curl_easy_getinfo(ch->cp, CURLINFO_EFFECTIVE_URL, &s_code) == CURLE_OK) {
3316 			CAAS("url", s_code);
3317 		}
3318 		if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_TYPE, &s_code) == CURLE_OK) {
3319 			if (s_code != NULL) {
3320 				CAAS("content_type", s_code);
3321 			} else {
3322 				zval retnull;
3323 				ZVAL_NULL(&retnull);
3324 				CAAZ("content_type", &retnull);
3325 			}
3326 		}
3327 		if (curl_easy_getinfo(ch->cp, CURLINFO_HTTP_CODE, &l_code) == CURLE_OK) {
3328 			CAAL("http_code", l_code);
3329 		}
3330 		if (curl_easy_getinfo(ch->cp, CURLINFO_HEADER_SIZE, &l_code) == CURLE_OK) {
3331 			CAAL("header_size", l_code);
3332 		}
3333 		if (curl_easy_getinfo(ch->cp, CURLINFO_REQUEST_SIZE, &l_code) == CURLE_OK) {
3334 			CAAL("request_size", l_code);
3335 		}
3336 		if (curl_easy_getinfo(ch->cp, CURLINFO_FILETIME, &l_code) == CURLE_OK) {
3337 			CAAL("filetime", l_code);
3338 		}
3339 		if (curl_easy_getinfo(ch->cp, CURLINFO_SSL_VERIFYRESULT, &l_code) == CURLE_OK) {
3340 			CAAL("ssl_verify_result", l_code);
3341 		}
3342 		if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_COUNT, &l_code) == CURLE_OK) {
3343 			CAAL("redirect_count", l_code);
3344 		}
3345 		if (curl_easy_getinfo(ch->cp, CURLINFO_TOTAL_TIME, &d_code) == CURLE_OK) {
3346 			CAAD("total_time", d_code);
3347 		}
3348 		if (curl_easy_getinfo(ch->cp, CURLINFO_NAMELOOKUP_TIME, &d_code) == CURLE_OK) {
3349 			CAAD("namelookup_time", d_code);
3350 		}
3351 		if (curl_easy_getinfo(ch->cp, CURLINFO_CONNECT_TIME, &d_code) == CURLE_OK) {
3352 			CAAD("connect_time", d_code);
3353 		}
3354 		if (curl_easy_getinfo(ch->cp, CURLINFO_PRETRANSFER_TIME, &d_code) == CURLE_OK) {
3355 			CAAD("pretransfer_time", d_code);
3356 		}
3357 		if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_UPLOAD, &d_code) == CURLE_OK) {
3358 			CAAD("size_upload", d_code);
3359 		}
3360 		if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_DOWNLOAD, &d_code) == CURLE_OK) {
3361 			CAAD("size_download", d_code);
3362 		}
3363 		if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_DOWNLOAD, &d_code) == CURLE_OK) {
3364 			CAAD("speed_download", d_code);
3365 		}
3366 		if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_UPLOAD, &d_code) == CURLE_OK) {
3367 			CAAD("speed_upload", d_code);
3368 		}
3369 		if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d_code) == CURLE_OK) {
3370 			CAAD("download_content_length", d_code);
3371 		}
3372 		if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_UPLOAD, &d_code) == CURLE_OK) {
3373 			CAAD("upload_content_length", d_code);
3374 		}
3375 		if (curl_easy_getinfo(ch->cp, CURLINFO_STARTTRANSFER_TIME, &d_code) == CURLE_OK) {
3376 			CAAD("starttransfer_time", d_code);
3377 		}
3378 		if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_TIME, &d_code) == CURLE_OK) {
3379 			CAAD("redirect_time", d_code);
3380 		}
3381 #if LIBCURL_VERSION_NUM >= 0x071202 /* Available since 7.18.2 */
3382 		if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_URL, &s_code) == CURLE_OK) {
3383 			CAAS("redirect_url", s_code);
3384 		}
3385 #endif
3386 #if LIBCURL_VERSION_NUM >= 0x071300 /* Available since 7.19.0 */
3387 		if (curl_easy_getinfo(ch->cp, CURLINFO_PRIMARY_IP, &s_code) == CURLE_OK) {
3388 			CAAS("primary_ip", s_code);
3389 		}
3390 #endif
3391 #if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
3392 		if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
3393 			array_init(&listcode);
3394 			create_certinfo(ci, &listcode);
3395 			CAAZ("certinfo", &listcode);
3396 		}
3397 #endif
3398 #if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
3399 		if (curl_easy_getinfo(ch->cp, CURLINFO_PRIMARY_PORT, &l_code) == CURLE_OK) {
3400 			CAAL("primary_port", l_code);
3401 		}
3402 		if (curl_easy_getinfo(ch->cp, CURLINFO_LOCAL_IP, &s_code) == CURLE_OK) {
3403 			CAAS("local_ip", s_code);
3404 		}
3405 		if (curl_easy_getinfo(ch->cp, CURLINFO_LOCAL_PORT, &l_code) == CURLE_OK) {
3406 			CAAL("local_port", l_code);
3407 		}
3408 #endif
3409 #if LIBCURL_VERSION_NUM >= 0x073200 /* Available since 7.50.0 */
3410 		if (curl_easy_getinfo(ch->cp, CURLINFO_HTTP_VERSION, &l_code) == CURLE_OK) {
3411 			CAAL("http_version", l_code);
3412 		}
3413 #endif
3414 #if LIBCURL_VERSION_NUM >= 0x073400 /* Available since 7.52.0 */
3415 		if (curl_easy_getinfo(ch->cp, CURLINFO_PROTOCOL, &l_code) == CURLE_OK) {
3416 			CAAL("protocol", l_code);
3417 		}
3418 		if (curl_easy_getinfo(ch->cp, CURLINFO_PROXY_SSL_VERIFYRESULT, &l_code) == CURLE_OK) {
3419 			CAAL("ssl_verifyresult", l_code);
3420 		}
3421 		if (curl_easy_getinfo(ch->cp, CURLINFO_SCHEME, &s_code) == CURLE_OK) {
3422 			CAAS("scheme", s_code);
3423 		}
3424 #endif
3425 #if LIBCURL_VERSION_NUM >= 0x073d00 /* Available since 7.61.0 */
3426 		if (curl_easy_getinfo(ch->cp, CURLINFO_APPCONNECT_TIME_T, &co) == CURLE_OK) {
3427 			CAAL("appconnect_time_us", co);
3428 		}
3429 		if (curl_easy_getinfo(ch->cp, CURLINFO_CONNECT_TIME_T, &co) == CURLE_OK) {
3430 			CAAL("connect_time_us", co);
3431 		}
3432 		if (curl_easy_getinfo(ch->cp, CURLINFO_NAMELOOKUP_TIME_T, &co) == CURLE_OK) {
3433 			CAAL("namelookup_time_us", co);
3434 		}
3435 		if (curl_easy_getinfo(ch->cp, CURLINFO_PRETRANSFER_TIME_T, &co) == CURLE_OK) {
3436 			CAAL("pretransfer_time_us", co);
3437 		}
3438 		if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_TIME_T, &co) == CURLE_OK) {
3439 			CAAL("redirect_time_us", co);
3440 		}
3441 		if (curl_easy_getinfo(ch->cp, CURLINFO_STARTTRANSFER_TIME_T, &co) == CURLE_OK) {
3442 			CAAL("starttransfer_time_us", co);
3443 		}
3444 		if (curl_easy_getinfo(ch->cp, CURLINFO_TOTAL_TIME_T, &co) == CURLE_OK) {
3445 			CAAL("total_time_us", co);
3446 		}
3447 #endif
3448 		if (ch->header.str) {
3449 			CAASTR("request_header", ch->header.str);
3450 		}
3451 	} else {
3452 		switch (option) {
3453 			case CURLINFO_HEADER_OUT:
3454 				if (ch->header.str) {
3455 					RETURN_STR_COPY(ch->header.str);
3456 				} else {
3457 					RETURN_FALSE;
3458 				}
3459 #if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
3460 			case CURLINFO_CERTINFO: {
3461 				struct curl_certinfo *ci = NULL;
3462 
3463 				array_init(return_value);
3464 
3465 				if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
3466 					create_certinfo(ci, return_value);
3467 				} else {
3468 					RETURN_FALSE;
3469 				}
3470 				break;
3471 			}
3472 #endif
3473 			default: {
3474 				int type = CURLINFO_TYPEMASK & option;
3475 				switch (type) {
3476 					case CURLINFO_STRING:
3477 					{
3478 						char *s_code = NULL;
3479 
3480 						if (curl_easy_getinfo(ch->cp, option, &s_code) == CURLE_OK && s_code) {
3481 							RETURN_STRING(s_code);
3482 						} else {
3483 							RETURN_FALSE;
3484 						}
3485 						break;
3486 					}
3487 					case CURLINFO_LONG:
3488 					{
3489 						zend_long code = 0;
3490 
3491 						if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
3492 							RETURN_LONG(code);
3493 						} else {
3494 							RETURN_FALSE;
3495 						}
3496 						break;
3497 					}
3498 					case CURLINFO_DOUBLE:
3499 					{
3500 						double code = 0.0;
3501 
3502 						if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
3503 							RETURN_DOUBLE(code);
3504 						} else {
3505 							RETURN_FALSE;
3506 						}
3507 						break;
3508 					}
3509 					case CURLINFO_SLIST:
3510 					{
3511 						struct curl_slist *slist;
3512 						if (curl_easy_getinfo(ch->cp, option, &slist) == CURLE_OK) {
3513 							struct curl_slist *current = slist;
3514 							array_init(return_value);
3515 							while (current) {
3516 								add_next_index_string(return_value, current->data);
3517 								current = current->next;
3518 							}
3519 							curl_slist_free_all(slist);
3520 						} else {
3521 							RETURN_FALSE;
3522 						}
3523 						break;
3524 					}
3525 #if LIBCURL_VERSION_NUM >= 0x073700 /* Available since 7.55.0 */
3526 					case CURLINFO_OFF_T:
3527 					{
3528 						curl_off_t c_off;
3529 						if (curl_easy_getinfo(ch->cp, option, &c_off) == CURLE_OK) {
3530 							RETURN_LONG((long) c_off);
3531 						} else {
3532 							RETURN_FALSE;
3533 						}
3534 						break;
3535 					}
3536 #endif
3537 					default:
3538 						RETURN_FALSE;
3539 				}
3540 			}
3541 		}
3542 	}
3543 }
3544 /* }}} */
3545 
3546 /* {{{ proto string curl_error(resource ch)
3547    Return a string contain the last error for the current session */
PHP_FUNCTION(curl_error)3548 PHP_FUNCTION(curl_error)
3549 {
3550 	zval		*zid;
3551 	php_curl	*ch;
3552 
3553 	ZEND_PARSE_PARAMETERS_START(1, 1)
3554 		Z_PARAM_RESOURCE(zid)
3555 	ZEND_PARSE_PARAMETERS_END();
3556 
3557 	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3558 		RETURN_FALSE;
3559 	}
3560 
3561 	if (ch->err.no) {
3562 		ch->err.str[CURL_ERROR_SIZE] = 0;
3563 		RETURN_STRING(ch->err.str);
3564 	} else {
3565 		RETURN_EMPTY_STRING();
3566 	}
3567 }
3568 /* }}} */
3569 
3570 /* {{{ proto int curl_errno(resource ch)
3571    Return an integer containing the last error number */
PHP_FUNCTION(curl_errno)3572 PHP_FUNCTION(curl_errno)
3573 {
3574 	zval		*zid;
3575 	php_curl	*ch;
3576 
3577 	ZEND_PARSE_PARAMETERS_START(1,1)
3578 		Z_PARAM_RESOURCE(zid)
3579 	ZEND_PARSE_PARAMETERS_END();
3580 
3581 	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3582 		RETURN_FALSE;
3583 	}
3584 
3585 	RETURN_LONG(ch->err.no);
3586 }
3587 /* }}} */
3588 
3589 /* {{{ proto void curl_close(resource ch)
3590    Close a cURL session */
PHP_FUNCTION(curl_close)3591 PHP_FUNCTION(curl_close)
3592 {
3593 	zval		*zid;
3594 	php_curl	*ch;
3595 
3596 	ZEND_PARSE_PARAMETERS_START(1, 1)
3597 		Z_PARAM_RESOURCE(zid)
3598 	ZEND_PARSE_PARAMETERS_END();
3599 
3600 	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3601 		RETURN_FALSE;
3602 	}
3603 
3604 	if (ch->in_callback) {
3605 		php_error_docref(NULL, E_WARNING, "Attempt to close cURL handle from a callback");
3606 		return;
3607 	}
3608 
3609 	zend_list_close(Z_RES_P(zid));
3610 }
3611 /* }}} */
3612 
3613 /* {{{ _php_curl_close_ex()
3614    List destructor for curl handles */
_php_curl_close_ex(php_curl * ch)3615 static void _php_curl_close_ex(php_curl *ch)
3616 {
3617 #if PHP_CURL_DEBUG
3618 	fprintf(stderr, "DTOR CALLED, ch = %x\n", ch);
3619 #endif
3620 
3621 	_php_curl_verify_handlers(ch, 0);
3622 
3623 	/*
3624 	 * Libcurl is doing connection caching. When easy handle is cleaned up,
3625 	 * if the handle was previously used by the curl_multi_api, the connection
3626 	 * remains open un the curl multi handle is cleaned up. Some protocols are
3627 	 * sending content like the FTP one, and libcurl try to use the
3628 	 * WRITEFUNCTION or the HEADERFUNCTION. Since structures used in those
3629 	 * callback are freed, we need to use an other callback to which avoid
3630 	 * segfaults.
3631 	 *
3632 	 * Libcurl commit d021f2e8a00 fix this issue and should be part of 7.28.2
3633 	 */
3634 	if (ch->cp != NULL) {
3635 		curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION, curl_write_nothing);
3636 		curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION, curl_write_nothing);
3637 
3638 		curl_easy_cleanup(ch->cp);
3639 	}
3640 
3641 	/* cURL destructors should be invoked only by last curl handle */
3642 	if (--(*ch->clone) == 0) {
3643 		zend_llist_clean(&ch->to_free->str);
3644 		zend_llist_clean(&ch->to_free->post);
3645 		zend_llist_clean(&ch->to_free->stream);
3646 		zend_hash_destroy(ch->to_free->slist);
3647 		efree(ch->to_free->slist);
3648 		efree(ch->to_free);
3649 		efree(ch->clone);
3650 	}
3651 
3652 	smart_str_free(&ch->handlers->write->buf);
3653 	zval_ptr_dtor(&ch->handlers->write->func_name);
3654 	zval_ptr_dtor(&ch->handlers->read->func_name);
3655 	zval_ptr_dtor(&ch->handlers->write_header->func_name);
3656 	zval_ptr_dtor(&ch->handlers->std_err);
3657 	if (ch->header.str) {
3658 		zend_string_release_ex(ch->header.str, 0);
3659 	}
3660 
3661 	zval_ptr_dtor(&ch->handlers->write_header->stream);
3662 	zval_ptr_dtor(&ch->handlers->write->stream);
3663 	zval_ptr_dtor(&ch->handlers->read->stream);
3664 
3665 	efree(ch->handlers->write);
3666 	efree(ch->handlers->write_header);
3667 	efree(ch->handlers->read);
3668 
3669 	if (ch->handlers->progress) {
3670 		zval_ptr_dtor(&ch->handlers->progress->func_name);
3671 		efree(ch->handlers->progress);
3672 	}
3673 
3674 #if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
3675 	if (ch->handlers->fnmatch) {
3676 		zval_ptr_dtor(&ch->handlers->fnmatch->func_name);
3677 		efree(ch->handlers->fnmatch);
3678 	}
3679 #endif
3680 
3681 	efree(ch->handlers);
3682 #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
3683 	zval_ptr_dtor(&ch->postfields);
3684 #endif
3685 	efree(ch);
3686 }
3687 /* }}} */
3688 
3689 /* {{{ _php_curl_close()
3690    List destructor for curl handles */
_php_curl_close(zend_resource * rsrc)3691 static void _php_curl_close(zend_resource *rsrc)
3692 {
3693 	php_curl *ch = (php_curl *) rsrc->ptr;
3694 	_php_curl_close_ex(ch);
3695 }
3696 /* }}} */
3697 
3698 /* {{{ proto bool curl_strerror(int code)
3699       return string describing error code */
PHP_FUNCTION(curl_strerror)3700 PHP_FUNCTION(curl_strerror)
3701 {
3702 	zend_long code;
3703 	const char *str;
3704 
3705 	ZEND_PARSE_PARAMETERS_START(1, 1)
3706 		Z_PARAM_LONG(code)
3707 	ZEND_PARSE_PARAMETERS_END();
3708 
3709 	str = curl_easy_strerror(code);
3710 	if (str) {
3711 		RETURN_STRING(str);
3712 	} else {
3713 		RETURN_NULL();
3714 	}
3715 }
3716 /* }}} */
3717 
3718 /* {{{ _php_curl_reset_handlers()
3719    Reset all handlers of a given php_curl */
_php_curl_reset_handlers(php_curl * ch)3720 static void _php_curl_reset_handlers(php_curl *ch)
3721 {
3722 	if (!Z_ISUNDEF(ch->handlers->write->stream)) {
3723 		zval_ptr_dtor(&ch->handlers->write->stream);
3724 		ZVAL_UNDEF(&ch->handlers->write->stream);
3725 	}
3726 	ch->handlers->write->fp = NULL;
3727 	ch->handlers->write->method = PHP_CURL_STDOUT;
3728 
3729 	if (!Z_ISUNDEF(ch->handlers->write_header->stream)) {
3730 		zval_ptr_dtor(&ch->handlers->write_header->stream);
3731 		ZVAL_UNDEF(&ch->handlers->write_header->stream);
3732 	}
3733 	ch->handlers->write_header->fp = NULL;
3734 	ch->handlers->write_header->method = PHP_CURL_IGNORE;
3735 
3736 	if (!Z_ISUNDEF(ch->handlers->read->stream)) {
3737 		zval_ptr_dtor(&ch->handlers->read->stream);
3738 		ZVAL_UNDEF(&ch->handlers->read->stream);
3739 	}
3740 	ch->handlers->read->fp = NULL;
3741 	ch->handlers->read->res = NULL;
3742 	ch->handlers->read->method  = PHP_CURL_DIRECT;
3743 
3744 	if (!Z_ISUNDEF(ch->handlers->std_err)) {
3745 		zval_ptr_dtor(&ch->handlers->std_err);
3746 		ZVAL_UNDEF(&ch->handlers->std_err);
3747 	}
3748 
3749 	if (ch->handlers->progress) {
3750 		zval_ptr_dtor(&ch->handlers->progress->func_name);
3751 		efree(ch->handlers->progress);
3752 		ch->handlers->progress = NULL;
3753 	}
3754 
3755 #if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
3756 	if (ch->handlers->fnmatch) {
3757 		zval_ptr_dtor(&ch->handlers->fnmatch->func_name);
3758 		efree(ch->handlers->fnmatch);
3759 		ch->handlers->fnmatch = NULL;
3760 	}
3761 #endif
3762 
3763 }
3764 /* }}} */
3765 
3766 /* {{{ proto void curl_reset(resource ch)
3767    Reset all options of a libcurl session handle */
PHP_FUNCTION(curl_reset)3768 PHP_FUNCTION(curl_reset)
3769 {
3770 	zval       *zid;
3771 	php_curl   *ch;
3772 
3773 	ZEND_PARSE_PARAMETERS_START(1, 1)
3774 		Z_PARAM_RESOURCE(zid)
3775 	ZEND_PARSE_PARAMETERS_END();
3776 
3777 	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3778 		RETURN_FALSE;
3779 	}
3780 
3781 	if (ch->in_callback) {
3782 		php_error_docref(NULL, E_WARNING, "Attempt to reset cURL handle from a callback");
3783 		return;
3784 	}
3785 
3786 	curl_easy_reset(ch->cp);
3787 	_php_curl_reset_handlers(ch);
3788 	_php_curl_set_default_options(ch);
3789 }
3790 /* }}} */
3791 
3792 /* {{{ proto void curl_escape(resource ch, string str)
3793    URL encodes the given string */
PHP_FUNCTION(curl_escape)3794 PHP_FUNCTION(curl_escape)
3795 {
3796 	zend_string *str;
3797 	char        *res;
3798 	zval        *zid;
3799 	php_curl    *ch;
3800 
3801 	ZEND_PARSE_PARAMETERS_START(2,2)
3802 		Z_PARAM_RESOURCE(zid)
3803 		Z_PARAM_STR(str)
3804 	ZEND_PARSE_PARAMETERS_END();
3805 
3806 	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3807 		RETURN_FALSE;
3808 	}
3809 
3810 	if (ZEND_SIZE_T_INT_OVFL(ZSTR_LEN(str))) {
3811 		RETURN_FALSE;
3812 	}
3813 
3814 	if ((res = curl_easy_escape(ch->cp, ZSTR_VAL(str), ZSTR_LEN(str)))) {
3815 		RETVAL_STRING(res);
3816 		curl_free(res);
3817 	} else {
3818 		RETURN_FALSE;
3819 	}
3820 }
3821 /* }}} */
3822 
3823 /* {{{ proto void curl_unescape(resource ch, string str)
3824    URL decodes the given string */
PHP_FUNCTION(curl_unescape)3825 PHP_FUNCTION(curl_unescape)
3826 {
3827 	char        *out = NULL;
3828 	int          out_len;
3829 	zval        *zid;
3830 	zend_string *str;
3831 	php_curl    *ch;
3832 
3833 	ZEND_PARSE_PARAMETERS_START(2,2)
3834 		Z_PARAM_RESOURCE(zid)
3835 		Z_PARAM_STR(str)
3836 	ZEND_PARSE_PARAMETERS_END();
3837 
3838 	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3839 		RETURN_FALSE;
3840 	}
3841 
3842 	if (ZEND_SIZE_T_INT_OVFL(ZSTR_LEN(str))) {
3843 		RETURN_FALSE;
3844 	}
3845 
3846 	if ((out = curl_easy_unescape(ch->cp, ZSTR_VAL(str), ZSTR_LEN(str), &out_len))) {
3847 		RETVAL_STRINGL(out, out_len);
3848 		curl_free(out);
3849 	} else {
3850 		RETURN_FALSE;
3851 	}
3852 }
3853 /* }}} */
3854 
3855 #if LIBCURL_VERSION_NUM >= 0x071200 /* 7.18.0 */
3856 /* {{{ proto void curl_pause(resource ch, int bitmask)
3857        pause and unpause a connection */
PHP_FUNCTION(curl_pause)3858 PHP_FUNCTION(curl_pause)
3859 {
3860 	zend_long       bitmask;
3861 	zval       *zid;
3862 	php_curl   *ch;
3863 
3864 	ZEND_PARSE_PARAMETERS_START(2,2)
3865 		Z_PARAM_RESOURCE(zid)
3866 		Z_PARAM_LONG(bitmask)
3867 	ZEND_PARSE_PARAMETERS_END();
3868 
3869 	if ((ch = (php_curl*)zend_fetch_resource(Z_RES_P(zid), le_curl_name, le_curl)) == NULL) {
3870 		RETURN_FALSE;
3871 	}
3872 
3873 	RETURN_LONG(curl_easy_pause(ch->cp, bitmask));
3874 }
3875 /* }}} */
3876 #endif
3877 
3878 #endif /* HAVE_CURL */
3879