Пример #1
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;
}
Пример #2
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();
}
Пример #3
0
/**
 * Phalcon\Annotations\Annotation constructor
 *
 * @param array $reflectionData
 */
PHP_METHOD(Phalcon_Annotations_Annotation, __construct){

	zval *reflection_data, *name = NULL, *arguments, *expr_arguments;
	zval *argument = NULL, *expr = NULL, *resolved_argument = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &reflection_data);
	
	if (Z_TYPE_P(reflection_data) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_annotations_exception_ce, "Reflection data must be an array");
		return;
	}
	
	PHALCON_OBS_VAR(name);
	phalcon_array_fetch_string(&name, reflection_data, SL("name"), PH_NOISY);
	phalcon_update_property_this(this_ptr, SL("_name"), name TSRMLS_CC);
	
	/** 
	 * Process annotation arguments
	 */
	if (phalcon_array_isset_string_fetch(&expr_arguments, reflection_data, SS("arguments"))) {
	
		phalcon_is_iterable(expr_arguments, &ah0, &hp0, 0, 0);
	
		PHALCON_INIT_VAR(arguments);
		array_init_size(arguments, zend_hash_num_elements(ah0));

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

			PHALCON_GET_HVALUE(argument);
	
			PHALCON_OBS_NVAR(expr);
			phalcon_array_fetch_string(&expr, argument, SL("expr"), PH_NOISY);
	
			PHALCON_CALL_METHOD(&resolved_argument, this_ptr, "getexpression", expr);
			if (phalcon_array_isset_string_fetch(&n, argument, SS("name"))) {
				phalcon_array_update_zval(&arguments, n, resolved_argument, PH_COPY);
			} else {
				phalcon_array_append(&arguments, resolved_argument, 0);
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
		phalcon_update_property_this(this_ptr, SL("_arguments"), arguments TSRMLS_CC);
		phalcon_update_property_this(this_ptr, SL("_exprArguments"), expr_arguments TSRMLS_CC);
	}
	
	PHALCON_MM_RESTORE();
}
Пример #4
0
/**
 * Returns the prefix for all the generated urls. By default /
 *
 * @return string
 */
PHP_METHOD(Phalcon_Mvc_Url, getBaseUri){

	zval *base_uri = NULL, *slash, *_SERVER, *php_self, *uri = NULL;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(base_uri);
	phalcon_read_property_this(&base_uri, this_ptr, SL("_baseUri"), PH_NOISY TSRMLS_CC);
	if (Z_TYPE_P(base_uri) == IS_NULL) {
	
		PHALCON_INIT_VAR(slash);
		ZVAL_STRING(slash, "/", 1);
		_SERVER = phalcon_get_global(SS("_SERVER") TSRMLS_CC);
		if (phalcon_array_isset_string_fetch(&php_self, _SERVER, SS("PHP_SELF"))) {
			PHALCON_INIT_VAR(uri);
			phalcon_get_uri(uri, php_self);
		} else {
			PHALCON_INIT_NVAR(uri);
		}
	
		if (!zend_is_true(uri)) {
			PHALCON_CPY_WRT(base_uri, slash);
		} else {
			PHALCON_INIT_NVAR(base_uri);
			PHALCON_CONCAT_VVV(base_uri, slash, uri, slash);
		}
	
		phalcon_update_property_this(this_ptr, SL("_baseUri"), base_uri TSRMLS_CC);
	}
	
	RETURN_CCTOR(base_uri);
}
Пример #5
0
/**
 * This method is automatically called in Phalcon\Db\Adapter\Pdo constructor.
 * Call it when you need to restore a database connection.
 *
 * Support set search_path after connectted if schema is specified in config.
 *
 * @param array $descriptor
 * @return boolean
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo_Postgresql, connect){

	zval *descriptor = NULL, *schema = NULL, *sql, *password;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 1, &descriptor);
	
	if (!descriptor) {
		PHALCON_INIT_VAR(descriptor);
	} else {
		PHALCON_SEPARATE_PARAM(descriptor);
	}
	
	if (!zend_is_true(descriptor)) {
		PHALCON_OBS_NVAR(descriptor);
		phalcon_read_property_this(&descriptor, this_ptr, SL("_descriptor"), PH_NOISY TSRMLS_CC);
	}
	
	if (phalcon_array_isset_string(descriptor, SS("schema"))) {
		PHALCON_OBS_VAR(schema);
		phalcon_array_fetch_string(&schema, descriptor, SL("schema"), PH_NOISY);
		phalcon_array_unset_string(&descriptor, SS("schema"), PH_COPY);

		phalcon_update_property_this(this_ptr, SL("_schema"), schema TSRMLS_CC);
	}
	else {
		PHALCON_INIT_VAR(schema);
	}

	if (phalcon_array_isset_string_fetch(&password, descriptor, SS("password"))) {
		/* There is a bug in pdo_pgsql driver when the password is empty,
		 * the driver tries to access invalid memory:
		 *
		 * if (dbh->password[0] != '\'' && dbh->password[strlen(dbh->password) - 1] != '\'')
		 *
		 * To avoid this we set the password to null
		 */
		if (Z_TYPE_P(password) == IS_STRING && Z_STRLEN_P(password) == 0) {
			phalcon_array_update_string(&descriptor, SL("password"), PHALCON_GLOBAL(z_null), PH_COPY);
		}
	}

	
	PHALCON_CALL_PARENT(NULL, phalcon_db_adapter_pdo_postgresql_ce, this_ptr, "connect", descriptor);
	
	/** 
	 * Execute the search path in the after connect
	 */
	if (Z_TYPE_P(schema) == IS_STRING) {
		PHALCON_INIT_VAR(sql);
		PHALCON_CONCAT_SVS(sql, "SET search_path TO '", schema, "'");
		PHALCON_CALL_METHOD(NULL, this_ptr, "execute", sql);
	}
	
	PHALCON_MM_RESTORE();
}
Пример #6
0
/**
 * Phalcon\Mvc\Model\MetaData\Xcache constructor
 *
 * @param array $options
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Xcache, __construct){

	zval *options = NULL, *prefix, *ttl, *empty_array;

	phalcon_fetch_params(0, 0, 1, &options);
	
	if (options && Z_TYPE_P(options) == IS_ARRAY) {
		if (phalcon_array_isset_string_fetch(&prefix, options, SS("prefix"))) {
			phalcon_update_property_this(this_ptr, SL("_prefix"), prefix TSRMLS_CC);
		}

		if (phalcon_array_isset_string_fetch(&ttl, options, SS("lifetime"))) {
			phalcon_update_property_this(this_ptr, SL("_ttl"), ttl TSRMLS_CC);
		}
	}
	
	PHALCON_ALLOC_GHOST_ZVAL(empty_array);
	array_init(empty_array);
	phalcon_update_property_this(this_ptr, SL("_metaData"), empty_array TSRMLS_CC);
}
Пример #7
0
/**
 * Returns the DependencyInjector container
 *
 * @return Phalcon\DiInterface
 */
PHP_METHOD(Phalcon_Mvc_Model_Criteria, getDI){

	zval *params, *dependency_injector;

	params = phalcon_fetch_nproperty_this(this_ptr, SL("_params"), PH_NOISY TSRMLS_CC);
	if (phalcon_array_isset_string_fetch(&dependency_injector, params, SS("di"))) {
		RETURN_ZVAL(dependency_injector, 1, 0);
	}
	
	RETURN_NULL();
}
Пример #8
0
/**
 * Returns the order parameter in the criteria
 *
 * @return string
 */
PHP_METHOD(Phalcon_Mvc_Model_Criteria, getOrder){

	zval *params, *order;

	params = phalcon_fetch_nproperty_this(this_ptr, SL("_params"), PH_NOISY TSRMLS_CC);
	if (phalcon_array_isset_string_fetch(&order, params, SS("order"))) {
		RETURN_ZVAL(order, 1, 0);
	}
	
	RETURN_NULL();
}
Пример #9
0
/**
 * Returns the limit parameter in the criteria
 *
 * @return string
 */
PHP_METHOD(Phalcon_Mvc_Model_Criteria, getLimit){

	zval *params, *limit;

	params = phalcon_fetch_nproperty_this(this_ptr, SL("_params"), PH_NOISY TSRMLS_CC);
	if (phalcon_array_isset_string_fetch(&limit, params, SS("limit"))) {
		RETURN_ZVAL(limit, 1, 0);
	}
	
	RETURN_NULL();
}
Пример #10
0
/**
 * Returns the conditions parameter in the criteria
 *
 * @return string
 */
PHP_METHOD(Phalcon_Mvc_Model_Criteria, getConditions){

	zval *params, *conditions;

	params = phalcon_fetch_nproperty_this(this_ptr, SL("_params"), PH_NOISY TSRMLS_CC);
	if (phalcon_array_isset_string_fetch(&conditions, params, SS("conditions"))) {
		RETURN_ZVAL(conditions, 1, 0);
	}
	
	RETURN_NULL();
}
Пример #11
0
/**
 * Create internal connection to memcached
 */
PHP_METHOD(Phalcon_Cache_Backend_Memcache, _connect)
{
	zval *options, *memcache, *host, *port, *persistent;
	zval *success = NULL;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	options = phalcon_fetch_nproperty_this(this_ptr, SL("_options"), PH_NOISY TSRMLS_CC);
	ce0 = zend_fetch_class(SL("Memcache"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);

	PHALCON_INIT_VAR(memcache);
	object_init_ex(memcache, ce0);
	if (phalcon_has_constructor(memcache TSRMLS_CC)) {
		PHALCON_CALL_METHOD(NULL, memcache, "__construct");
	}
	
	if (
		   !phalcon_array_isset_string_fetch(&host, options, SS("host"))
		|| !phalcon_array_isset_string_fetch(&port, options, SS("port"))
		|| !phalcon_array_isset_string_fetch(&persistent, options, SS("persistent"))
	) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Unexpected inconsistency in options");
		return;
	}

	if (zend_is_true(persistent)) {
		PHALCON_CALL_METHOD(&success, memcache, "pconnect", host, port);
	} else {
		PHALCON_CALL_METHOD(&success, memcache, "connect", host, port);
	}
	
	if (!zend_is_true(success)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Cannot connect to Memcached server");
		return;
	}

	phalcon_update_property_this(this_ptr, SL("_memcache"), memcache TSRMLS_CC);
	RETURN_CTOR(memcache);
}
Пример #12
0
/**
 * Restore a Phalcon\Db\Index object from export
 *
 * @param array $data
 * @return Phalcon\Db\IndexInterface
 */
PHP_METHOD(Phalcon_Db_Index, __set_state){

	zval *data, *index_name, *columns;

	phalcon_fetch_params(0, 1, 0, &data);
	
	if (!phalcon_array_isset_string_fetch(&index_name, data, SS("_indexName"))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "_indexName parameter is required");
		return;
	}

	if (!phalcon_array_isset_string_fetch(&columns, data, SS("_columns"))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "_columns parameter is required");
		return;
	}
	
	/** 
	 * Return a Phalcon\Db\Index as part of the returning state
	 */
	object_init_ex(return_value, phalcon_db_index_ce);
	PHALCON_CALL_METHODW(NULL, return_value, "__construct", index_name, columns);
}
Пример #13
0
/**
 * Phalcon\Mvc\Model\MetaData\Files constructor
 *
 * @param array $options
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Files, __construct){

	zval *options = NULL, *meta_data_dir, *empty_array;

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

	if (options && Z_TYPE_P(options) == IS_ARRAY) {
		if (phalcon_array_isset_string_fetch(&meta_data_dir, options, SS("metaDataDir"))) {
			phalcon_update_property_this(this_ptr, SL("_metaDataDir"), meta_data_dir TSRMLS_CC);
		}
	}
	
	PHALCON_ALLOC_GHOST_ZVAL(empty_array);
	array_init(empty_array);
	phalcon_update_property_this(this_ptr, SL("_metaData"), empty_array TSRMLS_CC);
}
Пример #14
0
/**
 * Returns the CSS collection of assets
 *
 * @return Phalcon\Assets\Collection
 */
