xref: /dokuwiki/inc/compatibility.php (revision a4c648fffdf5bc5db83c9aa9c6c4bf5fb5edc85f)
16589c60cSAndreas Gohr<?php
26589c60cSAndreas Gohr/**
36589c60cSAndreas Gohr * compatibility functions
46589c60cSAndreas Gohr *
56589c60cSAndreas Gohr * This file contains a few functions that might be missing from the PHP build
66589c60cSAndreas Gohr */
76589c60cSAndreas Gohr
86589c60cSAndreas Gohrif (!function_exists('ctype_space')) {
96589c60cSAndreas Gohr    /**
106589c60cSAndreas Gohr     * Check for whitespace character(s)
116589c60cSAndreas Gohr     *
126589c60cSAndreas Gohr     * @param string $text
136589c60cSAndreas Gohr     * @return bool
14*a4c648ffSAndreas Gohr     * @see ctype_space
156589c60cSAndreas Gohr     */
16d868eb89SAndreas Gohr    function ctype_space($text)
17d868eb89SAndreas Gohr    {
186589c60cSAndreas Gohr        if (!is_string($text)) return false; #FIXME original treats between -128 and 255 inclusive as ASCII chars
196589c60cSAndreas Gohr        if (trim($text) === '') return true;
206589c60cSAndreas Gohr        return false;
216589c60cSAndreas Gohr    }
226589c60cSAndreas Gohr}
236589c60cSAndreas Gohr
246589c60cSAndreas Gohrif (!function_exists('ctype_digit')) {
256589c60cSAndreas Gohr    /**
266589c60cSAndreas Gohr     * Check for numeric character(s)
276589c60cSAndreas Gohr     *
286589c60cSAndreas Gohr     * @param string $text
296589c60cSAndreas Gohr     * @return bool
30*a4c648ffSAndreas Gohr     * @see ctype_digit
316589c60cSAndreas Gohr     */
32d868eb89SAndreas Gohr    function ctype_digit($text)
33d868eb89SAndreas Gohr    {
346589c60cSAndreas Gohr        if (!is_string($text)) return false; #FIXME original treats between -128 and 255 inclusive as ASCII chars
356589c60cSAndreas Gohr        if (preg_match('/^\d+$/', $text)) return true;
366589c60cSAndreas Gohr        return false;
376589c60cSAndreas Gohr    }
386589c60cSAndreas Gohr}
3992b9f196SAndreas Gohr
4092b9f196SAndreas Gohrif (!function_exists('gzopen') && function_exists('gzopen64')) {
4192b9f196SAndreas Gohr    /**
4292b9f196SAndreas Gohr     * work around for PHP compiled against certain zlib versions #865
4392b9f196SAndreas Gohr     *
4492b9f196SAndreas Gohr     * @link http://stackoverflow.com/questions/23417519/php-zlib-gzopen-not-exists
4592b9f196SAndreas Gohr     *
4692b9f196SAndreas Gohr     * @param string $filename
4792b9f196SAndreas Gohr     * @param string $mode
4892b9f196SAndreas Gohr     * @param int $use_include_path
4992b9f196SAndreas Gohr     * @return mixed
5092b9f196SAndreas Gohr     */
51d868eb89SAndreas Gohr    function gzopen($filename, $mode, $use_include_path = 0)
52d868eb89SAndreas Gohr    {
5392b9f196SAndreas Gohr        return gzopen64($filename, $mode, $use_include_path);
548457f8ceSAndreas Gohr    }
558457f8ceSAndreas Gohr}
568457f8ceSAndreas Gohr
578457f8ceSAndreas Gohrif (!function_exists('gzseek') && function_exists('gzseek64')) {
588457f8ceSAndreas Gohr    /**
598457f8ceSAndreas Gohr     * work around for PHP compiled against certain zlib versions #865
608457f8ceSAndreas Gohr     *
618457f8ceSAndreas Gohr     * @link http://stackoverflow.com/questions/23417519/php-zlib-gzopen-not-exists
628457f8ceSAndreas Gohr     *
638457f8ceSAndreas Gohr     * @param resource $zp
648457f8ceSAndreas Gohr     * @param int $offset
658457f8ceSAndreas Gohr     * @param int $whence
668457f8ceSAndreas Gohr     * @return int
678457f8ceSAndreas Gohr     */
68d868eb89SAndreas Gohr    function gzseek($zp, $offset, $whence = SEEK_SET)
69d868eb89SAndreas Gohr    {
708457f8ceSAndreas Gohr        return gzseek64($zp, $offset, $whence);
718457f8ceSAndreas Gohr    }
728457f8ceSAndreas Gohr}
738457f8ceSAndreas Gohr
748457f8ceSAndreas Gohrif (!function_exists('gztell') && function_exists('gztell64')) {
758457f8ceSAndreas Gohr    /**
768457f8ceSAndreas Gohr     * work around for PHP compiled against certain zlib versions #865
778457f8ceSAndreas Gohr     *
788457f8ceSAndreas Gohr     * @link   http://stackoverflow.com/questions/23417519/php-zlib-gzopen-not-exists
798457f8ceSAndreas Gohr     *
808457f8ceSAndreas Gohr     * @param resource $zp
818457f8ceSAndreas Gohr     * @return int
828457f8ceSAndreas Gohr     */
83d868eb89SAndreas Gohr    function gztell($zp)
84d868eb89SAndreas Gohr    {
858457f8ceSAndreas Gohr        return gztell64($zp);
8692b9f196SAndreas Gohr    }
8792b9f196SAndreas Gohr}
88*a4c648ffSAndreas Gohr
89*a4c648ffSAndreas Gohr/**
90*a4c648ffSAndreas Gohr * polyfill for PHP < 8
91*a4c648ffSAndreas Gohr * @see https://www.php.net/manual/en/function.str-starts-with
92*a4c648ffSAndreas Gohr */
93*a4c648ffSAndreas Gohrif (!function_exists('str_starts_with')) {
94*a4c648ffSAndreas Gohr    function str_starts_with(string $haystack, string $needle)
95*a4c648ffSAndreas Gohr    {
96*a4c648ffSAndreas Gohr        return empty($needle) || strpos($haystack, $needle) === 0;
97*a4c648ffSAndreas Gohr    }
98*a4c648ffSAndreas Gohr}
99*a4c648ffSAndreas Gohr
100*a4c648ffSAndreas Gohr/**
101*a4c648ffSAndreas Gohr * polyfill for PHP < 8
102*a4c648ffSAndreas Gohr * @see https://www.php.net/manual/en/function.str-contains
103*a4c648ffSAndreas Gohr */
104*a4c648ffSAndreas Gohrif (!function_exists('str_contains')) {
105*a4c648ffSAndreas Gohr    function str_contains(string $haystack, string $needle)
106*a4c648ffSAndreas Gohr    {
107*a4c648ffSAndreas Gohr        return empty($needle) || strpos($haystack, $needle) !== false;
108*a4c648ffSAndreas Gohr    }
109*a4c648ffSAndreas Gohr}
110*a4c648ffSAndreas Gohr
111*a4c648ffSAndreas Gohr/**
112*a4c648ffSAndreas Gohr * polyfill for PHP < 8
113*a4c648ffSAndreas Gohr * @see https://www.php.net/manual/en/function.str-ends-with
114*a4c648ffSAndreas Gohr */
115*a4c648ffSAndreas Gohrif (!function_exists('str_ends_with')) {
116*a4c648ffSAndreas Gohr    function str_ends_with(string $haystack, string $needle)
117*a4c648ffSAndreas Gohr    {
118*a4c648ffSAndreas Gohr        return empty($needle) || substr($haystack, -strlen($needle)) === $needle;
119*a4c648ffSAndreas Gohr    }
120*a4c648ffSAndreas Gohr}
121