예제 #1
0
/**
 * Returns the value of the CSRF token in session
 *
 * @return string
 */
PHP_METHOD(Phalcon_Security, getSessionToken){

	zval *dependency_injector, *service, *session;
	zval *key, *session_token;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(dependency_injector);
	phalcon_read_property_this(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC);
	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_flash_exception_ce, "A dependency injection container is required to access the 'session' service");
		return;
	}
	
	PHALCON_INIT_VAR(service);
	ZVAL_STRING(service, "session", 1);
	
	PHALCON_INIT_VAR(session);
	phalcon_call_method_p1(session, dependency_injector, "getshared", service);
	PHALCON_VERIFY_INTERFACE(session, phalcon_session_adapterinterface_ce);
	
	PHALCON_INIT_VAR(key);
	ZVAL_STRING(key, "$PHALCON/CSRF$", 1);
	
	PHALCON_INIT_VAR(session_token);
	phalcon_call_method_p1(session_token, session, "get", key);
	
	RETURN_CCTOR(session_token);
}
예제 #2
0
/**
 * Returns the messages stored in session
 *
 * @param boolean $remove
 * @return array
 */
PHP_METHOD(Phalcon_Flash_Session, _getSessionMessages){

	zval *remove, *dependency_injector, *service;
	zval *session, *index_name, *messages;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &remove);
	
	PHALCON_OBS_VAR(dependency_injector);
	phalcon_read_property_this(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC);
	if (unlikely(Z_TYPE_P(dependency_injector) != IS_OBJECT)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_flash_exception_ce, "A dependency injection container is required to access the 'session' service");
		return;
	}
	
	PHALCON_INIT_VAR(service);
	ZVAL_STRING(service, "session", 1);
	
	PHALCON_INIT_VAR(session);
	phalcon_call_method_p1(session, dependency_injector, "getshared", service);
	PHALCON_VERIFY_INTERFACE(session, phalcon_session_adapterinterface_ce);
	
	PHALCON_INIT_VAR(index_name);
	ZVAL_STRING(index_name, "_flashMessages", 1);
	
	PHALCON_INIT_VAR(messages);
	phalcon_call_method_p1(messages, session, "get", index_name);
	if (PHALCON_IS_TRUE(remove)) {
		phalcon_call_method_p1_noret(session, "remove", index_name);
	}
	
	RETURN_CCTOR(messages);
}
예제 #3
0
/**
 * Check if a model has certain attribute
 *
 *<code>
 *	var_dump($metaData->hasAttribute(new Robots(), 'name'));
 *</code>
 *
 * @param Phalcon\Mvc\ModelInterface $model
 * @param string $attribute
 * @return boolean
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData, hasAttribute) {

    zval *model, *attribute, *column_map, *meta_data;
    zval *data_types;

    PHALCON_MM_GROW();

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

    if (Z_TYPE_P(attribute) != IS_STRING) {
        PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Attribute must be a string");
        return;
    }

    PHALCON_INIT_VAR(column_map);
    phalcon_call_method_p1(column_map, this_ptr, "getreversecolumnmap", model);
    if (Z_TYPE_P(column_map) == IS_ARRAY) {
        if (phalcon_array_isset(column_map, attribute)) {
            RETURN_MM_TRUE;
        }
    } else {
        PHALCON_INIT_VAR(meta_data);
        phalcon_call_method_p1(meta_data, this_ptr, "readmetadata", model);

        PHALCON_OBS_VAR(data_types);
        phalcon_array_fetch_long(&data_types, meta_data, 4, PH_NOISY);
        if (phalcon_array_isset(data_types, attribute)) {
            RETURN_MM_TRUE;
        }
    }

    RETURN_MM_FALSE;
}
예제 #4
0
/**
 * Executes the validation
 *
 * @param Phalcon\Validation $validator
 * @param string $attribute
 * @return boolean
 */
