Report.php 38.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
/***************************************************************
 *  Copyright notice
 *
 *  (c) 2010 Glowbase GmbH <support@glowbase.com>
 *  All rights reserved
 *
 ***************************************************************/

namespace qfq;

//use qfq;

require_once(__DIR__ . '/Define.php');
require_once(__DIR__ . '/Utils.php');
require_once(__DIR__ . '/Variables.php');
require_once(__DIR__ . '/Error.php');
18
19
//require_once(__DIR__ . '/Db.php');
require_once(__DIR__ . '/../Database.php');
20
21
22
23
24
25
require_once(__DIR__ . '/Link.php');
require_once(__DIR__ . '/Sendmail.php');


class Report {

Carsten  Rose's avatar
Carsten Rose committed
26
27
28
29
30
    /**
     * @var SIP
     */
    private $sip = null;

31
32
33
34
35
36
37
38
39
40
41
    /**
     * @var string
     */
    private $dbAlias = '';
    /**
     * @var Log
     */
    private $log = null;

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

43
44
    // $indexArray[10][50][5]   one entry per 'sql' statement
    private $indexArray = array();
45

46
47
48
49
50
51
52
53
    // TODO to explain
//	private $resultArray = array();
    private $levelCount = 0;
    //private $counter = 0;
    /**
     * @var Variables
     */
    private $variables = null;
54

55
56
57
58
59
60
61
62
63
    /**
     * @var Utils
     */
    private $utils = null;

    /**
     * @var Db
     */
    private $db = null;
64

65
66
67
68
69
70
71
72
73
74
    /**
     * @var Sendmail
     */
    private $sendmail = null;

    private $page_control = array();

    // Emulate global variable: will be set much earlier in other functions. Will be shown in error messages.
    private $fr_error = array('uid' => '', 'pid' => '', 'row' => '', 'debug_level' => '0', 'full_level' => '');

75
    private $t3data = array();
76

77
78
    private $phpUnit = false;

79
    /**
80
     * Report constructor.
81
     *
82
     * @param array $t3data
83
     */
84
85
86
    public function __construct(array $t3data, $sessionName, $phpUnit = false) {

        $this->phpUnit = $phpUnit;
87

88
        $this->t3data = $t3data;
89
        $this->sip = new Sip($sessionName, $phpUnit);
90
91
92
93
94
95
96
97
98
99
100
101
102
103

        $this->page_control["msgbox"]["pagec"] = "Please confirm!";

        $this->page_control["hash"]["paged"] = "h";
        $this->page_control["hash"]["pagee"] = "h";
        $this->page_control["hash"]["pagen"] = "h";

        $this->page_control["icon"]["paged"] = "D";
        $this->page_control["icon"]["pagee"] = "E";
        $this->page_control["icon"]["pageh"] = "H";
        $this->page_control["icon"]["pagei"] = "I";
        $this->page_control["icon"]["pagen"] = "N";
        $this->page_control["icon"]["pages"] = "S";

104
105
106
        $this->db = new Database();
        $this->utils = new Utils();
        $this->variables = new Variables($t3data["uid"]);
107

108
        // Set static values, which won't change during this run.
109
        $this->fr_error["pid"] = isset($this->variables->resultArray['global.']['page_id']) ? $this->variables->resultArray['global.']['page_id'] : 0;
110
111
        $this->fr_error["uid"] = $t3data["uid"];
        $this->fr_error["debug_level"] = 0;
112

Carsten  Rose's avatar
Carsten Rose committed
113
        // Sanitize function for POST and GET Parameters.
114
115
116
117
        // Merged URL-Parameter (key1, id etc...) in resultArray.
        $this->variables->resultArray = array_merge($this->variables->resultArray, array("url." => $this->utils->sanitize()),
            array("global." => $this->variables->collectGlobalVariables()));

118
119
        // Create Logclass.
        $this->log = new Log($this->variables->resultArray['global.']);
120

121
122
        // Create sendmail Class. Take care to prepare a fr_log instance.
        $this->sendmail = new Sendmail($this->log);
123
124
125
126
127
128
129
130
    }

