Report.php 57.9 KB
Newer Older
1
2
3
4
<?php
/***************************************************************
 *  Copyright notice
 *
Carsten  Rose's avatar
Carsten Rose committed
5
 *  (c) 2010 Glowbase GmbH
6
 *
Carsten  Rose's avatar
Carsten Rose committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
22
23
 ***************************************************************/

Marc Egger's avatar
Marc Egger committed
24
25
26
27
28
29
30
31
32
33
34
namespace IMATHUZH\Qfq\Core\Report;

use IMATHUZH\Qfq\Core\Database\Database;
use IMATHUZH\Qfq\Core\Evaluate;
use IMATHUZH\Qfq\Core\Helper\HelperFile;
use IMATHUZH\Qfq\Core\Helper\KeyValueStringParser;
use IMATHUZH\Qfq\Core\Helper\OnString;
use IMATHUZH\Qfq\Core\Helper\Sanitize;
use IMATHUZH\Qfq\Core\Helper\Support;
use IMATHUZH\Qfq\Core\Store\Sip;
use IMATHUZH\Qfq\Core\Store\Store;
35

36
37
const DEFAULT_QUESTION = 'question';
const DEFAULT_ICON = 'icon';
38
const DEFAULT_BOOTSTRAP_BUTTON = 'bootstrapButton';
39

40
41
42
43
/**
 * Class Report
 * @package qfq
 */
44
45
class Report {

Carsten  Rose's avatar
Carsten Rose committed
46
47
48
49
50
    /**
     * @var SIP
     */
    private $sip = null;

Carsten  Rose's avatar
Carsten Rose committed
51
52
53
54
55
    /**
     * @var Link
     */
    private $link = null;

56
57
58
59
60
    /**
     * @var Store
     */
    private $store = null;

61
62
63
64
65
66
67
    /**
     * @var string
     */
    private $dbAlias = '';

