Sip.php 7.56 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
12
use qfq\CodeException;
use qfq\UserException;
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

    private $phpUnit = null;
    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;
            @session_start();
        } else {
            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
58
     * @param string $queryString Possible variants:
59
60
61
62
63
     *   * 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
64
     * @param string $mode Possible values: RETURN_URL|RETURN_SIP
Carsten  Rose's avatar
Carsten Rose committed
65
     * @return string
66
67
     *  * mode=RETURN_URL: return complete URL
     *  * mofr=RETURN_SIP: returns only the sip
Carsten  Rose's avatar
Carsten Rose committed
68
69
70
     * @throws CodeException
     * @throws UserException
     */
71
    public function queryStringToSip($queryString, $mode = RETURN_URL, $scriptName = 'index.php') {
Carsten  Rose's avatar
Carsten Rose committed
72

Carsten  Rose's avatar
Carsten Rose committed
73
        // Validate: Check if still the same fe_user is logged in.
Carsten  Rose's avatar
Carsten Rose committed
74
75
76
77
78
79
80
81
82
83
        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:
84
        $paramArray = KeyValueStringParser::parse($queryString, "=", "&");
Carsten  Rose's avatar
Carsten Rose committed
85
86
87
88
89

        // 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, ..'
90
        OnArray::sortKey($sipArray);
Carsten  Rose's avatar
Carsten Rose committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

        // 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;

108
            if ($script[0] === '?')
109
                $script = $scriptName . $script;
110

Carsten  Rose's avatar
Carsten Rose committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
            return $script . OnArray::toString($clientArray);
        } else {
            return $s;
        }
    }

    /**
     * @param $param
     * @param $script
     * @param array $clientArray
     * @param array $sipArray
     * @throws CodeException
     * @throws UserException
     */
    private function splitParamClientSip($paramArray, array &$clientArray, array &$sipArray) {

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

    /**
164
     * Fix first parameter mix of hostname / script / Get parameter and optional missing keyname
Carsten  Rose's avatar
Carsten Rose committed
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
199
200
201
202
203
204
205
206
207
208
209
210
211
     *
     * @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;
    }

    /**
     * @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
     * @throws UserException
212
     * @throws \qfq\UserException
Carsten  Rose's avatar
Carsten Rose committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
     */
    public function getVarsFromSip($s) {

        # Check if parameter is manipulated
        if (strlen($s) != 13) {
            throw new UserException("Broken Parameter", ERROR_BROKEN_PARAMETER);
        }

        # 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"]) {
                throw new UserException("No user logged in or user changed - please reload the previous site.", ERROR_FE_USER_UID_CHANGED);
            }
        }

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

        // Decode parameter
234
        return KeyValueStringParser::parse($_SESSION[$s], "=", "&");
Carsten  Rose's avatar
Carsten Rose committed
235
236
237
    }

    /**
238
239
240
241
242
243
244
245
246
247
248
249
     * @param $queryString
     * @return mixed
     */
    public function getSipFromQueryString($queryString) {
        if (isset($_SESSION[$queryString])) {
            return $_SESSION[$queryString];
        }
        return false;
    }

    /**
     * @param $queryString
Carsten  Rose's avatar
Carsten Rose committed
250
251
     * @return mixed
     */
252
253
254
    public function getQueryStringFromSip($s) {
        if (isset($_SESSION[$s])) {
            return $_SESSION[$s];
Carsten  Rose's avatar
Carsten Rose committed
255
256
257
258
259
        }
        return false;
    }

}