Commit ffb1215a authored by Carsten  Rose's avatar Carsten Rose
Browse files

Refs #7634. Parameter parsing.

parent aeaa4d4c
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 17.02.19
* Time: 15:40
*/
namespace qfq;
use qfq;
require_once(__DIR__ . '/../core/QuickFormQuery.php');
require_once(__DIR__ . '/../core/helper/OnString.php');
require_once(__DIR__ . '/../core/Constants.php');
require_once(__DIR__ . '/../core/exceptions/UserFormException.php');
require_once(__DIR__ . '/../core/exceptions/CodeException.php');
require_once(__DIR__ . '/../core/exceptions/DbException.php');
try {
$bodytext=OnString::extractFormRecordId($_SERVER['PATH_INFO']);
$qfq = new QuickFormQuery(['bodytext' => $bodytext]);
// $data = $qfq->rest();
} catch (qfq\CodeException $e) {
$answer[API_MESSAGE] = $e->formatMessage();
} catch (qfq\UserFormException $e) {
$answer[API_MESSAGE] = $e->formatMessage();
} catch (qfq\UserReportException $e) {
$answer[API_MESSAGE] = $e->formatMessage();
} catch (qfq\DbException $e) {
$answer[API_MESSAGE] = $e->formatMessage();
}
header("Content-Type: application/json");
echo json_encode($answer);
...@@ -425,6 +425,7 @@ const CLIENT_COOKIE_QFQ = 'cookieQfq'; ...@@ -425,6 +425,7 @@ const CLIENT_COOKIE_QFQ = 'cookieQfq';
// T3 Bodytext Keywords // T3 Bodytext Keywords
const TYPO3_FORM = CLIENT_FORM; const TYPO3_FORM = CLIENT_FORM;
const TYPO3_RECORD_ID = CLIENT_RECORD_ID; const TYPO3_RECORD_ID = CLIENT_RECORD_ID;
const TYPO3_REST_PATH = 'restPath';
const TYPO3_BE_USER_LOGGED_IN = 'beUserLoggedIn'; // 'yes' | 'no' const TYPO3_BE_USER_LOGGED_IN = 'beUserLoggedIn'; // 'yes' | 'no'
const TYPO3_BE_USER = 'beUser'; // 'yes' | 'no' const TYPO3_BE_USER = 'beUser'; // 'yes' | 'no'
const TYPO3_FE_USER = 'feUser'; const TYPO3_FE_USER = 'feUser';
......
...@@ -14,7 +14,8 @@ require_once(__DIR__ . '/../Constants.php'); ...@@ -14,7 +14,8 @@ require_once(__DIR__ . '/../Constants.php');
* Class OnString * Class OnString
* @package qfq * @package qfq
*/ */
class OnString { class OnString
{
/** /**
* Returns part of haystack string starting from and including the last occurrence of needle to the end of haystack. * Returns part of haystack string starting from and including the last occurrence of needle to the end of haystack.
...@@ -94,11 +95,11 @@ class OnString { ...@@ -94,11 +95,11 @@ class OnString {
* @param &$row - return the digit part of $pos * @param &$row - return the digit part of $pos
* @return bool - true if a alpha string and a numeric string is found, else false. * @return bool - true if a alpha string and a numeric string is found, else false.
*/ */
public static function splitExcelPos($pos, &$column, &$row){ public static function splitExcelPos($pos, &$column, &$row) {
preg_match_all('/[A-Z]+|\d+/', $pos, $matches); preg_match_all('/[A-Z]+|\d+/', $pos, $matches);
if(count($matches[0])!=2) { if (count($matches[0]) != 2) {
return false; return false;
} }
...@@ -164,7 +165,7 @@ class OnString { ...@@ -164,7 +165,7 @@ class OnString {
$nestingStart = 0; $nestingStart = 0;
// Process the string one start/end delimiter at a time // Process the string one start/end delimiter at a time
while(true) { while (true) {
// find the next start/end delimiter // find the next start/end delimiter
$nextDelimStartPos = strpos($str, $delimStart, $lastDelimPos + strlen($delimStart)); $nextDelimStartPos = strpos($str, $delimStart, $lastDelimPos + strlen($delimStart));
$nextDelimEndPos = strpos($str, $delimEnd, $lastDelimPos + strlen($delimEnd)); $nextDelimEndPos = strpos($str, $delimEnd, $lastDelimPos + strlen($delimEnd));
...@@ -183,7 +184,6 @@ class OnString { ...@@ -183,7 +184,6 @@ class OnString {
ERROR_MESSAGE_SUPPORT => "in '$str'"]), ERROR_MISSING_OPEN_DELIMITER); ERROR_MESSAGE_SUPPORT => "in '$str'"]), ERROR_MISSING_OPEN_DELIMITER);
break; break;
} elseif ($exprDepth == 0) { } elseif ($exprDepth == 0) {
// end of nesting -> replace \n inside nested expression with space // end of nesting -> replace \n inside nested expression with space
...@@ -199,11 +199,66 @@ class OnString { ...@@ -199,11 +199,66 @@ class OnString {
$lastDelimPos = $nextDelimPos; $lastDelimPos = $nextDelimPos;
} }
if ($exprDepth > 0 ) { if ($exprDepth > 0) {
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => "Missing close delimiter '$delimEnd'", throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => "Missing close delimiter '$delimEnd'",
ERROR_MESSAGE_SUPPORT => "in '$str'"]), ERROR_MISSING_CLOSE_DELIMITER); ERROR_MESSAGE_SUPPORT => "in '$str'"]), ERROR_MISSING_CLOSE_DELIMITER);
} }
return $str; return $str;
} }
/**
* Split a $_SERVER['PATH_INFO'] of the form '/form1/id1/form2/id2/form3/id3/...)' to
* r=id1
* form=form1
* restPath=form2/id2/form3/id3/...
*
* @param $pathInfo
* @return string
* @throws UserFormException
*/
public static function extractFormRecordId($pathInfo) {
$text = '';
if ($pathInfo == '') {
return '';
}
if ($pathInfo[0] == '/') {
$pathInfo = substr($pathInfo, 1);
}
$len = strlen($pathInfo);
if ($len > 0 && $pathInfo[$len - 1] == '/') {
$pathInfo = substr($pathInfo, 0, $len - 1);
}
if ($pathInfo == '') {
return '';
}
$param = explode('/', $pathInfo);
$cnt = count($param);
if ($cnt % 2 == 0) {
$id = array_pop($param);
if (!ctype_digit($id)) {
throw new UserFormException('Expect numerial id', ERROR_BROKEN_PARAMETER);
}
$text = TYPO3_RECORD_ID . '=' . $id . PHP_EOL;
}
$form = array_pop($param);
if (!ctype_alnum($form)) {
throw new UserFormException('Expect alphanumeric string', ERROR_BROKEN_PARAMETER);
}
$text .= TYPO3_FORM . '=' . $form . PHP_EOL;
if (count($param) > 0) {
$text .= TYPO3_REST_PATH . '=' . implode('/', $param) . PHP_EOL;
}
return $text;
}
} }
...@@ -14,7 +14,8 @@ use PHPUnit\Framework\TestCase; ...@@ -14,7 +14,8 @@ use PHPUnit\Framework\TestCase;
* Class OnStringTest * Class OnStringTest
* @package qfq * @package qfq
*/ */
class OnStringTest extends TestCase { class OnStringTest extends TestCase
{
/** /**
* *
...@@ -78,6 +79,48 @@ class OnStringTest extends TestCase { ...@@ -78,6 +79,48 @@ class OnStringTest extends TestCase {
OnString::removeNewlinesInNestedExpression("sqlInsert = {{SELECT *\nFROM test\nWHERE '{{var}}'='true'}}\nparam1=abc")); OnString::removeNewlinesInNestedExpression("sqlInsert = {{SELECT *\nFROM test\nWHERE '{{var}}'='true'}}\nparam1=abc"));
} }
/**
* @throws UserFormException
*/
public function testExtractFormRecordId() {
$this->assertEquals('', OnString::extractFormRecordId(''));
$this->assertEquals('', OnString::extractFormRecordId('/'));
$this->assertEquals('', OnString::extractFormRecordId('//'));
$this->assertEquals(TYPO3_FORM . '=path1' . PHP_EOL, OnString::extractFormRecordId('/path1'));
$this->assertEquals(TYPO3_FORM . '=path1' . PHP_EOL, OnString::extractFormRecordId('/path1/'));
$this->assertEquals(TYPO3_FORM . '=path1' . PHP_EOL, OnString::extractFormRecordId('path1/'));
$expected=TYPO3_RECORD_ID . '=12' . PHP_EOL . TYPO3_FORM . '=path1' . PHP_EOL;
$this->assertEquals($expected, OnString::extractFormRecordId('/path1/12'));
$expected=TYPO3_RECORD_ID . '=12' . PHP_EOL . TYPO3_FORM . '=path1' . PHP_EOL;
$this->assertEquals($expected, OnString::extractFormRecordId('/path1/12/'));
$expected=TYPO3_FORM . '=path2' . PHP_EOL . TYPO3_REST_PATH . '=path1/12'. PHP_EOL;
$this->assertEquals($expected, OnString::extractFormRecordId('/path1/12/path2'));
$expected=TYPO3_RECORD_ID . '=34' . PHP_EOL . TYPO3_FORM . '=path2' . PHP_EOL . TYPO3_REST_PATH . '=path1/12'. PHP_EOL;
$this->assertEquals($expected, OnString::extractFormRecordId('/path1/12/path2/34'));
}
/**
* @expectedException \qfq\UserFormException
*
*/
public function testExtractFormRecordId_1() {
# An alnum string is requested as path
$this->assertEquals('', OnString::extractFormRecordId('/%'));
}
/**
* @expectedException \qfq\UserFormException
*
*/
public function testExtractFormRecordId_2() {
# A numerical value is requested as id
$this->assertEquals('', OnString::extractFormRecordId('/path1/path2'));
}
/** /**
* @expectedException \qfq\UserFormException * @expectedException \qfq\UserFormException
* *
...@@ -85,6 +128,7 @@ class OnStringTest extends TestCase { ...@@ -85,6 +128,7 @@ class OnStringTest extends TestCase {
public function testRemoveNewlinesInNestedExpression_missingClosing_1() { public function testRemoveNewlinesInNestedExpression_missingClosing_1() {
$str = OnString::removeNewlinesInNestedExpression("Hi! {{Test "); $str = OnString::removeNewlinesInNestedExpression("Hi! {{Test ");
} }
/** /**
* @expectedException \qfq\UserFormException * @expectedException \qfq\UserFormException
* *
...@@ -92,6 +136,7 @@ class OnStringTest extends TestCase { ...@@ -92,6 +136,7 @@ class OnStringTest extends TestCase {
public function testRemoveNewlinesInNestedExpression_missingClosing_2() { public function testRemoveNewlinesInNestedExpression_missingClosing_2() {
$str = OnString::removeNewlinesInNestedExpression("Hi! {{Test}"); $str = OnString::removeNewlinesInNestedExpression("Hi! {{Test}");
} }
/** /**
* @expectedException \qfq\UserFormException * @expectedException \qfq\UserFormException
* *
...@@ -107,6 +152,7 @@ class OnStringTest extends TestCase { ...@@ -107,6 +152,7 @@ class OnStringTest extends TestCase {
public function testRemoveNewlinesInNestedExpression_missingOpening_1() { public function testRemoveNewlinesInNestedExpression_missingOpening_1() {
$str = OnString::removeNewlinesInNestedExpression("}}"); $str = OnString::removeNewlinesInNestedExpression("}}");
} }
/** /**
* @expectedException \qfq\UserFormException * @expectedException \qfq\UserFormException
* *
...@@ -114,6 +160,7 @@ class OnStringTest extends TestCase { ...@@ -114,6 +160,7 @@ class OnStringTest extends TestCase {
public function testRemoveNewlinesInNestedExpression_missingOpening_2() { public function testRemoveNewlinesInNestedExpression_missingOpening_2() {
$str = OnString::removeNewlinesInNestedExpression("{}}"); $str = OnString::removeNewlinesInNestedExpression("{}}");
} }
/** /**
* @expectedException \qfq\UserFormException * @expectedException \qfq\UserFormException
* *
......
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