From a95e560b499afc20f03b2d5f806f8c0ef3c49009 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Wed, 30 Apr 2014 14:14:51 +0200 Subject: [PATCH] Removing constants IS_PHP_* removing PHP < 5.2 support see #7113 Formatting code, fixing typos, removing custom replaces to string functions. --- main/inc/lib/internationalization.lib.php | 653 +++--------------- .../lib/internationalization_internal.lib.php | 91 +-- main/inc/lib/main_api.lib.php | 20 +- 3 files changed, 86 insertions(+), 678 deletions(-) diff --git a/main/inc/lib/internationalization.lib.php b/main/inc/lib/internationalization.lib.php index 0d94d0938e..935191d7d5 100644 --- a/main/inc/lib/internationalization.lib.php +++ b/main/inc/lib/internationalization.lib.php @@ -11,7 +11,6 @@ * @package chamilo.library */ - /** * Constants */ @@ -23,33 +22,40 @@ define('SPECIAL_CLOSING_TAG', '=]'); // Predefined date formats in Chamilo provided by the language sub-system. // To be used as a parameter for the function api_format_date() -define('TIME_NO_SEC_FORMAT', 0); // 15:23 -define('DATE_FORMAT_SHORT', 1); // Aug 25, 09 -define('DATE_FORMAT_LONG', 2); // Monday August 25, 09 -define('DATE_FORMAT_LONG_NO_DAY', 10); // August 25, 2009 -define('DATE_TIME_FORMAT_LONG', 3); // Monday August 25, 2009 at 03:28 PM +define('TIME_NO_SEC_FORMAT', 0); // 15:23 +define('DATE_FORMAT_SHORT', 1); // Aug 25, 09 +define('DATE_FORMAT_LONG', 2); // Monday August 25, 09 +define('DATE_FORMAT_LONG_NO_DAY', 10); // August 25, 2009 +define('DATE_TIME_FORMAT_LONG', 3); // Monday August 25, 2009 at 03:28 PM -define('DATE_FORMAT_NUMBER', 4); // 25.08.09 +define('DATE_FORMAT_NUMBER', 4); // 25.08.09 define('DATE_TIME_FORMAT_LONG_24H', 5); // August 25, 2009 at 15:28 -define('DATE_TIME_FORMAT_SHORT', 6); // Aug 25, 2009 at 03:28 PM -define('DATE_TIME_FORMAT_SHORT_TIME_FIRST', 7); // 03:28 PM, Aug 25 2009 -define('DATE_FORMAT_NUMBER_NO_YEAR', 8); // 25.08 dd-mm -define('DATE_FORMAT_ONLY_DAYNAME', 9); // Monday, Sunday, etc +define('DATE_TIME_FORMAT_SHORT', 6); // Aug 25, 2009 at 03:28 PM +define('DATE_TIME_FORMAT_SHORT_TIME_FIRST', 7); // 03:28 PM, Aug 25 2009 +define('DATE_FORMAT_NUMBER_NO_YEAR', 8); // 25.08 dd-mm +define('DATE_FORMAT_ONLY_DAYNAME', 9); // Monday, Sunday, etc // Formatting person's name. -define('PERSON_NAME_COMMON_CONVENTION', 0); // Formatting a person's name using the pattern as it has been - // configured in the internationalization database for every language. - // This (default) option would be the most used. -// The followind options may be used in limited number of places for overriding the common convention: -define('PERSON_NAME_WESTERN_ORDER', 1); // Formatting a person's name in Western order: first_name last_name -define('PERSON_NAME_EASTERN_ORDER', 2); // Formatting a person's name in Eastern order: last_name first_name -define('PERSON_NAME_LIBRARY_ORDER', 3); // Contextual: formatting person's name in library order: last_name, first_name -define('PERSON_NAME_EMAIL_ADDRESS', PERSON_NAME_WESTERN_ORDER); // Contextual: formatting a person's name assotiated with an email-address. Ivan: I am not sure how seems email servers an clients would interpret name order, so I assign the Western order. -define('PERSON_NAME_DATA_EXPORT', PERSON_NAME_EASTERN_ORDER); // Contextual: formatting a person's name for data-exporting operarions. For backward compatibility this format has been set to Eastern order. - -// The following constants are used for tunning language detection functionality. +// Formatting a person's name using the pattern as it has been +// configured in the internationalization database for every language. +// This (default) option would be the most used. +define('PERSON_NAME_COMMON_CONVENTION', 0); +// The following options may be used in limited number of places for overriding the common convention: + +// Formatting a person's name in Western order: first_name last_name +define('PERSON_NAME_WESTERN_ORDER', 1); +// Formatting a person's name in Eastern order: last_name first_name +define('PERSON_NAME_EASTERN_ORDER', 2); +// Contextual: formatting person's name in library order: last_name, first_name +define('PERSON_NAME_LIBRARY_ORDER', 3); +// Contextual: formatting a person's name assotiated with an email-address. Ivan: I am not sure how seems email servers an clients would interpret name order, so I assign the Western order. +define('PERSON_NAME_EMAIL_ADDRESS', PERSON_NAME_WESTERN_ORDER); +// Contextual: formatting a person's name for data-exporting operations. For backward compatibility this format has been set to Eastern order. +define('PERSON_NAME_DATA_EXPORT', PERSON_NAME_EASTERN_ORDER); + +// The following constants are used for tuning language detection functionality. // We reduce the text for language detection to the given number of characters -// for increaseing speed and to decrease memory consumption. +// for increasing speed and to decrease memory consumption. define ('LANGUAGE_DETECT_MAX_LENGTH', 2000); // Maximum allowed difference in so called delta-points for aborting certain language detection. // The value 80000 is good enough for speed and detection accuracy. @@ -58,15 +64,12 @@ define ('LANGUAGE_DETECT_MAX_LENGTH', 2000); define ('LANGUAGE_DETECT_MAX_DELTA', 140000); /** - * Initialization - */ - -/** - * Initialization of some internal default valies in the internationalization library. + * Initialization of some internal default values in the internationalization library. * @return void * Note: This function should be called only once in the global initialization script. */ -function api_initialize_internationalization() { +function api_initialize_internationalization() +{ if (MBSTRING_INSTALLED) { @ini_set('mbstring.func_overload', 0); @ini_set('mbstring.encoding_translation', 0); @@ -88,6 +91,7 @@ function api_set_internationalization_default_encoding($encoding) { _api_mb_internal_encoding($encoding); _api_mb_regex_encoding($encoding); _api_iconv_set_encoding('iconv_internal_encoding', $encoding); + return $result; } @@ -680,84 +684,84 @@ function api_format_date($time, $format = null, $language = null) { switch ($format) { case DATE_FORMAT_ONLY_DAYNAME: $date_format = get_lang('dateFormatOnlyDayName', '', $language); - if (IS_PHP_53 && INTL_INSTALLED) { + if (INTL_INSTALLED) { $datetype = IntlDateFormatter::SHORT; $timetype = IntlDateFormatter::NONE; } break; case DATE_FORMAT_NUMBER_NO_YEAR: $date_format = get_lang('dateFormatShortNumberNoYear', '', $language); - if (IS_PHP_53 && INTL_INSTALLED) { + if (INTL_INSTALLED) { $datetype = IntlDateFormatter::SHORT; $timetype = IntlDateFormatter::NONE; } break; case DATE_FORMAT_NUMBER: $date_format = get_lang('dateFormatShortNumber', '', $language); - if (IS_PHP_53 && INTL_INSTALLED) { + if (INTL_INSTALLED) { $datetype = IntlDateFormatter::SHORT; $timetype = IntlDateFormatter::NONE; } break; case TIME_NO_SEC_FORMAT: $date_format = get_lang('timeNoSecFormat', '', $language); - if (IS_PHP_53 && INTL_INSTALLED) { + if (INTL_INSTALLED) { $datetype = IntlDateFormatter::NONE; $timetype = IntlDateFormatter::SHORT; } break; case DATE_FORMAT_SHORT: $date_format = get_lang('dateFormatShort', '', $language); - if (IS_PHP_53 && INTL_INSTALLED) { + if (INTL_INSTALLED) { $datetype = IntlDateFormatter::LONG; $timetype = IntlDateFormatter::NONE; } break; case DATE_FORMAT_LONG: $date_format = get_lang('dateFormatLong', '', $language); - if (IS_PHP_53 && INTL_INSTALLED) { + if (INTL_INSTALLED) { $datetype = IntlDateFormatter::FULL; $timetype = IntlDateFormatter::NONE; } break; case DATE_TIME_FORMAT_LONG: $date_format = get_lang('dateTimeFormatLong', '', $language); - if (IS_PHP_53 && INTL_INSTALLED) { + if (INTL_INSTALLED) { $datetype = IntlDateFormatter::FULL; $timetype = IntlDateFormatter::SHORT; } break; case DATE_FORMAT_LONG_NO_DAY: $date_format = get_lang('dateFormatLongNoDay', '', $language); - if (IS_PHP_53 && INTL_INSTALLED) { + if (INTL_INSTALLED) { $datetype = IntlDateFormatter::FULL; $timetype = IntlDateFormatter::SHORT; } break; case DATE_TIME_FORMAT_SHORT: $date_format = get_lang('dateTimeFormatShort', '', $language); - if (IS_PHP_53 && INTL_INSTALLED) { + if (INTL_INSTALLED) { $datetype = IntlDateFormatter::FULL; $timetype = IntlDateFormatter::SHORT; } break; case DATE_TIME_FORMAT_SHORT_TIME_FIRST: $date_format = get_lang('dateTimeFormatShortTimeFirst', '', $language); - if (IS_PHP_53 && INTL_INSTALLED) { + if (INTL_INSTALLED) { $datetype = IntlDateFormatter::FULL; $timetype = IntlDateFormatter::SHORT; } break; case DATE_TIME_FORMAT_LONG_24H: $date_format = get_lang('dateTimeFormatLong24H', '', $language); - if (IS_PHP_53 && INTL_INSTALLED) { + if (INTL_INSTALLED) { $datetype = IntlDateFormatter::FULL; $timetype = IntlDateFormatter::SHORT; } break; default: $date_format = get_lang('dateTimeFormatLong', '', $language); - if (IS_PHP_53 && INTL_INSTALLED) { + if (INTL_INSTALLED) { $datetype = IntlDateFormatter::FULL; $timetype = IntlDateFormatter::SHORT; } @@ -766,7 +770,6 @@ function api_format_date($time, $format = null, $language = null) { $date_format = $format; } - //if (IS_PHP_53 && INTL_INSTALLED && $datetype !== null && $timetype !== null) { if (0) { //if using PHP 5.3 format dates like: $dateFormatShortNumber, can't be used // @@ -774,9 +777,6 @@ function api_format_date($time, $format = null, $language = null) { if (is_null($language)) { $language = api_get_language_isocode(); } - /*$date_formatter = datefmt_create($language, $datetype, $timetype, date_default_timezone_get()); - $formatted_date = api_to_system_encoding(datefmt_format($date_formatter, $time), 'UTF-8');*/ - $date_formatter = new IntlDateFormatter($language, $datetype, $timetype, date_default_timezone_get()); //$date_formatter->setPattern($date_format); $formatted_date = api_to_system_encoding($date_formatter->format($time), 'UTF-8'); @@ -1273,8 +1273,7 @@ function api_to_system_encoding($string, $from_encoding = null, $check_utf8_vali if (api_is_valid_utf8($string)) { return $string; } - } - elseif (api_is_utf8($from_encoding)) { + } elseif (api_is_utf8($from_encoding)) { if (!api_is_valid_utf8($string)) { return $string; } @@ -1315,8 +1314,7 @@ function api_htmlentities($string, $quote_style = ENT_COMPAT, $encoding = null) if (!api_is_utf8($encoding)) { // Just in case. $string = api_utf8_decode($string, $encoding); } - } - elseif (_api_convert_encoding_supports($encoding)) { + } elseif (_api_convert_encoding_supports($encoding)) { if (!api_is_utf8($encoding)) { $string = _api_convert_encoding($string, 'UTF-8', $encoding); } @@ -1330,7 +1328,7 @@ function api_htmlentities($string, $quote_style = ENT_COMPAT, $encoding = null) } /** - * Convers HTML entities into normal characters. + * Converts HTML entities into normal characters. * @param string $string The input string. * @param int $quote_style (optional) The quote style - ENT_COMPAT (default), ENT_QUOTES, ENT_NOQUOTES. * @param string $encoding (optional) The encoding (of the result) used in conversion. If it is omited, the platform character set is assumed. @@ -1355,7 +1353,7 @@ function api_html_entity_decode($string, $quote_style = ENT_COMPAT, $encoding = } return $string; } - return $string; // Here the function guves up. + return $string; // Here the function gives up. } /** @@ -1552,15 +1550,12 @@ function api_transliterate($string, $unknown = '?', $from_encoding = null) { } $ord = $ord & 255; $result .= isset($map[$bank]['en'][$ord]) ? $map[$bank]['en'][$ord] : $unknown; - $head = ''; - } - elseif ($c < "\x80") { + } elseif ($c < "\x80") { // ASCII byte. $result .= $c; $head = ''; - } - elseif ($c < "\xc0") { + } elseif ($c < "\xc0") { // Illegal tail bytes. if ($head == '') { $result .= $unknown; @@ -1744,32 +1739,11 @@ function api_str_split($string, $split_length = 1, $encoding = null) { * @link http://php.net/manual/en/function.mb-stripos */ function api_stripos($haystack, $needle, $offset = 0, $encoding = null) { - if (empty($encoding)) { - $encoding = _api_mb_internal_encoding(); - } - if (!is_string($needle)) { - $needle = (int)$needle; - if (api_is_utf8($encoding)) { - $needle = _api_utf8_chr($needle); - } else { - $needle = chr($needle); - } - } - if ($needle == '') { - return false; - } if (_api_mb_supports($encoding)) { - return @mb_stripos($haystack, $needle, $offset, $encoding); - } - elseif (api_is_encoding_supported($encoding)) { - if (MBSTRING_INSTALLED) { - if (!api_is_utf8($encoding)) { - $haystack = api_utf8_encode($haystack, $encoding); - $needle = api_utf8_encode($needle, $encoding); - } - return @mb_stripos($haystack, $needle, $offset, 'UTF-8'); + if (empty($encoding)) { + $encoding = _api_mb_internal_encoding(); } - return api_strpos(api_strtolower($haystack, $encoding), api_strtolower($needle, $encoding), $offset, $encoding); + return mb_stripos($haystack, $needle, $offset, $encoding); } return stripos($haystack, $needle, $offset); } @@ -1790,49 +1764,11 @@ function api_stripos($haystack, $needle, $offset = 0, $encoding = null) { * @link http://php.net/manual/en/function.mb-stristr */ function api_stristr($haystack, $needle, $before_needle = false, $encoding = null) { - if (empty($encoding)) { - $encoding = _api_mb_internal_encoding(); - } - if (!is_string($needle)) { - $needle = (int)$needle; - if (api_is_utf8($encoding)) { - $needle = _api_utf8_chr($needle); - } else { - $needle = chr($needle); - } - } - if ($needle == '') { - return false; - } if (_api_mb_supports($encoding)) { - return @mb_stristr($haystack, $needle, $before_needle, $encoding); - } - elseif (api_is_encoding_supported($encoding)) { - if (MBSTRING_INSTALLED) { - if (!api_is_utf8($encoding)) { - $haystack = api_utf8_encode($haystack, $encoding); - $needle = api_utf8_encode($needle, $encoding); - } - $result = @mb_stristr($haystack, $needle, $before_needle, 'UTF-8'); - if ($result === false) { - return false; - } - if (!api_is_utf8($encoding)) { - return api_utf8_decode($result, $encoding); - } - return $result; - } - $result = api_strstr(api_strtolower($haystack, $encoding), api_strtolower($needle, $encoding), $before_needle, $encoding); - if ($result === false) { - return false; - } - if ($before_needle) { - return api_substr($haystack, 0, api_strlen($result, $encoding), $encoding); + if (empty($encoding)) { + $encoding = _api_mb_internal_encoding(); } - return api_substr($haystack, api_strlen($haystack, $encoding) - api_strlen($result, $encoding), null, $encoding); - } - if (!IS_PHP_53) { - return stristr($haystack, $needle); + return mb_stristr($haystack, $needle, $before_needle, $encoding); } return stristr($haystack, $needle, $before_needle); } @@ -1864,9 +1800,6 @@ function api_strlen($string, $encoding = null) { if (_api_iconv_supports($encoding)) { return @iconv_strlen($string, $encoding); } - if (api_is_utf8($encoding)) { - return api_byte_count(preg_replace("/[\x80-\xBF]/", '', $string)); - } return strlen($string); } @@ -1886,43 +1819,10 @@ function api_strpos($haystack, $needle, $offset = 0, $encoding = null) { if (empty($encoding)) { $encoding = _api_mb_internal_encoding(); } - if (!is_string($needle)) { - $needle = (int)$needle; - if (api_is_utf8($encoding)) { - $needle = _api_utf8_chr($needle); - } else { - $needle = chr($needle); - } - } - if ($needle == '') { - return false; - } if (_api_is_single_byte_encoding($encoding)) { return strpos($haystack, $needle, $offset); - } - elseif (_api_mb_supports($encoding)) { - return @mb_strpos($haystack, $needle, $offset, $encoding); - } - elseif (api_is_encoding_supported($encoding)) { - if (!api_is_utf8($encoding)) { - $haystack = api_utf8_encode($haystack, $encoding); - $needle = api_utf8_encode($needle, $encoding); - } - if (MBSTRING_INSTALLED) { - return @mb_strpos($haystack, $needle, $offset, 'UTF-8'); - } - if (empty($offset)) { - $haystack = explode($needle, $haystack, 2); - if (count($haystack) > 1) { - return api_strlen($haystack[0]); - } - return false; - } - $haystack = api_substr($haystack, $offset); - if (($pos = api_strpos($haystack, $needle)) !== false ) { - return $pos + $offset; - } - return false; + } elseif (_api_mb_supports($encoding)) { + return mb_strpos($haystack, $needle, $offset, $encoding); } return strpos($haystack, $needle, $offset); } @@ -1946,17 +1846,6 @@ function api_strrchr($haystack, $needle, $before_needle = false, $encoding = nul if (empty($encoding)) { $encoding = _api_mb_internal_encoding(); } - if (!is_string($needle)) { - $needle = (int)$needle; - if (api_is_utf8($encoding)) { - $needle = _api_utf8_chr($needle); - } else { - $needle = chr($needle); - } - } - if ($needle == '') { - return false; - } if (_api_is_single_byte_encoding($encoding)) { if (!$before_needle) { return strrchr($haystack, $needle); @@ -1966,26 +1855,8 @@ function api_strrchr($haystack, $needle, $before_needle = false, $encoding = nul return false; } return api_substr($haystack, 0, api_strlen($haystack, $encoding) - api_strlen($result, $encoding), $encoding); - } - elseif (_api_mb_supports($encoding)) { - return @mb_strrchr($haystack, $needle, $before_needle, $encoding); - } - elseif (MBSTRING_INSTALLED && api_is_encoding_supported($encoding)) { - if (!api_is_utf8($encoding)) { - $haystack = api_utf8_encode($haystack, $encoding); - $needle = api_utf8_encode($needle, $encoding); - } - $result = @mb_strrchr($haystack, $needle, $before_needle, 'UTF-8'); - if ($result === false) { - return false; - } - if (!api_is_utf8($encoding)) { - return api_utf8_decode($result, $encoding); - } - return $result; - } - if (!$before_needle) { - return strrchr($haystack, $needle); + } elseif (_api_mb_supports($encoding)) { + return mb_strrchr($haystack, $needle, $before_needle, $encoding); } $result = strrchr($haystack, $needle); if ($result === false) { @@ -2030,7 +1901,8 @@ function api_strrev($string, $encoding = null) { * @link http://php.net/manual/en/function.strripos * @link http://php.net/manual/en/function.mb-strripos */ -function api_strripos($haystack, $needle, $offset = 0, $encoding = null) { +function api_strripos($haystack, $needle, $offset = 0, $encoding = null) +{ return api_strrpos(api_strtolower($haystack, $encoding), api_strtolower($needle, $encoding), $offset, $encoding); } @@ -2046,72 +1918,13 @@ function api_strripos($haystack, $needle, $offset = 0, $encoding = null) { * @link http://php.net/manual/en/function.strrpos * @link http://php.net/manual/en/function.mb-strrpos */ -function api_strrpos($haystack, $needle, $offset = 0, $encoding = null) { - - if (empty($encoding)) { - $encoding = _api_mb_internal_encoding(); - } - if (!is_string($needle)) { - $needle = (int)$needle; - if (api_is_utf8($encoding)) { - $needle = _api_utf8_chr($needle); - } else { - $needle = chr($needle); - } - } - if ($needle == '') { - return false; - } - if (_api_is_single_byte_encoding($encoding)) { - return strrpos($haystack, $needle, $offset); - } - if (_api_mb_supports($encoding) && IS_PHP_52) { - return @mb_strrpos($haystack, $needle, $offset, $encoding); - } elseif (api_is_encoding_supported($encoding)) { - - if (!api_is_utf8($encoding)) { - $haystack = api_utf8_encode($haystack, $encoding); - $needle = api_utf8_encode($needle, $encoding); - } - // In PHP 5.1 the $offset parameter didn't exist see http://php.net/manual/en/function.mb-strrpos.php - if (MBSTRING_INSTALLED && IS_PHP_SUP_OR_EQ_51) { - //return @mb_strrpos($haystack, $needle, $offset, 'UTF-8'); - //@todo fix the missing $offset parameter - return @mb_strrpos($haystack, $needle, 'UTF-8'); - } - if (MBSTRING_INSTALLED && IS_PHP_SUP_OR_EQ_52) { - return @mb_strrpos($haystack, $needle, $offset, 'UTF-8'); - } - - // This branch (this fragment of code) is an adaptation from the CakePHP(tm) Project, http://www.cakefoundation.org - $found = false; - $haystack = _api_utf8_to_unicode($haystack); - $haystack_count = count($haystack); - $matches = array_count_values($haystack); - $needle = _api_utf8_to_unicode($needle); - $needle_count = count($needle); - $position = $offset; - while (($found === false) && ($position < $haystack_count)) { - if (isset($needle[0]) && $needle[0] === $haystack[$position]) { - for ($i = 1; $i < $needle_count; $i++) { - if ($needle[$i] !== $haystack[$position + $i]) { - if ($needle[$i] === $haystack[($position + $i) -1]) { - $position--; - $found = true; - continue; - } - } - } - if (!$offset && isset($matches[$needle[0]]) && $matches[$needle[0]] > 1) { - $matches[$needle[0]] = $matches[$needle[0]] - 1; - } elseif ($i === $needle_count) { - $found = true; - $position--; - } - } - $position++; +function api_strrpos($haystack, $needle, $offset = 0, $encoding = null) +{ + if (_api_mb_supports($encoding)) { + if (empty($encoding)) { + $encoding = _api_mb_internal_encoding(); } - return ($found) ? $position : false; + return mb_strrpos($haystack, $needle, $offset, $encoding); } return strrpos($haystack, $needle, $offset); } @@ -2147,46 +1960,10 @@ function api_strstr($haystack, $needle, $before_needle = false, $encoding = null return false; } if (_api_is_single_byte_encoding($encoding)) { - // Adding the missing parameter $before_needle to the original function strstr(), PHP_VERSION < 5.3 - if (!$before_needle) { - return strstr($haystack, $needle); - } - if (!IS_PHP_53) { - $result = explode($needle, $haystack, 2); - if ($result === false || count($result) < 2) { - return false; - } - return $result[0]; - } return strstr($haystack, $needle, $before_needle); } if (_api_mb_supports($encoding)) { - return @mb_strstr($haystack, $needle, $before_needle, $encoding); - } - elseif (MBSTRING_INSTALLED && api_is_encoding_supported($encoding)) { - if (!api_is_utf8($encoding)) { - $haystack = api_utf8_encode($haystack, $encoding); - $needle = api_utf8_encode($needle, $encoding); - } - $result = @mb_strstr($haystack, $needle, $before_needle, 'UTF-8'); - if ($result !== false) { - if (!api_is_utf8($encoding)) { - return api_utf8_decode($result, $encoding); - } - return $result; - } - return false; - } - // Adding the missing parameter $before_needle to the original function strstr(), PHP_VERSION < 5.3 - if (!$before_needle) { - return strstr($haystack, $needle); - } - if (!IS_PHP_53) { - $result = explode($needle, $haystack, 2); - if ($result === false || count($result) < 2) { - return false; - } - return $result[0]; + return mb_strstr($haystack, $needle, $before_needle, $encoding); } return strstr($haystack, $needle, $before_needle); } @@ -2205,53 +1982,7 @@ function api_strtolower($string, $encoding = null) { $encoding = _api_mb_internal_encoding(); } if (_api_mb_supports($encoding)) { - return @mb_strtolower($string, $encoding); - } - elseif (api_is_encoding_supported($encoding)) { - if (!api_is_utf8($encoding)) { - $string = api_utf8_encode($string, $encoding); - } - if (MBSTRING_INSTALLED) { - $string = @mb_strtolower($string, 'UTF-8'); - } else { - // This branch (this fragment of code) is an adaptation from the CakePHP(tm) Project, http://www.cakefoundation.org - $codepoints = _api_utf8_to_unicode($string); - $length = count($codepoints); - $matched = false; - $result = array(); - for ($i = 0 ; $i < $length; $i++) { - $codepoint = $codepoints[$i]; - if ($codepoint < 128) { - $str = strtolower(chr($codepoint)); - $strlen = api_byte_count($str); - for ($ii = 0 ; $ii < $strlen; $ii++) { - $lower = ord($str[$ii]); - } - $result[] = $lower; - $matched = true; - } else { - $matched = false; - $properties = &_api_utf8_get_letter_case_properties($codepoint, 'upper'); - if (!empty($properties)) { - foreach ($properties as $key => $value) { - if ($properties[$key]['upper'] == $codepoint && count($properties[$key]['lower'][0]) === 1) { - $result[] = $properties[$key]['lower'][0]; - $matched = true; - break 1; - } - } - } - } - if ($matched === false) { - $result[] = $codepoint; - } - } - $string = _api_utf8_from_unicode($result); - } - if (!api_is_utf8($encoding)) { - return api_utf8_decode($string, $encoding); - } - return $string; + return mb_strtolower($string, $encoding); } return strtolower($string); } @@ -2270,91 +2001,7 @@ function api_strtoupper($string, $encoding = null) { $encoding = _api_mb_internal_encoding(); } if (_api_mb_supports($encoding)) { - return @mb_strtoupper($string, $encoding); - } - elseif (api_is_encoding_supported($encoding)) { - if (!api_is_utf8($encoding)) { - $string = api_utf8_encode($string, $encoding); - } - if (MBSTRING_INSTALLED) { - $string = @mb_strtoupper($string, 'UTF-8'); - } else { - // This branch (this fragment of code) is an adaptation from the CakePHP(tm) Project, http://www.cakefoundation.org - $codepoints = _api_utf8_to_unicode($string); - $length = count($codepoints); - $matched = false; - $replaced = array(); - $result = array(); - for ($i = 0 ; $i < $length; $i++) { - $codepoint = $codepoints[$i]; - if ($codepoint < 128) { - $str = strtoupper(chr($codepoint)); - $strlen = api_byte_count($str); - for ($ii = 0 ; $ii < $strlen; $ii++) { - $lower = ord($str[$ii]); - } - $result[] = $lower; - $matched = true; - } else { - $matched = false; - $properties = &_api_utf8_get_letter_case_properties($codepoint); - $property_count = count($properties); - if (!empty($properties)) { - foreach ($properties as $key => $value) { - $matched = false; - $replace = 0; - if ($length > 1 && count($properties[$key]['lower']) > 1) { - $j = 0; - for ($ii = 0; $ii < count($properties[$key]['lower']); $ii++) { - $next_codepoint = $next_codepoints[$i + $ii]; - if (isset($next_codepoint) && ($next_codepoint == $properties[$key]['lower'][$j + $ii])) { - $replace++; - } - } - if ($replace == count($properties[$key]['lower'])) { - $result[] = $properties[$key]['upper']; - $replaced = array_merge($replaced, array_values($properties[$key]['lower'])); - $matched = true; - break 1; - } - } elseif ($length > 1 && $property_count > 1) { - $j = 0; - for ($ii = 1; $ii < $property_count; $ii++) { - $next_codepoint = $next_codepoints[$i + $ii - 1]; - if (in_array($next_codepoint, $properties[$ii]['lower'])) { - for ($jj = 0; $jj < count($properties[$ii]['lower']); $jj++) { - $next_codepoint = $next_codepoints[$i + $jj]; - if (isset($next_codepoint) && ($next_codepoint == $properties[$ii]['lower'][$j + $jj])) { - $replace++; - } - } - if ($replace == count($properties[$ii]['lower'])) { - $result[] = $properties[$ii]['upper']; - $replaced = array_merge($replaced, array_values($properties[$ii]['lower'])); - $matched = true; - break 2; - } - } - } - } - if ($properties[$key]['lower'][0] == $codepoint) { - $result[] = $properties[$key]['upper']; - $matched = true; - break 1; - } - } - } - } - if ($matched === false && !in_array($codepoint, $replaced, true)) { - $result[] = $codepoint; - } - } - $string = _api_utf8_from_unicode($result); - } - if (!api_is_utf8($encoding)) { - return api_utf8_decode($string, $encoding); - } - return $string; + return mb_strtoupper($string, $encoding); } return strtoupper($string); } @@ -2383,82 +2030,7 @@ function api_substr($string, $start, $length = null, $encoding = null) { return substr($string, $start, $length); } if (_api_mb_supports($encoding)) { - return @mb_substr($string, $start, $length, $encoding); - } - elseif (api_is_encoding_supported($encoding)) { - if (!api_is_utf8($encoding)) { - $string = api_utf8_encode($string, $encoding); - } - if (MBSTRING_INSTALLED) { - $string = @mb_substr($string, $start, $length, 'UTF-8'); - } else { - // The following branch of code is from the Drupal CMS, see the function drupal_substr(). - $strlen = api_byte_count($string); - // Find the starting byte offset - $bytes = 0; - if ($start > 0) { - // Count all the continuation bytes from the start until we have found - // $start characters - $bytes = -1; $chars = -1; - while ($bytes < $strlen && $chars < $start) { - $bytes++; - $c = ord($string[$bytes]); - if ($c < 0x80 || $c >= 0xC0) { - $chars++; - } - } - } - else if ($start < 0) { - // Count all the continuation bytes from the end until we have found - // abs($start) characters - $start = abs($start); - $bytes = $strlen; $chars = 0; - while ($bytes > 0 && $chars < $start) { - $bytes--; - $c = ord($string[$bytes]); - if ($c < 0x80 || $c >= 0xC0) { - $chars++; - } - } - } - $istart = $bytes; - // Find the ending byte offset - if ($length === NULL) { - $bytes = $strlen - 1; - } - else if ($length > 0) { - // Count all the continuation bytes from the starting index until we have - // found $length + 1 characters. Then backtrack one byte. - $bytes = $istart; $chars = 0; - while ($bytes < $strlen && $chars < $length) { - $bytes++; - $c = ord($string[$bytes]); - if ($c < 0x80 || $c >= 0xC0) { - $chars++; - } - } - $bytes--; - } - else if ($length < 0) { - // Count all the continuation bytes from the end until we have found - // abs($length) characters - $length = abs($length); - $bytes = $strlen - 1; $chars = 0; - while ($bytes >= 0 && $chars < $length) { - $c = ord($string[$bytes]); - if ($c < 0x80 || $c >= 0xC0) { - $chars++; - } - $bytes--; - } - } - $iend = $bytes; - $string = substr($string, $istart, max(0, $iend - $istart + 1)); - } - if (!api_is_utf8($encoding)) { - $string = api_utf8_decode($string, $encoding); - } - return $string; + return mb_substr($string, $start, $length, $encoding); } return substr($string, $start, $length); } @@ -2476,7 +2048,7 @@ function api_substr_count($haystack, $needle, $encoding = null) { $encoding = _api_mb_internal_encoding(); } if (_api_mb_supports($encoding)) { - return @mb_substr_count($haystack, $needle, $encoding); + return mb_substr_count($haystack, $needle, $encoding); } return substr_count($haystack, $needle); } @@ -2544,7 +2116,7 @@ function api_ucfirst($string, $encoding = null) { if (empty($encoding)) { $encoding = _api_mb_internal_encoding(); } - return api_strtoupper(api_substr($string, 0, 1, $encoding), $encoding) . api_substr($string, 1, api_strlen($string, $encoding), $encoding); + return api_strtoupper(api_substr($string, 0, 1, $encoding), $encoding) . api_substr($string, 1, api_strlen($string, $encoding), $encoding); } /** @@ -2556,30 +2128,11 @@ function api_ucfirst($string, $encoding = null) { * @link http://php.net/manual/en/function.ucwords */ function api_ucwords($string, $encoding = null) { - if (empty($encoding)) { - $encoding = _api_mb_internal_encoding(); - } if (_api_mb_supports($encoding)) { - return @mb_convert_case($string, MB_CASE_TITLE, $encoding); - } - if (api_is_encoding_supported($encoding)) { - if (!api_is_utf8($encoding)) { - $string = api_utf8_encode($string, $encoding); - } - if (MBSTRING_INSTALLED) { - $string = @mb_convert_case($string, MB_CASE_TITLE, 'UTF-8'); - } else { - // The following fragment (branch) of code is based on the function utf8_ucwords() by Harry Fuecks - // See http://dev.splitbrain.org/view/darcs/dokuwiki/inc/utf8.php - // Note: [\x0c\x09\x0b\x0a\x0d\x20] matches - form feeds, horizontal tabs, vertical tabs, linefeeds and carriage returns. - // This corresponds to the definition of a "word" defined at http://www.php.net/ucwords - $pattern = '/(^|([\x0c\x09\x0b\x0a\x0d\x20]+))([^\x0c\x09\x0b\x0a\x0d\x20]{1})[^\x0c\x09\x0b\x0a\x0d\x20]*/u'; - $string = preg_replace_callback($pattern, '_api_utf8_ucwords_callback', $string); - } - if (!api_is_utf8($encoding)) { - return api_utf8_decode($string, $encoding); + if (empty($encoding)) { + $encoding = _api_mb_internal_encoding(); } - return $string; + return mb_convert_case($string, MB_CASE_TITLE, $encoding); } return ucwords($string); } @@ -3603,8 +3156,8 @@ function api_is_encoding_supported($encoding) { * Note: See the file chamilo/main/inc/lib/internationalization_database/non_utf8_encodings.php * if you wish to revise the leading non-UTF-8 encoding for your language. */ -function api_get_non_utf8_encoding($language = null) { - +function api_get_non_utf8_encoding($language = null) +{ $language_is_supported = api_is_language_supported($language); if (!$language_is_supported || empty($language)) { $language = api_get_interface_language(false, true); @@ -3725,7 +3278,6 @@ function api_detect_encoding($string, $language = null) { return $result; } - /** * String validation functions concerning certain encodings */ @@ -3751,51 +3303,6 @@ function api_is_valid_ascii(&$string) { return !preg_match('/[^\x00-\x7F]/S', $string); } -/** - * - * Experimental translation feature for Chamilo - * - * Install this in Ubuntu - * - * sudo locale-gen es_ES - * sudo apt-get install php-gettext - * - * Install Spanish locale: $ sudo locale-gen es_ES - * Install English locale: $ sudo locale-gen en_US - * - * To view the list of locales installed in ubuntu - * locale -a - * - * In Debian check this file More info: http://algorytmy.pl/doc/php/ref.gettext.php - * sudo vim /etc/locale.gen - * - * Translate po files using this GUI - * sudo apt-get install poedit - * - * Some help here: - * - * Config getext - * http://zez.org/article/articleview/42/3/ - * * - * Using getext in ubuntu - * http://www.sourcerally.net/regin/49-How-to-get-PHP-and-gettext-working-%28ubuntu,-debian%29 - * - * Getext tutorial - * http://mel.melaxis.com/devblog/2005/08/06/localizing-php-web-sites-using-gettext/ - * - */ -function setting_gettext() { - $domain = 'default'; - $locale = api_get_language_isocode(); - $locale = 'es_ES'; - putenv("LC_ALL=$locale"); - setlocale(LC_ALL, $locale); - bindtextdomain($domain, api_get_path(SYS_LANG_PATH)); - bind_textdomain_codeset($domain, 'UTF-8'); - textdomain($domain); -} - - /** * Return true a date is valid diff --git a/main/inc/lib/internationalization_internal.lib.php b/main/inc/lib/internationalization_internal.lib.php index 1652c58440..06d31d6a56 100755 --- a/main/inc/lib/internationalization_internal.lib.php +++ b/main/inc/lib/internationalization_internal.lib.php @@ -167,7 +167,6 @@ function _api_compare_n_grams(&$n_grams, $encoding, $max_delta = LANGUAGE_DETECT return $result; } - /** * Appendix to "Date and time formats" */ @@ -259,7 +258,6 @@ function _api_clean_person_name($person_name) { return preg_replace(array('/\s+/', '/, ,/', '/,+/', '/^[ ,]/', '/[ ,]$/'), array(' ', ', ', ',', '', ''), $person_name); } - /** * Appendix to "Multibyte string conversion functions" */ @@ -595,7 +593,6 @@ function _api_html_entity_from_unicode($codepoint) { return '&#'.$codepoint.';'; } - /** * Appendix to "Common multibyte string functions" */ @@ -681,7 +678,6 @@ function _api_utf8_ucwords_callback($matches) { return $matches[2] . api_ucfirst(ltrim($matches[0]), 'UTF-8'); } - /** * Appendix to "Common sting operations with arrays" */ @@ -702,7 +698,6 @@ function _api_array_utf8_decode($variable) { return $variable; } - /** * Appendix to "String comparison" */ @@ -832,7 +827,6 @@ function _api_get_collator_sort_flag($sort_flag = SORT_REGULAR) { return Collator::SORT_REGULAR; } - /** * ICU locales (accessible through intl extension). */ @@ -884,7 +878,6 @@ function api_get_default_locale() { return _api_set_default_locale(); } - /** * Appendix to "Encoding management functions" */ @@ -1035,7 +1028,7 @@ function _api_iconv_set_encoding($type, $encoding = null) { } /** - * Ckecks whether a given encoding is known to define single-byte characters only. + * Checks whether a given encoding is known to define single-byte characters only. * The result might be not accurate for unknown by this library encodings. This is not fatal, * then the library picks up conversions plus Unicode related internal algorithms. * @param string $encoding A given encoding identificator. @@ -1131,85 +1124,3 @@ function _api_html_entity_supports($encoding) { } return $supports[$encoding]; } - - -/** - * Upgrading the PHP5 mbstring extension - */ - -// A multibyte replacement of strchr(). This function exists in PHP 5 >= 5.2.0 -// See http://php.net/manual/en/function.mb-strrchr -if (MBSTRING_INSTALLED && !function_exists('mb_strchr')) { - function mb_strchr($haystack, $needle, $part = false, $encoding = null) { - if (empty($encoding)) { - $encoding = mb_internal_encoding(); - } - return mb_strstr($haystack, $needle, $part, $encoding); - } -} - -// A multibyte replacement of stripos(). This function exists in PHP 5 >= 5.2.0 -// See http://php.net/manual/en/function.mb-stripos -if (MBSTRING_INSTALLED && !function_exists('mb_stripos')) { - function mb_stripos($haystack, $needle, $offset = 0, $encoding = null) { - if (empty($encoding)) { - $encoding = mb_internal_encoding(); - } - return mb_strpos(mb_strtolower($haystack, $encoding), mb_strtolower($needle, $encoding), $offset, $encoding); - } -} - -// A multibyte replacement of stristr(). This function exists in PHP 5 >= 5.2.0 -// See http://php.net/manual/en/function.mb-stristr -if (MBSTRING_INSTALLED && !function_exists('mb_stristr')) { - function mb_stristr($haystack, $needle, $part = false, $encoding = null) { - if (empty($encoding)) { - $encoding = mb_internal_encoding(); - } - $pos = mb_strpos(mb_strtolower($haystack, $encoding), mb_strtolower($needle, $encoding), 0, $encoding); - if ($pos === false) { - return false; - } - if ($part) { - return mb_substr($haystack, 0, $pos + 1, $encoding); - } - return mb_substr($haystack, $pos, mb_strlen($haystack, $encoding), $encoding); - } -} - -// A multibyte replacement of strrchr(). This function exists in PHP 5 >= 5.2.0 -// See http://php.net/manual/en/function.mb-strrchr -if (MBSTRING_INSTALLED && !function_exists('mb_strrchr')) { - function mb_strrchr($haystack, $needle, $part = false, $encoding = null) { - if (empty($encoding)) { - $encoding = mb_internal_encoding(); - } - $needle = mb_substr($needle, 0, 1, $encoding); - $pos = mb_strrpos($haystack, $needle, mb_strlen($haystack, $encoding) - 1, $encoding); - if ($pos === false) { - return false; - } - if ($part) { - return mb_substr($haystack, 0, $pos + 1, $encoding); - } - return mb_substr($haystack, $pos, mb_strlen($haystack, $encoding), $encoding); - } -} - -// A multibyte replacement of strstr(). This function exists in PHP 5 >= 5.2.0 -// See http://php.net/manual/en/function.mb-strstr -if (MBSTRING_INSTALLED && !function_exists('mb_strstr')) { - function mb_strstr($haystack, $needle, $part = false, $encoding = null) { - if (empty($encoding)) { - $encoding = mb_internal_encoding(); - } - $pos = mb_strpos($haystack, $needle, 0, $encoding); - if ($pos === false) { - return false; - } - if ($part) { - return mb_substr($haystack, 0, $pos + 1, $encoding); - } - return mb_substr($haystack, $pos, mb_strlen($haystack, $encoding), $encoding); - } -} diff --git a/main/inc/lib/main_api.lib.php b/main/inc/lib/main_api.lib.php index 5ebe790a47..b3895dac39 100644 --- a/main/inc/lib/main_api.lib.php +++ b/main/inc/lib/main_api.lib.php @@ -221,16 +221,6 @@ define('USERNAME_PURIFIER', '/[^0-9A-Za-z_\.]/'); define('USERNAME_PURIFIER_MAIL', '/[^0-9A-Za-z_\.@]/'); define('USERNAME_PURIFIER_SHALLOW', '/\s/'); -// Constants for detection some important PHP5 subversions. -$php_version = (float) PHP_VERSION; - -define('IS_PHP_52', !((float)$php_version < 5.2)); -define('IS_PHP_53', !((float)$php_version < 5.3)); - -define('IS_PHP_SUP_OR_EQ_53', ($php_version >= 5.3)); -define('IS_PHP_SUP_OR_EQ_52', ($php_version >= 5.2 && !IS_PHP_53)); -define('IS_PHP_SUP_OR_EQ_51', ($php_version >= 5.1 && !IS_PHP_52 && !IS_PHP_53)); - // This constant is a result of Windows OS detection, it has a boolean value: // true whether the server runs on Windows OS, false otherwise. define('IS_WINDOWS_OS', api_is_windows_os()); @@ -7222,20 +7212,20 @@ function api_get_origin() function api_get_full_setting($variable, $key = null) { $variable = Database::escape_string($variable); $sql = "SELECT * - FROM settings_current + FROM settings_current WHERE variable = '$variable' "; - + if (!empty($key)) { $key = Database::escape_string($key); $sql .= "AND subkey = '$key'"; } - + $result = Database::query($sql); $setting = array(); - + while ($row = Database::fetch_assoc($result)) { $setting[] = $row; } - + return $setting; }