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