Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ResponseBuilder.php « API « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 10c3a001329e354e92d4782c7f00135daf64cc85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Piwik
 * @package Piwik
 */
namespace Piwik\API;

use Exception;
use Piwik\API\DataTableManipulator\Flattener;
use Piwik\API\DataTableManipulator\LabelFilter;
use Piwik\API\DataTableGenericFilter;
use Piwik\DataTable\Renderer\Json;
use Piwik\DataTable\Simple;
use Piwik\DataTable\Renderer;
use Piwik\Piwik;
use Piwik\Common;
use Piwik\DataTable;

/**
 * @package Piwik
 * @subpackage Piwik_API
 */
class ResponseBuilder
{
    const DISPLAY_BACKTRACE_DEBUG = false;

    private $request = null;
    private $outputFormat = null;

    private $apiModule = false;
    private $apiMethod = false;

    /**
     * @param string $outputFormat
     * @param array $request
     */
    public function __construct($outputFormat, $request = array())
    {
        $this->request = $request;
        $this->outputFormat = $outputFormat;
    }

    /**
     * This method processes the data resulting from the API call.
     *
     * - If the data resulted from the API call is a DataTable then
     *         - we apply the standard filters if the parameters have been found
     *           in the URL. For example to offset,limit the Table you can add the following parameters to any API
     *        call that returns a DataTable: filter_limit=10&filter_offset=20
     *         - we apply the filters that have been previously queued on the DataTable
     * @see DataTable::queueFilter()
     *         - we apply the renderer that generate the DataTable in a given format (XML, PHP, HTML, JSON, etc.)
     *           the format can be changed using the 'format' parameter in the request.
     *        Example: format=xml
     *
     * - If there is nothing returned (void) we display a standard success message
     *
     * - If there is a PHP array returned, we try to convert it to a dataTable
     *   It is then possible to convert this datatable to any requested format (xml/etc)
     *
     * - If a bool is returned we convert to a string (true is displayed as 'true' false as 'false')
     *
     * - If an integer / float is returned, we simply return it
     *
     * @param mixed $value      The initial returned value, before post process. If set to null, success response is returned.
     * @param bool|string $apiModule  The API module that was called
     * @param bool|string $apiMethod  The API method that was called
     * @return mixed  Usually a string, but can still be a PHP data structure if the format requested is 'original'
     */
    public function getResponse($value = null, $apiModule = false, $apiMethod = false)
    {
        $this->apiModule = $apiModule;
        $this->apiMethod = $apiMethod;

        // when null or void is returned from the api call, we handle it as a successful operation
        if (!isset($value)) {
            return $this->handleSuccess();
        }

        // If the returned value is an object DataTable we
        // apply the set of generic filters if asked in the URL
        // and we render the DataTable according to the format specified in the URL
        if ($value instanceof DataTable
            || $value instanceof DataTable\Map
        ) {
            return $this->handleDataTable($value);
        }

        // Case an array is returned from the API call, we convert it to the requested format
        // - if calling from inside the application (format = original)
        //    => the data stays unchanged (ie. a standard php array or whatever data structure)
        // - if any other format is requested, we have to convert this data structure (which we assume
        //   to be an array) to a DataTable in order to apply the requested DataTable_Renderer (for example XML)
        if (is_array($value)) {
            return $this->handleArray($value);
        }

        // original data structure requested, we return without process
        if ($this->outputFormat == 'original') {
            return $value;
        }

        if (is_object($value)
            || is_resource($value)
        ) {
            return $this->getResponseException(new Exception('The API cannot handle this data structure.'));
        }

        // bool // integer // float // serialized object
        return $this->handleScalar($value);
    }

    /**
     * Returns an error $message in the requested $format
     *
     * @param Exception $e
     * @throws Exception
     * @return string
     */
    public function getResponseException(Exception $e)
    {
        $format = strtolower($this->outputFormat);

        if ($format == 'original') {
            throw $e;
        }

        try {
            $renderer = Renderer::factory($format);
        } catch (Exception $exceptionRenderer) {
            return "Error: " . $e->getMessage() . " and: " . $exceptionRenderer->getMessage();
        }

        $e = $this->decorateExceptionWithDebugTrace($e);

        $renderer->setException($e);

        if ($format == 'php') {
            $renderer->setSerialize($this->caseRendererPHPSerialize());
        }

        return $renderer->renderException();
    }

    /**
     * @param Exception $e
     * @return Exception
     */
    protected function decorateExceptionWithDebugTrace(Exception $e)
    {
        // If we are in tests, show full backtrace
        if (defined('PIWIK_PATH_TEST_TO_ROOT')) {
            if (self::DISPLAY_BACKTRACE_DEBUG) {
                $message = $e->getMessage() . " in \n " . $e->getFile() . ":" . $e->getLine() . " \n " . $e->getTraceAsString();
            } else {
                $message = $e->getMessage() . "\n \n --> To temporarily debug this error further, set const DISPLAY_BACKTRACE_DEBUG=true; in " . basename(__FILE__);
            }
            return new Exception($message);
        }
        return $e;
    }