PHP_METHOD(Phalcon_Assets_Manager, getJs){

	zval *collections, *collection;

	collections = phalcon_fetch_nproperty_this(this_ptr, SL("_collections"), PH_NOISY TSRMLS_CC);

	/** 
	 * Check if the collection does not exist and create an implicit collection
	 */
	if (!phalcon_array_isset_string_fetch(&collection, collections, SS("js"))) {
		object_init_ex(return_value, phalcon_assets_collection_ce);
		return;
	}

	RETURN_ZVAL(collection, 1, 0);
}
Пример #15
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_fetch_nproperty_this(this_ptr, SL("_frontend"), PH_NOISY TSRMLS_CC);
	prefix   = phalcon_fetch_nproperty_this(this_ptr, SL("_prefix"), PH_NOISY TSRMLS_CC);
	
	PHALCON_INIT_VAR(prefixed_key);
	PHALCON_CONCAT_VV(prefixed_key, prefix, key_name);
	phalcon_update_property_this(this_ptr, SL("_lastKey"), prefixed_key TSRMLS_CC);
	
	PHALCON_CALL_METHOD(&collection, this_ptr, "_getcollection");
	
	PHALCON_INIT_VAR(conditions);
	array_init_size(conditions, 2);
	phalcon_array_update_string(&conditions, SL("key"), prefixed_key, PH_COPY);
	
	MAKE_STD_ZVAL(time_condition);
	array_init_size(time_condition, 1);
	add_assoc_long_ex(time_condition, SS("$gt"), (long int)time(NULL));
	add_assoc_zval_ex(conditions, SS("time"), time_condition);

	PHALCON_CALL_METHOD(&document, collection, "findone", conditions);
	if (Z_TYPE_P(document) == IS_ARRAY) { 
		if (likely(phalcon_array_isset_string_fetch(&cached_content, document, SS("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();
	}
	
}
Пример #16
0
Файл: db.c Проект: zpug/cphalcon
/**
 * Enables/disables options in the Database component
 *
 * @param array $options
 */
PHP_METHOD(Phalcon_Db, setup){

	zval *options, *escape_identifiers;

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

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

	/**
	 * Enables/Disables globally the escaping of SQL identifiers
	 */
	if (phalcon_array_isset_string_fetch(&escape_identifiers, options, SS("escapeSqlIdentifiers"))) {
		PHALCON_GLOBAL(db).escape_identifiers = zend_is_true(escape_identifiers);
	}
}
Пример #17
0
/**
 * Handles user-defined error
 *
 * @param int $severity
 * @param string $message
 * @param string $file
 * @param string $line
 * @param array $context
 * @return boolean
 */
PHP_METHOD(Phalcon_Debug, onUserDefinedError){

	zval *severity, *message, *file = NULL, *line = NULL, *context = NULL, *e, *previous = NULL, *exception;
	zend_class_entry *default_exception_ce;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 3, &severity, &message, &file, &line, &context);

	if (!file) {
		file = PHALCON_GLOBAL(z_null);
	}

	if (!line) {
		line = PHALCON_GLOBAL(z_null);
	}

	if (context && Z_TYPE_P(context) == IS_ARRAY) {
		if (
			phalcon_array_isset_string_fetch(&e, context, SS("e")) && 
			Z_TYPE_P(e) == IS_OBJECT && 
			instanceof_function_ex(Z_OBJCE_P(e), zend_exception_get_default(TSRMLS_C), 1 TSRMLS_CC)
		) {
			PHALCON_CPY_WRT(previous, e);
		} else {
			previous = PHALCON_GLOBAL(z_null);
		}
	} else {
		previous = PHALCON_GLOBAL(z_null);
	}

	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), severity, file, line, previous);

	zend_throw_exception_object(exception TSRMLS_CC);

	RETURN_MM_TRUE;
}
Пример #18
0
/**
 * Stores cached content into the file backend and stops the frontend
 *
 * @param int|string $keyName
 * @param string $content
 * @param long $lifetime
 * @param boolean $stopBuffer
 */
PHP_METHOD(Phalcon_Cache_Backend_File, save){

	zval *key_name = NULL, *content = NULL, *lifetime = NULL, *stop_buffer = NULL;
	zval *cache_file, *cached_content = NULL, *prepared_content = NULL, *status, *is_buffering = NULL;
	zval *last_key, *frontend, *options, *cache_dir;

	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_fetch_nproperty_this(this_ptr, SL("_lastKey"), PH_NOISY TSRMLS_CC);
	} else {
		zval *prefix = phalcon_fetch_nproperty_this(this_ptr, SL("_prefix"), PH_NOISY TSRMLS_CC);

		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_fetch_nproperty_this(this_ptr, SL("_frontend"), PH_NOISY TSRMLS_CC);
	options  = phalcon_fetch_nproperty_this(this_ptr, SL("_options"), PH_NOISY TSRMLS_CC);

	if (unlikely(!phalcon_array_isset_string_fetch(&cache_dir, options, SS("cacheDir")))) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Unexpected inconsistency in options");
		return;
	}

	PHALCON_INIT_VAR(cache_file);
	PHALCON_CONCAT_VV(cache_file, cache_dir, last_key);

	if (!content || !zend_is_true(content)) {
		PHALCON_CALL_METHOD(&cached_content, frontend, "getcontent");
	} else {
		cached_content = content;
	}

	PHALCON_CALL_METHOD(&prepared_content, frontend, "beforestore", cached_content);

	/**
	 * We use file_put_contents to respect open-base-dir directive
	 */
	PHALCON_INIT_VAR(status);
	if (!phalcon_is_numeric(cached_content)) {
		phalcon_file_put_contents(status, cache_file, prepared_content TSRMLS_CC);
	} else {
		phalcon_file_put_contents(status, cache_file, cached_content TSRMLS_CC);
	}
	if (PHALCON_IS_FALSE(status)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Cache directory is not writable");
		return;
	}

	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(this_ptr, SL("_started"), 0 TSRMLS_CC);

	PHALCON_MM_RESTORE();
}
Пример #19
0
/**
 * Returns a cached content
 *
 * @param int|string $keyName
 * @param   long $lifetime
 * @return  mixed
 */
PHP_METHOD(Phalcon_Cache_Backend_File, get){

	zval *key_name, *lifetime = NULL;
	zval *options, *prefix, *prefixed_key, *cache_dir;
	zval *cache_file, *tmp = NULL;
	zval *modified_time;
	zval *cached_content, *exception_message;
	long int now, ttl, mtime, diff;
	int expired;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &key_name, &lifetime);

	options = phalcon_fetch_nproperty_this(this_ptr, SL("_options"), PH_NOISY TSRMLS_CC);
	prefix  = phalcon_fetch_nproperty_this(this_ptr, SL("_prefix"), PH_NOISY TSRMLS_CC);

	PHALCON_INIT_VAR(prefixed_key);
	PHALCON_CONCAT_VV(prefixed_key, prefix, key_name);
	phalcon_update_property_this(this_ptr, SL("_lastKey"), prefixed_key TSRMLS_CC);

	if (unlikely(!phalcon_array_isset_string_fetch(&cache_dir, options, SS("cacheDir")))) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Unexpected inconsistency in options");
		return;
	}

	PHALCON_INIT_VAR(cache_file);
	PHALCON_CONCAT_VV(cache_file, cache_dir, prefixed_key);

	if (phalcon_file_exists(cache_file TSRMLS_CC) == SUCCESS) {

		zval *frontend = phalcon_fetch_nproperty_this(this_ptr, SL("_frontend"), PH_NOISY TSRMLS_CC);

		/**
		 * Check if the file has expired
		 */
		now = (long int)time(NULL);

		/**
		 * Take the lifetime from the frontend or read it from the set in start()
		 */
		if (!lifetime || Z_TYPE_P(lifetime) == IS_NULL) {
			zval *last_lifetime = phalcon_fetch_nproperty_this(this_ptr, SL("_lastLifetime"), PH_NOISY TSRMLS_CC);

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

		PHALCON_INIT_VAR(modified_time);
		phalcon_filemtime(modified_time, cache_file TSRMLS_CC);
		if (unlikely(Z_TYPE_P(modified_time) != IS_LONG)) {
			convert_to_long(modified_time);
		}

		mtime   = Z_LVAL_P(modified_time);
		diff    = now - ttl;
		expired = diff > mtime;

		/**
		 * The content is only retrieved if the content has not expired
		 */
		if (!expired) {

			/**
			 * Use file-get-contents to control that the openbase_dir can't be skipped
			 */
			PHALCON_INIT_VAR(cached_content);
			phalcon_file_get_contents(cached_content, cache_file TSRMLS_CC);
			if (PHALCON_IS_FALSE(cached_content)) {
				PHALCON_INIT_VAR(exception_message);
				PHALCON_CONCAT_SVS(exception_message, "Cache file ", cache_file, " could not be opened");
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_cache_exception_ce, exception_message);
				return;
			}

			if (phalcon_is_numeric(cached_content)) {
				RETURN_CCTOR(cached_content);
			} else {
				/**
				 * Use the frontend to process the content of the cache
				 */
				PHALCON_RETURN_CALL_METHOD(frontend, "afterretrieve", cached_content);
			}

			RETURN_MM();
		}
	}

	RETURN_MM_NULL();
}
Пример #20
0
/**
 * Returns a MongoDb collection based on the backend parameters
 *
 * @return MongoCollection
 */
PHP_METHOD(Phalcon_Cache_Backend_Mongo, _getCollection){

	zval *mongo_collection, *mongo_database = NULL;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	mongo_collection = phalcon_fetch_nproperty_this(this_ptr, SL("_collection"), PH_NOISY TSRMLS_CC);
	if (Z_TYPE_P(mongo_collection) != IS_OBJECT) {
		zval *options, *mongo;
		zval *server = NULL, *database = NULL, *collection = NULL;

		options = phalcon_fetch_nproperty_this(this_ptr, SL("_options"), PH_NOISY TSRMLS_CC);
	
		/** 
		 * If mongo is defined a valid Mongo object must be passed
		 */
		if (phalcon_array_isset_string_fetch(&mongo, options, SS("mongo"))) {
			if (Z_TYPE_P(mongo) != IS_OBJECT) {
				PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The 'mongo' parameter must be a valid Mongo instance");
				return;
			}
		} else {
			/** 
			 * Server must be defined otherwise
			 */
			phalcon_array_isset_string_fetch(&server, options, SS("server"));
			if (!server || Z_TYPE_P(server) != IS_STRING) {
				PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The backend requires a valid MongoDB connection string");
				return;
			}
	
			ce0 = zend_fetch_class(SL("MongoClient"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
	
			PHALCON_INIT_VAR(mongo);
			object_init_ex(mongo, ce0);
			assert(phalcon_has_constructor(mongo TSRMLS_CC));
			PHALCON_CALL_METHOD(NULL, mongo, "__construct", server);
		}
	
		/** 
		 * Check if the database name is a string
		 */
		phalcon_array_isset_string_fetch(&database, options, SS("db"));
		if (!database || Z_TYPE_P(database) != IS_STRING) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The backend requires a valid MongoDB db");
			return;
		}
	
		/** 
		 * Retrieve the connection name
		 */
		phalcon_array_isset_string_fetch(&collection, options, SS("collection"));
		if (!collection || Z_TYPE_P(collection) != IS_STRING) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The backend requires a valid MongoDB collection");
			return;
		}
	
		/** 
		 * Make the connection and get the collection
		 */
		PHALCON_CALL_METHOD(&mongo_database, mongo, "selectdb", database);
		PHALCON_RETURN_CALL_METHOD(mongo_database, "selectcollection", collection);
	}
	else {
		RETVAL_ZVAL(mongo_collection, 1, 0);
	}
	
	PHALCON_MM_RESTORE();
}
Пример #21
0
/**
 * Builds a SELECT statement
 *
 * @param array $definition
 * @return string
 */
