Example #1
0
/**
 * @todo yarick123: email_collect_recipients(...) will be completely rewritten to provide additional information such as language, user access,..
 * @todo yarick123:sort recipients list by language to reduce switches between different languages
 * @param int $p_bug_id
 * @param string $p_notify_type
 * @param array $p_extra_user_ids_to_email
 * @return array
 */
function email_collect_recipients($p_bug_id, $p_notify_type, $p_extra_user_ids_to_email = array())
{
    $c_bug_id = db_prepare_int($p_bug_id);
    $t_recipients = array();
    # add explicitly specified users
    if (ON == email_notify_flag($p_notify_type, 'explicit')) {
        foreach ($p_extra_user_ids_to_email as $t_user_id) {
            $t_recipients[$t_user_id] = true;
            log_event(LOG_EMAIL_RECIPIENT, sprintf('Issue = #%d, add explicitly specified user = @U%d', $p_bug_id, $t_user_id));
        }
    }
    # add Reporter
    if (ON == email_notify_flag($p_notify_type, 'reporter')) {
        $t_reporter_id = bug_get_field($p_bug_id, 'reporter_id');
        $t_recipients[$t_reporter_id] = true;
        log_event(LOG_EMAIL_RECIPIENT, sprintf('Issue = #%d, add Reporter = @U%d', $p_bug_id, $t_reporter_id));
    }
    # add Handler
    if (ON == email_notify_flag($p_notify_type, 'handler')) {
        $t_handler_id = bug_get_field($p_bug_id, 'handler_id');
        if ($t_handler_id > 0) {
            $t_recipients[$t_handler_id] = true;
            log_event(LOG_EMAIL_RECIPIENT, sprintf('Issue = #%d, add Handler = @U%d', $p_bug_id, $t_handler_id));
        }
    }
    $t_project_id = bug_get_field($p_bug_id, 'project_id');
    # add users monitoring the bug
    $t_bug_monitor_table = db_get_table('mantis_bug_monitor_table');
    if (ON == email_notify_flag($p_notify_type, 'monitor')) {
        $query = "SELECT DISTINCT user_id\n\t\t\t\t\t  FROM {$t_bug_monitor_table}\n\t\t\t\t\t  WHERE bug_id=" . db_param();
        $result = db_query_bound($query, array($c_bug_id));
        $count = db_num_rows($result);
        for ($i = 0; $i < $count; $i++) {
            $t_user_id = db_result($result, $i);
            $t_recipients[$t_user_id] = true;
            log_event(LOG_EMAIL_RECIPIENT, sprintf('Issue = #%d, add Monitor = @U%d', $p_bug_id, $t_user_id));
        }
    }
    # add users who contributed bugnotes
    $t_bugnote_id = bugnote_get_latest_id($p_bug_id);
    $t_bugnote_view = bugnote_get_field($t_bugnote_id, 'view_state');
    $t_bugnote_date = bugnote_get_field($t_bugnote_id, 'last_modified');
    $t_bug = bug_get($p_bug_id);
    $t_bug_date = $t_bug->last_updated;
    $t_bugnote_table = db_get_table('mantis_bugnote_table');
    if (ON == email_notify_flag($p_notify_type, 'bugnotes')) {
        $query = "SELECT DISTINCT reporter_id\n\t\t\t\t\t  FROM {$t_bugnote_table}\n\t\t\t\t\t  WHERE bug_id = " . db_param();
        $result = db_query_bound($query, array($c_bug_id));
        $count = db_num_rows($result);
        for ($i = 0; $i < $count; $i++) {
            $t_user_id = db_result($result, $i);
            $t_recipients[$t_user_id] = true;
            log_event(LOG_EMAIL_RECIPIENT, sprintf('Issue = #%d, add Note Author = @U%d', $p_bug_id, $t_user_id));
        }
    }
    # add project users who meet the thresholds
    $t_bug_is_private = bug_get_field($p_bug_id, 'view_state') == VS_PRIVATE;
    $t_threshold_min = email_notify_flag($p_notify_type, 'threshold_min');
    $t_threshold_max = email_notify_flag($p_notify_type, 'threshold_max');
    $t_threshold_users = project_get_all_user_rows($t_project_id, $t_threshold_min);
    foreach ($t_threshold_users as $t_user) {
        if ($t_user['access_level'] <= $t_threshold_max) {
            if (!$t_bug_is_private || access_compare_level($t_user['access_level'], config_get('private_bug_threshold'))) {
                $t_recipients[$t_user['id']] = true;
                log_event(LOG_EMAIL_RECIPIENT, sprintf('Issue = #%d, add Project User = @U%d', $p_bug_id, $t_user['id']));
            }
        }
    }
    # add users as specified by plugins
    $t_recipients_include_data = event_signal('EVENT_NOTIFY_USER_INCLUDE', array($p_bug_id, $p_notify_type));
    foreach ($t_recipients_include_data as $t_plugin => $t_recipients_include_data2) {
        foreach ($t_recipients_include_data2 as $t_callback => $t_recipients_included) {
            # only handle if we get an array from the callback
            if (is_array($t_recipients_included)) {
                foreach ($t_recipients_included as $t_user_id) {
                    $t_recipients[$t_user_id] = true;
                    log_event(LOG_EMAIL_RECIPIENT, sprintf('Issue = #%d, %s plugin added user @U%d', $p_bug_id, $t_plugin, $t_user_id));
                }
            }
        }
    }
    # FIXME: the value of $p_notify_type could at this stage be either a status
    # or a built-in actions such as 'owner and 'sponsor'. We have absolutely no
    # idea whether 'new' is indicating a new bug has been filed, or if the
    # status of an existing bug has been changed to 'new'. Therefore it is best
    # to just assume built-in actions have precedence over status changes.
    switch ($p_notify_type) {
        case 'new':
        case 'feedback':
            # This isn't really a built-in action (delete me!)
        # This isn't really a built-in action (delete me!)
        case 'reopened':
        case 'resolved':
        case 'closed':
        case 'bugnote':
            $t_pref_field = 'email_on_' . $p_notify_type;
            break;
        case 'owner':
            # The email_on_assigned notification type is now effectively
            # email_on_change_of_handler.
            $t_pref_field = 'email_on_assigned';
            break;
        case 'deleted':
        case 'updated':
        case 'sponsor':
        case 'relation':
        case 'monitor':
        case 'priority':
            # This is never used, but exists in the database!
            # FIXME: these notification actions are not actually implemented
            # in the database and therefore aren't adjustable on a per-user
            # basis! The exception is 'monitor' that makes no sense being a
            # customisable per-user preference.
            $t_pref_field = false;
            break;
        default:
            # Anything not built-in is probably going to be a status
            $t_pref_field = 'email_on_status';
            break;
    }
    # @@@ we could optimize by modifiying user_cache() to take an array
    #  of user ids so we could pull them all in.  We'll see if it's necessary
    $t_final_recipients = array();
    $t_user_ids = array_keys($t_recipients);
    user_cache_array_rows($t_user_ids);
    user_pref_cache_array_rows($t_user_ids);
    user_pref_cache_array_rows($t_user_ids, $t_bug->project_id);
    # Check whether users should receive the emails
    # and put email address to $t_recipients[user_id]
    foreach ($t_recipients as $t_id => $t_ignore) {
        # Possibly eliminate the current user
        if (auth_get_current_user_id() == $t_id && OFF == config_get('email_receive_own')) {
            log_event(LOG_EMAIL_RECIPIENT, sprintf('Issue = #%d, drop @U%d (own)', $p_bug_id, $t_id));
            continue;
        }
        # Eliminate users who don't exist anymore or who are disabled
        if (!user_exists($t_id) || !user_is_enabled($t_id)) {
            log_event(LOG_EMAIL_RECIPIENT, sprintf('Issue = #%d, drop @U%d (disabled)', $p_bug_id, $t_id));
            continue;
        }
        # Exclude users who have this notification type turned off
        if ($t_pref_field) {
            $t_notify = user_pref_get_pref($t_id, $t_pref_field);
            if (OFF == $t_notify) {
                log_event(LOG_EMAIL_RECIPIENT, sprintf('Issue = #%d, drop @U%d (pref %s off)', $p_bug_id, $t_id, $t_pref_field));
                continue;
            } else {
                # Users can define the severity of an issue before they are emailed for
                # each type of notification
                $t_min_sev_pref_field = $t_pref_field . '_min_severity';
                $t_min_sev_notify = user_pref_get_pref($t_id, $t_min_sev_pref_field);
                $t_bug_severity = bug_get_field($p_bug_id, 'severity');
                if ($t_bug_severity < $t_min_sev_notify) {
                    log_event(LOG_EMAIL_RECIPIENT, sprintf('Issue = #%d, drop @U%d (pref threshold)', $p_bug_id, $t_id));
                    continue;
                }
            }
        }
        # exclude users who don't have at least viewer access to the bug,
        # or who can't see bugnotes if the last update included a bugnote
        if (!access_has_bug_level(VIEWER, $p_bug_id, $t_id) || $t_bug_date == $t_bugnote_date && !access_has_bugnote_level(VIEWER, $t_bugnote_id, $t_id)) {
            log_event(LOG_EMAIL_RECIPIENT, sprintf('Issue = #%d, drop @U%d (access level)', $p_bug_id, $t_id));
            continue;
        }
        # check to exclude users as specified by plugins
        $t_recipient_exclude_data = event_signal('EVENT_NOTIFY_USER_EXCLUDE', array($p_bug_id, $p_notify_type, $t_id));
        $t_exclude = false;
        foreach ($t_recipient_exclude_data as $t_plugin => $t_recipient_exclude_data2) {
            foreach ($t_recipient_exclude_data2 as $t_callback => $t_recipient_excluded) {
                # exclude if any plugin returns true (excludes the user)
                if ($t_recipient_excluded) {
                    $t_exclude = true;
                    log_event(LOG_EMAIL_RECIPIENT, sprintf('Issue = #%d, %s plugin dropped user @U%d', $p_bug_id, $t_plugin, $t_id));
                }
            }
        }
        # user was excluded by a plugin
        if ($t_exclude) {
            continue;
        }
        # Finally, let's get their emails, if they've set one
        $t_email = user_get_email($t_id);
        if (is_blank($t_email)) {
            log_event(LOG_EMAIL_RECIPIENT, sprintf('Issue = #%d, drop @U%d (no email)', $p_bug_id, $t_id));
        } else {
            # @@@ we could check the emails for validity again but I think
            #   it would be too slow
            $t_final_recipients[$t_id] = $t_email;
        }
    }
    return $t_final_recipients;
}
Example #2
0
/**
 * Send notices when a bug handler is changed.
 * @param int $p_bug_id
 * @param int $p_prev_handler_id
 * @param int $p_new_handler_id
 * @return null
 */
