Sip.php 6.7 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\exceptions\CodeException;
use qfq\exceptions\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
26
27
28
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class Sip {

//    $_SESSION['fe_user_uid'] = <fe_user_uid>
//    $_SESSION[<sip>] = <urlparam>
//    $_SESSION[<urlparam>] = <sip>

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

    }

    /**
     * @param $urlParam
     * @return string
     * @throws CodeException
     * @throws UserException
     */
    public function urlParamToSip($urlParam, $mode = RETURN_URL) {

        // Validation: 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"]) {
                unset($_SESSION);
            }
        }

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

        // Split URL parameter:
76
        $paramArray = KeyValueStringParser::parse($urlParam, "=", "&");
Carsten  Rose's avatar
Carsten Rose committed
77
78
79
80
81

        // 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, ..'
82
        OnArray::sortKey($sipArray);
Carsten  Rose's avatar
Carsten Rose committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
164
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
212
213
214
215
216
217
218
219
220
221
222

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

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

    /**
     * Special fucntion to fix first parameter mix of hostname / script / Get parameter and optional missing keyname
     *
     * @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
     * @throws \qfq\exceptions\UserException
     */
    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])) {
            throw new UserException("SIP '$s' not registered - please reload the previous site.", ERROR_SIP_NOT_FOUND);
        }

        // Decode parameter
223
        return KeyValueStringParser::parse($_SESSION[$s], "=", "&");
Carsten  Rose's avatar
Carsten Rose committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
    }

    /**
     * @param $urlParam
     * @return mixed
     */
    public function getSipFromUrlParam($urlParam) {
        if (isset($_SESSION[$urlParam])) {
            return $_SESSION[$urlParam];
        }
        return false;
    }

}