Skip to:
Content

BuddyPress.org

Changeset 10332


Ignore:
Timestamp:
11/11/2015 03:35:47 AM (11 years ago)
Author:
boonebgorges
Message:

Improve sanitization of email notification settings.

Port of [10329] to the 2.2 branch.

Props boonebgorges, johnjamesjacoby, imath, dcavins.
See #6707.

Location:
branches/2.2
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2/src/bp-settings/bp-settings-actions.php

    r9355 r10332  
    295295    check_admin_referer( 'bp_settings_notifications' );
    296296
    297     if ( isset( $_POST['notifications'] ) ) {
    298         foreach ( (array) $_POST['notifications'] as $key => $value ) {
    299             bp_update_user_meta( (int) bp_displayed_user_id(), $key, $value );
    300         }
    301     }
     297    bp_settings_update_notification_settings( bp_displayed_user_id(), (array) $_POST['notifications'] );
    302298
    303299    // Switch feedback for super admins
  • branches/2.2/src/bp-settings/bp-settings-functions.php

    r9351 r10332  
    1010// Exit if accessed directly
    1111defined( 'ABSPATH' ) || exit;
     12
     13/**
     14 * Update email notification settings for a specific user.
     15 *
     16 * @since 2.2.4
     17 *
     18 * @param int   $user_id  ID of the user whose settings are being updated.
     19 * @param array $settings Settings array.
     20 */
     21function bp_settings_update_notification_settings( $user_id, $settings ) {
     22    $user_id = (int) $user_id;
     23
     24    $settings = bp_settings_sanitize_notification_settings( $settings );
     25    foreach ( $settings as $setting_key => $setting_value ) {
     26        bp_update_user_meta( $user_id, $setting_key, $setting_value );
     27    }
     28}
     29
     30/**
     31 * Sanitize email notification settings as submitted by a user.
     32 *
     33 * @since 2.2.4
     34 *
     35 * @param array $settings Array of settings.
     36 * @return array Sanitized settings.
     37 */
     38function bp_settings_sanitize_notification_settings( $settings = array() ) {
     39    $sanitized_settings = array();
     40
     41    if ( empty( $settings ) ) {
     42        return $sanitized_settings;
     43    }
     44
     45    // Get registered notification keys.
     46    $registered_notification_settings = bp_settings_get_registered_notification_keys();
     47
     48    /*
     49     * We sanitize values for core notification keys.
     50     *
     51     * @todo use register_meta()
     52     */
     53    $core_notification_settings = array(
     54        'notification_messages_new_message',
     55        'notification_activity_new_mention',
     56        'notification_activity_new_reply',
     57        'notification_groups_invite',
     58        'notification_groups_group_updated',
     59        'notification_groups_admin_promotion',
     60        'notification_groups_membership_request',
     61        'notification_membership_request_completed',
     62        'notification_friends_friendship_request',
     63        'notification_friends_friendship_accepted',
     64    );
     65
     66    foreach ( (array) $settings as $key => $value ) {
     67        // Skip if not a registered setting.
     68        if ( ! in_array( $key, $registered_notification_settings, true ) ) {
     69            continue;
     70        }
     71
     72        // Force core keys to 'yes' or 'no' values.
     73        if ( in_array( $key, $core_notification_settings, true ) ) {
     74            $value = 'yes' === $value ? 'yes' : 'no';
     75        }
     76
     77        $sanitized_settings[ $key ] = $value;
     78    }
     79
     80    return $sanitized_settings;
     81}
     82
     83/**
     84 * Build a dynamic whitelist of notification keys, based on what's hooked to 'bp_notification_settings'.
     85 *
     86 * @since 2.2.4
     87 *
     88 * @return array
     89 */
     90function bp_settings_get_registered_notification_keys() {
     91
     92    ob_start();
     93    do_action( 'bp_notification_settings' );
     94    $screen = ob_get_clean();
     95
     96    $matched = preg_match_all( '/<input[^>]+name="notifications\[([^\]]+)\]/', $screen, $matches );
     97
     98    if ( $matched && isset( $matches[1] ) ) {
     99        $key_whitelist = $matches[1];
     100    } else {
     101        $key_whitelist = array();
     102    }
     103
     104    return $key_whitelist;
     105}
Note: See TracChangeset for help on using the changeset viewer.