void inspect(zval* value) { switch (Z_TYPE_P(value)) { case IS_NULL: php_printf("The variable passed is of type IS_NULL\n"); break; case IS_LONG: php_printf("The variable passed is of type IS_LONG\n"); php_printf("The content is %ld\n", Z_LVAL_P(value)); break; case IS_DOUBLE: php_printf("The variable passed is of type IS_DOUBLE\n"); php_printf("The content is %f\n", Z_DVAL_P(value)); break; #if PHP_VERSION_ID >= 70000 case IS_TRUE: php_printf("The variable passed is of type IS_TRUE\n"); break; case IS_FALSE: php_printf("The variable passed is of type IS_FALSE\n"); break; #else case IS_BOOL: php_printf("The variable passed is of type IS_BOOL\n"); break; #endif case IS_OBJECT: php_printf("The variable passed is of type IS_OBJECT\n"); break; case IS_STRING: php_printf("The variable passed is of type IS_STRING\n"); php_printf("The content is \"%s\"\n", Z_STRVAL_P(value)); php_printf("The content reversed is \""); char *p = Z_STRVAL_P(value) + Z_STRLEN_P(value); while (--p >= Z_STRVAL_P(value)) php_printf("%c", *p); php_printf("\"\n"); break; case IS_RESOURCE: php_printf("The variable is of type IS_RESOURCE\n"); php_printf("The number of resource is: %ld\n", #if PHP_VERSION_ID >= 70000 Z_RES_HANDLE_P(value) #else Z_RESVAL_P(value) #endif ); break; case IS_ARRAY: php_printf("The variable passed is type of IS_ARRAY\n"); php_printf("There is %ld direct elements in the array\n", zend_hash_num_elements(Z_ARRVAL_P(value))); break; default: php_printf("unkown type"); break; } }
static PHP_FUNCTION(params_dump) { zval *arg;//定义参数值,zval可以表示所有的参数的值 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg) == FAILURE) { return; } switch(Z_TYPE_P(arg)) { case IS_NULL://arg is null php_printf("NULL\n"); break; case IS_BOOL://arg is true, false php_printf("bool(%s)\n", Z_BVAL_P(arg)?"true":"false"); break; case IS_LONG: php_printf("int(%d)\n", Z_LVAL_P(arg)); break; case IS_DOUBLE: php_printf("float(%f)\n", Z_DVAL_P(arg)); break; case IS_STRING: php_printf("string(%d)\n", Z_STRLEN_P(arg)); PHPWRITE(Z_STRVAL_P(arg), Z_STRLEN_P(arg)); php_printf("\n"); break; case IS_ARRAY: php_printf("array(%d){...}\n", zend_hash_num_elements(Z_ARRVAL_P(arg))); break; case IS_RESOURCE:{ const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RESVAL_P(arg) TSRMLS_CC); php_printf("resource#%ld(%s)\n", Z_RESVAL_P(arg), type_name?type_name:"Unknown"); break; } case IS_OBJECT:{ const zend_class_entry *ce = Z_OBJCE_P(arg); php_printf("object#%u(%s)\n", Z_OBJ_HANDLE_P(arg), (ce && ce->name)?ce->name:"Unknown"); break; } default: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown type: %d\n", Z_TYPE_P(arg)); } }
/* {{{ create_mqseries_bytes_resource * makes an mqseries_bytes reference, needed when returning message and correlation id's */ static zval* create_mqseries_bytes_resource(PMQBYTE bytes, size_t size TSRMLS_DC) { mqseries_bytes *pBytes; zval *z_bytes; MAKE_STD_ZVAL(z_bytes); pBytes = (mqseries_bytes *) emalloc(sizeof(mqseries_bytes)); pBytes->bytes = (PMQBYTE) emalloc(size*sizeof(MQBYTE)); memcpy(pBytes->bytes, bytes, size); ZEND_REGISTER_RESOURCE(z_bytes, pBytes, le_mqseries_bytes); pBytes->id = Z_RESVAL_P(z_bytes); return z_bytes; }
static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent) { char *host; int host_len; long port = -1; zval *zerrno = NULL, *zerrstr = NULL, *zcontext = NULL; double timeout = FG(default_socket_timeout); unsigned long conv; struct timeval tv; char *hashkey = NULL; php_stream *stream = NULL; php_stream_context *context = NULL; int err; RETVAL_FALSE; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lzzdr", &host, &host_len, &port, &zerrno, &zerrstr, &timeout, &zcontext) == FAILURE) { RETURN_FALSE; } if (zcontext) { ZEND_FETCH_RESOURCE(context, php_stream_context*, &zcontext, -1, "stream-context", php_le_stream_context()); } if (persistent) { spprintf(&hashkey, 0, "pfsockopen__%s:%ld", host, port); switch(php_stream_from_persistent_id(hashkey, &stream TSRMLS_CC)) { case PHP_STREAM_PERSISTENT_SUCCESS: if (_php_network_is_stream_alive(stream TSRMLS_CC)) { php_stream_to_zval(stream, return_value); } else { /* it died; we need to replace it */ php_stream_pclose(stream); break; } /* fall through */ case PHP_STREAM_PERSISTENT_FAILURE: efree(hashkey); return; } } /* prepare the timeout value for use */ conv = (unsigned long) (timeout * 1000000.0); tv.tv_sec = conv / 1000000; tv.tv_usec = conv % 1000000; if (zerrno) { zval_dtor(zerrno); ZVAL_LONG(zerrno, 0); } if (zerrstr) { zval_dtor(zerrstr); ZVAL_STRING(zerrstr, "", 1); } if (port > 0) { /* connect to a host */ enum php_sslflags_t { php_ssl_none, php_ssl_v23, php_ssl_tls }; enum php_sslflags_t ssl_flags = php_ssl_none; struct { char *proto; int protolen; int socktype; enum php_sslflags_t ssl_flags; /* more flags to be added here */ } sockmodes[] = { { "udp://", 6, SOCK_DGRAM, php_ssl_none }, { "tcp://", 6, SOCK_STREAM, php_ssl_none }, { "ssl://", 6, SOCK_STREAM, php_ssl_v23 }, { "tls://", 6, SOCK_STREAM, php_ssl_tls }, /* more modes to be added here */ { NULL, 0, 0 } }; int socktype = SOCK_STREAM; int i; for (i = 0; sockmodes[i].proto != NULL; i++) { if (strncmp(host, sockmodes[i].proto, sockmodes[i].protolen) == 0) { ssl_flags = sockmodes[i].ssl_flags; socktype = sockmodes[i].socktype; host += sockmodes[i].protolen; break; } } #ifndef HAVE_OPENSSL_EXT if (ssl_flags != php_ssl_none) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "no SSL support in this build"); } else #endif stream = php_stream_sock_open_host(host, (unsigned short)port, socktype, &tv, hashkey); /* Preserve error */ err = php_socket_errno(); if (stream == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s:%ld", host, port); } else if (context) { php_stream_context_set(stream, context); } #ifdef HAVE_OPENSSL_EXT if (stream && ssl_flags != php_ssl_none) { int ssl_ret = FAILURE; switch(ssl_flags) { case php_ssl_v23: ssl_ret = php_stream_sock_ssl_activate_with_method(stream, 1, SSLv23_client_method(), NULL TSRMLS_CC); break; case php_ssl_tls: ssl_ret = php_stream_sock_ssl_activate_with_method(stream, 1, TLSv1_client_method(), NULL TSRMLS_CC); break; default: /* unknown ?? */ break; } if (ssl_ret == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to activate SSL mode %d", ssl_flags); php_stream_close(stream); stream = NULL; } } #endif } else { /* FIXME: Win32 - this probably does not return sensible errno and errstr */ stream = php_stream_sock_open_unix(host, host_len, hashkey, &tv); err = php_socket_errno(); } if (hashkey) efree(hashkey); if (stream == NULL) { if (zerrno) { zval_dtor(zerrno); ZVAL_LONG(zerrno, err); } if (zerrstr) { char *buf = php_socket_strerror(err, NULL, 0); /* no need to dup; we would only need to efree buf anyway */ zval_dtor(zerrstr); ZVAL_STRING(zerrstr, buf, 0); } RETURN_FALSE; } if (zcontext) { zend_list_addref(Z_RESVAL_P(zcontext)); } php_stream_to_zval(stream, return_value); }
/** * Writes the log to the stream itself * * @param string $message * @param int $type * @param int $time * @param array $context * @see http://www.firephp.org/Wiki/Reference/Protocol */ PHP_METHOD(Phalcon_Logger_Adapter_Firephp, logInternal){ zval *message, *type, *time, *context, *formatter = NULL, *applied_format = NULL; zval *initialized, *index; sapi_header_line h = { NULL, 0, 0 }; smart_str str = { NULL, 0, 0 }; int size, offset; int separate_index = 0; size_t num_bytes; const int chunk = 4500; /* If headers has already been sent, we can do nothing. Exit early. */ if (SG(headers_sent)) { RETURN_FALSE; } PHALCON_MM_GROW(); phalcon_fetch_params(1, 4, 0, &message, &type, &time, &context); PHALCON_CALL_METHOD(&formatter, this_ptr, "getformatter"); initialized = phalcon_fetch_static_property_ce(phalcon_logger_adapter_firephp_ce, SL("_initialized") TSRMLS_CC); if (!zend_is_true(initialized)) { /** * Send the required initialization headers. * Use Zend API here so that the user can see the progress and because * if we delegate this to Phalcon and there will be a fatal errors, * chances are that the headers will never ne sent. */ h.line = "X-Wf-Protocol-1: http://meta.wildfirehq.org/Protocol/JsonStream/0.2"; h.line_len = sizeof("X-Wf-Protocol-1: http://meta.wildfirehq.org/Protocol/JsonStream/0.2")-1; sapi_header_op(SAPI_HEADER_REPLACE, &h TSRMLS_CC); h.line = "X-Wf-1-Plugin-1: http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3"; h.line_len = sizeof("X-Wf-1-Plugin-1: http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3")-1; sapi_header_op(SAPI_HEADER_REPLACE, &h TSRMLS_CC); h.line = "X-Wf-1-Structure-1: http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1"; h.line_len = sizeof("X-Wf-1-Structure-1: http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1")-1; sapi_header_op(SAPI_HEADER_REPLACE, &h TSRMLS_CC); ZVAL_TRUE(initialized); /* This will also update the property because "initialized" was not separated */ } PHALCON_CALL_METHOD(&applied_format, formatter, "format", message, type, time, context); convert_to_string(applied_format); index = phalcon_fetch_static_property_ce(phalcon_logger_adapter_firephp_ce, SL("_index") TSRMLS_CC); assert(Z_TYPE_P(index) == IS_LONG); if (Z_REFCOUNT_P(index) > 1) { PHALCON_INIT_VAR(index); separate_index = 1; } size = Z_STRLEN_P(applied_format); offset = 0; /** * We need to send the data in chunks not exceeding 5,000 bytes. * Allocate the smart string once to avoid performance penalties. */ smart_str_alloc4(&str, (uint)(size > chunk ? chunk : size), 0, num_bytes); while (size > 0) { smart_str_appends(&str, "X-Wf-1-1-1-"); smart_str_append_long(&str, Z_RESVAL_P(index)); smart_str_appends(&str, ": "); num_bytes = size > chunk ? chunk : size; if (offset) { /* This is not the first chunk, prepend the payload with "|" */ smart_str_appendc(&str, '|'); } /* Grab the chunk from the encoded string */ smart_str_appendl(&str, Z_STRVAL_P(applied_format) + offset, num_bytes); size -= num_bytes; offset += num_bytes; if (size) { /* If we have more data to send, append "|/" */ smart_str_appendl(&str, "|\\", 2); } smart_str_0(&str); /* Not strictly necessary but just to be safe */ /* Send the result */ h.line = str.c; h.line_len = str.len; sapi_header_op(SAPI_HEADER_REPLACE, &h TSRMLS_CC); ZVAL_LONG(index, Z_RESVAL_P(index)+1); /** * Do not free and then reallocate memory. Just pretend the string * is empty. We will take care of deallocation later. */ str.len = 0; } if (separate_index) { phalcon_update_static_property_ce(phalcon_logger_adapter_firephp_ce, SL("_index"), index TSRMLS_CC); } /* Deallocate the smnart string if it is not empty */ if (str.c) { smart_str_free(&str); } PHALCON_MM_RESTORE(); }