    /**
     * Main function. Parses bodytext and iterates over all queries.
     *
     * @return string
     */
    public function process() {
131

132
        $this->log->set_fr_error($this->fr_error);
133

134
        // Iteration over Bodytext
135
        $ttLineArray = explode("\n", $this->t3data['bodytext']);
136

137
138
139
        foreach ($ttLineArray as $index => $line) {
            // Fill $frArray, $indexArray, $resultArray
            $this->parseFRLine($line);
140
        }
141

142
143
144
145
146
147
        // Sort array
        $this->sortIndexArray($this->indexArray, $this->generateSortArg());

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

148
149
150
151
152
153
154

        if ($this->fr_error["debug_level"] >= DEBUG_EXTREME) {
//            debug($this->frArray);
//            debug($_SESSION);
        }

        return $content;
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

    /**
     * Split line in level, command, content and fill 'frArray', 'levelCount', 'indexArray'
     * Example: 10.50.5.sql = select * from person
     *
     * @param    string $ttLine : line to split in level, command, content
     * @return    void
     */
    private function parseFRLine($ttLine) {
        if ($this->fr_error["debug_level"] >= DEBUG_EXTREME) {
            // T3 function: debug()
//            debug(array('function' => 'parseFRLine'));
        }

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

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

        // comment ?
        if (substr($key, 0, 1) == "#") return;

        // select ...
        $value = trim($arr[1]);

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

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

        // 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);
        }
199
    }
200

201
202
203
204
205
    /**
     * @param $level
     * @param $frCmd
     * @param $value
     */
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
    private function setLine($level, $frCmd, $value) {
        if ($this->fr_error["debug_level"] >= DEBUG_EXTREME) {
            // T3 function: debug()
//            debug(array('function' => 'setLine'));
        }

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

        // per sql command
        //pro sql cmd wir der Indexarray abgefüllt. Dieser wird später verwendet um auf den $frArray zuzugreifen
        //if(preg_match("/^sql/i", $frCmd) == 1){
        if ($frCmd == "sql" || $frCmd == "form") {
            // Remember max level
            $this->levelCount = max(substr_count($level, '.') + 1, $this->levelCount);
            // $indexArray[10][50][5]
            $this->indexArray[] = explode(".", $level);
        }
224
    }
225
226
227
228

    /**
     * Sorts the associative array.
     *
229
230
     * @param    array $ary : The unsorted Level Array
     * @param    string $clause : the sort argument 0 ASC, 1 ASC... according to the number of columns
231
232
     * @return    The content that is displayed on the website
     */
233
    private function sortIndexArray(array &$ary, $clause, $ascending = true) {
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
        if ($this->fr_error["debug_level"] >= DEBUG_EXTREME) {
            // T3 function: debug()
//            debug(array('function' => 'sortIndexArray'));
        }

        $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);
            usort($ary, $sortFn);
        }
284
    }
285

286
287
288
289
290
    /**
     * generateSortArg
     *
     * @return string
     */
291
292
293
294
295
296
297
298
299
300
301
302
303
    private function generateSortArg() {
        if ($this->fr_error["debug_level"] >= DEBUG_EXTREME) {
            // T3 function: debug()
//            debug(array('function' => 'generateSortArg'));
        }

        $sortArg = "";

        for ($i = 0; $i < $this->levelCount; $i++) {
            $sortArg = $sortArg . $i . " ASC, ";
        }
        $sortArg = substr($sortArg, 0, strlen($sortArg) - 2);
        return $sortArg;
304
    }
305
306
307
308
309
310
311
312
313
314
315
316
317
318

    /**
     * Executes the queries recursive. This Method is called for each Sublevel.
     *
     * ROOTLEVEL
     * This method is called once from the main method.
     * For the first call the method executes the rootlevels
     *
     * SUBLEVEL
     * For each rootlevel the method calls it self whith the levelmode 0
     * 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
     *
     * @param int $cur_level Which level it will call [10] = level 1, [10.10] = level 2 ...
319
320
     * @param string $super_level_array The Value-Array of the indexarray [0=>10, 1=>50]
     * @param int $counter The outer numeric Arraykey of indexarray
321
322
323
324
325
326
327
     * @return string                       The content that is displayed on the website
     * @throws codeException
     * @throws SqlReportException
     * @throws SyntaxReportException
     * @throws UserReportException
     */