PHP_METHOD(Phalcon_Validation_Validator_ExclusionIn, validate){

	zval *validator, *attribute, *value, *option = NULL, *domain;
	zval *message_str = NULL, *joined_domain, *type, *message;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &validator, &attribute);
	
	PHALCON_INIT_VAR(value);
	phalcon_call_method_p1(value, validator, "getvalue", attribute);
	
	/** 
	 * A domain is an array with a list of valid values
	 */
	PHALCON_INIT_VAR(option);
	ZVAL_STRING(option, "domain", 1);
	
	PHALCON_INIT_VAR(domain);
	phalcon_call_method_p1(domain, this_ptr, "getoption", option);
	if (Z_TYPE_P(domain) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "Option 'domain' must be an array");
		return;
	}
	
	/** 
	 * Check if the value is contained by the array
	 */
	if (phalcon_fast_in_array(value, domain TSRMLS_CC)) {
	
		PHALCON_INIT_NVAR(option);
		ZVAL_STRING(option, "message", 1);
	
		PHALCON_INIT_VAR(message_str);
		phalcon_call_method_p1(message_str, this_ptr, "getoption", option);
		if (!zend_is_true(message_str)) {
			PHALCON_INIT_VAR(joined_domain);
			phalcon_fast_join_str(joined_domain, SL(", "), domain TSRMLS_CC);
	
			PHALCON_INIT_NVAR(message_str);
			PHALCON_CONCAT_SVSV(message_str, "Value of field '", attribute, "' must not be part of list: ", joined_domain);
		}
	
		PHALCON_INIT_VAR(type);
		ZVAL_STRING(type, "ExclusionIn", 1);
	
		PHALCON_INIT_VAR(message);
		object_init_ex(message, phalcon_validation_message_ce);
		phalcon_call_method_p3_noret(message, "__construct", message_str, attribute, type);
	
		phalcon_call_method_p1_noret(validator, "appendmessage", message);
		RETURN_MM_FALSE;
	}
	
	RETURN_MM_TRUE;
}
예제 #5
0
/**
 * Redirect by HTTP to another action or URL
 *
 *<code>
 *  //Using a string redirect (internal/external)
 *	$response->redirect("posts/index");
 *	$response->redirect("http://en.wikipedia.org", true);
 *	$response->redirect("http://www.example.com/new-location", true, 301);
 *
 *	//Making a redirection based on a named route
 *	$response->redirect(array(
 *		"for" => "index-lang",
 *		"lang" => "jp",
 *		"controller" => "index"
 *	));
 *</code>
 *
 * @param string $location
 * @param boolean $externalRedirect
 * @param int $statusCode
 * @return Phalcon\Http\ResponseInterface
 */
PHP_METHOD(Phalcon_Http_Response, redirect){

	zval *location = NULL, *external_redirect = NULL, *status_code = NULL;
	zval *header = NULL, *dependency_injector, *service;
	zval *url, *status_text, *header_name;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 3, &location, &external_redirect, &status_code);
	
	if (!location) {
		PHALCON_INIT_VAR(location);
	}
	
	if (!external_redirect) {
		PHALCON_INIT_VAR(external_redirect);
		ZVAL_BOOL(external_redirect, 0);
	}
	
	if (!status_code) {
		PHALCON_INIT_VAR(status_code);
		ZVAL_LONG(status_code, 302);
	}
	
	if (zend_is_true(external_redirect)) {
		PHALCON_CPY_WRT(header, location);
	} else {
		PHALCON_INIT_VAR(dependency_injector);
		phalcon_call_method(dependency_injector, this_ptr, "getdi");
	
		PHALCON_INIT_VAR(service);
		ZVAL_STRING(service, "url", 1);
	
		PHALCON_INIT_VAR(url);
		phalcon_call_method_p1(url, dependency_injector, "getshared", service);
	
		PHALCON_INIT_NVAR(header);
		phalcon_call_method_p1(header, url, "get", location);
	}
	
	/** 
	 * The HTTP status is 302 by default, a temporary redirection
	 */
	PHALCON_INIT_VAR(status_text);
	ZVAL_STRING(status_text, "Redirect", 1);
	phalcon_call_method_p2_noret(this_ptr, "setstatuscode", status_code, status_text);
	
	/** 
	 * Change the current location using 'Location'
	 */
	PHALCON_INIT_VAR(header_name);
	ZVAL_STRING(header_name, "Location", 1);
	phalcon_call_method_p2_noret(this_ptr, "setheader", header_name, header);
	
	RETURN_THIS();
}
예제 #6
0
파일: between.c 프로젝트: 11mariom/cphalcon
/**
 * Executes the validation
 *
 * @param Phalcon\Validation $validator
 * @param string $attribute
 * @return boolean
 */
PHP_METHOD(Phalcon_Validation_Validator_Between, validate){

	zval *validator, *attribute, *value, *option = NULL, *minimum;
	zval *maximum, *valid = NULL, *type, *message_str = NULL, *message;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &validator, &attribute);
	
	PHALCON_INIT_VAR(value);
	phalcon_call_method_p1(value, validator, "getvalue", attribute);
	
	PHALCON_INIT_VAR(option);
	ZVAL_STRING(option, "minimum", 1);
	
	PHALCON_INIT_VAR(minimum);
	phalcon_call_method_p1(minimum, this_ptr, "getoption", option);
	
	PHALCON_INIT_NVAR(option);
	ZVAL_STRING(option, "maximum", 1);
	
	PHALCON_INIT_VAR(maximum);
	phalcon_call_method_p1(maximum, this_ptr, "getoption", option);
	
	PHALCON_INIT_VAR(valid);
	is_smaller_or_equal_function(valid, minimum, value TSRMLS_CC);
	if (zend_is_true(valid)) {
		is_smaller_or_equal_function(valid, value, maximum TSRMLS_CC);
	}
	
	if (PHALCON_IS_FALSE(valid)) {
	
		PHALCON_INIT_VAR(type);
		ZVAL_STRING(type, "Between", 1);
	
		PHALCON_INIT_NVAR(option);
		ZVAL_STRING(option, "message", 1);
	
		PHALCON_INIT_VAR(message_str);
		phalcon_call_method_p1(message_str, this_ptr, "getoption", option);
		if (!zend_is_true(message_str)) {
			PHALCON_INIT_NVAR(message_str);
			PHALCON_CONCAT_VS(message_str, attribute, " is not between a valid range");
		}
	
		PHALCON_INIT_VAR(message);
		object_init_ex(message, phalcon_validation_message_ce);
		phalcon_call_method_p3_noret(message, "__construct", message_str, attribute, type);
	
		phalcon_call_method_p1_noret(validator, "appendmessage", message);
		RETURN_MM_FALSE;
	}
	
	RETURN_MM_TRUE;
}
예제 #7
0
파일: url.c 프로젝트: 11mariom/cphalcon
/**
 * Executes the validator
 *
 * @param Phalcon\Mvc\ModelInterface $record
 * @return boolean
 */
