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