328
329
    private function triggerReport($cur_level = 1, array $super_level_array = array(), $counter = 0) {
        $keys = array();
330
331
332
333
334
335
336
337
338
339
        if ($this->fr_error["debug_level"] >= DEBUG_EXTREME) {
            // T3 function: debug()
//            debug(array('function' => 'triggerReport'));
        }

        $lineDebug = 0;
        $content = "";
        $rowTotal = 0;

        // CurrentLevel "10.10.50"
340
341
342
343
344
345
        if (isset($this->indexArray[$counter]) && is_array($this->indexArray[$counter])) {
            $full_level = implode(".", $this->indexArray[$counter]);
        } else {
            $full_level = '';
        }

346
        // Superlevel "10.10"
347
348
349
350
351
        if (isset($super_level_array) && is_array($super_level_array)) {
            $full_super_level = implode(".", $super_level_array);
        } else {
            $full_super_level = '';
        }
352
353
354
355
356
357
358

        //condition1: indexArray
        //condition2: full_level == Superlevel (but at the length of the Superlevel)
        while ($counter < count($this->indexArray) && $full_super_level == substr($full_level, 0, strlen($full_super_level))) {

            //True: The cur_level is a subquery -> continue
            if ($cur_level != count($this->indexArray[$counter])) {
359
360
361
362
363
364
                ++$counter;
                if (isset($this->indexArray[$counter]) && is_array($this->indexArray[$counter])) {
                    $full_level = implode(".", $this->indexArray[$counter]);
                } else {
                    $full_level = '';
                }
365
366
367
368
                continue;
            }

            // Set dbAlias if one is specified. Else keep the parent one.
369
370
            //TODO dbAlias
//            $this->dbAlias = $this->getValueParentDefault("db", $full_super_level, $full_level, $cur_level, DB);
371
372
373
374
375
376
377
378
379
380

            // Set debug, if one is specified else keep the parent one.
            $lineDebug = $this->getValueParentDefault("debug", $full_super_level, $full_level, $cur_level, 0);
            $this->fr_error["debug_level"] = max($lineDebug, $this->fr_error["debug_level"]); // collect the biggest debuglevel >> debug $_SESSION at the end.

            // Prepare Error reporting
            $this->fr_error["row"] = $full_level . ".sql = " . $this->frArray[$full_level . ".sql"];
            $this->fr_error["full_level"] = $full_level . ".sql";

            // Setter function to emulate global variables
381
382
            //TODO implement logging
//            $this->db->set_fr_error($this->fr_error);
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
            $this->log->set_fr_error($this->fr_error);

            // Debug
            if ($this->fr_error["debug_level"] >= DEBUG_EXTREME) {
                // T3 function: debug()
//                debug(array('full_level' => $full_level));
            }

            // Prepare SQL: replace variables.
            $sql = $this->variables->doVariables($this->frArray[$full_level . ".sql"]);

            // DEBUG
            if ($this->fr_error["debug_level"] >= DEBUG_SQL) {
                // T3 function: debug()
//                debug(array("native" => $full_level . ".sql = " . $this->frArray[$full_level . ".sql"], "replaced" => $full_level . ".sql = " . $sql));
            }
            if ($this->fr_error["debug_level"] >= DEBUG_EXTREME) {
                // T3 function: debug()
//                debug($this->variables->resultArray);
            }

            //Execute SQL. All errors have been already catched.
            unset($result);
406
            $result = $this->db->sqlKeys($sql, $keys);
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

            // If an array is returned, $sql was a query, otherwise an 'insert', 'update', 'delete', ...
            // Query: total nummber of rows
            // insert: last_insert_id
            // delete, update: number of affected rows
            $rowTotal = is_array($result) ? count($result) : $result;

            $this->variables->resultArray[$full_level . ".line."]["total"] = $rowTotal;

            // HEAD: If there is at least one record, do 'head'.
            if ($rowTotal > 0)
                $content .= $this->variables->doVariables($this->frArray[$full_level . "." . "head"]);

            // Prepare row alteration
            $arrRbgd = explode("|", $this->frArray[$full_level . "." . "rbgd"]);

            //---------------------------------
            // Process each row of resultset
            $columnValueSeperator = "";
            $rowIndex = 0;
            foreach ($result as $row) {
                // record number counter
                $this->variables->resultArray[$full_level . ".line."]["count"] = ++$rowIndex;

                // replace ~count and ~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++) {
                    $row[$ii] = str_replace("~" . $full_level . ".line.count", $rowIndex, $row[$ii]);
                    $row[$ii] = str_replace("~" . $full_level . ".line.total", $rowTotal, $row[$ii]);
                }

                // Create Assoc Array of result
                unset($rowAssoc);
                for ($i = 0; $i < count($row); $i++) {
                    $rowAssoc[$keys[$i]] = $row[$i];
                }

                // SEP set seperator (empty on first run)
                $content .= $columnValueSeperator;
                $columnValueSeperator = $this->variables->doVariables($this->frArray[$full_level . "." . "sep"]);

                // RBGD: gerade- ungerade- Zeilen/Rows
                $content .= str_replace("rbgd", $arrRbgd[$rowIndex % 2], $this->frArray[$full_level . "." . "rbeg"]);

                //-----------------------------
                // COLUMNS: Collect all columns
                $content .= $this->collectRow($row, $keys, $full_level, $rowIndex);

                // REND
                $content .= $this->variables->doVariables($this->frArray[$full_level . "." . "rend"]);

                // Trigger subqueries of this level
                $content .= $this->triggerReport($cur_level + 1, $this->indexArray[$counter], $counter + 1);

                // RENR
                $content .= $this->variables->doVariables($this->frArray[$full_level . "." . "renr"]);

                // this is necessary: queries 2,3,.. should not be replaced with content of resultArray but with the most recent values. So delete the entry in resultArray.
                unset($this->variables->resultArray[$full_level . ".line."]["total"]);
                unset($this->variables->resultArray[$full_level . ".line."]["count"]);
            }

            // set last valid counts again: should be available for later queries
            $this->variables->resultArray[$full_level . ".line."]["total"] = $rowTotal;
            $this->variables->resultArray[$full_level . ".line."]["count"] = $rowIndex;

            //Print althead or tail
            if ($rowTotal > 0) {
                $content .= $this->variables->doVariables($this->frArray[$full_level . "." . "tail"]);
            } else {
                $content .= $this->variables->doVariables($this->frArray[$full_level . "." . "althead"]);
            }

479
480
481
482
483
484
            ++$counter;
            if (isset($this->indexArray[$counter]) && is_array($this->indexArray[$counter])) {
                $full_level = implode(".", $this->indexArray[$counter]);
            } else {
                $full_level = '';
            }
485
486
487
        }

        return $content;
