Esempio n. 1
0
/**
 * Phalcon\Http\Uri constructor
 *
 * @param mixed $uri
 */
PHP_METHOD(Phalcon_Http_Uri, __construct){

	zval *uri = NULL, *parts = NULL, *query, *params;

	PHALCON_MM_GROW();

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

	if (!uri || PHALCON_IS_EMPTY(uri)) {
		phalcon_update_property_empty_array(getThis(), SL("_parts"));
	} else if (Z_TYPE_P(uri) == IS_STRING) {
		PHALCON_CALL_FUNCTION(&parts, "parse_url", uri);
		if (phalcon_array_isset_str_fetch(&query, parts, SL("query"))) {
			PHALCON_INIT_VAR(params);
			ZVAL_MAKE_REF(params);
			PHALCON_CALL_FUNCTION(NULL, "parse_str", query, params);
			ZVAL_UNREF(params);
			phalcon_array_update_str(parts, SL("query"), params, PH_COPY);
		}

		phalcon_update_property_this(getThis(), SL("_parts"), parts);
	} else if (Z_TYPE_P(uri) == IS_ARRAY) {
		phalcon_update_property_this(getThis(), SL("_parts"), uri);
	} else if (Z_TYPE_P(uri) == IS_OBJECT && Z_OBJCE_P(uri) == phalcon_http_uri_ce) {
		parts = phalcon_read_property(uri, SL("_parts"), PH_NOISY);
		phalcon_update_property_this(getThis(), SL("_parts"), parts);
	} else {
		phalcon_update_property_empty_array(getThis(), SL("_parts"));
	}

	PHALCON_MM_RESTORE();
}
Esempio n. 2
0
/**
 * Returns the numeric active version
 *
 * <code>
 * echo Phalcon\Version::getId();
 * </code>
 *
 * @return int
 */
PHP_METHOD(Phalcon_Version, getId){

	zval *version = NULL, *major, *medium, *minor, *special, *special_number;
	zval *format, *real_medium = NULL, *real_minor = NULL;

	PHALCON_MM_GROW();

	PHALCON_CALL_SELF(&version, "_getversion");

	PHALCON_OBS_VAR(major);
	phalcon_array_fetch_long(&major, version, 0, PH_NOISY);

	PHALCON_OBS_VAR(medium);
	phalcon_array_fetch_long(&medium, version, 1, PH_NOISY);

	PHALCON_OBS_VAR(minor);
	phalcon_array_fetch_long(&minor, version, 2, PH_NOISY);

	PHALCON_OBS_VAR(special);
	phalcon_array_fetch_long(&special, version, 3, PH_NOISY);

	PHALCON_OBS_VAR(special_number);
	phalcon_array_fetch_long(&special_number, version, 4, PH_NOISY);

	PHALCON_INIT_VAR(format);
	ZVAL_STRING(format, "%02s", 1);

	PHALCON_CALL_FUNCTION(&real_medium, "sprintf", format, medium);

	PHALCON_CALL_FUNCTION(&real_minor, "sprintf", format, minor);
	PHALCON_CONCAT_VVVVV(return_value, major, real_medium, real_minor, special, special_number);
	RETURN_MM();
}
Esempio n. 3
0
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Xcache, reset)
{
	zval *meta = phalcon_fetch_nproperty_this(this_ptr, SL("_metaData"), PH_NOISY TSRMLS_CC);
	zval *real_key = NULL;

	PHALCON_MM_GROW();

	if (SUCCESS == phalcon_function_exists_ex(SL("xcache_unset_by_prefix") TSRMLS_CC)) {
		zval *prefix = phalcon_fetch_nproperty_this(this_ptr, SL("_prefix"), PH_NOISY TSRMLS_CC);

		PHALCON_INIT_VAR(real_key);
		phalcon_concat_svs(&real_key, SL("$PMM$"), prefix, SL("meta-"), 0 TSRMLS_CC);
		PHALCON_CALL_FUNCTION(NULL, "xcache_unset_by_prefix", real_key);
	}
	else if (Z_TYPE_P(meta) == IS_ARRAY) {
		HashTable *ht = Z_ARRVAL_P(meta);
		HashPosition hp;
		zval *prefix = phalcon_fetch_nproperty_this(this_ptr, SL("_prefix"), PH_NOISY TSRMLS_CC);

		for (
			zend_hash_internal_pointer_reset_ex(ht, &hp);
			zend_hash_get_current_key_type_ex(ht, &hp) != HASH_KEY_NON_EXISTENT;
			zend_hash_move_forward_ex(ht, &hp)
		) {
			zval key = phalcon_get_current_key_w(ht, &hp);

			PHALCON_INIT_NVAR(real_key);
			phalcon_concat_svsv(&real_key, SL("$PMM$"), prefix, SL("meta-"), &key, 0 TSRMLS_CC);
			PHALCON_CALL_FUNCTION(NULL, "xcache_unset", real_key);
		}
	}

	PHALCON_CALL_PARENT(NULL, phalcon_mvc_model_metadata_xcache_ce, getThis(), "reset");
	PHALCON_MM_RESTORE();
}
Esempio n. 4
0
/**
 * Gets the real mime type of the upload file using finfo
 *
 * @return string
 */