    // frArray[10.50.5.sql][select ...]
    private $frArray = array();
68

69
70
    // $indexArray[10][50][5]   one entry per 'sql' statement
    private $indexArray = array();
71

72
73
74
75
    // TODO to explain
//	private $resultArray = array();
    private $levelCount = 0;
    //private $counter = 0;
76

77
78
79
80
    /**
     * @var Variables
     */
    private $variables = null;
81

82
    /**
83
     * @var Database
84
85
     */
    private $db = null;
86

87
    private $dbIndexData = false;
88

89
90
91
92
93
    /**
     * @var Thumbnail
     */
    private $thumbnail = null;

94
95
96
97
98
    /**
     * @var Monitor
     */
    private $monitor = null;

99
100
101
102
    /**
     * @var array
     */
    private $pageDefaults = array();
103

104
    /**
Carsten  Rose's avatar
Carsten Rose committed
105
106
     * @var array - Emulate global variable: will be set much earlier in other functions. Will be shown in error
     *      messages.
107
     */
108
109
    private $fr_error = array('uid' => '', 'pid' => '', 'row' => '', 'debug_level' => '0', 'full_level' => '');

110
111
    private $phpUnit = false;

112
    private $showDebugInfoFlag = false;
113

114
    /**
115
     * Report constructor.
116
     *
Carsten  Rose's avatar
Carsten Rose committed
117
     * @param array $t3data
118
     * @param Evaluate $eval
Carsten  Rose's avatar
Carsten Rose committed
119
     * @param bool $phpUnit
Marc Egger's avatar
Marc Egger committed
120
121
122
123
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
124
     */
125
    public function __construct(array $t3data, Evaluate $eval, $phpUnit = false) {
126

127
        #TODO: rewrite $phpUnit to: "if (!defined('PHPUNIT_QFQ')) {...}"
128
        $this->phpUnit = $phpUnit;
129

130
131
        Support::setIfNotSet($t3data, "uid", 0);

132
        $this->sip = new Sip($phpUnit);
133
134
        if ($phpUnit) {
            $this->sip->sipUniqId('badcaffee1234');
135
136
            //TODO Webserver Umgebung besser faken
            $_SERVER['REQUEST_URI'] = 'localhost';
137
138
        }

139
        $this->store = Store::getInstance();
140

141
        $this->showDebugInfoFlag = (Support::findInSet(SYSTEM_SHOW_DEBUG_INFO_YES, $this->store->getVar(SYSTEM_SHOW_DEBUG_INFO, STORE_SYSTEM)));
142

143
144
        $this->checkUpdateSqlLog();

145
146
        $this->pageDefaults[DEFAULT_QUESTION]["pagec"] = "Please confirm!:info";
        $this->pageDefaults[DEFAULT_QUESTION]["paged"] = "Do you really want to delete the record?:warning";
147

148
149
150
151
152
153
        $this->pageDefaults[DEFAULT_ICON]["paged"] = TOKEN_DELETE;
        $this->pageDefaults[DEFAULT_ICON]["pagee"] = TOKEN_EDIT;
        $this->pageDefaults[DEFAULT_ICON]["pageh"] = TOKEN_HELP;
        $this->pageDefaults[DEFAULT_ICON]["pagei"] = TOKEN_INFO;
        $this->pageDefaults[DEFAULT_ICON]["pagen"] = TOKEN_NEW;
        $this->pageDefaults[DEFAULT_ICON]["pages"] = TOKEN_SHOW;
154

155
156
157
158
159
160
        $this->pageDefaults[DEFAULT_BOOTSTRAP_BUTTON]["pagec"] = TOKEN_BOOTSTRAP_BUTTON;
        $this->pageDefaults[DEFAULT_BOOTSTRAP_BUTTON]["paged"] = TOKEN_BOOTSTRAP_BUTTON;
        $this->pageDefaults[DEFAULT_BOOTSTRAP_BUTTON]["pagee"] = TOKEN_BOOTSTRAP_BUTTON;
        $this->pageDefaults[DEFAULT_BOOTSTRAP_BUTTON]["pagen"] = TOKEN_BOOTSTRAP_BUTTON;
        $this->pageDefaults[DEFAULT_BOOTSTRAP_BUTTON]["pages"] = TOKEN_BOOTSTRAP_BUTTON;

161
        // Default should already set in QuickFormQuery() Constructor
Carsten  Rose's avatar
Carsten Rose committed
162
163
164
165
        $this->dbIndexData = $this->store->getVar(TOKEN_DB_INDEX, STORE_TYPO3);
        if ($this->dbIndexData === false) {
            $this->dbIndexData = DB_INDEX_DEFAULT;
        }
166

167
        $this->db = new Database($this->dbIndexData);
168
        $this->variables = new Variables($eval, $t3data["uid"]);
169

170
171
        $this->link = new Link($this->sip, $this->dbIndexData, $phpUnit);

172
        // Set static values, which won't change during this run.
173
        $this->fr_error["pid"] = isset($this->variables->resultArray['global.']['page_id']) ? $this->variables->resultArray['global.']['page_id'] : 0;
174
        $this->fr_error["uid"] = $t3data['uid'];
175
        $this->fr_error["debug_level"] = 0;
176

Carsten  Rose's avatar
Carsten Rose committed
177
        // Sanitize function for POST and GET Parameters.
178
        // Merged URL-Parameter (key1, id etc...) in resultArray.
179
        $this->variables->resultArray = array_merge($this->variables->resultArray, array("global." => $this->variables->collectGlobalVariables()));
180
181
182

    }

183
    /**
Carsten  Rose's avatar
Carsten Rose committed
184
185
     * If a variable 'sqlLog' is given in STORE_TYPO3 (=Bodytext) make them relative to SYSTEM_PATH_EXT and copy it to
     * STORE_SYSTEM
186
     *
Marc Egger's avatar
Marc Egger committed
187
188
     * @throws \CodeException
     * @throws \UserFormException
189
190
191
192
193
     */
    private function checkUpdateSqlLog() {

        $sqlLog = $this->store->getVar(TYPO3_SQL_LOG, STORE_TYPO3);
        if (false !== $sqlLog) {
194
            $sqlLog = HelperFile::joinPathFilename($this->store->getVar(SYSTEM_EXT_PATH, STORE_SYSTEM), $sqlLog);
195
196
197
198
199
200
201
202
203
            $this->store->setVar(SYSTEM_SQL_LOG, $sqlLog, STORE_SYSTEM);
        }

        $sqlLogMode = $this->store->getVar(TYPO3_SQL_LOG_MODE, STORE_TYPO3);
        if (false !== $sqlLogMode) {
            $this->store->setVar(SYSTEM_SQL_LOG_MODE, $sqlLogMode, STORE_SYSTEM);
        }
    }

204
205
206
    /**
     * Main function. Parses bodytext and iterates over all queries.
     *
207
     * @param $bodyText
208
     * @return string
Marc Egger's avatar
Marc Egger committed
209
210
211
     * @throws \CodeException
     * @throws \DbException
     * @throws \DownloadException
212
213
214
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
215
216
217
218
219
     * @throws \Twig\Error\LoaderError
     * @throws \Twig\Error\RuntimeError
     * @throws \Twig\Error\SyntaxError
     * @throws \UserFormException
     * @throws \UserReportException
220
     */
221
    public function process($bodyText) {
222

223
224
225
226
227
        //phpUnit Test: clean environment
        $this->frArray = array();
        $this->indexArray = array();
        $this->levelCount = 0;

228
        // Iteration over Bodytext
229
        $ttLineArray = explode("\n", $bodyText);
230

231
232
        foreach ($ttLineArray as $index => $line) {
            // Fill $frArray, $indexArray, $resultArray
233
            $this->parseLine($line);
234
        }
235

236
237
238
239
240
241
        // Sort array
        $this->sortIndexArray($this->indexArray, $this->generateSortArg());

        // Report
        $content = $this->triggerReport();

242
        return $content;
243
    }
244
245
246
247
248