488
    }
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521

    /**
     * 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
     *
     * @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
     * @return   string  The calculated value.
     */
    private function getValueParentDefault($level_key, $full_super_level, $full_level, $cur_level, $default) {
        if ($this->fr_error["debug_level"] >= DEBUG_EXTREME) {
            // T3 function: debug()
//            debug(array('function' => 'getValueParentDefault'));
        }

        if ($this->frArray[$full_level . "." . $level_key]) {
            $value = $this->frArray[$full_level . "." . $level_key];
        } else {
            if ($cur_level == 1) {
                $value = $default;
            } else {
                $value = $this->variables->resultArray[$full_super_level . ".line."][$level_key];
            }
        }
        $this->variables->resultArray[$full_level . ".line."][$level_key] = $value;

        return ($value);
522
    }
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545

    /**
     * Steps through 'row' and collects all columns
     *
     * @param array $row Recent row fetch from sql resultset.
     * @param array $keys List of all columnnames
     * @param string $full_level Recent position to work on.
     * @param string $rowIndex Index of recent row in resultset.
     * @return string               Collected content of all printable columns
     * @throws SyntaxReportException
     */
    private function collectRow(array $row, array $keys, $full_level, $rowIndex) {
        $content = "";

        if ($this->fr_error["debug_level"] >= DEBUG_EXTREME) {
            // T3 function: debug()
//            debug(array('function' => 'collectRow'));
        }

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

            $this->fr_error["columnIndex"] = $ii;  // Debugging Information
            // Setter Function to emulate global variables.
546
547
            //TODO implement logging
//            $this->db->set_fr_error($this->fr_error);
548
549
550
551
552
553
554
555
556
557
558
            $this->log->set_fr_error($this->fr_error);

            $tmp = $this->renderColumn($ii, $keys[$ii], $row[$ii], $full_level, $rowIndex, $show);
            if ($show) {
                //prints
                $content .= $this->variables->doVariables($this->frArray[$full_level . "." . "fbeg"]);
                $content .= $tmp;
                $content .= $this->variables->doVariables($this->frArray[$full_level . "." . "fend"]);
            }
        }
        return ($content);
559
    }