PHP_METHOD(Phalcon_Http_Request_File, getRealType){

	zval *constant, *finfo = NULL, *temp_file, *mime;

	PHALCON_MM_GROW();

	mime = phalcon_fetch_nproperty_this(this_ptr, SL("_real_type"), PH_NOISY TSRMLS_CC);

	if (Z_TYPE_P(mime) == IS_STRING) {
		RETURN_CTOR(mime);
	}

	PHALCON_INIT_VAR(constant);
	if (!zend_get_constant(SL("FILEINFO_MIME_TYPE"), constant TSRMLS_CC)) {
		RETURN_MM_NULL();
	}

	PHALCON_CALL_FUNCTION(&finfo, "finfo_open", constant);

	if (Z_TYPE_P(finfo) != IS_RESOURCE) {
		RETURN_MM_NULL();
	}

	temp_file = phalcon_fetch_nproperty_this(this_ptr, SL("_tmp"), PH_NOISY TSRMLS_CC);

	PHALCON_RETURN_CALL_FUNCTION("finfo_file", finfo, temp_file);
	PHALCON_CALL_FUNCTION(NULL, "finfo_close", finfo);

	PHALCON_MM_RESTORE();
}
Esempio n. 5
0
PHP_METHOD(Phalcon_Http_Client_Adapter_Stream, __construct){

	zval *uri = NULL, *method = NULL, *upper_method, *header, *stream = NULL, *http, *option, *value;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &uri, &method);

	PHALCON_CALL_SELF(NULL, "setbaseuri", uri);

	if (method) {
		PHALCON_INIT_VAR(upper_method);
		phalcon_fast_strtoupper(upper_method, method);
		phalcon_update_property_this(this_ptr, SL("_method"), upper_method TSRMLS_CC);
	}

	PHALCON_INIT_VAR(header);
	object_init_ex(header, phalcon_http_client_header_ce);
	PHALCON_CALL_METHOD(NULL, header, "__construct");

	PHALCON_CALL_FUNCTION(&stream, "stream_context_create");

	PHALCON_INIT_VAR(http);
	ZVAL_STRING(http, "http", 1);

	PHALCON_INIT_VAR(option);
	ZVAL_STRING(option, "user_agent", 1);

	PHALCON_INIT_VAR(value);
	ZVAL_STRING(value, "Phalcon HTTP Client(Stream)", 1);

	PHALCON_CALL_METHOD(NULL, header, "set", option, value);

	phalcon_update_property_this(this_ptr, SL("_header"), header TSRMLS_CC);

	PHALCON_INIT_NVAR(option);
	ZVAL_STRING(option, "follow_location", 1);

	PHALCON_INIT_NVAR(value);
	ZVAL_LONG(value, 1);

	PHALCON_CALL_FUNCTION(NULL, "stream_context_set_option", stream, http, option, value);

	PHALCON_INIT_NVAR(option);
	ZVAL_STRING(option, "max_redirects", 1);

	PHALCON_INIT_NVAR(value);
	ZVAL_LONG(value, 20);

	PHALCON_CALL_FUNCTION(NULL, "stream_context_set_option", stream, http, option, value);

	phalcon_update_property_this(this_ptr, SL("_stream"), stream TSRMLS_CC);

	PHALCON_MM_RESTORE();
}
Esempio n. 6
0
/**
 * Concatenates strings using the separator only once without duplication in places concatenation
 *
 * <code>
 *    $str = Phalcon\Text::concat("/", "/tmp/", "/folder_1/", "/folder_2", "folder_3/");
 *    echo $str; // /tmp/folder_1/folder_2/folder_3/
 * </code>
 *
 * @param string separator
 * @param string a
 * @param string b
 * @param string ...N
 */