    /**
     * Split line in level, command, content and fill 'frArray', 'levelCount', 'indexArray'
     * Example: 10.50.5.sql = select * from person
     *
249
     * @param string $ttLine : line to split in level, command, content
Carsten  Rose's avatar
Carsten Rose committed
250
     *
Marc Egger's avatar
Marc Egger committed
251
     * @throws \UserReportException
252
     */
253
    private function parseLine($ttLine) {
254
255
256
257

        // 10.50.5.sql = select ...
        $arr = explode("=", trim($ttLine), 2);

258
        // no elements or only one: do nothing
259
        if (count($arr) < 2) {
260
            return;
261
        }
262

263
264
265
266
        // 10.50.5.sql
        $key = strtolower(trim($arr[0]));

        // comment ?
267
        if (empty($key) || $key[0] === "#") {
268
            return;
269
        }
270

271
        // select ... - if needed, trim surrounding single ticks
272
        $value = trim($arr[1]);
273
        $value = OnString::trimQuote($value);
274
275
276
277
278
279
280

        // 10.50.5.sql
        $arrKey = explode('.', $key);

        // frCmd = "sql"
        $frCmd = $arrKey[count($arrKey) - 1];

281
        if (strpos('|' . strtolower(TOKEN_VALID_LIST) . '|', '|' . $frCmd . '|') === false) {
Marc Egger's avatar
Marc Egger committed
282
            throw new \UserReportException ("Unknown token: $frCmd in Line '$ttLine''", ERROR_UNKNOWN_TOKEN);
283
284
        }

285
286
287
288
289
290
291
292
293
294
295
        // remove last item (cmd)
        unset($arrKey[count($arrKey) - 1]);

        // save elements only if there is a level specified
        if (count($arrKey)) {
            // level = "10.50.5"
            $level = implode(".", $arrKey);

            // fill Array
            $this->setLine($level, $frCmd, $value);
        }
296
    }
297

298
299
300
301
302
    /**
     * @param $level
     * @param $frCmd
     * @param $value
     */
303
304
305
306
307
308
    private function setLine($level, $frCmd, $value) {

        // store complete line reformatted in frArray
        $this->frArray[$level . "." . $frCmd] = $value;

        // per sql command
309
        //pro sql cmd wird der Indexarray abgefüllt. Dieser wird später verwendet um auf den $frArray zuzugreifen
310
        //if(preg_match("/^sql/i", $frCmd) == 1){
311
312
//        if ($frCmd === TOKEN_FORM || $frCmd === TOKEN_SQL) {
        if ($frCmd === TOKEN_SQL) {
313
314
315
316
317
            // Remember max level
            $this->levelCount = max(substr_count($level, '.') + 1, $this->levelCount);
            // $indexArray[10][50][5]
            $this->indexArray[] = explode(".", $level);
        }
318
319
320

        // set defaults
        if ($frCmd === TOKEN_SQL) {
321
322
            $arr = explode('|', TOKEN_VALID_LIST);
            foreach ($arr as $key) {
323
                if (!isset($this->frArray[$level . "." . $key])) {
324
                    $this->frArray[$level . "." . $key] = '';
325
                }
326
327
            }
        }
328
    }
329
330
331
332

    /**
     * Sorts the associative array.
     *
Carsten  Rose's avatar
Carsten Rose committed
333
     * @param array $ary : The unsorted Level Array
334
     * @param string $clause : the sort argument 0 ASC, 1 ASC... according to the number of columns
335
     * @param bool|true $ascending
336
     */
337
    private function sortIndexArray(array &$ary, $clause, $ascending = true) {
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

        $clause = str_ireplace('order by', '', $clause);
        $clause = preg_replace('/\s+/', ' ', $clause);
        $keys = explode(',', $clause);
        $dirMap = array('desc' => 1, 'asc' => -1);
        $def = $ascending ? -1 : 1;

        $keyAry = array();
        $dirAry = array();
        foreach ($keys as $key) {
            $key = explode(' ', trim($key));
            $keyAry[] = trim($key[0]);
            if (isset($key[1])) {
                $dir = strtolower(trim($key[1]));
                $dirAry[] = $dirMap[$dir] ? $dirMap[$dir] : $def;
            } else {
                $dirAry[] = $def;
            }
        }
        $fnBody = '';
        for ($i = count($keyAry) - 1; $i >= 0; $i--) {
            $k = $keyAry[$i];
            $t = $dirAry[$i];
            $f = -1 * $t;
            $aStr = '$a[\'' . $k . '\']';
            $bStr = '$b[\'' . $k . '\']';

            if (strpos($k, '(') !== false) {
                $aStr = '$a->' . $k;
                $bStr = '$b->' . $k;
            }

            if ($fnBody == '') {
                $fnBody .= "if({$aStr} == {$bStr}) { return 0; }\n";
                $fnBody .= "return ({$aStr} < {$bStr}) ? {$t} : {$f};\n";
            } else {
                $fnBody = "if({$aStr} == {$bStr}) {\n" . $fnBody;
                $fnBody .= "}\n";
                $fnBody .= "return ({$aStr} < {$bStr}) ? {$t} : {$f};\n";
            }
        }

        if ($fnBody) {
            $sortFn = create_function('$a,$b', $fnBody);
382
383
384
385
386

            // TODO: at the moment, $sortFn() triggers some E_NOTICE warnings. We stop these here for a short time.
            $errorSet = error_reporting();
            error_reporting($errorSet & ~E_NOTICE);

387
            usort($ary, $sortFn);
388
389

            error_reporting($errorSet);
Carsten  Rose's avatar
Carsten Rose committed
390
            error_clear_last();
391
        }
392
    }
393

394
395
396
397
398
    /**
     * generateSortArg
     *
     * @return string
     */
399
400
401
402
403
404
405
    private function generateSortArg() {

        $sortArg = "";

        for ($i = 0; $i < $this->levelCount; $i++) {
            $sortArg = $sortArg . $i . " ASC, ";
        }
406

407
        $sortArg = substr($sortArg, 0, strlen($sortArg) - 2);
408

409
        return $sortArg;
410
    }
411
412