PHP_METHOD(Phalcon_Mvc_Model_Validator_Url, validate){

	zval *record, *option = NULL, *field, *value, *flag, *is_valid;
	zval *message = NULL, *type;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &record);
	
	PHALCON_INIT_VAR(option);
	ZVAL_STRING(option, "field", 1);
	
	PHALCON_INIT_VAR(field);
	phalcon_call_method_p1(field, this_ptr, "getoption", option);
	if (Z_TYPE_P(field) != IS_STRING) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Field name must be a string");
		return;
	}
	
	PHALCON_INIT_VAR(value);
	phalcon_call_method_p1(value, record, "readattribute", field);
	
	PHALCON_INIT_VAR(flag);
	ZVAL_LONG(flag, 273);
	
	/** 
	 * Filters the format using FILTER_VALIDATE_URL
	 */
	PHALCON_INIT_VAR(is_valid);
	phalcon_call_func_p2(is_valid, "filter_var", value, flag);
	if (!zend_is_true(is_valid)) {
	
		/** 
		 * Check if the developer has defined a custom message
		 */
		PHALCON_INIT_NVAR(option);
		ZVAL_STRING(option, "message", 1);
	
		PHALCON_INIT_VAR(message);
		phalcon_call_method_p1(message, this_ptr, "getoption", option);
		if (!zend_is_true(message)) {
			PHALCON_INIT_NVAR(message);
			PHALCON_CONCAT_SVS(message, "'", field, "' does not have a valid url format");
		}
	
		PHALCON_INIT_VAR(type);
		ZVAL_STRING(type, "Url", 1);
		phalcon_call_method_p3_noret(this_ptr, "appendmessage", message, field, type);
		RETURN_MM_FALSE;
	}
	
	RETURN_MM_TRUE;
}
예제 #8
0
파일: bag.c 프로젝트: 11mariom/cphalcon
/**
 * Initializes the session bag. This method must not be called directly, the class calls it when its internal data is accesed
 */
