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