PHP_METHOD(Phalcon_Text, concat){

	zval *separator, *a, *b;
	zval *arg_num = NULL, *arg_list = NULL, *offset, *args = NULL;
	zval *c = NULL, *a_trimmed = NULL, *b_trimmed = NULL, *c_trimmed = NULL, *tmp = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 3, 0, &separator, &a, &b);

	PHALCON_CALL_FUNCTION(&arg_num, "func_num_args");

	if (Z_LVAL_P(arg_num) > 3) {
		PHALCON_CALL_FUNCTION(&arg_list, "func_get_args");

		PHALCON_INIT_VAR(offset);
		ZVAL_LONG(offset, 3);

		PHALCON_CALL_FUNCTION(&args, "array_slice", arg_list, offset);

		phalcon_is_iterable(args, &ah0, &hp0, 0, 0);

		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

			PHALCON_GET_HVALUE(c);

			PHALCON_INIT_NVAR(b_trimmed);
			phalcon_fast_trim(b_trimmed, b, separator, PHALCON_TRIM_RIGHT TSRMLS_CC);

			PHALCON_INIT_NVAR(c_trimmed);
			phalcon_fast_trim(c_trimmed, c, separator, PHALCON_TRIM_LEFT TSRMLS_CC);

			PHALCON_INIT_NVAR(tmp);
			PHALCON_CONCAT_VVV(tmp, b_trimmed, separator, c_trimmed)

			PHALCON_CPY_WRT(b, tmp);

			zend_hash_move_forward_ex(ah0, &hp0);
		}
	}

	PHALCON_INIT_NVAR(a_trimmed);
	phalcon_fast_trim(a_trimmed, a, separator, PHALCON_TRIM_RIGHT TSRMLS_CC);

	PHALCON_INIT_NVAR(b_trimmed);
	phalcon_fast_trim(b_trimmed, b, separator, PHALCON_TRIM_LEFT TSRMLS_CC);

	PHALCON_INIT_NVAR(tmp);
	PHALCON_CONCAT_VVV(tmp, a_trimmed, separator, b_trimmed)

	RETURN_CTOR(tmp);
}
Esempio n. 7
0
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Apc, reset)
{
	zval *real_key = NULL, *meta;
	zend_string *str_key;
	ulong idx;

	PHALCON_MM_GROW();

	meta = phalcon_read_property(getThis(), SL("_metaData"), PH_NOISY);

	if (Z_TYPE_P(meta) == IS_ARRAY) {
		zval *prefix = phalcon_read_property(getThis(), SL("_prefix"), PH_NOISY);

		ZEND_HASH_FOREACH_KEY(Z_ARRVAL_P(meta), idx, str_key) {
			zval key;
			if (str_key) {
				ZVAL_STR(&key, str_key);
			} else {
				ZVAL_LONG(&key, idx);
			}

			PHALCON_INIT_NVAR(real_key);
			phalcon_concat_svsv(real_key, SL("$PMM$"), prefix, SL("meta-"), &key, 0);
			PHALCON_CALL_FUNCTION(NULL, "apc_delete", real_key);
		} ZEND_HASH_FOREACH_END();
Esempio n. 8
0
/**
 * Returns cached content
 *
 * @param string $keyName
 * @param long $lifetime
 * @return mixed
 */
PHP_METHOD(Phalcon_Cache_Backend_Xcache, get){

	zval *key_name, *lifetime = NULL, *frontend, *prefix, *prefixed_key;
	zval *cached_content = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &key_name, &lifetime);
	
	frontend = phalcon_read_property(getThis(), SL("_frontend"), PH_NOISY);
	prefix   = phalcon_read_property(getThis(), SL("_prefix"), PH_NOISY);
	
	PHALCON_INIT_VAR(prefixed_key);
	PHALCON_CONCAT_SVV(prefixed_key, "_PHCX", prefix, key_name);
	phalcon_update_property_this(getThis(), SL("_lastKey"), prefixed_key);
	
	PHALCON_CALL_FUNCTION(&cached_content, "xcache_get", prefixed_key);
	if (Z_TYPE_P(cached_content) == IS_NULL) {
		RETURN_MM_NULL();
	}

	if (phalcon_is_numeric(cached_content)) {
		RETURN_CCTOR(cached_content);
	} else {
		PHALCON_RETURN_CALL_METHOD(frontend, "afterretrieve", cached_content);
	}
	
	RETURN_MM();
}
Esempio n. 9
0
/**
 * Writes the log to the stream itself
 *
 * @param string $message
 * @param int $type
 * @param int $time
 * @param array $context
 */
PHP_METHOD(Phalcon_Logger_Adapter_Syslog, logInternal){

	zval *message, *type, *time, *context, *formatter = NULL, *applied_format = NULL;
	zval *syslog_type, *syslog_message;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 4, 0, &message, &type, &time, &context);

	PHALCON_CALL_METHOD(&formatter, getThis(), "getformatter");
	PHALCON_CALL_METHOD(&applied_format, formatter, "format", message, type, time, context);
	if (Z_TYPE_P(applied_format) != IS_ARRAY) { 
		syslog_type    = type;
		syslog_message = applied_format;
	}
	else {
		PHALCON_OBS_VAR(syslog_type);
		phalcon_array_fetch_long(&syslog_type, applied_format, 0, PH_NOISY);

		PHALCON_OBS_VAR(syslog_message);
		phalcon_array_fetch_long(&syslog_message, applied_format, 1, PH_NOISY);
	}

	PHALCON_CALL_FUNCTION(NULL, "syslog", syslog_type, syslog_message);
	PHALCON_MM_RESTORE();
}
Esempio n. 10
0
/**
 * Phalcon\Translate\Adapter\Gettext constructor
 *
 * @param array $options
 * @throws \Phalcon\Translate\Exception
 */
PHP_METHOD(Phalcon_Translate_Adapter_Gettext, __construct){

	zval *options, *locale, *default_domain, *directory, *setting, *key = NULL, *value = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &options);
	
	if (Z_TYPE_P(options) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STRW(phalcon_translate_exception_ce, "Invalid options");
		return;
	}

	if (!phalcon_array_isset_string_fetch(&locale, options, SS("locale"))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_translate_exception_ce, "Parameter \"locale\" is required");
		return;
	}

	if (!phalcon_array_isset_string_fetch(&default_domain, options, SS("defaultDomain"))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_translate_exception_ce, "Parameter \"defaultDomain\" is required");
		return;
	}

	if (!phalcon_array_isset_string_fetch(&directory, options, SS("directory"))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_translate_exception_ce, "Parameter \"directory\" is required");
		return;
	}

	phalcon_update_property_this(this_ptr, SL("_locale"), locale TSRMLS_CC);
	phalcon_update_property_this(this_ptr, SL("_defaultDomain"), default_domain TSRMLS_CC);
	phalcon_update_property_this(this_ptr, SL("_directory"), directory TSRMLS_CC);

	PHALCON_INIT_VAR(setting);
	PHALCON_CONCAT_SV(setting, "LC_ALL=", locale);

	PHALCON_CALL_FUNCTION(NULL, "putenv", setting);

	setlocale(LC_ALL, Z_STRVAL_P(locale));
	
	if (Z_TYPE_P(directory) == IS_ARRAY) {
		phalcon_is_iterable(directory, &ah0, &hp0, 0, 0);
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
			PHALCON_GET_HKEY(key, ah0, hp0);
			PHALCON_GET_HVALUE(value);

			bindtextdomain(Z_STRVAL_P(key), Z_STRVAL_P(value));

			zend_hash_move_forward_ex(ah0, &hp0);
		}
	} else {
		bindtextdomain(Z_STRVAL_P(default_domain), Z_STRVAL_P(directory));
	}

	textdomain(Z_STRVAL_P(default_domain));

	PHALCON_MM_RESTORE();
}
Esempio n. 11
0
/**
 * Executes the validation
 *
 * @param Phalcon\Validation $validator
 * @param string $attribute
 * @return boolean
 */
