|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Joomla! Statistics Server |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved. |
|
6
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Joomla\StatsServer\Controllers; |
|
10
|
|
|
|
|
11
|
|
|
use Joomla\Controller\AbstractController; |
|
12
|
|
|
use Joomla\Cache\Item\Item; |
|
13
|
|
|
use Joomla\StatsServer\Views\Stats\StatsJsonView; |
|
14
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Controller for displaying submitted statistics data. |
|
18
|
|
|
* |
|
19
|
|
|
* @method \Joomla\StatsServer\WebApplication getApplication() Get the application object. |
|
20
|
|
|
* @property-read \Joomla\StatsServer\WebApplication $app Application object |
|
21
|
|
|
*/ |
|
22
|
|
|
class DisplayControllerGet extends AbstractController |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* The cache item pool. |
|
26
|
|
|
* |
|
27
|
|
|
* @var CacheItemPoolInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $cache; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* JSON view for displaying the statistics. |
|
33
|
|
|
* |
|
34
|
|
|
* @var StatsJsonView |
|
35
|
|
|
*/ |
|
36
|
|
|
private $view; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param StatsJsonView $view JSON view for displaying the statistics. |
|
42
|
|
|
* @param CacheItemPoolInterface $cache The cache item pool. |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public function __construct(StatsJsonView $view, CacheItemPoolInterface $cache) |
|
45
|
|
|
{ |
|
46
|
1 |
|
$this->cache = $cache; |
|
47
|
1 |
|
$this->view = $view; |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Execute the controller. |
|
52
|
|
|
* |
|
53
|
|
|
* @return boolean |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function execute() |
|
56
|
|
|
{ |
|
57
|
|
|
// Check if we are allowed to receive the raw data |
|
58
|
2 |
|
$authorizedRaw = $this->getInput()->server->getString('HTTP_JOOMLA_RAW', 'fail') === $this->getApplication()->get('stats.rawdata', false); |
|
59
|
|
|
|
|
60
|
|
|
// Check if a single data source is requested |
|
61
|
2 |
|
$source = $this->getInput()->getString('source', ''); |
|
62
|
|
|
|
|
63
|
2 |
|
$this->view->isAuthorizedRaw($authorizedRaw); |
|
64
|
2 |
|
$this->view->setSource($source); |
|
65
|
|
|
|
|
66
|
|
|
// Serve cached data if the cache layer is enabled and the raw data source is not requested |
|
67
|
2 |
|
if ($this->getApplication()->get('cache.enabled', false) && !$authorizedRaw) |
|
68
|
|
|
{ |
|
69
|
1 |
|
$key = md5(get_class($this->view) . __METHOD__ . $source); |
|
70
|
|
|
|
|
71
|
1 |
|
if ($this->cache->hasItem($key)) |
|
72
|
|
|
{ |
|
73
|
1 |
|
$item = $this->cache->getItem($key); |
|
74
|
|
|
|
|
75
|
|
|
// Make sure we got a hit on the item, otherwise we'll have to re-cache |
|
76
|
1 |
|
if ($item->isHit()) |
|
77
|
|
|
{ |
|
78
|
1 |
|
$body = $item->get(); |
|
79
|
|
|
} |
|
80
|
|
|
else |
|
81
|
|
|
{ |
|
82
|
|
|
$body = $this->view->render(); |
|
83
|
|
|
|
|
84
|
1 |
|
$this->cacheData($key, $body); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
else |
|
88
|
|
|
{ |
|
89
|
1 |
|
$body = $this->view->render(); |
|
90
|
|
|
|
|
91
|
1 |
|
$this->cacheData($key, $body); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
else |
|
95
|
|
|
{ |
|
96
|
1 |
|
$body = $this->view->render(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
$this->getApplication()->setBody($body); |
|
100
|
|
|
|
|
101
|
2 |
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Store the given data to the cache pool. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $key The key for the cache item. |
|
108
|
|
|
* @param mixed $data The data to be stored to cache. |
|
109
|
|
|
* |
|
110
|
|
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
1 |
|
private function cacheData(string $key, $data) |
|
113
|
|
|
{ |
|
114
|
1 |
|
$item = (new Item($key, $this->getApplication()->get('cache.lifetime', 900))) |
|
115
|
1 |
|
->set($data); |
|
116
|
|
|
|
|
117
|
1 |
|
$this->cache->save($item); |
|
118
|
1 |
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|