12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979 |
- <?php
- use Patchwork\Utf8;
- define('SPECIAL_OPENING_TAG', '[=');
- define('SPECIAL_CLOSING_TAG', '=]');
- define('TIME_NO_SEC_FORMAT', 0);
- define('DATE_FORMAT_SHORT', 1);
- define('DATE_FORMAT_LONG', 2);
- define('DATE_FORMAT_LONG_NO_DAY', 10);
- define('DATE_TIME_FORMAT_LONG', 3);
- define('DATE_FORMAT_NUMBER', 4);
- define('DATE_TIME_FORMAT_LONG_24H', 5);
- define('DATE_TIME_FORMAT_SHORT', 6);
- define('DATE_TIME_FORMAT_SHORT_TIME_FIRST', 7);
- define('DATE_FORMAT_NUMBER_NO_YEAR', 8);
- define('DATE_FORMAT_ONLY_DAYNAME', 9);
- define('PERSON_NAME_COMMON_CONVENTION', 0);
- define('PERSON_NAME_WESTERN_ORDER', 1);
- define('PERSON_NAME_EASTERN_ORDER', 2);
- define('PERSON_NAME_LIBRARY_ORDER', 3);
- define('PERSON_NAME_EMAIL_ADDRESS', PERSON_NAME_WESTERN_ORDER);
- define('PERSON_NAME_DATA_EXPORT', PERSON_NAME_EASTERN_ORDER);
- function get_lang($variable, $reserved = null, $language = null) {
- global
-
-
- $language_interface,
-
- $language_interface_initial_value;
- global $used_lang_vars, $_configuration;
-
-
-
- if (isset($_configuration['language_measure_frequency']) &&
- $_configuration['language_measure_frequency'] == 1
- ) {
- require_once api_get_path(SYS_CODE_PATH).'/cron/lang/langstats.class.php';
- global $langstats;
- $langstats->add_use($variable,'');
- }
- if (!isset($used_lang_vars)) {
- $used_lang_vars = array();
- }
-
- static $initialized, $encoding, $langpath, $test_server_mode, $show_special_markup;
- if (!isset($initialized)) {
- $langpath = api_get_path(SYS_LANG_PATH);
- $test_server_mode = api_get_setting('server_type') == 'test';
- $show_special_markup = api_get_setting('hide_dltt_markup') != 'true' || $test_server_mode;
- $initialized = true;
- }
-
- if (empty($language)) {
- $language = $language_interface;
- }
- $lang_postfix = isset($is_interface_language) && $is_interface_language ? '' : '('.$language.')';
- $is_interface_language = $language == $language_interface_initial_value;
-
- static $cache;
-
- if (isset($cache[$language][$variable])) {
-
-
- $ret = $cache[$language][$variable];
- $used_lang_vars[$variable.$lang_postfix] = $ret;
- return $ret;
- }
-
-
-
-
- $read_global_variables = $is_interface_language;
- if ($read_global_variables) {
- if (isset($GLOBALS[$variable])) {
- $langvar = $GLOBALS[$variable];
- } elseif (isset($GLOBALS["lang$variable"])) {
- $langvar = $GLOBALS["lang$variable"];
- } else {
- $langvar = $show_special_markup ? SPECIAL_OPENING_TAG.$variable.SPECIAL_CLOSING_TAG : $variable;
- }
- }
- if (empty($langvar) || !is_string($langvar)) {
- $langvar = $show_special_markup ? SPECIAL_OPENING_TAG.$variable.SPECIAL_CLOSING_TAG : $variable;
- }
- $ret = $cache[$language][$variable] = $langvar;
- $used_lang_vars[$variable.$lang_postfix] = $ret;
- return $ret;
- }
- function api_get_interface_language($purified = false, $check_sub_language = false)
- {
- global $language_interface;
- if (empty($language_interface)) {
- return 'english';
- }
- if ($check_sub_language) {
- static $parent_language_name = null;
- if (!isset($parent_language_name)) {
-
-
- $language_id = api_get_language_id($language_interface);
- $language_info = api_get_language_info($language_id);
- if (
- !empty($language_id) &&
- !empty($language_info)
- ) {
- if (!empty($language_info['parent_id'])) {
- $language_info = api_get_language_info($language_info['parent_id']);
- $parent_language_name = $language_info['english_name'];
- if (!empty($parent_language_name)) {
- return $parent_language_name;
- }
- }
- return $language_info['english_name'];
- }
- return 'english';
- } else {
- return $parent_language_name;
- }
- } else {
-
- $interface_language = $purified ? api_purify_language_id($language_interface) : $language_interface;
- }
- return $interface_language;
- }
- function api_purify_language_id($language) {
- static $purified = array();
- if (!isset($purified[$language])) {
- $purified[$language] = trim(str_replace(array('_unicode', '_latin', '_corporate', '_org', '_km'), '', strtolower($language)));
- }
- return $purified[$language];
- }
- function api_get_language_isocode($language = null, $default_code = 'en')
- {
- static $iso_code = array();
- if (empty($language)) {
- $language = api_get_interface_language(false, true);
- }
- if (!isset($iso_code[$language])) {
- if (!class_exists('Database')) {
- return $default_code;
- }
- $sql_result = Database::query("SELECT isocode FROM ".Database::get_main_table(TABLE_MAIN_LANGUAGE)." WHERE dokeos_folder = '$language'");
- if (Database::num_rows($sql_result)) {
- $result = Database::fetch_array($sql_result);
- $iso_code[$language] = trim($result['isocode']);
- } else {
- $language_purified_id = api_purify_language_id($language);
- $iso_code[$language] = isset($iso_code[$language_purified_id]) ? $iso_code[$language_purified_id] : null;
- }
- if (empty($iso_code[$language])) {
- $iso_code[$language] = $default_code;
- }
- }
- return $iso_code[$language];
- }
- function api_get_platform_isocodes()
- {
- $iso_code = array();
- $sql = "SELECT isocode FROM ".Database::get_main_table(TABLE_MAIN_LANGUAGE)."
- ORDER BY isocode ";
- $sql_result = Database::query($sql);
- if (Database::num_rows($sql_result)) {
- while ($row = Database::fetch_array($sql_result)) {;
- $iso_code[] = trim($row['isocode']);
- }
- }
- return $iso_code;
- }
- function api_get_text_direction($language = null)
- {
- static $text_direction = array();
- if (empty($language)) {
- $language = api_get_interface_language();
- }
- if (!isset($text_direction[$language])) {
- $text_direction[$language] = in_array(api_purify_language_id($language),
- array(
- 'arabic',
- 'ar',
- 'dari',
- 'prs',
- 'hebrew',
- 'he',
- 'iw',
- 'pashto',
- 'ps',
- 'persian',
- 'fa',
- 'ur',
- 'yiddish',
- 'yid'
- )
- ) ? 'rtl' : 'ltr';
- }
- return $text_direction[$language];
- }
- function api_get_timezones()
- {
- $timezone_identifiers = DateTimeZone::listIdentifiers();
- sort($timezone_identifiers);
- $out = array();
- foreach ($timezone_identifiers as $tz) {
- $out[$tz] = $tz;
- }
- $null_option = array('' => '');
- $result = array_merge($null_option, $out);
- return $result;
- }
- function _api_get_timezone()
- {
-
- $to_timezone = date_default_timezone_get();
-
- $timezone_value = api_get_setting('timezone_value', 'timezones');
- if ($timezone_value != null) {
- $to_timezone = $timezone_value;
- }
-
- $use_users_timezone = api_get_setting('use_users_timezone', 'timezones');
- if ($use_users_timezone == 'true') {
- $userId = api_get_user_id();
-
- $timezone_user = UserManager::get_extra_user_data_by_field($userId,'timezone');
- if (isset($timezone_user['timezone']) && $timezone_user['timezone'] != null) {
- $to_timezone = $timezone_user['timezone'];
- }
- }
- return $to_timezone;
- }
- function api_get_utc_datetime($time = null, $return_null_if_invalid_date = false, $returnObj = false)
- {
- $from_timezone = _api_get_timezone();
- $to_timezone = 'UTC';
- if (is_null($time) || empty($time) || $time == '0000-00-00 00:00:00') {
- if ($return_null_if_invalid_date) {
- return null;
- }
- if ($returnObj) {
- return $date = new DateTime(gmdate('Y-m-d H:i:s'));
- }
- return gmdate('Y-m-d H:i:s');
- }
-
- if (is_numeric($time)) {
- $time = intval($time);
- return gmdate('Y-m-d H:i:s', $time);
- }
- try {
- $date = new DateTime($time, new DateTimezone($from_timezone));
- $date->setTimezone(new DateTimeZone($to_timezone));
- if ($returnObj) {
- return $date;
- } else {
- return $date->format('Y-m-d H:i:s');
- }
- } catch (Exception $e) {
- return null;
- }
- }
- function api_get_local_time(
- $time = null,
- $to_timezone = null,
- $from_timezone = null,
- $return_null_if_invalid_date = false,
- $showTime = true,
- $humanForm = false
- ) {
-
- if (is_null($from_timezone)) {
- $from_timezone = 'UTC';
- }
-
- if (is_null($to_timezone)) {
- $to_timezone = _api_get_timezone();
- }
-
- if (is_null($time) || empty($time) || $time == '0000-00-00 00:00:00') {
- if ($return_null_if_invalid_date) {
- return null;
- }
- $from_timezone = 'UTC';
- $time = gmdate('Y-m-d H:i:s');
- }
- if (is_numeric($time)) {
- $time = intval($time);
- $from_timezone = 'UTC';
- $time = gmdate('Y-m-d H:i:s', $time);
- }
- if ($time instanceof DateTime) {
- $time = $time->format('Y-m-d H:i:s');
- $from_timezone = 'UTC';
- }
- try {
- $date = new DateTime($time, new DateTimezone($from_timezone));
- $date->setTimezone(new DateTimeZone($to_timezone));
- return apiGetHumanDateTime($date, $showTime, $humanForm);
- } catch (Exception $e) {
- return null;
- }
- }
- function api_strtotime($time, $timezone = null) {
- $system_timezone = date_default_timezone_get();
- if (!empty($timezone)) {
- date_default_timezone_set($timezone);
- }
- $timestamp = strtotime($time);
- date_default_timezone_set($system_timezone);
- return $timestamp;
- }
- function api_format_date($time, $format = null, $language = null)
- {
- $system_timezone = date_default_timezone_get();
- date_default_timezone_set(_api_get_timezone());
- if (is_string($time)) {
- $time = strtotime($time);
- }
- if (is_null($format)) {
- $format = DATE_TIME_FORMAT_LONG;
- }
- $datetype = null;
- $timetype = null;
- if (is_int($format)) {
- switch ($format) {
- case DATE_FORMAT_ONLY_DAYNAME:
- $date_format = get_lang('dateFormatOnlyDayName', '', $language);
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::SHORT;
- $timetype = IntlDateFormatter::NONE;
- }
- break;
- case DATE_FORMAT_NUMBER_NO_YEAR:
- $date_format = get_lang('dateFormatShortNumberNoYear', '', $language);
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::SHORT;
- $timetype = IntlDateFormatter::NONE;
- }
- break;
- case DATE_FORMAT_NUMBER:
- $date_format = get_lang('dateFormatShortNumber', '', $language);
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::SHORT;
- $timetype = IntlDateFormatter::NONE;
- }
- break;
- case TIME_NO_SEC_FORMAT:
- $date_format = get_lang('timeNoSecFormat', '', $language);
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::NONE;
- $timetype = IntlDateFormatter::SHORT;
- }
- break;
- case DATE_FORMAT_SHORT:
- $date_format = get_lang('dateFormatShort', '', $language);
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::LONG;
- $timetype = IntlDateFormatter::NONE;
- }
- break;
- case DATE_FORMAT_LONG:
- $date_format = get_lang('dateFormatLong', '', $language);
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::NONE;
- }
- break;
- case DATE_TIME_FORMAT_LONG:
- $date_format = get_lang('dateTimeFormatLong', '', $language);
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- }
- break;
- case DATE_FORMAT_LONG_NO_DAY:
- $date_format = get_lang('dateFormatLongNoDay', '', $language);
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- }
- break;
- case DATE_TIME_FORMAT_SHORT:
- $date_format = get_lang('dateTimeFormatShort', '', $language);
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- }
- break;
- case DATE_TIME_FORMAT_SHORT_TIME_FIRST:
- $date_format = get_lang('dateTimeFormatShortTimeFirst', '', $language);
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- }
- break;
- case DATE_TIME_FORMAT_LONG_24H:
- $date_format = get_lang('dateTimeFormatLong24H', '', $language);
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- }
- break;
- default:
- $date_format = get_lang('dateTimeFormatLong', '', $language);
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- }
- }
- } else {
- $date_format = $format;
- }
- if (0) {
-
-
-
- if (is_null($language)) {
- $language = api_get_language_isocode();
- }
- $date_formatter = new IntlDateFormatter($language, $datetype, $timetype, date_default_timezone_get());
-
- $formatted_date = api_to_system_encoding($date_formatter->format($time), 'UTF-8');
- } else {
-
- $translated = &_api_get_day_month_names($language);
- $date_format = str_replace(array('%A', '%a', '%B', '%b'),
- array($translated['days_long'][(int)strftime('%w', $time )],
- $translated['days_short'][(int)strftime('%w', $time)],
- $translated['months_long'][(int)strftime('%m', $time) - 1],
- $translated['months_short'][(int)strftime('%m', $time) - 1]),
- $date_format);
- $formatted_date = api_to_system_encoding(strftime($date_format, $time), 'UTF-8');
- }
- date_default_timezone_set($system_timezone);
- return $formatted_date;
- }
- function date_to_str_ago($date, $timeZone = 'UTC')
- {
- if ($date == '0000-00-00 00:00:00') {
- return '';
- }
- $timeAgo = new TimeAgo($timeZone, api_get_language_isocode());
- return $timeAgo->inWords($date);
- }
- function api_convert_and_format_date($time = null, $format = null, $from_timezone = null) {
-
- $time = api_get_local_time($time, null, $from_timezone);
-
- return api_format_date($time, $format);
- }
- function api_get_week_days_short($language = null) {
- $days = &_api_get_day_month_names($language);
- return $days['days_short'];
- }
- function api_get_week_days_long($language = null) {
- $days = &_api_get_day_month_names($language);
- return $days['days_long'];
- }
- function api_get_months_short($language = null) {
- $months = &_api_get_day_month_names($language);
- return $months['months_short'];
- }
- function api_get_months_long($language = null) {
- $months = &_api_get_day_month_names($language);
- return $months['months_long'];
- }
- function api_get_person_name(
- $first_name,
- $last_name,
- $title = null,
- $format = null,
- $language = null,
- $encoding = null,
- $username = null
- ) {
- static $valid = array();
- if (empty($format)) {
- $format = PERSON_NAME_COMMON_CONVENTION;
- }
-
- if (empty($language)) {
- $language = api_get_interface_language(false, true);
- }
- if (!isset($valid[$format][$language])) {
- if (is_int($format)) {
- switch ($format) {
- case PERSON_NAME_COMMON_CONVENTION:
- $valid[$format][$language] = _api_get_person_name_convention($language, 'format');
- $usernameOrderFromDatabase = api_get_setting('user_name_order');
- if (isset($usernameOrderFromDatabase) && !empty($usernameOrderFromDatabase)) {
- $valid[$format][$language] = $usernameOrderFromDatabase;
- }
- break;
- case PERSON_NAME_WESTERN_ORDER:
- $valid[$format][$language] = '%t %f %l';
- break;
- case PERSON_NAME_EASTERN_ORDER:
- $valid[$format][$language] = '%t %l %f';
- break;
- case PERSON_NAME_LIBRARY_ORDER:
- $valid[$format][$language] = '%t %l, %f';
- break;
- default:
- $valid[$format][$language] = '%t %f %l';
- break;
- }
- } else {
- $valid[$format][$language] = _api_validate_person_name_format($format);
- }
- }
- $format = $valid[$format][$language];
- $keywords = array(
- '%firstname',
- '%f',
- '%F',
- '%lastname',
- '%l',
- '%L',
- '%title',
- '%t',
- '%T',
- '%username',
- '%u',
- '%U',
- );
- $values = array(
- $first_name,
- $first_name,
- api_strtoupper($first_name),
- $last_name,
- $last_name,
- api_strtoupper($last_name),
- $title,
- $title,
- api_strtoupper($title),
- $username,
- $username,
- api_strtoupper($username),
- );
- $person_name = str_replace($keywords, $values, $format);
- return _api_clean_person_name($person_name);
- }
- function api_is_western_name_order($format = null, $language = null)
- {
- static $order = array();
- if (empty($format)) {
- $format = PERSON_NAME_COMMON_CONVENTION;
- }
- if (empty($language)) {
- $language = api_get_interface_language(false, true);
- }
- if (!isset($order[$format][$language])) {
- $test_name = api_get_person_name('%f', '%l', '%t', $format, $language);
- $order[$format][$language] = stripos($test_name, '%f') <= stripos($test_name, '%l');
- }
- return $order[$format][$language];
- }
- function api_sort_by_first_name($language = null) {
- $userNameSortBy = api_get_setting('user_name_sort_by');
- if (!empty($userNameSortBy) && in_array($userNameSortBy, array('firstname', 'lastname'))) {
- return $userNameSortBy == 'firstname' ? true : false;
- }
- static $sort_by_first_name = array();
- if (empty($language)) {
- $language = api_get_interface_language(false, true);
- }
- if (!isset($sort_by_first_name[$language])) {
- $sort_by_first_name[$language] = _api_get_person_name_convention($language, 'sort_by');
- }
- return $sort_by_first_name[$language];
- }
- function api_convert_encoding($string, $to_encoding, $from_encoding = 'UTF-8')
- {
- return mb_convert_encoding($string, $to_encoding, $from_encoding);
- }
- function api_utf8_encode($string, $from_encoding = 'UTF-8')
- {
- return mb_convert_encoding($string, 'UTF-8', $from_encoding);
- }
- function api_utf8_decode($string, $to_encoding = null)
- {
- return mb_convert_encoding($string, $to_encoding, 'UTF-8');
- }
- function api_to_system_encoding($string, $from_encoding = null, $check_utf8_validity = false)
- {
- $system_encoding = api_get_system_encoding();
- return api_convert_encoding($string, $system_encoding, $from_encoding);
- }
- function api_htmlentities($string, $quote_style = ENT_COMPAT, $encoding = 'UTF-8')
- {
- switch ($quote_style) {
- case ENT_COMPAT:
- $string = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string);
- break;
- case ENT_QUOTES:
- $string = str_replace(array('&', '\'', '"', '<', '>'), array('&', ''', '"', '<', '>'), $string);
- break;
- }
- return mb_convert_encoding($string, 'HTML-ENTITIES', 'UTF-8');
- }
- function api_html_entity_decode($string, $quote_style = ENT_COMPAT, $encoding = 'UTF-8') {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (api_is_encoding_supported($encoding)) {
- if (!api_is_utf8($encoding)) {
- $string = api_utf8_encode($string, $encoding);
- }
- $string = html_entity_decode($string, $quote_style, 'UTF-8');
- if (!api_is_utf8($encoding)) {
- return api_utf8_decode($string, $encoding);
- }
- return $string;
- }
- return $string;
- }
- function api_xml_http_response_encode($string, $from_encoding = 'UTF8')
- {
- if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
- if (empty($from_encoding)) {
- $from_encoding = _api_mb_internal_encoding();
- }
- if (!api_is_utf8($from_encoding)) {
- return api_utf8_encode($string, $from_encoding);
- }
- }
- return $string;
- }
- function api_transliterate($string, $unknown = '?', $from_encoding = null)
- {
- return URLify::transliterate($string);
- }
- function api_ord($character, $encoding) {
- return Patchwork\Utf8::ord(api_utf8_encode($character, $encoding));
- }
- function api_str_ireplace($search, $replace, $subject, & $count = null, $encoding = null)
- {
- return Utf8::str_ireplace($search, $replace, $subject, $count);
- }
- function api_str_split($string, $split_length = 1, $encoding = null)
- {
- return Utf8::str_split($string, $split_length);
- }
- function api_stripos($haystack, $needle, $offset = 0, $encoding = null)
- {
- return Utf8::stripos($haystack, $needle, $offset);
- }
- function api_stristr($haystack, $needle, $before_needle = false, $encoding = null)
- {
- return Utf8::stristr($haystack, $needle, $before_needle);
- }
- function api_strlen($string, $encoding = null)
- {
- return Utf8::strlen($string);
- }
- function api_strpos($haystack, $needle, $offset = 0, $encoding = null)
- {
- return Utf8::strpos($haystack, $needle, $offset);
- }
- function api_strrchr($haystack, $needle, $before_needle = false, $encoding = null)
- {
- return Utf8::strrchr($haystack, $needle);
- }
- function api_strrev($string, $encoding = null)
- {
- return Utf8::strrev($string);
- }
- function api_strripos($haystack, $needle, $offset = 0, $encoding = null)
- {
- return Utf8::strripos($haystack, $needle, $offset);
- }
- function api_strrpos($haystack, $needle, $offset = 0, $encoding = null)
- {
- return Utf8::strrpos($haystack, $needle, $offset);
- }
- function api_strstr($haystack, $needle, $before_needle = false, $encoding = null)
- {
- return Utf8::strstr($haystack, $needle, $before_needle);
- }
- function api_strtolower($string, $encoding = null)
- {
- return Utf8::strtolower($string);
- }
- function api_strtoupper($string, $encoding = null)
- {
- return Utf8::strtoupper($string);
- }
- function api_substr($string, $start, $length = null, $encoding = null)
- {
- if (is_null($length)) {
- $length = api_strlen($string, $encoding);
- }
- return Utf8::substr($string, $start, $length);
- }
- function api_substr_count($haystack, $needle, $encoding = null)
- {
- return Utf8::substr_count($haystack, $needle);
- }
- function api_substr_replace($string, $replacement, $start, $length = null, $encoding = null)
- {
- if (is_null($length)) {
- $length = api_strlen($string);
- }
- return UTf8::substr_replace($string, $replacement, $start, $length);
- }
- function api_ucfirst($string, $encoding = null)
- {
- return Utf8::ucfirst($string);
- }
- function api_ucwords($string, $encoding = null)
- {
- return Utf8::ucwords($string);
- }
- function api_preg_match($pattern, $subject, &$matches = null, $flags = 0, $offset = 0, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- return preg_match(api_is_utf8($encoding) ? $pattern.'u' : $pattern, $subject, $matches, $flags, $offset);
- }
- function api_preg_match_all($pattern, $subject, &$matches, $flags = PREG_PATTERN_ORDER, $offset = 0, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (is_null($flags)) {
- $flags = PREG_PATTERN_ORDER;
- }
- return preg_match_all(api_is_utf8($encoding) ? $pattern.'u' : $pattern, $subject, $matches, $flags, $offset);
- }
- function api_preg_replace($pattern, $replacement, $subject, $limit = -1, &$count = 0, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- $is_utf8 = api_is_utf8($encoding);
- if (is_array($pattern)) {
- foreach ($pattern as &$p) {
- $p = $is_utf8 ? $p.'u' : $p;
- }
- } else {
- $pattern = $is_utf8 ? $pattern.'u' : $pattern;
- }
- return preg_replace($pattern, $replacement, $subject, $limit, $count);
- }
- function api_preg_split($pattern, $subject, $limit = -1, $flags = 0, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- return preg_split(api_is_utf8($encoding) ? $pattern.'u' : $pattern, $subject, $limit, $flags);
- }
- function api_strcasecmp($string1, $string2, $language = null, $encoding = null) {
- return api_strcmp(api_strtolower($string1, $encoding), api_strtolower($string2, $encoding), $language, $encoding);
- }
- function api_strcmp($string1, $string2, $language = null, $encoding = null)
- {
- return strcmp($string1, $string2);
- }
- function api_strnatcmp($string1, $string2, $language = null, $encoding = null)
- {
- return strnatcmp($string1, $string2);
- }
- function api_natsort(&$array, $language = null, $encoding = null)
- {
- return natsort($array);
- }
- function api_natrsort(&$array, $language = null, $encoding = null)
- {
- return uasort($array, '_api_strnatrcmp');
- }
- function api_refine_encoding_id($encoding) {
- if (is_array($encoding)){
- return array_map('api_refine_encoding_id', $encoding);
- }
- return strtoupper(str_replace('_', '-', $encoding));
- }
- function api_equal_encodings($encoding1, $encoding2, $strict = false) {
- static $equal_encodings = array();
- if (is_array($encoding1)) {
- foreach ($encoding1 as $encoding) {
- if (api_equal_encodings($encoding, $encoding2, $strict)) {
- return true;
- }
- }
- return false;
- }
- elseif (is_array($encoding2)) {
- foreach ($encoding2 as $encoding) {
- if (api_equal_encodings($encoding1, $encoding, $strict)) {
- return true;
- }
- }
- return false;
- }
- if (!isset($equal_encodings[$encoding1][$encoding2][$strict])) {
- $encoding_1 = api_refine_encoding_id($encoding1);
- $encoding_2 = api_refine_encoding_id($encoding2);
- if ($encoding_1 == $encoding_2) {
- $result = true;
- } else {
- if ($strict) {
- $result = false;
- } else {
- $alias1 = _api_get_character_map_name($encoding_1);
- $alias2 = _api_get_character_map_name($encoding_2);
- $result = !empty($alias1) && !empty($alias2) && $alias1 == $alias2;
- }
- }
- $equal_encodings[$encoding1][$encoding2][$strict] = $result;
- }
- return $equal_encodings[$encoding1][$encoding2][$strict];
- }
- function api_is_utf8($encoding)
- {
- static $result = array();
- if (!isset($result[$encoding])) {
- $result[$encoding] = api_equal_encodings($encoding, 'UTF-8');
- }
- return $result[$encoding];
- }
- function api_get_system_encoding()
- {
- return 'UTF-8';
- }
- function api_is_encoding_supported($encoding) {
- static $supported = array();
- if (!isset($supported[$encoding])) {
- $supported[$encoding] = _api_mb_supports($encoding) || _api_iconv_supports($encoding) || _api_convert_encoding_supports($encoding);
- }
- return $supported[$encoding];
- }
- function api_detect_encoding($string, $language = null) {
-
- if (api_is_valid_utf8($string)) {
- return 'UTF-8';
- }
- return mb_detect_encoding($string);
- }
- function api_is_valid_utf8(&$string)
- {
- return Utf8::isUtf8($string);
- }
- function api_is_valid_ascii(&$string)
- {
- return mb_detect_encoding($string, 'ASCII', true) == 'ASCII' ? true : false;
- }
- function api_is_valid_date($date, $format = 'Y-m-d H:i:s')
- {
- $d = DateTime::createFromFormat($format, $date);
- return $d && $d->format($format) == $date;
- }
- function get_plugin_lang($variable, $pluginName) {
- $plugin = $pluginName::create();
- return $plugin->get_lang($variable);
- }
- function &_api_get_day_month_names($language = null) {
- static $date_parts = array();
- if (empty($language)) {
- $language = api_get_interface_language();
- }
- if (!isset($date_parts[$language])) {
- $week_day = array(
- 'Sunday',
- 'Monday',
- 'Tuesday',
- 'Wednesday',
- 'Thursday',
- 'Friday',
- 'Saturday',
- );
- $month = array(
- 'January',
- 'February',
- 'March',
- 'April',
- 'May',
- 'June',
- 'July',
- 'August',
- 'September',
- 'October',
- 'November',
- 'December',
- );
- for ($i = 0; $i < 7; $i++) {
- $date_parts[$language]['days_short'][] = get_lang($week_day[$i].'Short', '', $language);
- $date_parts[$language]['days_long'][] = get_lang($week_day[$i].'Long', '', $language);
- }
- for ($i = 0; $i < 12; $i++) {
- $date_parts[$language]['months_short'][] = get_lang($month[$i].'Short', '', $language);
- $date_parts[$language]['months_long'][] = get_lang($month[$i].'Long', '', $language);
- }
- }
- return $date_parts[$language];
- }
- function _api_get_person_name_convention($language, $type)
- {
- static $conventions;
- $language = api_purify_language_id($language);
- if (!isset($conventions)) {
- $file = dirname(__FILE__).'/internationalization_database/name_order_conventions.php';
- if (file_exists($file)) {
- $conventions = include ($file);
- } else {
- $conventions = array(
- 'english' => array(
- 'format' => 'title first_name last_name',
- 'sort_by' => 'first_name'
- )
- );
- }
-
- $customConventions = api_get_configuration_value('name_order_conventions');
- if (!empty($customConventions)) {
- foreach ($customConventions as $key => $data) {
- $conventions[$key] = $data;
- }
- }
- $search1 = array('FIRST_NAME', 'LAST_NAME', 'TITLE');
- $replacement1 = array('%F', '%L', '%T');
- $search2 = array('first_name', 'last_name', 'title');
- $replacement2 = array('%f', '%l', '%t');
- foreach (array_keys($conventions) as $key) {
- $conventions[$key]['format'] = str_replace($search1, $replacement1, $conventions[$key]['format']);
- $conventions[$key]['format'] = _api_validate_person_name_format(_api_clean_person_name(str_replace('%', ' %', str_ireplace($search2, $replacement2, $conventions[$key]['format']))));
- $conventions[$key]['sort_by'] = strtolower($conventions[$key]['sort_by']) != 'last_name' ? true : false;
- }
- }
- switch ($type) {
- case 'format':
- return is_string($conventions[$language]['format']) ? $conventions[$language]['format'] : '%t %f %l';
- case 'sort_by':
- return is_bool($conventions[$language]['sort_by']) ? $conventions[$language]['sort_by'] : true;
- }
- return null;
- }
- function _api_validate_person_name_format($format) {
- if (empty($format) || stripos($format, '%f') === false || stripos($format, '%l') === false) {
- return '%t %f %l';
- }
- return $format;
- }
- function _api_clean_person_name($person_name) {
- return preg_replace(array('/\s+/', '/, ,/', '/,+/', '/^[ ,]/', '/[ ,]$/'), array(' ', ', ', ',', '', ''), $person_name);
- }
- function _api_convert_encoding(&$string, $to_encoding, $from_encoding)
- {
- return mb_convert_encoding($string, $to_encoding, $from_encoding);
- }
- function _api_get_character_map_name($encoding) {
- static $character_map_selector;
- if (!isset($character_map_selector)) {
- $file = dirname(__FILE__).'/internationalization_database/conversion/character_map_selector.php';
- if (file_exists($file)) {
- $character_map_selector = include ($file);
- } else {
- $character_map_selector = array();
- }
- }
- return isset($character_map_selector[$encoding]) ? $character_map_selector[$encoding] : '';
- }
- function _api_strnatrcmp($string1, $string2)
- {
- return strnatcmp($string2, $string1);
- }
- function _api_mb_internal_encoding($encoding = 'UTF-8')
- {
- return mb_internal_encoding($encoding);
- }
- function _api_mb_supports($encoding) {
- static $supported = array();
- if (!isset($supported[$encoding])) {
- if (MBSTRING_INSTALLED) {
- $supported[$encoding] = api_equal_encodings($encoding, mb_list_encodings(), true);
- } else {
- $supported[$encoding] = false;
- }
- }
- return $supported[$encoding];
- }
- function _api_iconv_supports($encoding) {
- static $supported = array();
- if (!isset($supported[$encoding])) {
- if (ICONV_INSTALLED) {
- $enc = api_refine_encoding_id($encoding);
- if ($enc != 'HTML-ENTITIES') {
- $test_string = '';
- for ($i = 32; $i < 128; $i++) {
- $test_string .= chr($i);
- }
- $supported[$encoding] = (@iconv_strlen($test_string, $enc)) ? true : false;
- } else {
- $supported[$encoding] = false;
- }
- } else {
- $supported[$encoding] = false;
- }
- }
- return $supported[$encoding];
- }
- function _api_convert_encoding_supports($encoding) {
- static $supports = array();
- if (!isset($supports[$encoding])) {
- $supports[$encoding] = _api_get_character_map_name(api_refine_encoding_id($encoding)) != '';
- }
- return $supports[$encoding];
- }
- function apiGetHumanDateTime($date, $showTime = true, $humanForm = false) {
- if ($showTime) {
- if ($humanForm) {
- return $date->format('j M Y H:i:s');
- } else {
- return $date->format('Y-m-d H:i:s');
- }
- } else {
- if ($humanForm) {
- return $date->format('j M Y');
- } else {
- return $date->format('Y-m-d');
- }
- }
- }
|