PHP_METHOD(Phalcon_Validation_Validator_Url, validate){

	zval *validator, *attribute, *value = NULL, *allow_empty, *valid = NULL, *label;
	zval *pairs, *message_str, *code, *prepared = NULL, *message;
	zend_class_entry *ce = Z_OBJCE_P(getThis());

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &validator, &attribute);
	
	PHALCON_VERIFY_CLASS_EX(validator, phalcon_validation_ce, phalcon_validation_exception_ce, 1);

	PHALCON_CALL_METHOD(&value, validator, "getvalue", attribute);
	
	PHALCON_OBS_VAR(allow_empty);
	RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &allow_empty, getThis(), ISV(allowEmpty)));
	if (zend_is_true(allow_empty) && phalcon_validation_validator_isempty_helper(value)) {
		RETURN_MM_TRUE;
	}

	PHALCON_CALL_SELF(&valid, "valid", value);

	if (PHALCON_IS_FALSE(valid)) {
		PHALCON_OBS_VAR(label);
		RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &label, getThis(), ISV(label)));
		if (!zend_is_true(label)) {
			PHALCON_CALL_METHOD(&label, validator, "getlabel", attribute);
			if (!zend_is_true(label)) {
				PHALCON_CPY_WRT(label, attribute);
			}
		}

		PHALCON_ALLOC_INIT_ZVAL(pairs);
		array_init_size(pairs, 1);
		Z_TRY_ADDREF_P(label); add_assoc_zval_ex(pairs, SL(":field"), label);

		PHALCON_OBS_VAR(message_str);
		RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &message_str, getThis(), ISV(message)));
		if (!zend_is_true(message_str)) {
			PHALCON_OBSERVE_OR_NULLIFY_VAR(message_str);
			RETURN_MM_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(Z_OBJCE_P(validator), message_str, validator, "Url"));
		}
	
		PHALCON_OBS_VAR(code);
		RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &code, getThis(), ISV(code)));
		if (Z_TYPE_P(code) == IS_NULL) {
			ZVAL_LONG(code, 0);
		}

		PHALCON_CALL_FUNCTION(&prepared, "strtr", message_str, pairs);

		message = phalcon_validation_message_construct_helper(prepared, attribute, "Url", code);
		Z_TRY_DELREF_P(message);
	
		PHALCON_CALL_METHOD(NULL, validator, "appendmessage", message);
		RETURN_MM_FALSE;
	}
	
	RETURN_MM_TRUE;
}
Esempio n. 12
0
/**
 * Phalcon\Http\Request\File constructor
 *
 * @param array $file
 */
PHP_METHOD(Phalcon_Http_Request_File, __construct){

	zval *file, *name, *temp_name, *size, *type, *error, *key = NULL;
	zval *constant, *extension = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &file, &key);
	
	if (Z_TYPE_P(file) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_http_request_exception_ce, "Phalcon\\Http\\Request\\File requires a valid uploaded file");
		return;
	}
	if (phalcon_array_isset_string(file, SS("name"))) {
		PHALCON_OBS_VAR(name);
		phalcon_array_fetch_string(&name, file, SL("name"), PH_NOISY);
		phalcon_update_property_this(this_ptr, SL("_name"), name TSRMLS_CC);

		PHALCON_INIT_VAR(constant);
		if (zend_get_constant(SL("PATHINFO_EXTENSION"), constant TSRMLS_CC)) {
			PHALCON_CALL_FUNCTION(&extension, "pathinfo", name, constant);
			phalcon_update_property_this(this_ptr, SL("_extension"), extension TSRMLS_CC);
		}
	}
	
	if (phalcon_array_isset_string(file, SS("tmp_name"))) {
		PHALCON_OBS_VAR(temp_name);
		phalcon_array_fetch_string(&temp_name, file, SL("tmp_name"), PH_NOISY);
		phalcon_update_property_this(this_ptr, SL("_tmp"), temp_name TSRMLS_CC);
	} else {
		PHALCON_INIT_VAR(temp_name);
		ZVAL_NULL(temp_name);
 	}
	
	if (phalcon_array_isset_string(file, SS("size"))) {
		PHALCON_OBS_VAR(size);
		phalcon_array_fetch_string(&size, file, SL("size"), PH_NOISY);
		phalcon_update_property_this(this_ptr, SL("_size"), size TSRMLS_CC);
	}
	
	if (phalcon_array_isset_string(file, SS("type"))) {
		PHALCON_OBS_VAR(type);
		phalcon_array_fetch_string(&type, file, SL("type"), PH_NOISY);
		phalcon_update_property_this(this_ptr, SL("_type"), type TSRMLS_CC);
	}

	if (phalcon_array_isset_string(file, SS("error"))) {
		PHALCON_OBS_VAR(error);
		phalcon_array_fetch_string(&error, file, SL("error"), PH_NOISY);
		phalcon_update_property_this(this_ptr, SL("_error"), error TSRMLS_CC);
	}

	if (key) {
		phalcon_update_property_this(this_ptr, SL("_key"), key TSRMLS_CC);
	}

	PHALCON_CALL_PARENT(NULL, phalcon_http_request_file_ce, this_ptr, "__construct", temp_name);

	PHALCON_MM_RESTORE();
}
Esempio n. 13
0
/**
 * Starts the profile of a SQL sentence
 *
 * @param string $sqlStatement
 * @param $sqlVariables
 * @param $sqlBindTypes
 * @return Phalcon\Db\Profiler
 */
