1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786 |
- <?php
- use Patchwork\Utf8;
- use Chamilo\CoreBundle\Framework\Container;
- use Symfony\Component\Intl\Intl;
- 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);
- define('LANGUAGE_DETECT_MAX_LENGTH', 2000);
- define('LANGUAGE_DETECT_MAX_DELTA', 140000);
- function get_lang($variable, $reserved = null, $language = null)
- {
- $defaultDomain = 'messages';
- $translated = Container::getTranslator()->trans(
- $variable,
- array(),
- $defaultDomain
- );
- if ($translated == $variable) {
-
- $translated = Container::getTranslator()->trans(
- "lang$variable",
- array(),
- $defaultDomain
- );
- if ($translated == "lang$variable") {
- return $variable;
- }
- }
- return $translated;
- }
- function api_get_language_isocode()
- {
- return Container::getTranslator()->getLocale();
- }
- 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 = '')
- {
- static $textDirection = array();
- if (empty($language)) {
- $language = api_get_language_isocode();
- }
- if (!isset($textDirection[$language])) {
- $langList = array(
- 'ar',
- 'he',
- 'iw',
- 'ps',
- 'fa',
- 'ur',
- 'yi',
- );
- $textDirection[$language] = in_array(
- $language,
- $langList
- ) ? 'rtl' : 'ltr';
- }
- return $textDirection[$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()
- {
- $timezone = api_get_setting('platform.timezone');
-
- $use_users_timezone = api_get_setting('profile.use_users_timezone');
- 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) {
- $timezone = $timezone_user['timezone'];
- }
- }
- return $timezone;
- }
- function api_get_utc_datetime($time = null, $return_null_if_invalid_date = 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;
- }
- 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));
- 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)
- {
-
- 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 $date->format('Y-m-d H:i:s');
- } 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:
-
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::SHORT;
- $timetype = IntlDateFormatter::NONE;
- }
- break;
- case DATE_FORMAT_NUMBER_NO_YEAR:
-
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::SHORT;
- $timetype = IntlDateFormatter::NONE;
- }
- break;
- case DATE_FORMAT_NUMBER:
-
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::SHORT;
- $timetype = IntlDateFormatter::NONE;
- }
- break;
- case TIME_NO_SEC_FORMAT:
-
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::NONE;
- $timetype = IntlDateFormatter::SHORT;
- }
- break;
- case DATE_FORMAT_SHORT:
-
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::LONG;
- $timetype = IntlDateFormatter::NONE;
- }
- break;
- case DATE_FORMAT_LONG:
-
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::NONE;
- }
- break;
- case DATE_TIME_FORMAT_LONG:
-
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- }
- break;
- case DATE_FORMAT_LONG_NO_DAY:
-
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- }
- break;
- case DATE_TIME_FORMAT_SHORT:
-
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- }
- break;
- case DATE_TIME_FORMAT_SHORT_TIME_FIRST:
-
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- }
- break;
- case DATE_TIME_FORMAT_LONG_24H:
-
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- }
- break;
- default:
-
- if (INTL_INSTALLED) {
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- }
- }
- }
-
- if (is_null($language)) {
- $language = api_get_language_isocode();
- }
- $dateFormatter = new IntlDateFormatter(
- $language,
- $datetype,
- $timetype,
- date_default_timezone_get()
- );
-
- $formattedDate = api_to_system_encoding(
- $dateFormatter->format($time),
- 'UTF-8'
- );
- date_default_timezone_set($system_timezone);
- return $formattedDate;
- }
- function date_to_str_ago($date)
- {
- static $initialized = false;
- static $today, $yesterday;
- static $min_decade, $min_year, $min_month, $min_week, $min_day, $min_hour, $min_minute;
- static $min_decades, $min_years, $min_months, $min_weeks, $min_days, $min_hours, $min_minutes;
- static $sec_time_time, $sec_time_sing, $sec_time_plu;
- $system_timezone = date_default_timezone_get();
- date_default_timezone_set(_api_get_timezone());
- if (!$initialized) {
- $today = get_lang('Today');
- $yesterday = get_lang('Yesterday');
- $min_decade = get_lang('MinDecade');
- $min_year = get_lang('MinYear');
- $min_month = get_lang('MinMonth');
- $min_week = get_lang('MinWeek');
- $min_day = get_lang('MinDay');
- $min_hour = get_lang('MinHour');
- $min_minute = get_lang('MinMinute');
- $min_decades = get_lang('MinDecades');
- $min_years = get_lang('MinYears');
- $min_months = get_lang('MinMonths');
- $min_weeks = get_lang('MinWeeks');
- $min_days = get_lang('MinDays');
- $min_hours = get_lang('MinHours');
- $min_minutes = get_lang('MinMinutes');
-
-
-
- $sec_time_time = array(315569260, 31556926, 2629743.83, 604800, 86400, 3600, 60);
- $sec_time_sing = array($min_decade, $min_year, $min_month, $min_week, $min_day, $min_hour, $min_minute);
- $sec_time_plu = array($min_decades, $min_years, $min_months, $min_weeks, $min_days, $min_hours, $min_minutes);
- $initialized = true;
- }
- $dst_date = is_string($date) ? strtotime($date) : $date;
-
- $date_array = date('s/i/G/j/n/Y', $dst_date);
- $date_split = explode('/', $date_array);
- $dst_s = $date_split[0];
- $dst_m = $date_split[1];
- $dst_h = $date_split[2];
- $dst_day = $date_split[3];
- $dst_mth = $date_split[4];
- $dst_yr = $date_split[5];
- $dst_date = mktime($dst_h, $dst_m, $dst_s, $dst_mth, $dst_day, $dst_yr);
- $time = $offset = time() - $dst_date;
-
- $act_day = date('d');
- $act_mth = date('n');
- $act_yr = date('Y');
- if ($dst_day == $act_day && $dst_mth == $act_mth && $dst_yr == $act_yr) {
- return $today;
- }
- if ($dst_day == $act_day - 1 && $dst_mth == $act_mth && $dst_yr == $act_yr) {
- return $yesterday;
- }
- $str_result = array();
- $time_result = array();
- $key_result = array();
- $str = '';
- $i = 0;
- for ($i = 0; $i < count($sec_time_time); $i++) {
- $seconds = $sec_time_time[$i];
- if ($seconds > $time) {
- continue;
- }
- $current_value = intval($time/$seconds);
- if ($current_value != 1) {
- $date_str = $sec_time_plu[$i];
- } else {
- $date_str = $sec_time_sing[$i];
- }
- $key_result[] = $sec_time_sing[$i];
- $str_result[] = $current_value.' '.$date_str;
- $time_result[] = $current_value;
- $str .= $current_value.$date_str;
- $time %= $seconds;
- }
- if (!empty($key_result)) {
- if ($key_result[0] == $min_day && $key_result[1]== $min_minute) {
- $key_result[1] = ' 0 '.$min_hours;
- $str_result[0] = $time_result[0].' '.$key_result[0];
- $str_result[1] = $key_result[1];
- }
- if ($key_result[0] == $min_year && ($key_result[1] == $min_day || $key_result[1] == $min_week)) {
- $key_result[1] = ' 0 '.$min_months;
- $str_result[0] = $time_result[0].' '.$key_result[0];
- $str_result[1] = $key_result[1];
- }
- }
- if (!empty($str_result)) {
- if (!empty($str_result[1])) {
- $str = $str_result[0].', '.$str_result[1];
- } else {
- $str = $str_result[0];
- }
- }
- date_default_timezone_set($system_timezone);
- return $str;
- }
- 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_language_isocode();
- }
- 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(
- 'display.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_language_isocode();
- }
- 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('display.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_language_isocode();
- }
- 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,
- $fromFencoding = null,
- $check_utf8_validity = false
- )
- {
- $systemEncoding = api_get_system_encoding();
- return api_convert_encoding($string, $systemEncoding, $fromFencoding);
- }
- 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_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;
- }
- 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_is_utf8($encoding)
- {
- $encoding = strtolower($encoding);
- return $encoding == 'utf-8';
- }
- function api_get_system_encoding()
- {
- return 'UTF-8';
- }
- 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_language_isocode();
- }
- 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;
- if (!isset($conventions)) {
- $file = dirname(__FILE__).'/internationalization_database/name_order_conventions.php';
- if (file_exists($file)) {
- $conventions = include($file);
- } else {
- $conventions = array(
- 'en' => 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_strnatrcmp($string1, $string2)
- {
- return strnatcmp($string2, $string1);
- }
- function _api_mb_internal_encoding($encoding = 'UTF-8')
- {
- return mb_internal_encoding($encoding);
- }
|