    /**
413
     * Executes the queries recursive. This Method is called for each sublevel.
414
415
416
417
418
419
     *
     * ROOTLEVEL
     * This method is called once from the main method.
     * For the first call the method executes the rootlevels
     *
     * SUBLEVEL
420
     * For each rootlevel the method calls it self with the level mode 0
421
422
423
     * If the next Level is a Sublevel it will be executed and $this->counter will be added by 1
     * The sublevel calls the method again for a following sublevel
     *
424
425
426
427
     * @param int $cur_level
     * @param array $super_level_array
     * @param int $counter
     * @return string
Marc Egger's avatar
Marc Egger committed
428
429
430
     * @throws \CodeException
     * @throws \DbException
     * @throws \DownloadException
431
432
433
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
434
435
436
     * @throws \Twig\Error\LoaderError
     * @throws \Twig\Error\RuntimeError
     * @throws \Twig\Error\SyntaxError
Carsten  Rose's avatar
Carsten Rose committed
437
438
439
     * @throws \Twig_Error_Loader
     * @throws \Twig_Error_Runtime
     * @throws \Twig_Error_Syntax
440
441
     * @throws \UserFormException
     * @throws \UserReportException
442
     */
443
444
    private function triggerReport($cur_level = 1, array $super_level_array = array(), $counter = 0) {
        $keys = array();
445
        $stat = array();
446
447
448
        $content = "";

        // CurrentLevel "10.10.50"
449
        if (isset($this->indexArray[$counter]) && is_array($this->indexArray[$counter])) {
450
            $fullLevel = implode(".", $this->indexArray[$counter]);
451
        } else {
452
            $fullLevel = '';
453
454
        }

455
        // Superlevel "10.10"
456
457
        if (!empty($super_level_array) && is_array($super_level_array)) {
            $full_super_level = implode(".", $super_level_array) . '.';
458
459
460
        } else {
            $full_super_level = '';
        }
461
462
463

        //condition1: indexArray
        //condition2: full_level == Superlevel (but at the length of the Superlevel)
464
465
        while ($counter < count($this->indexArray) && $full_super_level == substr($fullLevel, 0, strlen($full_super_level))) {
            $contentLevel = '';
Nicola Chiapolini's avatar
Nicola Chiapolini committed
466
            $contentSubqueries = '';
467
468
469

            //True: The cur_level is a subquery -> continue
            if ($cur_level != count($this->indexArray[$counter])) {
470
471
                ++$counter;
                if (isset($this->indexArray[$counter]) && is_array($this->indexArray[$counter])) {
472
                    $fullLevel = implode(".", $this->indexArray[$counter]);
473
                } else {
474
                    $fullLevel = '';
475
                }
476
477
478
479
                continue;
            }

            // Set debug, if one is specified else keep the parent one.
480
            $lineDebug = $this->getValueParentDefault(TOKEN_DEBUG, $full_super_level, $fullLevel, $cur_level, 0);
481
482

            // Prepare Error reporting
483
484
            $this->store->setVar(SYSTEM_SQL_RAW, $this->frArray[$fullLevel . "." . TOKEN_SQL], STORE_SYSTEM);
            $this->store->setVar(SYSTEM_REPORT_FULL_LEVEL, $fullLevel, STORE_SYSTEM);
485

486
            // Prepare SQL: replace variables. Actual 'line.total' or 'line.count' will recalculated: don't replace them now!
487
488
            unset($this->variables->resultArray[$fullLevel . ".line."][LINE_TOTAL]);
            unset($this->variables->resultArray[$fullLevel . ".line."][LINE_COUNT]);
489

490
            $sql = $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_SQL]);
491

492
            $this->store->setVar(SYSTEM_SQL_FINAL, $sql, STORE_SYSTEM);
493

Nicola Chiapolini's avatar
Nicola Chiapolini committed
494
            //Execute SQL. All errors have been already catched.
495
            unset($result);
496
            $result = $this->db->sql($sql, ROW_KEYS, array(), '', $keys, $stat);
497
498

            // If an array is returned, $sql was a query, otherwise an 'insert', 'update', 'delete', ...
499
            // Query: total number of rows
500
501
            // insert, delete, update: number of affected rows
            $rowTotal = isset($stat[DB_NUM_ROWS]) ? $stat[DB_NUM_ROWS] : $stat[DB_AFFECTED_ROWS];
502

503
504
505
            $this->variables->resultArray[$fullLevel . ".line."][LINE_TOTAL] = $rowTotal;
            $this->variables->resultArray[$fullLevel . ".line."][LINE_COUNT] = is_array($result) ? 1 : 0;
            $this->variables->resultArray[$fullLevel . ".line."][LINE_INSERT_ID] = $stat[DB_INSERT_ID] ?? 0;
506

Nicola Chiapolini's avatar
Nicola Chiapolini committed
507
508
509
510
511

            /////////////////////////////////
            //    Render SHEAD and HEAD    //
            /////////////////////////////////

512
            $contentLevel .= $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_SHEAD]);
513
            // HEAD: If there is at least one record, do 'head'.
514
            if ($rowTotal > 0) {
515
                $contentLevel .= $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_HEAD]);
516
            }
517

Nicola Chiapolini's avatar
Nicola Chiapolini committed
518
519
520
521
            ///////////////////////
            //    Render rows    //
            ///////////////////////