PHP_METHOD(Phalcon_Db_Profiler, startProfile){

	zval *sql_statement, *sql_variables = NULL, *sql_bindtypes = NULL, *active_profile;
	zval *time = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 2, &sql_statement, &sql_variables, &sql_bindtypes);
	
	PHALCON_INIT_VAR(active_profile);
	object_init_ex(active_profile, phalcon_db_profiler_item_ce);
	PHALCON_CALL_METHOD(NULL, active_profile, "setsqlstatement", sql_statement);

	if (sql_variables) {
	    PHALCON_CALL_METHOD(NULL, active_profile, "setsqlvariables", sql_variables);
	}

	if (sql_bindtypes) {
	    PHALCON_CALL_METHOD(NULL, active_profile, "setsqlbindtypes", sql_bindtypes);
	}

	PHALCON_CALL_FUNCTION(&time, "microtime", PHALCON_GLOBAL(z_true));
	PHALCON_CALL_METHOD(NULL, active_profile, "setinitialtime", time);

	if (phalcon_method_exists_ex(this_ptr, SS("beforestartprofile") TSRMLS_CC) == SUCCESS) {
		PHALCON_CALL_METHOD(NULL, this_ptr, "beforestartprofile", active_profile);
	}
	
	phalcon_update_property_this(this_ptr, SL("_activeProfile"), active_profile TSRMLS_CC);
	
	RETURN_THIS();
}
Esempio n. 14
0
/**
 * Stops the active profile
 *
 * @return Phalcon\Db\Profiler
 */
PHP_METHOD(Phalcon_Db_Profiler, stopProfile){

	zval *final_time = NULL, *active_profile, *initial_time = NULL;
	zval *difference, *total_seconds, *new_total_seconds;

	PHALCON_MM_GROW();

	PHALCON_CALL_FUNCTION(&final_time, "microtime", PHALCON_GLOBAL(z_true));
	
	active_profile = phalcon_fetch_nproperty_this(this_ptr, SL("_activeProfile"), PH_NOISY TSRMLS_CC);
	PHALCON_CALL_METHOD(NULL, active_profile, "setfinaltime", final_time);
	
	PHALCON_CALL_METHOD(&initial_time, active_profile, "getinitialtime");
	
	PHALCON_INIT_VAR(difference);
	sub_function(difference, final_time, initial_time TSRMLS_CC);
	
	total_seconds = phalcon_fetch_nproperty_this(this_ptr, SL("_totalSeconds"), PH_NOISY TSRMLS_CC);
	
	PHALCON_INIT_VAR(new_total_seconds);
	phalcon_add_function(new_total_seconds, total_seconds, difference TSRMLS_CC);
	phalcon_update_property_this(this_ptr, SL("_totalSeconds"), new_total_seconds TSRMLS_CC);
	phalcon_update_property_array_append(this_ptr, SL("_allProfiles"), active_profile TSRMLS_CC);
	if (phalcon_method_exists_ex(this_ptr, SS("afterendprofile") TSRMLS_CC) == SUCCESS) {
		PHALCON_CALL_METHOD(NULL, this_ptr, "afterendprofile", active_profile);
	}
	
	RETURN_THIS();
}
Esempio n. 15
0
/**
 * Handles user-defined error
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Debug, onShutdown){

	zval *error = NULL, *message, *type, *file, *line, *exception;
	zend_class_entry *default_exception_ce;

	PHALCON_MM_GROW();

	PHALCON_CALL_FUNCTION(&error, "error_get_last");

	if (
		phalcon_array_isset_string_fetch(&message, error, SS("message")) && 
		phalcon_array_isset_string_fetch(&type, error, SS("type")) && 
		phalcon_array_isset_string_fetch(&file, error, SS("file")) && 
		phalcon_array_isset_string_fetch(&line, error, SS("line"))
		
	) {
		default_exception_ce = zend_get_error_exception(TSRMLS_C);

		ALLOC_INIT_ZVAL(exception);
		object_init_ex(exception, default_exception_ce);

		PHALCON_CALL_METHOD(NULL, exception, "__construct", message, PHALCON_GLOBAL(z_zero), type, file, line);

		zend_throw_exception_object(exception TSRMLS_CC);
	}

	RETURN_MM_TRUE;
}
Esempio n. 16
0
/**
 * Returns an HTML string of debugging information about any number of
 * variables, each wrapped in a "pre" tag.
 *
 * <code>
 *    $foo = "string";
 *    $bar = ["key" => "value"];
 *    $baz = new stdClass();
 *    echo (new \Phalcon\Debug\Dump())->variables($foo, $bar, $baz);
 *</code>
 *
 * @param mixed variable
 * @param ...
 */
