PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.2.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.2.0
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 / ResponseBuilder.php
matomo / app / core / API Last commit date
DataTableManipulator 1 year ago ApiRenderer.php 1 year ago CORSHandler.php 1 year ago DataTableGenericFilter.php 1 year ago DataTableManipulator.php 1 year ago DataTablePostProcessor.php 1 year ago DocumentationGenerator.php 1 year ago Inconsistencies.php 2 years ago NoDefaultValue.php 2 years ago Proxy.php 1 year ago Request.php 1 year ago ResponseBuilder.php 1 year ago
ResponseBuilder.php
209 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\API;
10
11 use Exception;
12 use Piwik\Common;
13 use Piwik\DataTable;
14 use Piwik\DataTable\DataTableInterface;
15 use Piwik\DataTable\Filter\ColumnDelete;
16 use Piwik\DataTable\Filter\Pattern;
17 use Piwik\DataTable\Renderer;
18 use Piwik\Http\HttpCodeException;
19 use Piwik\Plugins\Monolog\Processor\ExceptionToTextProcessor;
20 /**
21 */
22 class ResponseBuilder
23 {
24 private $outputFormat = null;
25 private $apiRenderer = null;
26 private $request = null;
27 private $sendHeader = \true;
28 private $postProcessDataTable = \true;
29 private $apiModule = \false;
30 private $apiMethod = \false;
31 private $shouldPrintBacktrace = \false;
32 /**
33 * @param string $outputFormat
34 * @param array $request
35 */
36 public function __construct($outputFormat, $request = array(), $shouldPrintBacktrace = null)
37 {
38 $this->outputFormat = $outputFormat;
39 $this->request = $request;
40 $this->apiRenderer = \Piwik\API\ApiRenderer::factory($outputFormat, $request);
41 $this->shouldPrintBacktrace = $shouldPrintBacktrace === null ? \Piwik_ShouldPrintBackTraceWithMessage() : $shouldPrintBacktrace;
42 }
43 public function disableSendHeader()
44 {
45 $this->sendHeader = \false;
46 }
47 public function disableDataTablePostProcessor()
48 {
49 $this->postProcessDataTable = \false;
50 }
51 /**
52 * This method processes the data resulting from the API call.
53 *
54 * - If the data resulted from the API call is a DataTable then
55 * - we apply the standard filters if the parameters have been found
56 * in the URL. For example to offset,limit the Table you can add the following parameters to any API
57 * call that returns a DataTable: filter_limit=10&filter_offset=20
58 * - we apply the filters that have been previously queued on the DataTable
59 * @see DataTable::queueFilter()
60 * - we apply the renderer that generate the DataTable in a given format (XML, PHP, HTML, JSON, etc.)
61 * the format can be changed using the 'format' parameter in the request.
62 * Example: format=xml
63 *
64 * - If there is nothing returned (void) we display a standard success message
65 *
66 * - If there is a PHP array returned, we try to convert it to a dataTable
67 * It is then possible to convert this datatable to any requested format (xml/etc)
68 *
69 * - If a bool is returned we convert to a string (true is displayed as 'true' false as 'false')
70 *
71 * - If an integer / float is returned, we simply return it
72 *
73 * @param mixed $value The initial returned value, before post process. If set to null, success response is returned.
74 * @param bool|string $apiModule The API module that was called
75 * @param bool|string $apiMethod The API method that was called
76 * @return mixed Usually a string, but can still be a PHP data structure if the format requested is 'original'
77 */
78 public function getResponse($value = null, $apiModule = \false, $apiMethod = \false)
79 {
80 $this->apiModule = $apiModule;
81 $this->apiMethod = $apiMethod;
82 $this->sendHeaderIfEnabled();
83 // when null or void is returned from the api call, we handle it as a successful operation
84 if (!isset($value)) {
85 if (ob_get_contents()) {
86 return null;
87 }
88 return $this->apiRenderer->renderSuccess('ok');
89 }
90 // If the returned value is an object DataTable we
91 // apply the set of generic filters if asked in the URL
92 // and we render the DataTable according to the format specified in the URL
93 if ($value instanceof DataTableInterface) {
94 return $this->handleDataTable($value);
95 }
96 // Case an array is returned from the API call, we convert it to the requested format
97 // - if calling from inside the application (format = original)
98 // => the data stays unchanged (ie. a standard php array or whatever data structure)
99 // - if any other format is requested, we have to convert this data structure (which we assume
100 // to be an array) to a DataTable in order to apply the requested DataTable_Renderer (for example XML)
101 if (is_array($value)) {
102 return $this->handleArray($value);
103 }
104 if (is_object($value)) {
105 return $this->apiRenderer->renderObject($value);
106 }
107 if (is_resource($value)) {
108 return $this->apiRenderer->renderResource($value);
109 }
110 return $this->apiRenderer->renderScalar($value);
111 }
112 /**
113 * Returns an error $message in the requested $format
114 *
115 * @param Exception|\Throwable $e
116 * @throws Exception
117 * @return string
118 */
119 public function getResponseException($e)
120 {
121 $e = $this->decorateExceptionWithDebugTrace($e);
122 $message = $this->formatExceptionMessage($e);
123 if ($this->sendHeader && $e instanceof HttpCodeException && $e->getCode() > 0) {
124 http_response_code($e->getCode());
125 }
126 $this->sendHeaderIfEnabled();
127 return $this->apiRenderer->renderException($message, $e);
128 }
129 /**
130 * @param Exception|\Throwable $e
131 * @return Exception
132 */
133 private function decorateExceptionWithDebugTrace($e)
134 {
135 // If we are in tests, show full backtrace
136 if (defined('PIWIK_PATH_TEST_TO_ROOT')) {
137 if ($this->shouldPrintBacktrace) {
138 $message = $e->getMessage() . " in \n " . $e->getFile() . ":" . $e->getLine() . " \n " . $e->getTraceAsString();
139 } else {
140 $message = $e->getMessage() . "\n \n --> To temporarily debug this error further, set const PIWIK_PRINT_ERROR_BACKTRACE=true; in index.php";
141 }
142 return new Exception($message);
143 }
144 return $e;
145 }
146 /**
147 * @param Exception|\Throwable $exception
148 * @return string
149 */
150 private function formatExceptionMessage($exception)
151 {
152 $message = ExceptionToTextProcessor::getMessageAndWholeBacktrace($exception, $this->shouldPrintBacktrace);
153 if ($exception instanceof \Piwik\Exception\Exception && $exception->isHtmlMessage() && \Piwik\API\Request::isRootRequestApiRequest()) {
154 $message = strip_tags(str_replace('<br />', \PHP_EOL, $message));
155 }
156 return Renderer::formatValueXml($message);
157 }
158 private function handleDataTable(DataTableInterface $datatable)
159 {
160 if ($this->postProcessDataTable) {
161 $postProcessor = new \Piwik\API\DataTablePostProcessor($this->apiModule, $this->apiMethod, $this->request);
162 $datatable = $postProcessor->process($datatable);
163 }
164 return $this->apiRenderer->renderDataTable($datatable);
165 }
166 private function handleArray($array)
167 {
168 $firstArray = null;
169 $firstKey = null;
170 if (!empty($array)) {
171 $firstArray = reset($array);
172 $firstKey = key($array);
173 }
174 $isAssoc = !empty($firstArray) && is_numeric($firstKey) && is_array($firstArray) && count(array_filter(array_keys($firstArray), 'is_string'));
175 if (is_numeric($firstKey)) {
176 $columns = Common::getRequestVar('filter_column', \false, 'array', $this->request);
177 $pattern = Common::getRequestVar('filter_pattern', '', 'string', $this->request);
178 if ($columns != array(\false) && $pattern !== '') {
179 $pattern = new Pattern(new DataTable(), $columns, $pattern);
180 $array = $pattern->filterArray($array);
181 }
182 $limit = Common::getRequestVar('filter_limit', -1, 'integer', $this->request);
183 $offset = Common::getRequestVar('filter_offset', '0', 'integer', $this->request);
184 if ($limit >= 0 || $offset > 0) {
185 if ($limit < 0) {
186 $limit = null;
187 // make sure to return all results from offset
188 }
189 $array = array_slice($array, $offset, $limit, $preserveKeys = \false);
190 }
191 }
192 if ($isAssoc) {
193 $hideColumns = Common::getRequestVar('hideColumns', '', 'string', $this->request);
194 $showColumns = Common::getRequestVar('showColumns', '', 'string', $this->request);
195 if ($hideColumns !== '' || $showColumns !== '') {
196 $columnDelete = new ColumnDelete(new DataTable(), $hideColumns, $showColumns);
197 $array = $columnDelete->filter($array);
198 }
199 }
200 return $this->apiRenderer->renderArray($array);
201 }
202 private function sendHeaderIfEnabled()
203 {
204 if ($this->sendHeader) {
205 $this->apiRenderer->sendHeader();
206 }
207 }
208 }
209