Compatibility Class for Deprecated / Broken PHP Functions

This class provides some compatibility for when PHP removes functions but code you need to run uses those functions.

Also, in some cases the way that PHP functions work is, well, broken. Alternatives that are not quite as broken or not broken at all are also in this class.


Namespace: \AWonderPHP
Fully Qualified Class Name: \AWonderPHP\Compat

This class only contains static methods, so their is no need to instantiate it.

While it is somewhat rare, sometimes PHP core removes a function. This is never done lightly, plugin and theme developers should update their code when it happens. But when it happens, what do you do as a blogmaster if a plugin or theme you use that has been abandoned by it’s developer uses one of those functions? The solution is to define an API compatible function of the same name in a plugin in your mu-plugins directory. This class helps you do that. For example:

if (! function_exists('gmp_random')) :
    function gmp_random(int $limiter) {
        return \AWonderPHP\Compat::gmpRandom($limiter);

Plugins and themes that call that function will now have an API compatible equivalent.

public static function uniqid(string $prefix = ”, bool $more_entropy = false): string

This is an API compatible input/output format compatible alternative to the PHP uniqid function.

Technically speaking that PHP function is not yet marked as deprecated or slated for removal, but it should be, because it is very broken.

Like the core PHP function, this function only creates a unique id. It does not have enough randomness to be used for cryptographic purposes. Unlike the core PHP function, collisions are far far far less likely to happen with the function in this class. They are still possible, but far less likely to happen.

public static function cryptoUniqid(string $prefix = ”, bool $prng = false, int $bytes = 16): string

This method is also an alternative to the PHP uniqid function, but the output format differs.

With this implementation, collisions never happen. However the output string in much longer than with the PHP uniqid function.

This version of the function only uses a regular UNIX timestamp for the first part of the unique id but the second part uses a base64 encoded number that defaults to 16 bytes in length but can be specified by the third parameter, with 12 bytes being the minimum.

If the second argument is false, a pRNG is only used the first time the function is called in a script, subsequent calls increment the random number in order to guarantee they are unique. If the second argument is true, it will always use a pRNG regardless of whether the function has already been called.

If you need a unique id that includes a machine readable timestamp for when it was created, this is the proper way to do it.

public static function bitmap2wbmp(string $bitmapname, string $wbmpname, int $dest_height = 0, int $dest_width = 0, $threshold = 0)

This method is a generic method for converting bitmap images to the WBMP format. This method can be used to replace the PHP png2wbmp and jpeg2wbmp functions, which will be removed in PHP 8.

The path to the bitmap file to be converted.
The output WBMP file to create.
The height in pixels of the destination WBMP file to create.
The width in pixels of the destination WBMP file to create.
Currently ignored until the gd image library supports it.

This is not 100% compatible with the deprecated methods as it ignores the $threshold parameter, though my understanding is that never actually worked anyway. If and when the gd library is updated to support threshold, then this method will be updated as well.

Some people will tell you this is pointless, that no one uses WBMP. They are wrong. It is sometimes used by people in developing countries where bandwidth is very restricted.

public static function gmpRandom(int $limiter, int $pseudolimb = 64)

This method replaces the deprecated gmp_random function, which I believe will be removed in PHP 7.3.

There are two major issues with the deprecated PHP function. First, the upper limit is not stable. It uses a GMP property called the limb size that changes from machine to machine. On some machines it is 32, on others it is 64. I believe it depends on the processor but it may also depend upon other factors. I have heard that it is 16 on some hardware but I have not seen that myself.

The second issue, the documentation states that if you feed it a negative $limiter it will return a negative number, but it doesn’t.

What this implementation does, it uses gmp_random_bits to generate the random number and gives the identical range of results that you would get using gmp_random if your implementation of GMP has a limb size of 64 (usually the case on Xeon powered servers). This implementation also will return a negative if the $limiter is negative.

If you want to use your native GMP limb size instead of 64 and you know your native GMP limb size, you can specify that as the second argument. I believe it is much faster when using the native limb size if your native limb size is smaller than 64 but I am not positive.

Anonymity protected with AWM Pluggable Unplugged