    /**
     * Returns true if the user requested to serialize the output data (&serialize=1 in the request)
     *
     * @param mixed $defaultSerializeValue  Default value in case the user hasn't specified a value
     * @return bool
     */
    protected function caseRendererPHPSerialize($defaultSerializeValue = 1)
    {
        $serialize = Common::getRequestVar('serialize', $defaultSerializeValue, 'int', $this->request);
        if ($serialize) {
            return true;
        }
        return false;
    }

    /**
     * Apply the specified renderer to the DataTable
     *
     * @param DataTable|array $dataTable
     * @return string
     */
    protected function getRenderedDataTable($dataTable)
    {
        $format = strtolower($this->outputFormat);

        // if asked for original dataStructure
        if ($format == 'original') {
            // by default "original" data is not serialized
            if ($this->caseRendererPHPSerialize($defaultSerialize = 0)) {
                $dataTable = serialize($dataTable);
            }
            return $dataTable;
        }

        $method = Common::getRequestVar('method', '', 'string', $this->request);

        $renderer = Renderer::factory($format);
        $renderer->setTable($dataTable);
        $renderer->setRenderSubTables(Common::getRequestVar('expanded', false, 'int', $this->request));
        $renderer->setHideIdSubDatableFromResponse(Common::getRequestVar('hideIdSubDatable', false, 'int', $this->request));

        if ($format == 'php') {
            $renderer->setSerialize($this->caseRendererPHPSerialize());
            $renderer->setPrettyDisplay(Common::getRequestVar('prettyDisplay', false, 'int', $this->request));
        } else if ($format == 'html') {
            $renderer->setTableId($this->request['method']);
        } else if ($format == 'csv' || $format == 'tsv') {
            $renderer->setConvertToUnicode(Common::getRequestVar('convertToUnicode', true, 'int', $this->request));
        }

        // prepare translation of column names
        if ($format == 'html' || $format == 'csv' || $format == 'tsv' || $format = 'rss') {
            $renderer->setApiMethod($method);
            $renderer->setIdSite(Common::getRequestVar('idSite', false, 'int', $this->request));
            $renderer->setTranslateColumnNames(Common::getRequestVar('translateColumnNames', false, 'int', $this->request));
        }

        return $renderer->render();
    }

    /**
     * Returns a success $message in the requested $format
     *
     * @param string $message
     * @return string
     */
    protected function handleSuccess($message = 'ok')
    {
        // return a success message only if no content has already been buffered, useful when APIs return raw text or html content to the browser
        if (!ob_get_contents()) {
            switch ($this->outputFormat) {
                case 'xml':
                    @header("Content-Type: text/xml;charset=utf-8");
                    $return =
                        "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" .
                            "<result>\n" .
                            "\t<success message=\"" . $message . "\" />\n" .
                            "</result>";
                    break;
                case 'json':
                    @header("Content-Type: application/json");
                    $return = '{"result":"success", "message":"' . $message . '"}';
                    break;
                case 'php':
                    $return = array('result' => 'success', 'message' => $message);
                    if ($this->caseRendererPHPSerialize()) {
                        $return = serialize($return);
                    }
                    break;

                case 'csv':
                    @header("Content-Type: application/vnd.ms-excel");
                    @header("Content-Disposition: attachment; filename=piwik-report-export.csv");
                    $return = "message\n" . $message;
                    break;

                default:
                    $return = 'Success:' . $message;
                    break;
            }
            return $return;
        }
    }

    /**
     * Converts the given scalar to an data table
     *
     * @param mixed $scalar
     * @return string
     */
    protected function handleScalar($scalar)
    {
        $dataTable = new Simple();
        $dataTable->addRowsFromArray(array($scalar));
        return $this->getRenderedDataTable($dataTable);
    }

    /**
     * Handles the given data table
     *
     * @param DataTable $datatable
     * @return string
     */
    protected function handleDataTable($datatable)
    {
        // if requested, flatten nested tables
        if (Common::getRequestVar('flat', '0', 'string', $this->request) == '1') {
            $flattener = new Flattener($this->apiModule, $this->apiMethod, $this->request);
            if (Common::getRequestVar('include_aggregate_rows', '0', 'string', $this->request) == '1') {
                $flattener->includeAggregateRows();
            }
            $datatable = $flattener->flatten($datatable);
        }

        // if the flag disable_generic_filters is defined we skip the generic filters
        if (0 == Common::getRequestVar('disable_generic_filters', '0', 'string', $this->request)) {
            $genericFilter = new DataTableGenericFilter($this->request);
            $genericFilter->filter($datatable);
        }

        // we automatically safe decode all datatable labels (against xss)
        $datatable->queueFilter('SafeDecodeLabel');

        // if the flag disable_queued_filters is defined we skip the filters that were queued
        if (Common::getRequestVar('disable_queued_filters', 0, 'int', $this->request) == 0) {
            $datatable->applyQueuedFilters();
        }

        // use the ColumnDelete filter if hideColumns/showColumns is provided (must be done
        // after queued filters are run so processed metrics can be removed, too)
        $hideColumns = Common::getRequestVar('hideColumns', '', 'string', $this->request);
        $showColumns = Common::getRequestVar('showColumns', '', 'string', $this->request);
        if ($hideColumns !== '' || $showColumns !== '') {
            $datatable->filter('ColumnDelete', array($hideColumns, $showColumns));
        }

        // apply label filter: only return rows matching the label parameter (more than one if more than one label)
        $label = $this->getLabelFromRequest($this->request);
        if (!empty($label)) {
            $addLabelIndex = Common::getRequestVar('labelFilterAddLabelIndex', 0, 'int', $this->request) == 1;

            $filter = new LabelFilter($this->apiModule, $this->apiMethod, $this->request);
            $datatable = $filter->filter($label, $datatable, $addLabelIndex);
        }
        return $this->getRenderedDataTable($datatable);
    }

