12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833 |
- <?php
- define('TIME_NO_SEC_FORMAT', 0);
- define('DATE_FORMAT_SHORT', 1);
- define('DATE_FORMAT_LONG', 2);
- define('DATE_TIME_FORMAT_LONG', 3);
- 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 api_initialize_internationalization() {
- if (MBSTRING_INSTALLED) {
- @ini_set('mbstring.func_overload', 0);
- @ini_set('mbstring.encoding_translation', 0);
- @ini_set('mbstring.http_input', 'pass');
- @ini_set('mbstring.http_output', 'pass');
- @ini_set('mbstring.language', 'neutral');
- }
- api_set_internationalization_default_encoding('ISO-8859-15');
- }
- function api_set_internationalization_default_encoding($encoding) {
- $encoding = api_refine_encoding_id($encoding);
- $result = _api_mb_internal_encoding();
- _api_mb_internal_encoding($encoding);
- _api_mb_regex_encoding($encoding);
- _api_iconv_set_encoding('iconv_internal_encoding', $encoding);
- return $result;
- }
- function get_lang($variable, $notrans = 'DLTT', $language = null) {
-
-
- global $language_interface;
-
- global $language_interface_initial_value;
-
- static $langpath;
- if (!isset($langpath)) {
- $langpath = api_get_path(SYS_LANG_PATH);
- }
- static $test_server_mode;
- if (!isset($test_server_mode)) {
- $test_server_mode = api_get_setting('server_type') == 'test';
- }
- static $hide_dltt_markup;
- if (!isset($hide_dltt_markup)) {
- $hide_dltt_markup = api_get_setting('hide_dltt_markup') == 'true';
- }
-
- if (empty($language)) {
- $language = $language_interface;
- }
- $is_interface_language = $language == $language_interface_initial_value;
-
- $dltt = $notrans == 'DLTT' && $is_interface_language;
-
- static $cache;
-
- if (isset($cache[$language][$dltt][$variable])) {
-
- return $cache[$language][$dltt][$variable];
- }
-
-
-
- $read_global_variables = $is_interface_language && !$test_server_mode;
-
- if (!$read_global_variables) {
- global $language_files;
- if (isset($language_files)) {
- if (!is_array($language_files)) {
- @include "$langpath$language/$language_files.inc.php";
- } else {
- foreach ($language_files as &$language_file) {
- @include "$langpath$language/$language_file.inc.php";
- }
- }
- }
- }
-
- if ($hide_dltt_markup || !$dltt) {
- $ot = ''; $ct = '';
- } else {
- $ot = '[='; $ct = '=]';
- }
-
- if (!$test_server_mode) {
- if ($read_global_variables) {
- $langvar = isset($GLOBALS[$variable]) ? $GLOBALS[$variable] : (isset($GLOBALS["lang$variable"]) ? $GLOBALS["lang$variable"] : $ot.$variable.$ct);
- } else {
-
- $langvar = isset($$variable) ? $$variable : (isset(${"lang$variable"}) ? ${"lang$variable"} : $ot.$variable.$ct);
- }
- return $cache[$language][$dltt][$variable] = is_string($langvar) ? _get_lang_purifier(str_replace("\\'", "'", $langvar), $language) : $langvar;
- }
-
- if (!is_string($variable)) {
- return $cache[$language][$dltt][$variable] = $ot.'get_lang(?)'.$ct;
- }
-
- $langvar = isset($$variable) ? $$variable : ${"lang$variable"};
-
- return $cache[$language][$dltt][$variable] =
- isset($langvar) && is_string($langvar) && !empty($langvar)
- ? _get_lang_purifier(str_replace("\\'", "'", $langvar), $language)
- : (!$dltt
- ? $ot.$variable.$ct
- : $ot.$variable.$ct."<a href=\"http://www.dokeos.com/DLTT/suggestion.php?file=".
- (!is_array($language_files) ? $language_files : implode('.inc.php', $language_files)).".inc.php&variable=$".
- $variable."&language=".$language."\" target=\"_blank\" style=\"color:#FF0000\"><strong>#</strong></a>");
- }
- function api_get_interface_language($purified = false) {
- global $language_interface;
- return empty($language_interface) ? 'english' : ($purified ? api_purify_language_id($language_interface) : $language_interface);
- }
- function api_is_language_supported($language) {
- static $supported = array();
- if (!isset($supported[$language])) {
- $supported[$language] = in_array(api_purify_language_id($language), array_keys(_api_non_utf8_encodings()));
- }
- return $supported[$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) || !api_is_language_supported($language)) {
- $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') {
- static $iso_code = array();
- if (empty($language)) {
- $language = api_get_interface_language();
- }
- 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_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_is_latin1_compatible($language) {
- static $latin1_languages;
- if (!isset($latin1_languages)) {
- $latin1_languages = _api_get_latin1_compatible_languages();
- }
- $language = api_purify_language_id($language);
- return in_array($language, $latin1_languages);
- }
- function api_detect_language(&$string, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (empty($string)) {
- return false;
- }
- $result_array = &_api_compare_n_grams(_api_generate_n_grams(api_substr($string, 0, LANGUAGE_DETECT_MAX_LENGTH, $encoding), $encoding), $encoding);
- if (empty($result_array)) {
- return false;
- }
- list($key, $delta_points) = each($result_array);
- return strstr($key, ':', true);
- }
- 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() {
- global $_user;
-
- $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') {
-
- require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
- $timezone_user = UserManager::get_extra_user_data_by_field($_user['user_id'],'timezone');
- if ($timezone_user['timezone'] != null) {
- $to_timezone = $timezone_user['timezone'];
- }
- }
- return $to_timezone;
- }
- function api_get_utc_datetime($time = null) {
- $from_timezone = _api_get_timezone();
- $to_timezone = 'UTC';
- if (is_null($time)) {
- return gmdate("Y-m-d H:i:s");
- }
-
- if (is_int($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) {
-
- if (is_null($from_timezone)) {
- $from_timezone = 'UTC';
- }
-
- if (is_null($to_timezone)) {
- $to_timezone = _api_get_timezone();
- }
-
- if (is_null($time)) {
- $from_timezone = 'UTC';
- $time = gmdate('Y-m-d H:i:s');
- }
- if (is_int($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();
- 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 TIME_NO_SEC_FORMAT:
- $date_format = get_lang('timeNoSecFormat', '', $language);
- if (IS_PHP_53) {
- $datetype = IntlDateFormatter::NONE;
- $timetype = IntlDateFormatter::SHORT;
- }
- break;
- case DATE_FORMAT_SHORT:
- $date_format = get_lang('dateFormatShort', '', $language);
- if (IS_PHP_53) {
- $datetype = IntlDateFormatter::LONG;
- $timetype = IntlDateFormatter::NONE;
- }
- break;
- case DATE_FORMAT_LONG:
- $date_format = get_lang('dateFormatLong', '', $language);
- if (IS_PHP_53) {
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::NONE;
- }
- break;
- case DATE_TIME_FORMAT_LONG:
- $date_format = get_lang('dateTimeFormatLong', '', $language);
- if (IS_PHP_53) {
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- }
- break;
- default:
- $date_format = get_lang('dateTimeFormatLong', '', $language);
- if (IS_PHP_53) {
- $datetype = IntlDateFormatter::FULL;
- $timetype = IntlDateFormatter::SHORT;
- }
- }
- }
-
- if (IS_PHP_53 && INTL_INSTALLED && $datetype !== null && $timetype !== null) {
-
- if (is_null($language)) {
- $language = api_get_language_isocode();
- }
- $date_formatter = datefmt_create($language, $datetype, $timetype, date_default_timezone_get());
- $formatted_date = api_to_system_encoding(datefmt_format($date_formatter, $time), 'UTF-8');
- } 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) {
- 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 = api_ucfirst(get_lang('Today'));
- $yesterday = api_ucfirst(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) {
-
- $datetime = 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();
- }
- if (empty($encoding)) {
- $encoding = _api_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');
- 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();
- }
- 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) {
- static $sort_by_first_name = array();
- if (empty($language)) {
- $language = api_get_interface_language();
- }
- 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_byte_count(& $string) {
- static $use_mb_strlen;
- if (!isset($use_mb_strlen)) {
- $use_mb_strlen = MBSTRING_INSTALLED && ((int) ini_get('mbstring.func_overload') & 2);
- }
- if ($use_mb_strlen) {
- return mb_strlen($string, '8bit');
- }
- return strlen($string);
-
-
- }
- function api_convert_encoding($string, $to_encoding, $from_encoding = null) {
- if (empty($from_encoding)) {
- $from_encoding = _api_mb_internal_encoding();
- }
- if (api_equal_encodings($to_encoding, $from_encoding)) {
- return $string;
- }
- if (_api_mb_supports($to_encoding) && _api_mb_supports($from_encoding)) {
- return @mb_convert_encoding($string, $to_encoding, $from_encoding);
- }
- if (_api_iconv_supports($to_encoding) && _api_iconv_supports($from_encoding)) {
- return @iconv($from_encoding, $to_encoding, $string);
- }
- if (api_is_utf8($to_encoding) && api_is_latin1($from_encoding, true)) {
- return utf8_encode($string);
- }
- if (api_is_latin1($to_encoding, true) && api_is_utf8($from_encoding)) {
- return utf8_decode($string);
- }
- if (_api_convert_encoding_supports($to_encoding) && _api_convert_encoding_supports($from_encoding)) {
- return _api_convert_encoding($string, $to_encoding, $from_encoding);
- }
- return $string;
- }
- function api_utf8_encode($string, $from_encoding = null) {
- if (empty($from_encoding)) {
- $from_encoding = _api_mb_internal_encoding();
- }
- if (api_is_utf8($from_encoding)) {
- return $string;
- }
- if (_api_mb_supports($from_encoding)) {
- return @mb_convert_encoding($string, 'UTF-8', $from_encoding);
- }
- if (_api_iconv_supports($from_encoding)) {
- return @iconv($from_encoding, 'UTF-8', $string);
- }
- if (api_is_latin1($from_encoding, true)) {
- return utf8_encode($string);
- }
- if (_api_convert_encoding_supports($from_encoding)) {
- return _api_convert_encoding($string, 'UTF-8', $from_encoding);
- }
- return $string;
- }
- function api_utf8_decode($string, $to_encoding = null) {
- if (empty($to_encoding)) {
- $to_encoding = _api_mb_internal_encoding();
- }
- if (api_is_utf8($to_encoding)) {
- return $string;
- }
- if (_api_mb_supports($to_encoding)) {
- return @mb_convert_encoding($string, $to_encoding, 'UTF-8');
- }
- if (_api_iconv_supports($to_encoding)) {
- return @iconv('UTF-8', $to_encoding, $string);
- }
- if (api_is_latin1($to_encoding, true)) {
- return utf8_decode($string);
- }
- if (_api_convert_encoding_supports($to_encoding)) {
- return _api_convert_encoding($string, $to_encoding, 'UTF-8');
- }
- return $string;
- }
- function api_to_system_encoding($string, $from_encoding = null, $check_utf8_validity = false) {
- $system_encoding = api_get_system_encoding();
- if (empty($from_encoding)) {
- if (api_is_utf8($system_encoding)) {
- $from_encoding = api_get_non_utf8_encoding();
- } else {
- $from_encoding = 'UTF-8';
- }
- }
- if (api_equal_encodings($system_encoding, $from_encoding)) {
- return $string;
- }
- if ($check_utf8_validity) {
- if (api_is_utf8($system_encoding)) {
- if (api_is_valid_utf8($string)) {
- return $string;
- }
- }
- elseif (api_is_utf8($from_encoding)) {
- if (!api_is_valid_utf8($string)) {
- return $string;
- }
- }
- }
- 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();
- }
- if (!api_is_utf8($encoding) && _api_html_entity_supports($encoding)) {
- return htmlentities($string, $quote_style, $encoding);
- }
- if (_api_mb_supports($encoding)) {
- if (!api_is_utf8($encoding)) {
- $string = api_utf8_encode($string, $encoding);
- }
- $string = @mb_convert_encoding(api_utf8_encode($string, $encoding), 'HTML-ENTITIES', 'UTF-8');
- if (!api_is_utf8($encoding)) {
- $string = api_utf8_decode($string, $encoding);
- }
- }
- elseif (_api_convert_encoding_supports($encoding)) {
- if (!api_is_utf8($encoding)) {
- $string = _api_convert_encoding($string, 'UTF-8', $encoding);
- }
- $string = implode(array_map('_api_html_entity_from_unicode', _api_utf8_to_unicode($string)));
- if (!api_is_utf8($encoding)) {
- $string = _api_convert_encoding($string, $encoding, 'UTF-8');
- }
- }
- else {
-
- return $string;
- }
- switch($quote_style) {
- case ENT_COMPAT:
- $string = str_replace('"', '"', $string);
- break;
- case ENT_QUOTES:
- $string = str_replace(array('\'', '"'), array(''', '"'), $string);
- break;
- }
- return $string;
- }
- function api_html_entity_decode($string, $quote_style = ENT_COMPAT, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (_api_html_entity_supports($encoding)) {
- return html_entity_decode($string, $quote_style, $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 = 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_file_system_encode($string, $from_encoding = null) {
- if (empty($from_encoding)) {
- $from_encoding = _api_mb_internal_encoding();
- }
- return api_convert_encoding($string, api_get_file_system_encoding(), $from_encoding);
- }
- function api_file_system_decode($string, $to_encoding = null) {
- if (empty($to_encoding)) {
- $to_encoding = _api_mb_internal_encoding();
- }
- return api_convert_encoding($string, $to_encoding, api_get_file_system_encoding());
- }
- function api_transliterate($string, $unknown = '?', $from_encoding = null) {
- static $map = array();
- $string = api_utf8_encode($string, $from_encoding);
-
- $string = preg_replace('/[\x00-\x08\x0b\x0c\x0e-\x1f]/', $unknown, $string);
-
-
-
- if (api_is_valid_ascii($string)) {
- return $string;
- }
- static $tail_bytes;
- if (!isset($tail_bytes)) {
-
-
- $tail_bytes = array();
- for ($n = 0; $n < 256; $n++) {
- if ($n < 0xc0) {
- $remaining = 0;
- }
- elseif ($n < 0xe0) {
- $remaining = 1;
- }
- elseif ($n < 0xf0) {
- $remaining = 2;
- }
- elseif ($n < 0xf8) {
- $remaining = 3;
- }
- elseif ($n < 0xfc) {
- $remaining = 4;
- }
- elseif ($n < 0xfe) {
- $remaining = 5;
- } else {
- $remaining = 0;
- }
- $tail_bytes[chr($n)] = $remaining;
- }
- }
-
-
-
-
- preg_match_all('/[\x00-\x7f]+|[\x80-\xff][\x00-\x40\x5b-\x5f\x7b-\xff]*/', $string, $matches);
- $result = '';
- foreach ($matches[0] as $str) {
- if ($str{0} < "\x80") {
-
-
- $result .= $str;
- continue;
- }
-
-
-
-
-
-
- $head = '';
- $chunk = api_byte_count($str);
-
- $len = $chunk + 1;
- for ($i = -1; --$len; ) {
- $c = $str{++$i};
- if ($remaining = $tail_bytes[$c]) {
-
- $sequence = $head = $c;
- do {
-
- if (--$len && ($c = $str{++$i}) >= "\x80" && $c < "\xc0") {
-
- $sequence .= $c;
- } else {
- if ($len == 0) {
-
-
-
- $result .= $unknown;
- break 2;
- } else {
-
- $result .= $unknown;
-
-
- --$i;
- ++$len;
- continue 2;
- }
- }
- } while (--$remaining);
- $n = ord($head);
- if ($n <= 0xdf) {
- $ord = ($n - 192) * 64 + (ord($sequence{1}) - 128);
- }
- else if ($n <= 0xef) {
- $ord = ($n - 224) * 4096 + (ord($sequence{1}) - 128) * 64 + (ord($sequence{2}) - 128);
- }
- else if ($n <= 0xf7) {
- $ord = ($n - 240) * 262144 + (ord($sequence{1}) - 128) * 4096 + (ord($sequence{2}) - 128) * 64 + (ord($sequence{3}) - 128);
- }
- else if ($n <= 0xfb) {
- $ord = ($n - 248) * 16777216 + (ord($sequence{1}) - 128) * 262144 + (ord($sequence{2}) - 128) * 4096 + (ord($sequence{3}) - 128) * 64 + (ord($sequence{4}) - 128);
- }
- else if ($n <= 0xfd) {
- $ord = ($n - 252) * 1073741824 + (ord($sequence{1}) - 128) * 16777216 + (ord($sequence{2}) - 128) * 262144 + (ord($sequence{3}) - 128) * 4096 + (ord($sequence{4}) - 128) * 64 + (ord($sequence{5}) - 128);
- }
-
- $bank = $ord >> 8;
-
- if (!isset($map[$bank])) {
- $file = dirname(__FILE__).'/internationalization_database/transliteration/' . sprintf('x%02x', $bank) . '.php';
- if (file_exists($file)) {
- $map[$bank] = include ($file);
- } else {
- $map[$bank] = array('en' => array());
- }
- }
- $ord = $ord & 255;
- $result .= isset($map[$bank]['en'][$ord]) ? $map[$bank]['en'][$ord] : $unknown;
- $head = '';
- }
- elseif ($c < "\x80") {
-
- $result .= $c;
- $head = '';
- }
- elseif ($c < "\xc0") {
-
- if ($head == '') {
- $result .= $unknown;
- }
- } else {
-
- $result .= $unknown;
- $head = '';
- }
- }
- }
- return $result;
- }
- function api_convert_encoding_xml($string, $to_encoding, $from_encoding = null) {
- return _api_convert_encoding_xml($string, $to_encoding, $from_encoding);
- }
- function api_utf8_encode_xml($string, $from_encoding = null) {
- return _api_convert_encoding_xml($string, 'UTF-8', $from_encoding);
- }
- function api_utf8_decode_xml($string, $to_encoding = null) {
- if (empty($to_encoding)) {
- $to_encoding = _api_mb_internal_encoding();
- }
- return _api_convert_encoding_xml($string, $to_encoding, 'UTF-8');
- }
- function api_ord($character, $encoding) {
- return _api_utf8_ord(api_utf8_encode($character, $encoding));
- }
- function api_chr($codepoint, $encoding) {
- return api_utf8_decode(_api_utf8_chr($codepoint), $encoding);
- }
- function api_str_ireplace($search, $replace, $subject, & $count = null, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (api_is_encoding_supported($encoding)) {
- if (!is_array($search) && !is_array($replace)) {
- if (!api_is_utf8($encoding)) {
- $search = api_utf8_encode($search, $encoding);
- }
- $slen = api_byte_count($search);
- if ( $slen == 0 ) {
- return $subject;
- }
- if (!api_is_utf8($encoding)) {
- $replace = api_utf8_encode($replace, $encoding);
- $subject = api_utf8_encode($subject, $encoding);
- }
- $lendif = api_byte_count($replace) - api_byte_count($search);
- $search = api_strtolower($search, 'UTF-8');
- $search = preg_quote($search);
- $lstr = api_strtolower($subject, 'UTF-8');
- $i = 0;
- $matched = 0;
- while (preg_match('/(.*)'.$search.'/Us', $lstr, $matches) ) {
- if ($i === $count) {
- break;
- }
- $mlen = api_byte_count($matches[0]);
- $lstr = substr($lstr, $mlen);
- $subject = substr_replace($subject, $replace, $matched + api_byte_count($matches[1]), $slen);
- $matched += $mlen + $lendif;
- $i++;
- }
- if (!api_is_utf8($encoding)) {
- $subject = api_utf8_decode($subject, $encoding);
- }
- return $subject;
- } else {
- foreach (array_keys($search) as $k) {
- if (is_array($replace)) {
- if (array_key_exists($k, $replace)) {
- $subject = api_str_ireplace($search[$k], $replace[$k], $subject, $count);
- } else {
- $subject = api_str_ireplace($search[$k], '', $subject, $count);
- }
- } else {
- $subject = api_str_ireplace($search[$k], $replace, $subject, $count);
- }
- }
- return $subject;
- }
- }
- if (is_null($count)) {
- return str_ireplace($search, $replace, $subject);
- }
- return str_ireplace($search, $replace, $subject, $count);
- }
- function api_str_split($string, $split_length = 1, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (empty($string)) {
- return array();
- }
- if ($split_length < 1) {
- return false;
- }
- if (_api_is_single_byte_encoding($encoding)) {
- return str_split($string, $split_length);
- }
- if (api_is_encoding_supported($encoding)) {
- $len = api_strlen($string);
- if ($len <= $split_length) {
- return array($string);
- }
- if (!api_is_utf8($encoding)) {
- $string = api_utf8_encode($string, $encoding);
- }
- if (preg_match_all('/.{'.$split_length.'}|[^\x00]{1,'.$split_length.'}$/us', $string, $result) === false) {
- return array();
- }
- if (!api_is_utf8($encoding)) {
- global $_api_encoding;
- $_api_encoding = $encoding;
- $result = _api_array_utf8_decode($result[0]);
- }
- return $result[0];
- }
- return str_split($string, $split_length);
- }
- function api_stripos($haystack, $needle, $offset = 0, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (!is_string($needle)) {
- $needle = (int)$needle;
- if (api_is_utf8($encoding)) {
- $needle = _api_utf8_chr($needle);
- } else {
- $needle = chr($needle);
- }
- }
- if ($needle == '') {
- return false;
- }
- if (_api_mb_supports($encoding)) {
- return @mb_stripos($haystack, $needle, $offset, $encoding);
- }
- elseif (api_is_encoding_supported($encoding)) {
- if (MBSTRING_INSTALLED) {
- if (!api_is_utf8($encoding)) {
- $haystack = api_utf8_encode($haystack, $encoding);
- $needle = api_utf8_encode($needle, $encoding);
- }
- return @mb_stripos($haystack, $needle, $offset, 'UTF-8');
- }
- return api_strpos(api_strtolower($haystack, $encoding), api_strtolower($needle, $encoding), $offset, $encoding);
- }
- return stripos($haystack, $needle, $offset);
- }
- function api_stristr($haystack, $needle, $before_needle = false, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (!is_string($needle)) {
- $needle = (int)$needle;
- if (api_is_utf8($encoding)) {
- $needle = _api_utf8_chr($needle);
- } else {
- $needle = chr($needle);
- }
- }
- if ($needle == '') {
- return false;
- }
- if (_api_mb_supports($encoding)) {
- return @mb_stristr($haystack, $needle, $before_needle, $encoding);
- }
- elseif (api_is_encoding_supported($encoding)) {
- if (MBSTRING_INSTALLED) {
- if (!api_is_utf8($encoding)) {
- $haystack = api_utf8_encode($haystack, $encoding);
- $needle = api_utf8_encode($needle, $encoding);
- }
- $result = @mb_stristr($haystack, $needle, $before_needle, 'UTF-8');
- if ($result === false) {
- return false;
- }
- if (!api_is_utf8($encoding)) {
- return api_utf8_decode($result, $encoding);
- }
- return $result;
- }
- $result = api_strstr(api_strtolower($haystack, $encoding), api_strtolower($needle, $encoding), $before_needle, $encoding);
- if ($result === false) {
- return false;
- }
- if ($before_needle) {
- return api_substr($haystack, 0, api_strlen($result, $encoding), $encoding);
- }
- return api_substr($haystack, api_strlen($haystack, $encoding) - api_strlen($result, $encoding), null, $encoding);
- }
- if (!IS_PHP_53) {
- return stristr($haystack, $needle);
- }
- return stristr($haystack, $needle, $before_needle);
- }
- function api_strlen($string, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (_api_is_single_byte_encoding($encoding)) {
- return strlen($string);
- }
- if (_api_mb_supports($encoding)) {
- return @mb_strlen($string, $encoding);
- }
- if (_api_iconv_supports($encoding)) {
- return @iconv_strlen($string, $encoding);
- }
- if (api_is_utf8($encoding)) {
- return api_byte_count(preg_replace("/[\x80-\xBF]/", '', $string));
- }
- return strlen($string);
- }
- function api_strpos($haystack, $needle, $offset = 0, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (!is_string($needle)) {
- $needle = (int)$needle;
- if (api_is_utf8($encoding)) {
- $needle = _api_utf8_chr($needle);
- } else {
- $needle = chr($needle);
- }
- }
- if ($needle == '') {
- return false;
- }
- if (_api_is_single_byte_encoding($encoding)) {
- return strpos($haystack, $needle, $offset);
- }
- elseif (_api_mb_supports($encoding)) {
- return @mb_strpos($haystack, $needle, $offset, $encoding);
- }
- elseif (api_is_encoding_supported($encoding)) {
- if (!api_is_utf8($encoding)) {
- $haystack = api_utf8_encode($haystack, $encoding);
- $needle = api_utf8_encode($needle, $encoding);
- }
- if (MBSTRING_INSTALLED) {
- return @mb_strpos($haystack, $needle, $offset, 'UTF-8');
- }
- if (empty($offset)) {
- $haystack = explode($needle, $haystack, 2);
- if (count($haystack) > 1) {
- return api_strlen($haystack[0]);
- }
- return false;
- }
- $haystack = api_substr($haystack, $offset);
- if (($pos = api_strpos($haystack, $needle)) !== false ) {
- return $pos + $offset;
- }
- return false;
- }
- return strpos($haystack, $needle, $offset);
- }
- function api_strrchr($haystack, $needle, $before_needle = false, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (!is_string($needle)) {
- $needle = (int)$needle;
- if (api_is_utf8($encoding)) {
- $needle = _api_utf8_chr($needle);
- } else {
- $needle = chr($needle);
- }
- }
- if ($needle == '') {
- return false;
- }
- if (_api_is_single_byte_encoding($encoding)) {
- if (!$before_needle) {
- return strrchr($haystack, $needle);
- }
- $result = strrchr($haystack, $needle);
- if ($result === false) {
- return false;
- }
- return api_substr($haystack, 0, api_strlen($haystack, $encoding) - api_strlen($result, $encoding), $encoding);
- }
- elseif (_api_mb_supports($encoding)) {
- return @mb_strrchr($haystack, $needle, $before_needle, $encoding);
- }
- elseif (MBSTRING_INSTALLED && api_is_encoding_supported($encoding)) {
- if (!api_is_utf8($encoding)) {
- $haystack = api_utf8_encode($haystack, $encoding);
- $needle = api_utf8_encode($needle, $encoding);
- }
- $result = @mb_strrchr($haystack, $needle, $before_needle, 'UTF-8');
- if ($result === false) {
- return false;
- }
- if (!api_is_utf8($encoding)) {
- return api_utf8_decode($result, $encoding);
- }
- return $result;
- }
- if (!$before_needle) {
- return strrchr($haystack, $needle);
- }
- $result = strrchr($haystack, $needle);
- if ($result === false) {
- return false;
- }
- return api_substr($haystack, 0, api_strlen($haystack, $encoding) - api_strlen($result, $encoding), $encoding);
- }
- function api_strrev($string, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (empty($string)) {
- return '';
- }
- if (_api_is_single_byte_encoding($encoding)) {
- return strrev($string);
- }
- if (api_is_encoding_supported($encoding)) {
- return implode(array_reverse(api_str_split($string, 1, $encoding)));
- }
- return strrev($string);
- }
- function api_strripos($haystack, $needle, $offset = 0, $encoding = null) {
- return api_strrpos(api_strtolower($haystack, $encoding), api_strtolower($needle, $encoding), $offset, $encoding);
- }
- function api_strrpos($haystack, $needle, $offset = 0, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (!is_string($needle)) {
- $needle = (int)$needle;
- if (api_is_utf8($encoding)) {
- $needle = _api_utf8_chr($needle);
- } else {
- $needle = chr($needle);
- }
- }
- if ($needle == '') {
- return false;
- }
- if (_api_is_single_byte_encoding($encoding)) {
- return strrpos($haystack, $needle, $offset);
- }
- if (_api_mb_supports($encoding) && IS_PHP_52) {
- return @mb_strrpos($haystack, $needle, $offset, $encoding);
- }
- elseif (api_is_encoding_supported($encoding)) {
- if (!api_is_utf8($encoding)) {
- $haystack = api_utf8_encode($haystack, $encoding);
- $needle = api_utf8_encode($needle, $encoding);
- }
- if (MBSTRING_INSTALLED && IS_PHP_52) {
- return @mb_strrpos($haystack, $needle, $offset, 'UTF-8');
- }
-
- $found = false;
- $haystack = _api_utf8_to_unicode($haystack);
- $haystack_count = count($haystack);
- $matches = array_count_values($haystack);
- $needle = _api_utf8_to_unicode($needle);
- $needle_count = count($needle);
- $position = $offset;
- while (($found === false) && ($position < $haystack_count)) {
- if (isset($needle[0]) && $needle[0] === $haystack[$position]) {
- for ($i = 1; $i < $needle_count; $i++) {
- if ($needle[$i] !== $haystack[$position + $i]) {
- if ($needle[$i] === $haystack[($position + $i) -1]) {
- $position--;
- $found = true;
- continue;
- }
- }
- }
- if (!$offset && isset($matches[$needle[0]]) && $matches[$needle[0]] > 1) {
- $matches[$needle[0]] = $matches[$needle[0]] - 1;
- } elseif ($i === $needle_count) {
- $found = true;
- $position--;
- }
- }
- $position++;
- }
- return ($found) ? $position : false;
- }
- return strrpos($haystack, $needle, $offset);
- }
- function api_strstr($haystack, $needle, $before_needle = false, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (!is_string($needle)) {
- $needle = (int)$needle;
- if (api_is_utf8($encoding)) {
- $needle = _api_utf8_chr($needle);
- } else {
- $needle = chr($needle);
- }
- }
- if ($needle == '') {
- return false;
- }
- if (_api_is_single_byte_encoding($encoding)) {
-
- if (!$before_needle) {
- return strstr($haystack, $needle);
- }
- if (!IS_PHP_53) {
- $result = explode($needle, $haystack, 2);
- if ($result === false || count($result) < 2) {
- return false;
- }
- return $result[0];
- }
- return strstr($haystack, $needle, $before_needle);
- }
- if (_api_mb_supports($encoding)) {
- return @mb_strstr($haystack, $needle, $before_needle, $encoding);
- }
- elseif (MBSTRING_INSTALLED && api_is_encoding_supported($encoding)) {
- if (!api_is_utf8($encoding)) {
- $haystack = api_utf8_encode($haystack, $encoding);
- $needle = api_utf8_encode($needle, $encoding);
- }
- $result = @mb_strstr($haystack, $needle, $before_needle, 'UTF-8');
- if ($result !== false) {
- if (!api_is_utf8($encoding)) {
- return api_utf8_decode($result, $encoding);
- }
- return $result;
- }
- return false;
- }
-
- if (!$before_needle) {
- return strstr($haystack, $needle);
- }
- if (!IS_PHP_53) {
- $result = explode($needle, $haystack, 2);
- if ($result === false || count($result) < 2) {
- return false;
- }
- return $result[0];
- }
- return strstr($haystack, $needle, $before_needle);
- }
- function api_strtolower($string, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (_api_mb_supports($encoding)) {
- return @mb_strtolower($string, $encoding);
- }
- elseif (api_is_encoding_supported($encoding)) {
- if (!api_is_utf8($encoding)) {
- $string = api_utf8_encode($string, $encoding);
- }
- if (MBSTRING_INSTALLED) {
- $string = @mb_strtolower($string, 'UTF-8');
- } else {
-
- $codepoints = _api_utf8_to_unicode($string);
- $length = count($codepoints);
- $matched = false;
- $result = array();
- for ($i = 0 ; $i < $length; $i++) {
- $codepoint = $codepoints[$i];
- if ($codepoint < 128) {
- $str = strtolower(chr($codepoint));
- $strlen = api_byte_count($str);
- for ($ii = 0 ; $ii < $strlen; $ii++) {
- $lower = ord($str[$ii]);
- }
- $result[] = $lower;
- $matched = true;
- } else {
- $matched = false;
- $properties = &_api_utf8_get_letter_case_properties($codepoint, 'upper');
- if (!empty($properties)) {
- foreach ($properties as $key => $value) {
- if ($properties[$key]['upper'] == $codepoint && count($properties[$key]['lower'][0]) === 1) {
- $result[] = $properties[$key]['lower'][0];
- $matched = true;
- break 1;
- }
- }
- }
- }
- if ($matched === false) {
- $result[] = $codepoint;
- }
- }
- $string = _api_utf8_from_unicode($result);
- }
- if (!api_is_utf8($encoding)) {
- return api_utf8_decode($string, $encoding);
- }
- return $string;
- }
- return strtolower($string);
- }
- function api_strtoupper($string, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (_api_mb_supports($encoding)) {
- return @mb_strtoupper($string, $encoding);
- }
- elseif (api_is_encoding_supported($encoding)) {
- if (!api_is_utf8($encoding)) {
- $string = api_utf8_encode($string, $encoding);
- }
- if (MBSTRING_INSTALLED) {
- $string = @mb_strtoupper($string, 'UTF-8');
- } else {
-
- $codepoints = _api_utf8_to_unicode($string);
- $length = count($codepoints);
- $matched = false;
- $replaced = array();
- $result = array();
- for ($i = 0 ; $i < $length; $i++) {
- $codepoint = $codepoints[$i];
- if ($codepoint < 128) {
- $str = strtoupper(chr($codepoint));
- $strlen = api_byte_count($str);
- for ($ii = 0 ; $ii < $strlen; $ii++) {
- $lower = ord($str[$ii]);
- }
- $result[] = $lower;
- $matched = true;
- } else {
- $matched = false;
- $properties = &_api_utf8_get_letter_case_properties($codepoint);
- $property_count = count($properties);
- if (!empty($properties)) {
- foreach ($properties as $key => $value) {
- $matched = false;
- $replace = 0;
- if ($length > 1 && count($properties[$key]['lower']) > 1) {
- $j = 0;
- for ($ii = 0; $ii < count($properties[$key]['lower']); $ii++) {
- $next_codepoint = $next_codepoints[$i + $ii];
- if (isset($next_codepoint) && ($next_codepoint == $properties[$key]['lower'][$j + $ii])) {
- $replace++;
- }
- }
- if ($replace == count($properties[$key]['lower'])) {
- $result[] = $properties[$key]['upper'];
- $replaced = array_merge($replaced, array_values($properties[$key]['lower']));
- $matched = true;
- break 1;
- }
- } elseif ($length > 1 && $property_count > 1) {
- $j = 0;
- for ($ii = 1; $ii < $property_count; $ii++) {
- $next_codepoint = $next_codepoints[$i + $ii - 1];
- if (in_array($next_codepoint, $properties[$ii]['lower'])) {
- for ($jj = 0; $jj < count($properties[$ii]['lower']); $jj++) {
- $next_codepoint = $next_codepoints[$i + $jj];
- if (isset($next_codepoint) && ($next_codepoint == $properties[$ii]['lower'][$j + $jj])) {
- $replace++;
- }
- }
- if ($replace == count($properties[$ii]['lower'])) {
- $result[] = $properties[$ii]['upper'];
- $replaced = array_merge($replaced, array_values($properties[$ii]['lower']));
- $matched = true;
- break 2;
- }
- }
- }
- }
- if ($properties[$key]['lower'][0] == $codepoint) {
- $result[] = $properties[$key]['upper'];
- $matched = true;
- break 1;
- }
- }
- }
- }
- if ($matched === false && !in_array($codepoint, $replaced, true)) {
- $result[] = $codepoint;
- }
- }
- $string = _api_utf8_from_unicode($result);
- }
- if (!api_is_utf8($encoding)) {
- return api_utf8_decode($string, $encoding);
- }
- return $string;
- }
- return strtoupper($string);
- }
- function api_substr($string, $start, $length = null, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
-
- if (is_null($length)) {
- $length = api_strlen($string, $encoding);
- }
- if (_api_is_single_byte_encoding($encoding)) {
- return substr($string, $start, $length);
- }
- if (_api_mb_supports($encoding)) {
- return @mb_substr($string, $start, $length, $encoding);
- }
- elseif (api_is_encoding_supported($encoding)) {
- if (!api_is_utf8($encoding)) {
- $string = api_utf8_encode($string, $encoding);
- }
- if (MBSTRING_INSTALLED) {
- $string = @mb_substr($string, $start, $length, 'UTF-8');
- } else {
-
- $strlen = api_byte_count($string);
-
- $bytes = 0;
- if ($start > 0) {
-
-
- $bytes = -1; $chars = -1;
- while ($bytes < $strlen && $chars < $start) {
- $bytes++;
- $c = ord($string[$bytes]);
- if ($c < 0x80 || $c >= 0xC0) {
- $chars++;
- }
- }
- }
- else if ($start < 0) {
-
-
- $start = abs($start);
- $bytes = $strlen; $chars = 0;
- while ($bytes > 0 && $chars < $start) {
- $bytes--;
- $c = ord($string[$bytes]);
- if ($c < 0x80 || $c >= 0xC0) {
- $chars++;
- }
- }
- }
- $istart = $bytes;
-
- if ($length === NULL) {
- $bytes = $strlen - 1;
- }
- else if ($length > 0) {
-
-
- $bytes = $istart; $chars = 0;
- while ($bytes < $strlen && $chars < $length) {
- $bytes++;
- $c = ord($string[$bytes]);
- if ($c < 0x80 || $c >= 0xC0) {
- $chars++;
- }
- }
- $bytes--;
- }
- else if ($length < 0) {
-
-
- $length = abs($length);
- $bytes = $strlen - 1; $chars = 0;
- while ($bytes >= 0 && $chars < $length) {
- $c = ord($string[$bytes]);
- if ($c < 0x80 || $c >= 0xC0) {
- $chars++;
- }
- $bytes--;
- }
- }
- $iend = $bytes;
- $string = substr($string, $istart, max(0, $iend - $istart + 1));
- }
- if (!api_is_utf8($encoding)) {
- $string = api_utf8_decode($string, $encoding);
- }
- return $string;
- }
- return substr($string, $start, $length);
- }
- function api_substr_count($haystack, $needle, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (_api_mb_supports($encoding)) {
- return @mb_substr_count($haystack, $needle, $encoding);
- }
- return substr_count($haystack, $needle);
- }
- function api_substr_replace($string, $replacement, $start, $length = null, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (_api_is_single_byte_encoding($encoding)) {
- if (is_null($length)) {
- return substr_replace($string, $replacement, $start);
- }
- return substr_replace($string, $replacement, $start, $length);
- }
- if (api_is_encoding_supported($encoding)) {
- if (is_null($length)) {
- $length = api_strlen($string);
- }
- if (!api_is_utf8($encoding)) {
- $string = api_utf8_encode($string, $encoding);
- $replacement = api_utf8_encode($replacement, $encoding);
- }
- $string = _api_utf8_to_unicode($string);
- array_splice($string, $start, $length, _api_utf8_to_unicode($replacement));
- $string = _api_utf8_from_unicode($string);
- if (!api_is_utf8($encoding)) {
- $string = api_utf8_decode($string, $encoding);
- }
- return $string;
- }
- if (is_null($length)) {
- return substr_replace($string, $replacement, $start);
- }
- return substr_replace($string, $replacement, $start, $length);
- }
- function api_ucfirst($string, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- return api_strtoupper(api_substr($string, 0, 1, $encoding), $encoding) . api_substr($string, 1, api_strlen($string, $encoding), $encoding);
- }
- function api_ucwords($string, $encoding = null) {
- if (empty($encoding)) {
- $encoding = _api_mb_internal_encoding();
- }
- if (_api_mb_supports($encoding)) {
- return @mb_convert_case($string, MB_CASE_TITLE, $encoding);
- }
- if (api_is_encoding_supported($encoding)) {
- if (!api_is_utf8($encoding)) {
- $string = api_utf8_encode($string, $encoding);
- }
- if (MBSTRING_INSTALLED) {
- $string = @mb_convert_case($string, MB_CASE_TITLE, 'UTF-8');
- } else {
-
-
-
-
- $pattern = '/(^|([\x0c\x09\x0b\x0a\x0d\x20]+))([^\x0c\x09\x0b\x0a\x0d\x20]{1})[^\x0c\x09\x0b\x0a\x0d\x20]*/u';
- $string = preg_replace_callback($pattern, '_api_utf8_ucwords_callback', $string);
- }
- if (!api_is_utf8($encoding)) {
- return api_utf8_decode($string, $encoding);
- }
- return $string;
- }
- 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_ereg_replace($pattern, $replacement, $string, $option = null) {
- $encoding = _api_mb_regex_encoding();
- if (_api_mb_supports($encoding)) {
- if (is_null($option)) {
- return @mb_ereg_replace($pattern, $replacement, $string);
- }
- return @mb_ereg_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_ereg_replace(api_utf8_encode($pattern, $encoding), api_utf8_encode($replacement, $encoding), api_utf8_encode($string, $encoding)), $encoding);
- } else {
- $result = api_utf8_decode(@mb_ereg_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 ereg_replace($pattern, $replacement, $string);
- }
- 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_split($pattern, $string, $limit = null) {
- $encoding = _api_mb_regex_encoding();
- if (_api_mb_supports($encoding)) {
- if (is_null($limit)) {
- return @mb_split($pattern, $string);
- }
- return @mb_split($pattern, $string, $limit);
- }
- if (MBSTRING_INSTALLED && api_is_encoding_supported($encoding)) {
- global $_api_encoding;
- $_api_encoding = $encoding;
- _api_mb_regex_encoding('UTF-8');
- if (is_null($limit)) {
- $result = @mb_split(api_utf8_encode($pattern, $encoding), api_utf8_encode($string, $encoding));
- } else {
- $result = @mb_split(api_utf8_encode($pattern, $encoding), api_utf8_encode($string, $encoding), $limit);
- }
- $result = _api_array_utf8_decode($result);
- _api_mb_regex_encoding($encoding);
- return $result;
- }
- if (is_null($limit)) {
- return split($pattern, $string);
- }
- return split($pattern, $string, $limit);
- }
- 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) {
- if (INTL_INSTALLED) {
- $collator = _api_get_collator($language);
- if (is_object($collator)) {
- $result = collator_compare($collator, api_utf8_encode($string1, $encoding), api_utf8_encode($string2, $encoding));
- return $result === false ? 0 : $result;
- }
- }
- return strcmp($string1, $string2);
- }
- function api_strnatcasecmp($string1, $string2, $language = null, $encoding = null) {
- return api_strnatcmp(api_strtolower($string1, $encoding), api_strtolower($string2, $encoding), $language, $encoding);
- }
- function api_strnatcmp($string1, $string2, $language = null, $encoding = null) {
- if (INTL_INSTALLED) {
- $collator = _api_get_alpha_numerical_collator($language);
- if (is_object($collator)) {
- $result = collator_compare($collator, api_utf8_encode($string1, $encoding), api_utf8_encode($string2, $encoding));
- return $result === false ? 0 : $result;
- }
- }
- 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_natrsort(&$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_rcmp');
- }
- }
- return uasort($array, '_api_strnatrcmp');
- }
- function api_natcasesort(&$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_casecmp');
- }
- }
- return natcasesort($array);
- }
- function api_natcasersort(&$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_casercmp');
- }
- }
- return uasort($array, '_api_strnatcasercmp');
- }
- function api_ksort(&$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 uksort($array, '_api_cmp');
- }
- }
- }
- return ksort($array, $sort_flag);
- }
- function api_krsort(&$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 uksort($array, '_api_rcmp');
- }
- }
- }
- return krsort($array, $sort_flag);
- }
- function api_knatsort(&$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 uksort($array, '_api_cmp');
- }
- }
- return uksort($array, 'strnatcmp');
- }
- function api_knatrsort(&$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 uksort($array, '_api_rcmp');
- }
- }
- return uksort($array, '_api_strnatrcmp');
- }
- function api_knatcasesort(&$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 uksort($array, '_api_casecmp');
- }
- }
- return uksort($array, 'strnatcasecmp');
- }
- function api_knatcasersort(&$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 uksort($array, '_api_casercmp');
- }
- }
- return uksort($array, '_api_strnatcasercmp');
- }
- function api_sort(&$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_sort($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 usort($array, '_api_cmp');
- }
- }
- }
- return sort($array, $sort_flag);
- }
- function api_rsort(&$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 usort($array, '_api_rcmp');
- }
- }
- }
- return rsort($array, $sort_flag);
- }
- 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_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_is_latin1($encoding, $strict = false) {
- static $latin1 = array();
- static $latin1_strict = array();
- if ($strict) {
- if (!isset($latin1_strict[$encoding])) {
- $latin1_strict[$encoding] = api_equal_encodings($encoding, array('ISO-8859-1', 'ISO8859-1', 'CP819', 'LATIN1'));
- }
- return $latin1_strict[$encoding];
- }
- if (!isset($latin1[$encoding])) {
- $latin1[$encoding] = api_equal_encodings($encoding, array(
- 'ISO-8859-1', 'ISO8859-1', 'CP819', 'LATIN1',
- 'ISO-8859-15', 'ISO8859-15', 'CP923', 'LATIN0', 'LATIN-9',
- 'WINDOWS-1252', 'CP1252', 'WIN-1252', 'WIN1252'
- ));
- }
- return $latin1[$encoding];
- }
- function api_get_system_encoding() {
- $system_encoding = api_get_setting('platform_charset');
- if (empty($system_encoding)) {
- global $charset;
- if (empty($charset)) {
- return _api_mb_internal_encoding();
- }
- return $charset;
- }
- return $system_encoding;
- }
- function api_get_file_system_encoding() {
- static $file_system_encoding;
- if (!isset($file_system_encoding)) {
- $locale = setlocale(LC_CTYPE, '0');
- $seek_pos = strpos($locale, '.');
- if ($seek_pos !== false) {
- $file_system_encoding = substr($locale, $seek_pos + 1);
- if (IS_WINDOWS_OS) {
- $file_system_encoding = 'CP'.$file_system_encoding;
- }
- }
-
- $file_system_encoding = str_ireplace('utf8', 'UTF-8', $file_system_encoding);
- $file_system_encoding = preg_replace('/^CP65001$/', 'UTF-8', $file_system_encoding);
- $file_system_encoding = preg_replace('/^CP(125[0-9])$/', 'WINDOWS-\1', $file_system_encoding);
- $file_system_encoding = str_replace('WINDOWS-1252', 'ISO-8859-15', $file_system_encoding);
- if (empty($file_system_encoding)) {
- if (IS_WINDOWS_OS) {
-
- $file_system_encoding = api_get_system_encoding();
- } else {
-
- $file_system_encoding = 'UTF-8';
- }
- }
- }
- return $file_system_encoding;
- }
- 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_get_non_utf8_encoding($language = null) {
- if (empty($language)) {
- $language = api_get_interface_language();
- }
- $language = api_purify_language_id($language);
- $encodings = & _api_non_utf8_encodings();
- if (is_array($encodings[$language])) {
- if (!empty($encodings[$language][0])) {
- return $encodings[$language][0];
- }
- return null;
- }
- return null;
- }
- function api_get_valid_encodings() {
- $encodings = & _api_non_utf8_encodings();
- if (!is_array($encodings)) {
- $encodings = array('english', array('ISO-8859-15'));
- }
- $result1 = array(); $result2 = array(); $result3 = array();
- foreach ($encodings as $value) {
- $encoding = api_refine_encoding_id(trim($value[0]));
- if (!empty($encoding)) {
- if (strpos($encoding, 'ISO-') === 0) {
- $result1[] = $encoding;
- } elseif (strpos($encoding, 'WINDOWS-') === 0) {
- $result2[] = $encoding;
- } else {
- $result3[] = $encoding;
- }
- }
- }
- $result1 = array_unique($result1);
- $result2 = array_unique($result2);
- $result3 = array_unique($result3);
- natsort($result1);
- natsort($result2);
- natsort($result3);
- return array_merge(array('UTF-8'), $result1, $result2, $result3);
- }
- function api_detect_encoding($string) {
- if (api_is_valid_utf8($string)) {
- return 'UTF-8';
- }
- $result = null;
- $delta_points_min = LANGUAGE_DETECT_MAX_DELTA;
- $encodings = api_get_valid_encodings();
- foreach ($encodings as $encoding) {
- if (api_is_encoding_supported($encoding) & !api_is_utf8($encoding)) {
- $result_array = & _api_compare_n_grams(_api_generate_n_grams(api_substr($string, 0, LANGUAGE_DETECT_MAX_LENGTH, $encoding), $encoding), $encoding);
- if (!empty($result_array)) {
- list($key, $delta_points) = each($result_array);
- if ($delta_points < $delta_points_min) {
- $pos = strpos($key, ':');
- $result_encoding = api_refine_encoding_id(substr($key, $pos + 1));
- if (api_equal_encodings($encoding, $result_encoding)) {
- if ($string == api_utf8_decode(api_utf8_encode($string, $encoding), $encoding)) {
- $delta_points_min = $delta_points;
- $result = $encoding;
- }
- }
- }
- }
- }
- }
- return $result;
- }
- function api_detect_encoding_html($string) {
- if (@preg_match('/<head.*(<meta[^>]*http-equiv=["\']*content-type[^>]*>).*<\/head>/si', $string, $matches)) {
- if (@preg_match('/<meta[^>]*charset=(.*)["\';][^>]*>/si', $matches[1], $matches)) {
- return api_refine_encoding_id(trim($matches[1]));
- }
- }
- return api_detect_encoding(strip_tags($string));
- }
- function api_detect_encoding_xml($string, $default_encoding = null) {
- if (preg_match(_PCRE_XML_ENCODING, $string, $matches)) {
- return api_refine_encoding_id($matches[1]);
- }
- if (api_is_valid_utf8($string)) {
- return 'UTF-8';
- }
- if (empty($default_encoding)) {
- $default_encoding = _api_mb_internal_encoding();
- }
- return api_refine_encoding_id($default_encoding);
- }
- function api_is_valid_utf8(&$string) {
-
-
-
-
-
- $str = (string)$string;
- $len = api_byte_count($str);
- $i = 0;
- while ($i < $len) {
- $byte1 = ord($str[$i++]);
-
- if (($byte1 & 0x80) == 0x00) {
-
-
-
-
-
- }
- elseif (($byte1 & 0xE0) == 0xC0) {
-
-
-
-
-
- if ($i == $len) {
- return false;
- }
- if (!((ord($str[$i++]) & 0xC0) == 0x80))
- return false;
- }
- elseif(($byte1 & 0xF0) == 0xE0) {
-
-
-
-
-
- if ($i == $len) {
- return false;
- }
- if (!((ord($str[$i++]) & 0xC0) == 0x80)) {
- return false;
- }
- if ($i == $len) {
- return false;
- }
- if (!((ord($str[$i++]) & 0xC0) == 0x80)) {
- return false;
- }
- }
- elseif(($byte1 & 0xF8) == 0xF0) {
-
-
-
-
-
- if ($i == $len) {
- return false;
- }
- if (!((ord($str[$i++]) & 0xC0) == 0x80)) {
- return false;
- }
- if ($i == $len) {
- return false;
- }
- if (!((ord($str[$i++]) & 0xC0) == 0x80)) {
- return false;
- }
- if ($i == $len) {
- return false;
- }
- if (!((ord($str[$i++]) & 0xC0) == 0x80)) {
- return false;
- }
- }
- elseif(($byte1 & 0xFC) == 0xF8) {
-
-
-
-
-
- if ($i == $len) {
- return false;
- }
- if (!((ord($str[$i++]) & 0xC0) == 0x80)) {
- return false;
- }
- if ($i == $len) {
- return false;
- }
- if (!((ord($str[$i++]) & 0xC0) == 0x80)) {
- return false;
- }
- if ($i == $len) {
- return false;
- }
- if (!((ord($str[$i++]) & 0xC0) == 0x80)) {
- return false;
- }
- if ($i == $len) {
- return false;
- }
- if (!((ord($str[$i++]) & 0xC0) == 0x80)) {
- return false;
- }
- }
- elseif(($byte1 & 0xFE) == 0xFC) {
-
-
-
-
-
- if ($i == $len) {
- return false;
- }
- if (!((ord($str[$i++]) & 0xC0) == 0x80)) {
- return false;
- }
- if ($i == $len) {
- return false;
- }
- if (!((ord($str[$i++]) & 0xC0) == 0x80)) {
- return false;
- }
- if ($i == $len) {
- return false;
- }
- if (!((ord($str[$i++]) & 0xC0) == 0x80)) {
- return false;
- }
- if ($i == $len) {
- return false;
- }
- if (!((ord($str[$i++]) & 0xC0) == 0x80)) {
- return false;
- }
- if ($i == $len) {
- return false;
- }
- if (!((ord($str[$i++]) & 0xC0) == 0x80)) {
- return false;
- }
- }
- else {
- return false;
- }
-
-
-
- }
- return true;
- }
- function api_is_valid_ascii(&$string) {
- if (MBSTRING_INSTALLED) {
- return @mb_detect_encoding($string, 'ASCII', true) == 'ASCII' ? true : false;
- }
- return !preg_match('/[^\x00-\x7F]/S', $string);
- }
- function & api_str_getcsv(& $string, $delimiter = ',', $enclosure = '"', $escape = '\\') {
- $delimiter = (string)$delimiter;
- if (api_byte_count($delimiter) > 1) { $delimiter = $delimiter[1]; }
- $enclosure = (string)$enclosure;
- if (api_byte_count($enclosure) > 1) { $enclosure = $enclosure[1]; }
- $escape = (string)$escape;
- if (api_byte_count($escape) > 1) { $escape = $escape[1]; }
- $str = (string)$string;
- $len = api_byte_count($str);
- $enclosed = false;
- $escaped = false;
- $value = '';
- $result = array();
- for ($i = 0; $i < $len; $i++) {
- $char = $str[$i];
- if ($char == $escape) {
- if (!$escaped) {
- $escaped = true;
- continue;
- }
- }
- $escaped = false;
- switch ($char) {
- case $enclosure:
- if ($enclosed && $str[$i + 1] == $enclosure) {
- $value .= $char;
- $i++;
- } else {
- $enclosed = !$enclosed;
- }
- break;
- case $delimiter:
- if (!$enclosed) {
- $result[] = $value;
- $value = '';
- } else {
- $value .= $char;
- }
- break;
- default:
- $value .= $char;
- break;
- }
- }
- if (!empty($value)) {
- $result[] = $value;
- }
- return $result;
- }
- function api_fgetcsv($handle, $length = null, $delimiter = ',', $enclosure = '"', $escape = '\\') {
- if (($line = is_null($length) ? fgets($handle): fgets($handle, $length)) !== false) {
- return api_str_getcsv($line, $delimiter, $enclosure, $escape);
- }
- return false;
- }
- function api_html_to_text($string) {
-
- $string = preg_replace('/<br[^>]*>/i', "\n", $string);
- $string = preg_replace('/<\/?(div|p|h[1-6]|table|ol|ul|blockquote)[^>]*>/i', "\n", $string);
- $string = preg_replace('/<\/(tr|li)[^>]*>/i', "\n", $string);
- $string = preg_replace('/<\/(td|th)[^>]*>/i', "\t", $string);
- $string = strip_tags($string);
-
- $string = str_replace(array("\r\n", "\n\r", "\r"), "\n", $string);
- $string = preg_replace('/\s*\n/', "\n", $string);
- $string = preg_replace('/\n+/', "\n", $string);
- return trim($string);
- }
- require_once dirname(__FILE__).'/internationalization_internal.lib.php';
|