/** * Detect the character encoding of a string to be handled by an encoder * Special-handling for chr(172) and chr(128) to chr(159) which fail to be detected by mb_detect_encoding() * * @param string $str * @param string $charset * @return string */ PHP_METHOD(Phalcon_Escaper, detectEncoding){ zval *str, charset = {}, strict_check = {}, detected = {}; phalcon_fetch_params(0, 1, 0, &str); /** * Check if charset is ASCII or ISO-8859-1 */ phalcon_is_basic_charset(&charset, str); if (Z_TYPE(charset) == IS_STRING) { RETURN_CTORW(&charset); } /** * We require mbstring extension here */ if (phalcon_function_exists_ex(SL("mb_detect_encoding")) == FAILURE) { RETURN_NULL(); } /** * Strict encoding detection with fallback to non-strict detection. */ ZVAL_TRUE(&strict_check); ZVAL_STRING(&charset, "UTF-32"); /** * Check for UTF-32 encoding */ PHALCON_CALL_FUNCTIONW(&detected, "mb_detect_encoding", str, &charset, &strict_check); if (zend_is_true(&detected)) { RETURN_CTORW(&charset); } ZVAL_STRING(&charset, "UTF-16"); /** * Check for UTF-16 encoding */ PHALCON_CALL_FUNCTIONW(&detected, "mb_detect_encoding", str, &charset, &strict_check); if (zend_is_true(&detected)) { RETURN_CTORW(&charset); } ZVAL_STRING(&charset, "UTF-8"); /** * Check for UTF-8 encoding */ PHALCON_CALL_FUNCTIONW(&detected, "mb_detect_encoding", str, &charset, &strict_check); if (zend_is_true(&detected)) { RETURN_CTORW(&charset); } ZVAL_STRING(&charset, "ISO-8859-1"); /** * Check for ISO-8859-1 encoding */ PHALCON_CALL_FUNCTIONW(&detected, "mb_detect_encoding", str, &charset, &strict_check); if (zend_is_true(&detected)) { RETURN_CTORW(&charset); } ZVAL_STRING(&charset, "ASCII"); /** * Check for ASCII encoding */ PHALCON_CALL_FUNCTIONW(&detected, "mb_detect_encoding", str, &charset, &strict_check); if (zend_is_true(&detected)) { RETURN_CTORW(&charset); } /** * Fallback to global detection */ PHALCON_RETURN_CALL_FUNCTIONW("mb_detect_encoding", str); }
/** * Detect the character encoding of a string to be handled by an encoder * Special-handling for chr(172) and chr(128) to chr(159) which fail to be detected by mb_detect_encoding() * * @param string $str * @param string $charset * @return string */ PHP_METHOD(Phalcon_Escaper, detectEncoding){ zval *str, *charset = NULL, *strict_check, *detected = NULL; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &str) == FAILURE) { RETURN_MM_NULL(); } /** * Check if charset is ASCII or ISO-8859-1 */ PHALCON_INIT_VAR(charset); phalcon_is_basic_charset(charset, str); if (Z_TYPE_P(charset) == IS_STRING) { RETURN_CTOR(charset); } /** * We require mbstring extension here */ if (phalcon_function_exists_ex(SS("mb_detect_encoding") TSRMLS_CC) == FAILURE) { RETURN_MM_NULL(); } /** * Strict encoding detection with fallback to non-strict detection. */ PHALCON_INIT_VAR(strict_check); ZVAL_BOOL(strict_check, 1); PHALCON_INIT_NVAR(charset); ZVAL_STRING(charset, "UTF-32", 1); /** * Check for UTF-32 encoding */ PHALCON_INIT_VAR(detected); PHALCON_CALL_FUNC_PARAMS_3(detected, "mb_detect_encoding", str, charset, strict_check); if (zend_is_true(detected)) { RETURN_CTOR(charset); } PHALCON_INIT_NVAR(charset); ZVAL_STRING(charset, "UTF-16", 1); /** * Check for UTF-16 encoding */ PHALCON_INIT_NVAR(detected); PHALCON_CALL_FUNC_PARAMS_3(detected, "mb_detect_encoding", str, charset, strict_check); if (zend_is_true(detected)) { RETURN_CTOR(charset); } PHALCON_INIT_NVAR(charset); ZVAL_STRING(charset, "UTF-8", 1); /** * Check for UTF-8 encoding */ PHALCON_INIT_NVAR(detected); PHALCON_CALL_FUNC_PARAMS_3(detected, "mb_detect_encoding", str, charset, strict_check); if (zend_is_true(detected)) { RETURN_CTOR(charset); } PHALCON_INIT_NVAR(charset); ZVAL_STRING(charset, "ISO-8859-1", 1); /** * Check for ISO-8859-1 encoding */ PHALCON_INIT_NVAR(detected); PHALCON_CALL_FUNC_PARAMS_3(detected, "mb_detect_encoding", str, charset, strict_check); if (zend_is_true(detected)) { RETURN_CTOR(charset); } PHALCON_INIT_NVAR(charset); ZVAL_STRING(charset, "ASCII", 1); /** * Check for ASCII encoding */ PHALCON_INIT_NVAR(detected); PHALCON_CALL_FUNC_PARAMS_3(detected, "mb_detect_encoding", str, charset, strict_check); if (zend_is_true(detected)) { RETURN_CTOR(charset); } /** * Fallback to global detection */ PHALCON_INIT_NVAR(charset); PHALCON_CALL_FUNC_PARAMS_1(charset, "mb_detect_encoding", str); RETURN_CCTOR(charset); }