    /**
     * Converts the given simple array to a data table
     *
     * @param array $array
     * @return string
     */
    protected function handleArray($array)
    {
        if ($this->outputFormat == 'original') {
            // we handle the serialization. Because some php array have a very special structure that
            // couldn't be converted with the automatic DataTable->addRowsFromSimpleArray
            // the user may want to request the original PHP data structure serialized by the API
            // in case he has to setup serialize=1 in the URL
            if ($this->caseRendererPHPSerialize($defaultSerialize = 0)) {
                return serialize($array);
            }
            return $array;
        }
        $multiDimensional = $this->handleMultiDimensionalArray($array);
        if ($multiDimensional !== false) {
            return $multiDimensional;
        }

        return $this->getRenderedDataTable($array);
    }

    /**
     * Is this a multi dimensional array?
     * Multi dim arrays are not supported by the Datatable renderer.
     * We manually render these.
     *
     * array(
     *         array(
     *             1,
     *             2 => array( 1,
     *                         2
     *             )
     *        ),
     *        array( 2,
     *               3
     *        )
     *    );
     *
     * @param array $array
     * @return string|bool  false if it isn't a multidim array
     */
    protected function handleMultiDimensionalArray($array)
    {
        $first = reset($array);
        foreach ($array as $first) {
            if (is_array($first)) {
                foreach ($first as $key => $value) {
                    // Yes, this is a multi dim array
                    if (is_array($value)) {
                        switch ($this->outputFormat) {
                            case 'json':
                                @header("Content-Type: application/json");
                                return self::convertMultiDimensionalArrayToJson($array);
                                break;

                            case 'php':
                                if ($this->caseRendererPHPSerialize($defaultSerialize = 0)) {
                                    return serialize($array);
                                }
                                return $array;

                            case 'xml':
                                @header("Content-Type: text/xml;charset=utf-8");
                                return $this->getRenderedDataTable($array);
                            default:
                                break;
                        }
                    }
                }
            }
        }
        return false;
    }

    /**
     * Render a multidimensional array to Json
     * Handle DataTable|Set elements in the first dimension only, following case does not work:
     * array(
     *        array(
     *            DataTable,
     *            2 => array(
     *                1,
     *                2
     *            ),
     *        ),
     *    );
     *
     * @param array $array  can contain scalar, arrays, DataTable and Set
     * @return string
     */
    public static function convertMultiDimensionalArrayToJson($array)
    {
        $isAssociative = Piwik::isAssociativeArray($array);

        if ($isAssociative) {
            $json = "{";
        } else {
            $json = "[";
        }

        foreach ($array as $key => $value) {
            if ($isAssociative) {
                $json .= "\"" . $key . "\":";
            }

            switch (true) {
                // Case dimension is a PHP array
                case (is_array($value)):

                    $json .= Common::json_encode($value);
                    break;

                // Case dimension is a Set or a DataTable
                case ($value instanceof DataTable\Map || $value instanceof DataTable):

                    $XMLRenderer = new Json();
                    $XMLRenderer->setTable($value);
                    $renderedReport = $XMLRenderer->render();
                    $json .= $renderedReport;
                    break;

                // Case scalar
                default:

                    $json .= Common::json_encode($value);
                    break;
            }

            $json .= ",";
        }

        // Remove trailing ","
        $json = substr($json, 0, strlen($json) - 1);

        if ($isAssociative) {
            $json .= "}";
        } else {
            $json .= "]";
        }
        return $json;
    }

    /**
     * Returns the value for the label query parameter which can be either a string
     * (ie, label=...) or array (ie, label[]=...).
     *
     * @param array $request
     * @return array
     */
    static public function getLabelFromRequest($request)
    {
        $label = Common::getRequestVar('label', array(), 'array', $request);
        if (empty($label)) {
            $label = Common::getRequestVar('label', '', 'string', $request);
            if (!empty($label)) {
                $label = array($label);
            }
        }

        $label = self::unsanitizeLabelParameter($label);
        return $label;
    }

    static public function unsanitizeLabelParameter($label)
    {
        // this is needed because Proxy uses Common::getRequestVar which in turn
        // uses Common::sanitizeInputValue. This causes the > that separates recursive labels
        // to become &gt; and we need to undo that here.
        $label = Common::unsanitizeInputValues($label);
        return $label;
    }
}