Admin
4 years ago
Builder
4 years ago
Helpers
4 years ago
CFF_Autolink.php
4 years ago
CFF_Blocks.php
4 years ago
CFF_Cache.php
4 years ago
CFF_Education.php
4 years ago
CFF_Elementor_Base.php
4 years ago
CFF_Elementor_Widget.php
4 years ago
CFF_Error_Reporter.php
4 years ago
CFF_FB_Settings.php
4 years ago
CFF_Feed_Elementor_Control.php
4 years ago
CFF_Feed_Locator.php
4 years ago
CFF_Feed_Pro.php
4 years ago
CFF_GDPR_Integrations.php
4 years ago
CFF_Group_Posts.php
4 years ago
CFF_HTTP_Request.php
4 years ago
CFF_Oembed.php
4 years ago
CFF_Parse.php
4 years ago
CFF_Resizer.php
4 years ago
CFF_Response.php
4 years ago
CFF_Shortcode.php
4 years ago
CFF_Shortcode_Display.php
4 years ago
CFF_SiteHealth.php
4 years ago
CFF_Utils.php
4 years ago
CFF_View.php
4 years ago
Custom_Facebook_Feed.php
4 years ago
SB_Facebook_Data_Encryption.php
4 years ago
SB_Facebook_Data_Manager.php
4 years ago
SB_Facebook_Data_Manager.php
534 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class SB_Facebook_Data_Manager |
| 4 | * |
| 5 | * @since 4.1 |
| 6 | */ |
| 7 | namespace CustomFacebookFeed; |
| 8 | use CustomFacebookFeed\Builder\CFF_Db; |
| 9 | use CustomFacebookFeed\CFF_Resizer; |
| 10 | use CustomFacebookFeed\SB_Facebook_Data_Encryption; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | die( '-1' ); |
| 14 | } |
| 15 | |
| 16 | class SB_Facebook_Data_Manager { |
| 17 | |
| 18 | /** |
| 19 | * Key and salt to use for remote encryption. |
| 20 | * |
| 21 | * @var string |
| 22 | * |
| 23 | * @since 4.1 |
| 24 | */ |
| 25 | private $key_salt; |
| 26 | |
| 27 | /** |
| 28 | * Start manager |
| 29 | * |
| 30 | * @since 4.1 |
| 31 | */ |
| 32 | public function init() { |
| 33 | $this->hooks(); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * Hook into certain features of the plugin and AJAX calls |
| 39 | * |
| 40 | * @since 4.1 |
| 41 | */ |
| 42 | public function hooks() { |
| 43 | add_action( 'cff_before_display_facebook', array( $this, 'update_last_used' ) ); |
| 44 | add_action( 'cff_before_display_facebook', array( $this, 'check' ) ); |
| 45 | add_action( 'sb_facebook_twicedaily', array( $this, 'maybe_delete_old_data' ) ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * To avoid a database update every page load, the check |
| 50 | * is done once a day |
| 51 | * |
| 52 | * @since 4.1 |
| 53 | */ |
| 54 | public function update_last_used() { |
| 55 | $statuses = $this->get_statuses(); |
| 56 | |
| 57 | // if this hasn't been updated in the last hour |
| 58 | if ( $statuses['last_used'] < cff_get_current_time() - 3600 ) { |
| 59 | // update the last used time |
| 60 | $statuses['last_used'] = cff_get_current_time(); |
| 61 | |
| 62 | $this->update_statuses( $statuses ); |
| 63 | } |
| 64 | |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Check for plain text instagram data in posts table |
| 69 | * |
| 70 | * @since 4.1 |
| 71 | */ |
| 72 | public function check() { |
| 73 | $this->encrypt_json_in_cff_facebook_posts(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Delete unused data after a period |
| 78 | * |
| 79 | * @return bool |
| 80 | * |
| 81 | * @since 4.1 |
| 82 | */ |
| 83 | public function maybe_delete_old_data() { |
| 84 | $statuses = $this->get_statuses(); |
| 85 | |
| 86 | $data_was_deleted = false; |
| 87 | |
| 88 | if ( $statuses['last_used'] < cff_get_current_time() - (21 * DAY_IN_SECONDS) ) { |
| 89 | |
| 90 | $this->delete_caches(); |
| 91 | \cff_main()->cff_error_reporter->add_action_log( 'Deleted all platform data.' ); |
| 92 | |
| 93 | $data_was_deleted = true; |
| 94 | } |
| 95 | |
| 96 | if ( $statuses['last_used'] < cff_get_current_time() - (90 * DAY_IN_SECONDS) ) { |
| 97 | CFF_Db::clear_cff_sources(); |
| 98 | |
| 99 | \cff_main()->cff_error_reporter->add_action_log( 'Deleted all connected accounts.' ); |
| 100 | |
| 101 | $data_was_deleted = true; |
| 102 | } |
| 103 | |
| 104 | return $data_was_deleted; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Delete feed caches |
| 109 | * |
| 110 | * @param bool $include_backup |
| 111 | * |
| 112 | * @since 2.9.4/5.12.4 |
| 113 | */ |
| 114 | public function delete_caches( $include_backup = true ) { |
| 115 | cff_delete_cache(); |
| 116 | CFF_Resizer::delete_resizing_table_and_images(); |
| 117 | \cff_main()->cff_error_reporter->add_action_log( 'Reset resizing tables.' ); |
| 118 | //CFF_Resizer::create_resizing_table_and_uploads_folder(); |
| 119 | CFF_Db::clear_cff_feed_caches(); |
| 120 | CFF_Db::clear_cff_sources(); |
| 121 | $this->delete_transient_backup_data( true ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Update all parts of the database for FB platform guidelines |
| 126 | * |
| 127 | * @throws Exception |
| 128 | * |
| 129 | * @since 4.1 |
| 130 | */ |
| 131 | public function update_db_for_dpa() { |
| 132 | global $wpdb; |
| 133 | $encryption = new SB_Facebook_Data_Encryption(); |
| 134 | $table_name_option = $wpdb->prefix . "options"; |
| 135 | $sources_table_name = $wpdb->prefix . "cff_sources"; |
| 136 | $wpdb->query( "ALTER TABLE $sources_table_name MODIFY access_token varchar(1000) NOT NULL default ''" ); |
| 137 | |
| 138 | $this->encrypt_json_in_cff_facebook_posts(); |
| 139 | $this->encrypt_sources_access_token(); |
| 140 | $this->encrypt_cff_backup_cache(); |
| 141 | $this->encrypt_cff_group_cache(); |
| 142 | $this->remove_access_token_from_feeds(); |
| 143 | $this->delete_transient_backup_data(); |
| 144 | $this->encrypt_cff_legacy_feed(); |
| 145 | $this->encrypt_oembed(); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | /** |
| 150 | * Encrypt a set of 50 posts if this has been attempted |
| 151 | * less than 30 times. |
| 152 | * |
| 153 | * @since 4.1 |
| 154 | */ |
| 155 | public function encrypt_json_in_cff_facebook_posts() { |
| 156 | $statuses = $this->get_statuses(); |
| 157 | // if this hasn't been updated in the last hour |
| 158 | if ( $statuses['num_db_updates'] > 30 ) { |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | $statuses['num_db_updates'] = $statuses['num_db_updates'] + 1; |
| 163 | $this->update_statuses( $statuses ); |
| 164 | |
| 165 | global $wpdb; |
| 166 | $encryption = new SB_Facebook_Data_Encryption(); |
| 167 | $table_name = $wpdb->prefix . CFF_POSTS_TABLE; |
| 168 | $feeds_posts_table_name = esc_sql( $wpdb->prefix . CFF_FEEDS_POSTS_TABLE ); |
| 169 | |
| 170 | $plaintext_posts = array(); |
| 171 | |
| 172 | if ( empty( $plaintext_posts ) ) { |
| 173 | $statuses['num_db_updates'] = 31; |
| 174 | $this->update_statuses( $statuses ); |
| 175 | } |
| 176 | |
| 177 | foreach ( $plaintext_posts as $post ) { |
| 178 | $json_data = $encryption->encrypt( $post['json_data'] ); |
| 179 | $updated = $wpdb->query( $wpdb->prepare( |
| 180 | "UPDATE $table_name as p |
| 181 | INNER JOIN $feeds_posts_table_name AS f ON p.id = f.id |
| 182 | SET p.json_data = %s |
| 183 | WHERE p.id = %d;", $json_data, $post['id'] ) ); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | |
| 188 | /** |
| 189 | * Encrypt sources Access tokens |
| 190 | * |
| 191 | * @since 4.1 |
| 192 | */ |
| 193 | public function encrypt_sources_access_token() { |
| 194 | global $wpdb; |
| 195 | $encryption = new SB_Facebook_Data_Encryption(); |
| 196 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 197 | |
| 198 | $sources_list = $wpdb->get_results( "SELECT * FROM $sources_table_name;", ARRAY_A ); |
| 199 | foreach ( $sources_list as $source ) { |
| 200 | $access_token = $encryption->maybe_encrypt( $source['access_token'] ); |
| 201 | $info = $encryption->maybe_encrypt( $source['info'] ); |
| 202 | |
| 203 | $updated = $wpdb->query( $wpdb->prepare( |
| 204 | "UPDATE $sources_table_name as s |
| 205 | SET s.access_token = %s, |
| 206 | s.info = %s, |
| 207 | s.last_updated = %s |
| 208 | WHERE s.id = %d;", $access_token, $info, date( 'Y-m-d H:i:s' ), $source['id'] ) ); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Encrypt a Backup Cache Data |
| 214 | * |
| 215 | * @since 4.1 |
| 216 | */ |
| 217 | public function encrypt_cff_backup_cache() { |
| 218 | global $wpdb; |
| 219 | $encryption = new SB_Facebook_Data_Encryption(); |
| 220 | $feed_cache_table_name = $wpdb->prefix . 'cff_feed_caches'; |
| 221 | |
| 222 | |
| 223 | $feed_caches = $wpdb->get_results( |
| 224 | "SELECT * FROM $feed_cache_table_name as p |
| 225 | WHERE p.cache_value LIKE '%{%'; |
| 226 | ", ARRAY_A ); |
| 227 | |
| 228 | if ( empty( $feed_caches ) ) { |
| 229 | $statuses['num_db_updates'] = 31; |
| 230 | $this->update_statuses( $statuses ); |
| 231 | } |
| 232 | |
| 233 | foreach ( $feed_caches as $cache ) { |
| 234 | $cache_value = $encryption->encrypt( $cache['cache_value'] ); |
| 235 | $updated = $wpdb->query( $wpdb->prepare( |
| 236 | "UPDATE $feed_cache_table_name as p |
| 237 | SET p.cache_value = %s |
| 238 | WHERE p.id = %d;", $cache_value, $cache['id'] ) ); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Update Group Posts Persistent Cache |
| 244 | * |
| 245 | * @throws Exception |
| 246 | * |
| 247 | * @since 4.1 |
| 248 | */ |
| 249 | public function encrypt_cff_group_cache(){ |
| 250 | global $wpdb; |
| 251 | $encryption = new SB_Facebook_Data_Encryption(); |
| 252 | $table_name = $wpdb->prefix . "options"; |
| 253 | $persistent_groups = $wpdb->get_results( " |
| 254 | SELECT * |
| 255 | FROM $table_name |
| 256 | WHERE `option_name` LIKE ('%!cff\_group\_%') |
| 257 | " ); |
| 258 | |
| 259 | foreach ($persistent_groups as $group) { |
| 260 | $cache_value = $encryption->maybe_encrypt( $group->option_value ); |
| 261 | $updated = $wpdb->query( $wpdb->prepare( |
| 262 | "UPDATE $table_name as gp |
| 263 | SET gp.option_value = %s |
| 264 | WHERE gp.option_id = %d;", $cache_value, $group->option_id ) ); |
| 265 | |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | public function encrypt_oembed() { |
| 270 | $cff_oembed_data = get_option( 'cff_oembed_token' ); |
| 271 | $sbi_oembed_data = get_option( 'sbi_oembed_token' ); |
| 272 | |
| 273 | if ( empty( $cff_oembed_data['access_token'] ) && empty( $sbi_oembed_data['access_token'] ) ) { |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | $encryption = new SB_Facebook_Data_Encryption(); |
| 278 | if ( isset( $cff_oembed_data['access_token'] ) && ! $encryption->decrypt( $cff_oembed_data['access_token'] ) ) { |
| 279 | $cff_oembed_data['access_token'] = $encryption->encrypt( $cff_oembed_data['access_token'] ); |
| 280 | } |
| 281 | |
| 282 | if ( isset( $sbi_oembed_data['access_token'] ) && ! $encryption->decrypt( $sbi_oembed_data['access_token'] ) ) { |
| 283 | $sbi_oembed_data['access_token'] = $encryption->encrypt( $sbi_oembed_data['access_token'] ); |
| 284 | } |
| 285 | |
| 286 | update_option( 'cff_oembed_token', $cff_oembed_data ); |
| 287 | update_option( 'sbi_oembed_token', $sbi_oembed_data ); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Update Group Posts Persistent Cache |
| 292 | * |
| 293 | * @throws Exception |
| 294 | * |
| 295 | * @since 4.1 |
| 296 | */ |
| 297 | public function encrypt_cff_legacy_feed(){ |
| 298 | global $wpdb; |
| 299 | $encryption = new SB_Facebook_Data_Encryption(); |
| 300 | $table_name = $wpdb->prefix . "options"; |
| 301 | $legacyfeed = $wpdb->get_results( " |
| 302 | SELECT * |
| 303 | FROM $table_name |
| 304 | WHERE `option_name` LIKE 'cff_legacy_feed_settings' |
| 305 | " ); |
| 306 | |
| 307 | foreach ($legacyfeed as $legacy) { |
| 308 | $cache_value = $encryption->maybe_encrypt( $legacy->option_value ); |
| 309 | $updated = $wpdb->query( $wpdb->prepare( |
| 310 | "UPDATE $table_name as gp |
| 311 | SET gp.option_value = %s |
| 312 | WHERE gp.option_id = %d;", $cache_value, $legacy->option_id ) ); |
| 313 | |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Update Feeds Table & Remove the Access Token from the Settings |
| 319 | * |
| 320 | * @throws Exception |
| 321 | * |
| 322 | * @since 4.1 |
| 323 | */ |
| 324 | public function remove_access_token_from_feeds() { |
| 325 | global $wpdb; |
| 326 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 327 | $feeds_list = $wpdb->get_results( |
| 328 | "SELECT * FROM $feeds_table_name", ARRAY_A ); |
| 329 | |
| 330 | foreach ( $feeds_list as $feed ) { |
| 331 | $settings = json_decode( $feed['settings'], true ); |
| 332 | unset($settings['accesstoken']); |
| 333 | |
| 334 | $settings = json_encode( $settings ); |
| 335 | |
| 336 | $updated = $wpdb->query( $wpdb->prepare( |
| 337 | "UPDATE $feeds_table_name as f |
| 338 | SET f.settings = %s |
| 339 | WHERE f.id = %d;", $settings, $feed['id'] ) ); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Data manager statuses |
| 345 | * |
| 346 | * @return array |
| 347 | * |
| 348 | * @since 4.1 |
| 349 | */ |
| 350 | public function get_statuses() { |
| 351 | $cff_statuses_option = get_option( 'cff_statuses', array() ); |
| 352 | |
| 353 | $return = isset( $cff_statuses_option['data_manager'] ) ? $cff_statuses_option['data_manager'] : $this->defaults(); |
| 354 | return $return; |
| 355 | } |
| 356 | |
| 357 | |
| 358 | /** |
| 359 | * Delete Backup data |
| 360 | * |
| 361 | * @since 4.1 |
| 362 | */ |
| 363 | public function delete_transient_backup_data( $processDeleteGroup = false ){ |
| 364 | global $wpdb; |
| 365 | $table_name = $wpdb->prefix . "options"; |
| 366 | $wpdb->query( " |
| 367 | DELETE |
| 368 | FROM $table_name |
| 369 | WHERE `option_name` LIKE ('%\_transient\_cff\_%') |
| 370 | " ); |
| 371 | $wpdb->query( " |
| 372 | DELETE |
| 373 | FROM $table_name |
| 374 | WHERE `option_name` LIKE ('%\_transient\_!cff\_%') |
| 375 | " ); |
| 376 | $wpdb->query( " |
| 377 | DELETE |
| 378 | FROM $table_name |
| 379 | WHERE `option_name` LIKE ('%\_transient\_cff\_ej\_%') |
| 380 | " ); |
| 381 | $wpdb->query( " |
| 382 | DELETE |
| 383 | FROM $table_name |
| 384 | WHERE `option_name` LIKE ('%\_transient\_cff\_tle\_%') |
| 385 | " ); |
| 386 | $wpdb->query( " |
| 387 | DELETE |
| 388 | FROM $table_name |
| 389 | WHERE `option_name` LIKE ('%\_transient\_cff\_album\_%') |
| 390 | " ); |
| 391 | $wpdb->query( " |
| 392 | DELETE |
| 393 | FROM $table_name |
| 394 | WHERE `option_name` LIKE ('%\_transient\_timeout\_cff\_%') |
| 395 | " ); |
| 396 | $wpdb->query( " |
| 397 | DELETE |
| 398 | FROM $table_name |
| 399 | WHERE `option_name` LIKE ('%\_transient\_timeout\_!cff\_%') |
| 400 | " ); |
| 401 | $wpdb->query( " |
| 402 | DELETE |
| 403 | FROM $table_name |
| 404 | WHERE `option_name` LIKE ('%cff\_backup\_%') |
| 405 | " ); |
| 406 | |
| 407 | if( $processDeleteGroup === true){ |
| 408 | $wpdb->query( " |
| 409 | DELETE |
| 410 | FROM $table_name |
| 411 | WHERE `option_name` LIKE ('%!cff\_group\_%') |
| 412 | " ); |
| 413 | $wpdb->query( " |
| 414 | DELETE |
| 415 | FROM $table_name |
| 416 | WHERE `option_name` LIKE 'cff_connected_accounts' |
| 417 | " ); |
| 418 | $wpdb->query( " |
| 419 | DELETE |
| 420 | FROM $table_name |
| 421 | WHERE `option_name` LIKE 'cff_access_token' |
| 422 | " ); |
| 423 | $wpdb->query( " |
| 424 | DELETE |
| 425 | FROM $table_name |
| 426 | WHERE `option_name` LIKE 'cff_oembed_token' |
| 427 | " ); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Update data manager status |
| 433 | * |
| 434 | * @param array $statuses |
| 435 | * |
| 436 | * @since 4.1 |
| 437 | */ |
| 438 | public function update_statuses( $statuses ) { |
| 439 | $cff_statuses_option = get_option( 'cff_statuses', array() ); |
| 440 | $cff_statuses_option['data_manager'] = $statuses; |
| 441 | |
| 442 | update_option( 'cff_statuses', $cff_statuses_option ); |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Reset the data manager |
| 447 | * |
| 448 | * @since 4.1 |
| 449 | */ |
| 450 | public function reset() { |
| 451 | $cff_statuses_option = get_option( 'cff_statuses', array() ); |
| 452 | $cff_statuses_option['data_manager'] = $this->defaults(); |
| 453 | |
| 454 | update_option( 'cff_statuses', $cff_statuses_option ); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Default values for manager |
| 459 | * |
| 460 | * @return array |
| 461 | * |
| 462 | * @since 4.1 |
| 463 | */ |
| 464 | public function defaults() { |
| 465 | return array( |
| 466 | 'last_used' => cff_get_current_time() - DAY_IN_SECONDS, |
| 467 | 'num_db_updates' => 0 |
| 468 | ); |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Encrypt using Smash Balloon's support key and salt |
| 473 | * |
| 474 | * @param string $encrypted_value |
| 475 | * |
| 476 | * @return bool|string |
| 477 | * |
| 478 | * @since 2.9.4/5.12.4 |
| 479 | */ |
| 480 | public function remote_encrypt( $encrypted_value ) { |
| 481 | $local_encrypt = new SB_Facebook_Data_Encryption(); |
| 482 | $raw_value = $local_encrypt->decrypt( $encrypted_value ); |
| 483 | if ( $this->key_salt === null ) { |
| 484 | $url = 'https://secure.smashballoon.com/'; |
| 485 | $args = array( |
| 486 | 'timeout' => 20 |
| 487 | ); |
| 488 | $response = wp_remote_get( $url, $args ); |
| 489 | |
| 490 | if ( ! is_wp_error( $response ) ) { |
| 491 | $this->key_salt = $response['body']; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | $key = substr( $this->key_salt, 0, 64 ); |
| 496 | $salt = substr( $this->key_salt, 64, 64 ); |
| 497 | |
| 498 | $args = array( |
| 499 | 'key' => $key, |
| 500 | 'salt' => $salt |
| 501 | ); |
| 502 | |
| 503 | $remote_encrypt = new SB_Facebook_Data_Encryption( $args ); |
| 504 | |
| 505 | return $remote_encrypt->encrypt( $raw_value ); |
| 506 | } |
| 507 | |
| 508 | public function remote_decrypt( $encrypted_value ) { |
| 509 | if ( $this->key_salt === null ) { |
| 510 | $url = 'https://secure.smashballoon.com/'; |
| 511 | $args = array( |
| 512 | 'timeout' => 20 |
| 513 | ); |
| 514 | $response = wp_remote_get( $url, $args ); |
| 515 | |
| 516 | if ( ! is_wp_error( $response ) ) { |
| 517 | $this->key_salt = $response['body']; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | $key = substr( $this->key_salt, 0, 64 ); |
| 522 | $salt = substr( $this->key_salt, 64, 64 ); |
| 523 | |
| 524 | $args = array( |
| 525 | 'key' => $key, |
| 526 | 'salt' => $salt |
| 527 | ); |
| 528 | |
| 529 | $remote_encrypt = new SB_Facebook_Data_Encryption( $args ); |
| 530 | |
| 531 | return $remote_encrypt->decrypt( $encrypted_value ); |
| 532 | } |
| 533 | } |
| 534 |