560
561
562
563

    /**
     * Renders column depending of column name (if name is a reserved column name)
     *
564
565
566
567
568
569
570
571
     * @param string $columnIndex
     * @param string $columnName
     * @param string $columnValue
     * @param string $full_level
     * @param string $rowIndex
     * @param bool $show
     * @return string rendered column
     * @throws SyntaxReportException
572
573
574
575
576
577
578
579
580
     */
    private function renderColumn($columnIndex, $columnName, $columnValue, $full_level, $rowIndex, &$show) {
        if ($this->fr_error["debug_level"] >= DEBUG_EXTREME) {
            // T3 function: debug()
//            debug(array('function' => 'renderColumn'));
        }
#debug(array('columnIndex'=>$columnIndex, 'columnName'=>$columnName, 'columnValue'=>$columnValue,'full_level'=>$full_level,"rowIndex"=>$rowIndex));
        $show = TRUE;
        if (substr($columnName, 0, 1) == "_") {
581
            $show = false;
582
583
584
585
586
587
            $columnName = substr($columnName, 1);
        }

        $content = "";
        switch ($columnName) {
            case "link":
Carsten  Rose's avatar
Carsten Rose committed
588
                $link = new Link($full_level, $this->frArray[$full_level . ".sql"], $columnValue, $rowIndex, $columnIndex, $this->fr_error, $this->sip);
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
                $content .= $link->renderLink($columnValue, $rowIndex, $this->frArray[$full_level . "." . "hash." . "$columnIndex"]);
#				unset $link;
                break;
            case "exec":
                $content .= $this->myExec($columnValue);
                break;

            case "Page":
            case "Pagec":
            case "Paged":
            case "Pagee":
            case "Pageh":
            case "Pagei":
            case "Pagen":
            case "Pages":
                $linkValue = $this->doFixColPosPage($columnName, $columnValue);

Carsten  Rose's avatar
Carsten Rose committed
606
                $link = new Link($full_level, $this->frArray[$full_level . ".sql"], $linkValue, $rowIndex, $columnIndex, $this->fr_error, $this->dbAlias, $this->sip);
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
                $content .= $link->renderLink($linkValue, $rowIndex, $this->frArray[$full_level . "." . "hash." . "$columnIndex"]);
                break;

            case "page":
            case "pagec":
            case "paged":
            case "pagee":
            case "pageh":
            case "pagei":
            case "pagen":
            case "pages":
#debug($columnValue);
                $linkValue = $this->doPage($columnName, $columnValue);
// debug($linkValue);

Carsten  Rose's avatar
Carsten Rose committed
622
                $link = new Link($full_level, $this->frArray[$full_level . ".sql"], $linkValue, $rowIndex, $columnIndex, $this->fr_error, $this->dbAlias, $this->sip);
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
                $content .= $link->renderLink($linkValue, $rowIndex, $this->frArray[$full_level . "." . "hash." . "$columnIndex"]);
                break;

            case "img":
                // "<path to image>|[alttext]|[text behind]" renders to: <img src="<path to image>" alt="[alttext]">[text behind]
                if (empty($columnValue)) break;
                $tmp = explode("|", $columnValue, 3);
                if ($tmp[0] == "") break;
                $content .= '<img src="' . $tmp[0] . '" alt="' . $tmp[1] . '">' . $tmp[2];
                break;

            case "mailto":
                // "<email address>|[Real Name]"  renders to (encrypted via JS): <a href="mailto://<email address>"><email address></a> OR <a href="mailto://<email address>">[Real Name]</a>
                $tmp = explode("|", $columnValue, 2);
                if ($tmp[0] == "") break;

                $t1 = explode("@", $tmp[0], 2);
                $content .= "<script language=javascript><!--" . chr(10);
                if (empty($tmp[1])) $tmp[1] = $tmp[0];

                $content .= 'var contact = "' . substr($tmp[1], 0, 2) . '"' . chr(10);
                $content .= 'var contact1 = "' . substr($tmp[1], 2) . '"' . chr(10);
                $content .= 'var email = "' . $t1[0] . '"' . chr(10);
                $content .= 'var emailHost = "' . $t1[1] . '"' . chr(10);

                $content .= 'document.write("<a href=" + "mail" + "to:" + email + "@" + emailHost+ ">" + contact + "</a>")' . chr(10);
                $content .= 'document.write("<a href=" + "mail" + "to:" + email + "@" + emailHost+ ">" + contact1 + "</a>")' . chr(10);
                $content .= '//--></script>';
                break;

            case "sendmail":
                // 'Absender|Empfaenger, mehrere mit Komma getrennt|Betreff|Mailinhalt'
                $tmp = explode("|", $columnValue, 4);

                $mail['receiver'] = $tmp[0];
                $mail['sender'] = $tmp[1];
                $mail['subject'] = $tmp[2];
                $mail['body'] = $tmp[3];

                $content = $this->sendmail->sendmail($mail);
                break;

            case "vertical":
                // '<Text>|[angle]|[width]|[height]|[tag]'   , width and heigth needs a unit (px, em ,...), 'tag' might be 'div', 'span', ...
                $arr = explode("|", $columnValue, 5);

                # angle
                $angle = $arr[1] ? $arr[1] : 270;

                # width
                $width = $arr[2] ? $arr[2] : "1em";
                $tmp = "width:$width; ";

                # height
                if ($arr[3])
                    $tmp .= "height:" . $arr[3] . "; ";

                # tag
                if ($arr[4]) {
                    $tag = explode(" ", trim($arr[4]), 2);
                    $tag_open = "<" . $arr[4] . " ";
                    $tag_close = "</" . $tag[0] . ">";
                } else {
                    $tag_open = "<div ";
                    $tag_close = "</div>";
                }

                # http://scottgale.com/blog/css-vertical-text/2010/03/01/
                #$style = "writing-mode:tb-rl; -webkit-transform:rotate(270deg); -moz-transform:rotate(270deg); -o-transform: rotate(270deg); white-space:nowrap; width:1em;  display: block;";
                $style = "width:1em;  filter: flipv fliph; transform: rotate(" . $angle . "deg) translate(-10em,0); transform-origin: 0 0; writing-mode:tb-rl; -webkit-transform:rotate(" . $angle . "deg); -moz-transform:rotate(" . $angle . "deg); -o-transform: rotate(" . $angle . "deg); white-space:nowrap; display: block;";

                #$style = "line-height: 1.5em; background:#eee; display: block; white-space: nowrap; padding-left: 3px; writing-mode: tb-rl; filter: flipv fliph; transform: rotate(270deg) translate(-10em,0); transform-origin: 0 0; -moz-transform: rotate(270deg) translate(-10em,0); -moz-transform-origin: 0 0; -webkit-transform: rotate(270deg) translate(-10em,0); -webkit-transform-origin: 0 0;";
                $content = $tag_open . 'style="' . $style . '">' . $arr[0] . $tag_close;


                break;

            case "F":
                $newColumnName = "";
                $newFinalColumnName = "";
                $remain = array();
704
                $striptags = false;
705
706
707
708
709
710
711
712
713
714
715
716
717
                $tag = "";

                # 'Q:mailto|Z|V:mail_first|support@example.com'
                $arr = explode("|", $columnValue);
                foreach ($arr as $value) {
                    $kv = explode(":", $value);
                    switch ($kv[0]) {
                        case "Q":
                            $newColumnName = $kv[1];
                            #if(!$newColumnName) throw new syntaxException ( "Missing a 'reserved column name' for parameter 'Q' in column 'F': $columnValue","",__FILE__,__LINE__,$this->fr_error);
                            if ($newColumnName == 'F') throw new SyntaxReportException ("Not allowed: 'F' as 'reserved column name' for parameter 'Q': $columnValue", "", __FILE__, __LINE__, $this->fr_error);
                            break;
                        case "Z":
718
                            $show = false;
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
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
                            break;
                        case "T":
                            $striptags = TRUE;
                            break;
                        case "X":
                            $tag = $kv[1];
                            if (!$tag) throw new SyntaxReportException ("Missing the 'tag' parameter for 'X'):  $columnValue", "", __FILE__, __LINE__, $this->fr_error);
                            break;
                        case "V":
                            $newFinalColumnName = $kv[1];
                            if (!$newFinalColumnName) throw new SyntaxReportException ("Missing the 'name' parameter for 'V':  $columnValue", "", __FILE__, __LINE__, $this->fr_error);
                            break;
                        case 'F':
                            throw new SyntaxReportException ("Qualifier 'F' is not allowed inside of a column with column name 'F':  $columnValue", "", __FILE__, __LINE__, $this->fr_error);
                            break;
                        # Save every non 'F' qualifier for later usage
                        default:
                            $remain[] = $value;
                            break;
                    }
                }
                # Check for needed action
                #			if(!$newColumnName) throw new syntaxException ( "Missing parameter 'Q' in column 'F': $columnValue","",__FILE__,__LINE__,$this->fr_error);
                if ($newColumnName)
                    $columnName = $newColumnName;
                else
                    $columnName = "no column name defined";

                # reconstruct remaining parameters
                $arr = implode("|", $remain);
                # render
                $content = $this->renderColumn($columnIndex, $columnName, $arr, $full_level, $rowIndex, $dummy);

                if ($newFinalColumnName)
                    $columnName = $newFinalColumnName;

                # striptags
                if ($striptags) $content = strip_tags($content);
                # tag
                if ($tag) {
                    $arr = explode(" ", $tag);
                    $content = "<" . $tag . ">" . $content . "</" . $arr[0] . ">";
                }
                break;

            default :
                $content .= $columnValue;
                break;
        }
        $this->variables->resultArray[$full_level . "."][$columnName] = $content;

        return $content;
771
    }
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792

    /**
     * Executes the Command in $cmd
     * RC: if RC==0 Returns Output, else 'RC - Output'
     *
     * @param    string $cmd : contains the Comand
     * @return   string  The content that is displayed on the website
     */
    private function myExec($cmd) {
        if ($this->fr_error["debug_level"] >= DEBUG_EXTREME) {
            // T3 function: debug()
//            debug(array('function' => 'myExec'));
        }

        exec($cmd, $arr, $rc);

        $output = implode('<BR>', $arr);
        if ($rc != 0)
            $output = $rc . " - " . $output;

        return ($output);
793
    }
