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