Exemplo n.º 1
0
/**
 * Executes the validation
 *
 * @param Phalcon\Validation $validator
 * @param string $attribute
 * @return boolean
 */
PHP_METHOD(Phalcon_Validation_Validator_Between, validate){

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

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

	PHALCON_VERIFY_CLASS_EX(validator, phalcon_validation_ce, phalcon_validation_exception_ce, 0);

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

	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&allow_empty, ce, getThis(), ISV(allowEmpty)));
	if (zend_is_true(&allow_empty) && phalcon_validation_validator_isempty_helper(&value)) {
		RETURN_TRUE;
	}

	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&minimum, ce, getThis(), "minimum"));
	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&maximum, ce, getThis(), "maximum"));

	PHALCON_CALL_SELFW(&valid, "valid", &value, &minimum, &maximum);

	if (PHALCON_IS_FALSE(&valid)) {
		RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&label, ce, getThis(), ISV(label)));
		if (!zend_is_true(&label)) {
			PHALCON_CALL_METHODW(&label, validator, "getlabel", attribute);
			if (!zend_is_true(&label)) {
				PHALCON_CPY_WRT_CTOR(&label, attribute);
			}
		}

		array_init_size(&pairs, 3);
		phalcon_array_update_str(&pairs, SL(":field"), &label, PH_COPY);
		phalcon_array_update_str(&pairs, SL(":min"), &minimum, PH_COPY);
		phalcon_array_update_str(&pairs, SL(":max"), &maximum, PH_COPY);

		RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
		if (!zend_is_true(&message_str)) {
			RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "Between"));
		}

		RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&code, ce, getThis(), ISV(code)));
		if (Z_TYPE_P(&code) == IS_NULL) {
			ZVAL_LONG(&code, 0);
		}

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

		phalcon_validation_message_construct_helper(&message, &prepared, attribute, "Between", &code);

		PHALCON_CALL_METHODW(NULL, validator, "appendmessage", &message);
		RETURN_FALSE;
	}

	RETURN_TRUE;
}
Exemplo n.º 2
0
/**
 * Phalcon\Http\Uri constructor
 *
 * @param mixed $uri
 */
PHP_METHOD(Phalcon_Http_Uri, __construct)
{
	zval *uri = NULL, parts = {}, query = {}, params = {};

	phalcon_fetch_params(0, 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_FUNCTIONW(&parts, "parse_url", uri);
		if (phalcon_array_isset_fetch_str(&query, &parts, SL("query"))) {
			ZVAL_MAKE_REF(&params);
			PHALCON_CALL_FUNCTIONW(NULL, "parse_str", &query, &params);
			ZVAL_UNREF(&params);
			phalcon_array_update_str(&parts, SL("query"), &params, PH_COPY);
		}

		phalcon_update_property_zval(getThis(), SL("_parts"), &parts);
	} else if (Z_TYPE_P(uri) == IS_ARRAY) {
		phalcon_update_property_zval(getThis(), SL("_parts"), uri);
	} else if (Z_TYPE_P(uri) == IS_OBJECT && Z_OBJCE_P(uri) == phalcon_http_uri_ce) {
		phalcon_return_property(&parts, uri, SL("_parts"));
		phalcon_update_property_zval(getThis(), SL("_parts"), &parts);
	} else {
		phalcon_update_property_empty_array(getThis(), SL("_parts"));
	}
}
Exemplo n.º 3
0
/**
 * Changes a parameter in the definition without resolve the service
 *
 * @param long $position
 * @param array $parameter
 * @return Phalcon\DI\Service
 */
PHP_METHOD(Phalcon_DI_Service, setParameter){

	zval *position, *parameter, definition = {}, arguments = {};

	phalcon_fetch_params(0, 2, 0, &position, &parameter);
	PHALCON_ENSURE_IS_LONG(position);

	phalcon_return_property(&definition, getThis(), SL("_definition"));

	if (unlikely(Z_TYPE(definition) != IS_ARRAY)) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_di_exception_ce, "Definition must be an array to update its parameters");
		return;
	}

	if (unlikely(Z_TYPE_P(parameter) != IS_ARRAY)) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_di_exception_ce, "The parameter must be an array");
		return;
	}

	/* Update the parameter */
	if (phalcon_array_isset_fetch_str(&arguments, &definition, SL("arguments"))) {
		phalcon_array_update_zval(&arguments, position, parameter, PH_COPY);
	} else {
		array_init_size(&arguments, 1);
		phalcon_array_update_zval(&arguments, position, parameter, PH_COPY);
	}

	phalcon_array_update_str(&definition, SL("arguments"), &arguments, PH_COPY);

	phalcon_update_property_zval(getThis(), SL("_definition"), &definition);

	RETURN_THISW();
}
Exemplo n.º 4
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();
}
Exemplo n.º 5
0
/**
 * Applies a format to a message before sent it to the internal log
 *
 * @param string $message
 * @param int $type
 * @param int $timestamp
 * @param array $context
 * @return string
 */
PHP_METHOD(Phalcon_Logger_Formatter_Json, format){

	zval *message, *type, *timestamp, *context, interpolated = {}, type_str = {}, log = {}, json = {};

	phalcon_fetch_params(0, 4, 0, &message, &type, &timestamp, &context);
	
	if (Z_TYPE_P(context) == IS_ARRAY) {
		PHALCON_CALL_METHODW(&interpolated, getThis(), "interpolate", message, context);
	} else {
		PHALCON_CPY_WRT(&interpolated, message);
	}

	PHALCON_CALL_METHODW(&type_str, getThis(), "gettypestring", type);

	array_init_size(&log, 3);
	phalcon_array_update_str(&log, SL("type"), &type_str, PH_COPY);
	phalcon_array_update_str(&log, SL("message"), &interpolated, PH_COPY);
	phalcon_array_update_str(&log, SL("timestamp"), timestamp, PH_COPY);

	RETURN_ON_FAILURE(phalcon_json_encode(&json, &log, 0));

	PHALCON_CONCAT_VS(return_value, &json, PHP_EOL);
}
Exemplo n.º 6
0
/**
 * Execute a crop.
 *
 * @param int $width
 * @param int $height
 * @param int $offset_x
 * @param int $offset_y
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, _crop)
{
	zval *width, *height, *offset_x, *offset_y, image = {}, tmp_image = {}, rect = {};

	phalcon_fetch_params(0, 4, 0, &width, &height, &offset_x, &offset_y);

	phalcon_read_property(&image, getThis(), SL("_image"), 0);

	array_init_size(&rect, 4);
	phalcon_array_update_str(&rect, SL("x"), offset_x, 0);
	phalcon_array_update_str(&rect, SL("y"), offset_y, 0);
	phalcon_array_update_str(&rect, SL("width"), width, 0);
	phalcon_array_update_str(&rect, SL("height"), height, 0);

	PHALCON_CALL_FUNCTIONW(&tmp_image, "imagecrop", &image, &rect);

	if (Z_TYPE(tmp_image) == IS_RESOURCE) {
		PHALCON_CALL_FUNCTIONW(NULL, "imagedestroy", &image);
		phalcon_update_property_zval(getThis(), SL("_image"), &tmp_image);
		phalcon_update_property_zval(getThis(), SL("_width"), width);
		phalcon_update_property_zval(getThis(), SL("_height"), height);
	}
}
Exemplo n.º 7
0
/**
 * Returns a cached content
 *
 * @param int|string $keyName
 * @param   long $lifetime
 * @return  mixed
 */
