2019-08-29 09:24:33 +02:00
|
|
|
<?php
|
|
|
|
App::uses('AppModel', 'Model');
|
|
|
|
class Community extends AppModel
|
|
|
|
{
|
|
|
|
public $useTable = false;
|
|
|
|
|
2022-09-23 17:37:32 +02:00
|
|
|
/**
|
|
|
|
* @param string $context
|
|
|
|
* @param string|null $value
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-08-29 09:24:33 +02:00
|
|
|
public function getCommunityList($context, $value)
|
|
|
|
{
|
|
|
|
try {
|
2022-09-23 17:37:32 +02:00
|
|
|
$community_list = FileAccessTool::readJsonFromFile(APP . 'files/community-metadata/defaults.json');
|
2019-08-29 09:24:33 +02:00
|
|
|
} catch (Exception $e) {
|
|
|
|
throw new NotFoundException(__('Default community list not in the expected format.'));
|
|
|
|
}
|
2022-09-23 17:37:32 +02:00
|
|
|
|
|
|
|
$fieldsToCheck = ['name', 'uuid', 'description', 'url', 'sector', 'nationality', 'type', 'org_uuid', 'org_name'];
|
2019-08-29 09:24:33 +02:00
|
|
|
foreach ($community_list as $k => $v) {
|
|
|
|
if ($v['misp_project_vetted'] === ($context === 'vetted')) {
|
|
|
|
$community_list[$k]['id'] = $k + 1;
|
|
|
|
$community_list[$k]['Org'] = array('uuid' => $v['org_uuid'], 'name' => $v['org_name']);
|
|
|
|
} else {
|
|
|
|
unset($community_list[$k]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!empty($value)) {
|
2022-09-23 17:37:32 +02:00
|
|
|
$value = mb_strtolower($value);
|
2019-08-29 09:24:33 +02:00
|
|
|
$found = false;
|
|
|
|
foreach ($fieldsToCheck as $field) {
|
2022-09-23 17:37:32 +02:00
|
|
|
if (strpos(mb_strtolower($v[$field]), $value) !== false) {
|
2019-08-29 09:24:33 +02:00
|
|
|
$found = true;
|
2022-09-23 17:37:32 +02:00
|
|
|
break;
|
2019-08-29 09:24:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$found) {
|
|
|
|
unset($community_list[$k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-23 17:37:32 +02:00
|
|
|
return array_values($community_list);
|
2019-08-29 09:24:33 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 17:37:32 +02:00
|
|
|
/**
|
|
|
|
* @param int|string $id Community ID or UUID
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-08-29 09:24:33 +02:00
|
|
|
public function getCommunity($id)
|
|
|
|
{
|
|
|
|
try {
|
2022-09-23 17:37:32 +02:00
|
|
|
$community_list = FileAccessTool::readJsonFromFile(APP . 'files/community-metadata/defaults.json');
|
2019-08-29 09:24:33 +02:00
|
|
|
} catch (Exception $e) {
|
|
|
|
throw new NotFoundException(__('Default community list not in the expected format.'));
|
|
|
|
}
|
2022-09-23 17:37:32 +02:00
|
|
|
|
2019-08-29 09:24:33 +02:00
|
|
|
foreach ($community_list as $k => $v) {
|
|
|
|
$community_list[$k]['id'] = $k + 1;
|
|
|
|
$community_list[$k]['Org'] = array('uuid' => $v['org_uuid'], 'name' => $v['org_name']);
|
|
|
|
}
|
2022-09-23 17:37:32 +02:00
|
|
|
|
|
|
|
$lookupField = Validation::uuid($id) ? 'uuid' : 'id';
|
2019-08-29 09:24:33 +02:00
|
|
|
foreach ($community_list as $s) {
|
2022-09-23 17:37:32 +02:00
|
|
|
if ($s[$lookupField === 'uuid' ? 'uuid' : 'id'] == $id) {
|
|
|
|
return $s;
|
2019-08-29 09:24:33 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-23 17:37:32 +02:00
|
|
|
throw new NotFoundException(__('Community not found.'));
|
2019-08-29 09:24:33 +02:00
|
|
|
}
|
|
|
|
}
|