Variables.php 5.73 KB
Newer Older
1
2
3
4
<?php
/***************************************************************
 *  Copyright notice
 *
Carsten  Rose's avatar
Carsten Rose committed
5
 *  (c) 2010 Glowbase GmbH
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 *  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!
 ***************************************************************/

Marc Egger's avatar
Marc Egger committed
24
namespace IMATHUZH\Qfq\Core\Report;
25

Marc Egger's avatar
Marc Egger committed
26
use IMATHUZH\Qfq\Core\Evaluate;
27
use IMATHUZH\Qfq\Core\Helper\OnString;
28
use IMATHUZH\Qfq\Core\Store\T3Info;
29

30

31
32
33
34
/**
 * Class Variables
 * @package qfq
 */
35
36
37
38
39
40
class Variables {

    public $resultArray = array();

    // TODO to explain
    private $tt_content_uid = 0;
41

42
43
44
45
    /**
     * @var Evaluate
     */
    private $eval;
46
47

    /**
48
49
     * @param Evaluate $eval
     * @param int $tt_content_uid
50
     */
51
52
53
    public function __construct(Evaluate $eval, $tt_content_uid = 0) {

        $this->eval = $eval;
54
55

        // specified once during initialisation.
56
        $this->tt_content_uid = $tt_content_uid;
57
58
59
60

    }

    /**
61
     * Matches on the variables {{10.20.name}} and substitutes them with the values
62
63
     *
     * @param string $text
Carsten  Rose's avatar
Carsten Rose committed
64
     *
65
     * @return mixed
Marc Egger's avatar
Marc Egger committed
66
67
68
69
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
70
71
     */
    public function doVariables($text) {
72
73
74
75
76
77
78

        if ($text == '') {
            return '';
        }

//        $str = preg_replace_callback("/{{(([a-zA-Z0-9.:_])*)}}/", array($this, 'replaceVariables'), $text);
        // Process all {{x[.x].name}}
79
//        $str = preg_replace_callback('/{{\s*(([0-9]+.)+[a-zA-Z0-9_.]+)\s*}}/', 'self::replaceVariables', $text);
80
81
//        $str = preg_replace_callback('/{{\s*(([0-9]+.)+[a-zA-Z0-9_.]+)(:.*)*\s*}}/', 'self::replaceVariables', $text);
        $str = preg_replace_callback('/{{\s*(([0-9]+.)+[a-zA-Z0-9_.]+)(:[a-zA-Z-]*)*\s*}}/', 'self::replaceVariables', $text);
82
83

        // Try the Stores
84
        return $this->eval->parse($str);
85
86
87
88
    }

    /**
     * Callbackfunction called by variableSQL()
89
90
91
     * Replaces the variablenames with the value from the resultArray.
     * a) 10.<colname>
     * b) 10.line.total|count|insertId|content
92
93
     *
     * @param $matches
Carsten  Rose's avatar
Carsten Rose committed
94
     *
95
96
     * @return string The content that is displayed on the website
     */
97
    public function replaceVariables($matches): string {
98

99
100
101
102
        // $matches[0]: {{10.20.<columnname>::u:}}
        // $matches[1]: 10.20.<columnname>
        // $matches[2]: 10.20
        // $matches[3]: ::u:
103
        $data = $matches[0];
104
105

        // index of last '.'
106
        $pos = strrpos($matches[1], ".");
107
        if ($pos !== false) {
108

109
110
            $fullLevel = substr($matches[1], 0, $pos + 1);
            $varName = substr($matches[1], $pos + 1, strlen($matches[1]));
111

112
            if (isset($this->resultArray[$fullLevel][$varName])) {
113
                //
114
                $data = $this->resultArray[$fullLevel][$varName];
115
                if (isset($matches[3])) {
116
117
                    $arr = explode(':', $matches[3]);
                    $data = OnString::escape($arr[1] ?? '', $this->resultArray[$fullLevel][$varName], $rcFlagWipe);
118
                }
119
120
            }
        }
121

122
        return $data;
123
124
125
126
127
    }

    /**
     * Collect Global Variables
     *
128
     * @param void
Carsten  Rose's avatar
Carsten Rose committed
129
     *
130
131
     * @return    array with global variables which might be replaced
     */
132
    public function collectGlobalVariables(): array {
133
134
        $arr = array();

135
136
        if (isset($_SERVER["REMOTE_ADDR"])) {
            //TODO: Variablen sollten vom STORE_TYPO3 genommen werden
Carsten  Rose's avatar
Carsten Rose committed
137
138
139
            $arr["REMOTE_ADDR"] = $_SERVER["REMOTE_ADDR"] ?? '';
            $arr["HTTP_HOST"] = $_SERVER["HTTP_HOST"] ?? '';
            $arr["REQUEST_URI"] = $_SERVER["REQUEST_URI"] ?? '';
140
141
142
143
144
145
146
147
148
149
150
151

            $protocol = 'http';
            if (isset($_SERVER['HTTPS'])) {
                if ($_SERVER["HTTPS"] != "off")
                    $protocol = 'https';
            }
            $arr["url"] = $protocol . "://" . $_SERVER["HTTP_HOST"];
            if ($_SERVER["SERVER_PORT"] != 80)
                $arr["url"] .= ":" . $_SERVER["SERVER_PORT"];
            $arr["url"] .= $arr["REQUEST_URI"];
        }

152
        if (isset($GLOBALS["TSFE"]->fe_user)) {
153
154
            $arr["fe_user_uid"] = $GLOBALS["TSFE"]->fe_user->user["uid"] ?? '-';
            $arr["fe_user"] = $GLOBALS["TSFE"]->fe_user->user["username"] ?? '-';
155
156
157
158
159
160
        } else {
            $arr["fe_user_uid"] = '-';
            $arr["fe_user"] = '-';
        }

        $arr["be_user_uid"] = (isset($GLOBALS['BE_USER'])) ? $GLOBALS['BE_USER']->user["uid"] : '-';
161
        $arr["ttcontent_uid"] = $this->tt_content_uid;
162

163
164
165
        if (isset($GLOBALS["TSFE"])) {
            $arr["page_id"] = $GLOBALS["TSFE"]->id;
            $arr["page_type"] = $GLOBALS["TSFE"]->type;
166

167
            $arr["page_language_uid"] = T3Info::getLanguageId();
168
        }
169
170

        return ($arr);
Carsten  Rose's avatar
Carsten Rose committed
171
    }
172

Carsten  Rose's avatar
Carsten Rose committed
173
    /**
Carsten  Rose's avatar
Carsten Rose committed
174
175
     * @param        $arr
     * @param        $return
Carsten  Rose's avatar
Carsten Rose committed
176
177
178
     * @param string $keyPath
     */
    public function linearizeArray($arr, &$return, $keyPath = "") {
179
180
        if (is_array($arr)) {
            foreach ($arr as $key => $value) {
Carsten  Rose's avatar
Carsten Rose committed
181
                $this->linearizeArray($value, $return, $keyPath . "_" . $key);
182
183
            }
        } else {
Carsten  Rose's avatar
Carsten Rose committed
184
            $return[ltrim($keyPath, "_")] = $arr;
185
        }
Carsten  Rose's avatar
Carsten Rose committed
186
    }
187
}