PHP_METHOD(Phalcon_Cache_Backend_Mongo, get){

	zval *key_name, *lifetime = NULL, *frontend, *prefix, *prefixed_key;
	zval *collection = NULL, *conditions, *document = NULL, *time_condition;
	zval *cached_content;

	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_VV(prefixed_key, prefix, key_name);
	phalcon_update_property_this(getThis(), SL("_lastKey"), prefixed_key);

	PHALCON_CALL_METHOD(&collection, getThis(), "_getcollection");

	PHALCON_INIT_VAR(conditions);
	array_init_size(conditions, 2);
	phalcon_array_update_str(conditions, SL("key"), prefixed_key, PH_COPY);

	PHALCON_ALLOC_INIT_ZVAL(time_condition);
	array_init_size(time_condition, 1);
	add_assoc_long_ex(time_condition, SL("$gt"), (long int)time(NULL));
	add_assoc_zval_ex(conditions, SL("time"), time_condition);

	PHALCON_CALL_METHOD(&document, collection, "findone", conditions);
	if (Z_TYPE_P(document) == IS_ARRAY) { 
		if (likely(phalcon_array_isset_str_fetch(&cached_content, document, SL("data")))) {
			if (phalcon_is_numeric(cached_content)) {
				RETURN_CCTOR(cached_content);
			} else {
				PHALCON_RETURN_CALL_METHOD(frontend, "afterretrieve", cached_content);
				RETURN_MM();
			}
		} else {
			PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The cache is corrupt");
			return;
		}
	} else {
		RETURN_MM_NULL();
	}

}
Exemplo n.º 8
0
/**
 * Phalcon\Cache\Backend\Libmemcached constructor
 *
 * @param Phalcon\Cache\FrontendInterface $frontend
 * @param array $options
 */
PHP_METHOD(Phalcon_Cache_Backend_Libmemcached, __construct){

	zval *frontend, *options = NULL, *server, *servers;

	PHALCON_MM_GROW();

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

	if (!options) {
		PHALCON_INIT_VAR(options);
	} else {
		PHALCON_SEPARATE_PARAM(options);
	}

	if (Z_TYPE_P(options) != IS_ARRAY) {
		PHALCON_INIT_NVAR(options);
		array_init(options);
	}

	if (!phalcon_array_isset_str(options, SL("servers"))) {
		PHALCON_INIT_VAR(servers);
		array_init_size(servers, 1);

		PHALCON_INIT_VAR(server);
		array_init_size(server, 3);

		phalcon_array_update_str_str(server, SL("host"), SL("127.0.0.1"), PH_COPY);
		phalcon_array_update_str_long(server, SL("port"), 11211, PH_COPY);
		phalcon_array_update_str_long(server, SL("weight"), 1, PH_COPY);

		phalcon_array_append(servers, server, PH_COPY);

		phalcon_array_update_str(options, SL("servers"), servers, PH_COPY);
	}

	if (!phalcon_array_isset_str(options, SL("statsKey"))) {
		phalcon_array_update_str_str(options, SL("statsKey"), SL("_PHCM"), PH_COPY);
	}

	PHALCON_CALL_PARENT(NULL, phalcon_cache_backend_libmemcached_ce, getThis(), "__construct", frontend, options);

	PHALCON_MM_RESTORE();
}
Exemplo n.º 9
0
/**
 * Sends the cookie to the HTTP client
 * Stores the cookie definition in session
 *
 * @return Phalcon\Http\Cookie
 */
