/** * Gets most possible client IPv4 Address. This method search in $_SERVER['REMOTE_ADDR'] and optionally in $_SERVER['HTTP_X_FORWARDED_FOR'] * * @param boolean $trustForwardedHeader * @return string */ PHP_METHOD(Phalcon_Http_Request, getClientAddress){ zval *trust_forwarded_header = NULL, *address = NULL, *_SERVER; zval *addresses, *first; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 1, &trust_forwarded_header); if (!trust_forwarded_header) { PHALCON_INIT_VAR(trust_forwarded_header); ZVAL_BOOL(trust_forwarded_header, 0); } PHALCON_INIT_VAR(address); /** * Proxies uses this IP */ phalcon_get_global(&_SERVER, SS("_SERVER") TSRMLS_CC); if (phalcon_array_isset_string(_SERVER, SS("HTTP_X_FORWARDED_FOR"))) { if (zend_is_true(trust_forwarded_header)) { PHALCON_OBS_NVAR(address); phalcon_array_fetch_string(&address, _SERVER, SL("HTTP_X_FORWARDED_FOR"), PH_NOISY); } } if (Z_TYPE_P(address) == IS_NULL) { if (phalcon_array_isset_string(_SERVER, SS("REMOTE_ADDR"))) { PHALCON_OBS_NVAR(address); phalcon_array_fetch_string(&address, _SERVER, SL("REMOTE_ADDR"), PH_NOISY); } } if (Z_TYPE_P(address) == IS_STRING) { if (phalcon_memnstr_str(address, SL(","))) { /** * The client address has multiples parts, only return the first part */ PHALCON_INIT_VAR(addresses); phalcon_fast_explode_str(addresses, SL(","), address); PHALCON_OBS_VAR(first); phalcon_array_fetch_long(&first, addresses, 0, PH_NOISY); RETURN_CCTOR(first); } RETURN_CCTOR(address); } RETURN_MM_FALSE; }
/** * Returns the major framework's version * * @return string */ PHP_METHOD(Phalcon_Debug, getMajorVersion){ zval *version = NULL, *parts, *major; PHALCON_MM_GROW(); PHALCON_CALL_CE_STATIC(&version, phalcon_version_ce, "get"); PHALCON_INIT_VAR(parts); phalcon_fast_explode_str(parts, SL(" "), version); PHALCON_OBS_VAR(major); phalcon_array_fetch_long(&major, parts, 0, PH_NOISY); RETURN_CCTOR(major); }
/** * Returns the major framework's version * * @return string */ PHP_METHOD(Phalcon_Debug, getMajorVersion){ zval *version, *parts, *major; PHALCON_MM_GROW(); PHALCON_INIT_VAR(version); PHALCON_CALL_STATIC(version, "phalcon\\version", "get"); PHALCON_INIT_VAR(parts); phalcon_fast_explode_str(parts, SL(" "), version TSRMLS_CC); PHALCON_OBS_VAR(major); phalcon_array_fetch_long(&major, parts, 0, PH_NOISY_CC); RETURN_CCTOR(major); }
/** * Gets most possible client IPv4 Address. This method search in $_SERVER['REMOTE_ADDR'] and optionally in $_SERVER['HTTP_X_FORWARDED_FOR'] * * @param boolean $trustForwardedHeader * @return string */ PHP_METHOD(Phalcon_Http_Request, getClientAddress){ zval *trust_forwarded_header = NULL, *_SERVER, address = {}, addresses = {}, first = {}; phalcon_fetch_params(0, 0, 1, &trust_forwarded_header); if (!trust_forwarded_header) { trust_forwarded_header = &PHALCON_GLOBAL(z_false); } _SERVER = phalcon_get_global_str(SL("_SERVER")); /** * Proxies use this IP */ if (zend_is_true(trust_forwarded_header)) { if (!phalcon_array_isset_fetch_str(&address, _SERVER, SL("HTTP_X_FORWARDED_FOR"))) { if (!phalcon_array_isset_fetch_str(&address, _SERVER, SL("REMOTE_ADDR"))) { phalcon_array_fetch_str(&address, _SERVER, SL("REMOTE_ADDR"), PH_NOISY); } } } else if (!phalcon_array_isset_fetch_str(&address, _SERVER, SL("REMOTE_ADDR"))) { phalcon_array_fetch_str(&address, _SERVER, SL("REMOTE_ADDR"), PH_NOISY); } if (Z_TYPE(address) == IS_STRING) { if (phalcon_memnstr_str(&address, SL(","))) { /** * The client address has multiples parts, only return the first part */ phalcon_fast_explode_str(&addresses, SL(","), &address); phalcon_array_fetch_long(&first, &addresses, 0, PH_NOISY); RETURN_CTORW(&first); } RETURN_CTORW(&address); } RETURN_FALSE; }
/** * Load config file * * @param string $filePath */ PHP_METHOD(Phalcon_Config_Adapter_Ini, read){ zval *file_path, *absolute_path = NULL, *scanner_mode = NULL, config_dir_path = {}, base_path = {}, ini_config = {}, config = {}, *directives; zend_string *str_key; ulong idx; phalcon_fetch_params(0, 1, 2, &file_path, &absolute_path, &scanner_mode); PHALCON_ENSURE_IS_STRING(file_path); if (!absolute_path) { absolute_path = &PHALCON_GLOBAL(z_false); } if (zend_is_true(absolute_path)) { PHALCON_CPY_WRT_CTOR(&config_dir_path, file_path); } else { phalcon_return_static_property_ce(&base_path, phalcon_config_adapter_ce, SL("_basePath")); PHALCON_CONCAT_VV(&config_dir_path, &base_path, file_path); } /** * Use the standard parse_ini_file */ if (scanner_mode && Z_TYPE_P(scanner_mode) == IS_LONG) { PHALCON_CALL_FUNCTIONW(&ini_config, "parse_ini_file", &config_dir_path, &PHALCON_GLOBAL(z_true), scanner_mode); } else { PHALCON_CALL_FUNCTIONW(&ini_config, "parse_ini_file", &config_dir_path, &PHALCON_GLOBAL(z_true)); } /** * Check if the file had errors */ if (Z_TYPE(ini_config) != IS_ARRAY) { zend_throw_exception_ex(phalcon_config_exception_ce, 0, "Configuration file '%s' cannot be read", Z_STRVAL(config_dir_path)); return; } array_init(&config); ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(ini_config), idx, str_key, directives) { zval section = {}, *value; if (str_key) { ZVAL_STR(§ion, str_key); } else { ZVAL_LONG(§ion, idx); } if (unlikely(Z_TYPE_P(directives) != IS_ARRAY) || zend_hash_num_elements(Z_ARRVAL_P(directives)) == 0) { phalcon_array_update_zval(&config, §ion, directives, PH_COPY); } else { ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(directives), idx, str_key, value) { zval key = {}, directive_parts = {}; if (str_key) { ZVAL_STR(&key, str_key); } else { ZVAL_LONG(&key, idx); } if (str_key && memchr(Z_STRVAL(key), '.', Z_STRLEN(key))) { phalcon_fast_explode_str(&directive_parts, SL("."), &key); phalcon_config_adapter_ini_update_zval_directive(&config, §ion, &directive_parts, value); } else { phalcon_array_update_multi_2(&config, §ion, &key, value, PH_COPY); } } ZEND_HASH_FOREACH_END(); }
/** * Fires an event in the events manager causing that active listeners be notified about it * *<code> * $eventsManager->fire('db', $connection); *</code> * * @param string $eventType * @param object $source * @param mixed $data * @param int $cancelable * @return mixed */ PHP_METHOD(Phalcon_Events_Manager, fire){ zval *event_type, *source, *data = NULL, *cancelable = NULL, *events; zval *exception_message, *event_parts, *type; zval *event_name, *status = NULL, *collect, *event = NULL, *fire_events = NULL; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 2, &event_type, &source, &data, &cancelable); if (!data) { PHALCON_INIT_VAR(data); } if (!cancelable) { PHALCON_INIT_VAR(cancelable); ZVAL_BOOL(cancelable, 1); } if (unlikely(Z_TYPE_P(event_type) != IS_STRING)) { PHALCON_THROW_EXCEPTION_STR(phalcon_events_exception_ce, "Event type must be a string"); return; } PHALCON_OBS_VAR(events); phalcon_read_property_this(&events, this_ptr, SL("_events"), PH_NOISY_CC); if (Z_TYPE_P(events) != IS_ARRAY) { RETURN_MM_NULL(); } /** * All valid events must have a colon separator */ if (!phalcon_memnstr_str(event_type, SL(":"))) { PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SV(exception_message, "Invalid event type ", event_type); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_events_exception_ce, exception_message); return; } PHALCON_INIT_VAR(event_parts); phalcon_fast_explode_str(event_parts, SL(":"), event_type); PHALCON_OBS_VAR(type); phalcon_array_fetch_long(&type, event_parts, 0, PH_NOISY); PHALCON_OBS_VAR(event_name); phalcon_array_fetch_long(&event_name, event_parts, 1, PH_NOISY); PHALCON_INIT_VAR(status); /** * Responses must be traced? */ PHALCON_OBS_VAR(collect); phalcon_read_property_this(&collect, this_ptr, SL("_collect"), PH_NOISY_CC); if (zend_is_true(collect)) { phalcon_update_property_null(this_ptr, SL("_responses") TSRMLS_CC); } PHALCON_INIT_VAR(event); /** * Check if events are grouped by type */ if (phalcon_array_isset(events, type)) { PHALCON_OBS_VAR(fire_events); phalcon_array_fetch(&fire_events, events, type, PH_NOISY); if (Z_TYPE_P(fire_events) == IS_ARRAY || Z_TYPE_P(fire_events) == IS_OBJECT) { /** * Create the event context */ object_init_ex(event, phalcon_events_event_ce); phalcon_call_method_p4_noret(event, "__construct", event_name, source, data, cancelable); /** * Call the events queue */ phalcon_call_method_p2(status, this_ptr, "firequeue", fire_events, event); } } /** * Check if there are listeners for the event type itself */ if (phalcon_array_isset(events, event_type)) { PHALCON_OBS_NVAR(fire_events); phalcon_array_fetch(&fire_events, events, event_type, PH_NOISY); if (Z_TYPE_P(fire_events) == IS_ARRAY || Z_TYPE_P(fire_events) == IS_OBJECT) { /** * Create the event if it wasn't created before */ if (Z_TYPE_P(event) == IS_NULL) { PHALCON_INIT_NVAR(event); object_init_ex(event, phalcon_events_event_ce); phalcon_call_method_p4_noret(event, "__construct", event_name, source, data, cancelable); } /** * Call the events queue */ PHALCON_INIT_NVAR(status); phalcon_call_method_p2(status, this_ptr, "firequeue", fire_events, event); } } RETURN_CCTOR(status); }
/** * Phalcon\Config\Adapter\Ini constructor * * @param string $filePath */ PHP_METHOD(Phalcon_Config_Adapter_Ini, __construct){ zval *file_path, *process_sections, *ini_config; zval *exception_message, *config, *directives = NULL; zval *section = NULL, *value = NULL, *key = NULL, *directive_parts = NULL, *left_part = NULL; zval *right_part = NULL; HashTable *ah0, *ah1; HashPosition hp0, hp1; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &file_path); PHALCON_INIT_VAR(process_sections); ZVAL_BOOL(process_sections, 1); /** * Use the standard parse_ini_file */ PHALCON_INIT_VAR(ini_config); phalcon_call_func_p2(ini_config, "parse_ini_file", file_path, process_sections); /** * Check if the file had errors */ if (PHALCON_IS_FALSE(ini_config)) { PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Configuration file ", file_path, " can't be loaded"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_config_exception_ce, exception_message); return; } PHALCON_INIT_VAR(config); array_init(config); phalcon_is_iterable(ini_config, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HKEY(section, ah0, hp0); PHALCON_GET_HVALUE(directives); if (unlikely(Z_TYPE_P(directives) != IS_ARRAY)) { Z_ADDREF_P(directives); if (phalcon_array_update_zval(&config, section, &directives, 0 TSRMLS_CC) != SUCCESS) { Z_DELREF_P(directives); } zend_hash_move_forward_ex(ah0, &hp0); continue; } phalcon_is_iterable(directives, &ah1, &hp1, 0, 0); if (zend_hash_num_elements(ah1) == 0) { phalcon_array_update_zval(&config, section, &directives, 0 TSRMLS_CC); zend_hash_move_forward_ex(ah0, &hp0); continue; } while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_HKEY(key, ah1, hp1); PHALCON_GET_HVALUE(value); if (phalcon_memnstr_str(key, SL(".") TSRMLS_CC)) { PHALCON_INIT_NVAR(directive_parts); phalcon_fast_explode_str(directive_parts, SL("."), key TSRMLS_CC); PHALCON_OBS_NVAR(left_part); phalcon_array_fetch_long(&left_part, directive_parts, 0, PH_NOISY_CC); PHALCON_OBS_NVAR(right_part); phalcon_array_fetch_long(&right_part, directive_parts, 1, PH_NOISY_CC); phalcon_array_update_zval_zval_zval_multi_3(&config, section, left_part, right_part, &value, 0 TSRMLS_CC); } else { phalcon_array_update_multi_2(&config, section, key, &value, 0 TSRMLS_CC); } zend_hash_move_forward_ex(ah1, &hp1); } zend_hash_move_forward_ex(ah0, &hp0); } /** * Calls the Phalcon\Config constructor */ PHALCON_CALL_PARENT_PARAMS_1_NORETURN(this_ptr, "Phalcon\\Config\\Adapter\\Ini", "__construct", config); PHALCON_MM_RESTORE(); }
PHP_METHOD(Phalcon_Http_Client_Header, parse){ zval *content, *content_parts = NULL, *key = NULL, *header = NULL, *header_parts = NULL, *name = NULL, *value = NULL, *trimmed = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &content); if (PHALCON_IS_EMPTY(content)) { RETURN_MM_FALSE; } if (Z_TYPE_P(content) == IS_STRING) { PHALCON_INIT_VAR(content_parts); phalcon_fast_explode_str(content_parts, SL("\r\n"), content); } else if (Z_TYPE_P(content) == IS_ARRAY) { PHALCON_CPY_WRT_CTOR(content_parts, content); } else { RETURN_MM_FALSE; } phalcon_is_iterable(content_parts, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HKEY(key, ah0, hp0); PHALCON_GET_HVALUE(header); if (Z_TYPE_P(header) == IS_STRING) { PHALCON_INIT_NVAR(header_parts); if (phalcon_memnstr_str(header , SL(":"))) { phalcon_fast_explode_str(header_parts, SL(":"), header); } else { if (phalcon_start_with_str(header , SL("HTTP/"))) { phalcon_fast_explode_str(header_parts, SL(" "), header); if (Z_TYPE_P(header_parts) == IS_ARRAY && phalcon_array_isset_long(header_parts, 1) && phalcon_array_isset_long(header_parts, 2)) { PHALCON_OBS_NVAR(value); phalcon_array_fetch_long(&value, header_parts, 1, PH_NOISY); phalcon_update_property_this(this_ptr, SL("_status_code"), value TSRMLS_CC); PHALCON_OBS_NVAR(value); phalcon_array_fetch_long(&value, header_parts, 2, PH_NOISY); phalcon_update_property_this(this_ptr, SL("_status_message"), value TSRMLS_CC); } } zend_hash_move_forward_ex(ah0, &hp0); continue; } } else { PHALCON_CPY_WRT_CTOR(header_parts, header); } if (Z_TYPE_P(header_parts) == IS_ARRAY && phalcon_array_isset_long(header_parts, 0) && phalcon_array_isset_long(header_parts, 1)) { PHALCON_OBS_NVAR(name); phalcon_array_fetch_long(&name, header_parts, 0, PH_NOISY); PHALCON_OBS_NVAR(value); phalcon_array_fetch_long(&value, header_parts, 1, PH_NOISY); PHALCON_INIT_NVAR(trimmed); phalcon_fast_trim(trimmed, value, NULL, PHALCON_TRIM_BOTH TSRMLS_CC); PHALCON_CALL_METHOD(NULL, this_ptr, "set", name, trimmed); } zend_hash_move_forward_ex(ah0, &hp0); } PHALCON_MM_RESTORE(); }
/** * Reconfigure the route adding a new pattern and a set of paths * * @param string $pattern * @param array $paths */ PHP_METHOD(Phalcon_Mvc_Router_Route, reConfigure){ zval *pattern, *paths = NULL, *module_name = NULL, *controller_name = NULL; zval *action_name = NULL, *parts, *number_parts, *route_paths = NULL; zval *real_class_name = NULL, *namespace_name, *lower_name; zval *pcre_pattern = NULL, *compiled_pattern = NULL; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 1, &pattern, &paths); if (!paths) { PHALCON_INIT_VAR(paths); } if (Z_TYPE_P(pattern) != IS_STRING) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_router_exception_ce, "The pattern must be string"); return; } if (Z_TYPE_P(paths) != IS_NULL) { if (Z_TYPE_P(paths) == IS_STRING) { PHALCON_INIT_VAR(module_name); PHALCON_INIT_VAR(controller_name); PHALCON_INIT_VAR(action_name); /** * Explode the short paths using the :: separator */ PHALCON_INIT_VAR(parts); phalcon_fast_explode_str(parts, SL("::"), paths); PHALCON_INIT_VAR(number_parts); phalcon_fast_count(number_parts, parts TSRMLS_CC); /** * Create the array paths dynamically */ switch (phalcon_get_intval(number_parts)) { case 3: PHALCON_OBS_NVAR(module_name); phalcon_array_fetch_long(&module_name, parts, 0, PH_NOISY); PHALCON_OBS_NVAR(controller_name); phalcon_array_fetch_long(&controller_name, parts, 1, PH_NOISY); PHALCON_OBS_NVAR(action_name); phalcon_array_fetch_long(&action_name, parts, 2, PH_NOISY); break; case 2: PHALCON_OBS_NVAR(controller_name); phalcon_array_fetch_long(&controller_name, parts, 0, PH_NOISY); PHALCON_OBS_NVAR(action_name); phalcon_array_fetch_long(&action_name, parts, 1, PH_NOISY); break; case 1: PHALCON_OBS_NVAR(controller_name); phalcon_array_fetch_long(&controller_name, parts, 0, PH_NOISY); break; } PHALCON_INIT_VAR(route_paths); array_init(route_paths); /** * Process module name */ if (Z_TYPE_P(module_name) != IS_NULL) { phalcon_array_update_string(&route_paths, SL("module"), &module_name, PH_COPY | PH_SEPARATE); } /** * Process controller name */ if (Z_TYPE_P(controller_name) != IS_NULL) { /** * Check if we need to obtain the namespace */ if (phalcon_memnstr_str(controller_name, SL("\\"))) { /** * Extract the real class name from the namespaced class */ PHALCON_INIT_VAR(real_class_name); phalcon_get_class_ns(real_class_name, controller_name, 0 TSRMLS_CC); /** * Extract the namespace from the namespaced class */ PHALCON_INIT_VAR(namespace_name); phalcon_get_ns_class(namespace_name, controller_name, 0 TSRMLS_CC); /** * Update the namespace */ if (zend_is_true(namespace_name)) { phalcon_array_update_string(&route_paths, SL("namespace"), &namespace_name, PH_COPY | PH_SEPARATE); } } else { PHALCON_CPY_WRT(real_class_name, controller_name); } /** * Always pass the controller to lowercase */ PHALCON_INIT_VAR(lower_name); phalcon_uncamelize(lower_name, real_class_name); /** * Update the controller path */ phalcon_array_update_string(&route_paths, SL("controller"), &lower_name, PH_COPY | PH_SEPARATE); } /** * Process action name */ if (Z_TYPE_P(action_name) != IS_NULL) { phalcon_array_update_string(&route_paths, SL("action"), &action_name, PH_COPY | PH_SEPARATE); } } else { PHALCON_CPY_WRT(route_paths, paths); } } else { PHALCON_INIT_NVAR(route_paths); array_init(route_paths); } if (Z_TYPE_P(route_paths) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_router_exception_ce, "The route contains invalid paths"); return; } /** * If the route starts with '#' we assume that it is a regular expression */ if (!phalcon_start_with_str(pattern, SL("#"))) { if (phalcon_memnstr_str(pattern, SL("{"))) { /** * The route has named parameters so we need to extract them */ PHALCON_INIT_VAR(pcre_pattern); phalcon_extract_named_params(pcre_pattern, pattern, route_paths); } else { PHALCON_CPY_WRT(pcre_pattern, pattern); } /** * Transform the route's pattern to a regular expression */ PHALCON_INIT_VAR(compiled_pattern); phalcon_call_method_p1(compiled_pattern, this_ptr, "compilepattern", pcre_pattern); } else { PHALCON_CPY_WRT(compiled_pattern, pattern); } /** * Update the original pattern */ phalcon_update_property_this(this_ptr, SL("_pattern"), pattern TSRMLS_CC); /** * Update the compiled pattern */ phalcon_update_property_this(this_ptr, SL("_compiledPattern"), compiled_pattern TSRMLS_CC); /** * Update the route's paths */ phalcon_update_property_this(this_ptr, SL("_paths"), route_paths TSRMLS_CC); PHALCON_MM_RESTORE(); }