Esempio n. 1
0
/**
 * Phalcon\Logger\Adapter\Stream constructor
 *
 * @param string $name
 * @param array $options
 */
PHP_METHOD(Phalcon_Logger_Adapter_Stream, __construct){

	zval *name, *options = NULL, mode = {}, stream = {};

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

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

	if (phalcon_array_isset_fetch_str(&mode, options, SL("mode"))) {
		if (phalcon_memnstr_str(&mode, SL("r"))) {
			PHALCON_THROW_EXCEPTION_STRW(phalcon_logger_exception_ce, "Stream must be opened in append or write mode");
			return;
		}
	} else {
		ZVAL_STRING(&mode, "ab");
	}

	/** 
	 * We use 'fopen' to respect to open-basedir directive
	 */
	PHALCON_CALL_FUNCTIONW(&stream, "fopen", name, &mode);
	if (Z_TYPE(stream) != IS_RESOURCE) {
		zend_throw_exception_ex(phalcon_logger_exception_ce, 0, "Cannot open stream '%s'", Z_STRVAL_P(name));
	} else {
		phalcon_update_property_this(getThis(), SL("_stream"), &stream);
	}
}
Esempio n. 2
0
/**
 * Changes a parameter in the definition without resolve the service
 *
 * @param long $position
 * @param array $parameter
 * @return Phalcon\DI\Service
 */
PHP_METHOD(Phalcon_DI_Service, setParameter){

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

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

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

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

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

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

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

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

	RETURN_THISW();
}
Esempio n. 3
0
/**
 * Phalcon\Paginator\Adapter\Model constructor
 *
 * @param array $config
 */
PHP_METHOD(Phalcon_Paginator_Adapter_Model, __construct)
{
    zval *config, limit = {}, page = {};

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

    phalcon_update_property_zval(getThis(), SL("_config"), config);

    if (phalcon_array_isset_fetch_str(&limit, config, SL("limit"))) {
        phalcon_update_property_zval(getThis(), SL("_limitRows"), &limit);
    }

    if (phalcon_array_isset_fetch_str(&page, config, SL("page"))) {
        phalcon_update_property_zval(getThis(), SL("_page"), &page);
    }
}
Esempio n. 4
0
/**
 * Phalcon\Http\Uri constructor
 *
 * @param mixed $uri
 */
PHP_METHOD(Phalcon_Http_Uri, __construct)
{
	zval *uri = NULL, parts = {}, query = {}, params = {};

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

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

		phalcon_update_property_zval(getThis(), SL("_parts"), &parts);
	} else if (Z_TYPE_P(uri) == IS_ARRAY) {
		phalcon_update_property_zval(getThis(), SL("_parts"), uri);
	} else if (Z_TYPE_P(uri) == IS_OBJECT && Z_OBJCE_P(uri) == phalcon_http_uri_ce) {
		phalcon_return_property(&parts, uri, SL("_parts"));
		phalcon_update_property_zval(getThis(), SL("_parts"), &parts);
	} else {
		phalcon_update_property_empty_array(getThis(), SL("_parts"));
	}
}
Esempio n. 5
0
/**
 * Handles routing information received from command-line arguments
 *
 * @param array $arguments
 */
PHP_METHOD(Phalcon_CLI_Router, handle){

	zval *arguments = NULL, module_name = {}, namespace_name = {}, task_name = {}, action_name = {};

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

	if (!arguments || Z_TYPE_P(arguments) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STRW(phalcon_cli_router_exception_ce, "Arguments must be an Array");
		return;
	}

	PHALCON_SEPARATE_PARAM(arguments);

	/** 
	 * Check for a module
	 */
	if (phalcon_array_isset_fetch_str(&module_name, arguments, SL("module"))) {
		phalcon_array_unset_str(arguments, SL("module"), PH_COPY);
		phalcon_update_property_this(getThis(), SL("_module"), &module_name);
	}

	/**
	 * Check for a namespace
	 */
	if (phalcon_array_isset_fetch_str(&namespace_name, arguments, SL("namespace"))) {
		phalcon_array_unset_str(arguments, SL("namespace"), PH_COPY);
		phalcon_update_property_this(getThis(), SL("_namespace"), &namespace_name);
	}

	/** 
	 * Check for a task
	 */
	if (phalcon_array_isset_fetch_str(&task_name, arguments, SL("task"))) {
		phalcon_array_unset_str(arguments, SL("task"), PH_COPY);
		phalcon_update_property_this(getThis(), SL("_task"), &task_name);
	}

	/** 
	 * Check for an action
	 */
	if (phalcon_array_isset_fetch_str(&action_name, arguments, SL("action"))) {
		phalcon_array_unset_str(arguments, SL("action"), PH_COPY);
		phalcon_update_property_this(getThis(), SL("_action"), &action_name);
	}

	phalcon_update_property_this(getThis(), SL("_params"), arguments);
}
Esempio n. 6
0
/**
 * Reads the cookie-related info from the SESSION to restore the cookie as it was set
 * This method is automatically called internally so normally you don't need to call it
 *
 * @return Phalcon\Http\Cookie
 */
PHP_METHOD(Phalcon_Http_Cookie, restore)
{
	zval *restored, *dependency_injector, service = {}, session = {}, name = {}, key = {}, definition = {}, expire = {}, domain = {}, path = {}, secure = {}, http_only = {};

	restored = phalcon_read_property(getThis(), SL("_restored"), PH_NOISY);
	if (!zend_is_true(restored)) {
		dependency_injector = phalcon_read_property(getThis(), SL("_dependencyInjector"), PH_NOISY);
		if (Z_TYPE_P(dependency_injector) == IS_OBJECT) {
			ZVAL_STRING(&service, ISV(session));

			PHALCON_CALL_METHODW(&session, dependency_injector, "getshared", &service);
			PHALCON_VERIFY_INTERFACEW(&session, phalcon_session_adapterinterface_ce);

			phalcon_return_property(&name, getThis(), SL("_name"));

			PHALCON_CONCAT_SV(&key, "_PHCOOKIE_", &name);

			PHALCON_CALL_METHODW(&definition, &session, "get", &key);
			if (Z_TYPE(definition) == IS_ARRAY) { 
				if (phalcon_array_isset_fetch_str(&expire, &definition, SL("expire"))) {
					phalcon_update_property_this(getThis(), SL("_expire"), &expire);
				}
				if (phalcon_array_isset_fetch_str(&domain, &definition, SL("domain"))) {
					phalcon_update_property_this(getThis(), SL("_domain"), &domain);
				}

				if (phalcon_array_isset_fetch_str(&path, &definition, SL("path"))) {
					phalcon_update_property_this(getThis(), SL("_path"), &path);
				}

				if (phalcon_array_isset_fetch_str(&secure, &definition, SL("secure"))) {
					phalcon_update_property_this(getThis(), SL("_secure"), &secure);
				}

				if (phalcon_array_isset_fetch_str(&http_only, &definition, SL("httpOnly"))) {
					phalcon_update_property_this(getThis(), SL("_httpOnly"), &http_only);
				}
			}
		}

		phalcon_update_property_bool(getThis(), SL("_restored"), 1);
	}

	RETURN_THISW();
}
Esempio n. 7
0
/**
 * Restore the internal state of a service
 *
 * @param array $attributes
 * @return Phalcon\DI\Service
 */