522
            if (is_array($result)) {
523
524

                // Prepare row alteration
525
                $arrRbgd = explode("|", $this->frArray[$fullLevel . "." . TOKEN_RBGD], 2);
526
527
528
529
530
                if (count($arrRbgd) < 2) {
                    $arrRbgd[] = '';
                    $arrRbgd[] = '';
                }

Nicola Chiapolini's avatar
Nicola Chiapolini committed
531
                // Prepare skip wrapping of indexed columns
Carsten  Rose's avatar
Carsten Rose committed
532
533
534
535
                $fSkipWrap = array();
                if ('' != ($str = ($this->frArray[$fullLevel . "." . TOKEN_FSKIPWRAP]) ?? '')) {
                    $str = str_replace(' ', '', $str);
                    $fSkipWrap = explode(',', $str);
536
537
538
539
540
                    // Decrement all values to start counting with 0.
                    foreach ($fSkipWrap as $key => $value) {
                        $fSkipWrap[$key] = $value - 1;
                    }

Carsten  Rose's avatar
Carsten Rose committed
541
542
543
                    $fSkipWrap = array_flip($fSkipWrap);
                }

544
545
                $newKeys = $this->splitColumnNames($keys, $fSkipWrap);

546
                //---------------------------------
Carsten  Rose's avatar
Carsten Rose committed
547
                // Process each row of result set
548
                $columnValueSeparator = "";
549
                $rowIndex = 0;
Carsten  Rose's avatar
Carsten Rose committed
550

551
                foreach ($result as $row) {
Nicola Chiapolini's avatar
Nicola Chiapolini committed
552
                    // increment record number counter
553
                    $this->variables->resultArray[$fullLevel . ".line."][LINE_COUNT] = ++$rowIndex;
554
555
556

                    // replace {{<level>.line.count}} and {{<level>.line.total}} in __result__, if the variables specify their own full_level. This can't be replaced before firing the query.
                    for ($ii = 0; $ii < count($row); $ii++) {
557
558
                        $row[$ii] = str_replace("{{" . $fullLevel . ".line.count}}", $rowIndex, $row[$ii]);
                        $row[$ii] = str_replace("{{" . $fullLevel . ".line.total}}", $rowTotal, $row[$ii]);
559
                    }
560

561
                    // SEP set separator (empty on first run)
562
563
                    $contentLevel .= $columnValueSeparator;
                    $columnValueSeparator = $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_RSEP]);
564

Carsten  Rose's avatar
Carsten Rose committed
565
                    // RBEG
566
                    $rbeg = $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_RBEG]);
567

568
                    // RBGD: even/odd rows
569
                    $contentLevel .= str_replace(TOKEN_RBGD, $arrRbgd[$rowIndex % 2], $rbeg);
570

571
572
                    //-----------------------------
                    // COLUMNS: Collect all columns
573
                    $contentLevel .= $this->collectRow($row, $newKeys, $fullLevel, $rowIndex);
574

575
                    // REND
576
                    $contentLevel .= $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_REND]);
577

578
                    // Trigger subqueries of this level
Nicola Chiapolini's avatar
Nicola Chiapolini committed
579
580
581
                    $contentSubquery = $this->triggerReport($cur_level + 1, $this->indexArray[$counter], $counter + 1);
                    $contentSubqueries .= $contentSubquery;
                    $contentLevel .= $contentSubquery;
582

583
                    // RENR
584
                    $contentLevel .= $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_RENR]);
585
                }
586
587
            }

Nicola Chiapolini's avatar
Nicola Chiapolini committed
588
589
590
591
592

            /////////////////////////////////////////////////
            //    Render TAIL, ALT_HEAD, ALT_SQL, STAIL    //
            /////////////////////////////////////////////////

593
            if ($rowTotal > 0) {
594
                // tail
595
                $contentLevel .= $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_TAIL]);
596
            } else {
597
                // althead
598
                $contentLevel .= $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_ALT_HEAD]);
599
                // altsql
600
                $sql = $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_ALT_SQL]);
601
602
                if (!empty($sql)) {
                    $result = $this->db->sql($sql, ROW_KEYS, array(), '', $keys, $stat);
603

604
605
                    $newKeys = $this->splitColumnNames($keys);

606
607
608
609
610
                    $this->variables->resultArray[$fullLevel . ".line."][LINE_ALT_TOTAL] = $rowTotal;
                    $this->variables->resultArray[$fullLevel . ".line."][LINE_ALT_COUNT] = is_array($result) ? 1 : 0;
                    $this->variables->resultArray[$fullLevel . ".line."][LINE_ALT_INSERT_ID] = $stat[DB_INSERT_ID] ?? 0;

                    if (is_array($result)) {
611
612
                        foreach ($result as $row) {
                            $rowIndex = 0;
613
                            $contentLevel .= $this->collectRow($row, $newKeys, $fullLevel, $rowIndex);
614
                        }
615
616
                    }
                }
617
            }
618

619
620
            $contentLevel .= $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_STAIL]);

Nicola Chiapolini's avatar
Nicola Chiapolini committed
621
622
623
624
625
626
627
628
629
630
631
632
633
634

            ///////////////////////
            //    render TWIG    //
            ///////////////////////

            $twig_template = $this->frArray[$fullLevel . "." . TOKEN_TWIG];
            if ($twig_template !== '') {
                $contentLevel = $this->renderTwig($twig_template, $result, $keys);
            }

            ////////////////////////////////////////////////////////////
            //    Show / Hide / Save Level Content  (TOKEN_CONTENT)   //
            ////////////////////////////////////////////////////////////

