PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/wordfence/wordfence/lib/wfNotification.php

https://bitbucket.org/youresolutions/sheffieldcareservices.yes1.co.uk
PHP | 160 lines | 137 code | 21 blank | 2 comment | 58 complexity | 39af657ebf8ff42a2da2d5a9baec4c39 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, MIT, Apache-2.0
  1. <?php
  2. class wfNotification {
  3. const PRIORITY_LOW = 1500;
  4. const PRIORITY_DEFAULT = 1000;
  5. const PRIORITY_HIGH = 500;
  6. const PRIORITY_HIGH_CRITICAL = 501;
  7. const PRIORITY_HIGH_WARNING = 502;
  8. protected $_id;
  9. protected $_category;
  10. protected $_priority;
  11. protected $_ctime;
  12. protected $_html;
  13. protected $_links;
  14. public static function notifications($since = 0) {
  15. global $wpdb;
  16. $prefix = wfDB::networkPrefix();
  17. $rawNotifications = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$prefix}wfNotifications WHERE `new` = 1 AND `ctime` > %d ORDER BY `priority` ASC, `ctime` DESC", $since), ARRAY_A);
  18. $notifications = array();
  19. foreach ($rawNotifications as $raw) {
  20. $notifications[] = new wfNotification($raw['id'], $raw['priority'], $raw['html'], $raw['category'], $raw['ctime'], json_decode($raw['links'], true), true);
  21. }
  22. return $notifications;
  23. }
  24. public static function getNotificationForID($id) {
  25. global $wpdb;
  26. $prefix = wfDB::networkPrefix();
  27. $rawNotifications = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$prefix}wfNotifications WHERE `id` = %s ORDER BY `priority` ASC, `ctime` DESC", $id), ARRAY_A);
  28. if (count($rawNotifications) == 1) {
  29. $raw = $rawNotifications[0];
  30. return new wfNotification($raw['id'], $raw['priority'], $raw['html'], $raw['category'], $raw['ctime'], json_decode($raw['links'], true), true);
  31. }
  32. return null;
  33. }
  34. public static function getNotificationForCategory($category, $requireNew = true) {
  35. global $wpdb;
  36. $prefix = wfDB::networkPrefix();
  37. $rawNotifications = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$prefix}wfNotifications WHERE " . ($requireNew ? '`new` = 1 AND ' : '') . "`category` = %s ORDER BY `priority` ASC, `ctime` DESC LIMIT 1", $category), ARRAY_A);
  38. if (count($rawNotifications) == 1) {
  39. $raw = $rawNotifications[0];
  40. return new wfNotification($raw['id'], $raw['priority'], $raw['html'], $raw['category'], $raw['ctime'], json_decode($raw['links'], true), true);
  41. }
  42. return null;
  43. }
  44. public static function reconcileNotificationsWithOptions() {
  45. $notification_updatesNeeded = wfConfig::get('notification_updatesNeeded');
  46. $notification_securityAlerts = wfConfig::get('notification_securityAlerts') || !wfConfig::p();
  47. $notification_promotions = wfConfig::get('notification_promotions') || !wfConfig::p();
  48. $notification_blogHighlights = wfConfig::get('notification_blogHighlights') || !wfConfig::p();
  49. $notification_productUpdates = wfConfig::get('notification_productUpdates') || !wfConfig::p();
  50. $notification_scanStatus = wfConfig::get('notification_scanStatus');
  51. $notifications = self::notifications();
  52. foreach ($notifications as $n) {
  53. $category = $n->category;
  54. if (preg_match('/^release/i', $category) && !$notification_productUpdates) { $n->markAsRead(); }
  55. if (preg_match('/^digest/i', $category) && !$notification_blogHighlights) { $n->markAsRead(); }
  56. if (preg_match('/^alert/i', $category) && !$notification_securityAlerts) { $n->markAsRead(); }
  57. if (preg_match('/^promo/i', $category) && !$notification_promotions) { $n->markAsRead(); }
  58. switch ($category) {
  59. case 'wfplugin_scan':
  60. if (!$notification_scanStatus) { $n->markAsRead(); }
  61. break;
  62. case 'wfplugin_updates':
  63. if (!$notification_updatesNeeded) { $n->markAsRead(); }
  64. break;
  65. case 'wfplugin_keyconflict':
  66. default:
  67. //Allow it
  68. break;
  69. }
  70. }
  71. }
  72. public function __construct($id, $priority, $html, $category = null, $ctime = null, $links = null, $memoryOnly = false) {
  73. if ($id === null) {
  74. $id = 'site-' . wfUtils::base32_encode(pack('I', wfConfig::atomicInc('lastNotificationID')));
  75. }
  76. if ($category === null) {
  77. $category = '';
  78. }
  79. if ($ctime === null) {
  80. $ctime = time();
  81. }
  82. if (!is_array($links)) {
  83. $links = array();
  84. }
  85. $this->_id = $id;
  86. $this->_category = $category;
  87. $this->_priority = $priority;
  88. $this->_ctime = $ctime;
  89. $this->_html = $html;
  90. $this->_links = $links;
  91. global $wpdb;
  92. if (!$memoryOnly) {
  93. $linksJSON = json_encode($links);
  94. $notification_updatesNeeded = wfConfig::get('notification_updatesNeeded');
  95. $notification_securityAlerts = wfConfig::get('notification_securityAlerts') || !wfConfig::p();
  96. $notification_promotions = wfConfig::get('notification_promotions') || !wfConfig::p();
  97. $notification_blogHighlights = wfConfig::get('notification_blogHighlights') || !wfConfig::p();
  98. $notification_productUpdates = wfConfig::get('notification_productUpdates') || !wfConfig::p();
  99. $notification_scanStatus = wfConfig::get('notification_scanStatus');
  100. if (preg_match('/^release/i', $category) && !$notification_productUpdates) { return; }
  101. if (preg_match('/^digest/i', $category) && !$notification_blogHighlights) { return; }
  102. if (preg_match('/^alert/i', $category) && !$notification_securityAlerts) { return; }
  103. if (preg_match('/^promo/i', $category) && !$notification_promotions) { return; }
  104. switch ($category) {
  105. case 'wfplugin_scan':
  106. if (!$notification_scanStatus) { return; }
  107. break;
  108. case 'wfplugin_updates':
  109. if (!$notification_updatesNeeded) { return; }
  110. break;
  111. case 'wfplugin_keyconflict':
  112. default:
  113. //Allow it
  114. break;
  115. }
  116. if (!empty($category)) {
  117. $existing = self::getNotificationForCategory($category);
  118. if ($existing) {
  119. $wpdb->query($wpdb->prepare("UPDATE {$wpdb->prefix}wfNotifications SET priority = %d, ctime = %d, html = %s, links = %s WHERE id = %s", $priority, $ctime, $html, $linksJSON, $existing->id));
  120. return;
  121. }
  122. }
  123. $wpdb->query($wpdb->prepare("INSERT IGNORE INTO {$wpdb->prefix}wfNotifications (id, category, priority, ctime, html, links) VALUES (%s, %s, %d, %d, %s, %s)", $id, $category, $priority, $ctime, $html, $linksJSON));
  124. }
  125. }
  126. public function __get($key){
  127. if ($key == 'id') { return $this->_id; }
  128. else if ($key == 'category') { return $this->_category; }
  129. else if ($key == 'priority') { return $this->_priority; }
  130. else if ($key == 'ctime') { return $this->_ctime; }
  131. else if ($key == 'html') { return $this->_html; }
  132. else if ($key == 'links') { return $this->_links; }
  133. throw new InvalidArgumentException();
  134. }
  135. public function markAsRead() {
  136. global $wpdb;
  137. $wpdb->query($wpdb->prepare("UPDATE {$wpdb->prefix}wfNotifications SET `new` = 0 WHERE `id` = %s", $this->_id));
  138. }
  139. }