ConsoleCommand
1 month ago
Dimension
1 month ago
API.php
6 months ago
AggregatedMetric.php
2 years ago
ArchivedMetric.php
1 year ago
Archiver.php
1 month ago
Categories.php
2 years ago
ComponentFactory.php
3 months ago
ComputedMetric.php
1 year ago
ConsoleCommand.php
1 month ago
Controller.php
1 month ago
ControllerAdmin.php
2 weeks ago
Dependency.php
1 month ago
LogTablesProvider.php
2 years ago
Manager.php
1 month ago
Menu.php
1 month ago
MetadataLoader.php
1 month ago
Metric.php
1 month ago
PluginException.php
1 year ago
ProcessedMetric.php
3 months ago
ReleaseChannels.php
3 months ago
Report.php
1 month ago
ReportsProvider.php
2 years ago
RequestProcessors.php
4 months ago
Segment.php
3 months ago
SettingsProvider.php
1 month ago
Tasks.php
1 month ago
ThemeStyles.php
2 weeks ago
ViewDataTable.php
3 months ago
Visualization.php
1 year ago
WidgetsProvider.php
3 months ago
API.php
138 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\Plugin; |
| 10 | |
| 11 | use Piwik\Container\StaticContainer; |
| 12 | use Piwik\Piwik; |
| 13 | use Piwik\Plugins\Login\PasswordVerifier; |
| 14 | use Piwik\Log\LoggerInterface; |
| 15 | use Exception; |
| 16 | /** |
| 17 | * The base class of all API singletons. |
| 18 | * |
| 19 | * Plugins that want to expose functionality through the Reporting API should create a class |
| 20 | * that extends this one. Every public method in that class that is not annotated with **@ignore** |
| 21 | * will be callable through Matomo's Web API. |
| 22 | * |
| 23 | * _Note: If your plugin calculates and stores reports, they should be made available through the API._ |
| 24 | * |
| 25 | * ### Examples |
| 26 | * |
| 27 | * **Defining an API for a plugin** |
| 28 | * |
| 29 | * class API extends \Piwik\Plugin\API |
| 30 | * { |
| 31 | * public function myMethod($idSite, $period, $date, $segment = false) |
| 32 | * { |
| 33 | * $dataTable = // ... get some data ... |
| 34 | * return $dataTable; |
| 35 | * } |
| 36 | * } |
| 37 | * |
| 38 | * **Linking to an API method** |
| 39 | * |
| 40 | * <a href="?module=API&method=MyPlugin.myMethod&idSite=1&period=day&date=2013-10-23">Link</a> |
| 41 | * |
| 42 | * @api |
| 43 | */ |
| 44 | abstract class API |
| 45 | { |
| 46 | private static $instances; |
| 47 | /** @var bool */ |
| 48 | protected $autoSanitizeInputParams = \true; |
| 49 | /** |
| 50 | * Returns the singleton instance for the derived class. If the singleton instance |
| 51 | * has not been created, this method will create it. |
| 52 | * |
| 53 | * @return static |
| 54 | */ |
| 55 | public static function getInstance() |
| 56 | { |
| 57 | $class = get_called_class(); |
| 58 | if (!isset(self::$instances[$class])) { |
| 59 | $container = StaticContainer::getContainer(); |
| 60 | $refl = new \ReflectionClass($class); |
| 61 | if (!$refl->getConstructor() || $refl->getConstructor()->isPublic()) { |
| 62 | self::$instances[$class] = $container->get($class); |
| 63 | } else { |
| 64 | /** @var LoggerInterface $logger */ |
| 65 | $logger = $container->get(LoggerInterface::class); |
| 66 | // BC with API defining a protected constructor |
| 67 | $logger->notice('The API class {class} defines a protected constructor which is deprecated, make the constructor public instead', ['class' => $class]); |
| 68 | self::$instances[$class] = new $class(); |
| 69 | } |
| 70 | } |
| 71 | return self::$instances[$class]; |
| 72 | } |
| 73 | /** |
| 74 | * Used in tests only |
| 75 | * @ignore |
| 76 | * @internal |
| 77 | */ |
| 78 | public static function unsetInstance() |
| 79 | { |
| 80 | $class = get_called_class(); |
| 81 | unset(self::$instances[$class]); |
| 82 | } |
| 83 | /** |
| 84 | * Used in tests only |
| 85 | * @ignore |
| 86 | * @internal |
| 87 | */ |
| 88 | public static function unsetAllInstances() |
| 89 | { |
| 90 | self::$instances = []; |
| 91 | } |
| 92 | /** |
| 93 | * Sets the singleton instance. For testing purposes. |
| 94 | * @ignore |
| 95 | * @internal |
| 96 | */ |
| 97 | public static function setSingletonInstance($instance) |
| 98 | { |
| 99 | $class = get_called_class(); |
| 100 | self::$instances[$class] = $instance; |
| 101 | } |
| 102 | /** |
| 103 | * Verifies if the given password matches the current users password |
| 104 | * |
| 105 | * @param $passwordConfirmation |
| 106 | * @throws Exception |
| 107 | */ |
| 108 | protected function confirmCurrentUserPassword( |
| 109 | #[\SensitiveParameter] |
| 110 | $passwordConfirmation) |
| 111 | { |
| 112 | $loginCurrentUser = Piwik::getCurrentUserLogin(); |
| 113 | if (!Piwik::doesUserRequirePasswordConfirmation($loginCurrentUser)) { |
| 114 | return; |
| 115 | // password confirmation disabled for user |
| 116 | } |
| 117 | if (empty($passwordConfirmation)) { |
| 118 | throw new Exception(Piwik::translate('UsersManager_ConfirmWithReAuthentication')); |
| 119 | } |
| 120 | try { |
| 121 | if (!StaticContainer::get(PasswordVerifier::class)->isPasswordCorrect($loginCurrentUser, $passwordConfirmation)) { |
| 122 | throw new Exception(Piwik::translate('UsersManager_CurrentPasswordNotCorrect')); |
| 123 | } |
| 124 | } catch (Exception $e) { |
| 125 | // in case of any error (e.g. the provided password is too weak) |
| 126 | throw new Exception(Piwik::translate('UsersManager_CurrentPasswordNotCorrect')); |
| 127 | } |
| 128 | } |
| 129 | /** |
| 130 | * @return bool |
| 131 | * @internal |
| 132 | */ |
| 133 | public function usesAutoSanitizeInputParams() |
| 134 | { |
| 135 | return $this->autoSanitizeInputParams; |
| 136 | } |
| 137 | } |
| 138 |