Exceptions
6 months ago
CnilPolicy.php
1 month ago
CompliancePolicy.php
3 months ago
PolicyManager.php
1 month ago
CompliancePolicy.php
160 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Policy; |
| 10 | |
| 11 | use Piwik\Piwik; |
| 12 | use Piwik\Plugin\Manager; |
| 13 | use Piwik\Settings\FieldConfig; |
| 14 | use Piwik\Settings\Interfaces\ConfigSettingInterface; |
| 15 | use Piwik\Settings\Interfaces\MeasurableSettingInterface; |
| 16 | use Piwik\Settings\Interfaces\SystemSettingInterface; |
| 17 | use Piwik\Settings\Interfaces\Traits\Getters\ConfigGetterTrait; |
| 18 | use Piwik\Settings\Interfaces\Traits\Setters\MeasurableSetterTrait; |
| 19 | use Piwik\Settings\Interfaces\Traits\Setters\SystemSetterTrait; |
| 20 | /** |
| 21 | * @implements SystemSettingInterface<bool> |
| 22 | * @implements MeasurableSettingInterface<bool> |
| 23 | */ |
| 24 | abstract class CompliancePolicy implements SystemSettingInterface, MeasurableSettingInterface, ConfigSettingInterface |
| 25 | { |
| 26 | /** |
| 27 | * @use SystemSetterTrait<bool> |
| 28 | */ |
| 29 | use SystemSetterTrait; |
| 30 | /** |
| 31 | * @use MeasurableSetterTrait<bool> |
| 32 | */ |
| 33 | use MeasurableSetterTrait; |
| 34 | /** |
| 35 | * @use ConfigGetterTrait<bool> |
| 36 | */ |
| 37 | use ConfigGetterTrait; |
| 38 | public static abstract function getName() : string; |
| 39 | public static abstract function getTitle() : string; |
| 40 | protected static abstract function generateDescription() : string; |
| 41 | protected static abstract function generateWarnings() : string; |
| 42 | public static function getDescription() : string |
| 43 | { |
| 44 | $description = static::generateDescription(); |
| 45 | /** |
| 46 | * This event is triggered while the description of a compliance policy is |
| 47 | * being generated. The policy description can be modified via this event. |
| 48 | * |
| 49 | * @param string &$description of the policy. |
| 50 | */ |
| 51 | Piwik::postEvent('CompliancePolicy.updatePolicyDescription', [&$description, static::class]); |
| 52 | $shouldShowWarnings = \true; |
| 53 | /** |
| 54 | * This event is triggered while the description of a compliance policy is |
| 55 | * being generated, and controls whether any warnings specific to the policy |
| 56 | * are displayed at the end of the description. |
| 57 | * |
| 58 | * @param bool &$shouldShowWarnings set to false if the warnings should be hidden |
| 59 | */ |
| 60 | Piwik::postEvent('CompliancePolicy.shouldShowWarnings', [&$shouldShowWarnings, static::class]); |
| 61 | if ($shouldShowWarnings) { |
| 62 | $warnings = static::generateWarnings(); |
| 63 | if (!empty($warnings)) { |
| 64 | $description .= '<br/>' . static::generateWarnings(); |
| 65 | } |
| 66 | } |
| 67 | return $description; |
| 68 | } |
| 69 | /** |
| 70 | * @return array<array<string>> of [['title' => (string) 'TITLE', 'note' => (string) 'NOTE']] |
| 71 | */ |
| 72 | public static abstract function getUnknownSettings() : array; |
| 73 | /** |
| 74 | * @return array<string, string> |
| 75 | */ |
| 76 | public static function getDetails() : array |
| 77 | { |
| 78 | return ['id' => static::getName(), 'title' => static::getTitle(), 'description' => static::getDescription()]; |
| 79 | } |
| 80 | protected static function getPluginManagerInstance() : Manager |
| 81 | { |
| 82 | return Manager::getInstance(); |
| 83 | } |
| 84 | protected static function getSystemDefaultValue() |
| 85 | { |
| 86 | return \false; |
| 87 | } |
| 88 | protected static function getSystemName() : string |
| 89 | { |
| 90 | return preg_replace('/\\s+/', '', static::getName()) . '_policy_enabled'; |
| 91 | } |
| 92 | protected static function getSystemType() : string |
| 93 | { |
| 94 | return FieldConfig::TYPE_BOOL; |
| 95 | } |
| 96 | protected static function getMeasurableDefaultValue() |
| 97 | { |
| 98 | return \false; |
| 99 | } |
| 100 | protected static function getMeasurableName() : string |
| 101 | { |
| 102 | return preg_replace('/\\s+/', '', static::getName()) . '_policy_enabled'; |
| 103 | } |
| 104 | protected static function getMeasurableType() : string |
| 105 | { |
| 106 | return FieldConfig::TYPE_BOOL; |
| 107 | } |
| 108 | protected static function getConfigSection() : string |
| 109 | { |
| 110 | return Piwik::getPluginNameOfMatomoClass(static::class); |
| 111 | } |
| 112 | protected static function getConfigSettingName() : string |
| 113 | { |
| 114 | return static::getSystemName(); |
| 115 | } |
| 116 | /** |
| 117 | * If the policy is active at the instance level, |
| 118 | * disabling the policy for a site will also disable it |
| 119 | * for the instance. |
| 120 | */ |
| 121 | public static function setActiveStatus(?int $idSite, bool $isActive) : void |
| 122 | { |
| 123 | if (isset($idSite)) { |
| 124 | static::setMeasurableValue($idSite, $isActive); |
| 125 | if (static::getSystemValue() && !$isActive) { |
| 126 | static::setSystemValue($isActive); |
| 127 | } |
| 128 | } else { |
| 129 | static::setSystemValue($isActive); |
| 130 | } |
| 131 | /** |
| 132 | * This event is triggered when the status of a compliance policy changes, and |
| 133 | * is to be used to perform extra actions when a policy is activated/deactivated. |
| 134 | * |
| 135 | * The status of a policy cannot be changed via this event. |
| 136 | * |
| 137 | * @param bool $isActive Whether the policy is being activated or deactivated |
| 138 | * @param int|null $idSite |
| 139 | * @param class-string<CompliancePolicy> The compliance policy in question |
| 140 | */ |
| 141 | Piwik::postEvent('CompliancePolicy.setActiveStatus', [$isActive, $idSite, static::class]); |
| 142 | } |
| 143 | /** |
| 144 | * If the policy is active at the instance level, then |
| 145 | * this function will return true for all sites. |
| 146 | */ |
| 147 | public static function isActive(?int $idSite) : bool |
| 148 | { |
| 149 | $instanceLevel = static::getSystemValue(); |
| 150 | if (!$instanceLevel && isset($idSite)) { |
| 151 | return static::getMeasurableValue($idSite); |
| 152 | } |
| 153 | return $instanceLevel; |
| 154 | } |
| 155 | public static function isConfigControlled() |
| 156 | { |
| 157 | return !is_null(static::getConfigValue()); |
| 158 | } |
| 159 | } |
| 160 |