PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.8.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.8.2
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Site.php
matomo / app / core Last commit date
API 3 months ago Access 3 months ago Application 3 months ago Archive 3 months ago ArchiveProcessor 3 months ago Archiver 2 years ago AssetManager 3 months ago Auth 6 months ago Category 6 months ago Changes 3 months ago CliMulti 1 year ago Columns 3 months ago Concurrency 3 months ago Config 3 months ago Container 3 months ago CronArchive 3 months ago DataAccess 3 months ago DataFiles 2 years ago DataTable 3 months ago Db 3 months ago DeviceDetector 1 year ago Email 2 years ago Exception 4 months ago Http 4 months ago Intl 3 months ago Log 2 years ago Mail 1 year ago Measurable 6 months ago Menu 3 months ago Metrics 3 months ago Notification 6 months ago Period 3 months ago Plugin 3 months ago Policy 3 months ago ProfessionalServices 1 year ago Report 1 year ago ReportRenderer 3 months ago Request 3 months ago Scheduler 3 months ago Segment 3 months ago Session 3 months ago Settings 3 months ago Tracker 3 months ago Translation 3 months ago Twig 1 year ago UpdateCheck 3 months ago Updater 4 months ago Updates 3 months ago Validators 1 year ago View 6 months ago ViewDataTable 3 months ago Visualization 1 year ago Widget 3 months ago .htaccess 2 years ago Access.php 3 months ago Archive.php 3 months ago ArchiveProcessor.php 4 months ago AssetManager.php 3 months ago Auth.php 6 months ago AuthResult.php 6 months ago BaseFactory.php 2 years ago Cache.php 2 years ago CacheId.php 4 months ago CliMulti.php 3 months ago Common.php 3 months ago Config.php 3 months ago Console.php 3 months ago Context.php 2 years ago Cookie.php 1 year ago CronArchive.php 3 months ago DI.php 3 months ago DataArray.php 5 months ago DataTable.php 3 months ago Date.php 3 months ago Db.php 3 months ago DbHelper.php 3 months ago Development.php 1 year ago ErrorHandler.php 6 months ago EventDispatcher.php 1 year ago ExceptionHandler.php 4 months ago FileIntegrity.php 3 months ago Filechecks.php 1 year ago Filesystem.php 3 months ago FrontController.php 4 months ago Http.php 4 months ago IP.php 1 year ago Log.php 3 months ago LogDeleter.php 1 year ago Mail.php 1 year ago Metrics.php 3 months ago NoAccessException.php 2 years ago Nonce.php 6 months ago Notification.php 6 months ago NumberFormatter.php 5 months ago Option.php 5 months ago Period.php 3 months ago Piwik.php 3 months ago Plugin.php 3 months ago Process.php 1 year ago Profiler.php 6 months ago ProxyHeaders.php 4 months ago ProxyHttp.php 5 months ago QuickForm2.php 3 months ago RankingQuery.php 5 months ago ReportRenderer.php 3 months ago Request.php 3 months ago Segment.php 3 months ago Sequence.php 6 months ago Session.php 3 months ago SettingsPiwik.php 3 months ago SettingsServer.php 1 year ago Singleton.php 2 years ago Site.php 4 months ago SiteContentDetector.php 3 months ago SupportedBrowser.php 2 years ago TCPDF.php 1 year ago Theme.php 1 year ago Timer.php 2 years ago Tracker.php 3 months ago Twig.php 3 months ago Unzip.php 1 year ago UpdateCheck.php 3 months ago Updater.php 3 months ago UpdaterErrorException.php 2 years ago Updates.php 3 months ago Url.php 3 months ago UrlHelper.php 3 months ago Version.php 3 months ago View.php 3 months ago bootstrap.php 1 year ago dispatch.php 2 years ago testMinimumPhpVersion.php 6 months ago
Site.php
597 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 */
9 namespace Piwik;
10
11 use Exception;
12 use Piwik\Exception\UnexpectedWebsiteFoundException;
13 use Piwik\Http\BadRequestException;
14 use Piwik\Plugins\SitesManager\API;
15 /**
16 * Provides access to individual [site entity](/guides/persistence-and-the-mysql-backend#websites-aka-sites) data
17 * (including name, URL, etc.).
18 *
19 * **Data Cache**
20 *
21 * Site data can be cached in order to avoid performing too many queries.
22 * If a method needs many site entities, it is more efficient to query all of what
23 * you need beforehand via the **SitesManager** API, then cache it using {@link setSites()} or
24 * {@link setSitesFromArray()}.
25 *
26 * Subsequent calls to `new Site($id)` will use the data in the cache instead of querying the database.
27 *
28 * ### Examples
29 *
30 * **Basic usage**
31 *
32 * $site = new Site($idSite);
33 * $name = $site->getName();
34 *
35 * **Without allocation**
36 *
37 * $name = Site::getNameFor($idSite);
38 *
39 * @api
40 */
41 class Site
42 {
43 public const DEFAULT_SITE_TYPE = "website";
44 private static $intProperties = ['idsite', 'ecommerce', 'sitesearch', 'exclude_unknown_urls', 'keep_url_fragment'];
45 /**
46 * @var int|null
47 */
48 protected $id = null;
49 /**
50 * @var array
51 */
52 protected static $infoSites = array();
53 private $site = array();
54 /**
55 * Constructor.
56 *
57 * @param int $idsite The ID of the site we want data for.
58 * @throws UnexpectedWebsiteFoundException
59 */
60 public function __construct($idsite)
61 {
62 $this->id = (int) $idsite;
63 if (!empty(self::$infoSites[$this->id])) {
64 $site = self::$infoSites[$this->id];
65 } else {
66 $site = API::getInstance()->getSiteFromId($this->id);
67 if (empty($site)) {
68 throw new UnexpectedWebsiteFoundException('The requested website id = ' . (int) $this->id . ' couldn\'t be found');
69 }
70 }
71 $sites = array(&$site);
72 self::triggerSetSitesEvent($sites);
73 self::setSiteFromArray($this->id, $site);
74 $this->site = $site;
75 // for serialized format to be predictable across php/mysql/pdo/mysqli versions, make sure the int props stay ints
76 foreach (self::$intProperties as $propertyName) {
77 $this->site[$propertyName] = (int) $this->site[$propertyName];
78 }
79 }
80 /**
81 * Sets the cached site data with an array that associates site IDs with
82 * individual site data.
83 *
84 * @param array $sites The array of sites data. Indexed by site ID. eg,
85 *
86 * array('1' => array('name' => 'Site 1', ...),
87 * '2' => array('name' => 'Site 2', ...))`
88 */
89 public static function setSites($sites)
90 {
91 self::triggerSetSitesEvent($sites);
92 foreach ($sites as $idsite => $site) {
93 self::setSiteFromArray($idsite, $site);
94 }
95 }
96 private static function triggerSetSitesEvent(&$sites)
97 {
98 /**
99 * Triggered so plugins can modify website entities without modifying the database.
100 *
101 * This event should **not** be used to add data that is expensive to compute. If you
102 * need to make HTTP requests or query the database for more information, this is not
103 * the place to do it.
104 *
105 * **Example**
106 *
107 * Piwik::addAction('Site.setSites', function (&$sites) {
108 * foreach ($sites as &$site) {
109 * $site['name'] .= " (original)";
110 * }
111 * });
112 *
113 * @param array $sites An array of website entities. [Learn more.](/guides/persistence-and-the-mysql-backend#websites-aka-sites)
114 *
115 * This is not yet public as it doesn't work 100% accurately. Eg if `setSiteFromArray()` is called directly this event will not be triggered.
116 * @ignore
117 */
118 \Piwik\Piwik::postEvent('Site.setSites', array(&$sites));
119 }
120 /**
121 * Sets a site information in memory (statically cached).
122 *
123 * Plugins can filter the website attributes before it is cached, eg. to change the website name,
124 * creation date, etc.
125 *
126 * @param $idSite
127 * @param $infoSite
128 * @throws Exception if website or idsite is invalid
129 * @internal
130 */
131 public static function setSiteFromArray($idSite, $infoSite)
132 {
133 if (empty($idSite) || empty($infoSite)) {
134 throw new UnexpectedWebsiteFoundException("An unexpected website was found in the request: website id was set to '{$idSite}' .");
135 }
136 self::$infoSites[$idSite] = $infoSite;
137 }
138 /**
139 * Sets the cached Site data with a non-associated array of site data.
140 *
141 * This method will trigger the `Sites.setSites` event modifying `$sites` before setting cached
142 * site data. In other words, this method will change the site data before it is cached and then
143 * return the modified array.
144 *
145 * @param array $sites The array of sites data. eg,
146 *
147 * array(
148 * array('idsite' => '1', 'name' => 'Site 1', ...),
149 * array('idsite' => '2', 'name' => 'Site 2', ...),
150 * )
151 * @return array The modified array.
152 * @internal
153 */
154 public static function setSitesFromArray($sites)
155 {
156 self::triggerSetSitesEvent($sites);
157 foreach ($sites as $site) {
158 $idSite = null;
159 if (!empty($site['idsite'])) {
160 $idSite = $site['idsite'];
161 }
162 self::setSiteFromArray($idSite, $site);
163 }
164 return $sites;
165 }
166 /**
167 * The Multisites reports displays the first calendar date as the earliest day available for all websites.
168 * Also, today is the later "today" available across all timezones.
169 * @param array $siteIds Array of IDs for each site being displayed.
170 * @return Date[] of two Date instances. First is the min-date & the second
171 * is the max date.
172 * @ignore
173 */
174 public static function getMinMaxDateAcrossWebsites($siteIds)
175 {
176 $siteIds = self::getIdSitesFromIdSitesString($siteIds);
177 $now = \Piwik\Date::now();
178 $minDate = null;
179 $maxDate = $now->subDay(1)->getTimestamp();
180 foreach ($siteIds as $idsite) {
181 // look for 'now' in the website's timezone
182 $timezone = \Piwik\Site::getTimezoneFor($idsite);
183 $date = \Piwik\Date::adjustForTimezone($now->getTimestamp(), $timezone);
184 if ($date > $maxDate) {
185 $maxDate = $date;
186 }
187 // look for the absolute minimum date
188 $creationDate = \Piwik\Site::getCreationDateFor($idsite);
189 $date = \Piwik\Date::adjustForTimezone(strtotime($creationDate), $timezone);
190 if (is_null($minDate) || $date < $minDate) {
191 $minDate = $date;
192 }
193 }
194 return array(\Piwik\Date::factory($minDate), \Piwik\Date::factory($maxDate));
195 }
196 /**
197 * Returns a string representation of the site this instance references.
198 *
199 * Useful for debugging.
200 *
201 * @return string
202 */
203 public function __toString()
204 {
205 return "site id=" . $this->getId() . ",\n\t\t\t\t name=" . $this->getName() . ",\n\t\t\t\t url = " . $this->getMainUrl() . ",\n\t\t\t\t IPs excluded = " . $this->getExcludedIps() . ",\n\t\t\t\t timezone = " . $this->getTimezone() . ",\n\t\t\t\t currency = " . $this->getCurrency() . ",\n\t\t\t\t creation date = " . $this->getCreationDate();
206 }
207 /**
208 * Returns the name of the site.
209 *
210 * @return string
211 * @throws Exception if data for the site cannot be found.
212 */
213 public function getName()
214 {
215 return $this->get('name');
216 }
217 /**
218 * Returns the main url of the site.
219 *
220 * @return string
221 * @throws Exception if data for the site cannot be found.
222 */
223 public function getMainUrl()
224 {
225 return $this->get('main_url');
226 }
227 /**
228 * Returns the id of the site.
229 *
230 * @return int
231 * @throws Exception if data for the site cannot be found.
232 */
233 public function getId()
234 {
235 return $this->id;
236 }
237 /**
238 * Returns a site property by name.
239 *
240 * @param string $name Name of the property to return (eg, `'main_url'` or `'name'`).
241 * @return mixed
242 * @throws Exception
243 */
244 protected function get($name)
245 {
246 if (isset($this->site[$name])) {
247 return $this->site[$name];
248 }
249 throw new Exception("The property {$name} could not be found on the website ID " . (int) $this->id);
250 }
251 /**
252 * Returns the website type (by default `"website"`, which means it is a single website).
253 *
254 * @return string
255 */
256 public function getType()
257 {
258 $type = $this->get('type');
259 return $type;
260 }
261 /**
262 * Returns the creation date of the site.
263 *
264 * @return Date
265 * @throws Exception if data for the site cannot be found.
266 */
267 public function getCreationDate()
268 {
269 $date = $this->get('ts_created');
270 return \Piwik\Date::factory($date);
271 }
272 /**
273 * Returns the timezone of the size.
274 *
275 * @return string
276 * @throws Exception if data for the site cannot be found.
277 */
278 public function getTimezone()
279 {
280 return $this->get('timezone');
281 }
282 /**
283 * Returns the currency of the site.
284 *
285 * @return string
286 * @throws Exception if data for the site cannot be found.
287 */
288 public function getCurrency()
289 {
290 return $this->get('currency');
291 }
292 /**
293 * Returns the excluded ips of the site.
294 *
295 * @return string
296 * @throws Exception if data for the site cannot be found.
297 */
298 public function getExcludedIps()
299 {
300 return $this->get('excluded_ips');
301 }
302 /**
303 * Returns the excluded query parameters of the site.
304 *
305 * @return string
306 * @throws Exception if data for the site cannot be found.
307 */
308 public function getExcludedQueryParameters()
309 {
310 return $this->get('excluded_parameters');
311 }
312 /**
313 * Returns whether ecommerce is enabled for the site.
314 *
315 * @return bool
316 * @throws Exception if data for the site cannot be found.
317 */
318 public function isEcommerceEnabled()
319 {
320 return $this->get('ecommerce') == 1;
321 }
322 /**
323 * Returns the site search keyword query parameters for the site.
324 *
325 * @return string
326 * @throws Exception if data for the site cannot be found.
327 */
328 public function getSearchKeywordParameters()
329 {
330 return $this->get('sitesearch_keyword_parameters');
331 }
332 /**
333 * Returns the site search category query parameters for the site.
334 *
335 * @return string
336 * @throws Exception if data for the site cannot be found.
337 */
338 public function getSearchCategoryParameters()
339 {
340 return $this->get('sitesearch_category_parameters');
341 }
342 /**
343 * Returns whether Site Search Tracking is enabled for the site.
344 *
345 * @return bool
346 * @throws Exception if data for the site cannot be found.
347 */
348 public function isSiteSearchEnabled()
349 {
350 return $this->get('sitesearch') == 1;
351 }
352 /**
353 * Returns the user that created this site.
354 *
355 * @return string|null If null, the site was created before the creation user was tracked.
356 */
357 public function getCreatorLogin()
358 {
359 return $this->get('creator_login');
360 }
361 /**
362 * Checks the given string for valid site IDs and returns them as an array.
363 *
364 * @param string|array $ids Comma separated idSite list, eg, `'1,2,3,4'` or an array of IDs, eg,
365 * `array(1, 2, 3, 4)`.
366 * @param bool|string $_restrictSitesToLogin Implementation detail. Used only when running as a scheduled task.
367 * @param bool $throwOnInvalid If true, throw when an invalid id is supplied.
368 * @return array<string>|array<int> An array of valid, unique integers.
369 */
370 public static function getIdSitesFromIdSitesString($ids, $_restrictSitesToLogin = \false, bool $throwOnInvalid = \false) : array
371 {
372 if (empty($ids)) {
373 return [];
374 }
375 if ($ids === 'all' || $ids === ['all']) {
376 return API::getInstance()->getSitesIdWithAtLeastViewAccess($_restrictSitesToLogin);
377 }
378 if (is_bool($ids)) {
379 if ($throwOnInvalid) {
380 throw new BadRequestException("The parameter 'idSite=' contains an invalid value.");
381 }
382 return [];
383 }
384 if (!is_array($ids)) {
385 $ids = explode(',', $ids);
386 }
387 $validIds = [];
388 foreach ($ids as $id) {
389 $id = is_string($id) ? trim($id) : $id;
390 if (is_null($id) || $id === '') {
391 continue;
392 }
393 if (is_numeric($id) && (string) $id === (string) (int) $id && (int) $id > 0) {
394 $validIds[] = (int) $id;
395 continue;
396 }
397 if ($throwOnInvalid) {
398 throw new BadRequestException("The parameter 'idSite=' contains an invalid value.");
399 }
400 }
401 $validIds = array_filter($validIds);
402 return array_unique($validIds);
403 }
404 /**
405 * Clears the site data cache.
406 *
407 * See also {@link setSites()} and {@link setSitesFromArray()}.
408 */
409 public static function clearCache()
410 {
411 self::$infoSites = array();
412 }
413 /**
414 * Clears the site data cache.
415 *
416 * See also {@link setSites()} and {@link setSitesFromArray()}.
417 */
418 public static function clearCacheForSite($idSite)
419 {
420 $idSite = (int) $idSite;
421 unset(self::$infoSites[$idSite]);
422 }
423 /**
424 * Utility function. Returns the value of the specified field for the
425 * site with the specified ID.
426 *
427 * @param int $idsite The ID of the site whose data is being accessed.
428 * @param string $field The name of the field to get.
429 * @return string
430 */
431 protected static function getFor($idsite, $field)
432 {
433 if (!isset(self::$infoSites[$idsite])) {
434 $site = API::getInstance()->getSiteFromId($idsite);
435 self::setSiteFromArray($idsite, $site);
436 }
437 return self::$infoSites[$idsite][$field];
438 }
439 /**
440 * Returns all websites pre-cached
441 *
442 * @ignore
443 */
444 public static function getSites()
445 {
446 return self::$infoSites;
447 }
448 /**
449 * @ignore
450 */
451 public static function getSite($idsite)
452 {
453 $idsite = (int) $idsite;
454 if (!isset(self::$infoSites[$idsite])) {
455 $site = API::getInstance()->getSiteFromId($idsite);
456 self::setSiteFromArray($idsite, $site);
457 }
458 return self::$infoSites[$idsite];
459 }
460 /**
461 * Returns the name of the site with the specified ID.
462 *
463 * @param int $idsite The site ID.
464 * @return string
465 */
466 public static function getNameFor($idsite)
467 {
468 return self::getFor($idsite, 'name');
469 }
470 /**
471 * Returns the group of the site with the specified ID.
472 *
473 * @param int $idsite The site ID.
474 * @return string
475 */
476 public static function getGroupFor($idsite)
477 {
478 return self::getFor($idsite, 'group');
479 }
480 /**
481 * Returns the timezone of the site with the specified ID.
482 *
483 * @param int $idsite The site ID.
484 * @return string
485 */
486 public static function getTimezoneFor($idsite)
487 {
488 return self::getFor($idsite, 'timezone');
489 }
490 /**
491 * Returns the type of the site with the specified ID.
492 *
493 * @param $idsite
494 * @return string
495 */
496 public static function getTypeFor($idsite)
497 {
498 return self::getFor($idsite, 'type');
499 }
500 /**
501 * Returns the creation date of the site with the specified ID.
502 *
503 * @param int $idsite The site ID.
504 * @return string
505 */
506 public static function getCreationDateFor($idsite)
507 {
508 return self::getFor($idsite, 'ts_created');
509 }
510 /**
511 * Returns the url for the site with the specified ID.
512 *
513 * @param int $idsite The site ID.
514 * @return string
515 */
516 public static function getMainUrlFor($idsite)
517 {
518 return self::getFor($idsite, 'main_url');
519 }
520 /**
521 * Returns whether the site with the specified ID is ecommerce enabled or not.
522 *
523 * @param int $idsite The site ID.
524 * @return string
525 */
526 public static function isEcommerceEnabledFor($idsite)
527 {
528 return self::getFor($idsite, 'ecommerce') == 1;
529 }
530 /**
531 * Returns whether the site with the specified ID is Site Search enabled.
532 *
533 * @param int $idsite The site ID.
534 * @return string
535 */
536 public static function isSiteSearchEnabledFor($idsite)
537 {
538 return self::getFor($idsite, 'sitesearch') == 1;
539 }
540 /**
541 * Returns the currency of the site with the specified ID.
542 *
543 * @param int $idsite The site ID.
544 * @return string
545 */
546 public static function getCurrencyFor($idsite)
547 {
548 return self::getFor($idsite, 'currency');
549 }
550 /**
551 * Returns the currency of the site with the specified ID.
552 *
553 * @param int $idsite The site ID.
554 * @return string
555 */
556 public static function getCurrencySymbolFor($idsite)
557 {
558 $currencyCode = self::getCurrencyFor($idsite);
559 $key = 'Intl_CurrencySymbol_' . $currencyCode;
560 $symbol = \Piwik\Piwik::translate($key);
561 if ($key === $symbol) {
562 return $currencyCode;
563 }
564 return $symbol;
565 }
566 /**
567 * Returns the excluded IP addresses of the site with the specified ID.
568 *
569 * @param int $idsite The site ID.
570 * @return string
571 */
572 public static function getExcludedIpsFor($idsite)
573 {
574 return self::getFor($idsite, 'excluded_ips');
575 }
576 /**
577 * Returns the excluded query parameters for the site with the specified ID.
578 *
579 * @param int $idsite The site ID.
580 * @return string
581 */
582 public static function getExcludedQueryParametersFor($idsite)
583 {
584 return self::getFor($idsite, 'excluded_parameters');
585 }
586 /**
587 * Returns the user that created this site.
588 *
589 * @param int $idsite The site ID.
590 * @return string|null If null, the site was created before the creation user was tracked.
591 */
592 public static function getCreatorLoginFor($idsite)
593 {
594 return self::getFor($idsite, 'creator_login');
595 }
596 }
597