Config
4 months ago
Db
3 months ago
Handler
2 years ago
Visit
1 month ago
Action.php
3 months ago
ActionPageview.php
2 years ago
BotRequest.php
3 months ago
BotRequestProcessor.php
1 month ago
Cache.php
6 months ago
Db.php
1 year ago
Failures.php
6 months ago
FingerprintSalt.php
1 year ago
GoalManager.php
1 month ago
Handler.php
2 years ago
IgnoreCookie.php
1 year ago
LogTable.php
1 year ago
Model.php
6 months ago
PageUrl.php
2 weeks ago
Request.php
1 month ago
RequestHandlerTrait.php
4 months ago
RequestProcessor.php
1 month ago
RequestSet.php
6 months ago
Response.php
3 months ago
ScheduledTasksRunner.php
1 year ago
Settings.php
3 months ago
TableLogAction.php
6 months ago
TrackerCodeGenerator.php
1 year ago
TrackerConfig.php
1 month ago
Visit.php
3 months ago
VisitExcluded.php
3 months ago
VisitInterface.php
3 months ago
Visitor.php
1 month ago
VisitorNotFoundInDb.php
1 month ago
VisitorRecognizer.php
1 year ago
Handler.php
100 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\Tracker; |
| 10 | |
| 11 | use Piwik\Container\StaticContainer; |
| 12 | use Piwik\Exception\InvalidRequestParameterException; |
| 13 | use Piwik\Exception\UnexpectedWebsiteFoundException; |
| 14 | use Piwik\Tracker; |
| 15 | use Exception; |
| 16 | use Piwik\Log\LoggerInterface; |
| 17 | class Handler |
| 18 | { |
| 19 | /** |
| 20 | * @var Response |
| 21 | */ |
| 22 | private $response; |
| 23 | /** |
| 24 | * @var ScheduledTasksRunner |
| 25 | */ |
| 26 | private $tasksRunner; |
| 27 | /** |
| 28 | * @var LoggerInterface |
| 29 | */ |
| 30 | private $logger; |
| 31 | public function __construct() |
| 32 | { |
| 33 | $this->setResponse(new \Piwik\Tracker\Response()); |
| 34 | $this->logger = StaticContainer::get(LoggerInterface::class); |
| 35 | } |
| 36 | public function setResponse($response) |
| 37 | { |
| 38 | $this->response = $response; |
| 39 | } |
| 40 | public function init(Tracker $tracker, \Piwik\Tracker\RequestSet $requestSet) |
| 41 | { |
| 42 | $this->response->init($tracker); |
| 43 | } |
| 44 | public function process(Tracker $tracker, \Piwik\Tracker\RequestSet $requestSet) |
| 45 | { |
| 46 | foreach ($requestSet->getRequests() as $request) { |
| 47 | $tracker->trackRequest($request); |
| 48 | } |
| 49 | } |
| 50 | public function onStartTrackRequests(Tracker $tracker, \Piwik\Tracker\RequestSet $requestSet) |
| 51 | { |
| 52 | } |
| 53 | public function onAllRequestsTracked(Tracker $tracker, \Piwik\Tracker\RequestSet $requestSet) |
| 54 | { |
| 55 | $tasks = $this->getScheduledTasksRunner(); |
| 56 | if ($tasks->shouldRun($tracker)) { |
| 57 | $tasks->runScheduledTasks(); |
| 58 | } |
| 59 | } |
| 60 | private function getScheduledTasksRunner() |
| 61 | { |
| 62 | if (is_null($this->tasksRunner)) { |
| 63 | $this->tasksRunner = new \Piwik\Tracker\ScheduledTasksRunner(); |
| 64 | } |
| 65 | return $this->tasksRunner; |
| 66 | } |
| 67 | /** |
| 68 | * @internal |
| 69 | */ |
| 70 | public function setScheduledTasksRunner(\Piwik\Tracker\ScheduledTasksRunner $runner) |
| 71 | { |
| 72 | $this->tasksRunner = $runner; |
| 73 | } |
| 74 | public function onException(Tracker $tracker, \Piwik\Tracker\RequestSet $requestSet, Exception $e) |
| 75 | { |
| 76 | $statusCode = 500; |
| 77 | if ($e instanceof UnexpectedWebsiteFoundException) { |
| 78 | $statusCode = 400; |
| 79 | } elseif ($e instanceof InvalidRequestParameterException) { |
| 80 | $statusCode = 400; |
| 81 | } |
| 82 | // if an internal server error, log as a real error, otherwise it's just malformed input |
| 83 | if ($statusCode == 500) { |
| 84 | $this->logger->error('Exception: {exception}', ['exception' => $e]); |
| 85 | } else { |
| 86 | $this->logger->debug('Exception: {exception}', ['exception' => $e]); |
| 87 | } |
| 88 | $this->response->outputException($tracker, $e, $statusCode); |
| 89 | } |
| 90 | public function finish(Tracker $tracker, \Piwik\Tracker\RequestSet $requestSet) |
| 91 | { |
| 92 | $this->response->outputResponse($tracker); |
| 93 | return $this->response->getOutput(); |
| 94 | } |
| 95 | public function getResponse() |
| 96 | { |
| 97 | return $this->response; |
| 98 | } |
| 99 | } |
| 100 |