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