PHP_METHOD(Phalcon_Debug_Dump, variables){

	zval *arg_list = NULL, *name = NULL, *variable = NULL, *output = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	PHALCON_CALL_FUNCTION(&arg_list, "func_get_args");

	phalcon_is_iterable(arg_list, &ah0, &hp0, 0, 0);

	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

		PHALCON_GET_HKEY(name, ah0, hp0);
		PHALCON_GET_HVALUE(variable);

		PHALCON_CALL_SELF(&output, "variable", variable);
		PHALCON_SCONCAT(return_value, output);

		zend_hash_move_forward_ex(ah0, &hp0);
	}

	RETURN_MM();
}
Esempio n. 17
0
PHP_METHOD(Phalcon_Mvc_Collection_GridFS, saveBytes){

	zval *bytes, *arr = NULL, *white_list = NULL, *mode = NULL;
	zval *old_sha1, *old_md5, *sha1 = NULL, *md5 = NULL, *status = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 3, &bytes, &arr, &white_list, &mode);

	if (!arr) {
		arr = &PHALCON_GLOBAL(z_null);
	}

	if (!white_list) {
		white_list = &PHALCON_GLOBAL(z_null);
	}

	if (!mode) {
		mode = &PHALCON_GLOBAL(z_null);
	}

	if (zend_is_true(bytes)) {
		old_sha1 = phalcon_read_property(getThis(), SL("sha1"), PH_NOISY);
		old_md5 = phalcon_read_property(getThis(), SL("md5"), PH_NOISY);

		PHALCON_CALL_FUNCTION(&sha1, "sha1", bytes);
		PHALCON_CALL_FUNCTION(&md5, "md5", bytes);

		phalcon_update_property_this(getThis(), SL("sha1"), sha1);
		phalcon_update_property_this(getThis(), SL("md5"), md5);
	}

	PHALCON_CALL_PARENT(&status, phalcon_mvc_collection_gridfs_ce, getThis(), "save", arr, white_list, mode);

	if (PHALCON_IS_FALSE(status)) {
		RETURN_MM_FALSE;
	}

	if (zend_is_true(bytes)) {
		PHALCON_CALL_SELF(&status, "store", bytes, &PHALCON_GLOBAL(z_null), &PHALCON_GLOBAL(z_null), &PHALCON_GLOBAL(z_true));
		if (zend_is_true(old_sha1)) {
			PHALCON_CALL_SELF(NULL, "remove", old_sha1, old_md5);
		}
	}

	RETURN_CTOR(status);
}
Esempio n. 18
0
/**
 * Listen for unsilent notices or warnings or user-defined error
 *
 * @return Phalcon\Debug
 */
PHP_METHOD(Phalcon_Debug, listenLowSeverity) {

    zval *handler;

    PHALCON_MM_GROW();

    PHALCON_INIT_VAR(handler);
    array_init_size(handler, 2);
    phalcon_array_append(handler, getThis(), PH_COPY);
    add_next_index_stringl(handler, SL("onUserDefinedError"));
    PHALCON_CALL_FUNCTION(NULL, "set_error_handler", handler);

    PHALCON_INIT_NVAR(handler);
    array_init_size(handler, 2);
    phalcon_array_append(handler, getThis(), PH_COPY);
    add_next_index_stringl(handler, SL("onShutdown"));
    PHALCON_CALL_FUNCTION(NULL, "register_shutdown_function", handler);

    RETURN_THIS();
}
Esempio n. 19
0
/**
 * Executes the validation
 *
 * @param string $value
 * @param int $minimum
 * @param int $maximum
 * @return boolean
 */
PHP_METHOD(Phalcon_Validation_Validator_StringLength, valid){

	zval *value, *minimum = NULL, *maximum = NULL;
	zval *length = NULL, *valid = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 2, &value, &minimum, &maximum);

	if (!minimum) {
		minimum = &PHALCON_GLOBAL(z_null);
	}

	if (!maximum) {
		maximum = &PHALCON_GLOBAL(z_null);
	}

	/* At least one of 'min' or 'max' must be set */
	if (Z_TYPE_P(minimum) == IS_NULL && Z_TYPE_P(maximum) == IS_NULL) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "A minimum or maximum must be set");
		return;
	}

	/* Check if mbstring is available to calculate the correct length */
	if (phalcon_function_exists_ex(SL("mb_strlen")) == SUCCESS) {
		PHALCON_CALL_FUNCTION(&length, "mb_strlen", value);
	} else {
		convert_to_string(value);
		PHALCON_INIT_VAR(length);
		ZVAL_LONG(length, Z_STRLEN_P(value));
	}

	/* Maximum length */
	if (Z_TYPE_P(maximum) != IS_NULL) {
		PHALCON_INIT_NVAR(valid);
		is_smaller_function(valid, maximum, length);
		if (PHALCON_IS_TRUE(valid)) {
			phalcon_update_property_str(getThis(), SL("_type"), SL("TooLong"));
			RETURN_MM_FALSE;
		}
	}

	/* Minimum length */
	if (Z_TYPE_P(minimum) != IS_NULL) {
		PHALCON_INIT_NVAR(valid);
		is_smaller_function(valid, length, minimum);
		if (PHALCON_IS_TRUE(valid)) {
			phalcon_update_property_str(getThis(), SL("_type"), SL("TooShort"));
			RETURN_MM_FALSE;
		}
	}

	RETURN_MM_TRUE;
}
Esempio n. 20
0
/**
 * Listen for uncaught exceptions
 *
 * @return Phalcon\Debug
 */
PHP_METHOD(Phalcon_Debug, listenExceptions) {

    zval *handler;

    PHALCON_MM_GROW();

    PHALCON_INIT_VAR(handler);
    array_init_size(handler, 2);
    phalcon_array_append(handler, getThis(), PH_COPY);
    add_next_index_stringl(handler, SL("onUncaughtException"));
    PHALCON_CALL_FUNCTION(NULL, "set_exception_handler", handler);
    RETURN_THIS();
}
Esempio n. 21
0
/**
 * Writes the meta-data to APC
 *
 * @param string $key
 * @param array $data
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Apc, write){

	zval *key, *data, *prefix, *apc_key, *ttl;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &key, &data);

	prefix = phalcon_read_property(getThis(), SL("_prefix"), PH_NOISY);

	PHALCON_INIT_VAR(apc_key);
	PHALCON_CONCAT_SVV(apc_key, "$PMM$", prefix, key);

	ttl = phalcon_read_property(getThis(), SL("_ttl"), PH_NOISY);
	PHALCON_CALL_FUNCTION(NULL, "apc_store", apc_key, data, ttl);

	PHALCON_MM_RESTORE();
}
Esempio n. 22
0
/**
 * Executes the validation
 *
 * @param string $value
 * @return boolean
 */
