.htaccess
1 year ago
Automation.php
10 months ago
General.php
3 months ago
Integrations.php
4 months ago
Logging.php
4 months ago
Maintenance.php
4 months ago
Notifications.php
4 months ago
Performance.php
3 months ago
Restore.php
1 year ago
Security.php
23 hours ago
Settings.php
4 months ago
Updates.php
4 months ago
index.html
1 year ago
web.config
1 year ago
Notifications.php
137 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\Settings; |
| 4 | |
| 5 | use JetBackup\Alert\Alert; |
| 6 | use JetBackup\Exception\DBException; |
| 7 | use JetBackup\Exception\FieldsValidationException; |
| 8 | use JetBackup\Wordpress\Wordpress; |
| 9 | use SleekDB\Exceptions\InvalidArgumentException; |
| 10 | use SleekDB\Exceptions\IOException; |
| 11 | |
| 12 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 13 | |
| 14 | class Notifications extends Settings { |
| 15 | |
| 16 | const SECTION = 'notifications'; |
| 17 | |
| 18 | const EMAILS = 'EMAILS'; |
| 19 | const ALTERNATE_EMAIL = 'ALTERNATE_EMAIL'; |
| 20 | const ADMIN_EMAIL = 'ADMIN_EMAIL'; |
| 21 | const NOTIFICATION_LEVELS_FREQUENCY = 'NOTIFICATION_LEVELS_FREQUENCY'; |
| 22 | |
| 23 | const NOTIFICATION_FREQUENCY_DISABLED = 0; |
| 24 | const NOTIFICATION_FREQUENCY_REAL_TIME = 1; |
| 25 | const NOTIFICATION_FREQUENCY_DAILY = 2; |
| 26 | |
| 27 | const NOTIFICATION_FREQUENCIES = [ |
| 28 | self::NOTIFICATION_FREQUENCY_DISABLED, |
| 29 | self::NOTIFICATION_FREQUENCY_DAILY, |
| 30 | self::NOTIFICATION_FREQUENCY_REAL_TIME |
| 31 | ]; |
| 32 | |
| 33 | const NOTIFICATION_FREQUENCY_DEFAULTS = [ |
| 34 | Alert::LEVEL_INFORMATION => self::NOTIFICATION_FREQUENCY_DISABLED, |
| 35 | Alert::LEVEL_WARNING => self::NOTIFICATION_FREQUENCY_DISABLED, |
| 36 | Alert::LEVEL_CRITICAL => self::NOTIFICATION_FREQUENCY_REAL_TIME, |
| 37 | ]; |
| 38 | |
| 39 | const NOTIFICATION_FREQUENCY_NAMES = [ |
| 40 | self::NOTIFICATION_FREQUENCY_DISABLED => 'Disabled', |
| 41 | self::NOTIFICATION_FREQUENCY_REAL_TIME => 'Real Time', |
| 42 | self::NOTIFICATION_FREQUENCY_DAILY => 'Daily', |
| 43 | |
| 44 | ]; |
| 45 | |
| 46 | /** |
| 47 | * @throws DBException |
| 48 | * @throws IOException |
| 49 | * @throws InvalidArgumentException |
| 50 | */ |
| 51 | public function __construct() { |
| 52 | parent::__construct(self::SECTION); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @return bool |
| 57 | */ |
| 58 | public function isEmailsEnabled():bool { return (bool) $this->get(self::EMAILS, true); } |
| 59 | |
| 60 | /** |
| 61 | * @param bool $value |
| 62 | * |
| 63 | * @return void |
| 64 | */ |
| 65 | public function setEmailsEnabled(bool $value):void { $this->set(self::EMAILS, $value); } |
| 66 | |
| 67 | /** |
| 68 | * @return string |
| 69 | */ |
| 70 | public function getAlternateEmail():string { return $this->get(self::ALTERNATE_EMAIL); } |
| 71 | |
| 72 | /** |
| 73 | * @param $value |
| 74 | * |
| 75 | * @return void |
| 76 | */ |
| 77 | public function setAlternateEmail($value):void { $this->set(self::ALTERNATE_EMAIL, $value); } |
| 78 | public function setAlertLevelFrequency(array $value):void { $this->set(self::NOTIFICATION_LEVELS_FREQUENCY, $value); } |
| 79 | |
| 80 | public function getAlertLevelFrequency():array {return $this->get(self::NOTIFICATION_LEVELS_FREQUENCY, self::NOTIFICATION_FREQUENCY_DEFAULTS);} |
| 81 | |
| 82 | /** |
| 83 | * @return array |
| 84 | */ |
| 85 | public function getDisplay():array { |
| 86 | |
| 87 | return [ |
| 88 | self::EMAILS => $this->isEmailsEnabled() ? 1 : 0, |
| 89 | self::ALTERNATE_EMAIL => $this->getAlternateEmail(), |
| 90 | self::ADMIN_EMAIL => Wordpress::getBlogInfo('admin_email'), |
| 91 | self::NOTIFICATION_LEVELS_FREQUENCY => $this->getAlertLevelFrequency(), |
| 92 | ]; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @return array |
| 97 | */ |
| 98 | public function getDisplayCLI():array { |
| 99 | |
| 100 | $alert_levels = []; |
| 101 | foreach($this->getAlertLevelFrequency() as $level => $frequency) |
| 102 | $alert_levels[] = Alert::LEVEL_NAMES[$level] . ' - ' . self::NOTIFICATION_FREQUENCY_NAMES[$frequency]; |
| 103 | |
| 104 | return [ |
| 105 | 'Emails' => $this->isEmailsEnabled() ? "Yes" : "No", |
| 106 | 'Alternate Email' => $this->getAlternateEmail(), |
| 107 | 'Admin Email' => Wordpress::getBlogInfo('admin_email'), |
| 108 | 'Email Alert Levels' => implode(", ", $alert_levels), |
| 109 | ]; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @throws FieldsValidationException |
| 114 | */ |
| 115 | public function validateFields():void { |
| 116 | |
| 117 | $changedFields = self::getChangedFields($this->getData(), (new Notifications())->getData()); |
| 118 | |
| 119 | if(in_array(self::NOTIFICATION_LEVELS_FREQUENCY, $changedFields)) { |
| 120 | $frequency = $this->getAlertLevelFrequency(); |
| 121 | |
| 122 | foreach ($frequency as $key => $value) { |
| 123 | |
| 124 | if (!($key & Alert::LEVELS)) |
| 125 | throw new FieldsValidationException("Invalid alert level key: $key"); |
| 126 | |
| 127 | if (!in_array($value, self::NOTIFICATION_FREQUENCIES)) |
| 128 | throw new FieldsValidationException("Invalid frequency value for $key: $value"); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if(in_array(self::ALTERNATE_EMAIL, $changedFields)) { |
| 133 | if ($this->getAlternateEmail() && !Wordpress::isEmail($this->getAlternateEmail())) |
| 134 | throw new FieldsValidationException("Email " . $this->getAlternateEmail() . " is not valid"); |
| 135 | } |
| 136 | } |
| 137 | } |