int TWIG_ARRAY_KEY_EXISTS(zval *array, char* key, int key_len) { if (Z_TYPE_P(array) != IS_ARRAY) { return 0; } return zend_symtable_exists(Z_ARRVAL_P(array), key, key_len + 1); }
/** * @brief Checks whether @a index exists in array @a arr * @param arr Array * @param index Index * @return isset($arr[$index]) * @retval 0 Not exists, @a arr is not an array or @a index is of not supported type * @retval 1 Exists * @note @c index will be handled as follows: @c NULL is treated as an empty string, @c double values are cast to @c integer, @c bool or @c resource are treated as @c integer * @throw E_WARNING if @a offset is not a scalar */ int ZEPHIR_FASTCALL zephir_array_isset(const zval *arr, zval *index) { HashTable *h; if (Z_TYPE_P(arr) != IS_ARRAY) { return 0; } h = Z_ARRVAL_P(arr); switch (Z_TYPE_P(index)) { case IS_NULL: return zephir_hash_exists(h, SS("")); case IS_DOUBLE: return zend_hash_index_exists(h, (ulong)Z_DVAL_P(index)); case IS_BOOL: case IS_LONG: case IS_RESOURCE: return zend_hash_index_exists(h, Z_LVAL_P(index)); case IS_STRING: return zend_symtable_exists(h, Z_STRVAL_P(index), Z_STRLEN_P(index)+1); default: zend_error(E_WARNING, "Illegal offset type"); return 0; } }
int TWIG_ARRAY_KEY_EXISTS(zval *array, zval *key) { if (Z_TYPE_P(array) != IS_ARRAY) { return 0; } convert_to_string(key); return zend_symtable_exists(Z_ARRVAL_P(array), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1); }
/* Iterate over the given php zval** and transform into protobuf compatible values. Write those into the given protobuf message pointer. */ int php_message (const ProtobufCMessage* message, zval* val) { HashPosition pos; HashTable* hash_table = Z_ARRVAL_P(val); zval** data; char* key; int i, j, key_len; long index; // check if all required fields are existent in the given php array. // NULL default values if not for (j=0 ; j < message->descriptor->n_fields ; ++j) { const ProtobufCFieldDescriptor* tmp_field = message->descriptor->fields + j; if (! zend_symtable_exists(hash_table, (char*)tmp_field->name, strlen((char*)(tmp_field->name)) + 1)) { if (tmp_field->label == PROTOBUF_C_LABEL_REQUIRED) { zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "cannot find required field '%s'", tmp_field->name); return 1; } else { null_field(message, tmp_field); } } } // copy php values to proto zend_hash_internal_pointer_reset_ex(hash_table, &pos); for (;; zend_hash_move_forward_ex(hash_table, &pos)) { zend_hash_get_current_data_ex(hash_table, (void**)&data, &pos); i = zend_hash_get_current_key_ex(hash_table, &key, &key_len, &index, 0, &pos); if (i == HASH_KEY_NON_EXISTANT) { break; } else if (i == HASH_KEY_IS_STRING) { const ProtobufCFieldDescriptor* field = find_field(message, key); if (field == NULL) continue; if (write_field(message, field, data) != 0) { zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "unable to pack field '%s'", field->name); return 1; } } } return 0; }
int TWIG_ARRAY_KEY_EXISTS(zval *array, zval *key) { if (Z_TYPE_P(array) != IS_ARRAY) { return 0; } switch (Z_TYPE_P(key)) { case IS_NULL: return zend_hash_exists(Z_ARRVAL_P(array), "", 1); case IS_BOOL: case IS_DOUBLE: convert_to_long(key); case IS_LONG: return zend_hash_index_exists(Z_ARRVAL_P(array), Z_LVAL_P(key)); default: convert_to_string(key); return zend_symtable_exists(Z_ARRVAL_P(array), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1); } }
PHP_METHOD(jsonrpc_server, rpcformat) { zval *payload; zval *object; zval **method = NULL; zval **jsonrpc = NULL; zval **params = NULL; object = getThis(); payload = zend_read_property( php_jsonrpc_server_entry, object, "payload", sizeof("payload")-1, 0 TSRMLS_CC ); if (Z_TYPE_P(payload) != IS_ARRAY) { RETVAL_LONG(-32600); return ; } if (!zend_symtable_exists(Z_ARRVAL_P(payload), "jsonrpc", strlen("jsonrpc")+1)) { RETVAL_LONG(-32600); return ; } if (!zend_symtable_exists(Z_ARRVAL_P(payload), "method", strlen("method")+1)) { RETVAL_LONG(-32601); return ; } //MAKE_STD_ZVAL(&method); if (zend_hash_find(Z_ARRVAL_P(payload), "method", strlen("method")+1, (void **)&method) == FAILURE) { RETVAL_LONG(-32601); return ; } if (Z_TYPE_PP(method) != IS_STRING) { RETVAL_LONG(-32601); return ; } //MAKE_STD_ZVAL(&jsonrpc); if (zend_hash_find(Z_ARRVAL_P(payload), "jsonrpc", strlen("jsonrpc")+1, (void **)&jsonrpc) == FAILURE) { RETVAL_LONG(-32600); return ; } if (strcmp(Z_STRVAL_PP(jsonrpc),"2.0") != 0) { RETVAL_LONG(-32600); return ; } //MAKE_STD_ZVAL(¶ms); if (zend_hash_find(Z_ARRVAL_P(payload), "params", strlen("params")+1, (void **)¶ms) == FAILURE) { RETVAL_LONG(-32602); return ; } if (Z_TYPE_PP(params) != IS_ARRAY) { RETVAL_LONG(-32602); return ; } RETVAL_LONG(0); return ; }
PHP_METHOD(jsonrpc_server, execute) { zval *object; zval *func; zval **func_params, **exec_params; zval *data, *payload, *error, *response; zval **payload_method, **payload_params; zval **id = NULL; sapi_header_line ctr = {0}; smart_str buf = {0}; zend_bool response_type = 0; // type:0 =>json type:1 => array object = getThis(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &response_type) == FAILURE) { return ; } payload = zend_read_property( php_jsonrpc_server_entry, getThis(), "payload", sizeof("payload")-1, 0 TSRMLS_CC ); MAKE_STD_ZVAL(error); array_init(error); MAKE_STD_ZVAL(func); ZVAL_STRINGL(func, "jsonformat", sizeof("jsonformat") - 1, 0); if (call_user_function(NULL, &object, func, return_value, 0, NULL TSRMLS_CC) == FAILURE){ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed calling Jsonrpc_Server::jsonformat()"); return ; } if (!Z_BVAL_P(return_value)){ add_assoc_long(error, "code", -32700); add_assoc_string(error, "message", "Parse error", 1); zval_dtor(return_value); array_init(return_value); add_assoc_string(return_value, "jsonrpc", "2.0", 1); add_assoc_zval(return_value, "error", error); goto getresponse; } ZVAL_STRINGL(func, "rpcformat", sizeof("rpcformat") - 1, 0); if (call_user_function(NULL, &object, func, return_value, 0, NULL TSRMLS_CC) == FAILURE){ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed calling Jsonrpc_Server::rpcformat()"); return ; } if (Z_LVAL_P(return_value) == -32600){ add_assoc_long(error, "code", -32600); add_assoc_string(error, "message", "Invalid Request", 1); zval_dtor(return_value); array_init(return_value); add_assoc_string(return_value, "jsonrpc", "2.0", 1); add_assoc_zval(return_value, "error", error); goto getresponse; }else if (Z_LVAL_P(return_value) == -32601){ add_assoc_long(error, "code", -32601); add_assoc_string(error, "message", "Method not found", 1); zval_dtor(return_value); array_init(return_value); add_assoc_string(return_value, "jsonrpc", "2.0", 1); add_assoc_zval(return_value, "error", error); goto getresponse; }else if (Z_LVAL_P(return_value) == -32602){ add_assoc_long(error, "code", -32602); add_assoc_string(error, "message", "Invalid params", 1); zval_dtor(return_value); array_init(return_value); add_assoc_string(return_value, "jsonrpc", "2.0", 1); add_assoc_zval(return_value, "error", error); goto getresponse; }else if (Z_LVAL_P(return_value) == -32603){ add_assoc_long(error, "code", -32603); add_assoc_string(error, "message", "Internal error", 1); zval_dtor(return_value); array_init(return_value); add_assoc_string(return_value, "jsonrpc", "2.0", 1); add_assoc_zval(return_value, "error", error); goto getresponse; } exec_params = emalloc(sizeof(zval *) * 2); payload = zend_read_property( php_jsonrpc_server_entry, getThis(), "payload", sizeof("payload")-1, 0 TSRMLS_CC ); if (zend_hash_find(Z_ARRVAL_P(payload), "method", strlen("method")+1, (void **)&payload_method) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "method is not find"); return ; } //jsr_dump_zval(*payload_method); exec_params[0] = *payload_method; if (zend_hash_find(Z_ARRVAL_P(payload), "params", strlen("params")+1, (void **)&payload_params) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "params is not find"); return ; } //shurrik_dump_zval(*payload_params); exec_params[1] = *payload_params; MAKE_STD_ZVAL(data); ZVAL_STRINGL(func, "executeprocedure", sizeof("executeprocedure") - 1, 0); if (call_user_function(NULL, &object, func, data, 2, exec_params TSRMLS_CC) == FAILURE){ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed calling Jsonrpc_Server::executeprocedure()"); return ; } efree(exec_params); if (Z_LVAL_P(data) == -32601){ add_assoc_long(error, "code", -32601); add_assoc_string(error, "message", "Method not found", 1); zval_dtor(return_value); array_init(return_value); add_assoc_string(return_value, "jsonrpc", "2.0", 1); add_assoc_zval(return_value, "error", error); goto getresponse; } zval_dtor(return_value); array_init(return_value); add_assoc_string(return_value, "jsonrpc", "2.0", 1); add_assoc_zval(return_value, "result", data); goto getresponse; return ; getresponse: if (Z_TYPE_P(payload) == IS_ARRAY){ if (!zend_symtable_exists(Z_ARRVAL_P(payload), "id", strlen("id")+1)) { }else { if (zend_hash_find(Z_ARRVAL_P(payload), "id", strlen("id")+1, (void **)&id ) == FAILURE) { //php_error_docref(NULL TSRMLS_CC, E_WARNING, "closeure is not expected to be a valid callback"); //return ; } if (Z_TYPE_PP(id) == IS_NULL){ add_assoc_null(return_value, "id"); }else if(Z_TYPE_PP(id) == IS_STRING){ convert_to_string(*id); add_assoc_string(return_value, "id", Z_STRVAL_PP(id), 0); }else if (Z_TYPE_PP(id) == IS_LONG){ convert_to_long(*id); add_assoc_long(return_value, "id", Z_LVAL_PP(id)); } } }else { add_assoc_null(return_value, "id"); } ctr.line = "Content-Type: application/json"; ctr.line_len = strlen(ctr.line); sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC); /*if (!response_type){ php_json_encode(&buf, return_value, 0 TSRMLS_CC); zval_dtor(return_value); zval_dtor(error); ZVAL_STRINGL(return_value, buf.c, buf.len, 1); smart_str_free(&buf); }*/ if (!response_type){ exec_params = emalloc(sizeof(zval *) * 1); exec_params[0] = return_value; ZVAL_STRINGL(func, "Jsonrpc_Yajl::generate", sizeof("Jsonrpc_Yajl::generate") - 1, 0); if (call_user_function(EG(function_table), NULL, func, return_value, 1, exec_params TSRMLS_CC) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed calling Jsonrpc_Yajl::generate()"); return ; } //ZVAL_STRINGL(return_value, Z_STRVAL_P(return_value), Z_STRLEN_P(return_value), 1); zval_dtor(error); efree(exec_params); } efree(func); return ; }