Variables.php 5.43 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
27
use IMATHUZH\Qfq\Core\Evaluate;
 
28

29

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

    public $resultArray = array();

    // TODO to explain
    private $tt_content_uid = 0;
40
41
42
43
    /**
     * @var Evaluate
     */
    private $eval;
44
45

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

        $this->eval = $eval;
52
53

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

    }

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

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

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

        // Try the Stores
        $str = $this->eval->parse($str);
Carsten  Rose's avatar
Carsten Rose committed
81

82
83
84
85
86
        return $str;
    }

    /**
     * Callbackfunction called by variableSQL()
87
     * Replaces the variablenames with the value from the resultArray
88
89
     *
     * @param $matches
Carsten  Rose's avatar
Carsten Rose committed
90
     *
91
92
93
94
95
96
     * @return string The content that is displayed on the website
     * @internal param string $content : The PlugIn content
     * @internal param array $conf : The PlugIn configuration
     */
    public function replaceVariables($matches) {

97
98
99
        // $matches[0]: {{variablename: 10.20.<columnname>}}
        // $matches[1]: variablename: 10.20.<columnname>
        $data = $matches[0];
100
101

        // index of last '.'
102
        $pos = strrpos($matches[1], ".");
103
104
105
        if ($pos !== false) {
            $fullLevel = substr($matches[1], 0, $pos + 1);
            $varName = substr($matches[1], $pos + 1, strlen($matches[1]));
106

107
108
109
110
            if (isset($this->resultArray[$fullLevel][$varName])) {
                $data = $this->resultArray[$fullLevel][$varName];
            }
        }
111

112
        // If not replaced, try the Stores
113
114
115
116
117
//        if ($data === $matches[0]) {
//            $dataTmp = $this->eval->parse($data);
//            if ($dataTmp !== false)
//                $data = $dataTmp;
//        }
118
119

        return $data;
120
121
122
123
124
125
    }

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

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

            $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"];
        }

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

        $arr["be_user_uid"] = (isset($GLOBALS['BE_USER'])) ? $GLOBALS['BE_USER']->user["uid"] : '-';
158
        $arr["ttcontent_uid"] = $this->tt_content_uid;
159
160
161
162
163
        if (isset($GLOBALS["TSFE"])) {
            $arr["page_id"] = $GLOBALS["TSFE"]->id;
            $arr["page_type"] = $GLOBALS["TSFE"]->type;
            $arr["page_language_uid"] = $GLOBALS["TSFE"]->sys_language_uid;
        }
164
165

        return ($arr);
Carsten  Rose's avatar
Carsten Rose committed
166
    }
167

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