Variables.php 6.35 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
24
25
 *
 *  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!
 ***************************************************************/

namespace qfq;

26
27
require_once(__DIR__ . '/../Evaluate.php');

28
29
30
31
32
33
34
35
//use qfq;

class Variables {

    public $resultArray = array();

    // TODO to explain
    private $tt_content_uid = 0;
36
37
38
39
    /**
     * @var Evaluate
     */
    private $eval;
40
41
42
43

    /**
     * @param int $tmp_ttcontent_uid
     */
44
45
46
    public function __construct(Evaluate $eval, $tt_content_uid = 0) {

        $this->eval = $eval;
47
48

        // specified once during initialisation.
49
        $this->tt_content_uid = $tt_content_uid;
50
51
52
53

    }

    /**
54
     * Matches on the variables {{10.20.name}} and substitutes them with the values
55
56
     *
     * @param string $text
Carsten  Rose's avatar
Carsten Rose committed
57
     *
58
59
60
     * @return mixed
     */
    public function doVariables($text) {
61
62
63
64
65

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

66
//        $str = preg_replace_callback("/(~([a-zA-Z0-9._])*)/", array($this, 'replaceVariables'), $text);
67
68
69
70
71
72
//        $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
73

74
75
76
77
78
79
80
81
        return $str;
    }

    /**
     * Callbackfunction called by variableSQL()
     * Replaces the variablenames whith the value from the resultArray
     *
     * @param $matches
Carsten  Rose's avatar
Carsten Rose committed
82
     *
83
84
85
86
87
88
     * @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) {

89
90
91
        // $matches[0]: {{variablename: 10.20.<columnname>}}
        // $matches[1]: variablename: 10.20.<columnname>
        $data = $matches[0];
92
93

        // index of last '.'
94
        $pos = strrpos($matches[1], ".");
95
96
97
        if ($pos !== false) {
            $fullLevel = substr($matches[1], 0, $pos + 1);
            $varName = substr($matches[1], $pos + 1, strlen($matches[1]));
98

99
100
101
102
            if (isset($this->resultArray[$fullLevel][$varName])) {
                $data = $this->resultArray[$fullLevel][$varName];
            }
        }
103

104
        // If not replaced, try the Stores
105
106
107
108
109
//        if ($data === $matches[0]) {
//            $dataTmp = $this->eval->parse($data);
//            if ($dataTmp !== false)
//                $data = $dataTmp;
//        }
110
111

        return $data;
112
113
114
115
116
117
    }

    /**
     * Collect Global Variables
     *
     * @param    void
Carsten  Rose's avatar
Carsten Rose committed
118
     *
119
120
121
122
123
     * @return    array with global variables which might be replaced
     */
    public function collectGlobalVariables() {
        $arr = array();

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
        if (isset($_SERVER["REMOTE_ADDR"])) {
            //TODO: Variablen sollten vom STORE_TYPO3 genommen werden
            $arr["REMOTE_ADDR"] = $_SERVER["REMOTE_ADDR"];
            $arr["HTTP_HOST"] = $_SERVER["HTTP_HOST"];
            $arr["REQUEST_URI"] = $_SERVER["REQUEST_URI"];

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

141
142
143
144
145
146
147
148
149
        if (isset($GLOBALS["TSFE"]->fe_user)) {
            $arr["fe_user_uid"] = $GLOBALS["TSFE"]->fe_user->user["uid"] ?: '-';
            $arr["fe_user"] = $GLOBALS["TSFE"]->fe_user->user["username"] ?: '-';
        } else {
            $arr["fe_user_uid"] = '-';
            $arr["fe_user"] = '-';
        }

        $arr["be_user_uid"] = (isset($GLOBALS['BE_USER'])) ? $GLOBALS['BE_USER']->user["uid"] : '-';
150
        $arr["ttcontent_uid"] = $this->tt_content_uid;
151
152
153
154
155
        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;
        }
156
        // Add all variables from ext_localconf
157
        //		database aliases can be used in form sql queries (e.g. select * from "{{global.t3_name}}".fe_users...)
158
        //		localconf can be used to configure an application
159
        $tmp = array();
160
        if (isset($GLOBALS['TYPO3_CONF_VARS'][FORMREPORT])) {
161
            $this->linearizeArray($GLOBALS['TYPO3_CONF_VARS'][FORMREPORT], $tmp);
162
        }
163
164

        // remove anything that contains "_password" "_username" in the key to prevent the webmaster from doing something stupid
165
166
167
168
        foreach ($tmp as $key => $value) {
            if (!strstr($key, "_password") && !strstr($key, "_username"))
                $arr[$key] = $value;
        }
169
170
171
172

        // Add t3 db name too
//        require(PATH_typo3conf.'localconf.php');
        //TODO: $typo_db kommt von T3: PHPSTorm weiss das nicht und meckert hier. Es ist auch nicht gut das die globale Variable genommen wird. Umbauen auf etwas sinnvollerers!
173
        //TODO: DBAlias sinnvoll setzen
174
175
176
177
        $typo_db = '';
        $arr["t3_name"] = $typo_db;

        return ($arr);
Carsten  Rose's avatar
Carsten Rose committed
178
    }
179

Carsten  Rose's avatar
Carsten Rose committed
180
    /**
Carsten  Rose's avatar
Carsten Rose committed
181
182
     * @param        $arr
     * @param        $return
Carsten  Rose's avatar
Carsten Rose committed
183
184
185
     * @param string $keyPath
     */
    public function linearizeArray($arr, &$return, $keyPath = "") {
186
187
        if (is_array($arr)) {
            foreach ($arr as $key => $value) {
Carsten  Rose's avatar
Carsten Rose committed
188
                $this->linearizeArray($value, $return, $keyPath . "_" . $key);
189
190
            }
        } else {
Carsten  Rose's avatar
Carsten Rose committed
191
            $return[ltrim($keyPath, "_")] = $arr;
192
        }
Carsten  Rose's avatar
Carsten Rose committed
193
    }
194
}