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