Error - Argument #1 ($array) must be of type array

202 views Asked by At

I have been trying to setup my WHMCS instance to automatically assign clients to a client group, based on the product.

I have found a code hook from Katamaze, which seems to have worked previously but it now throws an error as below on PHP 8.1 with WHMCS 8.8 - seems there was a vulnerability patched that resulted in some changed code in the new PHP, which has broken this HOOK.

<?php

/**
 * Assign Client to Group based on purchased Product/Service v1
 *
 * @package     WHMCS
 * @copyright   Katamaze
 * @link        https://katamaze.com
 * @author      Davide Mantenuto <[email protected]>
 */

use WHMCS\Database\Capsule;

add_hook('AcceptOrder', 1, function($vars)
{
    // Define group/product pairs. Instructions provided below
    // https://github.com/Katamaze/WHMCS-Free-Action-Hooks/blob/master/README.md#client-to-group-based-on-purchased-productservice
    $groups['products']['5'] = array('3', '18', '16', '5','6','7','8','9','10','11','12','13','14','15','19');
    $groups['products']['6'] = array('75','76');
    $groups['productaddons']['0'] = array('0');
    $groups['configurableoption']['0'] = array('0' => array('0'), '' => true);

    if (!$groups): return; endif;
    $userID = Capsule::table('tblorders')->where('id', $vars['orderid'])->pluck('userid')[0];
    $orderedProducts = Capsule::table('tblhosting')->where('orderid', $vars['orderid'])->pluck('packageid', 'id');
    $orderedProductAddons = Capsule::table('tblhostingaddons')->where('orderid', $vars['orderid'])->pluck('addonid');

    if ($groups['configurableoption'])
    {
        foreach (Capsule::select(Capsule::raw('SELECT t1.relid, t1.configid, t1.optionid, t1.qty, t2.optiontype FROM tblhostingconfigoptions as t1 LEFT JOIN tblproductconfigoptions AS t2 ON t1.configid = t2.id LEFT JOIN tblproductconfigoptionssub AS t3 ON t1.optionid = t3.id WHERE t1.relid IN (\'' . implode('\',\'', array_keys($orderedProducts)) . '\')')) as $v)
        {
            $relid = $v->relid;
            $configid = $v->configid;

            if (in_array($v->optiontype, array('3', '4')))
            {
                $value = ($v->qty ? true : false);
            }
            else
            {
                $value = $v->optionid;
            }

            unset($v);

            if ($value)
            {
                $orderedConfigurableOptions[$relid][$configid] = $value;
            }
        }
    }

    foreach ($groups['products'] as $group => $target)
    {
        if (array_intersect($orderedProducts, $target))
        {
            Capsule::table('tblclients')->where('id', $userID)->where('groupid', '0')->update(['groupid' => $group]);
            return;
        }
    }

    foreach ($groups['productaddons'] as $group => $target)
    {
        if (array_intersect($orderedProductAddons, $target))
        {
            Capsule::table('tblclients')->where('id', $userID)->where('groupid', '0')->update(['groupid' => $group]);
            return;
        }
    }

    foreach ($groups['configurableoption'] as $group => $configurableOptions)
    {
        foreach ($configurableOptions as $configID => $options)
        {
            if (is_array($options))
            {
                foreach ($orderedConfigurableOptions as $target)
                {
                    if (array_intersect($options, $target))
                    {
                        Capsule::table('tblclients')->where('id', $userID)->where('groupid', '0')->update(['groupid' => $group]);
                        return;
                    }
                }
            }
            else
            {
                foreach ($orderedConfigurableOptions as $target)
                {
                    if (in_array($configID, $target))
                    {
                        Capsule::table('tblclients')->where('id', $userID)->where('groupid', '0')->update(['groupid' => $group]);
                        return;
                    }
                }
            }
        }
    }
});

I suspect this will probably work if I dropped back to PHP 7.x but thats a fairly large undertaking, and needs a lot of changes to code...so I am yet to try that, and hoping somebody has a fix!

0

There are 0 answers