PHP_METHOD(Phalcon_Session_Bag, initialize){

	zval *session = NULL, *dependency_injector = NULL, *service;
	zval *name, *data = NULL;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(session);
	phalcon_read_property_this(&session, this_ptr, SL("_session"), PH_NOISY_CC);
	if (Z_TYPE_P(session) != IS_OBJECT) {
	
		PHALCON_OBS_VAR(dependency_injector);
		phalcon_read_property_this(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC);
		if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
	
			PHALCON_INIT_NVAR(dependency_injector);
			PHALCON_CALL_STATIC(dependency_injector, "phalcon\\di", "getdefault");
	
			if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
				PHALCON_THROW_EXCEPTION_STR(phalcon_session_exception_ce, "A dependency injection object is required to access the 'session' service");
				return;
			}
		}
	
		PHALCON_INIT_VAR(service);
		ZVAL_STRING(service, "session", 1);
	
		PHALCON_INIT_NVAR(session);
		phalcon_call_method_p1(session, dependency_injector, "getshared", service);
		PHALCON_VERIFY_INTERFACE(session, phalcon_session_adapterinterface_ce);
		phalcon_update_property_this(this_ptr, SL("_session"), session TSRMLS_CC);
	}
	
	PHALCON_OBS_VAR(name);
	phalcon_read_property_this(&name, this_ptr, SL("_name"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(data);
	phalcon_call_method_p1(data, session, "get", name);
	if (Z_TYPE_P(data) != IS_ARRAY) { 
		PHALCON_INIT_NVAR(data);
		array_init(data);
	}
	
	phalcon_update_property_this(this_ptr, SL("_data"), data TSRMLS_CC);
	phalcon_update_property_bool(this_ptr, SL("_initalized"), 1 TSRMLS_CC);
	
	PHALCON_MM_RESTORE();
}
예제 #9
0
파일: xcache.c 프로젝트: RSivakov/cphalcon
/**
 * Returns cached content
 *
 * @param string $keyName
 * @param long $lifetime
 * @return mixed
 */
PHP_METHOD(Phalcon_Cache_Backend_Xcache, get){

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

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &key_name, &lifetime);
	
	if (!lifetime) {
		PHALCON_INIT_VAR(lifetime);
	}
	
	PHALCON_OBS_VAR(frontend);
	phalcon_read_property_this(&frontend, this_ptr, SL("_frontend"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(prefix);
	phalcon_read_property_this(&prefix, this_ptr, SL("_prefix"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(prefixed_key);
	PHALCON_CONCAT_SVV(prefixed_key, "_PHCX", prefix, key_name);
	phalcon_update_property_this(this_ptr, SL("_lastKey"), prefixed_key TSRMLS_CC);
	
	PHALCON_INIT_VAR(cached_content);
	phalcon_call_func_p1(cached_content, "xcache_get", prefixed_key);
	if (Z_TYPE_P(cached_content) == IS_NULL) {
		RETURN_MM_NULL();
	}
	
	PHALCON_INIT_VAR(processed);
	phalcon_call_method_p1(processed, frontend, "afterretrieve", cached_content);
	
	RETURN_CCTOR(processed);
}
예제 #10
0
/**
 * Adds a message to the session flasher
 *
 * @param string $type
 * @param string $message
 */
PHP_METHOD(Phalcon_Flash_Session, message){

	zval *type, *message, *remove, *messages = NULL, *empty_array;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &type, &message);
	
	PHALCON_INIT_VAR(remove);
	ZVAL_BOOL(remove, 0);
	
	PHALCON_INIT_VAR(messages);
	phalcon_call_method_p1(messages, this_ptr, "_getsessionmessages", remove);
	if (Z_TYPE_P(messages) != IS_ARRAY) { 
		PHALCON_INIT_NVAR(messages);
		array_init(messages);
	}
	
	if (!phalcon_array_isset(messages, type)) {
		PHALCON_INIT_VAR(empty_array);
		array_init(empty_array);
		phalcon_array_update_zval(&messages, type, &empty_array, PH_COPY | PH_SEPARATE);
	}
	
	phalcon_array_update_append_multi_2(&messages, type, message, 0);
	phalcon_call_method_p1_noret(this_ptr, "_setsessionmessages", messages);
	
	PHALCON_MM_RESTORE();
}
예제 #11
0
/**
 * Checks for annotations in the controller docblock
 *
 * @param string $handler
 * @param Phalcon\Annotations\AdapterInterface
 */
PHP_METHOD(Phalcon_Mvc_Router_Annotations, processControllerAnnotation){

	zval *handler, *annotation, *name, *position, *value;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &handler, &annotation);
	
	PHALCON_INIT_VAR(name);
	phalcon_call_method(name, annotation, "getname");
	
	/** 
	 * @RoutePrefix add a prefix for all the routes defined in the model
	 */
	if (PHALCON_IS_STRING(name, "RoutePrefix")) {
		PHALCON_INIT_VAR(position);
		ZVAL_LONG(position, 0);
	
		PHALCON_INIT_VAR(value);
		phalcon_call_method_p1(value, annotation, "getargument", position);
		phalcon_update_property_this(this_ptr, SL("_routePrefix"), value TSRMLS_CC);
	}
	
	PHALCON_MM_RESTORE();
}
예제 #12
0
파일: micro.c 프로젝트: RSivakov/cphalcon
/**
 * Sets the DependencyInjector container
 *
 * @param Phalcon\DiInterface $dependencyInjector
 */
PHP_METHOD(Phalcon_Mvc_Micro, setDI){

	zval *dependency_injector, *service, *exists;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &dependency_injector);
	
	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_application_exception_ce, "The dependency injector must be an object");
		return;
	}
	
	/** 
	 * We automatically set ourselves as application service
	 */
	PHALCON_INIT_VAR(service);
	ZVAL_STRING(service, "application", 1);
	
	PHALCON_INIT_VAR(exists);
	phalcon_call_method_p1(exists, dependency_injector, "has", service);
	if (!zend_is_true(exists)) {
		phalcon_call_method_p2_noret(dependency_injector, "set", service, this_ptr);
	}
	
	phalcon_update_property_this(this_ptr, SL("_dependencyInjector"), dependency_injector TSRMLS_CC);
	
	PHALCON_MM_RESTORE();
}
예제 #13
0
파일: micro.c 프로젝트: RSivakov/cphalcon
/**
 * Maps a route to a handler that only matches if the HTTP method is HEAD
 *
 * @param string $routePattern
 * @param callable $handler
 * @return Phalcon\Mvc\Router\RouteInterface
 */
PHP_METHOD(Phalcon_Mvc_Micro, head){

	zval *route_pattern, *handler, *router, *route;
	zval *route_id;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &route_pattern, &handler);
	
	/** 
	 * We create a router even if there is no one in the DI
	 */
	PHALCON_INIT_VAR(router);
	phalcon_call_method(router, this_ptr, "getrouter");
	
	/** 
	 * Routes are added to the router restricting to HEAD
	 */
	PHALCON_INIT_VAR(route);
	phalcon_call_method_p1(route, router, "addhead", route_pattern);
	
	/** 
	 * Using the id produced by the router we store the handler
	 */
	PHALCON_INIT_VAR(route_id);
	phalcon_call_method(route_id, route, "getrouteid");
	phalcon_update_property_array(this_ptr, SL("_handlers"), route_id, handler TSRMLS_CC);
	
	/** 
	 * The route is returned, the developer can add more things on it
	 */
	RETURN_CCTOR(route);
}
예제 #14
0
파일: line.c 프로젝트: golovanov/cphalcon
/**
 * Applies a format to a message before sent it to the internal log
 *
 * @param string $message
 * @param int $type
 * @param int $timestamp
 * @return string
 */