PHP_METHOD(Phalcon_Db_Dialect, select){

	zval *definition, *escape_char = NULL, *columns, *selected_columns, *distinct;
	zval *column = NULL, *column_sql = NULL;
	zval *column_domain_sql = NULL, *column_alias_sql = NULL;
	zval *columns_sql = NULL, *tables, *selected_tables;
	zval *table = NULL, *sql_table = NULL, *tables_sql = NULL, *sql, *joins;
	zval *join = NULL, *type = NULL, *sql_join = NULL, *join_conditions_array = NULL;
	zval *join_expressions = NULL, *join_condition = NULL, *join_expression = NULL;
	zval *join_conditions = NULL, *where_conditions;
	zval *where_expression = NULL, *group_items, *group_fields;
	zval *group_field = NULL, *group_expression = NULL, *group_sql;
	zval *group_clause, *having_conditions, *having_expression = NULL;
	zval *order_fields, *order_items, *order_item = NULL;
	zval *order_expression = NULL, *order_sql_item = NULL, *sql_order_type = NULL;
	zval *order_sql_item_type = NULL, *order_sql, *tmp1 = NULL, *tmp2 = NULL;
	zval *limit_value;
	zval *number, *offset;
	HashTable *ah0, *ah1, *ah2, *ah3, *ah4, *ah5;
	HashPosition hp0, hp1, hp2, hp3, hp4, hp5;
	zval **hd;

	PHALCON_MM_GROW();

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

	if (Z_TYPE_P(definition) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid SELECT definition");
		return;
	}
	if (!phalcon_array_isset_string(definition, SS("tables"))) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "The index 'tables' is required in the definition array");
		return;
	}
	
	if (!phalcon_array_isset_string_fetch(&columns, definition, SS("columns"))) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "The index 'columns' is required in the definition array");
		return;
	}
	
	if (PHALCON_GLOBAL(db).escape_identifiers) {
		PHALCON_OBS_VAR(escape_char);
		phalcon_read_property_this(&escape_char, this_ptr, SL("_escapeChar"), PH_NOISY TSRMLS_CC);
	} else {
		PHALCON_INIT_NVAR(escape_char);
	}
	
	if (Z_TYPE_P(columns) == IS_ARRAY) { 
	
		PHALCON_INIT_VAR(selected_columns);
		array_init(selected_columns);
	
		phalcon_is_iterable(columns, &ah0, &hp0, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
			zval *column_item, *column_alias, *column_domain;
	
			PHALCON_GET_HVALUE(column);
	
			/** 
			 * Escape column name
			 */
			if (
				    phalcon_array_isset_long_fetch(&column_item, column, 0)
				 || phalcon_array_isset_string_fetch(&column_item, column, SS("column"))
			) {
				if (Z_TYPE_P(column_item) == IS_ARRAY) {
					PHALCON_CALL_METHOD(&column_sql, this_ptr, "getsqlexpression", column_item, escape_char);
				} else if (PHALCON_IS_STRING(column_item, "*")) {
					PHALCON_CPY_WRT(column_sql, column_item);
				} else if (PHALCON_GLOBAL(db).escape_identifiers) {
					PHALCON_INIT_NVAR(column_sql);
					PHALCON_CONCAT_VVV(column_sql, escape_char, column_item, escape_char);
				} else {
					PHALCON_CPY_WRT(column_sql, column_item);
				}
			}
			else {
				PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid SELECT definition");
				return;
			}
	
			/** 
			 * Escape column domain
			 */
			if (phalcon_array_isset_long_fetch(&column_domain, column, 1)) {
				if (zend_is_true(column_domain)) {
					if (PHALCON_GLOBAL(db).escape_identifiers) {
						PHALCON_INIT_NVAR(column_domain_sql);
						PHALCON_CONCAT_VVVSV(column_domain_sql, escape_char, column_domain, escape_char, ".", column_sql);
					} else {
						PHALCON_INIT_NVAR(column_domain_sql);
						PHALCON_CONCAT_VSV(column_domain_sql, column_domain, ".", column_sql);
					}
				} else {
					PHALCON_CPY_WRT(column_domain_sql, column_sql);
				}
			} else {
				PHALCON_CPY_WRT(column_domain_sql, column_sql);
			}
	
			/** 
			 * Escape column alias
			 */
			if (phalcon_array_isset_long_fetch(&column_alias, column, 2)) {
	
				if (zend_is_true(column_alias)) {
					if (PHALCON_GLOBAL(db).escape_identifiers) {
						PHALCON_INIT_NVAR(column_alias_sql);
						PHALCON_CONCAT_VSVVV(column_alias_sql, column_domain_sql, " AS ", escape_char, column_alias, escape_char);
					} else {
						PHALCON_INIT_NVAR(column_alias_sql);
						PHALCON_CONCAT_VSV(column_alias_sql, column_domain_sql, " AS ", column_alias);
					}
				} else {
					PHALCON_CPY_WRT(column_alias_sql, column_domain_sql);
				}
			} else {
				PHALCON_CPY_WRT(column_alias_sql, column_domain_sql);
			}
	
			phalcon_array_append(&selected_columns, column_alias_sql, PH_SEPARATE);
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
		PHALCON_INIT_VAR(columns_sql);
		phalcon_fast_join_str(columns_sql, SL(", "), selected_columns TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(columns_sql, columns);
	}
	
	/** 
	 * Check and escape tables
	 */
	PHALCON_OBS_VAR(tables);
	phalcon_array_fetch_string(&tables, definition, SL("tables"), PH_NOISY);
	if (Z_TYPE_P(tables) == IS_ARRAY) { 
	
		PHALCON_INIT_VAR(selected_tables);
		array_init(selected_tables);
	
		phalcon_is_iterable(tables, &ah1, &hp1, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
			PHALCON_GET_HVALUE(table);
	
			PHALCON_CALL_METHOD(&sql_table, this_ptr, "getsqltable", table, escape_char);
			phalcon_array_append(&selected_tables, sql_table, PH_SEPARATE);
	
			zend_hash_move_forward_ex(ah1, &hp1);
		}
	
		PHALCON_INIT_VAR(tables_sql);
		phalcon_fast_join_str(tables_sql, SL(", "), selected_tables TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(tables_sql, tables);
	}

	PHALCON_INIT_VAR(sql);
	if (phalcon_array_isset_string_fetch(&distinct, definition, SS("distinct"))) {
		assert(Z_TYPE_P(distinct) == IS_LONG);
		if (Z_LVAL_P(distinct) == 0) {
			ZVAL_STRING(sql, "SELECT ALL ", 1);
		}
		else if (Z_LVAL_P(distinct) == 1) {
			ZVAL_STRING(sql, "SELECT DISTINCT ", 1);
		}
		else {
			ZVAL_STRING(sql, "SELECT ", 1);
		}
	}
	else {
		ZVAL_STRING(sql, "SELECT ", 1);
	}
	
	PHALCON_SCONCAT_VSV(sql, columns_sql, " FROM ", tables_sql);
	
	/** 
	 * Check for joins
	 */
	if (phalcon_array_isset_string(definition, SS("joins"))) {
	
		PHALCON_OBS_VAR(joins);
		phalcon_array_fetch_string(&joins, definition, SL("joins"), PH_NOISY);
	
		phalcon_is_iterable(joins, &ah2, &hp2, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) {
	
			PHALCON_GET_HVALUE(join);
	
			PHALCON_OBS_NVAR(type);
			phalcon_array_fetch_string(&type, join, SL("type"), PH_NOISY);
	
			PHALCON_OBS_NVAR(table);
			phalcon_array_fetch_string(&table, join, SL("source"), PH_NOISY);
	
			PHALCON_CALL_METHOD(&sql_table, this_ptr, "getsqltable", table, escape_char);
			phalcon_array_append(&selected_tables, sql_table, PH_SEPARATE);
	
			PHALCON_INIT_NVAR(sql_join);
			PHALCON_CONCAT_SVSV(sql_join, " ", type, " JOIN ", sql_table);
	
			/** 
			 * Check if the join has conditions
			 */
			if (phalcon_array_isset_string(join, SS("conditions"))) {
	
				PHALCON_OBS_NVAR(join_conditions_array);
				phalcon_array_fetch_string(&join_conditions_array, join, SL("conditions"), PH_NOISY);
				if (phalcon_fast_count_ev(join_conditions_array TSRMLS_CC)) {
	
					PHALCON_INIT_NVAR(join_expressions);
					array_init(join_expressions);
	
					phalcon_is_iterable(join_conditions_array, &ah3, &hp3, 0, 0);
	
					while (zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) == SUCCESS) {
	
						PHALCON_GET_HVALUE(join_condition);
	
						PHALCON_CALL_METHOD(&join_expression, this_ptr, "getsqlexpression", join_condition, escape_char);
						phalcon_array_append(&join_expressions, join_expression, PH_SEPARATE);
	
						zend_hash_move_forward_ex(ah3, &hp3);
					}
	
					PHALCON_INIT_NVAR(join_conditions);
					phalcon_fast_join_str(join_conditions, SL(" AND "), join_expressions TSRMLS_CC);
					PHALCON_SCONCAT_SVS(sql_join, " ON ", join_conditions, " ");
				}
			}
	
			phalcon_concat_self(&sql, sql_join TSRMLS_CC);
	
			zend_hash_move_forward_ex(ah2, &hp2);
		}
	
	}
	
	/* Check for a WHERE clause */
	if (phalcon_array_isset_string_fetch(&where_conditions, definition, SS("where"))) {
	
		if (Z_TYPE_P(where_conditions) == IS_ARRAY) { 
			PHALCON_CALL_METHOD(&where_expression, this_ptr, "getsqlexpression", where_conditions, escape_char);
			PHALCON_SCONCAT_SV(sql, " WHERE ", where_expression);
		} else {
			PHALCON_SCONCAT_SV(sql, " WHERE ", where_conditions);
		}
	}
	
	/* Check for a GROUP clause */
	if (phalcon_array_isset_string_fetch(&group_fields, definition, SS("group"))) {
	
		PHALCON_INIT_VAR(group_items);
		array_init(group_items);
	
		phalcon_is_iterable(group_fields, &ah4, &hp4, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah4, (void**) &hd, &hp4) == SUCCESS) {
	
			PHALCON_GET_HVALUE(group_field);
	
			PHALCON_CALL_METHOD(&group_expression, this_ptr, "getsqlexpression", group_field, escape_char);
			phalcon_array_append(&group_items, group_expression, PH_SEPARATE);
	
			zend_hash_move_forward_ex(ah4, &hp4);
		}
	
		PHALCON_INIT_VAR(group_sql);
		phalcon_fast_join_str(group_sql, SL(", "), group_items TSRMLS_CC);
	
		PHALCON_INIT_VAR(group_clause);
		PHALCON_CONCAT_SV(group_clause, " GROUP BY ", group_sql);
		phalcon_concat_self(&sql, group_clause TSRMLS_CC);
	}

	/* Check for a HAVING clause */
	if (phalcon_array_isset_string_fetch(&having_conditions, definition, SS("having"))) {
		PHALCON_CALL_METHOD(&having_expression, this_ptr, "getsqlexpression", having_conditions, escape_char);
		PHALCON_SCONCAT_SV(sql, " HAVING ", having_expression);
	}
	
	/* Check for a ORDER clause */
	if (phalcon_array_isset_string_fetch(&order_fields, definition, SS("order"))) {
		PHALCON_INIT_VAR(order_items);
		array_init(order_items);
	
		phalcon_is_iterable(order_fields, &ah5, &hp5, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah5, (void**) &hd, &hp5) == SUCCESS) {
	
			PHALCON_GET_HVALUE(order_item);
	
			PHALCON_OBS_NVAR(order_expression);
			phalcon_array_fetch_long(&order_expression, order_item, 0, PH_NOISY);
	
			PHALCON_CALL_METHOD(&order_sql_item, this_ptr, "getsqlexpression", order_expression, escape_char);
	
			/** 
			 * In the numeric 1 position could be a ASC/DESC clause
			 */
			if (phalcon_array_isset_long(order_item, 1)) {
				PHALCON_OBS_NVAR(sql_order_type);
				phalcon_array_fetch_long(&sql_order_type, order_item, 1, PH_NOISY);
	
				PHALCON_INIT_NVAR(order_sql_item_type);
				PHALCON_CONCAT_VSV(order_sql_item_type, order_sql_item, " ", sql_order_type);
			} else {
				PHALCON_CPY_WRT(order_sql_item_type, order_sql_item);
			}
	
			phalcon_array_append(&order_items, order_sql_item_type, PH_SEPARATE);
	
			zend_hash_move_forward_ex(ah5, &hp5);
		}
	
		PHALCON_INIT_VAR(order_sql);
		phalcon_fast_join_str(order_sql, SL(", "), order_items TSRMLS_CC);
		PHALCON_SCONCAT_SV(sql, " ORDER BY ", order_sql);
	}
	
	/** 
	 * Check for a LIMIT condition
	 */
	if (phalcon_array_isset_string_fetch(&limit_value, definition, SS("limit"))) {
		if (likely(Z_TYPE_P(limit_value) == IS_ARRAY)) {
			if (likely(phalcon_array_isset_string_fetch(&number, limit_value, SS("number")))) {
				PHALCON_OBS_NVAR(tmp1);
				phalcon_array_fetch_string(&tmp1, number, SL("value"), PH_NOISY);

				/**
				 * Check for a OFFSET condition
				 */
				if (phalcon_array_isset_string_fetch(&offset, limit_value, SS("offset"))) {
					PHALCON_OBS_NVAR(tmp2);
					phalcon_array_fetch_string(&tmp2, offset, SL("value"), PH_NOISY);
					PHALCON_SCONCAT_SVSV(sql, " LIMIT ", tmp1, " OFFSET ", tmp2);
				} else {
					PHALCON_SCONCAT_SV(sql, " LIMIT ", tmp1);
				}
			}
		} else {
			PHALCON_SCONCAT_SV(sql, " LIMIT ", limit_value);
		}
	}
	
	RETURN_CTOR(sql);
}
Пример #22
0
PHP_METHOD(Phalcon_Http_Client_Adapter_Curl, sendInternal){

	zval *uri = NULL, *url = NULL, *method, *useragent, *data, *type, *files, *timeout, *curl, *username, *password, *authtype;
	zval *file = NULL, *body, *uniqid = NULL, *boundary, *key = NULL, *value = NULL;
	zval *path_parts = NULL, *filename, *basename, *filedata = NULL;
	zval *constant, *header, *headers = NULL;
	zval *content = NULL, *errorno = NULL, *error = NULL, *headersize = NULL, *httpcode = NULL, *headerstr, *bodystr, *response, *tmp = NULL;
	zend_class_entry *curlfile_ce;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;

	PHALCON_MM_GROW();

	PHALCON_CALL_SELF(&uri, "geturi");
	PHALCON_CALL_METHOD(&url, uri, "build");

	method = phalcon_fetch_nproperty_this(this_ptr, SL("_method"), PH_NOISY TSRMLS_CC);
	useragent = phalcon_fetch_nproperty_this(this_ptr, SL("_useragent"), PH_NOISY TSRMLS_CC);
	data = phalcon_fetch_nproperty_this(this_ptr, SL("_data"), PH_NOISY TSRMLS_CC);
	type = phalcon_fetch_nproperty_this(this_ptr, SL("_type"), PH_NOISY TSRMLS_CC);
	files = phalcon_fetch_nproperty_this(this_ptr, SL("_files"), PH_NOISY TSRMLS_CC);
	timeout = phalcon_fetch_nproperty_this(this_ptr, SL("_timeout"), PH_NOISY TSRMLS_CC);
	curl = phalcon_fetch_nproperty_this(this_ptr, SL("_curl"), PH_NOISY TSRMLS_CC);
	username = phalcon_fetch_nproperty_this(this_ptr, SL("_username"), PH_NOISY TSRMLS_CC);
	password = phalcon_fetch_nproperty_this(this_ptr, SL("_password"), PH_NOISY TSRMLS_CC);
	authtype = phalcon_fetch_nproperty_this(this_ptr, SL("_authtype"), PH_NOISY TSRMLS_CC);

	PHALCON_INIT_VAR(constant);
	if (!zend_get_constant(SL("CURLOPT_URL"), constant TSRMLS_CC)) {
		RETURN_MM_FALSE;
	}

	PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, url);

	PHALCON_INIT_NVAR(constant);
	if (zend_get_constant(SL("CURLOPT_CONNECTTIMEOUT"), constant TSRMLS_CC)) {
		PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, timeout);
	}

	PHALCON_INIT_NVAR(constant);
	if (zend_get_constant(SL("CURLOPT_TIMEOUT"), constant TSRMLS_CC)) {
		PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, timeout);
	}

	PHALCON_INIT_NVAR(constant);
	if (PHALCON_IS_NOT_EMPTY(method) && zend_get_constant(SL("CURLOPT_CUSTOMREQUEST"), constant TSRMLS_CC)) {
		PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, method);
	}

	PHALCON_INIT_NVAR(constant);
	if (zend_get_constant(SL("CURLOPT_USERAGENT"), constant TSRMLS_CC) && PHALCON_IS_NOT_EMPTY(useragent)) {
		PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, useragent);
	}

	header = phalcon_fetch_nproperty_this(this_ptr, SL("_header"), PH_NOISY TSRMLS_CC);

	if (PHALCON_IS_NOT_EMPTY(username)) {
		if (PHALCON_IS_STRING(authtype, "any")) {
			PHALCON_INIT_NVAR(constant);
			PHALCON_INIT_NVAR(tmp);
			if (zend_get_constant(SL("CURLOPT_HTTPAUTH"), constant TSRMLS_CC) && zend_get_constant(SL("CURLAUTH_ANY"), tmp TSRMLS_CC)) {
				PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, tmp);

				PHALCON_INIT_NVAR(constant);
				if (zend_get_constant(SL("CURLOPT_USERPWD"), constant TSRMLS_CC)) {
					PHALCON_INIT_NVAR(tmp);
					PHALCON_CONCAT_VSV(tmp, username, ":", password);
					PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, tmp);
				}
			}
		} else if (PHALCON_IS_STRING(authtype, "basic")) {
			PHALCON_INIT_NVAR(constant);
			PHALCON_INIT_NVAR(tmp);
			if (zend_get_constant(SL("CURLOPT_HTTPAUTH"), constant TSRMLS_CC) && zend_get_constant(SL("CURLAUTH_BASIC"), tmp TSRMLS_CC)) {
				PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, tmp);

				PHALCON_INIT_NVAR(constant);
				if (zend_get_constant(SL("CURLOPT_USERPWD"), constant TSRMLS_CC)) {
					PHALCON_INIT_NVAR(tmp);
					PHALCON_CONCAT_VSV(tmp, username, ":", password);
					PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, tmp);
				}
			}
		} else if (PHALCON_IS_STRING(authtype, "digest")) {
			PHALCON_INIT_NVAR(constant);
			PHALCON_INIT_NVAR(tmp);
			if (zend_get_constant(SL("CURLOPT_HTTPAUTH"), constant TSRMLS_CC) && zend_get_constant(SL("CURLAUTH_DIGEST"), tmp TSRMLS_CC)) {
				PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, tmp);

				PHALCON_INIT_NVAR(constant);
				if (zend_get_constant(SL("CURLOPT_USERPWD"), constant TSRMLS_CC)) {
					PHALCON_INIT_NVAR(tmp);
					PHALCON_CONCAT_VSV(tmp, username, ":", password);
					PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, tmp);
				}
			}
		}
	}

	PHALCON_INIT_NVAR(constant);
	if (zend_get_constant(SL("CURLOPT_SAFE_UPLOAD"), constant TSRMLS_CC)) {
		PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, PHALCON_GLOBAL(z_true));
	}

	if (Z_TYPE_P(data) == IS_STRING && PHALCON_IS_NOT_EMPTY(data)) {
		PHALCON_INIT_NVAR(key);
		ZVAL_STRING(key, "Content-Type", 1);

		if (PHALCON_IS_EMPTY(type)) {
			PHALCON_INIT_NVAR(type);
			ZVAL_STRING(type, "application/x-www-form-urlencoded", 1);
		}

		PHALCON_CALL_METHOD(NULL, header, "set", key, type);

		PHALCON_INIT_NVAR(constant);
		zend_get_constant(SL("CURLOPT_POSTFIELDS"), constant TSRMLS_CC);

		PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, data);
	} else if (phalcon_class_exists(SL("CURLFile"), 0 TSRMLS_CC)) {
		if (Z_TYPE_P(data) != IS_ARRAY) {
			PHALCON_INIT_NVAR(data);
			array_init(data);
		}

		if (Z_TYPE_P(files) == IS_ARRAY) {
			phalcon_is_iterable(files, &ah0, &hp0, 0, 0);
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
				PHALCON_GET_HVALUE(file);

				if (PHALCON_IS_NOT_EMPTY(file)) {
					curlfile_ce = zend_fetch_class(SL("CURLFile"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);

					PHALCON_INIT_NVAR(tmp);
					object_init_ex(tmp, curlfile_ce);
					PHALCON_CALL_METHOD(NULL, tmp, "__construct", file);

					phalcon_array_append(&data, tmp, PH_COPY);
				}

				zend_hash_move_forward_ex(ah0, &hp0);
			}
		}

		PHALCON_INIT_NVAR(constant);
		zend_get_constant(SL("CURLOPT_POSTFIELDS"), constant TSRMLS_CC);

		PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, data);
	} else {
		PHALCON_CALL_FUNCTION(&uniqid, "uniqid");

		PHALCON_INIT_VAR(boundary);
		PHALCON_CONCAT_SV(boundary, "--------------", uniqid);

		PHALCON_INIT_VAR(body);

		if (Z_TYPE_P(data) == IS_ARRAY) {
			phalcon_is_iterable(data, &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);

				PHALCON_SCONCAT_SVS(body, "--", boundary, "\r\n");
				PHALCON_SCONCAT_SVSVS(body, "Content-Disposition: form-data; name=\"", key, "\"\r\n\r\n", value, "\r\n");

				zend_hash_move_forward_ex(ah0, &hp0);
			}
		}


		if (Z_TYPE_P(files) == IS_ARRAY) {
			phalcon_is_iterable(files, &ah1, &hp1, 0, 0);
			while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
				PHALCON_GET_HVALUE(file);

				if (PHALCON_IS_NOT_EMPTY(file)) {
					PHALCON_CALL_FUNCTION(&path_parts, "pathinfo", file);

					if (phalcon_array_isset_string_fetch(&filename, path_parts, SS("filename")) && phalcon_array_isset_string_fetch(&basename, path_parts, SS("basename"))) {
						PHALCON_CALL_FUNCTION(&filedata, "file_get_contents", file);

						PHALCON_SCONCAT_SVS(body, "--", boundary, "\r\n");
						PHALCON_SCONCAT_SVSVS(body, "Content-Disposition: form-data; name=\"", filename, "\"; filename=\"", basename, "\"\r\n");
						PHALCON_SCONCAT_SVS(body, "Content-Type: application/octet-stream\r\n\r\n", filedata, "\r\n");
					}
				}

				zend_hash_move_forward_ex(ah1, &hp1);
			}
		}

		if (!PHALCON_IS_EMPTY(body)) {
			PHALCON_SCONCAT_SVS(body, "--", boundary, "--\r\n");

			PHALCON_INIT_NVAR(key);
			ZVAL_STRING(key, "Content-Type", 1);

			PHALCON_INIT_NVAR(value);
			PHALCON_CONCAT_SV(value, "multipart/form-data; boundary=", boundary);

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

			PHALCON_INIT_NVAR(key);
			ZVAL_STRING(key, "Content-Length", 1);		

			PHALCON_INIT_NVAR(value);
			ZVAL_LONG(value, Z_STRLEN_P(body));

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

			PHALCON_CALL_METHOD(&headers, header, "build");

			PHALCON_INIT_NVAR(constant);
			zend_get_constant(SL("CURLOPT_POSTFIELDS"), constant TSRMLS_CC);

			PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, body);

			PHALCON_INIT_NVAR(constant);
			zend_get_constant(SL("CURLOPT_HTTPHEADER"), constant TSRMLS_CC);

			PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, headers);
		}
	}

	PHALCON_CALL_FUNCTION(&content, "curl_exec", curl);
	PHALCON_CALL_FUNCTION(&errorno, "curl_errno", curl);

	if (PHALCON_IS_TRUE(errorno)) {
		PHALCON_CALL_FUNCTION(&error, "curl_error", curl);

		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_http_client_exception_ce, error);
		return;
	}

	PHALCON_INIT_VAR(response);
	object_init_ex(response, phalcon_http_client_response_ce);
	PHALCON_CALL_METHOD(NULL, response, "__construct");

	PHALCON_INIT_NVAR(constant);
	if (zend_get_constant(SL("CURLINFO_HTTP_CODE"), constant TSRMLS_CC)) {
		PHALCON_CALL_FUNCTION(&httpcode, "curl_getinfo", curl, constant);
		PHALCON_CALL_METHOD(NULL, response, "setstatuscode", httpcode);
	}

	PHALCON_INIT_NVAR(constant);
	if (zend_get_constant(SL("CURLINFO_HEADER_SIZE"), constant TSRMLS_CC)) {
		PHALCON_CALL_FUNCTION(&headersize, "curl_getinfo", curl, constant);

		PHALCON_INIT_VAR(headerstr);
		phalcon_substr(headerstr, content, 0 , Z_LVAL_P(headersize));

		PHALCON_INIT_VAR(bodystr);
		phalcon_substr(bodystr, content, Z_LVAL_P(headersize) , Z_STRLEN_P(content));

		PHALCON_CALL_METHOD(NULL, response, "setheader", headerstr);
		PHALCON_CALL_METHOD(NULL, response, "setbody", bodystr);
	}

	RETURN_CTOR(response);
}
Пример #23
0
/**
 * Phalcon\Db\Column constructor
 *
 * @param string $columnName
 * @param array $definition
 */
