1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\SitesManager;
use Exception;
use Piwik\API\ResponseBuilder;
use Piwik\Common;
use Piwik\Config;
use Piwik\DataTable\Renderer\Json;
use Piwik\Piwik;
use Piwik\Plugin\Manager;
use Piwik\Plugins\SitesManager\SiteContentDetection\Matomo;
use Piwik\Plugins\SitesManager\SiteContentDetection\SiteContentDetectionAbstract;
use Piwik\Plugins\SitesManager\SiteContentDetection\WordPress;
use Piwik\SiteContentDetector;
use Piwik\Session;
use Piwik\SettingsPiwik;
use Piwik\Url;
use Piwik\View;
/**
*
*/
class Controller extends \Piwik\Plugin\ControllerAdmin
{
/** @var SiteContentDetector */
private $siteContentDetector;
public function __construct(SiteContentDetector $siteContentDetector)
{
$this->siteContentDetector = $siteContentDetector;
parent::__construct();
}
/**
* Main view showing listing of websites and settings
*/
public function index()
{
Piwik::checkUserHasSomeAdminAccess();
SitesManager::dieIfSitesAdminIsDisabled();
$pluginManager = Manager::getInstance();
$rollUpEnabled = $pluginManager->isPluginLoaded('RollUpReporting')
&& $pluginManager->isPluginActivated('RollUpReporting');
return $this->renderTemplate('index', ['rollUpEnabled' => $rollUpEnabled]);
}
public function globalSettings()
{
Piwik::checkUserHasSuperUserAccess();
return $this->renderTemplate(
'globalSettings',
[
'commonSensitiveQueryParams' => Config::getInstance()->SitesManager['CommonPIIParams'],
]
);
}
public function getGlobalSettings()
{
Piwik::checkUserHasSomeViewAccess();
$response = new ResponseBuilder(Common::getRequestVar('format'));
$globalSettings = [];
$globalSettings['keepURLFragmentsGlobal'] = API::getInstance()->getKeepURLFragmentsGlobal();
$globalSettings['defaultCurrency'] = API::getInstance()->getDefaultCurrency();
$globalSettings['searchKeywordParametersGlobal'] = API::getInstance()->getSearchKeywordParametersGlobal();
$globalSettings['searchCategoryParametersGlobal'] = API::getInstance()->getSearchCategoryParametersGlobal();
$globalSettings['defaultTimezone'] = API::getInstance()->getDefaultTimezone();
$globalSettings['excludedIpsGlobal'] = API::getInstance()->getExcludedIpsGlobal();
$globalSettings['excludedQueryParametersGlobal'] = API::getInstance()->getExcludedQueryParametersGlobal();
$globalSettings['excludedUserAgentsGlobal'] = API::getInstance()->getExcludedUserAgentsGlobal();
$globalSettings['excludedReferrersGlobal'] = API::getInstance()->getExcludedReferrersGlobal();
$globalSettings['exclusionTypeForQueryParams'] = API::getInstance()->getExclusionTypeForQueryParams();
return $response->getResponse($globalSettings);
}
/**
* Records Global settings when user submit changes
*/
public function setGlobalSettings()
{
$response = new ResponseBuilder(Common::getRequestVar('format'));
try {
$this->checkTokenInUrl();
$timezone = Common::getRequestVar('timezone', false);
$excludedIps = Common::getRequestVar('excludedIps', false);
$excludedQueryParameters = Common::getRequestVar('excludedQueryParameters', false);
$excludedUserAgents = Common::getRequestVar('excludedUserAgents', false);
$excludedReferrers = Common::getRequestVar('excludedReferrers', false);
$currency = Common::getRequestVar('currency', false);
$searchKeywordParameters = Common::getRequestVar('searchKeywordParameters', $default = "");
$searchCategoryParameters = Common::getRequestVar('searchCategoryParameters', $default = "");
$keepURLFragments = Common::getRequestVar('keepURLFragments', $default = 0);
$exclusionTypeForQueryParams = Common::getRequestVar('exclusionTypeForQueryParams', $default = "");
$api = API::getInstance();
$api->setDefaultTimezone($timezone);
$api->setDefaultCurrency($currency);
$api->setGlobalExcludedIps($excludedIps);
$api->setGlobalExcludedUserAgents($excludedUserAgents);
$api->setGlobalExcludedReferrers($excludedReferrers);
$api->setGlobalSearchParameters($searchKeywordParameters, $searchCategoryParameters);
$api->setKeepURLFragmentsGlobal($keepURLFragments);
$api->setGlobalQueryParamExclusion($exclusionTypeForQueryParams, $excludedQueryParameters);
$toReturn = $response->getResponse();
} catch (Exception $e) {
$toReturn = $response->getResponseException($e);
}
return $toReturn;
}
public function ignoreNoDataMessage()
{
Piwik::checkUserHasSomeViewAccess();
$session = new Session\SessionNamespace('siteWithoutData');
$session->ignoreMessage = true;
$session->setExpirationSeconds($oneHour = 60 * 60);
$url = Url::getCurrentUrlWithoutQueryString() . Url::getCurrentQueryStringWithParametersModified(array('module' => 'CoreHome', 'action' => 'index'));
Url::redirectToUrl($url);
}
public function siteWithoutData()
{
$this->checkSitePermission();
return $this->renderTemplateAs('siteWithoutData', [
'inviteUserLink' => $this->getInviteUserLink(),
'hideWhatIsNew' => true,
], $viewType = 'basic');
}
public function getTrackingMethodsForSite()
{
$this->checkSitePermission();
$this->siteContentDetector->detectContent([], $this->idSite);
$trackingMethods = [];
$instructionUrls = [];
$othersInstructions = [];
foreach ($this->siteContentDetector->getSiteContentDetectionsByType() as $detections) {
foreach ($detections as $obj) {
$tabContent = $obj->renderInstructionsTab($this->siteContentDetector);
$othersInstruction = $obj->renderOthersInstruction($this->siteContentDetector);
$instructionUrl = $obj->getInstructionUrl();
$isRecommended = $obj->isRecommended($this->siteContentDetector);
$recommendationDetails = $obj->getRecommendationDetails($this->siteContentDetector);
/**
* Event that can be used to manipulate the content of a certain tab on the no data page
*
* @param string $tabContent Content of the tab
* @param SiteContentDetector $detector Instance of SiteContentDetector, holding current detection results
*/
Piwik::postEvent('Template.siteWithoutDataTab.' . $obj::getId() . '.content', [&$tabContent, $this->siteContentDetector]);
/**
* Event that can be used to manipulate the content of a record on the others tab on the no data page
*
* @param string $othersInstruction Content of the record
* @param SiteContentDetector $detector Instance of SiteContentDetector, holding current detection results
*/
Piwik::postEvent('Template.siteWithoutDataTab.' . $obj::getId() . '.others', [&$othersInstruction, $this->siteContentDetector]);
if (!empty($tabContent)) {
$trackingMethods[] = [
'id' => $obj::getId(),
'name' => $obj::getName(),
'type' => $obj::getContentType(),
'content' => $tabContent,
'icon' => $obj::getIcon(),
'priority' => $obj::getPriority(),
'wasDetected' => $this->siteContentDetector->wasDetected($obj::getId()),
'isRecommended' => $isRecommended,
'recommendationTitle' => $recommendationDetails['title'],
'recommendationText' => $recommendationDetails['text'],
'recommendationButton' => $recommendationDetails['button'],
];
}
if (!empty($othersInstruction)) {
$othersInstructions[] = [
'id' => $obj::getId(),
'name' => $obj::getName(),
'type' => $obj::getContentType(),
'othersInstruction' => $othersInstruction,
];
}
if (!empty($instructionUrl)) {
$instructionUrls[] = [
'id' => $obj::getId(),
'name' => $obj::getName(),
'type' => $obj::getContentType(),
'instructionUrl' => $obj::getInstructionUrl(),
];
}
}
}
usort($trackingMethods, function ($a, $b) {
if ($a['isRecommended'] === $b['isRecommended']) {
return $a['priority'] === $b['priority'] ? 0 : ($a['priority'] < $b['priority'] ? -1 : 1);
}
return $a['isRecommended'] ? -1 : 1;
});
usort($othersInstructions, function ($a, $b) {
return strnatcmp($a['name'], $b['name']);
});
usort($instructionUrls, function ($a, $b) {
return strnatcmp($a['name'], $b['name']);
});
// add integration and others tab
$trackingMethods[] = [
'id' => 'Integrations',
'name' => Piwik::translate('SitesManager_Integrations'),
'type' => SiteContentDetectionAbstract::TYPE_OTHER,
'content' => $this->renderIntegrationsTab($instructionUrls),
'icon' => './plugins/SitesManager/images/integrations.svg',
'priority' => 10000,
'wasDetected' => false,
'isRecommended' => false,
'recommendationTitle' => '',
'recommendationText' => '',
'recommendationButton' => '',
];
$trackingMethods[] = [
'id' => 'Other',
'name' => Piwik::translate('SitesManager_SiteWithoutDataOtherWays'),
'type' => SiteContentDetectionAbstract::TYPE_OTHER,
'content' => $this->renderOthersTab($othersInstructions),
'icon' => './plugins/SitesManager/images/others.svg',
'priority' => 10001,
'wasDetected' => false,
'isRecommended' => false,
'recommendationTitle' => '',
'recommendationText' => '',
'recommendationButton' => '',
];
$recommendedMethod = null;
$matomoIndex = null;
foreach ($trackingMethods as $index => $tab) {
// Note: We recommend the first method that is recommended (after sorting by priority)
if (true === $tab['isRecommended']) {
$recommendedMethod = $tab;
unset($trackingMethods[$index]);
break;
}
if ($tab['id'] === Matomo::getId()) {
$matomoIndex = $index;
}
}
// fall back to javascript code recommendation if nothing was detected
if (null === $recommendedMethod && null !== $matomoIndex) {
$recommendedMethod = $trackingMethods[$matomoIndex];
unset($trackingMethods[$matomoIndex]);
}
Json::sendHeaderJSON();
echo json_encode([
'trackingMethods' => $trackingMethods,
'recommendedMethod' => $recommendedMethod,
]);
exit;
}
private function getGoogleAnalyticsImporterInstruction()
{
$googleAnalyticsImporterInstruction = [];
if (!Manager::getInstance()->isPluginLoaded('GoogleAnalyticsImporter')) {
$googleAnalyticsImporterInstruction = [
'id' => 'GoogleAnalyticsImporter',
'name' => Piwik::translate('CoreAdminHome_ImportFromGoogleAnalytics'),
'type' => SiteContentDetectionAbstract::TYPE_OTHER,
'othersInstruction' => Piwik::translate(
'CoreAdminHome_ImportFromGoogleAnalyticsDescription',
['<a href="' . Url::addCampaignParametersToMatomoLink('https://plugins.matomo.org/GoogleAnalyticsImporter') . '" rel="noopener noreferrer" target="_blank">', '</a>']
),
];
}
/**
* @ignore
*/
Piwik::postEvent('SitesManager.siteWithoutData.customizeImporterMessage', [&$googleAnalyticsImporterInstruction]);
return $googleAnalyticsImporterInstruction;
}
private function renderIntegrationsTab($instructionUrls): string
{
$view = new View('@SitesManager/_integrationsTab');
$view->instruction = $this->getCmsInstruction();
$view->instructionUrls = $instructionUrls;
$view->trackers = $this->siteContentDetector->getDetectsByType(SiteContentDetectionAbstract::TYPE_TRACKER);
$view->idSite = $this->idSite;
$view->matomoUrl = Url::getCurrentUrlWithoutFileName();
return $view->render();
}
private function renderOthersTab($othersInstructions): string
{
array_unshift(
$othersInstructions,
[
'id' => 'ImageTracking',
'name' => Piwik::translate('CoreAdminHome_ImageTracking'),
'type' => SiteContentDetectionAbstract::TYPE_OTHER,
'othersInstruction' => Piwik::translate(
'SitesManager_ImageTrackingDescription',
['<a href="' . Url::addCampaignParametersToMatomoLink('https://matomo.org/docs/tracking-api/reference/') . '" rel="noreferrer noopener" target="_blank">', '</a>']
),
],
[
'id' => 'LogAnalytics',
'name' => Piwik::translate('SitesManager_LogAnalytics'),
'type' => SiteContentDetectionAbstract::TYPE_OTHER,
'othersInstruction' => Piwik::translate(
'SitesManager_LogAnalyticsDescription',
['<a href="' . Url::addCampaignParametersToMatomoLink('https://matomo.org/log-analytics/') . '" rel="noreferrer noopener" target="_blank">', '</a>']
),
],
[
'id' => 'MobileAppsAndSDKs',
'name' => Piwik::translate('SitesManager_MobileAppsAndSDKs'),
'type' => SiteContentDetectionAbstract::TYPE_OTHER,
'othersInstruction' => Piwik::translate(
'SitesManager_MobileAppsAndSDKsDescription',
['<a href="' . Url::addCampaignParametersToMatomoLink('https://matomo.org/integrate/#programming-language-platforms-and-frameworks') . '" rel="noreferrer noopener" target="_blank">', '</a>']
),
],
[
'id' => 'HttpTrackingApi',
'name' => Piwik::translate('CoreAdminHome_HttpTrackingApi'),
'type' => SiteContentDetectionAbstract::TYPE_OTHER,
'othersInstruction' => Piwik::translate(
'CoreAdminHome_HttpTrackingApiDescription',
['<a href="' . Url::addCampaignParametersToMatomoLink('https://developer.matomo.org/api-reference/tracking-api') . '" rel="noreferrer noopener" target="_blank">', '</a>']
),
]
);
$googleAnalyticsImporterInstruction = $this->getGoogleAnalyticsImporterInstruction();
if (!empty($googleAnalyticsImporterInstruction)) {
$othersInstructions[] = $googleAnalyticsImporterInstruction;
}
$view = new View('@SitesManager/_othersTab');
$view->othersInstructions = $othersInstructions;
$view->idSite = $this->idSite;
$view->matomoUrl = Url::getCurrentUrlWithoutFileName();
return $view->render();
}
private function getCmsInstruction()
{
$detectedCMSes = $this->siteContentDetector->getDetectsByType(SiteContentDetectionAbstract::TYPE_CMS);
if (
empty($detectedCMSes)
|| $this->siteContentDetector->wasDetected(WordPress::getId())
) {
return '';
}
$detectedCms = null;
foreach ($detectedCMSes as $detected) {
$detectedCms = $this->siteContentDetector->getSiteContentDetectionById($detected);
if (null !== $detectedCms && !empty($detectedCms::getInstructionUrl())) {
break;
}
}
if (null === $detectedCms || empty($detectedCms::getInstructionUrl())) {
return '';
}
return Piwik::translate(
'SitesManager_SiteWithoutDataDetectedSite',
[
$detectedCms::getName(),
'<a target="_blank" rel="noreferrer noopener" href="' . $detectedCms::getInstructionUrl() . '">',
'</a>',
]
);
}
private function getInviteUserLink()
{
$request = \Piwik\Request::fromRequest();
$idSite = $request->getIntegerParameter('idSite', 0);
if (!$idSite || !Piwik::isUserHasAdminAccess($idSite)) {
return Url::addCampaignParametersToMatomoLink('https://matomo.org/faq/general/manage-users/#imanadmin-creating-users');
}
return SettingsPiwik::getPiwikUrl() . 'index.php?' . Url::getQueryStringFromParameters([
'idSite' => $idSite,
'module' => 'UsersManager',
'action' => 'index',
]);
}
}
|