794
795
796
797
798
799

    /**
     * The main method of the PlugIn
     *
     * @param    string $content : The PlugIn content
     * @param    array $conf : The PlugIn configuration
Carsten  Rose's avatar
Carsten Rose committed
800
     * @return    string The content that is displayed on the website
801
     */
Carsten  Rose's avatar
Carsten Rose committed
802
803
804
    //Checkt ob der Beginn von Array2 gleich ist wie Array1
    // gibt true/false zurück

805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
    /**
     * Renders PageX: convert position content to token content. Respect default values depending on PageX
     *
     * @param    string $columnName
     * @param    string $columnValue
     * @return string rendered link
     *
     * $columnValue:
     * -------------
     * [<page id|alias>[&param=value&...]] | [record id] | [text] | [tooltip] | [msgbox] | [class] | [target] | [render mode] | [create hash]
     *
     * param[0]: <page id|alias>[&param=value&..
     * param[1]: record id
     * param[2]: text
     * param[3]: tooltip
     * param[4]: msgbox
     * param[5]: class
     * param[6]: target
     * param[7]: render mode
     * param[8]: create hash
     * @throws SyntaxReportException
     */
    private function doFixColPosPage($columnName, $columnValue) {
        if ($this->fr_error["debug_level"] >= DEBUG_EXTREME) {
            // T3 function: debug()
//            debug(array('function' => 'doFixColPosPage'));
        }

        $link = "";

        // Split definition
        $param = explode('|', $columnValue);
        if (count($param) > 9) throw new SyntaxReportException ("Too many parameter (max=9): $columnValue", "", __FILE__, __LINE__, $this->fr_error);

        // make first 'P' lowercase
        $columnName = 'p' . substr($columnName, 1);

        // -- Page --
        // Split PageId|PageAlias and  URL Params
        $tmparr = explode('&', $param[0], 2);

        $link .= $this->composeLinkPart('p', $tmparr[0]);            // -- PageID --
        $link .= $this->composeLinkPart('U', $tmparr[1]);            // -- URL Params --
        $link .= $this->composeLinkPart('i', $param[1]);                // -- record id --
        $link .= $this->composeLinkPart('t', $param[2]);                // -- Text --
        $link .= $this->composeLinkPart('o', $param[3]);                // -- tooltip --
        $link .= $this->composeLinkPart('q', $param[4], $this->page_control["msgbox"][$columnName]);                // -- msgbox
        $link .= $this->composeLinkPart('c', $param[5]);                // -- class --
        $link .= $this->composeLinkPart('g', $param[6]);                // -- target --
        $link .= $this->composeLinkPart('r', $param[7]);                // -- render mode --

        if (!$param[8]) $param[8] = $this->page_control["hash"][$columnName]; // if no hash behaviour defined, use default
        if ($param[8] == "h") $link .= "h|";

        if ($this->page_control["icon"][$columnName]) $link .= $this->page_control["icon"][$columnName] . "|";

        return ($link);
862
    }