PHP_METHOD(Phalcon_Db_Column, __construct){

	zval *column_name, *definition, *type, *not_null;
	zval *primary, *size, *scale, *dunsigned, *is_numeric;
	zval *auto_increment, *first, *after, *bind_type, *default_value;

	phalcon_fetch_params(0, 2, 0, &column_name, &definition);
	
	phalcon_update_property_this(this_ptr, SL("_columnName"), column_name TSRMLS_CC);
	
	/** 
	 * Get the column type, one of the TYPE_* constants
	 */
	if (phalcon_array_isset_string_fetch(&type, definition, SS("type"))) {
		phalcon_update_property_this(this_ptr, SL("_type"), type TSRMLS_CC);
	} else {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "Column type is required");
		return;
	}
	
	/** 
	 * Check if the field is nullable
	 */
	if (phalcon_array_isset_string_fetch(&not_null, definition, SS("notNull"))) {
		phalcon_update_property_this(this_ptr, SL("_notNull"), not_null TSRMLS_CC);
	}
	
	/** 
	 * Check if the field is primary key
	 */
	if (phalcon_array_isset_string_fetch(&primary, definition, SS("primary"))) {
		phalcon_update_property_this(this_ptr, SL("_primary"), primary TSRMLS_CC);
	}
	
	if (phalcon_array_isset_string_fetch(&size, definition, SS("size"))) {
		phalcon_update_property_this(this_ptr, SL("_size"), size TSRMLS_CC);
	}
	
	/** 
	 * Check if the column has a decimal scale
	 */
	if (phalcon_array_isset_string_fetch(&scale, definition, SS("scale"))) {
		int i_type     = phalcon_get_intval(type);
		int is_numeric = (i_type == 3 || i_type == 7 || i_type == 9);
	
		if (is_numeric) {
			phalcon_update_property_this(this_ptr, SL("_scale"), scale TSRMLS_CC);
		}
	}
	
	/** 
	 * Check if the field is unsigned (only MySQL)
	 */
	if (phalcon_array_isset_string_fetch(&dunsigned, definition, SS("unsigned"))) {
		phalcon_update_property_this(this_ptr, SL("_unsigned"), dunsigned TSRMLS_CC);
	}
	
	/** 
	 * Check if the field is numeric
	 */
	if (phalcon_array_isset_string_fetch(&is_numeric, definition, SS("isNumeric"))) {
		phalcon_update_property_this(this_ptr, SL("_isNumeric"), is_numeric TSRMLS_CC);
	}
	
	/** 
	 * Check if the field is auto-increment/serial
	 */
	if (phalcon_array_isset_string_fetch(&auto_increment, definition, SS("autoIncrement"))) {
		if (PHALCON_IS_LONG(type, 0)) {
			phalcon_update_property_this(this_ptr, SL("_autoIncrement"), auto_increment TSRMLS_CC);
		} else {
			PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "Column type cannot be auto-increment");
			return;
		}
	}
	
	/** 
	 * Check if the field is placed at the first position of the table
	 */
	if (phalcon_array_isset_string_fetch(&first, definition, SS("first"))) {
		phalcon_update_property_this(this_ptr, SL("_first"), first TSRMLS_CC);
	}
	
	/** 
	 * Name of the column which is placed before the current field
	 */
	if (phalcon_array_isset_string_fetch(&after, definition, SS("after"))) {
		phalcon_update_property_this(this_ptr, SL("_after"), after TSRMLS_CC);
	}
	
	/** 
	 * The bind type to cast the field when passing it to PDO
	 */
	if (phalcon_array_isset_string_fetch(&bind_type, definition, SS("bindType"))) {
		phalcon_update_property_this(this_ptr, SL("_bindType"), bind_type TSRMLS_CC);
	}
	
	/** 
	 * Default values
	 */
	if (phalcon_array_isset_string_fetch(&default_value, definition, SS("default"))) {
		phalcon_update_property_this(this_ptr, SL("_default"), default_value TSRMLS_CC);
	}
}
Пример #24
0
PHP_METHOD(Phalcon_Http_Client_Adapter_Stream, sendInternal){

	zval *stream, *http, *option = NULL, *header, *handler, *base_uri, *url = NULL, *method, *useragent, *timeout;
	zval *fp = NULL, *meta = NULL, *wrapper_data, *bodystr = NULL, *response;

	PHALCON_MM_GROW();

	stream = phalcon_fetch_nproperty_this(this_ptr, SL("_stream"), PH_NOISY TSRMLS_CC);
	header = phalcon_fetch_nproperty_this(this_ptr, SL("_header"), PH_NOISY TSRMLS_CC);
	method = phalcon_fetch_nproperty_this(this_ptr, SL("_method"), PH_NOISY TSRMLS_CC);
	base_uri = phalcon_fetch_nproperty_this(this_ptr, SL("_base_uri"), PH_NOISY TSRMLS_CC);
	useragent = phalcon_fetch_nproperty_this(this_ptr, SL("_useragent"), PH_NOISY TSRMLS_CC);
	timeout = phalcon_fetch_nproperty_this(this_ptr, SL("_timeout"), PH_NOISY TSRMLS_CC);

	PHALCON_INIT_VAR(http);
	ZVAL_STRING(http, "http", 1);
	
	PHALCON_INIT_NVAR(option);
	ZVAL_STRING(option, "method", 1);

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

	if (PHALCON_IS_NOT_EMPTY(useragent)) {
		PHALCON_INIT_NVAR(option);
		ZVAL_STRING(option, "User-Agent", 1);

		PHALCON_CALL_METHOD(NULL, header, "set", option, useragent);
	}

	PHALCON_CALL_FUNCTION(NULL, "stream_context_set_option", stream, http, option, useragent);
	
	PHALCON_INIT_NVAR(option);
	ZVAL_STRING(option, "timeout", 1);

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

	PHALCON_CALL_METHOD(&url, base_uri, "build");

	PHALCON_CALL_SELF(NULL, "buildBody");

	PHALCON_INIT_VAR(handler);
	array_init_size(handler, 2);
	phalcon_array_append(&handler, this_ptr, PH_SEPARATE);
	add_next_index_stringl(handler, SL("errorHandler"), 1);

	PHALCON_CALL_FUNCTION(NULL, "set_error_handler", handler);

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

	PHALCON_CALL_FUNCTION(&fp, "fopen", url, option, PHALCON_GLOBAL(z_false), stream);

	PHALCON_CALL_FUNCTION(NULL, "restore_error_handler");

	PHALCON_CALL_FUNCTION(&meta, "stream_get_meta_data", fp);
	PHALCON_CALL_FUNCTION(&bodystr, "stream_get_contents", fp);
	PHALCON_CALL_FUNCTION(NULL, "fclose", fp);

	PHALCON_INIT_VAR(response);
	object_init_ex(response, phalcon_http_client_response_ce);
	PHALCON_CALL_METHOD(NULL, response, "__construct");

	if (phalcon_array_isset_string_fetch(&wrapper_data, meta, SS("wrapper_data"))) {
		PHALCON_CALL_METHOD(NULL, response, "setHeader", wrapper_data);
	}

	PHALCON_CALL_METHOD(NULL, response, "setbody", bodystr);

	RETURN_CTOR(response);
}
Пример #25
0
PHP_METHOD(Phalcon_Http_Client_Adapter_Stream, buildBody){

	zval *stream, *header, *data, *file, *username, *password, *authtype, *digest, *method, *entity_body;
	zval *key = NULL, *value = NULL, *realm, *qop, *nonce, *nc, *cnonce, *qoc, *ha1 = NULL, *path = NULL, *md5_entity_body = NULL, *ha2 = NULL;
	zval *http, *option = NULL, *body, *headers = NULL, *uniqid = NULL, *boundary;
	zval *path_parts = NULL, *filename, *basename, *filedata = NULL, *tmp = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	stream = phalcon_fetch_nproperty_this(this_ptr, SL("_stream"), PH_NOISY TSRMLS_CC);
	header = phalcon_fetch_nproperty_this(this_ptr, SL("_header"), PH_NOISY TSRMLS_CC);
	data = phalcon_fetch_nproperty_this(this_ptr, SL("_data"), PH_NOISY TSRMLS_CC);
	file = phalcon_fetch_nproperty_this(this_ptr, SL("_file"), PH_NOISY TSRMLS_CC);
	username = phalcon_fetch_nproperty_this(this_ptr, SL("_username"), PH_NOISY TSRMLS_CC);
	password = phalcon_fetch_nproperty_this(this_ptr, SL("_password"), PH_NOISY TSRMLS_CC);
	authtype = phalcon_fetch_nproperty_this(this_ptr, SL("_authtype"), PH_NOISY TSRMLS_CC);
	digest = phalcon_fetch_nproperty_this(this_ptr, SL("_digest"), PH_NOISY TSRMLS_CC);
	method = phalcon_fetch_nproperty_this(this_ptr, SL("_method"), PH_NOISY TSRMLS_CC);
	entity_body = phalcon_fetch_nproperty_this(this_ptr, SL("_entity_body"), PH_NOISY TSRMLS_CC);

	if (PHALCON_IS_NOT_EMPTY(username)) {
		if (PHALCON_IS_STRING(authtype, "basic")) {
			PHALCON_INIT_NVAR(key);
			ZVAL_STRING(key, "Authorization", 1);

			PHALCON_INIT_NVAR(value);
			PHALCON_CONCAT_SVSV(value, "Basic ", username, ":", password);

			PHALCON_CALL_METHOD(NULL, header, "set", key, value);
		} else if (PHALCON_IS_STRING(authtype, "digest") && PHALCON_IS_NOT_EMPTY(digest)) {
			if (phalcon_array_isset_string_fetch(&realm, digest, SS("realm"))) {
				PHALCON_INIT_VAR(realm);
				ZVAL_NULL(realm);
			}

			PHALCON_INIT_NVAR(tmp);
			PHALCON_CONCAT_VSVSV(tmp, username, ":", realm, ":", password);

			PHALCON_CALL_FUNCTION(&ha1, "md5", tmp);

			if (!phalcon_array_isset_string_fetch(&qop, digest, SS("qop"))) {
				PHALCON_INIT_VAR(qop);
				ZVAL_NULL(qop);
			}

			if (PHALCON_IS_EMPTY(qop) || phalcon_memnstr_str(qop, SL("auth"))) {
				PHALCON_CALL_SELF(&path, "getpath");

				PHALCON_INIT_NVAR(tmp);
				PHALCON_CONCAT_VSV(tmp, method, ":", path);

				PHALCON_CALL_FUNCTION(&ha2, "md5", tmp);
				
			} else if (phalcon_memnstr_str(qop, SL("auth-int"))) {
				PHALCON_CALL_SELF(&path, "getpath");

				PHALCON_CALL_FUNCTION(&md5_entity_body, "md5", entity_body);

				PHALCON_INIT_NVAR(tmp);
				PHALCON_CONCAT_VSVSV(tmp, method, ":", path, ":", md5_entity_body);

				PHALCON_CALL_FUNCTION(&ha2, "md5", tmp);
			}

			PHALCON_INIT_NVAR(key);
			ZVAL_STRING(key, "Authorization", 1);

			if (phalcon_array_isset_string_fetch(&nonce, digest, SS("nonce"))) {
				PHALCON_INIT_VAR(nonce);
				ZVAL_NULL(nonce);
			}


			if (PHALCON_IS_EMPTY(qop)) {
				PHALCON_INIT_NVAR(tmp);
				PHALCON_CONCAT_VSVSV(tmp, ha1, ":", nonce, ":", ha2);

				PHALCON_CALL_FUNCTION(&value, "md5", tmp);

				PHALCON_INIT_NVAR(tmp);
				PHALCON_CONCAT_SV(tmp, "Digest ", value);

				PHALCON_CALL_METHOD(NULL, header, "set", key, tmp);
			} else {			
				if (phalcon_array_isset_string_fetch(&nc, digest, SS("nc"))) {
					PHALCON_INIT_VAR(nc);
					ZVAL_NULL(nc);
				}
				
				if (phalcon_array_isset_string_fetch(&cnonce, digest, SS("cnonce"))) {
					PHALCON_INIT_VAR(cnonce);
					ZVAL_NULL(cnonce);
				}
				
				if (phalcon_array_isset_string_fetch(&qoc, digest, SS("qoc"))) {
					PHALCON_INIT_VAR(qoc);
					ZVAL_NULL(qoc);
				}
				
				if (phalcon_array_isset_string_fetch(&qoc, digest, SS("qoc"))) {
					PHALCON_INIT_VAR(qoc);
					ZVAL_NULL(qoc);
				}

				PHALCON_INIT_NVAR(tmp);
				PHALCON_CONCAT_VSVSVS(tmp, ha1, ":", nonce, ":", nc, ":");

				PHALCON_SCONCAT_VSVSV(tmp, cnonce, ":", qoc, ":", ha2);

				PHALCON_CALL_FUNCTION(&value, "md5", tmp);

				PHALCON_INIT_NVAR(tmp);
				PHALCON_CONCAT_SV(tmp, "Digest ", value);

				PHALCON_CALL_METHOD(NULL, header, "set", key, tmp);
			}
		}
	}

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

	PHALCON_CALL_FUNCTION(&uniqid, "uniqid");

	PHALCON_INIT_VAR(boundary);
	PHALCON_CONCAT_SV(boundary, "--------------", uniqid);

	PHALCON_INIT_VAR(body);

	if (PHALCON_IS_NOT_EMPTY(data) && Z_TYPE_P(data) == IS_STRING){
		PHALCON_INIT_NVAR(key);
		ZVAL_STRING(key, "Content-Type", 1);

		PHALCON_INIT_NVAR(value);
		ZVAL_STRING(value, "application/x-www-form-urlencoded", 1);

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

		PHALCON_INIT_NVAR(option);
		ZVAL_LONG(option, PHALCON_HTTP_CLIENT_HEADER_BUILD_FIELDS);
		
		PHALCON_CALL_METHOD(&headers, header, "build", option);

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

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

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

		PHALCON_CALL_FUNCTION(NULL, "stream_context_set_option", stream, http, option, data);
		RETURN_MM();
	}
	
	if (Z_TYPE_P(data) == IS_ARRAY) {
		phalcon_is_iterable(data, &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);

			PHALCON_SCONCAT_SVS(body, "--", boundary, "\r\n");
			PHALCON_SCONCAT_SVSVS(body, "Content-Disposition: form-data; name=\"", key, "\"\r\n\r\n", value, "\r\n");

			zend_hash_move_forward_ex(ah0, &hp0);
		}
	}

	if (PHALCON_IS_NOT_EMPTY(file)) {
		PHALCON_CALL_FUNCTION(&path_parts, "pathinfo", file);

		if (phalcon_array_isset_string_fetch(&filename, path_parts, SS("filename")) && phalcon_array_isset_string_fetch(&basename, path_parts, SS("basename"))) {
			PHALCON_CALL_FUNCTION(&filedata, "file_get_contents", file);

			PHALCON_SCONCAT_SVS(body, "--", boundary, "\r\n");
			PHALCON_SCONCAT_SVSVS(body, "Content-Disposition: form-data; name=\"", filename, "\"; filename=\"", basename, "\"\r\n");
			PHALCON_SCONCAT_SVS(body, "Content-Type: application/octet-stream\r\n\r\n", filedata, "\r\n");
		}
	}

	if (!PHALCON_IS_EMPTY(body)) {
		PHALCON_SCONCAT_SVS(body, "--", boundary, "--\r\n");

		PHALCON_INIT_NVAR(key);
		ZVAL_STRING(key, "Content-Type", 1);

		PHALCON_INIT_NVAR(value);
		PHALCON_CONCAT_SV(value, "multipart/form-data; boundary=", boundary);

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

		PHALCON_INIT_NVAR(key);
		ZVAL_STRING(key, "Content-Length", 1);		

		PHALCON_INIT_NVAR(value);
		ZVAL_LONG(value, Z_STRLEN_P(body));

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

		PHALCON_INIT_NVAR(option);
		ZVAL_LONG(option, PHALCON_HTTP_CLIENT_HEADER_BUILD_FIELDS);
		
		PHALCON_CALL_METHOD(&headers, header, "build", option);

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

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

		PHALCON_INIT_NVAR(option);
		ZVAL_STRING(option, "content", 1);
		
		PHALCON_CALL_FUNCTION(NULL, "stream_context_set_option", stream, http, option, body);
	}

	PHALCON_MM_RESTORE();
}
Пример #26
0
/**
 * Resolves an annotation expression
 *
 * @param array $expr
 * @return mixed
 */