function email_owner_changed($p_bug_id, $p_prev_handler_id, $p_new_handler_id)
{
    if ($p_prev_handler_id == 0 && $p_new_handler_id != 0) {
        log_event(LOG_EMAIL, sprintf('Issue #%d assigned to user @U%d.', $p_bug_id, $p_new_handler_id));
    } else {
        if ($p_prev_handler_id != 0 && $p_new_handler_id == 0) {
            log_event(LOG_EMAIL, sprintf('Issue #%d is no longer assigned to @U%d.', $p_bug_id, $p_prev_handler_id));
        } else {
            log_event(LOG_EMAIL, sprintf('Issue #%d is assigned to @U%d instead of @U%d.', $p_bug_id, $p_new_handler_id, $p_prev_handler_id));
        }
    }
    $t_message_id = $p_new_handler_id == NO_USER ? 'email_notification_title_for_action_bug_unassigned' : 'email_notification_title_for_action_bug_assigned';
    $t_extra_user_ids_to_email = array();
    if ($p_prev_handler_id !== NO_USER && $p_prev_handler_id != $p_new_handler_id) {
        if (email_notify_flag('owner', 'handler') == ON) {
            $t_extra_user_ids_to_email[] = $p_prev_handler_id;
        }
    }
    email_generic($p_bug_id, 'owner', $t_message_id, null, $t_extra_user_ids_to_email);
}
Example #3
0
function email_collect_recipients($p_bug_id, $p_notify_type)
{
    $c_bug_id = db_prepare_int($p_bug_id);
    $t_recipients = array();
    # add Reporter
    if (ON == email_notify_flag($p_notify_type, 'reporter')) {
        $t_reporter_id = bug_get_field($p_bug_id, 'reporter_id');
        $t_recipients[$t_reporter_id] = true;
        log_event(LOG_EMAIL_RECIPIENT, "bug={$p_bug_id}, add reporter={$t_reporter_id}");
    }
    # add Handler
    if (ON == email_notify_flag($p_notify_type, 'handler')) {
        $t_handler_id = bug_get_field($p_bug_id, 'handler_id');
        if ($t_handler_id > 0) {
            $t_recipients[$t_handler_id] = true;
            log_event(LOG_EMAIL_RECIPIENT, "bug={$p_bug_id}, add handler={$t_handler_id}");
        }
    }
    $t_project_id = bug_get_field($p_bug_id, 'project_id');
    # add users monitoring the bug
    $t_bug_monitor_table = config_get('mantis_bug_monitor_table');
    if (ON == email_notify_flag($p_notify_type, 'monitor')) {
        $query = "SELECT DISTINCT user_id\n\t\t\t\t\t  FROM {$t_bug_monitor_table}\n\t\t\t\t\t  WHERE bug_id={$c_bug_id}";
        $result = db_query($query);
        $count = db_num_rows($result);
        for ($i = 0; $i < $count; $i++) {
            $t_user_id = db_result($result, $i);
            $t_recipients[$t_user_id] = true;
            log_event(LOG_EMAIL_RECIPIENT, "bug={$p_bug_id}, add monitor={$t_user_id}");
        }
    }
    # add users who contributed bugnotes
    $t_bugnote_id = bugnote_get_latest_id($p_bug_id);
    $t_bugnote_view = bugnote_get_field($t_bugnote_id, 'view_state');
    $t_bugnote_date = db_unixtimestamp(bugnote_get_field($t_bugnote_id, 'last_modified'));
    $t_bug_date = bug_get_field($p_bug_id, 'last_updated');
    $t_bugnote_table = config_get('mantis_bugnote_table');
    if (ON == email_notify_flag($p_notify_type, 'bugnotes')) {
        $query = "SELECT DISTINCT reporter_id\n\t\t\t\t\t  FROM {$t_bugnote_table}\n\t\t\t\t\t  WHERE bug_id = {$c_bug_id}";
        $result = db_query($query);
        $count = db_num_rows($result);
        for ($i = 0; $i < $count; $i++) {
            $t_user_id = db_result($result, $i);
            $t_recipients[$t_user_id] = true;
            log_event(LOG_EMAIL_RECIPIENT, "bug={$p_bug_id}, add note author={$t_user_id}");
        }
    }
    # add project users who meet the thresholds
    $t_bug_is_private = bug_get_field($p_bug_id, 'view_state') == VS_PRIVATE;
    $t_threshold_min = email_notify_flag($p_notify_type, 'threshold_min');
    $t_threshold_max = email_notify_flag($p_notify_type, 'threshold_max');
    $t_threshold_users = project_get_all_user_rows($t_project_id, $t_threshold_min);
    foreach ($t_threshold_users as $t_user) {
        if ($t_user['access_level'] <= $t_threshold_max) {
            if (!$t_bug_is_private || access_compare_level($t_user['access_level'], config_get('private_bug_threshold'))) {
                $t_recipients[$t_user['id']] = true;
                log_event(LOG_EMAIL_RECIPIENT, "bug={$p_bug_id}, add project user=" . $t_user['id']);
            }
        }
    }
    # set up to eliminate unwanted users
    #  get list of status values that are not covered specifically in the prefs
    #  These are handled by email_on_status generically
    #  @@@ thraxisp note that email_on_assigned was co-opted to handle change in handler
    $t_status_change = get_enum_to_array(config_get('status_enum_string'));
    unset($t_status_change[NEW_]);
    unset($t_status_change[FEEDBACK]);
    unset($t_status_change[RESOLVED]);
    unset($t_status_change[CLOSED]);
    if ('owner' == $p_notify_type) {
        $t_pref_field = 'email_on_assigned';
    } else {
        if (in_array($p_notify_type, $t_status_change)) {
            $t_pref_field = 'email_on_status';
        } else {
            $t_pref_field = 'email_on_' . $p_notify_type;
        }
    }
    $t_user_pref_table = config_get('mantis_user_pref_table');
    if (!db_field_exists($t_pref_field, $t_user_pref_table)) {
        $t_pref_field = false;
    }
    # @@@ we could optimize by modifiying user_cache() to take an array
    #  of user ids so we could pull them all in.  We'll see if it's necessary
    $t_final_recipients = array();
    # Check whether users should receive the emails
    # and put email address to $t_recipients[user_id]
    foreach ($t_recipients as $t_id => $t_ignore) {
        # Possibly eliminate the current user
        if (auth_get_current_user_id() == $t_id && OFF == config_get('email_receive_own')) {
            log_event(LOG_EMAIL_RECIPIENT, "bug={$p_bug_id}, drop {$t_id} (own)");
            continue;
        }
        # Eliminate users who don't exist anymore or who are disabled
        if (!user_exists($t_id) || !user_is_enabled($t_id)) {
            log_event(LOG_EMAIL_RECIPIENT, "bug={$p_bug_id}, drop {$t_id} (disabled)");
            continue;
        }
        # Exclude users who have this notification type turned off
        if ($t_pref_field) {
            $t_notify = user_pref_get_pref($t_id, $t_pref_field);
            if (OFF == $t_notify) {
                log_event(LOG_EMAIL_RECIPIENT, "bug={$p_bug_id}, drop {$t_id} (pref {$t_pref_field} off)");
                continue;
            } else {
                # Users can define the severity of an issue before they are emailed for
                # each type of notification
                $t_min_sev_pref_field = $t_pref_field . '_min_severity';
                $t_min_sev_notify = user_pref_get_pref($t_id, $t_min_sev_pref_field);
                $t_bug_severity = bug_get_field($p_bug_id, 'severity');
                if ($t_bug_severity < $t_min_sev_notify) {
                    log_event(LOG_EMAIL_RECIPIENT, "bug={$p_bug_id}, drop {$t_id} (pref threshold)");
                    continue;
                }
            }
        }
        # check that user can see bugnotes if the last update included a bugnote
        if ($t_bug_date == $t_bugnote_date) {
            if (!access_has_bugnote_level(VIEWER, $t_bugnote_id, $t_id)) {
                log_event(LOG_EMAIL_RECIPIENT, "bug={$p_bug_id}, drop {$t_id} (access level)");
                continue;
            }
        }
        # Finally, let's get their emails, if they've set one
        $t_email = user_get_email($t_id);
        if (is_blank($t_email)) {
            log_event(LOG_EMAIL_RECIPIENT, "bug={$p_bug_id}, drop {$t_id} (no email)");
        } else {
            # @@@ we could check the emails for validity again but I think
            #   it would be too slow
            $t_final_recipients[$t_id] = $t_email;
        }
    }
    return $t_final_recipients;
}
Example #4
0
/**
 * Send notices when a bug handler is changed.
 * @param int $p_bug_id
 * @param int $p_prev_handler_id
 * @param int $p_new_handler_id
 * @return null
 */
function email_owner_changed($p_bug_id, $p_prev_handler_id, $p_new_handler_id)
{
    $t_message_id = $p_new_handler_id == NO_USER ? 'email_notification_title_for_action_bug_unassigned' : 'email_notification_title_for_action_bug_assigned';
    $t_extra_user_ids_to_email = array();
    if ($p_prev_handler_id !== NO_USER && $p_prev_handler_id != $p_new_handler_id) {
        if (email_notify_flag('owner', 'handler') == ON) {
            $t_extra_user_ids_to_email[] = $p_prev_handler_id;
        }
    }
    email_generic($p_bug_id, 'owner', $t_message_id, null, $t_extra_user_ids_to_email);
}