<commit_before><?php
class JSONConverterTool {

	public function generateTop() {
		return '{"response":[';
	}

	public function generateBottom() {
		return ']}' . PHP_EOL;
	}

	public function convert($event, $isSiteAdmin=false, $raw = false) {
		$toRearrange = array('Org', 'Orgc', 'SharingGroup', 'Attribute', 'ShadowAttribute', 'RelatedAttribute', 'RelatedEvent', 'Galaxy', 'Object');
		foreach ($toRearrange as $object) {
			if (isset($event[$object])) {
				$event['Event'][$object] = $event[$object];
				unset($event[$object]);
			}
			if ($object == 'SharingGroup' && isset($event['Event']['SharingGroup']) && empty($event['Event']['SharingGroup'])) {
				unset($event['Event']['SharingGroup']);
			}
			if ($object == 'Galaxy') {
				foreach ($event['Event']['Galaxy'] as $k => $galaxy) {
					foreach ($galaxy['GalaxyCluster'] as $k2 => $cluster){
						if (empty($cluster['meta'])) {
							$event['Event']['Galaxy'][$k]['GalaxyCluster'][$k2]['meta'] = new stdclass();
						}
					}
				}
			}
		}

		if (isset($event['EventTag'])) {
			foreach ($event['EventTag'] as $k => $tag) {
				unset($tag['Tag']['org_id']);
				$event['Event']['Tag'][$k] = $tag['Tag'];
			}
		}

		//
		// cleanup the array from things we do not want to expose
		//
		unset($event['Event']['user_id']);
		// hide the org field is we are not in showorg mode
		if (!Configure::read('MISP.showorg') && !$isSiteAdmin) {
			unset($event['Event']['org']);
			unset($event['Event']['orgc']);
			unset($event['Event']['from']);
		}

		if (isset($event['Event']['Attribute'])) {
			$event['Event']['Attribute'] = $this->__cleanAttributes($event['Event']['Attribute']);
			if (!empty($event['Sighting'])) {
				foreach ($event['Event']['Attribute'] as $ak => $attribute) {
					foreach ($event['Sighting'] as $as => $sighting) {
						if ($attribute['id'] == $sighting['attribute_id']) {
							$event['Event']['Attribute'][$ak]['Sighting'][] = $sighting;
						}
					}
				}
				unset($event['Sighting']);
			}
		}
		if (isset($event['Event']['Object'])) {
			$event['Event']['Object'] = $this->__cleanObjects($event['Event']['Object']);
		}

		unset($event['Event']['RelatedAttribute']);
		if (isset($event['Event']['RelatedEvent'])) {
			foreach ($event['Event']['RelatedEvent'] as $key => $value) {
				unset($event['Event']['RelatedEvent'][$key]['Event']['user_id']);
				if (!Configure::read('MISP.showorg') && !$isSiteAdmin) {
					unset($event['Event']['RelatedEvent'][$key]['Event']['org']);
					unset($event['Event']['RelatedEvent'][$key]['Event']['orgc']);
				}
			}
		}
		$result = array('Event' => $event['Event']);
		if (isset($event['errors'])) $result = array_merge($result, array('errors' => $event['errors']));
		if ($raw) return $result;
		return json_encode($result, JSON_PRETTY_PRINT);
	}

