PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.0.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.0.1
5.13.0 5.12.1 5.12.0 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 2 years ago Access 2 years ago Application 2 years ago Archive 2 years ago ArchiveProcessor 2 years ago Archiver 2 years ago AssetManager 2 years ago Auth 2 years ago Category 2 years ago Changes 2 years ago CliMulti 2 years ago Columns 2 years ago Concurrency 2 years ago Config 2 years ago Container 2 years ago CronArchive 2 years ago DataAccess 2 years ago DataFiles 2 years ago DataTable 2 years ago Db 2 years ago DeviceDetector 2 years ago Email 2 years ago Exception 2 years ago Http 2 years ago Intl 2 years ago Log 2 years ago Mail 2 years ago Measurable 2 years ago Menu 2 years ago Metrics 2 years ago Notification 2 years ago Period 2 years ago Plugin 2 years ago ProfessionalServices 2 years ago Report 2 years ago ReportRenderer 2 years ago Scheduler 2 years ago Segment 2 years ago Session 2 years ago Settings 2 years ago Tracker 2 years ago Translation 2 years ago Twig 2 years ago UpdateCheck 2 years ago Updater 2 years ago Updates 2 years ago Validators 2 years ago View 2 years ago ViewDataTable 2 years ago Visualization 2 years ago Widget 2 years ago .htaccess 2 years ago Access.php 2 years ago Archive.php 2 years ago ArchiveProcessor.php 2 years ago AssetManager.php 2 years ago Auth.php 2 years ago AuthResult.php 2 years ago BaseFactory.php 2 years ago Cache.php 2 years ago CacheId.php 2 years ago CliMulti.php 2 years ago Common.php 2 years ago Config.php 2 years ago Console.php 2 years ago Context.php 2 years ago Cookie.php 2 years ago CronArchive.php 2 years ago DI.php 2 years ago DataArray.php 2 years ago DataTable.php 2 years ago Date.php 2 years ago Db.php 2 years ago DbHelper.php 2 years ago Development.php 2 years ago ErrorHandler.php 2 years ago EventDispatcher.php 2 years ago ExceptionHandler.php 2 years ago FileIntegrity.php 2 years ago Filechecks.php 2 years ago Filesystem.php 2 years ago FrontController.php 2 years ago Http.php 2 years ago IP.php 2 years ago Log.php 2 years ago LogDeleter.php 2 years ago Mail.php 2 years ago Metrics.php 2 years ago NoAccessException.php 2 years ago Nonce.php 2 years ago Notification.php 2 years ago NumberFormatter.php 2 years ago Option.php 2 years ago Period.php 2 years ago Piwik.php 2 years ago Plugin.php 2 years ago Profiler.php 2 years ago ProxyHeaders.php 2 years ago ProxyHttp.php 2 years ago QuickForm2.php 2 years ago RankingQuery.php 2 years ago ReportRenderer.php 2 years ago Request.php 2 years ago Segment.php 2 years ago Sequence.php 2 years ago Session.php 2 years ago SettingsPiwik.php 2 years ago SettingsServer.php 2 years ago Singleton.php 2 years ago Site.php 2 years ago SiteContentDetector.php 2 years ago SupportedBrowser.php 2 years ago TCPDF.php 2 years ago Theme.php 2 years ago Timer.php 2 years ago Tracker.php 2 years ago Twig.php 2 years ago Unzip.php 2 years ago UpdateCheck.php 2 years ago Updater.php 2 years ago UpdaterErrorException.php 2 years ago Updates.php 2 years ago Url.php 2 years ago UrlHelper.php 2 years ago Version.php 2 years ago View.php 2 years ago bootstrap.php 2 years ago dispatch.php 2 years ago testMinimumPhpVersion.php 2 years ago
Site.php
587 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 *
9 */
10 namespace Piwik;
11
12 use Exception;
13 use Piwik\Exception\UnexpectedWebsiteFoundException;
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 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 * @return array An array of valid, unique integers.
368 */
369 public static function getIdSitesFromIdSitesString($ids, $_restrictSitesToLogin = false)
370 {
371 if (empty($ids)) {
372 return [];
373 }
374 if ($ids === 'all') {
375 return API::getInstance()->getSitesIdWithAtLeastViewAccess($_restrictSitesToLogin);
376 }
377 if (is_bool($ids)) {
378 return array();
379 }
380 if (!is_array($ids)) {
381 $ids = explode(',', $ids);
382 }
383 $validIds = array();
384 foreach ($ids as $id) {
385 $id = is_string($id) ? trim($id) : $id;
386 if (!empty($id) && is_numeric($id) && $id > 0) {
387 $validIds[] = $id;
388 }
389 }
390 $validIds = array_filter($validIds);
391 $validIds = array_unique($validIds);
392 return $validIds;
393 }
394 /**
395 * Clears the site data cache.
396 *
397 * See also {@link setSites()} and {@link setSitesFromArray()}.
398 */
399 public static function clearCache()
400 {
401 self::$infoSites = array();
402 }
403 /**
404 * Clears the site data cache.
405 *
406 * See also {@link setSites()} and {@link setSitesFromArray()}.
407 */
408 public static function clearCacheForSite($idSite)
409 {
410 $idSite = (int) $idSite;
411 unset(self::$infoSites[$idSite]);
412 }
413 /**
414 * Utility function. Returns the value of the specified field for the
415 * site with the specified ID.
416 *
417 * @param int $idsite The ID of the site whose data is being accessed.
418 * @param string $field The name of the field to get.
419 * @return string
420 */
421 protected static function getFor($idsite, $field)
422 {
423 if (!isset(self::$infoSites[$idsite])) {
424 $site = API::getInstance()->getSiteFromId($idsite);
425 self::setSiteFromArray($idsite, $site);
426 }
427 return self::$infoSites[$idsite][$field];
428 }
429 /**
430 * Returns all websites pre-cached
431 *
432 * @ignore
433 */
434 public static function getSites()
435 {
436 return self::$infoSites;
437 }
438 /**
439 * @ignore
440 */
441 public static function getSite($idsite)
442 {
443 $idsite = (int) $idsite;
444 if (!isset(self::$infoSites[$idsite])) {
445 $site = API::getInstance()->getSiteFromId($idsite);
446 self::setSiteFromArray($idsite, $site);
447 }
448 return self::$infoSites[$idsite];
449 }
450 /**
451 * Returns the name of the site with the specified ID.
452 *
453 * @param int $idsite The site ID.
454 * @return string
455 */
456 public static function getNameFor($idsite)
457 {
458 return self::getFor($idsite, 'name');
459 }
460 /**
461 * Returns the group of the site with the specified ID.
462 *
463 * @param int $idsite The site ID.
464 * @return string
465 */
466 public static function getGroupFor($idsite)
467 {
468 return self::getFor($idsite, 'group');
469 }
470 /**
471 * Returns the timezone of the site with the specified ID.
472 *
473 * @param int $idsite The site ID.
474 * @return string
475 */
476 public static function getTimezoneFor($idsite)
477 {
478 return self::getFor($idsite, 'timezone');
479 }
480 /**
481 * Returns the type of the site with the specified ID.
482 *
483 * @param $idsite
484 * @return string
485 */
486 public static function getTypeFor($idsite)
487 {
488 return self::getFor($idsite, 'type');
489 }
490 /**
491 * Returns the creation date of the site with the specified ID.
492 *
493 * @param int $idsite The site ID.
494 * @return string
495 */
496 public static function getCreationDateFor($idsite)
497 {
498 return self::getFor($idsite, 'ts_created');
499 }
500 /**
501 * Returns the url for the site with the specified ID.
502 *
503 * @param int $idsite The site ID.
504 * @return string
505 */
506 public static function getMainUrlFor($idsite)
507 {
508 return self::getFor($idsite, 'main_url');
509 }
510 /**
511 * Returns whether the site with the specified ID is ecommerce enabled or not.
512 *
513 * @param int $idsite The site ID.
514 * @return string
515 */
516 public static function isEcommerceEnabledFor($idsite)
517 {
518 return self::getFor($idsite, 'ecommerce') == 1;
519 }
520 /**
521 * Returns whether the site with the specified ID is Site Search enabled.
522 *
523 * @param int $idsite The site ID.
524 * @return string
525 */
526 public static function isSiteSearchEnabledFor($idsite)
527 {
528 return self::getFor($idsite, 'sitesearch') == 1;
529 }
530 /**
531 * Returns the currency of the site with the specified ID.
532 *
533 * @param int $idsite The site ID.
534 * @return string
535 */
536 public static function getCurrencyFor($idsite)
537 {
538 return self::getFor($idsite, 'currency');
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 getCurrencySymbolFor($idsite)
547 {
548 $currencyCode = self::getCurrencyFor($idsite);
549 $key = 'Intl_CurrencySymbol_' . $currencyCode;
550 $symbol = \Piwik\Piwik::translate($key);
551 if ($key === $symbol) {
552 return $currencyCode;
553 }
554 return $symbol;
555 }
556 /**
557 * Returns the excluded IP addresses of the site with the specified ID.
558 *
559 * @param int $idsite The site ID.
560 * @return string
561 */
562 public static function getExcludedIpsFor($idsite)
563 {
564 return self::getFor($idsite, 'excluded_ips');
565 }
566 /**
567 * Returns the excluded query parameters for the site with the specified ID.
568 *
569 * @param int $idsite The site ID.
570 * @return string
571 */
572 public static function getExcludedQueryParametersFor($idsite)
573 {
574 return self::getFor($idsite, 'excluded_parameters');
575 }
576 /**
577 * Returns the user that created this site.
578 *
579 * @param int $idsite The site ID.
580 * @return string|null If null, the site was created before the creation user was tracked.
581 */
582 public static function getCreatorLoginFor($idsite)
583 {
584 return self::getFor($idsite, 'creator_login');
585 }
586 }
587