Dimension
5 years ago
API.php
5 years ago
AggregatedMetric.php
5 years ago
ArchivedMetric.php
5 years ago
Archiver.php
5 years ago
Categories.php
5 years ago
ComponentFactory.php
5 years ago
ComputedMetric.php
5 years ago
ConsoleCommand.php
5 years ago
Controller.php
5 years ago
ControllerAdmin.php
5 years ago
Dependency.php
5 years ago
LogTablesProvider.php
5 years ago
Manager.php
5 years ago
Menu.php
5 years ago
MetadataLoader.php
5 years ago
Metric.php
5 years ago
PluginException.php
5 years ago
ProcessedMetric.php
5 years ago
ReleaseChannels.php
5 years ago
Report.php
5 years ago
ReportsProvider.php
5 years ago
RequestProcessors.php
5 years ago
Segment.php
5 years ago
SettingsProvider.php
5 years ago
Tasks.php
5 years ago
ThemeStyles.php
5 years ago
ViewDataTable.php
5 years ago
Visualization.php
5 years ago
WidgetsProvider.php
5 years ago
ControllerAdmin.php
406 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik\Plugin; |
| 10 | |
| 11 | use Piwik\Config as PiwikConfig; |
| 12 | use Piwik\Config; |
| 13 | use Piwik\Container\StaticContainer; |
| 14 | use Piwik\Development; |
| 15 | use Piwik\Menu\MenuAdmin; |
| 16 | use Piwik\Menu\MenuTop; |
| 17 | use Piwik\Notification; |
| 18 | use Piwik\Notification\Manager as NotificationManager; |
| 19 | use Piwik\Piwik; |
| 20 | use Piwik\Plugins\Marketplace\Marketplace; |
| 21 | use Piwik\Tracker\TrackerConfig; |
| 22 | use Piwik\Url; |
| 23 | use Piwik\Version; |
| 24 | use Piwik\View; |
| 25 | use Piwik\ProxyHttp; |
| 26 | use Piwik\SettingsPiwik; |
| 27 | |
| 28 | /** |
| 29 | * Base class of plugin controllers that provide administrative functionality. |
| 30 | * |
| 31 | * See {@link Controller} to learn more about Piwik controllers. |
| 32 | * |
| 33 | */ |
| 34 | abstract class ControllerAdmin extends Controller |
| 35 | { |
| 36 | private static function notifyWhenTrackingStatisticsDisabled() |
| 37 | { |
| 38 | $statsEnabled = PiwikConfig::getInstance()->Tracker['record_statistics']; |
| 39 | if ($statsEnabled == "0") { |
| 40 | $notification = new Notification(Piwik::translate('General_StatisticsAreNotRecorded')); |
| 41 | $notification->context = Notification::CONTEXT_INFO; |
| 42 | Notification\Manager::notify('ControllerAdmin_StatsAreNotRecorded', $notification); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | private static function notifyAnyInvalidLicense() |
| 47 | { |
| 48 | if (!Marketplace::isMarketplaceEnabled()) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | if (Piwik::isUserIsAnonymous()) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | if (!Piwik::isUserHasSomeAdminAccess()) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | if (Development::isEnabled()) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | $expired = StaticContainer::get('Piwik\Plugins\Marketplace\Plugins\InvalidLicenses'); |
| 65 | |
| 66 | $messageLicenseMissing = $expired->getMessageNoLicense(); |
| 67 | if (!empty($messageLicenseMissing)) { |
| 68 | $notification = new Notification($messageLicenseMissing); |
| 69 | $notification->raw = true; |
| 70 | $notification->context = Notification::CONTEXT_ERROR; |
| 71 | $notification->title = Piwik::translate('Marketplace_LicenseMissing'); |
| 72 | Notification\Manager::notify('ControllerAdmin_LicenseMissingWarning', $notification); |
| 73 | } |
| 74 | |
| 75 | $messageExceeded = $expired->getMessageExceededLicenses(); |
| 76 | if (!empty($messageExceeded)) { |
| 77 | $notification = new Notification($messageExceeded); |
| 78 | $notification->raw = true; |
| 79 | $notification->context = Notification::CONTEXT_WARNING; |
| 80 | $notification->title = Piwik::translate('Marketplace_LicenseExceeded'); |
| 81 | Notification\Manager::notify('ControllerAdmin_LicenseExceededWarning', $notification); |
| 82 | } |
| 83 | |
| 84 | $messageExpired = $expired->getMessageExpiredLicenses(); |
| 85 | if (!empty($messageExpired)) { |
| 86 | $notification = new Notification($messageExpired); |
| 87 | $notification->raw = true; |
| 88 | $notification->context = Notification::CONTEXT_WARNING; |
| 89 | $notification->title = Piwik::translate('Marketplace_LicenseExpired'); |
| 90 | Notification\Manager::notify('ControllerAdmin_LicenseExpiredWarning', $notification); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private static function notifyAnyInvalidPlugin() |
| 95 | { |
| 96 | if (!Piwik::hasUserSuperUserAccess()) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | $missingPlugins = \Piwik\Plugin\Manager::getInstance()->getMissingPlugins(); |
| 101 | |
| 102 | if (empty($missingPlugins)) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | $pluginsLink = Url::getCurrentQueryStringWithParametersModified(array( |
| 107 | 'module' => 'CorePluginsAdmin', 'action' => 'plugins' |
| 108 | )); |
| 109 | |
| 110 | $invalidPluginsWarning = Piwik::translate('CoreAdminHome_InvalidPluginsWarning', array( |
| 111 | self::getPiwikVersion(), |
| 112 | '<strong>' . implode('</strong>, <strong>', $missingPlugins) . '</strong>')) |
| 113 | . "<br/>" |
| 114 | . Piwik::translate('CoreAdminHome_InvalidPluginsYouCanUninstall', array( |
| 115 | '<a href="' . $pluginsLink . '"/>', |
| 116 | '</a>' |
| 117 | )); |
| 118 | |
| 119 | $notification = new Notification($invalidPluginsWarning); |
| 120 | $notification->raw = true; |
| 121 | $notification->context = Notification::CONTEXT_WARNING; |
| 122 | $notification->title = Piwik::translate('General_Warning'); |
| 123 | Notification\Manager::notify('ControllerAdmin_InvalidPluginsWarning', $notification); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Calls {@link setBasicVariablesView()} and {@link setBasicVariablesAdminView()} |
| 128 | * using the supplied view. |
| 129 | * |
| 130 | * @param View $view |
| 131 | * @param string $viewType If 'admin', the admin variables are set as well as basic ones. |
| 132 | */ |
| 133 | protected function setBasicVariablesViewAs($view, $viewType = 'admin') |
| 134 | { |
| 135 | $this->setBasicVariablesNoneAdminView($view); |
| 136 | if ($viewType === 'admin') { |
| 137 | self::setBasicVariablesAdminView($view); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | private static function notifyIfURLIsNotSecure() |
| 142 | { |
| 143 | $isURLSecure = ProxyHttp::isHttps(); |
| 144 | if ($isURLSecure) { |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | if (!Piwik::hasUserSuperUserAccess()) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | if (Url::isLocalHost(Url::getCurrentHost())) { |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | if (Development::isEnabled()) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | $message = Piwik::translate('General_CurrentlyUsingUnsecureHttp'); |
| 161 | |
| 162 | $message .= " "; |
| 163 | |
| 164 | $message .= Piwik::translate('General_ReadThisToLearnMore', |
| 165 | array('<a rel="noreferrer noopener" target="_blank" href="https://matomo.org/faq/how-to/faq_91/">', '</a>') |
| 166 | ); |
| 167 | |
| 168 | $notification = new Notification($message); |
| 169 | $notification->context = Notification::CONTEXT_WARNING; |
| 170 | $notification->raw = true; |
| 171 | Notification\Manager::notify('ControllerAdmin_HttpIsUsed', $notification); |
| 172 | } |
| 173 | |
| 174 | private static function notifyIfDevelopmentModeOnButNotInstalledThroughGit() |
| 175 | { |
| 176 | if (!Piwik::hasUserSuperUserAccess()) { |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | if (!Development::isEnabled()) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | if (SettingsPiwik::isGitDeployment()) { |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | $message = Piwik::translate('General_WarningDevelopmentModeOnButNotGitInstalled'); |
| 189 | |
| 190 | $notification = new Notification($message); |
| 191 | $notification->context = Notification::CONTEXT_WARNING; |
| 192 | $notification->raw = true; |
| 193 | $notification->flags = Notification::FLAG_CLEAR; |
| 194 | Notification\Manager::notify('ControllerAdmin_DevelopmentModeOn', $notification); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @ignore |
| 199 | */ |
| 200 | public static function displayWarningIfConfigFileNotWritable() |
| 201 | { |
| 202 | $isConfigFileWritable = PiwikConfig::getInstance()->isFileWritable(); |
| 203 | |
| 204 | if (!$isConfigFileWritable) { |
| 205 | $exception = PiwikConfig::getInstance()->getConfigNotWritableException(); |
| 206 | $message = $exception->getMessage(); |
| 207 | |
| 208 | $notification = new Notification($message); |
| 209 | $notification->raw = true; |
| 210 | $notification->context = Notification::CONTEXT_WARNING; |
| 211 | Notification\Manager::notify('ControllerAdmin_ConfigNotWriteable', $notification); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
| 216 | private static function notifyIfEAcceleratorIsUsed() |
| 217 | { |
| 218 | $isEacceleratorUsed = ini_get('eaccelerator.enable'); |
| 219 | if (empty($isEacceleratorUsed)) { |
| 220 | return; |
| 221 | } |
| 222 | $message = sprintf("You are using the PHP accelerator & optimizer eAccelerator which is known to be not compatible with Matomo. |
| 223 | We have disabled eAccelerator, which might affect the performance of Matomo. |
| 224 | Read the %srelated ticket%s for more information and how to fix this problem.", |
| 225 | '<a rel="noreferrer noopener" target="_blank" href="https://github.com/matomo-org/matomo/issues/4439">', '</a>'); |
| 226 | |
| 227 | $notification = new Notification($message); |
| 228 | $notification->context = Notification::CONTEXT_WARNING; |
| 229 | $notification->raw = true; |
| 230 | Notification\Manager::notify('ControllerAdmin_EacceleratorIsUsed', $notification); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * PHP Version required by the next major Matomo version |
| 235 | * @return string |
| 236 | */ |
| 237 | private static function getNextRequiredMinimumPHP() |
| 238 | { |
| 239 | return '7.2'; |
| 240 | } |
| 241 | |
| 242 | private static function isUsingPhpVersionCompatibleWithNextPiwik() |
| 243 | { |
| 244 | return version_compare( PHP_VERSION, self::getNextRequiredMinimumPHP(), '>=' ); |
| 245 | } |
| 246 | |
| 247 | private static function notifyWhenPhpVersionIsNotCompatibleWithNextMajorPiwik() |
| 248 | { |
| 249 | if (self::isUsingPhpVersionCompatibleWithNextPiwik()) { |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | $youMustUpgradePHP = Piwik::translate('General_YouMustUpgradePhpVersionToReceiveLatestPiwik'); |
| 254 | $message = Piwik::translate('General_PiwikCannotBeUpgradedBecausePhpIsTooOld') |
| 255 | . ' ' |
| 256 | . sprintf(Piwik::translate('General_PleaseUpgradeYourPhpVersionSoYourPiwikDataStaysSecure'), self::getNextRequiredMinimumPHP()) |
| 257 | ; |
| 258 | |
| 259 | $notification = new Notification($message); |
| 260 | $notification->title = $youMustUpgradePHP; |
| 261 | $notification->priority = Notification::PRIORITY_LOW; |
| 262 | $notification->context = Notification::CONTEXT_WARNING; |
| 263 | $notification->type = Notification::TYPE_TRANSIENT; |
| 264 | $notification->flags = Notification::FLAG_NO_CLEAR; |
| 265 | NotificationManager::notify('PHPVersionTooOldForNewestPiwikCheck', $notification); |
| 266 | } |
| 267 | |
| 268 | private static function notifyWhenPhpVersionIsEOL() |
| 269 | { |
| 270 | if (defined('PIWIK_TEST_MODE')) { // to avoid changing every admin UI test |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | $notifyPhpIsEOL = Piwik::hasUserSuperUserAccess() && ! self::isPhpVersionAtLeast71(); |
| 275 | if (!$notifyPhpIsEOL) { |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | $deprecatedMajorPhpVersion = PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION; |
| 280 | $message = Piwik::translate('General_WarningPiwikWillStopSupportingPHPVersion', array($deprecatedMajorPhpVersion, self::getNextRequiredMinimumPHP())) |
| 281 | . "<br/> " |
| 282 | . Piwik::translate('General_WarningPhpVersionXIsTooOld', $deprecatedMajorPhpVersion); |
| 283 | |
| 284 | $notification = new Notification($message); |
| 285 | $notification->raw = true; |
| 286 | $notification->title = Piwik::translate('General_Warning'); |
| 287 | $notification->priority = Notification::PRIORITY_LOW; |
| 288 | $notification->context = Notification::CONTEXT_WARNING; |
| 289 | $notification->type = Notification::TYPE_TRANSIENT; |
| 290 | $notification->flags = Notification::FLAG_NO_CLEAR; |
| 291 | NotificationManager::notify('PHP71VersionCheck', $notification); |
| 292 | } |
| 293 | |
| 294 | private static function notifyWhenDebugOnDemandIsEnabled($trackerSetting) |
| 295 | { |
| 296 | if (!Development::isEnabled() |
| 297 | && Piwik::hasUserSuperUserAccess() && |
| 298 | TrackerConfig::getConfigValue($trackerSetting)) { |
| 299 | |
| 300 | $message = Piwik::translate('General_WarningDebugOnDemandEnabled'); |
| 301 | $message = sprintf($message, '"' . $trackerSetting . '"', '"[Tracker] ' . $trackerSetting . '"', '"0"', |
| 302 | '"config/config.ini.php"'); |
| 303 | $notification = new Notification($message); |
| 304 | $notification->title = Piwik::translate('General_Warning'); |
| 305 | $notification->priority = Notification::PRIORITY_LOW; |
| 306 | $notification->context = Notification::CONTEXT_WARNING; |
| 307 | $notification->type = Notification::TYPE_TRANSIENT; |
| 308 | $notification->flags = Notification::FLAG_NO_CLEAR; |
| 309 | NotificationManager::notify('Tracker' . $trackerSetting, $notification); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Assigns view properties that would be useful to views that render admin pages. |
| 315 | * |
| 316 | * Assigns the following variables: |
| 317 | * |
| 318 | * - **statisticsNotRecorded** - Set to true if the `[Tracker] record_statistics` INI |
| 319 | * config is `0`. If not `0`, this variable will not be defined. |
| 320 | * - **topMenu** - The result of `MenuTop::getInstance()->getMenu()`. |
| 321 | * - **enableFrames** - The value of the `[General] enable_framed_pages` INI config option. If |
| 322 | * true, {@link Piwik\View::setXFrameOptions()} is called on the view. |
| 323 | * - **isSuperUser** - Whether the current user is a superuser or not. |
| 324 | * - **usingOldGeoIPPlugin** - Whether this Piwik install is currently using the old GeoIP |
| 325 | * plugin or not. |
| 326 | * - **invalidPluginsWarning** - Set if some of the plugins to load (determined by INI configuration) |
| 327 | * are invalid or missing. |
| 328 | * - **phpVersion** - The current PHP version. |
| 329 | * - **phpIsNewEnough** - Whether the current PHP version is new enough to run Piwik. |
| 330 | * - **adminMenu** - The result of `MenuAdmin::getInstance()->getMenu()`. |
| 331 | * |
| 332 | * @param View $view |
| 333 | * @api |
| 334 | */ |
| 335 | public static function setBasicVariablesAdminView(View $view) |
| 336 | { |
| 337 | self::notifyWhenTrackingStatisticsDisabled(); |
| 338 | self::notifyIfEAcceleratorIsUsed(); |
| 339 | self::notifyIfURLIsNotSecure(); |
| 340 | self::notifyIfDevelopmentModeOnButNotInstalledThroughGit(); |
| 341 | |
| 342 | $view->topMenu = MenuTop::getInstance()->getMenu(); |
| 343 | |
| 344 | $view->isDataPurgeSettingsEnabled = self::isDataPurgeSettingsEnabled(); |
| 345 | $enableFrames = PiwikConfig::getInstance()->General['enable_framed_settings']; |
| 346 | $view->enableFrames = $enableFrames; |
| 347 | |
| 348 | if (!$enableFrames) { |
| 349 | $view->setXFrameOptions('sameorigin'); |
| 350 | } |
| 351 | |
| 352 | $view->isSuperUser = Piwik::hasUserSuperUserAccess(); |
| 353 | |
| 354 | self::notifyAnyInvalidLicense(); |
| 355 | self::notifyAnyInvalidPlugin(); |
| 356 | self::notifyWhenPhpVersionIsEOL(); |
| 357 | self::notifyWhenPhpVersionIsNotCompatibleWithNextMajorPiwik(); |
| 358 | self::notifyWhenDebugOnDemandIsEnabled('debug'); |
| 359 | self::notifyWhenDebugOnDemandIsEnabled('debug_on_demand'); |
| 360 | |
| 361 | /** |
| 362 | * Posted when rendering an admin page and notifications about any warnings or errors should be triggered. |
| 363 | * You can use it for example when you have a plugin that needs to be configured in order to work and the |
| 364 | * plugin has not been configured yet. It can be also used to cancel / remove other notifications by calling |
| 365 | * eg `Notification\Manager::cancel($notificationId)`. |
| 366 | * |
| 367 | * **Example** |
| 368 | * |
| 369 | * public function onTriggerAdminNotifications(Piwik\Widget\WidgetsList $list) |
| 370 | * { |
| 371 | * if ($pluginFooIsNotConfigured) { |
| 372 | * $notification = new Notification('The plugin foo has not been configured yet'); |
| 373 | * $notification->context = Notification::CONTEXT_WARNING; |
| 374 | * Notification\Manager::notify('fooNotConfigured', $notification); |
| 375 | * } |
| 376 | * } |
| 377 | * |
| 378 | */ |
| 379 | Piwik::postEvent('Controller.triggerAdminNotifications'); |
| 380 | |
| 381 | $view->adminMenu = MenuAdmin::getInstance()->getMenu(); |
| 382 | |
| 383 | $notifications = $view->notifications; |
| 384 | |
| 385 | if (empty($notifications)) { |
| 386 | $view->notifications = NotificationManager::getAllNotificationsToDisplay(); |
| 387 | NotificationManager::cancelAllNonPersistent(); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | public static function isDataPurgeSettingsEnabled() |
| 392 | { |
| 393 | return (bool) Config::getInstance()->General['enable_delete_old_data_settings_admin']; |
| 394 | } |
| 395 | |
| 396 | protected static function getPiwikVersion() |
| 397 | { |
| 398 | return "Matomo " . Version::VERSION; |
| 399 | } |
| 400 | |
| 401 | private static function isPhpVersionAtLeast71() |
| 402 | { |
| 403 | return version_compare(PHP_VERSION, '7.1', '>='); |
| 404 | } |
| 405 | } |
| 406 |