PHP_METHOD(Phalcon_DI_Service, __set_state){

	zval *attributes, name = {}, definition = {}, shared = {};

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

	if (
			!phalcon_array_isset_fetch_str(&name, attributes, SL("_name"))
		 || !phalcon_array_isset_fetch_str(&definition, attributes, SL("_definition"))
		 || !phalcon_array_isset_fetch_str(&shared, attributes, SL("_shared"))
	) {
		PHALCON_THROW_EXCEPTION_STRW(spl_ce_BadMethodCallException, "Bad parameters passed to Phalcon\\DI\\Service::__set_state()");
		return;
	}

	object_init_ex(return_value, phalcon_di_service_ce);
	PHALCON_CALL_METHODW(NULL, return_value, "__construct", &name, &definition, &shared);
}
Esempio n. 8
0
/**
 * Phalcon\Mvc\Model\MetaData\Apc constructor
 *
 * @param array $options
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Apc, __construct){

	zval *options = NULL, prefix = {}, lifetime = {};

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

	if (options && Z_TYPE_P(options) == IS_ARRAY) {
		if (phalcon_array_isset_fetch_str(&prefix, options, SL("prefix"))) {
			phalcon_update_property_zval(getThis(), SL("_prefix"), &prefix);
		}

		if (phalcon_array_isset_fetch_str(&lifetime, options, SL("lifetime"))) {
			phalcon_update_property_zval(getThis(), SL("_ttl"), &lifetime);
		}
	}

	phalcon_update_property_empty_array(getThis(), SL("_metaData"));
}
Esempio n. 9
0
/**
 * Gets HTTP user agent used to made the request
 *
 * @return string
 */
PHP_METHOD(Phalcon_Http_Request, getUserAgent){

	zval *server, user_agent = {};

	server = phalcon_get_global_str(SL("_SERVER"));
	if (phalcon_array_isset_fetch_str(&user_agent, server, SL("HTTP_USER_AGENT"))) {
		RETURN_CTORW(&user_agent);
	}

	RETURN_EMPTY_STRING();
}
Esempio n. 10
0
/**
 * Gets active server name
 *
 * @return string
 */
PHP_METHOD(Phalcon_Http_Request, getServerName){

	zval *server, server_name = {};

	server = phalcon_get_global_str(SL("_SERVER"));
	if (phalcon_array_isset_fetch_str(&server_name, server, SL("SERVER_NAME"))) {
		RETURN_CTORW(&server_name);
	}

	RETURN_STRING("localhost");
}
Esempio n. 11
0
/**
 * Gets active server address IP
 *
 * @return string
 */
PHP_METHOD(Phalcon_Http_Request, getServerAddress){

	zval *server, server_addr = {};

	server = phalcon_get_global_str(SL("_SERVER"));
	if (phalcon_array_isset_fetch_str(&server_addr, server, SL("SERVER_ADDR"))) {
		RETURN_CTORW(&server_addr);
	}

	RETURN_STRING("127.0.0.1");
}
Esempio n. 12
0
/**
 * Gets most possible client IPv4 Address. This method search in $_SERVER['REMOTE_ADDR'] and optionally in $_SERVER['HTTP_X_FORWARDED_FOR']
 *
 * @param boolean $trustForwardedHeader
 * @return string
 */
PHP_METHOD(Phalcon_Http_Request, getClientAddress){

	zval *trust_forwarded_header = NULL, *_SERVER, address = {}, addresses = {}, first = {};

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

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

	_SERVER = phalcon_get_global_str(SL("_SERVER"));

	/**
	 * Proxies use this IP
	 */
	if (zend_is_true(trust_forwarded_header)) {
		if (!phalcon_array_isset_fetch_str(&address, _SERVER, SL("HTTP_X_FORWARDED_FOR"))) {
			if (!phalcon_array_isset_fetch_str(&address, _SERVER, SL("REMOTE_ADDR"))) {
				phalcon_array_fetch_str(&address, _SERVER, SL("REMOTE_ADDR"), PH_NOISY);
			}
		}
	} else if (!phalcon_array_isset_fetch_str(&address, _SERVER, SL("REMOTE_ADDR"))) {
		phalcon_array_fetch_str(&address, _SERVER, SL("REMOTE_ADDR"), PH_NOISY);
	}

	if (Z_TYPE(address) == IS_STRING) {
		if (phalcon_memnstr_str(&address, SL(","))) {
			/**
			 * The client address has multiples parts, only return the first part
			 */
			phalcon_fast_explode_str(&addresses, SL(","), &address);

			phalcon_array_fetch_long(&first, &addresses, 0, PH_NOISY);
			RETURN_CTORW(&first);
		}

		RETURN_CTORW(&address);
	}

	RETURN_FALSE;
}
Esempio n. 13
0
/**
 * Phalcon\Annotations\Adapter\Files constructor
 *
 * @param array $options
 */
PHP_METHOD(Phalcon_Annotations_Adapter_Files, __construct){

	zval *options = NULL, annotations_dir = {};

	phalcon_fetch_params(0, 0, 1, &options);
	
	if (options && Z_TYPE_P(options) == IS_ARRAY) {
		if (phalcon_array_isset_fetch_str(&annotations_dir, options, SL("annotationsDir"))) {
			phalcon_update_property_zval(getThis(), SL("_annotationsDir"), &annotations_dir);
		}
	}
}
Esempio n. 14
0
/**
 * Retrieve the URI path
 *
 * @return string
 */
