API
5 years ago
Access
5 years ago
Application
5 years ago
Archive
5 years ago
ArchiveProcessor
5 years ago
Archiver
5 years ago
AssetManager
5 years ago
Auth
5 years ago
Category
5 years ago
CliMulti
5 years ago
Columns
5 years ago
Composer
5 years ago
Concurrency
5 years ago
Config
5 years ago
Container
5 years ago
CronArchive
5 years ago
DataAccess
5 years ago
DataFiles
5 years ago
DataTable
5 years ago
Db
5 years ago
DeviceDetector
5 years ago
Email
5 years ago
Exception
5 years ago
Http
5 years ago
Intl
5 years ago
Mail
5 years ago
Measurable
5 years ago
Menu
5 years ago
Metrics
5 years ago
Notification
5 years ago
Period
5 years ago
Plugin
5 years ago
ProfessionalServices
5 years ago
Report
5 years ago
ReportRenderer
5 years ago
Scheduler
5 years ago
Segment
5 years ago
Session
5 years ago
Settings
5 years ago
Tracker
5 years ago
Translation
5 years ago
UpdateCheck
5 years ago
Updater
5 years ago
Updates
5 years ago
Validators
5 years ago
View
5 years ago
ViewDataTable
5 years ago
Visualization
5 years ago
Widget
5 years ago
.htaccess
6 years ago
Access.php
5 years ago
Archive.php
5 years ago
ArchiveProcessor.php
5 years ago
AssetManager.php
5 years ago
Auth.php
5 years ago
AuthResult.php
5 years ago
BaseFactory.php
5 years ago
Cache.php
5 years ago
CacheId.php
5 years ago
CliMulti.php
5 years ago
Common.php
5 years ago
Config.php
5 years ago
Console.php
5 years ago
Context.php
5 years ago
Cookie.php
5 years ago
CronArchive.php
5 years ago
DataArray.php
5 years ago
DataTable.php
5 years ago
Date.php
5 years ago
Db.php
5 years ago
DbHelper.php
5 years ago
Development.php
5 years ago
ErrorHandler.php
5 years ago
EventDispatcher.php
5 years ago
ExceptionHandler.php
5 years ago
FileIntegrity.php
5 years ago
Filechecks.php
5 years ago
Filesystem.php
5 years ago
FrontController.php
5 years ago
Http.php
5 years ago
IP.php
5 years ago
Log.php
5 years ago
LogDeleter.php
5 years ago
Mail.php
5 years ago
Metrics.php
5 years ago
NoAccessException.php
5 years ago
Nonce.php
5 years ago
Notification.php
5 years ago
NumberFormatter.php
5 years ago
Option.php
5 years ago
Period.php
5 years ago
Piwik.php
5 years ago
Plugin.php
5 years ago
Profiler.php
5 years ago
ProxyHeaders.php
5 years ago
ProxyHttp.php
5 years ago
QuickForm2.php
5 years ago
RankingQuery.php
5 years ago
ReportRenderer.php
5 years ago
Segment.php
5 years ago
Sequence.php
5 years ago
Session.php
5 years ago
SettingsPiwik.php
5 years ago
SettingsServer.php
5 years ago
Singleton.php
5 years ago
Site.php
5 years ago
SupportedBrowser.php
5 years ago
TCPDF.php
5 years ago
Theme.php
5 years ago
Timer.php
5 years ago
Tracker.php
5 years ago
Twig.php
5 years ago
Unzip.php
5 years ago
UpdateCheck.php
5 years ago
Updater.php
5 years ago
UpdaterErrorException.php
5 years ago
Updates.php
5 years ago
Url.php
5 years ago
UrlHelper.php
5 years ago
Version.php
5 years ago
View.php
5 years ago
bootstrap.php
5 years ago
dispatch.php
5 years ago
testMinimumPhpVersion.php
5 years ago
Context.php
94 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | */ |
| 8 | |
| 9 | namespace Piwik; |
| 10 | |
| 11 | /** |
| 12 | * Methods related to changing the context Matomo code is running in. |
| 13 | */ |
| 14 | class Context |
| 15 | { |
| 16 | public static function executeWithQueryParameters(array $parametersRequest, callable $callback) |
| 17 | { |
| 18 | // Temporarily sets the Request array to this API call context |
| 19 | $saveGET = $_GET; |
| 20 | $savePOST = $_POST; |
| 21 | $saveQUERY_STRING = @$_SERVER['QUERY_STRING']; |
| 22 | foreach ($parametersRequest as $param => $value) { |
| 23 | $_GET[$param] = $value; |
| 24 | $_POST[$param] = $value; |
| 25 | } |
| 26 | |
| 27 | try { |
| 28 | return $callback(); |
| 29 | } finally { |
| 30 | $_GET = $saveGET; |
| 31 | $_POST = $savePOST; |
| 32 | $_SERVER['QUERY_STRING'] = $saveQUERY_STRING; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Temporarily overwrites the idSite parameter so all code executed by `$callback()` |
| 38 | * will use that idSite. |
| 39 | * |
| 40 | * Useful when you need to change the idSite context for a chunk of code. For example, |
| 41 | * if we are archiving for more than one site in sequence, we don't want to use |
| 42 | * the same caches for both archiving executions. |
| 43 | * |
| 44 | * @param string|int $idSite |
| 45 | * @param callable $callback |
| 46 | * @return mixed returns result of $callback |
| 47 | */ |
| 48 | public static function changeIdSite($idSite, $callback) |
| 49 | { |
| 50 | // temporarily set the idSite query parameter so archiving will end up using |
| 51 | // the correct site aware caches |
| 52 | $originalGetIdSite = isset($_GET['idSite']) ? $_GET['idSite'] : null; |
| 53 | $originalPostIdSite = isset($_POST['idSite']) ? $_POST['idSite'] : null; |
| 54 | |
| 55 | $originalGetIdSites = isset($_GET['idSites']) ? $_GET['idSites'] : null; |
| 56 | $originalPostIdSites = isset($_POST['idSites']) ? $_POST['idSites'] : null; |
| 57 | |
| 58 | $originalTrackerGetIdSite = isset($_GET['idsite']) ? $_GET['idsite'] : null; |
| 59 | $originalTrackerPostIdSite = isset($_POST['idsite']) ? $_POST['idsite'] : null; |
| 60 | |
| 61 | try { |
| 62 | $_GET['idSite'] = $_POST['idSite'] = $idSite; |
| 63 | |
| 64 | if (Tracker::$initTrackerMode) { |
| 65 | $_GET['idsite'] = $_POST['idsite'] = $idSite; |
| 66 | } |
| 67 | |
| 68 | // idSites is a deprecated query param that is still in use. since it is deprecated and new |
| 69 | // supported code shouldn't rely on it, we can (more) safely unset it here, since we are just |
| 70 | // calling downstream matomo code. we unset it because we don't want it interfering w/ |
| 71 | // code in $callback(). |
| 72 | unset($_GET['idSites']); |
| 73 | unset($_POST['idSites']); |
| 74 | |
| 75 | return $callback(); |
| 76 | } finally { |
| 77 | self::resetIdSiteParam($_GET, 'idSite', $originalGetIdSite); |
| 78 | self::resetIdSiteParam($_POST, 'idSite', $originalPostIdSite); |
| 79 | self::resetIdSiteParam($_GET, 'idSites', $originalGetIdSites); |
| 80 | self::resetIdSiteParam($_POST, 'idSites', $originalPostIdSites); |
| 81 | self::resetIdSiteParam($_GET, 'idsite', $originalTrackerGetIdSite); |
| 82 | self::resetIdSiteParam($_POST, 'idsite', $originalTrackerPostIdSite); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private static function resetIdSiteParam(&$superGlobal, $paramName, $originalValue) |
| 87 | { |
| 88 | if ($originalValue !== null) { |
| 89 | $superGlobal[$paramName] = $originalValue; |
| 90 | } else { |
| 91 | unset($superGlobal[$paramName]); |
| 92 | } |
| 93 | } |
| 94 | } |