635
636
637
            $token = $this->frArray[$fullLevel . "." . TOKEN_CONTENT];
            switch ($token) {

Nicola Chiapolini's avatar
Nicola Chiapolini committed
638
639
640
641
                case TOKEN_CONTENT_HIDE_LEVEL:
                    $this->variables->resultArray[$fullLevel . ".line."][TOKEN_CONTENT] = $contentLevel;
                    $contentLevel = $contentSubqueries;
                    break;
642
643
644
645
646
647
648
                case TOKEN_CONTENT_HIDE:
                    $this->variables->resultArray[$fullLevel . ".line."][TOKEN_CONTENT] = $contentLevel;
                    $contentLevel = '';
                    break;
                case TOKEN_CONTENT_STORE:
                    $this->variables->resultArray[$fullLevel . ".line."][TOKEN_CONTENT] = $contentLevel;
                    break;
649
                case TOKEN_CONTENT_SHOW:
650
651
652
653
                case '':
                    break;

                default:
Marc Egger's avatar
Marc Egger committed
654
                    throw new \UserReportException ("Unknown token: $token in Line '$fullLevel''", ERROR_UNKNOWN_TOKEN);
655
656
657
658
                    break;
            }

            $content .= $contentLevel;
659

Nicola Chiapolini's avatar
Nicola Chiapolini committed
660
661
662
663
664

            ///////////////////////////////
            //    Switch to next Level   //
            ///////////////////////////////

665
666
            ++$counter;
            if (isset($this->indexArray[$counter]) && is_array($this->indexArray[$counter])) {
667
                $fullLevel = implode(".", $this->indexArray[$counter]);
668
            } else {
669
                $fullLevel = '';
670
            }
671
672
        }
        return $content;
673
    }
674

675
676
    /**
     * Called with an array of column names.
Carsten  Rose's avatar
Carsten Rose committed
677
     * Each column name can be split in multiple string by '|': [s1[|s2[|s3]]]
678
     * Each s1|s2|s3 can be:  {title}, _{special column name}, _hide, _noWrap, _={title},  _+{tag}, _<{tag1}><{tag2}>
679
680
681
682
683
684
685
686
687
     *
     * Return an Array: newKeys[idx][C_FULL|C_TITLE|C_NO_WRAP|C_HIDE]
     *
     * @param array $keys
     * @param array $fSkipWrap
     * @return array
     */
    private function splitColumnNames(array $keys, array $fSkipWrap = array()) {

Carsten  Rose's avatar
Carsten Rose committed
688
        // Split key names in title / specialColumnName / fSkipWrap / hide
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
        $ii = 0;
        $newKeys = array();

        foreach ($keys as $key) {
            $newKeys[$ii][C_FULL] = $key;

            $arr = explode(PARAM_DELIMITER, $key);
            foreach ($arr as $kk) {
                if (($kk[0] ?? '') == TOKEN_COLUMN_CTRL) {
                    switch ($kk) {
                        case TOKEN_COLUMN_CTRL . COLUMN_NO_WRAP:
                            $newKeys[$ii][C_NO_WRAP] = 1;
                            break;
                        case TOKEN_COLUMN_CTRL . COLUMN_HIDE:
                            $newKeys[$ii][C_HIDE] = 1;
                            break;
                        default:
                            $newKeys[$ii][C_SPECIAL] = $kk;
                            break;
                    }
                } else {
                    $newKeys[$ii][C_TITLE] = $kk;
                }
            }

            // Explicit given fSkipWrap
            if (isset($fSkipWrap[$ii])) {
                $newKeys[$ii][C_NO_WRAP] = $fSkipWrap[$ii];
            }

            // Fallback, if no dedicated title is given.
            if (!isset($newKeys[$ii][C_TITLE])) {
                // If no title is given, check if there is a specialColumnName (backward compatibility)
                $newKeys[$ii][C_TITLE] = isset($newKeys[$ii][C_SPECIAL]) ? $newKeys[$ii][C_SPECIAL] : $newKeys[$ii][C_FULL];
            }

            if (($newKeys[$ii][C_TITLE][0] ?? '') == TOKEN_COLUMN_CTRL) {
                $newKeys[$ii][C_TITLE] = substr($newKeys[$ii][C_TITLE], 1);
            }
            $ii++;
        }

        return $newKeys;
    }