	private function __cleanAttributes($attributes) {
		// remove value1 and value2 from the output and remove invalid utf8 characters for the xml parser
		foreach ($attributes as $key => $value) {
			if (isset($value['SharingGroup']) && empty($value['SharingGroup'])) {<commit_msg>fix: Added sightings to object attributes in the JSON output, fixes #3007<commit_after><?php
class JSONConverterTool {

	public function generateTop() {
		return '{"response":[';
	}

	public function generateBottom() {
		return ']}' . PHP_EOL;
	}

	public function convert($event, $isSiteAdmin=false, $raw = false) {
		$toRearrange = array('Org', 'Orgc', 'SharingGroup', 'Attribute', 'ShadowAttribute', 'RelatedAttribute', 'RelatedEvent', 'Galaxy', 'Object');
		foreach ($toRearrange as $object) {
			if (isset($event[$object])) {
				$event['Event'][$object] = $event[$object];
				unset($event[$object]);
			}
			if ($object == 'SharingGroup' && isset($event['Event']['SharingGroup']) && empty($event['Event']['SharingGroup'])) {
				unset($event['Event']['SharingGroup']);
			}
			if ($object == 'Galaxy') {
				foreach ($event['Event']['Galaxy'] as $k => $galaxy) {
					foreach ($galaxy['GalaxyCluster'] as $k2 => $cluster){
						if (empty($cluster['meta'])) {
							$event['Event']['Galaxy'][$k]['GalaxyCluster'][$k2]['meta'] = new stdclass();
						}
					}
				}
			}
		}

		if (isset($event['EventTag'])) {
			foreach ($event['EventTag'] as $k => $tag) {
				unset($tag['Tag']['org_id']);
				$event['Event']['Tag'][$k] = $tag['Tag'];
			}
		}

		//
		// cleanup the array from things we do not want to expose
		//
		unset($event['Event']['user_id']);
		// hide the org field is we are not in showorg mode
		if (!Configure::read('MISP.showorg') && !$isSiteAdmin) {
			unset($event['Event']['org']);
			unset($event['Event']['orgc']);
			unset($event['Event']['from']);
		}

		if (isset($event['Event']['Attribute'])) {
			$event['Event']['Attribute'] = $this->__cleanAttributes($event['Event']['Attribute']);
			if (!empty($event['Sighting'])) {
				foreach ($event['Event']['Attribute'] as $ak => $attribute) {
					foreach ($event['Sighting'] as $as => $sighting) {
						if ($attribute['id'] == $sighting['attribute_id']) {
							$event['Event']['Attribute'][$ak]['Sighting'][] = $sighting;
						}
					}
				}
			}
		}
		if (isset($event['Event']['Object'])) {
			$event['Event']['Object'] = $this->__cleanObjects($event['Event']['Object']);
			if (!empty($event['Sighting'])) {
				foreach ($event['Event']['Object'] as $k => $object) {
					if (!empty($object['Attribute'])) {
						foreach ($object['Attribute'] as $ak => $attribute) {
							foreach ($event['Sighting'] as $as => $sighting) {
								if ($attribute['id'] == $sighting['attribute_id']) {
									$event['Event']['Object'][$k]['Attribute'][$ak]['Sighting'][] = $sighting;
								}
							}
						}
					}
				}
			}
		}
		if (!empty($event['Sighting'])) {
			unset($event['Sighting']);
		}

		unset($event['Event']['RelatedAttribute']);
		if (isset($event['Event']['RelatedEvent'])) {
			foreach ($event['Event']['RelatedEvent'] as $key => $value) {
				unset($event['Event']['RelatedEvent'][$key]['Event']['user_id']);
				if (!Configure::read('MISP.showorg') && !$isSiteAdmin) {
					unset($event['Event']['RelatedEvent'][$key]['Event']['org']);
					unset($event['Event']['RelatedEvent'][$key]['Event']['orgc']);
				}
			}
		}
		$result = array('Event' => $event['Event']);
		if (isset($event['errors'])) $result = array_merge($result, array('errors' => $event['errors']));
		if ($raw) return $result;
		return json_encode($result, JSON_PRETTY_PRINT);
	}

	private function __cleanAttributes($attributes) {
		// remove value1 and value2 from the output and remove invalid utf8 characters for the xml parser
		foreach ($attributes as $key => $value) {
			if (isset($value['SharingGroup']) && empty($value['SharingGroup'])) {
{'fraction_non_alphanumeric': 0.2396051450792701, 'fraction_numerical': 0.0020939276099311995, 'mean_word_length': 4.67175572519084, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4804927', 'n_tokens_mistral': 2561, 'n_tokens_neox': 2102, 'n_words': 405}