Skip to content

Commit

Permalink
Fix form encryption when formEncrypt parameter is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
joeke committed Apr 26, 2017
1 parent 975d11f commit 98bf8d5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,22 @@
if ($mode === 'update') {
$newForm = $modx->getObject('FormItForm', array('hash' => $formHashKey));
}
if ($newForm === null) $newForm = $modx->newObject('FormItForm');
if ($newForm === null) {
$newForm = $modx->newObject('FormItForm');
}

// Array from which to populate form record
$newFormArray = array();

// Handle encryption
if($formEncrypt){
$encryptionType = 1;
if ($formEncrypt) {
$dataArray = $newForm->encrypt($modx->toJSON($dataArray));
// Only set encryption type if encryption is successful
if ($dataArray) {
// Set encryption type to 2 (openssl)
$encryptionType = 2;
}
} else {
$dataArray = $modx->toJSON($dataArray);
}
Expand All @@ -119,16 +130,14 @@
$formHashKey = ($formHashKeyRandom) ? $newForm->generatePseudoRandomHash() : pathinfo($formit->getStoreKey(), PATHINFO_BASENAME);
}

// Array from which to populate form record
$newFormArray = array();

// Special case: if updateSavedForm has the flag 'values' we only merge in
// the form values, not the other stuff
if ($mode === 'update' && $updateSavedForm === 'values') {
$newFormArray = $newForm->toArray();
$newFormArray = array_merge($newFormArray, array(
'values' => $dataArray,
));
'encryption_type' => $encryptionType,
));
} else {
// In all other cases, we overwrite the record completely!
// In create mode we must save the hash. In update mode, the
Expand All @@ -140,6 +149,7 @@
'ip' => $modx->getOption('REMOTE_ADDR', $_SERVER, ''),
'context_key' => $modx->resource->get('context_key'),
'encrypted' => $formEncrypt,
'encryption_type' => $encryptionType,
'hash' => $formHashKey,
);
}
Expand Down
13 changes: 8 additions & 5 deletions core/components/formit/processors/mgr/form/encrypt.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ public function prepareQueryBeforeCount(xPDOQuery $c)

public function prepareRow(xPDOObject $object)
{
$object->set('encrypted', 1);
$object->set('encryption_type', 2);
$values = $object->get('values');
$object->set('values', $object->encrypt($values));
$object->save();
/* only save when encrypt method returns a value */
$values = $object->encrypt($object->get('values'));
if ($values) {
$object->set('encrypted', 1);
$object->set('encryption_type', 2);
$object->set('values', $values);
$object->save();
}
$ff = $object->toArray();
return $ff;
}
Expand Down
8 changes: 4 additions & 4 deletions core/components/formit/processors/mgr/form/migrate.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public function process()
$oldValues = $form->get('values');
$oldValues = $form->decrypt($oldValues, 1);
$newValues = $form->encrypt($oldValues);

$this->modx->exec("UPDATE {$this->modx->getTableName('FormItForm')}
if ($newValues) {
$this->modx->exec("UPDATE {$this->modx->getTableName('FormItForm')}
SET {$this->modx->escape('encryption_type')} = {$this->modx->quote(2)},
{$this->modx->escape('values')} = {$this->modx->quote($newValues)}
WHERE {$this->modx->escape('id')} = {$this->modx->quote($form->get('id'))}");

$count++;
$count++;
}
}

if ($count === 0) {
Expand Down

0 comments on commit 98bf8d5

Please sign in to comment.