PHP_METHOD(Phalcon_Annotations_Annotation, getExpression){

	zval *expr = NULL, *type, *items, *item = NULL;
	zval *resolved_item = NULL, *exception_message;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &expr);
	
	PHALCON_SEPARATE_PARAM(expr);
	
	if (Z_TYPE_P(expr) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_annotations_exception_ce, "The expression is not valid");
		return;
	}
	
	PHALCON_OBS_VAR(type);
	phalcon_array_fetch_string(&type, expr, SL("type"), PH_NOISY);
	
	switch (phalcon_get_intval(type)) {
	
		case PHANNOT_T_INTEGER:
		case PHANNOT_T_DOUBLE:
		case PHANNOT_T_STRING:
		case PHANNOT_T_IDENTIFIER:
			if (return_value_ptr) {
				zval_ptr_dtor(return_value_ptr);
				phalcon_array_fetch_string(return_value_ptr, expr, SL("value"), PH_NOISY);
			}
			else {
				phalcon_array_fetch_string(&return_value, expr, SL("value"), PH_NOISY);
			}
			RETURN_MM();
			/* no break because of implicit return */
	
		case PHANNOT_T_NULL:
			RETURN_MM_NULL();
			/* no break because of implicit return */
	
		case PHANNOT_T_FALSE:
			RETURN_MM_FALSE;
			/* no break because of implicit return */
	
		case PHANNOT_T_TRUE:
			RETURN_MM_TRUE;
			/* no break because of implicit return */
	
		case PHANNOT_T_ARRAY:
			PHALCON_OBS_VAR(items);
			phalcon_array_fetch_string(&items, expr, SL("items"), PH_NOISY);
	
			phalcon_is_iterable(items, &ah0, &hp0, 0, 0);
			array_init_size(return_value, zend_hash_num_elements(ah0));
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
				zval *name;
	
				PHALCON_GET_HVALUE(item);
	
				PHALCON_OBS_NVAR(expr);
				phalcon_array_fetch_string(&expr, item, SL("expr"), PH_NOISY);
	
				PHALCON_CALL_METHOD(&resolved_item, this_ptr, "getexpression", expr);
				if (phalcon_array_isset_string_fetch(&name, item, SS("name"))) {
					phalcon_array_update_zval(&return_value, name, resolved_item, PH_COPY);
				} else {
					phalcon_array_append(&return_value, resolved_item, 0);
				}
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
			RETURN_MM();
			/* no break because of implicit return */
	
		case PHANNOT_T_ANNOTATION:
			object_init_ex(return_value, phalcon_annotations_annotation_ce);
			PHALCON_CALL_METHOD(NULL, return_value, "__construct", expr);
			RETURN_MM();
			/* no break because of implicit return */
	
		default:
			PHALCON_INIT_VAR(exception_message);
			PHALCON_CONCAT_SVS(exception_message, "The expression ", type, " is unknown");
			PHALCON_THROW_EXCEPTION_ZVAL(phalcon_annotations_exception_ce, exception_message);
			return;
	
	}
	
	PHALCON_MM_RESTORE();
}
Пример #27
0
/**
 * Check whether internal resource has rows to fetch
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset_Complex, valid){

	zval *type = NULL, *row = NULL, *underscore;
	zval *empty_str, *active_row = NULL;
	zval *dirty_state, *column = NULL, *alias = NULL, *source = NULL, *attributes = NULL;
	zval *column_map = NULL, *row_model = NULL, *attribute = NULL, *column_alias = NULL;
	zval *column_value = NULL;
	zval *value = NULL, *sql_alias = NULL, *n_alias = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;
	int i_type, is_partial;

	PHALCON_MM_GROW();

	type       = phalcon_fetch_nproperty_this(this_ptr, SL("_type"), PH_NOISY TSRMLS_CC);
	i_type     = (Z_TYPE_P(type) == IS_LONG) ? Z_LVAL_P(type) : phalcon_get_intval(type);
	is_partial = (i_type == PHALCON_MVC_MODEL_RESULTSET_TYPE_PARTIAL);
	type       = NULL;

	PHALCON_INIT_VAR(row);
	if (is_partial) {
		/** 
		 * The result is bigger than 32 rows so it's retrieved one by one
		 */
		zval *result = phalcon_fetch_nproperty_this(this_ptr, SL("_result"), PH_NOISY TSRMLS_CC);
		if (PHALCON_IS_NOT_FALSE(result)) {
			PHALCON_CALL_METHOD(&row, result, "fetch", result);
		} else {
			ZVAL_FALSE(row);
		}
	} else {
		/** 
		 * The full rows are dumped in this_ptr->rows
		 */
		zval *rows = phalcon_fetch_nproperty_this(this_ptr, SL("_rows"), PH_NOISY TSRMLS_CC);
		if (Z_TYPE_P(rows) == IS_ARRAY) { 
			phalcon_array_get_current(row, rows);
			if (Z_TYPE_P(row) == IS_OBJECT) {
				zend_hash_move_forward(Z_ARRVAL_P(rows));
			}
		} else {
			ZVAL_FALSE(row);
		}
	}
	
	/** 
	 * Valid records are arrays
	 */
	if (Z_TYPE_P(row) == IS_ARRAY || Z_TYPE_P(row) == IS_OBJECT) {
	
		/** 
		 * The result type=1 so we need to build every row
		 */
		if (is_partial) {
	
			/** 
			 * Get current hydration mode
			 */
			zval *hydrate_mode  = phalcon_fetch_nproperty_this(this_ptr, SL("_hydrateMode"), PH_NOISY TSRMLS_CC);
			zval *columns_types = phalcon_fetch_nproperty_this(this_ptr, SL("_columnTypes"), PH_NOISY TSRMLS_CC);
			int i_hydrate_mode  = phalcon_get_intval(hydrate_mode);
	
			PHALCON_INIT_VAR(underscore);
			ZVAL_STRING(underscore, "_", 1);
	
			PHALCON_INIT_VAR(empty_str);
			ZVAL_EMPTY_STRING(empty_str);
	
			/** 
			 * Each row in a complex result is a Phalcon\Mvc\Model\Row instance
			 */
			PHALCON_INIT_VAR(active_row);
			switch (i_hydrate_mode) {
				case 0:
					object_init_ex(active_row, phalcon_mvc_model_row_ce);
					break;
	
				case 1:
					array_init(active_row);
					break;
	
				case 2:
				default:
					object_init(active_row);
					break;
			}
	
			/** 
			 * Create every record according to the column types
			 */
	
			/** 
			 * Set records as dirty state PERSISTENT by default
			 */
			PHALCON_INIT_VAR(dirty_state);
			ZVAL_LONG(dirty_state, 0);
	
			phalcon_is_iterable(columns_types, &ah0, &hp0, 0, 0);
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_HKEY(alias, ah0, hp0);
				PHALCON_GET_HVALUE(column);
	
				PHALCON_OBS_NVAR(type);
				phalcon_array_fetch_string(&type, column, SL("type"), PH_NOISY);
				if (PHALCON_IS_STRING(type, "object")) {
	
					/** 
					 * Object columns are assigned column by column
					 */
					PHALCON_OBS_NVAR(source);
					phalcon_array_fetch_string(&source, column, SL("column"), PH_NOISY);
	
					PHALCON_OBS_NVAR(attributes);
					phalcon_array_fetch_string(&attributes, column, SL("attributes"), PH_NOISY);
	
					PHALCON_OBS_NVAR(column_map);
					phalcon_array_fetch_string(&column_map, column, SL("columnMap"), PH_NOISY);
	
					/** 
					 * Assign the values from the _source_attribute notation to its real column name
					 */
					PHALCON_INIT_NVAR(row_model);
					array_init(row_model);
	
					phalcon_is_iterable(attributes, &ah1, &hp1, 0, 0);
	
					while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
						PHALCON_GET_HVALUE(attribute);
	
						/** 
						 * Columns are supposed to be in the form _table_field
						 */
						PHALCON_INIT_NVAR(column_alias);
						PHALCON_CONCAT_VVVV(column_alias, underscore, source, underscore, attribute);
	
						PHALCON_OBS_NVAR(column_value);
						phalcon_array_fetch(&column_value, row, column_alias, PH_NOISY);
						phalcon_array_update_zval(&row_model, attribute, column_value, PH_COPY | PH_SEPARATE);
	
						zend_hash_move_forward_ex(ah1, &hp1);
					}
	
					/** 
					 * Generate the column value according to the hydration type
					 */
					switch (phalcon_get_intval(hydrate_mode)) {
	
						case 0: {
							zval *keep_snapshots, *instance;

							/** 
							 * Check if the resultset must keep snapshots
							 */
							if (!phalcon_array_isset_string_fetch(&keep_snapshots, column, SS("keepSnapshots"))) {
								keep_snapshots = PHALCON_GLOBAL(z_false);
							}
	
							/** 
							 * Get the base instance
							 */
							if (!phalcon_array_isset_string_fetch(&instance, column, SS("instance"))) {
								php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Undefined index: instance");
								instance = PHALCON_GLOBAL(z_null);
							}
	
							/** 
							 * Assign the values to the attributes using a column map
							 */
							PHALCON_CALL_CE_STATIC(&value, phalcon_mvc_model_ce, "cloneresultmap", instance, row_model, column_map, dirty_state, keep_snapshots);
							break;
						}
	
						default:
							/** 
							 * Other kinds of hydrations
							 */
							PHALCON_CALL_CE_STATIC(&value, phalcon_mvc_model_ce, "cloneresultmaphydrate", row_model, column_map, hydrate_mode);
							break;
	
					}
	
					/** 
					 * The complete object is assigned to an attribute with the name of the alias or
					 * the model name
					 */
					PHALCON_OBS_NVAR(attribute);
					if (phalcon_array_isset_string(column, SS("balias"))) {
						phalcon_array_fetch_string(&attribute, column, SL("balias"), PH_NOISY);
					}
				} else {
					/** 
					 * Scalar columns are simply assigned to the result object
					 */
					if (phalcon_array_isset_string(column, SS("sqlAlias"))) {
						PHALCON_OBS_NVAR(sql_alias);
						phalcon_array_fetch_string(&sql_alias, column, SL("sqlAlias"), PH_NOISY);
	
						PHALCON_OBS_NVAR(value);
						phalcon_array_fetch(&value, row, sql_alias, PH_NOISY);
					} else {
						PHALCON_OBS_NVAR(value);
						if (phalcon_array_isset(row, alias)) {
							phalcon_array_fetch(&value, row, alias, PH_NOISY);
						}
					}
	
					/** 
					 * If a 'balias' is defined is not an unnamed scalar
					 */
					if (phalcon_array_isset_string(column, SS("balias"))) {
						PHALCON_CPY_WRT(attribute, alias);
					} else {
						PHALCON_INIT_NVAR(n_alias);
						phalcon_fast_str_replace(n_alias, underscore, empty_str, alias);
						PHALCON_CPY_WRT(attribute, n_alias);
					}

					assert(attribute != NULL);
				}
	
				/** 
				 * Assign the instance according to the hydration type
				 */
				if (unlikely(!attribute)) {
					zend_throw_exception_ex(phalcon_mvc_model_exception_ce, 0 TSRMLS_CC, "Unexpected inconsistency: attribute is NULL");
					RETURN_MM();
				}
	
				switch (phalcon_get_intval(hydrate_mode)) {
	
					case 1:
						phalcon_array_update_zval(&active_row, attribute, value, PH_COPY | PH_SEPARATE);
						break;
	
					default:
						phalcon_update_property_zval_zval(active_row, attribute, value TSRMLS_CC);
						break;
	
				}
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
			/** 
			 * Store the generated row in this_ptr->activeRow to be retrieved by 'current'
			 */
			phalcon_update_property_this(this_ptr, SL("_activeRow"), active_row TSRMLS_CC);
		} else {
			/** 
			 * The row is already built so we just assign it to the activeRow
			 */
			phalcon_update_property_this(this_ptr, SL("_activeRow"), row TSRMLS_CC);
		}
		RETURN_MM_TRUE;
	}
	
	/** 
	 * There are no results to retrieve so we update this_ptr->activeRow as false
	 */
	phalcon_update_property_bool(this_ptr, SL("_activeRow"), 0 TSRMLS_CC);
	RETURN_MM_FALSE;
}
Пример #28
0
/**
 * Restores the internal state of a Phalcon\Db\Column object
 *
 * @param array $data
 * @return \Phalcon\Db\Column
 */