863

Carsten  Rose's avatar
Carsten Rose committed
864
865
866
867
868
869
870
871
872
    /**
     * The main method of the PlugIn
     *
     * @param    string $content : The PlugIn content
     * @param    array $conf : The PlugIn configuration
     * @return   string The content that is displayed on the website
     */
    //Check ob arr1 nur 1 Feld mehr hat als arr2

873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
    /**
     * If there is a value (or a defaultValue): compose it together with qualifier and delimiter.
     *
     * @param    string $qualifier
     * @param    string $value
     * @param    string $defaultValue
     *
     * @return    string        rendered link
     */
    private function composeLinkPart($qualifier, $value, $defaultValue = "") {

        if ($this->fr_error["debug_level"] >= DEBUG_EXTREME) {
            // T3 function: debug()
//            debug(array('function' => 'composeLinkPart'));
        }

        if (!$value) $value = $defaultValue;

        if ($value)
            return ($qualifier . ":" . $value . "|");

        return '';
895
    }
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952

    /**
     * Renders pageX: extract token and determine if any default value has be applied
     *
     * @param    string $columnName
     * @param    string $columnValue
     *
     * @return    string        rendered link
     */
    private function doPage($columnName, $columnValue) {
        $defaultQuestion = '';

        if ($this->fr_error["debug_level"] >= DEBUG_EXTREME) {
            // T3 function: debug()
//        debug(array('function' => 'doPage'));

        }
        $param = explode('|', $columnValue);

        # get all defaultvalues, depending on the columnname
        $defaultImage = $this->page_control["icon"][$columnName];
        $defaultHash = $this->page_control["hash"][$columnName];
        # define defaultquestion only, if pagetype needs a question
        if ($this->page_control["msgbox"][$columnName]) $defaultQuestion = 'q:' . $this->page_control["msgbox"][$columnName];

        foreach ($param as $key) {
            switch (substr($key, 0, 1)) {
                case 'P':
                case 'E':
                case 'N':
                case 'D':
                case 'H':
                case 'I':
                case 'S':
                case 'B':
                case 'C':
                    $defaultImage = '';    // if any of the img token is given: no default
                    break;
                case 'h':
                    $defaultHash = '';    // if a hash definition is given: no default
                    break;
                case 'q':
                    $defaultQuestion = '';    // if a question is given: no default
                    break;
            }
        }

        $columnValue .= "|";

        // append defaulst
        if ($defaultImage) $columnValue .= $defaultImage . "|";
        if ($defaultHash) $columnValue .= $defaultHash . "|";
        if ($defaultQuestion) $columnValue .= $defaultQuestion . "|";

#debug($columnValue);

        return ($columnValue);
953
    }