PHP_METHOD(Phalcon_Http_Uri, getPath)
{
	zval parts = {}, value = {};

	phalcon_read_property(&parts, getThis(), SL("_parts"), PH_NOISY);

	if (!phalcon_array_isset_fetch_str(&value, &parts, SL("path"))) {
		 RETURN_NULL();
	}

	RETURN_CTORW(&value);
}
Esempio n. 15
0
/**
 * Phalcon\Binary\Writer constructor
 *
 * @param  string|resource $data
 * @param  int $endian
 * @throws \InvalidArgumentException
 */
PHP_METHOD(Phalcon_Binary_Writer, __construct){

	zval *data = NULL, *endian = NULL, filename = {}, mode = {}, handler = {}, fstat = {}, size = {};

	phalcon_fetch_params(0, 0, 2, &data, &endian);

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

	if (Z_TYPE_P(data) == IS_STRING || Z_TYPE_P(data) == IS_NULL) {
		ZVAL_STRING(&filename, "php://memory");
		ZVAL_STRING(&mode, "br+");
		PHALCON_CALL_FUNCTIONW(&handler, "fopen", &filename, &mode);
		PHALCON_CALL_FUNCTIONW(NULL, "fwrite", &handler, data);

		PHALCON_CALL_FUNCTIONW(&fstat, "fstat", &handler);
		if (phalcon_array_isset_fetch_str(&size, &fstat, SL("size"))) {
			phalcon_update_property_zval(getThis(), SL("_position"), &size);
		}
		phalcon_update_property_zval(getThis(), SL("_output"), &handler);
	} else if (Z_TYPE_P(data) == IS_RESOURCE) {
		phalcon_update_property_zval(getThis(), SL("_output"), data);

		PHALCON_CALL_FUNCTIONW(&fstat, "fstat", data);
		if (phalcon_array_isset_fetch_str(&size, &fstat, SL("size"))) {
			phalcon_update_property_zval(getThis(), SL("_position"), &size);
		}
	} else {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_binary_exception_ce, "Data must be set as string or resource");
		return;
	}

	if (endian && Z_TYPE_P(endian) != IS_NULL) {
		if (Z_TYPE_P(endian) != IS_LONG || Z_LVAL_P(endian) < 0 || Z_LVAL_P(endian) > 2) {
			PHALCON_THROW_EXCEPTION_STRW(phalcon_binary_exception_ce, "Endian must be set as big or little");
		}
		phalcon_update_property_zval(getThis(), SL("_endian"), endian);
	}
}
Esempio n. 16
0
/**
 * Create internal connection to memcached
 */
PHP_METHOD(Phalcon_Cache_Backend_Memcache, _connect)
{
	zval options = {}, memcache = {}, host = {}, port = {}, persistent = {}, success = {};
	zend_class_entry *ce0;

	phalcon_return_property(&options, getThis(), SL("_options"));
	ce0 = phalcon_fetch_str_class(SL("Memcache"), ZEND_FETCH_CLASS_AUTO);

	object_init_ex(&memcache, ce0);
	if (phalcon_has_constructor(&memcache)) {
		PHALCON_CALL_METHODW(NULL, &memcache, "__construct");
	}

	if (
		   !phalcon_array_isset_fetch_str(&host, &options, SL("host"))
		|| !phalcon_array_isset_fetch_str(&port, &options, SL("port"))
		|| !phalcon_array_isset_fetch_str(&persistent, &options, SL("persistent"))
	) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_cache_exception_ce, "Unexpected inconsistency in options");
		return;
	}

	if (zend_is_true(&persistent)) {
		PHALCON_CALL_METHODW(&success, &memcache, "pconnect", &host, &port);
	} else {
		PHALCON_CALL_METHODW(&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_zval(getThis(), SL("_memcache"), &memcache);
	RETURN_CTORW(&memcache);
}
Esempio n. 17
0
/**
 * Phalcon\Paginator\Adapter\NativeArray constructor
 *
 * @param array $config
 */
PHP_METHOD(Phalcon_Paginator_Adapter_NativeArray, __construct){

	zval *config, limit = {}, page = {}, data = {};

	phalcon_fetch_params(0, 1, 0, &config);
	
	if (phalcon_array_isset_fetch_str(&limit, config, SL("limit"))) {
		phalcon_update_property_this(getThis(), SL("_limitRows"), &limit);
	}
	
	if (phalcon_array_isset_fetch_str(&page, config, SL("page"))) {
		phalcon_update_property_this(getThis(), SL("_page"), &page);
	}

	if (!phalcon_array_isset_fetch_str(&data, config, SL("data"))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_paginator_exception_ce, "Parameter 'data' is required");
		return;
	} else if (Z_TYPE_P(&data) != IS_ARRAY) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_paginator_exception_ce, "'data' should be an array");
		return;
	}

	phalcon_update_property_this(getThis(), SL("_data"), &data);
}
Esempio n. 18
0
/**
 * Returns uri
 *
 * @return string
 */