PHP_METHOD(Phalcon_Db_Column, __set_state){

	zval *data, *definition, *column_name, *column_type = NULL;
	zval *not_null, *primary, *size, *scale, *dunsigned, *after;
	zval *is_numeric, *first, *bind_type, *default_value;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &data);
	
	if (Z_TYPE_P(data) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Column state must be an array");
		return;
	}
	
	if (!phalcon_array_isset_string_fetch(&column_name, data, SS("_columnName"))) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Column name is required");
		return;
	}

	PHALCON_INIT_VAR(definition);
	array_init(definition);

	if (phalcon_array_isset_string_fetch(&column_type, data, SS("_type"))) {
		phalcon_array_update_string(&definition, SL("type"), column_type, PH_COPY);
	}
	
	if (phalcon_array_isset_string_fetch(&not_null, data, SS("_notNull"))) {
		phalcon_array_update_string(&definition, SL("notNull"), not_null, PH_COPY);
	}
	
	if (phalcon_array_isset_string_fetch(&primary, data, SS("_primary"))) {
		phalcon_array_update_string(&definition, SL("primary"), primary, PH_COPY);
	}
	
	if (phalcon_array_isset_string_fetch(&size, data, SS("_size"))) {
		phalcon_array_update_string(&definition, SL("size"), size, PH_COPY);
	}
	
	if (phalcon_array_isset_string_fetch(&scale, data, SS("_scale"))) {
		phalcon_array_update_string(&definition, SL("scale"), scale, PH_COPY);
	}

	if (phalcon_array_isset_string_fetch(&dunsigned, data, SS("_unsigned"))) {
		phalcon_array_update_string(&definition, SL("unsigned"), dunsigned, PH_COPY);
	}
	
	if (phalcon_array_isset_string_fetch(&after, data, SS("_after"))) {
		phalcon_array_update_string(&definition, SL("after"), after, PH_COPY);
	}
	
	if (phalcon_array_isset_string_fetch(&is_numeric, data, SS("_isNumeric"))) {
		phalcon_array_update_string(&definition, SL("isNumeric"), is_numeric, PH_COPY);
	}
	
	if (phalcon_array_isset_string_fetch(&first, data, SS("_first"))) {
		phalcon_array_update_string(&definition, SL("first"), first, PH_COPY);
	}
	
	if (phalcon_array_isset_string_fetch(&bind_type, data, SS("_bindType"))) {
		phalcon_array_update_string(&definition, SL("bindType"), bind_type, PH_COPY);
	}
	
	if (phalcon_array_isset_string_fetch(&default_value, data, SS("_default"))) {
		phalcon_array_update_string(&definition, SL("default"), default_value, PH_COPY);
	}
	
	object_init_ex(return_value, phalcon_db_column_ce);
	PHALCON_CALL_METHOD(NULL, return_value, "__construct", column_name, definition);
	
	RETURN_MM();
}
Пример #29
0
/**
 * Builds a SELECT statement
 *
 * @param array $definition
 * @return string
 */
