Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 1... Línea 1...
1
<?php
1
<?php
-
 
2
declare(strict_types=1);
2
namespace ParagonIE\ConstantTime;
3
namespace ParagonIE\ConstantTime;
Línea -... Línea 4...
-
 
4
 
-
 
5
use TypeError;
3
 
6
 
4
/**
7
/**
5
 *  Copyright (c) 2016 - 2017 Paragon Initiative Enterprises.
8
 *  Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
6
 *  Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
9
 *  Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
7
 *
10
 *
8
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
11
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
9
 *  of this software and associated documentation files (the "Software"), to deal
12
 *  of this software and associated documentation files (the "Software"), to deal
Línea 40... Línea 43...
40
     * @ref mbstring.func_overload
43
     * @ref mbstring.func_overload
41
     *
44
     *
42
     * @param string $str
45
     * @param string $str
43
     * @return int
46
     * @return int
44
     */
47
     */
45
    public static function safeStrlen($str)
48
    public static function safeStrlen(
-
 
49
        #[\SensitiveParameter]
-
 
50
        string $str
46
    {
51
    ): int {
47
        if (\function_exists('mb_strlen')) {
52
        if (\function_exists('mb_strlen')) {
-
 
53
            // mb_strlen in PHP 7.x can return false.
-
 
54
            /** @psalm-suppress RedundantCast */
48
            return (int) \mb_strlen($str, '8bit');
55
            return (int) \mb_strlen($str, '8bit');
49
        } else {
56
        } else {
50
            return (int) \strlen($str);
57
            return \strlen($str);
51
        }
58
        }
52
    }
59
    }
Línea 53... Línea 60...
53
 
60
 
54
    /**
61
    /**
Línea 57... Línea 64...
57
     * @ref mbstring.func_overload
64
     * @ref mbstring.func_overload
58
     *
65
     *
59
     * @staticvar boolean $exists
66
     * @staticvar boolean $exists
60
     * @param string $str
67
     * @param string $str
61
     * @param int $start
68
     * @param int $start
62
     * @param int $length
69
     * @param ?int $length
63
     * @return string
70
     * @return string
-
 
71
     *
64
     * @throws \TypeError
72
     * @throws TypeError
65
     */
73
     */
66
    public static function safeSubstr(
74
    public static function safeSubstr(
67
        $str,
75
        #[\SensitiveParameter]
68
        $start = 0,
76
        string $str,
69
        $length = \null
-
 
70
    ) {
-
 
71
        if (\function_exists('mb_substr')) {
-
 
72
            // mb_substr($str, 0, null, '8bit') returns an empty string on PHP
-
 
73
            // 5.3, so we have to find the length ourselves.
-
 
74
            if (\is_null($length)) {
-
 
75
                if ($start >= 0) {
77
        int $start = 0,
76
                    $length = self::safeStrlen($str) - $start;
-
 
77
                } else {
-
 
78
                    $length = -$start;
-
 
79
                }
-
 
80
            }
-
 
81
            // $length calculation above might result in a 0-length string
-
 
82
            if ($length === 0) {
78
        ?int $length = null
83
                return '';
-
 
84
            }
79
    ): string {
85
            return \mb_substr($str, $start, $length, '8bit');
-
 
86
        }
-
 
87
        if ($length === 0) {
80
        if ($length === 0) {
88
            return '';
81
            return '';
89
        }
82
        }
-
 
83
        if (\function_exists('mb_substr')) {
-
 
84
            return \mb_substr($str, $start, $length, '8bit');
-
 
85
        }
90
        // Unlike mb_substr(), substr() doesn't accept null for length
86
        // Unlike mb_substr(), substr() doesn't accept NULL for length
91
        if (!is_null($length)) {
87
        if ($length !== null) {
92
            return \substr($str, $start, $length);
88
            return \substr($str, $start, $length);
93
        } else {
89
        } else {
94
            return \substr($str, $start);
90
            return \substr($str, $start);
95
        }
91
        }
96
    }
92
    }
97
}
-
 
98
93
}
-
 
94