xref: /dokuwiki/inc/compatibility.php (revision 4b230b99ca279d0f160ebd0981007d2de6120995)
1<?php
2/**
3 * compatibility functions
4 *
5 * This file contains a few functions that might be missing from the PHP build
6 */
7
8if (!function_exists('ctype_space')) {
9    /**
10     * Check for whitespace character(s)
11     *
12     * @param string $text
13     * @return bool
14     * @see ctype_space
15     */
16    function ctype_space($text)
17    {
18        if (!is_string($text)) return false; #FIXME original treats between -128 and 255 inclusive as ASCII chars
19        if (trim($text) === '') return true;
20        return false;
21    }
22}
23
24if (!function_exists('ctype_digit')) {
25    /**
26     * Check for numeric character(s)
27     *
28     * @param string $text
29     * @return bool
30     * @see ctype_digit
31     */
32    function ctype_digit($text)
33    {
34        if (!is_string($text)) return false; #FIXME original treats between -128 and 255 inclusive as ASCII chars
35        if (preg_match('/^\d+$/', $text)) return true;
36        return false;
37    }
38}
39
40if (!function_exists('gzopen') && function_exists('gzopen64')) {
41    /**
42     * work around for PHP compiled against certain zlib versions #865
43     *
44     * @link http://stackoverflow.com/questions/23417519/php-zlib-gzopen-not-exists
45     *
46     * @param string $filename
47     * @param string $mode
48     * @param int $use_include_path
49     * @return mixed
50     */
51    function gzopen($filename, $mode, $use_include_path = 0)
52    {
53        return gzopen64($filename, $mode, $use_include_path);
54    }
55}
56
57if (!function_exists('gzseek') && function_exists('gzseek64')) {
58    /**
59     * work around for PHP compiled against certain zlib versions #865
60     *
61     * @link http://stackoverflow.com/questions/23417519/php-zlib-gzopen-not-exists
62     *
63     * @param resource $zp
64     * @param int $offset
65     * @param int $whence
66     * @return int
67     */
68    function gzseek($zp, $offset, $whence = SEEK_SET)
69    {
70        return gzseek64($zp, $offset, $whence);
71    }
72}
73
74if (!function_exists('gztell') && function_exists('gztell64')) {
75    /**
76     * work around for PHP compiled against certain zlib versions #865
77     *
78     * @link   http://stackoverflow.com/questions/23417519/php-zlib-gzopen-not-exists
79     *
80     * @param resource $zp
81     * @return int
82     */
83    function gztell($zp)
84    {
85        return gztell64($zp);
86    }
87}
88
89/**
90 * polyfill for PHP < 8
91 * @see https://www.php.net/manual/en/function.str-starts-with
92 */
93if (!function_exists('str_starts_with')) {
94    function str_starts_with(string $haystack, string $needle)
95    {
96        return empty($needle) || strpos($haystack, $needle) === 0;
97    }
98}
99
100/**
101 * polyfill for PHP < 8
102 * @see https://www.php.net/manual/en/function.str-contains
103 */
104if (!function_exists('str_contains')) {
105    function str_contains(string $haystack, string $needle)
106    {
107        return empty($needle) || strpos($haystack, $needle) !== false;
108    }
109}
110
111/**
112 * polyfill for PHP < 8
113 * @see https://www.php.net/manual/en/function.str-ends-with
114 */
115if (!function_exists('str_ends_with')) {
116    function str_ends_with(string $haystack, string $needle)
117    {
118        return empty($needle) || substr($haystack, -strlen($needle)) === $needle;
119    }
120}
121