PHP_METHOD(Phalcon_Http_Uri, build)
{
	zval parts = {}, uri = {}, scheme = {}, host = {}, user = {}, pass = {}, port = {}, path = {}, query = {}, fragment = {}, tmp = {};

	phalcon_read_property(&parts, getThis(), SL("_parts"), PH_NOISY);

	if (phalcon_array_isset_fetch_str(&scheme, &parts, SL("scheme")) && PHALCON_IS_NOT_EMPTY(&scheme)) {
		if (phalcon_array_isset_fetch_str(&host, &parts, SL("host")) && PHALCON_IS_NOT_EMPTY(&host)) {
			if (phalcon_array_isset_fetch_str(&user, &parts, SL("user")) && PHALCON_IS_NOT_EMPTY(&user)) {
				if (phalcon_array_isset_fetch_str(&pass, &parts, SL("pass")) && PHALCON_IS_NOT_EMPTY(&pass)) {
					PHALCON_CONCAT_VSVSVSV(&uri, &scheme, "://", &user, ":", &pass, "@", &host);
				} else {
					PHALCON_CONCAT_VSVSV(&uri, &scheme, "://", &user, "@", &host);
				}
			} else {
				PHALCON_CONCAT_VSV(&uri, &scheme, "://", &host);
			}
		} else {
			PHALCON_CONCAT_VS(&uri, &scheme, ":");
		}
	}

	if (phalcon_array_isset_fetch_str(&port, &parts, SL("port")) && PHALCON_IS_NOT_EMPTY(&port)) {
		PHALCON_SCONCAT_SV(&uri, ":", &port);
	}

	if (phalcon_array_isset_fetch_str(&path, &parts, SL("path")) && PHALCON_IS_NOT_EMPTY(&path)) {
		if (!phalcon_start_with_str(&path, SL("/"))) {
			PHALCON_SCONCAT_SV(&uri, "/", &path);
		} else {
			PHALCON_SCONCAT(&uri, &path);
		}
	}

	if (phalcon_array_isset_fetch_str(&query, &parts, SL("query")) && PHALCON_IS_NOT_EMPTY(&query)) {
		phalcon_http_build_query(&tmp, &query, "&");
		PHALCON_SCONCAT_SV(&uri, "?", &tmp);
	}

	if (phalcon_array_isset_fetch_str(&fragment, &parts, SL("fragment")) && PHALCON_IS_NOT_EMPTY(&fragment)) {
		PHALCON_SCONCAT_SV(&uri, "#", &fragment);
	}

	RETURN_CTORW(&uri);
}
Esempio n. 19
0
/**
 * Phalcon\Paginator\Adapter\Sql
 *
 * @param array $config
 */
PHP_METHOD(Phalcon_Paginator_Adapter_Sql, __construct){

	zval *config, db = {}, sql = {}, total_sql = {}, bind = {}, limit = {}, page = {};
	long int i_limit;

	phalcon_fetch_params(0, 1, 0, &config);
	
	if (!phalcon_array_isset_fetch_str(&db, config, SL("db"))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_paginator_exception_ce, "Parameter 'db' is required");
		return;
	}
	
	if (!phalcon_array_isset_fetch_str(&sql, config, SL("sql"))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_paginator_exception_ce, "Parameter 'sql' is required");
		return;
	}
	
	if (!phalcon_array_isset_fetch_str(&total_sql, config, SL("total_sql"))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_paginator_exception_ce, "Parameter 'sql' is required");
		return;
	}

	if (phalcon_array_isset_fetch_str(&bind, config, SL("bind"))) {
		if (Z_TYPE_P(&bind) != IS_ARRAY) {
			phalcon_update_property_empty_array(getThis(), SL("_bind"));
		} else {
			phalcon_update_property_this(getThis(), SL("_bind"), &bind);
		}
	} else {
		phalcon_update_property_empty_array(getThis(), SL("_bind"));
	}

	PHALCON_VERIFY_INTERFACE_EX(&db, phalcon_db_adapterinterface_ce, phalcon_paginator_exception_ce, 0);

	phalcon_update_property_this(getThis(), SL("_db"), &db);
	phalcon_update_property_this(getThis(), SL("_sql"), &sql);
	phalcon_update_property_this(getThis(), SL("_total_sql"), &total_sql);

	if (!phalcon_array_isset_fetch_str(&limit, config, SL("limit"))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_paginator_exception_ce, "Parameter 'limit' is required");
		return;
	}

	i_limit = phalcon_get_intval(&limit);
	if (i_limit < 1) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_paginator_exception_ce, "'limit' should be positive");
		return;
	}

	phalcon_update_property_this(getThis(), SL("_limitRows"), &limit);
	
	if (phalcon_array_isset_fetch_str(&page, config, SL("page"))) {
		phalcon_update_property_this(getThis(), SL("_page"), &page);
	}
}
Esempio n. 20
0
/**
 * Checks whether request has been made using SOAP
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Http_Request, isSoapRequested)
{
	zval *server, content_type = {};

	server = phalcon_get_global_str(SL("_SERVER"));
	if (phalcon_array_isset_str(server, SL("HTTP_SOAPACTION"))) {
		RETURN_TRUE;
	}

	if (phalcon_array_isset_fetch_str(&content_type, server, SL("CONTENT_TYPE"))) {
		if (phalcon_memnstr_str(&content_type, SL("application/soap+xml"))) {
			RETURN_TRUE;
		}
	}

	RETURN_FALSE;
}
Esempio n. 21
0
/**
 * Checks if GD is enabled
 *
 * @return  boolean
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, check){

	zval gd_version = {}, ret = {}, gd_info = {}, version = {}, exception_message = {}, pattern = {}, matches = {};

	if (phalcon_function_exists_ex(SL("gd_info")) == FAILURE) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "GD is either not installed or not enabled, check your configuration");
		return;
	}

	if (!phalcon_get_constant(&gd_version, SL("GD_VERSION"))) {
		PHALCON_CALL_FUNCTIONW(&gd_info, "gd_info");

		if (phalcon_array_isset_fetch_str(&gd_version, &gd_info, SL("GD Version"))) {
			ZVAL_STRING(&pattern, "#\\d+\\.\\d+(?:\\.\\d+)?#");
			ZVAL_NULL(&matches);
			ZVAL_MAKE_REF(&matches);
			RETURN_ON_FAILURE(phalcon_preg_match(&ret, &pattern, &gd_version, &matches));
			ZVAL_UNREF(&matches);

			if (zend_is_true(&ret)) {
				if (!phalcon_array_isset_fetch_long(&version, &matches, 0)) {
					ZVAL_EMPTY_STRING(&version);
				}
			} else {
				ZVAL_EMPTY_STRING(&version);
			}
		} else {
			PHALCON_CPY_WRT_CTOR(&version, &gd_version);
		}
	}

	if (-1 == php_version_compare(Z_STRVAL_P(&gd_version), "2.0.1")) {
		PHALCON_CONCAT_SV(&exception_message, "Phalcon\\Image\\Adapter\\GD requires GD version '2.0.1' or greater, you have '", &gd_version);
		PHALCON_THROW_EXCEPTION_ZVALW(phalcon_image_exception_ce, &exception_message);
		return;
	}

	phalcon_update_static_property_ce(phalcon_image_adapter_gd_ce, SL("_checked"), &PHALCON_GLOBAL(z_true));

	RETURN_TRUE;
}
Esempio n. 22
0
/**
 * Returns a parameter in a specific position
 *
 * @param int $position
 * @return array
 */
