1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723 |
- <?php
- use Patchwork\Utf8 as u;
- use Chamilo\CoreBundle\Framework\Container;
- use Symfony\Component\Intl\DateFormatter\IntlDateFormatter;
- 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)
- {
- $defaultDomain = 'all';
- $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_interface_language($purified = false, $check_sub_language = false)
- {
- global $app;
- return $app['language'];
- }
- function api_get_valid_language($language)
- {
- static $enabled_languages;
- if (!isset($enabled_languages)) {
- $enabled_languages_info = api_get_languages();
- $enabled_languages = $enabled_languages_info['folder'];
- }
- $language = str_replace('_km', '_KM', strtolower(trim($language)));
- if (empty($language) || !in_array($language, $enabled_languages)) {
- $language = api_get_setting('platformLanguage');
- }
- return $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')
- {
- return Container::getTranslator()->getLocale();
- }
- function api_get_platform_isocodes()
- {
- $iso_code = array();
- $sql_result = Database::query(
- "SELECT isocode FROM ".Database::get_main_table(TABLE_MAIN_LANGUAGE)."
- ORDER BY isocode "
- );
- 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()
- {
- return date_default_timezone_get();
- $userId = api_get_user_id();
-
- $to_timezone = date_default_timezone_get();
-
-
-
- $use_users_timezone = api_get_setting('use_users_timezone', 'timezones');
- if ($use_users_timezone == 'true' && !empty($userId) && !api_is_anonymous()) {
- $userInfo = api_get_user_info();
- $extraFields = $userInfo['extra_fields'];
-
-
- if (isset($extraFields['extra_timezone']) && $extraFields['extra_timezone'] != null) {
- $to_timezone = $extraFields['extra_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
- ) {
-
- 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);
- }
- 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)
- {
- 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:
- $datetype = IntlDateFormatter::SHORT;
- $timetype = IntlDateFormatter::NONE;
- break;
- case DATE_FORMAT_NUMBER_NO_YEAR:
- $datetype = IntlDateFormatter::SHORT;
- $timetype = IntlDateFormatter::NONE;
- break;
- case DATE_FORMAT_NUMBER:
- $datetype = IntlDateFormatter::SHORT;
- $timetype = IntlDateFormatter::NONE;
- break;
- case TIME_NO_SEC_FORMAT:
- $datetype = IntlDateFormatter::NONE;
- $timetype = IntlDateFormatter::SHORT;
- break;
- case DATE_FORMAT_SHORT:
- $datetype = IntlDateFormatter::LONG;
- $timetype = IntlDateFormatter::NONE;
- break;
- case DATE_FORMAT_LONG:
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::NONE;
- break;
- case DATE_TIME_FORMAT_LONG:
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- break;
- case DATE_FORMAT_LONG_NO_DAY:
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- break;
- case DATE_TIME_FORMAT_SHORT:
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- break;
- case DATE_TIME_FORMAT_SHORT_TIME_FIRST:
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- break;
- case DATE_TIME_FORMAT_LONG_24H:
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- break;
- default:
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- }
- }
-
- 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'
- );
- return $formatted_date;
- }
- 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 ($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[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)
- {
- static $valid = array();
- if (empty($format)) {
- $format = PERSON_NAME_COMMON_CONVENTION;
- }
- if (empty($language)) {
- $language = api_get_interface_language(false, true);
- }
- if (empty($encoding)) {
- $encoding = mb_internal_encoding();
- }
- 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];
- $person_name = str_replace(array('%f', '%l', '%t'), array($first_name, $last_name, $title), $format);
- if (strpos($format, '%F') !== false || strpos($format, '%L') !== false || strpos($format, '%T') !== false) {
- $person_name = str_replace(
- array(
- '%F',
- '%L',
- '%T'
- ),
- array(
- api_strtoupper($first_name, $encoding),
- api_strtoupper($last_name, $encoding),
- api_strtoupper($title, $encoding)
- ),
- $person_name
- );
- }
- 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('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_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 = null)
- {
- return mb_convert_encoding($string, $to_encoding, $from_encoding);
- }
- function api_utf8_encode($string, $from_encoding = null)
- {
- return u::utf8_encode($string);
- }
- function api_utf8_decode($string, $to_encoding = null)
- {
- return u::utf8_decode($string);
- }
- 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 = null)
- {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- return htmlentities($string, $quote_style, $encoding);
- }
- function _api_html_entity_supports($encoding) {
- static $supports = array();
- if (!isset($supports[$encoding])) {
-
- $html_entity_encodings = array(
- 'ISO-8859-1',
- 'ISO-8859-15',
- 'UTF-8',
- 'CP866',
- 'CP1251',
- 'CP1252',
- 'KOI8-R',
- 'BIG5', '950',
- 'GB2312', '936',
- 'BIG5-HKSCS',
- 'Shift_JIS', 'SJIS', '932',
- 'EUC-JP', 'EUCJP'
- );
- $supports[$encoding] = api_equal_encodings($encoding, $html_entity_encodings);
- }
- return $supports[$encoding];
- }
- function api_html_entity_decode($string, $quote_style = ENT_COMPAT, $encoding = null)
- {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- return html_entity_decode($string, $quote_style, $encoding);
- }
- function api_xml_http_response_encode($string, $from_encoding = null)
- {
- 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_mb_internal_encoding()
- {
- return mb_internal_encoding();
- }
- function api_transliterate($string, $unknown = '?', $from_encoding = null)
- {
- return URLify::transliterate($string);
- }
- function api_str_ireplace($search, $replace, $subject, & $count = null, $encoding = null)
- {
- return str_ireplace($search, $replace, $subject, $count);
- }
- function api_str_split($string, $split_length = 1, $encoding = null)
- {
- return str_split($string, $split_length);
- }
- function api_stripos($haystack, $needle, $offset = 0, $encoding = null)
- {
- return stripos($haystack, $needle, $offset);
- }
- function api_stristr($haystack, $needle, $before_needle = false, $encoding = null)
- {
- return stristr($haystack, $needle, $before_needle);
- }
- function api_strlen($string, $encoding = null)
- {
- return mb_strlen($string);
- }
- function api_strpos($haystack, $needle, $offset = 0, $encoding = null)
- {
- return mb_strpos($haystack, $needle, $offset, $encoding);
- }
- function api_strripos($haystack, $needle, $offset = 0, $encoding = null)
- {
- return mb_strripos($haystack, $needle, $offset, $encoding);
- }
- function api_strrpos($haystack, $needle, $offset = 0, $encoding = null)
- {
- return mb_strrpos($haystack, $needle, $offset);
- }
- function api_strstr($haystack, $needle, $before_needle = false, $encoding = null)
- {
- return mb_strstr($haystack, $needle, $before_needle);
- }
- function api_strtolower($string, $encoding = null)
- {
- return strtolower($string);
- }
- function api_strtoupper($string, $encoding = null)
- {
- return strtoupper($string);
- }
- function api_substr($string, $start, $length = null, $encoding = null)
- {
- return substr($string, $start, $length);
- }
- function api_substr_replace($string, $replacement, $start, $length = null, $encoding = null)
- {
- return substr_replace($string, $replacement, $start, $length);
- }
- function api_ucfirst($string, $encoding = null)
- {
- return ucfirst($string);
- }
- function api_ucwords($string, $encoding = null)
- {
- return 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_replace_callback($pattern, $callback, $subject, $limit = -1, &$count = 0, $encoding = null)
- {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (is_array($pattern)) {
- foreach ($pattern as &$p) {
- $p = api_is_utf8($encoding) ? $p.'u' : $p;
- }
- } else {
- $pattern = api_is_utf8($encoding) ? $pattern.'u' : $pattern;
- }
- return preg_replace_callback($pattern, $callback, $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_ereg($pattern, $string, & $regs = null)
- {
- $count = func_num_args();
- $encoding = _api_mb_regex_encoding();
- if (_api_mb_supports($encoding)) {
- if ($count < 3) {
- return @mb_ereg($pattern, $string);
- }
- return @mb_ereg($pattern, $string, $regs);
- }
- if (MBSTRING_INSTALLED && api_is_encoding_supported($encoding)) {
- global $_api_encoding;
- $_api_encoding = $encoding;
- _api_mb_regex_encoding('UTF-8');
- if ($count < 3) {
- $result = @mb_ereg(api_utf8_encode($pattern, $encoding), api_utf8_encode($string, $encoding));
- } else {
- $result = @mb_ereg(api_utf8_encode($pattern, $encoding), api_utf8_encode($string, $encoding), $regs);
- $regs = _api_array_utf8_decode($regs);
- }
- _api_mb_regex_encoding($encoding);
- return $result;
- }
- if ($count < 3) {
- return ereg($pattern, $string);
- }
- return ereg($pattern, $string, $regs);
- }
- function api_eregi($pattern, $string, & $regs = null)
- {
- $count = func_num_args();
- $encoding = _api_mb_regex_encoding();
- if (_api_mb_supports($encoding)) {
- if ($count < 3) {
- return @mb_eregi($pattern, $string);
- }
- return @mb_eregi($pattern, $string, $regs);
- }
- if (MBSTRING_INSTALLED && api_is_encoding_supported($encoding)) {
- global $_api_encoding;
- $_api_encoding = $encoding;
- _api_mb_regex_encoding('UTF-8');
- if ($count < 3) {
- $result = @mb_eregi(api_utf8_encode($pattern, $encoding), api_utf8_encode($string, $encoding));
- } else {
- $result = @mb_eregi(api_utf8_encode($pattern, $encoding), api_utf8_encode($string, $encoding), $regs);
- $regs = _api_array_utf8_decode($regs);
- }
- _api_mb_regex_encoding($encoding);
- return $result;
- }
- if ($count < 3) {
- return eregi($pattern, $string);
- }
- return eregi($pattern, $string, $regs);
- }
- function api_eregi_replace($pattern, $replacement, $string, $option = null)
- {
- $encoding = _api_mb_regex_encoding();
- if (_api_mb_supports($encoding)) {
- if (is_null($option)) {
- return @mb_eregi_replace($pattern, $replacement, $string);
- }
- return @mb_eregi_replace($pattern, $replacement, $string, $option);
- }
- if (MBSTRING_INSTALLED && api_is_encoding_supported($encoding)) {
- _api_mb_regex_encoding('UTF-8');
- if (is_null($option)) {
- $result = api_utf8_decode(
- @mb_eregi_replace(
- api_utf8_encode($pattern, $encoding),
- api_utf8_encode($replacement, $encoding),
- api_utf8_encode($string, $encoding)
- ),
- $encoding
- );
- } else {
- $result = api_utf8_decode(
- @mb_eregi_replace(
- api_utf8_encode($pattern, $encoding),
- api_utf8_encode($replacement, $encoding),
- api_utf8_encode($string, $encoding),
- $option
- ),
- $encoding
- );
- }
- _api_mb_regex_encoding($encoding);
- return $result;
- }
- return eregi_replace($pattern, $replacement, $string);
- }
- function api_strcasecmp($string1, $string2, $language = null, $encoding = null)
- {
- return strcasecmp($string1, $string2);
- }
- function api_strcmp($string1, $string2, $language = null, $encoding = null)
- {
- return strcmp($string1, $string2);
- }
- function api_strnatcasecmp($string1, $string2, $language = null, $encoding = null)
- {
- return strnatcasecmp($string1, $string2);
- }
- function api_strnatcmp($string1, $string2, $language = null, $encoding = null)
- {
- return strnatcmp($string1, $string2);
- }
- function api_asort(&$array, $sort_flag = SORT_REGULAR, $language = null, $encoding = null)
- {
- if (INTL_INSTALLED) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- $collator = _api_get_collator($language);
- if (is_object($collator)) {
- if (api_is_utf8($encoding)) {
- $sort_flag = ($sort_flag == SORT_LOCALE_STRING) ? SORT_STRING : $sort_flag;
- return collator_asort($collator, $array, _api_get_collator_sort_flag($sort_flag));
- } elseif ($sort_flag == SORT_STRING || $sort_flag == SORT_LOCALE_STRING) {
- global $_api_collator, $_api_encoding;
- $_api_collator = $collator;
- $_api_encoding = $encoding;
- return uasort($array, '_api_cmp');
- }
- }
- }
- return asort($array, $sort_flag);
- }
- function api_arsort(&$array, $sort_flag = SORT_REGULAR, $language = null, $encoding = null)
- {
- if (INTL_INSTALLED) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- $collator = _api_get_collator($language);
- if (is_object($collator)) {
- if ($sort_flag == SORT_STRING || $sort_flag == SORT_LOCALE_STRING) {
- global $_api_collator, $_api_encoding;
- $_api_collator = $collator;
- $_api_encoding = $encoding;
- return uasort($array, '_api_rcmp');
- }
- }
- }
- return arsort($array, $sort_flag);
- }
- function api_natsort(&$array, $language = null, $encoding = null)
- {
- if (INTL_INSTALLED) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- $collator = _api_get_alpha_numerical_collator($language);
- if (is_object($collator)) {
- global $_api_collator, $_api_encoding;
- $_api_collator = $collator;
- $_api_encoding = $encoding;
- return uasort($array, '_api_cmp');
- }
- }
- return natsort($array);
- }
- function _api_strnatrcmp($string1, $string2) {
- return strnatcmp($string2, $string1);
- }
- function api_natrsort(&$array, $language = null, $encoding = null)
- {
- return uasort($array, '_api_strnatrcmp');
- }
- function api_in_array_nocase($needle, $haystack, $strict = false, $encoding = null)
- {
- if (is_array($needle)) {
- foreach ($needle as $item) {
- if (api_in_array_nocase($item, $haystack, $strict, $encoding)) {
- return true;
- }
- }
- return false;
- }
- if (!is_string($needle)) {
- return in_array($needle, $haystack, $strict);
- }
- $needle = api_strtolower($needle, $encoding);
- if (!is_array($haystack)) {
- return false;
- }
- foreach ($haystack as $item) {
- if ($strict && !is_string($item)) {
- continue;
- }
- if (api_strtolower($item, $encoding) == $needle) {
- return true;
- }
- }
- return false;
- }
- function api_get_system_encoding()
- {
- global $configuration;
- return isset($configuration['platform_charset']) ? $configuration['platform_charset'] : '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)
- {
- return mb_detect_encoding($string);
- }
- function api_is_valid_utf8($string)
- {
- return u::isUtf8($string);
- }
- 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 _api_get_person_name_convention($language, $type)
- {
- global $app;
- $conventions = $app['name_order_conventions'];
- $language = api_purify_language_id($language);
- 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_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];
- }
|