PHP_METHOD(Phalcon_Http_Cookie, send){

	zval *name, *value, *expire, *domain, *path, *secure;
	zval *http_only, *dependency_injector, *definition;
	zval *service = NULL, *session = NULL, *key, *encryption, *crypt = NULL;
	zval *encrypt_value = NULL, *has_session = NULL;

	PHALCON_MM_GROW();

	name = phalcon_read_property(getThis(), SL("_name"), PH_NOISY);
	value = phalcon_read_property(getThis(), SL("_value"), PH_NOISY);
	expire = phalcon_read_property(getThis(), SL("_expire"), PH_NOISY);
	domain = phalcon_read_property(getThis(), SL("_domain"), PH_NOISY);
	path = phalcon_read_property(getThis(), SL("_path"), PH_NOISY);
	secure = phalcon_read_property(getThis(), SL("_secure"), PH_NOISY);
	http_only = phalcon_read_property(getThis(), SL("_httpOnly"), PH_NOISY);
	dependency_injector = phalcon_read_property(getThis(), SL("_dependencyInjector"), PH_NOISY);
	if (Z_TYPE_P(dependency_injector) == IS_OBJECT) {
		PHALCON_INIT_VAR(service);
		ZVAL_STR(service, IS(session));

		PHALCON_CALL_METHOD(&has_session, dependency_injector, "has", service);
		if (zend_is_true(has_session)) {
			PHALCON_INIT_VAR(definition);
			array_init(definition);
			if (!PHALCON_IS_LONG(expire, 0)) {
				phalcon_array_update_str(definition, SL("expire"), expire, PH_COPY);
			}

			if (PHALCON_IS_NOT_EMPTY(path)) {
				phalcon_array_update_str(definition, SL("path"), path, PH_COPY);
			}

			if (PHALCON_IS_NOT_EMPTY(domain)) {
				phalcon_array_update_string(definition, IS(domain), domain, PH_COPY);
			}

			if (PHALCON_IS_NOT_EMPTY(secure)) {
				phalcon_array_update_str(definition, SL("secure"), secure, PH_COPY);
			}

			if (PHALCON_IS_NOT_EMPTY(http_only)) {
				phalcon_array_update_str(definition, SL("httpOnly"), http_only, PH_COPY);
			}

			/**
			 * The definition is stored in session
			 */
			if (phalcon_fast_count_ev(definition)) {
				PHALCON_CALL_METHOD(&session, dependency_injector, "getshared", service);

				if (Z_TYPE_P(session) != IS_NULL) {
					PHALCON_VERIFY_INTERFACE(session, phalcon_session_adapterinterface_ce);

					PHALCON_INIT_VAR(key);
					PHALCON_CONCAT_SV(key, "_PHCOOKIE_", name);
					PHALCON_CALL_METHOD(NULL, session, "set", key, definition);
				}
			}
		}
	}

	encryption = phalcon_read_property(getThis(), SL("_useEncryption"), PH_NOISY);
	if (zend_is_true(encryption) && PHALCON_IS_NOT_EMPTY(value)) {
		if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_http_cookie_exception_ce, "A dependency injection object is required to access the 'filter' service");
			return;
		}

		PHALCON_INIT_NVAR(service);
		ZVAL_STRING(service, "crypt");

		PHALCON_CALL_METHOD(&crypt, dependency_injector, "getshared", service);
		PHALCON_VERIFY_INTERFACE(crypt, phalcon_cryptinterface_ce);

		/**
		 * Encrypt the value also coding it with base64
		 */
		PHALCON_CALL_METHOD(&encrypt_value, crypt, "encryptbase64", value);
	} else {
		PHALCON_CPY_WRT(encrypt_value, value);
	}

	/** 
	 * Sets the cookie using the standard 'setcookie' function
	 */
	convert_to_string_ex(name);
	convert_to_string_ex(encrypt_value);
	convert_to_long_ex(expire);
	convert_to_string_ex(path);
	convert_to_string_ex(domain);
	convert_to_long_ex(secure);
	convert_to_long_ex(http_only);

	php_setcookie(Z_STR_P(name), Z_STR_P(encrypt_value), Z_LVAL_P(expire), Z_STR_P(path), Z_STR_P(domain), Z_LVAL_P(secure), 1, Z_LVAL_P(http_only));

	RETURN_THIS();
}
Exemplo n.º 10
0
PHP_METHOD(Phalcon_Mvc_Collection_GridFS, store){

	zval *file, *metadata = NULL, *options = NULL, *isBytes = NULL;
	zval *mongo_id = NULL, *source = NULL, *files_source;
	zval *connection = NULL, *mongo_collection = NULL, *grid_fs = NULL;
	zval *sha1, *md5, *criteria, *operation, *field, *value, *new_object;
	zval *status = NULL, *ok, *exist;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 3, &file, &metadata, &options, &isBytes);

	if (!metadata) {
		PHALCON_INIT_VAR(metadata);
		array_init_size(metadata, 1);
	} else {
		PHALCON_SEPARATE_PARAM(metadata);

		if (Z_TYPE_P(metadata) != IS_ARRAY) {
			PHALCON_INIT_NVAR(metadata);
			array_init_size(metadata, 1);
		}
	}

	if (!options) {
		PHALCON_INIT_NVAR(options);
		array_init_size(options, 1);
	} else {
		PHALCON_SEPARATE_PARAM(options);

		if (Z_TYPE_P(options) != IS_ARRAY) {
			PHALCON_INIT_NVAR(options);
			array_init_size(options, 1);
		}
	}

	if (!isBytes) {
		isBytes = &PHALCON_GLOBAL(z_false);
	}

	phalcon_array_update_str_long(options, SL("w"), 0, 0);

	PHALCON_CALL_SELF(&mongo_id, "getid");

	if (!zend_is_true(mongo_id)) {
		RETURN_MM_FALSE;
	}

	PHALCON_CALL_METHOD(&source, getThis(), "getsource");

	PHALCON_INIT_VAR(files_source);
	PHALCON_CONCAT_VS(files_source, source, ".files");

	PHALCON_CALL_METHOD(&connection, getThis(), "getconnection");

	PHALCON_CALL_METHOD(&mongo_collection, connection, "selectcollection", files_source);
	if (Z_TYPE_P(mongo_collection) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_collection_exception_ce, "Couldn't select mongo collection");
		return;
	}

	sha1 = phalcon_read_property(getThis(), SL("sha1"), PH_NOISY);
	md5 = phalcon_read_property(getThis(), SL("md5"), PH_NOISY);

	PHALCON_INIT_VAR(criteria);
	array_init_size(criteria, 3);

	phalcon_array_update_str(criteria, SL("md5"), md5, PH_COPY);
	phalcon_array_update_str(criteria, SL("sha1"), sha1, PH_COPY);

	PHALCON_INIT_VAR(operation);
	ZVAL_STRING(operation, "$gte");

	PHALCON_INIT_VAR(field);
	ZVAL_STRING(field, "use");

	PHALCON_INIT_VAR(value);
	ZVAL_LONG(value, 1)

	phalcon_array_update_multi_2(criteria, field, operation, value, PH_COPY);

	PHALCON_INIT_NVAR(operation);
	ZVAL_STRING(operation, "$inc");

	PHALCON_INIT_VAR(new_object);
	array_init_size(new_object, 1);

	phalcon_array_update_multi_2(new_object, operation, field, value, PH_COPY);
	
	PHALCON_CALL_METHOD(&status, mongo_collection, "update", criteria, new_object);

	if (phalcon_array_isset_str_fetch(&ok, status, SL("ok"))) {
		if (zend_is_true(ok)) {
			if (phalcon_array_isset_str_fetch(&exist, status, SL("updatedExisting"))) {
				if (zend_is_true(exist)) {
					RETURN_MM_TRUE;
				}
			}
		} else {
			RETURN_MM_FALSE;
		}
	}

	PHALCON_CALL_METHOD(&grid_fs, connection, "getgridfs", source);
	if (Z_TYPE_P(grid_fs) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_collection_exception_ce, "Couldn't select mongo GridFS");
		return;
	}

	phalcon_array_update_str(metadata, SL("sha1"), sha1, PH_COPY);
	phalcon_array_update_str(metadata, SL("use"), value, PH_COPY);

	if (zend_is_true(isBytes)) {
		PHALCON_CALL_METHOD(&status, grid_fs, "storebytes", file, metadata, options);
	} else {
		PHALCON_CALL_METHOD(&status, grid_fs, "storefile", file, metadata, options);
	}

	if (zend_is_true(status)) {
		RETURN_MM_TRUE;
	}

	RETURN_MM_FALSE;
}
Exemplo n.º 11
0
/**
 * Reads annotations from the class dockblocks, its methods and/or properties
 *
 * @param string $className
 * @return array
 */
