new: [internal] Streamlining of the push process

- rework of the internals
- cleaner separation of concerns into more specialised functions
pull/3551/head
iglocska 2018-08-03 14:44:22 +02:00
parent fe3fc13fbd
commit 547af5d071
1 changed files with 66 additions and 68 deletions

View File

@ -887,12 +887,9 @@ class Event extends AppModel
return $result;
}
if (strlen($newLocation)) { // HTTP/1.1 302 Found and Location: http://<newLocation>
$result = $this->restfulEventToServer($event, $server, $newLocation, $newLocation, $newTextBody, $HttpSocket);
if (is_numeric($result)) {
$error = $this->__resolveErrorCode($result, $event, $server);
if ($error) {
return $error . ' Error code: ' . $result;
}
$result = $this->__executeRestfulEventToServer($event, $server, $newLocation, $newLocation, $newTextBody, $HttpSocket);
if ($result !== true) {
return $result;
}
}
$uploadFailed = false;
@ -950,7 +947,6 @@ class Event extends AppModel
case '200': // 200 (OK) + entity-action-result
if ($response->isOk()) {
$newTextBody = $response->body();
$newLocation = null;
return true;
} else {
try {
@ -989,7 +985,7 @@ class Event extends AppModel
return $this->__handleRestfulEventToServerResponse($response, $newLocation, $newTextBody);
}
private function __updateEventForSync($event, $server)
private function __rearrangeEventStructureForSync($event)
{
// rearrange things to be compatible with the Xml::fromArray()
$objectsToRearrange = array('Attribute', 'Object', 'Orgc', 'SharingGroup', 'EventTag', 'Org', 'ShadowAttribute');
@ -999,21 +995,66 @@ class Event extends AppModel
unset($event[$o]);
}
}
// cleanup the array from things we do not want to expose
foreach (array('Org', 'org_id', 'orgc_id', 'proposal_email_lock', 'org', 'orgc') as $field) {
unset($event['Event'][$field]);
}
foreach ($event['Event']['EventTag'] as $kt => $tag) {
if (!$tag['Tag']['exportable']) {
unset($event['Event']['EventTag'][$kt]);
} else {
unset($tag['org_id']);
$event['Event']['Tag'][] = $tag['Tag'];
}
}
unset($event['Event']['EventTag']);
return $event;
}
// since we fetch the event and filter on tags after / server, we need to cull all of the non exportable tags
private function __removeNonExportableTags($data, $dataType)
{
if (!empty($data[$dataType . 'Tag'])) {
foreach ($data[$dataType . 'Tag'] as $k => $tag) {
if (!$tag['Tag']['exportable']) {
unset($data[$dataType . 'Tag'][$k]);
} else {
unset($tag['org_id']);
$data['Tag'][] = $tag['Tag'];
}
}
unset($data[$dataType . 'Tag']);
}
return $data;
}
private function __prepareAttributesForSync($data, $server) {
// prepare attribute for sync
if (!empty($data['Attribute'])) {
foreach ($data['Attribute'] as $key => $attribute) {
$data['Attribute'][$key] = $this->__updateAttributeForSync($attribute, $server);
if (empty($data['Attribute'][$key])) {
unset($data['Attribute'][$key]);
} else {
$data['Attribute'][$key] = $this->__removeNonExportableTags($data['Attribute'][$key], 'Attribute');
}
}
$data['Attribute'] = array_values($data['Attribute']);
}
return $data;
}
private function __prepareObjectsForSync($data, $server) {
// prepare Object for sync
if (!empty($data['Object'])) {
foreach ($data['Object'] as $key => $object) {
$data['Object'][$key] = $this->__updateObjectForSync($object, $server);
if (empty($data['Object'][$key])) {
unset($data['Object'][$key]);
} else {
$data['Object'][$key]['Attribute'] = $this->__prepareAttributesForSync($data['Object'][$key]['Attribute'], $server);
}
}
$data['Object'] = array_values($data['Object']);
}
return $data;
}
private function __updateEventForSync($event, $server)
{
$event = $this->__rearrangeEventStructureForSync($event);
$event['Event'] = $this->__removeNonExportableTags($event['Event'], 'Event');
// Add the local server to the list of instances in the SG
if (isset($event['Event']['SharingGroup']) && isset($event['Event']['SharingGroup']['SharingGroupServer'])) {
foreach ($event['Event']['SharingGroup']['SharingGroupServer'] as &$s) {
@ -1022,37 +1063,13 @@ class Event extends AppModel
}
}
}
// prepare attribute for sync
if (!empty($event['Event']['Attribute'])) {
foreach ($event['Event']['Attribute'] as $key => $attribute) {
$event['Event']['Attribute'][$key] = $this->__updateAttributeForSync($attribute, $server);
if (empty($event['Event']['Attribute'][$key])) {
unset($event['Event']['Attribute'][$key]);
}
}
}
// prepare Object for sync
if (!empty($event['Event']['Object'])) {
foreach ($event['Event']['Object'] as $key => $object) {
$event['Event']['Object'][$key] = $this->__updateObjectForSync($object, $server);
if (empty($event['Event']['Object'][$key])) {
unset($event['Event']['Object'][$key]);
}
}
}
$event['Event'] = $this->__prepareAttributesForSync($event['Event'], $server);
$event['Event'] = $this->__prepareObjectsForSync($event['Event'], $server);
// Downgrade the event from connected communities to community only
if (!$server['Server']['internal'] && $event['Event']['distribution'] == 2) {
$event['Event']['distribution'] = 1;
}
if (!empty($event['Event']['Attribute'])) {
$event['Event']['Attribute'] = array_values($event['Event']['Attribute']);
}
if (!empty($event['Event']['Object'])) {
$event['Event']['Object'] = array_values($event['Event']['Object']);
}
return $event;
}
@ -1079,14 +1096,6 @@ class Event extends AppModel
}
}
}
if (!empty($object['Attribute'])) {
foreach ($object['Attribute'] as $key => $attribute) {
$object['Attribute'][$key] = $this->__updateAttributeForSync($attribute, $server);
if (empty($object['Attribute'][$key])) {
unset($object['Attribute'][$key]);
}
}
}
return $object;
}
@ -1107,7 +1116,7 @@ class Event extends AppModel
return false;
}
// Add the local server to the list of instances in the SG
if (isset($attribute['SharingGroup']['SharingGroupServer'])) {
if (!empty($attribute['SharingGroup']['SharingGroupServer'])) {
foreach ($attribute['SharingGroup']['SharingGroupServer'] as &$s) {
if ($s['server_id'] == 0) {
$s['Server'] = array('id' => 0, 'url' => Configure::read('MISP.baseurl'));
@ -1115,29 +1124,18 @@ class Event extends AppModel
}
}
}
foreach ($attribute['AttributeTag'] as $kt => $tag) {
if (!$tag['Tag']['exportable']) {
unset($attribute['AttributeTag'][$kt]);
} else {
unset($tag['Tag']['org_id']);
$attribute['Tag'][] = $tag['Tag'];
}
}
unset($attribute['AttributeTag']);
// remove value1 and value2 from the output
unset($attribute['value1']);
unset($attribute['value2']);
// also add the encoded attachment
if ($this->Attribute->typeIsAttachment($attribute['type'])) {
$encodedFile = $this->Attribute->base64EncodeAttachment($attribute);
$attribute['data'] = $encodedFile;
$attribute['data'] = $this->Attribute->base64EncodeAttachment($attribute);
}
// Passing the attribute ID together with the attribute could cause the deletion of attributes after a publish/push
// Basically, if the attribute count differed between two instances, and the instance with the lower attribute
// count pushed, the old attributes with the same ID got overwritten. Unsetting the ID before pushing it
// solves the issue and a new attribute is always created.
unset($attribute['id']);
// remove value1 and value2 from the output
unset($attribute['value1']);
unset($attribute['value2']);
return $attribute;
}