PHP_METHOD(Phalcon_DI_Service, getParameter){

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

	phalcon_fetch_params(0, 1, 0, &position);
	PHALCON_ENSURE_IS_LONG(position);

	phalcon_return_property(&definition, getThis(), SL("_definition"));
	if (Z_TYPE(definition) != IS_ARRAY) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_di_exception_ce, "Definition must be an array to obtain its parameters");
		return;
	}

	/* Update the parameter */
	if (
		!phalcon_array_isset_fetch_str(&arguments, &definition, SL("arguments")) ||
		!phalcon_array_isset_fetch(return_value, &arguments, position, 0)
	) {
		RETURN_NULL();
	}
}
Esempio n. 23
0
/**
 * Stores cached content into the XCache backend and stops the frontend
 *
 * @param string $keyName
 * @param string $content
 * @param long $lifetime
 * @param boolean $stopBuffer
 */
PHP_METHOD(Phalcon_Cache_Backend_Xcache, save){

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

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

	if (!key_name || Z_TYPE_P(key_name) == IS_NULL) {
		phalcon_return_property(&last_key, getThis(), SL("_lastKey"));
	} else {
		phalcon_return_property(&prefix, getThis(), SL("_prefix"));
		PHALCON_CONCAT_SVV(&last_key, "_PHCX", &prefix, key_name);
	}

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

	phalcon_return_property(&frontend, getThis(), SL("_frontend"));
	if (!content || Z_TYPE_P(content) == IS_NULL) {
		PHALCON_CALL_METHODW(&cached_content, &frontend, "getcontent");
	} else {
		PHALCON_CPY_WRT(&cached_content, content);
	}

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

	/** 
	 * Take the lifetime from the frontend or read it from the set in start()
	 */
	if (!lifetime || Z_TYPE_P(lifetime) == IS_NULL) {
		phalcon_return_property(&ttl, getThis(), SL("_lastLifetime"));

		if (Z_TYPE(ttl) <= IS_NULL) {
			PHALCON_CALL_METHODW(&ttl, &frontend, "getlifetime");
		}
	} else {
		PHALCON_CPY_WRT(&ttl, lifetime);
	}

	if (Z_TYPE(prepared_content) > IS_NULL) {
		PHALCON_CALL_FUNCTIONW(&success, "xcache_set", &last_key, &prepared_content, &ttl);
	} else {
		PHALCON_CALL_FUNCTIONW(&success, "xcache_set", &last_key, &cached_content, &ttl);
	}

	PHALCON_CALL_METHODW(&is_buffering, &frontend, "isbuffering");
	if (!stop_buffer || PHALCON_IS_TRUE(stop_buffer)) {
		PHALCON_CALL_METHODW(NULL, &frontend, "stop");
	}

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

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

	if (zend_is_true(&success)) {
		phalcon_return_property(&options, getThis(), SL("_options"));

		if (unlikely(!phalcon_array_isset_fetch_str(&special_key, &options, SL("statsKey")))) {
			PHALCON_THROW_EXCEPTION_STRW(phalcon_cache_exception_ce, "Unexpected inconsistency in options");
			return;
		}

		/** 
		 * xcache_list() is available only to the administrator (unless XCache was
		 * patched). We have to update the list of the stored keys.
		 */
		PHALCON_CALL_FUNCTIONW(&keys, "xcache_get", &special_key);
		if (Z_TYPE(keys) != IS_ARRAY) {
			array_init(&keys);
		}

		phalcon_array_update_zval(&keys, &last_key, &ttl, PH_COPY);
		PHALCON_CALL_FUNCTIONW(NULL, "xcache_set", &special_key, &keys, &PHALCON_GLOBAL(z_zero));
	}
}
Esempio n. 24
0
/**
 * Handles routing information received from command-line arguments
 *
 * @param array $arguments
 */
PHP_METHOD(Phalcon_CLI_Router, handle){

	zval *arguments = NULL, longopts = {}, options = {}, module_name = {}, namespace_name = {}, task_name = {}, action_name = {};

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

	if (!arguments || Z_TYPE_P(arguments) != IS_ARRAY) {
		if (unlikely(!strcmp(sapi_module.name, "cli"))) {
			array_init(&longopts);
			phalcon_array_append_string(&longopts, SL("module::"), 0);
			phalcon_array_append_string(&longopts, SL("namespace::"), 0);
			phalcon_array_append_string(&longopts, SL("task::"), 0);
			phalcon_array_append_string(&longopts, SL("action::"), 0);
			phalcon_array_append_string(&longopts, SL("params::"), 0);
			PHALCON_CALL_FUNCTIONW(&options, "getopt", &PHALCON_GLOBAL(z_null), &longopts);
		} else {
			array_init(&options);
		}
	} else {
		PHALCON_CPY_WRT_CTOR(&options, arguments);
	}

	/**
	 * Check for a module
	 */
	if (phalcon_array_isset_fetch_str(&module_name, &options, SL("module"))) {
		phalcon_array_unset_str(arguments, SL("module"), PH_COPY);
	} else {
		ZVAL_NULL(&module_name);
	}
	phalcon_update_property_zval(getThis(), SL("_module"), &module_name);

	/**
	 * Check for a namespace
	 */
	if (phalcon_array_isset_fetch_str(&namespace_name, &options, SL("namespace"))) {
		phalcon_array_unset_str(arguments, SL("namespace"), PH_COPY);
	} else {
		ZVAL_NULL(&namespace_name);
	}
	phalcon_update_property_zval(getThis(), SL("_namespace"), &namespace_name);

	/**
	 * Check for a task
	 */
	if (phalcon_array_isset_fetch_str(&task_name, &options, SL("task"))) {
		phalcon_array_unset_str(arguments, SL("task"), PH_COPY);
	} else {
		ZVAL_NULL(&task_name);
	}
	phalcon_update_property_zval(getThis(), SL("_task"), &task_name);

	/**
	 * Check for an action
	 */
	if (phalcon_array_isset_fetch_str(&action_name, &options, SL("action"))) {
		phalcon_array_unset_str(arguments, SL("action"), PH_COPY);
	} else {
		ZVAL_NULL(&action_name);
	}
	phalcon_update_property_zval(getThis(), SL("_action"), &action_name);

	phalcon_update_property_zval(getThis(), SL("_params"), &options);
}
Esempio n. 25
0
/**
 * Constructor for Phalcon\Session\Adapter\Memcache
 *
 * @param array $options
 */