PHP_METHOD(Phalcon_Annotations_Reader, parse){

	zval *class_name, *annotations;
	zval *class_annotations, *annotations_properties, *annotations_methods;
	zend_class_entry *class_ce;
	const char *file;
	uint32_t line;

	PHALCON_MM_GROW();

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

	if (unlikely(Z_TYPE_P(class_name) != IS_STRING)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_annotations_exception_ce, "The class name must be a string");
		return;
	}

	class_ce = zend_fetch_class(Z_STR_P(class_name), ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_SILENT);
	if (!class_ce) {
		PHALCON_THROW_EXCEPTION_FORMAT(phalcon_annotations_exception_ce, "Class %s does not exist", Z_STRVAL_P(class_name));
		return;
	}

	if (class_ce->type != ZEND_USER_CLASS) {
		array_init(return_value);
		RETURN_MM();
	}

	PHALCON_INIT_VAR(annotations);
	array_init(annotations);

	file = ZSTR_VAL(phalcon_get_class_filename(class_ce));
	if (!file) {
		file = "(unknown)";
	}

	/* Class info */
	{
		zend_string *cmt;

		if ((cmt = phalcon_get_class_doc_comment(class_ce)) != NULL) {
			line = phalcon_get_class_startline(class_ce);

			PHALCON_INIT_VAR(class_annotations);
			RETURN_MM_ON_FAILURE(phannot_parse_annotations(class_annotations, cmt, file, line));

			if (Z_TYPE_P(class_annotations) == IS_ARRAY) {
				phalcon_array_update_str(annotations, SL("class"), class_annotations, PH_COPY);
			}
		}
	}

	/* Get class properties */
	{
		HashTable *props = &class_ce->properties_info;
		if (zend_hash_num_elements(props) > 0) {
			zend_property_info *property;

			PHALCON_INIT_VAR(annotations_properties);
			array_init_size(annotations_properties, zend_hash_num_elements(props));

			ZEND_HASH_FOREACH_PTR(props, property) {
				zend_string *cmt;

				if ((cmt = phalcon_get_property_doc_comment(property)) != NULL) {
					zval *property_annotations;

					PHALCON_ALLOC_INIT_ZVAL(property_annotations);
					if (FAILURE == phannot_parse_annotations(property_annotations, cmt, file, 0)) {
						zval_ptr_dtor(property_annotations);
						RETURN_MM();
					}

					if (Z_TYPE_P(property_annotations) == IS_ARRAY) {
						const char *prop_name, *class_name;
						if (zend_unmangle_property_name(property->name, &class_name, &prop_name) == SUCCESS) {
							add_assoc_zval_ex(annotations_properties, prop_name, strlen(prop_name), property_annotations);
						}
					} else {
						zval_ptr_dtor(property_annotations);
					}
				}
			} ZEND_HASH_FOREACH_END();

			if (zend_hash_num_elements(Z_ARRVAL_P(annotations_properties))) {
				phalcon_array_update_str(annotations, SL("properties"), annotations_properties, PH_COPY);
			}
		}
Exemplo n.º 12
0
/**
 * Returns a slice of the resultset to show in the pagination
 *
 * @return stdClass
 */
PHP_METHOD(Phalcon_Paginator_Adapter_Sql, getPaginate){

	zval db = {}, sql = {}, total_sql = {}, bind = {}, limit = {}, number_page = {}, number = {}, fetch_mode = {}, items = {}, row = {}, rowcount = {};
	long int i_limit, i_number_page, i_number, i_before, i_rowcount;
	long int i_total_pages, i_next;
	ldiv_t tp;

	phalcon_return_property(&db, getThis(), SL("_db"));
	phalcon_return_property(&sql, getThis(), SL("_sql"));
	phalcon_return_property(&total_sql, getThis(), SL("_total_sql"));
	phalcon_return_property(&bind, getThis(), SL("_bind"));
	phalcon_return_property(&limit, getThis(), SL("_limitRows"));
	phalcon_return_property(&number_page, getThis(), SL("_page"));
	phalcon_return_property(&fetch_mode, getThis(), SL("_fetchMode"));

	i_limit       = phalcon_get_intval(&limit);
	i_number_page = phalcon_get_intval(&number_page);

	if (i_limit < 1) {
		/* This should never happen unless someone deliberately modified the properties of the object */
		i_limit = 10;
	}

	if (!i_number_page) {
		i_number_page = 1;
	}

	i_number = (i_number_page - 1) * i_limit;
	i_before = (i_number_page == 1) ? 1 : (i_number_page - 1);

	PHALCON_CALL_METHODW(&row, &db, "fetchone", &total_sql, &fetch_mode, &bind);

	phalcon_return_property(&rowcount, &row, SL("rowcount"));

	PHALCON_SEPARATE(&bind);
	/* Set the limit clause avoiding negative offsets */
	if (i_number < i_limit) {
		phalcon_array_update_str(&bind, SL("limit"), &limit, PH_COPY);
		phalcon_array_update_str_long(&bind, SL("offset"), 0, 0);
	} else {
		ZVAL_LONG(&number, i_number);
		phalcon_array_update_str(&bind, SL("limit"), &limit, PH_COPY);
		phalcon_array_update_str(&bind, SL("offset"), &number, PH_COPY);
	}

	PHALCON_CALL_METHODW(&items, &db, "fetchall", &sql, &fetch_mode, &bind);
	
	i_rowcount    = phalcon_get_intval(&rowcount);
	tp            = ldiv(i_rowcount, i_limit);
	i_total_pages = tp.quot + (tp.rem ? 1 : 0);
	i_next        = (i_number_page < i_total_pages) ? (i_number_page + 1) : i_total_pages;

	object_init(return_value);
	phalcon_update_property_zval(return_value, SL("items"),       &items);
	phalcon_update_property_long(return_value, SL("before"),      i_before);
	phalcon_update_property_long(return_value, SL("first"),       1);
	phalcon_update_property_long(return_value, SL("next"),        i_next);
	phalcon_update_property_long(return_value, SL("last"),        i_total_pages);
	phalcon_update_property_long(return_value, SL("current"),     i_number_page);
	phalcon_update_property_long(return_value, SL("total_pages"), i_total_pages);
	phalcon_update_property_long(return_value, SL("total_items"), i_rowcount);
}
Exemplo n.º 13
0
/**
 * Constructor for Phalcon\Session\Adapter\Memcache
 *
 * @param array $options
 */
PHP_METHOD(Phalcon_Session_Adapter_Memcache, __construct){

	zval *options, host = {}, port = {}, lifetime = {}, persistent = {}, prefix = {};
	zval frontend_option = {}, backend_option = {}, frontend_data = {}, memcache = {};
	zval callable_open = {}, callable_close = {}, callable_read = {}, callable_write = {}, callable_destroy = {}, callable_gc = {};

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

	if (Z_TYPE_P(options) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STRW(phalcon_session_exception_ce, "The options must be an array");
		return;
	}

	if (!phalcon_array_isset_fetch_str(&host, options, SL("host"))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_session_exception_ce, "No session host given in options");
		return;
	}

	if (!phalcon_array_isset_fetch_str(&port, options, SL("port"))) {
		ZVAL_LONG(&port, 11211);
	}

	if (!phalcon_array_isset_fetch_str(&lifetime, options, SL("lifetime"))) {
		ZVAL_LONG(&lifetime, 8600);
	} else {
		phalcon_update_property_this(getThis(), SL("_lifetime"), &lifetime);
	}

	if (!phalcon_array_isset_fetch_str(&persistent, options, SL("persistent"))) {
		ZVAL_FALSE(&persistent);
	}

	if (!phalcon_array_isset_fetch_str(&prefix, options, SL("prefix"))) {
		ZVAL_EMPTY_STRING(&prefix);
	}

	/* create memcache instance */
	array_init_size(&frontend_option, 1);

	phalcon_array_update_str(&frontend_option, SL("lifetime"), &lifetime, PH_COPY);

	object_init_ex(&frontend_data, phalcon_cache_frontend_data_ce);

	PHALCON_CALL_METHODW(NULL, &frontend_data, "__construct", &frontend_option);

	array_init_size(&backend_option, 3);

	phalcon_array_update_str(&backend_option, SL("host"), &host, PH_COPY);
	phalcon_array_update_str(&backend_option, SL("port"), &port, PH_COPY);
	phalcon_array_update_str(&backend_option, SL("persistent"), &persistent, PH_COPY);
	phalcon_array_update_str(&backend_option, SL("prefix"), &prefix, PH_COPY);

	object_init_ex(&memcache, phalcon_cache_backend_memcache_ce);

	PHALCON_CALL_METHODW(NULL, &memcache, "__construct", &frontend_data, &backend_option);

	phalcon_update_property_this(getThis(), SL("_memcache"), &memcache);

	/* open callback */
	array_init_size(&callable_open, 2);
	phalcon_array_append(&callable_open, getThis(), 0);
	phalcon_array_append_string(&callable_open, SL("open"), 0);

	/* close callback */
	array_init_size(&callable_close, 2);
	phalcon_array_append(&callable_close, getThis(), 0);
	phalcon_array_append_string(&callable_close, SL("close"), 0);

	/* read callback */
	array_init_size(&callable_read, 2);
	phalcon_array_append(&callable_read, getThis(), 0);
	phalcon_array_append_string(&callable_read, SL("read"), 0);

	/* write callback */
	array_init_size(&callable_write, 2);
	phalcon_array_append(&callable_write, getThis(), 0);
	phalcon_array_append_string(&callable_write, SL("write"), 0);

	/* destroy callback */
	array_init_size(&callable_destroy, 2);
	phalcon_array_append(&callable_destroy, getThis(), 0);
	phalcon_array_append_string(&callable_destroy, SL("destroy"), 0);

	/* gc callback */
	array_init_size(&callable_gc, 2);
	phalcon_array_append(&callable_gc, getThis(), 0);
	phalcon_array_append_string(&callable_gc, SL("gc"), 0);

	PHALCON_CALL_FUNCTIONW(NULL, "session_set_save_handler", &callable_open, &callable_close, &callable_read, &callable_write, &callable_destroy, &callable_gc);
	PHALCON_CALL_PARENTW(NULL, phalcon_session_adapter_memcache_ce, getThis(), "__construct", options);
}
Exemplo n.º 14
0
/**
 * Resolves the service based on its configuration
 *
 * @param string $name
 * @param array $parameters
 * @return mixed
 */
