/** * Gets the real mime type of the upload file using finfo * * @return string */ PHP_METHOD(Phalcon_Http_Request_File, getRealType){ zval *constant, *finfo = NULL, *temp_file, *mime; PHALCON_MM_GROW(); mime = phalcon_fetch_nproperty_this(this_ptr, SL("_real_type"), PH_NOISY TSRMLS_CC); if (Z_TYPE_P(mime) == IS_STRING) { RETURN_CTOR(mime); } PHALCON_INIT_VAR(constant); if (!zend_get_constant(SL("FILEINFO_MIME_TYPE"), constant TSRMLS_CC)) { RETURN_MM_NULL(); } PHALCON_CALL_FUNCTION(&finfo, "finfo_open", constant); if (Z_TYPE_P(finfo) != IS_RESOURCE) { RETURN_MM_NULL(); } temp_file = phalcon_fetch_nproperty_this(this_ptr, SL("_tmp"), PH_NOISY TSRMLS_CC); PHALCON_RETURN_CALL_FUNCTION("finfo_file", finfo, temp_file); PHALCON_CALL_FUNCTION(NULL, "finfo_close", finfo); PHALCON_MM_RESTORE(); }
/** * Utility to normalize a string's encoding to UTF-32. * * @param string $str * @return string */ PHP_METHOD(Phalcon_Escaper, normalizeEncoding){ zval *str, *encoding = NULL, *charset; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &str); /** * mbstring is required here */ if (phalcon_function_exists_ex(SS("mb_convert_encoding") TSRMLS_CC) == FAILURE) { PHALCON_THROW_EXCEPTION_STR(phalcon_escaper_exception_ce, "Extension 'mbstring' is required"); return; } PHALCON_CALL_METHOD(&encoding, this_ptr, "detectencoding", str); PHALCON_INIT_VAR(charset); ZVAL_STRING(charset, "UTF-32", 1); /** * Convert to UTF-32 (4 byte characters, regardless of actual number of bytes in * the character). */ PHALCON_RETURN_CALL_FUNCTION("mb_convert_encoding", str, charset, encoding); RETURN_MM(); }
/** * Gets decoded BSON HTTP raw request body * * @return string */ PHP_METHOD(Phalcon_Http_Request, getBsonRawBody) { zval raw_body = {}; PHALCON_CALL_METHODW(&raw_body, getThis(), "getrawbody"); if (Z_TYPE(raw_body) == IS_STRING) { PHALCON_RETURN_CALL_FUNCTION("bson_decode", &raw_body); } }
/** * Reduces multiple slashes in a string to single slashes * * <code> * echo Phalcon\Text::reduceSlashes("foo//bar/baz"); // foo/bar/baz * echo Phalcon\Text::reduceSlashes("http://foo.bar///baz/buz"); // http://foo.bar/baz/buz * </code> */ PHP_METHOD(Phalcon_Text, reduceSlashes){ zval *str, *pattern, *replacement; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &str); PHALCON_INIT_VAR(pattern); ZVAL_STRING(pattern, "#(?<!:)//+#", 1); PHALCON_INIT_VAR(replacement); ZVAL_STRING(replacement, "/", 1); PHALCON_RETURN_CALL_FUNCTION("preg_replace", pattern, replacement, str); RETURN_MM(); }
/** * Uppercases a string, this function makes use of the mbstring extension if available * * @param string $str * @return string */ PHP_METHOD(Phalcon_Text, upper){ zval *str; phalcon_fetch_params(0, 1, 0, &str); /** * 'upper' checks for the mbstring extension to make a correct lowercase * transformation */ if (phalcon_function_exists_ex(SS("mb_strtoupper") TSRMLS_CC) == SUCCESS) { PHALCON_MM_GROW(); PHALCON_RETURN_CALL_FUNCTION("mb_strtoupper", str); RETURN_MM(); } phalcon_fast_strtoupper(return_value, str); }
/** * Makes an underscored or dashed phrase human-readable * * <code> * echo Phalcon\Text::humanize('start-a-horse'); // 'start a horse' * echo Phalcon\Text::humanize('five_cats'); // 'five cats' * </code> */ PHP_METHOD(Phalcon_Text, humanize){ zval *str, *trimmed, *pattern, *replacement; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &str); PHALCON_INIT_VAR(trimmed); phalcon_fast_trim(trimmed, str, NULL, PHALCON_TRIM_BOTH TSRMLS_CC); PHALCON_INIT_VAR(pattern); ZVAL_STRING(pattern, "#[_-]+#", 1); PHALCON_INIT_VAR(replacement); ZVAL_STRING(replacement, " ", 1); PHALCON_RETURN_CALL_FUNCTION("preg_replace", pattern, replacement, trimmed); RETURN_MM(); }
/** * 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(); phalcon_fetch_params(1, 1, 0, &str); /** * 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. */ strict_check = PHALCON_GLOBAL(z_true); PHALCON_INIT_NVAR(charset); ZVAL_STRING(charset, "UTF-32", 1); /** * Check for UTF-32 encoding */ PHALCON_CALL_FUNCTION(&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_CALL_FUNCTION(&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_CALL_FUNCTION(&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_CALL_FUNCTION(&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_CALL_FUNCTION(&detected, "mb_detect_encoding", str, charset, strict_check); if (zend_is_true(detected)) { RETURN_CTOR(charset); } /** * Fallback to global detection */ PHALCON_RETURN_CALL_FUNCTION("mb_detect_encoding", str); RETURN_MM(); }
/** * Returns human readable sizes * * @param int $size * @param string $forceUnit * @param string $format * @param boolean $si * @return string */ PHP_METHOD(Phalcon_Text, bytes){ zval *z_size, *z_force_unit = NULL, *format = NULL, *si = NULL; char *force_unit; const char **units; const char *units1[] = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; const char *units2[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}; double size; int mod, power = 0, found = 0, i, j = 0; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 3, &z_size, &z_force_unit, &format, &si); PHALCON_SEPARATE_PARAM(z_size); convert_to_double(z_size); size = Z_DVAL_P(z_size); if (!z_force_unit) { PHALCON_INIT_VAR(z_force_unit); } else { PHALCON_SEPARATE_PARAM(z_force_unit); convert_to_string(z_force_unit); } if (!format) { PHALCON_INIT_VAR(format); } else { PHALCON_SEPARATE_PARAM(format); convert_to_string(format); } if (PHALCON_IS_EMPTY(format)) { PHALCON_INIT_NVAR(format); ZVAL_STRING(format, "%01.2f %s", 1); } if (!si) { si = PHALCON_GLOBAL(z_true); } if (!zend_is_true(si) || (!PHALCON_IS_EMPTY(z_force_unit) && phalcon_memnstr_str(z_force_unit, SL("i")))) { units = units2; mod = 1024; } else { units = units1; mod = 1000; } if (!PHALCON_IS_EMPTY(z_force_unit)) { force_unit = Z_STRVAL_P(z_force_unit); for (i = 0; i < sizeof(units); i++) { if (strcasecmp(force_unit, units[i]) == 0) { found = 1; power = i; break; } } } if (found) { while(j < power) { size /= mod; j++; } } else { while(size > mod) { size /= mod; power++; } } PHALCON_INIT_NVAR(z_size); ZVAL_DOUBLE(z_size, size); PHALCON_INIT_NVAR(z_force_unit); ZVAL_STRING(z_force_unit, units[power], 1); PHALCON_RETURN_CALL_FUNCTION("sprintf", format, z_size, z_force_unit); RETURN_MM(); }