PHP_METHOD(Phalcon_Session_Adapter_Memcache, __construct){

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

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

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

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

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

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

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

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

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

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

	object_init_ex(&frontend_data, phalcon_cache_frontend_data_ce);

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

	array_init_size(&backend_option, 3);

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

	object_init_ex(&memcache, phalcon_cache_backend_memcache_ce);

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

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

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

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

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

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

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

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

	PHALCON_CALL_FUNCTIONW(NULL, "session_set_save_handler", &callable_open, &callable_close, &callable_read, &callable_write, &callable_destroy, &callable_gc);
	PHALCON_CALL_PARENTW(NULL, phalcon_session_adapter_memcache_ce, getThis(), "__construct", options);
}
Esempio n. 26
0
/**
 * Phalcon\Image\GD constructor
 *
 * @param string $file
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, __construct){

	zval *file, *width = NULL, *height = NULL, exception_message = {}, checked = {}, realpath = {}, img_width = {}, img_height = {}, type = {}, mime = {}, format = {}, image = {}, imageinfo = {}, saveflag = {}, blendmode = {};

	phalcon_fetch_params(0, 1, 2, &file, &width, &height);

	if (Z_TYPE_P(file) != IS_STRING) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "file parameter should be a string");
		return;
	}

	phalcon_return_static_property_ce(&checked, phalcon_image_adapter_gd_ce, SL("_checked"));

	if (!zend_is_true(&checked)) {
		PHALCON_CALL_CE_STATICW(NULL, phalcon_image_adapter_gd_ce, "check");
	}

	phalcon_update_property_zval(getThis(), SL("_file"), file);

	if (phalcon_file_exists(file) != FAILURE) {
		phalcon_file_realpath(&realpath, file);
		if (unlikely(Z_TYPE(realpath) != IS_STRING)) {
			convert_to_string(&realpath);
		}

		phalcon_update_property_zval(getThis(), SL("_realpath"), &realpath);

		PHALCON_CALL_FUNCTIONW(&imageinfo, "getimagesize", &realpath);

		if (phalcon_array_isset_fetch_long(&img_width, &imageinfo, 0)) {
			phalcon_update_property_zval(getThis(), SL("_width"), &img_width);
		}

		if (phalcon_array_isset_fetch_long(&img_height, &imageinfo, 1)) {
			phalcon_update_property_zval(getThis(), SL("_height"), &img_height);
		}

		if (phalcon_array_isset_fetch_long(&type, &imageinfo, 2)) {
			convert_to_long(&type);
			phalcon_update_property_zval(getThis(), SL("_type"), &type);
		} else {
			ZVAL_LONG(&type, -1);
		}

		if (phalcon_array_isset_fetch_str(&mime, &imageinfo, SL("mime"))) {
			convert_to_string(&mime);
			phalcon_update_property_zval(getThis(), SL("_mime"), &mime);
		}

		assert(Z_TYPE(type) == IS_LONG);

		switch (Z_LVAL(type)) {
			case 1: // GIF
				ZVAL_STRING(&format, "gif");
				PHALCON_CALL_FUNCTIONW(&image, "imagecreatefromgif", &realpath);
				break;

			case 2: // JPEG
				ZVAL_STRING(&format, "jpg");
				PHALCON_CALL_FUNCTIONW(&image, "imagecreatefromjpeg", &realpath);
				break;

			case 3: // PNG
				ZVAL_STRING(&format, "png");
				PHALCON_CALL_FUNCTIONW(&image, "imagecreatefrompng", &realpath);
				break;

			default:
				if (PHALCON_IS_NOT_EMPTY(&mime)) {
					zend_throw_exception_ex(phalcon_image_exception_ce, 0, "Installed GD does not support '%s' images", Z_STRVAL(mime));
				} else {
					zend_throw_exception_ex(phalcon_image_exception_ce, 0, "Installed GD does not support such images");
				}
				return;
		}

		if (Z_TYPE(image) != IS_RESOURCE) {
			assert(Z_TYPE(realpath) == IS_STRING);
			zend_throw_exception_ex(phalcon_image_exception_ce, 0, "Failed to create image from file '%s'", Z_STRVAL(realpath));
			return;
		}

		phalcon_update_property_zval(getThis(), SL("_format"), &format);

		ZVAL_TRUE(&saveflag);

		PHALCON_CALL_FUNCTIONW(NULL, "imagesavealpha", &image, &saveflag);
	} else if (width && height) {
		PHALCON_CALL_FUNCTIONW(&image, "imagecreatetruecolor", width, height);

		if (Z_TYPE(image) != IS_RESOURCE) {
			PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "imagecreatetruecolor() failed");
			return;
		}

		ZVAL_TRUE(&blendmode);
		ZVAL_TRUE(&saveflag);

		PHALCON_CALL_FUNCTIONW(NULL, "imagealphablending", &image, &blendmode);
		PHALCON_CALL_FUNCTIONW(NULL, "imagesavealpha", &image, &saveflag);

		phalcon_update_property_zval(getThis(), SL("_realpath"), file);
		phalcon_update_property_zval(getThis(), SL("_width"), width);
		phalcon_update_property_zval(getThis(), SL("_height"), height);

		ZVAL_LONG(&type, 3);

		phalcon_update_property_zval(getThis(), SL("_type"), &type);

		ZVAL_STRING(&format, "png");

		phalcon_update_property_zval(getThis(), SL("_format"), &format);

		ZVAL_STRING(&mime, "image/png");

		phalcon_update_property_zval(getThis(), SL("_mime"), &mime);
	} else {
		PHALCON_CONCAT_SVS(&exception_message, "Failed to create image from file '", file, "'");
		PHALCON_THROW_EXCEPTION_ZVALW(phalcon_image_exception_ce, &exception_message);
		return;
	}

	phalcon_update_property_zval(getThis(), SL("_image"), &image);
}
Esempio n. 27
0
/**
 * Handle the command-line arguments.
 *  
 * 
 * <code>
 * 	$arguments = array(
 * 		'task' => 'taskname',
 * 		'action' => 'action',
 * 		'params' => array('parameter1', 'parameter2')
 * 	);
 * 	$console->handle($arguments);
 * </code>
 *
 * @param array $arguments
 * @return mixed
 */