Nicola Chiapolini's avatar
Nicola Chiapolini committed
734
735
736
    /**
     * Render given Twig template with content from $result
     *
737
738
739
740
     * @param $twig_template
     * @param $result
     * @param $keys
     * @return string
Nicola Chiapolini's avatar
Nicola Chiapolini committed
741
     * @throws \CodeException
742
743
744
     * @throws \Twig_Error_Loader
     * @throws \Twig_Error_Runtime
     * @throws \Twig_Error_Syntax
Nicola Chiapolini's avatar
Nicola Chiapolini committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
     * @throws \UserFormException
     * @throws \UserReportException
     */
    private function renderTwig($twig_template, $result, $keys) {
        if (count(array_unique($keys)) !== count($keys)) {
            throw new \UserReportException("Twig Error: There are multiple columns with the same name in the SQL query.", ERROR_TWIG_COLUMN_NOT_UNIQUE);
        }

        // Turn Result into Associative array
        $resultAssoc = array();
        foreach ($result as $i => $row) {
            foreach ($row as $j => $value) {
                $resultAssoc[$i][$keys[$j]] = $value;
            }
        }

        $tmpl_start = substr($twig_template, 0, 5);

        if ($tmpl_start == "file:") {
            $loader = new \Twig\Loader\FilesystemLoader([".", "typo3conf/ext/qfq/Resources/Public/twig_templates"]);
            $twig_template = substr($twig_template, 5);
        } else {
            $loader = new \Twig\Loader\ArrayLoader(array(
                "string_template" => trim($twig_template, '"\''))  # trim is needed for backward compatibility for MNF
            );
            $twig_template = "string_template";
        }

        $twig = new \Twig\Environment($loader, array());

        // Add QFQ Link Filter
        $filter = new \Twig\TwigFilter('qfqlink', function ($string) {
            return $this->link->renderLink($string);
        }, ['is_safe' => ['html']]);
        $twig->addFilter($filter);

Marc Egger's avatar
Marc Egger committed
781
782
783
        // Json decode Filter
        // E.g.: {% set obj = '["this is one", "this is two"]' | json_decode%}
        $filter = new \Twig\TwigFilter('json_decode', function ($string) {
784
            return json_decode($string, true);
Marc Egger's avatar
Marc Egger committed
785
786
787
        }, ['is_safe' => ['html']]);
        $twig->addFilter($filter);

Nicola Chiapolini's avatar
Nicola Chiapolini committed
788
789
790
791
792
793
794
795
796
797
        // render Twig
        $contentTwig = $twig->render($twig_template, array(
            'context' => $resultAssoc,  // backward compatibility for MNF
            'result' => $resultAssoc,
            'store' => array(
                'record' => $this->store->getStore(STORE_RECORD),
                'sip' => $this->store->getStore(STORE_SIP),
                'typo3' => $this->store->getStore(STORE_TYPO3),
                'user' => $this->store->getStore(STORE_USER),
                'system' => $this->store->getStore(STORE_SYSTEM),
Marc Egger's avatar
Marc Egger committed
798
                'var' => $this->store->getStore(STORE_VAR),
Nicola Chiapolini's avatar
Nicola Chiapolini committed
799
800
801
802
803
            )
        ));
        return $contentTwig;
    }

804
805
806
807
808
809
810
    /**
     * Determine value:
     * 1) if one specified in line: take it
     * 2) if one specified in upper level: take it
     * 3) if none above take default
     * Set value on $full_level
     *
811
812
813
814
815
     * @param string $level_key - 'db' or 'debug'
     * @param string $full_super_level - f.e.: 10.10.
     * @param string $full_level - f.e.: 10.10.10.
     * @param string $cur_level - f.e.: 2
     * @param string $default - f.e.: 0
Carsten  Rose's avatar
Carsten Rose committed
816
     *
817
818
819
820
     * @return   string  The calculated value.
     */
    private function getValueParentDefault($level_key, $full_super_level, $full_level, $cur_level, $default) {

821
        if (!empty($this->frArray[$full_level . "." . $level_key])) {
822
823
824
825
826
            $value = $this->frArray[$full_level . "." . $level_key];
        } else {
            if ($cur_level == 1) {
                $value = $default;
            } else {
827
                $value = $this->variables->resultArray[$full_super_level . ".line."][$level_key] ?? '';
828
829
830
831
832
            }
        }
        $this->variables->resultArray[$full_level . ".line."][$level_key] = $value;

        return ($value);
833
    }
834
835
836
837

    /**
     * Steps through 'row' and collects all columns
     *
Carsten  Rose's avatar
Carsten Rose committed
838
839
     * @param array $row Recent row fetch from sql resultset.
     * @param array $keys List of all columnnames
840
841
     * @param string $full_level Recent position to work on.
     * @param string $rowIndex Index of recent row in resultset.
Carsten  Rose's avatar
Carsten Rose committed
842
     *
843
     * @return string               Collected content of all printable columns
Marc Egger's avatar
Marc Egger committed
844
845
846
     * @throws \CodeException
     * @throws \DbException
     * @throws \DownloadException
847
848
849
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
850
851
852
853
854
     * @throws \Twig\Error\LoaderError
     * @throws \Twig\Error\RuntimeError
     * @throws \Twig\Error\SyntaxError
     * @throws \UserFormException
     * @throws \UserReportException
855
     */
856
    private function collectRow(array $row, array $keys, $full_level, $rowIndex) {
857
        $content = "";
858
        $assoc = array();
859

860
        $fsep = '';
Carsten  Rose's avatar
Carsten Rose committed
861

862
863
        for ($ii = 0; $ii < count($keys); $ii++) {

864
            // Debugging
865
            $this->store->setVar(SYSTEM_REPORT_COLUMN_INDEX, $ii + 1, STORE_SYSTEM);
866
            $this->store->setVar(SYSTEM_REPORT_COLUMN_NAME, $keys[$ii][C_FULL], STORE_SYSTEM);
867
            $this->store->setVar(SYSTEM_REPORT_COLUMN_VALUE, $row[$ii], STORE_SYSTEM);
868

869
870
            $flagOutput = false;
            $renderedColumn = $this->renderColumn($ii, $keys[$ii], $row[$ii], $full_level, $rowIndex, $flagOutput);
871

872
            $keyAssoc = OnString::stripFirstCharIf(TOKEN_COLUMN_CTRL, $keys[$ii][C_TITLE]);
873
            $keyAssoc = OnString::stripFirstCharIf(COLUMN_STORE_USER, $keyAssoc);
874
875
            if ($keyAssoc != '') {
                $assoc[$keyAssoc] = $row[$ii];
876
                $assoc[REPORT_TOKEN_FINAL_VALUE . $keyAssoc] = $renderedColumn;
877
878
            }

879
            if ($flagOutput) {
880
                //prints
Carsten  Rose's avatar
Carsten Rose committed
881

882
                if (!isset($keys[$ii][C_NO_WRAP])) {
Carsten  Rose's avatar
Carsten Rose committed
883
884
885
886
                    $content .= $this->variables->doVariables($fsep);
                    $content .= $this->variables->doVariables($this->frArray[$full_level . "." . TOKEN_FBEG]);
                }

887
                $content .= $renderedColumn;
Carsten  Rose's avatar
Carsten Rose committed
888

889
                if (!isset($keys[$ii][C_NO_WRAP])) {
Carsten  Rose's avatar
Carsten Rose committed
890
891
892
                    $content .= $this->variables->doVariables($this->frArray[$full_level . "." . TOKEN_FEND]);
                }

893
                $fsep = $this->frArray[$full_level . "." . TOKEN_FSEP];
894
895
            }
        }
Carsten  Rose's avatar
Carsten Rose committed
896

897
        $this->store->appendToStore($assoc, STORE_RECORD);
898

899
        return ($content);
900
    }
