PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.0.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.0.2
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 / API / ApiRenderer.php
matomo / app / core / API Last commit date
DataTableManipulator 5 years ago ApiRenderer.php 5 years ago CORSHandler.php 5 years ago DataTableGenericFilter.php 5 years ago DataTableManipulator.php 5 years ago DataTablePostProcessor.php 5 years ago DocumentationGenerator.php 5 years ago Inconsistencies.php 5 years ago NoDefaultValue.php 5 years ago Proxy.php 5 years ago Request.php 5 years ago ResponseBuilder.php 5 years ago
ApiRenderer.php
153 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\API;
10
11 use Exception;
12 use Piwik\Common;
13 use Piwik\DataTable\Renderer;
14 use Piwik\DataTable;
15 use Piwik\Piwik;
16 use Piwik\Plugin;
17 use Piwik\SettingsServer;
18
19 /**
20 * API renderer
21 */
22 abstract class ApiRenderer
23 {
24 protected $request;
25
26 protected $hideIdSubDataTable;
27
28 final public function __construct($request)
29 {
30 $this->request = $request;
31 $this->init();
32 }
33
34 protected function init()
35 {
36 $this->hideIdSubDataTable = Common::getRequestVar('hideIdSubDatable', false, 'int', $this->request);
37 }
38
39 protected function shouldSendBacktrace()
40 {
41 return Common::isPhpCliMode() && SettingsServer::isArchivePhpTriggered();
42 }
43
44 abstract public function sendHeader();
45
46 public function renderSuccess($message)
47 {
48 return 'Success:' . $message;
49 }
50
51 /**
52 * @param $message
53 * @param Exception|\Throwable $exception
54 * @return mixed
55 */
56 public function renderException($message, $exception)
57 {
58 return $message;
59 }
60
61 public function renderScalar($scalar)
62 {
63 $dataTable = new DataTable\Simple();
64 $dataTable->addRowsFromArray(array($scalar));
65 return $this->renderDataTable($dataTable);
66 }
67
68 public function renderDataTable($dataTable)
69 {
70 $renderer = $this->buildDataTableRenderer($dataTable);
71 return $renderer->render();
72 }
73
74 public function renderArray($array)
75 {
76 $renderer = $this->buildDataTableRenderer($array);
77 return $renderer->render();
78 }
79
80 public function renderObject($object)
81 {
82 $exception = new Exception('The API cannot handle this data structure.');
83 return $this->renderException($exception->getMessage(), $exception);
84 }
85
86 public function renderResource($resource)
87 {
88 $exception = new Exception('The API cannot handle this data structure.');
89 return $this->renderException($exception->getMessage(), $exception);
90 }
91
92 /**
93 * @param $dataTable
94 * @return Renderer
95 */
96 protected function buildDataTableRenderer($dataTable)
97 {
98 $format = self::getFormatFromClass(get_class($this));
99
100 $idSite = Common::getRequestVar('idSite', 0, 'int', $this->request);
101
102 if (empty($idSite)) {
103 $idSite = 'all';
104 }
105
106 $renderer = Renderer::factory($format);
107 $renderer->setTable($dataTable);
108 $renderer->setIdSite($idSite);
109 $renderer->setRenderSubTables(Common::getRequestVar('expanded', false, 'int', $this->request));
110 $renderer->setHideIdSubDatableFromResponse($this->hideIdSubDataTable);
111
112 return $renderer;
113 }
114
115 /**
116 * @param string $format
117 * @param array $request
118 * @return ApiRenderer
119 * @throws Exception
120 */
121 public static function factory($format, $request)
122 {
123 if (Common::mb_strtolower($format) === 'json2') {
124 $format = 'json';
125 }
126 $formatToCheck = '\\' . ucfirst(strtolower($format));
127
128 $rendererClassnames = Plugin\Manager::getInstance()->findMultipleComponents('Renderer', 'Piwik\\API\\ApiRenderer');
129
130 foreach ($rendererClassnames as $klassName) {
131 if (Common::stringEndsWith($klassName, $formatToCheck)) {
132 return new $klassName($request);
133 }
134 }
135
136 $availableRenderers = array();
137 foreach ($rendererClassnames as $rendererClassname) {
138 $availableRenderers[] = self::getFormatFromClass($rendererClassname);
139 }
140
141 $availableRenderers = implode(', ', $availableRenderers);
142 Common::sendHeader('Content-Type: text/plain; charset=utf-8');
143 throw new Exception(Piwik::translate('General_ExceptionInvalidRendererFormat', array($format, $availableRenderers)));
144 }
145
146 private static function getFormatFromClass($klassname)
147 {
148 $klass = explode('\\', $klassname);
149
150 return strtolower(end($klass));
151 }
152 }
153