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