PHP_METHOD(Phalcon_Validation_Validator_Url, valid){

	zval *value, *validate_url, *valid = NULL;

	PHALCON_MM_GROW();

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

	PHALCON_ALLOC_INIT_ZVAL(validate_url);
	ZVAL_LONG(validate_url, 273);
	
	PHALCON_CALL_FUNCTION(&valid, "filter_var", value, validate_url);
	if (!zend_is_true(valid)) {
		RETURN_MM_FALSE;
	}
	
	RETURN_MM_TRUE;
}
Esempio n. 23
0
/**
 *  Writes the metadata to XCache
 *
 * @param string $key
 * @param array $data
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Xcache, write){

	zval *key, *data, *prefix, *xc_key, *ttl;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &key, &data);
	
	prefix = phalcon_fetch_nproperty_this(this_ptr, SL("_prefix"), PH_NOISY TSRMLS_CC);
	
	PHALCON_INIT_VAR(xc_key);
	PHALCON_CONCAT_SVV(xc_key, "$PMM$", prefix, key);
	
	PHALCON_OBS_VAR(ttl);
	phalcon_read_property_this(&ttl, this_ptr, SL("_ttl"), PH_NOISY TSRMLS_CC);
	PHALCON_CALL_FUNCTION(NULL, "xcache_set", xc_key, data, ttl);
	
	PHALCON_MM_RESTORE();
}
Esempio n. 24
0
/**
 * Phalcon\Logger\Adapter\Syslog constructor
 *
 * @param string $name
 * @param array $options
 */
PHP_METHOD(Phalcon_Logger_Adapter_Syslog, __construct){

	zval *name, *options = NULL, *option = NULL, *facility = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &name, &options);

	if (!options) {
		options = &PHALCON_GLOBAL(z_null);
	}

	/** 
	 * We use 'fopen' to respect to open-basedir directive
	 */
	if (zend_is_true(name)) {
		if (phalcon_array_isset_str(options, SL("option"))) {
			PHALCON_OBS_VAR(option);
			phalcon_array_fetch_str(&option, options, SL("option"), PH_NOISY);
		} else {
			/** 
			 * Open the log in LOG_ODELAY mode
			 */
			PHALCON_INIT_NVAR(option);
			ZVAL_LONG(option, 4);
		}
		if (phalcon_array_isset_str(options, SL("facility"))) {
			PHALCON_OBS_VAR(facility);
			phalcon_array_fetch_str(&facility, options, SL("facility"), PH_NOISY);
		} else {
			/** 
			 * By default the facility is LOG_USER
			 */
			PHALCON_INIT_NVAR(facility);
			ZVAL_LONG(facility, 8);
		}

		PHALCON_CALL_FUNCTION(NULL, "openlog", name, option, facility);
		phalcon_update_property_bool(getThis(), SL("_opened"), 1);
	}

	PHALCON_MM_RESTORE();
}
Esempio n. 25
0
/**
 * Reads metadata from XCache
 *
 * @param  string $key
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Xcache, read){

	zval *key, *prefix, *xc_key, *data = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &key);
	
	prefix = phalcon_fetch_nproperty_this(this_ptr, SL("_prefix"), PH_NOISY TSRMLS_CC);
	
	PHALCON_INIT_VAR(xc_key);
	PHALCON_CONCAT_SVV(xc_key, "$PMM$", prefix, key);
	
	PHALCON_CALL_FUNCTION(&data, "xcache_get", xc_key);
	if (Z_TYPE_P(data) == IS_ARRAY) { 
		RETURN_CCTOR(data);
	}
	
	RETURN_MM_NULL();
}
Esempio n. 26
0
/**
 * Alias of variables() method
 *
 * @param mixed variable
 * @param ...
 */
PHP_METHOD(Phalcon_Debug_Dump, all){

	zval *method_name, *call_object, *arg_list = NULL;

	PHALCON_MM_GROW();

	PHALCON_INIT_VAR(method_name);
	ZVAL_STRING(method_name, "variables", 1);

	PHALCON_INIT_VAR(call_object);
	array_init_size(call_object, 2);
	phalcon_array_append(&call_object, this_ptr, PH_COPY);
	phalcon_array_append(&call_object, method_name, PH_COPY);

	PHALCON_CALL_FUNCTION(&arg_list, "func_get_args");

	PHALCON_CALL_USER_FUNC_ARRAY(return_value, call_object, arg_list);

	RETURN_MM();
}
Esempio n. 27
0
/**
 * Reads meta-data from APC
 *
 * @param  string $key
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Apc, read)
{
	zval *key, *prefix, *apc_key, *data = NULL;

	PHALCON_MM_GROW();

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

	prefix = phalcon_read_property(getThis(), SL("_prefix"), PH_NOISY);

	PHALCON_INIT_VAR(apc_key);
	PHALCON_CONCAT_SVV(apc_key, "$PMM$", prefix, key);

	PHALCON_CALL_FUNCTION(&data, "apc_fetch", apc_key);
	if (Z_TYPE_P(data) == IS_ARRAY) { 
		RETURN_CCTOR(data);
	}

	RETURN_MM_NULL();
}
Esempio n. 28
0
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Apc, reset){

	zval meta = {}, prefix = {};
	zend_string *str_key;
	ulong idx;

	phalcon_read_property(&meta, getThis(), SL("_metaData"), PH_NOISY);

	if (Z_TYPE(meta) == IS_ARRAY) {
		phalcon_read_property(&prefix, getThis(), SL("_prefix"), PH_NOISY);

		ZEND_HASH_FOREACH_KEY(Z_ARRVAL(meta), idx, str_key) {
			zval key = {}, real_key = {};
			if (str_key) {
				ZVAL_STR(&key, str_key);
			} else {
				ZVAL_LONG(&key, idx);
			}

			phalcon_concat_svsv(&real_key, SL("$PMM$"), &prefix, SL("meta-"), &key, 0);
			PHALCON_CALL_FUNCTION(NULL, "apc_delete", &real_key);
		} ZEND_HASH_FOREACH_END();
Esempio n. 29
0
/**
 * Render the image and return the binary string.
 *
 * @param string $ext image type to return: png, jpg, gif, etc
 * @param int $quality quality of image: 1-100
 * @return Phalcon\Image\Adapter
 */
