xref: /PHP-5.3/ext/curl/interface.c (revision a2045ff3)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2013 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 
94 #ifdef PHP_CURL_NEED_OPENSSL_TSL /* {{{ */
95 static MUTEX_T *php_curl_openssl_tsl = NULL;
96 
php_curl_ssl_lock(int mode,int n,const char * file,int line)97 static void php_curl_ssl_lock(int mode, int n, const char * file, int line)
98 {
99 	if (mode & CRYPTO_LOCK) {
100 		tsrm_mutex_lock(php_curl_openssl_tsl[n]);
101 	} else {
102 		tsrm_mutex_unlock(php_curl_openssl_tsl[n]);
103 	}
104 }
105 
php_curl_ssl_id(void)106 static unsigned long php_curl_ssl_id(void)
107 {
108 	return (unsigned long) tsrm_thread_id();
109 }
110 #endif
111 /* }}} */
112 
113 #ifdef PHP_CURL_NEED_GNUTLS_TSL /* {{{ */
php_curl_ssl_mutex_create(void ** m)114 static int php_curl_ssl_mutex_create(void **m)
115 {
116 	if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) {
117 		return SUCCESS;
118 	} else {
119 		return FAILURE;
120 	}
121 }
122 
php_curl_ssl_mutex_destroy(void ** m)123 static int php_curl_ssl_mutex_destroy(void **m)
124 {
125 	tsrm_mutex_free(*((MUTEX_T *) m));
126 	return SUCCESS;
127 }
128 
php_curl_ssl_mutex_lock(void ** m)129 static int php_curl_ssl_mutex_lock(void **m)
130 {
131 	return tsrm_mutex_lock(*((MUTEX_T *) m));
132 }
133 
php_curl_ssl_mutex_unlock(void ** m)134 static int php_curl_ssl_mutex_unlock(void **m)
135 {
136 	return tsrm_mutex_unlock(*((MUTEX_T *) m));
137 }
138 
139 static struct gcry_thread_cbs php_curl_gnutls_tsl = {
140 	GCRY_THREAD_OPTION_USER,
141 	NULL,
142 	php_curl_ssl_mutex_create,
143 	php_curl_ssl_mutex_destroy,
144 	php_curl_ssl_mutex_lock,
145 	php_curl_ssl_mutex_unlock
146 };
147 #endif
148 /* }}} */
149 
150 static void _php_curl_close_ex(php_curl *ch TSRMLS_DC);
151 static void _php_curl_close(zend_rsrc_list_entry *rsrc TSRMLS_DC);
152 
153 
154 #define SAVE_CURL_ERROR(__handle, __err) (__handle)->err.no = (int) __err;
155 
156 #define CAAL(s, v) add_assoc_long_ex(return_value, s, sizeof(s), (long) v);
157 #define CAAD(s, v) add_assoc_double_ex(return_value, s, sizeof(s), (double) v);
158 #define CAAS(s, v) add_assoc_string_ex(return_value, s, sizeof(s), (char *) (v ? v : ""), 1);
159 #define CAAZ(s, v) add_assoc_zval_ex(return_value, s, sizeof(s), (zval *) v);
160 
161 #if defined(PHP_WIN32) || defined(__GNUC__)
162 # define php_curl_ret(__ret) RETVAL_FALSE; return __ret;
163 #else
164 # define php_curl_ret(__ret) RETVAL_FALSE; return;
165 #endif
166 
php_curl_option_url(php_curl * ch,const char * url,const int len)167 static int php_curl_option_url(php_curl *ch, const char *url, const int len) /* {{{ */
168 {
169 	CURLcode error = CURLE_OK;
170 #if LIBCURL_VERSION_NUM < 0x071100
171 	char *copystr = NULL;
172 #endif
173 	TSRMLS_FETCH();
174 
175 	/* Disable file:// if open_basedir or safe_mode are used */
176 	if ((PG(open_basedir) && *PG(open_basedir)) || PG(safe_mode)) {
177 #if LIBCURL_VERSION_NUM >= 0x071304
178 		error = curl_easy_setopt(ch->cp, CURLOPT_PROTOCOLS, CURLPROTO_ALL & ~CURLPROTO_FILE);
179 #else
180 		php_url *uri;
181 
182 		if (!(uri = php_url_parse_ex(url, len))) {
183 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid URL '%s'", url);
184 			return 0;
185 		}
186 
187 		if (uri->scheme && !strncasecmp("file", uri->scheme, sizeof("file"))) {
188 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Protocol 'file' disabled in cURL");
189 			php_url_free(uri);
190 			return 0;
191 		}
192 		php_url_free(uri);
193 #endif
194 	}
195 	/* Strings passed to libcurl as 'char *' arguments, are copied by the library... NOTE: before 7.17.0 strings were not copied. */
196 #if LIBCURL_VERSION_NUM >= 0x071100
197 	error = curl_easy_setopt(ch->cp, CURLOPT_URL, url);
198 #else
199 	copystr = estrndup(url, len);
200 	error = curl_easy_setopt(ch->cp, CURLOPT_URL, copystr);
201 	zend_llist_add_element(&ch->to_free->str, &copystr);
202 #endif
203 
204 	return (error == CURLE_OK ? 1 : 0);
205 }
206 /* }}} */
207 
_php_curl_verify_handlers(php_curl * ch,int reporterror TSRMLS_DC)208 int _php_curl_verify_handlers(php_curl *ch, int reporterror TSRMLS_DC) /* {{{ */
209 {
210 	php_stream *stream;
211 	if (!ch || !ch->handlers) {
212 		return 0;
213 	}
214 
215 	if (ch->handlers->std_err) {
216 		stream = (php_stream *) zend_fetch_resource(&ch->handlers->std_err TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
217 		if (stream == NULL) {
218 			if (reporterror) {
219 				php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_STDERR resource has gone away, resetting to stderr");
220 			}
221 			zval_ptr_dtor(&ch->handlers->std_err);
222 			ch->handlers->std_err = NULL;
223 
224 			curl_easy_setopt(ch->cp, CURLOPT_STDERR, stderr);
225 		}
226 	}
227 	if (ch->handlers->read && ch->handlers->read->stream) {
228 		stream = (php_stream *) zend_fetch_resource(&ch->handlers->read->stream TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
229 		if (stream == NULL) {
230 			if (reporterror) {
231 				php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_INFILE resource has gone away, resetting to default");
232 			}
233 			zval_ptr_dtor(&ch->handlers->read->stream);
234 			ch->handlers->read->fd = 0;
235 			ch->handlers->read->fp = 0;
236 			ch->handlers->read->stream = NULL;
237 
238 			curl_easy_setopt(ch->cp, CURLOPT_INFILE, (void *) ch);
239 		}
240 	}
241 	if (ch->handlers->write_header && ch->handlers->write_header->stream) {
242 		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());
243 		if (stream == NULL) {
244 			if (reporterror) {
245 				php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_WRITEHEADER resource has gone away, resetting to default");
246 			}
247 			zval_ptr_dtor(&ch->handlers->write_header->stream);
248 			ch->handlers->write_header->fp = 0;
249 			ch->handlers->write_header->stream = NULL;
250 
251 			ch->handlers->write_header->method = PHP_CURL_IGNORE;
252 			curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER, (void *) ch);
253 		}
254 	}
255 	if (ch->handlers->write && ch->handlers->write->stream) {
256 		stream = (php_stream *) zend_fetch_resource(&ch->handlers->write->stream TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
257 		if (stream == NULL) {
258 			if (reporterror) {
259 				php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_FILE resource has gone away, resetting to default");
260 			}
261 			zval_ptr_dtor(&ch->handlers->write->stream);
262 			ch->handlers->write->fp = 0;
263 			ch->handlers->write->stream = NULL;
264 
265 			ch->handlers->write->method = PHP_CURL_STDOUT;
266 			ch->handlers->write->type   = PHP_CURL_ASCII;
267 			curl_easy_setopt(ch->cp, CURLOPT_FILE, (void *) ch);
268 		}
269 	}
270 	return 1;
271 }
272 /* }}} */
273 
274 /* {{{ arginfo */
275 ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_version, 0, 0, 0)
276 	ZEND_ARG_INFO(0, version)
277 ZEND_END_ARG_INFO()
278 
279 ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_init, 0, 0, 0)
280 	ZEND_ARG_INFO(0, url)
281 ZEND_END_ARG_INFO()
282 
283 ZEND_BEGIN_ARG_INFO(arginfo_curl_copy_handle, 0)
284 	ZEND_ARG_INFO(0, ch)
285 ZEND_END_ARG_INFO()
286 
287 ZEND_BEGIN_ARG_INFO(arginfo_curl_setopt, 0)
288 	ZEND_ARG_INFO(0, ch)
289 	ZEND_ARG_INFO(0, option)
290 	ZEND_ARG_INFO(0, value)
291 ZEND_END_ARG_INFO()
292 
293 ZEND_BEGIN_ARG_INFO(arginfo_curl_setopt_array, 0)
294 	ZEND_ARG_INFO(0, ch)
295 	ZEND_ARG_ARRAY_INFO(0, options, 0)
296 ZEND_END_ARG_INFO()
297 
298 ZEND_BEGIN_ARG_INFO(arginfo_curl_exec, 0)
299 	ZEND_ARG_INFO(0, ch)
300 ZEND_END_ARG_INFO()
301 
302 ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_getinfo, 0, 0, 1)
303 	ZEND_ARG_INFO(0, ch)
304 	ZEND_ARG_INFO(0, option)
305 ZEND_END_ARG_INFO()
306 
307 ZEND_BEGIN_ARG_INFO(arginfo_curl_error, 0)
308 	ZEND_ARG_INFO(0, ch)
309 ZEND_END_ARG_INFO()
310 
311 ZEND_BEGIN_ARG_INFO(arginfo_curl_errno, 0)
312 	ZEND_ARG_INFO(0, ch)
313 ZEND_END_ARG_INFO()
314 
315 ZEND_BEGIN_ARG_INFO(arginfo_curl_close, 0)
316 	ZEND_ARG_INFO(0, ch)
317 ZEND_END_ARG_INFO()
318 
319 ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_init, 0)
320 ZEND_END_ARG_INFO()
321 
322 ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_add_handle, 0)
323 	ZEND_ARG_INFO(0, mh)
324 	ZEND_ARG_INFO(0, ch)
325 ZEND_END_ARG_INFO()
326 
327 ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_remove_handle, 0)
328 	ZEND_ARG_INFO(0, mh)
329 	ZEND_ARG_INFO(0, ch)
330 ZEND_END_ARG_INFO()
331 
332 ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_multi_select, 0, 0, 1)
333 	ZEND_ARG_INFO(0, mh)
334 	ZEND_ARG_INFO(0, timeout)
335 ZEND_END_ARG_INFO()
336 
337 ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_multi_exec, 0, 0, 1)
338 	ZEND_ARG_INFO(0, mh)
339 	ZEND_ARG_INFO(1, still_running)
340 ZEND_END_ARG_INFO()
341 
342 ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_getcontent, 0)
343 	ZEND_ARG_INFO(0, ch)
344 ZEND_END_ARG_INFO()
345 
346 ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_multi_info_read, 0, 0, 1)
347 	ZEND_ARG_INFO(0, mh)
348 	ZEND_ARG_INFO(1, msgs_in_queue)
349 ZEND_END_ARG_INFO()
350 
351 ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_close, 0)
352 	ZEND_ARG_INFO(0, mh)
353 ZEND_END_ARG_INFO()
354 /* }}} */
355 
356 /* {{{ curl_functions[]
357  */
358 const zend_function_entry curl_functions[] = {
359 	PHP_FE(curl_init,                arginfo_curl_init)
360 	PHP_FE(curl_copy_handle,         arginfo_curl_copy_handle)
361 	PHP_FE(curl_version,             arginfo_curl_version)
362 	PHP_FE(curl_setopt,              arginfo_curl_setopt)
363 	PHP_FE(curl_setopt_array,        arginfo_curl_setopt_array)
364 	PHP_FE(curl_exec,                arginfo_curl_exec)
365 	PHP_FE(curl_getinfo,             arginfo_curl_getinfo)
366 	PHP_FE(curl_error,               arginfo_curl_error)
367 	PHP_FE(curl_errno,               arginfo_curl_errno)
368 	PHP_FE(curl_close,               arginfo_curl_close)
369 	PHP_FE(curl_multi_init,          arginfo_curl_multi_init)
370 	PHP_FE(curl_multi_add_handle,    arginfo_curl_multi_add_handle)
371 	PHP_FE(curl_multi_remove_handle, arginfo_curl_multi_remove_handle)
372 	PHP_FE(curl_multi_select,        arginfo_curl_multi_select)
373 	PHP_FE(curl_multi_exec,          arginfo_curl_multi_exec)
374 	PHP_FE(curl_multi_getcontent,    arginfo_curl_multi_getcontent)
375 	PHP_FE(curl_multi_info_read,     arginfo_curl_multi_info_read)
376 	PHP_FE(curl_multi_close,         arginfo_curl_multi_close)
377 	PHP_FE_END
378 };
379 /* }}} */
380 
381 /* {{{ curl_module_entry
382  */
383 zend_module_entry curl_module_entry = {
384 	STANDARD_MODULE_HEADER,
385 	"curl",
386 	curl_functions,
387 	PHP_MINIT(curl),
388 	PHP_MSHUTDOWN(curl),
389 	NULL,
390 	NULL,
391 	PHP_MINFO(curl),
392 	NO_VERSION_YET,
393 	STANDARD_MODULE_PROPERTIES
394 };
395 /* }}} */
396 
397 #ifdef COMPILE_DL_CURL
398 ZEND_GET_MODULE (curl)
399 #endif
400 
401 /* {{{ PHP_INI_BEGIN */
PHP_INI_BEGIN()402 PHP_INI_BEGIN()
403 	PHP_INI_ENTRY("curl.cainfo", "", PHP_INI_SYSTEM, NULL)
404 PHP_INI_END()
405 /* }}} */
406 
407 /* {{{ PHP_MINFO_FUNCTION
408  */
409 PHP_MINFO_FUNCTION(curl)
410 {
411 	curl_version_info_data *d;
412 	char **p;
413 	char str[1024];
414 	size_t n = 0;
415 
416 	d = curl_version_info(CURLVERSION_NOW);
417 	php_info_print_table_start();
418 	php_info_print_table_row(2, "cURL support",    "enabled");
419 	php_info_print_table_row(2, "cURL Information", d->version);
420 	sprintf(str, "%d", d->age);
421 	php_info_print_table_row(2, "Age", str);
422 
423 	/* To update on each new cURL release using src/main.c in cURL sources */
424 	if (d->features) {
425 		struct feat {
426 			const char *name;
427 			int bitmask;
428 		};
429 
430 		unsigned int i;
431 
432 		static const struct feat feats[] = {
433 #if LIBCURL_VERSION_NUM > 0x070a06 /* 7.10.7 */
434 			{"AsynchDNS", CURL_VERSION_ASYNCHDNS},
435 #endif
436 #if LIBCURL_VERSION_NUM > 0x070a05 /* 7.10.6 */
437 			{"Debug", CURL_VERSION_DEBUG},
438 			{"GSS-Negotiate", CURL_VERSION_GSSNEGOTIATE},
439 #endif
440 #if LIBCURL_VERSION_NUM > 0x070b02 /* 7.12.0 */
441 			{"IDN", CURL_VERSION_IDN},
442 #endif
443 #ifdef CURL_VERSION_IPV6
444 			{"IPv6", CURL_VERSION_IPV6},
445 #endif
446 #if LIBCURL_VERSION_NUM > 0x070b00 /* 7.11.1 */
447 			{"Largefile", CURL_VERSION_LARGEFILE},
448 #endif
449 #if LIBCURL_VERSION_NUM > 0x070a05 /* 7.10.6 */
450 			{"NTLM", CURL_VERSION_NTLM},
451 #endif
452 #if LIBCURL_VERSION_NUM > 0x070a07 /* 7.10.8 */
453 			{"SPNEGO", CURL_VERSION_SPNEGO},
454 #endif
455 #ifdef CURL_VERSION_SSL
456 			{"SSL",  CURL_VERSION_SSL},
457 #endif
458 #if LIBCURL_VERSION_NUM > 0x070d01 /* 7.13.2 */
459 			{"SSPI",  CURL_VERSION_SSPI},
460 #endif
461 #ifdef CURL_VERSION_KERBEROS4
462 			{"krb4", CURL_VERSION_KERBEROS4},
463 #endif
464 #ifdef CURL_VERSION_LIBZ
465 			{"libz", CURL_VERSION_LIBZ},
466 #endif
467 #if LIBCURL_VERSION_NUM > 0x070f03 /* 7.15.4 */
468 			{"CharConv", CURL_VERSION_CONV},
469 #endif
470 			{NULL, 0}
471 		};
472 
473 		php_info_print_table_row(1, "Features");
474 		for(i=0; i<sizeof(feats)/sizeof(feats[0]); i++) {
475 			if (feats[i].name) {
476 				php_info_print_table_row(2, feats[i].name, d->features & feats[i].bitmask ? "Yes" : "No");
477 			}
478 		}
479 	}
480 
481 	n = 0;
482 	p = (char **) d->protocols;
483 	while (*p != NULL) {
484 			n += sprintf(str + n, "%s%s", *p, *(p + 1) != NULL ? ", " : "");
485 			p++;
486 	}
487 	php_info_print_table_row(2, "Protocols", str);
488 
489 	php_info_print_table_row(2, "Host", d->host);
490 
491 	if (d->ssl_version) {
492 		php_info_print_table_row(2, "SSL Version", d->ssl_version);
493 	}
494 
495 	if (d->libz_version) {
496 		php_info_print_table_row(2, "ZLib Version", d->libz_version);
497 	}
498 
499 #if defined(CURLVERSION_SECOND) && CURLVERSION_NOW >= CURLVERSION_SECOND
500 	if (d->ares) {
501 		php_info_print_table_row(2, "ZLib Version", d->ares);
502 	}
503 #endif
504 
505 #if defined(CURLVERSION_THIRD) && CURLVERSION_NOW >= CURLVERSION_THIRD
506 	if (d->libidn) {
507 		php_info_print_table_row(2, "libIDN Version", d->libidn);
508 	}
509 #endif
510 
511 #if LIBCURL_VERSION_NUM >= 0x071300
512 
513 	if (d->iconv_ver_num) {
514 		php_info_print_table_row(2, "IconV Version", d->iconv_ver_num);
515 	}
516 
517 	if (d->libssh_version) {
518 		php_info_print_table_row(2, "libSSH Version", d->libssh_version);
519 	}
520 #endif
521 	php_info_print_table_end();
522 }
523 /* }}} */
524 
525 #define REGISTER_CURL_CONSTANT(__c) REGISTER_LONG_CONSTANT(#__c, __c, CONST_CS | CONST_PERSISTENT)
526 
527 /* {{{ PHP_MINIT_FUNCTION
528  */
PHP_MINIT_FUNCTION(curl)529 PHP_MINIT_FUNCTION(curl)
530 {
531 	le_curl = zend_register_list_destructors_ex(_php_curl_close, NULL, "curl", module_number);
532 	le_curl_multi_handle = zend_register_list_destructors_ex(_php_curl_multi_close, NULL, "curl_multi", module_number);
533 
534 	REGISTER_INI_ENTRIES();
535 
536 	/* See http://curl.haxx.se/lxr/source/docs/libcurl/symbols-in-versions
537 	   or curl src/docs/libcurl/symbols-in-versions for a (almost) complete list
538 	   of options and which version they were introduced */
539 
540 	/* Constants for curl_setopt() */
541 #if LIBCURL_VERSION_NUM > 0x070a07 /* CURLOPT_IPRESOLVE is available since curl 7.10.8 */
542 	REGISTER_CURL_CONSTANT(CURLOPT_IPRESOLVE);
543 	REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_WHATEVER);
544 	REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_V4);
545 	REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_V6);
546 #endif
547 	REGISTER_CURL_CONSTANT(CURLOPT_DNS_USE_GLOBAL_CACHE);
548 	REGISTER_CURL_CONSTANT(CURLOPT_DNS_CACHE_TIMEOUT);
549 	REGISTER_CURL_CONSTANT(CURLOPT_PORT);
550 	REGISTER_CURL_CONSTANT(CURLOPT_FILE);
551 	REGISTER_CURL_CONSTANT(CURLOPT_READDATA);
552 	REGISTER_CURL_CONSTANT(CURLOPT_INFILE);
553 	REGISTER_CURL_CONSTANT(CURLOPT_INFILESIZE);
554 	REGISTER_CURL_CONSTANT(CURLOPT_URL);
555 	REGISTER_CURL_CONSTANT(CURLOPT_PROXY);
556 	REGISTER_CURL_CONSTANT(CURLOPT_VERBOSE);
557 	REGISTER_CURL_CONSTANT(CURLOPT_HEADER);
558 	REGISTER_CURL_CONSTANT(CURLOPT_HTTPHEADER);
559 	REGISTER_CURL_CONSTANT(CURLOPT_NOPROGRESS);
560 	REGISTER_CURL_CONSTANT(CURLOPT_PROGRESSFUNCTION);
561 	REGISTER_CURL_CONSTANT(CURLOPT_NOBODY);
562 	REGISTER_CURL_CONSTANT(CURLOPT_FAILONERROR);
563 	REGISTER_CURL_CONSTANT(CURLOPT_UPLOAD);
564 	REGISTER_CURL_CONSTANT(CURLOPT_POST);
565 	REGISTER_CURL_CONSTANT(CURLOPT_FTPLISTONLY);
566 	REGISTER_CURL_CONSTANT(CURLOPT_FTPAPPEND);
567 	REGISTER_CURL_CONSTANT(CURLOPT_NETRC);
568 	REGISTER_CURL_CONSTANT(CURLOPT_FOLLOWLOCATION);
569 #if CURLOPT_FTPASCII != 0
570 	REGISTER_CURL_CONSTANT(CURLOPT_FTPASCII);
571 #endif
572 	REGISTER_CURL_CONSTANT(CURLOPT_PUT);
573 #if CURLOPT_MUTE != 0
574 	REGISTER_CURL_CONSTANT(CURLOPT_MUTE);
575 #endif
576 	REGISTER_CURL_CONSTANT(CURLOPT_USERPWD);
577 	REGISTER_CURL_CONSTANT(CURLOPT_PROXYUSERPWD);
578 	REGISTER_CURL_CONSTANT(CURLOPT_RANGE);
579 	REGISTER_CURL_CONSTANT(CURLOPT_TIMEOUT);
580 #if LIBCURL_VERSION_NUM > 0x071002
581 	REGISTER_CURL_CONSTANT(CURLOPT_TIMEOUT_MS);
582 #endif
583 	REGISTER_CURL_CONSTANT(CURLOPT_POSTFIELDS);
584 	REGISTER_CURL_CONSTANT(CURLOPT_REFERER);
585 	REGISTER_CURL_CONSTANT(CURLOPT_USERAGENT);
586 	REGISTER_CURL_CONSTANT(CURLOPT_FTPPORT);
587 	REGISTER_CURL_CONSTANT(CURLOPT_FTP_USE_EPSV);
588 	REGISTER_CURL_CONSTANT(CURLOPT_LOW_SPEED_LIMIT);
589 	REGISTER_CURL_CONSTANT(CURLOPT_LOW_SPEED_TIME);
590 	REGISTER_CURL_CONSTANT(CURLOPT_RESUME_FROM);
591 	REGISTER_CURL_CONSTANT(CURLOPT_COOKIE);
592 	REGISTER_CURL_CONSTANT(CURLOPT_COOKIESESSION);
593 	REGISTER_CURL_CONSTANT(CURLOPT_AUTOREFERER);
594 	REGISTER_CURL_CONSTANT(CURLOPT_SSLCERT);
595 	REGISTER_CURL_CONSTANT(CURLOPT_SSLCERTPASSWD);
596 	REGISTER_CURL_CONSTANT(CURLOPT_WRITEHEADER);
597 	REGISTER_CURL_CONSTANT(CURLOPT_SSL_VERIFYHOST);
598 	REGISTER_CURL_CONSTANT(CURLOPT_COOKIEFILE);
599 	REGISTER_CURL_CONSTANT(CURLOPT_SSLVERSION);
600 	REGISTER_CURL_CONSTANT(CURLOPT_TIMECONDITION);
601 	REGISTER_CURL_CONSTANT(CURLOPT_TIMEVALUE);
602 	REGISTER_CURL_CONSTANT(CURLOPT_CUSTOMREQUEST);
603 	REGISTER_CURL_CONSTANT(CURLOPT_STDERR);
604 	REGISTER_CURL_CONSTANT(CURLOPT_TRANSFERTEXT);
605 	REGISTER_CURL_CONSTANT(CURLOPT_RETURNTRANSFER);
606 	REGISTER_CURL_CONSTANT(CURLOPT_QUOTE);
607 	REGISTER_CURL_CONSTANT(CURLOPT_POSTQUOTE);
608 	REGISTER_CURL_CONSTANT(CURLOPT_INTERFACE);
609 	REGISTER_CURL_CONSTANT(CURLOPT_KRB4LEVEL);
610 	REGISTER_CURL_CONSTANT(CURLOPT_HTTPPROXYTUNNEL);
611 	REGISTER_CURL_CONSTANT(CURLOPT_FILETIME);
612 	REGISTER_CURL_CONSTANT(CURLOPT_WRITEFUNCTION);
613 	REGISTER_CURL_CONSTANT(CURLOPT_READFUNCTION);
614 #if CURLOPT_PASSWDFUNCTION != 0
615 	REGISTER_CURL_CONSTANT(CURLOPT_PASSWDFUNCTION);
616 #endif
617 	REGISTER_CURL_CONSTANT(CURLOPT_HEADERFUNCTION);
618 	REGISTER_CURL_CONSTANT(CURLOPT_MAXREDIRS);
619 	REGISTER_CURL_CONSTANT(CURLOPT_MAXCONNECTS);
620 	REGISTER_CURL_CONSTANT(CURLOPT_CLOSEPOLICY);
621 	REGISTER_CURL_CONSTANT(CURLOPT_FRESH_CONNECT);
622 	REGISTER_CURL_CONSTANT(CURLOPT_FORBID_REUSE);
623 	REGISTER_CURL_CONSTANT(CURLOPT_RANDOM_FILE);
624 	REGISTER_CURL_CONSTANT(CURLOPT_EGDSOCKET);
625 	REGISTER_CURL_CONSTANT(CURLOPT_CONNECTTIMEOUT);
626 #if LIBCURL_VERSION_NUM > 0x071002
627 	REGISTER_CURL_CONSTANT(CURLOPT_CONNECTTIMEOUT_MS);
628 #endif
629 	REGISTER_CURL_CONSTANT(CURLOPT_SSL_VERIFYPEER);
630 	REGISTER_CURL_CONSTANT(CURLOPT_CAINFO);
631 	REGISTER_CURL_CONSTANT(CURLOPT_CAPATH);
632 	REGISTER_CURL_CONSTANT(CURLOPT_COOKIEJAR);
633 	REGISTER_CURL_CONSTANT(CURLOPT_SSL_CIPHER_LIST);
634 	REGISTER_CURL_CONSTANT(CURLOPT_BINARYTRANSFER);
635 	REGISTER_CURL_CONSTANT(CURLOPT_NOSIGNAL);
636 	REGISTER_CURL_CONSTANT(CURLOPT_PROXYTYPE);
637 	REGISTER_CURL_CONSTANT(CURLOPT_BUFFERSIZE);
638 	REGISTER_CURL_CONSTANT(CURLOPT_HTTPGET);
639 	REGISTER_CURL_CONSTANT(CURLOPT_HTTP_VERSION);
640 	REGISTER_CURL_CONSTANT(CURLOPT_SSLKEY);
641 	REGISTER_CURL_CONSTANT(CURLOPT_SSLKEYTYPE);
642 	REGISTER_CURL_CONSTANT(CURLOPT_SSLKEYPASSWD);
643 	REGISTER_CURL_CONSTANT(CURLOPT_SSLENGINE);
644 	REGISTER_CURL_CONSTANT(CURLOPT_SSLENGINE_DEFAULT);
645 	REGISTER_CURL_CONSTANT(CURLOPT_SSLCERTTYPE);
646 	REGISTER_CURL_CONSTANT(CURLOPT_CRLF);
647 	REGISTER_CURL_CONSTANT(CURLOPT_ENCODING);
648 	REGISTER_CURL_CONSTANT(CURLOPT_PROXYPORT);
649 	REGISTER_CURL_CONSTANT(CURLOPT_UNRESTRICTED_AUTH);
650 	REGISTER_CURL_CONSTANT(CURLOPT_FTP_USE_EPRT);
651 #if LIBCURL_VERSION_NUM > 0x070b01 /* CURLOPT_TCP_NODELAY is available since curl 7.11.2 */
652 	REGISTER_CURL_CONSTANT(CURLOPT_TCP_NODELAY);
653 #endif
654 	REGISTER_CURL_CONSTANT(CURLOPT_HTTP200ALIASES);
655 	REGISTER_CURL_CONSTANT(CURL_TIMECOND_IFMODSINCE);
656 	REGISTER_CURL_CONSTANT(CURL_TIMECOND_IFUNMODSINCE);
657 	REGISTER_CURL_CONSTANT(CURL_TIMECOND_LASTMOD);
658 
659 #if LIBCURL_VERSION_NUM > 0x070f04 /* CURLOPT_MAX_RECV_SPEED_LARGE & CURLOPT_MAX_SEND_SPEED_LARGE  are available since curl 7.15.5 */
660 	REGISTER_CURL_CONSTANT(CURLOPT_MAX_RECV_SPEED_LARGE);
661 	REGISTER_CURL_CONSTANT(CURLOPT_MAX_SEND_SPEED_LARGE);
662 #endif
663 
664 #if LIBCURL_VERSION_NUM > 0x070a05 /* CURLOPT_HTTPAUTH is available since curl 7.10.6 */
665 	REGISTER_CURL_CONSTANT(CURLOPT_HTTPAUTH);
666 	/* http authentication options */
667 	REGISTER_CURL_CONSTANT(CURLAUTH_BASIC);
668 	REGISTER_CURL_CONSTANT(CURLAUTH_DIGEST);
669 	REGISTER_CURL_CONSTANT(CURLAUTH_GSSNEGOTIATE);
670 	REGISTER_CURL_CONSTANT(CURLAUTH_NTLM);
671 	REGISTER_CURL_CONSTANT(CURLAUTH_ANY);
672 	REGISTER_CURL_CONSTANT(CURLAUTH_ANYSAFE);
673 #endif
674 
675 #if LIBCURL_VERSION_NUM > 0x070a06 /* CURLOPT_PROXYAUTH & CURLOPT_FTP_CREATE_MISSING_DIRS are available since curl 7.10.7 */
676 	REGISTER_CURL_CONSTANT(CURLOPT_PROXYAUTH);
677 	REGISTER_CURL_CONSTANT(CURLOPT_FTP_CREATE_MISSING_DIRS);
678 #endif
679 
680 	REGISTER_CURL_CONSTANT(CURLOPT_PRIVATE);
681 
682 	/* Constants effecting the way CURLOPT_CLOSEPOLICY works */
683 	REGISTER_CURL_CONSTANT(CURLCLOSEPOLICY_LEAST_RECENTLY_USED);
684 	REGISTER_CURL_CONSTANT(CURLCLOSEPOLICY_LEAST_TRAFFIC);
685 	REGISTER_CURL_CONSTANT(CURLCLOSEPOLICY_SLOWEST);
686 	REGISTER_CURL_CONSTANT(CURLCLOSEPOLICY_CALLBACK);
687 	REGISTER_CURL_CONSTANT(CURLCLOSEPOLICY_OLDEST);
688 
689 	/* Info constants */
690 	REGISTER_CURL_CONSTANT(CURLINFO_EFFECTIVE_URL);
691 	REGISTER_CURL_CONSTANT(CURLINFO_HTTP_CODE);
692 	REGISTER_CURL_CONSTANT(CURLINFO_HEADER_SIZE);
693 	REGISTER_CURL_CONSTANT(CURLINFO_REQUEST_SIZE);
694 	REGISTER_CURL_CONSTANT(CURLINFO_TOTAL_TIME);
695 	REGISTER_CURL_CONSTANT(CURLINFO_NAMELOOKUP_TIME);
696 	REGISTER_CURL_CONSTANT(CURLINFO_CONNECT_TIME);
697 	REGISTER_CURL_CONSTANT(CURLINFO_PRETRANSFER_TIME);
698 	REGISTER_CURL_CONSTANT(CURLINFO_SIZE_UPLOAD);
699 	REGISTER_CURL_CONSTANT(CURLINFO_SIZE_DOWNLOAD);
700 	REGISTER_CURL_CONSTANT(CURLINFO_SPEED_DOWNLOAD);
701 	REGISTER_CURL_CONSTANT(CURLINFO_SPEED_UPLOAD);
702 	REGISTER_CURL_CONSTANT(CURLINFO_FILETIME);
703 	REGISTER_CURL_CONSTANT(CURLINFO_SSL_VERIFYRESULT);
704 	REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_LENGTH_DOWNLOAD);
705 	REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_LENGTH_UPLOAD);
706 	REGISTER_CURL_CONSTANT(CURLINFO_STARTTRANSFER_TIME);
707 	REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_TYPE);
708 	REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_TIME);
709 	REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_COUNT);
710 	REGISTER_CURL_CONSTANT(CURLINFO_HEADER_OUT);
711 	REGISTER_CURL_CONSTANT(CURLINFO_PRIVATE);
712 #if LIBCURL_VERSION_NUM >  0x071301
713 	REGISTER_CURL_CONSTANT(CURLINFO_CERTINFO);
714 #endif
715 #if LIBCURL_VERSION_NUM >= 0x071202
716     REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_URL);
717 #endif
718 
719 
720 	/* cURL protocol constants (curl_version) */
721 	REGISTER_CURL_CONSTANT(CURL_VERSION_IPV6);
722 	REGISTER_CURL_CONSTANT(CURL_VERSION_KERBEROS4);
723 	REGISTER_CURL_CONSTANT(CURL_VERSION_SSL);
724 	REGISTER_CURL_CONSTANT(CURL_VERSION_LIBZ);
725 
726 	/* version constants */
727 	REGISTER_CURL_CONSTANT(CURLVERSION_NOW);
728 
729 	/* Error Constants */
730 	REGISTER_CURL_CONSTANT(CURLE_OK);
731 	REGISTER_CURL_CONSTANT(CURLE_UNSUPPORTED_PROTOCOL);
732 	REGISTER_CURL_CONSTANT(CURLE_FAILED_INIT);
733 	REGISTER_CURL_CONSTANT(CURLE_URL_MALFORMAT);
734 	REGISTER_CURL_CONSTANT(CURLE_URL_MALFORMAT_USER);
735 	REGISTER_CURL_CONSTANT(CURLE_COULDNT_RESOLVE_PROXY);
736 	REGISTER_CURL_CONSTANT(CURLE_COULDNT_RESOLVE_HOST);
737 	REGISTER_CURL_CONSTANT(CURLE_COULDNT_CONNECT);
738 	REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_SERVER_REPLY);
739 	REGISTER_CURL_CONSTANT(CURLE_FTP_ACCESS_DENIED);
740 	REGISTER_CURL_CONSTANT(CURLE_FTP_USER_PASSWORD_INCORRECT);
741 	REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_PASS_REPLY);
742 	REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_USER_REPLY);
743 	REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_PASV_REPLY);
744 	REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_227_FORMAT);
745 	REGISTER_CURL_CONSTANT(CURLE_FTP_CANT_GET_HOST);
746 	REGISTER_CURL_CONSTANT(CURLE_FTP_CANT_RECONNECT);
747 	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_SET_BINARY);
748 	REGISTER_CURL_CONSTANT(CURLE_PARTIAL_FILE);
749 	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_RETR_FILE);
750 	REGISTER_CURL_CONSTANT(CURLE_FTP_WRITE_ERROR);
751 	REGISTER_CURL_CONSTANT(CURLE_FTP_QUOTE_ERROR);
752 	REGISTER_CURL_CONSTANT(CURLE_HTTP_NOT_FOUND);
753 	REGISTER_CURL_CONSTANT(CURLE_WRITE_ERROR);
754 	REGISTER_CURL_CONSTANT(CURLE_MALFORMAT_USER);
755 	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_STOR_FILE);
756 	REGISTER_CURL_CONSTANT(CURLE_READ_ERROR);
757 	REGISTER_CURL_CONSTANT(CURLE_OUT_OF_MEMORY);
758 	REGISTER_CURL_CONSTANT(CURLE_OPERATION_TIMEOUTED);
759 	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_SET_ASCII);
760 	REGISTER_CURL_CONSTANT(CURLE_FTP_PORT_FAILED);
761 	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_USE_REST);
762 	REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_GET_SIZE);
763 	REGISTER_CURL_CONSTANT(CURLE_HTTP_RANGE_ERROR);
764 	REGISTER_CURL_CONSTANT(CURLE_HTTP_POST_ERROR);
765 	REGISTER_CURL_CONSTANT(CURLE_SSL_CONNECT_ERROR);
766 	REGISTER_CURL_CONSTANT(CURLE_FTP_BAD_DOWNLOAD_RESUME);
767 	REGISTER_CURL_CONSTANT(CURLE_FILE_COULDNT_READ_FILE);
768 	REGISTER_CURL_CONSTANT(CURLE_LDAP_CANNOT_BIND);
769 	REGISTER_CURL_CONSTANT(CURLE_LDAP_SEARCH_FAILED);
770 	REGISTER_CURL_CONSTANT(CURLE_LIBRARY_NOT_FOUND);
771 	REGISTER_CURL_CONSTANT(CURLE_FUNCTION_NOT_FOUND);
772 	REGISTER_CURL_CONSTANT(CURLE_ABORTED_BY_CALLBACK);
773 	REGISTER_CURL_CONSTANT(CURLE_BAD_FUNCTION_ARGUMENT);
774 	REGISTER_CURL_CONSTANT(CURLE_BAD_CALLING_ORDER);
775 	REGISTER_CURL_CONSTANT(CURLE_HTTP_PORT_FAILED);
776 	REGISTER_CURL_CONSTANT(CURLE_BAD_PASSWORD_ENTERED);
777 	REGISTER_CURL_CONSTANT(CURLE_TOO_MANY_REDIRECTS);
778 	REGISTER_CURL_CONSTANT(CURLE_UNKNOWN_TELNET_OPTION);
779 	REGISTER_CURL_CONSTANT(CURLE_TELNET_OPTION_SYNTAX);
780 	REGISTER_CURL_CONSTANT(CURLE_OBSOLETE);
781 	REGISTER_CURL_CONSTANT(CURLE_SSL_PEER_CERTIFICATE);
782 	REGISTER_CURL_CONSTANT(CURLE_GOT_NOTHING);
783 	REGISTER_CURL_CONSTANT(CURLE_SSL_ENGINE_NOTFOUND);
784 	REGISTER_CURL_CONSTANT(CURLE_SSL_ENGINE_SETFAILED);
785 	REGISTER_CURL_CONSTANT(CURLE_SEND_ERROR);
786 	REGISTER_CURL_CONSTANT(CURLE_RECV_ERROR);
787 	REGISTER_CURL_CONSTANT(CURLE_SHARE_IN_USE);
788 	REGISTER_CURL_CONSTANT(CURLE_SSL_CERTPROBLEM);
789 	REGISTER_CURL_CONSTANT(CURLE_SSL_CIPHER);
790 	REGISTER_CURL_CONSTANT(CURLE_SSL_CACERT);
791 	REGISTER_CURL_CONSTANT(CURLE_BAD_CONTENT_ENCODING);
792 #if LIBCURL_VERSION_NUM >= 0x070a08
793 	REGISTER_CURL_CONSTANT(CURLE_LDAP_INVALID_URL);
794 	REGISTER_CURL_CONSTANT(CURLE_FILESIZE_EXCEEDED);
795 #endif
796 #if LIBCURL_VERSION_NUM >= 0x070b00
797 	REGISTER_CURL_CONSTANT(CURLE_FTP_SSL_FAILED);
798 #endif
799 	REGISTER_CURL_CONSTANT(CURLPROXY_HTTP);
800 	REGISTER_CURL_CONSTANT(CURLPROXY_SOCKS4);
801 	REGISTER_CURL_CONSTANT(CURLPROXY_SOCKS5);
802 
803 	REGISTER_CURL_CONSTANT(CURL_NETRC_OPTIONAL);
804 	REGISTER_CURL_CONSTANT(CURL_NETRC_IGNORED);
805 	REGISTER_CURL_CONSTANT(CURL_NETRC_REQUIRED);
806 
807 	REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_NONE);
808 	REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_1_0);
809 	REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_1_1);
810 
811 	REGISTER_CURL_CONSTANT(CURLM_CALL_MULTI_PERFORM);
812 	REGISTER_CURL_CONSTANT(CURLM_OK);
813 	REGISTER_CURL_CONSTANT(CURLM_BAD_HANDLE);
814 	REGISTER_CURL_CONSTANT(CURLM_BAD_EASY_HANDLE);
815 	REGISTER_CURL_CONSTANT(CURLM_OUT_OF_MEMORY);
816 	REGISTER_CURL_CONSTANT(CURLM_INTERNAL_ERROR);
817 
818 	REGISTER_CURL_CONSTANT(CURLMSG_DONE);
819 
820 #if LIBCURL_VERSION_NUM >= 0x070c02
821 	REGISTER_CURL_CONSTANT(CURLOPT_FTPSSLAUTH);
822 	REGISTER_CURL_CONSTANT(CURLFTPAUTH_DEFAULT);
823 	REGISTER_CURL_CONSTANT(CURLFTPAUTH_SSL);
824 	REGISTER_CURL_CONSTANT(CURLFTPAUTH_TLS);
825 #endif
826 
827 #if LIBCURL_VERSION_NUM >  0x070b00
828 	REGISTER_CURL_CONSTANT(CURLOPT_FTP_SSL);
829 	REGISTER_CURL_CONSTANT(CURLFTPSSL_NONE);
830 	REGISTER_CURL_CONSTANT(CURLFTPSSL_TRY);
831 	REGISTER_CURL_CONSTANT(CURLFTPSSL_CONTROL);
832 	REGISTER_CURL_CONSTANT(CURLFTPSSL_ALL);
833 #endif
834 
835 #if LIBCURL_VERSION_NUM > 0x071301
836 	REGISTER_CURL_CONSTANT(CURLOPT_CERTINFO);
837 	REGISTER_CURL_CONSTANT(CURLOPT_POSTREDIR);
838 #endif
839 
840 /* SSH support works in 7.19.0+ using libssh2 */
841 #if LIBCURL_VERSION_NUM >= 0x071300
842 	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_NONE);
843 	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_PUBLICKEY);
844 	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_PASSWORD);
845 	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_HOST);
846 	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_KEYBOARD);
847 	REGISTER_CURL_CONSTANT(CURLSSH_AUTH_DEFAULT);
848 	REGISTER_CURL_CONSTANT(CURLOPT_SSH_AUTH_TYPES);
849 	REGISTER_CURL_CONSTANT(CURLOPT_KEYPASSWD);
850 	REGISTER_CURL_CONSTANT(CURLOPT_SSH_PUBLIC_KEYFILE);
851 	REGISTER_CURL_CONSTANT(CURLOPT_SSH_PRIVATE_KEYFILE);
852 	REGISTER_CURL_CONSTANT(CURLOPT_SSH_HOST_PUBLIC_KEY_MD5);
853 	REGISTER_CURL_CONSTANT(CURLE_SSH);
854 #endif
855 
856 #if LIBCURL_VERSION_NUM >= 0x071304
857 	REGISTER_CURL_CONSTANT(CURLOPT_REDIR_PROTOCOLS);
858 	REGISTER_CURL_CONSTANT(CURLOPT_PROTOCOLS);
859 	REGISTER_CURL_CONSTANT(CURLPROTO_HTTP);
860 	REGISTER_CURL_CONSTANT(CURLPROTO_HTTPS);
861 	REGISTER_CURL_CONSTANT(CURLPROTO_FTP);
862 	REGISTER_CURL_CONSTANT(CURLPROTO_FTPS);
863 	REGISTER_CURL_CONSTANT(CURLPROTO_SCP);
864 	REGISTER_CURL_CONSTANT(CURLPROTO_SFTP);
865 	REGISTER_CURL_CONSTANT(CURLPROTO_TELNET);
866 	REGISTER_CURL_CONSTANT(CURLPROTO_LDAP);
867 	REGISTER_CURL_CONSTANT(CURLPROTO_LDAPS);
868 	REGISTER_CURL_CONSTANT(CURLPROTO_DICT);
869 	REGISTER_CURL_CONSTANT(CURLPROTO_FILE);
870 	REGISTER_CURL_CONSTANT(CURLPROTO_TFTP);
871 	REGISTER_CURL_CONSTANT(CURLPROTO_ALL);
872 #endif
873 
874 #if LIBCURL_VERSION_NUM >= 0x070f01
875 	REGISTER_CURL_CONSTANT(CURLOPT_FTP_FILEMETHOD);
876 	REGISTER_CURL_CONSTANT(CURLOPT_FTP_SKIP_PASV_IP);
877 #endif
878 
879 #if LIBCURL_VERSION_NUM >= 0x071001
880 	REGISTER_CURL_CONSTANT(CURLFTPMETHOD_MULTICWD);
881 	REGISTER_CURL_CONSTANT(CURLFTPMETHOD_NOCWD);
882 	REGISTER_CURL_CONSTANT(CURLFTPMETHOD_SINGLECWD);
883 #endif
884 
885 #ifdef PHP_CURL_NEED_OPENSSL_TSL
886 	if (!CRYPTO_get_id_callback()) {
887 		int i, c = CRYPTO_num_locks();
888 
889 		php_curl_openssl_tsl = malloc(c * sizeof(MUTEX_T));
890 		if (!php_curl_openssl_tsl) {
891 			return FAILURE;
892 		}
893 
894 		for (i = 0; i < c; ++i) {
895 			php_curl_openssl_tsl[i] = tsrm_mutex_alloc();
896 		}
897 
898 		CRYPTO_set_id_callback(php_curl_ssl_id);
899 		CRYPTO_set_locking_callback(php_curl_ssl_lock);
900 	}
901 #endif
902 #ifdef PHP_CURL_NEED_GNUTLS_TSL
903 	gcry_control(GCRYCTL_SET_THREAD_CBS, &php_curl_gnutls_tsl);
904 #endif
905 
906 	if (curl_global_init(CURL_GLOBAL_SSL) != CURLE_OK) {
907 		return FAILURE;
908 	}
909 
910 #ifdef PHP_CURL_URL_WRAPPERS
911 # if HAVE_CURL_VERSION_INFO
912 	{
913 		curl_version_info_data *info = curl_version_info(CURLVERSION_NOW);
914 		char **p = (char **)info->protocols;
915 
916 		while (*p != NULL) {
917 			/* Do not enable cURL "file" protocol and make sure cURL is always used when --with-curlwrappers is enabled */
918 			if (strncasecmp(*p, "file", sizeof("file")-1) != 0) {
919 				php_unregister_url_stream_wrapper(*p TSRMLS_CC);
920 				php_register_url_stream_wrapper(*p, &php_curl_wrapper TSRMLS_CC);
921 			}
922 			(void) *p++;
923 		}
924 	}
925 # else
926 	php_unregister_url_stream_wrapper("http");
927 	php_register_url_stream_wrapper("http", &php_curl_wrapper TSRMLS_CC);
928 	php_unregister_url_stream_wrapper("https");
929 	php_register_url_stream_wrapper("https", &php_curl_wrapper TSRMLS_CC);
930 	php_unregister_url_stream_wrapper("ftp");
931 	php_register_url_stream_wrapper("ftp", &php_curl_wrapper TSRMLS_CC);
932 	php_unregister_url_stream_wrapper("ftps");
933 	php_register_url_stream_wrapper("ftps", &php_curl_wrapper TSRMLS_CC);
934 	php_unregister_url_stream_wrapper("ldap");
935 	php_register_url_stream_wrapper("ldap", &php_curl_wrapper TSRMLS_CC);
936 # endif
937 #endif
938 
939 	return SUCCESS;
940 }
941 /* }}} */
942 
943 /* {{{ PHP_MSHUTDOWN_FUNCTION
944  */
PHP_MSHUTDOWN_FUNCTION(curl)945 PHP_MSHUTDOWN_FUNCTION(curl)
946 {
947 #ifdef PHP_CURL_URL_WRAPPERS
948 	php_unregister_url_stream_wrapper("http" TSRMLS_CC);
949 	php_unregister_url_stream_wrapper("https" TSRMLS_CC);
950 	php_unregister_url_stream_wrapper("ftp" TSRMLS_CC);
951 	php_unregister_url_stream_wrapper("ldap" TSRMLS_CC);
952 #endif
953 	curl_global_cleanup();
954 #ifdef PHP_CURL_NEED_OPENSSL_TSL
955 	if (php_curl_openssl_tsl) {
956 		int i, c = CRYPTO_num_locks();
957 
958 		CRYPTO_set_id_callback(NULL);
959 		CRYPTO_set_locking_callback(NULL);
960 
961 		for (i = 0; i < c; ++i) {
962 			tsrm_mutex_free(php_curl_openssl_tsl[i]);
963 		}
964 
965 		free(php_curl_openssl_tsl);
966 		php_curl_openssl_tsl = NULL;
967 	}
968 #endif
969 	UNREGISTER_INI_ENTRIES();
970 	return SUCCESS;
971 }
972 /* }}} */
973 
974 /* {{{ curl_write_nothing
975  * Used as a work around. See _php_curl_close_ex
976  */
curl_write_nothing(char * data,size_t size,size_t nmemb,void * ctx)977 static size_t curl_write_nothing(char *data, size_t size, size_t nmemb, void *ctx)
978 {
979 	return size * nmemb;
980 }
981 /* }}} */
982 
983 /* {{{ curl_write
984  */
curl_write(char * data,size_t size,size_t nmemb,void * ctx)985 static size_t curl_write(char *data, size_t size, size_t nmemb, void *ctx)
986 {
987 	php_curl       *ch     = (php_curl *) ctx;
988 	php_curl_write *t      = ch->handlers->write;
989 	size_t          length = size * nmemb;
990 	TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
991 
992 #if PHP_CURL_DEBUG
993 	fprintf(stderr, "curl_write() called\n");
994 	fprintf(stderr, "data = %s, size = %d, nmemb = %d, ctx = %x\n", data, size, nmemb, ctx);
995 #endif
996 
997 	switch (t->method) {
998 		case PHP_CURL_STDOUT:
999 			PHPWRITE(data, length);
1000 			break;
1001 		case PHP_CURL_FILE:
1002 			return fwrite(data, size, nmemb, t->fp);
1003 		case PHP_CURL_RETURN:
1004 			if (length > 0) {
1005 				smart_str_appendl(&t->buf, data, (int) length);
1006 			}
1007 			break;
1008 		case PHP_CURL_USER: {
1009 			zval **argv[2];
1010 			zval *retval_ptr = NULL;
1011 			zval *handle = NULL;
1012 			zval *zdata = NULL;
1013 			int   error;
1014 			zend_fcall_info fci;
1015 
1016 			MAKE_STD_ZVAL(handle);
1017 			ZVAL_RESOURCE(handle, ch->id);
1018 			zend_list_addref(ch->id);
1019 			argv[0] = &handle;
1020 
1021 			MAKE_STD_ZVAL(zdata);
1022 			ZVAL_STRINGL(zdata, data, length, 1);
1023 			argv[1] = &zdata;
1024 
1025 			fci.size = sizeof(fci);
1026 			fci.function_table = EG(function_table);
1027 			fci.object_ptr = NULL;
1028 			fci.function_name = t->func_name;
1029 			fci.retval_ptr_ptr = &retval_ptr;
1030 			fci.param_count = 2;
1031 			fci.params = argv;
1032 			fci.no_separation = 0;
1033 			fci.symbol_table = NULL;
1034 
1035 			ch->in_callback = 1;
1036 			error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
1037 			ch->in_callback = 0;
1038 			if (error == FAILURE) {
1039 				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not call the CURLOPT_WRITEFUNCTION");
1040 				length = -1;
1041 			} else if (retval_ptr) {
1042 				if (Z_TYPE_P(retval_ptr) != IS_LONG) {
1043 					convert_to_long_ex(&retval_ptr);
1044 				}
1045 				length = Z_LVAL_P(retval_ptr);
1046 				zval_ptr_dtor(&retval_ptr);
1047 			}
1048 
1049 			zval_ptr_dtor(argv[0]);
1050 			zval_ptr_dtor(argv[1]);
1051 			break;
1052 		}
1053 	}
1054 
1055 	return length;
1056 }
1057 /* }}} */
1058 
1059 /* {{{ curl_progress
1060  */
curl_progress(void * clientp,double dltotal,double dlnow,double ultotal,double ulnow)1061 static size_t curl_progress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
1062 {
1063 	php_curl       *ch = (php_curl *) clientp;
1064 	php_curl_progress  *t  = ch->handlers->progress;
1065 	int             length = -1;
1066 	size_t	rval = 0;
1067 
1068 #if PHP_CURL_DEBUG
1069 	fprintf(stderr, "curl_progress() called\n");
1070 	fprintf(stderr, "clientp = %x, dltotal = %f, dlnow = %f, ultotal = %f, ulnow = %f\n", clientp, dltotal, dlnow, ultotal, ulnow);
1071 #endif
1072 
1073 	switch (t->method) {
1074 		case PHP_CURL_USER: {
1075 			zval **argv[4];
1076 			zval  *zdltotal = NULL;
1077 			zval  *zdlnow = NULL;
1078 			zval  *zultotal = NULL;
1079 			zval  *zulnow = NULL;
1080 			zval  *retval_ptr;
1081 			int   error;
1082 			zend_fcall_info fci;
1083 			TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
1084 
1085 			MAKE_STD_ZVAL(zdltotal);
1086 			MAKE_STD_ZVAL(zdlnow);
1087 			MAKE_STD_ZVAL(zultotal);
1088 			MAKE_STD_ZVAL(zulnow);
1089 
1090 			ZVAL_LONG(zdltotal, (long) dltotal);
1091 			ZVAL_LONG(zdlnow, (long) dlnow);
1092 			ZVAL_LONG(zultotal, (long) ultotal);
1093 			ZVAL_LONG(zulnow, (long) ulnow);
1094 
1095 			argv[0] = &zdltotal;
1096 			argv[1] = &zdlnow;
1097 			argv[2] = &zultotal;
1098 			argv[3] = &zulnow;
1099 
1100 			fci.size = sizeof(fci);
1101 			fci.function_table = EG(function_table);
1102 			fci.function_name = t->func_name;
1103 			fci.object_ptr = NULL;
1104 			fci.retval_ptr_ptr = &retval_ptr;
1105 			fci.param_count = 4;
1106 			fci.params = argv;
1107 			fci.no_separation = 0;
1108 			fci.symbol_table = NULL;
1109 
1110 			ch->in_callback = 1;
1111 			error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
1112 			ch->in_callback = 0;
1113 			if (error == FAILURE) {
1114 				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot call the CURLOPT_PROGRESSFUNCTION");
1115 				length = -1;
1116 			} else if (retval_ptr) {
1117 				if (Z_TYPE_P(retval_ptr) != IS_LONG) {
1118 					convert_to_long_ex(&retval_ptr);
1119 				}
1120 				if (0 != Z_LVAL_P(retval_ptr)) {
1121 					rval = 1;
1122 				}
1123 				zval_ptr_dtor(&retval_ptr);
1124 			}
1125 			zval_ptr_dtor(argv[0]);
1126 			zval_ptr_dtor(argv[1]);
1127 			zval_ptr_dtor(argv[2]);
1128 			zval_ptr_dtor(argv[3]);
1129 			break;
1130 		}
1131 	}
1132 	return rval;
1133 }
1134 /* }}} */
1135 
1136 /* {{{ curl_read
1137  */
curl_read(char * data,size_t size,size_t nmemb,void * ctx)1138 static size_t curl_read(char *data, size_t size, size_t nmemb, void *ctx)
1139 {
1140 	php_curl       *ch = (php_curl *) ctx;
1141 	php_curl_read  *t  = ch->handlers->read;
1142 	int             length = 0;
1143 
1144 	switch (t->method) {
1145 		case PHP_CURL_DIRECT:
1146 			if (t->fp) {
1147 				length = fread(data, size, nmemb, t->fp);
1148 			}
1149 			break;
1150 		case PHP_CURL_USER: {
1151 			zval **argv[3];
1152 			zval  *handle = NULL;
1153 			zval  *zfd = NULL;
1154 			zval  *zlength = NULL;
1155 			zval  *retval_ptr;
1156 			int   error;
1157 			zend_fcall_info fci;
1158 			TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
1159 
1160 			MAKE_STD_ZVAL(handle);
1161 			MAKE_STD_ZVAL(zfd);
1162 			MAKE_STD_ZVAL(zlength);
1163 
1164 			ZVAL_RESOURCE(handle, ch->id);
1165 			zend_list_addref(ch->id);
1166 			ZVAL_RESOURCE(zfd, t->fd);
1167 			zend_list_addref(t->fd);
1168 			ZVAL_LONG(zlength, (int) size * nmemb);
1169 
1170 			argv[0] = &handle;
1171 			argv[1] = &zfd;
1172 			argv[2] = &zlength;
1173 
1174 			fci.size = sizeof(fci);
1175 			fci.function_table = EG(function_table);
1176 			fci.function_name = t->func_name;
1177 			fci.object_ptr = NULL;
1178 			fci.retval_ptr_ptr = &retval_ptr;
1179 			fci.param_count = 3;
1180 			fci.params = argv;
1181 			fci.no_separation = 0;
1182 			fci.symbol_table = NULL;
1183 
1184 			ch->in_callback = 1;
1185 			error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
1186 			ch->in_callback = 0;
1187 			if (error == FAILURE) {
1188 				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot call the CURLOPT_READFUNCTION");
1189 #if LIBCURL_VERSION_NUM >= 0x070c01 /* 7.12.1 */
1190 				length = CURL_READFUNC_ABORT;
1191 #endif
1192 			} else if (retval_ptr) {
1193 				if (Z_TYPE_P(retval_ptr) == IS_STRING) {
1194 					length = MIN((int) (size * nmemb), Z_STRLEN_P(retval_ptr));
1195 					memcpy(data, Z_STRVAL_P(retval_ptr), length);
1196 				}
1197 				zval_ptr_dtor(&retval_ptr);
1198 			}
1199 
1200 			zval_ptr_dtor(argv[0]);
1201 			zval_ptr_dtor(argv[1]);
1202 			zval_ptr_dtor(argv[2]);
1203 			break;
1204 		}
1205 	}
1206 
1207 	return length;
1208 }
1209 /* }}} */
1210 
1211 /* {{{ curl_write_header
1212  */
curl_write_header(char * data,size_t size,size_t nmemb,void * ctx)1213 static size_t curl_write_header(char *data, size_t size, size_t nmemb, void *ctx)
1214 {
1215 	php_curl       *ch  = (php_curl *) ctx;
1216 	php_curl_write *t   = ch->handlers->write_header;
1217 	size_t          length = size * nmemb;
1218 	TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
1219 
1220 	switch (t->method) {
1221 		case PHP_CURL_STDOUT:
1222 			/* Handle special case write when we're returning the entire transfer
1223 			 */
1224 			if (ch->handlers->write->method == PHP_CURL_RETURN && length > 0) {
1225 				smart_str_appendl(&ch->handlers->write->buf, data, (int) length);
1226 			} else {
1227 				PHPWRITE(data, length);
1228 			}
1229 			break;
1230 		case PHP_CURL_FILE:
1231 			return fwrite(data, size, nmemb, t->fp);
1232 		case PHP_CURL_USER: {
1233 			zval **argv[2];
1234 			zval  *handle = NULL;
1235 			zval  *zdata = NULL;
1236 			zval  *retval_ptr;
1237 			int   error;
1238 			zend_fcall_info fci;
1239 
1240 			MAKE_STD_ZVAL(handle);
1241 			MAKE_STD_ZVAL(zdata);
1242 
1243 			ZVAL_RESOURCE(handle, ch->id);
1244 			zend_list_addref(ch->id);
1245 			ZVAL_STRINGL(zdata, data, length, 1);
1246 
1247 			argv[0] = &handle;
1248 			argv[1] = &zdata;
1249 
1250 			fci.size = sizeof(fci);
1251 			fci.function_table = EG(function_table);
1252 			fci.function_name = t->func_name;
1253 			fci.symbol_table = NULL;
1254 			fci.object_ptr = NULL;
1255 			fci.retval_ptr_ptr = &retval_ptr;
1256 			fci.param_count = 2;
1257 			fci.params = argv;
1258 			fci.no_separation = 0;
1259 
1260 			ch->in_callback = 1;
1261 			error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
1262 			ch->in_callback = 0;
1263 			if (error == FAILURE) {
1264 				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not call the CURLOPT_HEADERFUNCTION");
1265 				length = -1;
1266 			} else if (retval_ptr) {
1267 				if (Z_TYPE_P(retval_ptr) != IS_LONG) {
1268 					convert_to_long_ex(&retval_ptr);
1269 				}
1270 				length = Z_LVAL_P(retval_ptr);
1271 				zval_ptr_dtor(&retval_ptr);
1272 			}
1273 			zval_ptr_dtor(argv[0]);
1274 			zval_ptr_dtor(argv[1]);
1275 			break;
1276 		}
1277 
1278 		case PHP_CURL_IGNORE:
1279 			return length;
1280 
1281 		default:
1282 			return -1;
1283 	}
1284 
1285 	return length;
1286 }
1287 /* }}} */
1288 
curl_debug(CURL * cp,curl_infotype type,char * buf,size_t buf_len,void * ctx)1289 static int curl_debug(CURL *cp, curl_infotype type, char *buf, size_t buf_len, void *ctx) /* {{{ */
1290 {
1291 	php_curl    *ch   = (php_curl *) ctx;
1292 
1293 	if (type == CURLINFO_HEADER_OUT) {
1294 		if (ch->header.str_len) {
1295 			efree(ch->header.str);
1296 		}
1297 		if (buf_len > 0) {
1298 			ch->header.str = estrndup(buf, buf_len);
1299 			ch->header.str_len = buf_len;
1300 		}
1301 	}
1302 
1303 	return 0;
1304 }
1305 /* }}} */
1306 
1307 #if CURLOPT_PASSWDFUNCTION != 0
1308 /* {{{ curl_passwd
1309  */
curl_passwd(void * ctx,char * prompt,char * buf,int buflen)1310 static size_t curl_passwd(void *ctx, char *prompt, char *buf, int buflen)
1311 {
1312 	php_curl    *ch   = (php_curl *) ctx;
1313 	zval        *func = ch->handlers->passwd;
1314 	zval        *argv[3];
1315 	zval        *retval = NULL;
1316 	int          error;
1317 	int          ret = -1;
1318 	TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
1319 
1320 	MAKE_STD_ZVAL(argv[0]);
1321 	MAKE_STD_ZVAL(argv[1]);
1322 	MAKE_STD_ZVAL(argv[2]);
1323 
1324 	ZVAL_RESOURCE(argv[0], ch->id);
1325 	zend_list_addref(ch->id);
1326 	ZVAL_STRING(argv[1], prompt, 1);
1327 	ZVAL_LONG(argv[2], buflen);
1328 
1329 	error = call_user_function(EG(function_table), NULL, func, retval, 2, argv TSRMLS_CC);
1330 	if (error == FAILURE) {
1331 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not call the CURLOPT_PASSWDFUNCTION");
1332 	} else if (Z_TYPE_P(retval) == IS_STRING) {
1333 		if (Z_STRLEN_P(retval) > buflen) {
1334 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Returned password is too long for libcurl to handle");
1335 		} else {
1336 			strlcpy(buf, Z_STRVAL_P(retval), Z_STRLEN_P(retval));
1337 		}
1338 	} else {
1339 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "User handler '%s' did not return a string", Z_STRVAL_P(func));
1340 	}
1341 
1342 	zval_ptr_dtor(&argv[0]);
1343 	zval_ptr_dtor(&argv[1]);
1344 	zval_ptr_dtor(&argv[2]);
1345 	zval_ptr_dtor(&retval);
1346 
1347 	return ret;
1348 }
1349 /* }}} */
1350 #endif
1351 
1352 /* {{{ curl_free_string
1353  */
curl_free_string(void ** string)1354 static void curl_free_string(void **string)
1355 {
1356 	efree(*string);
1357 }
1358 /* }}} */
1359 
1360 /* {{{ curl_free_post
1361  */
curl_free_post(void ** post)1362 static void curl_free_post(void **post)
1363 {
1364 	curl_formfree((struct HttpPost *) *post);
1365 }
1366 /* }}} */
1367 
1368 /* {{{ curl_free_slist
1369  */
curl_free_slist(void ** slist)1370 static void curl_free_slist(void **slist)
1371 {
1372 	curl_slist_free_all((struct curl_slist *) *slist);
1373 }
1374 /* }}} */
1375 
1376 /* {{{ proto array curl_version([int version])
1377    Return cURL version information. */
PHP_FUNCTION(curl_version)1378 PHP_FUNCTION(curl_version)
1379 {
1380 	curl_version_info_data *d;
1381 	long uversion = CURLVERSION_NOW;
1382 
1383 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &uversion) == FAILURE) {
1384 		return;
1385 	}
1386 
1387 	d = curl_version_info(uversion);
1388 	if (d == NULL) {
1389 		RETURN_FALSE;
1390 	}
1391 
1392 	array_init(return_value);
1393 
1394 	CAAL("version_number", d->version_num);
1395 	CAAL("age", d->age);
1396 	CAAL("features", d->features);
1397 	CAAL("ssl_version_number", d->ssl_version_num);
1398 	CAAS("version", d->version);
1399 	CAAS("host", d->host);
1400 	CAAS("ssl_version", d->ssl_version);
1401 	CAAS("libz_version", d->libz_version);
1402 	/* Add an array of protocols */
1403 	{
1404 		char **p = (char **) d->protocols;
1405 		zval  *protocol_list = NULL;
1406 
1407 		MAKE_STD_ZVAL(protocol_list);
1408 		array_init(protocol_list);
1409 
1410 		while (*p != NULL) {
1411 			add_next_index_string(protocol_list, *p, 1);
1412 			p++;
1413 		}
1414 		CAAZ("protocols", protocol_list);
1415 	}
1416 }
1417 /* }}} */
1418 
1419 /* {{{ alloc_curl_handle
1420  */
alloc_curl_handle(php_curl ** ch)1421 static void alloc_curl_handle(php_curl **ch)
1422 {
1423 	*ch                           = emalloc(sizeof(php_curl));
1424 	(*ch)->to_free                = ecalloc(1, sizeof(struct _php_curl_free));
1425 	(*ch)->handlers               = ecalloc(1, sizeof(php_curl_handlers));
1426 	(*ch)->handlers->write        = ecalloc(1, sizeof(php_curl_write));
1427 	(*ch)->handlers->write_header = ecalloc(1, sizeof(php_curl_write));
1428 	(*ch)->handlers->read         = ecalloc(1, sizeof(php_curl_read));
1429 	(*ch)->handlers->progress     = ecalloc(1, sizeof(php_curl_progress));
1430 
1431 	(*ch)->in_callback = 0;
1432 	(*ch)->header.str_len = 0;
1433 
1434 	memset(&(*ch)->err, 0, sizeof((*ch)->err));
1435 	(*ch)->handlers->write->stream = NULL;
1436 	(*ch)->handlers->write_header->stream = NULL;
1437 	(*ch)->handlers->read->stream = NULL;
1438 
1439 	zend_llist_init(&(*ch)->to_free->str,   sizeof(char *),            (llist_dtor_func_t) curl_free_string, 0);
1440 	zend_llist_init(&(*ch)->to_free->slist, sizeof(struct curl_slist), (llist_dtor_func_t) curl_free_slist,  0);
1441 	zend_llist_init(&(*ch)->to_free->post,  sizeof(struct HttpPost),   (llist_dtor_func_t) curl_free_post,   0);
1442 }
1443 /* }}} */
1444 
1445 #if LIBCURL_VERSION_NUM > 0x071301
1446 /* {{{ split_certinfo
1447  */
split_certinfo(char * string,zval * hash)1448 static void split_certinfo(char *string, zval *hash)
1449 {
1450 	char *org = estrdup(string);
1451 	char *s = org;
1452 	char *split;
1453 
1454 	if(org) {
1455         do {
1456 			char *key;
1457 			char *val;
1458 			char *tmp;
1459 
1460             split = strstr(s, "; ");
1461             if(split)
1462                 *split = '\0';
1463 
1464 			key = s;
1465 			tmp = memchr(key, '=', 64);
1466 			if(tmp) {
1467 				*tmp = '\0';
1468 				val = tmp+1;
1469 				add_assoc_string(hash, key, val, 1);
1470 			}
1471 			s = split+2;
1472 		} while(split);
1473 		efree(org);
1474 	}
1475 }
1476 /* }}} */
1477 
1478 /* {{{ create_certinfo
1479  */
create_certinfo(struct curl_certinfo * ci,zval * listcode TSRMLS_DC)1480 static void create_certinfo(struct curl_certinfo *ci, zval *listcode TSRMLS_DC)
1481 {
1482 	int i;
1483 
1484 	if(ci) {
1485 		zval *certhash = NULL;
1486 
1487 		for(i=0; i<ci->num_of_certs; i++) {
1488 			struct curl_slist *slist;
1489 
1490 			MAKE_STD_ZVAL(certhash);
1491 			array_init(certhash);
1492 			for(slist = ci->certinfo[i]; slist; slist = slist->next) {
1493 				int len;
1494 				char s[64];
1495 				char *tmp;
1496 				strncpy(s, slist->data, 64);
1497 				tmp = memchr(s, ':', 64);
1498 				if(tmp) {
1499 					*tmp = '\0';
1500 					len = strlen(s);
1501 					if(!strcmp(s, "Subject") || !strcmp(s, "Issuer")) {
1502 						zval *hash;
1503 
1504 						MAKE_STD_ZVAL(hash);
1505 						array_init(hash);
1506 
1507 						split_certinfo(&slist->data[len+1], hash);
1508 						add_assoc_zval(certhash, s, hash);
1509 					} else {
1510 						add_assoc_string(certhash, s, &slist->data[len+1], 1);
1511 					}
1512 				} else {
1513 					php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not extract hash key from certificate info");
1514 				}
1515 			}
1516 			add_next_index_zval(listcode, certhash);
1517 		}
1518 	}
1519 }
1520 /* }}} */
1521 #endif
1522 
1523 /* {{{ proto resource curl_init([string url])
1524    Initialize a cURL session */
PHP_FUNCTION(curl_init)1525 PHP_FUNCTION(curl_init)
1526 {
1527 	php_curl	*ch;
1528 	CURL		*cp;
1529 	zval		*clone;
1530 	char		*url = NULL;
1531 	int		url_len = 0;
1532 	char *cainfo;
1533 
1534 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &url, &url_len) == FAILURE) {
1535 		return;
1536 	}
1537 
1538 	cp = curl_easy_init();
1539 	if (!cp) {
1540 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize a new cURL handle");
1541 		RETURN_FALSE;
1542 	}
1543 
1544 	alloc_curl_handle(&ch);
1545 	TSRMLS_SET_CTX(ch->thread_ctx);
1546 
1547 	ch->cp = cp;
1548 
1549 	ch->handlers->write->method = PHP_CURL_STDOUT;
1550 	ch->handlers->write->type   = PHP_CURL_ASCII;
1551 	ch->handlers->read->method  = PHP_CURL_DIRECT;
1552 	ch->handlers->write_header->method = PHP_CURL_IGNORE;
1553 
1554 	ch->uses = 0;
1555 
1556 	MAKE_STD_ZVAL(clone);
1557 	ch->clone = clone;
1558 
1559 
1560 
1561 	curl_easy_setopt(ch->cp, CURLOPT_NOPROGRESS,        1);
1562 	curl_easy_setopt(ch->cp, CURLOPT_VERBOSE,           0);
1563 	curl_easy_setopt(ch->cp, CURLOPT_ERRORBUFFER,       ch->err.str);
1564 	curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION,     curl_write);
1565 	curl_easy_setopt(ch->cp, CURLOPT_FILE,              (void *) ch);
1566 	curl_easy_setopt(ch->cp, CURLOPT_READFUNCTION,      curl_read);
1567 	curl_easy_setopt(ch->cp, CURLOPT_INFILE,            (void *) ch);
1568 	curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION,    curl_write_header);
1569 	curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER,       (void *) ch);
1570 	curl_easy_setopt(ch->cp, CURLOPT_DNS_USE_GLOBAL_CACHE, 1);
1571 	curl_easy_setopt(ch->cp, CURLOPT_DNS_CACHE_TIMEOUT, 120);
1572 	curl_easy_setopt(ch->cp, CURLOPT_MAXREDIRS, 20); /* prevent infinite redirects */
1573 
1574 	cainfo = INI_STR("curl.cainfo");
1575 	if (cainfo && strlen(cainfo) > 0) {
1576 		curl_easy_setopt(ch->cp, CURLOPT_CAINFO, cainfo);
1577 	}
1578 
1579 #if defined(ZTS)
1580 	curl_easy_setopt(ch->cp, CURLOPT_NOSIGNAL, 1);
1581 #endif
1582 
1583 	if (url) {
1584 		if (!php_curl_option_url(ch, url, url_len)) {
1585 			_php_curl_close_ex(ch TSRMLS_CC);
1586 			RETURN_FALSE;
1587 		}
1588 	}
1589 
1590 	ZEND_REGISTER_RESOURCE(return_value, ch, le_curl);
1591 	ch->id = Z_LVAL_P(return_value);
1592 }
1593 /* }}} */
1594 
1595 /* {{{ proto resource curl_copy_handle(resource ch)
1596    Copy a cURL handle along with all of it's preferences */
PHP_FUNCTION(curl_copy_handle)1597 PHP_FUNCTION(curl_copy_handle)
1598 {
1599 	CURL		*cp;
1600 	zval		*zid;
1601 	php_curl	*ch, *dupch;
1602 
1603 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
1604 		return;
1605 	}
1606 
1607 	ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
1608 
1609 	cp = curl_easy_duphandle(ch->cp);
1610 	if (!cp) {
1611 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot duplicate cURL handle");
1612 		RETURN_FALSE;
1613 	}
1614 
1615 	alloc_curl_handle(&dupch);
1616 	TSRMLS_SET_CTX(dupch->thread_ctx);
1617 
1618 	dupch->cp = cp;
1619 	dupch->uses = 0;
1620 	ch->uses++;
1621 	if (ch->handlers->write->stream) {
1622 		Z_ADDREF_P(ch->handlers->write->stream);
1623 	}
1624 	dupch->handlers->write->stream = ch->handlers->write->stream;
1625 	dupch->handlers->write->method = ch->handlers->write->method;
1626 	dupch->handlers->write->type   = ch->handlers->write->type;
1627 	if (ch->handlers->read->stream) {
1628 		Z_ADDREF_P(ch->handlers->read->stream);
1629 	}
1630 	dupch->handlers->read->stream  = ch->handlers->read->stream;
1631 	dupch->handlers->read->method  = ch->handlers->read->method;
1632 	dupch->handlers->write_header->method = ch->handlers->write_header->method;
1633 	if (ch->handlers->write_header->stream) {
1634 		Z_ADDREF_P(ch->handlers->write_header->stream);
1635 	}
1636 	dupch->handlers->write_header->stream = ch->handlers->write_header->stream;
1637 
1638 	dupch->handlers->write->fp = ch->handlers->write->fp;
1639 	dupch->handlers->write_header->fp = ch->handlers->write_header->fp;
1640 	dupch->handlers->read->fp = ch->handlers->read->fp;
1641 	dupch->handlers->read->fd = ch->handlers->read->fd;
1642 #if CURLOPT_PASSWDDATA != 0
1643 	if (ch->handlers->passwd) {
1644 		zval_add_ref(&ch->handlers->passwd);
1645 		dupch->handlers->passwd = ch->handlers->passwd;
1646 		curl_easy_setopt(ch->cp, CURLOPT_PASSWDDATA, (void *) dupch);
1647 	}
1648 #endif
1649 	if (ch->handlers->write->func_name) {
1650 		zval_add_ref(&ch->handlers->write->func_name);
1651 		dupch->handlers->write->func_name = ch->handlers->write->func_name;
1652 	}
1653 	if (ch->handlers->read->func_name) {
1654 		zval_add_ref(&ch->handlers->read->func_name);
1655 		dupch->handlers->read->func_name = ch->handlers->read->func_name;
1656 	}
1657 	if (ch->handlers->write_header->func_name) {
1658 		zval_add_ref(&ch->handlers->write_header->func_name);
1659 		dupch->handlers->write_header->func_name = ch->handlers->write_header->func_name;
1660 	}
1661 
1662 	if (ch->handlers->progress->func_name) {
1663 		zval_add_ref(&ch->handlers->progress->func_name);
1664 		dupch->handlers->progress->func_name = ch->handlers->progress->func_name;
1665 	}
1666 	dupch->handlers->progress->method = ch->handlers->progress->method;
1667 
1668 	curl_easy_setopt(dupch->cp, CURLOPT_ERRORBUFFER,       dupch->err.str);
1669 	curl_easy_setopt(dupch->cp, CURLOPT_FILE,              (void *) dupch);
1670 	curl_easy_setopt(dupch->cp, CURLOPT_INFILE,            (void *) dupch);
1671 	curl_easy_setopt(dupch->cp, CURLOPT_WRITEHEADER,       (void *) dupch);
1672 	curl_easy_setopt(dupch->cp, CURLOPT_PROGRESSDATA,      (void *) dupch);
1673 
1674 	efree(dupch->to_free);
1675 	dupch->to_free = ch->to_free;
1676 
1677 	/* Keep track of cloned copies to avoid invoking curl destructors for every clone */
1678 	Z_ADDREF_P(ch->clone);
1679 	dupch->clone = ch->clone;
1680 
1681 	ZEND_REGISTER_RESOURCE(return_value, dupch, le_curl);
1682 	dupch->id = Z_LVAL_P(return_value);
1683 }
1684 /* }}} */
1685 
_php_curl_setopt(php_curl * ch,long option,zval ** zvalue,zval * return_value TSRMLS_DC)1686 static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue, zval *return_value TSRMLS_DC) /* {{{ */
1687 {
1688 	CURLcode     error=CURLE_OK;
1689 
1690 	switch (option) {
1691 		case CURLOPT_SSL_VERIFYHOST:
1692 			if(Z_BVAL_PP(zvalue) == 1) {
1693 #if LIBCURL_VERSION_NUM <= 0x071c00 /* 7.28.0 */
1694 				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");
1695 #else
1696 				php_error_docref(NULL TSRMLS_CC, E_NOTICE, "CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead");
1697 				error = curl_easy_setopt(ch->cp, option, 2);
1698 				break;
1699 #endif
1700 			}
1701 		case CURLOPT_INFILESIZE:
1702 		case CURLOPT_VERBOSE:
1703 		case CURLOPT_HEADER:
1704 		case CURLOPT_NOPROGRESS:
1705 		case CURLOPT_NOBODY:
1706 		case CURLOPT_FAILONERROR:
1707 		case CURLOPT_UPLOAD:
1708 		case CURLOPT_POST:
1709 		case CURLOPT_FTPLISTONLY:
1710 		case CURLOPT_FTPAPPEND:
1711 		case CURLOPT_NETRC:
1712 		case CURLOPT_PUT:
1713 #if CURLOPT_MUTE != 0
1714 		 case CURLOPT_MUTE:
1715 #endif
1716 		case CURLOPT_TIMEOUT:
1717 #if LIBCURL_VERSION_NUM > 0x071002
1718 		case CURLOPT_TIMEOUT_MS:
1719 #endif
1720 		case CURLOPT_FTP_USE_EPSV:
1721 		case CURLOPT_LOW_SPEED_LIMIT:
1722 		case CURLOPT_SSLVERSION:
1723 		case CURLOPT_LOW_SPEED_TIME:
1724 		case CURLOPT_RESUME_FROM:
1725 		case CURLOPT_TIMEVALUE:
1726 		case CURLOPT_TIMECONDITION:
1727 		case CURLOPT_TRANSFERTEXT:
1728 		case CURLOPT_HTTPPROXYTUNNEL:
1729 		case CURLOPT_FILETIME:
1730 		case CURLOPT_MAXREDIRS:
1731 		case CURLOPT_MAXCONNECTS:
1732 		case CURLOPT_CLOSEPOLICY:
1733 		case CURLOPT_FRESH_CONNECT:
1734 		case CURLOPT_FORBID_REUSE:
1735 		case CURLOPT_CONNECTTIMEOUT:
1736 #if LIBCURL_VERSION_NUM > 0x071002
1737 		case CURLOPT_CONNECTTIMEOUT_MS:
1738 #endif
1739 		case CURLOPT_SSL_VERIFYPEER:
1740 		case CURLOPT_DNS_USE_GLOBAL_CACHE:
1741 		case CURLOPT_NOSIGNAL:
1742 		case CURLOPT_PROXYTYPE:
1743 		case CURLOPT_BUFFERSIZE:
1744 		case CURLOPT_HTTPGET:
1745 		case CURLOPT_HTTP_VERSION:
1746 		case CURLOPT_CRLF:
1747 		case CURLOPT_DNS_CACHE_TIMEOUT:
1748 		case CURLOPT_PROXYPORT:
1749 		case CURLOPT_FTP_USE_EPRT:
1750 #if LIBCURL_VERSION_NUM > 0x070a05 /* CURLOPT_HTTPAUTH is available since curl 7.10.6 */
1751 		case CURLOPT_HTTPAUTH:
1752 #endif
1753 #if LIBCURL_VERSION_NUM > 0x070a06 /* CURLOPT_PROXYAUTH & CURLOPT_FTP_CREATE_MISSING_DIRS are available since curl 7.10.7 */
1754 		case CURLOPT_PROXYAUTH:
1755 		case CURLOPT_FTP_CREATE_MISSING_DIRS:
1756 #endif
1757 
1758 #if LIBCURL_VERSION_NUM >= 0x070c02
1759 		case CURLOPT_FTPSSLAUTH:
1760 #endif
1761 #if LIBCURL_VERSION_NUM >  0x070b00
1762 		case CURLOPT_FTP_SSL:
1763 #endif
1764 		case CURLOPT_UNRESTRICTED_AUTH:
1765 		case CURLOPT_PORT:
1766 		case CURLOPT_AUTOREFERER:
1767 		case CURLOPT_COOKIESESSION:
1768 #if LIBCURL_VERSION_NUM > 0x070b01 /* CURLOPT_TCP_NODELAY is available since curl 7.11.2 */
1769 		case CURLOPT_TCP_NODELAY:
1770 #endif
1771 #if LIBCURL_VERSION_NUM >= 0x71304
1772 		case CURLOPT_REDIR_PROTOCOLS:
1773 		case CURLOPT_PROTOCOLS:
1774 #endif
1775 #if LIBCURL_VERSION_NUM > 0x070a07 /* CURLOPT_IPRESOLVE is available since curl 7.10.8 */
1776 		case CURLOPT_IPRESOLVE:
1777 #endif
1778 #if LIBCURL_VERSION_NUM >= 0x070f01
1779 		case CURLOPT_FTP_FILEMETHOD:
1780 		case CURLOPT_FTP_SKIP_PASV_IP:
1781 #endif
1782 #if LIBCURL_VERSION_NUM >  0x071301
1783 		case CURLOPT_CERTINFO:
1784 #endif
1785 			convert_to_long_ex(zvalue);
1786 #if LIBCURL_VERSION_NUM >= 0x71304
1787 			if ((option == CURLOPT_PROTOCOLS || option == CURLOPT_REDIR_PROTOCOLS) &&
1788 				((PG(open_basedir) && *PG(open_basedir)) || PG(safe_mode)) && (Z_LVAL_PP(zvalue) & CURLPROTO_FILE)) {
1789 					php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLPROTO_FILE cannot be activated when in safe_mode or an open_basedir is set");
1790 					RETVAL_FALSE;
1791 					return 1;
1792 			}
1793 #endif
1794 			error = curl_easy_setopt(ch->cp, option, Z_LVAL_PP(zvalue));
1795 			break;
1796 #if LIBCURL_VERSION_NUM > 0x070f04
1797 		case CURLOPT_MAX_RECV_SPEED_LARGE:
1798 		case CURLOPT_MAX_SEND_SPEED_LARGE:
1799 			convert_to_long_ex(zvalue);
1800 			error = curl_easy_setopt(ch->cp, option, (curl_off_t)Z_LVAL_PP(zvalue));
1801 			break;
1802 #endif
1803 		case CURLOPT_FOLLOWLOCATION:
1804 			convert_to_long_ex(zvalue);
1805 			if ((PG(open_basedir) && *PG(open_basedir)) || PG(safe_mode)) {
1806 				if (Z_LVAL_PP(zvalue) != 0) {
1807 					php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set");
1808 					RETVAL_FALSE;
1809 					return 1;
1810 				}
1811 			}
1812 			error = curl_easy_setopt(ch->cp, option, Z_LVAL_PP(zvalue));
1813 			break;
1814 #if LIBCURL_VERSION_NUM > 0x071301
1815 		case CURLOPT_POSTREDIR:
1816 			convert_to_long_ex(zvalue);
1817 			error = curl_easy_setopt(ch->cp, CURLOPT_POSTREDIR, Z_LVAL_PP(zvalue) & CURL_REDIR_POST_ALL);
1818 			break;
1819 #endif
1820 		case CURLOPT_PRIVATE:
1821 		case CURLOPT_URL:
1822 		case CURLOPT_PROXY:
1823 		case CURLOPT_USERPWD:
1824 		case CURLOPT_PROXYUSERPWD:
1825 		case CURLOPT_RANGE:
1826 		case CURLOPT_CUSTOMREQUEST:
1827 		case CURLOPT_USERAGENT:
1828 		case CURLOPT_FTPPORT:
1829 		case CURLOPT_COOKIE:
1830 		case CURLOPT_REFERER:
1831 		case CURLOPT_INTERFACE:
1832 		case CURLOPT_KRB4LEVEL:
1833 		case CURLOPT_EGDSOCKET:
1834 		case CURLOPT_CAINFO:
1835 		case CURLOPT_CAPATH:
1836 		case CURLOPT_SSL_CIPHER_LIST:
1837 		case CURLOPT_SSLKEY:
1838 		case CURLOPT_SSLKEYTYPE:
1839 		case CURLOPT_SSLKEYPASSWD:
1840 		case CURLOPT_SSLENGINE:
1841 		case CURLOPT_SSLENGINE_DEFAULT:
1842 		case CURLOPT_SSLCERTTYPE:
1843 		case CURLOPT_ENCODING:
1844 #if LIBCURL_VERSION_NUM >= 0x071300
1845 		case CURLOPT_SSH_PUBLIC_KEYFILE:
1846 		case CURLOPT_SSH_PRIVATE_KEYFILE:
1847 #endif
1848 		{
1849 #if LIBCURL_VERSION_NUM < 0x071100
1850 			char *copystr = NULL;
1851 #endif
1852 
1853 			convert_to_string_ex(zvalue);
1854 #if LIBCURL_VERSION_NUM >= 0x071300
1855 			if (
1856 				option == CURLOPT_SSH_PUBLIC_KEYFILE || option == CURLOPT_SSH_PRIVATE_KEYFILE
1857 
1858 			) {
1859 				if (php_check_open_basedir(Z_STRVAL_PP(zvalue) TSRMLS_CC) || (PG(safe_mode) && !php_checkuid(Z_STRVAL_PP(zvalue), "rb+", CHECKUID_CHECK_MODE_PARAM))) {
1860 					RETVAL_FALSE;
1861 					return 1;
1862 				}
1863 			}
1864 #endif
1865 			if (option == CURLOPT_URL) {
1866 				if (!php_curl_option_url(ch, Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue))) {
1867 					RETVAL_FALSE;
1868 					return 1;
1869 				}
1870 			} else {
1871 				if (option == CURLOPT_PRIVATE) {
1872 					char *copystr;
1873 #if LIBCURL_VERSION_NUM < 0x071100
1874 string_copy:
1875 #endif
1876 					copystr = estrndup(Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue));
1877 					error = curl_easy_setopt(ch->cp, option, copystr);
1878 					zend_llist_add_element(&ch->to_free->str, &copystr);
1879 				} else {
1880 #if LIBCURL_VERSION_NUM >= 0x071100
1881 					/* Strings passed to libcurl as ’char *’ arguments, are copied by the library... NOTE: before 7.17.0 strings were not copied. */
1882 					error = curl_easy_setopt(ch->cp, option, Z_STRVAL_PP(zvalue));
1883 #else
1884 					goto string_copy;
1885 #endif
1886 				}
1887 			}
1888 			break;
1889 		}
1890 		case CURLOPT_FILE:
1891 		case CURLOPT_INFILE:
1892 		case CURLOPT_WRITEHEADER:
1893 		case CURLOPT_STDERR: {
1894 			FILE *fp = NULL;
1895 			int type;
1896 			void * what;
1897 
1898 			what = zend_fetch_resource(zvalue TSRMLS_CC, -1, "File-Handle", &type, 1, php_file_le_stream(), php_file_le_pstream());
1899 			if (!what) {
1900 				RETVAL_FALSE;
1901 				return 1;
1902 			}
1903 
1904 			if (FAILURE == php_stream_cast((php_stream *) what, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) {
1905 				RETVAL_FALSE;
1906 				return 1;
1907 			}
1908 
1909 			if (!fp) {
1910 				RETVAL_FALSE;
1911 				return 1;
1912 			}
1913 
1914 			error = CURLE_OK;
1915 			switch (option) {
1916 				case CURLOPT_FILE:
1917 					if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') {
1918 						if (ch->handlers->write->stream) {
1919 							Z_DELREF_P(ch->handlers->write->stream);
1920 						}
1921 						Z_ADDREF_PP(zvalue);
1922 						ch->handlers->write->fp = fp;
1923 						ch->handlers->write->method = PHP_CURL_FILE;
1924 						ch->handlers->write->stream = *zvalue;
1925 					} else {
1926 						php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable");
1927 						RETVAL_FALSE;
1928 						return 1;
1929 					}
1930 					break;
1931 				case CURLOPT_WRITEHEADER:
1932 					if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') {
1933 						if (ch->handlers->write_header->stream) {
1934 							Z_DELREF_P(ch->handlers->write_header->stream);
1935 						}
1936 						Z_ADDREF_PP(zvalue);
1937 						ch->handlers->write_header->fp = fp;
1938 						ch->handlers->write_header->method = PHP_CURL_FILE;
1939 						ch->handlers->write_header->stream = *zvalue;
1940 					} else {
1941 						php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable");
1942 						RETVAL_FALSE;
1943 						return 1;
1944 					}
1945 					break;
1946 				case CURLOPT_INFILE:
1947 					if (ch->handlers->read->stream) {
1948 						Z_DELREF_P(ch->handlers->read->stream);
1949 					}
1950 					Z_ADDREF_PP(zvalue);
1951 					ch->handlers->read->fp = fp;
1952 					ch->handlers->read->fd = Z_LVAL_PP(zvalue);
1953 					ch->handlers->read->stream = *zvalue;
1954 					break;
1955 				case CURLOPT_STDERR:
1956 					if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') {
1957 						if (ch->handlers->std_err) {
1958 							zval_ptr_dtor(&ch->handlers->std_err);
1959 						}
1960 						zval_add_ref(zvalue);
1961 						ch->handlers->std_err = *zvalue;
1962 					} else {
1963 						php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable");
1964 						RETVAL_FALSE;
1965 						return 1;
1966 					}
1967 					/* break omitted intentionally */
1968 				default:
1969 					error = curl_easy_setopt(ch->cp, option, fp);
1970 					break;
1971 			}
1972 
1973 			break;
1974 		}
1975 		case CURLOPT_RETURNTRANSFER:
1976 			convert_to_long_ex(zvalue);
1977 
1978 			if (Z_LVAL_PP(zvalue)) {
1979 				ch->handlers->write->method = PHP_CURL_RETURN;
1980 			} else {
1981 				ch->handlers->write->method = PHP_CURL_STDOUT;
1982 			}
1983 			break;
1984 		case CURLOPT_BINARYTRANSFER:
1985 			convert_to_long_ex(zvalue);
1986 
1987 			if (Z_LVAL_PP(zvalue)) {
1988 				ch->handlers->write->type = PHP_CURL_BINARY;
1989 			} else {
1990 				ch->handlers->write->type = PHP_CURL_ASCII;
1991 			}
1992 			break;
1993 		case CURLOPT_WRITEFUNCTION:
1994 			if (ch->handlers->write->func_name) {
1995 				zval_ptr_dtor(&ch->handlers->write->func_name);
1996 				ch->handlers->write->fci_cache = empty_fcall_info_cache;
1997 			}
1998 			zval_add_ref(zvalue);
1999 			ch->handlers->write->func_name = *zvalue;
2000 			ch->handlers->write->method = PHP_CURL_USER;
2001 			break;
2002 		case CURLOPT_READFUNCTION:
2003 			if (ch->handlers->read->func_name) {
2004 				zval_ptr_dtor(&ch->handlers->read->func_name);
2005 				ch->handlers->read->fci_cache = empty_fcall_info_cache;
2006 			}
2007 			zval_add_ref(zvalue);
2008 			ch->handlers->read->func_name = *zvalue;
2009 			ch->handlers->read->method = PHP_CURL_USER;
2010 			break;
2011 		case CURLOPT_PROGRESSFUNCTION:
2012 			curl_easy_setopt(ch->cp, CURLOPT_PROGRESSFUNCTION,	curl_progress);
2013 			curl_easy_setopt(ch->cp, CURLOPT_PROGRESSDATA, ch);
2014 			if (ch->handlers->progress->func_name) {
2015 				zval_ptr_dtor(&ch->handlers->progress->func_name);
2016 				ch->handlers->progress->fci_cache = empty_fcall_info_cache;
2017 			}
2018 			zval_add_ref(zvalue);
2019 			ch->handlers->progress->func_name = *zvalue;
2020 			ch->handlers->progress->method = PHP_CURL_USER;
2021 			break;
2022 		case CURLOPT_HEADERFUNCTION:
2023 			if (ch->handlers->write_header->func_name) {
2024 				zval_ptr_dtor(&ch->handlers->write_header->func_name);
2025 				ch->handlers->write_header->fci_cache = empty_fcall_info_cache;
2026 			}
2027 			zval_add_ref(zvalue);
2028 			ch->handlers->write_header->func_name = *zvalue;
2029 			ch->handlers->write_header->method = PHP_CURL_USER;
2030 			break;
2031 #if CURLOPT_PASSWDFUNCTION != 0
2032 		case CURLOPT_PASSWDFUNCTION:
2033 			if (ch->handlers->passwd) {
2034 				zval_ptr_dtor(&ch->handlers->passwd);
2035 			}
2036 			zval_add_ref(zvalue);
2037 			ch->handlers->passwd = *zvalue;
2038 			error = curl_easy_setopt(ch->cp, CURLOPT_PASSWDFUNCTION, curl_passwd);
2039 			error = curl_easy_setopt(ch->cp, CURLOPT_PASSWDDATA,     (void *) ch);
2040 			break;
2041 #endif
2042 		case CURLOPT_POSTFIELDS:
2043 			if (Z_TYPE_PP(zvalue) == IS_ARRAY || Z_TYPE_PP(zvalue) == IS_OBJECT) {
2044 				zval            **current;
2045 				HashTable        *postfields;
2046 				struct HttpPost  *first = NULL;
2047 				struct HttpPost  *last  = NULL;
2048 				char             *postval;
2049 				char             *string_key = NULL;
2050 				ulong             num_key;
2051 				uint              string_key_len;
2052 
2053 				postfields = HASH_OF(*zvalue);
2054 				if (!postfields) {
2055 					php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't get HashTable in CURLOPT_POSTFIELDS");
2056 					RETVAL_FALSE;
2057 					return 1;
2058 				}
2059 
2060 				for (zend_hash_internal_pointer_reset(postfields);
2061 					 zend_hash_get_current_data(postfields, (void **) &current) == SUCCESS;
2062 					 zend_hash_move_forward(postfields)
2063 				) {
2064 
2065 					SEPARATE_ZVAL(current);
2066 					convert_to_string_ex(current);
2067 
2068 					zend_hash_get_current_key_ex(postfields, &string_key, &string_key_len, &num_key, 0, NULL);
2069 
2070 					postval = Z_STRVAL_PP(current);
2071 
2072 					/* The arguments after _NAMELENGTH and _CONTENTSLENGTH
2073 					 * must be explicitly cast to long in curl_formadd
2074 					 * use since curl needs a long not an int. */
2075 					if (*postval == '@') {
2076 						char *type, *filename;
2077 						++postval;
2078 
2079 						if ((type = php_memnstr(postval, ";type=", sizeof(";type=") - 1, postval + Z_STRLEN_PP(current)))) {
2080 							*type = '\0';
2081 						}
2082 						if ((filename = php_memnstr(postval, ";filename=", sizeof(";filename=") - 1, postval + Z_STRLEN_PP(current)))) {
2083 							*filename = '\0';
2084 						}
2085 						/* safe_mode / open_basedir check */
2086 						if (php_check_open_basedir(postval TSRMLS_CC) || (PG(safe_mode) && !php_checkuid(postval, "rb+", CHECKUID_CHECK_MODE_PARAM))) {
2087 							RETVAL_FALSE;
2088 							return 1;
2089 						}
2090 						error = curl_formadd(&first, &last,
2091 										CURLFORM_COPYNAME, string_key,
2092 										CURLFORM_NAMELENGTH, (long)string_key_len - 1,
2093 										CURLFORM_FILENAME, filename ? filename + sizeof(";filename=") - 1 : postval,
2094 										CURLFORM_CONTENTTYPE, type ? type + sizeof(";type=") - 1 : "application/octet-stream",
2095 										CURLFORM_FILE, postval,
2096 										CURLFORM_END);
2097 						if (type) {
2098 							*type = ';';
2099 						}
2100 						if (filename) {
2101 							*filename = ';';
2102 						}
2103 					} else {
2104 						error = curl_formadd(&first, &last,
2105 											 CURLFORM_COPYNAME, string_key,
2106 											 CURLFORM_NAMELENGTH, (long)string_key_len - 1,
2107 											 CURLFORM_COPYCONTENTS, postval,
2108 											 CURLFORM_CONTENTSLENGTH, (long)Z_STRLEN_PP(current),
2109 											 CURLFORM_END);
2110 					}
2111 				}
2112 
2113 				SAVE_CURL_ERROR(ch, error);
2114 				if (error != CURLE_OK) {
2115 					RETVAL_FALSE;
2116 					return 1;
2117 				}
2118 
2119 				zend_llist_add_element(&ch->to_free->post, &first);
2120 				error = curl_easy_setopt(ch->cp, CURLOPT_HTTPPOST, first);
2121 
2122 			} else {
2123 #if LIBCURL_VERSION_NUM >= 0x071101
2124 				convert_to_string_ex(zvalue);
2125 				/* with curl 7.17.0 and later, we can use COPYPOSTFIELDS, but we have to provide size before */
2126 				error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, Z_STRLEN_PP(zvalue));
2127 				error = curl_easy_setopt(ch->cp, CURLOPT_COPYPOSTFIELDS, Z_STRVAL_PP(zvalue));
2128 #else
2129 				char *post = NULL;
2130 
2131 				convert_to_string_ex(zvalue);
2132 				post = estrndup(Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue));
2133 				zend_llist_add_element(&ch->to_free->str, &post);
2134 
2135 				error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDS, post);
2136 				error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, Z_STRLEN_PP(zvalue));
2137 #endif
2138 			}
2139 			break;
2140 		case CURLOPT_HTTPHEADER:
2141 		case CURLOPT_QUOTE:
2142 		case CURLOPT_HTTP200ALIASES:
2143 		case CURLOPT_POSTQUOTE: {
2144 			zval              **current;
2145 			HashTable          *ph;
2146 			struct curl_slist  *slist = NULL;
2147 
2148 			ph = HASH_OF(*zvalue);
2149 			if (!ph) {
2150 				php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must pass either an object or an array with the CURLOPT_HTTPHEADER, CURLOPT_QUOTE, CURLOPT_HTTP200ALIASES and CURLOPT_POSTQUOTE arguments");
2151 				RETVAL_FALSE;
2152 				return 1;
2153 			}
2154 
2155 			for (zend_hash_internal_pointer_reset(ph);
2156 				 zend_hash_get_current_data(ph, (void **) &current) == SUCCESS;
2157 				 zend_hash_move_forward(ph)
2158 			) {
2159 				SEPARATE_ZVAL(current);
2160 				convert_to_string_ex(current);
2161 
2162 				slist = curl_slist_append(slist, Z_STRVAL_PP(current));
2163 				if (!slist) {
2164 					php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not build curl_slist");
2165 					RETVAL_FALSE;
2166 					return 1;
2167 				}
2168 			}
2169 			zend_llist_add_element(&ch->to_free->slist, &slist);
2170 
2171 			error = curl_easy_setopt(ch->cp, option, slist);
2172 
2173 			break;
2174 		}
2175 		/* the following options deal with files, therefor safe_mode & open_basedir checks
2176 		 * are required.
2177 		 */
2178 		case CURLOPT_COOKIEJAR:
2179 		case CURLOPT_SSLCERT:
2180 		case CURLOPT_RANDOM_FILE:
2181 		case CURLOPT_COOKIEFILE: {
2182 #if LIBCURL_VERSION_NUM < 0x071100
2183 			char *copystr = NULL;
2184 #endif
2185 
2186 			convert_to_string_ex(zvalue);
2187 
2188 			if ((Z_STRLEN_PP(zvalue) && php_check_open_basedir(Z_STRVAL_PP(zvalue) TSRMLS_CC)) || (PG(safe_mode) && !php_checkuid(Z_STRVAL_PP(zvalue), "rb+", CHECKUID_CHECK_MODE_PARAM))) {
2189 				RETVAL_FALSE;
2190 				return 1;
2191 			}
2192 
2193 #if LIBCURL_VERSION_NUM >= 0x071100
2194 			error = curl_easy_setopt(ch->cp, option, Z_STRVAL_PP(zvalue));
2195 #else
2196 			copystr = estrndup(Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue));
2197 
2198 			error = curl_easy_setopt(ch->cp, option, copystr);
2199 			zend_llist_add_element(&ch->to_free->str, &copystr);
2200 #endif
2201 			break;
2202 		}
2203 		case CURLINFO_HEADER_OUT:
2204 			convert_to_long_ex(zvalue);
2205 			if (Z_LVAL_PP(zvalue) == 1) {
2206 				curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, curl_debug);
2207 				curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, (void *)ch);
2208 				curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 1);
2209 			} else {
2210 				curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, NULL);
2211 				curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, NULL);
2212 				curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 0);
2213 			}
2214 			break;
2215 	}
2216 
2217 	SAVE_CURL_ERROR(ch, error);
2218 	if (error != CURLE_OK) {
2219 		return 1;
2220 	} else {
2221 		return 0;
2222 	}
2223 }
2224 /* }}} */
2225 
2226 /* {{{ proto bool curl_setopt(resource ch, int option, mixed value)
2227    Set an option for a cURL transfer */
PHP_FUNCTION(curl_setopt)2228 PHP_FUNCTION(curl_setopt)
2229 {
2230 	zval       *zid, **zvalue;
2231 	long        options;
2232 	php_curl   *ch;
2233 
2234 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlZ", &zid, &options, &zvalue) == FAILURE) {
2235 		return;
2236 	}
2237 
2238 	ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
2239 
2240 	if (options <= 0) {
2241 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid curl configuration option");
2242 		RETURN_FALSE;
2243 	}
2244 
2245 	if (!_php_curl_setopt(ch, options, zvalue, return_value TSRMLS_CC)) {
2246 		RETURN_TRUE;
2247 	} else {
2248 		RETURN_FALSE;
2249 	}
2250 }
2251 /* }}} */
2252 
2253 /* {{{ proto bool curl_setopt_array(resource ch, array options)
2254    Set an array of option for a cURL transfer */
PHP_FUNCTION(curl_setopt_array)2255 PHP_FUNCTION(curl_setopt_array)
2256 {
2257 	zval		*zid, *arr, **entry;
2258 	php_curl	*ch;
2259 	ulong		option;
2260 	HashPosition	pos;
2261 	char		*string_key;
2262 	uint		str_key_len;
2263 
2264 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "za", &zid, &arr) == FAILURE) {
2265 		return;
2266 	}
2267 
2268 	ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
2269 
2270 	zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos);
2271 	while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **)&entry, &pos) == SUCCESS) {
2272 		if (zend_hash_get_current_key_ex(Z_ARRVAL_P(arr), &string_key, &str_key_len, &option, 0, &pos) != HASH_KEY_IS_LONG) {
2273 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array keys must be CURLOPT constants or equivalent integer values");
2274 			RETURN_FALSE;
2275 		}
2276 		if (_php_curl_setopt(ch, (long) option, entry, return_value TSRMLS_CC)) {
2277 			RETURN_FALSE;
2278 		}
2279 		zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos);
2280 	}
2281 	RETURN_TRUE;
2282 }
2283 /* }}} */
2284 
2285 /* {{{ _php_curl_cleanup_handle(ch)
2286    Cleanup an execution phase */
_php_curl_cleanup_handle(php_curl * ch)2287 void _php_curl_cleanup_handle(php_curl *ch)
2288 {
2289 	if (ch->handlers->write->buf.len > 0) {
2290 		smart_str_free(&ch->handlers->write->buf);
2291 	}
2292 	if (ch->header.str_len) {
2293 		efree(ch->header.str);
2294 		ch->header.str_len = 0;
2295 	}
2296 
2297 	memset(ch->err.str, 0, CURL_ERROR_SIZE + 1);
2298 	ch->err.no = 0;
2299 }
2300 /* }}} */
2301 
2302 /* {{{ proto bool curl_exec(resource ch)
2303    Perform a cURL session */
PHP_FUNCTION(curl_exec)2304 PHP_FUNCTION(curl_exec)
2305 {
2306 	CURLcode	error;
2307 	zval		*zid;
2308 	php_curl	*ch;
2309 
2310 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
2311 		return;
2312 	}
2313 
2314 	ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
2315 
2316 	_php_curl_verify_handlers(ch, 1 TSRMLS_CC);
2317 
2318 	_php_curl_cleanup_handle(ch);
2319 
2320 	error = curl_easy_perform(ch->cp);
2321 	SAVE_CURL_ERROR(ch, error);
2322 	/* CURLE_PARTIAL_FILE is returned by HEAD requests */
2323 	if (error != CURLE_OK && error != CURLE_PARTIAL_FILE) {
2324 		if (ch->handlers->write->buf.len > 0) {
2325 			smart_str_free(&ch->handlers->write->buf);
2326 		}
2327 		RETURN_FALSE;
2328 	}
2329 
2330 	if (ch->handlers->std_err) {
2331 		php_stream  *stream;
2332 		stream = (php_stream*)zend_fetch_resource(&ch->handlers->std_err TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
2333 		if (stream) {
2334 			php_stream_flush(stream);
2335 		}
2336 	}
2337 
2338 	if (ch->handlers->write->method == PHP_CURL_RETURN && ch->handlers->write->buf.len > 0) {
2339 		smart_str_0(&ch->handlers->write->buf);
2340 		RETURN_STRINGL(ch->handlers->write->buf.c, ch->handlers->write->buf.len, 1);
2341 	}
2342 
2343 	/* flush the file handle, so any remaining data is synched to disk */
2344 	if (ch->handlers->write->method == PHP_CURL_FILE && ch->handlers->write->fp) {
2345 		fflush(ch->handlers->write->fp);
2346 	}
2347 	if (ch->handlers->write_header->method == PHP_CURL_FILE && ch->handlers->write_header->fp) {
2348 		fflush(ch->handlers->write_header->fp);
2349 	}
2350 
2351 	if (ch->handlers->write->method == PHP_CURL_RETURN) {
2352 		RETURN_EMPTY_STRING();
2353 	} else {
2354 		RETURN_TRUE;
2355 	}
2356 }
2357 /* }}} */
2358 
2359 /* {{{ proto mixed curl_getinfo(resource ch [, int option])
2360    Get information regarding a specific transfer */
PHP_FUNCTION(curl_getinfo)2361 PHP_FUNCTION(curl_getinfo)
2362 {
2363 	zval		*zid;
2364 	php_curl	*ch;
2365 	long		option = 0;
2366 
2367 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zid, &option) == FAILURE) {
2368 		return;
2369 	}
2370 
2371 	ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
2372 
2373 	if (ZEND_NUM_ARGS() < 2) {
2374 		char   *s_code;
2375 		long    l_code;
2376 		double  d_code;
2377 #if LIBCURL_VERSION_NUM >  0x071301
2378 		struct curl_certinfo *ci = NULL;
2379 		zval *listcode;
2380 #endif
2381 
2382 		array_init(return_value);
2383 
2384 		if (curl_easy_getinfo(ch->cp, CURLINFO_EFFECTIVE_URL, &s_code) == CURLE_OK) {
2385 			CAAS("url", s_code);
2386 		}
2387 		if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_TYPE, &s_code) == CURLE_OK) {
2388 			if (s_code != NULL) {
2389 				CAAS("content_type", s_code);
2390 			} else {
2391 				zval *retnull;
2392 				MAKE_STD_ZVAL(retnull);
2393 				ZVAL_NULL(retnull);
2394 				CAAZ("content_type", retnull);
2395 			}
2396 		}
2397 		if (curl_easy_getinfo(ch->cp, CURLINFO_HTTP_CODE, &l_code) == CURLE_OK) {
2398 			CAAL("http_code", l_code);
2399 		}
2400 		if (curl_easy_getinfo(ch->cp, CURLINFO_HEADER_SIZE, &l_code) == CURLE_OK) {
2401 			CAAL("header_size", l_code);
2402 		}
2403 		if (curl_easy_getinfo(ch->cp, CURLINFO_REQUEST_SIZE, &l_code) == CURLE_OK) {
2404 			CAAL("request_size", l_code);
2405 		}
2406 		if (curl_easy_getinfo(ch->cp, CURLINFO_FILETIME, &l_code) == CURLE_OK) {
2407 			CAAL("filetime", l_code);
2408 		}
2409 		if (curl_easy_getinfo(ch->cp, CURLINFO_SSL_VERIFYRESULT, &l_code) == CURLE_OK) {
2410 			CAAL("ssl_verify_result", l_code);
2411 		}
2412 		if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_COUNT, &l_code) == CURLE_OK) {
2413 			CAAL("redirect_count", l_code);
2414 		}
2415 		if (curl_easy_getinfo(ch->cp, CURLINFO_TOTAL_TIME, &d_code) == CURLE_OK) {
2416 			CAAD("total_time", d_code);
2417 		}
2418 		if (curl_easy_getinfo(ch->cp, CURLINFO_NAMELOOKUP_TIME, &d_code) == CURLE_OK) {
2419 			CAAD("namelookup_time", d_code);
2420 		}
2421 		if (curl_easy_getinfo(ch->cp, CURLINFO_CONNECT_TIME, &d_code) == CURLE_OK) {
2422 			CAAD("connect_time", d_code);
2423 		}
2424 		if (curl_easy_getinfo(ch->cp, CURLINFO_PRETRANSFER_TIME, &d_code) == CURLE_OK) {
2425 			CAAD("pretransfer_time", d_code);
2426 		}
2427 		if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_UPLOAD, &d_code) == CURLE_OK) {
2428 			CAAD("size_upload", d_code);
2429 		}
2430 		if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_DOWNLOAD, &d_code) == CURLE_OK) {
2431 			CAAD("size_download", d_code);
2432 		}
2433 		if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_DOWNLOAD, &d_code) == CURLE_OK) {
2434 			CAAD("speed_download", d_code);
2435 		}
2436 		if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_UPLOAD, &d_code) == CURLE_OK) {
2437 			CAAD("speed_upload", d_code);
2438 		}
2439 		if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d_code) == CURLE_OK) {
2440 			CAAD("download_content_length", d_code);
2441 		}
2442 		if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_UPLOAD, &d_code) == CURLE_OK) {
2443 			CAAD("upload_content_length", d_code);
2444 		}
2445 		if (curl_easy_getinfo(ch->cp, CURLINFO_STARTTRANSFER_TIME, &d_code) == CURLE_OK) {
2446 			CAAD("starttransfer_time", d_code);
2447 		}
2448 		if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_TIME, &d_code) == CURLE_OK) {
2449 			CAAD("redirect_time", d_code);
2450 		}
2451 #if LIBCURL_VERSION_NUM > 0x071301
2452 		if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
2453 			MAKE_STD_ZVAL(listcode);
2454 			array_init(listcode);
2455 			create_certinfo(ci, listcode TSRMLS_CC);
2456 			CAAZ("certinfo", listcode);
2457 		}
2458 #endif
2459 #if LIBCURL_VERSION_NUM >= 0x071202
2460 		if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_URL, &s_code) == CURLE_OK) {
2461 			CAAS("redirect_url", s_code);
2462 		}
2463 #endif
2464 		if (ch->header.str_len > 0) {
2465 			CAAS("request_header", ch->header.str);
2466 		}
2467 	} else {
2468 		switch (option) {
2469 			case CURLINFO_PRIVATE:
2470 			case CURLINFO_EFFECTIVE_URL:
2471 			case CURLINFO_CONTENT_TYPE:
2472 #if LIBCURL_VERSION_NUM >= 0x071202
2473 			case CURLINFO_REDIRECT_URL:
2474 #endif
2475 			{
2476 				char *s_code = NULL;
2477 
2478 				if (curl_easy_getinfo(ch->cp, option, &s_code) == CURLE_OK && s_code) {
2479 					RETURN_STRING(s_code, 1);
2480 				} else {
2481 					RETURN_FALSE;
2482 				}
2483 				break;
2484 			}
2485 			case CURLINFO_HTTP_CODE:
2486 			case CURLINFO_HEADER_SIZE:
2487 			case CURLINFO_REQUEST_SIZE:
2488 			case CURLINFO_FILETIME:
2489 			case CURLINFO_SSL_VERIFYRESULT:
2490 			case CURLINFO_REDIRECT_COUNT: {
2491 				long code = 0;
2492 
2493 				if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
2494 					RETURN_LONG(code);
2495 				} else {
2496 					RETURN_FALSE;
2497 				}
2498 				break;
2499 			}
2500 			case CURLINFO_TOTAL_TIME:
2501 			case CURLINFO_NAMELOOKUP_TIME:
2502 			case CURLINFO_CONNECT_TIME:
2503 			case CURLINFO_PRETRANSFER_TIME:
2504 			case CURLINFO_SIZE_UPLOAD:
2505 			case CURLINFO_SIZE_DOWNLOAD:
2506 			case CURLINFO_SPEED_DOWNLOAD:
2507 			case CURLINFO_SPEED_UPLOAD:
2508 			case CURLINFO_CONTENT_LENGTH_DOWNLOAD:
2509 			case CURLINFO_CONTENT_LENGTH_UPLOAD:
2510 			case CURLINFO_STARTTRANSFER_TIME:
2511 			case CURLINFO_REDIRECT_TIME: {
2512 				double code = 0.0;
2513 
2514 				if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
2515 					RETURN_DOUBLE(code);
2516 				} else {
2517 					RETURN_FALSE;
2518 				}
2519 				break;
2520 			}
2521 			case CURLINFO_HEADER_OUT:
2522 				if (ch->header.str_len > 0) {
2523 					RETURN_STRINGL(ch->header.str, ch->header.str_len, 1);
2524 				} else {
2525 					RETURN_FALSE;
2526 				}
2527 #if LIBCURL_VERSION_NUM > 0x071301
2528 			case CURLINFO_CERTINFO: {
2529 				struct curl_certinfo *ci = NULL;
2530 
2531 				array_init(return_value);
2532 
2533 				if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
2534 					create_certinfo(ci, return_value TSRMLS_CC);
2535 				} else {
2536 					RETURN_FALSE;
2537 				}
2538 				break;
2539 			}
2540 #endif
2541 		}
2542 	}
2543 }
2544 /* }}} */
2545 
2546 /* {{{ proto string curl_error(resource ch)
2547    Return a string contain the last error for the current session */
PHP_FUNCTION(curl_error)2548 PHP_FUNCTION(curl_error)
2549 {
2550 	zval		*zid;
2551 	php_curl	*ch;
2552 
2553 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
2554 		return;
2555 	}
2556 
2557 	ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
2558 
2559 	ch->err.str[CURL_ERROR_SIZE] = 0;
2560 	RETURN_STRING(ch->err.str, 1);
2561 }
2562 /* }}} */
2563 
2564 /* {{{ proto int curl_errno(resource ch)
2565    Return an integer containing the last error number */
PHP_FUNCTION(curl_errno)2566 PHP_FUNCTION(curl_errno)
2567 {
2568 	zval		*zid;
2569 	php_curl	*ch;
2570 
2571 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
2572 		return;
2573 	}
2574 
2575 	ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
2576 
2577 	RETURN_LONG(ch->err.no);
2578 }
2579 /* }}} */
2580 
2581 /* {{{ proto void curl_close(resource ch)
2582    Close a cURL session */
PHP_FUNCTION(curl_close)2583 PHP_FUNCTION(curl_close)
2584 {
2585 	zval		*zid;
2586 	php_curl	*ch;
2587 
2588 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
2589 		return;
2590 	}
2591 
2592 	ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
2593 
2594 	if (ch->in_callback) {
2595 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to close cURL handle from a callback");
2596 		return;
2597 	}
2598 
2599 	if (ch->uses) {
2600 		ch->uses--;
2601 	} else {
2602 		zend_list_delete(Z_LVAL_P(zid));
2603 	}
2604 }
2605 /* }}} */
2606 
2607 /* {{{ _php_curl_close()
2608    List destructor for curl handles */
_php_curl_close_ex(php_curl * ch TSRMLS_DC)2609 static void _php_curl_close_ex(php_curl *ch TSRMLS_DC)
2610 {
2611 #if PHP_CURL_DEBUG
2612 	fprintf(stderr, "DTOR CALLED, ch = %x\n", ch);
2613 #endif
2614 
2615 	_php_curl_verify_handlers(ch, 0 TSRMLS_CC);
2616 
2617 	/*
2618 	 * Libcurl is doing connection caching. When easy handle is cleaned up,
2619 	 * if the handle was previously used by the curl_multi_api, the connection
2620 	 * remains open un the curl multi handle is cleaned up. Some protocols are
2621 	 * sending content like the FTP one, and libcurl try to use the
2622 	 * WRITEFUNCTION or the HEADERFUNCTION. Since structures used in those
2623 	 * callback are freed, we need to use an other callback to which avoid
2624 	 * segfaults.
2625 	 *
2626 	 * Libcurl commit d021f2e8a00 fix this issue and should be part of 7.28.2
2627 	 */
2628 	curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION, curl_write_nothing);
2629 	curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION, curl_write_nothing);
2630 
2631 	curl_easy_cleanup(ch->cp);
2632 
2633 	/* cURL destructors should be invoked only by last curl handle */
2634 	if (Z_REFCOUNT_P(ch->clone) <= 1) {
2635 		zend_llist_clean(&ch->to_free->str);
2636 		zend_llist_clean(&ch->to_free->slist);
2637 		zend_llist_clean(&ch->to_free->post);
2638 		efree(ch->to_free);
2639 		FREE_ZVAL(ch->clone);
2640 	} else {
2641 		Z_DELREF_P(ch->clone);
2642 	}
2643 
2644 	if (ch->handlers->write->buf.len > 0) {
2645 		smart_str_free(&ch->handlers->write->buf);
2646 	}
2647 	if (ch->handlers->write->func_name) {
2648 		zval_ptr_dtor(&ch->handlers->write->func_name);
2649 	}
2650 	if (ch->handlers->read->func_name) {
2651 		zval_ptr_dtor(&ch->handlers->read->func_name);
2652 	}
2653 	if (ch->handlers->write_header->func_name) {
2654 		zval_ptr_dtor(&ch->handlers->write_header->func_name);
2655 	}
2656 	if (ch->handlers->progress->func_name) {
2657 		zval_ptr_dtor(&ch->handlers->progress->func_name);
2658 	}
2659 	if (ch->handlers->passwd) {
2660 		zval_ptr_dtor(&ch->handlers->passwd);
2661 	}
2662 	if (ch->handlers->std_err) {
2663 		zval_ptr_dtor(&ch->handlers->std_err);
2664 	}
2665 	if (ch->header.str_len > 0) {
2666 		efree(ch->header.str);
2667 	}
2668 
2669 	if (ch->handlers->write_header->stream) {
2670 		zval_ptr_dtor(&ch->handlers->write_header->stream);
2671 	}
2672 	if (ch->handlers->write->stream) {
2673 		zval_ptr_dtor(&ch->handlers->write->stream);
2674 	}
2675 	if (ch->handlers->read->stream) {
2676 		zval_ptr_dtor(&ch->handlers->read->stream);
2677 	}
2678 
2679 	efree(ch->handlers->write);
2680 	efree(ch->handlers->write_header);
2681 	efree(ch->handlers->read);
2682 	efree(ch->handlers->progress);
2683 	efree(ch->handlers);
2684 	efree(ch);
2685 }
2686 /* }}} */
2687 
2688 /* {{{ _php_curl_close()
2689    List destructor for curl handles */
_php_curl_close(zend_rsrc_list_entry * rsrc TSRMLS_DC)2690 static void _php_curl_close(zend_rsrc_list_entry *rsrc TSRMLS_DC)
2691 {
2692 	php_curl *ch = (php_curl *) rsrc->ptr;
2693 	_php_curl_close_ex(ch TSRMLS_CC);
2694 }
2695 /* }}} */
2696 
2697 #endif /* HAVE_CURL */
2698 
2699 /*
2700  * Local variables:
2701  * tab-width: 4
2702  * c-basic-offset: 4
2703  * End:
2704  * vim600: fdm=marker
2705  * vim: noet sw=4 ts=4
2706  */
2707