Passed
Push — master ( 6f8b91...26ffea )
by Julito
10:53
created

getIsoToLegacy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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