Exemplo n.º 1
0
/**
 * Phalcon\Cache\Backend\Memcache constructor
 *
 * @param Phalcon\Cache\FrontendInterface $frontend
 * @param array $options
 */
PHP_METHOD(Phalcon_Cache_Backend_Memcache, __construct){

	zval *frontend, *opts = NULL, options = {};

	phalcon_fetch_params(0, 1, 1, &frontend, &opts);

	if (opts) {
		PHALCON_CPY_WRT(&options, opts);
	}

	if (Z_TYPE(options) != IS_ARRAY) { 
		array_init_size(&options, 4);
	}

	if (!phalcon_array_isset_str(&options, SL("host"))) {
		phalcon_array_update_str_str(&options, SL("host"), SL("127.0.0.1"), PH_COPY);
	}

	if (!phalcon_array_isset_str(&options, SL("port"))) {
		phalcon_array_update_str_long(&options, SL("port"), 11211, 0);
	}

	if (!phalcon_array_isset_str(&options, SL("persistent"))) {
		phalcon_array_update_str_bool(&options, SL("persistent"), 0, 0);
	}

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

	PHALCON_CALL_PARENTW(NULL, phalcon_cache_backend_memcache_ce, getThis(), "__construct", frontend, &options);
}
Exemplo n.º 2
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.º 3
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.º 4
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.º 5
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();
}
Exemplo n.º 6
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;
}