Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RTM] Allow to disable input encoding for a whole dca #708

Merged
merged 5 commits into from
Apr 21, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Resources/contao/classes/DataContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ protected function row($strPalette=null)
$this->varValue = \StringUtil::insertTagToSrc($this->varValue);
}

// Use raw request if set globally
if (isset($GLOBALS['TL_DCA'][$this->strTable]['config']['useRawRequestData']) && $GLOBALS['TL_DCA'][$this->strTable]['config']['useRawRequestData'] === true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a !isset($arrData['eval']['useRawRequestData']) check here too? This would make it possible to use the raw request for the whole DCA but opt-out of it for single fields.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, added in fd2ef3f.

{
$arrData['eval']['useRawRequestData'] = true;
}

/** @var Widget $objWidget */
$objWidget = new $strClass($strClass::getAttributesFromDca($arrData, $this->strInputName, $this->varValue, $this->strField, $this->strTable, $this));

Expand Down
8 changes: 6 additions & 2 deletions src/Resources/contao/controllers/BackendPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Contao;

use Patchwork\Utf8;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;


Expand Down Expand Up @@ -50,13 +51,16 @@ public function __construct()
*/
public function run()
{
/** @var Request $request */
$request = System::getContainer()->get('request_stack')->getCurrentRequest();

/** @var BackendTemplate|object $objTemplate */
$objTemplate = new \BackendTemplate('be_password');

if (\Input::post('FORM_SUBMIT') == 'tl_password')
{
$pw = \Input::postUnsafeRaw('password');
$cnf = \Input::postUnsafeRaw('confirm');
$pw = $request->request->get('password');
$cnf = $request->request->get('confirm');

// The passwords do not match
if ($pw != $cnf)
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/contao/forms/FormPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ class FormPassword extends \Widget


/**
* Always decode entities
* Always use raw request data.
*
* @param array $arrAttributes An optional attributes array
*/
public function __construct($arrAttributes=null)
{
parent::__construct($arrAttributes);
$this->decodeEntities = true;
$this->useRawRequestData = true;
}


Expand Down
13 changes: 8 additions & 5 deletions src/Resources/contao/library/Contao/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Contao;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategy;


Expand Down Expand Up @@ -320,6 +321,8 @@ public function authenticate()
*/
public function login()
{
/** @var Request $request */
$request = System::getContainer()->get('request_stack')->getCurrentRequest();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a behavior change, shouldn't we use getMasterRequest?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's almost always wrong to take the master request. The use cases are only very, very limited. So no: the current request is the way to go.

\System::loadLanguageFile('default');

// Do not continue if username or password are missing
Expand All @@ -339,7 +342,7 @@ public function login()
foreach ($GLOBALS['TL_HOOKS']['importUser'] as $callback)
{
$this->import($callback[0], 'objImport', true);
$blnLoaded = $this->objImport->{$callback[1]}(\Input::post('username', true), \Input::postUnsafeRaw('password'), $this->strTable);
$blnLoaded = $this->objImport->{$callback[1]}(\Input::post('username', true), $request->request->get('password'), $this->strTable);

// Load successfull
if ($blnLoaded === true)
Expand Down Expand Up @@ -399,17 +402,17 @@ public function login()
// The password has been generated with crypt()
if (\Encryption::test($this->password))
{
$blnAuthenticated = \Encryption::verify(\Input::postUnsafeRaw('password'), $this->password);
$blnAuthenticated = \Encryption::verify($request->request->get('password'), $this->password);
}
else
{
list($strPassword, $strSalt) = explode(':', $this->password);
$blnAuthenticated = ($strSalt == '') ? ($strPassword === sha1(\Input::postUnsafeRaw('password'))) : ($strPassword === sha1($strSalt . \Input::postUnsafeRaw('password')));
$blnAuthenticated = ($strSalt == '') ? ($strPassword === sha1($request->request->get('password'))) : ($strPassword === sha1($strSalt . $request->request->get('password')));

// Store a SHA-512 encrpyted version of the password
if ($blnAuthenticated)
{
$this->password = \Encryption::hash(\Input::postUnsafeRaw('password'));
$this->password = \Encryption::hash($request->request->get('password'));
}
}

Expand All @@ -419,7 +422,7 @@ public function login()
foreach ($GLOBALS['TL_HOOKS']['checkCredentials'] as $callback)
{
$this->import($callback[0], 'objAuth', true);
$blnAuthenticated = $this->objAuth->{$callback[1]}(\Input::post('username', true), \Input::postUnsafeRaw('password'), $this);
$blnAuthenticated = $this->objAuth->{$callback[1]}(\Input::post('username', true), $request->request->get('password'), $this);

// Authentication successfull
if ($blnAuthenticated === true)
Expand Down
18 changes: 18 additions & 0 deletions src/Resources/contao/library/Contao/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Doctrine\DBAL\Types\Type;
use Patchwork\Utf8;
use Symfony\Component\HttpFoundation\Request;


/**
Expand Down Expand Up @@ -83,6 +84,7 @@
* @property string $slabel The submit button label
* @property boolean $preserveTags Preserve HTML tags
* @property boolean $decodeEntities Decode HTML entities
* @property boolean useRawRequestData Use the raw request data from the Symfony request
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add useRawRequestData to the switch-case on line 345 too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jop! Added in 31732f9.

* @property integer $minlength The minimum length
* @property integer $maxlength The maximum length
* @property integer $minval The minimum value
Expand Down Expand Up @@ -263,6 +265,22 @@ public function __set($strKey, $varValue)
$this->strPrefix = $varValue;
break;

case 'useRawRequestData':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you have declared the property as a boolean, we should handle the false case as well, shouldn't we?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah not all attributes do unfortunately. So it's inconsistent already anyway but I added it.

if ($varValue === true)
{
/** @var Request $request */
$request = \System::getContainer()->get('request_stack')->getCurrentRequest();
$this->setInputCallback(function() use ($request) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could (but don't have to) simplify this:

$this->setInputCallback(function () {
    return \System::getContainer()->get('request_stack')->getCurrentRequest()->request->get($this->name);
});

return $request->request->get($this->name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like Widget::getPost() supports array syntax like field[foo][bar], do we need this functionality here too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. If you want the raw request data you need to handle the array yourself if you need to, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea where this array syntax is needed, maybe @aschempp can help us here? See Widget.php:781

});
}
else
{
$this->setInputCallback(null);
}

break;

case 'template':
$this->strTemplate = $varValue;
break;
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/contao/widgets/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ class Password extends \Widget


/**
* Always decode entities
* Always use raw request data.
*
* @param array $arrAttributes
*/
public function __construct($arrAttributes=null)
{
parent::__construct($arrAttributes);
$this->decodeEntities = true;
$this->useRawRequestData = true;
}


Expand Down