901
902
903
904

    /**
     * Renders column depending of column name (if name is a reserved column name)
     *
905
     * @param string $columnIndex
906
     * @param array $columnCtrl
907
908
909
     * @param string $columnValue
     * @param string $full_level
     * @param string $rowIndex
910
     * @param bool $flagOutput
Carsten  Rose's avatar
Carsten Rose committed
911
     *
912
     * @return string rendered column
Marc Egger's avatar
Marc Egger committed
913
914
915
     * @throws \CodeException
     * @throws \DbException
     * @throws \DownloadException
916
917
918
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
919
920
921
922
923
     * @throws \Twig\Error\LoaderError
     * @throws \Twig\Error\RuntimeError
     * @throws \Twig\Error\SyntaxError
     * @throws \UserFormException
     * @throws \UserReportException
924
     */
925
    private function renderColumn($columnIndex, array $columnCtrl, $columnValue, $full_level, $rowIndex, &$flagOutput) {
926
        $content = "";
927
928
        $flagControl = false;
        $flagOutput = true;
929

930
        // Special column name:  '_...'? Empty column names are allowed: check with isset
931
        if (($columnCtrl[C_SPECIAL][0] ?? '') === TOKEN_COLUMN_CTRL) {
932
            $flagControl = true;
933
            $columnName = substr($columnCtrl[C_SPECIAL], 1);
934

935
936
            // Special column name and hide output
            if (isset($columnCtrl[C_HIDE])) {
937
938
                $flagOutput = false;
            }
939

940
941
            switch ($columnName) {
                case COLUMN_LINK:
942
                case COLUMN_WEBSOCKET:
943
                case COLUMN_REST_CLIENT:
944
                    $content .= $this->link->renderLink($columnValue);
945
                    break;
Marc Egger's avatar
Marc Egger committed
946
947
948
                case COLUMN_SCRIPT:
                    $content .= ColumnScript::render($columnValue);
                    break;
949

950
951
952
953
                case COLUMN_EXEC:
                    $rc = '';
                    $content .= Support::qfqExec($columnValue, $rc);
                    break;
954

955
956
957
958
959
960
961
962
963
964
965
966
967
                // Uppercase 'P'
                case COLUMN_PPAGE:
                case COLUMN_PPAGEC:
                case COLUMN_PPAGED:
                case COLUMN_PPAGEE:
                case COLUMN_PPAGEH:
                case COLUMN_PPAGEI:
                case COLUMN_PPAGEN:
                case COLUMN_PPAGES:
                    $lowerColumnName = strtolower($columnName);
                    $tokenizedValue = $this->doFixColPosPage($columnName, $columnValue);
                    $linkValue = $this->doPage($lowerColumnName, $tokenizedValue);
                    $content .= $this->link->renderLink($linkValue);
968
969
                    break;

970
971
972
973
974
975
976
977
978
979
980
981
                // Lowercase 'P'
                case COLUMN_PAGE:
                case COLUMN_PAGEC:
                case COLUMN_PAGED:
                case COLUMN_PAGEE:
                case COLUMN_PAGEH:
                case COLUMN_PAGEI:
                case COLUMN_PAGEN:
                case COLUMN_PAGES:
                    $linkValue = $this->doPage($columnName, $columnValue);
                    $content .= $this->link->renderLink($linkValue);
                    break;
982

983
984
985
                case COLUMN_YANK:
                    $linkValue = $this->doYank($columnName, $columnValue);
                    $content .= $this->link->renderLink($linkValue);
Carsten  Rose's avatar
Carsten Rose committed
986
987
                    break;

988
989
990
991
992
993
994
995
                case COLUMN_PPDF:
                case COLUMN_FFILE:
                case COLUMN_ZZIP:
                    $lowerColumnName = strtolower($columnName);
                    $tokenizedValue = $this->doFixColPosDownload($columnValue);
                    $linkValue = $this->doDownload($lowerColumnName, $tokenizedValue);
                    $content .= $this->link->renderLink($linkValue);
                    break;
996

997
998
999
1000
                case COLUMN_PDF:
                case COLUMN_FILE:
                case COLUMN_ZIP:
                case COLUMN_EXCEL:
For faster browsing, not all history is shown. View entire blame