PHP_METHOD(Phalcon_DI, get){

	zval *name, *parameters = NULL, *noerror = NULL, events_manager = {}, event_name = {}, event_data = {}, service = {};
	zend_class_entry *ce;

	phalcon_fetch_params(0, 1, 2, &name, &parameters, &noerror);
	PHALCON_ENSURE_IS_STRING(name);

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

	if (!noerror) {
		noerror = &PHALCON_GLOBAL(z_false);
	}

	phalcon_return_property(&events_manager, getThis(), SL("_eventsManager"));
	if (Z_TYPE(events_manager) == IS_OBJECT) {
		ZVAL_STRING(&event_name, "di:beforeServiceResolve");

		array_init(&event_data);

		phalcon_array_update_str(&event_data, SL("name"), name, PH_COPY);
		phalcon_array_update_str(&event_data, SL("parameters"), parameters, PH_COPY);

		PHALCON_CALL_METHODW(NULL, &events_manager, "fire", &event_name, getThis(), &event_data);
	}

	if (phalcon_property_array_isset_fetch(&service, getThis(), SL("_services"), name)) {
		PHALCON_CALL_METHODW(return_value, &service, "resolve", parameters, getThis());
		ce = (Z_TYPE_P(return_value) == IS_OBJECT) ? Z_OBJCE_P(return_value) : NULL;
	} else {
		/* The DI also acts as builder for any class even if it isn't defined in the DI */
		if ((ce = phalcon_class_exists_ex(name, 1)) != NULL) {
			if (FAILURE == phalcon_create_instance_params_ce(return_value, ce, parameters)) {
				return;
			}
		} else {
			if(!zend_is_true(noerror)) {
				zend_throw_exception_ex(phalcon_di_exception_ce, 0, "Service '%s' was not found in the dependency injection container", Z_STRVAL_P(name));
			}
			RETURN_NULL();
		}
	}

	/* Pass the DI itself if the instance implements Phalcon\DI\InjectionAwareInterface */
	if (ce && instanceof_function_ex(ce, phalcon_di_injectionawareinterface_ce, 1)) {
		PHALCON_CALL_METHODW(NULL, return_value, "setdi", getThis());
	}

	if (Z_TYPE(events_manager) == IS_OBJECT) {
		ZVAL_STRING(&event_name, "di:afterServiceResolve");

		array_init(&event_data);

		phalcon_array_update_str(&event_data, SL("name"), name, PH_COPY);
		phalcon_array_update_str(&event_data, SL("parameters"), parameters, PH_COPY);
		phalcon_array_update_str(&event_data, SL("instance"), return_value, PH_COPY);

		PHALCON_CALL_METHODW(NULL, &events_manager, "fire", &event_name, getThis(), &event_data);
	}
}
Exemplo n.º 15
0
/**
 * Executes the validation
 *
 * @param Phalcon\Validation $validator
 * @param string $attribute
 * @return boolean
 */
PHP_METHOD(Phalcon_Validation_Validator_File, validate)
{
	zval *validator, *attribute, value = {}, allow_empty = {}, mimes = {}, minsize = {}, maxsize = {}, minwidth = {}, maxwidth = {}, minheight = {}, maxheight = {};
	zval valid = {}, type = {}, code = {}, message_str = {}, message = {}, join_mimes = {}, label = {}, pairs = {}, prepared = {};
	zend_class_entry *ce = Z_OBJCE_P(getThis());

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

	PHALCON_VERIFY_CLASS_EX(validator, phalcon_validation_ce, phalcon_validation_exception_ce, 0);

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

	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&allow_empty, ce, getThis(), ISV(allowEmpty)));
	if (zend_is_true(&allow_empty) && phalcon_validation_validator_isempty_helper(&value)) {
		RETURN_TRUE;
	}

	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&mimes, ce, getThis(), "mimes"));
	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&minsize, ce, getThis(), "minsize"));
	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&maxsize, ce, getThis(), "maxsize"));
	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&minwidth, ce, getThis(), "minwidth"));
	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&maxwidth, ce, getThis(), "maxwidth"));
	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&minheight, ce, getThis(), "minheight"));
	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&maxheight, ce, getThis(), "maxheight"));

	PHALCON_CALL_SELFW(&valid, "valid", &value, &minsize, &maxsize, &mimes, &minwidth, &maxwidth, &minheight, &maxheight);

	if (PHALCON_IS_FALSE(&valid)) {
		phalcon_return_property(&type, getThis(), SL("_type"));

		RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&label, ce, getThis(), ISV(label)));
		if (!zend_is_true(&label)) {
			PHALCON_CALL_METHODW(&label, validator, "getlabel", attribute);
			if (!zend_is_true(&label)) {
				PHALCON_CPY_WRT_CTOR(&label, attribute);
			}
		}

		RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&code, ce, getThis(), ISV(code)));
		if (Z_TYPE(code) == IS_NULL) {
			ZVAL_LONG(&code, 0);
		}

		array_init(&pairs);
		phalcon_array_update_str(&pairs, SL(":field"), &label, PH_COPY);

		if (phalcon_compare_strict_string(&type, SL("TooLarge"))) {
			phalcon_array_update_str(&pairs, SL(":max"), &maxsize, PH_COPY);

			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "FileMaxSize"));
			}

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

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "TooLarge", &code);
		} else if (phalcon_compare_strict_string(&type, SL("TooSmall"))) {
			phalcon_array_update_str(&pairs, SL(":min"), &minsize, PH_COPY);

			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "FileMinSize"));
			}

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

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "TooSmall", &code);
		} else if (phalcon_compare_strict_string(&type, SL("MimeValid"))) {
			phalcon_fast_join_str(&join_mimes, SL(", "), &mimes);
			phalcon_array_update_str(&pairs, SL(":mimes"), &join_mimes, PH_COPY);

			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "FileType"));
			}

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

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "FileType", &code);
		} else if (phalcon_compare_strict_string(&type, SL("TooLarge"))) {
			phalcon_array_update_str(&pairs, SL(":max"), &maxsize, PH_COPY);

			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "FileMaxSize"));
			}

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

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "TooLarge", &code);
		} else if (phalcon_compare_strict_string(&type, SL("TooNarrow"))) {
			phalcon_array_update_str(&pairs, SL(":min"), &minwidth, PH_COPY);

			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "ImageMinWidth"));
			}

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

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "TooNarrow", &code);
		} else if (phalcon_compare_strict_string(&type, SL("TooWide"))) {
			phalcon_array_update_str(&pairs, SL(":max"), &maxwidth, PH_COPY);

			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "ImageMaxWidth"));
			}

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

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "TooWide", &code);
		}  else if (phalcon_compare_strict_string(&type, SL("TooShort"))) {
			phalcon_array_update_str(&pairs, SL(":min"), &minheight, PH_COPY);

			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "ImageMinHeight"));
			}

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

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "TooShort", &code);
		} else if (phalcon_compare_strict_string(&type, SL("TooLong"))) {
			phalcon_array_update_str(&pairs, SL(":max"), &maxheight, PH_COPY);

			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "ImageMaxHeight"));
			}

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

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "TooLong", &code);
		} else {
			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "FileValid"));
			}

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

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "File", &code);
		}

		PHALCON_CALL_METHODW(NULL, validator, "appendmessage", &message);
		RETURN_FALSE;
	}

	RETURN_TRUE;
}
Exemplo n.º 16
0
/**
 * Phalcon\Mvc\Model\MetaData\Libmemcached constructor
 *
 * @param array $options
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Libmemcached, __construct){

	zval *options = NULL;
	zval *servers, *client, *lifetime, *prefix;
	zval *frontend_data, *libmemcached, *option;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &options);
	
	if (Z_TYPE_P(options) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The options must be an array");
		return;
	}

	if (!phalcon_array_isset_str_fetch(&servers, options, SL("servers"))) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "No servers given in options");
		return;
	}

	if (!phalcon_array_isset_str_fetch(&client, options, SL("client"))) {
		client = NULL;
	}

	if (!phalcon_array_isset_str_fetch(&lifetime, options, SL("lifetime"))) {
		PHALCON_INIT_VAR(lifetime);
		ZVAL_LONG(lifetime, 8600);
	}

	phalcon_update_property_this(getThis(), SL("_lifetime"), lifetime);

	if (!phalcon_array_isset_str_fetch(&prefix, options, SL("prefix"))) {
		PHALCON_INIT_VAR(prefix);
		ZVAL_EMPTY_STRING(prefix);
	}

	/* create libmemcached instance */
	PHALCON_INIT_VAR(option);
	array_init_size(option, 1);

	phalcon_array_update_str(option, SL("lifetime"), lifetime, PH_COPY);

	PHALCON_INIT_VAR(frontend_data);
	object_init_ex(frontend_data, phalcon_cache_frontend_data_ce);

	PHALCON_CALL_METHOD(NULL, frontend_data, "__construct", option);

	PHALCON_INIT_NVAR(option);
	array_init(option);

	phalcon_array_update_str_str(option, SL("statsKey"), SL("$PMM$"), PH_COPY);

	phalcon_array_update_str(option, SL("servers"), servers, PH_COPY);
	if (client) {
		phalcon_array_update_str(option, SL("client"), client, PH_COPY);
	}
	phalcon_array_update_str(option, SL("prefix"), prefix, PH_COPY);

	PHALCON_INIT_VAR(libmemcached);
	object_init_ex(libmemcached, phalcon_cache_backend_libmemcached_ce);

	PHALCON_CALL_METHOD(NULL, libmemcached, "__construct", frontend_data, option);

	phalcon_update_property_this(getThis(), SL("_libmemcached"), libmemcached);
	
	phalcon_update_property_empty_array(getThis(), SL("_metaData"));

	PHALCON_MM_RESTORE();
}
Exemplo n.º 17
0
/**
 * Stores cached content into the Mongo backend and stops the frontend
 *
 * @param int|string $keyName
 * @param string $content
 * @param long $lifetime
 * @param boolean $stopBuffer
 */
