// $_SESSION[$sip] => >> $_SESSION['badcaffee1234'] => 'form=Person&r=1&z=5678' // $_SESSION[$urlparam] => >> $_SESSION['form=Person&r=1&z=5678'] => 'badcaffee1234' private $phpUnit = false; 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; // } else { // session_start(); // } $feUserUid = Session::get(SESSION_FE_USER_UID); // Typo3: remember logged in FE User if (isset($GLOBALS["TSFE"]->fe_user->user["uid"]) && $feUserUid === false) { Session::set(SESSION_FE_USER_UID, $GLOBALS["TSFE"]->fe_user->user["uid"]); } } /** * @param string $queryString 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 * * @param string $mode Possible values: RETURN_URL|RETURN_SIP * @return string/array * * mode=RETURN_URL: return complete URL * * mode=RETURN_SIP: returns only the sip * * mode=RETURN_ARRAY: returns array with url ('_url') and all decoded and created parameters. * @throws CodeException * @throws UserFormException */ public function queryStringToSip($queryString, $mode = RETURN_URL, $phpScriptName = INDEX_PHP) { // Validate: Check if still the same fe_user is logged in. $this->checkFeUserUid(); $clientArray = array(); $sipArray = array(); // Split URL parameter: $paramArray = KeyValueStringParser::parse($queryString, "=", "&"); // If no 'r' is specified: define r=0 if (!isset($paramArray[SIP_RECORD_ID])) { $paramArray[SIP_RECORD_ID] = 0; } // 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, ..' OnArray::sortKey($sipArray); // Generate keyname for $_SESSION[] $sipParamString = OnArray::toString($sipArray); $sessionParamSip = Session::get($sipParamString); if ($sessionParamSip !== false) { $s = $sessionParamSip; } else { // Not found: create new entry $s = $this->sipUniqId(); Session::set($sipParamString, $s); Session::set($s, $sipParamString); } // Append SIP to final parameter $clientArray[CLIENT_SIP] = $s; if ($script[0] === '?') $script = $phpScriptName . $script; $clientArray['_url'] = $script . OnArray::toString($clientArray); switch ($mode) { case RETURN_URL: $rc = $clientArray['_url']; break; case RETURN_SIP: $rc = $s; break; case RETURN_ARRAY: $rc = array_merge($clientArray, $sipArray); break; default: throw new CodeException('Unknown Mode: "' . $mode . '"', ERROR_UNKNOWN_MODE); } return $rc; } /** * */ private function checkFeUserUid() { // Validate: Check if still the same fe_user is logged in. if (isset($GLOBALS["TSFE"]->fe_user->user["uid"])) { $feUserUid = Session::get(SESSION_FE_USER_UID); if ($feUserUid !== false && $feUserUid != $GLOBALS["TSFE"]->fe_user->user["uid"]) { Session::clear(); } } } /** * Splits the $paramArray in &$clientArray and &$sipArray. $sipArray contains all key/values pairs wich are not belong to Typo3. * * @param array $paramArray * @param array $clientArray * @param array $sipArray * @return string * @throws \qfq\CodeException */ private function splitParamClientSip(array $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: // Values in SIP should not urlencoded. $sipArray[$key] = urldecode($value); break; } } return $script; } /** * 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; } /** * Returns a new uniqid, which will be used as a SIP identifier * * @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 UserFormException * @throws \qfq\UserFormException */ public function getVarsFromSip($s) { # Check if parameter is manipulated if (strlen($s) != 13) { throw new UserFormException("Broken Parameter", ERROR_BROKEN_PARAMETER); } // Validate: Check if still the same fe_user is logged in. $this->checkFeUserUid(); # Check if index 's' exists. $sessionVar = Session::get($s); if ($sessionVar === false) { throw new UserFormException("SIP '$s' not registered - please reload the previous site and try again.", ERROR_SIP_NOT_FOUND); } // Decode parameter return KeyValueStringParser::parse($sessionVar, "=", "&"); } /** * Returns the sip for the given querystring. The querystring has to be sorted. * * @param $queryString * @return mixed */ public function getSipFromQueryString($queryString) { return Session::get($queryString); } /** * Returns the querystring for the given $sip * * @param $sip * @return bool */ public function getQueryStringFromSip($sip) { return Session::get($sip); } }