xref: /dokuwiki/inc/compatibility.php (revision 7bb40883ca42723ee0037eb3f976ea5c6c4793ae)
16589c60cSAndreas Gohr<?php
2d4f83172SAndreas Gohr
36589c60cSAndreas Gohr/**
46589c60cSAndreas Gohr * compatibility functions
56589c60cSAndreas Gohr *
66589c60cSAndreas Gohr * This file contains a few functions that might be missing from the PHP build
76589c60cSAndreas Gohr */
86589c60cSAndreas Gohr
96589c60cSAndreas Gohrif (!function_exists('ctype_space')) {
106589c60cSAndreas Gohr    /**
116589c60cSAndreas Gohr     * Check for whitespace character(s)
126589c60cSAndreas Gohr     *
136589c60cSAndreas Gohr     * @param string $text
146589c60cSAndreas Gohr     * @return bool
15a4c648ffSAndreas Gohr     * @see ctype_space
166589c60cSAndreas Gohr     */
17d868eb89SAndreas Gohr    function ctype_space($text)
18d868eb89SAndreas Gohr    {
196589c60cSAndreas Gohr        if (!is_string($text)) return false; #FIXME original treats between -128 and 255 inclusive as ASCII chars
206589c60cSAndreas Gohr        if (trim($text) === '') return true;
216589c60cSAndreas Gohr        return false;
226589c60cSAndreas Gohr    }
236589c60cSAndreas Gohr}
246589c60cSAndreas Gohr
256589c60cSAndreas Gohrif (!function_exists('ctype_digit')) {
266589c60cSAndreas Gohr    /**
276589c60cSAndreas Gohr     * Check for numeric character(s)
286589c60cSAndreas Gohr     *
296589c60cSAndreas Gohr     * @param string $text
306589c60cSAndreas Gohr     * @return bool
31a4c648ffSAndreas Gohr     * @see ctype_digit
326589c60cSAndreas Gohr     */
33d868eb89SAndreas Gohr    function ctype_digit($text)
34d868eb89SAndreas Gohr    {
356589c60cSAndreas Gohr        if (!is_string($text)) return false; #FIXME original treats between -128 and 255 inclusive as ASCII chars
366589c60cSAndreas Gohr        if (preg_match('/^\d+$/', $text)) return true;
376589c60cSAndreas Gohr        return false;
386589c60cSAndreas Gohr    }
396589c60cSAndreas Gohr}
4092b9f196SAndreas Gohr
4192b9f196SAndreas Gohrif (!function_exists('gzopen') && function_exists('gzopen64')) {
4292b9f196SAndreas Gohr    /**
4392b9f196SAndreas Gohr     * work around for PHP compiled against certain zlib versions #865
4492b9f196SAndreas Gohr     *
4592b9f196SAndreas Gohr     * @link http://stackoverflow.com/questions/23417519/php-zlib-gzopen-not-exists
4692b9f196SAndreas Gohr     *
4792b9f196SAndreas Gohr     * @param string $filename
4892b9f196SAndreas Gohr     * @param string $mode
4992b9f196SAndreas Gohr     * @param int $use_include_path
5092b9f196SAndreas Gohr     * @return mixed
5192b9f196SAndreas Gohr     */
52d868eb89SAndreas Gohr    function gzopen($filename, $mode, $use_include_path = 0)
53d868eb89SAndreas Gohr    {
5492b9f196SAndreas Gohr        return gzopen64($filename, $mode, $use_include_path);
558457f8ceSAndreas Gohr    }
568457f8ceSAndreas Gohr}
578457f8ceSAndreas Gohr
588457f8ceSAndreas Gohrif (!function_exists('gzseek') && function_exists('gzseek64')) {
598457f8ceSAndreas Gohr    /**
608457f8ceSAndreas Gohr     * work around for PHP compiled against certain zlib versions #865
618457f8ceSAndreas Gohr     *
628457f8ceSAndreas Gohr     * @link http://stackoverflow.com/questions/23417519/php-zlib-gzopen-not-exists
638457f8ceSAndreas Gohr     *
648457f8ceSAndreas Gohr     * @param resource $zp
658457f8ceSAndreas Gohr     * @param int $offset
668457f8ceSAndreas Gohr     * @param int $whence
678457f8ceSAndreas Gohr     * @return int
688457f8ceSAndreas Gohr     */
69d868eb89SAndreas Gohr    function gzseek($zp, $offset, $whence = SEEK_SET)
70d868eb89SAndreas Gohr    {
718457f8ceSAndreas Gohr        return gzseek64($zp, $offset, $whence);
728457f8ceSAndreas Gohr    }
738457f8ceSAndreas Gohr}
748457f8ceSAndreas Gohr
758457f8ceSAndreas Gohrif (!function_exists('gztell') && function_exists('gztell64')) {
768457f8ceSAndreas Gohr    /**
778457f8ceSAndreas Gohr     * work around for PHP compiled against certain zlib versions #865
788457f8ceSAndreas Gohr     *
798457f8ceSAndreas Gohr     * @link   http://stackoverflow.com/questions/23417519/php-zlib-gzopen-not-exists
808457f8ceSAndreas Gohr     *
818457f8ceSAndreas Gohr     * @param resource $zp
828457f8ceSAndreas Gohr     * @return int
838457f8ceSAndreas Gohr     */
84d868eb89SAndreas Gohr    function gztell($zp)
85d868eb89SAndreas Gohr    {
868457f8ceSAndreas Gohr        return gztell64($zp);
8792b9f196SAndreas Gohr    }
8892b9f196SAndreas Gohr}
89a4c648ffSAndreas Gohr
90a4c648ffSAndreas Gohr/**
91a4c648ffSAndreas Gohr * polyfill for PHP < 8
92a4c648ffSAndreas Gohr * @see https://www.php.net/manual/en/function.str-starts-with
93a4c648ffSAndreas Gohr */
94a4c648ffSAndreas Gohrif (!function_exists('str_starts_with')) {
95*7bb40883SAndreas Gohr    function str_starts_with(?string $haystack, ?string $needle)
96a4c648ffSAndreas Gohr    {
97*7bb40883SAndreas Gohr        return 0 === strncmp($haystack, $needle, \strlen($needle));
98a4c648ffSAndreas Gohr    }
99a4c648ffSAndreas Gohr}
100a4c648ffSAndreas Gohr
101a4c648ffSAndreas Gohr/**
102a4c648ffSAndreas Gohr * polyfill for PHP < 8
103a4c648ffSAndreas Gohr * @see https://www.php.net/manual/en/function.str-contains
104a4c648ffSAndreas Gohr */
105a4c648ffSAndreas Gohrif (!function_exists('str_contains')) {
106*7bb40883SAndreas Gohr    function str_contains(?string $haystack, ?string $needle)
107a4c648ffSAndreas Gohr    {
108*7bb40883SAndreas Gohr        return '' === $needle || false !== strpos($haystack, $needle);
109a4c648ffSAndreas Gohr    }
110a4c648ffSAndreas Gohr}
111a4c648ffSAndreas Gohr
112a4c648ffSAndreas Gohr/**
113a4c648ffSAndreas Gohr * polyfill for PHP < 8
114a4c648ffSAndreas Gohr * @see https://www.php.net/manual/en/function.str-ends-with
115a4c648ffSAndreas Gohr */
116a4c648ffSAndreas Gohrif (!function_exists('str_ends_with')) {
117*7bb40883SAndreas Gohr    function str_ends_with(?string $haystack, ?string $needle)
118a4c648ffSAndreas Gohr    {
119*7bb40883SAndreas Gohr        if ('' === $needle || $needle === $haystack) {
120*7bb40883SAndreas Gohr            return true;
121*7bb40883SAndreas Gohr        }
122*7bb40883SAndreas Gohr
123*7bb40883SAndreas Gohr        if ('' === $haystack) {
124*7bb40883SAndreas Gohr            return false;
125*7bb40883SAndreas Gohr        }
126*7bb40883SAndreas Gohr
127*7bb40883SAndreas Gohr        $needleLength = \strlen($needle);
128*7bb40883SAndreas Gohr
129*7bb40883SAndreas Gohr        return $needleLength <= \strlen($haystack) && 0 === substr_compare($haystack, $needle, -$needleLength);
130a4c648ffSAndreas Gohr    }
131a4c648ffSAndreas Gohr}
13236d03388SAndreas Gohr
13336d03388SAndreas Gohr/**
13436d03388SAndreas Gohr * polyfill for PHP < 8.1
13536d03388SAndreas Gohr * @see https://www.php.net/manual/en/function.array-is-list
13636d03388SAndreas Gohr */
13736d03388SAndreas Gohrif (!function_exists('array_is_list')) {
13836d03388SAndreas Gohr    function array_is_list(array $arr)
13936d03388SAndreas Gohr    {
14036d03388SAndreas Gohr        if ($arr === []) {
14136d03388SAndreas Gohr            return true;
14236d03388SAndreas Gohr        }
14336d03388SAndreas Gohr        return array_keys($arr) === range(0, count($arr) - 1);
14436d03388SAndreas Gohr    }
14536d03388SAndreas Gohr}
146