PHP_METHOD(Phalcon_Db_Dialect_Oracle, select){

	zval *definition, *escape_char = NULL, *columns, *selected_columns, *distinct;
	zval *column = NULL, *column_item = NULL, *column_sql = NULL, *columns_sql = NULL;
	zval *column_domain = NULL, *column_domain_sql = NULL, *column_alias = NULL;
	zval *column_alias_sql = NULL, *tables, *selected_tables;
	zval *table = NULL, *sql_table = NULL, *tables_sql = NULL, *sql, *joins;
	zval *join = NULL, *type = NULL, *sql_join = NULL, *join_conditions_array = NULL;
	zval *join_expressions = NULL, *join_condition = NULL, *join_expression = NULL;
	zval *join_conditions = NULL, *where_conditions;
	zval *where_expression = NULL, *group_items, *group_fields;
	zval *group_field = NULL, *group_expression = NULL, *group_sql;
	zval *group_clause, *having_conditions, *having_expression = NULL;
	zval *order_fields, *order_items, *order_item = NULL;
	zval *order_expression = NULL, *order_sql_item = NULL, *sql_order_type = NULL;
	zval *order_sql_item_type = NULL, *order_sql, *limit_value;
	zval *number, *offset, *tmp1 = NULL, *tmp2 = NULL;
	zval *z_one, *ini_range, *end_range = NULL, *sql_limit;
	HashTable *ah0, *ah1, *ah2, *ah3, *ah4, *ah5;
	HashPosition hp0, hp1, hp2, hp3, hp4, hp5;
	zval **hd;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &definition) == FAILURE) {
		RETURN_NULL();
	}

	if (Z_TYPE_P(definition) != IS_ARRAY) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "Invalid SELECT definition");
		return;
	}
	if (!phalcon_array_isset_string(definition, SS("tables"))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "The index 'tables' is required in the definition array");
		return;
	}

	if (!phalcon_array_isset_string(definition, SS("columns"))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "The index 'columns' is required in the definition array");
		return;
	}

	PHALCON_MM_GROW();

	if (PHALCON_GLOBAL(db).escape_identifiers) {
		PHALCON_OBS_VAR(escape_char);
		phalcon_read_property_this(&escape_char, this_ptr, SL("_escapeChar"), PH_NOISY TSRMLS_CC);
	} else {
		PHALCON_INIT_NVAR(escape_char);
	}

	PHALCON_OBS_VAR(columns);
	phalcon_array_fetch_string(&columns, definition, SL("columns"), PH_NOISY);
	if (Z_TYPE_P(columns) == IS_ARRAY) {

		PHALCON_INIT_VAR(selected_columns);
		array_init(selected_columns);

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

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

			PHALCON_GET_HVALUE(column);

			/**
			 * Escape column name
			 */
			PHALCON_OBS_NVAR(column_item);
			phalcon_array_fetch_long(&column_item, column, 0, PH_NOISY);
			if (Z_TYPE_P(column_item) == IS_ARRAY) {
				PHALCON_CALL_METHOD(&column_sql, this_ptr, "getsqlexpression", column_item, escape_char);
			} else if (PHALCON_IS_STRING(column_item, "*")) {
				PHALCON_CPY_WRT(column_sql, column_item);
			} else if (PHALCON_GLOBAL(db).escape_identifiers) {
				PHALCON_INIT_NVAR(column_sql);
				PHALCON_CONCAT_VVV(column_sql, escape_char, column_item, escape_char);
			} else {
				PHALCON_CPY_WRT(columns_sql, column_item);
			}

			/**
			 * Escape column domain
			 */
			if (phalcon_array_isset_long(column, 1)) {

				PHALCON_OBS_NVAR(column_domain);
				phalcon_array_fetch_long(&column_domain, column, 1, PH_NOISY);
				if (zend_is_true(column_domain)) {
					if (PHALCON_GLOBAL(db).escape_identifiers) {
						PHALCON_INIT_NVAR(column_domain_sql);
						PHALCON_CONCAT_VVVSV(column_domain_sql, escape_char, column_domain, escape_char, ".", column_sql);
					} else {
						PHALCON_INIT_NVAR(column_domain_sql);
						PHALCON_CONCAT_VSV(column_domain_sql, column_domain, ".", column_sql);
					}
				} else {
					PHALCON_CPY_WRT(column_domain_sql, column_sql);
				}
			} else {
				PHALCON_CPY_WRT(column_domain_sql, column_sql);
			}

			/**
			 * Escape column alias
			 */
			if (phalcon_array_isset_long(column, 2)) {

				PHALCON_OBS_NVAR(column_alias);
				phalcon_array_fetch_long(&column_alias, column, 2, PH_NOISY);
				if (zend_is_true(column_alias)) {
					if (PHALCON_GLOBAL(db).escape_identifiers) {
						PHALCON_INIT_NVAR(column_alias_sql);
						PHALCON_CONCAT_VSVVV(column_alias_sql, column_domain_sql, " ", escape_char, column_alias, escape_char);
					} else {
						PHALCON_INIT_NVAR(column_alias_sql);
						PHALCON_CONCAT_VSV(column_alias_sql, column_domain_sql, " ", column_alias);
					}
				} else {
					PHALCON_CPY_WRT(column_alias_sql, column_domain_sql);
				}
			} else {
				PHALCON_CPY_WRT(column_alias_sql, column_domain_sql);
			}

			phalcon_array_append(&selected_columns, column_alias_sql, PH_SEPARATE);

			zend_hash_move_forward_ex(ah0, &hp0);
		}

		PHALCON_INIT_VAR(columns_sql);
		phalcon_fast_join_str(columns_sql, SL(", "), selected_columns TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(columns_sql, columns);
	}

	/**
	 * Check and escape tables
	 */
	PHALCON_OBS_VAR(tables);
	phalcon_array_fetch_string(&tables, definition, SL("tables"), PH_NOISY);
	if (Z_TYPE_P(tables) == IS_ARRAY) {

		PHALCON_INIT_VAR(selected_tables);
		array_init(selected_tables);

		phalcon_is_iterable(tables, &ah1, &hp1, 0, 0);

		while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {

			PHALCON_GET_HVALUE(table);

			PHALCON_CALL_METHOD(&sql_table, this_ptr, "getsqltable", table, escape_char);
			phalcon_array_append(&selected_tables, sql_table, PH_SEPARATE);

			zend_hash_move_forward_ex(ah1, &hp1);
		}

		PHALCON_INIT_VAR(tables_sql);
		phalcon_fast_join_str(tables_sql, SL(", "), selected_tables TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(tables_sql, tables);
	}

	PHALCON_INIT_VAR(sql);
	if (phalcon_array_isset_string_fetch(&distinct, definition, SS("definition"))) {
		assert(Z_TYPE_P(distinct) == IS_LONG);
		if (Z_LVAL_P(distinct) == 0) {
			ZVAL_STRING(sql, "SELECT ALL ", 1);
		}
		else if (Z_LVAL_P(distinct) == 1) {
			ZVAL_STRING(sql, "SELECT DISTINCT ", 1);
		}
		else {
			ZVAL_STRING(sql, "SELECT ", 1);
		}
	}
	else {
		ZVAL_STRING(sql, "SELECT ", 1);
	}

	/**
	 * Check for joins
	 */
	if (phalcon_array_isset_string(definition, SS("joins"))) {

		PHALCON_OBS_VAR(joins);
		phalcon_array_fetch_string(&joins, definition, SL("joins"), PH_NOISY);

		phalcon_is_iterable(joins, &ah2, &hp2, 0, 0);

		while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) {

			PHALCON_GET_HVALUE(join);

			PHALCON_OBS_NVAR(type);
			phalcon_array_fetch_string(&type, join, SL("type"), PH_NOISY);

			PHALCON_OBS_NVAR(table);
			phalcon_array_fetch_string(&table, join, SL("source"), PH_NOISY);

			PHALCON_CALL_METHOD(&sql_table, this_ptr, "getsqltable", table, escape_char);
			phalcon_array_append(&selected_tables, sql_table, PH_SEPARATE);

			PHALCON_INIT_NVAR(sql_join);
			PHALCON_CONCAT_SVSV(sql_join, " ", type, " JOIN ", sql_table);

			/**
			 * Check if the join has conditions
			 */
			if (phalcon_array_isset_string(join, SS("conditions"))) {

				PHALCON_OBS_NVAR(join_conditions_array);
				phalcon_array_fetch_string(&join_conditions_array, join, SL("conditions"), PH_NOISY);
				if (phalcon_fast_count_ev(join_conditions_array TSRMLS_CC)) {

					PHALCON_INIT_NVAR(join_expressions);
					array_init(join_expressions);

					phalcon_is_iterable(join_conditions_array, &ah3, &hp3, 0, 0);

					while (zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) == SUCCESS) {

						PHALCON_GET_HVALUE(join_condition);

						PHALCON_CALL_METHOD(&join_expression, this_ptr, "getsqlexpression", join_condition, escape_char);
						phalcon_array_append(&join_expressions, join_expression, PH_SEPARATE);

						zend_hash_move_forward_ex(ah3, &hp3);
					}

					PHALCON_INIT_NVAR(join_conditions);
					phalcon_fast_join_str(join_conditions, SL(" AND "), join_expressions TSRMLS_CC);
					PHALCON_SCONCAT_SV(sql_join, " ON ", join_conditions);
				}
			}

			phalcon_concat_self(&sql, sql_join TSRMLS_CC);

			zend_hash_move_forward_ex(ah2, &hp2);
		}

	}

	/**
	 * Check for a WHERE clause
	 */
	if (phalcon_array_isset_string(definition, SS("where"))) {

		PHALCON_OBS_VAR(where_conditions);
		phalcon_array_fetch_string(&where_conditions, definition, SL("where"), PH_NOISY);
		if (Z_TYPE_P(where_conditions) == IS_ARRAY) {
			PHALCON_CALL_METHOD(&where_expression, this_ptr, "getsqlexpression", where_conditions, escape_char);
			PHALCON_SCONCAT_SV(sql, " WHERE ", where_expression);
		} else {
			PHALCON_SCONCAT_SV(sql, " WHERE ", where_conditions);
		}
	}

	/**
	 * Check for a GROUP clause
	 */
	if (phalcon_array_isset_string(definition, SS("group"))) {

		PHALCON_INIT_VAR(group_items);
		array_init(group_items);

		PHALCON_OBS_VAR(group_fields);
		phalcon_array_fetch_string(&group_fields, definition, SL("group"), PH_NOISY);

		phalcon_is_iterable(group_fields, &ah4, &hp4, 0, 0);

		while (zend_hash_get_current_data_ex(ah4, (void**) &hd, &hp4) == SUCCESS) {

			PHALCON_GET_HVALUE(group_field);

			PHALCON_CALL_METHOD(&group_expression, this_ptr, "getsqlexpression", group_field, escape_char);
			phalcon_array_append(&group_items, group_expression, PH_SEPARATE);

			zend_hash_move_forward_ex(ah4, &hp4);
		}

		PHALCON_INIT_VAR(group_sql);
		phalcon_fast_join_str(group_sql, SL(", "), group_items TSRMLS_CC);

		PHALCON_INIT_VAR(group_clause);
		PHALCON_CONCAT_SV(group_clause, " GROUP BY ", group_sql);
		phalcon_concat_self(&sql, group_clause TSRMLS_CC);
	}

	/**
	 * Check for a HAVING clause
	 */
	if (phalcon_array_isset_string(definition, SS("having"))) {
		PHALCON_OBS_VAR(having_conditions);
		phalcon_array_fetch_string(&having_conditions, definition, SL("having"), PH_NOISY);

		PHALCON_CALL_METHOD(&having_expression, this_ptr, "getsqlexpression", having_conditions, escape_char);
		PHALCON_SCONCAT_SV(sql, " HAVING ", having_expression);
	}

	/**
	 * Check for a ORDER clause
	 */
	if (phalcon_array_isset_string(definition, SS("order"))) {

		PHALCON_OBS_VAR(order_fields);
		phalcon_array_fetch_string(&order_fields, definition, SL("order"), PH_NOISY);

		PHALCON_INIT_VAR(order_items);
		array_init(order_items);

		phalcon_is_iterable(order_fields, &ah5, &hp5, 0, 0);

		while (zend_hash_get_current_data_ex(ah5, (void**) &hd, &hp5) == SUCCESS) {

			PHALCON_GET_HVALUE(order_item);

			PHALCON_OBS_NVAR(order_expression);
			phalcon_array_fetch_long(&order_expression, order_item, 0, PH_NOISY);

			PHALCON_CALL_METHOD(&order_sql_item, this_ptr, "getsqlexpression", order_expression, escape_char);

			/**
			 * In the numeric 1 position could be a ASC/DESC clause
			 */
			if (phalcon_array_isset_long(order_item, 1)) {
				PHALCON_OBS_NVAR(sql_order_type);
				phalcon_array_fetch_long(&sql_order_type, order_item, 1, PH_NOISY);

				PHALCON_INIT_NVAR(order_sql_item_type);
				PHALCON_CONCAT_VSV(order_sql_item_type, order_sql_item, " ", sql_order_type);
			} else {
				PHALCON_CPY_WRT(order_sql_item_type, order_sql_item);
			}

			phalcon_array_append(&order_items, order_sql_item_type, PH_SEPARATE);

			zend_hash_move_forward_ex(ah5, &hp5);
		}

		PHALCON_INIT_VAR(order_sql);
		phalcon_fast_join_str(order_sql, SL(", "), order_items TSRMLS_CC);
		PHALCON_SCONCAT_SV(sql, " ORDER BY ", order_sql);
	}

    /**
	 * Oracle does not implement the LIMIT clause as some RDBMS do.
	 * We have to simulate it with subqueries and ROWNUM.
	 * Unfortunately because we use the column wildcard "*",
	 * this puts an extra column into the query result set.
	 */
	if (phalcon_array_isset_string_fetch(&limit_value, definition, SS("limit"))) {
		if (likely(Z_TYPE_P(limit_value) == IS_ARRAY)) {
			if (likely(phalcon_array_isset_string_fetch(&number, limit_value, SS("number")))) {
				PHALCON_OBS_NVAR(tmp1);
				phalcon_array_fetch_string(&tmp1, number, SL("value"), PH_NOISY);

				if (phalcon_array_isset_string_fetch(&offset, limit_value, SS("offset"))) {
					PHALCON_OBS_NVAR(tmp2);
					phalcon_array_fetch_string(&tmp2, offset, SL("value"), PH_NOISY);
				} else {
					PHALCON_INIT_NVAR(tmp2);
					ZVAL_LONG(tmp2, 0);
				}

				z_one = PHALCON_GLOBAL(z_one);

				PHALCON_INIT_VAR(ini_range);
				phalcon_add_function(ini_range, tmp2, z_one TSRMLS_CC);

				PHALCON_INIT_VAR(end_range);
				phalcon_add_function(end_range, tmp2, tmp1 TSRMLS_CC);

				PHALCON_INIT_VAR(sql_limit);
				PHALCON_SCONCAT_SVSVSV(sql_limit,"SELECT Z2.* FROM (SELECT Z1.*, ROWNUM DB_ROWNUM FROM ( ", sql, " ) Z1 ) Z2 WHERE Z2.DB_ROWNUM BETWEEN ", ini_range , " AND ",  end_range );
				PHALCON_CPY_WRT(sql, sql_limit);
			}
		} else {

			PHALCON_INIT_VAR(ini_range);
			ZVAL_LONG(ini_range, 1);

			PHALCON_CPY_WRT(end_range, limit_value);

			PHALCON_INIT_VAR(sql_limit);
			PHALCON_SCONCAT_SVSVSV(sql_limit,"SELECT Z2.* FROM (SELECT Z1.*, ROWNUM DB_ROWNUM FROM ( ", sql, " ) Z1 ) Z2 WHERE Z2.DB_ROWNUM BETWEEN ", ini_range , " AND ",  end_range );
			PHALCON_CPY_WRT(sql, sql_limit);
		}
	}

	RETURN_CTOR(sql);
}
Пример #30
0
/**
 * Stores cached content into the Memcached backend and stops the frontend
 *
 * @param int|string $keyName
 * @param string $content
 * @param long $lifetime
 * @param boolean $stopBuffer
 */
