Sip.php 7.98 KB
Newer Older
Carsten  Rose's avatar
Carsten Rose committed
1
2
3
4
5
6
7
8
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/9/16
 * Time: 10:26 PM
 */

9
namespace qfq;
Carsten  Rose's avatar
Carsten Rose committed
10

11
use qfq\CodeException;
12
use qfq\UserFormException;
13
14
use qfq\OnArray;
use qfq\KeyValueStringParser;
Carsten  Rose's avatar
Carsten Rose committed
15
16
17
18
19

require_once(__DIR__ . '/../../qfq/helper/OnArray.php');
require_once(__DIR__ . '/../../qfq/Constants.php');
require_once(__DIR__ . '/../../qfq/exceptions/CodeException.php');

Carsten  Rose's avatar
Carsten Rose committed
20
21
22
23
/**
 * Class Sip
 * @package qfq\store
 */
Carsten  Rose's avatar
Carsten Rose committed
24
25
class Sip {

Carsten  Rose's avatar
Carsten Rose committed
26
27
28
// $_SESSION['fe_user_uid'] = <fe_user_uid>
// $_SESSION[$sip] => <urlparam>  >> $_SESSION['badcaffee1234'] => 'form=Person&r=1&z=5678'
// $_SESSION[$urlparam] => <sip>  >> $_SESSION['form=Person&r=1&z=5678'] => 'badcaffee1234'
Carsten  Rose's avatar
Carsten Rose committed
29

30
    private $phpUnit = false;
Carsten  Rose's avatar
Carsten Rose committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    private $staticUniqId = false;

    function __construct($sessionname, $phpUnit = false) {

        $this->phpUnit = $phpUnit;

        if ($sessionname == "") {
            throw new CodeException('Missing "sessionname"', ERROR_MISSING_SESSIONNAME);
        }

        session_name();

        if ($phpUnit) {
            $_SESSION = null;
45
//            @session_start();
Carsten  Rose's avatar
Carsten Rose committed
46
        } else {
Carsten  Rose's avatar
Carsten Rose committed
47
48

//            session_name($sessionname);
Carsten  Rose's avatar
Carsten Rose committed
49
50
51
52
53
54
55
56
57
58
59
            session_start();
        }

        // Typo3: remember logged in FE User
        if (isset($GLOBALS["TSFE"]->fe_user->user["uid"]) && !isset($_SESSION[SESSION_FE_USER_UID])) {
            $_SESSION[SESSION_FE_USER_UID] = $GLOBALS["TSFE"]->fe_user->user["uid"];
        }

    }

    /**
Carsten  Rose's avatar
Carsten Rose committed
60
     * @param string $queryString Possible variants:
61
62
63
64
65
     *   * http://www.math.uzh.ch/index.php?a=1&s=4b3403665fea6&r=45&type=99&id=person
     *   * index.php?a=1&s=4b3403665fea6&r=45&type=99&id=person
     *   * ?a=1&s=4b3403665fea6&r=45&type=99&id=person
     *   * a=1&s=4b3403665fea6&r=45&type=99&id=person
     *
Carsten  Rose's avatar
Carsten Rose committed
66
     * @param string $mode Possible values: RETURN_URL|RETURN_SIP
Carsten  Rose's avatar
Carsten Rose committed
67
     * @return string
68
     *  * mode=RETURN_URL: return complete URL
Carsten  Rose's avatar
Carsten Rose committed
69
     *  * mode=RETURN_SIP: returns only the sip
Carsten  Rose's avatar
Carsten Rose committed
70
     * @throws CodeException
71
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
72
     */
73
    public function queryStringToSip($queryString, $mode = RETURN_URL, $scriptName = 'index.php') {
Carsten  Rose's avatar
Carsten Rose committed
74

Carsten  Rose's avatar
Carsten Rose committed
75
        // Validate: Check if still the same fe_user is logged in.
Carsten  Rose's avatar
Carsten Rose committed
76
77
78
79
80
81
82
83
84
85
        if (isset($GLOBALS["TSFE"]->fe_user->user["uid"])) {
            if ($_SESSION[SESSION_FE_USER_UID] != $GLOBALS["TSFE"]->fe_user->user["uid"]) {
                unset($_SESSION);
            }
        }

        $clientArray = array();
        $sipArray = array();

        // Split URL parameter:
86
        $paramArray = KeyValueStringParser::parse($queryString, "=", "&");
Carsten  Rose's avatar
Carsten Rose committed
87
88
89
90
91

        // Split parameter between Script, Client and SIP
        $script = $this->splitParamClientSip($paramArray, $clientArray, $sipArray);

        // sort array to guarantee identical respresentation in $_SESSION. Param 'a, r, b, ...' should be saved as 'a, b, r, ..'
92
        OnArray::sortKey($sipArray);
Carsten  Rose's avatar
Carsten Rose committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

        // Generate keyname for $_SESSION[]
        $sipParamString = OnArray::toString($sipArray);

        if (isset($_SESSION[$sipParamString])) {
            $s = $_SESSION[$sipParamString];
        } else {
            // Not found: create new entry
            $s = $this->sipUniqId();
            $_SESSION[$sipParamString] = $s;
            $_SESSION[$s] = $sipParamString;
        }

        if ($mode == RETURN_URL) {
            // Append SIP to final parameter
            $clientArray[CLIENT_SIP] = $s;

110
            if ($script[0] === '?')
111
                $script = $scriptName . $script;
112

Carsten  Rose's avatar
Carsten Rose committed
113
114
115
116
117
118
119
            return $script . OnArray::toString($clientArray);
        } else {
            return $s;
        }
    }

