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