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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\SitesManager;
use Piwik\Access;
use Piwik\API\Request;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Exception\UnexpectedWebsiteFoundException;
use Piwik\Piwik;
use Piwik\Plugins\CoreHome\SystemSummary;
use Piwik\Plugins\PrivacyManager\PrivacyManager;
use Piwik\Settings\Storage\Backend\MeasurableSettingsTable;
use Piwik\Tracker\Cache;
use Piwik\Tracker\Model as TrackerModel;
use Piwik\Session\SessionNamespace;
/**
*
*/
class SitesManager extends \Piwik\Plugin
{
const KEEP_URL_FRAGMENT_USE_DEFAULT = 0;
const KEEP_URL_FRAGMENT_YES = 1;
const KEEP_URL_FRAGMENT_NO = 2;
/**
* @see \Piwik\Plugin::registerEvents
*/
public function registerEvents()
{
return array(
'AssetManager.getJavaScriptFiles' => 'getJsFiles',
'AssetManager.getStylesheetFiles' => 'getStylesheetFiles',
'Tracker.Cache.getSiteAttributes' => array('function' => 'recordWebsiteDataInCache', 'before' => true),
'Tracker.setTrackerCacheGeneral' => 'setTrackerCacheGeneral',
'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys',
'SitesManager.deleteSite.end' => 'onSiteDeleted',
'System.addSystemSummaryItems' => 'addSystemSummaryItems',
'Request.dispatch' => 'redirectDashboardToWelcomePage',
);
}
public function addSystemSummaryItems(&$systemSummary)
{
$websites = Request::processRequest('SitesManager.getAllSites', array('filter_limit' => '-1'));
$numWebsites = count($websites);
$systemSummary[] = new SystemSummary\Item($key = 'websites', Piwik::translate('CoreHome_SystemSummaryNWebsites', $numWebsites), $value = null, $url = array('module' => 'SitesManager', 'action' => 'index'), $icon = '', $order = 10);
}
public function redirectDashboardToWelcomePage(&$module, &$action)
{
if ($module !== 'CoreHome' || $action !== 'index') {
return;
}
$siteId = Common::getRequestVar('idSite', false, 'int');
if (!$siteId) {
return;
}
// Skip the screen if purging logs is enabled
$settings = PrivacyManager::getPurgeDataSettings();
if ($settings['delete_logs_enable'] == 1) {
return;
}
if (self::hasTrackedAnyTraffic($siteId)) {
$session = new SessionNamespace('siteWithoutData');
if (!empty($session->ignoreMessage)) {
return;
}
$module = 'SitesManager';
$action = 'siteWithoutData';
}
}
public static function hasTrackedAnyTraffic($siteId)
{
$shouldPerformEmptySiteCheck = true;
/**
* Posted before checking to display the "No data has been recorded yet" message.
* If your Measurable should never have visits, you can use this event to make
* sure that message is never displayed.
*
* @param bool &$shouldPerformEmptySiteCheck Set this value to true to perform the
* check, false if otherwise.
* @param int $siteId The ID of the site we would perform a check for.
*/
Piwik::postEvent('SitesManager.shouldPerformEmptySiteCheck', [&$shouldPerformEmptySiteCheck, $siteId]);
$trackerModel = new TrackerModel();
return $shouldPerformEmptySiteCheck && $trackerModel->isSiteEmpty($siteId);
}
public function onSiteDeleted($idSite)
{
// we do not delete logs here on purpose (you can run these queries on the log_ tables to delete all data)
Cache::deleteCacheWebsiteAttributes($idSite);
$archiveInvalidator = StaticContainer::get('Piwik\Archive\ArchiveInvalidator');
$archiveInvalidator->forgetRememberedArchivedReportsToInvalidateForSite($idSite);
MeasurableSettingsTable::removeAllSettingsForSite($idSite);
}
/**
* Get CSS files
*/
public function getStylesheetFiles(&$stylesheets)
{
$stylesheets[] = "plugins/SitesManager/stylesheets/SitesManager.less";
}
/**
* Get JavaScript files
*/
public function getJsFiles(&$jsFiles)
{
$jsFiles[] = "plugins/SitesManager/angularjs/sites-manager/api-helper.service.js";
$jsFiles[] = "plugins/SitesManager/angularjs/sites-manager/api-site.service.js";
$jsFiles[] = "plugins/SitesManager/angularjs/sites-manager/api-core.service.js";
$jsFiles[] = "plugins/SitesManager/angularjs/sites-manager/sites-manager-type-model.js";
$jsFiles[] = "plugins/SitesManager/angularjs/sites-manager/sites-manager-admin-sites-model.js";
$jsFiles[] = "plugins/SitesManager/angularjs/sites-manager/multiline-field.directive.js";
$jsFiles[] = "plugins/SitesManager/angularjs/sites-manager/edit-trigger.directive.js";
$jsFiles[] = "plugins/SitesManager/angularjs/sites-manager/sites-manager.controller.js";
$jsFiles[] = "plugins/SitesManager/angularjs/sites-manager/sites-manager-site.controller.js";
}
/**
* Hooks when a website tracker cache is flushed (website updated, cache deleted, or empty cache)
* Will record in the tracker config file all data needed for this website in Tracker.
*
* @param array $array
* @param int $idSite
* @return void
*/
public function recordWebsiteDataInCache(&$array, $idSite)
{
$idSite = (int) $idSite;
$website = API::getInstance()->getSiteFromId($idSite);
$urls = API::getInstance()->getSiteUrlsFromId($idSite);
// add the 'hosts' entry in the website array
$array['urls'] = $urls;
$array['hosts'] = $this->getTrackerHosts($urls);
$array['exclude_unknown_urls'] = $website['exclude_unknown_urls'];
$array['excluded_ips'] = $this->getTrackerExcludedIps($website);
$array['excluded_parameters'] = self::getTrackerExcludedQueryParameters($website);
$array['excluded_user_agents'] = self::getExcludedUserAgents($website);
$array['keep_url_fragment'] = self::shouldKeepURLFragmentsFor($website);
$array['sitesearch'] = $website['sitesearch'];
$array['sitesearch_keyword_parameters'] = $this->getTrackerSearchKeywordParameters($website);
$array['sitesearch_category_parameters'] = $this->getTrackerSearchCategoryParameters($website);
$array['timezone'] = $this->getTimezoneFromWebsite($website);
$array['ts_created'] = $website['ts_created'];
$array['type'] = $website['type'];
}
public function setTrackerCacheGeneral(&$cache)
{
Access::doAsSuperUser(function () use (&$cache) {
$cache['global_excluded_user_agents'] = self::filterBlankFromCommaSepList(API::getInstance()->getExcludedUserAgentsGlobal());
$cache['global_excluded_ips'] = self::filterBlankFromCommaSepList(API::getInstance()->getExcludedIpsGlobal());
});
}
/**
* Returns whether we should keep URL fragments for a specific site.
*
* @param array $site DB data for the site.
* @return bool
*/
private static function getTimezoneFromWebsite($site)
{
if (!empty($site['timezone'])) {
return $site['timezone'];
}
}
/**
* Returns whether we should keep URL fragments for a specific site.
*
* @param array $site DB data for the site.
* @return bool
*/
private static function shouldKeepURLFragmentsFor($site)
{
if ($site['keep_url_fragment'] == self::KEEP_URL_FRAGMENT_YES) {
return true;
} else if ($site['keep_url_fragment'] == self::KEEP_URL_FRAGMENT_NO) {
return false;
}
return API::getInstance()->getKeepURLFragmentsGlobal();
}
private function getTrackerSearchKeywordParameters($website)
{
$searchParameters = $website['sitesearch_keyword_parameters'];
if (empty($searchParameters)) {
$searchParameters = API::getInstance()->getSearchKeywordParametersGlobal();
}
return explode(",", $searchParameters);
}
private function getTrackerSearchCategoryParameters($website)
{
$searchParameters = $website['sitesearch_category_parameters'];
if (empty($searchParameters)) {
$searchParameters = API::getInstance()->getSearchCategoryParametersGlobal();
}
return explode(",", $searchParameters);
}
/**
* Returns the array of excluded IPs to save in the config file
*
* @param array $website
* @return array
*/
private function getTrackerExcludedIps($website)
{
$excludedIps = $website['excluded_ips'];
$globalExcludedIps = API::getInstance()->getExcludedIpsGlobal();
$excludedIps .= ',' . $globalExcludedIps;
$ipRanges = array();
foreach (explode(',', $excludedIps) as $ip) {
$ipRange = API::getInstance()->getIpsForRange($ip);
if ($ipRange !== false) {
$ipRanges[] = $ipRange;
}
}
return $ipRanges;
}
/**
* Returns the array of excluded user agent substrings for a site. Filters out
* any garbage data & trims each entry.
*
* @param array $website The full set of information for a site.
* @return array
*/
private static function getExcludedUserAgents($website)
{
$excludedUserAgents = API::getInstance()->getExcludedUserAgentsGlobal();
if (API::getInstance()->isSiteSpecificUserAgentExcludeEnabled()) {
$excludedUserAgents .= ',' . $website['excluded_user_agents'];
}
return self::filterBlankFromCommaSepList($excludedUserAgents);
}
/**
* Returns the array of URL query parameters to exclude from URLs
*
* @param array $website
* @return array
*/
public static function getTrackerExcludedQueryParameters($website)
{
$excludedQueryParameters = $website['excluded_parameters'];
$globalExcludedQueryParameters = API::getInstance()->getExcludedQueryParametersGlobal();
$excludedQueryParameters .= ',' . $globalExcludedQueryParameters;
return self::filterBlankFromCommaSepList($excludedQueryParameters);
}
/**
* Trims each element of a comma-separated list of strings, removes empty elements and
* returns the result (as an array).
*
* @param string $parameters The unfiltered list.
* @return array The filtered list of strings as an array.
*/
private static function filterBlankFromCommaSepList($parameters)
{
$parameters = explode(',', $parameters);
$parameters = array_filter($parameters, 'strlen');
$parameters = array_unique($parameters);
return $parameters;
}
/**
* Returns the hosts alias URLs
* @param int $idSite
* @return array
*/
private function getTrackerHosts($urls)
{
$hosts = array();
foreach ($urls as $url) {
$url = parse_url($url);
if (isset($url['host'])) {
$hosts[] = $url['host'];
}
}
return $hosts;
}
public function getClientSideTranslationKeys(&$translationKeys)
{
$translationKeys[] = "General_Save";
$translationKeys[] = "General_OrCancel";
$translationKeys[] = "General_Actions";
$translationKeys[] = "General_Search";
$translationKeys[] = "General_Previous";
$translationKeys[] = "General_Next";
$translationKeys[] = "General_Pagination";
$translationKeys[] = "General_Cancel";
$translationKeys[] = "General_ClickToSearch";
$translationKeys[] = "General_PaginationWithoutTotal";
$translationKeys[] = "General_Loading";
$translationKeys[] = "Actions_SubmenuSitesearch";
$translationKeys[] = "SitesManager_OnlyOneSiteAtTime";
$translationKeys[] = "SitesManager_DeleteConfirm";
$translationKeys[] = "SitesManager_Urls";
$translationKeys[] = "SitesManager_ExcludedIps";
$translationKeys[] = "SitesManager_ExcludedParameters";
$translationKeys[] = "SitesManager_ExcludedUserAgents";
$translationKeys[] = "SitesManager_Timezone";
$translationKeys[] = "SitesManager_Currency";
$translationKeys[] = "SitesManager_ShowTrackingTag";
$translationKeys[] = "SitesManager_AliasUrlHelp";
$translationKeys[] = "SitesManager_OnlyMatchedUrlsAllowed";
$translationKeys[] = "SitesManager_OnlyMatchedUrlsAllowedHelp";
$translationKeys[] = "SitesManager_OnlyMatchedUrlsAllowedHelpExamples";
$translationKeys[] = "SitesManager_KeepURLFragmentsLong";
$translationKeys[] = "SitesManager_HelpExcludedIpAddresses";
$translationKeys[] = "SitesManager_ListOfQueryParametersToExclude";
$translationKeys[] = "SitesManager_PiwikWillAutomaticallyExcludeCommonSessionParameters";
$translationKeys[] = "SitesManager_GlobalExcludedUserAgentHelp1";
$translationKeys[] = "SitesManager_GlobalListExcludedUserAgents_Desc";
$translationKeys[] = "SitesManager_GlobalExcludedUserAgentHelp2";
$translationKeys[] = "SitesManager_WebsitesManagement";
$translationKeys[] = "SitesManager_MainDescription";
$translationKeys[] = "SitesManager_YouCurrentlyHaveAccessToNWebsites";
$translationKeys[] = "SitesManager_SuperUserAccessCan";
$translationKeys[] = "SitesManager_EnableSiteSearch";
$translationKeys[] = "SitesManager_DisableSiteSearch";
$translationKeys[] = "SitesManager_SearchUseDefault";
$translationKeys[] = "SitesManager_Sites";
$translationKeys[] = "SitesManager_SiteSearchUse";
$translationKeys[] = "SitesManager_SearchKeywordLabel";
$translationKeys[] = "SitesManager_SearchCategoryLabel";
$translationKeys[] = "SitesManager_YourCurrentIpAddressIs";
$translationKeys[] = "SitesManager_SearchKeywordParametersDesc";
$translationKeys[] = "SitesManager_SearchCategoryParametersDesc";
$translationKeys[] = "SitesManager_CurrencySymbolWillBeUsedForGoals";
$translationKeys[] = "SitesManager_ChangingYourTimezoneWillOnlyAffectDataForward";
$translationKeys[] = "SitesManager_AdvancedTimezoneSupportNotFound";
$translationKeys[] = "SitesManager_UTCTimeIs";
$translationKeys[] = "SitesManager_EnableEcommerce";
$translationKeys[] = "SitesManager_NotAnEcommerceSite";
$translationKeys[] = "SitesManager_EcommerceHelp";
$translationKeys[] = "SitesManager_PiwikOffersEcommerceAnalytics";
$translationKeys[] = "SitesManager_GlobalWebsitesSettings";
$translationKeys[] = "SitesManager_GlobalListExcludedIps";
$translationKeys[] = "SitesManager_ListOfIpsToBeExcludedOnAllWebsites";
$translationKeys[] = "SitesManager_GlobalListExcludedQueryParameters";
$translationKeys[] = "SitesManager_ListOfQueryParametersToBeExcludedOnAllWebsites";
$translationKeys[] = "SitesManager_GlobalListExcludedUserAgents";
$translationKeys[] = "SitesManager_EnableSiteSpecificUserAgentExclude_Help";
$translationKeys[] = "SitesManager_EnableSiteSpecificUserAgentExclude";
$translationKeys[] = "SitesManager_KeepURLFragments";
$translationKeys[] = "SitesManager_KeepURLFragmentsHelp";
$translationKeys[] = "SitesManager_KeepURLFragmentsHelp2";
$translationKeys[] = "SitesManager_TrackingSiteSearch";
$translationKeys[] = "SitesManager_SearchParametersNote";
$translationKeys[] = "SitesManager_SearchParametersNote2";
$translationKeys[] = "SitesManager_SearchCategoryDesc";
$translationKeys[] = "SitesManager_DefaultTimezoneForNewWebsites";
$translationKeys[] = "SitesManager_SelectDefaultTimezone";
$translationKeys[] = "SitesManager_DefaultCurrencyForNewWebsites";
$translationKeys[] = "SitesManager_SelectDefaultCurrency";
$translationKeys[] = "SitesManager_AddMeasurable";
$translationKeys[] = "SitesManager_AddSite";
$translationKeys[] = "SitesManager_XManagement";
$translationKeys[] = "SitesManager_ChooseMeasurableTypeHeadline";
$translationKeys[] = "SitesManager_Type";
$translationKeys[] = "General_Measurables";
$translationKeys[] = "Goals_Ecommerce";
$translationKeys[] = "SitesManager_NotFound";
$translationKeys[] = "SitesManager_DeleteSiteExplanation";
}
}
|