Cannot use string offset as an array

  • The following line...

    $config['installedpackages']['zerotier']['config']['enabled'] = true;

    produces the following error when used in a packages GUI code...

    Fatal error: Uncaught Error: Cannot use string offset as an array

    However, I can exec that exact line using the pfSense php shell without an issue.

    What string offset is it talking about?

  • Rebel Alliance Developer Netgate

    It's a bug in the package that needs fixed for PHP 7.2.

    Contact the author of that package to have them fix it. That is not a package that is present in the pfSense repository, or one developed/maintained by Netgate.

  • I am actually trying to work on the package myself. Is there any documentation on the config system and how to interact with it? The functions to use and such.

  • Rebel Alliance Developer Netgate

    No, but that's not a config or pfSense issue -- that's a general issue with PHP 7.x and multi-level hashed arrays like that. You have to initialize each layer, you can't assume any part of it is an array, or you get errors when trying to perform some operations.

  • I see the following line in another package ...

    $a_mailers = getarraybyref($config, 'installedpackages', 'haproxy', 'email_mailers', 'item');

    would I need to do something like that ?

  • @coreybrett
    Yes that would solve the issue :)

  • Is ['config'][0]['enable'] a requirement for storing the state of a package?

  • @coreybrett
    I think not. Afaik the ['config'][0] is only for trying to support a remnant of a old config format iirc.

  • Rebel Alliance Developer Netgate

    If a package uses XML forms to store data they are automatically put in that style of array. For example, if you use pkg_edit.php?xml=foo.xml for settings with multiple values (lists of things, for example) then it would be stored with that last [0] as the item id, so you'd have 0, 1, 2 and so on for each separate entry. It's just easier to use the same format for the other style as well for consistency, even if it's always 0.

  • Hello, I'm trying to do something similar on my side but from the comments i was not able to understand what's wrong in my code and how to make it work. So here is one of the lines I'm trying to execute:

    $config['aliases']['alias'][1]['name'] = 'LANNetworks';

    And this is producing the following error:

    Fatal error: Uncaught Error: Cannot use string offset as an array in /usr/local/sbin/pfSsh.php(371) : eval()'d code:4

    This peace of code works fine on previous version of pfsense 2.4.3 but in 2.4.4 it's giving me this problem. Would somebody be able to help me? Thanks in advance.

  • @justleo
    You would need to write something like this:

    if (!is_array($config['aliases'])) $config['aliases'] = array();
    if (!is_array($config['aliases']['alias'])) $config['aliases']['alias'] = array();
    if (!is_array($config['aliases']['alias'][1])) $config['aliases']['alias'][1] = array();
    $config['aliases']['alias'][1]['name'] = 'LANNetworks';

    Or imho easier:

    $myalias = &getarraybyref($config, 'aliases', 'alias', 1);
    $myalias['name'] = 'LANNetworks';

  • Rebel Alliance Developer Netgate


    init_config_arr(array('aliases', 'alias', 1));
    $config['aliases']['alias'][1]['name'] = 'LANNetworks';

  • @jimp Thanks for your quick help, I was able to make it work with your second suggention:

    init_config_arr(array('aliases', 'alias', 0));
    $config['aliases']['alias'][0]['name'] = 'LANNetworks';
    $config['aliases']['alias'][0]['address'] = 'CIDR';
    $config['aliases']['alias'][0]['descr'] = 'SomeDescription';
    $config['aliases']['alias'][0]['type'] = 'network';

Log in to reply