PHP_METHOD(Phalcon_Logger_Formatter_Line, format) {

    zval *message, *type, *timestamp, *format = NULL, *date_format;
    zval *date, *date_wildcard, *new_format = NULL, *type_string;
    zval *type_wildcard, *message_wildcard, *eol;

    PHALCON_MM_GROW();

    phalcon_fetch_params(1, 3, 0, &message, &type, &timestamp);

    PHALCON_OBS_VAR(format);
    phalcon_read_property_this(&format, this_ptr, SL("_format"), PH_NOISY_CC);

    /**
     * Check if the format has the %date% placeholder
     */
    if (phalcon_memnstr_str(format, SL("%date%"))) {
        PHALCON_OBS_VAR(date_format);
        phalcon_read_property_this(&date_format, this_ptr, SL("_dateFormat"), PH_NOISY_CC);

        PHALCON_INIT_VAR(date);
        phalcon_call_func_p2(date, "date", date_format, timestamp);

        PHALCON_INIT_VAR(date_wildcard);
        ZVAL_STRING(date_wildcard, "%date%", 1);

        PHALCON_INIT_VAR(new_format);
        phalcon_fast_str_replace(new_format, date_wildcard, date, format);
    } else {
        PHALCON_CPY_WRT(new_format, format);
    }

    /**
     * Check if the format has the %type% placeholder
     */
    if (phalcon_memnstr_str(format, SL("%type%"))) {
        PHALCON_INIT_VAR(type_string);
        phalcon_call_method_p1(type_string, this_ptr, "gettypestring", type);

        PHALCON_INIT_VAR(type_wildcard);
        ZVAL_STRING(type_wildcard, "%type%", 1);

        PHALCON_INIT_NVAR(format);
        phalcon_fast_str_replace(format, type_wildcard, type_string, new_format);
    } else {
        PHALCON_CPY_WRT(format, new_format);
    }

    PHALCON_INIT_VAR(message_wildcard);
    ZVAL_STRING(message_wildcard, "%message%", 1);

    PHALCON_INIT_NVAR(new_format);
    phalcon_fast_str_replace(new_format, message_wildcard, message, format);

    PHALCON_INIT_VAR(eol);
    ZVAL_STRING(eol, PHP_EOL, 1);
    PHALCON_CONCAT_VV(return_value, new_format, eol);

    RETURN_MM();
}
예제 #15
0
/**
 * Gets HTTP schema (http/https)
 *
 * @return string
 */
PHP_METHOD(Phalcon_Http_Request, getScheme){

	zval *https_header, *https, *scheme = NULL;

	PHALCON_MM_GROW();

	PHALCON_INIT_VAR(https_header);
	ZVAL_STRING(https_header, "HTTPS", 1);
	
	PHALCON_INIT_VAR(https);
	phalcon_call_method_p1(https, this_ptr, "getserver", https_header);
	if (zend_is_true(https)) {
		if (PHALCON_IS_STRING(https, "off")) {
			PHALCON_INIT_VAR(scheme);
			ZVAL_STRING(scheme, "http", 1);
		} else {
			PHALCON_INIT_NVAR(scheme);
			ZVAL_STRING(scheme, "https", 1);
		}
	} else {
		PHALCON_INIT_NVAR(scheme);
		ZVAL_STRING(scheme, "http", 1);
	}
	
	RETURN_CTOR(scheme);
}
예제 #16
0
/**
 * Executes the validator
 *
 * @param Phalcon\Mvc\ModelInterface $record
 * @return boolean
 */
PHP_METHOD(Phalcon_Mvc_Model_Validator_Numericality, validate){

	zval *record, *option = NULL, *field, *value, *message = NULL, *type;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &record);
	
	PHALCON_INIT_VAR(option);
	ZVAL_STRING(option, "field", 1);
	
	PHALCON_INIT_VAR(field);
	phalcon_call_method_p1(field, this_ptr, "getoption", option);
	if (Z_TYPE_P(field) != IS_STRING) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Field name must be a string");
		return;
	}
	
	PHALCON_INIT_VAR(value);
	phalcon_call_method_p1(value, record, "readattribute", field);
	
	/** 
	 * Check if the value is numeric using is_numeric in the PHP userland
	 */
	if (!phalcon_is_numeric(value)) {
	
		/** 
		 * Check if the developer has defined a custom message
		 */
		PHALCON_INIT_NVAR(option);
		ZVAL_STRING(option, "message", 1);
	
		PHALCON_INIT_VAR(message);
		phalcon_call_method_p1(message, this_ptr, "getoption", option);
		if (!zend_is_true(message)) {
			PHALCON_INIT_NVAR(message);
			PHALCON_CONCAT_SVS(message, "Value of field '", field, "' must be numeric");
		}
	
		PHALCON_INIT_VAR(type);
		ZVAL_STRING(type, "Numericality", 1);
		phalcon_call_method_p3_noret(this_ptr, "appendmessage", message, field, type);
		RETURN_MM_FALSE;
	}
	
	RETURN_MM_TRUE;
}
예제 #17
0
/**
 * Sets a Expires header to use HTTP cache
 *
 *<code>
 *	$this->response->setExpires(new DateTime());
 *</code>
 *
 * @param DateTime $datetime
 * @return Phalcon\Http\ResponseInterface
 */
