chg: [internal] Attribute::onDemandEncrypt faster

pull/7823/head
Jakub Onderka 2021-10-09 18:45:51 +02:00
parent 9826847660
commit 4049c1e127
2 changed files with 22 additions and 48 deletions

View File

@ -445,9 +445,10 @@ class Attribute extends AppModel
$result = true;
// if the 'data' field is set on the $this->data then save the data to the correct file
if (isset($this->data['Attribute']['type']) && $this->typeIsAttachment($this->data['Attribute']['type'])) {
if (!empty($this->data['Attribute']['data_raw'])) {
if (isset($this->data['Attribute']['data_raw'])) {
$this->data['Attribute']['data'] = $this->data['Attribute']['data_raw'];
} elseif (!empty($this->data['Attribute']['data'])) {
unset($this->data['Attribute']['data_raw']);
} elseif (isset($this->data['Attribute']['data'])) {
$this->data['Attribute']['data'] = base64_decode($this->data['Attribute']['data']);
}
$result = $this->saveAttachment($this->data['Attribute']);
@ -3295,7 +3296,7 @@ class Attribute extends AppModel
}
}
$saveResult = true;
foreach ($attributes as $k => $attribute) {
foreach ($attributes as $attribute) {
if (!empty($attribute['encrypt']) && $attribute['encrypt']) {
$attribute = $this->onDemandEncrypt($attribute);
}
@ -3314,14 +3315,26 @@ class Attribute extends AppModel
return $saveResult;
}
public function onDemandEncrypt($attribute)
/**
* @param array $attribute
* @return array
*/
public function onDemandEncrypt(array $attribute)
{
if (strpos($attribute['value'], '|') !== false) {
$temp = explode('|', $attribute['value']);
$attribute['value'] = $temp[0];
}
$result = $this->handleMaliciousBase64($attribute['event_id'], $attribute['value'], $attribute['data'], array('md5'));
$attribute['data'] = $result['data'];
$content = base64_decode($attribute['data']);
if ($content === false) {
$this->log("Invalid attachment data provided for attribute with ID {$attribute['id']}.");
return $attribute;
}
$result = $this->handleMaliciousRaw($attribute['value'], $content, array('md5'));
$attribute['data_raw'] = $result['data_raw'];
unset($attribute['data']);
$attribute['value'] = $attribute['value'] . '|' . $result['md5'];
return $attribute;
}
@ -3637,9 +3650,7 @@ class Attribute extends AppModel
}
}
if (isset($attribute['encrypt'])) {
$result = $this->handleMaliciousBase64($eventId, $attribute['value'], $attribute['data'], array('md5'));
$attribute['data'] = $result['data'];
$attribute['value'] = $attribute['value'] . '|' . $result['md5'];
$attribute = $this->onDemandEncrypt($attribute);
}
$this->create();
if (!isset($attribute['distribution'])) {
@ -3710,9 +3721,7 @@ class Attribute extends AppModel
$attribute['event_id'] = $eventId;
$attribute['object_id'] = $objectId;
if (isset($attribute['encrypt'])) {
$result = $this->handleMaliciousBase64($eventId, $attribute['value'], $attribute['data'], array('md5'));
$attribute['data'] = $result['data'];
$attribute['value'] = $attribute['value'] . '|' . $result['md5'];
$attribute = $this->onDemandEncrypt($attribute);
}
unset($attribute['id']);
if (isset($attribute['uuid'])) {

View File

@ -8,45 +8,10 @@
*/
class TrimBehavior extends ModelBehavior
{
/**
*
* @param Model $Model
* @param unknown_type $settings
*/
public function setup(Model $Model, $settings = array())
{
if (!isset($this->settings[$Model->alias])) {
$this->settings[$Model->alias] = array(
'fields' => 'all',
);
}
$this->settings[$Model->alias] = array_merge(
$this->settings[$Model->alias],
(array)$settings
);
}
/**
*
* @param $options
*/
public function beforeValidate(Model $Model, $options = array())
{
$this->trimStringFields($Model);
return true;
}
/**
* Trim String Fields
*
* @param Model $Model
* @param unknown_type $array
*/
public function trimStringFields(Model $Model)
{
foreach ($Model->data[$Model->name] as $key => $field) {
if (is_string($field)) {
if ($key !== 'data' && $key !== 'data_raw' && is_string($field)) {
$Model->data[$Model->name][$key] = trim($field);
}
}