} else {
                     if (intval($counts[UNREAD_MAIL]) > 0) {
                         $read_status = UNREAD_MAIL;
                     }
                 }
                 // update
                 if ($read_status != $originals[$privmsg_id]) {
                     // update sender if read status changed
                     $sql = "UPDATE " . PRIVMSGA_RECIPS_TABLE . "\n                                    SET privmsg_read = {$read_status}, privmsg_distrib = 0\n                                    WHERE privmsg_id = {$privmsg_id}\n                                        AND privmsg_direct = 0";
                     if (!$db->sql_query($sql)) {
                         message_die(GENERAL_ERROR, 'Could not update unread status for the sender of this message', '', __LINE__, __FILE__, $sql);
                     }
                 }
             }
         }
         resync_pm($view_user_id);
     }
 }
 // set the page title and include the page header
 $page_title = _lang('Private_Messaging');
 if (!defined('IN_PCP')) {
     include $phpbb_root_path . 'includes/page_header.' . $phpEx;
 }
 // template name
 $template->set_filenames(array('body' => 'privmsga_body.tpl'));
 // send header
 privmsg_header($view_user_id, $folder_id);
 // send list
 privmsg_list($privmsg_rowset, $recips, $folder_id, true, $mark_ids);
 // pagination
 $page_list = generate_pagination("profile.{$phpEx}?mode=privmsg", $count_in_sub_folder, $board_config['topics_per_page'], $pm_start);