PHP_METHOD(Phalcon_Http_Response, setExpires){

	zval *datetime, *headers, *date, *utc_zone, *timezone;
	zval *format, *utc_format, *utc_date, *expires_header;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &datetime);
	
	if (Z_TYPE_P(datetime) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_http_response_exception_ce, "datetime parameter must be an instance of DateTime");
		return;
	}
	
	PHALCON_INIT_VAR(headers);
	phalcon_call_method(headers, this_ptr, "getheaders");
	
	PHALCON_INIT_VAR(date);
	if (phalcon_clone(date, datetime TSRMLS_CC) == FAILURE) {
		return;
	}
	
	/** 
	 * All the expiration times are sent in UTC
	 */
	PHALCON_INIT_VAR(utc_zone);
	ZVAL_STRING(utc_zone, "UTC", 1);
	ce0 = zend_fetch_class(SL("DateTimeZone"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
	
	PHALCON_INIT_VAR(timezone);
	object_init_ex(timezone, ce0);
	if (phalcon_has_constructor(timezone TSRMLS_CC)) {
		phalcon_call_method_p1_noret(timezone, "__construct", utc_zone);
	}
	
	/** 
	 * Change the timezone to utc
	 */
	phalcon_call_method_p1_noret(date, "settimezone", timezone);
	
	PHALCON_INIT_VAR(format);
	ZVAL_STRING(format, "D, d M Y H:i:s", 1);
	
	PHALCON_INIT_VAR(utc_format);
	phalcon_call_method_p1(utc_format, date, "format", format);
	
	PHALCON_INIT_VAR(utc_date);
	PHALCON_CONCAT_VS(utc_date, utc_format, " GMT");
	
	/** 
	 * The 'Expires' header set this info
	 */
	PHALCON_INIT_VAR(expires_header);
	ZVAL_STRING(expires_header, "Expires", 1);
	phalcon_call_method_p2_noret(this_ptr, "setheader", expires_header, utc_date);
	
	RETURN_THIS();
}
예제 #18
0
/**
 * Returns a cached content
 *
 * @param int|string $keyName
 * @param   long $lifetime
 * @return  mixed
 */
PHP_METHOD(Phalcon_Cache_Backend_Memcache, get){

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

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &key_name, &lifetime);
	
	if (!lifetime) {
		PHALCON_INIT_VAR(lifetime);
	}
	
	PHALCON_OBS_VAR(memcache);
	phalcon_read_property_this(&memcache, this_ptr, SL("_memcache"), PH_NOISY_CC);
	if (Z_TYPE_P(memcache) != IS_OBJECT) {
		phalcon_call_method_noret(this_ptr, "_connect");
	
		PHALCON_OBS_NVAR(memcache);
		phalcon_read_property_this(&memcache, this_ptr, SL("_memcache"), PH_NOISY_CC);
	}
	
	PHALCON_OBS_VAR(frontend);
	phalcon_read_property_this(&frontend, this_ptr, SL("_frontend"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(prefix);
	phalcon_read_property_this(&prefix, this_ptr, SL("_prefix"), PH_NOISY_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_INIT_VAR(cached_content);
	phalcon_call_method_p1(cached_content, memcache, "get", prefixed_key);
	if (PHALCON_IS_FALSE(cached_content)) {
		RETURN_MM_NULL();
	}
	
	phalcon_call_method_p1(return_value, frontend, "afterretrieve", cached_content);
	RETURN_MM();
}
예제 #19
0
파일: bag.c 프로젝트: RSivakov/cphalcon
/**
 * Magic getter to obtain values from the session bag
 *
 *<code>
 * echo $user->name;
 *</code>
 *
 * @param string $property
 * @return string
 */
PHP_METHOD(Phalcon_Session_Bag, __get){

	zval *property, *value;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &property);
	
	PHALCON_INIT_VAR(value);
	phalcon_call_method_p1(value, this_ptr, "get", property);
	RETURN_CCTOR(value);
}
예제 #20
0
파일: bag.c 프로젝트: RSivakov/cphalcon
/**
 * Magic isset to check whether a property is defined in the bag
 *
 *<code>
 * var_dump(isset($user['name']));
 *</code>
 *
 * @param string $property
 * @return boolean
 */
PHP_METHOD(Phalcon_Session_Bag, __isset){

	zval *property, *exists;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &property);
	
	PHALCON_INIT_VAR(exists);
	phalcon_call_method_p1(exists, this_ptr, "has", property);
	RETURN_CCTOR(exists);
}
예제 #21
0
파일: bag.c 프로젝트: RSivakov/cphalcon
/**
 * Magic unset to remove items using the array syntax
 *
 *<code>
 * unset($user['name']);
 *</code>
 *
 * @param string $property
 * @return boolean
 */
PHP_METHOD(Phalcon_Session_Bag, __unset){

	zval *property, *success;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &property);
	
	PHALCON_INIT_VAR(success);
	phalcon_call_method_p1(success, this_ptr, "remove", property);
	RETURN_CCTOR(success);
}
예제 #22
0
/**
 * Returns a PDO prepared statement to be executed with 'executePrepared'
 *
 *<code>
 * $statement = $db->prepare('SELECT * FROM robots WHERE name = :name');
 * $result = $connection->executePrepared($statement, array('name' => 'Voltron'));
 *</code>
 *
 * @param string $sqlStatement
 * @return \PDOStatement
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo, prepare){

	zval *sql_statement, *pdo;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &sql_statement);
	
	PHALCON_OBS_VAR(pdo);
	phalcon_read_property_this(&pdo, this_ptr, SL("_pdo"), PH_NOISY_CC);
	phalcon_call_method_p1(return_value, pdo, "prepare", sql_statement);
	RETURN_MM();
}
예제 #23
0
/**
 * Escapes a value to avoid SQL injections according to the active charset in the connection
 *
 *<code>
 *	$escapedStr = $connection->escapeString('some dangerous value');
 *</code>
 *
 * @param string $str
 * @return string
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo, escapeString){

	zval *str, *pdo;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &str);
	
	PHALCON_OBS_VAR(pdo);
	phalcon_read_property_this(&pdo, this_ptr, SL("_pdo"), PH_NOISY_CC);
	phalcon_call_method_p1(return_value, pdo, "quote", str);
	RETURN_MM();
}
예제 #24
0
/**
 * Returns the messages in the session flasher
 *
 * @param string $type
 * @param boolean $remove
 * @return array
 */
