Alert.php
249 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\Alert; |
| 4 | |
| 5 | use Exception; |
| 6 | use JetBackup\Ajax\Ajax; |
| 7 | use JetBackup\CLI\CLI; |
| 8 | use JetBackup\Data\Engine; |
| 9 | use JetBackup\Data\SleekStore; |
| 10 | use JetBackup\Entities\Util; |
| 11 | use JetBackup\Exception\DBException; |
| 12 | use JetBackup\Exception\NotificationException; |
| 13 | use JetBackup\Factory; |
| 14 | use JetBackup\JetBackup; |
| 15 | use JetBackup\Notification\Notification; |
| 16 | use JetBackup\Settings\Notifications; |
| 17 | use JetBackup\Wordpress\Wordpress; |
| 18 | use SleekDB\Exceptions\InvalidArgumentException; |
| 19 | use SleekDB\Exceptions\IOException; |
| 20 | use SleekDB\QueryBuilder; |
| 21 | |
| 22 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 23 | |
| 24 | class Alert extends Engine { |
| 25 | |
| 26 | const COLLECTION = 'alerts'; |
| 27 | |
| 28 | const UNIQUE_ID = 'unique_id'; |
| 29 | const CREATED = 'created'; |
| 30 | const TITLE = 'title'; |
| 31 | const MESSAGE = 'message'; |
| 32 | const LEVEL = 'level'; |
| 33 | |
| 34 | const EMAIL_SENT = 'email_sent'; |
| 35 | |
| 36 | const LEVEL_INFORMATION = 1; |
| 37 | const LEVEL_WARNING = 2; |
| 38 | const LEVEL_CRITICAL = 4; |
| 39 | |
| 40 | const LEVELS = self::LEVEL_INFORMATION | self::LEVEL_WARNING | self::LEVEL_CRITICAL; |
| 41 | |
| 42 | const LEVEL_NAMES = [ |
| 43 | self::LEVEL_INFORMATION => 'Information', |
| 44 | self::LEVEL_WARNING => 'Warning', |
| 45 | self::LEVEL_CRITICAL => 'Critical', |
| 46 | ]; |
| 47 | |
| 48 | public function __construct($_id=null) { |
| 49 | parent::__construct(self::COLLECTION); |
| 50 | if($_id) $this->_loadById((int) $_id); |
| 51 | } |
| 52 | |
| 53 | public function setUniqueId($id) { $this->set(self::UNIQUE_ID, $id); } |
| 54 | public function getUniqueId():string { return $this->get(self::UNIQUE_ID); } |
| 55 | |
| 56 | public function setCreated($value):void { $this->set(self::CREATED, $value); } |
| 57 | public function getCreated() { return $this->get(self::CREATED); } |
| 58 | |
| 59 | public function setTitle($value):void { $this->set(self::TITLE, $value); } |
| 60 | public function getTitle() { return $this->get(self::TITLE); } |
| 61 | |
| 62 | public function setMessage($value):void { $this->set(self::MESSAGE, $value); } |
| 63 | public function getMessage() { return $this->get(self::MESSAGE); } |
| 64 | |
| 65 | public function setEmailSent(bool $bool):void { $this->set(self::EMAIL_SENT, $bool);} |
| 66 | public function isEmailSent() : bool { return $this->get(self::EMAIL_SENT, false); } |
| 67 | public function setLevel(int $value):void { $this->set(self::LEVEL, $value); } |
| 68 | public function getLevel():int { return (int) $this->get(self::LEVEL); } |
| 69 | |
| 70 | public function save():void { |
| 71 | if(!$this->getUniqueId()) $this->setUniqueId(Util::generateUniqueId()); |
| 72 | parent::save(); |
| 73 | } |
| 74 | |
| 75 | public static function db():SleekStore { |
| 76 | return new SleekStore(self::COLLECTION); |
| 77 | } |
| 78 | |
| 79 | public static function query():QueryBuilder { |
| 80 | return self::db()->createQueryBuilder(); |
| 81 | } |
| 82 | |
| 83 | public static function getAlertNotificationFrequency(int $level) : int { |
| 84 | $levels = Factory::getSettingsNotifications()->getAlertLevelFrequency(); |
| 85 | return $levels[$level] ?? 0; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @return int |
| 90 | * @throws IOException |
| 91 | * @throws InvalidArgumentException |
| 92 | */ |
| 93 | public static function getTotalAlerts():int { |
| 94 | return count(self::query() |
| 95 | ->select([JetBackup::ID_FIELD]) |
| 96 | ->getQuery() |
| 97 | ->fetch()); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @return int |
| 102 | * @throws IOException |
| 103 | * @throws InvalidArgumentException |
| 104 | */ |
| 105 | public static function getTotalCriticalAlerts():int { |
| 106 | return count(self::query() |
| 107 | ->select([JetBackup::ID_FIELD]) |
| 108 | ->where([self::LEVEL, '=', self::LEVEL_CRITICAL]) |
| 109 | ->getQuery() |
| 110 | ->fetch()); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @throws IOException |
| 115 | * @throws DBException |
| 116 | * @throws InvalidArgumentException|NotificationException |
| 117 | * @throws Exception |
| 118 | */ |
| 119 | public static function processDailyAlerts() { |
| 120 | |
| 121 | if (!Factory::getSettingsNotifications()->isEmailsEnabled()) return; |
| 122 | $levels = Factory::getSettingsNotifications()->getAlertLevelFrequency(); |
| 123 | $alerts = []; |
| 124 | |
| 125 | foreach ($levels as $level => $frequency) { |
| 126 | |
| 127 | if($frequency != Notifications::NOTIFICATION_FREQUENCY_DAILY) continue; |
| 128 | |
| 129 | $results = self::query() |
| 130 | ->select([JetBackup::ID_FIELD]) |
| 131 | ->where([self::EMAIL_SENT, '=', false]) |
| 132 | ->where([self::LEVEL, '=', $level]) |
| 133 | ->getQuery() |
| 134 | ->fetch(); |
| 135 | |
| 136 | if (empty($results)) continue; |
| 137 | |
| 138 | foreach ($results as $alert_id) { |
| 139 | $alert = new Alert($alert_id[JetBackup::ID_FIELD]); |
| 140 | if(!$alert->getId()) continue; |
| 141 | $alerts[] = [ |
| 142 | 'title' => $alert->getTitle(), |
| 143 | 'message' => $alert->getMessage(), |
| 144 | 'level' => self::LEVEL_NAMES[$alert->getLevel()], |
| 145 | 'date' => Util::date( |
| 146 | Wordpress::getDateFormat() . ' ' . Wordpress::getTimeFormat(), |
| 147 | (int) $alert->getCreated() |
| 148 | ), |
| 149 | ]; |
| 150 | |
| 151 | $alert->setEmailSent(true); |
| 152 | $alert->save(); |
| 153 | } |
| 154 | |
| 155 | } |
| 156 | |
| 157 | if (empty($alerts)) return; |
| 158 | |
| 159 | Notification::message() |
| 160 | ->addParam('backup_domain', Wordpress::getSiteDomain()) |
| 161 | ->addParam('notification_frequency', Notifications::NOTIFICATION_FREQUENCY_NAMES[Notifications::NOTIFICATION_FREQUENCY_DAILY]) |
| 162 | ->addParam('alerts', $alerts) |
| 163 | ->send('JetBackup WordPress Notification', 'alert'); |
| 164 | |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @throws InvalidArgumentException |
| 169 | * @throws IOException |
| 170 | */ |
| 171 | public static function clearAlerts():void { |
| 172 | self::query()->getQuery()->delete(); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @param string $title |
| 177 | * @param string $message |
| 178 | * @param int $level |
| 179 | * |
| 180 | * @return void |
| 181 | * @throws IOException |
| 182 | * @throws InvalidArgumentException |
| 183 | * @throws NotificationException |
| 184 | * @throws Exception |
| 185 | */ |
| 186 | public static function add(string $title, string $message, int $level):void { |
| 187 | |
| 188 | $alert = new Alert(); |
| 189 | $alert->setEmailSent(false); |
| 190 | |
| 191 | if(self::getAlertNotificationFrequency($level) == Notifications::NOTIFICATION_FREQUENCY_REAL_TIME) { |
| 192 | |
| 193 | Notification::message() |
| 194 | ->addParam('backup_domain', Wordpress::getSiteDomain()) |
| 195 | ->addParam('notification_frequency', Notifications::NOTIFICATION_FREQUENCY_NAMES[Notifications::NOTIFICATION_FREQUENCY_REAL_TIME]) |
| 196 | ->addParam('alerts', [[ |
| 197 | 'title' => $title, |
| 198 | 'message' => $message, |
| 199 | 'level' => self::LEVEL_NAMES[$level], |
| 200 | 'date' => Util::date( |
| 201 | Wordpress::getDateFormat() . ' ' . Wordpress::getTimeFormat(), |
| 202 | (int) $alert->getCreated() |
| 203 | ) |
| 204 | ]]) |
| 205 | ->send('JetBackup WordPress Notification', 'alert'); |
| 206 | |
| 207 | $alert->setEmailSent(true); |
| 208 | } |
| 209 | |
| 210 | $alert->setCreated(time()); |
| 211 | $alert->setTitle($title); |
| 212 | $alert->setMessage($message); |
| 213 | $alert->setLevel($level); |
| 214 | $alert->setEngine(Engine::ENGINE_WP); |
| 215 | $alert->save(); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * @throws Exception |
| 220 | */ |
| 221 | public function getDisplay(): array { |
| 222 | return [ |
| 223 | JetBackup::ID_FIELD => $this->getId(), |
| 224 | self::UNIQUE_ID => $this->getUniqueId(), |
| 225 | self::CREATED => $this->getCreated(), |
| 226 | self::TITLE => $this->getTitle(), |
| 227 | self::MESSAGE => $this->getMessage(), |
| 228 | Engine::ENGINE => $this->getEngine(), |
| 229 | 'engine_name' => $this->getEngineName(), |
| 230 | self::LEVEL => $this->getLevel(), |
| 231 | self::EMAIL_SENT => $this->isEmailSent() |
| 232 | ]; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * @throws Exception |
| 237 | */ |
| 238 | public function getDisplayCLI(): array { |
| 239 | return [ |
| 240 | 'ID' => $this->getId(), |
| 241 | 'Created' => CLI::date($this->getCreated()), |
| 242 | 'Title' => $this->getTitle(), |
| 243 | 'Message' => $this->getMessage(), |
| 244 | 'Engine' => $this->getEngineName(), |
| 245 | 'Level' => self::LEVEL_NAMES[$this->getLevel()], |
| 246 | 'Email Sent' => $this->isEmailSent() ? 'Yes' : 'No' |
| 247 | ]; |
| 248 | } |
| 249 | } |