PHP_METHOD(Phalcon_CLI_Console, handle){

	zval *_arguments = NULL, arguments = {}, dependency_injector = {}, events_manager = {}, event_name = {}, service = {}, router = {}, module_name = {};
	zval status = {}, modules = {}, exception_msg = {}, module = {}, path = {}, class_name = {}, module_object = {};
	zval namespace_name = {}, task_name = {}, action_name = {}, params = {}, dispatcher = {};

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

	if (!_arguments) {
		array_init(&arguments);
	} else {
		PHALCON_CPY_WRT(&arguments, _arguments);
	}

	phalcon_return_property(&events_manager, getThis(), SL("_eventsManager"));

	ZVAL_STRING(&service, ISV(router));

	PHALCON_CALL_METHODW(&dependency_injector, getThis(), "getdi");
	if (Z_TYPE(dependency_injector) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_FORMATW(phalcon_cli_console_exception_ce, "A dependency injection container is required to access the '%s' service", Z_STRVAL(service));
		return;
	}

	PHALCON_CALL_METHODW(&router, &dependency_injector, "getshared", &service);
	PHALCON_VERIFY_CLASSW(&router, phalcon_cli_router_ce);

	PHALCON_CALL_METHODW(NULL, &router, "handle", &arguments);
	PHALCON_CALL_METHODW(&module_name, &router, "getmodulename");
	if (zend_is_true(&module_name)) {
		if (Z_TYPE(events_manager) == IS_OBJECT) {
			ZVAL_STRING(&event_name, "console:beforeStartModule");

			PHALCON_CALL_METHODW(&status, &events_manager, "fire", &event_name, getThis(), &module_name);
			if (PHALCON_IS_FALSE(&status)) {
				RETURN_FALSE;
			}
		}

		phalcon_read_property(&modules, getThis(), SL("_modules"), PH_NOISY);
		if (!phalcon_array_isset_fetch(&module, &modules, &module_name, 0)) {
			PHALCON_CONCAT_SVS(&exception_msg, "Module '", &module_name, "' isn't registered in the console container");
			PHALCON_THROW_EXCEPTION_ZVALW(phalcon_cli_console_exception_ce, &exception_msg);
			return;
		}

		if (Z_TYPE_P(&module) != IS_ARRAY) { 
			PHALCON_THROW_EXCEPTION_STRW(phalcon_cli_console_exception_ce, "Invalid module definition path");
			return;
		}

		if (phalcon_array_isset_fetch_str(&path, &module, SL("path"))) {
			convert_to_string_ex(&path);

			if (phalcon_file_exists(&path) == SUCCESS) {
				RETURN_ON_FAILURE(phalcon_require(Z_STRVAL(path)));
			} else {
				zend_throw_exception_ex(phalcon_cli_console_exception_ce, 0, "Modules definition path '%s' does not exist", Z_STRVAL(path));
				return;
			}
		}

		if (!phalcon_array_isset_fetch_str(&class_name, &module, SL("className"))) {
			ZVAL_STRING(&class_name, "Module");
		}

		PHALCON_CALL_METHODW(&module_object, &dependency_injector, "getshared", &class_name);
		PHALCON_CALL_METHODW(NULL, &module_object, "registerautoloaders");
		PHALCON_CALL_METHODW(NULL, &module_object, "registerservices", &dependency_injector);
		if (Z_TYPE(events_manager) == IS_OBJECT) {
			phalcon_update_property_zval(getThis(), SL("_moduleObject"), &module_object);

			ZVAL_STRING(&event_name, "console:afterStartModule");

			PHALCON_CALL_METHODW(&status, &events_manager, "fire", &event_name, getThis(), &module_name);
			if (PHALCON_IS_FALSE(&status)) {
				RETURN_FALSE;
			}
		}
	}

	PHALCON_CALL_METHODW(&namespace_name, &router, "getnamespacename");
	PHALCON_CALL_METHODW(&task_name, &router, "gettaskname");
	PHALCON_CALL_METHODW(&action_name, &router, "getactionname");
	PHALCON_CALL_METHODW(&params, &router, "getparams");

	ZVAL_STRING(&service, ISV(dispatcher));

	PHALCON_CALL_METHODW(&dispatcher, &dependency_injector, "getshared", &service);
	PHALCON_VERIFY_INTERFACEW(&dispatcher, phalcon_dispatcherinterface_ce);

	PHALCON_CALL_METHODW(NULL, &dispatcher, "setnamespacename", &namespace_name);
	PHALCON_CALL_METHODW(NULL, &dispatcher, "settaskname", &task_name);
	PHALCON_CALL_METHODW(NULL, &dispatcher, "setactionname", &action_name);
	PHALCON_CALL_METHODW(NULL, &dispatcher, "setparams", &params);
	if (Z_TYPE(events_manager) == IS_OBJECT) {
		ZVAL_STRING(&event_name, "console:beforeHandleTask");

		PHALCON_CALL_METHODW(&status, &events_manager, "fire", &event_name, getThis(), &dispatcher);
		if (PHALCON_IS_FALSE(&status)) {
			RETURN_FALSE;
		}
	}

	PHALCON_CALL_METHODW(&status, &dispatcher, "dispatch");

	if (Z_TYPE(events_manager) == IS_OBJECT) {
		ZVAL_STRING(&event_name, "console:afterHandleTask");
		PHALCON_CALL_METHODW(NULL, &events_manager, "fire", &event_name, getThis(), &status);
	}

	RETURN_CTORW(&status);
}
Esempio n. 28
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, last_key = {}, prefix = {}, cached_content = {}, prepared_content = {}, ttl = {}, flags = {}, success = {};
	zval keys = {}, is_buffering = {}, frontend = {}, memcache = {}, options = {}, special_key = {};

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

	if (!key_name || Z_TYPE_P(key_name) == IS_NULL) {
		phalcon_return_property(&last_key, getThis(), SL("_lastKey"));
	} else {
		phalcon_return_property(&prefix, getThis(), SL("_prefix"));
		PHALCON_CONCAT_VV(&last_key, &prefix, key_name);
	}

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

	phalcon_return_property(&frontend, getThis(), SL("_frontend"));

	/** 
	 * Check if a connection is created or make a new one
	 */
	phalcon_return_property(&memcache, getThis(), SL("_memcache"));
	if (Z_TYPE(memcache) != IS_OBJECT) {
		PHALCON_CALL_METHODW(&memcache, getThis(), "_connect");
	}

	if (!content || Z_TYPE_P(content) == IS_NULL) {
		PHALCON_CALL_METHODW(&cached_content, &frontend, "getcontent");
	} else {
		PHALCON_CPY_WRT(&cached_content, content);
	}

	/** 
	 * Prepare the content in the frontend
	 */
	PHALCON_CALL_METHODW(&prepared_content, &frontend, "beforestore", &cached_content);

	if (!lifetime || Z_TYPE_P(lifetime) == IS_NULL) {
		phalcon_return_property(&ttl, getThis(), SL("_lastLifetime"));

		if (Z_TYPE(ttl) == IS_NULL) {
			PHALCON_CALL_METHODW(&ttl, &frontend, "getlifetime");
		}
	} else {
		PHALCON_CPY_WRT(&ttl, lifetime);
	}

	ZVAL_LONG(&flags, 0);

	/** 
	 * We store without flags
	 */
	if (phalcon_is_numeric(&cached_content)) {
		PHALCON_CALL_METHODW(&success, &memcache, "set", &last_key, &cached_content, &flags, &ttl);
	} else {
		PHALCON_CALL_METHODW(&success, &memcache, "set", &last_key, &prepared_content, &flags, &ttl);
	}

	if (!zend_is_true(&success)) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_cache_exception_ce, "Failed to store data in memcached");
		return;
	}

	phalcon_return_property(&options, getThis(), SL("_options"));

	if (unlikely(!phalcon_array_isset_fetch_str(&special_key, &options, SL("statsKey")))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_cache_exception_ce, "Unexpected inconsistency in options");
		return;
	}

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

		if (!phalcon_array_isset(&keys, &last_key)) {
			phalcon_array_update_zval(&keys, &last_key, &ttl, PH_COPY);
			PHALCON_CALL_METHODW(NULL, &memcache, "set", &special_key, &keys);
		}
	}

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

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

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

	phalcon_update_property_bool(getThis(), SL("_started"), 0);
}
Esempio n. 29
0
/**
 * Composite one image onto another

 *
 * @param Phalcon\Image\Adapter $mask  mask Image instance
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, _mask){

	zval *mask, image = {}, mask_image = {}, blob = {}, mask_image_width = {}, mask_image_height = {}, newimage = {}, image_width = {}, image_height = {}, saveflag = {}, color = {}, c = {}, alpha = {}, temp_image = {};
	int x, y, w, h, i;

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

	phalcon_return_property(&image, getThis(), SL("_image"));

	PHALCON_CALL_METHODW(&blob, mask, "render");
	PHALCON_CALL_FUNCTIONW(&mask_image, "imagecreatefromstring", &blob);

	ZVAL_TRUE(&saveflag);

	PHALCON_CALL_FUNCTIONW(NULL, "imagesavealpha", &mask_image, &saveflag);

	PHALCON_CALL_FUNCTIONW(&mask_image_width, "imagesx", &mask_image);
	PHALCON_CALL_FUNCTIONW(&mask_image_height, "imagesy", &mask_image);

	phalcon_return_property(&image_width, getThis(), SL("_width"));
	phalcon_return_property(&image_height, getThis(), SL("_height"));

	PHALCON_CALL_METHODW(&newimage, getThis(), "_create", &image_width, &image_height);

	PHALCON_CALL_FUNCTIONW(NULL, "imagesavealpha", &newimage, &saveflag);

	ZVAL_LONG(&c, 0);
	ZVAL_LONG(&alpha, 127);

	PHALCON_CALL_FUNCTIONW(&color, "imagecolorallocatealpha", &newimage, &c, &c, &c, &alpha);
	PHALCON_CALL_FUNCTIONW(NULL, "imagefill", &newimage, &c, &c, &color);

	if(!PHALCON_IS_EQUAL(&image_width, &mask_image_width) || !PHALCON_IS_EQUAL(&image_height, &mask_image_height)) {
		PHALCON_CALL_FUNCTIONW(&temp_image, "imagecreatetruecolor", &image_width, &image_height);
		PHALCON_CALL_FUNCTIONW(NULL, "imagecopyresampled", &temp_image, &mask_image, &c, &c, &c, &c, &image_width, &image_height, &mask_image_width, &mask_image_height);
		PHALCON_CALL_FUNCTIONW(NULL, "imagedestroy", &mask_image);

		PHALCON_CPY_WRT_CTOR(&mask_image, &temp_image);
	}

	w = phalcon_get_intval(&image_width);
	h = phalcon_get_intval(&image_height);

	for (x=0; x < w; x++) {
		zval zx = {};
		ZVAL_LONG(&zx, x);
		for (y=0; y < h; y++) {
			zval zy = {}, index = {}, red = {}, index2 = {}, color = {}, r = {}, g = {}, b = {};
			ZVAL_LONG(&zy, y);

			PHALCON_CALL_FUNCTIONW(&index, "imagecolorat", &mask_image, &zx, &zy);
			PHALCON_CALL_FUNCTIONW(&alpha, "imagecolorsforindex", &mask_image, &index);

			if (phalcon_array_isset_fetch_str(&red, &alpha, SL("red"))) {
				i = (int)(127 - (phalcon_get_intval(&red) / 2));
				ZVAL_LONG(&alpha, i);
			}

			PHALCON_CALL_FUNCTIONW(&index2, "imagecolorat", &image, &zx, &zy);
			PHALCON_CALL_FUNCTIONW(&color, "imagecolorsforindex", &image, &index2);

			phalcon_array_isset_fetch_str(&r, &color, SL("red"));
			phalcon_array_isset_fetch_str(&g, &color, SL("green"));
			phalcon_array_isset_fetch_str(&b, &color, SL("blue"));

			PHALCON_CALL_FUNCTIONW(&color, "imagecolorallocatealpha", &newimage, &r, &g, &b, &alpha);
			PHALCON_CALL_FUNCTIONW(NULL, "imagesetpixel", &newimage, &zx, &zy, &color);
		}
	}

	PHALCON_CALL_FUNCTIONW(NULL, "imagedestroy", &image);
	PHALCON_CALL_FUNCTIONW(NULL, "imagedestroy", &mask_image);

	phalcon_update_property_zval(getThis(), SL("_image"), &newimage);
}