    /**
Carsten  Rose's avatar
Carsten Rose committed
120
121
122
     * Splits the $paramArray in &$clientArray and &$sipArray. $sipArray contains all key/values pairs wich are not belong to Typo3.
     *
     * @param array $paramArray
Carsten  Rose's avatar
Carsten Rose committed
123
124
     * @param array $clientArray
     * @param array $sipArray
Carsten  Rose's avatar
Carsten Rose committed
125
126
     * @return string
     * @throws \qfq\CodeException
Carsten  Rose's avatar
Carsten Rose committed
127
     */
Carsten  Rose's avatar
Carsten Rose committed
128
    private function splitParamClientSip(array $paramArray, array &$clientArray, array &$sipArray) {
Carsten  Rose's avatar
Carsten Rose committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

        $script = '';

        // Possible variants:
        //   http://www.math.uzh.ch/index.php?a=1&s=4b3403665fea6&r=45&type=99&id=person
        //   index.php?a=1&s=4b3403665fea6&r=45&type=99&id=person
        //   ?a=1&s=4b3403665fea6&r=45&type=99&id=person
        //   a=1&s=4b3403665fea6&r=45&type=99&id=person
        $flagFirst = true;
        foreach ($paramArray AS $key => $value) {

            // first key/value pair: a) save potential existing url/script part, b) if no keyname is specified, call it 'id'.
            if ($flagFirst) {
                $flagFirst = false;

                $script = $this->splitAndFix($key, $value);
            }

            // copy every parameter either to $clientArray or to $sipArray
            switch ($key) {
                //special T3 parameter.
                case 'L':
                case 'type':
                case 'id':
                    $clientArray[$key] = $value;
                    break;
                case CLIENT_SIP:
                    throw new CodeException('SIP Parameter ist not allowed to be stored as a regular URL Parameter', ERROR_SIP_NOT_ALLOWED_AS_PARAM);
                default:
                    $sipArray[$key] = $value;
                    break;
            }
        }

        return $script;
    }

    /**
167
     * Fix first parameter mix of hostname / script / Get parameter and optional missing keyname
Carsten  Rose's avatar
Carsten Rose committed
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
     *
     * @param $key
     * @param $value
     * @return string Part upto first '?',
     */
    private function splitAndFix(&$key, &$value) {

        $script = '';
        $tmpArray = explode('?', $key, 2);

        if (count($tmpArray) > 1) {
            $script = $tmpArray[0];
            $key = $tmpArray[1]; // new key, now without the hostname / scriptname.
        }
        $script .= '?';


        // empty value: keyname omitted - define keyname as 'id'
        if ($value === '') {
            $value = $key;
            $key = 'id';
        }

        return $script;
    }

    /**
Carsten  Rose's avatar
Carsten Rose committed
195
196
     * Returns a new uniqid, which will be used as a SIP identifier
     *
Carsten  Rose's avatar
Carsten Rose committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
     * @param bool|false $staticUniqId
     * @return bool|string
     */
    public function sipUniqId($staticUniqId = false) {
        if ($this->phpUnit) {
            if ($staticUniqId !== false) {
                $this->staticUniqId = $staticUniqId;
            }
            return $this->staticUniqId;
        }

        return uniqid();
    }

    /**
     * Retrieve Params stored in $_SESSION[$s]
     *
     * @param $s
     * @return array Parameter Array
216
217
     * @throws UserFormException
     * @throws \qfq\UserFormException
Carsten  Rose's avatar
Carsten Rose committed
218
219
220
221
222
     */
    public function getVarsFromSip($s) {

        # Check if parameter is manipulated
        if (strlen($s) != 13) {
223
            throw new UserFormException("Broken Parameter", ERROR_BROKEN_PARAMETER);
Carsten  Rose's avatar
Carsten Rose committed
224
225
226
227
228
        }

        # Check if still the same fe_user is logged in.
        if (isset($GLOBALS["TSFE"]->fe_user->user["uid"])) {
            if ($_SESSION[SESSION_FE_USER_UID] != $GLOBALS["TSFE"]->fe_user->user["uid"]) {
229
                throw new UserFormException("No user logged in or user changed - please reload the previous site.", ERROR_FE_USER_UID_CHANGED);
Carsten  Rose's avatar
Carsten Rose committed
230
231
232
233
234
            }
        }

        # Check if index 's' exists.
        if (!isset($_SESSION[$s])) {
235
            throw new UserFormException("SIP '$s' not registered - please reload the previous site and try again.", ERROR_SIP_NOT_FOUND);
Carsten  Rose's avatar
Carsten Rose committed
236
237
238
        }

        // Decode parameter
239
        return KeyValueStringParser::parse($_SESSION[$s], "=", "&");
Carsten  Rose's avatar
Carsten Rose committed
240
241
242
    }

    /**
243
244
     * Returns the sip for the given querystring. The querystring has to be sorted.
     *
245
246
247
248
249
250
251
252
253
254
255
     * @param $queryString
     * @return mixed
     */
    public function getSipFromQueryString($queryString) {
        if (isset($_SESSION[$queryString])) {
            return $_SESSION[$queryString];
        }
        return false;
    }

    /**
256
257
258
259
     * Returns the querystring for the given $sip
     *
     * @param $sip
     * @return bool
Carsten  Rose's avatar
Carsten Rose committed
260
     */
261
262
263
    public function getQueryStringFromSip($sip) {
        if (isset($_SESSION[$sip])) {
            return $_SESSION[$sip];
Carsten  Rose's avatar
Carsten Rose committed
264
265
266
267
268
        }
        return false;
    }

}