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

LTP GCOV extension - code coverage report
Current view: directory - xmlrpc - xmlrpc-epi-php.c
Test: PHP Code Coverage
Date: 2009-07-02 Instrumented lines: 576
Code covered: 16.5 % Executed lines: 95
Legend: not executed executed

       1                 : /*
       2                 :   This file is part of, or distributed with, libXMLRPC - a C library for 
       3                 :   xml-encoded function calls.
       4                 : 
       5                 :   Author: Dan Libby (dan@libby.com)
       6                 :   Epinions.com may be contacted at feedback@epinions-inc.com
       7                 : */
       8                 : 
       9                 : /*  
      10                 :   Copyright 2001 Epinions, Inc. 
      11                 : 
      12                 :   Subject to the following 3 conditions, Epinions, Inc.  permits you, free 
      13                 :   of charge, to (a) use, copy, distribute, modify, perform and display this 
      14                 :   software and associated documentation files (the "Software"), and (b) 
      15                 :   permit others to whom the Software is furnished to do so as well.  
      16                 : 
      17                 :   1) The above copyright notice and this permission notice shall be included 
      18                 :   without modification in all copies or substantial portions of the 
      19                 :   Software.  
      20                 : 
      21                 :   2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF 
      22                 :   ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY 
      23                 :   IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR 
      24                 :   PURPOSE OR NONINFRINGEMENT.  
      25                 : 
      26                 :   3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, 
      27                 :   SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT 
      28                 :   OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING 
      29                 :   NEGLIGENCE), EVEN IF EPINIONS, INC.  IS AWARE OF THE POSSIBILITY OF SUCH 
      30                 :   DAMAGES.    
      31                 : 
      32                 : */
      33                 : 
      34                 : /* auto-generated portions of this file are also subject to the php license */
      35                 : 
      36                 : /*
      37                 :    +----------------------------------------------------------------------+
      38                 :    | PHP Version 5                                                        |
      39                 :    +----------------------------------------------------------------------+
      40                 :    | Copyright (c) 1997-2009 The PHP Group                                |
      41                 :    +----------------------------------------------------------------------+
      42                 :    | This source file is subject to version 3.01 of the PHP license,      |
      43                 :    | that is bundled with this package in the file LICENSE, and is        |
      44                 :    | available through the world-wide-web at the following url:           |
      45                 :    | http://www.php.net/license/3_01.txt                                  |
      46                 :    | If you did not receive a copy of the PHP license and are unable to   |
      47                 :    | obtain it through the world-wide-web, please send a note to          |
      48                 :    | license@php.net so we can mail you a copy immediately.               |
      49                 :    +----------------------------------------------------------------------+
      50                 :    | Author: Dan Libby                                                    |
      51                 :    +----------------------------------------------------------------------+
      52                 :  */
      53                 : 
      54                 : /* $Id: xmlrpc-epi-php.c,v 1.39.2.5.2.17 2009/05/22 12:50:44 felipe Exp $ */
      55                 : 
      56                 : /**********************************************************************
      57                 : * BUGS:                                                               *
      58                 : *  - when calling a php user function, there appears to be no way to  *
      59                 : *    distinguish between a return value of null, and no return value  *
      60                 : *    at all.  The xml serialization layer(s) will then return a value *
      61                 : *    of null, when the right thing may be no value at all. (SOAP)     *
      62                 : **********************************************************************/
      63                 : 
      64                 : #ifdef HAVE_CONFIG_H
      65                 : #include "config.h"
      66                 : #endif
      67                 : 
      68                 : #include "php.h"
      69                 : #include "ext/standard/info.h"
      70                 : #include "ext/date/php_date.h"
      71                 : #include "php_ini.h"
      72                 : #include "php_xmlrpc.h"
      73                 : #include "xmlrpc.h"
      74                 : 
      75                 : #define PHP_EXT_VERSION "0.51"
      76                 : 
      77                 : static int le_xmlrpc_server;
      78                 : 
      79                 : zend_function_entry xmlrpc_functions[] = {
      80                 :         PHP_FE(xmlrpc_encode,                                                                   NULL) 
      81                 :         PHP_FE(xmlrpc_decode,                                                                   NULL)
      82                 :         PHP_FE(xmlrpc_decode_request,                                                   second_arg_force_ref)
      83                 :         PHP_FE(xmlrpc_encode_request,                                                   NULL)
      84                 :         PHP_FE(xmlrpc_get_type,                                                                 NULL)
      85                 :         PHP_FE(xmlrpc_set_type,                                                                 first_arg_force_ref)
      86                 :         PHP_FE(xmlrpc_is_fault,                                                                 NULL)
      87                 :         PHP_FE(xmlrpc_server_create,                                                    NULL)
      88                 :         PHP_FE(xmlrpc_server_destroy,                                                   NULL)
      89                 :         PHP_FE(xmlrpc_server_register_method,                                   NULL)
      90                 :         PHP_FE(xmlrpc_server_call_method,                                               NULL)
      91                 :         PHP_FE(xmlrpc_parse_method_descriptions,                                NULL)
      92                 :         PHP_FE(xmlrpc_server_add_introspection_data,                    NULL)
      93                 :         PHP_FE(xmlrpc_server_register_introspection_callback,   NULL)
      94                 :         {NULL, NULL, NULL}
      95                 : };
      96                 : 
      97                 : zend_module_entry xmlrpc_module_entry = {
      98                 :         STANDARD_MODULE_HEADER,
      99                 :         "xmlrpc",
     100                 :         xmlrpc_functions,
     101                 :         PHP_MINIT(xmlrpc),
     102                 :         NULL,
     103                 :         NULL,
     104                 :         NULL,
     105                 :         PHP_MINFO(xmlrpc),
     106                 :         PHP_EXT_VERSION,
     107                 :         STANDARD_MODULE_PROPERTIES
     108                 : };
     109                 : 
     110                 : #ifdef COMPILE_DL_XMLRPC
     111                 : ZEND_GET_MODULE(xmlrpc)
     112                 : # ifdef PHP_WIN32
     113                 : # include "zend_arg_defs.c"
     114                 : # endif
     115                 : #endif
     116                 : 
     117                 : /*******************************
     118                 : * local structures and defines *
     119                 : *******************************/
     120                 : 
     121                 : /* per server data */
     122                 : typedef struct _xmlrpc_server_data {
     123                 :         zval* method_map;
     124                 :         zval* introspection_map;
     125                 :         XMLRPC_SERVER server_ptr;
     126                 : } xmlrpc_server_data;
     127                 : 
     128                 : 
     129                 : /* how to format output */
     130                 : typedef struct _php_output_options {
     131                 :         int b_php_out;
     132                 :         int b_auto_version;
     133                 :         STRUCT_XMLRPC_REQUEST_OUTPUT_OPTIONS xmlrpc_out;
     134                 : } php_output_options;
     135                 : 
     136                 : /* data passed to C callback */
     137                 : typedef struct _xmlrpc_callback_data {
     138                 :         zval* xmlrpc_method;
     139                 :         zval* php_function;
     140                 :         zval* caller_params;
     141                 :         zval* return_data;
     142                 :         xmlrpc_server_data* server;
     143                 :         char php_executed;
     144                 : } xmlrpc_callback_data;
     145                 : 
     146                 : /* output options */
     147                 : #define OUTPUT_TYPE_KEY       "output_type"
     148                 : #define OUTPUT_TYPE_KEY_LEN   (sizeof(OUTPUT_TYPE_KEY) - 1)
     149                 : #define OUTPUT_TYPE_VALUE_PHP "php"
     150                 : #define OUTPUT_TYPE_VALUE_XML "xml"
     151                 : 
     152                 : #define VERBOSITY_KEY                  "verbosity"
     153                 : #define VERBOSITY_KEY_LEN              (sizeof(VERBOSITY_KEY) - 1)
     154                 : #define VERBOSITY_VALUE_NO_WHITE_SPACE "no_white_space"
     155                 : #define VERBOSITY_VALUE_NEWLINES_ONLY  "newlines_only"
     156                 : #define VERBOSITY_VALUE_PRETTY         "pretty"
     157                 : 
     158                 : #define ESCAPING_KEY             "escaping"
     159                 : #define ESCAPING_KEY_LEN         (sizeof(ESCAPING_KEY) - 1)
     160                 : #define ESCAPING_VALUE_CDATA     "cdata"
     161                 : #define ESCAPING_VALUE_NON_ASCII "non-ascii"
     162                 : #define ESCAPING_VALUE_NON_PRINT "non-print"
     163                 : #define ESCAPING_VALUE_MARKUP    "markup"
     164                 : 
     165                 : #define VERSION_KEY          "version"
     166                 : #define VERSION_KEY_LEN      (sizeof(VERSION_KEY) - 1)
     167                 : #define VERSION_VALUE_SIMPLE "simple"
     168                 : #define VERSION_VALUE_XMLRPC "xmlrpc"
     169                 : #define VERSION_VALUE_SOAP11 "soap 1.1"
     170                 : #define VERSION_VALUE_AUTO   "auto"
     171                 : 
     172                 : #define ENCODING_KEY     "encoding"
     173                 : #define ENCODING_KEY_LEN (sizeof(ENCODING_KEY) - 1)
     174                 : #define ENCODING_DEFAULT "iso-8859-1"
     175                 : 
     176                 : /* value types */
     177                 : #define OBJECT_TYPE_ATTR  "xmlrpc_type"
     178                 : #define OBJECT_VALUE_ATTR "scalar"
     179                 : #define OBJECT_VALUE_TS_ATTR "timestamp"
     180                 : 
     181                 : /* faults */
     182                 : #define FAULT_CODE       "faultCode"
     183                 : #define FAULT_CODE_LEN   (sizeof(FAULT_CODE) - 1)
     184                 : #define FAULT_STRING     "faultString"
     185                 : #define FAULT_STRING_LEN (sizeof(FAULT_STRING) - 1)
     186                 : 
     187                 : /***********************
     188                 : * forward declarations *
     189                 : ***********************/
     190                 : XMLRPC_VALUE_TYPE get_zval_xmlrpc_type(zval* value, zval** newvalue);
     191                 : static void php_xmlrpc_introspection_callback(XMLRPC_SERVER server, void* data);
     192                 : int sset_zval_xmlrpc_type(zval* value, XMLRPC_VALUE_TYPE type);
     193                 : zval* decode_request_worker(zval* xml_in, zval* encoding_in, zval* method_name_out);
     194                 : const char* xmlrpc_type_as_str(XMLRPC_VALUE_TYPE type, XMLRPC_VECTOR_TYPE vtype);
     195                 : XMLRPC_VALUE_TYPE xmlrpc_str_as_type(const char* str);
     196                 : XMLRPC_VECTOR_TYPE xmlrpc_str_as_vector_type(const char* str);
     197                 : int set_zval_xmlrpc_type(zval* value, XMLRPC_VALUE_TYPE type);
     198                 : 
     199                 : /*********************
     200                 : * startup / shutdown *
     201                 : *********************/
     202                 : 
     203                 : static void destroy_server_data(xmlrpc_server_data *server)
     204               0 : {
     205               0 :         if (server) {
     206               0 :                 XMLRPC_ServerDestroy(server->server_ptr);
     207                 : 
     208               0 :                 zval_dtor(server->method_map);
     209               0 :                 FREE_ZVAL(server->method_map);
     210                 : 
     211               0 :                 zval_dtor(server->introspection_map);
     212               0 :                 FREE_ZVAL(server->introspection_map);
     213                 : 
     214               0 :                 efree(server);
     215                 :         }
     216               0 : }
     217                 : 
     218                 : /* called when server is being destructed. either when xmlrpc_server_destroy
     219                 :  * is called, or when request ends.  */
     220                 : static void xmlrpc_server_destructor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
     221               0 : {
     222               0 :         if (rsrc && rsrc->ptr) {
     223               0 :                 destroy_server_data((xmlrpc_server_data*) rsrc->ptr);
     224                 :         }
     225               0 : }
     226                 : 
     227                 : /* module init */
     228                 : PHP_MINIT_FUNCTION(xmlrpc)
     229            9357 : {
     230            9357 :         le_xmlrpc_server = zend_register_list_destructors_ex(xmlrpc_server_destructor, NULL, "xmlrpc server", module_number);
     231                 : 
     232            9357 :         return SUCCESS;
     233                 : }
     234                 : 
     235                 : /* display info in phpinfo() */
     236                 : PHP_MINFO_FUNCTION(xmlrpc)
     237               3 : {
     238               3 :         php_info_print_table_start();
     239               3 :         php_info_print_table_row(2, "core library version", XMLRPC_GetVersionString());
     240               3 :         php_info_print_table_row(2, "php extension version", PHP_EXT_VERSION);
     241               3 :         php_info_print_table_row(2, "author", "Dan Libby");
     242               3 :         php_info_print_table_row(2, "homepage", "http://xmlrpc-epi.sourceforge.net");
     243               3 :         php_info_print_table_row(2, "open sourced by", "Epinions.com");
     244               3 :         php_info_print_table_end();
     245               3 : }
     246                 : 
     247                 : /*******************
     248                 : * random utilities *
     249                 : *******************/
     250                 : 
     251                 : /* Utility functions for adding data types to arrays, with or without key (assoc, non-assoc).
     252                 :  * Could easily be further generalized to work with objects.
     253                 :  */
     254                 : #if 0
     255                 : static int add_long(zval* list, char* id, int num) {
     256                 :         if(id) return add_assoc_long(list, id, num);
     257                 :         else   return add_next_index_long(list, num);
     258                 : }
     259                 : 
     260                 : static int add_double(zval* list, char* id, double num) {
     261                 :         if(id) return add_assoc_double(list, id, num);
     262                 :         else   return add_next_index_double(list, num);
     263                 : }
     264                 : 
     265                 : static int add_string(zval* list, char* id, char* string, int duplicate) {
     266                 :         if(id) return add_assoc_string(list, id, string, duplicate);
     267                 :         else   return add_next_index_string(list, string, duplicate);
     268                 : }
     269                 : 
     270                 : static int add_stringl(zval* list, char* id, char* string, uint length, int duplicate) {
     271                 :         if(id) return add_assoc_stringl(list, id, string, length, duplicate);
     272                 :         else   return add_next_index_stringl(list, string, length, duplicate);
     273                 : }
     274                 : 
     275                 : #endif
     276                 : 
     277                 : static int add_zval(zval* list, const char* id, zval** val)
     278               0 : {
     279               0 :         if (list && val) {
     280               0 :                 if (id) {
     281               0 :                         int id_len = strlen(id);
     282               0 :                         if (!(id_len > 1 && id[0] == '0') && is_numeric_string((char *)id, id_len, NULL, NULL, 0) == IS_LONG) {
     283               0 :                                 long index = strtol(id, NULL, 0);
     284               0 :                                 return zend_hash_index_update(Z_ARRVAL_P(list), index, (void *) val, sizeof(zval **), NULL);
     285                 :                         } else {
     286               0 :                                 return zend_hash_update(Z_ARRVAL_P(list), (char*) id, strlen(id) + 1, (void *) val, sizeof(zval **), NULL);
     287                 :                         }
     288                 :                 } else {
     289               0 :                         return zend_hash_next_index_insert(Z_ARRVAL_P(list), (void *) val, sizeof(zval **), NULL); 
     290                 :                 }
     291                 :         }
     292                 :         /* what is the correct return on error? */
     293               0 :         return 0;
     294                 : }
     295                 : 
     296                 : #define my_zend_hash_get_current_key(ht, my_key, num_index) zend_hash_get_current_key(ht, my_key, num_index, 0)
     297                 : 
     298                 : 
     299                 : /*************************
     300                 : * input / output options *
     301                 : *************************/
     302                 : 
     303                 : /* parse an array (user input) into output options suitable for use by xmlrpc engine
     304                 :  * and determine whether to return data as xml or php vars */
     305                 : static void set_output_options(php_output_options* options, zval* output_opts)
     306               1 : {
     307               1 :         if (options) {
     308                 : 
     309                 :                 /* defaults */
     310               1 :                 options->b_php_out = 0;
     311               1 :                 options->b_auto_version = 1;
     312               1 :                 options->xmlrpc_out.version = xmlrpc_version_1_0;
     313               1 :                 options->xmlrpc_out.xml_elem_opts.encoding = ENCODING_DEFAULT;
     314               1 :                 options->xmlrpc_out.xml_elem_opts.verbosity = xml_elem_pretty;
     315               1 :                 options->xmlrpc_out.xml_elem_opts.escaping = xml_elem_markup_escaping | xml_elem_non_ascii_escaping | xml_elem_non_print_escaping;
     316                 : 
     317               1 :                 if (output_opts && Z_TYPE_P(output_opts) == IS_ARRAY) {
     318                 :                         zval** val;
     319                 : 
     320                 :                         /* type of output (xml/php) */
     321               0 :                         if (zend_hash_find(Z_ARRVAL_P(output_opts), OUTPUT_TYPE_KEY, OUTPUT_TYPE_KEY_LEN + 1, (void**) &val) == SUCCESS) {
     322               0 :                                 if (Z_TYPE_PP(val) == IS_STRING) {
     323               0 :                                         if (!strcmp(Z_STRVAL_PP(val), OUTPUT_TYPE_VALUE_PHP)) {
     324               0 :                                                 options->b_php_out = 1;
     325               0 :                                         } else if (!strcmp(Z_STRVAL_PP(val), OUTPUT_TYPE_VALUE_XML)) {
     326               0 :                                                 options->b_php_out = 0;
     327                 :                                         }
     328                 :                                 }
     329                 :                         }
     330                 : 
     331                 :                         /* verbosity of generated xml */
     332               0 :                         if (zend_hash_find(Z_ARRVAL_P(output_opts), VERBOSITY_KEY, VERBOSITY_KEY_LEN + 1, (void**) &val) == SUCCESS) {
     333               0 :                                 if (Z_TYPE_PP(val) == IS_STRING) {
     334               0 :                                         if (!strcmp(Z_STRVAL_PP(val), VERBOSITY_VALUE_NO_WHITE_SPACE)) {
     335               0 :                                                 options->xmlrpc_out.xml_elem_opts.verbosity = xml_elem_no_white_space;
     336               0 :                                         } else if (!strcmp(Z_STRVAL_PP(val), VERBOSITY_VALUE_NEWLINES_ONLY)) {
     337               0 :                                                 options->xmlrpc_out.xml_elem_opts.verbosity = xml_elem_newlines_only;
     338               0 :                                         } else if (!strcmp(Z_STRVAL_PP(val), VERBOSITY_VALUE_PRETTY)) {
     339               0 :                                                 options->xmlrpc_out.xml_elem_opts.verbosity = xml_elem_pretty;
     340                 :                                         }
     341                 :                                 }
     342                 :                         }
     343                 : 
     344                 :                         /* version of xml to output */
     345               0 :                         if (zend_hash_find(Z_ARRVAL_P(output_opts), VERSION_KEY, VERSION_KEY_LEN + 1, (void**) &val) == SUCCESS) {
     346               0 :                                 if (Z_TYPE_PP(val) == IS_STRING) {
     347               0 :                                         options->b_auto_version = 0;
     348               0 :                                         if (!strcmp(Z_STRVAL_PP(val), VERSION_VALUE_XMLRPC)) {
     349               0 :                                                 options->xmlrpc_out.version = xmlrpc_version_1_0;
     350               0 :                                         } else if (!strcmp(Z_STRVAL_PP(val), VERSION_VALUE_SIMPLE)) {
     351               0 :                                                 options->xmlrpc_out.version = xmlrpc_version_simple;
     352               0 :                                         } else if (!strcmp((*val)->value.str.val, VERSION_VALUE_SOAP11)) {
     353               0 :                                                         options->xmlrpc_out.version = xmlrpc_version_soap_1_1;
     354                 :                                         } else { /* if(!strcmp((*val)->value.str.val, VERSION_VALUE_AUTO)) { */
     355               0 :                                                         options->b_auto_version = 1;
     356                 :                                         }
     357                 :                                 }
     358                 :                         }
     359                 : 
     360                 :                         /* encoding code set */
     361               0 :                         if(zend_hash_find(Z_ARRVAL_P(output_opts), ENCODING_KEY, ENCODING_KEY_LEN + 1, (void**)&val) == SUCCESS) {
     362               0 :                                 if(Z_TYPE_PP(val) == IS_STRING) {
     363               0 :                                         options->xmlrpc_out.xml_elem_opts.encoding = estrdup(Z_STRVAL_PP(val));
     364                 :                                 }
     365                 :                         }
     366                 : 
     367                 :                         /* escaping options */
     368               0 :                         if (zend_hash_find(Z_ARRVAL_P(output_opts), ESCAPING_KEY, ESCAPING_KEY_LEN + 1, (void**)&val) == SUCCESS) {
     369                 :                                 /* multiple values allowed.  check if array */
     370               0 :                                 if (Z_TYPE_PP(val) == IS_ARRAY) {
     371                 :                                         zval** iter_val;
     372               0 :                                         zend_hash_internal_pointer_reset(Z_ARRVAL_PP(val));
     373               0 :                                         options->xmlrpc_out.xml_elem_opts.escaping = xml_elem_no_escaping;
     374                 :                                         while(1) {
     375               0 :                                                 if(zend_hash_get_current_data(Z_ARRVAL_PP(val), (void**)&iter_val) == SUCCESS) {
     376               0 :                                                         if(Z_TYPE_PP(iter_val) == IS_STRING && Z_STRVAL_PP(iter_val)) {
     377               0 :                                                                 if(!strcmp(Z_STRVAL_PP(iter_val), ESCAPING_VALUE_CDATA)) {
     378               0 :                                                                         options->xmlrpc_out.xml_elem_opts.escaping |= xml_elem_cdata_escaping;
     379                 :                                                                 }
     380               0 :                                                                 else if(!strcmp(Z_STRVAL_PP(iter_val), ESCAPING_VALUE_NON_ASCII)) {
     381               0 :                                                                         options->xmlrpc_out.xml_elem_opts.escaping |= xml_elem_non_ascii_escaping;
     382                 :                                                                 }
     383               0 :                                                                 else if(!strcmp(Z_STRVAL_PP(iter_val), ESCAPING_VALUE_NON_PRINT)) {
     384               0 :                                                                         options->xmlrpc_out.xml_elem_opts.escaping |= xml_elem_non_print_escaping;
     385                 :                                                                 }
     386               0 :                                                                 else if(!strcmp(Z_STRVAL_PP(iter_val), ESCAPING_VALUE_MARKUP)) {
     387               0 :                                                                         options->xmlrpc_out.xml_elem_opts.escaping |= xml_elem_markup_escaping;
     388                 :                                                                 }
     389                 :                                                         }
     390                 :                                                 }
     391                 :                                                 else {
     392               0 :                                                         break;
     393                 :                                                 }
     394                 : 
     395               0 :                                                 zend_hash_move_forward(Z_ARRVAL_PP(val));
     396               0 :                                         }
     397                 :                                 }
     398                 :                                 /* else, check for single value */
     399               0 :                                 else if(Z_TYPE_PP(val) == IS_STRING) {
     400               0 :                                         if(!strcmp(Z_STRVAL_PP(val), ESCAPING_VALUE_CDATA)) {
     401               0 :                                                 options->xmlrpc_out.xml_elem_opts.escaping = xml_elem_cdata_escaping;
     402                 :                                         }
     403               0 :                                         else if(!strcmp(Z_STRVAL_PP(val), ESCAPING_VALUE_NON_ASCII)) {
     404               0 :                                                 options->xmlrpc_out.xml_elem_opts.escaping = xml_elem_non_ascii_escaping;
     405                 :                                         }
     406               0 :                                         else if(!strcmp(Z_STRVAL_PP(val), ESCAPING_VALUE_NON_PRINT)) {
     407               0 :                                                 options->xmlrpc_out.xml_elem_opts.escaping = xml_elem_non_print_escaping;
     408                 :                                         }
     409               0 :                                         else if(!strcmp(Z_STRVAL_PP(val), ESCAPING_VALUE_MARKUP)) {
     410               0 :                                                 options->xmlrpc_out.xml_elem_opts.escaping = xml_elem_markup_escaping;
     411                 :                                         }
     412                 :                                 }
     413                 :                         }
     414                 :                 }
     415                 :         }
     416               1 : }
     417                 : 
     418                 : 
     419                 : /******************
     420                 : * encode / decode *
     421                 : ******************/
     422                 : 
     423                 : /* php arrays have no distinction between array and struct types.
     424                 :  * they even allow mixed.  Thus, we determine the type by iterating
     425                 :  * through the entire array and figuring out each element.
     426                 :  * room for some optimation here if we stop after a specific # of elements.
     427                 :  */
     428                 : static XMLRPC_VECTOR_TYPE determine_vector_type (HashTable *ht)
     429               1 : {
     430               1 :         int bArray = 0, bStruct = 0, bMixed = 0;
     431                 :         unsigned long num_index;
     432                 :         char* my_key;
     433                 : 
     434               1 :         zend_hash_internal_pointer_reset(ht);
     435                 :         while(1) {
     436               2 :                 int res = my_zend_hash_get_current_key(ht, &my_key, &num_index);
     437               2 :                 if(res == HASH_KEY_IS_LONG) {
     438               0 :                         if(bStruct) {
     439               0 :                                 bMixed = 1;
     440               0 :                                 break;
     441                 :                         }
     442               0 :                         bArray = 1;
     443                 :                 }
     444               2 :                 else if(res == HASH_KEY_NON_EXISTANT) {
     445               1 :                         break;
     446                 :                 }
     447               1 :                 else if(res == HASH_KEY_IS_STRING) {
     448               1 :                         if(bArray) {
     449               0 :                                 bMixed = 1;
     450               0 :                                 break;
     451                 :                         }
     452               1 :                         bStruct = 1;
     453                 :                 }
     454                 : 
     455               1 :                 zend_hash_move_forward(ht);
     456               1 :         }
     457               1 :     return bMixed ? xmlrpc_vector_mixed : (bStruct ? xmlrpc_vector_struct : xmlrpc_vector_array);
     458                 : }
     459                 : 
     460                 : /* recursively convert php values into xmlrpc values */
     461                 : static XMLRPC_VALUE PHP_to_XMLRPC_worker (const char* key, zval* in_val, int depth TSRMLS_DC)
     462               2 : {
     463               2 :         XMLRPC_VALUE xReturn = NULL;
     464               2 :         if(in_val) {
     465               2 :                 zval* val = NULL;
     466               2 :                 XMLRPC_VALUE_TYPE type = get_zval_xmlrpc_type(in_val, &val);
     467               2 :                 if(val) {
     468               2 :                         switch(type) {
     469                 :                                 case xmlrpc_base64:
     470               0 :                                         if(Z_TYPE_P(val) == IS_NULL) {
     471               0 :                                                 xReturn = XMLRPC_CreateValueEmpty();
     472               0 :                                                 XMLRPC_SetValueID(xReturn, key, 0);
     473                 :                                         } else {
     474               0 :                                                 xReturn = XMLRPC_CreateValueBase64(key, Z_STRVAL_P(val), Z_STRLEN_P(val));
     475                 :                                         }
     476               0 :                                         break;
     477                 :                                 case xmlrpc_datetime:
     478               0 :                                         convert_to_string(val);
     479               0 :                                         xReturn = XMLRPC_CreateValueDateTime_ISO8601(key, Z_STRVAL_P(val));
     480               0 :                                         break;
     481                 :                                 case xmlrpc_boolean:
     482               0 :                                         convert_to_boolean(val);
     483               0 :                                         xReturn = XMLRPC_CreateValueBoolean(key, Z_LVAL_P(val));
     484               0 :                                         break;
     485                 :                                 case xmlrpc_int:
     486               0 :                                         convert_to_long(val);
     487               0 :                                         xReturn = XMLRPC_CreateValueInt(key, Z_LVAL_P(val));
     488               0 :                                         break;
     489                 :                                 case xmlrpc_double:
     490               0 :                                         convert_to_double(val);
     491               0 :                                         xReturn = XMLRPC_CreateValueDouble(key, Z_DVAL_P(val));
     492               0 :                                 break;
     493                 :                                 case xmlrpc_string:
     494               1 :                                         convert_to_string(val);
     495               1 :                                         xReturn = XMLRPC_CreateValueString(key, Z_STRVAL_P(val), Z_STRLEN_P(val));
     496               1 :                                 break;
     497                 :                                 case xmlrpc_vector:     {
     498                 :                                         unsigned long num_index;
     499                 :                                         zval** pIter;
     500                 :                                         char* my_key;
     501               1 :                                         HashTable *ht = NULL;
     502                 : 
     503               1 :                                         ht = HASH_OF(val);
     504               1 :                                         if (ht && ht->nApplyCount > 1) {
     505               0 :                                                 php_error_docref(NULL TSRMLS_CC, E_ERROR, "XML-RPC doesn't support circular references");
     506               0 :                                                 return NULL;
     507                 :                                         }
     508                 : 
     509               1 :                                         convert_to_array(val);
     510               1 :                                         xReturn = XMLRPC_CreateVector(key, determine_vector_type(Z_ARRVAL_P(val)));
     511                 : 
     512               1 :                                         zend_hash_internal_pointer_reset(Z_ARRVAL_P(val));
     513               3 :                                         while(zend_hash_get_current_data(Z_ARRVAL_P(val), (void**)&pIter) == SUCCESS) {
     514               1 :                                                 int res = my_zend_hash_get_current_key(Z_ARRVAL_P(val), &my_key, &num_index);
     515                 :                     
     516               1 :                                                 switch (res) {
     517                 :                                                         case HASH_KEY_NON_EXISTANT:
     518               0 :                                                         break;
     519                 :                                                         case HASH_KEY_IS_STRING:
     520                 :                                                         case HASH_KEY_IS_LONG:
     521               1 :                                                                 ht = HASH_OF(*pIter);
     522               1 :                                                                 if (ht) {
     523               0 :                                                                         ht->nApplyCount++;
     524                 :                                                                 }
     525               1 :                                                                 if (res == HASH_KEY_IS_LONG) {
     526               0 :                                                                         XMLRPC_AddValueToVector(xReturn, PHP_to_XMLRPC_worker(0, *pIter, depth++ TSRMLS_CC));
     527                 :                                                                 } else {
     528               1 :                                                                         XMLRPC_AddValueToVector(xReturn, PHP_to_XMLRPC_worker(my_key, *pIter, depth++ TSRMLS_CC));
     529                 :                                                                 }
     530               1 :                                                                 if (ht) {
     531               0 :                                                                         ht->nApplyCount--;
     532                 :                                                                 }
     533                 :                                                                 break;
     534                 :                                                 }
     535               1 :                                                 zend_hash_move_forward(Z_ARRVAL_P(val));
     536                 :                                         }
     537                 :                                 }
     538                 :                                 break;
     539                 :                         default:
     540                 :                                 break;
     541                 :                         }
     542                 :                 }
     543                 :         }
     544               2 :    return xReturn;
     545                 : }
     546                 : 
     547                 : static XMLRPC_VALUE PHP_to_XMLRPC(zval* root_val TSRMLS_DC)
     548               1 : {
     549               1 :         return PHP_to_XMLRPC_worker(NULL, root_val, 0 TSRMLS_CC);
     550                 : }
     551                 : 
     552                 : /* recursively convert xmlrpc values into php values */
     553                 : static zval* XMLRPC_to_PHP(XMLRPC_VALUE el)
     554               0 : {
     555               0 :         zval* elem = NULL;
     556                 :         const char* pStr;
     557                 : 
     558               0 :         if(el) {
     559               0 :                 XMLRPC_VALUE_TYPE type = XMLRPC_GetValueType(el);
     560                 : 
     561               0 :                 MAKE_STD_ZVAL(elem); /* init. very important.  spent a frustrating day finding this out. */
     562                 : 
     563               0 :                 switch(type) {
     564                 :                         case xmlrpc_empty:
     565               0 :                                 Z_TYPE_P(elem) = IS_NULL;
     566               0 :                                 break;
     567                 :                         case xmlrpc_string:
     568               0 :                                 pStr = XMLRPC_GetValueString(el);
     569               0 :                                 if(pStr) {
     570               0 :                                         Z_STRLEN_P(elem) = XMLRPC_GetValueStringLen(el);
     571               0 :                                         Z_STRVAL_P(elem) = estrndup(pStr, Z_STRLEN_P(elem));
     572               0 :                                         Z_TYPE_P(elem) = IS_STRING;
     573                 :                                 }
     574               0 :                                 break;
     575                 :                         case xmlrpc_int:
     576               0 :                                 Z_LVAL_P(elem) = XMLRPC_GetValueInt(el);
     577               0 :                                 Z_TYPE_P(elem) = IS_LONG;
     578               0 :                                 break;
     579                 :                         case xmlrpc_boolean:
     580               0 :                                 Z_LVAL_P(elem) = XMLRPC_GetValueBoolean(el);
     581               0 :                                 Z_TYPE_P(elem) = IS_BOOL;
     582               0 :                                 break;
     583                 :                         case xmlrpc_double:
     584               0 :                 Z_DVAL_P(elem) = XMLRPC_GetValueDouble(el);
     585               0 :                 Z_TYPE_P(elem) = IS_DOUBLE;
     586               0 :                 break;
     587                 :                          case xmlrpc_datetime:
     588               0 :                                 Z_STRLEN_P(elem) = XMLRPC_GetValueStringLen(el);
     589               0 :                                 Z_STRVAL_P(elem) = estrndup(XMLRPC_GetValueDateTime_ISO8601(el), Z_STRLEN_P(elem));
     590               0 :                                 Z_TYPE_P(elem) = IS_STRING;
     591               0 :                                 break;
     592                 :                          case xmlrpc_base64:
     593               0 :                                 pStr = XMLRPC_GetValueBase64(el);
     594               0 :                                 if(pStr) {
     595               0 :                                    Z_STRLEN_P(elem) = XMLRPC_GetValueStringLen(el);
     596               0 :                                    Z_STRVAL_P(elem) = estrndup(pStr, Z_STRLEN_P(elem));
     597               0 :                                    Z_TYPE_P(elem) = IS_STRING;
     598                 :                                 }
     599               0 :                                 break;
     600                 :                 case xmlrpc_vector:
     601               0 :                                 array_init(elem);
     602                 :                                 {
     603               0 :                                         XMLRPC_VALUE xIter = XMLRPC_VectorRewind(el);
     604                 : 
     605               0 :                                         while( xIter ) {
     606               0 :                                                 zval *val = XMLRPC_to_PHP(xIter);
     607               0 :                                                 if (val) {
     608               0 :                                                         add_zval(elem, XMLRPC_GetValueID(xIter), &val);
     609                 :                                                 }
     610               0 :                                                 xIter = XMLRPC_VectorNext(el);
     611                 :                                         }
     612                 :                                 }
     613                 :                                 break;
     614                 :                 default:
     615                 :                 break;
     616                 :         }
     617               0 :                 set_zval_xmlrpc_type(elem, type);
     618                 :         }
     619               0 :         return elem;
     620                 : }
     621                 : 
     622                 : /* {{{ proto string xmlrpc_encode_request(string method, mixed params)
     623                 :    Generates XML for a method request */
     624                 : PHP_FUNCTION(xmlrpc_encode_request)
     625               1 : {
     626               1 :         XMLRPC_REQUEST xRequest = NULL;
     627                 :         zval **method, **vals, **out_opts;
     628                 :         char* outBuf;
     629                 :         php_output_options out;
     630                 : 
     631               1 :         if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > 3 || (zend_get_parameters_ex(ZEND_NUM_ARGS(), &method, &vals, &out_opts) == FAILURE)) {
     632               0 :                 WRONG_PARAM_COUNT; /* prints/logs a warning and returns */
     633                 :         }
     634                 : 
     635               1 :         set_output_options(&out, (ZEND_NUM_ARGS() == 3) ? *out_opts : 0);
     636                 : 
     637               1 :         if(return_value_used) {
     638               1 :                 xRequest = XMLRPC_RequestNew();
     639                 : 
     640               1 :                 if(xRequest) {
     641               1 :                         XMLRPC_RequestSetOutputOptions(xRequest, &out.xmlrpc_out);
     642               1 :                         if (Z_TYPE_PP(method) == IS_NULL) {
     643               0 :                                 XMLRPC_RequestSetRequestType(xRequest, xmlrpc_request_response);
     644                 :                         } else {
     645               1 :                                 convert_to_string_ex(method);
     646               1 :                                 XMLRPC_RequestSetMethodName(xRequest, Z_STRVAL_PP(method));
     647               1 :                                 XMLRPC_RequestSetRequestType(xRequest, xmlrpc_request_call);
     648                 :                         }
     649               1 :                         if (Z_TYPE_PP(vals) != IS_NULL) {
     650               1 :                                 XMLRPC_RequestSetData(xRequest, PHP_to_XMLRPC(*vals TSRMLS_CC));
     651                 :                         }
     652                 : 
     653               1 :                         outBuf = XMLRPC_REQUEST_ToXML(xRequest, 0);
     654               1 :                         if(outBuf) {
     655               1 :                                 RETVAL_STRING(outBuf, 1);
     656               1 :                                 free(outBuf);
     657                 :                         }
     658               1 :                         XMLRPC_RequestFree(xRequest, 1);
     659                 :                 }
     660                 :         }
     661                 :         
     662               1 :         if (strcmp(out.xmlrpc_out.xml_elem_opts.encoding, ENCODING_DEFAULT) != 0) {
     663               0 :                 efree((char *)out.xmlrpc_out.xml_elem_opts.encoding);
     664                 :         }
     665                 : }
     666                 : /* }}} */
     667                 : 
     668                 : /* {{{ proto string xmlrpc_encode(mixed value)
     669                 :    Generates XML for a PHP value */
     670                 : PHP_FUNCTION(xmlrpc_encode)
     671               0 : {
     672               0 :         XMLRPC_VALUE xOut = NULL;
     673                 :         zval **arg1;
     674                 :         char *outBuf;
     675                 : 
     676               0 :         if (ZEND_NUM_ARGS() != 1 || (zend_get_parameters_ex(1, &arg1) == FAILURE)) {
     677               0 :                 WRONG_PARAM_COUNT;
     678                 :         }
     679                 : 
     680               0 :         if( return_value_used ) {
     681                 :                 /* convert native php type to xmlrpc type */
     682               0 :                 xOut = PHP_to_XMLRPC(*arg1 TSRMLS_CC);
     683                 : 
     684                 :                 /* generate raw xml from xmlrpc data */
     685               0 :                 outBuf = XMLRPC_VALUE_ToXML(xOut, 0);
     686                 : 
     687               0 :                 if(xOut) {
     688               0 :                         if(outBuf) {
     689               0 :                                 RETVAL_STRING(outBuf, 1);
     690               0 :                                 free(outBuf);
     691                 :                         }
     692                 :                         /* cleanup */
     693               0 :                         XMLRPC_CleanupValue(xOut);
     694                 :                 }
     695                 :         }
     696                 : }
     697                 : /* }}} */
     698                 : 
     699                 : 
     700                 : zval* decode_request_worker (zval* xml_in, zval* encoding_in, zval* method_name_out)
     701               0 : {
     702               0 :         zval* retval = NULL;
     703                 :         XMLRPC_REQUEST response;
     704               0 :         STRUCT_XMLRPC_REQUEST_INPUT_OPTIONS opts = {{0}};
     705               0 :         opts.xml_elem_opts.encoding = encoding_in ? utf8_get_encoding_id_from_string(Z_STRVAL_P(encoding_in)) : ENCODING_DEFAULT;
     706                 : 
     707                 :         /* generate XMLRPC_REQUEST from raw xml */
     708               0 :         response = XMLRPC_REQUEST_FromXML(Z_STRVAL_P(xml_in), Z_STRLEN_P(xml_in), &opts);
     709               0 :         if(response) {
     710                 :                 /* convert xmlrpc data to native php types */
     711               0 :                 retval = XMLRPC_to_PHP(XMLRPC_RequestGetData(response));
     712                 : 
     713               0 :                 if(XMLRPC_RequestGetRequestType(response) == xmlrpc_request_call) {
     714               0 :                         if(method_name_out) {
     715               0 :                                 zval_dtor(method_name_out);
     716               0 :                                 Z_TYPE_P(method_name_out) = IS_STRING;
     717               0 :                                 Z_STRVAL_P(method_name_out) = estrdup(XMLRPC_RequestGetMethodName(response));
     718               0 :                                 Z_STRLEN_P(method_name_out) = strlen(Z_STRVAL_P(method_name_out));
     719                 :                         }
     720                 :                 }
     721                 : 
     722                 :                 /* dust, sweep, and mop */
     723               0 :                 XMLRPC_RequestFree(response, 1);
     724                 :         }
     725               0 :         return retval;
     726                 : }
     727                 : 
     728                 : /* {{{ proto array xmlrpc_decode_request(string xml, string& method [, string encoding])
     729                 :    Decodes XML into native PHP types */
     730                 : PHP_FUNCTION(xmlrpc_decode_request)
     731               0 : {
     732               0 :         zval **xml, **method, **encoding = NULL;
     733               0 :         int argc = ZEND_NUM_ARGS();
     734                 : 
     735               0 :         if (argc < 2 || argc > 3 || (zend_get_parameters_ex(argc, &xml, &method, &encoding) == FAILURE)) {
     736               0 :                 WRONG_PARAM_COUNT;
     737                 :         }
     738                 : 
     739               0 :         convert_to_string_ex(xml);
     740               0 :         convert_to_string_ex(method);
     741               0 :         if(argc == 3) {
     742               0 :                 convert_to_string_ex(encoding);
     743                 :         }
     744                 : 
     745               0 :         if(return_value_used) {
     746               0 :                 zval* retval = decode_request_worker(*xml, encoding ? *encoding : NULL, *method);
     747               0 :                 if(retval) {
     748               0 :                         *return_value = *retval;
     749               0 :                         FREE_ZVAL(retval);
     750                 :                 }
     751                 :         }
     752                 : }
     753                 : /* }}} */
     754                 : 
     755                 : 
     756                 : /* {{{ proto array xmlrpc_decode(string xml [, string encoding])
     757                 :    Decodes XML into native PHP types */
     758                 : PHP_FUNCTION(xmlrpc_decode)
     759               0 : {
     760               0 :         zval **arg1, **arg2 = NULL;
     761               0 :         int argc = ZEND_NUM_ARGS();
     762                 : 
     763               0 :         if (argc < 1 || argc > 2 || (zend_get_parameters_ex(argc, &arg1, &arg2) == FAILURE)) {
     764               0 :                 WRONG_PARAM_COUNT;
     765                 :         }
     766                 : 
     767               0 :         convert_to_string_ex(arg1);
     768               0 :         if(argc == 2) {
     769               0 :                 convert_to_string_ex(arg2);
     770                 :         }
     771                 : 
     772               0 :         if(return_value_used) {
     773               0 :                 zval* retval = decode_request_worker(*arg1, arg2 ? *arg2 : NULL, NULL);
     774               0 :                 if(retval) {
     775               0 :                         *return_value = *retval;
     776               0 :                         FREE_ZVAL(retval);
     777                 :                 }
     778                 :         }
     779                 : }
     780                 : /* }}} */
     781                 : 
     782                 : 
     783                 : /*************************
     784                 : * server related methods *
     785                 : *************************/
     786                 : 
     787                 : /* {{{ proto resource xmlrpc_server_create(void)
     788                 :    Creates an xmlrpc server */
     789                 : PHP_FUNCTION(xmlrpc_server_create)
     790               0 : {
     791               0 :         if(ZEND_NUM_ARGS() != 0) {
     792               0 :                 WRONG_PARAM_COUNT;
     793                 :         }
     794                 : 
     795               0 :         if(return_value_used) {
     796                 :                 zval *method_map, *introspection_map;
     797               0 :                 xmlrpc_server_data *server = emalloc(sizeof(xmlrpc_server_data));
     798               0 :                 MAKE_STD_ZVAL(method_map);
     799               0 :                 MAKE_STD_ZVAL(introspection_map);
     800                 :                 
     801               0 :                 array_init(method_map);
     802               0 :                 array_init(introspection_map);
     803                 :                 
     804                 :                 /* allocate server data.  free'd in destroy_server_data() */
     805               0 :                 server->method_map = method_map;
     806               0 :                 server->introspection_map = introspection_map;
     807               0 :                 server->server_ptr = XMLRPC_ServerCreate();
     808                 : 
     809               0 :                 XMLRPC_ServerRegisterIntrospectionCallback(server->server_ptr, php_xmlrpc_introspection_callback);
     810                 : 
     811                 :                 /* store for later use */
     812               0 :                 ZEND_REGISTER_RESOURCE(return_value,server, le_xmlrpc_server);
     813                 :         }
     814                 : }
     815                 : /* }}} */
     816                 : 
     817                 : /* {{{ proto int xmlrpc_server_destroy(resource server)
     818                 :    Destroys server resources */
     819                 : PHP_FUNCTION(xmlrpc_server_destroy)
     820               0 : {
     821                 :         zval **arg1;
     822               0 :         int bSuccess = FAILURE;
     823                 : 
     824               0 :         if (ZEND_NUM_ARGS() != 1 || (zend_get_parameters_ex(1, &arg1) == FAILURE)) {
     825               0 :                 WRONG_PARAM_COUNT;
     826                 :         }
     827                 : 
     828               0 :         if(Z_TYPE_PP(arg1) == IS_RESOURCE) {
     829                 :                 int type;
     830                 : 
     831               0 :                 xmlrpc_server_data *server = zend_list_find(Z_LVAL_PP(arg1), &type);
     832                 : 
     833               0 :                 if(server && type == le_xmlrpc_server) {
     834               0 :                         bSuccess = zend_list_delete(Z_LVAL_PP(arg1));
     835                 : 
     836                 :                         /* called by hashtable destructor
     837                 :                          * destroy_server_data(server);
     838                 :                          */
     839                 :                 }
     840                 :         }
     841               0 :         RETVAL_LONG(bSuccess == SUCCESS);
     842                 : }
     843                 : /* }}} */
     844                 : 
     845                 :            
     846                 : /* called by xmlrpc C engine as method handler for all registered methods.
     847                 :  * it then calls the corresponding PHP function to handle the method.
     848                 :  */
     849                 : static XMLRPC_VALUE php_xmlrpc_callback(XMLRPC_SERVER server, XMLRPC_REQUEST xRequest, void* data)
     850               0 : {
     851               0 :         xmlrpc_callback_data* pData = (xmlrpc_callback_data*)data;
     852                 :         zval* xmlrpc_params;
     853                 :         zval* callback_params[3];
     854                 :         TSRMLS_FETCH();
     855                 : 
     856                 :         /* convert xmlrpc to native php types */
     857               0 :         xmlrpc_params = XMLRPC_to_PHP(XMLRPC_RequestGetData(xRequest));
     858                 : 
     859                 :         /* setup data hoojum */
     860               0 :         callback_params[0] = pData->xmlrpc_method;
     861               0 :         callback_params[1] = xmlrpc_params;
     862               0 :         callback_params[2] = pData->caller_params;
     863                 : 
     864                 :         /* Use same C function for all methods */
     865                 : 
     866                 :         /* php func prototype: function user_func($method_name, $xmlrpc_params, $user_params) */
     867               0 :         call_user_function(CG(function_table), NULL, pData->php_function, pData->return_data, 3, callback_params TSRMLS_CC);
     868                 : 
     869               0 :         pData->php_executed = 1;
     870                 : 
     871               0 :         zval_ptr_dtor(&xmlrpc_params);
     872                 : 
     873               0 :         return NULL;
     874                 : }
     875                 : 
     876                 : /* called by the C server when it first receives an introspection request.  We pass this on to
     877                 :  * our PHP listeners, if any
     878                 :  */
     879                 : static void php_xmlrpc_introspection_callback(XMLRPC_SERVER server, void* data)
     880               0 : {
     881                 :         zval retval, **php_function;
     882                 :         zval* callback_params[1];
     883                 :         char *php_function_name;
     884               0 :         xmlrpc_callback_data* pData = (xmlrpc_callback_data*)data;
     885                 :         TSRMLS_FETCH();
     886                 : 
     887                 :         /* setup data hoojum */
     888               0 :         callback_params[0] = pData->caller_params;
     889                 : 
     890                 :         /* loop through and call all registered callbacks */
     891               0 :         zend_hash_internal_pointer_reset(Z_ARRVAL_P(pData->server->introspection_map));
     892                 :         while(1) {
     893               0 :                 if(zend_hash_get_current_data(Z_ARRVAL_P(pData->server->introspection_map), (void**)&php_function) == SUCCESS) {
     894                 : 
     895               0 :                         if (zend_is_callable(*php_function, 0, &php_function_name)) {
     896                 :                                 /* php func prototype: function string user_func($user_params) */
     897               0 :                                 if (call_user_function(CG(function_table), NULL, *php_function, &retval, 1, callback_params TSRMLS_CC) == SUCCESS) {
     898                 :                                         XMLRPC_VALUE xData;
     899               0 :                                         STRUCT_XMLRPC_ERROR err = {0};
     900                 : 
     901                 :                                         /* return value should be a string */
     902               0 :                                         convert_to_string(&retval);
     903                 : 
     904               0 :                                         xData = XMLRPC_IntrospectionCreateDescription(Z_STRVAL(retval), &err);
     905                 : 
     906               0 :                                         if(xData) {
     907               0 :                                                 if(!XMLRPC_ServerAddIntrospectionData(server, xData)) {
     908               0 :                                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to add introspection data returned from %s(), improper element structure", php_function_name);
     909                 :                                                 }
     910               0 :                                                 XMLRPC_CleanupValue(xData);
     911                 :                                         } else {
     912                 :                                                 /* could not create description */
     913               0 :                                                 if(err.xml_elem_error.parser_code) {
     914               0 :                                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "xml parse error: [line %ld, column %ld, message: %s] Unable to add introspection data returned from %s()", 
     915                 :                                                         err.xml_elem_error.column, err.xml_elem_error.line, err.xml_elem_error.parser_error, php_function_name);
     916                 :                                                 } else {
     917               0 :                                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to add introspection data returned from %s()", php_function_name);
     918                 :                                                 }
     919                 :                                         }
     920               0 :                                         zval_dtor(&retval);
     921                 :                                 } else {
     922                 :                                         /* user func failed */
     923               0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error calling user introspection callback: %s()", php_function_name);
     924                 :                                 }
     925                 :                         } else {
     926               0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid callback '%s' passed", php_function_name);
     927                 :                         }
     928               0 :                         efree(php_function_name);
     929                 :                 } else {
     930               0 :                         break;
     931                 :                 }
     932               0 :                 zend_hash_move_forward(Z_ARRVAL_P(pData->server->introspection_map));
     933               0 :         }
     934                 : 
     935                 :         /* so we don't call the same callbacks ever again */
     936               0 :         zend_hash_clean(Z_ARRVAL_P(pData->server->introspection_map));
     937               0 : }
     938                 : 
     939                 : /* {{{ proto bool xmlrpc_server_register_method(resource server, string method_name, string function)
     940                 :    Register a PHP function to handle method matching method_name */
     941                 : PHP_FUNCTION(xmlrpc_server_register_method)
     942               0 : {
     943                 :         zval **method_key, **method_name, **handle, *method_name_save;
     944                 :         int type;
     945                 :         xmlrpc_server_data* server;
     946                 : 
     947               0 :         if (ZEND_NUM_ARGS() != 3 || (zend_get_parameters_ex(3, &handle, &method_key, &method_name) == FAILURE)) {
     948               0 :                 WRONG_PARAM_COUNT;
     949                 :         }
     950                 : 
     951               0 :         server = zend_list_find(Z_LVAL_PP(handle), &type);
     952                 : 
     953               0 :         if(type == le_xmlrpc_server) {
     954                 :                 /* register with C engine. every method just calls our standard callback, 
     955                 :                  * and it then dispatches to php as necessary
     956                 :                  */
     957               0 :                 if(XMLRPC_ServerRegisterMethod(server->server_ptr, Z_STRVAL_PP(method_key), php_xmlrpc_callback)) {
     958                 :                         /* save for later use */
     959               0 :                         MAKE_STD_ZVAL(method_name_save);
     960               0 :                         *method_name_save = **method_name;
     961               0 :                         zval_copy_ctor(method_name_save);
     962                 : 
     963                 :                         /* register our php method */
     964               0 :                         add_zval(server->method_map, Z_STRVAL_PP(method_key), &method_name_save);
     965                 : 
     966               0 :                         RETURN_BOOL(1);
     967                 :                 }
     968                 :         }
     969               0 :         RETURN_BOOL(0);
     970                 : }
     971                 : /* }}} */
     972                 : 
     973                 : 
     974                 : /* {{{ proto bool xmlrpc_server_register_introspection_callback(resource server, string function)
     975                 :    Register a PHP function to generate documentation */
     976                 : PHP_FUNCTION(xmlrpc_server_register_introspection_callback)
     977               0 : {
     978                 :         zval **method_name, **handle, *method_name_save;
     979                 :         int type;
     980                 :         xmlrpc_server_data* server;
     981                 : 
     982               0 :         if (ZEND_NUM_ARGS() != 2 || (zend_get_parameters_ex(2, &handle, &method_name) == FAILURE)) {
     983               0 :                 WRONG_PARAM_COUNT;
     984                 :         }
     985                 : 
     986               0 :         server = zend_list_find(Z_LVAL_PP(handle), &type);
     987                 : 
     988               0 :         if(type == le_xmlrpc_server) {
     989                 :                 /* save for later use */
     990               0 :                 MAKE_STD_ZVAL(method_name_save);
     991               0 :                 *method_name_save = **method_name;
     992               0 :                 zval_copy_ctor(method_name_save);
     993                 : 
     994                 :                 /* register our php method */
     995               0 :                 add_zval(server->introspection_map, NULL, &method_name_save);
     996                 : 
     997               0 :                 RETURN_BOOL(1);
     998                 :         }
     999               0 :         RETURN_BOOL(0);
    1000                 : }
    1001                 : /* }}} */
    1002                 : 
    1003                 : 
    1004                 : /* this function is itchin for a re-write */
    1005                 : 
    1006                 : /* {{{ proto mixed xmlrpc_server_call_method(resource server, string xml, mixed user_data [, array output_options])
    1007                 :    Parses XML requests and call methods */
    1008                 : PHP_FUNCTION(xmlrpc_server_call_method)
    1009               0 : {
    1010               0 :         xmlrpc_callback_data data = {0};
    1011                 :         XMLRPC_REQUEST xRequest;
    1012                 :         STRUCT_XMLRPC_REQUEST_INPUT_OPTIONS input_opts;
    1013                 :         xmlrpc_server_data* server;
    1014               0 :         zval **rawxml, **caller_params, **handle, **output_opts = NULL;
    1015                 :         int type;
    1016                 :         php_output_options out;
    1017               0 :         int argc =ZEND_NUM_ARGS();
    1018                 :         
    1019               0 :         if (argc < 3 || argc > 4 || (zend_get_parameters_ex(argc, &handle, &rawxml, &caller_params, &output_opts) != SUCCESS)) {
    1020               0 :                 WRONG_PARAM_COUNT;
    1021                 :         }
    1022                 :         /* user output options */
    1023               0 :         if (argc == 3) {
    1024               0 :                 set_output_options(&out, NULL);
    1025                 :         }
    1026                 :         else {
    1027               0 :                 set_output_options(&out, *output_opts);
    1028                 :         }
    1029                 : 
    1030               0 :         server = zend_list_find(Z_LVAL_PP(handle), &type);
    1031                 : 
    1032               0 :         if(type == le_xmlrpc_server) {
    1033                 :                 /* HACK: use output encoding for now */
    1034               0 :                 input_opts.xml_elem_opts.encoding = utf8_get_encoding_id_from_string(out.xmlrpc_out.xml_elem_opts.encoding);
    1035                 : 
    1036                 :                 /* generate an XMLRPC_REQUEST from the raw xml input */
    1037               0 :                 xRequest = XMLRPC_REQUEST_FromXML(Z_STRVAL_PP(rawxml), Z_STRLEN_PP(rawxml), &input_opts);
    1038                 : 
    1039               0 :                 if(xRequest) {
    1040               0 :                         const char* methodname = XMLRPC_RequestGetMethodName(xRequest);
    1041                 :                         zval **php_function;
    1042               0 :                         XMLRPC_VALUE xAnswer = NULL;
    1043               0 :                         MAKE_STD_ZVAL(data.xmlrpc_method); /* init. very important.  spent a frustrating day finding this out. */
    1044               0 :                         MAKE_STD_ZVAL(data.return_data);
    1045               0 :                         Z_TYPE_P(data.return_data) = IS_NULL;  /* in case value is never init'd, we don't dtor to think it is a string or something */
    1046               0 :                         Z_TYPE_P(data.xmlrpc_method) = IS_NULL;
    1047                 : 
    1048               0 :                         if (!methodname) {
    1049               0 :                                 methodname = "";
    1050                 :                         }
    1051                 :             
    1052                 :                         /* setup some data to pass to the callback function */
    1053               0 :                         Z_STRVAL_P(data.xmlrpc_method) = estrdup(methodname);
    1054               0 :                         Z_STRLEN_P(data.xmlrpc_method) = strlen(methodname);
    1055               0 :                         Z_TYPE_P(data.xmlrpc_method) = IS_STRING;
    1056               0 :                         data.caller_params = *caller_params;
    1057               0 :                         data.php_executed = 0;
    1058               0 :                         data.server = server;
    1059                 : 
    1060                 :                         /* check if the called method has been previous registered */
    1061               0 :                         if(zend_hash_find(Z_ARRVAL_P(server->method_map),
    1062                 :                               Z_STRVAL_P(data.xmlrpc_method), 
    1063                 :                               Z_STRLEN_P(data.xmlrpc_method) + 1, 
    1064                 :                               (void**)&php_function) == SUCCESS) {
    1065                 : 
    1066               0 :                                 data.php_function = *php_function;
    1067                 :                         }
    1068                 : 
    1069                 :                         /* We could just call the php method directly ourselves at this point, but we do this 
    1070                 :                          * with a C callback in case the xmlrpc library ever implements some cool usage stats,
    1071                 :                          * or somesuch.
    1072                 :                          */
    1073               0 :                         xAnswer = XMLRPC_ServerCallMethod(server->server_ptr, xRequest, &data);
    1074               0 :                         if(xAnswer && out.b_php_out) {
    1075               0 :                                 zval_dtor(data.return_data);
    1076               0 :                                 FREE_ZVAL(data.return_data);
    1077               0 :                                 data.return_data = XMLRPC_to_PHP(xAnswer);
    1078               0 :                         } else if(data.php_executed && !out.b_php_out) {
    1079               0 :                                 xAnswer = PHP_to_XMLRPC(data.return_data TSRMLS_CC);
    1080                 :                         }
    1081                 : 
    1082                 :                         /* should we return data as xml? */
    1083               0 :                         if(!out.b_php_out) {
    1084               0 :                                 XMLRPC_REQUEST xResponse = XMLRPC_RequestNew();
    1085               0 :                                 if(xResponse) {
    1086               0 :                                         char *outBuf = 0;
    1087               0 :                                         int buf_len = 0;
    1088                 : 
    1089                 :                                         /* automagically determine output serialization type from request type */
    1090               0 :                                         if (out.b_auto_version) { 
    1091               0 :                                                 XMLRPC_REQUEST_OUTPUT_OPTIONS opts = XMLRPC_RequestGetOutputOptions(xRequest);
    1092               0 :                                                 if (opts) {
    1093               0 :                                                         out.xmlrpc_out.version = opts->version;
    1094                 :                                                 }
    1095                 :                                         }