Skip to content

Commit

Permalink
Initial commit after extracting from pr neos/neos-development-collect…
Browse files Browse the repository at this point in the history
…ion#2514 for the neos development distribution.
  • Loading branch information
mficzel committed Jul 5, 2019
0 parents commit dc56982
Show file tree
Hide file tree
Showing 18 changed files with 650 additions and 0 deletions.
72 changes: 72 additions & 0 deletions Classes/Domain/Model/FieldDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
namespace Neos\Fusion\Form\Domain\Model;

/*
* This file is part of the Neos.Fusion.Form package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\Error\Messages\Result;

class FieldDefinition
{

/**
* @var string|null
*/
protected $name;

/**
* @var string|null
*/
protected $value;

/**
* @var bool
*/
protected $validationResult;

/**
* FieldDefinition constructor.
*
* @param string|null $name
* @param string|array|null $value
* @param bool $multiple
*/
public function __construct(string $name = null, $value = null, Result $validationResult = null)
{
$this->name = $name;
$this->value = $value;
$this->validationResult = $validationResult;
}

/**
* @return string|null
*/
public function getName(): ?string
{
return $this->name;
}

/**
* @return string|array|null
*/
public function getValue()
{
return $this->value;
}

/**
* @return Result|null
*/
public function getValidationResult(): ?Result
{
return $this->validationResult;
}

}
104 changes: 104 additions & 0 deletions Classes/Domain/Model/FormDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
declare(strict_types=1);

namespace Neos\Fusion\Form\Domain\Model;

/*
* This file is part of the Neos.Fusion.Form package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\Error\Messages\Result;

/**
* Used to output an HTML <form> tag which is targeted at the specified action, in the current controller and package.
*/
class FormDefinition
{
/**
* @var string
*/
protected $name;

/**
* @var mixed
*/
protected $object;

/**
* @var string
*/
protected $fieldNamePrefix;

/**
* @var array
*/
protected $submittedValues;

/**
* @var Result
*/
protected $mappingResults;

/**
* FormDefinition constructor.
* @param string|null $name
* @param object|null $object
* @param string|null $fieldNamePrefix
* @param array|null $submittedValues
* @param Result|null $mappingResults
*/
public function __construct(string $name = null,object $object = null, string $fieldNamePrefix = null, array$submittedValues = null, Result $mappingResults = null)
{
$this->name = $name;
$this->object = $object;
$this->fieldNamePrefix = $fieldNamePrefix;
$this->submittedValues = $submittedValues;
$this->mappingResults = $mappingResults;
}

/**
* @return string
*/
public function getName(): ?string
{
return $this->name;
}

/**
* @return string
*/
public function getFieldNamePrefix(): ?string
{
return $this->fieldNamePrefix;
}

/**
* @return mixed
*/
public function getObject(): ?object
{
return $this->object;
}

/**
* @return array
*/
public function getSubmittedValues(): ?array
{
return $this->submittedValues;
}

/**
* @return Result
*/
public function getMappingResults(): ?Result
{
return $this->mappingResults;
}
}
121 changes: 121 additions & 0 deletions Classes/Eel/FormHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
declare(strict_types=1);

namespace Neos\Fusion\Form\Eel;

/*
* This file is part of the Neos.Fusion.Form package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\Flow\Annotations as Flow;
use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Security\Context as SecurityContext;
use Neos\Flow\Security\Cryptography\HashService;
use Neos\Flow\Mvc\Controller\MvcPropertyMappingConfigurationService;

class FormHelper implements ProtectedContextAwareInterface
{
/**
* @Flow\Inject
* @var SecurityContext
*/
protected $securityContext;

/**
* @Flow\Inject
* @var MvcPropertyMappingConfigurationService
*/
protected $mvcPropertyMappingConfigurationService;

/**
* @Flow\Inject
* @var HashService
*/
protected $hashService;

/**
* Calculate the trusted properties token for the given form content
*
* @param array $arguments
* @param string|null $fieldNamePrefix
*/
public function argumentsWithHmac(array $arguments = [], string $excludeNamespace = '')
{
if ($excludeNamespace !== null && isset($arguments[$excludeNamespace])) {
unset($arguments[$excludeNamespace]);
}
return $this->hashService->appendHmac(base64_encode(serialize($arguments)));
}

/**
* Calculate the trusted properties token for the given form content
*
* @param string $content
* @param string|null $fieldNamePrefix
*/
public function trustedPropertiesToken(string $content, string $fieldNamePrefix = '')
{
$domDocument = new \DOMDocument('1.0', 'UTF-8');
// ignore parsing errors
$useInternalErrorsBackup = libxml_use_internal_errors(true);
$domDocument->loadHTML($content);
$xpath = new \DOMXPath($domDocument);
if ($useInternalErrorsBackup !== true) {
libxml_use_internal_errors($useInternalErrorsBackup);
}

$elements = $xpath->query("//*[@name]");
$formFieldNames = [];
foreach($elements as $element) {
$formFieldNames[] = (string)$element->getAttribute('name');
}
return $this->mvcPropertyMappingConfigurationService->generateTrustedPropertiesToken($formFieldNames, $fieldNamePrefix);
}

/**
* Returns CSRF token which is required for "unsafe" requests (e.g. POST, PUT, DELETE, ...)
*
* @return string
*/
public function csrfToken(): string
{
return $this->securityContext->getCsrfProtectionToken();
}

/**
* Prepend the gigen fieldNamePrefix to the fieldName the
*
* @param string $name
* @param string|null $prefix
* @return string
*/
public function prefixFieldName(string $fieldName, string $fieldNamePrefix = null)
{
if (!$fieldNamePrefix) {
return $fieldName;
} else {
$fieldNameSegments = explode('[', $fieldName, 2);
$fieldName = $fieldNamePrefix . '[' . $fieldNameSegments[0] . ']';
if (count($fieldNameSegments) > 1) {
$fieldName .= '[' . $fieldNameSegments[1];
}
return $fieldName;
}
}

/**
* @param string $methodName
* @return bool
*/
public function allowsCallOfMethod($methodName)
{
return true;
}

}
Loading

0 comments on commit dc56982

Please sign in to comment.