Esempio n. 1
0
/**
 * Load config file
 *
 * @param string $filePath
 */
PHP_METHOD(Phalcon_Config_Adapter_Php, read){

	zval *file_path, *absolute_path = NULL, config_dir_path = {}, *base_path, config = {};

	phalcon_fetch_params(0, 1, 1, &file_path, &absolute_path);
	PHALCON_ENSURE_IS_STRING(file_path);

	if (absolute_path == NULL) {
		absolute_path = &PHALCON_GLOBAL(z_false);
	}

	if (zend_is_true(absolute_path)) {
		PHALCON_CPY_WRT(&config_dir_path, file_path);
	} else {
		base_path = phalcon_read_static_property_ce(phalcon_config_adapter_ce, SL("_basePath"));

		PHALCON_CONCAT_VV(&config_dir_path, base_path, file_path);
	}

	if (phalcon_require_ret(&config, Z_STRVAL(config_dir_path)) == FAILURE) {
		zend_throw_exception_ex(phalcon_config_exception_ce, 0, "Configuration file '%s' cannot be read", Z_STRVAL(config_dir_path));
		PHALCON_PTR_DTOR(&config_dir_path);
		return;
	}
	PHALCON_PTR_DTOR(&config_dir_path);

	if (Z_TYPE(config) == IS_ARRAY) {
		PHALCON_CALL_METHODW(NULL, getThis(), "val", &config);
	}
	PHALCON_PTR_DTOR(&config);
	RETURN_THISW();
}
Esempio n. 2
0
/**
 * Phalcon\Config\Adapter factory
 *
 * @param string $filePath
 * @param string $absolutePath
 */
PHP_METHOD(Phalcon_Config_Adapter, factory){

	zval *file_path = NULL, *absolute_path = NULL, *instances, *instance = NULL, *class_name;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 2, &file_path, &absolute_path);

	instances = phalcon_read_static_property_ce(phalcon_config_adapter_ce, SL("_instances"));
	if (!phalcon_array_isset_fetch(&instance, instances, file_path)) {
		PHALCON_INIT_VAR(class_name);
		phalcon_get_called_class(class_name );
		ce0 = phalcon_fetch_class(class_name);

		object_init_ex(return_value, ce0);

		if (file_path) {
			if (absolute_path == NULL) {
				absolute_path = &PHALCON_GLOBAL(z_false);
			}

			PHALCON_CALL_METHOD(NULL, return_value, "__construct", file_path, absolute_path);
		} else {
			PHALCON_CALL_METHOD(NULL, return_value, "__construct");
		}

		phalcon_update_static_property_array_multi_ce(phalcon_config_adapter_ce, SL("_instances"), return_value, SL("z"), 1, file_path);
		RETURN_MM();
	}

	RETURN_CTOR(instance);
}
Esempio n. 3
0
/**
 * Load config file
 *
 * @param string $filePath
 */
PHP_METHOD(Phalcon_Config_Adapter_Yaml, read){

	zval *file_path, *absolute_path = NULL, config_dir_path = {}, *base_path = NULL, config = {};

	phalcon_fetch_params(0, 1, 1, &file_path, &absolute_path);
	PHALCON_ENSURE_IS_STRING(file_path);

	if (absolute_path == NULL) {
		absolute_path = &PHALCON_GLOBAL(z_false);
	}

	if (zend_is_true(absolute_path)) {
		PHALCON_CPY_WRT_CTOR(&config_dir_path, file_path);
	} else {
		base_path = phalcon_read_static_property_ce(phalcon_config_adapter_ce, SL("_basePath"));

		PHALCON_CONCAT_VV(&config_dir_path, base_path, file_path);
	}

	PHALCON_CALL_FUNCTIONW(&config, "yaml_parse_file", &config_dir_path);

	if (Z_TYPE(config) == IS_ARRAY) {
		PHALCON_CALL_METHODW(NULL, getThis(), "val", &config);
	}

	RETURN_THISW();
}
Esempio n. 4
0
/**
 * Gets base path
 *
 * @return string
 */
PHP_METHOD(Phalcon_Config_Adapter, getBasePath){

	zval *base_path;
	
	base_path = phalcon_read_static_property_ce(phalcon_config_adapter_ce, SL("_basePath"));

	RETURN_ZVAL(base_path, 1, 0);
}
Esempio n. 5
0
/**
 * Phalcon\DI constructor
 *
 */
