Changeset 3735839
- Timestamp:
- 10/09/2026 03:58:16 AM (5 weeks ago)
- Location:
- push-notification-for-post-and-buddypress/tags/3.24
- Files:
-
- 3 edited
- 1 copied
-
. (copied) (copied from push-notification-for-post-and-buddypress/trunk )
-
pnfpb_push_notification.php (modified) (3 diffs)
-
public/ajax_routines/pnfpb_update_deviceid_ajax.php (modified) (3 diffs)
-
readme.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
push-notification-for-post-and-buddypress/tags/3.24/pnfpb_push_notification.php
r3717101 r3735839 4 4 Plugin URI: https://www.muraliwebworld.com/groups/wordpress-plugins-by-muralidharan-indiacitys-com-technologies/forum/topic/push-notification-for-post-and-buddypress/ 5 5 Description: Push notification for Post,custom post,BuddyPress,Woocommerce,Android/IOS mobile apps. Configure push notification settings in <a href="admin.php?page=pnfpb-icfcm-slug"><strong>settings page</strong></a> 6 Version: 3.2 36 Version: 3.24 7 7 Author: Muralidharan Ramasamy 8 8 Author URI: https://www.muraliwebworld.com … … 35 35 } 36 36 if (!defined("PNFPB_PLUGIN_VERSION")) { 37 define("PNFPB_PLUGIN_VERSION", "3.2 3");37 define("PNFPB_PLUGIN_VERSION", "3.24"); 38 38 } 39 39 if (!defined("PNFPB_URL")) { … … 2387 2387 ?> 2388 2388 <div class="notice notice-info is-dismissible pnfpb-ai-upgrade-notice"> 2389 <p><strong><?php echo esc_html__('PNFPB 3.23 version update', 'push-notification-for-post-and-buddypress'); ?></strong></p> 2390 <p><?php echo esc_html__('Firebase Notification - New feature - Token cleanup tab in admin settings to clean stale/invalid subscription tokens.', 'push-notification-for-post-and-buddypress'); ?></p> 2391 <p><?php echo esc_html__('New feature - to delay custom subscription prompt after specified number of seconds/after scrolling down to specified percentage of page.', 'push-notification-for-post-and-buddypress'); ?></p> 2392 <p><?php echo esc_html__('Ondemand selected users notification updated to send notification directly to user subscription token instead of topic.', 'push-notification-for-post-and-buddypress'); ?></p> 2389 <p><strong><?php echo esc_html__('PNFPB 3.24 version update', 'push-notification-for-post-and-buddypress'); ?></strong></p> 2390 <p><?php echo esc_html__('Security fix', 'push-notification-for-post-and-buddypress'); ?></p> 2393 2391 </div> 2394 2392 <script type="text/javascript"> -
push-notification-for-post-and-buddypress/tags/3.24/public/ajax_routines/pnfpb_update_deviceid_ajax.php
r3499865 r3735839 23 23 echo wp_json_encode(array('subscriptionstatus' => 'error', 'message' => 'Security validation failed')); 24 24 } else { 25 require __DIR__ . '/../../vendor/autoload.php'; 26 27 28 /** Get instance of class to subscribe Firebase topics based on subscription options **/ 29 $PNFPB_ajax_subscription_update_obj = new PNFPB_httpv1_subscription_option_update(); 30 31 // phpcs:ignoreFile WordPress.DB.DirectDatabaseQuery 32 33 $bpdeviceid = ''; 34 if (isset($_POST['device_id'])) { 35 /** Sanitize the device id generated by Google's Firebase for the user who subscribed push notification **/ 36 $bpdeviceid = sanitize_text_field(wp_unslash($_POST['device_id'])); 37 } 38 $bpsubscribeoptions = '10000000000'; 39 if (isset($_POST['subscriptionoptions']) && is_numeric(sanitize_text_field(wp_unslash($_POST['subscriptionoptions'])))) { 40 $bpsubscribeoptions = sanitize_text_field(wp_unslash($_POST['subscriptionoptions'])); 41 $bpsubscribeoptions = esc_html($bpsubscribeoptions); 42 } 43 if (isset($_POST['onesignal_subscriptionoptions']) && is_numeric(sanitize_text_field(wp_unslash($_POST['onesignal_subscriptionoptions'])))) { 44 $bpsubscribeoptions = sanitize_text_field(wp_unslash($_POST['onesignal_subscriptionoptions'])); 45 $bpsubscribeoptions = esc_html($bpsubscribeoptions); 46 } 47 if ($bpsubscribeoptions === '' || $bpsubscribeoptions === '10000') { 48 $bpsubscribeoptions = '10000000000'; 49 } 50 $bponesignalid = 0; 51 if (isset($_POST['onesignal_get_subscriptionoptions_id'])) { 52 $bponesignalid = sanitize_text_field(wp_unslash($_POST['onesignal_get_subscriptionoptions_id'])); 53 $bponesignalid = esc_html($bponesignalid); 54 if ($bponesignalid === '1pnfpbadm') { 55 $bponesignalid = 1; 56 } 57 if ($bponesignalid === null || ($bponesignalid !== null && !is_numeric($bponesignalid)) || $bponesignalid === '') { 58 $bponesignalid = 0; 59 } 60 } 61 if (isset($_POST['progressier_subscriptionoptions']) && sanitize_text_field(wp_unslash($_POST['progressier_subscriptionoptions']))) { 62 $bpsubscribeoptions = sanitize_text_field(wp_unslash($_POST['progressier_subscriptionoptions'])); 63 $bpsubscribeoptions = esc_html($bpsubscribeoptions); 64 if ($bpsubscribeoptions === '' || $bpsubscribeoptions === '10000') { 65 $bpsubscribeoptions = '10000000000'; 66 } 67 } 68 $bpprogressierid = 0; 69 if (isset($_POST['progressier_get_subscriptionoptions_id']) && is_numeric(sanitize_text_field(wp_unslash($_POST['progressier_get_subscriptionoptions_id'])))) { 70 $bpprogressierid = sanitize_text_field(wp_unslash($_POST['progressier_get_subscriptionoptions_id'])); 71 $bpprogressierid = esc_html($bpprogressierid); 72 if ($bpprogressierid === null || ($bpprogressierid !== null && !is_numeric($bpprogressierid)) || $bpprogressierid === '') { 73 $bpprogressierid = 0; 74 } 75 } 76 77 $bpwebtoappid = 0; 78 if (isset($_POST['webtoapp_userid']) && is_numeric(sanitize_text_field(wp_unslash($_POST['webtoapp_userid'])))) { 79 $bpwebtoappid = sanitize_text_field(wp_unslash($_POST['webtoapp_userid'])); 80 $bpwebtoappid = esc_html($bpwebtoappid); 81 if ($bpwebtoappid === null || ($bpwebtoappid !== null && !is_numeric($bpwebtoappid)) || $bpwebtoappid === '') { 82 $bpwebtoappid = 0; 83 } 84 } 85 86 $bpwebtoapp_deviceid = ''; 87 if (isset($_POST['webtoapp_deviceid'])) { 88 $bpwebtoapp_deviceid = sanitize_text_field(wp_unslash($_POST['webtoapp_deviceid'])); 89 $bpwebtoapp_deviceid = esc_html($bpwebtoapp_deviceid); 90 } 91 92 $bpnotification_id = 0; 93 if (isset($_POST['notification_id']) && is_numeric(sanitize_text_field(wp_unslash($_POST['notification_id'])))) { 94 /** Sanitize the device id generated by Google's Firebase for the user who subscribed push notification **/ 95 $bpnotification_id = sanitize_text_field(wp_unslash($_POST['notification_id'])); 96 $bpnotification_id = intval(esc_html($bpnotification_id)); 97 } 98 99 /** securing data from Firebase who subscribed push notification **/ 100 $bpdeviceid = esc_html($bpdeviceid); 25 // SECURITY FIX: Get current user ID for ownership validation 26 // Current user may be anonymous (0) for public notifications or logged-in user 27 $current_user_id = 0; 101 28 $pushtype = 'normal'; 102 global $wpdb;103 $table = $wpdb->prefix . 'pnfpb_ic_subscribed_deviceids_web';104 $dbname = $wpdb->dbname;105 29 if (isset($_POST['pushtype'])) { 106 30 $pushtype = sanitize_text_field(wp_unslash($_POST['pushtype'])); 107 31 $pushtype = esc_html($pushtype); 108 }109 110 $delivered_notification_title = '';111 $delivered_notification_content = '';112 $delivered_notification_browser = '';113 $delivered_notification_token = '';114 $delivered_notification_id = 0;115 $delivered_notification_userid = 0;116 $delivered_notification_confirmation = 0;117 $delivered_notification_opened_confirmation = 0;118 119 $pnfpb_endpoint = null;120 $pnfpb_options = null;121 $pnfpb_ipaddress = null;122 $pnfpb_subscription_auth_token = null;123 124 if (isset($_POST['pnfpb_endpoint'])) {125 $pnfpb_endpoint = sanitize_text_field($_POST['pnfpb_endpoint']);126 }127 if (isset($_POST['pnfpb_options'])) {128 $pnfpb_options = sanitize_text_field($_POST['pnfpb_options']);129 }130 if (isset($_POST['pnfpb_subscription_token'])) {131 $pnfpb_subscription_auth_token = sanitize_text_field($_POST['pnfpb_subscription_token']);132 32 } 133 if (!empty($_SERVER['HTTP_CLIENT_IP'])) { 134 $pnfpb_ipaddress = sanitize_text_field($_SERVER['HTTP_CLIENT_IP']); 135 } 136 //whether ip is from the proxy 137 elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { 138 $pnfpb_ipaddress = sanitize_text_field($_SERVER['HTTP_X_FORWARDED_FOR']); 139 } 140 //whether ip is from the remote address 141 else { 142 $pnfpb_ipaddress = sanitize_text_field($_SERVER['REMOTE_ADDR']); 143 } 144 $bpuserid = 0; 145 if ( is_user_logged_in() ) { 146 $bpuserid = get_current_user_id(); 147 } 148 149 /********************************************************************************************* 150 * Dispatcher: echo JSON response, then run Firebase topic subscription asynchronously. 151 * 152 * Priority order: 153 * 1. fastcgi_finish_request() / litespeed_finish_request() — closes HTTP connection 154 * immediately so the user gets their response while Firebase calls run in the background. 155 * 2. WP-Cron fallback — stores tasks in a transient and schedules a background event 156 * for shared-hosting environments where fcgi functions are unavailable. 157 * 158 * @param string $json_response Already-encoded JSON string to send to the client. 159 * @param array $sync_tasks One or more task arrays, each containing: 160 * 'type' => 'multiple' | 'default' 161 * 'subscribeoptions' => string (required for 'multiple') 162 * 'deviceid' => string 163 * 'pushtype' => string (optional, for 'multiple') 164 * 'old_option' => string (optional, for 'multiple') 165 * 'version' => string (optional, for 'multiple') 166 *********************************************************************************************/ 167 $pnfpb_dispatch_topic_sync = function ( $json_response, array $sync_tasks ) use ( $PNFPB_ajax_subscription_update_obj ) { 168 echo $json_response; 169 170 $has_fcgi = function_exists( 'fastcgi_finish_request' ) || function_exists( 'litespeed_finish_request' ); 171 172 if ( $has_fcgi ) { 173 // Flush output buffer and close the HTTP connection so the client gets its response immediately. 174 if ( ob_get_level() > 0 ) { 175 @ob_end_flush(); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged 33 if (!is_user_logged_in() && ($pushtype === 'webtoapp_subscribed_users' || $pushtype === 'progressier_subscribed_users')) { 34 echo wp_json_encode(array('subscriptionstatus' => 'error', 'message' => 'Login required to subscribe to notification')); 35 } else { 36 if ( is_user_logged_in() ) { 37 $current_user_id = get_current_user_id(); 38 } 39 40 require __DIR__ . '/../../vendor/autoload.php'; 41 42 43 /** Get instance of class to subscribe Firebase topics based on subscription options **/ 44 $PNFPB_ajax_subscription_update_obj = new PNFPB_httpv1_subscription_option_update(); 45 46 // phpcs:ignoreFile WordPress.DB.DirectDatabaseQuery 47 48 $bpdeviceid = ''; 49 if (isset($_POST['device_id'])) { 50 /** Sanitize the device id generated by Google's Firebase for the user who subscribed push notification **/ 51 $bpdeviceid = sanitize_text_field(wp_unslash($_POST['device_id'])); 52 } 53 $bpsubscribeoptions = '10000000000'; 54 if (isset($_POST['subscriptionoptions']) && is_numeric(sanitize_text_field(wp_unslash($_POST['subscriptionoptions'])))) { 55 $bpsubscribeoptions = sanitize_text_field(wp_unslash($_POST['subscriptionoptions'])); 56 $bpsubscribeoptions = esc_html($bpsubscribeoptions); 57 } 58 if (isset($_POST['onesignal_subscriptionoptions']) && is_numeric(sanitize_text_field(wp_unslash($_POST['onesignal_subscriptionoptions'])))) { 59 $bpsubscribeoptions = sanitize_text_field(wp_unslash($_POST['onesignal_subscriptionoptions'])); 60 $bpsubscribeoptions = esc_html($bpsubscribeoptions); 61 } 62 if ($bpsubscribeoptions === '' || $bpsubscribeoptions === '10000') { 63 $bpsubscribeoptions = '10000000000'; 64 } 65 66 if (isset($_POST['progressier_subscriptionoptions']) && sanitize_text_field(wp_unslash($_POST['progressier_subscriptionoptions']))) { 67 $bpsubscribeoptions = sanitize_text_field(wp_unslash($_POST['progressier_subscriptionoptions'])); 68 $bpsubscribeoptions = esc_html($bpsubscribeoptions); 69 if ($bpsubscribeoptions === '' || $bpsubscribeoptions === '10000') { 70 $bpsubscribeoptions = '10000000000'; 176 71 } 177 flush(); 178 if ( function_exists( 'fastcgi_finish_request' ) ) { 179 fastcgi_finish_request(); 72 } 73 74 $bpwebtoapp_deviceid = ''; 75 if (isset($_POST['webtoapp_deviceid'])) { 76 $bpwebtoapp_deviceid = sanitize_text_field(wp_unslash($_POST['webtoapp_deviceid'])); 77 $bpwebtoapp_deviceid = esc_html($bpwebtoapp_deviceid); 78 } 79 80 $bpnotification_id = 0; 81 if (isset($_POST['notification_id']) && is_numeric(sanitize_text_field(wp_unslash($_POST['notification_id'])))) { 82 /** Sanitize the device id generated by Google's Firebase for the user who subscribed push notification **/ 83 $bpnotification_id = sanitize_text_field(wp_unslash($_POST['notification_id'])); 84 $bpnotification_id = intval(esc_html($bpnotification_id)); 85 } 86 87 /** securing data from Firebase who subscribed push notification **/ 88 $bpdeviceid = esc_html($bpdeviceid); 89 90 global $wpdb; 91 $table = $wpdb->prefix . 'pnfpb_ic_subscribed_deviceids_web'; 92 $dbname = $wpdb->dbname; 93 94 $delivered_notification_title = ''; 95 $delivered_notification_content = ''; 96 $delivered_notification_browser = ''; 97 $delivered_notification_token = ''; 98 $delivered_notification_id = 0; 99 $delivered_notification_userid = 0; 100 $delivered_notification_confirmation = 0; 101 $delivered_notification_opened_confirmation = 0; 102 103 $pnfpb_endpoint = null; 104 $pnfpb_options = null; 105 $pnfpb_ipaddress = null; 106 $pnfpb_subscription_auth_token = null; 107 108 if (isset($_POST['pnfpb_endpoint'])) { 109 $pnfpb_endpoint = sanitize_text_field($_POST['pnfpb_endpoint']); 110 } 111 if (isset($_POST['pnfpb_options'])) { 112 $pnfpb_options = sanitize_text_field($_POST['pnfpb_options']); 113 } 114 if (isset($_POST['pnfpb_subscription_token'])) { 115 $pnfpb_subscription_auth_token = sanitize_text_field($_POST['pnfpb_subscription_token']); 116 } 117 if (!empty($_SERVER['HTTP_CLIENT_IP'])) { 118 $pnfpb_ipaddress = sanitize_text_field($_SERVER['HTTP_CLIENT_IP']); 119 } 120 //whether ip is from the proxy 121 elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { 122 $pnfpb_ipaddress = sanitize_text_field($_SERVER['HTTP_X_FORWARDED_FOR']); 123 } 124 //whether ip is from the remote address 125 else { 126 $pnfpb_ipaddress = sanitize_text_field($_SERVER['REMOTE_ADDR']); 127 } 128 $bpuserid = 0; 129 if ( is_user_logged_in() ) { 130 $bpuserid = get_current_user_id(); 131 } 132 133 /********************************************************************************************* 134 * Dispatcher: echo JSON response, then run Firebase topic subscription asynchronously. 135 * 136 * Priority order: 137 * 1. fastcgi_finish_request() / litespeed_finish_request() — closes HTTP connection 138 * immediately so the user gets their response while Firebase calls run in the background. 139 * 2. WP-Cron fallback — stores tasks in a transient and schedules a background event 140 * for shared-hosting environments where fcgi functions are unavailable. 141 * 142 * @param string $json_response Already-encoded JSON string to send to the client. 143 * @param array $sync_tasks One or more task arrays, each containing: 144 * 'type' => 'multiple' | 'default' 145 * 'subscribeoptions' => string (required for 'multiple') 146 * 'deviceid' => string 147 * 'pushtype' => string (optional, for 'multiple') 148 * 'old_option' => string (optional, for 'multiple') 149 * 'version' => string (optional, for 'multiple') 150 *********************************************************************************************/ 151 $pnfpb_dispatch_topic_sync = function ( $json_response, array $sync_tasks ) use ( $PNFPB_ajax_subscription_update_obj ) { 152 echo $json_response; 153 154 $has_fcgi = function_exists( 'fastcgi_finish_request' ) || function_exists( 'litespeed_finish_request' ); 155 156 if ( $has_fcgi ) { 157 // Flush output buffer and close the HTTP connection so the client gets its response immediately. 158 if ( ob_get_level() > 0 ) { 159 @ob_end_flush(); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged 160 } 161 flush(); 162 if ( function_exists( 'fastcgi_finish_request' ) ) { 163 fastcgi_finish_request(); 164 } else { 165 litespeed_finish_request(); 166 } 167 // Client has its response — run Firebase topic calls without blocking the user. 168 foreach ( $sync_tasks as $task ) { 169 if ( $task['type'] === 'multiple' ) { 170 $PNFPB_ajax_subscription_update_obj->PNFPB_httpv1_multiple_subscription_option_update( 171 $task['subscribeoptions'], 172 $task['deviceid'], 173 isset( $task['pushtype'] ) ? $task['pushtype'] : '', 174 isset( $task['old_option'] ) ? $task['old_option'] : '', 175 isset( $task['version'] ) ? $task['version'] : 'v5' 176 ); 177 } elseif ( $task['type'] === 'default' ) { 178 $PNFPB_ajax_subscription_update_obj->PNFPB_httpv1_default_subscription_option_update( $task['deviceid'] ); 179 } 180 } 180 181 } else { 181 litespeed_finish_request(); 182 // Shared-hosting fallback: append tasks to a named cumulative queue. 183 // The queue is flushed synchronously at the start of every notification dispatch, 184 // eliminating the race window between a subscription change and an FCM send. 185 $pnfpb_pending = get_transient( 'pnfpb_pending_topic_sync' ); 186 if ( ! is_array( $pnfpb_pending ) ) { 187 $pnfpb_pending = array(); 188 } 189 foreach ( $sync_tasks as $task ) { 190 $pnfpb_pending[] = $task; 191 } 192 set_transient( 'pnfpb_pending_topic_sync', $pnfpb_pending, DAY_IN_SECONDS ); 193 // Safety drain: WP-Cron event to process any tasks not yet flushed by a notification. 194 if ( ! wp_next_scheduled( 'pnfpb_async_topic_subscription', array( 'pnfpb_pending_topic_sync' ) ) ) { 195 wp_schedule_single_event( time() + 300, 'pnfpb_async_topic_subscription', array( 'pnfpb_pending_topic_sync' ) ); 196 spawn_cron(); 197 } 182 198 } 183 // Client has its response — run Firebase topic calls without blocking the user. 184 foreach ( $sync_tasks as $task ) { 185 if ( $task['type'] === 'multiple' ) { 186 $PNFPB_ajax_subscription_update_obj->PNFPB_httpv1_multiple_subscription_option_update( 187 $task['subscribeoptions'], 188 $task['deviceid'], 189 isset( $task['pushtype'] ) ? $task['pushtype'] : '', 190 isset( $task['old_option'] ) ? $task['old_option'] : '', 191 isset( $task['version'] ) ? $task['version'] : 'v5' 192 ); 193 } elseif ( $task['type'] === 'default' ) { 194 $PNFPB_ajax_subscription_update_obj->PNFPB_httpv1_default_subscription_option_update( $task['deviceid'] ); 195 } 196 } 197 } else { 198 // Shared-hosting fallback: append tasks to a named cumulative queue. 199 // The queue is flushed synchronously at the start of every notification dispatch, 200 // eliminating the race window between a subscription change and an FCM send. 201 $pnfpb_pending = get_transient( 'pnfpb_pending_topic_sync' ); 202 if ( ! is_array( $pnfpb_pending ) ) { 203 $pnfpb_pending = array(); 204 } 205 foreach ( $sync_tasks as $task ) { 206 $pnfpb_pending[] = $task; 207 } 208 set_transient( 'pnfpb_pending_topic_sync', $pnfpb_pending, DAY_IN_SECONDS ); 209 // Safety drain: WP-Cron event to process any tasks not yet flushed by a notification. 210 if ( ! wp_next_scheduled( 'pnfpb_async_topic_subscription', array( 'pnfpb_pending_topic_sync' ) ) ) { 211 wp_schedule_single_event( time() + 300, 'pnfpb_async_topic_subscription', array( 'pnfpb_pending_topic_sync' ) ); 212 spawn_cron(); 213 } 214 } 215 }; 216 217 /********************************************************************************************* 218 * Main AJAX dispatcher — routes each pushtype to its handler via switch/case. 219 *********************************************************************************************/ 220 switch ( $pushtype ) { 221 222 /*---------------------------------------------------------------------------------- 223 * Send Firebase credentials to JavaScript 224 *----------------------------------------------------------------------------------*/ 225 case 'icfirebasecred': 226 echo wp_json_encode( array( 227 'apiKey' => get_option( 'pnfpb_ic_fcm_api' ), 228 'authDomain' => get_option( 'pnfpb_ic_fcm_authdomain' ), 229 'databaseURL' => get_option( 'pnfpb_ic_fcm_databaseurl' ), 230 'projectId' => get_option( 'pnfpb_ic_fcm_projectid' ), 231 'storageBucket' => get_option( 'pnfpb_ic_fcm_storagebucket' ), 232 'messagingSenderId' => get_option( 'pnfpb_ic_fcm_messagingsenderid' ), 233 'appId' => get_option( 'pnfpb_ic_fcm_appid' ), 234 'publicKey' => get_option( 'pnfpb_ic_fcm_publickey' ), 235 'pnfpb_authsw_token' => '0', 236 ) ); 199 }; 200 201 /********************************************************************************************* 202 * Main AJAX dispatcher — routes each pushtype to its handler via switch/case. 203 *********************************************************************************************/ 204 switch ( $pushtype ) { 205 206 /*---------------------------------------------------------------------------------- 207 * Send Firebase credentials to JavaScript 208 *----------------------------------------------------------------------------------*/ 209 case 'icfirebasecred': 210 echo wp_json_encode( array( 211 'apiKey' => get_option( 'pnfpb_ic_fcm_api' ), 212 'authDomain' => get_option( 'pnfpb_ic_fcm_authdomain' ), 213 'databaseURL' => get_option( 'pnfpb_ic_fcm_databaseurl' ), 214 'projectId' => get_option( 'pnfpb_ic_fcm_projectid' ), 215 'storageBucket' => get_option( 'pnfpb_ic_fcm_storagebucket' ), 216 'messagingSenderId' => get_option( 'pnfpb_ic_fcm_messagingsenderid' ), 217 'appId' => get_option( 'pnfpb_ic_fcm_appid' ), 218 'publicKey' => get_option( 'pnfpb_ic_fcm_publickey' ), 219 'pnfpb_authsw_token' => '0', 220 ) ); 221 break; 222 223 /*---------------------------------------------------------------------------------- 224 * Send Web Push credentials to JavaScript 225 *----------------------------------------------------------------------------------*/ 226 case 'icwebpushcred': 227 echo wp_json_encode( array( 'vapidKey' => get_option( 'pnfpb_web_push_vapid_public_key' ) ) ); 228 break; 229 230 /*---------------------------------------------------------------------------------- 231 * Return notification read-receipt token for webview 232 *----------------------------------------------------------------------------------*/ 233 case 'pnfpb_read_receipt_webview': 234 if ( $bpnotification_id > 0 ) { 235 $data = array( 'notification_status' => 'failed', 'notification_token' => '' ); 236 $read_receipt_table_name = $wpdb->prefix . 'pnfpb_ic_total_statistics_notifications'; 237 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE notificationid = %d', array( $read_receipt_table_name, $bpnotification_id ) ) ); 238 foreach ( $results as $result ) { 239 $data = array( 'notification_status' => 'success', 'notification_token' => $result->notification_auth_token ); 240 } 241 echo wp_json_encode( $data ); 242 } else { 243 echo wp_json_encode( array( 'notification_status' => 'failed', 'notification_token' => '' ) ); 244 } 245 break; 246 247 /*---------------------------------------------------------------------------------- 248 * Register a WebToApp subscribed user 249 *----------------------------------------------------------------------------------*/ 250 case 'webtoapp_subscribed_users': 251 // SECURITY FIX: Prevent arbitrary user ID injection 252 // - If anonymous (current_user_id = 0): User should not specify userid in POST 253 // Use userid from POST only if it's 0 (anonymous), otherwise default to 0 254 // - If authenticated: User can only subscribe for own userid or 0 (public) 255 // - Admins: Can still manage any user's devices 256 257 if (get_option("pnfpb_webtoapp_push") === "1") { 258 $bpwebtoappid = 0; 259 if ( is_user_logged_in() ) { 260 $bpwebtoappid = get_current_user_id(); 261 } 262 263 $results = $wpdb->get_results( $wpdb->prepare( 264 'SELECT * FROM %i WHERE userid = %d AND device_id LIKE %s', 265 array( $table, $bpwebtoappid, '%' . $wpdb->esc_like( $bpwebtoapp_deviceid ) . '%' ) 266 ) ); 267 if ( count( $results ) <= 0 ) { 268 $data = array( 'userid' => $bpwebtoappid, 'device_id' => $bpwebtoapp_deviceid, 'subscription_option' => '100000000000', 'ip_address' => $pnfpb_ipaddress ); 269 $insertstatus = $wpdb->insert( $table, $data ); 270 if ( ! $insertstatus ) { 271 echo wp_json_encode( array( 'subscriptionstatus' => 'error', 'message' => $insertstatus ) ); 272 } else { 273 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'message' => $bpwebtoapp_deviceid ) ); 274 } 275 } else { 276 foreach ( $results as $result ) { 277 if ( $result->subscription_option === null || $result->subscription_option === '' ) { 278 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE userid = %d', array( $table, $bpsubscribeoptions, $bpwebtoappid ) ) ); 279 } 280 } 281 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'message' => $result->device_id ) ); 282 } 283 } else { 284 echo wp_json_encode( array( 'subscriptionstatus' => 'error', 'message' => 'Webtoapp push notification not enabled' ) ); 285 } 286 break; 287 /*---------------------------------------------------------------------------------- 288 * Register a Progressier subscribed user 289 *----------------------------------------------------------------------------------*/ 290 case 'progressier_subscribed_users': 291 // SECURITY FIX: Prevent arbitrary user ID injection 292 // - If anonymous (current_user_id = 0): User should not specify userid in POST 293 // Use userid from POST only if it's 0 (anonymous), otherwise default to 0 294 // - If authenticated: User can only subscribe for own userid or 0 (public) 295 // - Admins: Can still manage any user's devices 296 297 if (get_option("pnfpb_progressier_push") === "1" ) { 298 $progressier_externalid = 0; 299 if ( is_user_logged_in() ) { 300 $progressier_externalid = get_current_user_id(); 301 } 302 303 $results = $wpdb->get_results( $wpdb->prepare( 304 'SELECT * FROM %i WHERE userid = %d AND device_id LIKE %s', 305 array( $table, $progressier_externalid, '%' . $wpdb->esc_like( 'progressier' ) . '%' ) 306 ) ); 307 if ( count( $results ) <= 0 ) { 308 $length = 10; 309 $progressier_deviceid = substr( str_shuffle( '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' ), 1, $length ) . 'progressier@N'; 310 $data = array( 'userid' => $progressier_externalid, 'device_id' => $progressier_deviceid, 'subscription_option' => '100000000000', 'ip_address' => $pnfpb_ipaddress ); 311 $insertstatus = $wpdb->insert( $table, $data ); 312 if ( ! $insertstatus ) { 313 echo wp_json_encode( array( 'subscriptionstatus' => 'error', 'message' => $insertstatus ) ); 314 } else { 315 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'message' => $progressier_deviceid ) ); 316 } 317 } else { 318 foreach ( $results as $result ) { 319 if ( $result->subscription_option === null || $result->subscription_option === '' ) { 320 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE userid = %d', array( $table, $bpsubscribeoptions, $progressier_externalid ) ) ); 321 } 322 } 323 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'message' => $result->device_id ) ); 324 } 325 } else { 326 echo wp_json_encode( array( 'subscriptionstatus' => 'error', 'message' => 'Progressier push notification not enabled' ) ); 327 } 237 328 break; 238 329 239 /*---------------------------------------------------------------------------------- 240 * Send Web Push credentials to JavaScript 241 *----------------------------------------------------------------------------------*/ 242 case 'icwebpushcred': 243 echo wp_json_encode( array( 'vapidKey' => get_option( 'pnfpb_web_push_vapid_public_key' ) ) ); 330 /*---------------------------------------------------------------------------------- 331 * Register a OneSignal subscribed user 332 *----------------------------------------------------------------------------------*/ 333 case 'onesignal_subscribed_users': 334 // SECURITY FIX: Prevent arbitrary user ID injection 335 // - If anonymous (current_user_id = 0): User should not specify userid in POST 336 // Use userid from POST only if it's 0 (anonymous), otherwise default to 0 337 // - If authenticated: User can only subscribe for own userid or 0 (public) 338 // - Admins: Can still manage any user's devices 339 340 if (get_option("pnfpb_onesignal_push") === "1") { 341 $onesignal_externalid = 0; 342 if ( is_user_logged_in() ) { 343 $onesignal_externalid = get_current_user_id(); 344 } 345 346 $results = $wpdb->get_results( $wpdb->prepare( 347 'SELECT * FROM %i WHERE userid = %d AND device_id LIKE %s', 348 array( $table, $onesignal_externalid, '%' . $wpdb->esc_like( 'onesignal' ) . '%' ) 349 ) ); 350 if ( count( $results ) <= 0 ) { 351 $data = array( 'userid' => $onesignal_externalid, 'device_id' => 'onesignal@N', 'subscription_option' => '100000000000', 'ip_address' => $pnfpb_ipaddress ); 352 $insertstatus = $wpdb->insert( $table, $data ); 353 if ( ! $insertstatus ) { 354 echo 'error in inserting onesignal subscription details in PNFPB for id: ' . esc_html( $onesignal_externalid ); 355 } else { 356 echo ''; 357 } 358 } else { 359 foreach ( $results as $result ) { 360 if ( $result->subscription_option === null || $result->subscription_option === '' ) { 361 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE userid = %d', array( $table, $bpsubscribeoptions, $onesignal_externalid ) ) ); 362 } 363 } 364 } 365 } else { 366 echo wp_json_encode( array( 'subscriptionstatus' => 'error', 'message' => 'Onesignal push notification not enabled' ) ); 367 } 244 368 break; 245 369 246 /*---------------------------------------------------------------------------------- 247 * Return notification read-receipt token for webview 248 *----------------------------------------------------------------------------------*/ 249 case 'pnfpb_read_receipt_webview': 250 if ( $bpnotification_id > 0 ) { 251 $data = array( 'notification_status' => 'failed', 'notification_token' => '' ); 252 $read_receipt_table_name = $wpdb->prefix . 'pnfpb_ic_total_statistics_notifications'; 253 $results = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE notificationid = %d', array( $read_receipt_table_name, $bpnotification_id ) ) ); 254 foreach ( $results as $result ) { 255 $data = array( 'notification_status' => 'success', 'notification_token' => $result->notification_auth_token ); 256 } 257 echo wp_json_encode( $data ); 258 } else { 259 echo wp_json_encode( array( 'notification_status' => 'failed', 'notification_token' => '' ) ); 260 } 261 break; 262 263 /*---------------------------------------------------------------------------------- 264 * Register a WebToApp subscribed user 265 *----------------------------------------------------------------------------------*/ 266 case 'webtoapp_subscribed_users': 267 $results = $wpdb->get_results( $wpdb->prepare( 268 'SELECT * FROM %i WHERE userid = %d AND device_id LIKE %s', 269 array( $table, $bpwebtoappid, '%' . $wpdb->esc_like( $bpwebtoapp_deviceid ) . '%' ) 270 ) ); 271 if ( count( $results ) <= 0 ) { 272 $data = array( 'userid' => $bpwebtoappid, 'device_id' => $bpwebtoapp_deviceid, 'subscription_option' => '100000000000', 'ip_address' => $pnfpb_ipaddress ); 273 $insertstatus = $wpdb->insert( $table, $data ); 274 if ( ! $insertstatus ) { 275 echo wp_json_encode( array( 'subscriptionstatus' => 'error', 'message' => $insertstatus ) ); 276 } else { 277 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'message' => $bpwebtoapp_deviceid ) ); 278 } 279 } else { 280 foreach ( $results as $result ) { 281 if ( $result->subscription_option === null || $result->subscription_option === '' ) { 282 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE userid = %d', array( $table, $bpsubscribeoptions, $bpwebtoappid ) ) ); 283 } 284 } 285 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'message' => $result->device_id ) ); 286 } 287 break; 288 /*---------------------------------------------------------------------------------- 289 * Register a Progressier subscribed user 290 *----------------------------------------------------------------------------------*/ 291 case 'progressier_subscribed_users': 292 if ( isset( $_POST['progressier_external_id'] ) ) { 293 $progressier_externalid = sanitize_text_field( wp_unslash($_POST['progressier_external_id'] )); 294 $progressier_externalid = esc_html( $progressier_externalid ); 295 if ( $progressier_externalid === null || ( $progressier_externalid !== null && ! is_numeric( $progressier_externalid ) ) || $progressier_externalid === '' ) { 296 $progressier_externalid = 0; 297 } 298 $results = $wpdb->get_results( $wpdb->prepare( 299 'SELECT * FROM %i WHERE userid = %d AND device_id LIKE %s', 300 array( $table, $progressier_externalid, '%' . $wpdb->esc_like( 'progressier' ) . '%' ) 301 ) ); 302 if ( count( $results ) <= 0 ) { 303 $length = 10; 304 $progressier_deviceid = substr( str_shuffle( '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' ), 1, $length ) . 'progressier@N'; 305 $data = array( 'userid' => $progressier_externalid, 'device_id' => $progressier_deviceid, 'subscription_option' => '100000000000', 'ip_address' => $pnfpb_ipaddress ); 306 $insertstatus = $wpdb->insert( $table, $data ); 307 if ( ! $insertstatus ) { 308 echo wp_json_encode( array( 'subscriptionstatus' => 'error', 'message' => $insertstatus ) ); 309 } else { 310 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'message' => $progressier_deviceid ) ); 311 } 312 } else { 313 foreach ( $results as $result ) { 314 if ( $result->subscription_option === null || $result->subscription_option === '' ) { 315 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE userid = %d', array( $table, $bpsubscribeoptions, $progressier_externalid ) ) ); 316 } 317 } 318 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'message' => $result->device_id ) ); 319 } 320 } 321 break; 322 323 /*---------------------------------------------------------------------------------- 324 * Register a OneSignal subscribed user 325 *----------------------------------------------------------------------------------*/ 326 case 'onesignal_subscribed_users': 327 if ( isset( $_POST['onesignal_externalid'] ) ) { 328 $onesignal_externalid = sanitize_text_field(wp_unslash( $_POST['onesignal_externalid'] )); 329 $onesignal_externalid = esc_html( $onesignal_externalid ); 330 if ( $onesignal_externalid === '1pnfpbadm' ) { 331 $onesignal_externalid = 1; 332 } 333 if ( $onesignal_externalid === null || ( $onesignal_externalid !== null && ! is_numeric( $onesignal_externalid ) ) || $onesignal_externalid === '' ) { 334 $onesignal_externalid = 0; 335 } 336 $results = $wpdb->get_results( $wpdb->prepare( 337 'SELECT * FROM %i WHERE userid = %d AND device_id LIKE %s', 338 array( $table, $onesignal_externalid, '%' . $wpdb->esc_like( 'onesignal' ) . '%' ) 339 ) ); 340 if ( count( $results ) <= 0 ) { 341 $data = array( 'userid' => $onesignal_externalid, 'device_id' => 'onesignal@N', 'subscription_option' => '100000000000', 'ip_address' => $pnfpb_ipaddress ); 370 /*---------------------------------------------------------------------------------- 371 * Get OneSignal frontend subscription options 372 *----------------------------------------------------------------------------------*/ 373 case 'onesignal_get_frontend_subscriptions': 374 if (get_option("pnfpb_onesignal_push") === "1") { 375 $data = ''; 376 $bponesignalid = 0; 377 if ( is_user_logged_in() ) { 378 $bponesignalid = get_current_user_id(); 379 } 380 if ( $bponesignalid !== 0 ) { 381 $results = $wpdb->get_results( $wpdb->prepare( 382 'SELECT * FROM %i WHERE userid = %d AND device_id LIKE %s', 383 array( $table, $bponesignalid, '%' . $wpdb->esc_like( 'onesignal' ) . '%' ) 384 ) ); 385 foreach ( $results as $result ) { 386 $data = array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $result->subscription_option ); 387 } 388 echo wp_json_encode( $data ); 389 } else { 390 echo wp_json_encode( array( 'subscriptionstatus' => 'notsubscribed', 'subscriptionoptions' => '' ) ); 391 } 392 } else { 393 echo wp_json_encode( array( 'subscriptionstatus' => 'error', 'message' => 'Onesignal push notification not enabled' ) ); 394 } 395 break; 396 397 /*---------------------------------------------------------------------------------- 398 * Update OneSignal frontend subscription options 399 *----------------------------------------------------------------------------------*/ 400 case 'onesignal_frontend_subscriptions': 401 if (get_option("pnfpb_onesignal_push") === "1") { 402 if ( $bpuserid !== 0 ) { 403 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE userid = %d', array( $table, $bpsubscribeoptions, $bpuserid ) ) ); 404 } 405 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $bpsubscribeoptions ) ); 406 } else { 407 echo wp_json_encode( array( 'subscriptionstatus' => 'error', 'message' => 'Onesignal push notification not enabled' ) ); 408 } 409 break; 410 411 /*---------------------------------------------------------------------------------- 412 * Get Progressier frontend subscription options 413 *----------------------------------------------------------------------------------*/ 414 case 'progressier_get_frontend_subscriptions': 415 if (get_option("pnfpb_progressier_push") === "1" ) { 416 $data = ''; 417 $bpprogressierid = 0; 418 if ( is_user_logged_in() ) { 419 $bpprogressierid = get_current_user_id(); 420 } 421 if ( $bpprogressierid !== 0 ) { 422 $results = $wpdb->get_results( $wpdb->prepare( 423 'SELECT * FROM %i WHERE userid = %d AND device_id LIKE %s', 424 array( $table, $bpprogressierid, '%' . $wpdb->esc_like( 'progressier' ) . '%' ) 425 ) ); 426 foreach ( $results as $result ) { 427 $data = array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $result->subscription_option ); 428 } 429 echo wp_json_encode( $data ); 430 } else { 431 echo wp_json_encode( array( 'subscriptionstatus' => 'notsubscribed', 'subscriptionoptions' => '' ) ); 432 } 433 } else { 434 echo wp_json_encode( array( 'subscriptionstatus' => 'error', 'message' => 'Progressier push notification not enabled' ) ); 435 } 436 break; 437 438 /*---------------------------------------------------------------------------------- 439 * Update Progressier frontend subscription options 440 *----------------------------------------------------------------------------------*/ 441 case 'progressier_frontend_subscriptions': 442 if (get_option("pnfpb_progressier_push") === "1" ) { 443 if ( $bpuserid !== 0 ) { 444 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE userid = %d', array( $table, $bpsubscribeoptions, $bpuserid ) ) ); 445 } 446 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $bpsubscribeoptions ) ); 447 } else { 448 echo wp_json_encode( array( 'subscriptionstatus' => 'error', 'message' => 'Progressier push notification not enabled' ) ); 449 } 450 break; 451 452 /*---------------------------------------------------------------------------------- 453 * Add a new Firebase-subscribed device (first-time subscription) 454 *----------------------------------------------------------------------------------*/ 455 case 'normal': 456 $deviceid_exists = array(); 457 if ( $bpdeviceid !== '' ) { 458 $deviceid_exists = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE device_id = %s', $table, $bpdeviceid ) ); 459 } 460 if ( $pnfpb_subscription_auth_token !== '' ) { 461 $deviceid_exists = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE subscription_auth_token = %s', $table, $pnfpb_subscription_auth_token ) ); 462 } 463 foreach ( $deviceid_exists as $result ) { 464 if ( ( $result->userid == 0 || ( $result->userid != $bpuserid && $bpuserid !== 0 ) ) && $bpdeviceid !== '' ) { 465 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET userid = %d WHERE device_id = %s', $table, $bpuserid, $bpdeviceid ) ); 466 } 467 if ( ( $result->userid == 0 || ( $result->userid != $bpuserid && $bpuserid !== 0 ) ) && $pnfpb_subscription_auth_token !== '' ) { 468 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET userid = %d WHERE device_id = %s OR subscription_auth_token = %s', $table, $bpuserid, $pnfpb_subscription_auth_token ) ); 469 } 470 if ( is_null( $result->web_auth ) 471 || is_null( $result->web_256 ) 472 || is_null( $result->subscription_auth_token ) 473 || ( $result->web_auth === '' ) 474 || ( $result->web_256 === '' ) 475 || ( $result->subscription_auth_token === '' ) 476 ) { 477 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET web_auth = %s, web_256 = %s, subscription_auth_token = %s, ip_address = %s WHERE device_id = %s', $table, $pnfpb_endpoint, $pnfpb_options, $pnfpb_subscription_auth_token, $pnfpb_ipaddress, $bpdeviceid ) ); 478 } 479 } 480 if ( count( $deviceid_exists ) > 0 ) { 481 echo 'duplicate'; 482 } else { 483 // First-time registration: insert record then dispatch Firebase topic sync. 484 $data = array( 'userid' => $bpuserid, 'device_id' => $bpdeviceid, 'subscription_option' => $bpsubscribeoptions, 'web_auth' => $pnfpb_endpoint, 'web_256' => $pnfpb_options, 'subscription_auth_token' => $pnfpb_subscription_auth_token, 'ip_address' => $pnfpb_ipaddress, 'firebase_version' => 'v5' ); 342 485 $insertstatus = $wpdb->insert( $table, $data ); 343 if ( ! $insertstatus ) { 344 echo 'error in inserting onesignal subscription details in PNFPB for id: ' . esc_html( $onesignal_externalid ); 345 } else { 346 echo ''; 347 } 348 } else { 349 foreach ( $results as $result ) { 350 if ( $result->subscription_option === null || $result->subscription_option === '' ) { 351 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE userid = %d', array( $table, $bpsubscribeoptions, $onesignal_externalid ) ) ); 352 } 353 } 354 } 355 } 356 break; 357 358 /*---------------------------------------------------------------------------------- 359 * Get OneSignal frontend subscription options 360 *----------------------------------------------------------------------------------*/ 361 case 'onesignal_get_frontend_subscriptions': 362 $data = ''; 363 if ( $bponesignalid !== 0 ) { 364 $results = $wpdb->get_results( $wpdb->prepare( 365 'SELECT * FROM %i WHERE userid = %d AND device_id LIKE %s', 366 array( $table, $bponesignalid, '%' . $wpdb->esc_like( 'onesignal' ) . '%' ) 367 ) ); 368 foreach ( $results as $result ) { 369 $data = array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $result->subscription_option ); 370 } 371 echo wp_json_encode( $data ); 372 } else { 373 echo wp_json_encode( array( 'subscriptionstatus' => 'notsubscribed', 'subscriptionoptions' => '' ) ); 374 } 375 break; 376 377 /*---------------------------------------------------------------------------------- 378 * Update OneSignal frontend subscription options 379 *----------------------------------------------------------------------------------*/ 380 case 'onesignal_frontend_subscriptions': 381 if ( $bpuserid !== 0 ) { 382 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE userid = %d', array( $table, $bpsubscribeoptions, $bpuserid ) ) ); 383 } 384 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $bpsubscribeoptions ) ); 385 break; 386 387 /*---------------------------------------------------------------------------------- 388 * Get Progressier frontend subscription options 389 *----------------------------------------------------------------------------------*/ 390 case 'progressier_get_frontend_subscriptions': 391 $data = ''; 392 if ( $bpprogressierid !== 0 ) { 393 $results = $wpdb->get_results( $wpdb->prepare( 394 'SELECT * FROM %i WHERE userid = %d AND device_id LIKE %s', 395 array( $table, $bpprogressierid, '%' . $wpdb->esc_like( 'progressier' ) . '%' ) 396 ) ); 397 foreach ( $results as $result ) { 398 $data = array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $result->subscription_option ); 399 } 400 echo wp_json_encode( $data ); 401 } else { 402 echo wp_json_encode( array( 'subscriptionstatus' => 'notsubscribed', 'subscriptionoptions' => '' ) ); 403 } 404 break; 405 406 /*---------------------------------------------------------------------------------- 407 * Update Progressier frontend subscription options 408 *----------------------------------------------------------------------------------*/ 409 case 'progressier_frontend_subscriptions': 410 if ( $bpuserid !== 0 ) { 411 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE userid = %d', array( $table, $bpsubscribeoptions, $bpuserid ) ) ); 412 } 413 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $bpsubscribeoptions ) ); 414 break; 415 416 /*---------------------------------------------------------------------------------- 417 * Add a new Firebase-subscribed device (first-time subscription) 418 *----------------------------------------------------------------------------------*/ 419 case 'normal': 420 $deviceid_exists = array(); 421 if ( $bpdeviceid !== '' ) { 422 $deviceid_exists = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE device_id = %s', $table, $bpdeviceid ) ); 423 } 424 if ( $pnfpb_subscription_auth_token !== '' ) { 425 $deviceid_exists = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE subscription_auth_token = %s', $table, $pnfpb_subscription_auth_token ) ); 426 } 427 foreach ( $deviceid_exists as $result ) { 428 if ( ( $result->userid == 0 || ( $result->userid != $bpuserid && $bpuserid !== 0 ) ) && $bpdeviceid !== '' ) { 429 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET userid = %d WHERE device_id = %s', $table, $bpuserid, $bpdeviceid ) ); 430 } 431 if ( ( $result->userid == 0 || ( $result->userid != $bpuserid && $bpuserid !== 0 ) ) && $pnfpb_subscription_auth_token !== '' ) { 432 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET userid = %d WHERE device_id = %s OR subscription_auth_token = %s', $table, $bpuserid, $pnfpb_subscription_auth_token ) ); 433 } 434 if ( is_null( $result->web_auth ) 435 || is_null( $result->web_256 ) 436 || is_null( $result->subscription_auth_token ) 437 || ( $result->web_auth === '' ) 438 || ( $result->web_256 === '' ) 439 || ( $result->subscription_auth_token === '' ) 440 ) { 441 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET web_auth = %s, web_256 = %s, subscription_auth_token = %s, ip_address = %s WHERE device_id = %s', $table, $pnfpb_endpoint, $pnfpb_options, $pnfpb_subscription_auth_token, $pnfpb_ipaddress, $bpdeviceid ) ); 442 } 443 } 444 if ( count( $deviceid_exists ) > 0 ) { 445 echo 'duplicate'; 446 } else { 447 // First-time registration: insert record then dispatch Firebase topic sync. 448 $data = array( 'userid' => $bpuserid, 'device_id' => $bpdeviceid, 'subscription_option' => $bpsubscribeoptions, 'web_auth' => $pnfpb_endpoint, 'web_256' => $pnfpb_options, 'subscription_auth_token' => $pnfpb_subscription_auth_token, 'ip_address' => $pnfpb_ipaddress, 'firebase_version' => 'v5' ); 449 $insertstatus = $wpdb->insert( $table, $data ); 450 451 $json_response = ( $insertstatus > 0 ) 452 ? wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $bpsubscribeoptions ) ) 453 : wp_json_encode( array( 'subscriptionstatus' => 'fail', 'subscriptionoptions' => $bpsubscribeoptions ) ); 454 $subscription_option_array = $bpsubscribeoptions !== '' ? str_split( $bpsubscribeoptions ) : array(); 455 $sync_tasks = array(); 456 457 if ( get_option( 'pnfpb_httpv1_push' ) === '1' && $bpsubscribeoptions !== '' 458 && ( count( $subscription_option_array ) > 0 && $subscription_option_array[0] !== '1' ) 459 ) { 460 $sync_tasks[] = array( 'type' => 'multiple', 'subscribeoptions' => $bpsubscribeoptions, 'deviceid' => $bpdeviceid, 'pushtype' => '', 'old_option' => '', 'version' => 'v5' ); 486 487 $json_response = ( $insertstatus > 0 ) 488 ? wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $bpsubscribeoptions ) ) 489 : wp_json_encode( array( 'subscriptionstatus' => 'fail', 'subscriptionoptions' => $bpsubscribeoptions ) ); 490 $subscription_option_array = $bpsubscribeoptions !== '' ? str_split( $bpsubscribeoptions ) : array(); 491 $sync_tasks = array(); 492 493 if ( get_option( 'pnfpb_httpv1_push' ) === '1' && $bpsubscribeoptions !== '' 494 && ( count( $subscription_option_array ) > 0 && $subscription_option_array[0] !== '1' ) 495 ) { 496 $sync_tasks[] = array( 'type' => 'multiple', 'subscribeoptions' => $bpsubscribeoptions, 'deviceid' => $bpdeviceid, 'pushtype' => '', 'old_option' => '', 'version' => 'v5' ); 497 } 498 if ( get_option( 'pnfpb_httpv1_push' ) === '1' 499 && ( count( $subscription_option_array ) <= 0 500 || ( count( $subscription_option_array ) > 0 && $subscription_option_array[0] === '1' ) ) 501 ) { 502 $sync_tasks[] = array( 'type' => 'default', 'deviceid' => $bpdeviceid ); 503 } 504 505 if ( ! empty( $sync_tasks ) ) { 506 $pnfpb_dispatch_topic_sync( $json_response, $sync_tasks ); 507 } else { 508 echo $json_response; 509 } 510 } 511 break; 512 513 /*---------------------------------------------------------------------------------- 514 * Update subscription options from the frontend settings / custom prompt 515 *----------------------------------------------------------------------------------*/ 516 case 'subscribe-button': 517 $old_subscription_option = ''; 518 $old_subscription_option_values = $wpdb->get_col( $wpdb->prepare( 'SELECT subscription_option FROM %i WHERE device_id = %s', $table, $bpdeviceid ) ); 519 foreach ( $old_subscription_option_values as $old_subscription_option_value ) { 520 $old_subscription_option = $old_subscription_option_value; 521 } 522 523 $subscription_option_array = array(); 524 $old_subscription_option_array = array(); 525 526 if ( $bpsubscribeoptions !== '' ) { 527 $subscription_option_array = str_split( $bpsubscribeoptions ); 528 if ( get_option( 'pnfpb_ic_fcm_buddypress_comments_radio_enable' ) === '2' 529 && count( $subscription_option_array ) > 3 530 && $subscription_option_array[2] === '1' 531 ) { 532 $subscription_option_array[2] = '0'; 533 $subscription_option_array[3] = '1'; 534 $bpsubscribeoptions = implode( '', $subscription_option_array ); 535 } 536 } 537 538 if ( $old_subscription_option !== '' ) { 539 $old_subscription_option_array = str_split( $old_subscription_option ); 540 if ( get_option( 'pnfpb_ic_fcm_buddypress_comments_radio_enable' ) === '2' 541 && count( $old_subscription_option_array ) > 3 542 && $old_subscription_option_array[2] === '1' 543 ) { 544 $old_subscription_option_array[2] = '0'; 545 $old_subscription_option_array[3] = '1'; 546 $old_subscription_option = implode( '', $old_subscription_option_array ); 547 } 548 } 549 550 // Pre-build Firebase sync tasks before any output. 551 $sync_tasks = array(); 552 if ( get_option( 'pnfpb_httpv1_push' ) === '1' && $bpsubscribeoptions !== '' ) { 553 $sync_tasks[] = array( 'type' => 'multiple', 'subscribeoptions' => $bpsubscribeoptions, 'deviceid' => $bpdeviceid, 'pushtype' => '', 'old_option' => $old_subscription_option, 'version' => 'v5' ); 461 554 } 462 555 if ( get_option( 'pnfpb_httpv1_push' ) === '1' … … 467 560 } 468 561 469 if ( ! empty( $sync_tasks ) ) { 470 $pnfpb_dispatch_topic_sync( $json_response, $sync_tasks ); 471 } else { 472 echo $json_response; 473 } 474 } 475 break; 476 477 /*---------------------------------------------------------------------------------- 478 * Update subscription options from the frontend settings / custom prompt 479 *----------------------------------------------------------------------------------*/ 480 case 'subscribe-button': 481 $old_subscription_option = ''; 482 $old_subscription_option_values = $wpdb->get_col( $wpdb->prepare( 'SELECT subscription_option FROM %i WHERE device_id = %s', $table, $bpdeviceid ) ); 483 foreach ( $old_subscription_option_values as $old_subscription_option_value ) { 484 $old_subscription_option = $old_subscription_option_value; 485 } 486 487 $subscription_option_array = array(); 488 $old_subscription_option_array = array(); 489 490 if ( $bpsubscribeoptions !== '' ) { 491 $subscription_option_array = str_split( $bpsubscribeoptions ); 492 if ( get_option( 'pnfpb_ic_fcm_buddypress_comments_radio_enable' ) === '2' 493 && count( $subscription_option_array ) > 3 494 && $subscription_option_array[2] === '1' 495 ) { 496 $subscription_option_array[2] = '0'; 497 $subscription_option_array[3] = '1'; 498 $bpsubscribeoptions = implode( '', $subscription_option_array ); 499 } 500 } 501 502 if ( $old_subscription_option !== '' ) { 503 $old_subscription_option_array = str_split( $old_subscription_option ); 504 if ( get_option( 'pnfpb_ic_fcm_buddypress_comments_radio_enable' ) === '2' 505 && count( $old_subscription_option_array ) > 3 506 && $old_subscription_option_array[2] === '1' 507 ) { 508 $old_subscription_option_array[2] = '0'; 509 $old_subscription_option_array[3] = '1'; 510 $old_subscription_option = implode( '', $old_subscription_option_array ); 511 } 512 } 513 514 // Pre-build Firebase sync tasks before any output. 515 $sync_tasks = array(); 516 if ( get_option( 'pnfpb_httpv1_push' ) === '1' && $bpsubscribeoptions !== '' ) { 517 $sync_tasks[] = array( 'type' => 'multiple', 'subscribeoptions' => $bpsubscribeoptions, 'deviceid' => $bpdeviceid, 'pushtype' => '', 'old_option' => $old_subscription_option, 'version' => 'v5' ); 518 } 519 if ( get_option( 'pnfpb_httpv1_push' ) === '1' 520 && ( count( $subscription_option_array ) <= 0 521 || ( count( $subscription_option_array ) > 0 && $subscription_option_array[0] === '1' ) ) 522 ) { 523 $sync_tasks[] = array( 'type' => 'default', 'deviceid' => $bpdeviceid ); 524 } 525 526 if ( $bpdeviceid !== '' ) { 527 if ( $bpuserid !== 0 ) { 528 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE userid = %d', $table, $bpsubscribeoptions, $bpuserid ) ); 529 } else { 530 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE device_id = %s', $table, $bpsubscribeoptions, $bpdeviceid ) ); 531 } 532 $deviceid_select_status = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE device_id = %s', $table, $bpdeviceid ) ); 533 $json_response = wp_json_encode( array( 'subscriptionstatus' => 'subscribed updated', 'subscriptionoptions' => $bpsubscribeoptions ) ); 534 if ( ! empty( $sync_tasks ) ) { 535 $pnfpb_dispatch_topic_sync( $json_response, $sync_tasks ); 536 } else { 537 echo $json_response; 538 } 539 } else { 540 if ( $bpuserid !== 0 ) { 541 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE userid = %d', $table, $bpsubscribeoptions, $bpuserid ) ); 542 } else { 543 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE device_id = %s', $table, $bpsubscribeoptions, $bpdeviceid ) ); 544 } 545 $json_response = wp_json_encode( array( 'subscriptionstatus' => 'subscribed webview', 'subscriptionoptions' => $bpsubscribeoptions ) ); 546 if ( ! empty( $sync_tasks ) ) { 547 $pnfpb_dispatch_topic_sync( $json_response, $sync_tasks ); 548 } else { 549 echo $json_response; 550 } 551 } 552 break; 553 554 /*---------------------------------------------------------------------------------- 555 * Check whether a device is already subscribed (called on page load) 556 *----------------------------------------------------------------------------------*/ 557 case 'checkdeviceid': 558 $deviceid_select_status = array(); 559 if ( $bpdeviceid !== '' && $bpuserid > 0 ) { 560 /* Check if user id is present; if user just logged in, link userid to their token. */ 561 $deviceid_select_status = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE userid = %d AND device_id = %s', $table, $bpuserid, $bpdeviceid ) ); 562 if ( $deviceid_select_status != null && $deviceid_select_status->subscription_option ) { 563 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $deviceid_select_status->subscription_option ) ); 564 } else { 565 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET userid = %d, subscription_auth_token = %s WHERE device_id = %s', $table, $bpuserid, $pnfpb_subscription_auth_token, $bpdeviceid ) ); 566 if ( $deviceid_update_status <= 0 ) { 567 $data = array( 'userid' => $bpuserid, 'device_id' => $bpdeviceid, 'subscription_option' => '100000000000', 'web_auth' => $pnfpb_endpoint, 'web_256' => $pnfpb_options, 'subscription_auth_token' => $pnfpb_subscription_auth_token, 'ip_address' => $pnfpb_ipaddress, 'firebase_version' => 'v5' ); 568 $insertstatus = $wpdb->insert( $table, $data ); 569 if ( $insertstatus > 0 ) { 570 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => '100000000000' ) ); 562 if ( $bpdeviceid !== '' ) { 563 if ( $bpuserid !== 0 ) { 564 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE userid = %d', $table, $bpsubscribeoptions, $bpuserid ) ); 565 } else { 566 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE device_id = %s', $table, $bpsubscribeoptions, $bpdeviceid ) ); 567 } 568 $deviceid_select_status = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE device_id = %s', $table, $bpdeviceid ) ); 569 $json_response = wp_json_encode( array( 'subscriptionstatus' => 'subscribed updated', 'subscriptionoptions' => $bpsubscribeoptions ) ); 570 if ( ! empty( $sync_tasks ) ) { 571 $pnfpb_dispatch_topic_sync( $json_response, $sync_tasks ); 572 } else { 573 echo $json_response; 574 } 575 } else { 576 if ( $bpuserid !== 0 ) { 577 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE userid = %d', $table, $bpsubscribeoptions, $bpuserid ) ); 578 } else { 579 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE device_id = %s', $table, $bpsubscribeoptions, $bpdeviceid ) ); 580 } 581 $json_response = wp_json_encode( array( 'subscriptionstatus' => 'subscribed webview', 'subscriptionoptions' => $bpsubscribeoptions ) ); 582 if ( ! empty( $sync_tasks ) ) { 583 $pnfpb_dispatch_topic_sync( $json_response, $sync_tasks ); 584 } else { 585 echo $json_response; 586 } 587 } 588 break; 589 590 /*---------------------------------------------------------------------------------- 591 * Check whether a device is already subscribed (called on page load) 592 *----------------------------------------------------------------------------------*/ 593 case 'checkdeviceid': 594 $deviceid_select_status = array(); 595 if ( $bpdeviceid !== '' && $bpuserid > 0 ) { 596 /* Check if user id is present; if user just logged in, link userid to their token. */ 597 $deviceid_select_status = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE userid = %d AND device_id = %s', $table, $bpuserid, $bpdeviceid ) ); 598 if ( $deviceid_select_status != null && $deviceid_select_status->subscription_option ) { 599 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $deviceid_select_status->subscription_option ) ); 600 } else { 601 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET userid = %d, subscription_auth_token = %s WHERE device_id = %s', $table, $bpuserid, $pnfpb_subscription_auth_token, $bpdeviceid ) ); 602 if ( $deviceid_update_status <= 0 ) { 603 $data = array( 'userid' => $bpuserid, 'device_id' => $bpdeviceid, 'subscription_option' => '100000000000', 'web_auth' => $pnfpb_endpoint, 'web_256' => $pnfpb_options, 'subscription_auth_token' => $pnfpb_subscription_auth_token, 'ip_address' => $pnfpb_ipaddress, 'firebase_version' => 'v5' ); 604 $insertstatus = $wpdb->insert( $table, $data ); 605 if ( $insertstatus > 0 ) { 606 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => '100000000000' ) ); 607 } else { 608 echo wp_json_encode( array( 'subscriptionstatus' => 'not-subscribed', 'subscriptionoptions' => '100000000000' ) ); 609 } 571 610 } else { 572 echo wp_json_encode( array( 'subscriptionstatus' => 'not-subscribed', 'subscriptionoptions' => '100000000000' ) ); 573 } 574 } else { 575 if ( $deviceid_select_status != null && $deviceid_select_status->subscription_option ) { 576 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $deviceid_select_status->subscription_option ) ); 577 } else { 578 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => '100000000000000000000' ) ); 579 } 580 } 581 } 582 } else { 583 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => '100000000000000000000' ) ); 584 } 585 break; 586 587 /*---------------------------------------------------------------------------------- 588 * Check subscription status for a specific BuddyPress group 589 *----------------------------------------------------------------------------------*/ 590 case 'checkdeviceidforgroup': 591 if ( $bpdeviceid !== '' || $pnfpb_endpoint !== '' ) { 592 $bpgroupid = sanitize_text_field( wp_unslash( $_POST['bpgroup_id'] ) ); 593 $bpgroupdeviceid = $bpdeviceid . '!!' . $bpgroupid . '!!'; 594 $deviceid_select_status = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE web_auth = %s OR device_id = %s AND userid = %d', $table, $pnfb_endpoint, $bpgroupdeviceid, $bpuserid ) ); 595 $subscriptionoptions = '000000000000'; 596 if ( count( $deviceid_select_status ) > 0 ) { 597 $version_value = 'L'; 611 if ( $deviceid_select_status != null && $deviceid_select_status->subscription_option ) { 612 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $deviceid_select_status->subscription_option ) ); 613 } else { 614 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => '100000000000000000000' ) ); 615 } 616 } 617 } 618 } else { 619 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => '100000000000000000000' ) ); 620 } 621 break; 622 623 /*---------------------------------------------------------------------------------- 624 * Check subscription status for a specific BuddyPress group 625 *----------------------------------------------------------------------------------*/ 626 case 'checkdeviceidforgroup': 627 if ( $bpdeviceid !== '' || $pnfpb_endpoint !== '' ) { 628 $bpgroupid = sanitize_text_field( wp_unslash( $_POST['bpgroup_id'] ) ); 629 $bpgroupdeviceid = $bpdeviceid . '!!' . $bpgroupid . '!!'; 630 $deviceid_select_status = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE web_auth = %s OR device_id = %s AND userid = %d', $table, $pnfb_endpoint, $bpgroupdeviceid, $bpuserid ) ); 631 $subscriptionoptions = '000000000000'; 632 if ( count( $deviceid_select_status ) > 0 ) { 633 $version_value = 'L'; 634 foreach ( $deviceid_select_status as $result ) { 635 $version_value = $result->firebase_version; 636 $subscriptionoptions = $result->subscription_option; 637 if ( $subscriptionoptions === '10000' || $subscriptionoptions === '' || is_null( $subscriptionoptions ) ) { 638 $subscriptionoptions = '100000000000'; 639 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE web_auth = %s OR device_id = %s AND userid = %d', $table, $subscriptionoptions, $pnfb_endpoint, $bpgroupdeviceid, $bpuserid ) ); 640 } 641 $webpush_option = get_option( 'pnfpb_webpush_push' ); 642 $webpush_firebase = get_option( 'pnfpb_webpush_push_firebase' ); 643 if ( $bpdeviceid !== $result->device_id && $webpush_option !== '1' && $webpush_option !== '2' && $webpush_firebase !== '1' && get_option( 'pnfpb_httpv1_push' ) === '1' ) { 644 $group_name = groups_get_slug( $bpgroupid ); 645 $client = new Google_Client(); 646 $client->useApplicationDefaultCredentials(); 647 $configArray = json_decode( get_option( 'pnfpb_sa_json_data' ), true ); 648 $client->setAuthConfig( $configArray ); 649 $client->addScope( 'https://www.googleapis.com/auth/firebase.messaging' ); 650 $client->refreshTokenWithAssertion(); 651 $pnfpb_fbauth_token_array = $client->getAccessToken(); 652 $pnfpb_fbauth_token = $pnfpb_fbauth_token_array['access_token']; 653 update_option( 'pnfpb_firebase_oauth_token', $pnfpb_fbauth_token ); 654 $url = 'https://iid.googleapis.com/iid/v1:batchAdd'; 655 $headers = array( 'Authorization' => 'Bearer ' . $pnfpb_fbauth_token, 'Content-Type' => 'application/json', 'access_token_auth' => 'true' ); 656 $grouptopic = '/topics/' . $group_name; 657 $fields = array( 'to' => $grouptopic, 'registration_tokens' => array( $bpdeviceid ) ); 658 $body = wp_json_encode( $fields ); 659 $args = array( 'httpversion' => '1.0', 'blocking' => false, 'body' => $body, 'headers' => $headers ); 660 $apiresults = wp_remote_post( $url, $args ); 661 $deviceid_version_update_status = $wpdb->query( $wpdb->prepare( "UPDATE %i SET firebase_version = %s, WHERE device_id = %s AND userid = %d", $table, 'v5', $bpdeviceid . '!!' . $bpgroupid . '!!' . $group_device_identifier_array[1], $bpuserid ) ); 662 } 663 } 664 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $subscriptionoptions ) ); 665 } else { 666 echo wp_json_encode( array( 'subscriptionstatus' => 'not-subscribed', 'subscriptionoptions' => $subscriptionoptions ) ); 667 } 668 } 669 break; 670 671 /*---------------------------------------------------------------------------------- 672 * Subscribe to BuddyPress group push notifications 673 *----------------------------------------------------------------------------------*/ 674 case 'subscribe-group-button': 675 if ( $bpdeviceid !== '' || $pnfpb_endpoint !== '' ) { 676 $bpgroupid = sanitize_text_field( wp_unslash( $_POST['bpgroup_id'] ) ); 677 $cookievalue = ''; 678 if ( isset( $_COOKIE[ 'pnfpb_group_push_notification_' . $bpgroupid ] ) ) { 679 $cookievalue = $_COOKIE[ 'pnfpb_group_push_notification_' . $bpgroupid ]; 680 } 681 $bpnewdeviceid = $bpdeviceid . '!!' . $bpgroupid . '!!' . $cookievalue; 682 $deviceid_select_status = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE device_id = %s OR web_auth = %s', $table, $bpdeviceid, $pnfpb_endpoint ) ); 683 $subscriptionoptions = '000000000000'; 598 684 foreach ( $deviceid_select_status as $result ) { 599 $version_value = $result->firebase_version;600 685 $subscriptionoptions = $result->subscription_option; 601 if ( $subscriptionoptions === '10000' || $subscriptionoptions === '' || is_null( $subscriptionoptions ) ) { 602 $subscriptionoptions = '100000000000'; 603 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET subscription_option = %s WHERE web_auth = %s OR device_id = %s AND userid = %d', $table, $subscriptionoptions, $pnfb_endpoint, $bpgroupdeviceid, $bpuserid ) ); 604 } 605 $webpush_option = get_option( 'pnfpb_webpush_push' ); 606 $webpush_firebase = get_option( 'pnfpb_webpush_push_firebase' ); 607 if ( $bpdeviceid !== $result->device_id && $webpush_option !== '1' && $webpush_option !== '2' && $webpush_firebase !== '1' && get_option( 'pnfpb_httpv1_push' ) === '1' ) { 608 $group_name = groups_get_slug( $bpgroupid ); 609 $client = new Google_Client(); 686 } 687 $uniqueid = uniqid(); 688 setcookie( 'pnfpb_group_push_notification_' . $bpgroupid, $uniqueid, time() + ( 86400 * 30 ), '/' ); 689 $bpnewdeviceid = $bpdeviceid . '!!' . $bpgroupid . '!!' . $uniqueid; 690 $data = array( 'userid' => $bpuserid, 'device_id' => $bpnewdeviceid, 'subscription_option' => $subscriptionoptions, 'web_auth' => $pnfpb_endpoint, 'web_256' => $pnfpb_options, 'subscription_auth_token' => $pnfpb_subscription_auth_token, 'ip_address' => $pnfpb_ipaddress, 'firebase_version' => 'v5' ); 691 $insertstatus = $wpdb->insert( $table, $data ); 692 if ( $insertstatus > 0 ) { 693 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $subscriptionoptions ) ); 694 } else { 695 echo wp_json_encode( array( 'subscriptionstatus' => 'deleted' ) ); 696 } 697 if ( get_option( 'pnfpb_httpv1_push' ) === '1' ) { 698 $group_name = 'pnfpbgroupid' . $bpgroupid; 699 $pnfpb_oauth_timestamp = get_option( 'pnfpb_firebase_oauth_timestamp' ); 700 $pnfpb_fbauth_token = get_option( 'pnfpb_firebase_oauth_token' ); 701 $one_hour_ago_timestamp = strtotime( '-1 hour' ); 702 if ( $pnfpb_fbauth_token === false || $pnfpb_fbauth_token === '' 703 || $pnfpb_oauth_timestamp === false || $pnfpb_oauth_timestamp === '' 704 || ! is_numeric( $pnfpb_oauth_timestamp ) 705 || ( is_numeric( $pnfpb_oauth_timestamp ) && $pnfpb_oauth_timestamp < $one_hour_ago_timestamp ) 706 ) { 707 $client = new Google_Client(); 610 708 $client->useApplicationDefaultCredentials(); 611 709 $configArray = json_decode( get_option( 'pnfpb_sa_json_data' ), true ); … … 616 714 $pnfpb_fbauth_token = $pnfpb_fbauth_token_array['access_token']; 617 715 update_option( 'pnfpb_firebase_oauth_token', $pnfpb_fbauth_token ); 618 $url = 'https://iid.googleapis.com/iid/v1:batchAdd'; 619 $headers = array( 'Authorization' => 'Bearer ' . $pnfpb_fbauth_token, 'Content-Type' => 'application/json', 'access_token_auth' => 'true' ); 620 $grouptopic = '/topics/' . $group_name; 621 $fields = array( 'to' => $grouptopic, 'registration_tokens' => array( $bpdeviceid ) ); 622 $body = wp_json_encode( $fields ); 623 $args = array( 'httpversion' => '1.0', 'blocking' => false, 'body' => $body, 'headers' => $headers ); 624 $apiresults = wp_remote_post( $url, $args ); 625 $deviceid_version_update_status = $wpdb->query( $wpdb->prepare( "UPDATE %i SET firebase_version = %s, WHERE device_id = %s AND userid = %d", $table, 'v5', $bpdeviceid . '!!' . $bpgroupid . '!!' . $group_device_identifier_array[1], $bpuserid ) ); 626 } 627 } 628 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $subscriptionoptions ) ); 629 } else { 630 echo wp_json_encode( array( 'subscriptionstatus' => 'not-subscribed', 'subscriptionoptions' => $subscriptionoptions ) ); 631 } 632 } 633 break; 634 635 /*---------------------------------------------------------------------------------- 636 * Subscribe to BuddyPress group push notifications 637 *----------------------------------------------------------------------------------*/ 638 case 'subscribe-group-button': 639 if ( $bpdeviceid !== '' || $pnfpb_endpoint !== '' ) { 640 $bpgroupid = sanitize_text_field( wp_unslash( $_POST['bpgroup_id'] ) ); 641 $cookievalue = ''; 642 if ( isset( $_COOKIE[ 'pnfpb_group_push_notification_' . $bpgroupid ] ) ) { 643 $cookievalue = $_COOKIE[ 'pnfpb_group_push_notification_' . $bpgroupid ]; 644 } 645 $bpnewdeviceid = $bpdeviceid . '!!' . $bpgroupid . '!!' . $cookievalue; 646 $deviceid_select_status = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE device_id = %s OR web_auth = %s', $table, $bpdeviceid, $pnfpb_endpoint ) ); 647 $subscriptionoptions = '000000000000'; 648 foreach ( $deviceid_select_status as $result ) { 649 $subscriptionoptions = $result->subscription_option; 650 } 651 $uniqueid = uniqid(); 652 setcookie( 'pnfpb_group_push_notification_' . $bpgroupid, $uniqueid, time() + ( 86400 * 30 ), '/' ); 653 $bpnewdeviceid = $bpdeviceid . '!!' . $bpgroupid . '!!' . $uniqueid; 654 $data = array( 'userid' => $bpuserid, 'device_id' => $bpnewdeviceid, 'subscription_option' => $subscriptionoptions, 'web_auth' => $pnfpb_endpoint, 'web_256' => $pnfpb_options, 'subscription_auth_token' => $pnfpb_subscription_auth_token, 'ip_address' => $pnfpb_ipaddress, 'firebase_version' => 'v5' ); 655 $insertstatus = $wpdb->insert( $table, $data ); 656 if ( $insertstatus > 0 ) { 657 echo wp_json_encode( array( 'subscriptionstatus' => 'subscribed', 'subscriptionoptions' => $subscriptionoptions ) ); 658 } else { 659 echo wp_json_encode( array( 'subscriptionstatus' => 'deleted' ) ); 660 } 661 if ( get_option( 'pnfpb_httpv1_push' ) === '1' ) { 662 $group_name = 'pnfpbgroupid' . $bpgroupid; 663 $pnfpb_oauth_timestamp = get_option( 'pnfpb_firebase_oauth_timestamp' ); 664 $pnfpb_fbauth_token = get_option( 'pnfpb_firebase_oauth_token' ); 665 $one_hour_ago_timestamp = strtotime( '-1 hour' ); 666 if ( $pnfpb_fbauth_token === false || $pnfpb_fbauth_token === '' 667 || $pnfpb_oauth_timestamp === false || $pnfpb_oauth_timestamp === '' 668 || ! is_numeric( $pnfpb_oauth_timestamp ) 669 || ( is_numeric( $pnfpb_oauth_timestamp ) && $pnfpb_oauth_timestamp < $one_hour_ago_timestamp ) 670 ) { 671 $client = new Google_Client(); 672 $client->useApplicationDefaultCredentials(); 673 $configArray = json_decode( get_option( 'pnfpb_sa_json_data' ), true ); 674 $client->setAuthConfig( $configArray ); 675 $client->addScope( 'https://www.googleapis.com/auth/firebase.messaging' ); 676 $client->refreshTokenWithAssertion(); 677 $pnfpb_fbauth_token_array = $client->getAccessToken(); 678 $pnfpb_fbauth_token = $pnfpb_fbauth_token_array['access_token']; 679 update_option( 'pnfpb_firebase_oauth_token', $pnfpb_fbauth_token ); 680 update_option( 'pnfpb_firebase_oauth_timestamp', strtotime( 'now' ) ); 681 } 682 $url = 'https://iid.googleapis.com/iid/v1:batchAdd'; 683 $headers = array( 'Authorization' => 'Bearer ' . $pnfpb_fbauth_token, 'Content-Type' => 'application/json', 'access_token_auth' => 'true' ); 684 $grouptopic = '/topics/' . $group_name; 685 $fields = array( 'to' => $grouptopic, 'registration_tokens' => array( $bpdeviceid ) ); 686 $body = wp_json_encode( $fields ); 687 $args = array( 'httpversion' => '1.0', 'blocking' => false, 'body' => $body, 'headers' => $headers ); 688 $apiresults = wp_remote_post( $url, $args ); 689 } 690 } 691 break; 692 693 /*---------------------------------------------------------------------------------- 694 * Unsubscribe from BuddyPress group push notifications 695 *----------------------------------------------------------------------------------*/ 696 case 'unsubscribe-group-button': 697 if ( $bpdeviceid !== '' || $pnfpb_endpoint !== '' ) { 698 $bpgroupid = sanitize_text_field( wp_unslash( $_POST['bpgroup_id'] ) ); 699 $cookievalue = ''; 700 if ( isset( $_COOKIE[ 'pnfpb_group_push_notification_' . $bpgroupid ] ) ) { 701 $cookievalue = $_COOKIE[ 'pnfpb_group_push_notification_' . $bpgroupid ]; 702 } 716 update_option( 'pnfpb_firebase_oauth_timestamp', strtotime( 'now' ) ); 717 } 718 $url = 'https://iid.googleapis.com/iid/v1:batchAdd'; 719 $headers = array( 'Authorization' => 'Bearer ' . $pnfpb_fbauth_token, 'Content-Type' => 'application/json', 'access_token_auth' => 'true' ); 720 $grouptopic = '/topics/' . $group_name; 721 $fields = array( 'to' => $grouptopic, 'registration_tokens' => array( $bpdeviceid ) ); 722 $body = wp_json_encode( $fields ); 723 $args = array( 'httpversion' => '1.0', 'blocking' => false, 'body' => $body, 'headers' => $headers ); 724 $apiresults = wp_remote_post( $url, $args ); 725 } 726 } 727 break; 728 729 /*---------------------------------------------------------------------------------- 730 * Unsubscribe from BuddyPress group push notifications 731 *----------------------------------------------------------------------------------*/ 732 case 'unsubscribe-group-button': 733 if ( $bpdeviceid !== '' || $pnfpb_endpoint !== '' ) { 734 $bpgroupid = sanitize_text_field( wp_unslash( $_POST['bpgroup_id'] ) ); 735 $cookievalue = ''; 736 if ( isset( $_COOKIE[ 'pnfpb_group_push_notification_' . $bpgroupid ] ) ) { 737 $cookievalue = $_COOKIE[ 'pnfpb_group_push_notification_' . $bpgroupid ]; 738 } 739 $deviceid_update_status = 0; 740 if ( isset( $bpdeviceid ) && $bpdeviceid !== '' ) { 741 $bpolddeviceid = $bpdeviceid . '!!' . $bpgroupid . '!!' . $cookievalue; 742 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'DELETE from %i WHERE userid = %d AND device_id = %s', $table, $bpuserid, $bpolddeviceid ) ); 743 } elseif ( isset( $pnfpb_endpoint ) && $pnfpb_endpoint !== '' ) { 744 $bpolddeviceid = '!!' . $bpgroupid . '!!' . $cookievalue; 745 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'DELETE from %i WHERE userid = %d AND web_auth = %s AND device_id = %s', $table, $bpuserid, $pnfpb_endpoint, $bpolddeviceid ) ); 746 } 747 if ( get_option( 'pnfpb_httpv1_push' ) === '1' ) { 748 $group_name = 'pnfpbgroupid' . $bpgroupid; 749 $pnfpb_oauth_timestamp = get_option( 'pnfpb_firebase_oauth_timestamp' ); 750 $pnfpb_fbauth_token = get_option( 'pnfpb_firebase_oauth_token' ); 751 $one_hour_ago_timestamp = strtotime( '-1 hour' ); 752 if ( $pnfpb_fbauth_token === false || $pnfpb_fbauth_token === '' 753 || $pnfpb_oauth_timestamp === false || $pnfpb_oauth_timestamp === '' 754 || ! is_numeric( $pnfpb_oauth_timestamp ) 755 || ( is_numeric( $pnfpb_oauth_timestamp ) && $pnfpb_oauth_timestamp < $one_hour_ago_timestamp ) 756 ) { 757 $client = new Google_Client(); 758 $client->useApplicationDefaultCredentials(); 759 $configArray = json_decode( get_option( 'pnfpb_sa_json_data' ), true ); 760 $client->setAuthConfig( $configArray ); 761 $client->addScope( 'https://www.googleapis.com/auth/firebase.messaging' ); 762 $client->refreshTokenWithAssertion(); 763 $pnfpb_fbauth_token_array = $client->getAccessToken(); 764 $pnfpb_fbauth_token = $pnfpb_fbauth_token_array['access_token']; 765 update_option( 'pnfpb_firebase_oauth_token', $pnfpb_fbauth_token ); 766 update_option( 'pnfpb_firebase_oauth_timestamp', strtotime( 'now' ) ); 767 } 768 $url = 'https://iid.googleapis.com/iid/v1:batchRemove'; 769 $headers = array( 'Authorization' => 'Bearer ' . $pnfpb_fbauth_token, 'Content-Type' => 'application/json', 'access_token_auth' => 'true' ); 770 $grouptopic = '/topics/' . $group_name; 771 $fields = array( 'to' => $grouptopic, 'registration_tokens' => array( $bpdeviceid ) ); 772 $body = wp_json_encode( $fields ); 773 $args = array( 'httpversion' => '1.0', 'blocking' => false, 'body' => $body, 'headers' => $headers ); 774 $apiresults = wp_remote_post( $url, $args ); 775 } 776 if ( $deviceid_update_status > 0 ) { 777 echo wp_json_encode( array( 'subscriptionstatus' => 'deleted' ) ); 778 } else { 779 echo wp_json_encode( array( 'subscriptionstatus' => 'failed in unsubscribe button' ) ); 780 } 781 } 782 break; 783 784 /*---------------------------------------------------------------------------------- 785 * Delete a push notification token (user unsubscribed entirely) 786 *----------------------------------------------------------------------------------*/ 787 case 'deletepushtoken': 703 788 $deviceid_update_status = 0; 704 789 if ( isset( $bpdeviceid ) && $bpdeviceid !== '' ) { 705 $bpolddeviceid = $bpdeviceid . '!!' . $bpgroupid . '!!' . $cookievalue; 706 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'DELETE from %i WHERE userid = %d AND device_id = %s', $table, $bpuserid, $bpolddeviceid ) ); 790 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'DELETE from %i WHERE device_id = %s', $table, $bpdeviceid ) ); 791 if ( $deviceid_update_status > 0 ) { 792 echo wp_json_encode( array( 'subscriptionstatus' => 'deleted', 'deviceidupdatestauts' => $deviceid_update_status, 'deviceid' => $bpdeviceid ) ); 793 } else { 794 echo wp_json_encode( array( 'subscriptionstatus' => 'failed in deleting push token', 'deviceidupdatestauts' => $deviceid_update_status, 'deviceid' => $bpdeviceid ) ); 795 } 707 796 } elseif ( isset( $pnfpb_endpoint ) && $pnfpb_endpoint !== '' ) { 708 $bpolddeviceid = '!!' . $bpgroupid . '!!' . $cookievalue; 709 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'DELETE from %i WHERE userid = %d AND web_auth = %s AND device_id = %s', $table, $bpuserid, $pnfpb_endpoint, $bpolddeviceid ) ); 710 } 711 if ( get_option( 'pnfpb_httpv1_push' ) === '1' ) { 712 $group_name = 'pnfpbgroupid' . $bpgroupid; 713 $pnfpb_oauth_timestamp = get_option( 'pnfpb_firebase_oauth_timestamp' ); 714 $pnfpb_fbauth_token = get_option( 'pnfpb_firebase_oauth_token' ); 715 $one_hour_ago_timestamp = strtotime( '-1 hour' ); 716 if ( $pnfpb_fbauth_token === false || $pnfpb_fbauth_token === '' 717 || $pnfpb_oauth_timestamp === false || $pnfpb_oauth_timestamp === '' 718 || ! is_numeric( $pnfpb_oauth_timestamp ) 719 || ( is_numeric( $pnfpb_oauth_timestamp ) && $pnfpb_oauth_timestamp < $one_hour_ago_timestamp ) 720 ) { 721 $client = new Google_Client(); 722 $client->useApplicationDefaultCredentials(); 723 $configArray = json_decode( get_option( 'pnfpb_sa_json_data' ), true ); 724 $client->setAuthConfig( $configArray ); 725 $client->addScope( 'https://www.googleapis.com/auth/firebase.messaging' ); 726 $client->refreshTokenWithAssertion(); 727 $pnfpb_fbauth_token_array = $client->getAccessToken(); 728 $pnfpb_fbauth_token = $pnfpb_fbauth_token_array['access_token']; 729 update_option( 'pnfpb_firebase_oauth_token', $pnfpb_fbauth_token ); 730 update_option( 'pnfpb_firebase_oauth_timestamp', strtotime( 'now' ) ); 731 } 732 $url = 'https://iid.googleapis.com/iid/v1:batchRemove'; 733 $headers = array( 'Authorization' => 'Bearer ' . $pnfpb_fbauth_token, 'Content-Type' => 'application/json', 'access_token_auth' => 'true' ); 734 $grouptopic = '/topics/' . $group_name; 735 $fields = array( 'to' => $grouptopic, 'registration_tokens' => array( $bpdeviceid ) ); 736 $body = wp_json_encode( $fields ); 737 $args = array( 'httpversion' => '1.0', 'blocking' => false, 'body' => $body, 'headers' => $headers ); 738 $apiresults = wp_remote_post( $url, $args ); 739 } 740 if ( $deviceid_update_status > 0 ) { 741 echo wp_json_encode( array( 'subscriptionstatus' => 'deleted' ) ); 742 } else { 743 echo wp_json_encode( array( 'subscriptionstatus' => 'failed in unsubscribe button' ) ); 744 } 745 } 746 break; 747 748 /*---------------------------------------------------------------------------------- 749 * Delete a push notification token (user unsubscribed entirely) 750 *----------------------------------------------------------------------------------*/ 751 case 'deletepushtoken': 752 $deviceid_update_status = 0; 753 if ( isset( $bpdeviceid ) && $bpdeviceid !== '' ) { 754 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'DELETE from %i WHERE device_id = %s', $table, $bpdeviceid ) ); 755 if ( $deviceid_update_status > 0 ) { 756 echo wp_json_encode( array( 'subscriptionstatus' => 'deleted', 'deviceidupdatestauts' => $deviceid_update_status, 'deviceid' => $bpdeviceid ) ); 757 } else { 758 echo wp_json_encode( array( 'subscriptionstatus' => 'failed in deleting push token', 'deviceidupdatestauts' => $deviceid_update_status, 'deviceid' => $bpdeviceid ) ); 759 } 760 } elseif ( isset( $pnfpb_endpoint ) && $pnfpb_endpoint !== '' ) { 761 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'DELETE from %i WHERE web_auth = %s', $table, $pnfpb_endpoint ) ); 762 if ( $deviceid_update_status > 0 ) { 763 echo wp_json_encode( array( 'subscriptionstatus' => 'deleted', 'deviceidupdatestauts' => $deviceid_update_status, 'deviceid' => $bpdeviceid ) ); 764 } else { 765 echo wp_json_encode( array( 'subscriptionstatus' => 'failed in deleting push token', 'deviceidupdatestauts' => $deviceid_update_status, 'deviceid' => $bpdeviceid ) ); 766 } 767 } 768 break; 769 770 /*---------------------------------------------------------------------------------- 771 * Unknown or unhandled pushtype 772 *----------------------------------------------------------------------------------*/ 773 default: 774 echo wp_json_encode( array( 'subscriptionstatus' => 'failed' ) ); 775 break; 797 $deviceid_update_status = $wpdb->query( $wpdb->prepare( 'DELETE from %i WHERE web_auth = %s', $table, $pnfpb_endpoint ) ); 798 if ( $deviceid_update_status > 0 ) { 799 echo wp_json_encode( array( 'subscriptionstatus' => 'deleted', 'deviceidupdatestauts' => $deviceid_update_status, 'deviceid' => $bpdeviceid ) ); 800 } else { 801 echo wp_json_encode( array( 'subscriptionstatus' => 'failed in deleting push token', 'deviceidupdatestauts' => $deviceid_update_status, 'deviceid' => $bpdeviceid ) ); 802 } 803 } 804 break; 805 806 /*---------------------------------------------------------------------------------- 807 * Unknown or unhandled pushtype 808 *----------------------------------------------------------------------------------*/ 809 default: 810 echo wp_json_encode( array( 'subscriptionstatus' => 'failed' ) ); 811 break; 812 } 776 813 } 777 814 } -
push-notification-for-post-and-buddypress/tags/3.24/readme.txt
r3717101 r3735839 7 7 Tested up to: 7.1 8 8 Requires PHP: 8.1 9 Stable tag: 3.2 39 Stable tag: 3.24 10 10 License: GPLv2 or later 11 11 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 235 235 236 236 == Changelog == 237 = 3.24 version = 238 * Security fix 237 239 = 3.23 version = 238 240 * New feature - Token cleanup tab in admin settings to clean stale/invalid subscription tokens. … … 241 243 242 244 == Upgrade Notice == 243 * New Token cleanup tab in admin settings to clean stale/invalid subscription tokens.245 * Security fix
Note:
See TracChangeset
for help on using the changeset viewer.