Bug in Perch 2.8.23 when using PHP 7.2

  • Hi. I inherited a Perch 2.8.23 implementation and while moving the site from an Ubuntu 16.04 server (PHP 7.0) to an Ubuntu 18.04 server (PHP 7.2), I found a little BUG that stopped us from logging in.


    In line 160 of the "/perch/core/lib/PerchAuthenticatedUser.class.php" file, "$data" is defined as a string and the following line it is treated as an array:

    Code: /perch/core/lib/PerchAuthenticatedUser.class.php
    1. $data = '';
    2. $data['key'] = PERCH_LICENSE_KEY;
    3. $data['host'] = $_SERVER['SERVER_NAME'];
    4. $data['version'] = $Perch->version;
    5. $data['php'] = phpversion();
    6. $content = http_build_query($data);


    Since PHP 7.1, this is no longer allowed. The solution is to change line 160 to:


    Code: /perch/core/lib/PerchAuthenticatedUser.class.php
    1. $data = [];


    Refer to https://www.php.net/manual/en/migration71.incompatible.php:


    Assignment via string index access on an empty string


    String modification by character on an empty string now works like for non-empty strings, i.e. writing to an out of range offset pads the string with spaces, where non-integer types are converted to integer, and only the first character of the assigned string is used. Formerly, empty strings where silently treated like an empty array.


    PHP
    1. <?php
    2. $a = '';
    3. $a[10] = 'foo';
    4. var_dump($a);
    5. ?>


    Output of the above example in PHP 7.0:

    Code
    1. array(1) {
    2. [10]=> string(3) "foo"
    3. }

    Output of the above example in PHP 7.1:

    Code
    1. string(11) " f"
  • drewm

    Approved the thread.