ConsoleCommand
2 years ago
Dimension
2 years ago
API.php
2 years ago
AggregatedMetric.php
2 years ago
ArchivedMetric.php
2 years ago
Archiver.php
2 years ago
Categories.php
2 years ago
ComponentFactory.php
2 years ago
ComputedMetric.php
2 years ago
ConsoleCommand.php
2 years ago
Controller.php
2 years ago
ControllerAdmin.php
2 years ago
Dependency.php
2 years ago
LogTablesProvider.php
2 years ago
Manager.php
2 years ago
Menu.php
2 years ago
MetadataLoader.php
2 years ago
Metric.php
2 years ago
PluginException.php
2 years ago
ProcessedMetric.php
2 years ago
ReleaseChannels.php
2 years ago
Report.php
2 years ago
ReportsProvider.php
2 years ago
RequestProcessors.php
2 years ago
Segment.php
2 years ago
SettingsProvider.php
2 years ago
Tasks.php
2 years ago
ThemeStyles.php
2 years ago
ViewDataTable.php
2 years ago
Visualization.php
2 years ago
WidgetsProvider.php
2 years ago
API.php
136 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | * |
| 9 | */ |
| 10 | namespace Piwik\Plugin; |
| 11 | |
| 12 | use Piwik\Container\StaticContainer; |
| 13 | use Piwik\Piwik; |
| 14 | use Piwik\Plugins\Login\PasswordVerifier; |
| 15 | use Piwik\Log\LoggerInterface; |
| 16 | use Exception; |
| 17 | /** |
| 18 | * The base class of all API singletons. |
| 19 | * |
| 20 | * Plugins that want to expose functionality through the Reporting API should create a class |
| 21 | * that extends this one. Every public method in that class that is not annotated with **@ignore** |
| 22 | * will be callable through Matomo's Web API. |
| 23 | * |
| 24 | * _Note: If your plugin calculates and stores reports, they should be made available through the API._ |
| 25 | * |
| 26 | * ### Examples |
| 27 | * |
| 28 | * **Defining an API for a plugin** |
| 29 | * |
| 30 | * class API extends \Piwik\Plugin\API |
| 31 | * { |
| 32 | * public function myMethod($idSite, $period, $date, $segment = false) |
| 33 | * { |
| 34 | * $dataTable = // ... get some data ... |
| 35 | * return $dataTable; |
| 36 | * } |
| 37 | * } |
| 38 | * |
| 39 | * **Linking to an API method** |
| 40 | * |
| 41 | * <a href="?module=API&method=MyPlugin.myMethod&idSite=1&period=day&date=2013-10-23">Link</a> |
| 42 | * |
| 43 | * @api |
| 44 | */ |
| 45 | abstract class API |
| 46 | { |
| 47 | private static $instances; |
| 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($passwordConfirmation) |
| 109 | { |
| 110 | $loginCurrentUser = Piwik::getCurrentUserLogin(); |
| 111 | if (!Piwik::doesUserRequirePasswordConfirmation($loginCurrentUser)) { |
| 112 | return; |
| 113 | // password confirmation disabled for user |
| 114 | } |
| 115 | if (empty($passwordConfirmation)) { |
| 116 | throw new Exception(Piwik::translate('UsersManager_ConfirmWithPassword')); |
| 117 | } |
| 118 | try { |
| 119 | if (!StaticContainer::get(PasswordVerifier::class)->isPasswordCorrect($loginCurrentUser, $passwordConfirmation)) { |
| 120 | throw new Exception(Piwik::translate('UsersManager_CurrentPasswordNotCorrect')); |
| 121 | } |
| 122 | } catch (Exception $e) { |
| 123 | // in case of any error (e.g. the provided password is too weak) |
| 124 | throw new Exception(Piwik::translate('UsersManager_CurrentPasswordNotCorrect')); |
| 125 | } |
| 126 | } |
| 127 | /** |
| 128 | * @return bool |
| 129 | * @internal |
| 130 | */ |
| 131 | public function usesAutoSanitizeInputParams() |
| 132 | { |
| 133 | return $this->autoSanitizeInputParams; |
| 134 | } |
| 135 | } |
| 136 |