PHP_METHOD(Phalcon_Cache_Backend_Mongo, save){

	zval *key_name = NULL, *content = NULL, *lifetime = NULL, *stop_buffer = NULL;
	zval *last_key, *frontend, *cached_content = NULL;
	zval *prepared_content = NULL, *ttl = NULL, *collection = NULL, *timestamp;
	zval *conditions, *document = NULL, *data, *is_buffering = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 4, &key_name, &content, &lifetime, &stop_buffer);

	if (!key_name || Z_TYPE_P(key_name) == IS_NULL) {
		last_key = phalcon_read_property(getThis(), SL("_lastKey"), PH_NOISY);
	} else {
		zval *prefix = phalcon_read_property(getThis(), SL("_prefix"), PH_NOISY);

		PHALCON_INIT_VAR(last_key);
		PHALCON_CONCAT_VV(last_key, prefix, key_name);
	}

	if (!zend_is_true(last_key)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The cache must be started first");
		return;
	}

	frontend = phalcon_read_property(getThis(), SL("_frontend"), PH_NOISY);
	if (!content || Z_TYPE_P(content) == IS_NULL) {
		PHALCON_CALL_METHOD(&cached_content, frontend, "getcontent");
	} else {
		cached_content = content;
	}

	if (!phalcon_is_numeric(cached_content)) {
		PHALCON_CALL_METHOD(&prepared_content, frontend, "beforestore", cached_content);
	}

	if (!lifetime || Z_TYPE_P(lifetime) == IS_NULL) {
		zval *tmp = phalcon_read_property(getThis(), SL("_lastLifetime"), PH_NOISY);

		if (Z_TYPE_P(tmp) == IS_NULL) {
			PHALCON_CALL_METHOD(&ttl, frontend, "getlifetime");
		}
		else {
			ttl = tmp;
		}
	} else {
		ttl = lifetime;
	}

	PHALCON_CALL_METHOD(&collection, getThis(), "_getcollection");

	PHALCON_INIT_VAR(timestamp);
	ZVAL_LONG(timestamp, (long) time(NULL) + phalcon_get_intval(ttl));

	PHALCON_INIT_VAR(conditions);
	array_init_size(conditions, 1);
	phalcon_array_update_str(conditions, SL("key"), last_key, PH_COPY);

	PHALCON_CALL_METHOD(&document, collection, "findone", conditions);

	if (Z_TYPE_P(document) == IS_ARRAY) { 
		phalcon_array_update_str(document, SL("time"), timestamp, PH_COPY);
		if (prepared_content) {
			phalcon_array_update_str(document, SL("data"), prepared_content, PH_COPY);
		} else {
			phalcon_array_update_str(document, SL("data"), cached_content, PH_COPY);
		}
		PHALCON_CALL_METHOD(NULL, collection, "save", document);
	} else {
		PHALCON_INIT_VAR(data);
		array_init_size(data, 3);
		phalcon_array_update_str(data, SL("key"), last_key, PH_COPY);
		phalcon_array_update_str(data, SL("time"), timestamp, PH_COPY);

		if (prepared_content) {
			phalcon_array_update_str(data, SL("data"), prepared_content, PH_COPY);
		} else {
			phalcon_array_update_str(data, SL("data"), cached_content, PH_COPY);
		}

		PHALCON_CALL_METHOD(NULL, collection, "save", data);
	}

	PHALCON_CALL_METHOD(&is_buffering, frontend, "isbuffering");

	if (!stop_buffer || PHALCON_IS_TRUE(stop_buffer)) {
		PHALCON_CALL_METHOD(NULL, frontend, "stop");
	}

	if (PHALCON_IS_TRUE(is_buffering)) {
		zend_print_zval(cached_content, 0);
	}

	phalcon_update_property_bool(getThis(), SL("_started"), 0);

	PHALCON_MM_RESTORE();
}
Exemplo n.º 18
0
/**
 * Handles a MVC request
 *
 * @param string $uri
 * @return Phalcon\Http\ResponseInterface
 */
