burnacid / MyBB-Form-Creator

A MyBB 1.8 form creator
GNU General Public License v3.0
11 stars 6 forks source link

Sends PMs to indvidual users when set to groups. #137

Open Rhababo opened 1 year ago

Rhababo commented 1 year ago

Describe the bug When using a form that sends PM's to groups, It sends multiple individual PM's to each member of the group, rather than a single PM with multiple recipients. This flags the messages as pm flooding and prevents all but one of the messages from going through.

To Reproduce Steps to reproduce the behavior:

  1. Create a form that is set to send PM's to user groups with multiple users in the group.
  2. Have a non-admin (Registered) user fill out and send the form.
  3. See error

Expected behavior When sending to a group, it should be sent as a single PM instead of several individual PM's.

Desktop (please complete the following information): MyBB version: 1.8.36 Plugin version: 2.6.6 PHP version: 8.1.16 MySQL version: 5.7

I corrected this issue by changing the "get_usergroup_users" function in plugins/formcreator.php to only return an array of recipient userid's.

function get_usergroup_users_uid($gid)
{
    global $db;

    if (is_array($gid)) {
        $additionwhere = "";
        foreach ($gid as $groupid) {
            $additionwhere .= " OR CONCAT(',',additionalgroups,',') LIKE '%," . intval($groupid) . ",%'";
        }

        $query = $db->simple_select("users", "uid", "usergroup IN (" . implode(",", $gid) . ")" . $additionwhere);
    } else {
        $query = $db->simple_select("users", "uid", "usergroup IN (" . intval($gid) . ") OR CONCAT(',',additionalgroups,',') LIKE '%," . intval($gid) . ",%'");
    }

    if ($db->num_rows($query)) {
        $rownum = 0;
        while ($user = $db->fetch_array($query)) {
            $userarray[$rownum] = $user['uid'];
            $rownum++;
        }
        return $userarray;
    } else {
        return false;
    }
}

(This function is only called in form.php when sending PM's to groups)

once get_usergroup_users returns an array of receipients uids, then you can just set that as the "toid" value in the $pm array at line 260 (or so) in form.php

 $group_members = get_usergroup_users_uid($formcreator->settings['pmgroups']);

                        $pmhandler = new PMDataHandler();

                        $pm = array(
                            "subject" => $subject,
                            "message" => $message,
                            "icon" => $formcreator->settings['posticon'],
                            "toid" => $group_members,
                            "fromid" => $uid,
                            "do" => '',
                            "pmid" => ''
                        );

This solution worked for me, but I haven't tested it fully with other configurations

burnacid commented 1 year ago

Thanks, I'll setup some tests to check. Feel free to create a pull request of this if you like