PHP_METHOD(Phalcon_Flash_Session, getMessages){

	zval *type = NULL, *remove = NULL, *messages, *return_messages;
	zval *do_remove;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 2, &type, &remove);
	
	if (!type) {
		PHALCON_INIT_VAR(type);
	}
	
	if (!remove) {
		PHALCON_INIT_VAR(remove);
		ZVAL_BOOL(remove, 1);
	}

	if (Z_TYPE_P(type) != IS_NULL) {
		PHALCON_INIT_VAR(do_remove);
		ZVAL_FALSE(do_remove);
	}
	else {
		do_remove = remove;
	}

	PHALCON_INIT_VAR(messages);
	phalcon_call_method_p1(messages, this_ptr, "_getsessionmessages", do_remove);
	if (Z_TYPE_P(messages) == IS_ARRAY) {
		if (likely(Z_TYPE_P(type) != IS_NULL)) {
			if (phalcon_array_isset(messages, type)) {
				PHALCON_OBS_VAR(return_messages);
				phalcon_array_fetch(&return_messages, messages, type, PH_NOISY);
				RETVAL_ZVAL(return_messages, 1, 0);
				if (zend_is_true(remove)) {
					phalcon_array_unset(&messages, type, 0);
					phalcon_call_method_p1_noret(this_ptr, "_setsessionmessages", messages);
				}

				PHALCON_MM_RESTORE();
				return;
			}

			RETURN_MM_EMPTY_ARRAY();
		}
	
		RETURN_CCTOR(messages);
	}
	
	RETURN_MM_EMPTY_ARRAY();
}
예제 #25
0
/**
 * Generates a pseudo random token key to be used as input's name in a CSRF check
 *
 * @param int $numberBytes
 * @return string
 */
PHP_METHOD(Phalcon_Security, getTokenKey){

	zval *number_bytes = NULL, *random_bytes, *base64bytes;
	zval *safe_bytes, *dependency_injector, *service;
	zval *session, *key;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 1, &number_bytes);
	
	if (!number_bytes) {
		PHALCON_INIT_VAR(number_bytes);
		ZVAL_LONG(number_bytes, 12);
	}
	
	if (phalcon_function_exists_ex(SS("openssl_random_pseudo_bytes") TSRMLS_CC) == FAILURE) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_security_exception_ce, "Openssl extension must be loaded");
		return;
	}
	
	PHALCON_INIT_VAR(random_bytes);
	phalcon_call_func_p1(random_bytes, "openssl_random_pseudo_bytes", number_bytes);
	
	PHALCON_INIT_VAR(base64bytes);
	phalcon_base64_encode(base64bytes, random_bytes);
	
	PHALCON_INIT_VAR(safe_bytes);
	phalcon_filter_alphanum(safe_bytes, base64bytes);
	
	PHALCON_OBS_VAR(dependency_injector);
	phalcon_read_property_this(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC);
	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_flash_exception_ce, "A dependency injection container is required to access the 'session' service");
		return;
	}
	
	PHALCON_INIT_VAR(service);
	ZVAL_STRING(service, "session", 1);
	
	PHALCON_INIT_VAR(session);
	phalcon_call_method_p1(session, dependency_injector, "getshared", service);
	PHALCON_VERIFY_INTERFACE(session, phalcon_session_adapterinterface_ce);
	
	PHALCON_INIT_VAR(key);
	ZVAL_STRING(key, "$PHALCON/CSRF/KEY$", 1);
	phalcon_call_method_p2_noret(session, "set", key, safe_bytes);
	
	RETURN_CTOR(safe_bytes);
}
예제 #26
0
파일: date.c 프로젝트: 11mariom/cphalcon
/**
 * Renders the element widget returning html
 *
 * @param array $attributes
 * @return string
 */