function distribute_pm($user_id, $privmsg_recip_ids = array())
{
    global $db;
    global $folders;
    // fix the privmsg_recip_ids
    if (!is_array($privmsg_recip_ids) && !empty($privmsg_recip_ids)) {
        $privmsg_recip_ids = array(intval($privmsg_recip_ids));
    }
    $s_recips_ids = empty($privmsg_recip_ids) ? '' : implode(', ', $privmsg_recip_ids);
    // first do some cleaning job
    adjustbox_pm($user_id);
    // then get the ids of the non-processed recips
    $s_privmsg_recip_ids = '';
    $s_privmsg_ids = '';
    $sql_where = "privmsg_distrib = 0";
    if (!empty($s_recip_ids)) {
        $sql_where = "( privmsg_distrib = 0 OR privmsg_recip_id IN ({$s_recip_ids}) )";
    }
    $sql = "SELECT privmsg_id, privmsg_recip_id\n                FROM " . PRIVMSGA_RECIPS_TABLE . "\n                WHERE privmsg_user_id = {$user_id}\n                    AND {$sql_where}";
    if (!($result = $db->sql_query($sql))) {
        message_die(GENERAL_ERROR, 'Could not get un-distrib recips', '', __LINE__, __FILE__, $sql);
    }
    while ($row = $db->sql_fetchrow($result)) {
        $s_privmsg_ids .= (empty($s_privmsg_ids) ? '' : ', ') . $row['privmsg_id'];
        $s_privmsg_recip_ids .= (empty($s_privmsg_recip_ids) ? '' : ', ') . $row['privmsg_recip_id'];
    }
    // nothing to process
    if (empty($s_privmsg_recip_ids)) {
        return;
    }
    $folders_list[INBOX] = empty($folders['sub'][INBOX]) ? INBOX : INBOX . ', ' . implode(', ', $folders['sub'][INBOX]);
    $folders_list[OUTBOX] = empty($folders['sub'][OUTBOX]) ? OUTBOX : OUTBOX . ', ' . implode(', ', $folders['sub'][OUTBOX]);
    $folders_list[SENTBOX] = empty($folders['sub'][SENTBOX]) ? SENTBOX : SENTBOX . ', ' . implode(', ', $folders['sub'][SENTBOX]);
    $folders_list[SAVEBOX] = empty($folders['sub'][SAVEBOX]) ? SAVEBOX : SAVEBOX . ', ' . implode(', ', $folders['sub'][SAVEBOX]);
    // rules
    $error = false;
    $error_msg = '';
    // system rules
    $sql = "UPDATE " . PRIVMSGA_RECIPS_TABLE . "\n                SET privmsg_folder_id = " . SENTBOX . "\n                WHERE privmsg_recip_id IN ({$s_privmsg_recip_ids})\n                    AND privmsg_direct = 0\n                    AND privmsg_folder_id IN (" . $folders_list[OUTBOX] . ")\n                    AND privmsg_read = " . READ_MAIL . "\n                    AND privmsg_status = " . STS_TRANSIT;
    if (!$db->sql_query($sql)) {
        _error('Sent_box_feeding_failed');
    }
    // read custom rules
    $sql = "SELECT *\n                FROM " . PRIVMSGA_RULES_TABLE . "\n                WHERE rules_user_id = {$user_id}\n                ORDER BY rules_name";
    if (!($result = $db->sql_query($sql))) {
        message_die(GENERAL_ERROR, 'Could not read messages rules', '', __LINE__, __FILE__, $sql);
    }
    $words = array();
    $users = array();
    $groups = array();
    $friends = array();
    $frules = array();
    while ($row = $db->sql_fetchrow($result)) {
        $folder_id = $row['rules_folder_id'];
        // own code: init all sub arrays!
        if (empty($groups[$folder_id])) {
            $groups[$folder_id] = array();
        }
        if (empty($users[$folder_id])) {
            $users[$folder_id] = array();
        }
        if (!empty($words[$folder_id])) {
            $words[$folder_id] = array();
        }
        if (empty($frules) || !in_array($folder_id, $frules)) {
            $frules[] = $folder_id;
        }
        if (!empty($row['rules_word'])) {
            $word_list = explode(',', $row['rules_word']);
            for ($i = 0; $i < count($word_list); $i++) {
                $words[$folder_id][] = str_replace("\\'", "''", trim($word_list[$i]));
            }
        } else {
            if (empty($row['rules_group_id'])) {
                $users[$folder_id][] = 0;
            } else {
                if ($row['rules_group_id'] == FRIEND_LIST_GROUP) {
                    $friends[$folder_id] = true;
                } else {
                    $groups[$folder_id][] = $row['rules_group_id'];
                }
            }
        }
    }
    // build sql requests for custom rules
    for ($i = 0; $i < count($frules); $i++) {
        // choose the good list of folders
        $folder_id = $frules[$i];
        $folder_main = $folders['main'][$folder_id];
        // resulting privmsg_id
        $s_privmsg_recip_id_res = '';
        if (!empty($words[$folder_id])) {
            // prepare the request
            $sql = "SELECT pr.privmsg_recip_id\n                        FROM " . PRIVMSGA_TABLE . " p, " . PRIVMSGA_RECIPS_TABLE . " pr\n                        WHERE p.privmsg_id = pr.privmsg_id\n                            AND pr.privmsg_recip_id IN ({$s_privmsg_recip_ids})\n                            AND pr.privmsg_folder_id IN (" . $folders_list[$folder_main] . ")";
            // words filter
            $words_subject = '';
            $words_text = '';
            for ($k = 0; $k < count($words[$frules[$i]]); $k++) {
                $words_subject .= (empty($words_subject) ? '' : ' OR ') . "p.privmsg_subject LIKE '%" . $words[$frules[$i]][$k] . "%'";
                $words_text .= (empty($words_text) ? '' : ' OR ') . "p.privmsg_text LIKE '%" . $words[$frules[$i]][$k] . "%'";
            }
            // process the request
            $sql .= " AND ({$words_subject}) AND ({$words_text})";
            if (!($result = $db->sql_query($sql))) {
                _error('Word_search_failed');
            } else {
                while ($row = $db->sql_fetchrow($result)) {
                    $s_privmsg_recip_id_res .= (empty($s_privmsg_recip_id_res) ? '' : ', ') . $row['privmsg_recip_id'];
                }
            }
        }
        // groups/users
        if (!empty($groups[$folder_id])) {
            $s_groups = implode(', ', $groups[$folder_id]);
            $sql = "SELECT pr.privmsg_recip_id\n                        FROM " . PRIVMSGA_RECIPS_TABLE . " pr, " . PRIVMSGA_RECIPS_TABLE . " pa, " . USER_GROUP_TABLE . " ug\n                        WHERE pr.privmsg_recip_id IN ({$s_privmsg_recip_ids})\n                            AND pa.privmsg_id = pr.privmsg_id AND pa.privmsg_direct <> pr.privmsg_direct\n                            AND pr.privmsg_folder_id IN (" . $folders_list[$folder_main] . ")\n                            AND ug.user_id=pa.privmsg_user_id AND ug.group_id IN ({$s_groups})";
            // process the request
            if (!($result = $db->sql_query($sql))) {
                _error('Group_search_failed');
            } else {
                while ($row = $db->sql_fetchrow($result)) {
                    $s_privmsg_recip_id_res .= (empty($s_privmsg_recip_id_res) ? '' : ', ') . $row['privmsg_recip_id'];
                }
            }
        }
        // friend list
        if ($friends[$folder_id] && defined('IN_PCP')) {
            $sql = "SELECT pr.privmsg_recip_id\n                        FROM " . PRIVMSGA_RECIPS_TABLE . " pr, " . PRIVMSGA_RECIPS_TABLE . " pa, " . BUDDYS_TABLE . " b\n                        WHERE pr.privmsg_recip_id IN ({$s_privmsg_recip_ids})\n                            AND pa.privmsg_id = pr.privmsg_id AND pa.privmsg_direct <> pr.privmsg_direct\n                            AND pr.privmsg_folder_id IN (" . $folders_list[$folder_main] . ")\n                            AND b.user_id = {$view_user_id} AND b.buddy_id = pa.privmsg_user_id AND b.buddy_ignore = 0";
            // process the request
            if (!($result = $db->sql_query($sql))) {
                _error('Friends_search_failed');
            } else {
                while ($row = $db->sql_fetchrow($result)) {
                    $s_privmsg_recip_id_res .= (empty($s_privmsg_recip_id_res) ? '' : ', ') . $row['privmsg_recip_id'];
                }
            }
        }
        // system messages
        if (!empty($users[$folder_id])) {
            $sql = "SELECT pr.privmsg_recip_id\n                        FROM " . PRIVMSGA_RECIPS_TABLE . " pr, " . PRIVMSGA_RECIPS_TABLE . " pa\n                        WHERE pr.privmsg_recip_id IN ({$s_privmsg_recip_ids})\n                            AND pa.privmsg_id = pr.privmsg_id AND pa.privmsg_direct <> pr.privmsg_direct\n                            AND pr.privmsg_folder_id IN (" . $folders_list[$folder_main] . ")\n                            AND pa.privmsg_user_id = 0";
            // process the request
            if (!($result = $db->sql_query($sql))) {
                _error('Sysuser_search_failed');
            } else {
                while ($row = $db->sql_fetchrow($result)) {
                    $s_privmsg_recip_id_res .= (empty($s_privmsg_recip_id_res) ? '' : ', ') . $row['privmsg_recip_id'];
                }
            }
        }
        // move the recip
        $sql = "UPDATE " . PRIVMSGA_RECIPS_TABLE . "\n                    SET privmsg_folder_id = {$folder_id},\n                        privmsg_distrib = 1\n                    WHERE privmsg_recip_id IN ({$s_privmsg_recip_id_res})";
        if (!$db->sql_query($sql)) {
            _error('Distribution_failed');
        }
    }
    // add the final rule
    $sql = "UPDATE " . PRIVMSGA_RECIPS_TABLE . "\n                SET privmsg_distrib = 1\n                WHERE privmsg_recip_id IN ({$s_privmsg_recip_ids})\n                    AND privmsg_distrib = 0";
    if (!$db->sql_query($sql)) {
        _error('Remove_undistrib_flag_failed');
    }
    if ($error) {
        message_die(GENERAL_ERROR, $error_msg, _lang('Distribution_failed'));
    }
    // adjust the user's counts
    resync_pm($view_user_id);
}