PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/curl - interface.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 989 1165 84.9 %
Date: 2014-04-18 Functions: 28 31 90.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2013 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             : 
      91             : int  le_curl;
      92             : int  le_curl_multi_handle;
      93             : 
      94             : #ifdef PHP_CURL_NEED_OPENSSL_TSL /* {{{ */
      95             : static MUTEX_T *php_curl_openssl_tsl = NULL;
      96             : 
      97             : static void php_curl_ssl_lock(int mode, int n, const char * file, int line)
      98             : {
      99             :         if (mode & CRYPTO_LOCK) {
     100             :                 tsrm_mutex_lock(php_curl_openssl_tsl[n]);
     101             :         } else {
     102             :                 tsrm_mutex_unlock(php_curl_openssl_tsl[n]);
     103             :         }
     104             : }
     105             : 
     106             : static unsigned long php_curl_ssl_id(void)
     107             : {
     108             :         return (unsigned long) tsrm_thread_id();
     109             : }
     110             : #endif
     111             : /* }}} */
     112             : 
     113             : #ifdef PHP_CURL_NEED_GNUTLS_TSL /* {{{ */
     114             : static int php_curl_ssl_mutex_create(void **m)
     115             : {
     116             :         if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) {
     117             :                 return SUCCESS;
     118             :         } else {
     119             :                 return FAILURE;
     120             :         }
     121             : }
     122             : 
     123             : static int php_curl_ssl_mutex_destroy(void **m)
     124             : {
     125             :         tsrm_mutex_free(*((MUTEX_T *) m));
     126             :         return SUCCESS;
     127             : }
     128             : 
     129             : static int php_curl_ssl_mutex_lock(void **m)
     130             : {
     131             :         return tsrm_mutex_lock(*((MUTEX_T *) m));
     132             : }
     133             : 
     134             : static int php_curl_ssl_mutex_unlock(void **m)
     135             : {
     136             :         return tsrm_mutex_unlock(*((MUTEX_T *) m));
     137             : }
     138             : 
     139             : static struct gcry_thread_cbs php_curl_gnutls_tsl = {
     140             :         GCRY_THREAD_OPTION_USER,
     141             :         NULL,
     142             :         php_curl_ssl_mutex_create,
     143             :         php_curl_ssl_mutex_destroy,
     144             :         php_curl_ssl_mutex_lock,
     145             :         php_curl_ssl_mutex_unlock
     146             : };
     147             : #endif
     148             : /* }}} */
     149             : 
     150             : static void _php_curl_close_ex(php_curl *ch TSRMLS_DC);
     151             : static void _php_curl_close(zend_rsrc_list_entry *rsrc TSRMLS_DC);
     152             : 
     153             : 
     154             : #define SAVE_CURL_ERROR(__handle, __err) (__handle)->err.no = (int) __err;
     155             : 
     156             : #define CAAL(s, v) add_assoc_long_ex(return_value, s, sizeof(s), (long) v);
     157             : #define CAAD(s, v) add_assoc_double_ex(return_value, s, sizeof(s), (double) v);
     158             : #define CAAS(s, v) add_assoc_string_ex(return_value, s, sizeof(s), (char *) (v ? v : ""), 1);
     159             : #define CAAZ(s, v) add_assoc_zval_ex(return_value, s, sizeof(s), (zval *) v);
     160             : 
     161             : #if defined(PHP_WIN32) || defined(__GNUC__)
     162             : # define php_curl_ret(__ret) RETVAL_FALSE; return __ret;
     163             : #else
     164             : # define php_curl_ret(__ret) RETVAL_FALSE; return;
     165             : #endif
     166             : 
     167        1105 : static int php_curl_option_url(php_curl *ch, const char *url, const int len) /* {{{ */
     168             : {
     169        1105 :         CURLcode error = CURLE_OK;
     170             : #if LIBCURL_VERSION_NUM < 0x071100
     171             :         char *copystr = NULL;
     172             : #endif
     173             :         TSRMLS_FETCH();
     174             : 
     175             :         /* Disable file:// if open_basedir or safe_mode are used */
     176        1105 :         if ((PG(open_basedir) && *PG(open_basedir)) || PG(safe_mode)) {
     177             : #if LIBCURL_VERSION_NUM >= 0x071304
     178           0 :                 error = curl_easy_setopt(ch->cp, CURLOPT_PROTOCOLS, CURLPROTO_ALL & ~CURLPROTO_FILE);
     179             : #else
     180             :                 php_url *uri;
     181             : 
     182             :                 if (!(uri = php_url_parse_ex(url, len))) {
     183             :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid URL '%s'", url);
     184             :                         return 0;
     185             :                 }
     186             : 
     187             :                 if (uri->scheme && !strncasecmp("file", uri->scheme, sizeof("file"))) {
     188             :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Protocol 'file' disabled in cURL");
     189             :                         php_url_free(uri);
     190             :                         return 0;
     191             :                 }
     192             :                 php_url_free(uri);
     193             : #endif
     194             :         }
     195             :         /* Strings passed to libcurl as 'char *' arguments, are copied by the library... NOTE: before 7.17.0 strings were not copied. */
     196             : #if LIBCURL_VERSION_NUM >= 0x071100
     197        1105 :         error = curl_easy_setopt(ch->cp, CURLOPT_URL, url);
     198             : #else
     199             :         copystr = estrndup(url, len);
     200             :         error = curl_easy_setopt(ch->cp, CURLOPT_URL, copystr);
     201             :         zend_llist_add_element(&ch->to_free->str, &copystr);
     202             : #endif
     203             : 
     204        1105 :         return (error == CURLE_OK ? 1 : 0);
     205             : }
     206             : /* }}} */
     207             : 
     208        1346 : int _php_curl_verify_handlers(php_curl *ch, int reporterror TSRMLS_DC) /* {{{ */
     209             : {
     210             :         php_stream *stream;
     211        1346 :         if (!ch || !ch->handlers) {
     212           0 :                 return 0;
     213             :         }
     214             : 
     215        1346 :         if (ch->handlers->std_err) {
     216          15 :                 stream = (php_stream *) zend_fetch_resource(&ch->handlers->std_err TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
     217          15 :                 if (stream == NULL) {
     218           6 :                         if (reporterror) {
     219           4 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_STDERR resource has gone away, resetting to stderr");
     220             :                         }
     221           6 :                         zval_ptr_dtor(&ch->handlers->std_err);
     222           6 :                         ch->handlers->std_err = NULL;
     223             : 
     224           6 :                         curl_easy_setopt(ch->cp, CURLOPT_STDERR, stderr);
     225             :                 }
     226             :         }
     227        1346 :         if (ch->handlers->read && ch->handlers->read->stream) {
     228           7 :                 stream = (php_stream *) zend_fetch_resource(&ch->handlers->read->stream TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
     229           7 :                 if (stream == NULL) {
     230           3 :                         if (reporterror) {
     231           3 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_INFILE resource has gone away, resetting to default");
     232             :                         }
     233           3 :                         zval_ptr_dtor(&ch->handlers->read->stream);
     234           3 :                         ch->handlers->read->fd = 0;
     235           3 :                         ch->handlers->read->fp = 0;
     236           3 :                         ch->handlers->read->stream = NULL;
     237             : 
     238           3 :                         curl_easy_setopt(ch->cp, CURLOPT_INFILE, (void *) ch);
     239             :                 }
     240             :         }
     241        1346 :         if (ch->handlers->write_header && ch->handlers->write_header->stream) {
     242           3 :                 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());
     243           3 :                 if (stream == NULL) {
     244           3 :                         if (reporterror) {
     245           3 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_WRITEHEADER resource has gone away, resetting to default");
     246             :                         }
     247           3 :                         zval_ptr_dtor(&ch->handlers->write_header->stream);
     248           3 :                         ch->handlers->write_header->fp = 0;
     249           3 :                         ch->handlers->write_header->stream = NULL;
     250             : 
     251           3 :                         ch->handlers->write_header->method = PHP_CURL_IGNORE;
     252           3 :                         curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER, (void *) ch);
     253             :                 }
     254             :         }
     255        1346 :         if (ch->handlers->write && ch->handlers->write->stream) {
     256        1035 :                 stream = (php_stream *) zend_fetch_resource(&ch->handlers->write->stream TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
     257        1035 :                 if (stream == NULL) {
     258           4 :                         if (reporterror) {
     259           3 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_FILE resource has gone away, resetting to default");
     260             :                         }
     261           4 :                         zval_ptr_dtor(&ch->handlers->write->stream);
     262           4 :                         ch->handlers->write->fp = 0;
     263           4 :                         ch->handlers->write->stream = NULL;
     264             : 
     265           4 :                         ch->handlers->write->method = PHP_CURL_STDOUT;
     266           4 :                         ch->handlers->write->type   = PHP_CURL_ASCII;
     267           4 :                         curl_easy_setopt(ch->cp, CURLOPT_FILE, (void *) ch);
     268             :                 }
     269             :         }
     270        1346 :         return 1;
     271             : }
     272             : /* }}} */
     273             : 
     274             : /* {{{ arginfo */
     275             : ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_version, 0, 0, 0)
     276             :         ZEND_ARG_INFO(0, version)
     277             : ZEND_END_ARG_INFO()
     278             : 
     279             : ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_init, 0, 0, 0)
     280             :         ZEND_ARG_INFO(0, url)
     281             : ZEND_END_ARG_INFO()
     282             : 
     283             : ZEND_BEGIN_ARG_INFO(arginfo_curl_copy_handle, 0)
     284             :         ZEND_ARG_INFO(0, ch)
     285             : ZEND_END_ARG_INFO()
     286             : 
     287             : ZEND_BEGIN_ARG_INFO(arginfo_curl_setopt, 0)
     288             :         ZEND_ARG_INFO(0, ch)
     289             :         ZEND_ARG_INFO(0, option)
     290             :         ZEND_ARG_INFO(0, value)
     291             : ZEND_END_ARG_INFO()
     292             : 
     293             : ZEND_BEGIN_ARG_INFO(arginfo_curl_setopt_array, 0)
     294             :         ZEND_ARG_INFO(0, ch)
     295             :         ZEND_ARG_ARRAY_INFO(0, options, 0)
     296             : ZEND_END_ARG_INFO()
     297             : 
     298             : ZEND_BEGIN_ARG_INFO(arginfo_curl_exec, 0)
     299             :         ZEND_ARG_INFO(0, ch)
     300             : ZEND_END_ARG_INFO()
     301             : 
     302             : ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_getinfo, 0, 0, 1)
     303             :         ZEND_ARG_INFO(0, ch)
     304             :         ZEND_ARG_INFO(0, option)
     305             : ZEND_END_ARG_INFO()
     306             : 
     307             : ZEND_BEGIN_ARG_INFO(arginfo_curl_error, 0)
     308             :         ZEND_ARG_INFO(0, ch)
     309             : ZEND_END_ARG_INFO()
     310             : 
     311             : ZEND_BEGIN_ARG_INFO(arginfo_curl_errno, 0)
     312             :         ZEND_ARG_INFO(0, ch)
     313             : ZEND_END_ARG_INFO()
     314             : 
     315             : ZEND_BEGIN_ARG_INFO(arginfo_curl_close, 0)
     316             :         ZEND_ARG_INFO(0, ch)
     317             : ZEND_END_ARG_INFO()
     318             : 
     319             : ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_init, 0)
     320             : ZEND_END_ARG_INFO()
     321             : 
     322             : ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_add_handle, 0)
     323             :         ZEND_ARG_INFO(0, mh)
     324             :         ZEND_ARG_INFO(0, ch)
     325             : ZEND_END_ARG_INFO()
     326             : 
     327             : ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_remove_handle, 0)
     328             :         ZEND_ARG_INFO(0, mh)
     329             :         ZEND_ARG_INFO(0, ch)
     330             : ZEND_END_ARG_INFO()
     331             : 
     332             : ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_multi_select, 0, 0, 1)
     333             :         ZEND_ARG_INFO(0, mh)
     334             :         ZEND_ARG_INFO(0, timeout)
     335             : ZEND_END_ARG_INFO()
     336             : 
     337             : ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_multi_exec, 0, 0, 1)
     338             :         ZEND_ARG_INFO(0, mh)
     339             :         ZEND_ARG_INFO(1, still_running)
     340             : ZEND_END_ARG_INFO()
     341             : 
     342             : ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_getcontent, 0)
     343             :         ZEND_ARG_INFO(0, ch)
     344             : ZEND_END_ARG_INFO()
     345             : 
     346             : ZEND_BEGIN_ARG_INFO_EX(arginfo_curl_multi_info_read, 0, 0, 1)
     347             :         ZEND_ARG_INFO(0, mh)
     348             :         ZEND_ARG_INFO(1, msgs_in_queue)
     349             : ZEND_END_ARG_INFO()
     350             : 
     351             : ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_close, 0)
     352             :         ZEND_ARG_INFO(0, mh)
     353             : ZEND_END_ARG_INFO()
     354             : /* }}} */
     355             : 
     356             : /* {{{ curl_functions[]
     357             :  */
     358             : const zend_function_entry curl_functions[] = {
     359             :         PHP_FE(curl_init,                arginfo_curl_init)
     360             :         PHP_FE(curl_copy_handle,         arginfo_curl_copy_handle)
     361             :         PHP_FE(curl_version,             arginfo_curl_version)
     362             :         PHP_FE(curl_setopt,              arginfo_curl_setopt)
     363             :         PHP_FE(curl_setopt_array,        arginfo_curl_setopt_array)
     364             :         PHP_FE(curl_exec,                arginfo_curl_exec)
     365             :         PHP_FE(curl_getinfo,             arginfo_curl_getinfo)
     366             :         PHP_FE(curl_error,               arginfo_curl_error)
     367             :         PHP_FE(curl_errno,               arginfo_curl_errno)
     368             :         PHP_FE(curl_close,               arginfo_curl_close)
     369             :         PHP_FE(curl_multi_init,          arginfo_curl_multi_init)
     370             :         PHP_FE(curl_multi_add_handle,    arginfo_curl_multi_add_handle)
     371             :         PHP_FE(curl_multi_remove_handle, arginfo_curl_multi_remove_handle)
     372             :         PHP_FE(curl_multi_select,        arginfo_curl_multi_select)
     373             :         PHP_FE(curl_multi_exec,          arginfo_curl_multi_exec)
     374             :         PHP_FE(curl_multi_getcontent,    arginfo_curl_multi_getcontent)
     375             :         PHP_FE(curl_multi_info_read,     arginfo_curl_multi_info_read)
     376             :         PHP_FE(curl_multi_close,         arginfo_curl_multi_close)
     377             :         PHP_FE_END
     378             : };
     379             : /* }}} */
     380             : 
     381             : /* {{{ curl_module_entry
     382             :  */
     383             : zend_module_entry curl_module_entry = {
     384             :         STANDARD_MODULE_HEADER,
     385             :         "curl",
     386             :         curl_functions,
     387             :         PHP_MINIT(curl),
     388             :         PHP_MSHUTDOWN(curl),
     389             :         NULL,
     390             :         NULL,
     391             :         PHP_MINFO(curl),
     392             :         NO_VERSION_YET,
     393             :         STANDARD_MODULE_PROPERTIES
     394             : };
     395             : /* }}} */
     396             : 
     397             : #ifdef COMPILE_DL_CURL
     398             : ZEND_GET_MODULE (curl)
     399             : #endif
     400             : 
     401             : /* {{{ PHP_INI_BEGIN */
     402             : PHP_INI_BEGIN()
     403             :         PHP_INI_ENTRY("curl.cainfo", "", PHP_INI_SYSTEM, NULL)
     404             : PHP_INI_END()
     405             : /* }}} */
     406             : 
     407             : /* {{{ PHP_MINFO_FUNCTION
     408             :  */
     409         148 : PHP_MINFO_FUNCTION(curl)
     410             : {
     411             :         curl_version_info_data *d;
     412             :         char **p;
     413             :         char str[1024];
     414         148 :         size_t n = 0;
     415             : 
     416         148 :         d = curl_version_info(CURLVERSION_NOW);
     417         148 :         php_info_print_table_start();
     418         148 :         php_info_print_table_row(2, "cURL support",    "enabled");
     419         148 :         php_info_print_table_row(2, "cURL Information", d->version);
     420         148 :         sprintf(str, "%d", d->age);
     421         148 :         php_info_print_table_row(2, "Age", str);
     422             : 
     423             :         /* To update on each new cURL release using src/main.c in cURL sources */
     424         148 :         if (d->features) {
     425             :                 struct feat {
     426             :                         const char *name;
     427             :                         int bitmask;
     428             :                 };
     429             : 
     430             :                 unsigned int i;
     431             : 
     432             :                 static const struct feat feats[] = {
     433             : #if LIBCURL_VERSION_NUM > 0x070a06 /* 7.10.7 */
     434             :                         {"AsynchDNS", CURL_VERSION_ASYNCHDNS},
     435             : #endif
     436             : #if LIBCURL_VERSION_NUM > 0x070a05 /* 7.10.6 */
     437             :                         {"Debug", CURL_VERSION_DEBUG},
     438             :                         {"GSS-Negotiate", CURL_VERSION_GSSNEGOTIATE},
     439             : #endif
     440             : #if LIBCURL_VERSION_NUM > 0x070b02 /* 7.12.0 */
     441             :                         {"IDN", CURL_VERSION_IDN},
     442             : #endif
     443             : #ifdef CURL_VERSION_IPV6
     444             :                         {"IPv6", CURL_VERSION_IPV6},
     445             : #endif
     446             : #if LIBCURL_VERSION_NUM > 0x070b00 /* 7.11.1 */
     447             :                         {"Largefile", CURL_VERSION_LARGEFILE},
     448             : #endif
     449             : #if LIBCURL_VERSION_NUM > 0x070a05 /* 7.10.6 */
     450             :                         {"NTLM", CURL_VERSION_NTLM},
     451             : #endif
     452             : #if LIBCURL_VERSION_NUM > 0x070a07 /* 7.10.8 */
     453             :                         {"SPNEGO", CURL_VERSION_SPNEGO},
     454             : #endif
     455             : #ifdef CURL_VERSION_SSL
     456             :                         {"SSL",  CURL_VERSION_SSL},
     457             : #endif
     458             : #if LIBCURL_VERSION_NUM > 0x070d01 /* 7.13.2 */
     459             :                         {"SSPI",  CURL_VERSION_SSPI},
     460             : #endif
     461             : #ifdef CURL_VERSION_KERBEROS4
     462             :                         {"krb4", CURL_VERSION_KERBEROS4},
     463             : #endif
     464             : #ifdef CURL_VERSION_LIBZ
     465             :                         {"libz", CURL_VERSION_LIBZ},
     466             : #endif
     467             : #if LIBCURL_VERSION_NUM > 0x070f03 /* 7.15.4 */
     468             :                         {"CharConv", CURL_VERSION_CONV},
     469             : #endif
     470             :                         {NULL, 0}
     471             :                 };
     472             : 
     473         148 :                 php_info_print_table_row(1, "Features");
     474        2220 :                 for(i=0; i<sizeof(feats)/sizeof(feats[0]); i++) {
     475        2072 :                         if (feats[i].name) {
     476        1924 :                                 php_info_print_table_row(2, feats[i].name, d->features & feats[i].bitmask ? "Yes" : "No");
     477             :                         }
     478             :                 }
     479             :         }
     480             : 
     481         148 :         n = 0;
     482         148 :         p = (char **) d->protocols;
     483        2072 :         while (*p != NULL) {
     484        1776 :                         n += sprintf(str + n, "%s%s", *p, *(p + 1) != NULL ? ", " : "");
     485        1776 :                         p++;
     486             :         }
     487         148 :         php_info_print_table_row(2, "Protocols", str);
     488             : 
     489         148 :         php_info_print_table_row(2, "Host", d->host);
     490             : 
     491         148 :         if (d->ssl_version) {
     492         148 :                 php_info_print_table_row(2, "SSL Version", d->ssl_version);
     493             :         }
     494             : 
     495         148 :         if (d->libz_version) {
     496         148 :                 php_info_print_table_row(2, "ZLib Version", d->libz_version);
     497             :         }
     498             : 
     499             : #if defined(CURLVERSION_SECOND) && CURLVERSION_NOW >= CURLVERSION_SECOND
     500             :         if (d->ares) {
     501             :                 php_info_print_table_row(2, "ZLib Version", d->ares);
     502             :         }
     503             : #endif
     504             : 
     505             : #if defined(CURLVERSION_THIRD) && CURLVERSION_NOW >= CURLVERSION_THIRD
     506             :         if (d->libidn) {
     507             :                 php_info_print_table_row(2, "libIDN Version", d->libidn);
     508             :         }
     509             : #endif
     510             : 
     511             : #if LIBCURL_VERSION_NUM >= 0x071300
     512             : 
     513         148 :         if (d->iconv_ver_num) {
     514           0 :                 php_info_print_table_row(2, "IconV Version", d->iconv_ver_num);
     515             :         }
     516             : 
     517         148 :         if (d->libssh_version) {
     518         148 :                 php_info_print_table_row(2, "libSSH Version", d->libssh_version);
     519             :         }
     520             : #endif
     521         148 :         php_info_print_table_end();
     522         148 : }
     523             : /* }}} */
     524             : 
     525             : #define REGISTER_CURL_CONSTANT(__c) REGISTER_LONG_CONSTANT(#__c, __c, CONST_CS | CONST_PERSISTENT)
     526             : 
     527             : /* {{{ PHP_MINIT_FUNCTION
     528             :  */
     529       19341 : PHP_MINIT_FUNCTION(curl)
     530             : {
     531       19341 :         le_curl = zend_register_list_destructors_ex(_php_curl_close, NULL, "curl", module_number);
     532       19341 :         le_curl_multi_handle = zend_register_list_destructors_ex(_php_curl_multi_close, NULL, "curl_multi", module_number);
     533             : 
     534       19341 :         REGISTER_INI_ENTRIES();
     535             : 
     536             :         /* See http://curl.haxx.se/lxr/source/docs/libcurl/symbols-in-versions
     537             :            or curl src/docs/libcurl/symbols-in-versions for a (almost) complete list
     538             :            of options and which version they were introduced */
     539             : 
     540             :         /* Constants for curl_setopt() */
     541             : #if LIBCURL_VERSION_NUM > 0x070a07 /* CURLOPT_IPRESOLVE is available since curl 7.10.8 */
     542       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_IPRESOLVE);
     543       19341 :         REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_WHATEVER);
     544       19341 :         REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_V4);
     545       19341 :         REGISTER_CURL_CONSTANT(CURL_IPRESOLVE_V6);
     546             : #endif
     547       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_DNS_USE_GLOBAL_CACHE);
     548       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_DNS_CACHE_TIMEOUT);
     549       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_PORT);
     550       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_FILE);
     551       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_READDATA);
     552       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_INFILE);
     553       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_INFILESIZE);
     554       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_URL);
     555       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_PROXY);
     556       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_VERBOSE);
     557       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_HEADER);
     558       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_HTTPHEADER);
     559       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_NOPROGRESS);
     560       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_PROGRESSFUNCTION);
     561       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_NOBODY);
     562       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_FAILONERROR);
     563       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_UPLOAD);
     564       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_POST);
     565       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_FTPLISTONLY);
     566       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_FTPAPPEND);
     567       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_NETRC);
     568       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_FOLLOWLOCATION);
     569             : #if CURLOPT_FTPASCII != 0
     570             :         REGISTER_CURL_CONSTANT(CURLOPT_FTPASCII);
     571             : #endif
     572       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_PUT);
     573             : #if CURLOPT_MUTE != 0
     574             :         REGISTER_CURL_CONSTANT(CURLOPT_MUTE);
     575             : #endif
     576       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_USERPWD);
     577       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_PROXYUSERPWD);
     578       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_RANGE);
     579       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_TIMEOUT);
     580             : #if LIBCURL_VERSION_NUM > 0x071002
     581       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_TIMEOUT_MS);
     582             : #endif
     583       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_POSTFIELDS);
     584       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_REFERER);
     585       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_USERAGENT);
     586       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_FTPPORT);
     587       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_FTP_USE_EPSV);
     588       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_LOW_SPEED_LIMIT);
     589       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_LOW_SPEED_TIME);
     590       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_RESUME_FROM);
     591       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_COOKIE);
     592       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_COOKIESESSION);
     593       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_AUTOREFERER);
     594       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLCERT);
     595       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLCERTPASSWD);
     596       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_WRITEHEADER);
     597       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_SSL_VERIFYHOST);
     598       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_COOKIEFILE);
     599       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLVERSION);
     600       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_TIMECONDITION);
     601       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_TIMEVALUE);
     602       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_CUSTOMREQUEST);
     603       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_STDERR);
     604       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_TRANSFERTEXT);
     605       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_RETURNTRANSFER);
     606       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_QUOTE);
     607       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_POSTQUOTE);
     608       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_INTERFACE);
     609       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_KRB4LEVEL);
     610       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_HTTPPROXYTUNNEL);
     611       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_FILETIME);
     612       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_WRITEFUNCTION);
     613       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_READFUNCTION);
     614             : #if CURLOPT_PASSWDFUNCTION != 0
     615             :         REGISTER_CURL_CONSTANT(CURLOPT_PASSWDFUNCTION);
     616             : #endif
     617       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_HEADERFUNCTION);
     618       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_MAXREDIRS);
     619       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_MAXCONNECTS);
     620       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_CLOSEPOLICY);
     621       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_FRESH_CONNECT);
     622       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_FORBID_REUSE);
     623       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_RANDOM_FILE);
     624       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_EGDSOCKET);
     625       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_CONNECTTIMEOUT);
     626             : #if LIBCURL_VERSION_NUM > 0x071002
     627       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_CONNECTTIMEOUT_MS);
     628             : #endif
     629       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_SSL_VERIFYPEER);
     630       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_CAINFO);
     631       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_CAPATH);
     632       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_COOKIEJAR);
     633       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_SSL_CIPHER_LIST);
     634       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_BINARYTRANSFER);
     635       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_NOSIGNAL);
     636       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_PROXYTYPE);
     637       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_BUFFERSIZE);
     638       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_HTTPGET);
     639       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_HTTP_VERSION);
     640       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLKEY);
     641       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLKEYTYPE);
     642       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLKEYPASSWD);
     643       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLENGINE);
     644       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLENGINE_DEFAULT);
     645       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_SSLCERTTYPE);
     646       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_CRLF);
     647       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_ENCODING);
     648       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_PROXYPORT);
     649       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_UNRESTRICTED_AUTH);
     650       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_FTP_USE_EPRT);
     651             : #if LIBCURL_VERSION_NUM > 0x070b01 /* CURLOPT_TCP_NODELAY is available since curl 7.11.2 */
     652       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_TCP_NODELAY);
     653             : #endif
     654       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_HTTP200ALIASES);
     655       19341 :         REGISTER_CURL_CONSTANT(CURL_TIMECOND_IFMODSINCE);
     656       19341 :         REGISTER_CURL_CONSTANT(CURL_TIMECOND_IFUNMODSINCE);
     657       19341 :         REGISTER_CURL_CONSTANT(CURL_TIMECOND_LASTMOD);
     658             : 
     659             : #if LIBCURL_VERSION_NUM > 0x070f04 /* CURLOPT_MAX_RECV_SPEED_LARGE & CURLOPT_MAX_SEND_SPEED_LARGE  are available since curl 7.15.5 */
     660       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_MAX_RECV_SPEED_LARGE);
     661       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_MAX_SEND_SPEED_LARGE);
     662             : #endif 
     663             : 
     664             : #if LIBCURL_VERSION_NUM > 0x070a05 /* CURLOPT_HTTPAUTH is available since curl 7.10.6 */
     665       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_HTTPAUTH);
     666             :         /* http authentication options */
     667       19341 :         REGISTER_CURL_CONSTANT(CURLAUTH_BASIC);
     668       19341 :         REGISTER_CURL_CONSTANT(CURLAUTH_DIGEST);
     669       19341 :         REGISTER_CURL_CONSTANT(CURLAUTH_GSSNEGOTIATE);
     670       19341 :         REGISTER_CURL_CONSTANT(CURLAUTH_NTLM);
     671       19341 :         REGISTER_CURL_CONSTANT(CURLAUTH_ANY);
     672       19341 :         REGISTER_CURL_CONSTANT(CURLAUTH_ANYSAFE);
     673             : #endif
     674             : 
     675             : #if LIBCURL_VERSION_NUM > 0x070a06 /* CURLOPT_PROXYAUTH & CURLOPT_FTP_CREATE_MISSING_DIRS are available since curl 7.10.7 */
     676       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_PROXYAUTH);
     677       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_FTP_CREATE_MISSING_DIRS);
     678             : #endif
     679             : 
     680       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_PRIVATE);
     681             : 
     682             :         /* Constants effecting the way CURLOPT_CLOSEPOLICY works */
     683       19341 :         REGISTER_CURL_CONSTANT(CURLCLOSEPOLICY_LEAST_RECENTLY_USED);
     684       19341 :         REGISTER_CURL_CONSTANT(CURLCLOSEPOLICY_LEAST_TRAFFIC);
     685       19341 :         REGISTER_CURL_CONSTANT(CURLCLOSEPOLICY_SLOWEST);
     686       19341 :         REGISTER_CURL_CONSTANT(CURLCLOSEPOLICY_CALLBACK);
     687       19341 :         REGISTER_CURL_CONSTANT(CURLCLOSEPOLICY_OLDEST);
     688             : 
     689             :         /* Info constants */
     690       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_EFFECTIVE_URL);
     691       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_HTTP_CODE);
     692       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_HEADER_SIZE);
     693       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_REQUEST_SIZE);
     694       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_TOTAL_TIME);
     695       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_NAMELOOKUP_TIME);
     696       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_CONNECT_TIME);
     697       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_PRETRANSFER_TIME);
     698       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_SIZE_UPLOAD);
     699       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_SIZE_DOWNLOAD);
     700       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_SPEED_DOWNLOAD);
     701       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_SPEED_UPLOAD);
     702       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_FILETIME);
     703       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_SSL_VERIFYRESULT);
     704       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_LENGTH_DOWNLOAD);
     705       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_LENGTH_UPLOAD);
     706       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_STARTTRANSFER_TIME);
     707       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_TYPE);
     708       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_TIME);
     709       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_COUNT);
     710       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_HEADER_OUT);
     711       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_PRIVATE);
     712             : #if LIBCURL_VERSION_NUM >  0x071301
     713       19341 :         REGISTER_CURL_CONSTANT(CURLINFO_CERTINFO);
     714             : #endif
     715             : #if LIBCURL_VERSION_NUM >= 0x071202
     716       19341 :     REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_URL);
     717             : #endif
     718             : 
     719             : 
     720             :         /* cURL protocol constants (curl_version) */
     721       19341 :         REGISTER_CURL_CONSTANT(CURL_VERSION_IPV6);
     722       19341 :         REGISTER_CURL_CONSTANT(CURL_VERSION_KERBEROS4);
     723       19341 :         REGISTER_CURL_CONSTANT(CURL_VERSION_SSL);
     724       19341 :         REGISTER_CURL_CONSTANT(CURL_VERSION_LIBZ);
     725             : 
     726             :         /* version constants */
     727       19341 :         REGISTER_CURL_CONSTANT(CURLVERSION_NOW);
     728             : 
     729             :         /* Error Constants */
     730       19341 :         REGISTER_CURL_CONSTANT(CURLE_OK);
     731       19341 :         REGISTER_CURL_CONSTANT(CURLE_UNSUPPORTED_PROTOCOL);
     732       19341 :         REGISTER_CURL_CONSTANT(CURLE_FAILED_INIT);
     733       19341 :         REGISTER_CURL_CONSTANT(CURLE_URL_MALFORMAT);
     734       19341 :         REGISTER_CURL_CONSTANT(CURLE_URL_MALFORMAT_USER);
     735       19341 :         REGISTER_CURL_CONSTANT(CURLE_COULDNT_RESOLVE_PROXY);
     736       19341 :         REGISTER_CURL_CONSTANT(CURLE_COULDNT_RESOLVE_HOST);
     737       19341 :         REGISTER_CURL_CONSTANT(CURLE_COULDNT_CONNECT);
     738       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_SERVER_REPLY);
     739       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_ACCESS_DENIED);
     740       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_USER_PASSWORD_INCORRECT);
     741       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_PASS_REPLY);
     742       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_USER_REPLY);
     743       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_PASV_REPLY);
     744       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_WEIRD_227_FORMAT);
     745       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_CANT_GET_HOST);
     746       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_CANT_RECONNECT);
     747       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_SET_BINARY);
     748       19341 :         REGISTER_CURL_CONSTANT(CURLE_PARTIAL_FILE);
     749       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_RETR_FILE);
     750       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_WRITE_ERROR);
     751       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_QUOTE_ERROR);
     752       19341 :         REGISTER_CURL_CONSTANT(CURLE_HTTP_NOT_FOUND);
     753       19341 :         REGISTER_CURL_CONSTANT(CURLE_WRITE_ERROR);
     754       19341 :         REGISTER_CURL_CONSTANT(CURLE_MALFORMAT_USER);
     755       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_STOR_FILE);
     756       19341 :         REGISTER_CURL_CONSTANT(CURLE_READ_ERROR);
     757       19341 :         REGISTER_CURL_CONSTANT(CURLE_OUT_OF_MEMORY);
     758       19341 :         REGISTER_CURL_CONSTANT(CURLE_OPERATION_TIMEOUTED);
     759       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_SET_ASCII);
     760       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_PORT_FAILED);
     761       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_USE_REST);
     762       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_COULDNT_GET_SIZE);
     763       19341 :         REGISTER_CURL_CONSTANT(CURLE_HTTP_RANGE_ERROR);
     764       19341 :         REGISTER_CURL_CONSTANT(CURLE_HTTP_POST_ERROR);
     765       19341 :         REGISTER_CURL_CONSTANT(CURLE_SSL_CONNECT_ERROR);
     766       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_BAD_DOWNLOAD_RESUME);
     767       19341 :         REGISTER_CURL_CONSTANT(CURLE_FILE_COULDNT_READ_FILE);
     768       19341 :         REGISTER_CURL_CONSTANT(CURLE_LDAP_CANNOT_BIND);
     769       19341 :         REGISTER_CURL_CONSTANT(CURLE_LDAP_SEARCH_FAILED);
     770       19341 :         REGISTER_CURL_CONSTANT(CURLE_LIBRARY_NOT_FOUND);
     771       19341 :         REGISTER_CURL_CONSTANT(CURLE_FUNCTION_NOT_FOUND);
     772       19341 :         REGISTER_CURL_CONSTANT(CURLE_ABORTED_BY_CALLBACK);
     773       19341 :         REGISTER_CURL_CONSTANT(CURLE_BAD_FUNCTION_ARGUMENT);
     774       19341 :         REGISTER_CURL_CONSTANT(CURLE_BAD_CALLING_ORDER);
     775       19341 :         REGISTER_CURL_CONSTANT(CURLE_HTTP_PORT_FAILED);
     776       19341 :         REGISTER_CURL_CONSTANT(CURLE_BAD_PASSWORD_ENTERED);
     777       19341 :         REGISTER_CURL_CONSTANT(CURLE_TOO_MANY_REDIRECTS);
     778       19341 :         REGISTER_CURL_CONSTANT(CURLE_UNKNOWN_TELNET_OPTION);
     779       19341 :         REGISTER_CURL_CONSTANT(CURLE_TELNET_OPTION_SYNTAX);
     780       19341 :         REGISTER_CURL_CONSTANT(CURLE_OBSOLETE);
     781       19341 :         REGISTER_CURL_CONSTANT(CURLE_SSL_PEER_CERTIFICATE);
     782       19341 :         REGISTER_CURL_CONSTANT(CURLE_GOT_NOTHING);
     783       19341 :         REGISTER_CURL_CONSTANT(CURLE_SSL_ENGINE_NOTFOUND);
     784       19341 :         REGISTER_CURL_CONSTANT(CURLE_SSL_ENGINE_SETFAILED);
     785       19341 :         REGISTER_CURL_CONSTANT(CURLE_SEND_ERROR);
     786       19341 :         REGISTER_CURL_CONSTANT(CURLE_RECV_ERROR);
     787       19341 :         REGISTER_CURL_CONSTANT(CURLE_SHARE_IN_USE);
     788       19341 :         REGISTER_CURL_CONSTANT(CURLE_SSL_CERTPROBLEM);
     789       19341 :         REGISTER_CURL_CONSTANT(CURLE_SSL_CIPHER);
     790       19341 :         REGISTER_CURL_CONSTANT(CURLE_SSL_CACERT);
     791       19341 :         REGISTER_CURL_CONSTANT(CURLE_BAD_CONTENT_ENCODING);
     792             : #if LIBCURL_VERSION_NUM >= 0x070a08
     793       19341 :         REGISTER_CURL_CONSTANT(CURLE_LDAP_INVALID_URL);
     794       19341 :         REGISTER_CURL_CONSTANT(CURLE_FILESIZE_EXCEEDED);
     795             : #endif
     796             : #if LIBCURL_VERSION_NUM >= 0x070b00
     797       19341 :         REGISTER_CURL_CONSTANT(CURLE_FTP_SSL_FAILED);
     798             : #endif
     799       19341 :         REGISTER_CURL_CONSTANT(CURLPROXY_HTTP);
     800       19341 :         REGISTER_CURL_CONSTANT(CURLPROXY_SOCKS4);
     801       19341 :         REGISTER_CURL_CONSTANT(CURLPROXY_SOCKS5);
     802             : 
     803       19341 :         REGISTER_CURL_CONSTANT(CURL_NETRC_OPTIONAL);
     804       19341 :         REGISTER_CURL_CONSTANT(CURL_NETRC_IGNORED);
     805       19341 :         REGISTER_CURL_CONSTANT(CURL_NETRC_REQUIRED);
     806             : 
     807       19341 :         REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_NONE);
     808       19341 :         REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_1_0);
     809       19341 :         REGISTER_CURL_CONSTANT(CURL_HTTP_VERSION_1_1);
     810             : 
     811       19341 :         REGISTER_CURL_CONSTANT(CURLM_CALL_MULTI_PERFORM);
     812       19341 :         REGISTER_CURL_CONSTANT(CURLM_OK);
     813       19341 :         REGISTER_CURL_CONSTANT(CURLM_BAD_HANDLE);
     814       19341 :         REGISTER_CURL_CONSTANT(CURLM_BAD_EASY_HANDLE);
     815       19341 :         REGISTER_CURL_CONSTANT(CURLM_OUT_OF_MEMORY);
     816       19341 :         REGISTER_CURL_CONSTANT(CURLM_INTERNAL_ERROR);
     817             : 
     818       19341 :         REGISTER_CURL_CONSTANT(CURLMSG_DONE);
     819             : 
     820             : #if LIBCURL_VERSION_NUM >= 0x070c02
     821       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_FTPSSLAUTH);
     822       19341 :         REGISTER_CURL_CONSTANT(CURLFTPAUTH_DEFAULT);
     823       19341 :         REGISTER_CURL_CONSTANT(CURLFTPAUTH_SSL);
     824       19341 :         REGISTER_CURL_CONSTANT(CURLFTPAUTH_TLS);
     825             : #endif
     826             : 
     827             : #if LIBCURL_VERSION_NUM >  0x070b00
     828       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_FTP_SSL);
     829       19341 :         REGISTER_CURL_CONSTANT(CURLFTPSSL_NONE);
     830       19341 :         REGISTER_CURL_CONSTANT(CURLFTPSSL_TRY);
     831       19341 :         REGISTER_CURL_CONSTANT(CURLFTPSSL_CONTROL);
     832       19341 :         REGISTER_CURL_CONSTANT(CURLFTPSSL_ALL);
     833             : #endif
     834             : 
     835             : #if LIBCURL_VERSION_NUM > 0x071301
     836       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_CERTINFO);
     837       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_POSTREDIR);
     838             : #endif
     839             : 
     840             : /* SSH support works in 7.19.0+ using libssh2 */
     841             : #if LIBCURL_VERSION_NUM >= 0x071300
     842       19341 :         REGISTER_CURL_CONSTANT(CURLSSH_AUTH_NONE);
     843       19341 :         REGISTER_CURL_CONSTANT(CURLSSH_AUTH_PUBLICKEY);
     844       19341 :         REGISTER_CURL_CONSTANT(CURLSSH_AUTH_PASSWORD);
     845       19341 :         REGISTER_CURL_CONSTANT(CURLSSH_AUTH_HOST);
     846       19341 :         REGISTER_CURL_CONSTANT(CURLSSH_AUTH_KEYBOARD);
     847       19341 :         REGISTER_CURL_CONSTANT(CURLSSH_AUTH_DEFAULT);
     848       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_SSH_AUTH_TYPES);
     849       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_KEYPASSWD);
     850       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_SSH_PUBLIC_KEYFILE);
     851       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_SSH_PRIVATE_KEYFILE);
     852       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_SSH_HOST_PUBLIC_KEY_MD5);
     853       19341 :         REGISTER_CURL_CONSTANT(CURLE_SSH);
     854             : #endif
     855             : 
     856             : #if LIBCURL_VERSION_NUM >= 0x071304
     857       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_REDIR_PROTOCOLS);
     858       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_PROTOCOLS);
     859       19341 :         REGISTER_CURL_CONSTANT(CURLPROTO_HTTP);
     860       19341 :         REGISTER_CURL_CONSTANT(CURLPROTO_HTTPS);
     861       19341 :         REGISTER_CURL_CONSTANT(CURLPROTO_FTP);
     862       19341 :         REGISTER_CURL_CONSTANT(CURLPROTO_FTPS);
     863       19341 :         REGISTER_CURL_CONSTANT(CURLPROTO_SCP);
     864       19341 :         REGISTER_CURL_CONSTANT(CURLPROTO_SFTP);
     865       19341 :         REGISTER_CURL_CONSTANT(CURLPROTO_TELNET);
     866       19341 :         REGISTER_CURL_CONSTANT(CURLPROTO_LDAP);
     867       19341 :         REGISTER_CURL_CONSTANT(CURLPROTO_LDAPS);
     868       19341 :         REGISTER_CURL_CONSTANT(CURLPROTO_DICT);
     869       19341 :         REGISTER_CURL_CONSTANT(CURLPROTO_FILE);
     870       19341 :         REGISTER_CURL_CONSTANT(CURLPROTO_TFTP);
     871       19341 :         REGISTER_CURL_CONSTANT(CURLPROTO_ALL);
     872             : #endif
     873             : 
     874             : #if LIBCURL_VERSION_NUM >= 0x070f01
     875       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_FTP_FILEMETHOD);
     876       19341 :         REGISTER_CURL_CONSTANT(CURLOPT_FTP_SKIP_PASV_IP);
     877             : #endif
     878             : 
     879             : #if LIBCURL_VERSION_NUM >= 0x071001
     880       19341 :         REGISTER_CURL_CONSTANT(CURLFTPMETHOD_MULTICWD);
     881       19341 :         REGISTER_CURL_CONSTANT(CURLFTPMETHOD_NOCWD);
     882       19341 :         REGISTER_CURL_CONSTANT(CURLFTPMETHOD_SINGLECWD);
     883             : #endif
     884             : 
     885             : #ifdef PHP_CURL_NEED_OPENSSL_TSL
     886             :         if (!CRYPTO_get_id_callback()) {
     887             :                 int i, c = CRYPTO_num_locks();
     888             : 
     889             :                 php_curl_openssl_tsl = malloc(c * sizeof(MUTEX_T));
     890             :                 if (!php_curl_openssl_tsl) {
     891             :                         return FAILURE;
     892             :                 }
     893             : 
     894             :                 for (i = 0; i < c; ++i) {
     895             :                         php_curl_openssl_tsl[i] = tsrm_mutex_alloc();
     896             :                 }
     897             : 
     898             :                 CRYPTO_set_id_callback(php_curl_ssl_id);
     899             :                 CRYPTO_set_locking_callback(php_curl_ssl_lock);
     900             :         }
     901             : #endif
     902             : #ifdef PHP_CURL_NEED_GNUTLS_TSL
     903             :         gcry_control(GCRYCTL_SET_THREAD_CBS, &php_curl_gnutls_tsl);
     904             : #endif
     905             : 
     906       19341 :         if (curl_global_init(CURL_GLOBAL_SSL) != CURLE_OK) {
     907           0 :                 return FAILURE;
     908             :         }
     909             : 
     910             : #ifdef PHP_CURL_URL_WRAPPERS
     911             : # if HAVE_CURL_VERSION_INFO
     912             :         {
     913       19341 :                 curl_version_info_data *info = curl_version_info(CURLVERSION_NOW);
     914       19341 :                 char **p = (char **)info->protocols;
     915             : 
     916      270774 :                 while (*p != NULL) {
     917             :                         /* Do not enable cURL "file" protocol and make sure cURL is always used when --with-curlwrappers is enabled */
     918      232092 :                         if (strncasecmp(*p, "file", sizeof("file")-1) != 0) {
     919      212751 :                                 php_unregister_url_stream_wrapper(*p TSRMLS_CC);
     920      212751 :                                 php_register_url_stream_wrapper(*p, &php_curl_wrapper TSRMLS_CC);
     921             :                         }
     922      232092 :                         (void) *p++;
     923             :                 }
     924             :         }
     925             : # else
     926             :         php_unregister_url_stream_wrapper("http");
     927             :         php_register_url_stream_wrapper("http", &php_curl_wrapper TSRMLS_CC);
     928             :         php_unregister_url_stream_wrapper("https");
     929             :         php_register_url_stream_wrapper("https", &php_curl_wrapper TSRMLS_CC);
     930             :         php_unregister_url_stream_wrapper("ftp");
     931             :         php_register_url_stream_wrapper("ftp", &php_curl_wrapper TSRMLS_CC);
     932             :         php_unregister_url_stream_wrapper("ftps");
     933             :         php_register_url_stream_wrapper("ftps", &php_curl_wrapper TSRMLS_CC);
     934             :         php_unregister_url_stream_wrapper("ldap");
     935             :         php_register_url_stream_wrapper("ldap", &php_curl_wrapper TSRMLS_CC);
     936             : # endif
     937             : #endif
     938             : 
     939       19341 :         return SUCCESS;
     940             : }
     941             : /* }}} */
     942             : 
     943             : /* {{{ PHP_MSHUTDOWN_FUNCTION
     944             :  */
     945       19376 : PHP_MSHUTDOWN_FUNCTION(curl)
     946             : {
     947             : #ifdef PHP_CURL_URL_WRAPPERS
     948       19376 :         php_unregister_url_stream_wrapper("http" TSRMLS_CC);
     949       19376 :         php_unregister_url_stream_wrapper("https" TSRMLS_CC);
     950       19376 :         php_unregister_url_stream_wrapper("ftp" TSRMLS_CC);
     951       19376 :         php_unregister_url_stream_wrapper("ldap" TSRMLS_CC);
     952             : #endif
     953       19376 :         curl_global_cleanup();
     954             : #ifdef PHP_CURL_NEED_OPENSSL_TSL
     955             :         if (php_curl_openssl_tsl) {
     956             :                 int i, c = CRYPTO_num_locks();
     957             : 
     958             :                 CRYPTO_set_id_callback(NULL);
     959             :                 CRYPTO_set_locking_callback(NULL);
     960             : 
     961             :                 for (i = 0; i < c; ++i) {
     962             :                         tsrm_mutex_free(php_curl_openssl_tsl[i]);
     963             :                 }
     964             : 
     965             :                 free(php_curl_openssl_tsl);
     966             :                 php_curl_openssl_tsl = NULL;
     967             :         }
     968             : #endif
     969       19376 :         UNREGISTER_INI_ENTRIES();
     970       19376 :         return SUCCESS;
     971             : }
     972             : /* }}} */
     973             : 
     974             : /* {{{ curl_write_nothing
     975             :  * Used as a work around. See _php_curl_close_ex
     976             :  */
     977           0 : static size_t curl_write_nothing(char *data, size_t size, size_t nmemb, void *ctx) 
     978             : {
     979           0 :         return size * nmemb;
     980             : }
     981             : /* }}} */
     982             : 
     983             : /* {{{ curl_write
     984             :  */
     985          73 : static size_t curl_write(char *data, size_t size, size_t nmemb, void *ctx)
     986             : {
     987          73 :         php_curl       *ch     = (php_curl *) ctx;
     988          73 :         php_curl_write *t      = ch->handlers->write;
     989          73 :         size_t          length = size * nmemb;
     990             :         TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
     991             : 
     992             : #if PHP_CURL_DEBUG
     993             :         fprintf(stderr, "curl_write() called\n");
     994             :         fprintf(stderr, "data = %s, size = %d, nmemb = %d, ctx = %x\n", data, size, nmemb, ctx);
     995             : #endif
     996             : 
     997          73 :         switch (t->method) {
     998             :                 case PHP_CURL_STDOUT:
     999          14 :                         PHPWRITE(data, length);
    1000          14 :                         break;
    1001             :                 case PHP_CURL_FILE:
    1002           2 :                         return fwrite(data, size, nmemb, t->fp);
    1003             :                 case PHP_CURL_RETURN:
    1004          55 :                         if (length > 0) {
    1005          55 :                                 smart_str_appendl(&t->buf, data, (int) length);
    1006             :                         }
    1007          55 :                         break;
    1008             :                 case PHP_CURL_USER: {
    1009             :                         zval **argv[2];
    1010           2 :                         zval *retval_ptr = NULL;
    1011           2 :                         zval *handle = NULL;
    1012           2 :                         zval *zdata = NULL;
    1013             :                         int   error;
    1014             :                         zend_fcall_info fci;
    1015             : 
    1016           2 :                         MAKE_STD_ZVAL(handle);
    1017           2 :                         ZVAL_RESOURCE(handle, ch->id);
    1018           2 :                         zend_list_addref(ch->id);
    1019           2 :                         argv[0] = &handle;
    1020             : 
    1021           2 :                         MAKE_STD_ZVAL(zdata);
    1022           2 :                         ZVAL_STRINGL(zdata, data, length, 1);
    1023           2 :                         argv[1] = &zdata;
    1024             : 
    1025           2 :                         fci.size = sizeof(fci);
    1026           2 :                         fci.function_table = EG(function_table);
    1027           2 :                         fci.object_ptr = NULL;
    1028           2 :                         fci.function_name = t->func_name;
    1029           2 :                         fci.retval_ptr_ptr = &retval_ptr;
    1030           2 :                         fci.param_count = 2;
    1031           2 :                         fci.params = argv;
    1032           2 :                         fci.no_separation = 0;
    1033           2 :                         fci.symbol_table = NULL;
    1034             : 
    1035           2 :                         ch->in_callback = 1;
    1036           2 :                         error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
    1037           2 :                         ch->in_callback = 0;
    1038           2 :                         if (error == FAILURE) {
    1039           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not call the CURLOPT_WRITEFUNCTION");
    1040           0 :                                 length = -1;
    1041           2 :                         } else if (retval_ptr) {
    1042           2 :                                 if (Z_TYPE_P(retval_ptr) != IS_LONG) {
    1043           0 :                                         convert_to_long_ex(&retval_ptr);
    1044             :                                 }
    1045           2 :                                 length = Z_LVAL_P(retval_ptr);
    1046           2 :                                 zval_ptr_dtor(&retval_ptr);
    1047             :                         }
    1048             : 
    1049           2 :                         zval_ptr_dtor(argv[0]);
    1050           2 :                         zval_ptr_dtor(argv[1]);
    1051             :                         break;
    1052             :                 }
    1053             :         }
    1054             : 
    1055          71 :         return length;
    1056             : }
    1057             : /* }}} */
    1058             : 
    1059             : /* {{{ curl_progress
    1060             :  */
    1061           7 : static size_t curl_progress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
    1062             : {
    1063           7 :         php_curl       *ch = (php_curl *) clientp;
    1064           7 :         php_curl_progress  *t  = ch->handlers->progress;
    1065           7 :         int             length = -1;
    1066           7 :         size_t  rval = 0;
    1067             : 
    1068             : #if PHP_CURL_DEBUG
    1069             :         fprintf(stderr, "curl_progress() called\n");
    1070             :         fprintf(stderr, "clientp = %x, dltotal = %f, dlnow = %f, ultotal = %f, ulnow = %f\n", clientp, dltotal, dlnow, ultotal, ulnow);
    1071             : #endif
    1072             : 
    1073           7 :         switch (t->method) {
    1074             :                 case PHP_CURL_USER: {
    1075             :                         zval **argv[4];
    1076           7 :                         zval  *zdltotal = NULL;
    1077           7 :                         zval  *zdlnow = NULL;
    1078           7 :                         zval  *zultotal = NULL;
    1079           7 :                         zval  *zulnow = NULL;
    1080             :                         zval  *retval_ptr;
    1081             :                         int   error;
    1082             :                         zend_fcall_info fci;
    1083             :                         TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
    1084             : 
    1085           7 :                         MAKE_STD_ZVAL(zdltotal);
    1086           7 :                         MAKE_STD_ZVAL(zdlnow);
    1087           7 :                         MAKE_STD_ZVAL(zultotal);
    1088           7 :                         MAKE_STD_ZVAL(zulnow);
    1089             : 
    1090           7 :                         ZVAL_LONG(zdltotal, (long) dltotal);
    1091           7 :                         ZVAL_LONG(zdlnow, (long) dlnow);
    1092           7 :                         ZVAL_LONG(zultotal, (long) ultotal);
    1093           7 :                         ZVAL_LONG(zulnow, (long) ulnow);
    1094             : 
    1095           7 :                         argv[0] = &zdltotal;
    1096           7 :                         argv[1] = &zdlnow;
    1097           7 :                         argv[2] = &zultotal;
    1098           7 :                         argv[3] = &zulnow;
    1099             : 
    1100           7 :                         fci.size = sizeof(fci);
    1101           7 :                         fci.function_table = EG(function_table);
    1102           7 :                         fci.function_name = t->func_name;
    1103           7 :                         fci.object_ptr = NULL;
    1104           7 :                         fci.retval_ptr_ptr = &retval_ptr;
    1105           7 :                         fci.param_count = 4;
    1106           7 :                         fci.params = argv;
    1107           7 :                         fci.no_separation = 0;
    1108           7 :                         fci.symbol_table = NULL;
    1109             : 
    1110           7 :                         ch->in_callback = 1;
    1111           7 :                         error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
    1112           7 :                         ch->in_callback = 0;
    1113           7 :                         if (error == FAILURE) {
    1114           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot call the CURLOPT_PROGRESSFUNCTION");
    1115           0 :                                 length = -1;
    1116           7 :                         } else if (retval_ptr) {
    1117           7 :                                 if (Z_TYPE_P(retval_ptr) != IS_LONG) {
    1118          28 :                                         convert_to_long_ex(&retval_ptr);
    1119             :                                 }
    1120           7 :                                 if (0 != Z_LVAL_P(retval_ptr)) {
    1121           0 :                                         rval = 1;
    1122             :                                 }
    1123           7 :                                 zval_ptr_dtor(&retval_ptr);
    1124             :                         }
    1125           7 :                         zval_ptr_dtor(argv[0]);
    1126           7 :                         zval_ptr_dtor(argv[1]);
    1127           7 :                         zval_ptr_dtor(argv[2]);
    1128           7 :                         zval_ptr_dtor(argv[3]);
    1129             :                         break;
    1130             :                 }
    1131             :         }
    1132           7 :         return rval;
    1133             : }
    1134             : /* }}} */
    1135             : 
    1136             : /* {{{ curl_read
    1137             :  */
    1138           3 : static size_t curl_read(char *data, size_t size, size_t nmemb, void *ctx)
    1139             : {
    1140           3 :         php_curl       *ch = (php_curl *) ctx;
    1141           3 :         php_curl_read  *t  = ch->handlers->read;
    1142           3 :         int             length = 0;
    1143             : 
    1144           3 :         switch (t->method) {
    1145             :                 case PHP_CURL_DIRECT:
    1146           1 :                         if (t->fp) {
    1147           1 :                                 length = fread(data, size, nmemb, t->fp);
    1148             :                         }
    1149           1 :                         break;
    1150             :                 case PHP_CURL_USER: {
    1151             :                         zval **argv[3];
    1152           2 :                         zval  *handle = NULL;
    1153           2 :                         zval  *zfd = NULL;
    1154           2 :                         zval  *zlength = NULL;
    1155             :                         zval  *retval_ptr;
    1156             :                         int   error;
    1157             :                         zend_fcall_info fci;
    1158             :                         TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
    1159             : 
    1160           2 :                         MAKE_STD_ZVAL(handle);
    1161           2 :                         MAKE_STD_ZVAL(zfd);
    1162           2 :                         MAKE_STD_ZVAL(zlength);
    1163             : 
    1164           2 :                         ZVAL_RESOURCE(handle, ch->id);
    1165           2 :                         zend_list_addref(ch->id);
    1166           2 :                         ZVAL_RESOURCE(zfd, t->fd);
    1167           2 :                         zend_list_addref(t->fd);
    1168           2 :                         ZVAL_LONG(zlength, (int) size * nmemb);
    1169             : 
    1170           2 :                         argv[0] = &handle;
    1171           2 :                         argv[1] = &zfd;
    1172           2 :                         argv[2] = &zlength;
    1173             : 
    1174           2 :                         fci.size = sizeof(fci);
    1175           2 :                         fci.function_table = EG(function_table);
    1176           2 :                         fci.function_name = t->func_name;
    1177           2 :                         fci.object_ptr = NULL;
    1178           2 :                         fci.retval_ptr_ptr = &retval_ptr;
    1179           2 :                         fci.param_count = 3;
    1180           2 :                         fci.params = argv;
    1181           2 :                         fci.no_separation = 0;
    1182           2 :                         fci.symbol_table = NULL;
    1183             : 
    1184           2 :                         ch->in_callback = 1;
    1185           2 :                         error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
    1186           2 :                         ch->in_callback = 0;
    1187           2 :                         if (error == FAILURE) {
    1188           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot call the CURLOPT_READFUNCTION");
    1189             : #if LIBCURL_VERSION_NUM >= 0x070c01 /* 7.12.1 */
    1190           0 :                                 length = CURL_READFUNC_ABORT;
    1191             : #endif
    1192           2 :                         } else if (retval_ptr) {
    1193           2 :                                 if (Z_TYPE_P(retval_ptr) == IS_STRING) {
    1194           2 :                                         length = MIN((int) (size * nmemb), Z_STRLEN_P(retval_ptr));
    1195           2 :                                         memcpy(data, Z_STRVAL_P(retval_ptr), length);
    1196             :                                 }
    1197           2 :                                 zval_ptr_dtor(&retval_ptr);
    1198             :                         }
    1199             : 
    1200           2 :                         zval_ptr_dtor(argv[0]);
    1201           2 :                         zval_ptr_dtor(argv[1]);
    1202           2 :                         zval_ptr_dtor(argv[2]);
    1203             :                         break;
    1204             :                 }
    1205             :         }
    1206             : 
    1207           3 :         return length;
    1208             : }
    1209             : /* }}} */
    1210             : 
    1211             : /* {{{ curl_write_header
    1212             :  */
    1213         432 : static size_t curl_write_header(char *data, size_t size, size_t nmemb, void *ctx)
    1214             : {
    1215         432 :         php_curl       *ch  = (php_curl *) ctx;
    1216         432 :         php_curl_write *t   = ch->handlers->write_header;
    1217         432 :         size_t          length = size * nmemb;
    1218             :         TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
    1219             : 
    1220         432 :         switch (t->method) {
    1221             :                 case PHP_CURL_STDOUT:
    1222             :                         /* Handle special case write when we're returning the entire transfer
    1223             :                          */
    1224           0 :                         if (ch->handlers->write->method == PHP_CURL_RETURN && length > 0) {
    1225           0 :                                 smart_str_appendl(&ch->handlers->write->buf, data, (int) length);
    1226             :                         } else {
    1227           0 :                                 PHPWRITE(data, length);
    1228             :                         }
    1229           0 :                         break;
    1230             :                 case PHP_CURL_FILE:
    1231           0 :                         return fwrite(data, size, nmemb, t->fp);
    1232             :                 case PHP_CURL_USER: {
    1233             :                         zval **argv[2];
    1234           1 :                         zval  *handle = NULL;
    1235           1 :                         zval  *zdata = NULL;
    1236             :                         zval  *retval_ptr;
    1237             :                         int   error;
    1238             :                         zend_fcall_info fci;
    1239             : 
    1240           1 :                         MAKE_STD_ZVAL(handle);
    1241           1 :                         MAKE_STD_ZVAL(zdata);
    1242             : 
    1243           1 :                         ZVAL_RESOURCE(handle, ch->id);
    1244           1 :                         zend_list_addref(ch->id);
    1245           1 :                         ZVAL_STRINGL(zdata, data, length, 1);
    1246             : 
    1247           1 :                         argv[0] = &handle;
    1248           1 :                         argv[1] = &zdata;
    1249             : 
    1250           1 :                         fci.size = sizeof(fci);
    1251           1 :                         fci.function_table = EG(function_table);
    1252           1 :                         fci.function_name = t->func_name;
    1253           1 :                         fci.symbol_table = NULL;
    1254           1 :                         fci.object_ptr = NULL;
    1255           1 :                         fci.retval_ptr_ptr = &retval_ptr;
    1256           1 :                         fci.param_count = 2;
    1257           1 :                         fci.params = argv;
    1258           1 :                         fci.no_separation = 0;
    1259             : 
    1260           1 :                         ch->in_callback = 1;
    1261           1 :                         error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
    1262           1 :                         ch->in_callback = 0;
    1263           1 :                         if (error == FAILURE) {
    1264           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not call the CURLOPT_HEADERFUNCTION");
    1265           0 :                                 length = -1;
    1266           1 :                         } else if (retval_ptr) {
    1267           1 :                                 if (Z_TYPE_P(retval_ptr) != IS_LONG) {
    1268           4 :                                         convert_to_long_ex(&retval_ptr);
    1269             :                                 }
    1270           1 :                                 length = Z_LVAL_P(retval_ptr);
    1271           1 :                                 zval_ptr_dtor(&retval_ptr);
    1272             :                         }
    1273           1 :                         zval_ptr_dtor(argv[0]);
    1274           1 :                         zval_ptr_dtor(argv[1]);
    1275           1 :                         break;
    1276             :                 }
    1277             : 
    1278             :                 case PHP_CURL_IGNORE:
    1279         431 :                         return length;
    1280             : 
    1281             :                 default:
    1282           0 :                         return -1;
    1283             :         }
    1284             : 
    1285           1 :         return length;
    1286             : }
    1287             : /* }}} */
    1288             : 
    1289           0 : static int curl_debug(CURL *cp, curl_infotype type, char *buf, size_t buf_len, void *ctx) /* {{{ */
    1290             : {
    1291           0 :         php_curl    *ch   = (php_curl *) ctx;
    1292             : 
    1293           0 :         if (type == CURLINFO_HEADER_OUT) {
    1294           0 :                 if (ch->header.str_len) {
    1295           0 :                         efree(ch->header.str);
    1296             :                 }
    1297           0 :                 if (buf_len > 0) {
    1298           0 :                         ch->header.str = estrndup(buf, buf_len);
    1299           0 :                         ch->header.str_len = buf_len;
    1300             :                 }
    1301             :         }
    1302             : 
    1303           0 :         return 0;
    1304             : }
    1305             : /* }}} */
    1306             : 
    1307             : #if CURLOPT_PASSWDFUNCTION != 0
    1308             : /* {{{ curl_passwd
    1309             :  */
    1310             : static size_t curl_passwd(void *ctx, char *prompt, char *buf, int buflen)
    1311             : {
    1312             :         php_curl    *ch   = (php_curl *) ctx;
    1313             :         zval        *func = ch->handlers->passwd;
    1314             :         zval        *argv[3];
    1315             :         zval        *retval = NULL;
    1316             :         int          error;
    1317             :         int          ret = -1;
    1318             :         TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);
    1319             : 
    1320             :         MAKE_STD_ZVAL(argv[0]);
    1321             :         MAKE_STD_ZVAL(argv[1]);
    1322             :         MAKE_STD_ZVAL(argv[2]);
    1323             : 
    1324             :         ZVAL_RESOURCE(argv[0], ch->id);
    1325             :         zend_list_addref(ch->id);
    1326             :         ZVAL_STRING(argv[1], prompt, 1);
    1327             :         ZVAL_LONG(argv[2], buflen);
    1328             : 
    1329             :         error = call_user_function(EG(function_table), NULL, func, retval, 2, argv TSRMLS_CC);
    1330             :         if (error == FAILURE) {
    1331             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not call the CURLOPT_PASSWDFUNCTION");
    1332             :         } else if (Z_TYPE_P(retval) == IS_STRING) {
    1333             :                 if (Z_STRLEN_P(retval) > buflen) {
    1334             :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Returned password is too long for libcurl to handle");
    1335             :                 } else {
    1336             :                         strlcpy(buf, Z_STRVAL_P(retval), Z_STRLEN_P(retval));
    1337             :                 }
    1338             :         } else {
    1339             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "User handler '%s' did not return a string", Z_STRVAL_P(func));
    1340             :         }
    1341             : 
    1342             :         zval_ptr_dtor(&argv[0]);
    1343             :         zval_ptr_dtor(&argv[1]);
    1344             :         zval_ptr_dtor(&argv[2]);
    1345             :         zval_ptr_dtor(&retval);
    1346             : 
    1347             :         return ret;
    1348             : }
    1349             : /* }}} */
    1350             : #endif
    1351             : 
    1352             : /* {{{ curl_free_string
    1353             :  */
    1354           1 : static void curl_free_string(void **string)
    1355             : {
    1356           1 :         efree(*string);
    1357           1 : }
    1358             : /* }}} */
    1359             : 
    1360             : /* {{{ curl_free_post
    1361             :  */
    1362           6 : static void curl_free_post(void **post)
    1363             : {
    1364           6 :         curl_formfree((struct HttpPost *) *post);
    1365           6 : }
    1366             : /* }}} */
    1367             : 
    1368             : /* {{{ curl_free_slist
    1369             :  */
    1370           3 : static void curl_free_slist(void **slist)
    1371             : {
    1372           3 :         curl_slist_free_all((struct curl_slist *) *slist);
    1373           3 : }
    1374             : /* }}} */
    1375             : 
    1376             : /* {{{ proto array curl_version([int version])
    1377             :    Return cURL version information. */
    1378          30 : PHP_FUNCTION(curl_version)
    1379             : {
    1380             :         curl_version_info_data *d;
    1381          30 :         long uversion = CURLVERSION_NOW;
    1382             : 
    1383          30 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &uversion) == FAILURE) {
    1384           8 :                 return;
    1385             :         }
    1386             : 
    1387          22 :         d = curl_version_info(uversion);
    1388          22 :         if (d == NULL) {
    1389           0 :                 RETURN_FALSE;
    1390             :         }
    1391             : 
    1392          22 :         array_init(return_value);
    1393             : 
    1394          22 :         CAAL("version_number", d->version_num);
    1395          22 :         CAAL("age", d->age);
    1396          22 :         CAAL("features", d->features);
    1397          22 :         CAAL("ssl_version_number", d->ssl_version_num);
    1398          22 :         CAAS("version", d->version);
    1399          22 :         CAAS("host", d->host);
    1400          22 :         CAAS("ssl_version", d->ssl_version);
    1401          22 :         CAAS("libz_version", d->libz_version);
    1402             :         /* Add an array of protocols */
    1403             :         {
    1404          22 :                 char **p = (char **) d->protocols;
    1405          22 :                 zval  *protocol_list = NULL;
    1406             : 
    1407          22 :                 MAKE_STD_ZVAL(protocol_list);
    1408          22 :                 array_init(protocol_list);
    1409             : 
    1410         308 :                 while (*p != NULL) {
    1411         264 :                         add_next_index_string(protocol_list, *p, 1);
    1412         264 :                         p++;
    1413             :                 }
    1414          22 :                 CAAZ("protocols", protocol_list);
    1415             :         }
    1416             : }
    1417             : /* }}} */
    1418             : 
    1419             : /* {{{ alloc_curl_handle
    1420             :  */
    1421         101 : static void alloc_curl_handle(php_curl **ch)
    1422             : {
    1423         101 :         *ch                           = emalloc(sizeof(php_curl));
    1424         101 :         (*ch)->to_free                = ecalloc(1, sizeof(struct _php_curl_free));
    1425         101 :         (*ch)->handlers               = ecalloc(1, sizeof(php_curl_handlers));
    1426         101 :         (*ch)->handlers->write        = ecalloc(1, sizeof(php_curl_write));
    1427         101 :         (*ch)->handlers->write_header = ecalloc(1, sizeof(php_curl_write));
    1428         101 :         (*ch)->handlers->read         = ecalloc(1, sizeof(php_curl_read));
    1429         101 :         (*ch)->handlers->progress     = ecalloc(1, sizeof(php_curl_progress));
    1430             : 
    1431         101 :         (*ch)->in_callback = 0;
    1432         101 :         (*ch)->header.str_len = 0;
    1433             : 
    1434         101 :         memset(&(*ch)->err, 0, sizeof((*ch)->err));
    1435         101 :         (*ch)->handlers->write->stream = NULL;
    1436         101 :         (*ch)->handlers->write_header->stream = NULL;
    1437         101 :         (*ch)->handlers->read->stream = NULL;
    1438             : 
    1439         101 :         zend_llist_init(&(*ch)->to_free->str,   sizeof(char *),            (llist_dtor_func_t) curl_free_string, 0);
    1440         101 :         zend_llist_init(&(*ch)->to_free->slist, sizeof(struct curl_slist), (llist_dtor_func_t) curl_free_slist,  0);
    1441         101 :         zend_llist_init(&(*ch)->to_free->post,  sizeof(struct HttpPost),   (llist_dtor_func_t) curl_free_post,   0);
    1442         101 : }
    1443             : /* }}} */
    1444             : 
    1445             : #if LIBCURL_VERSION_NUM > 0x071301
    1446             : /* {{{ split_certinfo
    1447             :  */
    1448           0 : static void split_certinfo(char *string, zval *hash)
    1449             : {
    1450           0 :         char *org = estrdup(string);
    1451           0 :         char *s = org;
    1452             :         char *split;
    1453             : 
    1454           0 :         if(org) {
    1455             :         do {
    1456             :                         char *key;
    1457             :                         char *val;
    1458             :                         char *tmp;
    1459             : 
    1460           0 :             split = strstr(s, "; ");
    1461           0 :             if(split)
    1462           0 :                 *split = '\0';
    1463             :                         
    1464           0 :                         key = s;
    1465           0 :                         tmp = memchr(key, '=', 64);
    1466           0 :                         if(tmp) {
    1467           0 :                                 *tmp = '\0';
    1468           0 :                                 val = tmp+1;
    1469           0 :                                 add_assoc_string(hash, key, val, 1);
    1470             :                         }
    1471           0 :                         s = split+2;
    1472           0 :                 } while(split);
    1473           0 :                 efree(org);
    1474             :         }
    1475           0 : }
    1476             : /* }}} */
    1477             : 
    1478             : /* {{{ create_certinfo
    1479             :  */
    1480          13 : static void create_certinfo(struct curl_certinfo *ci, zval *listcode TSRMLS_DC)
    1481             : {
    1482             :         int i;
    1483             :                         
    1484          13 :         if(ci) {
    1485          13 :                 zval *certhash = NULL;
    1486             :                 
    1487          13 :                 for(i=0; i<ci->num_of_certs; i++) {
    1488             :                         struct curl_slist *slist;
    1489             :                         
    1490           0 :                         MAKE_STD_ZVAL(certhash);
    1491           0 :                         array_init(certhash);
    1492           0 :                         for(slist = ci->certinfo[i]; slist; slist = slist->next) {
    1493             :                                 int len;
    1494             :                                 char s[64];
    1495             :                                 char *tmp;
    1496           0 :                                 strncpy(s, slist->data, 64);
    1497           0 :                                 tmp = memchr(s, ':', 64);
    1498           0 :                                 if(tmp) {
    1499           0 :                                         *tmp = '\0';
    1500           0 :                                         len = strlen(s);
    1501           0 :                                         if(!strcmp(s, "Subject") || !strcmp(s, "Issuer")) {
    1502             :                                                 zval *hash;
    1503             : 
    1504           0 :                                                 MAKE_STD_ZVAL(hash);
    1505           0 :                                                 array_init(hash);
    1506             :                                                 
    1507           0 :                                                 split_certinfo(&slist->data[len+1], hash);
    1508           0 :                                                 add_assoc_zval(certhash, s, hash);
    1509             :                                         } else {
    1510           0 :                                                 add_assoc_string(certhash, s, &slist->data[len+1], 1);
    1511             :                                         }
    1512             :                                 } else {
    1513           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not extract hash key from certificate info"); 
    1514             :                                 }
    1515             :                         }
    1516           0 :                         add_next_index_zval(listcode, certhash);
    1517             :                 }
    1518             :         }
    1519          13 : }
    1520             : /* }}} */
    1521             : #endif
    1522             : 
    1523             : /* {{{ proto resource curl_init([string url])
    1524             :    Initialize a cURL session */
    1525          90 : PHP_FUNCTION(curl_init)
    1526             : {
    1527             :         php_curl        *ch;
    1528             :         CURL            *cp;
    1529             :         zval            *clone;
    1530          90 :         char            *url = NULL;
    1531          90 :         int             url_len = 0;
    1532             :         char *cainfo;
    1533             : 
    1534          90 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &url, &url_len) == FAILURE) {
    1535           0 :                 return;
    1536             :         }
    1537             : 
    1538          90 :         cp = curl_easy_init();
    1539          90 :         if (!cp) {
    1540           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize a new cURL handle");
    1541           0 :                 RETURN_FALSE;
    1542             :         }
    1543             : 
    1544          90 :         alloc_curl_handle(&ch);
    1545             :         TSRMLS_SET_CTX(ch->thread_ctx);
    1546             : 
    1547          90 :         ch->cp = cp;
    1548             : 
    1549          90 :         ch->handlers->write->method = PHP_CURL_STDOUT;
    1550          90 :         ch->handlers->write->type   = PHP_CURL_ASCII;
    1551          90 :         ch->handlers->read->method  = PHP_CURL_DIRECT;
    1552          90 :         ch->handlers->write_header->method = PHP_CURL_IGNORE;
    1553             : 
    1554          90 :         ch->uses = 0;
    1555             : 
    1556          90 :         MAKE_STD_ZVAL(clone);
    1557          90 :         ch->clone = clone;
    1558             : 
    1559             : 
    1560             : 
    1561          90 :         curl_easy_setopt(ch->cp, CURLOPT_NOPROGRESS,        1);
    1562          90 :         curl_easy_setopt(ch->cp, CURLOPT_VERBOSE,           0);
    1563          90 :         curl_easy_setopt(ch->cp, CURLOPT_ERRORBUFFER,       ch->err.str);
    1564          90 :         curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION,     curl_write);
    1565          90 :         curl_easy_setopt(ch->cp, CURLOPT_FILE,              (void *) ch);
    1566          90 :         curl_easy_setopt(ch->cp, CURLOPT_READFUNCTION,      curl_read);
    1567          90 :         curl_easy_setopt(ch->cp, CURLOPT_INFILE,            (void *) ch);
    1568          90 :         curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION,    curl_write_header);
    1569          90 :         curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER,       (void *) ch);
    1570          90 :         curl_easy_setopt(ch->cp, CURLOPT_DNS_USE_GLOBAL_CACHE, 1);
    1571          90 :         curl_easy_setopt(ch->cp, CURLOPT_DNS_CACHE_TIMEOUT, 120);
    1572          90 :         curl_easy_setopt(ch->cp, CURLOPT_MAXREDIRS, 20); /* prevent infinite redirects */
    1573             : 
    1574          90 :         cainfo = INI_STR("curl.cainfo");
    1575          90 :         if (cainfo && strlen(cainfo) > 0) {
    1576           0 :                 curl_easy_setopt(ch->cp, CURLOPT_CAINFO, cainfo);
    1577             :         }
    1578             : 
    1579             : #if defined(ZTS)
    1580             :         curl_easy_setopt(ch->cp, CURLOPT_NOSIGNAL, 1);
    1581             : #endif
    1582             : 
    1583          90 :         if (url) {
    1584          13 :                 if (!php_curl_option_url(ch, url, url_len)) {
    1585           0 :                         _php_curl_close_ex(ch TSRMLS_CC);
    1586           0 :                         RETURN_FALSE;
    1587             :                 }
    1588             :         }
    1589             : 
    1590          90 :         ZEND_REGISTER_RESOURCE(return_value, ch, le_curl);
    1591          90 :         ch->id = Z_LVAL_P(return_value);
    1592             : }
    1593             : /* }}} */
    1594             : 
    1595             : /* {{{ proto resource curl_copy_handle(resource ch)
    1596             :    Copy a cURL handle along with all of it's preferences */
    1597          11 : PHP_FUNCTION(curl_copy_handle)
    1598             : {
    1599             :         CURL            *cp;
    1600             :         zval            *zid;
    1601             :         php_curl        *ch, *dupch;
    1602             : 
    1603          11 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
    1604           0 :                 return;
    1605             :         }
    1606             : 
    1607          11 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
    1608             : 
    1609          11 :         cp = curl_easy_duphandle(ch->cp);
    1610          11 :         if (!cp) {
    1611           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot duplicate cURL handle");
    1612           0 :                 RETURN_FALSE;
    1613             :         }
    1614             : 
    1615          11 :         alloc_curl_handle(&dupch);
    1616             :         TSRMLS_SET_CTX(dupch->thread_ctx);
    1617             : 
    1618          11 :         dupch->cp = cp;
    1619          11 :         dupch->uses = 0;
    1620          11 :         ch->uses++;
    1621          11 :         if (ch->handlers->write->stream) {
    1622           1 :                 Z_ADDREF_P(ch->handlers->write->stream);
    1623             :         }
    1624          11 :         dupch->handlers->write->stream = ch->handlers->write->stream;
    1625          11 :         dupch->handlers->write->method = ch->handlers->write->method;
    1626          11 :         dupch->handlers->write->type   = ch->handlers->write->type;
    1627          11 :         if (ch->handlers->read->stream) {
    1628           0 :                 Z_ADDREF_P(ch->handlers->read->stream);
    1629             :         }
    1630          11 :         dupch->handlers->read->stream  = ch->handlers->read->stream;
    1631          11 :         dupch->handlers->read->method  = ch->handlers->read->method;
    1632          11 :         dupch->handlers->write_header->method = ch->handlers->write_header->method;
    1633          11 :         if (ch->handlers->write_header->stream) {
    1634           0 :                 Z_ADDREF_P(ch->handlers->write_header->stream);
    1635             :         }
    1636          11 :         dupch->handlers->write_header->stream = ch->handlers->write_header->stream;
    1637             : 
    1638          11 :         dupch->handlers->write->fp = ch->handlers->write->fp;
    1639          11 :         dupch->handlers->write_header->fp = ch->handlers->write_header->fp;
    1640          11 :         dupch->handlers->read->fp = ch->handlers->read->fp;
    1641          11 :         dupch->handlers->read->fd = ch->handlers->read->fd;
    1642             : #if CURLOPT_PASSWDDATA != 0
    1643             :         if (ch->handlers->passwd) {
    1644             :                 zval_add_ref(&ch->handlers->passwd);
    1645             :                 dupch->handlers->passwd = ch->handlers->passwd;
    1646             :                 curl_easy_setopt(ch->cp, CURLOPT_PASSWDDATA, (void *) dupch);
    1647             :         }
    1648             : #endif
    1649          11 :         if (ch->handlers->write->func_name) {
    1650           0 :                 zval_add_ref(&ch->handlers->write->func_name);
    1651           0 :                 dupch->handlers->write->func_name = ch->handlers->write->func_name;
    1652             :         }
    1653          11 :         if (ch->handlers->read->func_name) {
    1654           0 :                 zval_add_ref(&ch->handlers->read->func_name);
    1655           0 :                 dupch->handlers->read->func_name = ch->handlers->read->func_name;
    1656             :         }
    1657          11 :         if (ch->handlers->write_header->func_name) {
    1658           0 :                 zval_add_ref(&ch->handlers->write_header->func_name);
    1659           0 :                 dupch->handlers->write_header->func_name = ch->handlers->write_header->func_name;
    1660             :         }
    1661             :         
    1662          11 :         if (ch->handlers->progress->func_name) {
    1663           1 :                 zval_add_ref(&ch->handlers->progress->func_name);
    1664           1 :                 dupch->handlers->progress->func_name = ch->handlers->progress->func_name;
    1665             :         }
    1666          11 :         dupch->handlers->progress->method = ch->handlers->progress->method;
    1667             : 
    1668          11 :         curl_easy_setopt(dupch->cp, CURLOPT_ERRORBUFFER,       dupch->err.str);
    1669          11 :         curl_easy_setopt(dupch->cp, CURLOPT_FILE,              (void *) dupch);
    1670          11 :         curl_easy_setopt(dupch->cp, CURLOPT_INFILE,            (void *) dupch);
    1671          11 :         curl_easy_setopt(dupch->cp, CURLOPT_WRITEHEADER,       (void *) dupch);
    1672          11 :         curl_easy_setopt(dupch->cp, CURLOPT_PROGRESSDATA,      (void *) dupch); 
    1673             : 
    1674          11 :         efree(dupch->to_free);
    1675          11 :         dupch->to_free = ch->to_free;
    1676             : 
    1677             :         /* Keep track of cloned copies to avoid invoking curl destructors for every clone */
    1678          11 :         Z_ADDREF_P(ch->clone);
    1679          11 :         dupch->clone = ch->clone;
    1680             : 
    1681          11 :         ZEND_REGISTER_RESOURCE(return_value, dupch, le_curl);
    1682          11 :         dupch->id = Z_LVAL_P(return_value);
    1683             : }
    1684             : /* }}} */
    1685             : 
    1686        2255 : static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue, zval *return_value TSRMLS_DC) /* {{{ */
    1687             : {
    1688        2255 :         CURLcode     error=CURLE_OK;
    1689             : 
    1690        2255 :         switch (option) {
    1691             :                 case CURLOPT_SSL_VERIFYHOST:
    1692           5 :                         if(Z_BVAL_PP(zvalue) == 1) {
    1693             : #if LIBCURL_VERSION_NUM <= 0x071c00 /* 7.28.0 */
    1694           2 :                                 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");
    1695             : #else
    1696             :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead");
    1697             :                                 error = curl_easy_setopt(ch->cp, option, 2);
    1698             :                                 break;
    1699             : #endif
    1700             :                         }
    1701             :                 case CURLOPT_INFILESIZE:
    1702             :                 case CURLOPT_VERBOSE:
    1703             :                 case CURLOPT_HEADER:
    1704             :                 case CURLOPT_NOPROGRESS:
    1705             :                 case CURLOPT_NOBODY:
    1706             :                 case CURLOPT_FAILONERROR:
    1707             :                 case CURLOPT_UPLOAD:
    1708             :                 case CURLOPT_POST:
    1709             :                 case CURLOPT_FTPLISTONLY:
    1710             :                 case CURLOPT_FTPAPPEND:
    1711             :                 case CURLOPT_NETRC:
    1712             :                 case CURLOPT_PUT:
    1713             : #if CURLOPT_MUTE != 0
    1714             :                  case CURLOPT_MUTE:
    1715             : #endif
    1716             :                 case CURLOPT_TIMEOUT:
    1717             : #if LIBCURL_VERSION_NUM > 0x071002
    1718             :                 case CURLOPT_TIMEOUT_MS:
    1719             : #endif
    1720             :                 case CURLOPT_FTP_USE_EPSV:
    1721             :                 case CURLOPT_LOW_SPEED_LIMIT:
    1722             :                 case CURLOPT_SSLVERSION:
    1723             :                 case CURLOPT_LOW_SPEED_TIME:
    1724             :                 case CURLOPT_RESUME_FROM:
    1725             :                 case CURLOPT_TIMEVALUE:
    1726             :                 case CURLOPT_TIMECONDITION:
    1727             :                 case CURLOPT_TRANSFERTEXT:
    1728             :                 case CURLOPT_HTTPPROXYTUNNEL:
    1729             :                 case CURLOPT_FILETIME:
    1730             :                 case CURLOPT_MAXREDIRS:
    1731             :                 case CURLOPT_MAXCONNECTS:
    1732             :                 case CURLOPT_CLOSEPOLICY:
    1733             :                 case CURLOPT_FRESH_CONNECT:
    1734             :                 case CURLOPT_FORBID_REUSE:
    1735             :                 case CURLOPT_CONNECTTIMEOUT:
    1736             : #if LIBCURL_VERSION_NUM > 0x071002
    1737             :                 case CURLOPT_CONNECTTIMEOUT_MS:
    1738             : #endif
    1739             :                 case CURLOPT_SSL_VERIFYPEER:
    1740             :                 case CURLOPT_DNS_USE_GLOBAL_CACHE:
    1741             :                 case CURLOPT_NOSIGNAL:
    1742             :                 case CURLOPT_PROXYTYPE:
    1743             :                 case CURLOPT_BUFFERSIZE:
    1744             :                 case CURLOPT_HTTPGET:
    1745             :                 case CURLOPT_HTTP_VERSION:
    1746             :                 case CURLOPT_CRLF:
    1747             :                 case CURLOPT_DNS_CACHE_TIMEOUT:
    1748             :                 case CURLOPT_PROXYPORT:
    1749             :                 case CURLOPT_FTP_USE_EPRT:
    1750             : #if LIBCURL_VERSION_NUM > 0x070a05 /* CURLOPT_HTTPAUTH is available since curl 7.10.6 */
    1751             :                 case CURLOPT_HTTPAUTH:
    1752             : #endif
    1753             : #if LIBCURL_VERSION_NUM > 0x070a06 /* CURLOPT_PROXYAUTH & CURLOPT_FTP_CREATE_MISSING_DIRS are available since curl 7.10.7 */
    1754             :                 case CURLOPT_PROXYAUTH:
    1755             :                 case CURLOPT_FTP_CREATE_MISSING_DIRS:
    1756             : #endif
    1757             : 
    1758             : #if LIBCURL_VERSION_NUM >= 0x070c02
    1759             :                 case CURLOPT_FTPSSLAUTH:
    1760             : #endif
    1761             : #if LIBCURL_VERSION_NUM >  0x070b00
    1762             :                 case CURLOPT_FTP_SSL:
    1763             : #endif
    1764             :                 case CURLOPT_UNRESTRICTED_AUTH:
    1765             :                 case CURLOPT_PORT:
    1766             :                 case CURLOPT_AUTOREFERER:
    1767             :                 case CURLOPT_COOKIESESSION:
    1768             : #if LIBCURL_VERSION_NUM > 0x070b01 /* CURLOPT_TCP_NODELAY is available since curl 7.11.2 */
    1769             :                 case CURLOPT_TCP_NODELAY:
    1770             : #endif
    1771             : #if LIBCURL_VERSION_NUM >= 0x71304
    1772             :                 case CURLOPT_REDIR_PROTOCOLS:
    1773             :                 case CURLOPT_PROTOCOLS:
    1774             : #endif
    1775             : #if LIBCURL_VERSION_NUM > 0x070a07 /* CURLOPT_IPRESOLVE is available since curl 7.10.8 */
    1776             :                 case CURLOPT_IPRESOLVE:
    1777             : #endif
    1778             : #if LIBCURL_VERSION_NUM >= 0x070f01
    1779             :                 case CURLOPT_FTP_FILEMETHOD:
    1780             :                 case CURLOPT_FTP_SKIP_PASV_IP:
    1781             : #endif
    1782             : #if LIBCURL_VERSION_NUM >  0x071301
    1783             :                 case CURLOPT_CERTINFO:
    1784             : #endif
    1785          55 :                         convert_to_long_ex(zvalue);
    1786             : #if LIBCURL_VERSION_NUM >= 0x71304
    1787          29 :                         if ((option == CURLOPT_PROTOCOLS || option == CURLOPT_REDIR_PROTOCOLS) &&
    1788           0 :                                 ((PG(open_basedir) && *PG(open_basedir)) || PG(safe_mode)) && (Z_LVAL_PP(zvalue) & CURLPROTO_FILE)) {
    1789           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLPROTO_FILE cannot be activated when in safe_mode or an open_basedir is set");
    1790           0 :                                         RETVAL_FALSE;
    1791           0 :                                         return 1;
    1792             :                         }
    1793             : #endif
    1794          29 :                         error = curl_easy_setopt(ch->cp, option, Z_LVAL_PP(zvalue));
    1795          29 :                         break;
    1796             : #if LIBCURL_VERSION_NUM > 0x070f04
    1797             :                 case CURLOPT_MAX_RECV_SPEED_LARGE:
    1798             :                 case CURLOPT_MAX_SEND_SPEED_LARGE:
    1799           0 :                         convert_to_long_ex(zvalue);
    1800           0 :                         error = curl_easy_setopt(ch->cp, option, (curl_off_t)Z_LVAL_PP(zvalue));
    1801           0 :                         break;
    1802             : #endif
    1803             :                 case CURLOPT_FOLLOWLOCATION:
    1804           2 :                         convert_to_long_ex(zvalue);
    1805           2 :                         if ((PG(open_basedir) && *PG(open_basedir)) || PG(safe_mode)) {
    1806           2 :                                 if (Z_LVAL_PP(zvalue) != 0) {
    1807           2 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set");
    1808           2 :                                         RETVAL_FALSE;
    1809           2 :                                         return 1;
    1810             :                                 }
    1811             :                         }
    1812           0 :                         error = curl_easy_setopt(ch->cp, option, Z_LVAL_PP(zvalue));
    1813           0 :                         break;
    1814             : #if LIBCURL_VERSION_NUM > 0x071301
    1815             :                 case CURLOPT_POSTREDIR:
    1816           0 :                         convert_to_long_ex(zvalue);
    1817           0 :                         error = curl_easy_setopt(ch->cp, CURLOPT_POSTREDIR, Z_LVAL_PP(zvalue) & CURL_REDIR_POST_ALL);
    1818           0 :                         break;
    1819             : #endif
    1820             :                 case CURLOPT_PRIVATE:
    1821             :                 case CURLOPT_URL:
    1822             :                 case CURLOPT_PROXY:
    1823             :                 case CURLOPT_USERPWD:
    1824             :                 case CURLOPT_PROXYUSERPWD:
    1825             :                 case CURLOPT_RANGE:
    1826             :                 case CURLOPT_CUSTOMREQUEST:
    1827             :                 case CURLOPT_USERAGENT:
    1828             :                 case CURLOPT_FTPPORT:
    1829             :                 case CURLOPT_COOKIE:
    1830             :                 case CURLOPT_REFERER:
    1831             :                 case CURLOPT_INTERFACE:
    1832             :                 case CURLOPT_KRB4LEVEL:
    1833             :                 case CURLOPT_EGDSOCKET:
    1834             :                 case CURLOPT_CAINFO:
    1835             :                 case CURLOPT_CAPATH:
    1836             :                 case CURLOPT_SSL_CIPHER_LIST:
    1837             :                 case CURLOPT_SSLKEY:
    1838             :                 case CURLOPT_SSLKEYTYPE:
    1839             :                 case CURLOPT_SSLKEYPASSWD:
    1840             :                 case CURLOPT_SSLENGINE:
    1841             :                 case CURLOPT_SSLENGINE_DEFAULT:
    1842             :                 case CURLOPT_SSLCERTTYPE:
    1843             :                 case CURLOPT_ENCODING:
    1844             : #if LIBCURL_VERSION_NUM >= 0x071300
    1845             :                 case CURLOPT_SSH_PUBLIC_KEYFILE:
    1846             :                 case CURLOPT_SSH_PRIVATE_KEYFILE:
    1847             : #endif
    1848             :                 {
    1849             : #if LIBCURL_VERSION_NUM < 0x071100
    1850             :                         char *copystr = NULL;
    1851             : #endif
    1852             : 
    1853        1098 :                         convert_to_string_ex(zvalue);
    1854             : #if LIBCURL_VERSION_NUM >= 0x071300
    1855        1098 :                         if (
    1856             :                                 option == CURLOPT_SSH_PUBLIC_KEYFILE || option == CURLOPT_SSH_PRIVATE_KEYFILE
    1857             : 
    1858             :                         ) {
    1859           0 :                                 if (php_check_open_basedir(Z_STRVAL_PP(zvalue) TSRMLS_CC) || (PG(safe_mode) && !php_checkuid(Z_STRVAL_PP(zvalue), "rb+", CHECKUID_CHECK_MODE_PARAM))) {
    1860           0 :                                         RETVAL_FALSE;
    1861           0 :                                         return 1;
    1862             :                                 }
    1863             :                         }
    1864             : #endif
    1865        1098 :                         if (option == CURLOPT_URL) {
    1866        1092 :                                 if (!php_curl_option_url(ch, Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue))) {
    1867           0 :                                         RETVAL_FALSE;
    1868           0 :                                         return 1;
    1869             :                                 }
    1870             :                         } else {
    1871           6 :                                 if (option == CURLOPT_PRIVATE) {
    1872             :                                         char *copystr;
    1873             : #if LIBCURL_VERSION_NUM < 0x071100
    1874             : string_copy:
    1875             : #endif
    1876           1 :                                         copystr = estrndup(Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue));
    1877           1 :                                         error = curl_easy_setopt(ch->cp, option, copystr);
    1878           1 :                                         zend_llist_add_element(&ch->to_free->str, &copystr);
    1879             :                                 } else {
    1880             : #if LIBCURL_VERSION_NUM >= 0x071100
    1881             :                                         /* Strings passed to libcurl as ’char *’ arguments, are copied by the library... NOTE: before 7.17.0 strings were not copied. */
    1882           5 :                                         error = curl_easy_setopt(ch->cp, option, Z_STRVAL_PP(zvalue));
    1883             : #else                           
    1884             :                                         goto string_copy;                       
    1885             : #endif
    1886             :                                 }
    1887             :                         }
    1888        1098 :                         break;
    1889             :                 }
    1890             :                 case CURLOPT_FILE:
    1891             :                 case CURLOPT_INFILE:
    1892             :                 case CURLOPT_WRITEHEADER:
    1893             :                 case CURLOPT_STDERR: {
    1894        1051 :                         FILE *fp = NULL;
    1895             :                         int type;
    1896             :                         void * what;
    1897             : 
    1898        1051 :                         what = zend_fetch_resource(zvalue TSRMLS_CC, -1, "File-Handle", &type, 1, php_file_le_stream(), php_file_le_pstream());
    1899        1051 :                         if (!what) {
    1900           0 :                                 RETVAL_FALSE;
    1901           0 :                                 return 1;
    1902             :                         }
    1903             : 
    1904        1051 :                         if (FAILURE == php_stream_cast((php_stream *) what, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) {
    1905           0 :                                 RETVAL_FALSE;
    1906           0 :                                 return 1;
    1907             :                         }
    1908             : 
    1909        1051 :                         if (!fp) {
    1910           0 :                                 RETVAL_FALSE;
    1911           0 :                                 return 1;
    1912             :                         }
    1913             : 
    1914        1051 :                         error = CURLE_OK;
    1915        1051 :                         switch (option) {
    1916             :                                 case CURLOPT_FILE:
    1917        2063 :                                         if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') {
    1918        1031 :                                                 if (ch->handlers->write->stream) {
    1919        1023 :                                                         Z_DELREF_P(ch->handlers->write->stream);
    1920             :                                                 }
    1921        1031 :                                                 Z_ADDREF_PP(zvalue);
    1922        1031 :                                                 ch->handlers->write->fp = fp;
    1923        1031 :                                                 ch->handlers->write->method = PHP_CURL_FILE;
    1924        1031 :                                                 ch->handlers->write->stream = *zvalue;
    1925             :                                         } else {
    1926           1 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable");
    1927           1 :                                                 RETVAL_FALSE;
    1928           1 :                                                 return 1;
    1929             :                                         }
    1930        1031 :                                         break;
    1931             :                                 case CURLOPT_WRITEHEADER:
    1932           6 :                                         if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') {
    1933           3 :                                                 if (ch->handlers->write_header->stream) {
    1934           0 :                                                         Z_DELREF_P(ch->handlers->write_header->stream);
    1935             :                                                 }
    1936           3 :                                                 Z_ADDREF_PP(zvalue);
    1937           3 :                                                 ch->handlers->write_header->fp = fp;
    1938           3 :                                                 ch->handlers->write_header->method = PHP_CURL_FILE;
    1939           3 :                                                 ch->handlers->write_header->stream = *zvalue;
    1940             :                                         } else {
    1941           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable");
    1942           0 :                                                 RETVAL_FALSE;
    1943           0 :                                                 return 1;
    1944             :                                         }
    1945           3 :                                         break;
    1946             :                                 case CURLOPT_INFILE:
    1947           5 :                                         if (ch->handlers->read->stream) {
    1948           0 :                                                 Z_DELREF_P(ch->handlers->read->stream);
    1949             :                                         }
    1950           5 :                                         Z_ADDREF_PP(zvalue);
    1951           5 :                                         ch->handlers->read->fp = fp;
    1952           5 :                                         ch->handlers->read->fd = Z_LVAL_PP(zvalue);
    1953           5 :                                         ch->handlers->read->stream = *zvalue;
    1954           5 :                                         break;
    1955             :                                 case CURLOPT_STDERR:
    1956          22 :                                         if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') {
    1957          11 :                                                 if (ch->handlers->std_err) {
    1958           1 :                                                         zval_ptr_dtor(&ch->handlers->std_err);
    1959             :                                                 }
    1960          11 :                                                 zval_add_ref(zvalue);
    1961          11 :                                                 ch->handlers->std_err = *zvalue;
    1962             :                                         } else {
    1963           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable");
    1964           0 :                                                 RETVAL_FALSE;
    1965           0 :                                                 return 1;
    1966             :                                         }
    1967             :                                         /* break omitted intentionally */
    1968             :                                 default:
    1969          11 :                                         error = curl_easy_setopt(ch->cp, option, fp);
    1970             :                                         break;
    1971             :                         }
    1972             : 
    1973        1050 :                         break;
    1974             :                 }
    1975             :                 case CURLOPT_RETURNTRANSFER:
    1976          93 :                         convert_to_long_ex(zvalue);
    1977             : 
    1978          51 :                         if (Z_LVAL_PP(zvalue)) {
    1979          50 :                                 ch->handlers->write->method = PHP_CURL_RETURN;
    1980             :                         } else {
    1981           1 :                                 ch->handlers->write->method = PHP_CURL_STDOUT;
    1982             :                         }
    1983          51 :                         break;
    1984             :                 case CURLOPT_BINARYTRANSFER:
    1985           6 :                         convert_to_long_ex(zvalue);
    1986             : 
    1987           1 :                         if (Z_LVAL_PP(zvalue)) {
    1988           1 :                                 ch->handlers->write->type = PHP_CURL_BINARY;
    1989             :                         } else {
    1990           0 :                                 ch->handlers->write->type = PHP_CURL_ASCII;
    1991             :                         }
    1992           1 :                         break;
    1993             :                 case CURLOPT_WRITEFUNCTION:
    1994           2 :                         if (ch->handlers->write->func_name) {
    1995           0 :                                 zval_ptr_dtor(&ch->handlers->write->func_name);
    1996           0 :                                 ch->handlers->write->fci_cache = empty_fcall_info_cache;
    1997             :                         }
    1998           2 :                         zval_add_ref(zvalue);
    1999           2 :                         ch->handlers->write->func_name = *zvalue;
    2000           2 :                         ch->handlers->write->method = PHP_CURL_USER;
    2001           2 :                         break;
    2002             :                 case CURLOPT_READFUNCTION:
    2003           1 :                         if (ch->handlers->read->func_name) {
    2004           0 :                                 zval_ptr_dtor(&ch->handlers->read->func_name);
    2005           0 :                                 ch->handlers->read->fci_cache = empty_fcall_info_cache;
    2006             :                         }
    2007           1 :                         zval_add_ref(zvalue);
    2008           1 :                         ch->handlers->read->func_name = *zvalue;
    2009           1 :                         ch->handlers->read->method = PHP_CURL_USER;
    2010           1 :                         break;
    2011             :                 case CURLOPT_PROGRESSFUNCTION:
    2012           1 :                         curl_easy_setopt(ch->cp, CURLOPT_PROGRESSFUNCTION,   curl_progress);
    2013           1 :                         curl_easy_setopt(ch->cp, CURLOPT_PROGRESSDATA, ch);
    2014           1 :                         if (ch->handlers->progress->func_name) {
    2015           0 :                                 zval_ptr_dtor(&ch->handlers->progress->func_name);
    2016           0 :                                 ch->handlers->progress->fci_cache = empty_fcall_info_cache;
    2017             :                         }
    2018           1 :                         zval_add_ref(zvalue);
    2019           1 :                         ch->handlers->progress->func_name = *zvalue;
    2020           1 :                         ch->handlers->progress->method = PHP_CURL_USER;
    2021           1 :                         break;
    2022             :                 case CURLOPT_HEADERFUNCTION:
    2023           1 :                         if (ch->handlers->write_header->func_name) {
    2024           0 :                                 zval_ptr_dtor(&ch->handlers->write_header->func_name);
    2025           0 :                                 ch->handlers->write_header->fci_cache = empty_fcall_info_cache;
    2026             :                         }
    2027           1 :                         zval_add_ref(zvalue);
    2028           1 :                         ch->handlers->write_header->func_name = *zvalue;
    2029           1 :                         ch->handlers->write_header->method = PHP_CURL_USER;
    2030           1 :                         break;
    2031             : #if CURLOPT_PASSWDFUNCTION != 0
    2032             :                 case CURLOPT_PASSWDFUNCTION:
    2033             :                         if (ch->handlers->passwd) {
    2034             :                                 zval_ptr_dtor(&ch->handlers->passwd);
    2035             :                         }
    2036             :                         zval_add_ref(zvalue);
    2037             :                         ch->handlers->passwd = *zvalue;
    2038             :                         error = curl_easy_setopt(ch->cp, CURLOPT_PASSWDFUNCTION, curl_passwd);
    2039             :                         error = curl_easy_setopt(ch->cp, CURLOPT_PASSWDDATA,     (void *) ch);
    2040             :                         break;
    2041             : #endif
    2042             :                 case CURLOPT_POSTFIELDS:
    2043          15 :                         if (Z_TYPE_PP(zvalue) == IS_ARRAY || Z_TYPE_PP(zvalue) == IS_OBJECT) {
    2044             :                                 zval            **current;
    2045             :                                 HashTable        *postfields;
    2046           6 :                                 struct HttpPost  *first = NULL;
    2047           6 :                                 struct HttpPost  *last  = NULL;
    2048             :                                 char             *postval;
    2049           6 :                                 char             *string_key = NULL;
    2050             :                                 ulong             num_key;
    2051             :                                 uint              string_key_len;
    2052             : 
    2053           6 :                                 postfields = HASH_OF(*zvalue);
    2054           6 :                                 if (!postfields) {
    2055           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't get HashTable in CURLOPT_POSTFIELDS");
    2056           0 :                                         RETVAL_FALSE;
    2057           0 :                                         return 1;
    2058             :                                 }
    2059             : 
    2060          20 :                                 for (zend_hash_internal_pointer_reset(postfields);
    2061          14 :                                          zend_hash_get_current_data(postfields, (void **) &current) == SUCCESS;
    2062             :                                          zend_hash_move_forward(postfields)
    2063           8 :                                 ) {
    2064             : 
    2065          16 :                                         SEPARATE_ZVAL(current);
    2066           8 :                                         convert_to_string_ex(current);
    2067             : 
    2068           8 :                                         zend_hash_get_current_key_ex(postfields, &string_key, &string_key_len, &num_key, 0, NULL);
    2069             : 
    2070           8 :                                         postval = Z_STRVAL_PP(current);
    2071             : 
    2072             :                                         /* The arguments after _NAMELENGTH and _CONTENTSLENGTH
    2073             :                                          * must be explicitly cast to long in curl_formadd
    2074             :                                          * use since curl needs a long not an int. */
    2075           8 :                                         if (*postval == '@') {
    2076             :                                                 char *type, *filename;
    2077           5 :                                                 ++postval;
    2078             : 
    2079           5 :                                                 if ((type = php_memnstr(postval, ";type=", sizeof(";type=") - 1, postval + Z_STRLEN_PP(current)))) {
    2080           3 :                                                         *type = '\0';
    2081             :                                                 }
    2082           5 :                                                 if ((filename = php_memnstr(postval, ";filename=", sizeof(";filename=") - 1, postval + Z_STRLEN_PP(current)))) {
    2083           3 :                                                         *filename = '\0';
    2084             :                                                 }
    2085             :                                                 /* safe_mode / open_basedir check */
    2086           5 :                                                 if (php_check_open_basedir(postval TSRMLS_CC) || (PG(safe_mode) && !php_checkuid(postval, "rb+", CHECKUID_CHECK_MODE_PARAM))) {
    2087           0 :                                                         RETVAL_FALSE;
    2088           0 :                                                         return 1;
    2089             :                                                 }
    2090           5 :                                                 error = curl_formadd(&first, &last,
    2091             :                                                                                 CURLFORM_COPYNAME, string_key,
    2092             :                                                                                 CURLFORM_NAMELENGTH, (long)string_key_len - 1,
    2093             :                                                                                 CURLFORM_FILENAME, filename ? filename + sizeof(";filename=") - 1 : postval,
    2094             :                                                                                 CURLFORM_CONTENTTYPE, type ? type + sizeof(";type=") - 1 : "application/octet-stream",
    2095             :                                                                                 CURLFORM_FILE, postval,
    2096             :                                                                                 CURLFORM_END);
    2097           5 :                                                 if (type) {
    2098           3 :                                                         *type = ';';
    2099             :                                                 }
    2100           5 :                                                 if (filename) {
    2101           3 :                                                         *filename = ';';
    2102             :                                                 }
    2103             :                                         } else {
    2104           3 :                                                 error = curl_formadd(&first, &last,
    2105             :                                                                                          CURLFORM_COPYNAME, string_key,
    2106             :                                                                                          CURLFORM_NAMELENGTH, (long)string_key_len - 1,
    2107             :                                                                                          CURLFORM_COPYCONTENTS, postval,
    2108           3 :                                                                                          CURLFORM_CONTENTSLENGTH, (long)Z_STRLEN_PP(current),
    2109             :                                                                                          CURLFORM_END);
    2110             :                                         }
    2111             :                                 }
    2112             : 
    2113           6 :                                 SAVE_CURL_ERROR(ch, error);
    2114           6 :                                 if (error != CURLE_OK) {
    2115           0 :                                         RETVAL_FALSE;
    2116           0 :                                         return 1;
    2117             :                                 }
    2118             : 
    2119           6 :                                 zend_llist_add_element(&ch->to_free->post, &first);
    2120           6 :                                 error = curl_easy_setopt(ch->cp, CURLOPT_HTTPPOST, first);
    2121             : 
    2122             :                         } else {
    2123             : #if LIBCURL_VERSION_NUM >= 0x071101
    2124           3 :                                 convert_to_string_ex(zvalue);
    2125             :                                 /* with curl 7.17.0 and later, we can use COPYPOSTFIELDS, but we have to provide size before */
    2126           3 :                                 error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, Z_STRLEN_PP(zvalue));
    2127           3 :                                 error = curl_easy_setopt(ch->cp, CURLOPT_COPYPOSTFIELDS, Z_STRVAL_PP(zvalue));
    2128             : #else
    2129             :                                 char *post = NULL;
    2130             : 
    2131             :                                 convert_to_string_ex(zvalue);
    2132             :                                 post = estrndup(Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue));
    2133             :                                 zend_llist_add_element(&ch->to_free->str, &post);
    2134             : 
    2135             :                                 error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDS, post);
    2136             :                                 error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, Z_STRLEN_PP(zvalue));
    2137             : #endif
    2138             :                         }
    2139           9 :                         break;
    2140             :                 case CURLOPT_HTTPHEADER:
    2141             :                 case CURLOPT_QUOTE:
    2142             :                 case CURLOPT_HTTP200ALIASES:
    2143             :                 case CURLOPT_POSTQUOTE: {
    2144             :                         zval              **current;
    2145             :                         HashTable          *ph;
    2146           4 :                         struct curl_slist  *slist = NULL;
    2147             : 
    2148           4 :                         ph = HASH_OF(*zvalue);
    2149           4 :                         if (!ph) {
    2150           1 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must pass either an object or an array with the CURLOPT_HTTPHEADER, CURLOPT_QUOTE, CURLOPT_HTTP200ALIASES and CURLOPT_POSTQUOTE arguments");
    2151           1 :                                 RETVAL_FALSE;
    2152           1 :                                 return 1;
    2153             :                         }
    2154             : 
    2155           9 :                         for (zend_hash_internal_pointer_reset(ph);
    2156           6 :                                  zend_hash_get_current_data(ph, (void **) &current) == SUCCESS;
    2157             :                                  zend_hash_move_forward(ph)
    2158           3 :                         ) {
    2159           6 :                                 SEPARATE_ZVAL(current);
    2160           3 :                                 convert_to_string_ex(current);
    2161             : 
    2162           3 :                                 slist = curl_slist_append(slist, Z_STRVAL_PP(current));
    2163           3 :                                 if (!slist) {
    2164           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not build curl_slist");
    2165           0 :                                         RETVAL_FALSE;
    2166           0 :                                         return 1;
    2167             :                                 }
    2168             :                         }
    2169           3 :                         zend_llist_add_element(&ch->to_free->slist, &slist);
    2170             : 
    2171           3 :                         error = curl_easy_setopt(ch->cp, option, slist);
    2172             : 
    2173           3 :                         break;
    2174             :                 }
    2175             :                 /* the following options deal with files, therefor safe_mode & open_basedir checks
    2176             :                  * are required.
    2177             :                  */
    2178             :                 case CURLOPT_COOKIEJAR:
    2179             :                 case CURLOPT_SSLCERT:
    2180             :                 case CURLOPT_RANDOM_FILE:
    2181             :                 case CURLOPT_COOKIEFILE: {
    2182             : #if LIBCURL_VERSION_NUM < 0x071100
    2183             :                         char *copystr = NULL;
    2184             : #endif
    2185             : 
    2186           3 :                         convert_to_string_ex(zvalue);
    2187             : 
    2188           3 :                         if ((Z_STRLEN_PP(zvalue) && php_check_open_basedir(Z_STRVAL_PP(zvalue) TSRMLS_CC)) || (PG(safe_mode) && !php_checkuid(Z_STRVAL_PP(zvalue), "rb+", CHECKUID_CHECK_MODE_PARAM))) {
    2189           1 :                                 RETVAL_FALSE;
    2190           1 :                                 return 1;
    2191             :                         }
    2192             : 
    2193             : #if LIBCURL_VERSION_NUM >= 0x071100
    2194           2 :                         error = curl_easy_setopt(ch->cp, option, Z_STRVAL_PP(zvalue));
    2195             : #else
    2196             :                         copystr = estrndup(Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue));
    2197             : 
    2198             :                         error = curl_easy_setopt(ch->cp, option, copystr);
    2199             :                         zend_llist_add_element(&ch->to_free->str, &copystr);
    2200             : #endif
    2201           2 :                         break;
    2202             :                 }
    2203             :                 case CURLINFO_HEADER_OUT:
    2204           0 :                         convert_to_long_ex(zvalue);
    2205           0 :                         if (Z_LVAL_PP(zvalue) == 1) {
    2206           0 :                                 curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, curl_debug);
    2207           0 :                                 curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, (void *)ch);
    2208           0 :                                 curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 1);
    2209             :                         } else {
    2210           0 :                                 curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, NULL);
    2211           0 :                                 curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, NULL);
    2212           0 :                                 curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 0);
    2213             :                         }
    2214             :                         break;
    2215             :         }
    2216             : 
    2217        2250 :         SAVE_CURL_ERROR(ch, error);
    2218        2250 :         if (error != CURLE_OK) {
    2219           0 :                 return 1;
    2220             :         } else {
    2221        2250 :                 return 0;
    2222             :         }
    2223             : }
    2224             : /* }}} */
    2225             : 
    2226             : /* {{{ proto bool curl_setopt(resource ch, int option, mixed value)
    2227             :    Set an option for a cURL transfer */
    2228        2230 : PHP_FUNCTION(curl_setopt)
    2229             : {
    2230             :         zval       *zid, **zvalue;
    2231             :         long        options;
    2232             :         php_curl   *ch;
    2233             : 
    2234        2230 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlZ", &zid, &options, &zvalue) == FAILURE) {
    2235          10 :                 return;
    2236             :         }
    2237             : 
    2238        2220 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
    2239             : 
    2240        2220 :         if (options <= 0) {
    2241           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid curl configuration option");
    2242           1 :                 RETURN_FALSE;
    2243             :         }
    2244             : 
    2245        2219 :         if (!_php_curl_setopt(ch, options, zvalue, return_value TSRMLS_CC)) {
    2246        2214 :                 RETURN_TRUE;
    2247             :         } else {
    2248           5 :                 RETURN_FALSE;
    2249             :         }
    2250             : }
    2251             : /* }}} */
    2252             : 
    2253             : /* {{{ proto bool curl_setopt_array(resource ch, array options)
    2254             :    Set an array of option for a cURL transfer */
    2255          12 : PHP_FUNCTION(curl_setopt_array)
    2256             : {
    2257             :         zval            *zid, *arr, **entry;
    2258             :         php_curl        *ch;
    2259             :         ulong           option;
    2260             :         HashPosition    pos;
    2261             :         char            *string_key;
    2262             :         uint            str_key_len;
    2263             : 
    2264          12 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "za", &zid, &arr) == FAILURE) {
    2265           0 :                 return;
    2266             :         }
    2267             : 
    2268          12 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
    2269             : 
    2270          12 :         zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos);
    2271          60 :         while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **)&entry, &pos) == SUCCESS) {
    2272          36 :                 if (zend_hash_get_current_key_ex(Z_ARRVAL_P(arr), &string_key, &str_key_len, &option, 0, &pos) != HASH_KEY_IS_LONG) {
    2273           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array keys must be CURLOPT constants or equivalent integer values");
    2274           0 :                         RETURN_FALSE;
    2275             :                 }
    2276          36 :                 if (_php_curl_setopt(ch, (long) option, entry, return_value TSRMLS_CC)) {
    2277           0 :                         RETURN_FALSE;
    2278             :                 }
    2279          36 :                 zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos);
    2280             :         }
    2281          12 :         RETURN_TRUE;
    2282             : }
    2283             : /* }}} */
    2284             : 
    2285             : /* {{{ _php_curl_cleanup_handle(ch)
    2286             :    Cleanup an execution phase */
    2287        1106 : void _php_curl_cleanup_handle(php_curl *ch)
    2288             : {
    2289        1106 :         if (ch->handlers->write->buf.len > 0) {
    2290           4 :                 smart_str_free(&ch->handlers->write->buf);
    2291             :         }
    2292        1106 :         if (ch->header.str_len) {
    2293           0 :                 efree(ch->header.str);
    2294           0 :                 ch->header.str_len = 0;
    2295             :         }
    2296             : 
    2297        1106 :         memset(ch->err.str, 0, CURL_ERROR_SIZE + 1);
    2298        1106 :         ch->err.no = 0;
    2299        1106 : }
    2300             : /* }}} */
    2301             : 
    2302             : /* {{{ proto bool curl_exec(resource ch)
    2303             :    Perform a cURL session */
    2304        1083 : PHP_FUNCTION(curl_exec)
    2305             : {
    2306             :         CURLcode        error;
    2307             :         zval            *zid;
    2308             :         php_curl        *ch;
    2309             : 
    2310        1083 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
    2311           0 :                 return;
    2312             :         }
    2313             : 
    2314        1083 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
    2315             : 
    2316        1083 :         _php_curl_verify_handlers(ch, 1 TSRMLS_CC);
    2317             : 
    2318        1083 :         _php_curl_cleanup_handle(ch);
    2319             : 
    2320        1083 :         error = curl_easy_perform(ch->cp);
    2321        1083 :         SAVE_CURL_ERROR(ch, error);
    2322             :         /* CURLE_PARTIAL_FILE is returned by HEAD requests */
    2323        1083 :         if (error != CURLE_OK && error != CURLE_PARTIAL_FILE) {
    2324        1036 :                 if (ch->handlers->write->buf.len > 0) {
    2325           0 :                         smart_str_free(&ch->handlers->write->buf);
    2326             :                 }
    2327        1036 :                 RETURN_FALSE;
    2328             :         }
    2329             : 
    2330          47 :         if (ch->handlers->std_err) {
    2331             :                 php_stream  *stream;
    2332           2 :                 stream = (php_stream*)zend_fetch_resource(&ch->handlers->std_err TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream());
    2333           2 :                 if (stream) {
    2334           2 :                         php_stream_flush(stream);
    2335             :                 }
    2336             :         }
    2337             : 
    2338          47 :         if (ch->handlers->write->method == PHP_CURL_RETURN && ch->handlers->write->buf.len > 0) {
    2339          32 :                 smart_str_0(&ch->handlers->write->buf);
    2340          32 :                 RETURN_STRINGL(ch->handlers->write->buf.c, ch->handlers->write->buf.len, 1);
    2341             :         }
    2342             : 
    2343             :         /* flush the file handle, so any remaining data is synched to disk */
    2344          15 :         if (ch->handlers->write->method == PHP_CURL_FILE && ch->handlers->write->fp) {
    2345           2 :                 fflush(ch->handlers->write->fp);
    2346             :         }
    2347          15 :         if (ch->handlers->write_header->method == PHP_CURL_FILE && ch->handlers->write_header->fp) {
    2348           0 :                 fflush(ch->handlers->write_header->fp);
    2349             :         }
    2350             : 
    2351          15 :         if (ch->handlers->write->method == PHP_CURL_RETURN) {
    2352           0 :                 RETURN_EMPTY_STRING();
    2353             :         } else {
    2354          15 :                 RETURN_TRUE;
    2355             :         }
    2356             : }
    2357             : /* }}} */
    2358             : 
    2359             : /* {{{ proto mixed curl_getinfo(resource ch [, int option])
    2360             :    Get information regarding a specific transfer */
    2361          18 : PHP_FUNCTION(curl_getinfo)
    2362             : {
    2363             :         zval            *zid;
    2364             :         php_curl        *ch;
    2365          18 :         long            option = 0;
    2366             : 
    2367          18 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zid, &option) == FAILURE) {
    2368           0 :                 return;
    2369             :         }
    2370             : 
    2371          18 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
    2372             : 
    2373          18 :         if (ZEND_NUM_ARGS() < 2) {
    2374             :                 char   *s_code;
    2375             :                 long    l_code;
    2376             :                 double  d_code;
    2377             : #if LIBCURL_VERSION_NUM >  0x071301
    2378          13 :                 struct curl_certinfo *ci = NULL;
    2379             :                 zval *listcode;
    2380             : #endif
    2381             : 
    2382          13 :                 array_init(return_value);
    2383             : 
    2384          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_EFFECTIVE_URL, &s_code) == CURLE_OK) {
    2385          13 :                         CAAS("url", s_code);
    2386             :                 }
    2387          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_TYPE, &s_code) == CURLE_OK) {
    2388          13 :                         if (s_code != NULL) {
    2389           0 :                                 CAAS("content_type", s_code);
    2390             :                         } else {
    2391             :                                 zval *retnull;
    2392          13 :                                 MAKE_STD_ZVAL(retnull);
    2393          13 :                                 ZVAL_NULL(retnull);
    2394          13 :                                 CAAZ("content_type", retnull);
    2395             :                         }
    2396             :                 }
    2397          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_HTTP_CODE, &l_code) == CURLE_OK) {
    2398          13 :                         CAAL("http_code", l_code);
    2399             :                 }
    2400          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_HEADER_SIZE, &l_code) == CURLE_OK) {
    2401          13 :                         CAAL("header_size", l_code);
    2402             :                 }
    2403          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_REQUEST_SIZE, &l_code) == CURLE_OK) {
    2404          13 :                         CAAL("request_size", l_code);
    2405             :                 }
    2406          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_FILETIME, &l_code) == CURLE_OK) {
    2407          13 :                         CAAL("filetime", l_code);
    2408             :                 }
    2409          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_SSL_VERIFYRESULT, &l_code) == CURLE_OK) {
    2410          13 :                         CAAL("ssl_verify_result", l_code);
    2411             :                 }
    2412          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_COUNT, &l_code) == CURLE_OK) {
    2413          13 :                         CAAL("redirect_count", l_code);
    2414             :                 }
    2415          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_TOTAL_TIME, &d_code) == CURLE_OK) {
    2416          13 :                         CAAD("total_time", d_code);
    2417             :                 }
    2418          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_NAMELOOKUP_TIME, &d_code) == CURLE_OK) {
    2419          13 :                         CAAD("namelookup_time", d_code);
    2420             :                 }
    2421          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_CONNECT_TIME, &d_code) == CURLE_OK) {
    2422          13 :                         CAAD("connect_time", d_code);
    2423             :                 }
    2424          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_PRETRANSFER_TIME, &d_code) == CURLE_OK) {
    2425          13 :                         CAAD("pretransfer_time", d_code);
    2426             :                 }
    2427          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_UPLOAD, &d_code) == CURLE_OK) {
    2428          13 :                         CAAD("size_upload", d_code);
    2429             :                 }
    2430          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_SIZE_DOWNLOAD, &d_code) == CURLE_OK) {
    2431          13 :                         CAAD("size_download", d_code);
    2432             :                 }
    2433          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_DOWNLOAD, &d_code) == CURLE_OK) {
    2434          13 :                         CAAD("speed_download", d_code);
    2435             :                 }
    2436          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_SPEED_UPLOAD, &d_code) == CURLE_OK) {
    2437          13 :                         CAAD("speed_upload", d_code);
    2438             :                 }
    2439          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d_code) == CURLE_OK) {
    2440          13 :                         CAAD("download_content_length", d_code);
    2441             :                 }
    2442          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_CONTENT_LENGTH_UPLOAD, &d_code) == CURLE_OK) {
    2443          13 :                         CAAD("upload_content_length", d_code);
    2444             :                 }
    2445          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_STARTTRANSFER_TIME, &d_code) == CURLE_OK) {
    2446          13 :                         CAAD("starttransfer_time", d_code);
    2447             :                 }
    2448          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_TIME, &d_code) == CURLE_OK) {
    2449          13 :                         CAAD("redirect_time", d_code);
    2450             :                 }
    2451             : #if LIBCURL_VERSION_NUM > 0x071301
    2452          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
    2453          13 :                         MAKE_STD_ZVAL(listcode);
    2454          13 :                         array_init(listcode);
    2455          13 :                         create_certinfo(ci, listcode TSRMLS_CC);
    2456          13 :                         CAAZ("certinfo", listcode);
    2457             :                 }
    2458             : #endif
    2459             : #if LIBCURL_VERSION_NUM >= 0x071202
    2460          13 :                 if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_URL, &s_code) == CURLE_OK) {
    2461          13 :                         CAAS("redirect_url", s_code);
    2462             :                 }
    2463             : #endif
    2464          13 :                 if (ch->header.str_len > 0) {
    2465           0 :                         CAAS("request_header", ch->header.str);
    2466             :                 }
    2467             :         } else {
    2468           5 :                 switch (option) {
    2469             :                         case CURLINFO_PRIVATE:
    2470             :                         case CURLINFO_EFFECTIVE_URL:
    2471             :                         case CURLINFO_CONTENT_TYPE:
    2472             : #if LIBCURL_VERSION_NUM >= 0x071202
    2473             :                         case CURLINFO_REDIRECT_URL:
    2474             : #endif
    2475             :                         {
    2476           4 :                                 char *s_code = NULL;
    2477             : 
    2478           4 :                                 if (curl_easy_getinfo(ch->cp, option, &s_code) == CURLE_OK && s_code) {
    2479           4 :                                         RETURN_STRING(s_code, 1);
    2480             :                                 } else {
    2481           0 :                                         RETURN_FALSE;
    2482             :                                 }
    2483             :                                 break;
    2484             :                         }
    2485             :                         case CURLINFO_HTTP_CODE:
    2486             :                         case CURLINFO_HEADER_SIZE:
    2487             :                         case CURLINFO_REQUEST_SIZE:
    2488             :                         case CURLINFO_FILETIME:
    2489             :                         case CURLINFO_SSL_VERIFYRESULT:
    2490             :                         case CURLINFO_REDIRECT_COUNT: {
    2491           1 :                                 long code = 0;
    2492             : 
    2493           1 :                                 if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
    2494           1 :                                         RETURN_LONG(code);
    2495             :                                 } else {
    2496           0 :                                         RETURN_FALSE;
    2497             :                                 }
    2498             :                                 break;
    2499             :                         }
    2500             :                         case CURLINFO_TOTAL_TIME:
    2501             :                         case CURLINFO_NAMELOOKUP_TIME:
    2502             :                         case CURLINFO_CONNECT_TIME:
    2503             :                         case CURLINFO_PRETRANSFER_TIME:
    2504             :                         case CURLINFO_SIZE_UPLOAD:
    2505             :                         case CURLINFO_SIZE_DOWNLOAD:
    2506             :                         case CURLINFO_SPEED_DOWNLOAD:
    2507             :                         case CURLINFO_SPEED_UPLOAD:
    2508             :                         case CURLINFO_CONTENT_LENGTH_DOWNLOAD:
    2509             :                         case CURLINFO_CONTENT_LENGTH_UPLOAD:
    2510             :                         case CURLINFO_STARTTRANSFER_TIME:
    2511             :                         case CURLINFO_REDIRECT_TIME: {
    2512           0 :                                 double code = 0.0;
    2513             : 
    2514           0 :                                 if (curl_easy_getinfo(ch->cp, option, &code) == CURLE_OK) {
    2515           0 :                                         RETURN_DOUBLE(code);
    2516             :                                 } else {
    2517           0 :                                         RETURN_FALSE;
    2518             :                                 }
    2519             :                                 break;
    2520             :                         }
    2521             :                         case CURLINFO_HEADER_OUT:
    2522           0 :                                 if (ch->header.str_len > 0) {
    2523           0 :                                         RETURN_STRINGL(ch->header.str, ch->header.str_len, 1);
    2524             :                                 } else {
    2525           0 :                                         RETURN_FALSE;
    2526             :                                 }
    2527             : #if LIBCURL_VERSION_NUM > 0x071301
    2528             :                         case CURLINFO_CERTINFO: {
    2529           0 :                                 struct curl_certinfo *ci = NULL;
    2530             : 
    2531           0 :                                 array_init(return_value);
    2532             :                                 
    2533           0 :                                 if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) {
    2534           0 :                                         create_certinfo(ci, return_value TSRMLS_CC);
    2535             :                                 } else {
    2536           0 :                                         RETURN_FALSE;
    2537             :                                 }
    2538             :                                 break;
    2539             :                         }
    2540             : #endif
    2541             :                 }
    2542             :         }
    2543             : }
    2544             : /* }}} */
    2545             : 
    2546             : /* {{{ proto string curl_error(resource ch)
    2547             :    Return a string contain the last error for the current session */
    2548           5 : PHP_FUNCTION(curl_error)
    2549             : {
    2550             :         zval            *zid;
    2551             :         php_curl        *ch;
    2552             : 
    2553           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
    2554           0 :                 return;
    2555             :         }
    2556             : 
    2557           5 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
    2558             : 
    2559           5 :         ch->err.str[CURL_ERROR_SIZE] = 0;
    2560           5 :         RETURN_STRING(ch->err.str, 1);
    2561             : }
    2562             : /* }}} */
    2563             : 
    2564             : /* {{{ proto int curl_errno(resource ch)
    2565             :    Return an integer containing the last error number */
    2566           4 : PHP_FUNCTION(curl_errno)
    2567             : {
    2568             :         zval            *zid;
    2569             :         php_curl        *ch;
    2570             : 
    2571           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
    2572           0 :                 return;
    2573             :         }
    2574             : 
    2575           4 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
    2576             : 
    2577           4 :         RETURN_LONG(ch->err.no);
    2578             : }
    2579             : /* }}} */
    2580             : 
    2581             : /* {{{ proto void curl_close(resource ch)
    2582             :    Close a cURL session */
    2583          55 : PHP_FUNCTION(curl_close)
    2584             : {
    2585             :         zval            *zid;
    2586             :         php_curl        *ch;
    2587             : 
    2588          55 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zid) == FAILURE) {
    2589           0 :                 return;
    2590             :         }
    2591             : 
    2592          55 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
    2593             : 
    2594          55 :         if (ch->in_callback) {
    2595           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to close cURL handle from a callback");
    2596           0 :                 return;
    2597             :         }
    2598             : 
    2599          55 :         if (ch->uses) {
    2600           6 :                 ch->uses--;
    2601             :         } else {
    2602          49 :                 zend_list_delete(Z_LVAL_P(zid));
    2603             :         }
    2604             : }
    2605             : /* }}} */
    2606             : 
    2607             : /* {{{ _php_curl_close()
    2608             :    List destructor for curl handles */
    2609         101 : static void _php_curl_close_ex(php_curl *ch TSRMLS_DC)
    2610             : {
    2611             : #if PHP_CURL_DEBUG
    2612             :         fprintf(stderr, "DTOR CALLED, ch = %x\n", ch);
    2613             : #endif
    2614             : 
    2615         101 :         _php_curl_verify_handlers(ch, 0 TSRMLS_CC);
    2616             : 
    2617             :         /* 
    2618             :          * Libcurl is doing connection caching. When easy handle is cleaned up,
    2619             :          * if the handle was previously used by the curl_multi_api, the connection 
    2620             :          * remains open un the curl multi handle is cleaned up. Some protocols are
    2621             :          * sending content like the FTP one, and libcurl try to use the 
    2622             :          * WRITEFUNCTION or the HEADERFUNCTION. Since structures used in those
    2623             :          * callback are freed, we need to use an other callback to which avoid
    2624             :          * segfaults.
    2625             :          *
    2626             :          * Libcurl commit d021f2e8a00 fix this issue and should be part of 7.28.2 
    2627             :          */
    2628         101 :         curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION, curl_write_nothing);
    2629         101 :         curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION, curl_write_nothing);
    2630             : 
    2631         101 :         curl_easy_cleanup(ch->cp);
    2632             : 
    2633             :         /* cURL destructors should be invoked only by last curl handle */
    2634         202 :         if (Z_REFCOUNT_P(ch->clone) <= 1) {
    2635          90 :                 zend_llist_clean(&ch->to_free->str);
    2636          90 :                 zend_llist_clean(&ch->to_free->slist);
    2637          90 :                 zend_llist_clean(&ch->to_free->post);
    2638          90 :                 efree(ch->to_free);
    2639          90 :                 FREE_ZVAL(ch->clone);
    2640             :         } else {
    2641          11 :                 Z_DELREF_P(ch->clone);
    2642             :         }
    2643             : 
    2644         101 :         if (ch->handlers->write->buf.len > 0) {
    2645          46 :                 smart_str_free(&ch->handlers->write->buf);
    2646             :         }
    2647         101 :         if (ch->handlers->write->func_name) {
    2648           2 :                 zval_ptr_dtor(&ch->handlers->write->func_name);
    2649             :         }
    2650         101 :         if (ch->handlers->read->func_name) {
    2651           1 :                 zval_ptr_dtor(&ch->handlers->read->func_name);
    2652             :         }
    2653         101 :         if (ch->handlers->write_header->func_name) {
    2654           1 :                 zval_ptr_dtor(&ch->handlers->write_header->func_name);
    2655             :         }
    2656         101 :         if (ch->handlers->progress->func_name) {
    2657           2 :                 zval_ptr_dtor(&ch->handlers->progress->func_name);
    2658             :         }
    2659         101 :         if (ch->handlers->passwd) {
    2660           0 :                 zval_ptr_dtor(&ch->handlers->passwd);
    2661             :         }
    2662         101 :         if (ch->handlers->std_err) {
    2663           4 :                 zval_ptr_dtor(&ch->handlers->std_err);
    2664             :         }
    2665         101 :         if (ch->header.str_len > 0) {
    2666           0 :                 efree(ch->header.str);
    2667             :         }
    2668             : 
    2669         101 :         if (ch->handlers->write_header->stream) {
    2670           0 :                 zval_ptr_dtor(&ch->handlers->write_header->stream);
    2671             :         }
    2672         101 :         if (ch->handlers->write->stream) {
    2673           5 :                 zval_ptr_dtor(&ch->handlers->write->stream);
    2674             :         }
    2675         101 :         if (ch->handlers->read->stream) {
    2676           2 :                 zval_ptr_dtor(&ch->handlers->read->stream);
    2677             :         }
    2678             : 
    2679         101 :         efree(ch->handlers->write);
    2680         101 :         efree(ch->handlers->write_header);
    2681         101 :         efree(ch->handlers->read);
    2682         101 :         efree(ch->handlers->progress);
    2683         101 :         efree(ch->handlers);
    2684         101 :         efree(ch);
    2685         101 : }
    2686             : /* }}} */
    2687             : 
    2688             : /* {{{ _php_curl_close()
    2689             :    List destructor for curl handles */
    2690         101 : static void _php_curl_close(zend_rsrc_list_entry *rsrc TSRMLS_DC)
    2691             : {
    2692         101 :         php_curl *ch = (php_curl *) rsrc->ptr;
    2693         101 :         _php_curl_close_ex(ch TSRMLS_CC);
    2694         101 : }
    2695             : /* }}} */
    2696             : 
    2697             : #endif /* HAVE_CURL */
    2698             : 
    2699             : /*
    2700             :  * Local variables:
    2701             :  * tab-width: 4
    2702             :  * c-basic-offset: 4
    2703             :  * End:
    2704             :  * vim600: fdm=marker
    2705             :  * vim: noet sw=4 ts=4
    2706             :  */

Generated by: LCOV version 1.10

Generated at Fri, 18 Apr 2014 07:01:25 +0000 (2 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.