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