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
|
<?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\Live;
use Piwik\Cache;
use Piwik\API\Request;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Plugins\FeatureFlags\FeatureFlagManager;
use Piwik\Site;
use Piwik\Plugins\Live\Settings\VisitorLogDisabled as VisitorLogDisabledSetting;
use Piwik\Plugins\PrivacyManager\FeatureFlags\PrivacyCompliance;
/**
*
*/
class Live extends \Piwik\Plugin
{
/**
* @see \Piwik\Plugin::registerEvents
*/
public function registerEvents()
{
return [
'AssetManager.getJavaScriptFiles' => 'getJsFiles',
'AssetManager.getStylesheetFiles' => 'getStylesheetFiles',
'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys',
'Live.renderAction' => 'renderAction',
'Live.renderActionTooltip' => 'renderActionTooltip',
'Live.renderVisitorDetails' => 'renderVisitorDetails',
'Live.renderVisitorIcons' => 'renderVisitorIcons',
'Template.jsGlobalVariables' => 'addJsGlobalVariables',
'API.getPagesComparisonsDisabledFor' => 'getPagesComparisonsDisabledFor',
];
}
public function getPagesComparisonsDisabledFor(&$pages)
{
$pages[] = 'General_Visitors.Live_VisitorLog';
$pages[] = 'General_Visitors.General_RealTime';
}
public function addJsGlobalVariables(&$out)
{
$actionsToDisplayCollapsed = (int)StaticContainer::get('Live.pageViewActionsToDisplayCollapsed');
$out .= "
piwik.visitorLogEnabled = " . json_encode(self::isVisitorLogEnabled()) . ";
piwik.visitorProfileEnabled = " . json_encode(self::isVisitorProfileEnabled()) . ";
piwik.visitorLogActionsToDisplayCollapsed = $actionsToDisplayCollapsed;
";
}
/**
* Throws an exception if visits log is disabled
*
* @param null|int|array $idSite
* @throws \Exception
*/
public static function checkIsVisitorLogEnabled($idSite = null): void
{
$featureFlagManager = StaticContainer::get(FeatureFlagManager::class);
if ($featureFlagManager->isFeatureActive(PrivacyCompliance::class)) {
if (VisitorLogDisabledSetting::getInstance()->getValue() === true) {
throw new \Exception('Visits log is deactivated globally. A user with super user access can enable this feature in the general settings.');
}
} else {
$systemSettings = new SystemSettings();
if ($systemSettings->disableVisitorLog->getValue() === true) {
throw new \Exception('Visits log is deactivated globally. A user with super user access can enable this feature in the general settings.');
}
}
if (empty($idSite)) {
$idSite = Common::getRequestVar('idSite', '', 'string');
}
if (!empty($idSite)) {
$idSites = Site::getIdSitesFromIdSitesString($idSite);
foreach ($idSites as $idSite) {
if ($featureFlagManager->isFeatureActive(PrivacyCompliance::class)) {
if (VisitorLogDisabledSetting::getInstance($idSite)->getValue() === true) {
throw new \Exception('Visits log is deactivated in website settings. A user with at least admin access can enable this feature in the settings for this website (idSite=' . $idSite . ').');
}
} else {
$settings = new MeasurableSettings($idSite);
if ($settings->disableVisitorLog->getValue() === true) {
throw new \Exception('Visits log is deactivated in website settings. A user with at least admin access can enable this feature in the settings for this website (idSite=' . $idSite . ').');
}
}
}
}
}
/**
* Returns whether visits log is enabled (for the given site)
*
* @param null|int|array $idSite
*/
public static function isVisitorLogEnabled($idSite = null): bool
{
try {
self::checkIsVisitorLogEnabled($idSite);
} catch (\Exception $e) {
return false;
}
return true;
}
/**
* Throws an exception if visitor profile is disabled
*
* @param null|int|array $idSite
* @throws \Exception
*/
public static function checkIsVisitorProfileEnabled($idSite = null): void
{
self::checkIsVisitorLogEnabled($idSite); // visitor log is required for visitor profile
$systemSettings = new SystemSettings();
if ($systemSettings->disableVisitorProfile->getValue() === true) {
throw new \Exception('Visitor profile is deactivated globally. A user with super user access can enable this feature in the general settings.');
}
if (empty($idSite)) {
$idSite = Common::getRequestVar('idSite', '', 'string');
}
if (!empty($idSite)) {
$idSites = Site::getIdSitesFromIdSitesString($idSite);
foreach ($idSites as $idSite) {
$settings = new MeasurableSettings($idSite);
if ($settings->disableVisitorProfile->getValue() === true) {
throw new \Exception('Visitor profile is deactivated in website settings. A user with at least admin access can enable this feature in the settings for this website (idSite=' . $idSite . ').');
}
}
}
}
/**
* Returns whether visitor profile is enabled (for the given site)
*
* @param null|int|array $idSite
*/
public static function isVisitorProfileEnabled($idSite = null): bool
{
try {
self::checkIsVisitorProfileEnabled($idSite);
} catch (\Exception $e) {
return false;
}
return true;
}
public function getStylesheetFiles(&$stylesheets)
{
$stylesheets[] = "plugins/Live/stylesheets/live.less";
$stylesheets[] = "plugins/Live/stylesheets/visitor_profile.less";
}
public function getJsFiles(&$jsFiles)
{
$jsFiles[] = "node_modules/visibilityjs/lib/visibility.core.js";
$jsFiles[] = "plugins/Live/javascripts/live.js";
$jsFiles[] = "plugins/Live/javascripts/SegmentedVisitorLog.js";
$jsFiles[] = "plugins/Live/javascripts/visitorActions.js";
$jsFiles[] = "plugins/Live/javascripts/visitorProfile.js";
$jsFiles[] = "plugins/Live/javascripts/visitorLog.js";
$jsFiles[] = "plugins/Live/javascripts/rowaction.js";
}
public function getClientSideTranslationKeys(&$translationKeys)
{
$translationKeys[] = "Live_VisitorProfile";
$translationKeys[] = "Live_ClickToViewAllActions";
$translationKeys[] = "Live_NoMoreVisits";
$translationKeys[] = "Live_ShowMap";
$translationKeys[] = "Live_HideMap";
$translationKeys[] = "Live_PageRefreshed";
$translationKeys[] = "Live_RowActionTooltipTitle";
$translationKeys[] = "Live_RowActionTooltipDefault";
$translationKeys[] = "Live_RowActionTooltipWithDimension";
$translationKeys[] = "Live_SegmentedVisitorLogTitle";
$translationKeys[] = "General_Segment";
$translationKeys[] = "General_And";
$translationKeys[] = 'Live_ClickToSeeAllContents';
$translationKeys[] = 'General_ColumnNbVisits';
$translationKeys[] = 'Live_LastHours';
$translationKeys[] = 'Live_LastMinutes';
$translationKeys[] = 'Live_VisitorsInRealTime';
$translationKeys[] = 'Live_OnClickPause';
$translationKeys[] = 'Live_OnClickStart';
$translationKeys[] = 'Live_LinkVisitorLog';
$translationKeys[] = 'Live_VisitorLog';
$translationKeys[] = 'General_ColumnNbVisitsDocumentation';
$translationKeys[] = 'General_ColumnNbActionsDocumentation';
}
public function renderAction(&$renderedAction, $action, $previousAction, $visitorDetails)
{
$visitorDetailsInstances = Visitor::getAllVisitorDetailsInstances();
foreach ($visitorDetailsInstances as $instance) {
$renderedAction .= $instance->renderAction($action, $previousAction, $visitorDetails);
}
}
public function renderActionTooltip(&$tooltip, $action, $visitInfo)
{
$detailEntries = [];
$visitorDetailsInstances = Visitor::getAllVisitorDetailsInstances();
foreach ($visitorDetailsInstances as $instance) {
$detailEntries = array_merge($detailEntries, $instance->renderActionTooltip($action, $visitInfo));
}
usort($detailEntries, function ($a, $b) {
return version_compare($a[0], $b[0]);
});
foreach ($detailEntries as $detailEntry) {
$tooltip .= $detailEntry[1];
}
}
public function renderVisitorDetails(&$renderedDetails, $visitorDetails)
{
$detailEntries = [];
$visitorDetailsInstances = Visitor::getAllVisitorDetailsInstances();
foreach ($visitorDetailsInstances as $instance) {
$detailEntries = array_merge($detailEntries, $instance->renderVisitorDetails($visitorDetails));
}
usort($detailEntries, function ($a, $b) {
return version_compare($a[0], $b[0]);
});
foreach ($detailEntries as $detailEntry) {
$renderedDetails .= $detailEntry[1];
}
}
public function renderVisitorIcons(&$renderedDetails, $visitorDetails)
{
$visitorDetailsInstances = Visitor::getAllVisitorDetailsInstances();
foreach ($visitorDetailsInstances as $instance) {
$renderedDetails .= $instance->renderIcons($visitorDetails);
}
}
/**
* Returns the segment for the most recent visitor id
*
* This method uses the transient cache to ensure it returns always the same id within one request
* as `Request::processRequest('Live.getMostRecentVisitorId')` might return different ids on each call
*
* @return mixed|string
*/
public static function getSegmentWithVisitorId()
{
$cache = Cache::getTransientCache();
$cacheId = 'segmentWithVisitorId';
if ($cache->contains($cacheId)) {
return $cache->fetch($cacheId);
}
$segment = Request::getRawSegmentFromRequest();
if (!empty($segment)) {
$segment = urldecode($segment) . ';';
}
$idVisitor = Common::getRequestVar('visitorId', false);
if ($idVisitor === false) {
$idVisitor = Request::processRequest('Live.getMostRecentVisitorId');
}
$result = urlencode($segment . 'visitorId==' . $idVisitor);
$cache->save($cacheId, $result);
return $result;
}
}
|