PHP_METHOD(Phalcon_Forms_Element_Date, render){

	zval *attributes = NULL, *widget_attributes;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 1, &attributes);
	
	if (!attributes) {
		PHALCON_INIT_VAR(attributes);
	}
	
	PHALCON_INIT_VAR(widget_attributes);
	phalcon_call_method_p1(widget_attributes, this_ptr, "prepareattributes", attributes);
	PHALCON_CALL_STATIC_PARAMS_1(return_value, "phalcon\\tag", "datefield", widget_attributes);
	RETURN_MM();
}
예제 #27
0
/**
 * Checks whether request has been made using ajax. Checks if $_SERVER['HTTP_X_REQUESTED_WITH']=='XMLHttpRequest'
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Http_Request, isAjax){

	zval *requested_header, *xml_http_request;
	zval *requested_with;

	PHALCON_MM_GROW();

	PHALCON_INIT_VAR(requested_header);
	ZVAL_STRING(requested_header, "HTTP_X_REQUESTED_WITH", 1);
	
	PHALCON_INIT_VAR(xml_http_request);
	ZVAL_STRING(xml_http_request, "XMLHttpRequest", 1);
	
	PHALCON_INIT_VAR(requested_with);
	phalcon_call_method_p1(requested_with, this_ptr, "getheader", requested_header);
	is_equal_function(return_value, requested_with, xml_http_request TSRMLS_CC);
	RETURN_MM();
}
예제 #28
0
파일: hidden.c 프로젝트: RSivakov/cphalcon
/**
 * Renders the element widget returning html
 *
 * @param array $attributes
 * @return string
 */
PHP_METHOD(Phalcon_Forms_Element_Hidden, render){

	zval *attributes = NULL, *widget_attributes, *code;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 1, &attributes);
	
	if (!attributes) {
		PHALCON_INIT_VAR(attributes);
	}
	
	PHALCON_INIT_VAR(widget_attributes);
	phalcon_call_method_p1(widget_attributes, this_ptr, "prepareattributes", attributes);
	
	PHALCON_INIT_VAR(code);
	PHALCON_CALL_STATIC_PARAMS_1(code, "phalcon\\tag", "hiddenfield", widget_attributes);
	RETURN_CCTOR(code);
}
예제 #29
0
파일: element.c 프로젝트: RSivakov/cphalcon
/**
 * Returns the element's value
 *
 * @return mixed
 */
PHP_METHOD(Phalcon_Forms_Element, getValue){

	zval *name, *value = NULL, *form, *has_default_value;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(name);
	phalcon_read_property_this(&name, this_ptr, SL("_name"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(value);
	
	/** 
	 * Get the related form
	 */
	PHALCON_OBS_VAR(form);
	phalcon_read_property_this(&form, this_ptr, SL("_form"), PH_NOISY_CC);
	if (Z_TYPE_P(form) == IS_OBJECT) {
	
		/** 
		 * Check if the tag has a default value
		 */
		PHALCON_INIT_VAR(has_default_value);
		PHALCON_CALL_STATIC_PARAMS_1(has_default_value, "phalcon\\tag", "hasvalue", name);
		if (!zend_is_true(has_default_value)) {
			/** 
			 * Gets the possible value for the widget
			 */
			phalcon_call_method_p1(value, form, "getvalue", name);
		}
	}
	
	/** 
	 * Assign the default value if there is no form available
	 */
	if (Z_TYPE_P(value) == IS_NULL) {
		PHALCON_OBS_NVAR(value);
		phalcon_read_property_this(&value, this_ptr, SL("_value"), PH_NOISY_CC);
	}
	
	RETURN_CCTOR(value);
}
예제 #30
0
/**
 * Returns the insert id for the auto_increment/serial column inserted in the lastest executed SQL statement
 *
 *<code>
 * //Inserting a new robot
 * $success = $connection->insert(
 *     "robots",
 *     array("Astro Boy", 1952),
 *     array("name", "year")
 * );
 *
 * //Getting the generated id
 * $id = $connection->lastInsertId();
 *</code>
 *
 * @param string $sequenceName
 * @return int
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo, lastInsertId){

	zval *sequence_name = NULL, *pdo;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 1, &sequence_name);
	
	if (!sequence_name) {
		PHALCON_INIT_VAR(sequence_name);
	}
	
	PHALCON_OBS_VAR(pdo);
	phalcon_read_property_this(&pdo, this_ptr, SL("_pdo"), PH_NOISY_CC);
	if (Z_TYPE_P(pdo) != IS_OBJECT) {
		RETURN_MM_FALSE;
	}
	
	phalcon_call_method_p1(return_value, pdo, "lastinsertid", sequence_name);
	RETURN_MM();
}