/** * 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); }
/** * Returns the messages stored in session * * @param boolean $remove * @return array */ PHP_METHOD(Phalcon_Flash_Session, _getSessionMessages){ zval *remove, *dependency_injector, *service; zval *session = NULL, *index_name; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &remove); dependency_injector = phalcon_fetch_nproperty_this(this_ptr, SL("_dependencyInjector"), PH_NOISY TSRMLS_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); PHALCON_ZVAL_MAYBE_INTERNED_STRING(service, phalcon_interned_session); PHALCON_CALL_METHOD(&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_RETURN_CALL_METHOD(session, "get", index_name); if (PHALCON_IS_TRUE(remove)) { PHALCON_CALL_METHOD(NULL, session, "remove", index_name); } RETURN_MM(); }
/** * Returns the messages stored in session * * @param boolean $remove * @return array */ PHP_METHOD(Phalcon_Flash_Session, _getSessionMessages){ zval *remove, *dependency_injector = NULL, *service; zval *session = NULL, *index_name; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &remove); PHALCON_CALL_METHOD(&dependency_injector, this_ptr, "getdi"); PHALCON_INIT_VAR(service); PHALCON_ZVAL_MAYBE_INTERNED_STRING(service, phalcon_interned_session); PHALCON_CALL_METHOD(&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_RETURN_CALL_METHOD(session, "get", index_name); if (PHALCON_IS_TRUE(remove)) { PHALCON_CALL_METHOD(NULL, session, "remove", index_name); } RETURN_MM(); }
/** * 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); }
/** * 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); }
/** * Phalcon\Mvc\Micro constructor * * @param Phalcon\DiInterface $dependencyInjector */ PHP_METHOD(Phalcon_Mvc_Micro, __construct){ zval *dependency_injector = NULL; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 1, &dependency_injector); if (dependency_injector && Z_TYPE_P(dependency_injector) == IS_OBJECT) { PHALCON_VERIFY_INTERFACE(dependency_injector, phalcon_diinterface_ce); PHALCON_CALL_METHOD(NULL, getThis(), "setdi", dependency_injector); } PHALCON_MM_RESTORE(); }
/** * 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(); }
/** * Create a Phalcon\Cache based on the internal cache options * * @return Phalcon\Cache\BackendInterface */ PHP_METHOD(Phalcon_Mvc_View_Simple, _createCache){ zval *dependency_injector, *cache_service = NULL; zval *cache_options; PHALCON_MM_GROW(); PHALCON_OBS_VAR(dependency_injector); phalcon_read_property_this(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY TSRMLS_CC); if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_view_exception_ce, "A dependency injector container is required to obtain the view cache services"); return; } PHALCON_INIT_VAR(cache_service); ZVAL_STRING(cache_service, "viewCache", 1); PHALCON_OBS_VAR(cache_options); phalcon_read_property_this(&cache_options, this_ptr, SL("_cacheOptions"), PH_NOISY TSRMLS_CC); if (Z_TYPE_P(cache_options) == IS_ARRAY) { if (phalcon_array_isset_string(cache_options, SS("service"))) { PHALCON_OBS_NVAR(cache_service); phalcon_array_fetch_string(&cache_service, cache_options, SL("service"), PH_NOISY); } } /** * The injected service must be an object */ PHALCON_RETURN_CALL_METHOD(dependency_injector, "getshared", cache_service); if (return_value_ptr) { return_value = *return_value_ptr; } if (Z_TYPE_P(return_value) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_view_exception_ce, "The injected caching service is invalid"); return; } PHALCON_VERIFY_INTERFACE(return_value, phalcon_cache_backendinterface_ce); PHALCON_MM_RESTORE(); }
/** * Handles a MVC request * * @param string $uri * @return Phalcon\Http\ResponseInterface */ PHP_METHOD(Phalcon_Mvc_JsonRpc, handle){ zval *uri = NULL, *dependency_injector, *events_manager; zval *status = NULL, *service = NULL, *request = NULL, *response = NULL; zval *json = NULL, *data = NULL, *jsonrpc_message, *jsonrpc_error, *jsonrpc_result = NULL; zval *jsonrpc_method, *jsonrpc_params, *jsonrpc_id; zval *url = NULL, *router = NULL, *module_name = NULL; zval *module_object = NULL, *modules; zval *module, *class_name = NULL, *module_params; zval *namespace_name = NULL; zval *controller_name = NULL, *action_name = NULL, *params = NULL, *exact = NULL; zval *dispatcher = NULL, *controller = NULL, *returned_response = NULL; zval *path; PHALCON_MM_GROW(); dependency_injector = phalcon_read_property(getThis(), SL("_dependencyInjector"), PH_NOISY); if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_jsonrpc_exception_ce, "A dependency injection object is required to access internal services"); return; } events_manager = phalcon_read_property(getThis(), SL("_eventsManager"), PH_NOISY); if (Z_TYPE_P(events_manager) != IS_OBJECT) { events_manager = NULL; } else { PHALCON_VERIFY_INTERFACE_EX(events_manager, phalcon_events_managerinterface_ce, phalcon_mvc_jsonrpc_exception_ce, 1); } /* Call boot event, this allows the developer to perform initialization actions */ if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:boot", getThis(), NULL)) { RETURN_MM_FALSE; } /* Deserializer Json */ PHALCON_INIT_NVAR(service); ZVAL_STR(service, IS(request)); PHALCON_CALL_METHOD(&request, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(request, phalcon_http_requestinterface_ce); PHALCON_CALL_METHOD(&json, request, "getrawbody"); PHALCON_CALL_FUNCTION(&data, "json_decode", json, &PHALCON_GLOBAL(z_true)); PHALCON_INIT_NVAR(service); ZVAL_STR(service, IS(response)); PHALCON_CALL_METHOD(&response, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(response, phalcon_http_responseinterface_ce); PHALCON_INIT_VAR(jsonrpc_message); array_init(jsonrpc_message); PHALCON_INIT_VAR(jsonrpc_error); array_init(jsonrpc_error); if (PHALCON_IS_EMPTY(data)) { phalcon_array_update_str_long(jsonrpc_error, SL("code"), __LINE__, 0); phalcon_array_update_str_str(jsonrpc_error, SL("message"), SL("Parse error"), PH_COPY); } else if (Z_TYPE_P(data) != IS_ARRAY) { phalcon_array_update_str_long(jsonrpc_error, SL("code"), __LINE__, 0); phalcon_array_update_str_str(jsonrpc_error, SL("message"), SL("Parse error"), PH_COPY); } else if (!phalcon_array_isset_str(data, SL("jsonrpc"))) { phalcon_array_update_str_long(jsonrpc_error, SL("code"), __LINE__, 0); phalcon_array_update_str_str(jsonrpc_error, SL("message"), SL("Invalid Request"), PH_COPY); } else if (!phalcon_array_isset_str(data, SL("method"))) { phalcon_array_update_str_long(jsonrpc_error, SL("code"), __LINE__, 0); phalcon_array_update_str_str(jsonrpc_error, SL("message"), SL("Invalid Request"), PH_COPY); } else { PHALCON_OBS_VAR(jsonrpc_method); phalcon_array_fetch_str(&jsonrpc_method, data, SL("method"), PH_NOISY); if (phalcon_array_isset_str(data, SL("params"))) { PHALCON_OBS_VAR(jsonrpc_params); phalcon_array_fetch_str(&jsonrpc_params, data, SL("params"), PH_NOISY); } else { PHALCON_INIT_VAR(jsonrpc_params); array_init(jsonrpc_params); } PHALCON_INIT_NVAR(service); ZVAL_STR(service, IS(url)); PHALCON_CALL_METHOD(&url, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(url, phalcon_mvc_urlinterface_ce); PHALCON_CALL_METHOD(&uri, url, "get", jsonrpc_method); PHALCON_INIT_NVAR(service); ZVAL_STR(service, IS(router)); PHALCON_CALL_METHOD(&router, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(router, phalcon_mvc_routerinterface_ce); /* Handle the URI pattern (if any) */ PHALCON_CALL_METHOD(NULL, router, "handle", uri); /* Load module config */ PHALCON_CALL_METHOD(&module_name, router, "getmodulename"); /* Load module config */ PHALCON_CALL_METHOD(&module_name, router, "getmodulename"); /* If the router doesn't return a valid module we use the default module */ if (!zend_is_true(module_name)) { module_name = phalcon_read_property(getThis(), SL("_defaultModule"), PH_NOISY); } /** * Process the module definition */ if (zend_is_true(module_name)) { if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:beforeStartModule", getThis(), module_name)) { RETURN_MM_FALSE; } /** * Check if the module passed by the router is registered in the modules container */ modules = phalcon_read_property(getThis(), SL("_modules"), PH_NOISY); if (!phalcon_array_isset_fetch(&module, modules, module_name)) { convert_to_string(module_name); zend_throw_exception_ex(phalcon_mvc_jsonrpc_exception_ce, 0, "Module %s is not registered in the jsonrpc container", Z_STRVAL_P(module_name)); RETURN_MM(); } /** * A module definition must be an array or an object */ if (Z_TYPE_P(module) != IS_ARRAY && Z_TYPE_P(module) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_jsonrpc_exception_ce, "Invalid module definition"); return; } /* An array module definition contains a path to a module definition class */ if (Z_TYPE_P(module) == IS_ARRAY) { /* Class name used to load the module definition */ if (phalcon_array_isset_str(module, SL("className"))) { PHALCON_OBS_VAR(class_name); phalcon_array_fetch_str(&class_name, module, SL("className"), PH_NOISY); } else { PHALCON_INIT_NVAR(class_name); ZVAL_STRING(class_name, "Module"); } /* If the developer has specified a path, try to include the file */ if (phalcon_array_isset_str(module, SL("path"))) { PHALCON_OBS_VAR(path); phalcon_array_fetch_str(&path, module, SL("path"), PH_NOISY); convert_to_string_ex(path); if (Z_TYPE_P(class_name) != IS_STRING || phalcon_class_exists(class_name, 0) == NULL) { if (phalcon_file_exists(path) == SUCCESS) { RETURN_MM_ON_FAILURE(phalcon_require(Z_STRVAL_P(path))); } else { zend_throw_exception_ex(phalcon_mvc_jsonrpc_exception_ce, 0, "Module definition path '%s' does not exist", Z_STRVAL_P(path)); RETURN_MM(); } } } PHALCON_CALL_METHOD(&module_object, dependency_injector, "get", class_name); /** * 'registerAutoloaders' and 'registerServices' are automatically called */ PHALCON_CALL_METHOD(NULL, module_object, "registerautoloaders", dependency_injector); PHALCON_CALL_METHOD(NULL, module_object, "registerservices", dependency_injector); } else if (Z_TYPE_P(module) == IS_OBJECT && instanceof_function(Z_OBJCE_P(module), zend_ce_closure)) { /* A module definition object, can be a Closure instance */ PHALCON_INIT_VAR(module_params); array_init_size(module_params, 1); phalcon_array_append(module_params, dependency_injector, PH_COPY); PHALCON_CALL_USER_FUNC_ARRAY(&status, module, module_params); } else { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_jsonrpc_exception_ce, "Invalid module definition"); return; } /* Calling afterStartModule event */ if (events_manager) { if (!module_object) { module_object = &PHALCON_GLOBAL(z_null); } phalcon_update_property_this(getThis(), SL("_moduleObject"), module_object); if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:afterStartModule", getThis(), module_name)) { RETURN_MM_FALSE; } } } /* We get the parameters from the router and assign them to the dispatcher */ PHALCON_CALL_METHOD(&module_name, router, "getmodulename"); PHALCON_CALL_METHOD(&namespace_name, router, "getnamespacename"); PHALCON_CALL_METHOD(&controller_name, router, "getcontrollername"); PHALCON_CALL_METHOD(&action_name, router, "getactionname"); PHALCON_CALL_METHOD(¶ms, router, "getparams"); PHALCON_CALL_METHOD(&exact, router, "isexactcontrollername"); PHALCON_INIT_NVAR(service); ZVAL_STR(service, IS(dispatcher)); PHALCON_CALL_METHOD(&dispatcher, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(dispatcher, phalcon_dispatcherinterface_ce); /* Assign the values passed from the router */ PHALCON_CALL_METHOD(NULL, dispatcher, "setmodulename", module_name); PHALCON_CALL_METHOD(NULL, dispatcher, "setnamespacename", namespace_name); PHALCON_CALL_METHOD(NULL, dispatcher, "setcontrollername", controller_name, exact); PHALCON_CALL_METHOD(NULL, dispatcher, "setactionname", action_name); PHALCON_CALL_METHOD(NULL, dispatcher, "setparams", jsonrpc_params); /* Calling beforeHandleRequest */ RETURN_MM_ON_FAILURE(phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:beforeHandleRequest", getThis(), dispatcher)); /* The dispatcher must return an object */ PHALCON_CALL_METHOD(&controller, dispatcher, "dispatch"); PHALCON_INIT_VAR(returned_response); /* Get the latest value returned by an action */ PHALCON_CALL_METHOD(&jsonrpc_result, dispatcher, "getreturnedvalue"); } /* Calling afterHandleRequest */ if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:afterHandleRequest", getThis(), controller) && EG(exception)) { RETURN_MM(); } phalcon_array_update_str_str(jsonrpc_message, SL("jsonrpc"), SL("2.0"), PH_COPY); if (PHALCON_IS_NOT_EMPTY(jsonrpc_error)) { phalcon_array_update_str(jsonrpc_message, SL("error"), jsonrpc_error, PH_COPY); } if (jsonrpc_result != NULL) { phalcon_array_update_str(jsonrpc_message, SL("result"), jsonrpc_result, PH_COPY); } if (phalcon_array_isset_str_fetch(&jsonrpc_id, data, SL("id"))) { phalcon_array_update_str(jsonrpc_message, SL("id"), jsonrpc_id, PH_COPY); } else { phalcon_array_update_str(jsonrpc_message, SL("id"), &PHALCON_GLOBAL(z_null), PH_COPY); } PHALCON_CALL_METHOD(NULL, response, "setjsoncontent", jsonrpc_message); /* Calling beforeSendResponse */ if (FAILURE == phalcon_mvc_jsonrpc_fire_event(events_manager, "jsonrpc:beforeSendResponse", getThis(), response) && EG(exception)) { RETURN_MM(); } /* Headers are automatically sent */ PHALCON_CALL_METHOD(NULL, response, "sendheaders"); /* Cookies are automatically sent */ PHALCON_CALL_METHOD(NULL, response, "sendcookies"); /* Return the response */ RETURN_CCTOR(response); }
/** * 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, *dependency_injector, *events_manager; zval *service = NULL, *router, *module_name, *event_name = NULL; zval *status = NULL, *modules, *exception_msg = NULL, *module; zval *path, *class_name = NULL, *module_object, *task_name; zval *action_name, *params, *dispatcher, *task; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 1, &arguments); if (!arguments) { PHALCON_INIT_VAR(arguments); array_init(arguments); } 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_cli_console_exception_ce, "A dependency injection object is required to access internal services"); return; } PHALCON_OBS_VAR(events_manager); phalcon_read_property_this(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC); PHALCON_INIT_VAR(service); ZVAL_STRING(service, "router", 1); PHALCON_INIT_VAR(router); phalcon_call_method_p1(router, dependency_injector, "getshared", service); phalcon_call_method_p1_noret(router, "handle", arguments); PHALCON_INIT_VAR(module_name); phalcon_call_method(module_name, router, "getmodulename"); if (zend_is_true(module_name)) { if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_VAR(event_name); ZVAL_STRING(event_name, "console:beforeStartModule", 1); PHALCON_INIT_VAR(status); phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, module_name); if (PHALCON_IS_FALSE(status)) { RETURN_MM_FALSE; } } PHALCON_OBS_VAR(modules); phalcon_read_property_this(&modules, this_ptr, SL("_modules"), PH_NOISY_CC); if (!phalcon_array_isset(modules, module_name)) { PHALCON_INIT_VAR(exception_msg); PHALCON_CONCAT_SVS(exception_msg, "Module '", module_name, "' isn't registered in the console container"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_cli_console_exception_ce, exception_msg); return; } PHALCON_OBS_VAR(module); phalcon_array_fetch(&module, modules, module_name, PH_NOISY); if (Z_TYPE_P(module) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_cli_console_exception_ce, "Invalid module definition path"); return; } if (phalcon_array_isset_string(module, SS("path"))) { PHALCON_OBS_VAR(path); phalcon_array_fetch_string(&path, module, SL("path"), PH_NOISY); if (phalcon_file_exists(path TSRMLS_CC) == SUCCESS) { if (phalcon_require(path TSRMLS_CC) == FAILURE) { return; } } else { PHALCON_INIT_NVAR(exception_msg); PHALCON_CONCAT_SVS(exception_msg, "Module definition path '", path, "\" doesn't exist"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_cli_console_exception_ce, exception_msg); return; } } if (phalcon_array_isset_string(module, SS("className"))) { PHALCON_OBS_VAR(class_name); phalcon_array_fetch_string(&class_name, module, SL("className"), PH_NOISY); } else { PHALCON_INIT_NVAR(class_name); ZVAL_STRING(class_name, "Module", 1); } PHALCON_INIT_VAR(module_object); phalcon_call_method_p1(module_object, dependency_injector, "get", class_name); phalcon_call_method_noret(module_object, "registerautoloaders"); phalcon_call_method_p1_noret(module_object, "registerservices", dependency_injector); if (Z_TYPE_P(events_manager) == IS_OBJECT) { phalcon_update_property_this(this_ptr, SL("_moduleObject"), module_object TSRMLS_CC); PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "console:afterStartModule", 1); PHALCON_INIT_NVAR(status); phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, module_name); if (PHALCON_IS_FALSE(status)) { RETURN_MM_FALSE; } } } PHALCON_INIT_VAR(task_name); phalcon_call_method(task_name, router, "gettaskname"); PHALCON_INIT_VAR(action_name); phalcon_call_method(action_name, router, "getactionname"); PHALCON_INIT_VAR(params); phalcon_call_method(params, router, "getparams"); PHALCON_INIT_NVAR(service); ZVAL_STRING(service, "dispatcher", 1); PHALCON_INIT_VAR(dispatcher); phalcon_call_method_p1(dispatcher, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(dispatcher, phalcon_dispatcherinterface_ce); phalcon_call_method_p1_noret(dispatcher, "settaskname", task_name); phalcon_call_method_p1_noret(dispatcher, "setactionname", action_name); phalcon_call_method_p1_noret(dispatcher, "setparams", params); if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "console:beforeHandleTask", 1); PHALCON_INIT_NVAR(status); phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, dispatcher); if (PHALCON_IS_FALSE(status)) { RETURN_MM_FALSE; } } PHALCON_INIT_VAR(task); phalcon_call_method(task, dispatcher, "dispatch"); if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "console:afterHandleTask", 1); phalcon_call_method_p3_noret(events_manager, "fire", event_name, this_ptr, task); } RETURN_CCTOR(task); }
/** * Executes the validator * * @param Phalcon\Mvc\ModelInterface $record * @return boolean */ PHP_METHOD(Phalcon_Mvc_Model_Validator_Uniqueness, validate){ zval *record, *option = NULL, *field = NULL, *dependency_injector = NULL; zval *service, *meta_data = NULL, *bind_types, *bind_data_types = NULL; zval *column_map = NULL, *conditions, *bind_params; zval *number = NULL, *compose_field = NULL, *column_field = NULL; zval *exception_message = NULL, *value = NULL, *compose_condition = NULL; zval *bind_type = NULL, *condition = NULL, *operation_made = NULL; zval *primary_fields = NULL, *primary_field = NULL, *attribute_field = NULL; zval *join_conditions, *params; zval *message = NULL, *join_fields, *type, *is_set_code = NULL, *code = NULL; HashTable *ah0, *ah1; HashPosition hp0, hp1; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &record); PHALCON_INIT_VAR(option); ZVAL_STRING(option, "field", 1); PHALCON_CALL_METHOD(&field, this_ptr, "getoption", option); PHALCON_CALL_METHOD(&dependency_injector, record, "getdi"); PHALCON_INIT_VAR(service); ZVAL_STRING(service, "modelsMetadata", 1); PHALCON_CALL_METHOD(&meta_data, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(meta_data, phalcon_mvc_model_metadatainterface_ce); /** * PostgreSQL check if the compared constant has the same type as the column, so we * make cast to the data passed to match those column types */ PHALCON_INIT_VAR(bind_types); array_init(bind_types); PHALCON_CALL_METHOD(&bind_data_types, meta_data, "getbindtypes", record); if (PHALCON_GLOBAL(orm).column_renaming) { PHALCON_CALL_METHOD(&column_map, meta_data, "getreversecolumnmap", record); } else { PHALCON_INIT_VAR(column_map); } PHALCON_INIT_VAR(conditions); array_init(conditions); PHALCON_INIT_VAR(bind_params); array_init(bind_params); PHALCON_INIT_VAR(number); ZVAL_LONG(number, 0); if (Z_TYPE_P(field) == IS_ARRAY) { /** * The field can be an array of values */ phalcon_is_iterable(field, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(compose_field); /** * The reversed column map is used in the case to get real column name */ if (Z_TYPE_P(column_map) == IS_ARRAY) { if (phalcon_array_isset(column_map, compose_field)) { PHALCON_OBS_NVAR(column_field); phalcon_array_fetch(&column_field, column_map, compose_field, PH_NOISY); } else { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Column '", compose_field, "\" isn't part of the column map"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } } else { PHALCON_CPY_WRT(column_field, compose_field); } /** * Some database systems require that we pass the values using bind casting */ if (!phalcon_array_isset(bind_data_types, column_field)) { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Column '", column_field, "\" isn't part of the table columns"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } /** * The attribute could be "protected" so we read using "readattribute" */ PHALCON_CALL_METHOD(&value, record, "readattribute", compose_field); PHALCON_INIT_NVAR(compose_condition); PHALCON_CONCAT_SVSV(compose_condition, "[", compose_field, "] = ?", number); phalcon_array_append(&conditions, compose_condition, PH_SEPARATE); phalcon_array_append(&bind_params, value, PH_SEPARATE); PHALCON_OBS_NVAR(bind_type); phalcon_array_fetch(&bind_type, bind_data_types, column_field, PH_NOISY); phalcon_array_append(&bind_types, bind_type, PH_SEPARATE); phalcon_increment(number); zend_hash_move_forward_ex(ah0, &hp0); } } else { /** * The reversed column map is used in the case to get real column name */ if (Z_TYPE_P(column_map) == IS_ARRAY) { if (phalcon_array_isset(column_map, field)) { PHALCON_OBS_NVAR(column_field); phalcon_array_fetch(&column_field, column_map, field, PH_NOISY); } else { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Column '", field, "\" isn't part of the column map"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } } else { PHALCON_CPY_WRT(column_field, field); } /** * Some database systems require that we pass the values using bind casting */ if (!phalcon_array_isset(bind_data_types, column_field)) { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Column '", column_field, "\" isn't part of the table columns"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } /** * We're checking the uniqueness with only one field */ PHALCON_CALL_METHOD(&value, record, "readattribute", field); PHALCON_INIT_VAR(condition); PHALCON_CONCAT_SVS(condition, "[", field, "] = ?0"); phalcon_array_append(&conditions, condition, PH_SEPARATE); phalcon_array_append(&bind_params, value, PH_SEPARATE); PHALCON_OBS_NVAR(bind_type); phalcon_array_fetch(&bind_type, bind_data_types, column_field, PH_NOISY); phalcon_array_append(&bind_types, bind_type, PH_SEPARATE); phalcon_increment(number); } /** * If the operation is update, there must be values in the object */ PHALCON_CALL_METHOD(&operation_made, record, "getoperationmade"); if (PHALCON_IS_LONG(operation_made, 2)) { /** * We build a query with the primary key attributes */ if (PHALCON_GLOBAL(orm).column_renaming) { PHALCON_CALL_METHOD(&column_map, meta_data, "getcolumnmap", record); } else { PHALCON_INIT_VAR(column_map); } PHALCON_CALL_METHOD(&primary_fields, meta_data, "getprimarykeyattributes", record); phalcon_is_iterable(primary_fields, &ah1, &hp1, 0, 0); while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_HVALUE(primary_field); if (!phalcon_array_isset(bind_data_types, primary_field)) { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Column '", primary_field, "\" isn't part of the table columns"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } /** * Rename the column if there is a column map */ if (Z_TYPE_P(column_map) == IS_ARRAY) { if (phalcon_array_isset(column_map, primary_field)) { PHALCON_OBS_NVAR(attribute_field); phalcon_array_fetch(&attribute_field, column_map, primary_field, PH_NOISY); } else { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Column '", primary_field, "\" isn't part of the column map"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } } else { PHALCON_CPY_WRT(attribute_field, primary_field); } /** * Create a condition based on the renamed primary key */ PHALCON_CALL_METHOD(&value, record, "readattribute", primary_field); PHALCON_INIT_NVAR(condition); PHALCON_CONCAT_SVSV(condition, "[", attribute_field, "] <> ?", number); phalcon_array_append(&conditions, condition, PH_SEPARATE); phalcon_array_append(&bind_params, value, PH_SEPARATE); PHALCON_OBS_NVAR(bind_type); phalcon_array_fetch(&bind_type, bind_data_types, primary_field, PH_NOISY); phalcon_array_append(&bind_types, bind_type, PH_SEPARATE); phalcon_increment(number); zend_hash_move_forward_ex(ah1, &hp1); } } PHALCON_INIT_VAR(join_conditions); phalcon_fast_join_str(join_conditions, SL(" AND "), conditions TSRMLS_CC); /** * We don't trust the user, so we pass the parameters as bound parameters */ PHALCON_INIT_VAR(params); array_init_size(params, 4); phalcon_array_update_string(¶ms, SL("di"), dependency_injector, PH_COPY); phalcon_array_update_string(¶ms, SL("conditions"), join_conditions, PH_COPY); phalcon_array_update_string(¶ms, SL("bind"), bind_params, PH_COPY); phalcon_array_update_string(¶ms, SL("bindTypes"), bind_types, PH_COPY); /** * Check using a standard count */ PHALCON_CALL_CE_STATIC(&number, Z_OBJCE_P(record), "count", params); if (!PHALCON_IS_LONG(number, 0)) { /** * Check if the developer has defined a custom message */ PHALCON_INIT_NVAR(option); PHALCON_ZVAL_MAYBE_INTERNED_STRING(option, phalcon_interned_message); PHALCON_CALL_METHOD(&message, this_ptr, "getoption", option); if (!zend_is_true(message)) { if (Z_TYPE_P(field) == IS_ARRAY) { PHALCON_INIT_VAR(join_fields); phalcon_fast_join_str(join_fields, SL(", "), field TSRMLS_CC); PHALCON_INIT_NVAR(message); PHALCON_CONCAT_SVS(message, "Value of fields: '", join_fields, "' are already present in another record"); } else { PHALCON_INIT_NVAR(message); PHALCON_CONCAT_SVS(message, "Value of field: '", field, "' is already present in another record"); } } /** * Append the message to the validator */ PHALCON_INIT_VAR(type); ZVAL_STRING(type, "Unique", 1); /* * Is code set */ PHALCON_INIT_NVAR(option); PHALCON_ZVAL_MAYBE_INTERNED_STRING(option, phalcon_interned_code); PHALCON_CALL_METHOD(&is_set_code, this_ptr, "issetoption", option); if (zend_is_true(is_set_code)) { PHALCON_CALL_METHOD(&code, this_ptr, "getoption", option); } else { PHALCON_INIT_VAR(code); ZVAL_LONG(code, 0); } PHALCON_CALL_METHOD(NULL, this_ptr, "appendmessage", message, field, type, code); RETURN_MM_FALSE; } RETURN_MM_TRUE; }
/** * Gets the a value to validate in the array/object data source * * @param string $attribute * @return mixed */ PHP_METHOD(Phalcon_Validation, getValue){ zval *attribute, *entity, *method, *value = NULL, *data, *values; zval *filters, *field_filters, *service_name; zval *dependency_injector = NULL, *filter_service; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &attribute); PHALCON_OBS_VAR(entity); phalcon_read_property_this(&entity, this_ptr, SL("_entity"), PH_NOISY_CC); /** * If the entity is an object use it to retrieve the values */ if (Z_TYPE_P(entity) == IS_OBJECT) { PHALCON_INIT_VAR(method); PHALCON_CONCAT_SV(method, "get", attribute); if (phalcon_method_exists(entity, method TSRMLS_CC) == SUCCESS) { PHALCON_INIT_VAR(value); phalcon_call_method_zval(value, entity, method); } else if (phalcon_method_exists_ex(entity, SS("readattribute") TSRMLS_CC) == SUCCESS) { PHALCON_INIT_VAR(value); phalcon_call_method_p1(value, entity, "readattribute", attribute); } else if (phalcon_isset_property_zval(entity, attribute TSRMLS_CC)) { PHALCON_OBS_VAR(value); phalcon_read_property_zval(&value, entity, attribute, PH_NOISY_CC); } else { PHALCON_INIT_VAR(value); } RETURN_CCTOR(value); } PHALCON_OBS_VAR(data); phalcon_read_property_this(&data, this_ptr, SL("_data"), PH_NOISY_CC); if (Z_TYPE_P(data) != IS_ARRAY) { if (Z_TYPE_P(data) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "There is no data to validate"); return; } } /** * Check if there is a calculated value */ PHALCON_OBS_VAR(values); phalcon_read_property_this(&values, this_ptr, SL("_values"), PH_NOISY_CC); if (phalcon_array_isset(values, attribute)) { PHALCON_OBS_NVAR(value); phalcon_array_fetch(&value, values, attribute, PH_NOISY); RETURN_CCTOR(value); } PHALCON_INIT_NVAR(value); if (Z_TYPE_P(data) == IS_ARRAY) { if (phalcon_array_isset(data, attribute)) { PHALCON_OBS_NVAR(value); phalcon_array_fetch(&value, data, attribute, PH_NOISY); } } else { if (Z_TYPE_P(data) == IS_OBJECT) { if (phalcon_isset_property_zval(data, attribute TSRMLS_CC)) { PHALCON_OBS_NVAR(value); phalcon_read_property_zval(&value, data, attribute, PH_NOISY_CC); } } } if (Z_TYPE_P(value) != IS_NULL) { PHALCON_OBS_VAR(filters); phalcon_read_property_this(&filters, this_ptr, SL("_filters"), PH_NOISY_CC); if (Z_TYPE_P(filters) == IS_ARRAY) { if (phalcon_array_isset(filters, attribute)) { PHALCON_OBS_VAR(field_filters); phalcon_array_fetch(&field_filters, filters, attribute, PH_NOISY); if (zend_is_true(field_filters)) { PHALCON_INIT_VAR(service_name); ZVAL_STRING(service_name, "filter", 1); PHALCON_INIT_VAR(dependency_injector); phalcon_call_method(dependency_injector, this_ptr, "getdi"); 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_validation_exception_ce, "A dependency injector is required to obtain the 'filter' service"); return; } } PHALCON_INIT_VAR(filter_service); phalcon_call_method_p1(filter_service, dependency_injector, "getshared", service_name); if (Z_TYPE_P(filter_service) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "Returned 'filter' service is invalid"); return; } PHALCON_VERIFY_INTERFACE(filter_service, phalcon_filterinterface_ce); phalcon_call_method_p2(return_value, filter_service, "sanitize", value, field_filters); RETURN_MM(); } } } /** * Cache the calculated value */ phalcon_update_property_array(this_ptr, SL("_values"), attribute, value TSRMLS_CC); RETURN_CCTOR(value); } RETURN_MM_NULL(); }
/** * Loads registered template engines, if none is registered it will use Phalcon\Mvc\View\Engine\Php * * @return array */ PHP_METHOD(Phalcon_Mvc_View_Simple, _loadTemplateEngines){ zval *engines = NULL, *dependency_injector, *registered_engines; zval *php_engine, *arguments, *engine_service = NULL; zval *extension = NULL, *engine_object = NULL, *exception_message = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); PHALCON_OBS_VAR(engines); phalcon_read_property_this(&engines, this_ptr, SL("_engines"), PH_NOISY TSRMLS_CC); /** * If the engines aren't initialized 'engines' is false */ if (PHALCON_IS_FALSE(engines)) { PHALCON_OBS_VAR(dependency_injector); phalcon_read_property_this(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY TSRMLS_CC); PHALCON_INIT_NVAR(engines); array_init(engines); PHALCON_OBS_VAR(registered_engines); phalcon_read_property_this(®istered_engines, this_ptr, SL("_registeredEngines"), PH_NOISY TSRMLS_CC); if (Z_TYPE_P(registered_engines) != IS_ARRAY) { /** * We use Phalcon\Mvc\View\Engine\Php as default */ PHALCON_INIT_VAR(php_engine); object_init_ex(php_engine, phalcon_mvc_view_engine_php_ce); PHALCON_CALL_METHOD(NULL, php_engine, "__construct", this_ptr, dependency_injector); /** * Use .phtml as extension for the PHP engine */ phalcon_array_update_string(&engines, SL(".phtml"), php_engine, PH_COPY | PH_SEPARATE); } else { if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_view_exception_ce, "A dependency injector container is required to obtain the application services"); return; } /** * Arguments for instantiated engines */ PHALCON_INIT_VAR(arguments); array_init_size(arguments, 2); phalcon_array_append(&arguments, this_ptr, PH_SEPARATE); phalcon_array_append(&arguments, dependency_injector, PH_SEPARATE); phalcon_is_iterable(registered_engines, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HKEY(extension, ah0, hp0); PHALCON_GET_HVALUE(engine_service); if (Z_TYPE_P(engine_service) == IS_OBJECT) { /** * Engine can be a closure */ if (instanceof_function(Z_OBJCE_P(engine_service), zend_ce_closure TSRMLS_CC)) { PHALCON_INIT_NVAR(engine_object);/**/ PHALCON_CALL_USER_FUNC_ARRAY(engine_object, engine_service, arguments); } else { PHALCON_CPY_WRT(engine_object, engine_service); } } else { /** * Engine can be a string representing a service in the DI */ if (Z_TYPE_P(engine_service) == IS_STRING) { PHALCON_CALL_METHOD(&engine_object, dependency_injector, "getshared", engine_service, arguments); PHALCON_VERIFY_INTERFACE(engine_object, phalcon_mvc_view_engineinterface_ce); } else { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SV(exception_message, "Invalid template engine registration for extension: ", extension); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_view_exception_ce, exception_message); return; } } phalcon_array_update_zval(&engines, extension, engine_object, PH_COPY | PH_SEPARATE); zend_hash_move_forward_ex(ah0, &hp0); } } phalcon_update_property_this(this_ptr, SL("_engines"), engines TSRMLS_CC); } else { PHALCON_OBS_NVAR(engines); phalcon_read_property_this(&engines, this_ptr, SL("_engines"), PH_NOISY TSRMLS_CC); } RETURN_CCTOR(engines); }
/** * Check if the CSRF token sent in the request is the same that the current in session * * @param string $tokenKey * @param string $tokenValue * @return boolean */ PHP_METHOD(Phalcon_Security, checkToken){ zval *token_key = NULL, *token_value = NULL, *dependency_injector; zval *service = NULL, *session, *key = NULL, *request, *token = NULL, *session_token; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 2, &token_key, &token_value); if (!token_key) { PHALCON_INIT_VAR(token_key); } else { PHALCON_SEPARATE_PARAM(token_key); } if (!token_value) { PHALCON_INIT_VAR(token_value); } 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); if (Z_TYPE_P(token_key) == IS_NULL) { PHALCON_INIT_VAR(key); ZVAL_STRING(key, "$PHALCON/CSRF/KEY$", 1); PHALCON_INIT_NVAR(token_key); phalcon_call_method_p1(token_key, session, "get", key); } if (Z_TYPE_P(token_value) == IS_NULL) { PHALCON_INIT_NVAR(service); ZVAL_STRING(service, "request", 1); PHALCON_INIT_VAR(request); phalcon_call_method_p1(request, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(request, phalcon_http_requestinterface_ce); /** * We always check if the value is correct in post */ PHALCON_INIT_VAR(token); phalcon_call_method_p1(token, request, "getpost", token_key); } else { PHALCON_CPY_WRT(token, token_value); } PHALCON_INIT_NVAR(key); ZVAL_STRING(key, "$PHALCON/CSRF$", 1); PHALCON_INIT_VAR(session_token); phalcon_call_method_p1(session_token, session, "get", key); /** * The value is the same? */ is_equal_function(return_value, token, session_token TSRMLS_CC); RETURN_MM(); }
/** * Sends the cookie to the HTTP client * Stores the cookie definition in session * * @return Phalcon\Http\Cookie */ PHP_METHOD(Phalcon_Http_Cookie, send){ zval *name, *value, *expire, *domain, *path, *secure; zval *http_only, *dependency_injector, *definition; zval *service = NULL, *session = NULL, *key, *encryption, *crypt = NULL; zval *encrypt_value = NULL, *has_session = NULL; PHALCON_MM_GROW(); name = phalcon_read_property(getThis(), SL("_name"), PH_NOISY); value = phalcon_read_property(getThis(), SL("_value"), PH_NOISY); expire = phalcon_read_property(getThis(), SL("_expire"), PH_NOISY); domain = phalcon_read_property(getThis(), SL("_domain"), PH_NOISY); path = phalcon_read_property(getThis(), SL("_path"), PH_NOISY); secure = phalcon_read_property(getThis(), SL("_secure"), PH_NOISY); http_only = phalcon_read_property(getThis(), SL("_httpOnly"), PH_NOISY); dependency_injector = phalcon_read_property(getThis(), SL("_dependencyInjector"), PH_NOISY); if (Z_TYPE_P(dependency_injector) == IS_OBJECT) { PHALCON_INIT_VAR(service); ZVAL_STR(service, IS(session)); PHALCON_CALL_METHOD(&has_session, dependency_injector, "has", service); if (zend_is_true(has_session)) { PHALCON_INIT_VAR(definition); array_init(definition); if (!PHALCON_IS_LONG(expire, 0)) { phalcon_array_update_str(definition, SL("expire"), expire, PH_COPY); } if (PHALCON_IS_NOT_EMPTY(path)) { phalcon_array_update_str(definition, SL("path"), path, PH_COPY); } if (PHALCON_IS_NOT_EMPTY(domain)) { phalcon_array_update_string(definition, IS(domain), domain, PH_COPY); } if (PHALCON_IS_NOT_EMPTY(secure)) { phalcon_array_update_str(definition, SL("secure"), secure, PH_COPY); } if (PHALCON_IS_NOT_EMPTY(http_only)) { phalcon_array_update_str(definition, SL("httpOnly"), http_only, PH_COPY); } /** * The definition is stored in session */ if (phalcon_fast_count_ev(definition)) { PHALCON_CALL_METHOD(&session, dependency_injector, "getshared", service); if (Z_TYPE_P(session) != IS_NULL) { PHALCON_VERIFY_INTERFACE(session, phalcon_session_adapterinterface_ce); PHALCON_INIT_VAR(key); PHALCON_CONCAT_SV(key, "_PHCOOKIE_", name); PHALCON_CALL_METHOD(NULL, session, "set", key, definition); } } } } encryption = phalcon_read_property(getThis(), SL("_useEncryption"), PH_NOISY); if (zend_is_true(encryption) && PHALCON_IS_NOT_EMPTY(value)) { if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_http_cookie_exception_ce, "A dependency injection object is required to access the 'filter' service"); return; } PHALCON_INIT_NVAR(service); ZVAL_STRING(service, "crypt"); PHALCON_CALL_METHOD(&crypt, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(crypt, phalcon_cryptinterface_ce); /** * Encrypt the value also coding it with base64 */ PHALCON_CALL_METHOD(&encrypt_value, crypt, "encryptbase64", value); } else { PHALCON_CPY_WRT(encrypt_value, value); } /** * Sets the cookie using the standard 'setcookie' function */ convert_to_string_ex(name); convert_to_string_ex(encrypt_value); convert_to_long_ex(expire); convert_to_string_ex(path); convert_to_string_ex(domain); convert_to_long_ex(secure); convert_to_long_ex(http_only); php_setcookie(Z_STR_P(name), Z_STR_P(encrypt_value), Z_LVAL_P(expire), Z_STR_P(path), Z_STR_P(domain), Z_LVAL_P(secure), 1, Z_LVAL_P(http_only)); RETURN_THIS(); }
/** * Produce the routing parameters from the rewrite information * * @param string $uri */ PHP_METHOD(Phalcon_Mvc_Router_Annotations, handle){ zval *uri = NULL, *real_uri = NULL, *processed, *annotations_service = NULL; zval *handlers, *controller_suffix, *scope = NULL, *prefix = NULL; zval *dependency_injector = NULL, *service = NULL, *handler = NULL; zval *controller_name = NULL; zval *namespace_name = NULL, *module_name = NULL, *suffixed = NULL; zval *handler_annotations = NULL, *class_annotations = NULL; zval *annotations = NULL, *annotation = NULL, *method_annotations = NULL; zval *collection = NULL, *method = NULL; HashTable *ah0, *ah1, *ah2, *ah3; HashPosition hp0, hp1, hp2, hp3; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 1, &uri); if (!uri) { uri = PHALCON_GLOBAL(z_null); } if (!zend_is_true(uri)) { /** * If 'uri' isn't passed as parameter it reads $_GET['_url'] */ PHALCON_CALL_METHOD(&real_uri, this_ptr, "getrewriteuri"); } else { PHALCON_CPY_WRT(real_uri, uri); } PHALCON_OBS_VAR(processed); phalcon_read_property_this(&processed, this_ptr, SL("_processed"), PH_NOISY TSRMLS_CC); if (!zend_is_true(processed)) { PHALCON_INIT_VAR(annotations_service); PHALCON_OBS_VAR(handlers); phalcon_read_property_this(&handlers, this_ptr, SL("_handlers"), PH_NOISY TSRMLS_CC); if (Z_TYPE_P(handlers) == IS_ARRAY) { PHALCON_OBS_VAR(controller_suffix); phalcon_read_property_this(&controller_suffix, this_ptr, SL("_controllerSuffix"), PH_NOISY TSRMLS_CC); phalcon_is_iterable(handlers, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(scope); if (Z_TYPE_P(scope) == IS_ARRAY) { /** * A prefix (if any) must be in position 0 */ PHALCON_OBS_NVAR(prefix); phalcon_array_fetch_long(&prefix, scope, 0, PH_NOISY); if (Z_TYPE_P(prefix) == IS_STRING) { if (!phalcon_start_with(real_uri, prefix, NULL)) { zend_hash_move_forward_ex(ah0, &hp0); continue; } } if (Z_TYPE_P(annotations_service) != IS_OBJECT) { PHALCON_OBS_NVAR(dependency_injector); phalcon_read_property_this(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY TSRMLS_CC); if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_router_exception_ce, "A dependency injection container is required to access the 'annotations' service"); return; } PHALCON_INIT_NVAR(service); ZVAL_STRING(service, "annotations", 1); PHALCON_CALL_METHOD(&annotations_service, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(annotations_service, phalcon_annotations_adapterinterface_ce); } /** * The controller must be in position 1 */ PHALCON_OBS_NVAR(handler); phalcon_array_fetch_long(&handler, scope, 1, PH_NOISY); if (phalcon_memnstr_str(handler, SL("\\"))) { /** * Extract the real class name from the namespaced class */ PHALCON_INIT_NVAR(controller_name); phalcon_get_class_ns(controller_name, handler, 0 TSRMLS_CC); /** * Extract the namespace from the namespaced class */ PHALCON_INIT_NVAR(namespace_name); phalcon_get_ns_class(namespace_name, handler, 0 TSRMLS_CC); } else { PHALCON_CPY_WRT(controller_name, handler); PHALCON_INIT_NVAR(namespace_name); } phalcon_update_property_null(this_ptr, SL("_routePrefix") TSRMLS_CC); /** * Check if the scope has a module associated */ if (phalcon_array_isset_long(scope, 2)) { PHALCON_OBS_NVAR(module_name); phalcon_array_fetch_long(&module_name, scope, 2, PH_NOISY); } else { PHALCON_INIT_NVAR(module_name); } PHALCON_INIT_NVAR(suffixed); PHALCON_CONCAT_VV(suffixed, handler, controller_suffix); /** * Get the annotations from the class */ PHALCON_CALL_METHOD(&handler_annotations, annotations_service, "get", suffixed); /** * Process class annotations */ PHALCON_CALL_METHOD(&class_annotations, handler_annotations, "getclassannotations"); if (Z_TYPE_P(class_annotations) == IS_OBJECT) { /** * Process class annotations */ PHALCON_CALL_METHOD(&annotations, class_annotations, "getannotations"); if (Z_TYPE_P(annotations) == IS_ARRAY) { phalcon_is_iterable(annotations, &ah1, &hp1, 0, 0); while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_HVALUE(annotation); PHALCON_CALL_METHOD(NULL, this_ptr, "processcontrollerannotation", controller_name, annotation); zend_hash_move_forward_ex(ah1, &hp1); } } } /** * Process method annotations */ PHALCON_CALL_METHOD(&method_annotations, handler_annotations, "getmethodsannotations"); if (Z_TYPE_P(method_annotations) == IS_ARRAY) { phalcon_is_iterable(method_annotations, &ah2, &hp2, 0, 0); while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) { PHALCON_GET_HKEY(method, ah2, hp2); PHALCON_GET_HVALUE(collection); if (Z_TYPE_P(collection) == IS_OBJECT) { PHALCON_CALL_METHOD(&annotations, collection, "getannotations"); phalcon_is_iterable(annotations, &ah3, &hp3, 0, 0); while (zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) == SUCCESS) { PHALCON_GET_HVALUE(annotation); PHALCON_CALL_METHOD(NULL, this_ptr, "processactionannotation", module_name, namespace_name, controller_name, method, annotation); zend_hash_move_forward_ex(ah3, &hp3); } } zend_hash_move_forward_ex(ah2, &hp2); } } } zend_hash_move_forward_ex(ah0, &hp0); } } phalcon_update_property_bool(this_ptr, SL("_processed"), 1 TSRMLS_CC); } /** * Call the parent handle method() */ PHALCON_CALL_PARENT(NULL, phalcon_mvc_router_annotations_ce, this_ptr, "handle", real_uri); PHALCON_MM_RESTORE(); }
/** * Builds a Phalcon\Mvc\Model\Criteria based on an input array like $_POST * * @param Phalcon\DiInterface $dependencyInjector * @param string $modelName * @param array $data * @return Phalcon\Mvc\Model\Criteria */ PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){ zval *dependency_injector, *model_name, *data; zval *conditions, *service, *meta_data = NULL, *model; zval *data_types = NULL, *bind, *value = NULL, *field = NULL, *type, *condition = NULL; zval *value_pattern = NULL, *join_conditions; zval *column_map = NULL; HashTable *ah0; HashPosition hp0; zval **hd; zend_class_entry *ce0; PHALCON_MM_GROW(); phalcon_fetch_params(1, 3, 0, &dependency_injector, &model_name, &data); if (Z_TYPE_P(data) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Input data must be an Array"); return; } if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A dependency injector container is required to obtain the ORM services"); return; } object_init_ex(return_value, phalcon_mvc_model_criteria_ce); if (zend_hash_num_elements(Z_ARRVAL_P(data))) { PHALCON_INIT_VAR(service); PHALCON_ZVAL_MAYBE_INTERNED_STRING(service, phalcon_interned_modelsMetadata); PHALCON_CALL_METHOD(&meta_data, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(meta_data, phalcon_mvc_model_metadatainterface_ce); ce0 = phalcon_fetch_class(model_name TSRMLS_CC); PHALCON_INIT_VAR(model); object_init_ex(model, ce0); if (phalcon_has_constructor(model TSRMLS_CC)) { PHALCON_CALL_METHOD(NULL, model, "__construct"); } PHALCON_VERIFY_INTERFACE_EX(model, phalcon_mvc_modelinterface_ce, phalcon_mvc_model_exception_ce, 1); if (PHALCON_GLOBAL(orm).column_renaming) { PHALCON_CALL_METHOD(&column_map, meta_data, "getreversecolumnmap", model); if (Z_TYPE_P(column_map) != IS_ARRAY) { PHALCON_INIT_NVAR(column_map); } } else { column_map = PHALCON_GLOBAL(z_null); } PHALCON_CALL_METHOD(&data_types, meta_data, "getdatatypes", model); PHALCON_INIT_VAR(bind); array_init(bind); PHALCON_INIT_VAR(conditions); array_init(conditions); /** * We look for attributes in the array passed as data */ phalcon_is_iterable(data, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { zval *real_field; PHALCON_GET_HKEY(field, ah0, hp0); PHALCON_GET_HVALUE(value); if (Z_TYPE_P(column_map) != IS_ARRAY || !phalcon_array_isset_fetch(&real_field, column_map, field)) { real_field = field; } if (phalcon_array_isset_fetch(&type, data_types, real_field)) { if (Z_TYPE_P(value) != IS_NULL && !PHALCON_IS_STRING(value, "")) { if (PHALCON_IS_LONG(type, 2)) { /** * For varchar types we use LIKE operator */ PHALCON_INIT_NVAR(condition); PHALCON_CONCAT_VSVS(condition, field, " LIKE :", field, ":"); PHALCON_INIT_NVAR(value_pattern); PHALCON_CONCAT_SVS(value_pattern, "%", value, "%"); phalcon_array_update_zval(&bind, field, value_pattern, PH_COPY); } else { /** * For the rest of data types we use a plain = operator */ PHALCON_INIT_NVAR(condition); PHALCON_CONCAT_VSVS(condition, field, "=:", field, ":"); phalcon_array_update_zval(&bind, field, value, PH_COPY); } phalcon_array_append(&conditions, condition, 0); } } zend_hash_move_forward_ex(ah0, &hp0); } if (zend_hash_num_elements(Z_ARRVAL_P(conditions))) { PHALCON_INIT_VAR(join_conditions); phalcon_fast_join_str(join_conditions, SL(" AND "), conditions TSRMLS_CC); PHALCON_CALL_METHOD(NULL, return_value, "where", join_conditions, bind); } } PHALCON_CALL_METHOD(NULL, return_value, "setmodelname", model_name); RETURN_MM(); }
/** * Returns the cookie's value * * @param string|array $filters * @param string $defaultValue * @return mixed */ PHP_METHOD(Phalcon_Http_Cookie, getValue){ zval *filters = NULL, *default_value = NULL, *restored, *dependency_injector = NULL; zval *readed, *name, *_COOKIE, *value = NULL, *encryption; zval *service = NULL, *crypt = NULL, *decrypted_value = NULL, *filter = NULL; PHALCON_MM_GROW(); phalcon_fetch_params(0, 0, 2, &filters, &default_value); if (!filters) { filters = &PHALCON_GLOBAL(z_null); } if (!default_value) { default_value = &PHALCON_GLOBAL(z_null); } restored = phalcon_read_property(getThis(), SL("_restored"), PH_NOISY); if (!zend_is_true(restored)) { PHALCON_CALL_METHOD(NULL, getThis(), "restore"); } PHALCON_INIT_VAR(dependency_injector); readed = phalcon_read_property(getThis(), SL("_readed"), PH_NOISY); if (PHALCON_IS_FALSE(readed)) { name = phalcon_read_property(getThis(), SL("_name"), PH_NOISY); _COOKIE = phalcon_get_global(SL("_COOKIE")); if (phalcon_array_isset_fetch(&value, _COOKIE, name)) { encryption = phalcon_read_property(getThis(), SL("_useEncryption"), PH_NOISY); if (zend_is_true(encryption)) { dependency_injector = phalcon_read_property(getThis(), SL("_dependencyInjector"), PH_NOISY); if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_http_cookie_exception_ce, "A dependency injection object is required to access the 'filter' service"); return; } PHALCON_INIT_VAR(service); ZVAL_STRING(service, "crypt"); PHALCON_CALL_METHOD(&crypt, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(crypt, phalcon_cryptinterface_ce); /** * Decrypt the value also decoding it with base64 */ PHALCON_CALL_METHOD(&decrypted_value, crypt, "decryptbase64", value); } else { PHALCON_CPY_WRT(decrypted_value, value); } /** * Update the decrypted value */ phalcon_update_property_this(getThis(), SL("_value"), decrypted_value); if (Z_TYPE_P(filters) != IS_NULL) { filter = phalcon_read_property(getThis(), SL("_filter"), PH_NOISY); if (Z_TYPE_P(filter) != IS_OBJECT) { if (Z_TYPE_P(dependency_injector) == IS_NULL) { dependency_injector = phalcon_read_property(getThis(), SL("_dependencyInjector"), PH_NOISY); PHALCON_VERIFY_INTERFACE_EX(dependency_injector, phalcon_diinterface_ce, phalcon_http_cookie_exception_ce, 1); } PHALCON_INIT_NVAR(service); ZVAL_STR(service, IS(filter)); PHALCON_CALL_METHOD(&filter, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(filter, phalcon_filterinterface_ce); phalcon_update_property_this(getThis(), SL("_filter"), filter); } PHALCON_RETURN_CALL_METHOD(filter, "sanitize", decrypted_value, filters); RETURN_MM(); } /** * Return the value without filtering */ RETURN_CTOR(decrypted_value); } RETURN_CTOR(default_value); } value = phalcon_read_property(getThis(), SL("_value"), PH_NOISY); RETURN_CTOR(value); }
/** * Sets a cookie to be sent at the end of the request * This method overrides any cookie set before with the same name * * @param string $name * @param mixed $value * @param int $expire * @param string $path * @param boolean $secure * @param string $domain * @param boolean $httpOnly * @return Phalcon\Http\Response\Cookies */ PHP_METHOD(Phalcon_Http_Response_Cookies, set){ zval *name, *value = NULL, *expire = NULL, *path = NULL, *secure = NULL, *domain = NULL; zval *http_only = NULL, *cookies, *encryption, *dependency_injector = NULL; zval *cookie = NULL, *registered, *service, *response = NULL; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 6, &name, &value, &expire, &path, &secure, &domain, &http_only); if (Z_TYPE_P(name) != IS_STRING) { PHALCON_THROW_EXCEPTION_STR(phalcon_http_cookie_exception_ce, "The cookie name must be string"); return; } if (!value) { value = PHALCON_GLOBAL(z_null); } if (!expire) { PHALCON_OBS_VAR(expire); phalcon_read_property_this(&expire, this_ptr, SL("_expire"), PH_NOISY TSRMLS_CC); } if (!path) { PHALCON_OBS_VAR(path); phalcon_read_property_this(&path, this_ptr, SL("_path"), PH_NOISY TSRMLS_CC); } if (!secure) { PHALCON_OBS_VAR(secure); phalcon_read_property_this(&secure, this_ptr, SL("_secure"), PH_NOISY TSRMLS_CC); } if (!domain) { PHALCON_OBS_VAR(domain); phalcon_read_property_this(&domain, this_ptr, SL("_domain"), PH_NOISY TSRMLS_CC); } if (!http_only) { PHALCON_OBS_VAR(http_only); phalcon_read_property_this(&http_only, this_ptr, SL("_httpOnly"), PH_NOISY TSRMLS_CC); } PHALCON_OBS_VAR(cookies); phalcon_read_property_this(&cookies, this_ptr, SL("_cookies"), PH_NOISY TSRMLS_CC); PHALCON_OBS_VAR(encryption); phalcon_read_property_this(&encryption, this_ptr, SL("_useEncryption"), PH_NOISY TSRMLS_CC); PHALCON_CALL_METHOD(&dependency_injector, this_ptr, "getdi"); /** * Check if the cookie needs to be updated or */ if (!phalcon_array_isset(cookies, name)) { PHALCON_INIT_VAR(cookie); object_init_ex(cookie, phalcon_http_cookie_ce); PHALCON_CALL_METHOD(NULL, cookie, "__construct", name, value, expire, path, secure, domain, http_only); /** * Pass the DI to created cookies */ PHALCON_CALL_METHOD(NULL, cookie, "setdi", dependency_injector); /** * Enable encryption in the cookie */ if (zend_is_true(encryption)) { PHALCON_CALL_METHOD(NULL, cookie, "useencryption", encryption); } phalcon_update_property_array(this_ptr, SL("_cookies"), name, cookie TSRMLS_CC); } else { PHALCON_OBS_NVAR(cookie); phalcon_array_fetch(&cookie, cookies, name, PH_NOISY); /** * Override any settings in the cookie */ PHALCON_CALL_METHOD(NULL, cookie, "setvalue", value); PHALCON_CALL_METHOD(NULL, cookie, "setexpiration", expire); PHALCON_CALL_METHOD(NULL, cookie, "setpath", path); PHALCON_CALL_METHOD(NULL, cookie, "setsecure", secure); PHALCON_CALL_METHOD(NULL, cookie, "setdomain", domain); PHALCON_CALL_METHOD(NULL, cookie, "sethttponly", http_only); } /** * Register the cookies bag in the response */ PHALCON_OBS_VAR(registered); phalcon_read_property_this(®istered, this_ptr, SL("_registered"), PH_NOISY TSRMLS_CC); if (PHALCON_IS_FALSE(registered)) { PHALCON_INIT_VAR(service); PHALCON_ZVAL_MAYBE_INTERNED_STRING(service, phalcon_interned_response); PHALCON_CALL_METHOD(&response, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(response, phalcon_http_responseinterface_ce); /** * Pass the cookies bag to the response so it can send the headers at the of the * request */ PHALCON_CALL_METHOD(NULL, response, "setcookies", this_ptr); } RETURN_THIS(); }
/** * 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; zval *session = NULL, *name, *key, *definition = NULL, *expire, *domain; zval *path, *secure, *http_only; PHALCON_MM_GROW(); 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) { PHALCON_INIT_VAR(service); ZVAL_STR(service, IS(session)); PHALCON_CALL_METHOD(&session, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(session, phalcon_session_adapterinterface_ce); name = phalcon_read_property(getThis(), SL("_name"), PH_NOISY); PHALCON_INIT_VAR(key); PHALCON_CONCAT_SV(key, "_PHCOOKIE_", name); PHALCON_CALL_METHOD(&definition, session, "get", key); if (Z_TYPE_P(definition) == IS_ARRAY) { if (phalcon_array_isset_str(definition, SL("expire"))) { PHALCON_OBS_VAR(expire); phalcon_array_fetch_str(&expire, definition, SL("expire"), PH_NOISY); phalcon_update_property_this(getThis(), SL("_expire"), expire); } if (phalcon_array_isset_str(definition, SL("domain"))) { PHALCON_OBS_VAR(domain); phalcon_array_fetch_str(&domain, definition, SL("domain"), PH_NOISY); phalcon_update_property_this(getThis(), SL("_domain"), domain); } if (phalcon_array_isset_str(definition, SL("path"))) { PHALCON_OBS_VAR(path); phalcon_array_fetch_str(&path, definition, SL("path"), PH_NOISY); phalcon_update_property_this(getThis(), SL("_path"), path); } if (phalcon_array_isset_str(definition, SL("secure"))) { PHALCON_OBS_VAR(secure); phalcon_array_fetch_str(&secure, definition, SL("secure"), PH_NOISY); phalcon_update_property_this(getThis(), SL("_secure"), secure); } if (phalcon_array_isset_str(definition, SL("httpOnly"))) { PHALCON_OBS_VAR(http_only); phalcon_array_fetch_str(&http_only, definition, SL("httpOnly"), PH_NOISY); phalcon_update_property_this(getThis(), SL("_httpOnly"), http_only); } } } phalcon_update_property_bool(getThis(), SL("_restored"), 1); } RETURN_THIS(); }
/** * Generates a URL * *<code> * * //Generate a URL appending the URI to the base URI * echo $url->get('products/edit/1'); * * //Generate a URL for a predefined route * echo $url->get(array('for' => 'blog-post', 'title' => 'some-cool-stuff', 'year' => '2012')); * echo $url->get(array('for' => 'blog-post', 'hostname' => true, 'title' => 'some-cool-stuff', 'year' => '2012')); * *</code> * * @param string|array $uri * @param array|object args Optional arguments to be appended to the query string * @param bool|null $local * @return string */ PHP_METHOD(Phalcon_Mvc_Url, get){ zval *uri = NULL, *args = NULL, *local = NULL, *base_uri = NULL, *router = NULL, *dependency_injector; zval *service, *route_name, *hostname, *route = NULL, *exception_message; zval *pattern = NULL, *paths = NULL, *processed_uri = NULL, *query_string; zval *matched, *regexp; zval *generator = NULL, *arguments; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 3, &uri, &args, &local); if (!uri) { uri = &PHALCON_GLOBAL(z_null); } if (!args) { args = &PHALCON_GLOBAL(z_null); } if (!local) { local = &PHALCON_GLOBAL(z_null); } else { PHALCON_SEPARATE_PARAM(local); } PHALCON_CALL_METHOD(&base_uri, getThis(), "getbaseuri"); if (Z_TYPE_P(uri) == IS_STRING) { if (strstr(Z_STRVAL_P(uri), ":")) { PHALCON_INIT_VAR(matched); PHALCON_INIT_VAR(regexp); ZVAL_STRING(regexp, "/^[^:\\/?#]++:/"); RETURN_MM_ON_FAILURE(phalcon_preg_match(matched, regexp, uri, NULL)); if (zend_is_true(matched)) { PHALCON_INIT_NVAR(local); ZVAL_FALSE(local); } } if (Z_TYPE_P(local) == IS_NULL || zend_is_true(local)) { PHALCON_CONCAT_VV(return_value, base_uri, uri); } else { ZVAL_ZVAL(return_value, uri, 1, 0); } } else if (Z_TYPE_P(uri) == IS_ARRAY) { if (!phalcon_array_isset_str_fetch(&route_name, uri, SL("for"))) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_url_exception_ce, "It's necessary to define the route name with the parameter \"for\""); return; } router = phalcon_read_property(getThis(), SL("_router"), PH_NOISY); /** * Check if the router has not previously set */ if (Z_TYPE_P(router) != IS_OBJECT) { dependency_injector = phalcon_read_property(getThis(), SL("_dependencyInjector"), PH_NOISY); if (!zend_is_true(dependency_injector)) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_url_exception_ce, "A dependency injector container is required to obtain the \"url\" service"); return; } PHALCON_INIT_VAR(service); ZVAL_STR(service, IS(router)); router = NULL; PHALCON_CALL_METHOD(&router, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(router, phalcon_mvc_routerinterface_ce); phalcon_update_property_this(getThis(), SL("_router"), router); } /** * Every route is uniquely identified by a name */ PHALCON_CALL_METHOD(&route, router, "getroutebyname", route_name); if (Z_TYPE_P(route) != IS_OBJECT) { PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Cannot obtain a route using the name \"", route_name, "\""); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_url_exception_ce, exception_message); return; } PHALCON_CALL_METHOD(&pattern, route, "getpattern"); /** * Return the reversed paths */ PHALCON_CALL_METHOD(&paths, route, "getreversedpaths"); /** * Return the Url Generator */ PHALCON_CALL_METHOD(&generator, route, "geturlgenerator"); if (phalcon_is_callable(generator) || (Z_TYPE_P(generator) == IS_OBJECT && instanceof_function(Z_OBJCE_P(generator), zend_ce_closure))) { PHALCON_INIT_VAR(arguments); array_init_size(arguments, 3); phalcon_array_append(arguments, base_uri, PH_COPY); phalcon_array_append(arguments, paths, PH_COPY); phalcon_array_append(arguments, uri, PH_COPY); PHALCON_CALL_USER_FUNC_ARRAY(&return_value, generator, arguments); } else { /** * Replace the patterns by its variables */ PHALCON_INIT_NVAR(processed_uri); phalcon_replace_paths(processed_uri, pattern, paths, uri); PHALCON_CONCAT_VV(return_value, base_uri, processed_uri); if (phalcon_array_isset_str_fetch(&hostname, uri, SL("hostname"))) { if (zend_is_true(hostname)) { PHALCON_CALL_METHOD(&hostname, route, "gethostname"); PHALCON_INIT_NVAR(processed_uri); PHALCON_CONCAT_VV(processed_uri, hostname, return_value); ZVAL_ZVAL(return_value, processed_uri, 1, 0); } } } } if (zend_is_true(args)) { PHALCON_INIT_VAR(query_string); phalcon_http_build_query(query_string, args, "&"); if (Z_TYPE_P(query_string) == IS_STRING && Z_STRLEN_P(query_string)) { if (phalcon_memnstr_str(return_value, "?", 1)) { PHALCON_SCONCAT_SV(return_value, "&", query_string); } else { PHALCON_SCONCAT_SV(return_value, "?", query_string); } } } RETURN_MM(); }
/** * 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; static const char* redirect_phrases[] = { /* 300 */ "Multiple Choices", /* 301 */ "Moved Permanently", /* 302 */ "Found", /* 303 */ "See Other", /* 304 */ "Not Modified", /* 305 */ "Use Proxy", /* 306 */ "Switch Proxy", /* 307 */ "Temporary Redirect", /* 308 */ "Permanent Redirect" }; 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); } else if (unlikely(Z_TYPE_P(status_code) != IS_LONG)) { PHALCON_SEPARATE_PARAM(status_code); convert_to_long(status_code); } 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_VERIFY_INTERFACE(url, phalcon_mvc_urlinterface_ce); 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); if (Z_LVAL_P(status_code) < 300 || Z_LVAL_P(status_code) > 308) { ZVAL_STRING(status_text, "Redirect", 1); } else { ZVAL_STRING(status_text, redirect_phrases[Z_LVAL_P(status_code) - 300], 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(); }
/** * Generates a URL * *<code> * * //Generate a URL appending the URI to the base URI * echo $url->get('products/edit/1'); * * //Generate a URL for a predefined route * echo $url->get(array('for' => 'blog-post', 'title' => 'some-cool-stuff', 'year' => '2012')); * *</code> * * @param string|array $uri * @param array|object args Optional arguments to be appended to the query string * @param bool|null $local * @return string */ PHP_METHOD(Phalcon_Mvc_Url, get){ zval **uri = NULL, *base_uri = NULL, *router = NULL, *dependency_injector; zval *service, *route_name, *route = NULL, *exception_message; zval *pattern = NULL, *paths = NULL, *processed_uri, **args = NULL, *query_string; zval *matched, *regexp; zval **z_local = NULL; int local = 1; phalcon_fetch_params_ex(0, 3, &uri, &args, &z_local); PHALCON_MM_GROW(); if (!uri) { uri = &PHALCON_GLOBAL(z_null); } else if (z_local && Z_TYPE_PP(z_local) != IS_NULL) { if (!zend_is_true(*z_local)) { local = 0; } } else if (Z_TYPE_PP(uri) == IS_STRING && strstr(Z_STRVAL_PP(uri), ":")) { PHALCON_INIT_VAR(matched); PHALCON_INIT_VAR(regexp); ZVAL_STRING(regexp, "/^[^:\\/?#]++:/", 1); RETURN_MM_ON_FAILURE(phalcon_preg_match(matched, regexp, *uri, NULL TSRMLS_CC)); if (zend_is_true(matched)) { local = 0; } } PHALCON_CALL_METHOD(&base_uri, this_ptr, "getbaseuri"); if (Z_TYPE_PP(uri) == IS_ARRAY) { if (!phalcon_array_isset_string_fetch(&route_name, *uri, SS("for"))) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_url_exception_ce, "It's necessary to define the route name with the parameter \"for\""); return; } router = phalcon_fetch_nproperty_this(this_ptr, SL("_router"), PH_NOISY TSRMLS_CC); /** * Check if the router has not previously set */ if (Z_TYPE_P(router) != IS_OBJECT) { dependency_injector = phalcon_fetch_nproperty_this(this_ptr, SL("_dependencyInjector"), PH_NOISY TSRMLS_CC); if (!zend_is_true(dependency_injector)) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_url_exception_ce, "A dependency injector container is required to obtain the \"url\" service"); return; } PHALCON_INIT_VAR(service); PHALCON_ZVAL_MAYBE_INTERNED_STRING(service, phalcon_interned_router); router = NULL; PHALCON_CALL_METHOD(&router, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(router, phalcon_mvc_routerinterface_ce); phalcon_update_property_this(this_ptr, SL("_router"), router TSRMLS_CC); } /** * Every route is uniquely identified by a name */ PHALCON_CALL_METHOD(&route, router, "getroutebyname", route_name); if (Z_TYPE_P(route) != IS_OBJECT) { PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Cannot obtain a route using the name \"", route_name, "\""); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_url_exception_ce, exception_message); return; } PHALCON_CALL_METHOD(&pattern, route, "getpattern"); /** * Return the reversed paths */ PHALCON_CALL_METHOD(&paths, route, "getreversedpaths"); /** * Replace the patterns by its variables */ PHALCON_INIT_VAR(processed_uri); phalcon_replace_paths(processed_uri, pattern, paths, *uri TSRMLS_CC); PHALCON_CONCAT_VV(return_value, base_uri, processed_uri); } else { if (local) { PHALCON_CONCAT_VV(return_value, base_uri, *uri); } else { ZVAL_ZVAL(return_value, *uri, 1, 0); } } if (args) { PHALCON_INIT_VAR(query_string); phalcon_http_build_query(query_string, *args, "&" TSRMLS_CC); if (Z_TYPE_P(query_string) == IS_STRING && Z_STRLEN_P(query_string)) { if (phalcon_memnstr_str(return_value, "?", 1)) { PHALCON_SCONCAT_SV(return_value, "&", query_string); } else { PHALCON_SCONCAT_SV(return_value, "?", query_string); } } } RETURN_MM(); }