|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SimpleSAML\Module\perun; |
| 4 | + |
| 5 | +use SimpleSAML\Error\Exception; |
| 6 | +use SimpleSAML\Logger; |
| 7 | +use SimpleSAML\Configuration; |
| 8 | + |
| 9 | +/** |
| 10 | + * Implementation of WarningConfiguration using json file as the source of warning attributes |
| 11 | + * @package SimpleSAML\Module\perun |
| 12 | + * @author Dominik Baránek <[email protected]> |
| 13 | + */ |
| 14 | +class WarningConfigurationFile extends WarningConfiguration |
| 15 | +{ |
| 16 | + public function getSourceOfWarningAttributes() |
| 17 | + { |
| 18 | + $file = null; |
| 19 | + $data = null; |
| 20 | + |
| 21 | + $config = Configuration::getConfig(WarningConfiguration::CONFIG_FILE_NAME); |
| 22 | + |
| 23 | + try { |
| 24 | + $file = $config->getString(WarningConfiguration::WARNING_FILE); |
| 25 | + |
| 26 | + set_error_handler(function () { |
| 27 | + Logger::warning( |
| 28 | + "perun:WarningConfigurationFile: " . |
| 29 | + "missing or invalid disco.warning.file parameter in module_perun.php" |
| 30 | + ); |
| 31 | + }); |
| 32 | + |
| 33 | + $json_data = file_get_contents($file); |
| 34 | + restore_error_handler(); |
| 35 | + $data = json_decode($json_data, true); |
| 36 | + } catch (\Exception $ex) { |
| 37 | + Logger::warning( |
| 38 | + "perun:WarningConfigurationFile: missing or invalid disco.warning.file parameter in module_perun.php" |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + return $data; |
| 43 | + } |
| 44 | + |
| 45 | + public function getWarningAttributes() |
| 46 | + { |
| 47 | + $data = self::getSourceOfWarningAttributes(); |
| 48 | + |
| 49 | + if ($data !== null) { |
| 50 | + if (isset($data[WarningConfiguration::WARNING_IS_ON])) { |
| 51 | + $this->warningIsOn = $data[WarningConfiguration::WARNING_IS_ON]; |
| 52 | + } else { |
| 53 | + Logger::warning( |
| 54 | + "perun:warningConfigurationFile: " . |
| 55 | + "missing or invalid warningIsOn parameter in file with warning configuration" |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + if (isset($data[WarningConfiguration::WARNING_TYPE])) { |
| 60 | + $this->warningType = $data[WarningConfiguration::WARNING_TYPE]; |
| 61 | + |
| 62 | + if (!in_array($this->warningType, $this->allowedTypes)) { |
| 63 | + Logger::info('perun:warningConfigurationFile: warningType has invalid value, value set to INFO'); |
| 64 | + $this->warningType = 'INFO'; |
| 65 | + } |
| 66 | + } else { |
| 67 | + Logger::warning( |
| 68 | + "perun:warningConfigurationFile: " . |
| 69 | + "missing or invalid warningType parameter in file with warning configuration" |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + if (isset($data[WarningConfiguration::WARNING_TITLE])) { |
| 74 | + $this->warningTitle = $data[WarningConfiguration::WARNING_TITLE]; |
| 75 | + } else { |
| 76 | + Logger::warning( |
| 77 | + "perun:warningConfigurationFile: " . |
| 78 | + "missing or invalid warningTitle parameter in file with warning configuration" |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + if (isset($data[WarningConfiguration::WARNING_TEXT])) { |
| 83 | + $this->warningText = $data[WarningConfiguration::WARNING_TEXT]; |
| 84 | + } else { |
| 85 | + Logger::warning( |
| 86 | + "perun:warningConfigurationFile: " . |
| 87 | + "missing or invalid warningText parameter in file with warning configuration" |
| 88 | + ); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return array( |
| 93 | + 'warningIsOn' => $this->warningIsOn, |
| 94 | + 'warningType' => $this->warningType, |
| 95 | + 'warningTitle' => $this->warningTitle, |
| 96 | + 'warningText' => $this->warningText |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
0 commit comments