PHP_METHOD(Phalcon_Image_Adapter, render) {

    zval *ext = NULL, *quality = NULL, *constant, *file;

    PHALCON_MM_GROW();

    phalcon_fetch_params(1, 0, 2, &ext, &quality);

    if (!ext) {
        file = phalcon_fetch_nproperty_this(this_ptr, SL("_file"), PH_NOISY TSRMLS_CC);

        PHALCON_INIT_VAR(constant);
        if (!zend_get_constant(SL("PATHINFO_EXTENSION"), constant TSRMLS_CC)) {
            RETURN_MM();
        }

        ext = NULL;
        PHALCON_CALL_FUNCTION(&ext, "pathinfo", file, constant);

        if (!PHALCON_IS_NOT_EMPTY(ext)) {
            ZVAL_STRING(ext, "png", 1);
        }
    }

    if (!quality) {
        PHALCON_INIT_VAR(quality);
        ZVAL_LONG(quality, 100);
    } else {
        if (Z_TYPE_P(quality) != IS_LONG) {
            PHALCON_SEPARATE_PARAM(quality);
            convert_to_long(quality);
        }
    }

    PHALCON_RETURN_CALL_METHOD(this_ptr, "_render", ext, quality);

    RETURN_MM();
}
Esempio n. 30
0
/**
 * Executes the validation
 *
 * @param Phalcon\Validation $validator
 * @param string $attribute
 * @return boolean
 */
PHP_METHOD(Phalcon_Validation_Validator_InclusionIn, validate){

	zval *validator, *attribute, *value = NULL, *allow_empty, *valid = NULL;
	zval *label, *domain, *joined_domain, *pairs, *message_str, *message, *code;
	zval *prepared = NULL;
	zend_class_entry *ce = Z_OBJCE_P(getThis());

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &validator, &attribute);

	PHALCON_VERIFY_CLASS_EX(validator, phalcon_validation_ce, phalcon_validation_exception_ce, 1);

	PHALCON_CALL_METHOD(&value, validator, "getvalue", attribute);

	PHALCON_OBS_VAR(allow_empty);
	RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &allow_empty, getThis(), phalcon_interned_allowEmpty TSRMLS_CC));
	if (zend_is_true(allow_empty) && phalcon_validation_validator_isempty_helper(value)) {
		RETURN_MM_TRUE;
	}

	/* A domain is an array with a list of valid values */
	PHALCON_OBS_VAR(domain);
	RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &domain, getThis(), phalcon_interned_domain TSRMLS_CC));
	if (Z_TYPE_P(domain) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "Option 'domain' must be an array");
		return;
	}

	PHALCON_CALL_SELF(&valid, "valid", value, domain);

	if (PHALCON_IS_FALSE(valid)) {
		PHALCON_OBS_VAR(label);
		RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &label, getThis(), phalcon_interned_label TSRMLS_CC));
		if (!zend_is_true(label)) {
			PHALCON_CALL_METHOD(&label, validator, "getlabel", attribute);
			if (!zend_is_true(label)) {
				PHALCON_CPY_WRT(label, attribute);
			}
		}

		ALLOC_INIT_ZVAL(joined_domain);
		phalcon_fast_join_str(joined_domain, SL(", "), domain TSRMLS_CC);

		PHALCON_ALLOC_GHOST_ZVAL(pairs);
		array_init_size(pairs, 2);
		Z_ADDREF_P(label); add_assoc_zval_ex(pairs, SS(":field"), label);
		add_assoc_zval_ex(pairs, SS(":domain"), joined_domain);

		PHALCON_OBS_VAR(message_str);
		RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &message_str, getThis(), phalcon_interned_message TSRMLS_CC));
		if (!zend_is_true(message_str)) {
			PHALCON_OBSERVE_OR_NULLIFY_VAR(message_str);
			RETURN_MM_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(Z_OBJCE_P(validator), &message_str, validator, "InclusionIn" TSRMLS_CC));
		}
	
		PHALCON_OBS_VAR(code);
		RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &code, getThis(), phalcon_interned_code TSRMLS_CC));
		if (Z_TYPE_P(code) == IS_NULL) {
			ZVAL_LONG(code, 0);
		}

		PHALCON_CALL_FUNCTION(&prepared, "strtr", message_str, pairs);

		message = phalcon_validation_message_construct_helper(prepared, attribute, "InclusionIn", code TSRMLS_CC);
		Z_DELREF_P(message);

		PHALCON_CALL_METHOD(NULL, validator, "appendmessage", message);
		RETURN_MM_FALSE;
	}

	RETURN_MM_TRUE;
}