1/*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2015 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author: Sterling Hughes <sterling@php.net>                           |
16   +----------------------------------------------------------------------+
17*/
18
19/* $Id$ */
20
21#define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "php.h"
28
29#if HAVE_CURL
30
31#include <stdio.h>
32#include <string.h>
33
34#ifdef PHP_WIN32
35#include <winsock2.h>
36#include <sys/types.h>
37#endif
38
39#include <curl/curl.h>
40#include <curl/easy.h>
41
42/* As of curl 7.11.1 this is no longer defined inside curl.h */
43#ifndef HttpPost
44#define HttpPost curl_httppost
45#endif
46
47/* {{{ cruft for thread safe SSL crypto locks */
48#if defined(ZTS) && defined(HAVE_CURL_SSL)
49# ifdef PHP_WIN32
50#  define PHP_CURL_NEED_OPENSSL_TSL
51#  include <openssl/crypto.h>
52# else /* !PHP_WIN32 */
53#  if defined(HAVE_CURL_OPENSSL)
54#   if defined(HAVE_OPENSSL_CRYPTO_H)
55#    define PHP_CURL_NEED_OPENSSL_TSL
56#    include <openssl/crypto.h>
57#   else
58#    warning \
59    "libcurl was compiled with OpenSSL support, but configure could not find " \
60    "openssl/crypto.h; thus no SSL crypto locking callbacks will be set, which may " \
61    "cause random crashes on SSL requests"
62#   endif
63#  elif defined(HAVE_CURL_GNUTLS)
64#   if defined(HAVE_GCRYPT_H)
65#    define PHP_CURL_NEED_GNUTLS_TSL
66#    include <gcrypt.h>
67#   else
68#    warning \
69    "libcurl was compiled with GnuTLS support, but configure could not find " \
70    "gcrypt.h; thus no SSL crypto locking callbacks will be set, which may " \
71    "cause random crashes on SSL requests"
72#   endif
73#  else
74#   warning \
75    "libcurl was compiled with SSL support, but configure could not determine which" \
76    "library was used; thus no SSL crypto locking callbacks will be set, which may " \
77    "cause random crashes on SSL requests"
78#  endif /* HAVE_CURL_OPENSSL || HAVE_CURL_GNUTLS */
79# endif /* PHP_WIN32 */
80#endif /* ZTS && HAVE_CURL_SSL */
81/* }}} */
82
83#define SMART_STR_PREALLOC 4096
84
85#include "ext/standard/php_smart_str.h"
86#include "ext/standard/info.h"
87#include "ext/standard/file.h"
88#include "ext/standard/url.h"
89#include "php_curl.h"
90
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#if LIBCURL_VERSION_NUM >= 0x071200 /* Available since 7.18.0 */
849    REGISTER_CURL_CONSTANT(CURLPROXY_SOCKS4A);
850    REGISTER_CURL_CONSTANT(CURLPROXY_SOCKS5_HOSTNAME);
851#endif
852
853    /* Curl Share constants */
854    REGISTER_CURL_CONSTANT(CURLSHOPT_NONE);
855    REGISTER_CURL_CONSTANT(CURLSHOPT_SHARE);
856    REGISTER_CURL_CONSTANT(CURLSHOPT_UNSHARE);
857
858    /* Curl Http Version constants (CURLOPT_HTTP_VERSION) */
859    REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_1_0);
860    REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_1_1);
861#if LIBCURL_VERSION_NUM >= 0x072100 /* 7.33.0 */
862    REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_2_0);
863#endif
864    REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_NONE);
865
866    /* Curl Lock constants */
867    REGISTER_CURL_CONSTANT(CURL_LOCK_DATA_COOKIE);
868    REGISTER_CURL_CONSTANT(CURL_LOCK_DATA_DNS);
869    REGISTER_CURL_CONSTANT(CURL_LOCK_DATA_SSL_SESSION);
870
871    /* Curl NETRC constants (CURLOPT_NETRC) */
872    REGISTER_CURL_CONSTANT(CURL_NETRC_IGNORED);
873    REGISTER_CURL_CONSTANT(CURL_NETRC_OPTIONAL);
874    REGISTER_CURL_CONSTANT(CURL_NETRC_REQUIRED);
875
876    /* Curl SSL Version constants (CURLOPT_SSLVERSION) */
877    REGISTER_CURL_CONSTANT(CURL_SSLVERSION_DEFAULT);
878    REGISTER_CURL_CONSTANT(CURL_SSLVERSION_SSLv2);
879    REGISTER_CURL_CONSTANT(CURL_SSLVERSION_SSLv3);
880    REGISTER_CURL_CONSTANT(CURL_SSLVERSION_TLSv1);
881
882    /* Curl TIMECOND constants (CURLOPT_TIMECONDITION) */
883    REGISTER_CURL_CONSTANT(CURL_TIMECOND_IFMODSINCE);
884    REGISTER_CURL_CONSTANT(CURL_TIMECOND_IFUNMODSINCE);
885    REGISTER_CURL_CONSTANT(CURL_TIMECOND_LASTMOD);
886    REGISTER_CURL_CONSTANT(CURL_TIMECOND_NONE);
887
888    /* Curl version constants */
889    REGISTER_CURL_CONSTANT(CURL_VERSION_IPV6);
890    REGISTER_CURL_CONSTANT(CURL_VERSION_KERBEROS4);
891    REGISTER_CURL_CONSTANT(CURL_VERSION_LIBZ);
892    REGISTER_CURL_CONSTANT(CURL_VERSION_SSL);
893#if LIBCURL_VERSION_NUM >= 0x072100 /* 7.33.0 */
894    REGISTER_CURL_CONSTANT(CURL_VERSION_HTTP2);
895#endif
896
897#if LIBCURL_VERSION_NUM >= 0x070a06 /* Available since 7.10.6 */
898    REGISTER_CURL_CONSTANT(CURLOPT_HTTPAUTH);
899    /* http authentication options */
900    REGISTER_CURL_CONSTANT(CURLAUTH_ANY);
901    REGISTER_CURL_CONSTANT(CURLAUTH_ANYSAFE);
902    REGISTER_CURL_CONSTANT(CURLAUTH_BASIC);
903    REGISTER_CURL_CONSTANT(CURLAUTH_DIGEST);
904    REGISTER_CURL_CONSTANT(CURLAUTH_GSSNEGOTIATE);
905    REGISTER_CURL_CONSTANT(CURLAUTH_NONE);
906    REGISTER_CURL_CONSTANT(CURLAUTH_NTLM);
907#endif
908
909#if LIBCURL_VERSION_NUM >= 0x070a07 /* Available since 7.10.7 */
910    REGISTER_CURL_CONSTANT(CURLINFO_HTTP_CONNECTCODE);
911    REGISTER_CURL_CONSTANT(CURLOPT_FTP_CREATE_MISSING_DIRS);
912    REGISTER_CURL_CONSTANT(CURLOPT_PROXYAUTH);
913#endif
914
915#if LIBCURL_VERSION_NUM >= 0x070a08 /* Available since 7.10.8 */
916    REGISTER_CURL_CONSTANT(CURLE_FILESIZE_EXCEEDED);
917    REGISTER_CURL_CONSTANT(CURLE_LDAP_INVALID_URL);
918    REGISTER_CURL_CONSTANT(CURLINFO_HTTPAUTH_AVAIL);
919    REGISTER_CURL_CONSTANT(CURLINFO_RESPONSE_CODE);
920    REGISTER_CURL_CONSTANT(CURLINFO_PROXYAUTH_AVAIL);
921    REGISTER_CURL_CONSTANT(CURLOPT_FTP_RESPONSE_TIMEOUT);
922    REGISTER_CURL_CONSTANT(CURLOPT_IPRESOLVE);
923    REGISTER_CURL_CONSTANT(CURLOPT_MAXFILESIZE);
924    REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_V4);
925    REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_V6);
926    REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_WHATEVER);
927#endif
928
929#if LIBCURL_VERSION_NUM >= 0x070b00 /* Available since 7.11.0 */
930    REGISTER_CURL_CONSTANT(CURLE_FTP_SSL_FAILED);
931    REGISTER_CURL_CONSTANT(CURLFTPSSL_ALL);
932    REGISTER_CURL_CONSTANT(CURLFTPSSL_CONTROL);
933    REGISTER_CURL_CONSTANT(CURLFTPSSL_NONE);
934    REGISTER_CURL_CONSTANT(CURLFTPSSL_TRY);
935    REGISTER_CURL_CONSTANT(CURLOPT_FTP_SSL);
936    REGISTER_CURL_CONSTANT(CURLOPT_NETRC_FILE);
937#endif
938
939#if LIBCURL_VERSION_NUM >= 0x070c02 /* Available since 7.12.2 */
940    REGISTER_CURL_CONSTANT(CURLFTPAUTH_DEFAULT);
941    REGISTER_CURL_CONSTANT(CURLFTPAUTH_SSL);
942    REGISTER_CURL_CONSTANT(CURLFTPAUTH_TLS);
943    REGISTER_CURL_CONSTANT(CURLOPT_FTPSSLAUTH);
944#endif
945
946#if LIBCURL_VERSION_NUM >= 0x070d00 /* Available since 7.13.0 */
947    REGISTER_CURL_CONSTANT(CURLOPT_FTP_ACCOUNT);
948#endif
949
950#if LIBCURL_VERSION_NUM >= 0x070b02 /* Available since 7.11.2 */
951    REGISTER_CURL_CONSTANT(CURLOPT_TCP_NODELAY);
952#endif
953
954#if LIBCURL_VERSION_NUM >= 0x070c02 /* Available since 7.12.2 */
955    REGISTER_CURL_CONSTANT(CURLINFO_OS_ERRNO);
956#endif
957
958#if LIBCURL_VERSION_NUM >= 0x070c03 /* Available since 7.12.3 */
959    REGISTER_CURL_CONSTANT(CURLINFO_NUM_CONNECTS);
960    REGISTER_CURL_CONSTANT(CURLINFO_SSL_ENGINES);
961#endif
962
963#if LIBCURL_VERSION_NUM >= 0x070e01 /* Available since 7.14.1 */
964    REGISTER_CURL_CONSTANT(CURLINFO_COOKIELIST);
965    REGISTER_CURL_CONSTANT(CURLOPT_COOKIELIST);
966    REGISTER_CURL_CONSTANT(CURLOPT_IGNORE_CONTENT_LENGTH);
967#endif
968
969#if LIBCURL_VERSION_NUM >= 0x070f00 /* Available since 7.15.0 */
970    REGISTER_CURL_CONSTANT(CURLOPT_FTP_SKIP_PASV_IP);
971#endif
972
973#if LIBCURL_VERSION_NUM >= 0x070f01 /* Available since 7.15.1 */
974    REGISTER_CURL_CONSTANT(CURLOPT_FTP_FILEMETHOD);
975#endif
976
977#if LIBCURL_VERSION_NUM >= 0x070f02 /* Available since 7.15.2 */
978    REGISTER_CURL_CONSTANT(CURLOPT_CONNECT_ONLY);
979    REGISTER_CURL_CONSTANT(CURLOPT_LOCALPORT);
980    REGISTER_CURL_CONSTANT(CURLOPT_LOCALPORTRANGE);
981#endif
982
983#if LIBCURL_VERSION_NUM >= 0x070f03 /* Available since 7.15.3 */
984    REGISTER_CURL_CONSTANT(CURLFTPMETHOD_MULTICWD);
985    REGISTER_CURL_CONSTANT(CURLFTPMETHOD_NOCWD);
986    REGISTER_CURL_CONSTANT(CURLFTPMETHOD_SINGLECWD);
987#endif
988
989#if LIBCURL_VERSION_NUM >= 0x070f04 /* Available since 7.15.4 */
990    REGISTER_CURL_CONSTANT(CURLINFO_FTP_ENTRY_PATH);
991#endif
992
993#if LIBCURL_VERSION_NUM >= 0x070f05 /* Available since 7.15.5 */
994    REGISTER_CURL_CONSTANT(CURLOPT_FTP_ALTERNATIVE_TO_USER);
995    REGISTER_CURL_CONSTANT(CURLOPT_MAX_RECV_SPEED_LARGE);
996    REGISTER_CURL_CONSTANT(CURLOPT_MAX_SEND_SPEED_LARGE);
997#endif
998
999#if LIBCURL_VERSION_NUM >= 0x071000 /* Available since 7.16.0 */
1000    REGISTER_CURL_CONSTANT(CURLOPT_SSL_SESSIONID_CACHE);
1001    REGISTER_CURL_CONSTANT(CURLMOPT_PIPELINING);
1002#endif
1003
1004#if LIBCURL_VERSION_NUM >= 0x071001 /* Available since 7.16.1 */
1005    REGISTER_CURL_CONSTANT(CURLE_SSH);
1006    REGISTER_CURL_CONSTANT(CURLOPT_FTP_SSL_CCC);
1007    REGISTER_CURL_CONSTANT(CURLOPT_SSH_AUTH_TYPES);
1008    REGISTER_CURL_CONSTANT(CURLOPT_SSH_PRIVATE_KEYFILE);
1009    REGISTER_CURL_CONSTANT(CURLOPT_SSH_PUBLIC_KEYFILE);
1010    REGISTER_CURL_CONSTANT(CURLFTPSSL_CCC_ACTIVE);
1011    REGISTER_CURL_CONSTANT(CURLFTPSSL_CCC_NONE);
1012    REGISTER_CURL_CONSTANT(CURLFTPSSL_CCC_PASSIVE);
1013#endif
1014
1015#if LIBCURL_VERSION_NUM >= 0x071002 /* Available since 7.16.2 */
1016    REGISTER_CURL_CONSTANT(CURLOPT_CONNECTTIMEOUT_MS);
1017    REGISTER_CURL_CONSTANT(CURLOPT_HTTP_CONTENT_DECODING);
1018    REGISTER_CURL_CONSTANT(CURLOPT_HTTP_TRANSFER_DECODING);
1019    REGISTER_CURL_CONSTANT(CURLOPT_TIMEOUT_MS);
1020#endif
1021
1022#if LIBCURL_VERSION_NUM >= 0x071003 /* Available since 7.16.3 */
1023    REGISTER_CURL_CONSTANT(CURLMOPT_MAXCONNECTS);
1024#endif
1025
1026#if LIBCURL_VERSION_NUM >= 0x071004 /* Available since 7.16.4 */
1027    REGISTER_CURL_CONSTANT(CURLOPT_KRBLEVEL);
1028    REGISTER_CURL_CONSTANT(CURLOPT_NEW_DIRECTORY_PERMS);
1029    REGISTER_CURL_CONSTANT(CURLOPT_NEW_FILE_PERMS);
1030#endif
1031
1032#if LIBCURL_VERSION_NUM >= 0x071100 /* Available since 7.17.0 */
1033    REGISTER_CURL_CONSTANT(CURLOPT_APPEND);
1034    REGISTER_CURL_CONSTANT(CURLOPT_DIRLISTONLY);
1035    REGISTER_CURL_CONSTANT(CURLOPT_USE_SSL);
1036    /* Curl SSL Constants */
1037    REGISTER_CURL_CONSTANT(CURLUSESSL_ALL);
1038    REGISTER_CURL_CONSTANT(CURLUSESSL_CONTROL);
1039    REGISTER_CURL_CONSTANT(CURLUSESSL_NONE);
1040    REGISTER_CURL_CONSTANT(CURLUSESSL_TRY);
1041#endif
1042
1043#if LIBCURL_VERSION_NUM >= 0x071101 /* Available since 7.17.1 */
1044    REGISTER_CURL_CONSTANT(CURLOPT_SSH_HOST_PUBLIC_KEY_MD5);
1045#endif
1046
1047#if LIBCURL_VERSION_NUM >= 0x071200 /* Available since 7.18.0 */
1048    REGISTER_CURL_CONSTANT(CURLOPT_PROXY_TRANSFER_MODE);
1049    REGISTER_CURL_CONSTANT(CURLPAUSE_ALL);
1050    REGISTER_CURL_CONSTANT(CURLPAUSE_CONT);
1051    REGISTER_CURL_CONSTANT(CURLPAUSE_RECV);
1052    REGISTER_CURL_CONSTANT(CURLPAUSE_RECV_CONT);
1053    REGISTER_CURL_CONSTANT(CURLPAUSE_SEND);
1054    REGISTER_CURL_CONSTANT(CURLPAUSE_SEND_CONT);
1055    REGISTER_CURL_CONSTANT(CURL_READFUNC_PAUSE);
1056    REGISTER_CURL_CONSTANT(CURL_WRITEFUNC_PAUSE);
1057#endif
1058
1059#if LIBCURL_VERSION_NUM >= 0x071202 /* Available since 7.18.2 */
1060    REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_URL);
1061#endif
1062
1063#if LIBCURL_VERSION_NUM >= 0x071300 /* Available since 7.19.0 */
1064    REGISTER_CURL_CONSTANT(CURLINFO_APPCONNECT_TIME);
1065    REGISTER_CURL_CONSTANT(CURLINFO_PRIMARY_IP);
1066
1067    REGISTER_CURL_CONSTANT(CURLOPT_ADDRESS_SCOPE);
1068    REGISTER_CURL_CONSTANT(CURLOPT_CRLFILE);
1069    REGISTER_CURL_CONSTANT(CURLOPT_ISSUERCERT);
1070    REGISTER_CURL_CONSTANT(CURLOPT_KEYPASSWD);
1071
1072    REGISTER_CURL_CONSTANT(CURLSSH_AUTH_ANY);
1073    REGISTER_CURL_CONSTANT(CURLSSH_AUTH_DEFAULT);
1074    REGISTER_CURL_CONSTANT(CURLSSH_AUTH_HOST);
1075    REGISTER_CURL_CONSTANT(CURLSSH_AUTH_KEYBOARD);
1076    REGISTER_CURL_CONSTANT(CURLSSH_AUTH_NONE);
1077    REGISTER_CURL_CONSTANT(CURLSSH_AUTH_PASSWORD);
1078    REGISTER_CURL_CONSTANT(CURLSSH_AUTH_PUBLICKEY);
1079#endif
1080
1081#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
1082    REGISTER_CURL_CONSTANT(CURLINFO_CERTINFO);
1083    REGISTER_CURL_CONSTANT(CURLOPT_CERTINFO);
1084    REGISTER_CURL_CONSTANT(CURLOPT_PASSWORD);
1085    REGISTER_CURL_CONSTANT(CURLOPT_POSTREDIR);
1086    REGISTER_CURL_CONSTANT(CURLOPT_PROXYPASSWORD);
1087    REGISTER_CURL_CONSTANT(CURLOPT_PROXYUSERNAME);
1088    REGISTER_CURL_CONSTANT(CURLOPT_USERNAME);
1089#endif
1090
1091#if LIBCURL_VERSION_NUM >= 0x071303 /* Available since 7.19.3 */
1092    REGISTER_CURL_CONSTANT(CURLAUTH_DIGEST_IE);
1093#endif
1094
1095#if LIBCURL_VERSION_NUM >= 0x071304 /* Available since 7.19.4 */
1096    REGISTER_CURL_CONSTANT(CURLINFO_CONDITION_UNMET);
1097
1098    REGISTER_CURL_CONSTANT(CURLOPT_NOPROXY);
1099    REGISTER_CURL_CONSTANT(CURLOPT_PROTOCOLS);
1100    REGISTER_CURL_CONSTANT(CURLOPT_REDIR_PROTOCOLS);
1101    REGISTER_CURL_CONSTANT(CURLOPT_SOCKS5_GSSAPI_NEC);
1102    REGISTER_CURL_CONSTANT(CURLOPT_SOCKS5_GSSAPI_SERVICE);
1103    REGISTER_CURL_CONSTANT(CURLOPT_TFTP_BLKSIZE);
1104
1105    REGISTER_CURL_CONSTANT(CURLPROTO_ALL);
1106    REGISTER_CURL_CONSTANT(CURLPROTO_DICT);
1107    REGISTER_CURL_CONSTANT(CURLPROTO_FILE);
1108    REGISTER_CURL_CONSTANT(CURLPROTO_FTP);
1109    REGISTER_CURL_CONSTANT(CURLPROTO_FTPS);
1110    REGISTER_CURL_CONSTANT(CURLPROTO_HTTP);
1111    REGISTER_CURL_CONSTANT(CURLPROTO_HTTPS);
1112    REGISTER_CURL_CONSTANT(CURLPROTO_LDAP);
1113    REGISTER_CURL_CONSTANT(CURLPROTO_LDAPS);
1114    REGISTER_CURL_CONSTANT(CURLPROTO_SCP);
1115    REGISTER_CURL_CONSTANT(CURLPROTO_SFTP);
1116    REGISTER_CURL_CONSTANT(CURLPROTO_TELNET);
1117    REGISTER_CURL_CONSTANT(CURLPROTO_TFTP);
1118#endif
1119
1120#if LIBCURL_VERSION_NUM >= 0x071306 /* Available since 7.19.6 */
1121    REGISTER_CURL_CONSTANT(CURLOPT_SSH_KNOWNHOSTS);
1122#endif
1123
1124#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
1125    REGISTER_CURL_CONSTANT(CURLINFO_RTSP_CLIENT_CSEQ);
1126    REGISTER_CURL_CONSTANT(CURLINFO_RTSP_CSEQ_RECV);
1127    REGISTER_CURL_CONSTANT(CURLINFO_RTSP_SERVER_CSEQ);
1128    REGISTER_CURL_CONSTANT(CURLINFO_RTSP_SESSION_ID);
1129    REGISTER_CURL_CONSTANT(CURLOPT_FTP_USE_PRET);
1130    REGISTER_CURL_CONSTANT(CURLOPT_MAIL_FROM);
1131    REGISTER_CURL_CONSTANT(CURLOPT_MAIL_RCPT);
1132    REGISTER_CURL_CONSTANT(CURLOPT_RTSP_CLIENT_CSEQ);
1133    REGISTER_CURL_CONSTANT(CURLOPT_RTSP_REQUEST);
1134    REGISTER_CURL_CONSTANT(CURLOPT_RTSP_SERVER_CSEQ);
1135    REGISTER_CURL_CONSTANT(CURLOPT_RTSP_SESSION_ID);
1136    REGISTER_CURL_CONSTANT(CURLOPT_RTSP_STREAM_URI);
1137    REGISTER_CURL_CONSTANT(CURLOPT_RTSP_TRANSPORT);
1138    REGISTER_CURL_CONSTANT(CURLPROTO_IMAP);
1139    REGISTER_CURL_CONSTANT(CURLPROTO_IMAPS);
1140    REGISTER_CURL_CONSTANT(CURLPROTO_POP3);
1141    REGISTER_CURL_CONSTANT(CURLPROTO_POP3S);
1142    REGISTER_CURL_CONSTANT(CURLPROTO_RTSP);
1143    REGISTER_CURL_CONSTANT(CURLPROTO_SMTP);
1144    REGISTER_CURL_CONSTANT(CURLPROTO_SMTPS);
1145    REGISTER_CURL_CONSTANT(CURL_RTSPREQ_ANNOUNCE);
1146    REGISTER_CURL_CONSTANT(CURL_RTSPREQ_DESCRIBE);
1147    REGISTER_CURL_CONSTANT(CURL_RTSPREQ_GET_PARAMETER);
1148    REGISTER_CURL_CONSTANT(CURL_RTSPREQ_OPTIONS);
1149    REGISTER_CURL_CONSTANT(CURL_RTSPREQ_PAUSE);
1150    REGISTER_CURL_CONSTANT(CURL_RTSPREQ_PLAY);
1151    REGISTER_CURL_CONSTANT(CURL_RTSPREQ_RECEIVE);
1152    REGISTER_CURL_CONSTANT(CURL_RTSPREQ_RECORD);
1153    REGISTER_CURL_CONSTANT(CURL_RTSPREQ_SETUP);
1154    REGISTER_CURL_CONSTANT(CURL_RTSPREQ_SET_PARAMETER);
1155    REGISTER_CURL_CONSTANT(CURL_RTSPREQ_TEARDOWN);
1156#endif
1157
1158#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
1159    REGISTER_CURL_CONSTANT(CURLINFO_LOCAL_IP);
1160    REGISTER_CURL_CONSTANT(CURLINFO_LOCAL_PORT);
1161    REGISTER_CURL_CONSTANT(CURLINFO_PRIMARY_PORT);
1162    REGISTER_CURL_CONSTANT(CURLOPT_FNMATCH_FUNCTION);
1163    REGISTER_CURL_CONSTANT(CURLOPT_WILDCARDMATCH);
1164    REGISTER_CURL_CONSTANT(CURLPROTO_RTMP);
1165    REGISTER_CURL_CONSTANT(CURLPROTO_RTMPE);
1166    REGISTER_CURL_CONSTANT(CURLPROTO_RTMPS);
1167    REGISTER_CURL_CONSTANT(CURLPROTO_RTMPT);
1168    REGISTER_CURL_CONSTANT(CURLPROTO_RTMPTE);
1169    REGISTER_CURL_CONSTANT(CURLPROTO_RTMPTS);
1170    REGISTER_CURL_CONSTANT(CURL_FNMATCHFUNC_FAIL);
1171    REGISTER_CURL_CONSTANT(CURL_FNMATCHFUNC_MATCH);
1172    REGISTER_CURL_CONSTANT(CURL_FNMATCHFUNC_NOMATCH);
1173#endif
1174
1175#if LIBCURL_VERSION_NUM >= 0x071502 /* Available since 7.21.2 */
1176    REGISTER_CURL_CONSTANT(CURLPROTO_GOPHER);
1177#endif
1178
1179#if LIBCURL_VERSION_NUM >= 0x071503 /* Available since 7.21.3 */
1180    REGISTER_CURL_CONSTANT(CURLAUTH_ONLY);
1181    REGISTER_CURL_CONSTANT(CURLOPT_RESOLVE);
1182#endif
1183
1184#if LIBCURL_VERSION_NUM >= 0x071504 /* Available since 7.21.4 */
1185    REGISTER_CURL_CONSTANT(CURLOPT_TLSAUTH_PASSWORD);
1186    REGISTER_CURL_CONSTANT(CURLOPT_TLSAUTH_TYPE);
1187    REGISTER_CURL_CONSTANT(CURLOPT_TLSAUTH_USERNAME);
1188    REGISTER_CURL_CONSTANT(CURL_TLSAUTH_SRP);
1189#endif
1190
1191#if LIBCURL_VERSION_NUM >= 0x071506 /* Available since 7.21.6 */
1192    REGISTER_CURL_CONSTANT(CURLOPT_ACCEPT_ENCODING);
1193    REGISTER_CURL_CONSTANT(CURLOPT_TRANSFER_ENCODING);
1194#endif
1195
1196#if LIBCURL_VERSION_NUM >= 0x071600 /* Available since 7.22.0 */
1197    REGISTER_CURL_CONSTANT(CURLGSSAPI_DELEGATION_FLAG);
1198    REGISTER_CURL_CONSTANT(CURLGSSAPI_DELEGATION_POLICY_FLAG);
1199    REGISTER_CURL_CONSTANT(CURLOPT_GSSAPI_DELEGATION);
1200#endif
1201
1202#if LIBCURL_VERSION_NUM >= 0x071800 /* Available since 7.24.0 */
1203    REGISTER_CURL_CONSTANT(CURLOPT_ACCEPTTIMEOUT_MS);
1204    REGISTER_CURL_CONSTANT(CURLOPT_DNS_SERVERS);
1205#endif
1206
1207#if LIBCURL_VERSION_NUM >= 0x071900 /* Available since 7.25.0 */
1208    REGISTER_CURL_CONSTANT(CURLOPT_MAIL_AUTH);
1209    REGISTER_CURL_CONSTANT(CURLOPT_SSL_OPTIONS);
1210    REGISTER_CURL_CONSTANT(CURLOPT_TCP_KEEPALIVE);
1211    REGISTER_CURL_CONSTANT(CURLOPT_TCP_KEEPIDLE);
1212    REGISTER_CURL_CONSTANT(CURLOPT_TCP_KEEPINTVL);
1213    REGISTER_CURL_CONSTANT(CURLSSLOPT_ALLOW_BEAST);
1214#endif
1215
1216#if LIBCURL_VERSION_NUM >= 0x072200 /* Available since 7.34.0 */
1217    REGISTER_CURL_CONSTANT(CURL_SSLVERSION_TLSv1_0);
1218    REGISTER_CURL_CONSTANT(CURL_SSLVERSION_TLSv1_1);
1219    REGISTER_CURL_CONSTANT(CURL_SSLVERSION_TLSv1_2);
1220#endif
1221
1222#if CURLOPT_FTPASCII != 0
1223    REGISTER_CURL_CONSTANT(CURLOPT_FTPASCII);
1224#endif
1225#if CURLOPT_MUTE != 0
1226    REGISTER_CURL_CONSTANT(CURLOPT_MUTE);
1227#endif
1228#if CURLOPT_PASSWDFUNCTION != 0
1229    REGISTER_CURL_CONSTANT(CURLOPT_PASSWDFUNCTION);
1230#endif
1231    REGISTER_CURL_CONSTANT(CURLOPT_SAFE_UPLOAD);
1232
1233#ifdef PHP_CURL_NEED_OPENSSL_TSL
1234    if (!CRYPTO_get_id_callback()) {
1235        int i, c = CRYPTO_num_locks();
1236
1237        php_curl_openssl_tsl = malloc(c * sizeof(MUTEX_T));
1238        if (!php_curl_openssl_tsl) {
1239            return FAILURE;
1240        }
1241
1242        for (i = 0; i < c; ++i) {
1243            php_curl_openssl_tsl[i] = tsrm_mutex_alloc();
1244        }
1245
1246        CRYPTO_set_id_callback(php_curl_ssl_id);
1247        CRYPTO_set_locking_callback(php_curl_ssl_lock);
1248    }
1249#endif
1250#ifdef PHP_CURL_NEED_GNUTLS_TSL
1251    gcry_control(GCRYCTL_SET_THREAD_CBS, &php_curl_gnutls_tsl);
1252#endif
1253
1254    if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) {
1255        return FAILURE;
1256    }
1257
1258    curlfile_register_class(TSRMLS_C);
1259
1260    return SUCCESS;
1261}
1262/* }}} */
1263
1264/* {{{ PHP_MSHUTDOWN_FUNCTION
1265 */
1266PHP_MSHUTDOWN_FUNCTION(curl)
1267{
1268    curl_global_cleanup();
1269#ifdef PHP_CURL_NEED_OPENSSL_TSL
1270    if (php_curl_openssl_tsl) {
1271        int i, c = CRYPTO_num_locks();
1272
1273        CRYPTO_set_id_callback(NULL);
1274        CRYPTO_set_locking_callback(NULL);
1275
1276        for (i = 0; i < c; ++i) {
1277            tsrm_mutex_free(php_curl_openssl_tsl[i]);
1278        }
1279
1280        free(php_curl_openssl_tsl);
1281        php_curl_openssl_tsl = NULL;
1282    }
1283#endif
1284    UNREGISTER_INI_ENTRIES();
1285    return SUCCESS;
1286}
1287/* }}} */
1288
1289/* {{{ curl_write_nothing
1290 * Used as a work around. See _php_curl_close_ex
1291 */
1292static size_t curl_write_nothing(char *data, size_t size, size_t nmemb, void *ctx)
1293{
1294    return size * nmemb;
1295}
1296/* }}} */
1297
1298/* {{{ curl_write
1299 */
1300static size_t curl_write(char *data, size_t size, size_t nmemb, void *ctx)
1301{
1302    php_curl       *ch     = (php_curl *) ctx;
1303    php_curl_write *t      = ch->handlers->write;
1304    size_t          length = size * nmemb;
1305    TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
1306
1307#if PHP_CURL_DEBUG
1308    fprintf(stderr, "curl_write() called\n");
1309    fprintf(stderr, "data = %s, size = %d, nmemb = %d, ctx = %x\n", data, size, nmemb, ctx);
1310#endif
1311
1312    switch (t->method) {
1313        case PHP_CURL_STDOUT:
1314            PHPWRITE(data, length);
1315            break;
1316        case PHP_CURL_FILE:
1317            return fwrite(data, size, nmemb, t->fp);
1318        case PHP_CURL_RETURN:
1319            if (length > 0) {
1320                smart_str_appendl(&t->buf, data, (int) length);
1321            }
1322            break;
1323        case PHP_CURL_USER: {
1324            zval **argv[2];
1325            zval *retval_ptr = NULL;
1326            zval *handle = NULL;
1327            zval *zdata = NULL;
1328            int   error;
1329            zend_fcall_info fci;
1330
1331            MAKE_STD_ZVAL(handle);
1332            ZVAL_RESOURCE(handle, ch->id);
1333            zend_list_addref(ch->id);
1334            argv[0] = &handle;
1335
1336            MAKE_STD_ZVAL(zdata);
1337            ZVAL_STRINGL(zdata, data, length, 1);
1338            argv[1] = &zdata;
1339
1340            fci.size = sizeof(fci);
1341            fci.function_table = EG(function_table);
1342            fci.object_ptr = NULL;
1343            fci.function_name = t->func_name;
1344            fci.retval_ptr_ptr = &retval_ptr;
1345            fci.param_count = 2;
1346            fci.params = argv;
1347            fci.no_separation = 0;
1348            fci.symbol_table = NULL;
1349
1350            ch->in_callback = 1;
1351            error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
1352            ch->in_callback = 0;
1353            if (error == FAILURE) {
1354                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not call the CURLOPT_WRITEFUNCTION");
1355                length = -1;
1356            } else if (retval_ptr) {
1357                if (Z_TYPE_P(retval_ptr) != IS_LONG) {
1358                    convert_to_long_ex(&retval_ptr);
1359                }
1360                length = Z_LVAL_P(retval_ptr);
1361                zval_ptr_dtor(&retval_ptr);
1362            }
1363
1364            zval_ptr_dtor(argv[0]);
1365            zval_ptr_dtor(argv[1]);
1366            break;
1367        }
1368    }
1369
1370    return length;
1371}
1372/* }}} */
1373
1374#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
1375/* {{{ curl_fnmatch
1376 */
1377static int curl_fnmatch(void *ctx, const char *pattern, const char *string)
1378{
1379    php_curl       *ch = (php_curl *) ctx;
1380    php_curl_fnmatch *t = ch->handlers->fnmatch;
1381    int rval = CURL_FNMATCHFUNC_FAIL;
1382    switch (t->method) {
1383        case PHP_CURL_USER: {
1384            zval **argv[3];
1385            zval  *zhandle = NULL;
1386            zval  *zpattern = NULL;
1387            zval  *zstring = NULL;
1388            zval  *retval_ptr;
1389            int   error;
1390            zend_fcall_info fci;
1391            TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
1392
1393            MAKE_STD_ZVAL(zhandle);
1394            MAKE_STD_ZVAL(zpattern);
1395            MAKE_STD_ZVAL(zstring);
1396
1397            ZVAL_RESOURCE(zhandle, ch->id);
1398            zend_list_addref(ch->id);
1399            ZVAL_STRING(zpattern, pattern, 1);
1400            ZVAL_STRING(zstring, string, 1);
1401
1402            argv[0] = &zhandle;
1403            argv[1] = &zpattern;
1404            argv[2] = &zstring;
1405
1406            fci.size = sizeof(fci);
1407            fci.function_table = EG(function_table);
1408            fci.function_name = t->func_name;
1409            fci.object_ptr = NULL;
1410            fci.retval_ptr_ptr = &retval_ptr;
1411            fci.param_count = 3;
1412            fci.params = argv;
1413            fci.no_separation = 0;
1414            fci.symbol_table = NULL;
1415
1416            ch->in_callback = 1;
1417            error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
1418            ch->in_callback = 0;
1419            if (error == FAILURE) {
1420                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot call the CURLOPT_FNMATCH_FUNCTION");
1421            } else if (retval_ptr) {
1422                if (Z_TYPE_P(retval_ptr) != IS_LONG) {
1423                    convert_to_long_ex(&retval_ptr);
1424                }
1425                rval = Z_LVAL_P(retval_ptr);
1426                zval_ptr_dtor(&retval_ptr);
1427            }
1428            zval_ptr_dtor(argv[0]);
1429            zval_ptr_dtor(argv[1]);
1430            zval_ptr_dtor(argv[2]);
1431            break;
1432        }
1433    }
1434    return rval;
1435}
1436/* }}} */
1437#endif
1438
1439/* {{{ curl_progress
1440 */
1441static size_t curl_progress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
1442{
1443    php_curl       *ch = (php_curl *) clientp;
1444    php_curl_progress  *t  = ch->handlers->progress;
1445    size_t  rval = 0;
1446
1447#if PHP_CURL_DEBUG
1448    fprintf(stderr, "curl_progress() called\n");
1449    fprintf(stderr, "clientp = %x, dltotal = %f, dlnow = %f, ultotal = %f, ulnow = %f\n", clientp, dltotal, dlnow, ultotal, ulnow);
1450#endif
1451
1452    switch (t->method) {
1453        case PHP_CURL_USER: {
1454            zval **argv[5];
1455            zval  *handle = NULL;
1456            zval  *zdltotal = NULL;
1457            zval  *zdlnow = NULL;
1458            zval  *zultotal = NULL;
1459            zval  *zulnow = NULL;
1460            zval  *retval_ptr;
1461            int   error;
1462            zend_fcall_info fci;
1463            TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
1464
1465            MAKE_STD_ZVAL(handle);
1466            MAKE_STD_ZVAL(zdltotal);
1467            MAKE_STD_ZVAL(zdlnow);
1468            MAKE_STD_ZVAL(zultotal);
1469            MAKE_STD_ZVAL(zulnow);
1470
1471            ZVAL_RESOURCE(handle, ch->id);
1472            zend_list_addref(ch->id);
1473            ZVAL_LONG(zdltotal, (long) dltotal);
1474            ZVAL_LONG(zdlnow, (long) dlnow);
1475            ZVAL_LONG(zultotal, (long) ultotal);
1476            ZVAL_LONG(zulnow, (long) ulnow);
1477
1478            argv[0] = &handle;
1479            argv[1] = &zdltotal;
1480            argv[2] = &zdlnow;
1481            argv[3] = &zultotal;
1482            argv[4] = &zulnow;
1483
1484            fci.size = sizeof(fci);
1485            fci.function_table = EG(function_table);
1486            fci.function_name = t->func_name;
1487            fci.object_ptr = NULL;
1488            fci.retval_ptr_ptr = &retval_ptr;
1489            fci.param_count = 5;
1490            fci.params = argv;
1491            fci.no_separation = 0;
1492            fci.symbol_table = NULL;
1493
1494            ch->in_callback = 1;
1495            error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
1496            ch->in_callback = 0;
1497            if (error == FAILURE) {
1498                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot call the CURLOPT_PROGRESSFUNCTION");
1499            } else if (retval_ptr) {
1500                if (Z_TYPE_P(retval_ptr) != IS_LONG) {
1501                    convert_to_long_ex(&retval_ptr);
1502                }
1503                if (0 != Z_LVAL_P(retval_ptr)) {
1504                    rval = 1;
1505                }
1506                zval_ptr_dtor(&retval_ptr);
1507            }
1508            zval_ptr_dtor(argv[0]);
1509            zval_ptr_dtor(argv[1]);
1510            zval_ptr_dtor(argv[2]);
1511            zval_ptr_dtor(argv[3]);
1512            zval_ptr_dtor(argv[4]);
1513            break;
1514        }
1515    }
1516    return rval;
1517}
1518/* }}} */
1519
1520/* {{{ curl_read
1521 */
1522static size_t curl_read(char *data, size_t size, size_t nmemb, void *ctx)
1523{
1524    php_curl       *ch = (php_curl *) ctx;
1525    php_curl_read  *t  = ch->handlers->read;
1526    int             length = 0;
1527
1528    switch (t->method) {
1529        case PHP_CURL_DIRECT:
1530            if (t->fp) {
1531                length = fread(data, size, nmemb, t->fp);
1532            }
1533            break;
1534        case PHP_CURL_USER: {
1535            zval **argv[3];
1536            zval  *handle = NULL;
1537            zval  *zfd = NULL;
1538            zval  *zlength = NULL;
1539            zval  *retval_ptr;
1540            int   error;
1541            zend_fcall_info fci;
1542            TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
1543
1544            MAKE_STD_ZVAL(handle);
1545            MAKE_STD_ZVAL(zfd);
1546            MAKE_STD_ZVAL(zlength);
1547
1548            ZVAL_RESOURCE(handle, ch->id);
1549            zend_list_addref(ch->id);
1550            ZVAL_RESOURCE(zfd, t->fd);
1551            zend_list_addref(t->fd);
1552            ZVAL_LONG(zlength, (int) size * nmemb);
1553
1554            argv[0] = &handle;
1555            argv[1] = &zfd;
1556            argv[2] = &zlength;
1557
1558            fci.size = sizeof(fci);
1559            fci.function_table = EG(function_table);
1560            fci.function_name = t->func_name;
1561            fci.object_ptr = NULL;
1562            fci.retval_ptr_ptr = &retval_ptr;
1563            fci.param_count = 3;
1564            fci.params = argv;
1565            fci.no_separation = 0;
1566            fci.symbol_table = NULL;
1567
1568            ch->in_callback = 1;
1569            error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
1570            ch->in_callback = 0;
1571            if (error == FAILURE) {
1572                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot call the CURLOPT_READFUNCTION");
1573#if LIBCURL_VERSION_NUM >= 0x070c01 /* 7.12.1 */
1574                length = CURL_READFUNC_ABORT;
1575#endif
1576            } else if (retval_ptr) {
1577                if (Z_TYPE_P(retval_ptr) == IS_STRING) {
1578                    length = MIN((int) (size * nmemb), Z_STRLEN_P(retval_ptr));
1579                    memcpy(data, Z_STRVAL_P(retval_ptr), length);
1580                }
1581                zval_ptr_dtor(&retval_ptr);
1582            }
1583
1584            zval_ptr_dtor(argv[0]);
1585            zval_ptr_dtor(argv[1]);
1586            zval_ptr_dtor(argv[2]);
1587            break;
1588        }
1589    }
1590
1591    return length;
1592}
1593/* }}} */
1594
1595/* {{{ curl_write_header
1596 */
1597static size_t curl_write_header(char *data, size_t size, size_t nmemb, void *ctx)
1598{
1599    php_curl       *ch  = (php_curl *) ctx;
1600    php_curl_write *t   = ch->handlers->write_header;
1601    size_t          length = size * nmemb;
1602    TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
1603
1604    switch (t->method) {
1605        case PHP_CURL_STDOUT:
1606            /* Handle special case write when we're returning the entire transfer
1607             */
1608            if (ch->handlers->write->method == PHP_CURL_RETURN && length > 0) {
1609                smart_str_appendl(&ch->handlers->write->buf, data, (int) length);
1610            } else {
1611                PHPWRITE(data, length);
1612            }
1613            break;
1614        case PHP_CURL_FILE:
1615            return fwrite(data, size, nmemb, t->fp);
1616        case PHP_CURL_USER: {
1617            zval **argv[2];
1618            zval  *handle = NULL;
1619            zval  *zdata = NULL;
1620            zval  *retval_ptr;
1621            int   error;
1622            zend_fcall_info fci;
1623
1624            MAKE_STD_ZVAL(handle);
1625            MAKE_STD_ZVAL(zdata);
1626
1627            ZVAL_RESOURCE(handle, ch->id);
1628            zend_list_addref(ch->id);
1629            ZVAL_STRINGL(zdata, data, length, 1);
1630
1631            argv[0] = &handle;
1632            argv[1] = &zdata;
1633
1634            fci.size = sizeof(fci);
1635            fci.function_table = EG(function_table);
1636            fci.function_name = t->func_name;
1637            fci.symbol_table = NULL;
1638            fci.object_ptr = NULL;
1639            fci.retval_ptr_ptr = &retval_ptr;
1640            fci.param_count = 2;
1641            fci.params = argv;
1642            fci.no_separation = 0;
1643
1644            ch->in_callback = 1;
1645            error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
1646            ch->in_callback = 0;
1647            if (error == FAILURE) {
1648                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not call the CURLOPT_HEADERFUNCTION");
1649                length = -1;
1650            } else if (retval_ptr) {
1651                if (Z_TYPE_P(retval_ptr) != IS_LONG) {
1652                    convert_to_long_ex(&retval_ptr);
1653                }
1654                length = Z_LVAL_P(retval_ptr);
1655                zval_ptr_dtor(&retval_ptr);
1656            }
1657            zval_ptr_dtor(argv[0]);
1658            zval_ptr_dtor(argv[1]);
1659            break;
1660        }
1661
1662        case PHP_CURL_IGNORE:
1663            return length;
1664
1665        default:
1666            return -1;
1667    }
1668
1669    return length;
1670}
1671/* }}} */
1672
1673static int curl_debug(CURL *cp, curl_infotype type, char *buf, size_t buf_len, void *ctx) /* {{{ */
1674{
1675    php_curl    *ch   = (php_curl *) ctx;
1676
1677    if (type == CURLINFO_HEADER_OUT) {
1678        if (ch->header.str_len) {
1679            efree(ch->header.str);
1680        }
1681        if (buf_len > 0) {
1682            ch->header.str = estrndup(buf, buf_len);
1683            ch->header.str_len = buf_len;
1684        }
1685    }
1686
1687    return 0;
1688}
1689/* }}} */
1690
1691#if CURLOPT_PASSWDFUNCTION != 0
1692/* {{{ curl_passwd
1693 */
1694static size_t curl_passwd(void *ctx, char *prompt, char *buf, int buflen)
1695{
1696    php_curl    *ch   = (php_curl *) ctx;
1697    zval        *func = ch->handlers->passwd;
1698    zval        *argv[3];
1699    zval        *retval = NULL;
1700    int          error;
1701    int          ret = -1;
1702    TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
1703
1704    MAKE_STD_ZVAL(argv[0]);
1705    MAKE_STD_ZVAL(argv[1]);
1706    MAKE_STD_ZVAL(argv[2]);
1707
1708    ZVAL_RESOURCE(argv[0], ch->id);
1709    zend_list_addref(ch->id);
1710    ZVAL_STRING(argv[1], prompt, 1);
1711    ZVAL_LONG(argv[2], buflen);
1712
1713    error = call_user_function(EG(function_table), NULL, func, retval, 2, argv TSRMLS_CC);
1714    if (error == FAILURE) {
1715        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not call the CURLOPT_PASSWDFUNCTION");
1716    } else if (Z_TYPE_P(retval) == IS_STRING) {
1717        if (Z_STRLEN_P(retval) > buflen) {
1718            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Returned password is too long for libcurl to handle");
1719        } else {
1720            strlcpy(buf, Z_STRVAL_P(retval), Z_STRLEN_P(retval));
1721        }
1722    } else {
1723        php_error_docref(NULL TSRMLS_CC, E_WARNING, "User handler '%s' did not return a string", Z_STRVAL_P(func));
1724    }
1725
1726    zval_ptr_dtor(&argv[0]);
1727    zval_ptr_dtor(&argv[1]);
1728    zval_ptr_dtor(&argv[2]);
1729    zval_ptr_dtor(&retval);
1730
1731    return ret;
1732}
1733/* }}} */
1734#endif
1735
1736/* {{{ curl_free_string
1737 */
1738static void curl_free_string(void **string)
1739{
1740    efree(*string);
1741}
1742/* }}} */
1743
1744/* {{{ curl_free_post
1745 */
1746static void curl_free_post(void **post)
1747{
1748    curl_formfree((struct HttpPost *) *post);
1749}
1750/* }}} */
1751
1752/* {{{ curl_free_slist
1753 */
1754static void curl_free_slist(void *slist)
1755{
1756    curl_slist_free_all(*((struct curl_slist **) slist));
1757}
1758/* }}} */
1759
1760/* {{{ proto array curl_version([int version])
1761   Return cURL version information. */
1762PHP_FUNCTION(curl_version)
1763{
1764    curl_version_info_data *d;
1765    long uversion = CURLVERSION_NOW;
1766
1767    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &uversion) == FAILURE) {
1768        return;
1769    }
1770
1771    d = curl_version_info(uversion);
1772    if (d == NULL) {
1773        RETURN_FALSE;
1774    }
1775
1776    array_init(return_value);
1777
1778    CAAL("version_number", d->version_num);
1779    CAAL("age", d->age);
1780    CAAL("features", d->features);
1781    CAAL("ssl_version_number", d->ssl_version_num);
1782    CAAS("version", d->version);
1783    CAAS("host", d->host);
1784    CAAS("ssl_version", d->ssl_version);
1785    CAAS("libz_version", d->libz_version);
1786    /* Add an array of protocols */
1787    {
1788        char **p = (char **) d->protocols;
1789        zval  *protocol_list = NULL;
1790
1791        MAKE_STD_ZVAL(protocol_list);
1792        array_init(protocol_list);
1793
1794        while (*p != NULL) {
1795            add_next_index_string(protocol_list, *p, 1);
1796            p++;
1797        }
1798        CAAZ("protocols", protocol_list);
1799    }
1800}
1801/* }}} */
1802
1803/* {{{ alloc_curl_handle
1804 */
1805static void alloc_curl_handle(php_curl **ch)
1806{
1807    *ch                           = emalloc(sizeof(php_curl));
1808    (*ch)->to_free                = ecalloc(1, sizeof(struct _php_curl_free));
1809    (*ch)->handlers               = ecalloc(1, sizeof(php_curl_handlers));
1810    (*ch)->handlers->write        = ecalloc(1, sizeof(php_curl_write));
1811    (*ch)->handlers->write_header = ecalloc(1, sizeof(php_curl_write));
1812    (*ch)->handlers->read         = ecalloc(1, sizeof(php_curl_read));
1813    (*ch)->handlers->progress     = NULL;
1814#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
1815    (*ch)->handlers->fnmatch      = NULL;
1816#endif
1817
1818    (*ch)->in_callback = 0;
1819    (*ch)->header.str_len = 0;
1820
1821    memset(&(*ch)->err, 0, sizeof((*ch)->err));
1822    (*ch)->handlers->write->stream = NULL;
1823    (*ch)->handlers->write_header->stream = NULL;
1824    (*ch)->handlers->read->stream = NULL;
1825
1826    zend_llist_init(&(*ch)->to_free->str,   sizeof(char *),            (llist_dtor_func_t) curl_free_string, 0);
1827    zend_llist_init(&(*ch)->to_free->post,  sizeof(struct HttpPost),   (llist_dtor_func_t) curl_free_post,   0);
1828    (*ch)->safe_upload = 0; /* for now, for BC reason we allow unsafe API */
1829
1830    (*ch)->to_free->slist = emalloc(sizeof(HashTable));
1831    zend_hash_init((*ch)->to_free->slist, 4, NULL, curl_free_slist, 0);
1832}
1833/* }}} */
1834
1835#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
1836/* {{{ split_certinfo
1837 */
1838static void split_certinfo(char *string, zval *hash)
1839{
1840    char *org = estrdup(string);
1841    char *s = org;
1842    char *split;
1843
1844    if(org) {
1845        do {
1846            char *key;
1847            char *val;
1848            char *tmp;
1849
1850            split = strstr(s, "; ");
1851            if(split)
1852                *split = '\0';
1853
1854            key = s;
1855            tmp = memchr(key, '=', 64);
1856            if(tmp) {
1857                *tmp = '\0';
1858                val = tmp+1;
1859                add_assoc_string(hash, key, val, 1);
1860            }
1861            s = split+2;
1862        } while(split);
1863        efree(org);
1864    }
1865}
1866/* }}} */
1867
1868/* {{{ create_certinfo
1869 */
1870static void create_certinfo(struct curl_certinfo *ci, zval *listcode TSRMLS_DC)
1871{
1872    int i;
1873
1874    if(ci) {
1875        zval *certhash = NULL;
1876
1877        for(i=0; i<ci->num_of_certs; i++) {
1878            struct curl_slist *slist;
1879
1880            MAKE_STD_ZVAL(certhash);
1881            array_init(certhash);
1882            for(slist = ci->certinfo[i]; slist; slist = slist->next) {
1883                int len;
1884                char s[64];
1885                char *tmp;
1886                strncpy(s, slist->data, 64);
1887                tmp = memchr(s, ':', 64);
1888                if(tmp) {
1889                    *tmp = '\0';
1890                    len = strlen(s);
1891                    if(!strcmp(s, "Subject") || !strcmp(s, "Issuer")) {
1892                        zval *hash;
1893
1894                        MAKE_STD_ZVAL(hash);
1895                        array_init(hash);
1896
1897                        split_certinfo(&slist->data[len+1], hash);
1898                        add_assoc_zval(certhash, s, hash);
1899                    } else {
1900                        add_assoc_string(certhash, s, &slist->data[len+1], 1);
1901                    }
1902                } else {
1903                    php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not extract hash key from certificate info");
1904                }
1905            }
1906            add_next_index_zval(listcode, certhash);
1907        }
1908    }
1909}
1910/* }}} */
1911#endif
1912
1913/* {{{ _php_curl_set_default_options()
1914   Set default options for a handle */
1915static void _php_curl_set_default_options(php_curl *ch)
1916{
1917    char *cainfo;
1918
1919    curl_easy_setopt(ch->cp, CURLOPT_NOPROGRESS,        1);
1920    curl_easy_setopt(ch->cp, CURLOPT_VERBOSE,           0);
1921    curl_easy_setopt(ch->cp, CURLOPT_ERRORBUFFER,       ch->err.str);
1922    curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION,     curl_write);
1923    curl_easy_setopt(ch->cp, CURLOPT_FILE,              (void *) ch);
1924    curl_easy_setopt(ch->cp, CURLOPT_READFUNCTION,      curl_read);
1925    curl_easy_setopt(ch->cp, CURLOPT_INFILE,            (void *) ch);
1926    curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION,    curl_write_header);
1927    curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER,       (void *) ch);
1928    curl_easy_setopt(ch->cp, CURLOPT_DNS_USE_GLOBAL_CACHE, 1);
1929    curl_easy_setopt(ch->cp, CURLOPT_DNS_CACHE_TIMEOUT, 120);
1930    curl_easy_setopt(ch->cp, CURLOPT_MAXREDIRS, 20); /* prevent infinite redirects */
1931
1932    cainfo = INI_STR("curl.cainfo");
1933    if (cainfo && strlen(cainfo) > 0) {
1934        curl_easy_setopt(ch->cp, CURLOPT_CAINFO, cainfo);
1935    }
1936
1937#if defined(ZTS)
1938    curl_easy_setopt(ch->cp, CURLOPT_NOSIGNAL, 1);
1939#endif
1940}
1941/* }}} */
1942
1943/* {{{ proto resource curl_init([string url])
1944   Initialize a cURL session */
1945PHP_FUNCTION(curl_init)
1946{
1947    php_curl    *ch;
1948    CURL        *cp;
1949    zval        *clone;
1950    char        *url = NULL;
1951    int     url_len = 0;
1952
1953    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &url, &url_len) == FAILURE) {
1954        return;
1955    }
1956
1957    cp = curl_easy_init();
1958    if (!cp) {
1959        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize a new cURL handle");
1960        RETURN_FALSE;
1961    }
1962
1963    alloc_curl_handle(&ch);
1964    TSRMLS_SET_CTX(ch->thread_ctx);
1965
1966    ch->cp = cp;
1967
1968    ch->handlers->write->method = PHP_CURL_STDOUT;
1969    ch->handlers->read->method  = PHP_CURL_DIRECT;
1970    ch->handlers->write_header->method = PHP_CURL_IGNORE;
1971
1972    MAKE_STD_ZVAL(clone);
1973    ch->clone = clone;
1974
1975    _php_curl_set_default_options(ch);
1976
1977    if (url) {
1978        if (php_curl_option_url(ch, url, url_len TSRMLS_CC) == FAILURE) {
1979            _php_curl_close_ex(ch TSRMLS_CC);
1980            RETURN_FALSE;
1981        }
1982    }
1983
1984    ZEND_REGISTER_RESOURCE(return_value, ch, le_curl);
1985    ch->id = Z_LVAL_P(return_value);
1986}
1987/* }}} */
1988
1989/* {{{ proto resource curl_copy_handle(resource ch)
1990   Copy a cURL handle along with all of it's preferences */
1991PHP_FUNCTION(curl_copy_handle)
1992{
1993    CURL        *cp;
1994    zval        *zid;
1995    php_curl    *ch, *dupch;
1996
1997    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
1998        return;
1999    }
2000
2001    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
2002
2003    cp = curl_easy_duphandle(ch->cp);
2004    if (!cp) {
2005        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot duplicate cURL handle");
2006        RETURN_FALSE;
2007    }
2008
2009    alloc_curl_handle(&dupch);
2010    TSRMLS_SET_CTX(dupch->thread_ctx);
2011
2012    dupch->cp = cp;
2013    zend_list_addref(Z_LVAL_P(zid));
2014    if (ch->handlers->write->stream) {
2015        Z_ADDREF_P(ch->handlers->write->stream);
2016    }
2017    dupch->handlers->write->stream = ch->handlers->write->stream;
2018    dupch->handlers->write->method = ch->handlers->write->method;
2019    if (ch->handlers->read->stream) {
2020        Z_ADDREF_P(ch->handlers->read->stream);
2021    }
2022    dupch->handlers->read->stream  = ch->handlers->read->stream;
2023    dupch->handlers->read->method  = ch->handlers->read->method;
2024    dupch->handlers->write_header->method = ch->handlers->write_header->method;
2025    if (ch->handlers->write_header->stream) {
2026        Z_ADDREF_P(ch->handlers->write_header->stream);
2027    }
2028    dupch->handlers->write_header->stream = ch->handlers->write_header->stream;
2029
2030    dupch->handlers->write->fp = ch->handlers->write->fp;
2031    dupch->handlers->write_header->fp = ch->handlers->write_header->fp;
2032    dupch->handlers->read->fp = ch->handlers->read->fp;
2033    dupch->handlers->read->fd = ch->handlers->read->fd;
2034#if CURLOPT_PASSWDDATA != 0
2035    if (ch->handlers->passwd) {
2036        zval_add_ref(&ch->handlers->passwd);
2037        dupch->handlers->passwd = ch->handlers->passwd;
2038        curl_easy_setopt(ch->cp, CURLOPT_PASSWDDATA, (void *) dupch);
2039    }
2040#endif
2041    if (ch->handlers->write->func_name) {
2042        zval_add_ref(&ch->handlers->write->func_name);
2043        dupch->handlers->write->func_name = ch->handlers->write->func_name;
2044    }
2045    if (ch->handlers->read->func_name) {
2046        zval_add_ref(&ch->handlers->read->func_name);
2047        dupch->handlers->read->func_name = ch->handlers->read->func_name;
2048    }
2049    if (ch->handlers->write_header->func_name) {
2050        zval_add_ref(&ch->handlers->write_header->func_name);
2051        dupch->handlers->write_header->func_name = ch->handlers->write_header->func_name;
2052    }
2053
2054    curl_easy_setopt(dupch->cp, CURLOPT_ERRORBUFFER,       dupch->err.str);
2055    curl_easy_setopt(dupch->cp, CURLOPT_FILE,              (void *) dupch);
2056    curl_easy_setopt(dupch->cp, CURLOPT_INFILE,            (void *) dupch);
2057    curl_easy_setopt(dupch->cp, CURLOPT_WRITEHEADER,       (void *) dupch);
2058
2059    if (ch->handlers->progress) {
2060        dupch->handlers->progress = ecalloc(1, sizeof(php_curl_progress));
2061        if (ch->handlers->progress->func_name) {
2062            zval_add_ref(&ch->handlers->progress->func_name);
2063            dupch->handlers->progress->func_name = ch->handlers->progress->func_name;
2064        }
2065        dupch->handlers->progress->method = ch->handlers->progress->method;
2066        curl_easy_setopt(dupch->cp, CURLOPT_PROGRESSDATA, (void *) dupch);
2067    }
2068
2069/* Available since 7.21.0 */
2070#if LIBCURL_VERSION_NUM >= 0x071500
2071    if (ch->handlers->fnmatch) {
2072        dupch->handlers->fnmatch = ecalloc(1, sizeof(php_curl_fnmatch));
2073        if (ch->handlers->fnmatch->func_name) {
2074            zval_add_ref(&ch->handlers->fnmatch->func_name);
2075            dupch->handlers->fnmatch->func_name = ch->handlers->fnmatch->func_name;
2076        }
2077        dupch->handlers->fnmatch->method = ch->handlers->fnmatch->method;
2078        curl_easy_setopt(dupch->cp, CURLOPT_FNMATCH_DATA, (void *) dupch);
2079    }
2080#endif
2081
2082    efree(dupch->to_free->slist);
2083    efree(dupch->to_free);
2084    dupch->to_free = ch->to_free;
2085
2086    /* Keep track of cloned copies to avoid invoking curl destructors for every clone */
2087    Z_ADDREF_P(ch->clone);
2088    dupch->clone = ch->clone;
2089
2090    ZEND_REGISTER_RESOURCE(return_value, dupch, le_curl);
2091    dupch->id = Z_LVAL_P(return_value);
2092}
2093/* }}} */
2094
2095static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) /* {{{ */
2096{
2097    CURLcode     error=CURLE_OK;
2098
2099    switch (option) {
2100        /* Long options */
2101        case CURLOPT_SSL_VERIFYHOST:
2102            if(Z_BVAL_PP(zvalue) == 1) {
2103#if LIBCURL_VERSION_NUM <= 0x071c00 /* 7.28.0 */
2104                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");
2105#else
2106                php_error_docref(NULL TSRMLS_CC, E_NOTICE, "CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead");
2107                error = curl_easy_setopt(ch->cp, option, 2);
2108                break;
2109#endif
2110            }
2111        case CURLOPT_AUTOREFERER:
2112        case CURLOPT_BUFFERSIZE:
2113        case CURLOPT_CLOSEPOLICY:
2114        case CURLOPT_CONNECTTIMEOUT:
2115        case CURLOPT_COOKIESESSION:
2116        case CURLOPT_CRLF:
2117        case CURLOPT_DNS_CACHE_TIMEOUT:
2118        case CURLOPT_DNS_USE_GLOBAL_CACHE:
2119        case CURLOPT_FAILONERROR:
2120        case CURLOPT_FILETIME:
2121        case CURLOPT_FORBID_REUSE:
2122        case CURLOPT_FRESH_CONNECT:
2123        case CURLOPT_FTP_USE_EPRT:
2124        case CURLOPT_FTP_USE_EPSV:
2125        case CURLOPT_HEADER:
2126        case CURLOPT_HTTPGET:
2127        case CURLOPT_HTTPPROXYTUNNEL:
2128        case CURLOPT_HTTP_VERSION:
2129        case CURLOPT_INFILESIZE:
2130        case CURLOPT_LOW_SPEED_LIMIT:
2131        case CURLOPT_LOW_SPEED_TIME:
2132        case CURLOPT_MAXCONNECTS:
2133        case CURLOPT_MAXREDIRS:
2134        case CURLOPT_NETRC:
2135        case CURLOPT_NOBODY:
2136        case CURLOPT_NOPROGRESS:
2137        case CURLOPT_NOSIGNAL:
2138        case CURLOPT_PORT:
2139        case CURLOPT_POST:
2140        case CURLOPT_PROXYPORT:
2141        case CURLOPT_PROXYTYPE:
2142        case CURLOPT_PUT:
2143        case CURLOPT_RESUME_FROM:
2144        case CURLOPT_SSLVERSION:
2145        case CURLOPT_SSL_VERIFYPEER:
2146        case CURLOPT_TIMECONDITION:
2147        case CURLOPT_TIMEOUT:
2148        case CURLOPT_TIMEVALUE:
2149        case CURLOPT_TRANSFERTEXT:
2150        case CURLOPT_UNRESTRICTED_AUTH:
2151        case CURLOPT_UPLOAD:
2152        case CURLOPT_VERBOSE:
2153#if LIBCURL_VERSION_NUM >= 0x070a06 /* Available since 7.10.6 */
2154        case CURLOPT_HTTPAUTH:
2155#endif
2156#if LIBCURL_VERSION_NUM >= 0x070a07 /* Available since 7.10.7 */
2157        case CURLOPT_FTP_CREATE_MISSING_DIRS:
2158        case CURLOPT_PROXYAUTH:
2159#endif
2160#if LIBCURL_VERSION_NUM >= 0x070a08 /* Available since 7.10.8 */
2161        case CURLOPT_FTP_RESPONSE_TIMEOUT:
2162        case CURLOPT_IPRESOLVE:
2163        case CURLOPT_MAXFILESIZE:
2164#endif
2165#if LIBCURL_VERSION_NUM >= 0x070b02 /* Available since 7.11.2 */
2166        case CURLOPT_TCP_NODELAY:
2167#endif
2168#if LIBCURL_VERSION_NUM >= 0x070c02 /* Available since 7.12.2 */
2169        case CURLOPT_FTPSSLAUTH:
2170#endif
2171#if LIBCURL_VERSION_NUM >= 0x070e01 /* Available since 7.14.1 */
2172        case CURLOPT_IGNORE_CONTENT_LENGTH:
2173#endif
2174#if LIBCURL_VERSION_NUM >= 0x070f00 /* Available since 7.15.0 */
2175        case CURLOPT_FTP_SKIP_PASV_IP:
2176#endif
2177#if LIBCURL_VERSION_NUM >= 0x070f01 /* Available since 7.15.1 */
2178        case CURLOPT_FTP_FILEMETHOD:
2179#endif
2180#if LIBCURL_VERSION_NUM >= 0x070f02 /* Available since 7.15.2 */
2181        case CURLOPT_CONNECT_ONLY:
2182        case CURLOPT_LOCALPORT:
2183        case CURLOPT_LOCALPORTRANGE:
2184#endif
2185#if LIBCURL_VERSION_NUM >= 0x071000 /* Available since 7.16.0 */
2186        case CURLOPT_SSL_SESSIONID_CACHE:
2187#endif
2188#if LIBCURL_VERSION_NUM >= 0x071001 /* Available since 7.16.1 */
2189        case CURLOPT_FTP_SSL_CCC:
2190        case CURLOPT_SSH_AUTH_TYPES:
2191#endif
2192#if LIBCURL_VERSION_NUM >= 0x071002 /* Available since 7.16.2 */
2193        case CURLOPT_CONNECTTIMEOUT_MS:
2194        case CURLOPT_HTTP_CONTENT_DECODING:
2195        case CURLOPT_HTTP_TRANSFER_DECODING:
2196        case CURLOPT_TIMEOUT_MS:
2197#endif
2198#if LIBCURL_VERSION_NUM >= 0x071004 /* Available since 7.16.4 */
2199        case CURLOPT_NEW_DIRECTORY_PERMS:
2200        case CURLOPT_NEW_FILE_PERMS:
2201#endif
2202#if LIBCURL_VERSION_NUM >= 0x071100 /* Available since 7.17.0 */
2203        case CURLOPT_USE_SSL:
2204#elif LIBCURL_VERSION_NUM >= 0x070b00 /* Available since 7.11.0 */
2205        case CURLOPT_FTP_SSL:
2206#endif
2207#if LIBCURL_VERSION_NUM >= 0x071100 /* Available since 7.17.0 */
2208        case CURLOPT_APPEND:
2209        case CURLOPT_DIRLISTONLY:
2210#else
2211        case CURLOPT_FTPAPPEND:
2212        case CURLOPT_FTPLISTONLY:
2213#endif
2214#if LIBCURL_VERSION_NUM >= 0x071200 /* Available since 7.18.0 */
2215        case CURLOPT_PROXY_TRANSFER_MODE:
2216#endif
2217#if LIBCURL_VERSION_NUM >= 0x071300 /* Available since 7.19.0 */
2218        case CURLOPT_ADDRESS_SCOPE:
2219#endif
2220#if LIBCURL_VERSION_NUM >  0x071301 /* Available since 7.19.1 */
2221        case CURLOPT_CERTINFO:
2222#endif
2223#if LIBCURL_VERSION_NUM >= 0x071304 /* Available since 7.19.4 */
2224        case CURLOPT_NOPROXY:
2225        case CURLOPT_PROTOCOLS:
2226        case CURLOPT_REDIR_PROTOCOLS:
2227        case CURLOPT_SOCKS5_GSSAPI_NEC:
2228        case CURLOPT_TFTP_BLKSIZE:
2229#endif
2230#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2231        case CURLOPT_FTP_USE_PRET:
2232        case CURLOPT_RTSP_CLIENT_CSEQ:
2233        case CURLOPT_RTSP_REQUEST:
2234        case CURLOPT_RTSP_SERVER_CSEQ:
2235#endif
2236#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
2237        case CURLOPT_WILDCARDMATCH:
2238#endif
2239#if LIBCURL_VERSION_NUM >= 0x071504 /* Available since 7.21.4 */
2240        case CURLOPT_TLSAUTH_TYPE:
2241#endif
2242#if LIBCURL_VERSION_NUM >= 0x071600 /* Available since 7.22.0 */
2243        case CURLOPT_GSSAPI_DELEGATION:
2244#endif
2245#if LIBCURL_VERSION_NUM >= 0x071800 /* Available since 7.24.0 */
2246        case CURLOPT_ACCEPTTIMEOUT_MS:
2247#endif
2248#if LIBCURL_VERSION_NUM >= 0x071900 /* Available since 7.25.0 */
2249        case CURLOPT_SSL_OPTIONS:
2250        case CURLOPT_TCP_KEEPALIVE:
2251        case CURLOPT_TCP_KEEPIDLE:
2252        case CURLOPT_TCP_KEEPINTVL:
2253#endif
2254#if CURLOPT_MUTE != 0
2255        case CURLOPT_MUTE:
2256#endif
2257            convert_to_long_ex(zvalue);
2258#if LIBCURL_VERSION_NUM >= 0x71304
2259            if ((option == CURLOPT_PROTOCOLS || option == CURLOPT_REDIR_PROTOCOLS) &&
2260                (PG(open_basedir) && *PG(open_basedir)) && (Z_LVAL_PP(zvalue) & CURLPROTO_FILE)) {
2261                    php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLPROTO_FILE cannot be activated when an open_basedir is set");
2262                    return 1;
2263            }
2264#endif
2265            error = curl_easy_setopt(ch->cp, option, Z_LVAL_PP(zvalue));
2266            break;
2267        case CURLOPT_SAFE_UPLOAD:
2268            convert_to_long_ex(zvalue);
2269            ch->safe_upload = (Z_LVAL_PP(zvalue) != 0);
2270            break;
2271
2272        /* String options */
2273        case CURLOPT_CAINFO:
2274        case CURLOPT_CAPATH:
2275        case CURLOPT_COOKIE:
2276        case CURLOPT_EGDSOCKET:
2277        case CURLOPT_INTERFACE:
2278        case CURLOPT_PROXY:
2279        case CURLOPT_PROXYUSERPWD:
2280        case CURLOPT_REFERER:
2281        case CURLOPT_SSLCERTTYPE:
2282        case CURLOPT_SSLENGINE:
2283        case CURLOPT_SSLENGINE_DEFAULT:
2284        case CURLOPT_SSLKEY:
2285        case CURLOPT_SSLKEYPASSWD:
2286        case CURLOPT_SSLKEYTYPE:
2287        case CURLOPT_SSL_CIPHER_LIST:
2288        case CURLOPT_USERAGENT:
2289        case CURLOPT_USERPWD:
2290#if LIBCURL_VERSION_NUM >= 0x070e01 /* Available since 7.14.1 */
2291        case CURLOPT_COOKIELIST:
2292#endif
2293#if LIBCURL_VERSION_NUM >= 0x070f05 /* Available since 7.15.5 */
2294        case CURLOPT_FTP_ALTERNATIVE_TO_USER:
2295#endif
2296#if LIBCURL_VERSION_NUM >= 0x071101 /* Available since 7.17.1 */
2297        case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
2298#endif
2299#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
2300        case CURLOPT_PASSWORD:
2301        case CURLOPT_PROXYPASSWORD:
2302        case CURLOPT_PROXYUSERNAME:
2303        case CURLOPT_USERNAME:
2304#endif
2305#if LIBCURL_VERSION_NUM >= 0x071304 /* Available since 7.19.4 */
2306        case CURLOPT_SOCKS5_GSSAPI_SERVICE:
2307#endif
2308#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2309        case CURLOPT_MAIL_FROM:
2310        case CURLOPT_RTSP_STREAM_URI:
2311        case CURLOPT_RTSP_TRANSPORT:
2312#endif
2313#if LIBCURL_VERSION_NUM >= 0x071504 /* Available since 7.21.4 */
2314        case CURLOPT_TLSAUTH_PASSWORD:
2315        case CURLOPT_TLSAUTH_USERNAME:
2316#endif
2317#if LIBCURL_VERSION_NUM >= 0x071506 /* Available since 7.21.6 */
2318        case CURLOPT_ACCEPT_ENCODING:
2319        case CURLOPT_TRANSFER_ENCODING:
2320#else
2321        case CURLOPT_ENCODING:
2322#endif
2323#if LIBCURL_VERSION_NUM >= 0x071800 /* Available since 7.24.0 */
2324        case CURLOPT_DNS_SERVERS:
2325#endif
2326#if LIBCURL_VERSION_NUM >= 0x071900 /* Available since 7.25.0 */
2327        case CURLOPT_MAIL_AUTH:
2328#endif
2329        {
2330            convert_to_string_ex(zvalue);
2331            return php_curl_option_str(ch, option, Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue), 0 TSRMLS_CC);
2332        }
2333
2334        /* Curl nullable string options */
2335        case CURLOPT_CUSTOMREQUEST:
2336        case CURLOPT_FTPPORT:
2337        case CURLOPT_RANGE:
2338#if LIBCURL_VERSION_NUM >= 0x070d00 /* Available since 7.13.0 */
2339        case CURLOPT_FTP_ACCOUNT:
2340#endif
2341#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2342        case CURLOPT_RTSP_SESSION_ID:
2343#endif
2344#if LIBCURL_VERSION_NUM >= 0x071004 /* Available since 7.16.4 */
2345        case CURLOPT_KRBLEVEL:
2346#else
2347        case CURLOPT_KRB4LEVEL:
2348#endif
2349        {
2350            if (Z_TYPE_PP(zvalue) == IS_NULL) {
2351                error = curl_easy_setopt(ch->cp, option, NULL);
2352            } else {
2353                convert_to_string_ex(zvalue);
2354                return php_curl_option_str(ch, option, Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue), 0 TSRMLS_CC);
2355            }
2356            break;
2357        }
2358
2359        /* Curl private option */
2360        case CURLOPT_PRIVATE:
2361            convert_to_string_ex(zvalue);
2362            return php_curl_option_str(ch, option, Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue), 1 TSRMLS_CC);
2363
2364        /* Curl url option */
2365        case CURLOPT_URL:
2366            convert_to_string_ex(zvalue);
2367            return php_curl_option_url(ch, Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue) TSRMLS_CC);
2368
2369        /* Curl file handle options */
2370        case CURLOPT_FILE:
2371        case CURLOPT_INFILE:
2372        case CURLOPT_STDERR:
2373        case CURLOPT_WRITEHEADER: {
2374            FILE *fp = NULL;
2375            int type;
2376            void *what = NULL;
2377
2378            if (Z_TYPE_PP(zvalue) != IS_NULL) {
2379                what = zend_fetch_resource(zvalue TSRMLS_CC, -1, "File-Handle", &type, 1, php_file_le_stream(), php_file_le_pstream());
2380                if (!what) {
2381                    return FAILURE;
2382                }
2383
2384                if (FAILURE == php_stream_cast((php_stream *) what, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) {
2385                    return FAILURE;
2386                }
2387
2388                if (!fp) {
2389                    return FAILURE;
2390                }
2391            }
2392
2393            error = CURLE_OK;
2394            switch (option) {
2395                case CURLOPT_FILE:
2396                    if (!what) {
2397                        if (ch->handlers->write->stream) {
2398                            Z_DELREF_P(ch->handlers->write->stream);
2399                            ch->handlers->write->stream = NULL;
2400                        }
2401                        ch->handlers->write->fp = NULL;
2402                        ch->handlers->write->method = PHP_CURL_STDOUT;
2403                    } else if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') {
2404                        if (ch->handlers->write->stream) {
2405                            Z_DELREF_P(ch->handlers->write->stream);
2406                        }
2407                        Z_ADDREF_PP(zvalue);
2408                        ch->handlers->write->fp = fp;
2409                        ch->handlers->write->method = PHP_CURL_FILE;
2410                        ch->handlers->write->stream = *zvalue;
2411                    } else {
2412                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable");
2413                        return FAILURE;
2414                    }
2415                    break;
2416                case CURLOPT_WRITEHEADER:
2417                    if (!what) {
2418                        if (ch->handlers->write_header->stream) {
2419                            Z_DELREF_P(ch->handlers->write_header->stream);
2420                            ch->handlers->write_header->stream = NULL;
2421                        }
2422                        ch->handlers->write_header->fp = NULL;
2423                        ch->handlers->write_header->method = PHP_CURL_IGNORE;
2424                    } else if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') {
2425                        if (ch->handlers->write_header->stream) {
2426                            Z_DELREF_P(ch->handlers->write_header->stream);
2427                        }
2428                        Z_ADDREF_PP(zvalue);
2429                        ch->handlers->write_header->fp = fp;
2430                        ch->handlers->write_header->method = PHP_CURL_FILE;
2431                        ch->handlers->write_header->stream = *zvalue;
2432                    } else {
2433                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable");
2434                        return FAILURE;
2435                    }
2436                    break;
2437                case CURLOPT_INFILE:
2438                    if (!what) {
2439                        if (ch->handlers->read->stream) {
2440                            Z_DELREF_P(ch->handlers->read->stream);
2441                            ch->handlers->read->stream = NULL;
2442                        }
2443                        ch->handlers->read->fp = NULL;
2444                        ch->handlers->read->fd = 0;
2445                    } else {
2446                        if (ch->handlers->read->stream) {
2447                            Z_DELREF_P(ch->handlers->read->stream);
2448                        }
2449                        Z_ADDREF_PP(zvalue);
2450                        ch->handlers->read->fp = fp;
2451                        ch->handlers->read->fd = Z_LVAL_PP(zvalue);
2452                        ch->handlers->read->stream = *zvalue;
2453                    }
2454                    break;
2455                case CURLOPT_STDERR:
2456                    if (!what) {
2457                        if (ch->handlers->std_err) {
2458                            zval_ptr_dtor(&ch->handlers->std_err);
2459                            ch->handlers->std_err = NULL;
2460                        }
2461                    } else if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') {
2462                        if (ch->handlers->std_err) {
2463                            zval_ptr_dtor(&ch->handlers->std_err);
2464                        }
2465                        zval_add_ref(zvalue);
2466                        ch->handlers->std_err = *zvalue;
2467                    } else {
2468                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable");
2469                        return FAILURE;
2470                    }
2471                    /* break omitted intentionally */
2472                default:
2473                    error = curl_easy_setopt(ch->cp, option, fp);
2474                    break;
2475            }
2476            break;
2477        }
2478
2479        /* Curl linked list options */
2480        case CURLOPT_HTTP200ALIASES:
2481        case CURLOPT_HTTPHEADER:
2482        case CURLOPT_POSTQUOTE:
2483        case CURLOPT_PREQUOTE:
2484        case CURLOPT_QUOTE:
2485        case CURLOPT_TELNETOPTIONS:
2486#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2487        case CURLOPT_MAIL_RCPT:
2488#endif
2489#if LIBCURL_VERSION_NUM >= 0x071503 /* Available since 7.21.3 */
2490        case CURLOPT_RESOLVE:
2491#endif
2492        {
2493            zval              **current;
2494            HashTable          *ph;
2495            struct curl_slist  *slist = NULL;
2496
2497            ph = HASH_OF(*zvalue);
2498            if (!ph) {
2499                char *name = NULL;
2500                switch (option) {
2501                    case CURLOPT_HTTPHEADER:
2502                        name = "CURLOPT_HTTPHEADER";
2503                        break;
2504                    case CURLOPT_QUOTE:
2505                        name = "CURLOPT_QUOTE";
2506                        break;
2507                    case CURLOPT_HTTP200ALIASES:
2508                        name = "CURLOPT_HTTP200ALIASES";
2509                        break;
2510                    case CURLOPT_POSTQUOTE:
2511                        name = "CURLOPT_POSTQUOTE";
2512                        break;
2513                    case CURLOPT_PREQUOTE:
2514                        name = "CURLOPT_PREQUOTE";
2515                        break;
2516                    case CURLOPT_TELNETOPTIONS:
2517                        name = "CURLOPT_TELNETOPTIONS";
2518                        break;
2519#if LIBCURL_VERSION_NUM >= 0x071400 /* Available since 7.20.0 */
2520                    case CURLOPT_MAIL_RCPT:
2521                        name = "CURLOPT_MAIL_RCPT";
2522                        break;
2523#endif
2524#if LIBCURL_VERSION_NUM >= 0x071503 /* Available since 7.21.3 */
2525                    case CURLOPT_RESOLVE:
2526                        name = "CURLOPT_RESOLVE";
2527                        break;
2528#endif
2529                }
2530                php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must pass either an object or an array with the %s argument", name);
2531                return FAILURE;
2532            }
2533
2534            for (zend_hash_internal_pointer_reset(ph);
2535                 zend_hash_get_current_data(ph, (void **) &current) == SUCCESS;
2536                 zend_hash_move_forward(ph)
2537            ) {
2538                SEPARATE_ZVAL(current);
2539                convert_to_string_ex(current);
2540
2541                slist = curl_slist_append(slist, Z_STRVAL_PP(current));
2542                if (!slist) {
2543                    php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not build curl_slist");
2544                    return 1;
2545                }
2546            }
2547            zend_hash_index_update(ch->to_free->slist, (ulong) option, &slist, sizeof(struct curl_slist *), NULL);
2548
2549            error = curl_easy_setopt(ch->cp, option, slist);
2550
2551            break;
2552        }
2553
2554        case CURLOPT_BINARYTRANSFER:
2555            /* Do nothing, just backward compatibility */
2556            break;
2557
2558        case CURLOPT_FOLLOWLOCATION:
2559            convert_to_long_ex(zvalue);
2560            if (PG(open_basedir) && *PG(open_basedir)) {
2561                if (Z_LVAL_PP(zvalue) != 0) {
2562                    php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set");
2563                    return FAILURE;
2564                }
2565            }
2566            error = curl_easy_setopt(ch->cp, option, Z_LVAL_PP(zvalue));
2567            break;
2568
2569        case CURLOPT_HEADERFUNCTION:
2570            if (ch->handlers->write_header->func_name) {
2571                zval_ptr_dtor(&ch->handlers->write_header->func_name);
2572                ch->handlers->write_header->fci_cache = empty_fcall_info_cache;
2573            }
2574            zval_add_ref(zvalue);
2575            ch->handlers->write_header->func_name = *zvalue;
2576            ch->handlers->write_header->method = PHP_CURL_USER;
2577            break;
2578
2579        case CURLOPT_POSTFIELDS:
2580            if (Z_TYPE_PP(zvalue) == IS_ARRAY || Z_TYPE_PP(zvalue) == IS_OBJECT) {
2581                zval            **current;
2582                HashTable        *postfields;
2583                struct HttpPost  *first = NULL;
2584                struct HttpPost  *last  = NULL;
2585
2586                postfields = HASH_OF(*zvalue);
2587                if (!postfields) {
2588                    php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't get HashTable in CURLOPT_POSTFIELDS");
2589                    return FAILURE;
2590                }
2591
2592                for (zend_hash_internal_pointer_reset(postfields);
2593                     zend_hash_get_current_data(postfields, (void **) &current) == SUCCESS;
2594                     zend_hash_move_forward(postfields)
2595                ) {
2596                    char  *postval;
2597                    char  *string_key = NULL;
2598                    uint   string_key_len;
2599                    ulong  num_key;
2600                    int    numeric_key;
2601
2602                    zend_hash_get_current_key_ex(postfields, &string_key, &string_key_len, &num_key, 0, NULL);
2603
2604                    /* Pretend we have a string_key here */
2605                    if(!string_key) {
2606                        spprintf(&string_key, 0, "%ld", num_key);
2607                        string_key_len = strlen(string_key)+1;
2608                        numeric_key = 1;
2609                    } else {
2610                        numeric_key = 0;
2611                    }
2612
2613                    if(Z_TYPE_PP(current) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(current), curl_CURLFile_class TSRMLS_CC)) {
2614                        /* new-style file upload */
2615                        zval *prop;
2616                        char *type = NULL, *filename = NULL;
2617
2618                        prop = zend_read_property(curl_CURLFile_class, *current, "name", sizeof("name")-1, 0 TSRMLS_CC);
2619                        if(Z_TYPE_P(prop) != IS_STRING) {
2620                            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid filename for key %s", string_key);
2621                        } else {
2622                            postval = Z_STRVAL_P(prop);
2623
2624                            if (php_check_open_basedir(postval TSRMLS_CC)) {
2625                                return 1;
2626                            }
2627
2628                            prop = zend_read_property(curl_CURLFile_class, *current, "mime", sizeof("mime")-1, 0 TSRMLS_CC);
2629                            if(Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
2630                                type = Z_STRVAL_P(prop);
2631                            }
2632                            prop = zend_read_property(curl_CURLFile_class, *current, "postname", sizeof("postname")-1, 0 TSRMLS_CC);
2633                            if(Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
2634                                filename = Z_STRVAL_P(prop);
2635                            }
2636                            error = curl_formadd(&first, &last,
2637                                            CURLFORM_COPYNAME, string_key,
2638                                            CURLFORM_NAMELENGTH, (long)string_key_len - 1,
2639                                            CURLFORM_FILENAME, filename ? filename : postval,
2640                                            CURLFORM_CONTENTTYPE, type ? type : "application/octet-stream",
2641                                            CURLFORM_FILE, postval,
2642                                            CURLFORM_END);
2643                        }
2644
2645                        if (numeric_key) {
2646                            efree(string_key);
2647                        }
2648                        continue;
2649                    }
2650
2651                    SEPARATE_ZVAL(current);
2652                    convert_to_string_ex(current);
2653
2654                    postval = Z_STRVAL_PP(current);
2655
2656                    /* The arguments after _NAMELENGTH and _CONTENTSLENGTH
2657                     * must be explicitly cast to long in curl_formadd
2658                     * use since curl needs a long not an int. */
2659                    if (!ch->safe_upload && *postval == '@') {
2660                        char *type, *filename;
2661                        ++postval;
2662
2663                        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");
2664
2665                        if ((type = php_memnstr(postval, ";type=", sizeof(";type=") - 1, postval + Z_STRLEN_PP(current)))) {
2666                            *type = '\0';
2667                        }
2668                        if ((filename = php_memnstr(postval, ";filename=", sizeof(";filename=") - 1, postval + Z_STRLEN_PP(current)))) {
2669                            *filename = '\0';
2670                        }
2671                        /* open_basedir check */
2672                        if (php_check_open_basedir(postval TSRMLS_CC)) {
2673                            return FAILURE;
2674                        }
2675                        error = curl_formadd(&first, &last,
2676                                        CURLFORM_COPYNAME, string_key,
2677                                        CURLFORM_NAMELENGTH, (long)string_key_len - 1,
2678                                        CURLFORM_FILENAME, filename ? filename + sizeof(";filename=") - 1 : postval,
2679                                        CURLFORM_CONTENTTYPE, type ? type + sizeof(";type=") - 1 : "application/octet-stream",
2680                                        CURLFORM_FILE, postval,
2681                                        CURLFORM_END);
2682                        if (type) {
2683                            *type = ';';
2684                        }
2685                        if (filename) {
2686                            *filename = ';';
2687                        }
2688                    } else {
2689                        error = curl_formadd(&first, &last,
2690                                             CURLFORM_COPYNAME, string_key,
2691                                             CURLFORM_NAMELENGTH, (long)string_key_len - 1,
2692                                             CURLFORM_COPYCONTENTS, postval,
2693                                             CURLFORM_CONTENTSLENGTH, (long)Z_STRLEN_PP(current),
2694                                             CURLFORM_END);
2695                    }
2696
2697                    if (numeric_key) {
2698                        efree(string_key);
2699                    }
2700                }
2701
2702                SAVE_CURL_ERROR(ch, error);
2703                if (error != CURLE_OK) {
2704                    return FAILURE;
2705                }
2706
2707                if (Z_REFCOUNT_P(ch->clone) <= 1) {
2708                    zend_llist_clean(&ch->to_free->post);
2709                }
2710                zend_llist_add_element(&ch->to_free->post, &first);
2711                error = curl_easy_setopt(ch->cp, CURLOPT_HTTPPOST, first);
2712
2713            } else {
2714#if LIBCURL_VERSION_NUM >= 0x071101
2715                convert_to_string_ex(zvalue);
2716                /* with curl 7.17.0 and later, we can use COPYPOSTFIELDS, but we have to provide size before */
2717                error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, Z_STRLEN_PP(zvalue));
2718                error = curl_easy_setopt(ch->cp, CURLOPT_COPYPOSTFIELDS, Z_STRVAL_PP(zvalue));
2719#else
2720                char *post = NULL;
2721
2722                convert_to_string_ex(zvalue);
2723                post = estrndup(Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue));
2724                zend_llist_add_element(&ch->to_free->str, &post);
2725
2726                curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDS, post);
2727                error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, Z_STRLEN_PP(zvalue));
2728#endif
2729            }
2730            break;
2731
2732        case CURLOPT_PROGRESSFUNCTION:
2733            curl_easy_setopt(ch->cp, CURLOPT_PROGRESSFUNCTION,  curl_progress);
2734            curl_easy_setopt(ch->cp, CURLOPT_PROGRESSDATA, ch);
2735            if (ch->handlers->progress == NULL) {
2736                ch->handlers->progress = ecalloc(1, sizeof(php_curl_progress));
2737            } else if (ch->handlers->progress->func_name) {
2738                zval_ptr_dtor(&ch->handlers->progress->func_name);
2739                ch->handlers->progress->fci_cache = empty_fcall_info_cache;
2740            }
2741            zval_add_ref(zvalue);
2742            ch->handlers->progress->func_name = *zvalue;
2743            ch->handlers->progress->method = PHP_CURL_USER;
2744            break;
2745
2746        case CURLOPT_READFUNCTION:
2747            if (ch->handlers->read->func_name) {
2748                zval_ptr_dtor(&ch->handlers->read->func_name);
2749                ch->handlers->read->fci_cache = empty_fcall_info_cache;
2750            }
2751            zval_add_ref(zvalue);
2752            ch->handlers->read->func_name = *zvalue;
2753            ch->handlers->read->method = PHP_CURL_USER;
2754            break;
2755
2756        case CURLOPT_RETURNTRANSFER:
2757            convert_to_long_ex(zvalue);
2758            if (Z_LVAL_PP(zvalue)) {
2759                ch->handlers->write->method = PHP_CURL_RETURN;
2760            } else {
2761                ch->handlers->write->method = PHP_CURL_STDOUT;
2762            }
2763            break;
2764
2765        case CURLOPT_WRITEFUNCTION:
2766            if (ch->handlers->write->func_name) {
2767                zval_ptr_dtor(&ch->handlers->write->func_name);
2768                ch->handlers->write->fci_cache = empty_fcall_info_cache;
2769            }
2770            zval_add_ref(zvalue);
2771            ch->handlers->write->func_name = *zvalue;
2772            ch->handlers->write->method = PHP_CURL_USER;
2773            break;
2774
2775#if LIBCURL_VERSION_NUM >= 0x070f05 /* Available since 7.15.5 */
2776        case CURLOPT_MAX_RECV_SPEED_LARGE:
2777        case CURLOPT_MAX_SEND_SPEED_LARGE:
2778            convert_to_long_ex(zvalue);
2779            error = curl_easy_setopt(ch->cp, option, (curl_off_t)Z_LVAL_PP(zvalue));
2780            break;
2781#endif
2782
2783#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
2784        case CURLOPT_POSTREDIR:
2785            convert_to_long_ex(zvalue);
2786            error = curl_easy_setopt(ch->cp, CURLOPT_POSTREDIR, Z_LVAL_PP(zvalue) & CURL_REDIR_POST_ALL);
2787            break;
2788#endif
2789
2790#if CURLOPT_PASSWDFUNCTION != 0
2791        case CURLOPT_PASSWDFUNCTION:
2792            if (ch->handlers->passwd) {
2793                zval_ptr_dtor(&ch->handlers->passwd);
2794            }
2795            zval_add_ref(zvalue);
2796            ch->handlers->passwd = *zvalue;
2797            error = curl_easy_setopt(ch->cp, CURLOPT_PASSWDFUNCTION, curl_passwd);
2798            error = curl_easy_setopt(ch->cp, CURLOPT_PASSWDDATA,     (void *) ch);
2799            break;
2800#endif
2801
2802        /* the following options deal with files, therefore the open_basedir check
2803         * is required.
2804         */
2805        case CURLOPT_COOKIEFILE:
2806        case CURLOPT_COOKIEJAR:
2807        case CURLOPT_RANDOM_FILE:
2808        case CURLOPT_SSLCERT:
2809#if LIBCURL_VERSION_NUM >= 0x070b00 /* Available since 7.11.0 */
2810        case CURLOPT_NETRC_FILE:
2811#endif
2812#if LIBCURL_VERSION_NUM >= 0x071001 /* Available since 7.16.1 */
2813        case CURLOPT_SSH_PRIVATE_KEYFILE:
2814        case CURLOPT_SSH_PUBLIC_KEYFILE:
2815#endif
2816#if LIBCURL_VERSION_NUM >= 0x071300 /* Available since 7.19.0 */
2817        case CURLOPT_CRLFILE:
2818        case CURLOPT_ISSUERCERT:
2819#endif
2820#if LIBCURL_VERSION_NUM >= 0x071306 /* Available since 7.19.6 */
2821        case CURLOPT_SSH_KNOWNHOSTS:
2822#endif
2823        {
2824            convert_to_string_ex(zvalue);
2825
2826            if (Z_STRLEN_PP(zvalue) && php_check_open_basedir(Z_STRVAL_PP(zvalue) TSRMLS_CC)) {
2827                return FAILURE;
2828            }
2829
2830            return php_curl_option_str(ch, option, Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue), 0 TSRMLS_CC);
2831        }
2832
2833        case CURLINFO_HEADER_OUT:
2834            convert_to_long_ex(zvalue);
2835            if (Z_LVAL_PP(zvalue) == 1) {
2836                curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, curl_debug);
2837                curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, (void *)ch);
2838                curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 1);
2839            } else {
2840                curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, NULL);
2841                curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, NULL);
2842                curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 0);
2843            }
2844            break;
2845
2846        case CURLOPT_SHARE:
2847            {
2848                php_curlsh *sh = NULL;
2849                ZEND_FETCH_RESOURCE_NO_RETURN(sh, php_curlsh *, zvalue, -1, le_curl_share_handle_name, le_curl_share_handle);
2850                if (sh) {
2851                    curl_easy_setopt(ch->cp, CURLOPT_SHARE, sh->share);
2852                }
2853            }
2854            break;
2855
2856#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
2857        case CURLOPT_FNMATCH_FUNCTION:
2858            curl_easy_setopt(ch->cp, CURLOPT_FNMATCH_FUNCTION, curl_fnmatch);
2859            curl_easy_setopt(ch->cp, CURLOPT_FNMATCH_DATA, ch);
2860            if (ch->handlers->fnmatch == NULL) {
2861                ch->handlers->fnmatch = ecalloc(1, sizeof(php_curl_fnmatch));
2862            } else if (ch->handlers->fnmatch->func_name) {
2863                zval_ptr_dtor(&ch->handlers->fnmatch->func_name);
2864                ch->handlers->fnmatch->fci_cache = empty_fcall_info_cache;
2865            }
2866            zval_add_ref(zvalue);
2867            ch->handlers->fnmatch->func_name = *zvalue;
2868            ch->handlers->fnmatch->method = PHP_CURL_USER;
2869            break;
2870#endif
2871
2872    }
2873
2874    SAVE_CURL_ERROR(ch, error);
2875    if (error != CURLE_OK) {
2876        return FAILURE;
2877    } else {
2878        return SUCCESS;
2879    }
2880}
2881/* }}} */
2882
2883/* {{{ proto bool curl_setopt(resource ch, int option, mixed value)
2884   Set an option for a cURL transfer */
2885PHP_FUNCTION(curl_setopt)
2886{
2887    zval       *zid, **zvalue;
2888    long        options;
2889    php_curl   *ch;
2890
2891    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlZ", &zid, &options, &zvalue) == FAILURE) {
2892        return;
2893    }
2894
2895    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
2896
2897    if (options <= 0 && options != CURLOPT_SAFE_UPLOAD) {
2898        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid curl configuration option");
2899        RETURN_FALSE;
2900    }
2901
2902    if (_php_curl_setopt(ch, options, zvalue TSRMLS_CC) == SUCCESS) {
2903        RETURN_TRUE;
2904    } else {
2905        RETURN_FALSE;
2906    }
2907}
2908/* }}} */
2909
2910/* {{{ proto bool curl_setopt_array(resource ch, array options)
2911   Set an array of option for a cURL transfer */
2912PHP_FUNCTION(curl_setopt_array)
2913{
2914    zval        *zid, *arr, **entry;
2915    php_curl    *ch;
2916    ulong       option;
2917    HashPosition    pos;
2918    char        *string_key;
2919    uint        str_key_len;
2920
2921    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "za", &zid, &arr) == FAILURE) {
2922        return;
2923    }
2924
2925    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
2926
2927    zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos);
2928    while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **)&entry, &pos) == SUCCESS) {
2929        if (zend_hash_get_current_key_ex(Z_ARRVAL_P(arr), &string_key, &str_key_len, &option, 0, &pos) != HASH_KEY_IS_LONG) {
2930            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array keys must be CURLOPT constants or equivalent integer values");
2931            RETURN_FALSE;
2932        }
2933        if (_php_curl_setopt(ch, (long) option, entry TSRMLS_CC) == FAILURE) {
2934            RETURN_FALSE;
2935        }
2936        zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos);
2937    }
2938    RETURN_TRUE;
2939}
2940/* }}} */
2941
2942/* {{{ _php_curl_cleanup_handle(ch)
2943   Cleanup an execution phase */
2944void _php_curl_cleanup_handle(php_curl *ch)
2945{
2946    if (ch->handlers->write->buf.len > 0) {
2947        smart_str_free(&ch->handlers->write->buf);
2948    }
2949    if (ch->header.str_len) {
2950        efree(ch->header.str);
2951        ch->header.str_len = 0;
2952    }
2953
2954    memset(ch->err.str, 0, CURL_ERROR_SIZE + 1);
2955    ch->err.no = 0;
2956}
2957/* }}} */
2958
2959/* {{{ proto bool curl_exec(resource ch)
2960   Perform a cURL session */
2961PHP_FUNCTION(curl_exec)
2962{
2963    CURLcode    error;
2964    zval        *zid;
2965    php_curl    *ch;
2966
2967    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
2968        return;
2969    }
2970
2971    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
2972
2973    _php_curl_verify_handlers(ch, 1 TSRMLS_CC);
2974
2975    _php_curl_cleanup_handle(ch);
2976
2977    error = curl_easy_perform(ch->cp);
2978    SAVE_CURL_ERROR(ch, error);
2979    /* CURLE_PARTIAL_FILE is returned by HEAD requests */
2980    if (error != CURLE_OK && error != CURLE_PARTIAL_FILE) {
2981        if (ch->handlers->write->buf.len > 0) {
2982            smart_str_free(&ch->handlers->write->buf);
2983        }
2984        RETURN_FALSE;
2985    }
2986
2987    if (ch->handlers->std_err) {
2988        php_stream  *stream;
2989        stream = (php_stream*)zend_fetch_resource(&ch->handlers->std_err TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
2990        if (stream) {
2991            php_stream_flush(stream);
2992        }
2993    }
2994
2995    if (ch->handlers->write->method == PHP_CURL_RETURN && ch->handlers->write->buf.len > 0) {
2996        smart_str_0(&ch->handlers->write->buf);
2997        RETURN_STRINGL(ch->handlers->write->buf.c, ch->handlers->write->buf.len, 1);
2998    }
2999
3000    /* flush the file handle, so any remaining data is synched to disk */
3001    if (ch->handlers->write->method == PHP_CURL_FILE && ch->handlers->write->fp) {
3002        fflush(ch->handlers->write->fp);
3003    }
3004    if (ch->handlers->write_header->method == PHP_CURL_FILE && ch->handlers->write_header->fp) {
3005        fflush(ch->handlers->write_header->fp);
3006    }
3007
3008    if (ch->handlers->write->method == PHP_CURL_RETURN) {
3009        RETURN_EMPTY_STRING();
3010    } else {
3011        RETURN_TRUE;
3012    }
3013}
3014/* }}} */
3015
3016/* {{{ proto mixed curl_getinfo(resource ch [, int option])
3017   Get information regarding a specific transfer */
3018PHP_FUNCTION(curl_getinfo)
3019{
3020    zval        *zid;
3021    php_curl    *ch;
3022    long        option = 0;
3023
3024    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zid, &option) == FAILURE) {
3025        return;
3026    }
3027
3028    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
3029
3030    if (ZEND_NUM_ARGS() < 2) {
3031        char   *s_code;
3032        long    l_code;
3033        double  d_code;
3034#if LIBCURL_VERSION_NUM >  0x071301
3035        struct curl_certinfo *ci = NULL;
3036        zval *listcode;
3037#endif
3038
3039        array_init(return_value);
3040
3041        if (curl_easy_getinfo(ch->cp, CURLINFO_EFFECTIVE_URL, &s_code) == CURLE_OK) {
3042            CAAS("url", s_code);
3043        }
3044        if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_TYPE, &s_code) == CURLE_OK) {
3045            if (s_code != NULL) {
3046                CAAS("content_type", s_code);
3047            } else {
3048                zval *retnull;
3049                MAKE_STD_ZVAL(retnull);
3050                ZVAL_NULL(retnull);
3051                CAAZ("content_type", retnull);
3052            }
3053        }
3054        if (curl_easy_getinfo(ch->cp, CURLINFO_HTTP_CODE, &l_code) == CURLE_OK) {
3055            CAAL("http_code", l_code);
3056        }
3057        if (curl_easy_getinfo(ch->cp, CURLINFO_HEADER_SIZE, &l_code) == CURLE_OK) {
3058            CAAL("header_size", l_code);
3059        }
3060        if (curl_easy_getinfo(ch->cp, CURLINFO_REQUEST_SIZE, &l_code) == CURLE_OK) {
3061            CAAL("request_size", l_code);
3062        }
3063        if (curl_easy_getinfo(ch->cp, CURLINFO_FILETIME, &l_code) == CURLE_OK) {
3064            CAAL("filetime", l_code);
3065        }
3066        if (curl_easy_getinfo(ch->cp, CURLINFO_SSL_VERIFYRESULT, &l_code) == CURLE_OK) {
3067            CAAL("ssl_verify_result", l_code);
3068        }
3069        if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_COUNT, &l_code) == CURLE_OK) {
3070            CAAL("redirect_count", l_code);
3071        }
3072        if (curl_easy_getinfo(ch->cp, CURLINFO_TOTAL_TIME, &d_code) == CURLE_OK) {
3073            CAAD("total_time", d_code);
3074        }
3075        if (curl_easy_getinfo(ch->cp, CURLINFO_NAMELOOKUP_TIME, &d_code) == CURLE_OK) {
3076            CAAD("namelookup_time", d_code);
3077        }
3078        if (curl_easy_getinfo(ch->cp, CURLINFO_CONNECT_TIME, &d_code) == CURLE_OK) {
3079            CAAD("connect_time", d_code);
3080        }
3081        if (curl_easy_getinfo(ch->cp, CURLINFO_PRETRANSFER_TIME, &d_code) == CURLE_OK) {
3082            CAAD("pretransfer_time", d_code);
3083        }
3084        if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_UPLOAD, &d_code) == CURLE_OK) {
3085            CAAD("size_upload", d_code);
3086        }
3087        if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_DOWNLOAD, &d_code) == CURLE_OK) {
3088            CAAD("size_download", d_code);
3089        }
3090        if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_DOWNLOAD, &d_code) == CURLE_OK) {
3091            CAAD("speed_download", d_code);
3092        }
3093        if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_UPLOAD, &d_code) == CURLE_OK) {
3094            CAAD("speed_upload", d_code);
3095        }
3096        if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d_code) == CURLE_OK) {
3097            CAAD("download_content_length", d_code);
3098        }
3099        if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_UPLOAD, &d_code) == CURLE_OK) {
3100            CAAD("upload_content_length", d_code);
3101        }
3102        if (curl_easy_getinfo(ch->cp, CURLINFO_STARTTRANSFER_TIME, &d_code) == CURLE_OK) {
3103            CAAD("starttransfer_time", d_code);
3104        }
3105        if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_TIME, &d_code) == CURLE_OK) {
3106            CAAD("redirect_time", d_code);
3107        }
3108#if LIBCURL_VERSION_NUM >= 0x071202 /* Available since 7.18.2 */
3109        if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_URL, &s_code) == CURLE_OK) {
3110            CAAS("redirect_url", s_code);
3111        }
3112#endif
3113#if LIBCURL_VERSION_NUM >= 0x071300 /* Available since 7.19.0 */
3114        if (curl_easy_getinfo(ch->cp, CURLINFO_PRIMARY_IP, &s_code) == CURLE_OK) {
3115            CAAS("primary_ip", s_code);
3116        }
3117#endif
3118#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
3119        if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
3120            MAKE_STD_ZVAL(listcode);
3121            array_init(listcode);
3122            create_certinfo(ci, listcode TSRMLS_CC);
3123            CAAZ("certinfo", listcode);
3124        }
3125#endif
3126#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
3127        if (curl_easy_getinfo(ch->cp, CURLINFO_PRIMARY_PORT, &l_code) == CURLE_OK) {
3128            CAAL("primary_port", l_code);
3129        }
3130        if (curl_easy_getinfo(ch->cp, CURLINFO_LOCAL_IP, &s_code) == CURLE_OK) {
3131            CAAS("local_ip", s_code);
3132        }
3133        if (curl_easy_getinfo(ch->cp, CURLINFO_LOCAL_PORT, &l_code) == CURLE_OK) {
3134            CAAL("local_port", l_code);
3135        }
3136#endif
3137        if (ch->header.str_len > 0) {
3138            CAAS("request_header", ch->header.str);
3139        }
3140    } else {
3141        switch (option) {
3142            case CURLINFO_HEADER_OUT:
3143                if (ch->header.str_len > 0) {
3144                    RETURN_STRINGL(ch->header.str, ch->header.str_len, 1);
3145                } else {
3146                    RETURN_FALSE;
3147                }
3148#if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */
3149            case CURLINFO_CERTINFO: {
3150                struct curl_certinfo *ci = NULL;
3151
3152                array_init(return_value);
3153
3154                if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
3155                    create_certinfo(ci, return_value TSRMLS_CC);
3156                } else {
3157                    RETURN_FALSE;
3158                }
3159                break;
3160            }
3161#endif
3162            default: {
3163                int type = CURLINFO_TYPEMASK & option;
3164                switch (type) {
3165                    case CURLINFO_STRING:
3166                    {
3167                        char *s_code = NULL;
3168
3169                        if (curl_easy_getinfo(ch->cp, option, &s_code) == CURLE_OK && s_code) {
3170                            RETURN_STRING(s_code, 1);
3171                        } else {
3172                            RETURN_FALSE;
3173                        }
3174                        break;
3175                    }
3176                    case CURLINFO_LONG:
3177                    {
3178                        long code = 0;
3179
3180                        if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
3181                            RETURN_LONG(code);
3182                        } else {
3183                            RETURN_FALSE;
3184                        }
3185                        break;
3186                    }
3187                    case CURLINFO_DOUBLE:
3188                    {
3189                        double code = 0.0;
3190
3191                        if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
3192                            RETURN_DOUBLE(code);
3193                        } else {
3194                            RETURN_FALSE;
3195                        }
3196                        break;
3197                    }
3198#if LIBCURL_VERSION_NUM >= 0x070c03 /* Available since 7.12.3 */
3199                    case CURLINFO_SLIST:
3200                    {
3201                        struct curl_slist *slist;
3202                        array_init(return_value);
3203                        if (curl_easy_getinfo(ch->cp, option, &slist) == CURLE_OK) {
3204                            while (slist) {
3205                                add_next_index_string(return_value, slist->data, 1);
3206                                slist = slist->next;
3207                            }
3208                            curl_slist_free_all(slist);
3209                        } else {
3210                            RETURN_FALSE;
3211                        }
3212                        break;
3213                    }
3214#endif
3215                    default:
3216                        RETURN_FALSE;
3217                }
3218            }
3219        }
3220    }
3221}
3222/* }}} */
3223
3224/* {{{ proto string curl_error(resource ch)
3225   Return a string contain the last error for the current session */
3226PHP_FUNCTION(curl_error)
3227{
3228    zval        *zid;
3229    php_curl    *ch;
3230
3231    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
3232        return;
3233    }
3234
3235    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
3236
3237    ch->err.str[CURL_ERROR_SIZE] = 0;
3238    RETURN_STRING(ch->err.str, 1);
3239}
3240/* }}} */
3241
3242/* {{{ proto int curl_errno(resource ch)
3243   Return an integer containing the last error number */
3244PHP_FUNCTION(curl_errno)
3245{
3246    zval        *zid;
3247    php_curl    *ch;
3248
3249    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
3250        return;
3251    }
3252
3253    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
3254
3255    RETURN_LONG(ch->err.no);
3256}
3257/* }}} */
3258
3259/* {{{ proto void curl_close(resource ch)
3260   Close a cURL session */
3261PHP_FUNCTION(curl_close)
3262{
3263    zval        *zid;
3264    php_curl    *ch;
3265
3266    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
3267        return;
3268    }
3269
3270    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
3271
3272    if (ch->in_callback) {
3273        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to close cURL handle from a callback");
3274        return;
3275    }
3276
3277    zend_list_delete(Z_LVAL_P(zid));
3278}
3279/* }}} */
3280
3281/* {{{ _php_curl_close()
3282   List destructor for curl handles */
3283static void _php_curl_close_ex(php_curl *ch TSRMLS_DC)
3284{
3285#if PHP_CURL_DEBUG
3286    fprintf(stderr, "DTOR CALLED, ch = %x\n", ch);
3287#endif
3288
3289    _php_curl_verify_handlers(ch, 0 TSRMLS_CC);
3290
3291    /*
3292     * Libcurl is doing connection caching. When easy handle is cleaned up,
3293     * if the handle was previously used by the curl_multi_api, the connection
3294     * remains open un the curl multi handle is cleaned up. Some protocols are
3295     * sending content like the FTP one, and libcurl try to use the
3296     * WRITEFUNCTION or the HEADERFUNCTION. Since structures used in those
3297     * callback are freed, we need to use an other callback to which avoid
3298     * segfaults.
3299     *
3300     * Libcurl commit d021f2e8a00 fix this issue and should be part of 7.28.2
3301     */
3302    curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION, curl_write_nothing);
3303    curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION, curl_write_nothing);
3304
3305    curl_easy_cleanup(ch->cp);
3306
3307    /* cURL destructors should be invoked only by last curl handle */
3308    if (Z_REFCOUNT_P(ch->clone) <= 1) {
3309        zend_llist_clean(&ch->to_free->str);
3310        zend_llist_clean(&ch->to_free->post);
3311        zend_hash_destroy(ch->to_free->slist);
3312        efree(ch->to_free->slist);
3313        efree(ch->to_free);
3314        FREE_ZVAL(ch->clone);
3315    } else {
3316        Z_DELREF_P(ch->clone);
3317    }
3318
3319    if (ch->handlers->write->buf.len > 0) {
3320        smart_str_free(&ch->handlers->write->buf);
3321    }
3322    if (ch->handlers->write->func_name) {
3323        zval_ptr_dtor(&ch->handlers->write->func_name);
3324    }
3325    if (ch->handlers->read->func_name) {
3326        zval_ptr_dtor(&ch->handlers->read->func_name);
3327    }
3328    if (ch->handlers->write_header->func_name) {
3329        zval_ptr_dtor(&ch->handlers->write_header->func_name);
3330    }
3331#if CURLOPT_PASSWDFUNCTION != 0
3332    if (ch->handlers->passwd) {
3333        zval_ptr_dtor(&ch->handlers->passwd);
3334    }
3335#endif
3336    if (ch->handlers->std_err) {
3337        zval_ptr_dtor(&ch->handlers->std_err);
3338    }
3339    if (ch->header.str_len > 0) {
3340        efree(ch->header.str);
3341    }
3342
3343    if (ch->handlers->write_header->stream) {
3344        zval_ptr_dtor(&ch->handlers->write_header->stream);
3345    }
3346    if (ch->handlers->write->stream) {
3347        zval_ptr_dtor(&ch->handlers->write->stream);
3348    }
3349    if (ch->handlers->read->stream) {
3350        zval_ptr_dtor(&ch->handlers->read->stream);
3351    }
3352
3353    efree(ch->handlers->write);
3354    efree(ch->handlers->write_header);
3355    efree(ch->handlers->read);
3356
3357    if (ch->handlers->progress) {
3358        if (ch->handlers->progress->func_name) {
3359            zval_ptr_dtor(&ch->handlers->progress->func_name);
3360        }
3361        efree(ch->handlers->progress);
3362    }
3363
3364#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
3365    if (ch->handlers->fnmatch) {
3366        if (ch->handlers->fnmatch->func_name) {
3367            zval_ptr_dtor(&ch->handlers->fnmatch->func_name);
3368        }
3369        efree(ch->handlers->fnmatch);
3370    }
3371#endif
3372
3373    efree(ch->handlers);
3374    efree(ch);
3375}
3376/* }}} */
3377
3378/* {{{ _php_curl_close()
3379   List destructor for curl handles */
3380static void _php_curl_close(zend_rsrc_list_entry *rsrc TSRMLS_DC)
3381{
3382    php_curl *ch = (php_curl *) rsrc->ptr;
3383    _php_curl_close_ex(ch TSRMLS_CC);
3384}
3385/* }}} */
3386
3387#if LIBCURL_VERSION_NUM >= 0x070c00 /* Available since 7.12.0 */
3388/* {{{ proto bool curl_strerror(int code)
3389      return string describing error code */
3390PHP_FUNCTION(curl_strerror)
3391{
3392    long code;
3393    const char *str;
3394
3395    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code) == FAILURE) {
3396        return;
3397    }
3398
3399    str = curl_easy_strerror(code);
3400    if (str) {
3401        RETURN_STRING(str, 1);
3402    } else {
3403        RETURN_NULL();
3404    }
3405}
3406/* }}} */
3407#endif
3408
3409#if LIBCURL_VERSION_NUM >= 0x070c01 /* 7.12.1 */
3410/* {{{ _php_curl_reset_handlers()
3411   Reset all handlers of a given php_curl */
3412static void _php_curl_reset_handlers(php_curl *ch)
3413{
3414    if (ch->handlers->write->stream) {
3415        Z_DELREF_P(ch->handlers->write->stream);
3416        ch->handlers->write->stream = NULL;
3417    }
3418    ch->handlers->write->fp = NULL;
3419    ch->handlers->write->method = PHP_CURL_STDOUT;
3420
3421    if (ch->handlers->write_header->stream) {
3422        Z_DELREF_P(ch->handlers->write_header->stream);
3423        ch->handlers->write_header->stream = NULL;
3424    }
3425    ch->handlers->write_header->fp = NULL;
3426    ch->handlers->write_header->method = PHP_CURL_IGNORE;
3427
3428    if (ch->handlers->read->stream) {
3429        Z_DELREF_P(ch->handlers->read->stream);
3430        ch->handlers->read->stream = NULL;
3431    }
3432    ch->handlers->read->fp = NULL;
3433    ch->handlers->read->fd = 0;
3434    ch->handlers->read->method  = PHP_CURL_DIRECT;
3435
3436    if (ch->handlers->std_err) {
3437        zval_ptr_dtor(&ch->handlers->std_err);
3438        ch->handlers->std_err = NULL;
3439    }
3440
3441    if (ch->handlers->progress) {
3442        if (ch->handlers->progress->func_name) {
3443            zval_ptr_dtor(&ch->handlers->progress->func_name);
3444        }
3445        efree(ch->handlers->progress);
3446        ch->handlers->progress = NULL;
3447    }
3448
3449#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
3450    if (ch->handlers->fnmatch) {
3451        if (ch->handlers->fnmatch->func_name) {
3452            zval_ptr_dtor(&ch->handlers->fnmatch->func_name);
3453        }
3454        efree(ch->handlers->fnmatch);
3455        ch->handlers->fnmatch = NULL;
3456    }
3457#endif
3458
3459}
3460/* }}} */
3461
3462/* {{{ proto void curl_reset(resource ch)
3463   Reset all options of a libcurl session handle */
3464PHP_FUNCTION(curl_reset)
3465{
3466    zval       *zid;
3467    php_curl   *ch;
3468
3469    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
3470        return;
3471    }
3472
3473    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
3474
3475    if (ch->in_callback) {
3476        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to reset cURL handle from a callback");
3477        return;
3478    }
3479
3480    curl_easy_reset(ch->cp);
3481    _php_curl_reset_handlers(ch);
3482    _php_curl_set_default_options(ch);
3483}
3484/* }}} */
3485#endif
3486
3487#if LIBCURL_VERSION_NUM > 0x070f03 /* 7.15.4 */
3488/* {{{ proto void curl_escape(resource ch, string str)
3489   URL encodes the given string */
3490PHP_FUNCTION(curl_escape)
3491{
3492    char       *str = NULL, *res = NULL;
3493    int        str_len = 0;
3494    zval       *zid;
3495    php_curl   *ch;
3496
3497    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zid, &str, &str_len) == FAILURE) {
3498        return;
3499    }
3500
3501    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
3502
3503    if ((res = curl_easy_escape(ch->cp, str, str_len))) {
3504        RETVAL_STRING(res, 1);
3505        curl_free(res);
3506    } else {
3507        RETURN_FALSE;
3508    }
3509}
3510/* }}} */
3511
3512/* {{{ proto void curl_unescape(resource ch, string str)
3513   URL decodes the given string */
3514PHP_FUNCTION(curl_unescape)
3515{
3516    char       *str = NULL, *out = NULL;
3517    int        str_len = 0, out_len;
3518    zval       *zid;
3519    php_curl   *ch;
3520
3521    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zid, &str, &str_len) == FAILURE) {
3522        return;
3523    }
3524
3525    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
3526
3527    if ((out = curl_easy_unescape(ch->cp, str, str_len, &out_len))) {
3528        RETVAL_STRINGL(out, out_len, 1);
3529        curl_free(out);
3530    } else {
3531        RETURN_FALSE;
3532    }
3533}
3534/* }}} */
3535#endif
3536
3537#if LIBCURL_VERSION_NUM >= 0x071200 /* 7.18.0 */
3538/* {{{ proto void curl_pause(resource ch, int bitmask)
3539       pause and unpause a connection */
3540PHP_FUNCTION(curl_pause)
3541{
3542    long       bitmask;
3543    zval       *zid;
3544    php_curl   *ch;
3545
3546    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zid, &bitmask) == FAILURE) {
3547        return;
3548    }
3549
3550    ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
3551
3552    RETURN_LONG(curl_easy_pause(ch->cp, bitmask));
3553}
3554/* }}} */
3555#endif
3556
3557#endif /* HAVE_CURL */
3558
3559/*
3560 * Local variables:
3561 * tab-width: 4
3562 * c-basic-offset: 4
3563 * End:
3564 * vim600: fdm=marker
3565 * vim: noet sw=4 ts=4
3566 */
3567