Commit 1817fa1b authored by Carsten  Rose's avatar Carsten Rose
Browse files

Sip.php: SIP Store implemented.

parent f8a47719
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 1/9/16
* Time: 10:26 PM
*/
namespace qfq\store;
use qfq\exceptions\CodeException;
use qfq\exceptions\UserException;
//use qfq\helper;
use qfq\helper\OnArray;
require_once(__DIR__ . '/../../qfq/helper/OnArray.php');
require_once(__DIR__ . '/../../qfq/Constants.php');
require_once(__DIR__ . '/../../qfq/exceptions/CodeException.php');
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:
$parser = new \qfq\helper\KeyValueStringParser("=", "&");
$paramArray = $parser->parse($urlParam);
// 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, ..'
\qfq\helper\OnArray::sortKey($sipArray);
// 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
$parser = new \qfq\helper\KeyValueStringParser("=", "&");
return $parser->parse($_SESSION[$s]);
}
/**
* @param $urlParam
* @return mixed
*/
public function getSipFromUrlParam($urlParam) {
if (isset($_SESSION[$urlParam])) {
return $_SESSION[$urlParam];
}
return false;
}
}
\ No newline at end of file
<?php
require_once(__DIR__ . '/../../qfq/Constants.php');
require_once(__DIR__ . '/../../qfq/Form.php');
require_once(__DIR__ . '/../../qfq/store/Sip.php');
/**
* Created by PhpStorm.
* User: crose
* Date: 1/10/16
* Time: 10:55 PM
*/
class SipTest extends PHPUnit_Framework_TestCase {
public function testUrlparamToSip() {
$sip = new \qfq\store\Sip('fakesessionname', true);
$sip->sipUniqId('badcaffee1234');
$result = $sip->urlParamToSip("http://example.com/index.php?id=input&r=1&form=person", RETURN_URL);
$this->assertEquals('http://example.com/index.php?id=input&s=badcaffee1234', $result);
$result = $sip->urlParamToSip("http://example.com/index.php?id=input&r=1&form=person");
$this->assertEquals('http://example.com/index.php?id=input&s=badcaffee1234', $result);
$result = $sip->urlParamToSip("index.php?id=input&r=1&form=person", RETURN_URL);
$this->assertEquals('index.php?id=input&s=badcaffee1234', $result);
$result = $sip->urlParamToSip("?id=input&r=1&form=person", RETURN_URL);
$this->assertEquals('?id=input&s=badcaffee1234', $result);
$result = $sip->urlParamToSip("id=input&r=1&form=person", RETURN_URL);
$this->assertEquals('?id=input&s=badcaffee1234', $result);
$result = $sip->urlParamToSip("input&r=1&form=person", RETURN_URL);
$this->assertEquals('?id=input&s=badcaffee1234', $result);
$result = $sip->urlParamToSip("id=input&r=1&form=person&keySemId=23", RETURN_URL);
$this->assertEquals('?id=input&s=badcaffee1234', $result);
$result = $sip->urlParamToSip("id=input&r=1&form=person&type=99", RETURN_URL);
$this->assertEquals('?id=input&type=99&s=badcaffee1234', $result);
$result = $sip->urlParamToSip("id=input&r=1&form=person&L=1", RETURN_URL);
$this->assertEquals('?id=input&L=1&s=badcaffee1234', $result);
$result = $sip->urlParamToSip("id=input&r=1&L=2&form=person&type=99", RETURN_URL);
$this->assertEquals('?id=input&L=2&type=99&s=badcaffee1234', $result);
$result = $sip->urlParamToSip("id=input&r=1&L=2&form=person&type=99", RETURN_SIP);
$this->assertEquals('badcaffee1234', $result);
$sip->sipUniqId('badcaffee0000');
$result = $sip->urlParamToSip("id=input&r=10&L=2&form=person&type=99", RETURN_SIP);
$this->assertEquals('badcaffee0000', $result);
}
public function testGetVarsFromSip() {
$sip = new \qfq\store\Sip('fakesessionname', true);
$sip->sipUniqId('badcaffee1234');
$sip2 = $sip->urlParamToSip("http://example.com/index.php?a=1&b=2&c=3", RETURN_SIP);
$arr = $sip->getVarsFromSip($sip2);
$this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3], $arr);
$this->assertEquals('badcaffee1234', $sip2);
$sip2 = $sip->urlParamToSip("http://example.com/index.php?e=1&f=2&g=3", RETURN_SIP);
$arr = $sip->getVarsFromSip($sip2);
$this->assertEquals(['e' => 1, 'f' => 2, 'g' => 3], $arr);
$this->assertEquals('badcaffee1234', $sip2);
$sip->sipUniqId('badcaffee0000');
$sip2 = $sip->sipUniqId();
$this->assertEquals('badcaffee0000', $sip2);
$sip2 = $sip->urlParamToSip("http://example.com/index.php?aa=hello&bb=world", RETURN_SIP);
$arr = $sip->getVarsFromSip($sip2);
$this->assertEquals(['aa' => 'hello', 'bb' => 'world'], $arr);
$this->assertEquals('badcaffee0000', $sip2);
$sip2 = $sip->urlParamToSip("aaa=Don&bbb=John", RETURN_SIP);
$arr = $sip->getVarsFromSip($sip2);
$this->assertEquals(['aaa' => 'Don', 'bbb' => 'John'], $arr);
}
public function testGetSipFromUrlParam() {
$sip = new \qfq\store\Sip('fakesessionname', true);
$sip->sipUniqId('badcaffee1234');
$result = $sip->urlParamToSip("http://example.com/index.php?id=input&r=1&form=person", RETURN_URL);
$s = $sip->getSipFromUrlParam('form=person&r=1');
$this->assertEquals('badcaffee1234', $s);
$s = $sip->getSipFromUrlParam('UnknwonParameter=1234');
$this->assertFalse($s);
$sip->sipUniqId('badcaffee1111');
$url = $sip->urlParamToSip("a=1&b=2&c=3", RETURN_SIP);
$s = $sip->getSipFromUrlParam('a=1&b=2&c=3');
$this->assertEquals('badcaffee1111', $s);
}
public function testSipUniqId() {
$sip = new \qfq\store\Sip('fakesessionname', true);
$sip->sipUniqId('badcaffee1234');
$s = $sip->sipUniqId('badcaffee1234');
$this->assertEquals('badcaffee1234', $s);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment