1

I figured I'd test the new PHP 7.2 release to see how many problems it creates on my site (I've been using 7.1 before this without trouble) and I noted that it appeared to disrupt the MySQL database connection in the Dp.php file for one of my scripts with the following error:

An exception occurred: count(): Parameter must be an array or an object that implements Countable in /../Db.php on line 57

The code it references is this bit:

    if (!count($dsn)) {
        return $parsed;
    }

I believe this is related to the 'counting non-countable types' change in 7.2 (http://php.net/manual/en/migration72.incompatible.php) and thing its probably due to a "null" value but I'm no expert in PHP and am not sure how to fix it. Just for context, here's the full code block that relates to $dsn in the Dp.php file:

class Censura_Db
{
    protected $connected_server_info; //cache for server information

    public static function factory($dsn, $options = false)
    {
        $class = new self($dsn, $options);
        return $class;
    }

    public static function parseDSN($dsn)
    {
        $parsed = array();
        if (is_array($dsn)) {
            $dsn = array_merge($parsed, $dsn);
            if (!$dsn['dbsyntax']) {
                $dsn['dbsyntax'] = $dsn['phptype'];
            }
            return $dsn;
        }
        // Find phptype and dbsyntax
        if (($pos = strpos($dsn, '://')) !== false) {
            $str = substr($dsn, 0, $pos);
            $dsn = substr($dsn, $pos + 3);
        } else {
            $str = $dsn;
            $dsn = null;
        }
        // Get phptype and dbsyntax
        // $str => phptype(dbsyntax)
        if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
            $parsed['phptype'] = $arr[1];
            $parsed['dbsyntax'] = !$arr[2] ? $arr[1] : $arr[2];
        } else {
            $parsed['phptype'] = $str;
            $parsed['dbsyntax'] = $str;
        }
        if (!count($dsn)) {
            return $parsed;
        }
        // Get (if found): username and password
        // $dsn => username:password@protocol+hostspec/database
        if (($at = strrpos($dsn, '@')) !== false) {
            $str = substr($dsn, 0, $at);
            $dsn = substr($dsn, $at + 1);
            if (($pos = strpos($str, ':')) !== false) {
                $parsed['username'] = rawurldecode(substr($str, 0, $pos));
                $parsed['password'] = rawurldecode(substr($str, $pos + 1));
            } else {
                $parsed['username'] = rawurldecode($str);
            }
        }
        // Find protocol and hostspec
        // $dsn => proto(proto_opts)/database
        if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) {
            $proto = $match[1];
            $proto_opts = $match[2] ? $match[2] : false;
            $dsn = $match[3];
            // $dsn => protocol+hostspec/database (old format)
        } else {
            if (strpos($dsn, '+') !== false) {
                list($proto, $dsn) = explode('+', $dsn, 2);
            }
            if (strpos($dsn, '//') === 0
                && strpos($dsn, '/', 2) !== false
                && $parsed['phptype'] == 'oci8'
            ) {
                //oracle's "Easy Connect" syntax:
                //"username/password@[//]host[:port][/service_name]"
                //e.g. "scott/tiger@//mymachine:1521/oracle"
                $proto_opts = $dsn;
                $dsn = null;
            } elseif (strpos($dsn, '/') !== false) {
                list($proto_opts, $dsn) = explode('/', $dsn, 2);
            } else {
                $proto_opts = $dsn;
                $dsn = null;
            }
        }
        // process the different protocol options
        $parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp';
        $proto_opts = rawurldecode($proto_opts);
        if (strpos($proto_opts, ':') !== false) {
            list($proto_opts, $parsed['port']) = explode(':', $proto_opts);
        }
        if ($parsed['protocol'] == 'tcp') {
            $parsed['hostspec'] = $proto_opts;
        } elseif ($parsed['protocol'] == 'unix') {
            $parsed['socket'] = $proto_opts;
        }
        // Get dabase if any
        // $dsn => database
        if ($dsn) {
            // /database
            if (($pos = strpos($dsn, '?')) === false) {
                $parsed['database'] = $dsn;
                // /database?param1=value1&param2=value2
            } else {
                $parsed['database'] = substr($dsn, 0, $pos);
                $dsn = substr($dsn, $pos + 1);
                if (strpos($dsn, '&') !== false) {
                    $opts = explode('&', $dsn);
                } else { // database?param1=value1
                    $opts = array($dsn);
                }
                foreach ($opts as $opt) {
                    list($key, $value) = explode('=', $opt);
                    if (!isset($parsed[$key])) {
                        // don't allow params overwrite
                        $parsed[$key] = rawurldecode($value);
                    }
                }
            }
        }
        return $parsed;
    }
MarkJ99
  • 11
  • 1
  • 3
  • 2
    So what do you expect $dsn to be at that point? Looking at your code, you got explicit checks for array, so apparently it can be something else as well. Maybe you can make the check more explicit, for instance `if (!is_array($dsm) || count($dsm) == 0)` or have a look at [this related question](https://stackoverflow.com/questions/42899605/how-to-check-if-something-is-countable) to find out how to check if something is countable in the first place. – GolezTrol Dec 01 '17 at 08:11

1 Answers1

1

You're using count() on values that aren't arrays, apparently to check if they are null.

Use !== null or isset() instead.

  • So do you think changing `if (!count($dsn)) {` to something like `if (null !== $dsn && !count($dsn)) {` might work? – MarkJ99 Dec 01 '17 at 08:28