PHP_METHOD(Phalcon_Cache_Backend_Memcache, save){

	zval *key_name = NULL, *content = NULL, *lifetime = NULL, *stop_buffer = NULL;
	zval *cached_content = NULL, *prepared_content = NULL, *ttl = NULL, *flags, *success = NULL;
	zval *keys = NULL, *is_buffering = NULL;
	zval *last_key, *frontend, *memcache, *options, *special_key;

	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_fetch_nproperty_this(this_ptr, SL("_lastKey"), PH_NOISY TSRMLS_CC);
	} else {
		zval *prefix = phalcon_fetch_nproperty_this(this_ptr, SL("_prefix"), PH_NOISY TSRMLS_CC);
	
		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_fetch_nproperty_this(this_ptr, SL("_frontend"), PH_NOISY TSRMLS_CC);
	
	/** 
	 * Check if a connection is created or make a new one
	 */
	memcache = phalcon_fetch_nproperty_this(this_ptr, SL("_memcache"), PH_NOISY TSRMLS_CC);
	if (Z_TYPE_P(memcache) != IS_OBJECT) {
		memcache = NULL;
		PHALCON_CALL_METHOD(&memcache, this_ptr, "_connect");
	}

	if (!content || Z_TYPE_P(content) == IS_NULL) {
		PHALCON_CALL_METHOD(&cached_content, frontend, "getcontent");
	} else {
		cached_content = content;
	}
	
	/** 
	 * Prepare the content in the frontend
	 */
	PHALCON_CALL_METHOD(&prepared_content, frontend, "beforestore", cached_content);

	if (!lifetime || Z_TYPE_P(lifetime) == IS_NULL) {
		zval *tmp = phalcon_fetch_nproperty_this(this_ptr, SL("_lastLifetime"), PH_NOISY TSRMLS_CC);

		if (Z_TYPE_P(tmp) == IS_NULL) {
			PHALCON_CALL_METHOD(&ttl, frontend, "getlifetime");
		}
		else {
			ttl = tmp;
		}
	} else {
		ttl = lifetime;
	}
	
	PHALCON_INIT_VAR(flags);
	ZVAL_LONG(flags, 0);
	
	/** 
	 * We store without flags
	 */
	if (phalcon_is_numeric(cached_content)) {
		PHALCON_CALL_METHOD(&success, memcache, "set", last_key, cached_content, flags, ttl);
	} else {
		PHALCON_CALL_METHOD(&success, memcache, "set", last_key, prepared_content, flags, ttl);
	}

	if (!zend_is_true(success)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Failed to store data in memcached");
		return;
	}
	
	options = phalcon_fetch_nproperty_this(this_ptr, SL("_options"), PH_NOISY TSRMLS_CC);

	if (unlikely(!phalcon_array_isset_string_fetch(&special_key, options, SS("statsKey")))) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Unexpected inconsistency in options");
		return;
	}

	if (Z_TYPE_P(special_key) != IS_NULL) {
		/* Update the stats key */
		PHALCON_CALL_METHOD(&keys, memcache, "get", special_key);
		if (Z_TYPE_P(keys) != IS_ARRAY) {
			PHALCON_INIT_NVAR(keys);
			array_init(keys);
		}

		if (!phalcon_array_isset(keys, last_key)) {
			phalcon_array_update_zval(&keys, last_key, ttl, PH_COPY);
			PHALCON_CALL_METHOD(NULL, memcache, "set", special_key, keys);
		}
	}
	
	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(this_ptr, SL("_started"), 0 TSRMLS_CC);
	
	PHALCON_MM_RESTORE();
}