954

Carsten  Rose's avatar
Carsten Rose committed
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
    /**
     * Prepare Session Array with Hash Entry: Only for form
     *
     * @param $formName
     * @param $formRecordId
     * @return string
     */
    private function form2hash($formName, $formRecordId) {
        if ($this->fr_error["debug_level"] >= DEBUG_EXTREME) {
            // T3 function: debug()
//            debug(array('function' => 'form2hash'));
        }
        $hash = $this->utils->randomAlphaNumUnique();

        $_SESSION[FORMREPORT][$hash]['formName'][0] = $formName;
        $_SESSION[FORMREPORT][$hash]['referrer'] = $_SERVER['REQUEST_URI'];

        if (is_numeric($formRecordId) && $formRecordId >= 0) {
            $_SESSION[FORMREPORT][$hash]['idMap'][1]['recordId'] = $formRecordId;
            $_SESSION[FORMREPORT][$hash]['idMap'][1]['param'] = "";
        }

        return ($hash);
    }

980
981
982
    /**
     * Generate SortArgument
     *
983
984
     * @param $variable
     * @return string
985
986
987
988
989
990
991
992
993
994
     */
    private function getResultArrayIndex($variable) {
        if ($this->fr_error["debug_level"] >= DEBUG_EXTREME) {
            // T3 function: debug()
//            debug(array('function' => 'getResultArrayIndex'));
        }

        $variable = substr($variable, 1, strlen($variable));
        return "[" . preg_replace_callback("/[a-z]/", "replaceToIndex", $variable) . "][" . preg_replace_callback("/[^a-z]/", "replaceToIndex", $variable) . "]";

995
    }
996

997
998
999
1000
    /**
     * @param $arr1
     * @param $arr2
     * @return bool