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