2019-03-08 16:19:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
App::uses('AppModel', 'Model');
|
|
|
|
|
|
|
|
class DecayingModel extends AppModel
|
|
|
|
{
|
|
|
|
public $actsAs = array('Containable');
|
|
|
|
|
|
|
|
public $hasMany = array(
|
2019-04-03 10:05:45 +02:00
|
|
|
'DecayingModelMapping' => array(
|
|
|
|
'className' => 'DecayingModelMapping',
|
|
|
|
'foreignKey' => 'model_id',
|
|
|
|
'dependent' => true
|
|
|
|
)
|
2019-03-08 16:19:07 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
public function afterFind($results, $primary = false) {
|
|
|
|
foreach ($results as $k => $v) {
|
|
|
|
if (!empty($v['DecayingModel']['parameters'])) {
|
|
|
|
$decoded = json_decode($v['DecayingModel']['parameters'], true);
|
|
|
|
if ($decoded === null) {
|
|
|
|
$decoded = array();
|
|
|
|
}
|
|
|
|
$results[$k]['DecayingModel']['parameters'] = $decoded;
|
|
|
|
}
|
2019-04-03 11:51:29 +02:00
|
|
|
if (!empty($v['DecayingModel']['attribute_types'])) {
|
|
|
|
$decoded = json_decode($v['DecayingModel']['attribute_types'], true);
|
|
|
|
if ($decoded === null) {
|
|
|
|
$decoded = array();
|
|
|
|
}
|
|
|
|
$results[$k]['DecayingModel']['attribute_types'] = $decoded;
|
2019-04-04 09:37:51 +02:00
|
|
|
} else {
|
|
|
|
$results[$k]['DecayingModel']['attribute_types'] = array();
|
2019-04-03 11:51:29 +02:00
|
|
|
}
|
|
|
|
if (!empty($v['DecayingModel']['ref'])) {
|
|
|
|
$decoded = json_decode($v['DecayingModel']['ref'], true);
|
|
|
|
if ($decoded === null) {
|
|
|
|
$decoded = array();
|
|
|
|
}
|
|
|
|
$results[$k]['DecayingModel']['ref'] = $decoded;
|
|
|
|
}
|
2019-03-08 16:19:07 +01:00
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function beforeValidate($options = array()) {
|
2019-04-03 10:05:45 +02:00
|
|
|
parent::beforeValidate();
|
|
|
|
if (!empty($this->data['DecayingModel']['parameters']) && !is_array($this->data['DecayingModel']['parameters'])) {
|
2019-03-08 16:19:07 +01:00
|
|
|
$encoded = json_decode($this->data['DecayingModel']['parameters'], true);
|
|
|
|
if ($encoded !== null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-03 11:51:29 +02:00
|
|
|
if (!empty($this->data['DecayingModel']['attribute_types']) && !is_array($this->data['DecayingModel']['attribute_types'])) {
|
|
|
|
$encoded = json_decode($this->data['DecayingModel']['attribute_types'], true);
|
|
|
|
if ($encoded !== null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-08 16:19:07 +01:00
|
|
|
}
|
|
|
|
|
2019-04-03 10:05:45 +02:00
|
|
|
public function beforeSave($options = array()) {
|
|
|
|
if (isset($this->data['DecayingModel']['parameters']) && is_array($this->data['DecayingModel']['parameters'])) {
|
|
|
|
$this->data['DecayingModel']['parameters'] = json_encode($this->data['DecayingModel']['parameters']);
|
|
|
|
}
|
2019-04-03 11:51:29 +02:00
|
|
|
if (isset($this->data['DecayingModel']['attribute_types']) && is_array($this->data['DecayingModel']['attribute_types'])) {
|
|
|
|
$this->data['DecayingModel']['attribute_types'] = json_encode($this->data['DecayingModel']['attribute_types']);
|
|
|
|
}
|
|
|
|
if (isset($this->data['DecayingModel']['ref']) && is_array($this->data['DecayingModel']['ref'])) {
|
|
|
|
$this->data['DecayingModel']['ref'] = json_encode($this->data['DecayingModel']['ref']);
|
|
|
|
}
|
2019-04-03 10:05:45 +02:00
|
|
|
if (!isset($this->data['DecayingModel']['org_id'])) {
|
|
|
|
$this->data['DecayingModel']['org_id'] = Configure::read('MISP.host_org_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function __load_models($force = false)
|
|
|
|
{
|
|
|
|
$dir = new Folder(APP . 'files' . DS . 'misp-decaying-models' . DS . 'models');
|
|
|
|
$files = $dir->find('.*\.json');
|
|
|
|
$models = array();
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$file = new File($dir->pwd() . DS . $file);
|
|
|
|
$models[] = json_decode($file->read(), true);
|
|
|
|
$file->close();
|
|
|
|
}
|
|
|
|
return $models;
|
|
|
|
}
|
|
|
|
|
2019-04-03 11:51:29 +02:00
|
|
|
public function update($force=false)
|
2019-04-03 10:05:45 +02:00
|
|
|
{
|
|
|
|
$new_models = $this->__load_models($force);
|
|
|
|
$temp = $this->find('all', array(
|
|
|
|
'recursive' => -1
|
|
|
|
));
|
|
|
|
$existing_models = array();
|
|
|
|
foreach ($temp as $k => $model) {
|
|
|
|
$existing_models[$model['DecayingModel']['uuid']] = $model['DecayingModel'];
|
|
|
|
}
|
|
|
|
foreach ($new_models as $k => $new_model) {
|
|
|
|
if (isset($existing_models[$new_model['uuid']])) {
|
|
|
|
$existing_model = $existing_models[$new_model['uuid']];
|
|
|
|
if ($force || $new_model['version'] > $existing_model['version']) {
|
|
|
|
$new_model['id'] = $existing_model['id'];
|
2019-04-04 11:17:11 +02:00
|
|
|
$new_model['model_id'] = $existing_model['id'];
|
2019-04-03 10:05:45 +02:00
|
|
|
$this->save($new_model);
|
2019-04-04 11:17:11 +02:00
|
|
|
$this->DecayingModelMapping->resetMappingForModel($new_model);
|
2019-04-03 10:05:45 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->create();
|
|
|
|
$this->save($new_model);
|
2019-04-04 09:37:51 +02:00
|
|
|
$new_model['id'] = $this->Model->id;
|
2019-04-04 11:17:11 +02:00
|
|
|
$new_model['model_id'] = $this->Model->id;
|
|
|
|
$this->DecayingModelMapping->resetMappingForModel($new_model);
|
2019-04-03 10:05:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-08 16:19:07 +01:00
|
|
|
public function fetchAllowedModels($user) {
|
|
|
|
$conditions = array();
|
|
|
|
if (!$user['Role']['perm_site_admin']) {
|
|
|
|
if ($user['Role']['perm_decaying']) {
|
|
|
|
$conditions['org_id'] = $user['Organisation']['id'];
|
|
|
|
} else {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$decayingModel = $this->find('all', array(
|
|
|
|
'conditions' => $conditions,
|
|
|
|
'recursive' => -1,
|
|
|
|
));
|
|
|
|
|
|
|
|
return $decayingModel;
|
|
|
|
}
|
|
|
|
|
2019-04-03 10:05:45 +02:00
|
|
|
public function checkAuthorisation($user, $id, $full=true) {
|
2019-03-08 16:19:07 +01:00
|
|
|
// fetch the bare template
|
|
|
|
$decayingModel = $this->find('first', array(
|
|
|
|
'conditions' => array('id' => $id),
|
2019-04-03 10:05:45 +02:00
|
|
|
// 'recursive' => -1,
|
2019-03-08 16:19:07 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
// if not found return false
|
|
|
|
if (empty($decayingModel)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-04 09:37:51 +02:00
|
|
|
if ($full) {
|
|
|
|
$decayingModel['DecayingModel']['attribute_types'] = $this->DecayingModelMapping->getAssociatedTypes($user, $decayingModel['DecayingModel']['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
//if the user is a site admin, return the model without question
|
2019-03-08 16:19:07 +01:00
|
|
|
if ($user['Role']['perm_site_admin']) {
|
|
|
|
return $decayingModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($user['Organisation']['id'] == $decayingModel['DecayingModel']['org_id'] && $user['Role']['perm_decaying']) {
|
|
|
|
return $decayingModel;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-25 11:46:23 +02:00
|
|
|
// filter out taxonomies and entries not having a numerical value
|
|
|
|
public function listTaxonomiesWithNumericalValue()
|
|
|
|
{
|
|
|
|
$this->Taxonomy = ClassRegistry::init('Taxonomy');
|
2019-06-25 16:57:44 +02:00
|
|
|
$this->Tag = ClassRegistry::init('Tag');
|
2019-06-25 11:46:23 +02:00
|
|
|
$taxonomies = $this->Taxonomy->listTaxonomies(array('full' => true, 'enabled' => true));
|
|
|
|
$start_count = count($taxonomies);
|
|
|
|
foreach ($taxonomies as $namespace => $taxonomy) {
|
|
|
|
if(!empty($taxonomy['TaxonomyPredicate'])) {
|
2019-06-25 16:57:44 +02:00
|
|
|
$tags = $this->Tag->getTagsForNamespace($taxonomy['namespace'], false);
|
2019-06-25 11:46:23 +02:00
|
|
|
foreach($taxonomy['TaxonomyPredicate'] as $p => $predicate) {
|
|
|
|
if(!empty($predicate['TaxonomyEntry'])) {
|
|
|
|
foreach ($predicate['TaxonomyEntry'] as $e => $entry) {
|
|
|
|
if (!is_numeric($entry['numerical_value'])) {
|
|
|
|
unset($taxonomies[$namespace]['TaxonomyPredicate'][$p]['TaxonomyEntry'][$e]);
|
2019-06-25 16:57:44 +02:00
|
|
|
} else {
|
|
|
|
$tag_name = sprintf('%s:%s="%s"', $taxonomy['namespace'], $predicate['value'], $entry['value']);
|
|
|
|
$taxonomies[$namespace]['TaxonomyPredicate'][$p]['TaxonomyEntry'][$e]['Tag'] = $tags[strtoupper($tag_name)]['Tag'];
|
|
|
|
$taxonomies[$namespace]['TaxonomyPredicate'][$p]['TaxonomyEntry'][$e]['Tag']['numerical_value'] = $entry['numerical_value'];
|
2019-06-25 11:46:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($taxonomies[$namespace]['TaxonomyPredicate'][$p]['TaxonomyEntry'])) {
|
|
|
|
unset($taxonomies[$namespace]['TaxonomyPredicate'][$p]);
|
2019-06-25 16:57:44 +02:00
|
|
|
} else {
|
|
|
|
$taxonomies[$namespace]['TaxonomyPredicate'][$p]['TaxonomyEntry'] = array_values($taxonomies[$namespace]['TaxonomyPredicate'][$p]['TaxonomyEntry']);
|
2019-06-25 11:46:23 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unset($taxonomies[$namespace]['TaxonomyPredicate'][$p]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($taxonomies[$namespace]['TaxonomyPredicate'])) {
|
|
|
|
unset($taxonomies[$namespace]);
|
2019-06-25 16:57:44 +02:00
|
|
|
} else {
|
|
|
|
$taxonomies[$namespace]['TaxonomyPredicate'] = array_values($taxonomies[$namespace]['TaxonomyPredicate']);
|
2019-06-25 11:46:23 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unset($taxonomies[$namespace]);
|
|
|
|
}
|
|
|
|
}
|
2019-06-25 16:57:44 +02:00
|
|
|
|
2019-06-25 11:46:23 +02:00
|
|
|
return array(
|
|
|
|
'taxonomies' => $taxonomies,
|
|
|
|
'not_having_numerical_value' => $start_count - count($taxonomies)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-08 16:19:07 +01:00
|
|
|
}
|