internationalization.lib.php 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. use Patchwork\Utf8;
  5. /**
  6. * File: internationalization.lib.php
  7. * Internationalization library for Chamilo 1.x LMS
  8. * A library implementing internationalization related functions.
  9. * License: GNU General Public License Version 3 (Free Software Foundation)ww.
  10. *
  11. * @author Ivan Tcholakov, <ivantcholakov@gmail.com>, 2009, 2010
  12. * @author More authors, mentioned in the correpsonding fragments of this source.
  13. *
  14. * @package chamilo.library
  15. */
  16. // Special tags for marking untranslated variables.
  17. define('SPECIAL_OPENING_TAG', '[=');
  18. define('SPECIAL_CLOSING_TAG', '=]');
  19. // Predefined date formats in Chamilo provided by the language sub-system.
  20. // To be used as a parameter for the function api_format_date()
  21. define('TIME_NO_SEC_FORMAT', 0); // 15:23
  22. define('DATE_FORMAT_SHORT', 1); // Aug 25, 09
  23. define('DATE_FORMAT_LONG', 2); // Monday August 25, 09
  24. define('DATE_FORMAT_LONG_NO_DAY', 10); // August 25, 2009
  25. define('DATE_TIME_FORMAT_LONG', 3); // Monday August 25, 2009 at 03:28 PM
  26. define('DATE_FORMAT_NUMBER', 4); // 25.08.09
  27. define('DATE_TIME_FORMAT_LONG_24H', 5); // August 25, 2009 at 15:28
  28. define('DATE_TIME_FORMAT_SHORT', 6); // Aug 25, 2009 at 03:28 PM
  29. define('DATE_TIME_FORMAT_SHORT_TIME_FIRST', 7); // 03:28 PM, Aug 25 2009
  30. define('DATE_FORMAT_NUMBER_NO_YEAR', 8); // 25.08 dd-mm
  31. define('DATE_FORMAT_ONLY_DAYNAME', 9); // Monday, Sunday, etc
  32. // Formatting person's name.
  33. // Formatting a person's name using the pattern as it has been
  34. // configured in the internationalization database for every language.
  35. // This (default) option would be the most used.
  36. define('PERSON_NAME_COMMON_CONVENTION', 0);
  37. // The following options may be used in limited number of places for overriding the common convention:
  38. // Formatting a person's name in Western order: first_name last_name
  39. define('PERSON_NAME_WESTERN_ORDER', 1);
  40. // Formatting a person's name in Eastern order: last_name first_name
  41. define('PERSON_NAME_EASTERN_ORDER', 2);
  42. // Contextual: formatting person's name in library order: last_name, first_name
  43. define('PERSON_NAME_LIBRARY_ORDER', 3);
  44. // Contextual: formatting a person's name assotiated with an email-address.
  45. // Ivan: I am not sure how seems email servers an clients would interpret name order, so I assign the Western order.
  46. define('PERSON_NAME_EMAIL_ADDRESS', PERSON_NAME_WESTERN_ORDER);
  47. // Contextual: formatting a person's name for data-exporting operations.
  48. // For backward compatibility this format has been set to Eastern order.
  49. define('PERSON_NAME_DATA_EXPORT', PERSON_NAME_EASTERN_ORDER);
  50. /**
  51. * Returns a translated (localized) string, called by its ID.
  52. *
  53. * @param string $variable this is the ID (name) of the translated string to be retrieved
  54. * @param bool $returnEmptyIfNotFound If variable is not found, then: if false: returns variable name with or without brackets; true: returns ''
  55. * @param string $language (optional) Language ID. If it is omitted, the current interface language is assumed.
  56. *
  57. * @return string returns the requested string in the correspondent language
  58. *
  59. * @author Roan Embrechts
  60. * @author Patrick Cool
  61. * @author Ivan Tcholakov, 2009-2010 (caching functionality, additional parameter $language, other adaptations).
  62. *
  63. * Notes:
  64. * 1. If the name of a given language variable has the prefix "lang" it may be omited, i.e. get_lang('Yes') == get_lang('Yes').
  65. * 2. Untranslated variables might be indicated by special opening and closing tags - [= =]
  66. * The special tags do not show up in these two cases:
  67. * - when the system has been switched to "production server mode";
  68. * - when a special platform setting 'hide_dltt_markup' is set to "true" (the name of this setting comes from history);
  69. * 3. Translations are created many contributors through using a special tool: Chamilo Translation Application.
  70. *
  71. * @see http://translate.chamilo.org/
  72. */
  73. function get_lang($variable, $returnEmptyIfNotFound = false, $language = null)
  74. {
  75. // For serving some old hacks:
  76. // By manipulating this global variable the translation may
  77. // be done in different languages too (not the elegant way).
  78. global $language_interface;
  79. // Because of possibility for manipulations of the global
  80. // variable $language_interface, we need its initial value.
  81. global $language_interface_initial_value;
  82. global $used_lang_vars, $_configuration;
  83. // add language_measure_frequency to your main/inc/conf/configuration.php in order to generate language
  84. // variables frequency measurements (you can then see them trhough main/cron/lang/langstats.php)
  85. // The $langstats object is instanciated at the end of main/inc/global.inc.php
  86. if (isset($_configuration['language_measure_frequency']) &&
  87. $_configuration['language_measure_frequency'] == 1
  88. ) {
  89. require_once api_get_path(SYS_CODE_PATH).'/cron/lang/langstats.class.php';
  90. global $langstats;
  91. $langstats->add_use($variable, '');
  92. }
  93. if (!isset($used_lang_vars)) {
  94. $used_lang_vars = [];
  95. }
  96. // Caching results from some API functions, for speed.
  97. static $initialized, $show_special_markup;
  98. if (!isset($initialized)) {
  99. $test_server_mode = api_get_setting('server_type') === 'test';
  100. $show_special_markup = api_get_setting('hide_dltt_markup') != 'true' || $test_server_mode;
  101. $initialized = true;
  102. }
  103. // Combining both ways for requesting specific language.
  104. if (empty($language)) {
  105. $language = $language_interface;
  106. }
  107. $lang_postfix = isset($is_interface_language) && $is_interface_language ? '' : '('.$language.')';
  108. $is_interface_language = $language == $language_interface_initial_value;
  109. // This is a cache for already translated language variables. By using it, we avoid repetitive translations, gaining speed.
  110. static $cache;
  111. // Looking up into the cache for existing translation.
  112. if (isset($cache[$language][$variable])) {
  113. // There is a previously saved translation, returning it.
  114. //return $cache[$language][$variable];
  115. $ret = $cache[$language][$variable];
  116. $used_lang_vars[$variable.$lang_postfix] = $ret;
  117. return $ret;
  118. }
  119. // There is no cached translation, we have to retrieve it:
  120. // - from a global variable (the faster way) - on production server mode;
  121. // - from a local variable after reloading the language files - on test server mode or when requested language
  122. // is different than the genuine interface language.
  123. $read_global_variables = $is_interface_language;
  124. $langvar = '';
  125. if ($read_global_variables) {
  126. if (isset($GLOBALS[$variable])) {
  127. $langvar = $GLOBALS[$variable];
  128. } elseif (isset($GLOBALS["lang$variable"])) {
  129. $langvar = $GLOBALS["lang$variable"];
  130. } else {
  131. if (!$returnEmptyIfNotFound) {
  132. $langvar = $show_special_markup ? SPECIAL_OPENING_TAG.$variable.SPECIAL_CLOSING_TAG : $variable;
  133. } else {
  134. return '';
  135. }
  136. }
  137. }
  138. if (empty($langvar) || !is_string($langvar) && !$returnEmptyIfNotFound) {
  139. $langvar = $show_special_markup ? SPECIAL_OPENING_TAG.$variable.SPECIAL_CLOSING_TAG : $variable;
  140. }
  141. $ret = $cache[$language][$variable] = $langvar;
  142. $used_lang_vars[$variable.$lang_postfix] = $ret;
  143. return $ret;
  144. }
  145. /**
  146. * Gets the current interface language.
  147. *
  148. * @param bool $purified (optional) When it is true, a purified (refined)
  149. * language value will be returned, for example 'french' instead of 'french_unicode'
  150. * @param bool $setParentLanguageName
  151. *
  152. * @throws Exception
  153. *
  154. * @return string the current language of the interface
  155. */
  156. function api_get_interface_language(
  157. $purified = false,
  158. $check_sub_language = false,
  159. $setParentLanguageName = true
  160. ) {
  161. global $language_interface;
  162. if (empty($language_interface)) {
  163. return 'english';
  164. }
  165. if ($check_sub_language) {
  166. static $parent_language_name = null;
  167. if (!isset($parent_language_name)) {
  168. // 2. The current language is a sub language so we grab the father's
  169. // setting according to the internalization_database/name_order_convetions.php file
  170. $language_id = api_get_language_id($language_interface);
  171. $language_info = api_get_language_info($language_id);
  172. if (!empty($language_id) &&
  173. !empty($language_info)
  174. ) {
  175. if (!empty($language_info['parent_id'])) {
  176. $language_info = api_get_language_info($language_info['parent_id']);
  177. if ($setParentLanguageName) {
  178. $parent_language_name = $language_info['english_name'];
  179. }
  180. if (!empty($parent_language_name)) {
  181. return $parent_language_name;
  182. }
  183. }
  184. return $language_info['english_name'];
  185. }
  186. return 'english';
  187. } else {
  188. return $parent_language_name;
  189. }
  190. } else {
  191. // 2. Normal way
  192. $interface_language = $purified ? api_purify_language_id($language_interface) : $language_interface;
  193. }
  194. return $interface_language;
  195. }
  196. /**
  197. * Returns a purified language id, without possible suffixes that will disturb language identification in certain cases.
  198. *
  199. * @param string $language the input language identificator, for example 'french_unicode'
  200. * @param string the same purified or filtered language id, for example 'french'
  201. *
  202. * @return string
  203. */
  204. function api_purify_language_id($language)
  205. {
  206. static $purified = [];
  207. if (!isset($purified[$language])) {
  208. $purified[$language] = trim(
  209. str_replace(
  210. ['_unicode', '_latin', '_corporate', '_org', '_km'],
  211. '',
  212. strtolower($language)
  213. )
  214. );
  215. }
  216. return $purified[$language];
  217. }
  218. /**
  219. * Gets language isocode column from the language table, taking the given language as a query parameter.
  220. *
  221. * @param string $language This is the name of the folder containing translations for the corresponding language (e.g arabic, english).
  222. * @param string $default_code This is the value to be returned if there was no code found corresponding to the given language.
  223. * If $language is omitted, interface language is assumed then.
  224. *
  225. * @return string The found isocode or null on error.
  226. * Returned codes are according to the following standards (in order of preference):
  227. * - ISO 639-1 : Alpha-2 code (two-letters code - en, fr, es, ...)
  228. * - RFC 4646 : five-letter code based on the ISO 639 two-letter language codes
  229. * and the ISO 3166 two-letter territory codes (pt-BR, ...)
  230. * - ISO 639-2 : Alpha-3 code (three-letters code - ast, fur, ...)
  231. */
  232. function api_get_language_isocode($language = null, $default_code = 'en')
  233. {
  234. static $iso_code = [];
  235. if (empty($language)) {
  236. $language = api_get_interface_language(false, true);
  237. }
  238. if (!isset($iso_code[$language])) {
  239. if (!class_exists('Database')) {
  240. // This might happen, in case of calling this function early during the global initialization.
  241. return $default_code; // This might happen, in case of calling this function early during the global initialization.
  242. }
  243. $sql = "SELECT isocode
  244. FROM ".Database::get_main_table(TABLE_MAIN_LANGUAGE)."
  245. WHERE dokeos_folder = '$language'";
  246. $sql_result = Database::query($sql);
  247. if (Database::num_rows($sql_result)) {
  248. $result = Database::fetch_array($sql_result);
  249. $iso_code[$language] = trim($result['isocode']);
  250. } else {
  251. $language_purified_id = api_purify_language_id($language);
  252. $iso_code[$language] = isset($iso_code[$language_purified_id]) ? $iso_code[$language_purified_id] : null;
  253. }
  254. if (empty($iso_code[$language])) {
  255. $iso_code[$language] = $default_code;
  256. }
  257. }
  258. return $iso_code[$language];
  259. }
  260. /**
  261. * Gets language iso code column from the language table.
  262. *
  263. * @return array An array with the current isocodes
  264. *
  265. * */
  266. function api_get_platform_isocodes()
  267. {
  268. $iso_code = [];
  269. $sql = "SELECT isocode
  270. FROM ".Database::get_main_table(TABLE_MAIN_LANGUAGE)."
  271. ORDER BY isocode ";
  272. $sql_result = Database::query($sql);
  273. if (Database::num_rows($sql_result)) {
  274. while ($row = Database::fetch_array($sql_result)) {
  275. $iso_code[] = trim($row['isocode']);
  276. }
  277. }
  278. return $iso_code;
  279. }
  280. /**
  281. * Gets text direction according to the given language.
  282. *
  283. * @param string $language This is the name of the
  284. * folder containing translations for the corresponding language (e.g 'arabic', 'english', ...).
  285. * ISO-codes are acceptable too ('ar', 'en', ...).
  286. * If $language is omitted, interface language is assumed then.
  287. *
  288. * @return string the correspondent to the language text direction ('ltr' or 'rtl')
  289. */
  290. function api_get_text_direction($language = null)
  291. {
  292. static $text_direction = [];
  293. if (empty($language)) {
  294. $language = api_get_interface_language();
  295. }
  296. if (!isset($text_direction[$language])) {
  297. $text_direction[$language] = in_array(
  298. api_purify_language_id($language),
  299. [
  300. 'arabic',
  301. 'ar',
  302. 'dari',
  303. 'prs',
  304. 'hebrew',
  305. 'he',
  306. 'iw',
  307. 'pashto',
  308. 'ps',
  309. 'persian',
  310. 'fa',
  311. 'ur',
  312. 'yiddish',
  313. 'yid',
  314. ]
  315. ) ? 'rtl' : 'ltr';
  316. }
  317. return $text_direction[$language];
  318. }
  319. /**
  320. * Returns an alphabetized list of timezones in an associative array
  321. * that can be used to populate a select.
  322. *
  323. * @return array List of timezone identifiers
  324. *
  325. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  326. */
  327. function api_get_timezones()
  328. {
  329. $timezone_identifiers = DateTimeZone::listIdentifiers();
  330. sort($timezone_identifiers);
  331. $out = [];
  332. foreach ($timezone_identifiers as $tz) {
  333. $out[$tz] = $tz;
  334. }
  335. $null_option = ['' => ''];
  336. $result = array_merge($null_option, $out);
  337. return $result;
  338. }
  339. /**
  340. * Returns the timezone to be converted to/from, based on user or admin preferences.
  341. *
  342. * @return string The timezone chosen
  343. */
  344. function api_get_timezone()
  345. {
  346. $timezone = Session::read('system_timezone');
  347. if (empty($timezone)) {
  348. // First, get the default timezone of the server
  349. $timezone = date_default_timezone_get();
  350. // Second, see if a timezone has been chosen for the platform
  351. $timezoneFromSettings = api_get_setting('timezone_value', 'timezones');
  352. if ($timezoneFromSettings != null) {
  353. $timezone = $timezoneFromSettings;
  354. }
  355. // If allowed by the administrator
  356. $allowUserTimezones = api_get_setting('use_users_timezone', 'timezones');
  357. if ($allowUserTimezones === 'true') {
  358. $userId = api_get_user_id();
  359. // Get the timezone based on user preference, if it exists
  360. $newExtraField = new ExtraFieldValue('user');
  361. $data = $newExtraField->get_values_by_handler_and_field_variable($userId, 'timezone');
  362. if (!empty($data) && isset($data['timezone']) && !empty($data['timezone'])) {
  363. $timezone = $data['timezone'];
  364. }
  365. }
  366. Session::write('system_timezone', $timezone);
  367. }
  368. return $timezone;
  369. }
  370. /**
  371. * Returns the given date as a DATETIME in UTC timezone.
  372. * This function should be used before entering any date in the DB.
  373. *
  374. * @param mixed $time Date to be converted (can be a string supported by date() or a timestamp)
  375. * @param bool $returnNullIfInvalidDate If the date is not correct return null instead of the current date
  376. * @param bool $returnObj Returns a DateTime object
  377. *
  378. * @return string|DateTime The DATETIME in UTC to be inserted in the DB,
  379. * or null if the format of the argument is not supported
  380. *
  381. * @author Julio Montoya - Adding the 2nd parameter
  382. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  383. */
  384. function api_get_utc_datetime(
  385. $time = null,
  386. $returnNullIfInvalidDate = false,
  387. $returnObj = false
  388. ) {
  389. if (is_null($time) || empty($time) || $time === '0000-00-00 00:00:00') {
  390. if ($returnNullIfInvalidDate) {
  391. return null;
  392. }
  393. if ($returnObj) {
  394. return new DateTime(gmdate('Y-m-d H:i:s'), new DateTimeZone('UTC'));
  395. }
  396. return gmdate('Y-m-d H:i:s');
  397. }
  398. // If time is a timestamp, return directly in utc
  399. if (is_numeric($time)) {
  400. $time = (int) $time;
  401. return gmdate('Y-m-d H:i:s', $time);
  402. }
  403. try {
  404. $fromTimezone = api_get_timezone();
  405. $date = new DateTime($time, new DateTimezone($fromTimezone));
  406. $date->setTimezone(new DateTimeZone('UTC'));
  407. if ($returnObj) {
  408. return $date;
  409. } else {
  410. return $date->format('Y-m-d H:i:s');
  411. }
  412. } catch (Exception $e) {
  413. return null;
  414. }
  415. }
  416. /**
  417. * Returns a DATETIME string converted to the right timezone.
  418. *
  419. * @param mixed $time The time to be converted
  420. * @param string $to_timezone The timezone to be converted to.
  421. * If null, the timezone will be determined based on user preference,
  422. * or timezone chosen by the admin for the platform.
  423. * @param string $from_timezone The timezone to be converted from. If null, UTC will be assumed.
  424. * @param bool $returnNullIfInvalidDate
  425. * @param bool $showTime
  426. * @param bool $humanForm
  427. * @param string $format
  428. *
  429. * @return string The converted time formatted as Y-m-d H:i:s
  430. *
  431. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  432. */
  433. function api_get_local_time(
  434. $time = null,
  435. $to_timezone = null,
  436. $from_timezone = null,
  437. $returnNullIfInvalidDate = false,
  438. $showTime = true,
  439. $humanForm = false,
  440. $format = ''
  441. ) {
  442. // Determining the timezone to be converted from
  443. if (is_null($from_timezone)) {
  444. $from_timezone = 'UTC';
  445. }
  446. // If time is a timestamp, convert it to a string
  447. if (is_null($time) || empty($time) || $time == '0000-00-00 00:00:00') {
  448. if ($returnNullIfInvalidDate) {
  449. return null;
  450. }
  451. $from_timezone = 'UTC';
  452. $time = gmdate('Y-m-d H:i:s');
  453. }
  454. if (is_numeric($time)) {
  455. $time = (int) $time;
  456. if ($returnNullIfInvalidDate) {
  457. if (strtotime(date('d-m-Y H:i:s', $time)) !== (int) $time) {
  458. return null;
  459. }
  460. }
  461. $from_timezone = 'UTC';
  462. $time = gmdate('Y-m-d H:i:s', $time);
  463. }
  464. if ($time instanceof DateTime) {
  465. $time = $time->format('Y-m-d H:i:s');
  466. $from_timezone = 'UTC';
  467. }
  468. try {
  469. // Determining the timezone to be converted to
  470. if (is_null($to_timezone)) {
  471. $to_timezone = api_get_timezone();
  472. }
  473. $date = new DateTime($time, new DateTimezone($from_timezone));
  474. $date->setTimezone(new DateTimeZone($to_timezone));
  475. if (!empty($format)) {
  476. return $date->format($format);
  477. }
  478. return api_get_human_date_time($date, $showTime, $humanForm);
  479. } catch (Exception $e) {
  480. return '';
  481. }
  482. }
  483. /**
  484. * Converts a string into a timestamp safely (handling timezones), using strtotime.
  485. *
  486. * @param string $time to be converted
  487. * @param string $timezone (if null, the timezone will be determined based
  488. * on user preference, or timezone chosen by the admin for the platform)
  489. *
  490. * @return int Timestamp
  491. *
  492. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  493. */
  494. function api_strtotime($time, $timezone = null)
  495. {
  496. $system_timezone = date_default_timezone_get();
  497. if (!empty($timezone)) {
  498. date_default_timezone_set($timezone);
  499. }
  500. $timestamp = strtotime($time);
  501. if (!empty($timezone)) {
  502. // only reset timezone if it was changed
  503. date_default_timezone_set($system_timezone);
  504. }
  505. return $timestamp;
  506. }
  507. /**
  508. * Returns formatted date/time, correspondent to a given language.
  509. * The given date should be in the timezone chosen by the administrator
  510. * and/or user. Use api_get_local_time to get it.
  511. *
  512. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  513. * @author Christophe Gesche<gesche@ipm.ucl.ac.be>
  514. * originally inspired from from PhpMyAdmin
  515. * @author Ivan Tcholakov, 2009, code refactoring, adding support for predefined date/time formats.
  516. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  517. *
  518. * @param mixed $time Timestamp or datetime string
  519. * @param string|int Date format (see date formats in the Chamilo system:
  520. * TIME_NO_SEC_FORMAT,
  521. * DATE_FORMAT_SHORT,
  522. * DATE_FORMAT_LONG,
  523. * DATE_TIME_FORMAT_LONG
  524. * @param string $language (optional) Language id
  525. * If it is omitted, the current interface language is assumed
  526. *
  527. * @return string returns the formatted date
  528. *
  529. * @see http://php.net/manual/en/function.strftime.php
  530. */
  531. function api_format_date($time, $format = null, $language = null)
  532. {
  533. if (empty($time)) {
  534. return '';
  535. }
  536. $system_timezone = date_default_timezone_get();
  537. date_default_timezone_set(api_get_timezone());
  538. if (is_string($time)) {
  539. $time = strtotime($time);
  540. }
  541. if (is_null($format)) {
  542. $format = DATE_TIME_FORMAT_LONG;
  543. }
  544. $datetype = null;
  545. $timetype = null;
  546. if (is_int($format)) {
  547. switch ($format) {
  548. case DATE_FORMAT_ONLY_DAYNAME:
  549. $date_format = get_lang('dateFormatOnlyDayName', '', $language);
  550. if (INTL_INSTALLED) {
  551. $datetype = IntlDateFormatter::SHORT;
  552. $timetype = IntlDateFormatter::NONE;
  553. }
  554. break;
  555. case DATE_FORMAT_NUMBER_NO_YEAR:
  556. $date_format = get_lang('dateFormatShortNumberNoYear', '', $language);
  557. if (INTL_INSTALLED) {
  558. $datetype = IntlDateFormatter::SHORT;
  559. $timetype = IntlDateFormatter::NONE;
  560. }
  561. break;
  562. case DATE_FORMAT_NUMBER:
  563. $date_format = get_lang('dateFormatShortNumber', '', $language);
  564. if (INTL_INSTALLED) {
  565. $datetype = IntlDateFormatter::SHORT;
  566. $timetype = IntlDateFormatter::NONE;
  567. }
  568. break;
  569. case TIME_NO_SEC_FORMAT:
  570. $date_format = get_lang('timeNoSecFormat', '', $language);
  571. if (INTL_INSTALLED) {
  572. $datetype = IntlDateFormatter::NONE;
  573. $timetype = IntlDateFormatter::SHORT;
  574. }
  575. break;
  576. case DATE_FORMAT_SHORT:
  577. $date_format = get_lang('dateFormatShort', '', $language);
  578. if (INTL_INSTALLED) {
  579. $datetype = IntlDateFormatter::LONG;
  580. $timetype = IntlDateFormatter::NONE;
  581. }
  582. break;
  583. case DATE_FORMAT_LONG:
  584. $date_format = get_lang('dateFormatLong', '', $language);
  585. if (INTL_INSTALLED) {
  586. $datetype = IntlDateFormatter::FULL;
  587. $timetype = IntlDateFormatter::NONE;
  588. }
  589. break;
  590. case DATE_TIME_FORMAT_LONG:
  591. $date_format = get_lang('dateTimeFormatLong', '', $language);
  592. if (INTL_INSTALLED) {
  593. $datetype = IntlDateFormatter::FULL;
  594. $timetype = IntlDateFormatter::SHORT;
  595. }
  596. break;
  597. case DATE_FORMAT_LONG_NO_DAY:
  598. $date_format = get_lang('dateFormatLongNoDay', '', $language);
  599. if (INTL_INSTALLED) {
  600. $datetype = IntlDateFormatter::FULL;
  601. $timetype = IntlDateFormatter::SHORT;
  602. }
  603. break;
  604. case DATE_TIME_FORMAT_SHORT:
  605. $date_format = get_lang('dateTimeFormatShort', '', $language);
  606. if (INTL_INSTALLED) {
  607. $datetype = IntlDateFormatter::FULL;
  608. $timetype = IntlDateFormatter::SHORT;
  609. }
  610. break;
  611. case DATE_TIME_FORMAT_SHORT_TIME_FIRST:
  612. $date_format = get_lang('dateTimeFormatShortTimeFirst', '', $language);
  613. if (INTL_INSTALLED) {
  614. $datetype = IntlDateFormatter::FULL;
  615. $timetype = IntlDateFormatter::SHORT;
  616. }
  617. break;
  618. case DATE_TIME_FORMAT_LONG_24H:
  619. $date_format = get_lang('dateTimeFormatLong24H', '', $language);
  620. if (INTL_INSTALLED) {
  621. $datetype = IntlDateFormatter::FULL;
  622. $timetype = IntlDateFormatter::SHORT;
  623. }
  624. break;
  625. default:
  626. $date_format = get_lang('dateTimeFormatLong', '', $language);
  627. if (INTL_INSTALLED) {
  628. $datetype = IntlDateFormatter::FULL;
  629. $timetype = IntlDateFormatter::SHORT;
  630. }
  631. }
  632. } else {
  633. $date_format = $format;
  634. }
  635. if (0) {
  636. //if using PHP 5.3 format dates like: $dateFormatShortNumber, can't be used
  637. //
  638. // Use ICU
  639. if (is_null($language)) {
  640. $language = api_get_language_isocode();
  641. }
  642. $date_formatter = new IntlDateFormatter($language, $datetype, $timetype, date_default_timezone_get());
  643. //$date_formatter->setPattern($date_format);
  644. $formatted_date = api_to_system_encoding($date_formatter->format($time), 'UTF-8');
  645. } else {
  646. // We replace %a %A %b %B masks of date format with translated strings
  647. $translated = &_api_get_day_month_names($language);
  648. $date_format = str_replace(
  649. ['%A', '%a', '%B', '%b'],
  650. [
  651. $translated['days_long'][(int) strftime('%w', $time)],
  652. $translated['days_short'][(int) strftime('%w', $time)],
  653. $translated['months_long'][(int) strftime('%m', $time) - 1],
  654. $translated['months_short'][(int) strftime('%m', $time) - 1],
  655. ],
  656. $date_format
  657. );
  658. $formatted_date = api_to_system_encoding(strftime($date_format, $time), 'UTF-8');
  659. }
  660. date_default_timezone_set($system_timezone);
  661. return $formatted_date;
  662. }
  663. /**
  664. * Returns the difference between the current date (date(now)) with the parameter
  665. * $date in a string format like "2 days ago, 1 hour ago".
  666. * You can use it like this:
  667. * Display::dateToStringAgoAndLongDate($dateInUtc);.
  668. *
  669. * @param string $date Result of a date function in this format -> date('Y-m-d H:i:s', time());
  670. * @param string $timeZone
  671. * @param bool $returnDateDifference
  672. *
  673. * @return string
  674. *
  675. * @author Julio Montoya
  676. */
  677. function date_to_str_ago($date, $timeZone = 'UTC', $returnDateDifference = false)
  678. {
  679. if ($date === '0000-00-00 00:00:00') {
  680. return '';
  681. }
  682. $getOldTimezone = api_get_timezone();
  683. $isoCode = api_get_language_isocode();
  684. if ($isoCode == 'pt') {
  685. $isoCode = 'pt-BR';
  686. }
  687. $checkFile = api_get_path(SYS_PATH).'vendor/jimmiw/php-time-ago/translations/'.$isoCode.'.php';
  688. if (!file_exists($checkFile)) {
  689. $isoCode = 'en';
  690. }
  691. $timeAgo = new TimeAgo($timeZone, $isoCode);
  692. $value = $timeAgo->inWords($date);
  693. date_default_timezone_set($getOldTimezone);
  694. if ($returnDateDifference) {
  695. $value = $timeAgo->dateDifference($date);
  696. }
  697. return $value;
  698. }
  699. /**
  700. * Converts a date to the right timezone and localizes it in the format given as an argument.
  701. *
  702. * @param mixed The time to be converted
  703. * @param mixed Format to be used (TIME_NO_SEC_FORMAT, DATE_FORMAT_SHORT, DATE_FORMAT_LONG, DATE_TIME_FORMAT_LONG)
  704. * @param string Timezone to be converted from. If null, UTC will be assumed.
  705. *
  706. * @return string Converted and localized date
  707. *
  708. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  709. */
  710. function api_convert_and_format_date($time = null, $format = null, $from_timezone = null)
  711. {
  712. // First, convert the datetime to the right timezone
  713. $time = api_get_local_time($time, null, $from_timezone, true);
  714. // Second, localize the date
  715. return api_format_date($time, $format);
  716. }
  717. /**
  718. * Returns an array of translated week days in short names.
  719. *
  720. * @param string $language (optional) Language id. If it is omitted, the current interface language is assumed.
  721. *
  722. * @return string Returns an array of week days (short names).
  723. * Example: api_get_week_days_short('english') means array('Sun', 'Mon', ... 'Sat').
  724. * Note: For all languges returned days are in the English order.
  725. */
  726. function api_get_week_days_short($language = null)
  727. {
  728. $days = &_api_get_day_month_names($language);
  729. return $days['days_short'];
  730. }
  731. /**
  732. * Returns an array of translated week days.
  733. *
  734. * @param string $language (optional) Language id. If it is omitted,
  735. * the current interface language is assumed.
  736. *
  737. * @return string Returns an array of week days.
  738. * Example: api_get_week_days_long('english') means array('Sunday, 'Monday', ... 'Saturday').
  739. * Note: For all languges returned days are in the English order.
  740. */
  741. function api_get_week_days_long($language = null)
  742. {
  743. $days = &_api_get_day_month_names($language);
  744. return $days['days_long'];
  745. }
  746. /**
  747. * Returns an array of translated months in short names.
  748. *
  749. * @param string $language (optional) Language id.
  750. * If it is omitted, the current interface language is assumed.
  751. *
  752. * @return string Returns an array of months (short names).
  753. * Example: api_get_months_short('english') means array('Jan', 'Feb', ... 'Dec').
  754. */
  755. function api_get_months_short($language = null)
  756. {
  757. $months = &_api_get_day_month_names($language);
  758. return $months['months_short'];
  759. }
  760. /**
  761. * Returns an array of translated months.
  762. *
  763. * @param string $language (optional) Language id.
  764. * If it is omitted, the current interface language is assumed.
  765. *
  766. * @return string Returns an array of months.
  767. * Example: api_get_months_long('english') means array('January, 'February' ... 'December').
  768. */
  769. function api_get_months_long($language = null)
  770. {
  771. $months = &_api_get_day_month_names($language);
  772. return $months['months_long'];
  773. }
  774. /**
  775. * Name order conventions.
  776. */
  777. /**
  778. * Builds a person (full) name depending on the convention for a given language.
  779. *
  780. * @param string $first_name the first name of the person
  781. * @param string $last_name the last name of the person
  782. * @param string $title the title of the person
  783. * @param int|string $format (optional) The person name format.
  784. * It may be a pattern-string (for example '%t %l, %f' or '%T %F %L', ...) or
  785. * some of the constants
  786. * PERSON_NAME_COMMON_CONVENTION (default),
  787. * PERSON_NAME_WESTERN_ORDER,
  788. * PERSON_NAME_EASTERN_ORDER,
  789. * PERSON_NAME_LIBRARY_ORDER.
  790. * @param string $language (optional)
  791. * The language id. If it is omitted, the current interface language is assumed.
  792. * This parameter has meaning with the format PERSON_NAME_COMMON_CONVENTION only.
  793. * @param string $username
  794. *
  795. * @return string The result is sort of full name of the person.
  796. * Sample results:
  797. * Peter Ustinoff or Dr. Peter Ustinoff - the Western order
  798. * Ustinoff Peter or Dr. Ustinoff Peter - the Eastern order
  799. * Ustinoff, Peter or - Dr. Ustinoff, Peter - the library order
  800. * Note: See the file main/inc/lib/internationalization_database/name_order_conventions.php
  801. * where you can check the convention for your language.
  802. *
  803. * @author Carlos Vargas <carlos.vargas@dokeos.com> - initial implementation.
  804. * @author Ivan Tcholakov
  805. */
  806. function api_get_person_name(
  807. $first_name,
  808. $last_name,
  809. $title = null,
  810. $format = null,
  811. $language = null,
  812. $username = null
  813. ) {
  814. static $valid = [];
  815. if (empty($format)) {
  816. $format = PERSON_NAME_COMMON_CONVENTION;
  817. }
  818. // We check if the language is supported, otherwise we check the
  819. // interface language of the parent language of sublanguage
  820. if (empty($language)) {
  821. // Do not set $setParentLanguageName because this function is called before
  822. // the main language is loaded in global.inc.php
  823. $language = api_get_interface_language(false, true, false);
  824. }
  825. if (!isset($valid[$format][$language])) {
  826. if (is_int($format)) {
  827. switch ($format) {
  828. case PERSON_NAME_COMMON_CONVENTION:
  829. $valid[$format][$language] = _api_get_person_name_convention($language, 'format');
  830. $usernameOrderFromDatabase = api_get_setting('user_name_order');
  831. if (isset($usernameOrderFromDatabase) && !empty($usernameOrderFromDatabase)) {
  832. $valid[$format][$language] = $usernameOrderFromDatabase;
  833. }
  834. break;
  835. case PERSON_NAME_WESTERN_ORDER:
  836. $valid[$format][$language] = '%t %f %l';
  837. break;
  838. case PERSON_NAME_EASTERN_ORDER:
  839. $valid[$format][$language] = '%t %l %f';
  840. break;
  841. case PERSON_NAME_LIBRARY_ORDER:
  842. $valid[$format][$language] = '%t %l, %f';
  843. break;
  844. default:
  845. $valid[$format][$language] = '%t %f %l';
  846. break;
  847. }
  848. } else {
  849. $valid[$format][$language] = _api_validate_person_name_format($format);
  850. }
  851. }
  852. $format = $valid[$format][$language];
  853. $keywords = [
  854. '%firstname',
  855. '%f',
  856. '%F',
  857. '%lastname',
  858. '%l',
  859. '%L',
  860. '%title',
  861. '%t',
  862. '%T',
  863. '%username',
  864. '%u',
  865. '%U',
  866. ];
  867. $values = [
  868. $first_name,
  869. $first_name,
  870. api_strtoupper($first_name),
  871. $last_name,
  872. $last_name,
  873. api_strtoupper($last_name),
  874. $title,
  875. $title,
  876. api_strtoupper($title),
  877. $username,
  878. $username,
  879. api_strtoupper($username),
  880. ];
  881. $person_name = str_replace($keywords, $values, $format);
  882. return _api_clean_person_name($person_name);
  883. }
  884. /**
  885. * Checks whether a given format represents person name in Western order (for which first name is first).
  886. *
  887. * @param int|string $format (optional) The person name format.
  888. * It may be a pattern-string (for example '%t. %l, %f') or some of the constants
  889. * PERSON_NAME_COMMON_CONVENTION (default),
  890. * PERSON_NAME_WESTERN_ORDER,
  891. * PERSON_NAME_EASTERN_ORDER,
  892. * PERSON_NAME_LIBRARY_ORDER.
  893. * @param string $language (optional) The language id. If it is omitted,
  894. * the current interface language is assumed. This parameter has meaning with the
  895. * format PERSON_NAME_COMMON_CONVENTION only.
  896. *
  897. * @return bool The result TRUE means that the order is first_name last_name,
  898. * FALSE means last_name first_name.
  899. * Note: You may use this function for determining the order of the fields or
  900. * columns "First name" and "Last name" in forms, tables and reports.
  901. *
  902. * @author Ivan Tcholakov
  903. */
  904. function api_is_western_name_order($format = null, $language = null)
  905. {
  906. static $order = [];
  907. if (empty($format)) {
  908. $format = PERSON_NAME_COMMON_CONVENTION;
  909. }
  910. if (empty($language)) {
  911. $language = api_get_interface_language(false, true);
  912. }
  913. if (!isset($order[$format][$language])) {
  914. $test_name = api_get_person_name('%f', '%l', '%t', $format, $language);
  915. $order[$format][$language] = stripos($test_name, '%f') <= stripos($test_name, '%l');
  916. }
  917. return $order[$format][$language];
  918. }
  919. /**
  920. * Returns a directive for sorting person names depending on a given language
  921. * and based on the options in the internationalization "database".
  922. *
  923. * @param string $language (optional) The input language.
  924. * If it is omitted, the current interface language is assumed.
  925. *
  926. * @return bool Returns boolean value. TRUE means ORDER BY first_name, last_name
  927. * FALSE means ORDER BY last_name, first_name.
  928. * Note: You may use this function:
  929. * 2. for constructing the ORDER clause of SQL queries, related to first_name and last_name;
  930. * 3. for adjusting php-implemented sorting in tables and reports.
  931. *
  932. * @author Ivan Tcholakov
  933. */
  934. function api_sort_by_first_name($language = null)
  935. {
  936. static $sort_by_first_name = [];
  937. if (empty($language)) {
  938. $language = api_get_interface_language(false, true);
  939. }
  940. if (!isset($sort_by_first_name[$language])) {
  941. $sort_by_first_name[$language] = _api_get_person_name_convention($language, 'sort_by');
  942. }
  943. return $sort_by_first_name[$language];
  944. }
  945. /**
  946. * Multibyte string conversion functions.
  947. */
  948. /**
  949. * Converts character encoding of a given string.
  950. *
  951. * @param string $string the string being converted
  952. * @param string $to_encoding the encoding that $string is being converted to
  953. * @param string $from_encoding (optional) The encoding that $string is being converted from.
  954. * If it is omitted, the platform character set is assumed.
  955. *
  956. * @return string Returns the converted string.
  957. * This function is aimed at replacing the function mb_convert_encoding() for human-language strings.
  958. *
  959. * @see http://php.net/manual/en/function.mb-convert-encoding
  960. */
  961. function api_convert_encoding($string, $to_encoding, $from_encoding = 'UTF-8')
  962. {
  963. if (strtoupper($to_encoding) === strtoupper($from_encoding)) {
  964. return $string;
  965. }
  966. return mb_convert_encoding($string, $to_encoding, $from_encoding);
  967. }
  968. /**
  969. * Converts a given string into UTF-8 encoded string.
  970. *
  971. * @param string $string the string being converted
  972. * @param string $from_encoding (optional) The encoding that $string is being converted from.
  973. * If it is omitted, the platform character set is assumed.
  974. *
  975. * @return string Returns the converted string.
  976. * This function is aimed at replacing the function utf8_encode() for human-language strings.
  977. *
  978. * @see http://php.net/manual/en/function.utf8-encode
  979. */
  980. function api_utf8_encode($string, $from_encoding = 'UTF-8')
  981. {
  982. return mb_convert_encoding($string, 'UTF-8', $from_encoding);
  983. }
  984. /**
  985. * Converts a given string from UTF-8 encoding to a specified encoding.
  986. *
  987. * @param string $string the string being converted
  988. * @param string $to_encoding (optional) The encoding that $string is being converted to.
  989. * If it is omitted, the platform character set is assumed.
  990. *
  991. * @return string Returns the converted string.
  992. * This function is aimed at replacing the function utf8_decode() for human-language strings.
  993. *
  994. * @see http://php.net/manual/en/function.utf8-decode
  995. */
  996. function api_utf8_decode($string, $to_encoding = null)
  997. {
  998. return mb_convert_encoding($string, $to_encoding, 'UTF-8');
  999. }
  1000. /**
  1001. * Converts a given string into the system encoding (or platform character set).
  1002. * When $from encoding is omitted on UTF-8 platforms then language dependent encoding
  1003. * is guessed/assumed. On non-UTF-8 platforms omitted $from encoding is assumed as UTF-8.
  1004. * When the parameter $check_utf8_validity is true the function checks string's
  1005. * UTF-8 validity and decides whether to try to convert it or not.
  1006. * This function is useful for problem detection or making workarounds.
  1007. *
  1008. * @param string $string the string being converted
  1009. * @param string $from_encoding (optional) The encoding that $string is being converted from.
  1010. * It is guessed when it is omitted.
  1011. * @param bool $check_utf8_validity (optional) A flag for UTF-8 validity check as condition for making conversion
  1012. *
  1013. * @return string returns the converted string
  1014. */
  1015. function api_to_system_encoding($string, $from_encoding = null, $check_utf8_validity = false)
  1016. {
  1017. $system_encoding = api_get_system_encoding();
  1018. return api_convert_encoding($string, $system_encoding, $from_encoding);
  1019. }
  1020. /**
  1021. * Converts all applicable characters to HTML entities.
  1022. *
  1023. * @param string $string the input string
  1024. * @param int $quote_style (optional) The quote style - ENT_COMPAT (default), ENT_QUOTES, ENT_NOQUOTES
  1025. * @param string $encoding (optional) The encoding (of the input string) used in conversion.
  1026. * If it is omitted, the platform character set is assumed.
  1027. *
  1028. * @return string Returns the converted string.
  1029. * This function is aimed at replacing the function htmlentities() for human-language strings.
  1030. *
  1031. * @see http://php.net/manual/en/function.htmlentities
  1032. */
  1033. function api_htmlentities($string, $quote_style = ENT_COMPAT, $encoding = 'UTF-8')
  1034. {
  1035. switch ($quote_style) {
  1036. case ENT_COMPAT:
  1037. $string = str_replace(['&', '"', '<', '>'], ['&amp;', '&quot;', '&lt;', '&gt;'], $string);
  1038. break;
  1039. case ENT_QUOTES:
  1040. $string = str_replace(['&', '\'', '"', '<', '>'], ['&amp;', '&#039;', '&quot;', '&lt;', '&gt;'], $string);
  1041. break;
  1042. }
  1043. return mb_convert_encoding($string, 'HTML-ENTITIES', 'UTF-8');
  1044. }
  1045. /**
  1046. * Converts HTML entities into normal characters.
  1047. *
  1048. * @param string $string the input string
  1049. * @param int $quote_style (optional) The quote style - ENT_COMPAT (default), ENT_QUOTES, ENT_NOQUOTES
  1050. * @param string $encoding (optional) The encoding (of the result) used in conversion.
  1051. * If it is omitted, the platform character set is assumed.
  1052. *
  1053. * @return string Returns the converted string.
  1054. * This function is aimed at replacing the function html_entity_decode() for human-language strings.
  1055. *
  1056. * @see http://php.net/html_entity_decode
  1057. */
  1058. function api_html_entity_decode($string, $quote_style = ENT_COMPAT, $encoding = 'UTF-8')
  1059. {
  1060. if (empty($encoding)) {
  1061. $encoding = _api_mb_internal_encoding();
  1062. }
  1063. if (api_is_encoding_supported($encoding)) {
  1064. if (!api_is_utf8($encoding)) {
  1065. $string = api_utf8_encode($string, $encoding);
  1066. }
  1067. $string = html_entity_decode($string, $quote_style, 'UTF-8');
  1068. if (!api_is_utf8($encoding)) {
  1069. return api_utf8_decode($string, $encoding);
  1070. }
  1071. return $string;
  1072. }
  1073. return $string; // Here the function gives up.
  1074. }
  1075. /**
  1076. * This function encodes (conditionally) a given string to UTF-8 if XmlHttp-request has been detected.
  1077. *
  1078. * @param string $string the string being converted
  1079. * @param string $from_encoding (optional) The encoding that $string is being converted from.
  1080. * If it is omitted, the platform character set is assumed.
  1081. *
  1082. * @return string returns the converted string
  1083. */
  1084. function api_xml_http_response_encode($string, $from_encoding = 'UTF8')
  1085. {
  1086. if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  1087. if (empty($from_encoding)) {
  1088. $from_encoding = _api_mb_internal_encoding();
  1089. }
  1090. if (!api_is_utf8($from_encoding)) {
  1091. return api_utf8_encode($string, $from_encoding);
  1092. }
  1093. }
  1094. return $string;
  1095. }
  1096. /**
  1097. * Transliterates a string with arbitrary encoding into a plain ASCII string.
  1098. *
  1099. * Example:
  1100. * echo api_transliterate(api_html_entity_decode(
  1101. * '&#1060;&#1105;&#1076;&#1086;&#1088; '.
  1102. * '&#1052;&#1080;&#1093;&#1072;&#1081;&#1083;&#1086;&#1074;&#1080;&#1095; '.
  1103. * '&#1044;&#1086;&#1089;&#1090;&#1086;&#1077;&#1074;&#1082;&#1080;&#1081;',
  1104. * ENT_QUOTES, 'UTF-8'), 'X', 'UTF-8');
  1105. * The output should be: Fyodor Mihaylovich Dostoevkiy
  1106. *
  1107. * @param string $string the input string
  1108. * @param string $unknown (optional) Replacement character for unknown characters and illegal UTF-8 sequences
  1109. * @param string $from_encoding (optional) The encoding of the input string.
  1110. * If it is omitted, the platform character set is assumed.
  1111. *
  1112. * @return string plain ASCII output
  1113. */
  1114. function api_transliterate($string, $unknown = '?', $from_encoding = null)
  1115. {
  1116. return URLify::transliterate($string);
  1117. }
  1118. /**
  1119. * Takes the first character in a string and returns its Unicode codepoint.
  1120. *
  1121. * @param string $character the input string
  1122. * @param string $encoding (optional) The encoding of the input string.
  1123. * If it is omitted, the platform character set will be used by default.
  1124. *
  1125. * @return int Returns: the codepoint of the first character; or 0xFFFD (unknown character) when the input string is empty.
  1126. * This is a multibyte aware version of the function ord().
  1127. *
  1128. * @see http://php.net/manual/en/function.ord.php
  1129. * Note the difference with the original funtion ord(): ord('') returns 0, api_ord('') returns 0xFFFD (unknown character).
  1130. */
  1131. function api_ord($character, $encoding = 'UTF-8')
  1132. {
  1133. return Utf8::ord(api_utf8_encode($character, $encoding));
  1134. }
  1135. /**
  1136. * This function returns a string or an array with all occurrences of search
  1137. * in subject (ignoring case) replaced with the given replace value.
  1138. *
  1139. * @param mixed $search string or array of strings to be found
  1140. * @param mixed $replace string or array of strings used for replacement
  1141. * @param mixed $subject string or array of strings being searched
  1142. * @param int $count (optional) The number of matched and replaced needles
  1143. * will be returned in count, which is passed by reference
  1144. * @param string $encoding (optional) The used internally by this function character encoding.
  1145. * If it is omitted, the platform character set will be used by default.
  1146. *
  1147. * @return mixed String or array as a result.
  1148. * Notes:
  1149. * If $subject is an array, then the search and replace is performed with
  1150. * every entry of subject, the return value is an array.
  1151. * If $search and $replace are arrays, then the function takes a value from
  1152. * each array and uses it to do search and replace on subject.
  1153. * If $replace has fewer values than search, then an empty string is used for the rest of replacement values.
  1154. * If $search is an array and $replace is a string, then this replacement string is used for every value of search.
  1155. * This function is aimed at replacing the function str_ireplace() for human-language strings.
  1156. *
  1157. * @see http://php.net/manual/en/function.str-ireplace
  1158. *
  1159. * @author Henri Sivonen, mailto:hsivonen@iki.fi
  1160. *
  1161. * @see http://hsivonen.iki.fi/php-utf8/
  1162. * Adaptation for Chamilo 1.8.7, 2010
  1163. * Initial implementation Dokeos LMS, August 2009
  1164. *
  1165. * @author Ivan Tcholakov
  1166. */
  1167. function api_str_ireplace($search, $replace, $subject, &$count = null, $encoding = null)
  1168. {
  1169. return Utf8::str_ireplace($search, $replace, $subject, $count);
  1170. }
  1171. /**
  1172. * Converts a string to an array.
  1173. *
  1174. * @param string $string the input string
  1175. * @param int $split_length maximum character-length of the chunk, one character by default
  1176. * @param string $encoding (optional) The used internally by this function
  1177. * character encoding. If it is omitted, the platform character set will be used by default.
  1178. *
  1179. * @return array The result array of chunks with the spcified length.
  1180. * Notes:
  1181. * If the optional split_length parameter is specified, the returned array will be broken down into chunks
  1182. * with each being split_length in length, otherwise each chunk will be one character in length.
  1183. * FALSE is returned if split_length is less than 1.
  1184. * If the split_length length exceeds the length of string, the entire string is returned as the first (and only) array element.
  1185. * This function is aimed at replacing the function str_split() for human-language strings.
  1186. *
  1187. * @see http://php.net/str_split
  1188. */
  1189. function api_str_split($string, $split_length = 1, $encoding = null)
  1190. {
  1191. return Utf8::str_split($string, $split_length);
  1192. }
  1193. /**
  1194. * Finds position of first occurrence of a string within another, case insensitive.
  1195. *
  1196. * @param string $haystack the string from which to get the position of the first occurrence
  1197. * @param string $needle the string to be found
  1198. * @param int $offset The position in $haystack to start searching from.
  1199. * If it is omitted, searching starts from the beginning.
  1200. * @param string $encoding (optional) The used internally by this function
  1201. * character encoding. If it is omitted, the platform character set will be used by default.
  1202. *
  1203. * @return mixed Returns the numeric position of the first occurrence of
  1204. * $needle in the $haystack, or FALSE if $needle is not found.
  1205. * Note: The first character's position is 0, the second character position is 1, and so on.
  1206. * This function is aimed at replacing the functions stripos() and mb_stripos() for human-language strings.
  1207. *
  1208. * @see http://php.net/manual/en/function.stripos
  1209. * @see http://php.net/manual/en/function.mb-stripos
  1210. */
  1211. function api_stripos($haystack, $needle, $offset = 0, $encoding = null)
  1212. {
  1213. return Utf8::stripos($haystack, $needle, $offset);
  1214. }
  1215. /**
  1216. * Finds first occurrence of a string within another, case insensitive.
  1217. *
  1218. * @param string $haystack the string from which to get the first occurrence
  1219. * @param mixed $needle the string to be found
  1220. * @param bool $before_needle (optional) Determines which portion of $haystack
  1221. * this function returns. The default value is FALSE.
  1222. * @param string $encoding (optional) The used internally by this function
  1223. * character encoding. If it is omitted, the platform character set will be used by default.
  1224. *
  1225. * @return mixed Returns the portion of $haystack, or FALSE if $needle is not found.
  1226. * Notes:
  1227. * If $needle is not a string, it is converted to an integer and applied as the
  1228. * ordinal value (codepoint if the encoding is UTF-8) of a character.
  1229. * If $before_needle is set to TRUE, the function returns all of $haystack
  1230. * from the beginning to the first occurrence of $needle.
  1231. * If $before_needle is set to FALSE, the function returns all of $haystack f
  1232. * rom the first occurrence of $needle to the end.
  1233. * This function is aimed at replacing the functions stristr() and mb_stristr() for human-language strings.
  1234. *
  1235. * @see http://php.net/manual/en/function.stristr
  1236. * @see http://php.net/manual/en/function.mb-stristr
  1237. */
  1238. function api_stristr($haystack, $needle, $before_needle = false, $encoding = null)
  1239. {
  1240. return Utf8::stristr($haystack, $needle, $before_needle);
  1241. }
  1242. /**
  1243. * Returns length of the input string.
  1244. *
  1245. * @param string $string the string which length is to be calculated
  1246. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1247. *
  1248. * @return int Returns the number of characters within the string. A multi-byte character is counted as 1.
  1249. * This function is aimed at replacing the functions strlen() and mb_strlen() for human-language strings.
  1250. *
  1251. * @see http://php.net/manual/en/function.strlen
  1252. * @see http://php.net/manual/en/function.mb-strlen
  1253. * Note: When you use strlen() to test for an empty string, you needn't change it to api_strlen().
  1254. * For example, in lines like the following:
  1255. * if (strlen($string) > 0)
  1256. * if (strlen($string) != 0)
  1257. * there is no need the original function strlen() to be changed, it works correctly and faster for these cases.
  1258. */
  1259. function api_strlen($string, $encoding = null)
  1260. {
  1261. return Utf8::strlen($string);
  1262. }
  1263. /**
  1264. * Finds position of first occurrence of a string within another.
  1265. *
  1266. * @param string $haystack the string from which to get the position of the first occurrence
  1267. * @param string $needle the string to be found
  1268. * @param int $offset (optional) The position in $haystack to start searching from. If it is omitted, searching starts from the beginning.
  1269. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1270. *
  1271. * @return mixed Returns the numeric position of the first occurrence of $needle in the $haystack, or FALSE if $needle is not found.
  1272. * Note: The first character's position is 0, the second character position is 1, and so on.
  1273. * This function is aimed at replacing the functions strpos() and mb_strpos() for human-language strings.
  1274. *
  1275. * @see http://php.net/manual/en/function.strpos
  1276. * @see http://php.net/manual/en/function.mb-strpos
  1277. */
  1278. function api_strpos($haystack, $needle, $offset = 0, $encoding = null)
  1279. {
  1280. return Utf8::strpos($haystack, $needle, $offset);
  1281. }
  1282. /**
  1283. * Finds the last occurrence of a character in a string.
  1284. *
  1285. * @param string $haystack the string from which to get the last occurrence
  1286. * @param mixed $needle the string which first character is to be found
  1287. * @param bool $before_needle (optional) Determines which portion of $haystack this function returns. The default value is FALSE.
  1288. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1289. *
  1290. * @return mixed Returns the portion of $haystack, or FALSE if the first character from $needle is not found.
  1291. * Notes:
  1292. * If $needle is not a string, it is converted to an integer and applied as the ordinal value (codepoint if the encoding is UTF-8) of a character.
  1293. * If $before_needle is set to TRUE, the function returns all of $haystack from the beginning to the first occurrence.
  1294. * If $before_needle is set to FALSE, the function returns all of $haystack from the first occurrence to the end.
  1295. * This function is aimed at replacing the functions strrchr() and mb_strrchr() for human-language strings.
  1296. *
  1297. * @see http://php.net/manual/en/function.strrchr
  1298. * @see http://php.net/manual/en/function.mb-strrchr
  1299. */
  1300. function api_strrchr($haystack, $needle, $before_needle = false, $encoding = null)
  1301. {
  1302. return Utf8::strrchr($haystack, $needle);
  1303. }
  1304. /**
  1305. * Reverses a string.
  1306. *
  1307. * @param string $string the string to be reversed
  1308. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1309. *
  1310. * @return string Returns the reversed string.
  1311. * This function is aimed at replacing the function strrev() for human-language strings.
  1312. *
  1313. * @see http://php.net/manual/en/function.strrev
  1314. */
  1315. function api_strrev($string, $encoding = null)
  1316. {
  1317. return Utf8::strrev($string);
  1318. }
  1319. /**
  1320. * Finds the position of last occurrence (case insensitive) of a string in a string.
  1321. *
  1322. * @param string $haystack the string from which to get the position of the last occurrence
  1323. * @param string $needle the string to be found
  1324. * @param int $offset (optional) $offset may be specified to begin searching an arbitrary position. Negative values will stop searching at an arbitrary point prior to the end of the string.
  1325. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1326. *
  1327. * @return mixed Returns the numeric position of the first occurrence (case insensitive) of $needle in the $haystack, or FALSE if $needle is not found.
  1328. * Note: The first character's position is 0, the second character position is 1, and so on.
  1329. * This function is aimed at replacing the functions strripos() and mb_strripos() for human-language strings.
  1330. *
  1331. * @see http://php.net/manual/en/function.strripos
  1332. * @see http://php.net/manual/en/function.mb-strripos
  1333. */
  1334. function api_strripos($haystack, $needle, $offset = 0, $encoding = null)
  1335. {
  1336. return Utf8::strripos($haystack, $needle, $offset);
  1337. }
  1338. /**
  1339. * Finds the position of last occurrence of a string in a string.
  1340. *
  1341. * @param string $haystack the string from which to get the position of the last occurrence
  1342. * @param string $needle the string to be found
  1343. * @param int $offset (optional) $offset may be specified to begin searching an arbitrary position. Negative values will stop searching at an arbitrary point prior to the end of the string.
  1344. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1345. *
  1346. * @return mixed Returns the numeric position of the first occurrence of $needle in the $haystack, or FALSE if $needle is not found.
  1347. * Note: The first character's position is 0, the second character position is 1, and so on.
  1348. * This function is aimed at replacing the functions strrpos() and mb_strrpos() for human-language strings.
  1349. *
  1350. * @see http://php.net/manual/en/function.strrpos
  1351. * @see http://php.net/manual/en/function.mb-strrpos
  1352. */
  1353. function api_strrpos($haystack, $needle, $offset = 0, $encoding = null)
  1354. {
  1355. return Utf8::strrpos($haystack, $needle, $offset);
  1356. }
  1357. /**
  1358. * Finds first occurrence of a string within another.
  1359. *
  1360. * @param string $haystack the string from which to get the first occurrence
  1361. * @param mixed $needle the string to be found
  1362. * @param bool $before_needle (optional) Determines which portion of $haystack this function returns. The default value is FALSE.
  1363. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1364. *
  1365. * @return mixed Returns the portion of $haystack, or FALSE if $needle is not found.
  1366. * Notes:
  1367. * If $needle is not a string, it is converted to an integer and applied as the ordinal value (codepoint if the encoding is UTF-8) of a character.
  1368. * If $before_needle is set to TRUE, the function returns all of $haystack from the beginning to the first occurrence of $needle.
  1369. * If $before_needle is set to FALSE, the function returns all of $haystack from the first occurrence of $needle to the end.
  1370. * This function is aimed at replacing the functions strstr() and mb_strstr() for human-language strings.
  1371. *
  1372. * @see http://php.net/manual/en/function.strstr
  1373. * @see http://php.net/manual/en/function.mb-strstr
  1374. */
  1375. function api_strstr($haystack, $needle, $before_needle = false, $encoding = null)
  1376. {
  1377. return Utf8::strstr($haystack, $needle, $before_needle);
  1378. }
  1379. /**
  1380. * Makes a string lowercase.
  1381. *
  1382. * @param string $string the string being lowercased
  1383. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1384. *
  1385. * @return string Returns the string with all alphabetic characters converted to lowercase.
  1386. * This function is aimed at replacing the functions strtolower() and mb_strtolower() for human-language strings.
  1387. *
  1388. * @see http://php.net/manual/en/function.strtolower
  1389. * @see http://php.net/manual/en/function.mb-strtolower
  1390. */
  1391. function api_strtolower($string, $encoding = null)
  1392. {
  1393. return Utf8::strtolower($string);
  1394. }
  1395. /**
  1396. * Makes a string uppercase.
  1397. *
  1398. * @param string $string the string being uppercased
  1399. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1400. *
  1401. * @return string Returns the string with all alphabetic characters converted to uppercase.
  1402. * This function is aimed at replacing the functions strtoupper() and mb_strtoupper() for human-language strings.
  1403. *
  1404. * @see http://php.net/manual/en/function.strtoupper
  1405. * @see http://php.net/manual/en/function.mb-strtoupper
  1406. */
  1407. function api_strtoupper($string, $encoding = null)
  1408. {
  1409. return Utf8::strtoupper($string);
  1410. }
  1411. /**
  1412. * // Gets part of a string.
  1413. *
  1414. * @param string $string the input string
  1415. * @param int $start the first position from which the extracted part begins
  1416. * @param int $length the length in character of the extracted part
  1417. * @param string $encoding (optional) The used internally by this function
  1418. * character encoding. If it is omitted, the platform character set will be used by default.
  1419. *
  1420. * @return string Returns the part of the string specified by the start and length parameters.
  1421. * Note: First character's position is 0. Second character position is 1, and so on.
  1422. * This function is aimed at replacing the functions substr() and mb_substr() for human-language strings.
  1423. *
  1424. * @see http://php.net/manual/en/function.substr
  1425. * @see http://php.net/manual/en/function.mb-substr
  1426. */
  1427. function api_substr($string, $start, $length = null, $encoding = null)
  1428. {
  1429. if (is_null($length)) {
  1430. $length = api_strlen($string, $encoding);
  1431. }
  1432. return Utf8::substr($string, $start, $length);
  1433. }
  1434. /**
  1435. * Counts the number of substring occurrences.
  1436. *
  1437. * @param string $haystack the string being checked
  1438. * @param string $needle the string being found
  1439. * @param string $encoding (optional) The used internally by this function character encoding.
  1440. * If it is omitted, the platform character set will be used by default.
  1441. *
  1442. * @return int the number of times the needle substring occurs in the haystack string
  1443. *
  1444. * @see http://php.net/manual/en/function.mb-substr-count.php
  1445. */
  1446. function api_substr_count($haystack, $needle, $encoding = null)
  1447. {
  1448. return Utf8::substr_count($haystack, $needle);
  1449. }
  1450. /**
  1451. * Replaces text within a portion of a string.
  1452. *
  1453. * @param string $string the input string
  1454. * @param string $replacement the replacement string
  1455. * @param int $start The position from which replacing will begin.
  1456. * Notes:
  1457. * If $start is positive, the replacing will begin at the $start'th offset into the string.
  1458. * If $start is negative, the replacing will begin at the $start'th character from the end of the string.
  1459. * @param int $length (optional) The position where replacing will end.
  1460. * Notes:
  1461. * If given and is positive, it represents the length of the portion of the string which is to be replaced.
  1462. * If it is negative, it represents the number of characters from the end of string at which to stop replacing.
  1463. * If it is not given, then it will default to api_strlen($string); i.e. end the replacing at the end of string.
  1464. * If $length is zero, then this function will have the effect of inserting replacement into the string at the given start offset.
  1465. * @param string $encoding (optional) The used internally by this function character encoding.
  1466. * If it is omitted, the platform character set will be used by default.
  1467. *
  1468. * @return string The result string is returned.
  1469. * This function is aimed at replacing the function substr_replace() for human-language strings.
  1470. *
  1471. * @see http://php.net/manual/function.substr-replace
  1472. */
  1473. function api_substr_replace($string, $replacement, $start, $length = null, $encoding = null)
  1474. {
  1475. if (is_null($length)) {
  1476. $length = api_strlen($string);
  1477. }
  1478. return UTf8::substr_replace($string, $replacement, $start, $length);
  1479. }
  1480. /**
  1481. * Makes a string's first character uppercase.
  1482. *
  1483. * @param string $string the input string
  1484. * @param string $encoding (optional) The used internally by this function character encoding.
  1485. * If it is omitted, the platform character set will be used by default.
  1486. *
  1487. * @return string Returns a string with the first character capitalized, if that character is alphabetic.
  1488. * This function is aimed at replacing the function ucfirst() for human-language strings.
  1489. *
  1490. * @see http://php.net/manual/en/function.ucfirst
  1491. */
  1492. function api_ucfirst($string, $encoding = null)
  1493. {
  1494. return Utf8::ucfirst($string);
  1495. }
  1496. /**
  1497. * Uppercases the first character of each word in a string.
  1498. *
  1499. * @param string $string the input string
  1500. * @param string $encoding (optional) The used internally by this function character encoding.
  1501. * If it is omitted, the platform character set will be used by default.
  1502. *
  1503. * @return string Returns the modified string.
  1504. * This function is aimed at replacing the function ucwords() for human-language strings.
  1505. *
  1506. * @see http://php.net/manual/en/function.ucwords
  1507. */
  1508. function api_ucwords($string, $encoding = null)
  1509. {
  1510. return Utf8::ucwords($string);
  1511. }
  1512. /**
  1513. * Performs a regular expression match, UTF-8 aware when it is applicable.
  1514. *
  1515. * @param string $pattern the pattern to search for, as a string
  1516. * @param string $subject the input string
  1517. * @param array &$matches (optional) If matches is provided,
  1518. * then it is filled with the results of search (as an array).
  1519. * $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.
  1520. * @param int $flags (optional) Could be PREG_OFFSET_CAPTURE. If this flag is passed, for every occurring match the appendant string offset will also be returned.
  1521. * Note that this changes the return value in an array where every element is an array consisting of the matched string at index 0 and its string offset into subject at index 1.
  1522. * @param int $offset (optional) Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search.
  1523. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1524. *
  1525. * @return int|bool returns the number of times pattern matches or FALSE if an error occurred
  1526. *
  1527. * @see http://php.net/preg_match
  1528. */
  1529. function api_preg_match(
  1530. $pattern,
  1531. $subject,
  1532. &$matches = null,
  1533. $flags = 0,
  1534. $offset = 0,
  1535. $encoding = null
  1536. ) {
  1537. if (empty($encoding)) {
  1538. $encoding = _api_mb_internal_encoding();
  1539. }
  1540. return preg_match(api_is_utf8($encoding) ? $pattern.'u' : $pattern, $subject, $matches, $flags, $offset);
  1541. }
  1542. /**
  1543. * Performs a global regular expression match, UTF-8 aware when it is applicable.
  1544. *
  1545. * @param string $pattern the pattern to search for, as a string
  1546. * @param string $subject the input string
  1547. * @param array &$matches (optional) Array of all matches in multi-dimensional array ordered according to $flags
  1548. * @param int $flags (optional) Can be a combination of the following flags (note that it doesn't make sense to use PREG_PATTERN_ORDER together with PREG_SET_ORDER):
  1549. * PREG_PATTERN_ORDER - orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on;
  1550. * PREG_SET_ORDER - orders results so that $matches[0] is an array of first set of matches, $matches[1] is an array of second set of matches, and so on;
  1551. * PREG_OFFSET_CAPTURE - If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the value of matches
  1552. * in an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
  1553. * If no order flag is given, PREG_PATTERN_ORDER is assumed.
  1554. * @param int $offset (optional) Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search.
  1555. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1556. *
  1557. * @return int|bool returns the number of full pattern matches (which might be zero), or FALSE if an error occurred
  1558. *
  1559. * @see http://php.net/preg_match_all
  1560. */
  1561. function api_preg_match_all($pattern, $subject, &$matches, $flags = PREG_PATTERN_ORDER, $offset = 0, $encoding = null)
  1562. {
  1563. if (empty($encoding)) {
  1564. $encoding = _api_mb_internal_encoding();
  1565. }
  1566. if (is_null($flags)) {
  1567. $flags = PREG_PATTERN_ORDER;
  1568. }
  1569. return preg_match_all(api_is_utf8($encoding) ? $pattern.'u' : $pattern, $subject, $matches, $flags, $offset);
  1570. }
  1571. /**
  1572. * Performs a regular expression search and replace, UTF-8 aware when it is applicable.
  1573. *
  1574. * @param string|array $pattern The pattern to search for. It can be either a string or an array with strings.
  1575. * @param string|array $replacement the string or an array with strings to replace
  1576. * @param string|array $subject the string or an array with strings to search and replace
  1577. * @param int $limit The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).
  1578. * @param int &$count If specified, this variable will be filled with the number of replacements done
  1579. * @param string $encoding (optional) The used internally by this function character encoding.
  1580. * If it is omitted, the platform character set will be used by default.
  1581. *
  1582. * @return array|string|null returns an array if the subject parameter is an array, or a string otherwise.
  1583. * If matches are found, the new subject will be returned, otherwise subject will be returned unchanged or NULL if an error occurred.
  1584. *
  1585. * @see http://php.net/preg_replace
  1586. */
  1587. function api_preg_replace($pattern, $replacement, $subject, $limit = -1, &$count = 0, $encoding = null)
  1588. {
  1589. if (empty($encoding)) {
  1590. $encoding = _api_mb_internal_encoding();
  1591. }
  1592. $is_utf8 = api_is_utf8($encoding);
  1593. if (is_array($pattern)) {
  1594. foreach ($pattern as &$p) {
  1595. $p = $is_utf8 ? $p.'u' : $p;
  1596. }
  1597. } else {
  1598. $pattern = $is_utf8 ? $pattern.'u' : $pattern;
  1599. }
  1600. return preg_replace($pattern, $replacement, $subject, $limit, $count);
  1601. }
  1602. /**
  1603. * Splits a string by a regular expression, UTF-8 aware when it is applicable.
  1604. *
  1605. * @param string $pattern the pattern to search for, as a string
  1606. * @param string $subject the input string
  1607. * @param int $limit (optional) If specified, then only substrings up to $limit are returned with the rest of the string being placed in the last substring. A limit of -1, 0 or null means "no limit" and, as is standard across PHP.
  1608. * @param int $flags (optional) $flags can be any combination of the following flags (combined with bitwise | operator):
  1609. * PREG_SPLIT_NO_EMPTY - if this flag is set, only non-empty pieces will be returned;
  1610. * PREG_SPLIT_DELIM_CAPTURE - if this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well;
  1611. * PREG_SPLIT_OFFSET_CAPTURE - If this flag is set, for every occurring match the appendant string offset will also be returned.
  1612. * Note that this changes the return value in an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
  1613. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1614. *
  1615. * @return array returns an array containing substrings of $subject split along boundaries matched by $pattern
  1616. *
  1617. * @see http://php.net/preg_split
  1618. */
  1619. function api_preg_split($pattern, $subject, $limit = -1, $flags = 0, $encoding = null)
  1620. {
  1621. if (empty($encoding)) {
  1622. $encoding = _api_mb_internal_encoding();
  1623. }
  1624. return preg_split(api_is_utf8($encoding) ? $pattern.'u' : $pattern, $subject, $limit, $flags);
  1625. }
  1626. /**
  1627. * String comparison.
  1628. */
  1629. /**
  1630. * Performs string comparison, case insensitive, language sensitive, with extended multibyte support.
  1631. *
  1632. * @param string $string1 the first string
  1633. * @param string $string2 the second string
  1634. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1635. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1636. *
  1637. * @return int Returns < 0 if $string1 is less than $string2; > 0 if $string1 is greater than $string2; and 0 if the strings are equal.
  1638. * This function is aimed at replacing the function strcasecmp() for human-language strings.
  1639. *
  1640. * @see http://php.net/manual/en/function.strcasecmp
  1641. */
  1642. function api_strcasecmp($string1, $string2, $language = null, $encoding = null)
  1643. {
  1644. return api_strcmp(api_strtolower($string1, $encoding), api_strtolower($string2, $encoding), $language, $encoding);
  1645. }
  1646. /**
  1647. * Performs string comparison, case sensitive, language sensitive, with extended multibyte support.
  1648. *
  1649. * @param string $string1 the first string
  1650. * @param string $string2 the second string
  1651. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1652. * @param string $encoding (optional) The used internally by this function character encoding.
  1653. * If it is omitted, the platform character set will be used by default.
  1654. *
  1655. * @return int Returns < 0 if $string1 is less than $string2; > 0 if $string1 is greater than $string2; and 0 if the strings are equal.
  1656. * This function is aimed at replacing the function strcmp() for human-language strings.
  1657. *
  1658. * @see http://php.net/manual/en/function.strcmp.php
  1659. * @see http://php.net/manual/en/collator.compare.php
  1660. */
  1661. function api_strcmp($string1, $string2, $language = null, $encoding = null)
  1662. {
  1663. return strcmp($string1, $string2);
  1664. }
  1665. /**
  1666. * Performs string comparison in so called "natural order", case sensitive, language sensitive, with extended multibyte support.
  1667. *
  1668. * @param string $string1 the first string
  1669. * @param string $string2 the second string
  1670. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1671. * @param string $encoding (optional) The used internally by this function character encoding.
  1672. * If it is omitted, the platform character set will be used by default.
  1673. *
  1674. * @return int Returns < 0 if $string1 is less than $string2; > 0 if $string1 is greater than $string2; and 0 if the strings are equal.
  1675. * This function is aimed at replacing the function strnatcmp() for human-language strings.
  1676. *
  1677. * @see http://php.net/manual/en/function.strnatcmp.php
  1678. * @see http://php.net/manual/en/collator.compare.php
  1679. */
  1680. function api_strnatcmp($string1, $string2, $language = null, $encoding = null)
  1681. {
  1682. return strnatcmp($string1, $string2);
  1683. }
  1684. /**
  1685. * Sorting arrays.
  1686. */
  1687. /**
  1688. * Sorts an array using natural order algorithm.
  1689. *
  1690. * @param array $array the input array
  1691. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1692. * @param string $encoding (optional) The used internally by this function character encoding.
  1693. * If it is omitted, the platform character set will be used by default.
  1694. *
  1695. * @return bool Returns TRUE on success, FALSE on error.
  1696. * This function is aimed at replacing the function natsort() for sorting human-language strings.
  1697. *
  1698. * @see http://php.net/manual/en/function.natsort.php
  1699. */
  1700. function api_natsort(&$array, $language = null, $encoding = null)
  1701. {
  1702. return natsort($array);
  1703. }
  1704. /**
  1705. * Sorts an array using natural order algorithm in reverse order.
  1706. *
  1707. * @param array $array the input array
  1708. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1709. * @param string $encoding (optional) The used internally by this function character encoding.
  1710. * If it is omitted, the platform character set will be used by default.
  1711. *
  1712. * @return bool returns TRUE on success, FALSE on error
  1713. */
  1714. function api_natrsort(&$array, $language = null, $encoding = null)
  1715. {
  1716. return uasort($array, '_api_strnatrcmp');
  1717. }
  1718. /**
  1719. * Encoding management functions.
  1720. */
  1721. /**
  1722. * This function unifies the encoding identificators, so they could be compared.
  1723. *
  1724. * @param string|array $encoding the specified encoding
  1725. *
  1726. * @return string returns the encoding identificator modified in suitable for comparison way
  1727. */
  1728. function api_refine_encoding_id($encoding)
  1729. {
  1730. if (is_array($encoding)) {
  1731. return array_map('api_refine_encoding_id', $encoding);
  1732. }
  1733. return strtoupper(str_replace('_', '-', $encoding));
  1734. }
  1735. /**
  1736. * This function checks whether two $encoding are equal (same, equvalent).
  1737. *
  1738. * @param string|array $encoding1 The first encoding
  1739. * @param string|array $encoding2 The second encoding
  1740. * @param bool $strict When this parameter is TRUE the comparison ignores aliases of encodings.
  1741. * When the parameter is FALSE, aliases are taken into account.
  1742. *
  1743. * @return bool returns TRUE if the encodings are equal, FALSE otherwise
  1744. */
  1745. function api_equal_encodings($encoding1, $encoding2, $strict = false)
  1746. {
  1747. static $equal_encodings = [];
  1748. if (is_array($encoding1)) {
  1749. foreach ($encoding1 as $encoding) {
  1750. if (api_equal_encodings($encoding, $encoding2, $strict)) {
  1751. return true;
  1752. }
  1753. }
  1754. return false;
  1755. } elseif (is_array($encoding2)) {
  1756. foreach ($encoding2 as $encoding) {
  1757. if (api_equal_encodings($encoding1, $encoding, $strict)) {
  1758. return true;
  1759. }
  1760. }
  1761. return false;
  1762. }
  1763. if (!isset($equal_encodings[$encoding1][$encoding2][$strict])) {
  1764. $encoding_1 = api_refine_encoding_id($encoding1);
  1765. $encoding_2 = api_refine_encoding_id($encoding2);
  1766. if ($encoding_1 == $encoding_2) {
  1767. $result = true;
  1768. } else {
  1769. if ($strict) {
  1770. $result = false;
  1771. } else {
  1772. $alias1 = _api_get_character_map_name($encoding_1);
  1773. $alias2 = _api_get_character_map_name($encoding_2);
  1774. $result = !empty($alias1) && !empty($alias2) && $alias1 == $alias2;
  1775. }
  1776. }
  1777. $equal_encodings[$encoding1][$encoding2][$strict] = $result;
  1778. }
  1779. return $equal_encodings[$encoding1][$encoding2][$strict];
  1780. }
  1781. /**
  1782. * This function checks whether a given encoding is UTF-8.
  1783. *
  1784. * @param string $encoding the tested encoding
  1785. *
  1786. * @return bool returns TRUE if the given encoding id means UTF-8, otherwise returns false
  1787. */
  1788. function api_is_utf8($encoding)
  1789. {
  1790. static $result = [];
  1791. if (!isset($result[$encoding])) {
  1792. $result[$encoding] = api_equal_encodings($encoding, 'UTF-8');
  1793. }
  1794. return $result[$encoding];
  1795. }
  1796. /**
  1797. * This function returns the encoding, currently used by the system.
  1798. *
  1799. * @return string The system's encoding.
  1800. * Note: The value of api_get_setting('platform_charset') is tried to be returned first,
  1801. * on the second place the global variable $charset is tried to be returned. If for some
  1802. * reason both attempts fail, then the libraly's internal value will be returned.
  1803. */
  1804. function api_get_system_encoding()
  1805. {
  1806. return 'UTF-8';
  1807. }
  1808. /**
  1809. * Checks whether a specified encoding is supported by this API.
  1810. *
  1811. * @param string $encoding the specified encoding
  1812. *
  1813. * @return bool returns TRUE when the specified encoding is supported, FALSE othewise
  1814. */
  1815. function api_is_encoding_supported($encoding)
  1816. {
  1817. static $supported = [];
  1818. if (!isset($supported[$encoding])) {
  1819. $supported[$encoding] = _api_mb_supports($encoding) || _api_iconv_supports($encoding) || _api_convert_encoding_supports($encoding);
  1820. }
  1821. return $supported[$encoding];
  1822. }
  1823. /**
  1824. * Detects encoding of plain text.
  1825. *
  1826. * @param string $string the input text
  1827. * @param string $language (optional) The language of the input text, provided if it is known
  1828. *
  1829. * @return string returns the detected encoding
  1830. */
  1831. function api_detect_encoding($string, $language = null)
  1832. {
  1833. // Testing against valid UTF-8 first.
  1834. if (api_is_valid_utf8($string)) {
  1835. return 'UTF-8';
  1836. }
  1837. return mb_detect_encoding($string);
  1838. }
  1839. /**
  1840. * String validation functions concerning certain encodings.
  1841. */
  1842. /**
  1843. * Checks a string for UTF-8 validity.
  1844. *
  1845. * @param string $string
  1846. *
  1847. * @return string
  1848. */
  1849. function api_is_valid_utf8($string)
  1850. {
  1851. return Utf8::isUtf8($string);
  1852. }
  1853. /**
  1854. * Checks whether a string contains 7-bit ASCII characters only.
  1855. *
  1856. * @param string $string the string to be tested/validated
  1857. *
  1858. * @return bool returns TRUE when the tested string contains 7-bit
  1859. * ASCII characters only, FALSE othewise
  1860. */
  1861. function api_is_valid_ascii(&$string)
  1862. {
  1863. return mb_detect_encoding($string, 'ASCII', true) == 'ASCII' ? true : false;
  1864. }
  1865. /**
  1866. * Return true a date is valid.
  1867. *
  1868. * @param string $date example: 2014-06-30 13:05:05
  1869. * @param string $format example: "Y-m-d H:i:s"
  1870. *
  1871. * @return bool
  1872. */
  1873. function api_is_valid_date($date, $format = 'Y-m-d H:i:s')
  1874. {
  1875. $d = DateTime::createFromFormat($format, $date);
  1876. return $d && $d->format($format) == $date;
  1877. }
  1878. /**
  1879. * Returns the variable translated.
  1880. *
  1881. * @param string $variable the string to translate
  1882. * @param string $pluginName the Plugin name
  1883. *
  1884. * @return string the variable translated
  1885. */
  1886. function get_plugin_lang($variable, $pluginName)
  1887. {
  1888. $plugin = $pluginName::create();
  1889. return $plugin->get_lang($variable);
  1890. }
  1891. /**
  1892. * Returns an array of translated week days and months, short and normal names.
  1893. *
  1894. * @param string $language (optional Language id. If it is omitted,
  1895. * the current interface language is assumed.
  1896. *
  1897. * @return array returns a multidimensional array with translated week days and months
  1898. */
  1899. function &_api_get_day_month_names($language = null)
  1900. {
  1901. static $date_parts = [];
  1902. if (empty($language)) {
  1903. $language = api_get_interface_language();
  1904. }
  1905. if (!isset($date_parts[$language])) {
  1906. $week_day = [
  1907. 'Sunday',
  1908. 'Monday',
  1909. 'Tuesday',
  1910. 'Wednesday',
  1911. 'Thursday',
  1912. 'Friday',
  1913. 'Saturday',
  1914. ];
  1915. $month = [
  1916. 'January',
  1917. 'February',
  1918. 'March',
  1919. 'April',
  1920. 'May',
  1921. 'June',
  1922. 'July',
  1923. 'August',
  1924. 'September',
  1925. 'October',
  1926. 'November',
  1927. 'December',
  1928. ];
  1929. for ($i = 0; $i < 7; $i++) {
  1930. $date_parts[$language]['days_short'][] = get_lang(
  1931. $week_day[$i].'Short',
  1932. '',
  1933. $language
  1934. );
  1935. $date_parts[$language]['days_long'][] = get_lang(
  1936. $week_day[$i].'Long',
  1937. '',
  1938. $language
  1939. );
  1940. }
  1941. for ($i = 0; $i < 12; $i++) {
  1942. $date_parts[$language]['months_short'][] = get_lang(
  1943. $month[$i].'Short',
  1944. '',
  1945. $language
  1946. );
  1947. $date_parts[$language]['months_long'][] = get_lang(
  1948. $month[$i].'Long',
  1949. '',
  1950. $language
  1951. );
  1952. }
  1953. }
  1954. return $date_parts[$language];
  1955. }
  1956. /**
  1957. * Returns returns person name convention for a given language.
  1958. *
  1959. * @param string $language the input language
  1960. * @param string $type The type of the requested convention.
  1961. * It may be 'format' for name order convention or 'sort_by' for name sorting convention.
  1962. *
  1963. * @return mixed Depending of the requested type,
  1964. * the returned result may be string or boolean; null is returned on error;
  1965. */
  1966. function _api_get_person_name_convention($language, $type)
  1967. {
  1968. static $conventions;
  1969. $language = api_purify_language_id($language);
  1970. if (!isset($conventions)) {
  1971. $file = __DIR__.'/internationalization_database/name_order_conventions.php';
  1972. if (file_exists($file)) {
  1973. $conventions = include $file;
  1974. } else {
  1975. $conventions = [
  1976. 'english' => [
  1977. 'format' => 'title first_name last_name',
  1978. 'sort_by' => 'first_name',
  1979. ],
  1980. ];
  1981. }
  1982. // Overwrite classic conventions
  1983. $customConventions = api_get_configuration_value('name_order_conventions');
  1984. if (!empty($customConventions)) {
  1985. foreach ($customConventions as $key => $data) {
  1986. $conventions[$key] = $data;
  1987. }
  1988. }
  1989. $search1 = ['FIRST_NAME', 'LAST_NAME', 'TITLE'];
  1990. $replacement1 = ['%F', '%L', '%T'];
  1991. $search2 = ['first_name', 'last_name', 'title'];
  1992. $replacement2 = ['%f', '%l', '%t'];
  1993. foreach (array_keys($conventions) as $key) {
  1994. $conventions[$key]['format'] = str_replace($search1, $replacement1, $conventions[$key]['format']);
  1995. $conventions[$key]['format'] = _api_validate_person_name_format(
  1996. _api_clean_person_name(
  1997. str_replace(
  1998. '%',
  1999. ' %',
  2000. str_ireplace(
  2001. $search2,
  2002. $replacement2,
  2003. $conventions[$key]['format']
  2004. )
  2005. )
  2006. )
  2007. );
  2008. $conventions[$key]['sort_by'] = strtolower($conventions[$key]['sort_by']) != 'last_name' ? true : false;
  2009. }
  2010. }
  2011. switch ($type) {
  2012. case 'format':
  2013. return is_string($conventions[$language]['format']) ? $conventions[$language]['format'] : '%t %f %l';
  2014. case 'sort_by':
  2015. return is_bool($conventions[$language]['sort_by']) ? $conventions[$language]['sort_by'] : true;
  2016. }
  2017. return null;
  2018. }
  2019. /**
  2020. * Replaces non-valid formats for person names with the default (English) format.
  2021. *
  2022. * @param string $format the input format to be verified
  2023. *
  2024. * @return bool returns the same format if is is valid, otherwise returns a valid English format
  2025. */
  2026. function _api_validate_person_name_format($format)
  2027. {
  2028. if (empty($format) || stripos($format, '%f') === false || stripos($format, '%l') === false) {
  2029. return '%t %f %l';
  2030. }
  2031. return $format;
  2032. }
  2033. /**
  2034. * Removes leading, trailing and duplicate whitespace and/or commas in a full person name.
  2035. * Cleaning is needed for the cases when not all parts of the name are available
  2036. * or when the name is constructed using a "dirty" pattern.
  2037. *
  2038. * @param string $person_name the input person name
  2039. *
  2040. * @return string returns cleaned person name
  2041. */
  2042. function _api_clean_person_name($person_name)
  2043. {
  2044. return preg_replace(['/\s+/', '/, ,/', '/,+/', '/^[ ,]/', '/[ ,]$/'], [' ', ', ', ',', '', ''], $person_name);
  2045. }
  2046. /**
  2047. * Appendix to "Multibyte string conversion functions".
  2048. */
  2049. /**
  2050. * This is a php-implementation of a function that is similar to mb_convert_encoding() from mbstring extension.
  2051. * The function converts a given string from one to another character encoding.
  2052. *
  2053. * @param string $string the string being converted
  2054. * @param string $to_encoding the encoding that $string is being converted to
  2055. * @param string $from_encoding the encoding that $string is being converted from
  2056. *
  2057. * @return string returns the converted string
  2058. */
  2059. function _api_convert_encoding(&$string, $to_encoding, $from_encoding)
  2060. {
  2061. return mb_convert_encoding($string, $to_encoding, $from_encoding);
  2062. }
  2063. /**
  2064. * This function determines the name of corresponding to a given encoding conversion table.
  2065. * It is able to deal with some aliases of the encoding.
  2066. *
  2067. * @param string $encoding the given encoding identificator, for example 'WINDOWS-1252'
  2068. *
  2069. * @return string returns the name of the corresponding conversion table, for the same example - 'CP1252'
  2070. */
  2071. function _api_get_character_map_name($encoding)
  2072. {
  2073. static $character_map_selector;
  2074. if (!isset($character_map_selector)) {
  2075. $file = __DIR__.'/internationalization_database/conversion/character_map_selector.php';
  2076. if (file_exists($file)) {
  2077. $character_map_selector = include $file;
  2078. } else {
  2079. $character_map_selector = [];
  2080. }
  2081. }
  2082. return isset($character_map_selector[$encoding]) ? $character_map_selector[$encoding] : '';
  2083. }
  2084. /**
  2085. * Appendix to "String comparison".
  2086. */
  2087. /**
  2088. * A reverse function from php-core function strnatcmp(),
  2089. * performs string comparison in reverse natural (alpha-numerical) order.
  2090. *
  2091. * @param string $string1 the first string
  2092. * @param string $string2 the second string
  2093. *
  2094. * @return int returns 0 if $string1 = $string2; >0 if $string1 < $string2; <0 if $string1 > $string2
  2095. */
  2096. function _api_strnatrcmp($string1, $string2)
  2097. {
  2098. return strnatcmp($string2, $string1);
  2099. }
  2100. /**
  2101. * Sets/Gets internal character encoding of the common string functions within the PHP mbstring extension.
  2102. *
  2103. * @param string $encoding (optional) When this parameter is given, the function sets the internal encoding
  2104. *
  2105. * @return string When $encoding parameter is not given, the function returns the internal encoding.
  2106. * Note: This function is used in the global initialization script for setting the
  2107. * internal encoding to the platform's character set.
  2108. *
  2109. * @see http://php.net/manual/en/function.mb-internal-encoding
  2110. */
  2111. function _api_mb_internal_encoding($encoding = 'UTF-8')
  2112. {
  2113. return mb_internal_encoding($encoding);
  2114. }
  2115. /**
  2116. * Checks whether the specified encoding is supported by the PHP mbstring extension.
  2117. *
  2118. * @param string $encoding the specified encoding
  2119. *
  2120. * @return bool returns TRUE when the specified encoding is supported, FALSE othewise
  2121. */
  2122. function _api_mb_supports($encoding)
  2123. {
  2124. static $supported = [];
  2125. if (!isset($supported[$encoding])) {
  2126. if (MBSTRING_INSTALLED) {
  2127. $supported[$encoding] = api_equal_encodings($encoding, mb_list_encodings(), true);
  2128. } else {
  2129. $supported[$encoding] = false;
  2130. }
  2131. }
  2132. return $supported[$encoding];
  2133. }
  2134. /**
  2135. * Checks whether the specified encoding is supported by the PHP iconv extension.
  2136. *
  2137. * @param string $encoding the specified encoding
  2138. *
  2139. * @return bool returns TRUE when the specified encoding is supported, FALSE othewise
  2140. */
  2141. function _api_iconv_supports($encoding)
  2142. {
  2143. static $supported = [];
  2144. if (!isset($supported[$encoding])) {
  2145. if (ICONV_INSTALLED) {
  2146. $enc = api_refine_encoding_id($encoding);
  2147. if ($enc != 'HTML-ENTITIES') {
  2148. $test_string = '';
  2149. for ($i = 32; $i < 128; $i++) {
  2150. $test_string .= chr($i);
  2151. }
  2152. $supported[$encoding] = (@iconv_strlen($test_string, $enc)) ? true : false;
  2153. } else {
  2154. $supported[$encoding] = false;
  2155. }
  2156. } else {
  2157. $supported[$encoding] = false;
  2158. }
  2159. }
  2160. return $supported[$encoding];
  2161. }
  2162. // This function checks whether the function _api_convert_encoding() (the php-
  2163. // implementation) is able to convert from/to a given encoding.
  2164. function _api_convert_encoding_supports($encoding)
  2165. {
  2166. static $supports = [];
  2167. if (!isset($supports[$encoding])) {
  2168. $supports[$encoding] = _api_get_character_map_name(api_refine_encoding_id($encoding)) != '';
  2169. }
  2170. return $supports[$encoding];
  2171. }
  2172. /**
  2173. * Given a date object, return a human or ISO format, with or without h:m:s.
  2174. *
  2175. * @param object $date The Date object
  2176. * @param bool $showTime Whether to show the time and date (true) or only the date (false)
  2177. * @param bool $humanForm Whether to show day-month-year (true) or year-month-day (false)
  2178. *
  2179. * @return string Formatted date
  2180. */
  2181. function api_get_human_date_time($date, $showTime = true, $humanForm = false)
  2182. {
  2183. if ($showTime) {
  2184. if ($humanForm) {
  2185. return $date->format('j M Y H:i:s');
  2186. } else {
  2187. return $date->format('Y-m-d H:i:s');
  2188. }
  2189. } else {
  2190. if ($humanForm) {
  2191. return $date->format('j M Y');
  2192. } else {
  2193. return $date->format('Y-m-d');
  2194. }
  2195. }
  2196. }
  2197. /**
  2198. * @param string $language
  2199. *
  2200. * @return array
  2201. */
  2202. function api_get_language_files_to_load($language)
  2203. {
  2204. $parent_path = SubLanguageManager::get_parent_language_path($language);
  2205. $langPath = api_get_path(SYS_LANG_PATH);
  2206. $languagesToLoad = [
  2207. $langPath.'english/trad4all.inc.php', // include English always
  2208. ];
  2209. if (!empty($parent_path)) { // if the sub-language feature is on
  2210. // prepare string for current language and its parent
  2211. $lang_file = $langPath.$language.'/trad4all.inc.php';
  2212. $parent_lang_file = $langPath.$parent_path.'/trad4all.inc.php';
  2213. // load the parent language file first
  2214. if (file_exists($parent_lang_file)) {
  2215. $languagesToLoad[] = $parent_lang_file;
  2216. }
  2217. // overwrite the parent language translations if there is a child
  2218. if (file_exists($lang_file)) {
  2219. $languagesToLoad[] = $lang_file;
  2220. }
  2221. } else {
  2222. // if the sub-languages feature is not on, then just load the
  2223. // set language interface
  2224. // prepare string for current language
  2225. $langFile = $langPath.$language.'/trad4all.inc.php';
  2226. if (file_exists($langFile)) {
  2227. $languagesToLoad[] = $langFile;
  2228. }
  2229. // Check if language/custom.php exists
  2230. $customLanguage = $langPath.$language.'/custom.php';
  2231. if (file_exists($customLanguage)) {
  2232. $languagesToLoad[] = $customLanguage;
  2233. }
  2234. }
  2235. return $languagesToLoad;
  2236. }