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
|
<?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\JsTrackerInstallCheck;
use Piwik\Piwik;
use Piwik\Site;
use Piwik\UrlHelper;
/**
* @internal
*/
class API extends \Piwik\Plugin\API
{
/**
* @var JsTrackerInstallCheck
*/
protected $jsTrackerInstallCheck;
// disables automatic sanitizing for all public API methods in this class
protected $autoSanitizeInputParams = false;
public function __construct(JsTrackerInstallCheck $jsTrackerInstallCheck)
{
$this->jsTrackerInstallCheck = $jsTrackerInstallCheck;
}
/**
* Check whether a test request has been recorded for the provided nonce. If no request has been recorded or the
* nonce isn't found, return false. This also returns the main URL for the specified site so that we can auto-
* populate an input with it.
*
* @param int $idSite
* @param string $nonce Optional nonce string. If provided, it validates whether the success response matches the
* provided nonce. If omitted, it simply returns the most recent result for the provided site.
* @return array Indicates whether the check was successful and provides the main URL for the specified site.
* E.g. ['isSuccess' => true, 'mainUrl' => 'https://some-test-site.com']
* @throws \Exception If the user doesn't have the right permissions
*/
public function wasJsTrackerInstallTestSuccessful(int $idSite, string $nonce = ''): array
{
Piwik::checkUserHasViewAccess($idSite);
if (!empty($nonce) && !\preg_match('/^[a-f0-9]{32}$/i', $nonce)) {
throw new \Exception('The provided nonce is invalid.');
}
return [
'isSuccess' => $this->jsTrackerInstallCheck->checkForJsTrackerInstallTestSuccess($idSite, $nonce),
'mainUrl' => Site::getMainUrlFor($idSite),
];
}
/**
* Initiate a test whether the JS tracking code has been successfully installed for a site. It generates a nonce and
* stores it in the option table so that it can be accessed later during the Tracker.isExcludedVisit event.
*
* @param int $idSite
* @param string $url Optional URL to append the nonce to. If not provided, it uses the main URL of the site
* @return array containing the URL constructed using the main URL for the site and the newly created nonce as a
* query parameter.
* E.g. ['url' => 'https://some-site.com?tracker_install_check=c3dfa1abbbab6381baca0793b8dd5d', 'nonce' => 'c3dfa1abbbab6381baca0793b8dd5d']
* @throws \Exception If the user doesn't have the right permissions
*/
public function initiateJsTrackerInstallTest(int $idSite, string $url = ''): array
{
Piwik::checkUserHasViewAccess($idSite);
if (!empty($url) && !UrlHelper::isLookLikeUrl($url)) {
throw new \Exception(Piwik::translate('SitesManager_ExceptionInvalidUrl', $url));
}
return $this->jsTrackerInstallCheck->initiateJsTrackerInstallTest($idSite, $url);
}
}
|