PHP_METHOD(Phalcon_DI, __construct){

	zval *default_di;

	default_di = phalcon_read_static_property_ce(phalcon_di_ce, SL("_default"));
	if (Z_TYPE_P(default_di) == IS_NULL) {
		phalcon_update_static_property_ce(phalcon_di_ce, SL("_default"), getThis());
	}
}
Esempio n. 6
0
/**
 * Escapes a string with htmlentities
 *
 * @param string $value
 * @return string
 */
PHP_METHOD(Phalcon_Debug, _escapeString) {

    zval *value, *charset, *replaced_value;

    phalcon_fetch_params(0, 1, 0, &value);

    if (Z_TYPE_P(value) == IS_STRING) {
        zval line_break;
        zval escaped_line_break;

        charset = phalcon_read_static_property_ce(phalcon_debug_ce, SL("_charset"));

        ZVAL_STRING(&line_break, "\n");
        ZVAL_STRING(&escaped_line_break, "\\n");

        PHALCON_STR_REPLACE(&replaced_value, &line_break, &escaped_line_break, value);
        phalcon_htmlentities(return_value, replaced_value, NULL, charset);
        zval_ptr_dtor(replaced_value);
        return;
    }

    RETURN_ZVAL(value, 1, 0);
}
Esempio n. 7
0
/**
 * 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      = { 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, getThis(), "getformatter");

	initialized = phalcon_read_static_property_ce(phalcon_logger_adapter_firephp_ce, SL("_initialized"));
	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);

		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);

		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);

		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_read_static_property_ce(phalcon_logger_adapter_firephp_ce, SL("_index"));
	assert(Z_TYPE_P(index) == IS_LONG);

	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.
	 */
	num_bytes = smart_str_alloc(&str, (uint)(size > chunk ? chunk : size), 0);

	while (size > 0) {
		smart_str_appends(&str, "X-Wf-1-1-1-");
		smart_str_append_long(&str, Z_LVAL_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.s->val;
		h.line_len = str.s->len;
		sapi_header_op(SAPI_HEADER_REPLACE, &h);

		ZVAL_LONG(index, Z_LVAL_P(index)+1);

		/**
		 * Do not free and then reallocate memory. Just pretend the string
		 * is empty. We will take care of deallocation later.
		 */
		str.s->len = 0;
	}

	if (separate_index) {
		phalcon_update_static_property_ce(phalcon_logger_adapter_firephp_ce, SL("_index"), index);
	}

	/* Deallocate the smnart string if it is not empty */
	smart_str_free(&str);

	PHALCON_MM_RESTORE();
}
Esempio n. 8
0
PHP_METHOD(Phalcon_Http_Client_Header, build){

	zval *flags = NULL, *messages, *status_code, *message = NULL, *version, *lines, *line = NULL;
	zval *fields, *filed = NULL, *value = NULL, *join_filed;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	int f = 0;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 1, &flags);

	if (flags) {
		f = phalcon_get_intval(flags);
	}

	PHALCON_INIT_VAR(lines);
	array_init(lines);

	PHALCON_OBS_VAR(messages);
	phalcon_read_static_property_ce(&messages, phalcon_http_client_header_ce, SL("_messages") TSRMLS_CC);

	status_code = phalcon_fetch_nproperty_this(this_ptr, SL("_status_code"), PH_NOISY TSRMLS_CC);
	
	if ((f & PHALCON_HTTP_CLIENT_HEADER_BUILD_STATUS) && phalcon_array_isset_fetch(&message, messages, status_code)) {
		version  = phalcon_fetch_nproperty_this(this_ptr, SL("_version "), PH_NOISY TSRMLS_CC);

		PHALCON_INIT_NVAR(line);
		PHALCON_CONCAT_SVS(line, "HTTP/", version, " ");
		PHALCON_SCONCAT_VSV(line, status_code, " ", message);

		phalcon_merge_append(lines, line);

	}

	fields = phalcon_fetch_nproperty_this(this_ptr, SL("_fields"), PH_NOISY TSRMLS_CC);

	phalcon_is_iterable(fields, &ah0, &hp0, 0, 0);	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_HKEY(filed, ah0, hp0);
		PHALCON_GET_HVALUE(value);

		PHALCON_INIT_NVAR(line);
		PHALCON_CONCAT_VSV(line, filed, ": ", value);

		phalcon_merge_append(lines, line);

		zend_hash_move_forward_ex(ah0, &hp0);
	}

	if (f & PHALCON_HTTP_CLIENT_HEADER_BUILD_FIELDS) {
		PHALCON_INIT_VAR(join_filed);
		phalcon_fast_join_str(join_filed, SL("\r\n"), lines TSRMLS_CC);

		RETURN_CCTOR(join_filed);
	}

	RETURN_CCTOR(lines);
}