PHP_METHOD(Phalcon_Mvc_JsonRpc, handle){

	zval *uri = NULL, *dependency_injector, *events_manager;
	zval *status = NULL, *service = NULL, *request = NULL, *response = NULL;
	zval *json = NULL, *data = NULL, *jsonrpc_message, *jsonrpc_error, *jsonrpc_result = NULL;
	zval *jsonrpc_method, *jsonrpc_params, *jsonrpc_id;
	zval *url = NULL, *router = NULL, *module_name = NULL;
	zval *module_object = NULL, *modules;
	zval *module, *class_name = NULL, *module_params;
	zval *namespace_name = NULL;
	zval *controller_name = NULL, *action_name = NULL, *params = NULL, *exact = NULL;
	zval *dispatcher = NULL, *controller = NULL, *returned_response = NULL;
	zval *path;

	PHALCON_MM_GROW();
	
	dependency_injector = phalcon_read_property(getThis(), SL("_dependencyInjector"), PH_NOISY);
	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_jsonrpc_exception_ce, "A dependency injection object is required to access internal services");
		return;
	}
	
	events_manager = phalcon_read_property(getThis(), SL("_eventsManager"), PH_NOISY);
	if (Z_TYPE_P(events_manager) != IS_OBJECT) {
		events_manager = NULL;
	}
	else {
		PHALCON_VERIFY_INTERFACE_EX(events_manager, phalcon_events_managerinterface_ce, phalcon_mvc_jsonrpc_exception_ce, 1);
	}

	/* Call boot event, this allows the developer to perform initialization actions */
	if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:boot", getThis(), NULL)) {
		RETURN_MM_FALSE;
	}

	/* Deserializer Json */

	PHALCON_INIT_NVAR(service);
	ZVAL_STR(service, IS(request));
	
	PHALCON_CALL_METHOD(&request, dependency_injector, "getshared", service);
	PHALCON_VERIFY_INTERFACE(request, phalcon_http_requestinterface_ce);

	PHALCON_CALL_METHOD(&json, request, "getrawbody");
	PHALCON_CALL_FUNCTION(&data, "json_decode", json, &PHALCON_GLOBAL(z_true));


	PHALCON_INIT_NVAR(service);
	ZVAL_STR(service, IS(response));

	PHALCON_CALL_METHOD(&response, dependency_injector, "getshared", service);
	PHALCON_VERIFY_INTERFACE(response, phalcon_http_responseinterface_ce);

	PHALCON_INIT_VAR(jsonrpc_message);
	array_init(jsonrpc_message);
	
	PHALCON_INIT_VAR(jsonrpc_error);
	array_init(jsonrpc_error);

	if (PHALCON_IS_EMPTY(data)) {
		phalcon_array_update_str_long(jsonrpc_error, SL("code"), __LINE__, 0);
		phalcon_array_update_str_str(jsonrpc_error, SL("message"), SL("Parse error"), PH_COPY);
	} else if (Z_TYPE_P(data) != IS_ARRAY) {
		phalcon_array_update_str_long(jsonrpc_error, SL("code"), __LINE__, 0);
		phalcon_array_update_str_str(jsonrpc_error, SL("message"), SL("Parse error"), PH_COPY);
	} else if (!phalcon_array_isset_str(data, SL("jsonrpc"))) {		
			phalcon_array_update_str_long(jsonrpc_error, SL("code"), __LINE__, 0);
			phalcon_array_update_str_str(jsonrpc_error, SL("message"), SL("Invalid Request"), PH_COPY);
	} else if (!phalcon_array_isset_str(data, SL("method"))) {
			phalcon_array_update_str_long(jsonrpc_error, SL("code"), __LINE__, 0);
			phalcon_array_update_str_str(jsonrpc_error, SL("message"), SL("Invalid Request"), PH_COPY);
	} else {
		PHALCON_OBS_VAR(jsonrpc_method);
		phalcon_array_fetch_str(&jsonrpc_method, data, SL("method"), PH_NOISY);

		if (phalcon_array_isset_str(data, SL("params"))) {
			PHALCON_OBS_VAR(jsonrpc_params);
			phalcon_array_fetch_str(&jsonrpc_params, data, SL("params"), PH_NOISY);
		} else {
			PHALCON_INIT_VAR(jsonrpc_params);
			array_init(jsonrpc_params);
		}

		PHALCON_INIT_NVAR(service);
		ZVAL_STR(service, IS(url));
		PHALCON_CALL_METHOD(&url, dependency_injector, "getshared", service);
		PHALCON_VERIFY_INTERFACE(url, phalcon_mvc_urlinterface_ce);

		PHALCON_CALL_METHOD(&uri, url, "get", jsonrpc_method);

		PHALCON_INIT_NVAR(service);
		ZVAL_STR(service, IS(router));
		PHALCON_CALL_METHOD(&router, dependency_injector, "getshared", service);
		PHALCON_VERIFY_INTERFACE(router, phalcon_mvc_routerinterface_ce);

		/* Handle the URI pattern (if any) */
		PHALCON_CALL_METHOD(NULL, router, "handle", uri);
		
		/* Load module config */
		PHALCON_CALL_METHOD(&module_name, router, "getmodulename");

		/* Load module config */
		PHALCON_CALL_METHOD(&module_name, router, "getmodulename");
		
		/* If the router doesn't return a valid module we use the default module */
		if (!zend_is_true(module_name)) {
			module_name = phalcon_read_property(getThis(), SL("_defaultModule"), PH_NOISY);
		}
		
		/** 
		 * Process the module definition
		 */
		if (zend_is_true(module_name)) {
			if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:beforeStartModule", getThis(), module_name)) {
				RETURN_MM_FALSE;
			}

			/** 
			 * Check if the module passed by the router is registered in the modules container
			 */
			modules = phalcon_read_property(getThis(), SL("_modules"), PH_NOISY);
			if (!phalcon_array_isset_fetch(&module, modules, module_name)) {
				convert_to_string(module_name);
				zend_throw_exception_ex(phalcon_mvc_jsonrpc_exception_ce, 0, "Module %s is not registered in the jsonrpc container", Z_STRVAL_P(module_name));
				RETURN_MM();
			}
		
			/** 
			 * A module definition must be an array or an object
			 */
			if (Z_TYPE_P(module) != IS_ARRAY && Z_TYPE_P(module) != IS_OBJECT) {
				PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_jsonrpc_exception_ce, "Invalid module definition");
				return;
			}
		
			/* An array module definition contains a path to a module definition class */
			if (Z_TYPE_P(module) == IS_ARRAY) { 
				/* Class name used to load the module definition */
				if (phalcon_array_isset_str(module, SL("className"))) {
					PHALCON_OBS_VAR(class_name);
					phalcon_array_fetch_str(&class_name, module, SL("className"), PH_NOISY);
				} else {
					PHALCON_INIT_NVAR(class_name);
					ZVAL_STRING(class_name, "Module");
				}
		
				/* If the developer has specified a path, try to include the file */
				if (phalcon_array_isset_str(module, SL("path"))) {
		
					PHALCON_OBS_VAR(path);
					phalcon_array_fetch_str(&path, module, SL("path"), PH_NOISY);
					convert_to_string_ex(path);
					if (Z_TYPE_P(class_name) != IS_STRING || phalcon_class_exists(class_name, 0) == NULL) {
						if (phalcon_file_exists(path) == SUCCESS) {
							RETURN_MM_ON_FAILURE(phalcon_require(Z_STRVAL_P(path)));
						} else {
							zend_throw_exception_ex(phalcon_mvc_jsonrpc_exception_ce, 0, "Module definition path '%s' does not exist", Z_STRVAL_P(path));
							RETURN_MM();
						}
					}
				}
		
				PHALCON_CALL_METHOD(&module_object, dependency_injector, "get", class_name);
		
				/** 
				 * 'registerAutoloaders' and 'registerServices' are automatically called
				 */
				PHALCON_CALL_METHOD(NULL, module_object, "registerautoloaders", dependency_injector);
				PHALCON_CALL_METHOD(NULL, module_object, "registerservices", dependency_injector);
			} else if (Z_TYPE_P(module) == IS_OBJECT && instanceof_function(Z_OBJCE_P(module), zend_ce_closure)) {
				/* A module definition object, can be a Closure instance */
				PHALCON_INIT_VAR(module_params);
				array_init_size(module_params, 1);
				phalcon_array_append(module_params, dependency_injector, PH_COPY);

				PHALCON_CALL_USER_FUNC_ARRAY(&status, module, module_params);
			} else {
				PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_jsonrpc_exception_ce, "Invalid module definition");
				return;
			}
		
			/* Calling afterStartModule event */
			if (events_manager) {
				if (!module_object) {
					module_object = &PHALCON_GLOBAL(z_null);
				}

				phalcon_update_property_this(getThis(), SL("_moduleObject"), module_object);
				if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:afterStartModule", getThis(), module_name)) {
					RETURN_MM_FALSE;
				}
			}
		}
		
		/* We get the parameters from the router and assign them to the dispatcher */
		PHALCON_CALL_METHOD(&module_name, router, "getmodulename");
		PHALCON_CALL_METHOD(&namespace_name, router, "getnamespacename");
		PHALCON_CALL_METHOD(&controller_name, router, "getcontrollername");
		PHALCON_CALL_METHOD(&action_name, router, "getactionname");
		PHALCON_CALL_METHOD(&params, router, "getparams");
		PHALCON_CALL_METHOD(&exact, router, "isexactcontrollername");

		PHALCON_INIT_NVAR(service);
		ZVAL_STR(service, IS(dispatcher));
		
		PHALCON_CALL_METHOD(&dispatcher, dependency_injector, "getshared", service);
		PHALCON_VERIFY_INTERFACE(dispatcher, phalcon_dispatcherinterface_ce);
		
		/* Assign the values passed from the router */
		PHALCON_CALL_METHOD(NULL, dispatcher, "setmodulename", module_name);
		PHALCON_CALL_METHOD(NULL, dispatcher, "setnamespacename", namespace_name);
		PHALCON_CALL_METHOD(NULL, dispatcher, "setcontrollername", controller_name, exact);
		PHALCON_CALL_METHOD(NULL, dispatcher, "setactionname", action_name);
		PHALCON_CALL_METHOD(NULL, dispatcher, "setparams", jsonrpc_params);
		
		/* Calling beforeHandleRequest */
		RETURN_MM_ON_FAILURE(phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:beforeHandleRequest", getThis(), dispatcher));
		
		/* The dispatcher must return an object */
		PHALCON_CALL_METHOD(&controller, dispatcher, "dispatch");
		
		PHALCON_INIT_VAR(returned_response);
		
		/* Get the latest value returned by an action */
		PHALCON_CALL_METHOD(&jsonrpc_result, dispatcher, "getreturnedvalue");
	}
		
	/* Calling afterHandleRequest */
	if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:afterHandleRequest", getThis(), controller) && EG(exception)) {
		RETURN_MM();
	}
	
	phalcon_array_update_str_str(jsonrpc_message, SL("jsonrpc"), SL("2.0"), PH_COPY);

	if (PHALCON_IS_NOT_EMPTY(jsonrpc_error)) {
		phalcon_array_update_str(jsonrpc_message, SL("error"), jsonrpc_error, PH_COPY);
	}

	if (jsonrpc_result != NULL) {
		phalcon_array_update_str(jsonrpc_message, SL("result"), jsonrpc_result, PH_COPY);
	}
	
	if (phalcon_array_isset_str_fetch(&jsonrpc_id, data, SL("id"))) {
		phalcon_array_update_str(jsonrpc_message, SL("id"), jsonrpc_id, PH_COPY);
	} else {
		phalcon_array_update_str(jsonrpc_message, SL("id"), &PHALCON_GLOBAL(z_null), PH_COPY);
	}

	PHALCON_CALL_METHOD(NULL, response, "setjsoncontent", jsonrpc_message);
	

	/* Calling beforeSendResponse */
	if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:beforeSendResponse", getThis(), response) && EG(exception)) {
		RETURN_MM();
	}
	
	/* Headers are automatically sent */
	PHALCON_CALL_METHOD(NULL, response, "sendheaders");
	
	/* Cookies are automatically sent */
	PHALCON_CALL_METHOD(NULL, response, "sendcookies");
	
	/* Return the response */
	RETURN_CCTOR(response);
}
Exemplo n.º 19
0
PHP_METHOD(Phalcon_Mvc_Collection_GridFS, remove){

	zval *sha1 = NULL, *md5 = NULL, *source = NULL, *files_source;
	zval *connection = NULL, *mongo_collection = NULL;
	zval *criteria, *operation, *field, *value, *new_object;
	zval *status = NULL, *ok, *exist, *options, *grid_fs = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 2, &sha1, &md5);

	if (!sha1) {
		sha1 = phalcon_read_property(getThis(), SL("sha1"), PH_NOISY);
	}

	if (!md5) {
		md5 = phalcon_read_property(getThis(), SL("md5"), PH_NOISY);
	}

	PHALCON_CALL_METHOD(&source, getThis(), "getsource");

	PHALCON_INIT_VAR(files_source);
	PHALCON_CONCAT_VS(files_source, source, ".files");

	PHALCON_CALL_METHOD(&connection, getThis(), "getconnection");

	PHALCON_CALL_METHOD(&mongo_collection, connection, "selectcollection", files_source);
	if (Z_TYPE_P(mongo_collection) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_collection_exception_ce, "Couldn't select mongo collection");
		return;
	}

	PHALCON_INIT_VAR(criteria);
	array_init_size(criteria, 2);

	phalcon_array_update_str(criteria, SL("md5"), md5, PH_COPY);
	phalcon_array_update_str(criteria, SL("sha1"), sha1, PH_COPY);

	PHALCON_INIT_VAR(operation);
	ZVAL_STRING(operation, "$inc");

	PHALCON_INIT_VAR(field);
	ZVAL_STRING(field, "use");

	PHALCON_INIT_VAR(value);
	ZVAL_LONG(value, -1)

	PHALCON_INIT_VAR(new_object);
	array_init_size(new_object, 1);

	phalcon_array_update_multi_2(new_object, operation, field, value, PH_COPY);
	
	PHALCON_CALL_METHOD(&status, mongo_collection, "update", criteria, new_object);

	if (phalcon_array_isset_str_fetch(&ok, status, SL("ok"))) {
		if (zend_is_true(ok)) {
			if (phalcon_array_isset_str_fetch(&exist, status, SL("updatedExisting"))) {
				if (!zend_is_true(exist)) {
					RETURN_MM_FALSE;
				}
			}
		} else {
			RETURN_MM_FALSE;
		}
	}

	PHALCON_CALL_METHOD(&grid_fs, connection, "getgridfs", source);
	if (Z_TYPE_P(grid_fs) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_collection_exception_ce, "Couldn't select mongo GridFS");
		return;
	}

	PHALCON_INIT_NVAR(criteria);
	array_init_size(criteria, 3);

	phalcon_array_update_str(criteria, SL("sha1"), sha1, PH_COPY);
	phalcon_array_update_str(criteria, SL("md5"), md5, PH_COPY);

	PHALCON_INIT_NVAR(operation);
	ZVAL_STRING(operation, "$lte");

	PHALCON_INIT_NVAR(value);
	ZVAL_LONG(value, 0)

	phalcon_array_update_multi_2(criteria, field, operation, value, PH_COPY);

	PHALCON_INIT_VAR(options);
	array_init_size(options, 1);

	phalcon_array_update_str_long(options, SL("w"), 0, PH_COPY);

	PHALCON_RETURN_CALL_METHOD(grid_fs, "remove", criteria, options);
	RETURN_MM();
}