2014-01-23 12:25:04 +01:00
< ? php
App :: uses ( 'AppController' , 'Controller' );
2018-07-19 11:48:22 +02:00
class TagsController extends AppController
{
public $components = array ( 'Security' , 'RequestHandler' );
2014-01-23 12:25:04 +01:00
2018-07-19 11:48:22 +02:00
public $paginate = array (
'limit' => 50 ,
'order' => array (
'Tag.name' => 'asc'
),
'contain' => array (
'EventTag' => array (
'fields' => array ( 'EventTag.event_id' )
),
'AttributeTag' => array (
'fields' => array ( 'AttributeTag.event_id' , 'AttributeTag.attribute_id' )
),
'FavouriteTag' ,
'Organisation' => array (
'fields' => array ( 'id' , 'name' )
)
)
);
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public $helpers = array ( 'TextColour' );
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function index ( $favouritesOnly = false )
{
$this -> loadModel ( 'Attribute' );
$this -> loadModel ( 'Event' );
$this -> loadModel ( 'Taxonomy' );
if ( $this -> _isSiteAdmin ()) {
$this -> paginate [ 'contain' ][ 'User' ] = array ( 'fields' => array ( 'id' , 'email' ));
}
$taxonomies = $this -> Taxonomy -> listTaxonomies ( array ( 'full' => false , 'enabled' => true ));
$taxonomyNamespaces = array ();
if ( ! empty ( $taxonomies )) {
foreach ( $taxonomies as $taxonomy ) {
$taxonomyNamespaces [ $taxonomy [ 'namespace' ]] = $taxonomy ;
}
}
$taxonomyTags = array ();
$passedArgsArray = array ();
$this -> Event -> recursive = - 1 ;
if ( $favouritesOnly ) {
$tag_id_list = $this -> Tag -> FavouriteTag -> find ( 'list' , array (
'conditions' => array ( 'FavouriteTag.user_id' => $this -> Auth -> user ( 'id' )),
'fields' => array ( 'FavouriteTag.tag_id' )
));
if ( empty ( $tag_id_list )) {
$tag_id_list = array ( - 1 );
}
$this -> paginate [ 'conditions' ][ 'AND' ][ 'Tag.id' ] = $tag_id_list ;
}
if ( isset ( $this -> params [ 'named' ][ 'searchall' ])) {
$passedArgsArray [ 'all' ] = $this -> params [ 'named' ][ 'searchall' ];
} elseif ( $this -> request -> is ( 'post' )) {
$validNames = array ( 'filter' , 'searchall' , 'name' , 'search' );
foreach ( $validNames as $vn ) {
if ( ! empty ( $this -> request -> data [ $vn ])) {
$passedArgsArray [ 'all' ] = $this -> request -> data [ $vn ];
continue ;
}
}
}
if ( ! empty ( $passedArgsArray [ 'all' ])) {
$this -> paginate [ 'conditions' ][ 'AND' ][ 'LOWER(Tag.name) LIKE' ] = '%' . strtolower ( $passedArgsArray [ 'all' ]) . '%' ;
}
if ( $this -> _isRest ()) {
unset ( $this -> paginate [ 'limit' ]);
unset ( $this -> paginate [ 'contain' ][ 'EventTag' ]);
unset ( $this -> paginate [ 'contain' ][ 'AttributeTag' ]);
$paginated = $this -> Tag -> find ( 'all' , $this -> paginate );
} else {
$paginated = $this -> paginate ();
}
$tagList = array ();
$csv = array ();
$sgs = $this -> Tag -> EventTag -> Event -> SharingGroup -> fetchAllAuthorised ( $this -> Auth -> user ());
foreach ( $paginated as $k => $tag ) {
$tagList [] = $tag [ 'Tag' ][ 'id' ];
$paginated [ $k ][ 'Tag' ][ 'count' ] = $this -> Tag -> EventTag -> countForTag ( $tag [ 'Tag' ][ 'id' ], $this -> Auth -> user (), $sgs );
if ( ! $this -> _isRest ()) {
$paginated [ $k ][ 'event_ids' ] = array ();
$paginated [ $k ][ 'attribute_ids' ] = array ();
foreach ( $paginated [ $k ][ 'EventTag' ] as $et ) {
$paginated [ $k ][ 'event_ids' ][] = $et [ 'event_id' ];
}
unset ( $paginated [ $k ][ 'EventTag' ]);
foreach ( $paginated [ $k ][ 'AttributeTag' ] as $at ) {
$paginated [ $k ][ 'attribute_ids' ][] = $at [ 'attribute_id' ];
}
unset ( $paginated [ $k ][ 'AttributeTag' ]);
}
$paginated [ $k ][ 'Tag' ][ 'attribute_count' ] = $this -> Tag -> AttributeTag -> countForTag ( $tag [ 'Tag' ][ 'id' ], $this -> Auth -> user (), $sgs );
if ( ! empty ( $tag [ 'FavouriteTag' ])) {
foreach ( $tag [ 'FavouriteTag' ] as $ft ) {
if ( $ft [ 'user_id' ] == $this -> Auth -> user ( 'id' )) {
$paginated [ $k ][ 'Tag' ][ 'favourite' ] = true ;
}
}
if ( ! isset ( $paginated [ $k ][ 'Tag' ][ 'favourite' ])) {
$paginated [ $k ][ 'Tag' ][ 'favourite' ] = false ;
}
} else {
$paginated [ $k ][ 'Tag' ][ 'favourite' ] = false ;
}
unset ( $paginated [ $k ][ 'FavouriteTag' ]);
if ( ! empty ( $taxonomyNamespaces )) {
$taxonomyNamespaceArrayKeys = array_keys ( $taxonomyNamespaces );
foreach ( $taxonomyNamespaceArrayKeys as $tns ) {
if ( substr ( strtoupper ( $tag [ 'Tag' ][ 'name' ]), 0 , strlen ( $tns )) === strtoupper ( $tns )) {
$paginated [ $k ][ 'Tag' ][ 'Taxonomy' ] = $taxonomyNamespaces [ $tns ];
if ( ! isset ( $taxonomyTags [ $tns ])) {
$taxonomyTags [ $tns ] = $this -> Taxonomy -> getTaxonomyTags ( $taxonomyNamespaces [ $tns ][ 'id' ], true );
}
$paginated [ $k ][ 'Tag' ][ 'Taxonomy' ][ 'expanded' ] = isset ( $taxonomyTags [ $tns ][ strtoupper ( $tag [ 'Tag' ][ 'name' ])]) ? $taxonomyTags [ $tns ][ strtoupper ( $tag [ 'Tag' ][ 'name' ])] : $tag [ 'Tag' ][ 'name' ];
}
}
}
}
if ( ! $this -> _isRest ()) {
$this -> loadModel ( 'Sighting' );
$sightings [ 'event' ] = $this -> Sighting -> getSightingsForObjectIds ( $this -> Auth -> user (), $tagList );
$sightings [ 'attribute' ] = $this -> Sighting -> getSightingsForObjectIds ( $this -> Auth -> user (), $tagList , 'attribute' );
foreach ( $paginated as $k => $tag ) {
$objects = array ( 'event' , 'attribute' );
foreach ( $objects as $object ) {
foreach ( $tag [ $object . '_ids' ] as $objectid ) {
if ( isset ( $sightings [ $object ][ $objectid ])) {
foreach ( $sightings [ $object ][ $objectid ] as $date => $sightingCount ) {
if ( ! isset ( $tag [ 'sightings' ][ $date ])) {
$tag [ 'sightings' ][ $date ] = $sightingCount ;
} else {
$tag [ 'sightings' ][ $date ] += $sightingCount ;
}
}
}
}
}
if ( ! empty ( $tag [ 'sightings' ])) {
$startDate = ! empty ( $tag [ 'sightings' ]) ? min ( array_keys ( $tag [ 'sightings' ])) : date ( 'Y-m-d' );
$startDate = date ( 'Y-m-d' , strtotime ( " -3 days " , strtotime ( $startDate )));
$to = date ( 'Y-m-d' , time ());
for ( $date = $startDate ; strtotime ( $date ) <= strtotime ( $to ); $date = date ( 'Y-m-d' , strtotime ( " +1 day " , strtotime ( $date )))) {
if ( ! isset ( $csv [ $k ])) {
$csv [ $k ] = 'Date,Close\n' ;
}
if ( isset ( $tag [ 'sightings' ][ $date ])) {
$csv [ $k ] .= $date . ',' . $tag [ 'sightings' ][ $date ] . '\n' ;
} else {
$csv [ $k ] .= $date . ',0\n' ;
}
}
}
unset ( $paginated [ $k ][ 'event_ids' ]);
}
}
if ( $this -> _isRest ()) {
foreach ( $paginated as $key => $tag ) {
$paginated [ $key ] = $tag [ 'Tag' ];
}
$this -> set ( 'Tag' , $paginated );
$this -> set ( '_serialize' , array ( 'Tag' ));
} else {
$this -> set ( 'passedArgs' , json_encode ( $this -> passedArgs ));
$this -> set ( 'passedArgsArray' , $passedArgsArray );
$this -> set ( 'csv' , $csv );
$this -> set ( 'list' , $paginated );
$this -> set ( 'favouritesOnly' , $favouritesOnly );
}
// send perm_tagger to view for action buttons
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function add ()
{
if ( ! $this -> _isSiteAdmin () && ! $this -> userRole [ 'perm_tag_editor' ]) {
throw new NotFoundException ( 'You don\'t have permission to do that.' );
}
if ( $this -> request -> is ( 'post' )) {
if ( ! isset ( $this -> request -> data [ 'Tag' ])) {
$this -> request -> data = array ( 'Tag' => $this -> request -> data );
}
if ( isset ( $this -> request -> data [ 'Tag' ][ 'request' ])) {
$this -> request -> data [ 'Tag' ] = $this -> request -> data [ 'Tag' ][ 'request' ];
}
if ( ! isset ( $this -> request -> data [ 'Tag' ][ 'colour' ])) {
$this -> request -> data [ 'Tag' ][ 'colour' ] = $this -> Tag -> random_color ();
}
if ( isset ( $this -> request -> data [ 'Tag' ][ 'id' ])) {
unset ( $this -> request -> data [ 'Tag' ][ 'id' ]);
}
if ( $this -> _isRest ()) {
$tag = $this -> Tag -> find ( 'first' , array (
'conditions' => array (
'Tag.name' => $this -> request -> data [ 'Tag' ][ 'name' ]
),
'recursive' => - 1
));
if ( ! empty ( $tag )) {
return $this -> RestResponse -> viewData ( $tag , $this -> response -> type ());
}
}
if ( $this -> Tag -> save ( $this -> request -> data )) {
if ( $this -> _isRest ()) {
$tag = $this -> Tag -> find ( 'first' , array (
'conditions' => array (
'Tag.id' => $this -> Tag -> id
),
'recursive' => - 1
));
return $this -> RestResponse -> viewData ( $tag , $this -> response -> type ());
}
$this -> Flash -> success ( 'The tag has been saved.' );
$this -> redirect ( array ( 'action' => 'index' ));
} else {
if ( $this -> _isRest ()) {
$error_message = '' ;
foreach ( $this -> Tag -> validationErrors as $k => $v ) {
$error_message .= '[' . $k . ']: ' . $v [ 0 ];
}
throw new MethodNotAllowedException ( 'Could not add the Tag. ' . $error_message );
} else {
$this -> Flash -> error ( 'The tag could not be saved. Please, try again.' );
}
}
} elseif ( $this -> _isRest ()) {
return $this -> RestResponse -> describe ( 'Tag' , 'add' , false , $this -> response -> type ());
}
$this -> loadModel ( 'Organisation' );
$temp = $this -> Organisation -> find ( 'all' , array (
'conditions' => array ( 'local' => 1 ),
'fields' => array ( 'id' , 'name' ),
'recursive' => - 1
));
$orgs = array ( 0 => 'Unrestricted' );
if ( ! empty ( $temp )) {
foreach ( $temp as $org ) {
$orgs [ $org [ 'Organisation' ][ 'id' ]] = $org [ 'Organisation' ][ 'name' ];
}
}
$this -> set ( 'orgs' , $orgs );
$users = array ( 0 => 'Unrestricted' );
if ( $this -> _isSiteAdmin ()) {
$temp = $this -> Organisation -> User -> find ( 'all' , array (
'conditions' => array ( 'disabled' => 0 ),
'fields' => array ( 'id' , 'email' ),
'recursive' => - 1
));
if ( ! empty ( $temp )) {
foreach ( $temp as $user ) {
$users [ $user [ 'User' ][ 'id' ]] = $user [ 'User' ][ 'email' ];
}
}
$this -> set ( 'users' , $users );
}
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function quickAdd ()
{
if (( ! $this -> _isSiteAdmin () && ! $this -> userRole [ 'perm_tag_editor' ]) || ! $this -> request -> is ( 'post' )) {
throw new NotFoundException ( 'You don\'t have permission to do that.' );
}
if ( isset ( $this -> request -> data [ 'Tag' ][ 'request' ])) {
$this -> request -> data [ 'Tag' ] = $this -> request -> data [ 'Tag' ][ 'request' ];
}
if ( $this -> Tag -> quickAdd ( $this -> request -> data [ 'Tag' ][ 'name' ])) {
$this -> Flash -> success ( 'The tag has been saved.' );
} else {
$this -> Flash -> error ( 'The tag could not be saved. Please, try again.' );
}
$this -> redirect ( $this -> referer ());
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function edit ( $id = false )
{
if ( $id === false && ( ! $this -> _isRest () || ! $this -> request -> is ( 'get' ))) {
throw new NotFoundException ( 'No ID set.' );
} elseif ( ! empty ( $id )) {
$this -> Tag -> id = $id ;
if ( ! $this -> Tag -> exists ()) {
throw new NotFoundException ( 'Invalid tag' );
}
}
if ( ! $this -> _isSiteAdmin ()) {
throw new NotFoundException ( 'You don\'t have permission to do that.' );
}
if ( $this -> request -> is ( 'post' ) || $this -> request -> is ( 'put' )) {
2019-08-20 17:46:44 +02:00
if ( ! isset ( $this -> request -> data [ 'Tag' ])) {
$this -> request -> data = array ( 'Tag' => $this -> request -> data );
}
2018-07-19 11:48:22 +02:00
$this -> request -> data [ 'Tag' ][ 'id' ] = $id ;
if ( $this -> Tag -> save ( $this -> request -> data )) {
if ( $this -> _isRest ()) {
$tag = $this -> Tag -> find ( 'first' , array (
2019-02-11 13:22:08 +01:00
'conditions' => array (
2018-07-19 11:48:22 +02:00
'Tag.id' => $id
),
'recursive' => - 1
));
return $this -> RestResponse -> viewData ( $tag , $this -> response -> type ());
}
$this -> Flash -> success ( 'The Tag has been edited' );
$this -> redirect ( array ( 'action' => 'index' ));
} else {
if ( $this -> _isRest ()) {
$error_message = '' ;
foreach ( $this -> Tag -> validationErrors as $k => $v ) {
$error_message .= '[' . $k . ']: ' . $v [ 0 ];
}
throw new MethodNotAllowedException ( 'Could not add the Tag. ' . $error_message );
}
$this -> Flash -> error ( 'The Tag could not be saved. Please, try again.' );
}
} elseif ( $this -> _isRest ()) {
return $this -> RestResponse -> describe ( 'Tag' , 'edit' , false , $this -> response -> type ());
}
$this -> loadModel ( 'Organisation' );
$temp = $this -> Organisation -> find ( 'all' , array (
'conditions' => array ( 'local' => 1 ),
'fields' => array ( 'id' , 'name' ),
'recursive' => - 1
));
$orgs = array ( 0 => 'Unrestricted' );
if ( ! empty ( $temp )) {
foreach ( $temp as $org ) {
$orgs [ $org [ 'Organisation' ][ 'id' ]] = $org [ 'Organisation' ][ 'name' ];
}
}
$this -> set ( 'orgs' , $orgs );
$users = array ( 0 => 'Unrestricted' );
if ( $this -> _isSiteAdmin ()) {
$temp = $this -> Organisation -> User -> find ( 'all' , array (
'conditions' => array ( 'disabled' => 0 ),
'fields' => array ( 'id' , 'email' ),
'recursive' => - 1
));
if ( ! empty ( $temp )) {
foreach ( $temp as $user ) {
$users [ $user [ 'User' ][ 'id' ]] = $user [ 'User' ][ 'email' ];
}
}
$this -> set ( 'users' , $users );
}
$this -> request -> data = $this -> Tag -> read ( null , $id );
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function delete ( $id )
{
if ( ! $this -> _isSiteAdmin ()) {
throw new NotFoundException ( 'You don\'t have permission to do that.' );
}
if ( ! $this -> request -> is ( 'post' )) {
throw new MethodNotAllowedException ();
}
$this -> Tag -> id = $id ;
if ( ! $this -> Tag -> exists ()) {
throw new NotFoundException ( 'Invalid tag' );
}
if ( $this -> Tag -> delete ()) {
if ( $this -> _isRest ()) {
$this -> set ( 'name' , 'Tag deleted.' );
$this -> set ( 'message' , 'Tag deleted.' );
$this -> set ( 'url' , '/tags/delete/' . $id );
$this -> set ( '_serialize' , array ( 'name' , 'message' , 'url' ));
}
$this -> Flash -> success ( __ ( 'Tag deleted' ));
} else {
if ( $this -> _isRest ()) {
throw new MethodNotAllowedException ( 'Could not delete the tag, or tag doesn\'t exist.' );
}
$this -> Flash -> error ( __ ( 'Tag was not deleted' ));
}
if ( ! $this -> _isRest ()) {
$this -> redirect ( array ( 'action' => 'index' ));
}
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function view ( $id )
{
if ( $this -> _isRest ()) {
$contain = array ( 'EventTag' => array ( 'fields' => 'event_id' ));
$contain [ 'AttributeTag' ] = array ( 'fields' => 'attribute_id' );
$tag = $this -> Tag -> find ( 'first' , array (
'conditions' => array ( 'id' => $id ),
'recursive' => - 1 ,
'contain' => $contain
));
if ( empty ( $tag )) {
throw new MethodNotAllowedException ( 'Invalid Tag' );
}
if ( empty ( $tag [ 'EventTag' ])) {
$tag [ 'Tag' ][ 'count' ] = 0 ;
} else {
$eventIDs = array ();
foreach ( $tag [ 'EventTag' ] as $eventTag ) {
$eventIDs [] = $eventTag [ 'event_id' ];
}
$conditions = array ( 'Event.id' => $eventIDs );
if ( ! $this -> _isSiteAdmin ()) {
$conditions = array_merge (
$conditions ,
array ( 'OR' => array (
array ( 'AND' => array (
array ( 'Event.distribution >' => 0 ),
array ( 'Event.published =' => 1 )
)),
array ( 'Event.orgc_id' => $this -> Auth -> user ( 'org_id' ))
))
);
}
$events = $this -> Tag -> EventTag -> Event -> find ( 'all' , array (
'fields' => array ( 'Event.id' , 'Event.distribution' , 'Event.orgc_id' ),
'conditions' => $conditions
));
$tag [ 'Tag' ][ 'count' ] = count ( $events );
}
unset ( $tag [ 'EventTag' ]);
if ( empty ( $tag [ 'AttributeTag' ])) {
$tag [ 'Tag' ][ 'attribute_count' ] = 0 ;
} else {
$attributeIDs = array ();
foreach ( $tag [ 'AttributeTag' ] as $attributeTag ) {
$attributeIDs [] = $attributeTag [ 'attribute_id' ];
}
$conditions = array ( 'Attribute.id' => $attributeIDs );
if ( ! $this -> _isSiteAdmin ()) {
$conditions = array_merge (
$conditions ,
array ( 'OR' => array (
array ( 'AND' => array (
array ( 'Attribute.deleted =' => 0 ),
array ( 'Attribute.distribution >' => 0 ),
array ( 'Event.distribution >' => 0 ),
array ( 'Event.published =' => 1 )
)),
array ( 'Event.orgc_id' => $this -> Auth -> user ( 'org_id' ))
))
);
}
$attributes = $this -> Tag -> AttributeTag -> Attribute -> find ( 'all' , array (
'fields' => array ( 'Attribute.id' , 'Attribute.deleted' , 'Attribute.distribution' , 'Event.id' , 'Event.distribution' , 'Event.orgc_id' ),
'contain' => array ( 'Event' => array ( 'fields' => array ( 'id' , 'distribution' , 'orgc_id' ))),
'conditions' => $conditions
));
$tag [ 'Tag' ][ 'attribute_count' ] = count ( $attributes );
}
unset ( $tag [ 'AttributeTag' ]);
$this -> set ( 'Tag' , $tag [ 'Tag' ]);
$this -> set ( '_serialize' , 'Tag' );
} else {
throw new MethodNotAllowedException ( 'This action is only for REST users.' );
}
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function showEventTag ( $id )
{
$this -> loadModel ( 'EventTag' );
2019-11-04 11:54:22 +01:00
$this -> loadModel ( 'Taxonomy' );
2018-07-19 11:48:22 +02:00
if ( ! $this -> EventTag -> Event -> checkIfAuthorised ( $this -> Auth -> user (), $id )) {
throw new MethodNotAllowedException ( 'Invalid event.' );
}
$this -> loadModel ( 'GalaxyCluster' );
2019-09-20 15:59:20 +02:00
$cluster_names = $this -> GalaxyCluster -> find ( 'list' , array (
'fields' => array ( 'GalaxyCluster.tag_name' ),
'group' => array ( 'GalaxyCluster.id' , 'GalaxyCluster.tag_name' )
));
2018-07-19 11:48:22 +02:00
$this -> helpers [] = 'TextColour' ;
2019-07-19 16:23:15 +02:00
$conditions = array (
'event_id' => $id ,
'Tag.name !=' => $cluster_names
);
2018-07-19 11:48:22 +02:00
$tags = $this -> EventTag -> find ( 'all' , array (
2019-07-19 16:23:15 +02:00
'conditions' => $conditions ,
2018-07-19 11:48:22 +02:00
'contain' => array ( 'Tag' ),
2019-07-08 11:39:41 +02:00
'fields' => array ( 'Tag.id' , 'Tag.colour' , 'Tag.name' , 'EventTag.local' ),
2018-07-19 11:48:22 +02:00
));
2019-07-08 11:39:41 +02:00
foreach ( $tags as $k => $tag ) {
$tags [ $k ][ 'local' ] = $tag [ 'EventTag' ][ 'local' ];
}
2018-07-19 11:48:22 +02:00
$this -> set ( 'tags' , $tags );
$event = $this -> Tag -> EventTag -> Event -> find ( 'first' , array (
'recursive' => - 1 ,
'fields' => array ( 'Event.id' , 'Event.orgc_id' , 'Event.org_id' , 'Event.user_id' ),
'conditions' => array ( 'Event.id' => $id )
));
2019-03-19 15:59:31 +01:00
$this -> set ( 'required_taxonomies' , $this -> EventTag -> Event -> getRequiredTaxonomies ());
2019-11-04 15:12:55 +01:00
$tagConflicts = $this -> Taxonomy -> checkIfTagInconsistencies ( $tags );
2019-11-04 11:54:22 +01:00
$this -> set ( 'tagConflicts' , $tagConflicts );
2018-07-19 11:48:22 +02:00
$this -> set ( 'event' , $event );
$this -> layout = 'ajax' ;
$this -> render ( '/Events/ajax/ajaxTags' );
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function showAttributeTag ( $id )
{
$this -> helpers [] = 'TextColour' ;
$this -> loadModel ( 'AttributeTag' );
2019-11-04 15:12:55 +01:00
$this -> loadModel ( 'Taxonomy' );
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
$this -> Tag -> AttributeTag -> Attribute -> id = $id ;
if ( ! $this -> Tag -> AttributeTag -> Attribute -> exists ()) {
throw new NotFoundException ( __ ( 'Invalid attribute' ));
}
$this -> Tag -> AttributeTag -> Attribute -> read ();
$eventId = $this -> Tag -> AttributeTag -> Attribute -> data [ 'Attribute' ][ 'event_id' ];
2016-09-29 16:52:47 +02:00
2019-07-19 16:23:15 +02:00
$conditions = array ( 'attribute_id' => $id );
2018-07-19 11:48:22 +02:00
$attributeTags = $this -> AttributeTag -> find ( 'all' , array (
2019-07-19 16:23:15 +02:00
'conditions' => $conditions ,
2018-07-19 11:48:22 +02:00
'contain' => array ( 'Tag' ),
2019-07-08 11:39:41 +02:00
'fields' => array ( 'Tag.id' , 'Tag.colour' , 'Tag.name' , 'AttributeTag.local' ),
2018-07-19 11:48:22 +02:00
));
2019-07-08 11:39:41 +02:00
foreach ( $attributeTags as $k => $at ) {
$attributeTags [ $k ][ 'local' ] = $at [ 'AttributeTag' ][ 'local' ];
}
2018-11-23 14:11:33 +01:00
$this -> loadModel ( 'GalaxyCluster' );
$cluster_names = $this -> GalaxyCluster -> find ( 'list' , array ( 'fields' => array ( 'GalaxyCluster.tag_name' ), 'group' => array ( 'GalaxyCluster.tag_name' , 'GalaxyCluster.id' )));
foreach ( $attributeTags as $k => $attributeTag ) {
if ( in_array ( $attributeTag [ 'Tag' ][ 'name' ], $cluster_names )) {
unset ( $attributeTags [ $k ]);
}
}
2018-07-19 11:48:22 +02:00
$event = $this -> Tag -> AttributeTag -> Attribute -> Event -> find ( 'first' , array (
'recursive' => - 1 ,
'fields' => array ( 'Event.id' , 'Event.orgc_id' , 'Event.org_id' , 'Event.user_id' ),
'conditions' => array ( 'Event.id' => $eventId )
));
$this -> set ( 'event' , $event );
$this -> set ( 'attributeTags' , $attributeTags );
$this -> set ( 'attributeId' , $id );
2019-11-04 15:12:55 +01:00
$tagConflicts = $this -> Taxonomy -> checkIfTagInconsistencies ( $attributeTags );
$this -> set ( 'tagConflicts' , $tagConflicts );
2018-07-19 11:48:22 +02:00
$this -> layout = 'ajax' ;
$this -> render ( '/Attributes/ajax/ajaxAttributeTags' );
}
2016-09-29 16:52:47 +02:00
2018-12-20 21:48:04 +01:00
public function showTagControllerTag ( $id )
{
$this -> loadModel ( 'TagCollection' );
$tagCollection = $this -> TagCollection -> find ( 'first' , array (
'recursive' => - 1 ,
'contain' => array ( 'TagCollection' ),
'conditions' => array ( 'TagCollection.id' => $id )
));
if ( empty ( $tagCollection ) || ( ! $this -> _isSiteAdmin () && $tagCollection [ 'org_id' ] !== $this -> Auth -> user ( 'org_id' ))) {
throw new MethodNotAllowedException ( 'Invalid tag_collection.' );
}
$this -> loadModel ( 'GalaxyCluster' );
$cluster_names = $this -> GalaxyCluster -> find ( 'list' , array ( 'fields' => array ( 'GalaxyCluster.tag_name' ), 'group' => array ( 'GalaxyCluster.id' , 'GalaxyCluster.tag_name' )));
$this -> helpers [] = 'TextColour' ;
2018-12-26 23:48:49 +01:00
$tags = $this -> TagCollection -> TagCollectionTag -> find ( 'all' , array (
2018-12-20 21:48:04 +01:00
'conditions' => array (
'tag_collection_id' => $id ,
'Tag.name !=' => $cluster_names
),
'contain' => array ( 'Tag' ),
'fields' => array ( 'Tag.id' , 'Tag.colour' , 'Tag.name' ),
));
$this -> set ( 'tags' , $tags );
$event = $this -> Tag -> EventTag -> Event -> find ( 'first' , array (
'recursive' => - 1 ,
'fields' => array ( 'Event.id' , 'Event.orgc_id' , 'Event.org_id' , 'Event.user_id' ),
'conditions' => array ( 'Event.id' => $id )
));
$this -> set ( 'event' , $event );
$this -> layout = 'ajax' ;
$this -> render ( '/Events/ajax/ajaxTags' );
}
2018-07-19 11:48:22 +02:00
public function viewTag ( $id )
{
$tag = $this -> Tag -> find ( 'first' , array (
'conditions' => array (
'id' => $id
),
'recursive' => - 1 ,
));
$this -> layout = null ;
$this -> set ( 'tag' , $tag );
$this -> set ( 'id' , $id );
$this -> render ( 'ajax/view_tag' );
}
2016-09-29 16:52:47 +02:00
2016-06-04 01:08:16 +02:00
2018-12-20 21:48:04 +01:00
public function selectTaxonomy ( $id , $scope = 'event' )
2018-07-19 11:48:22 +02:00
{
if ( ! $this -> _isSiteAdmin () && ! $this -> userRole [ 'perm_tagger' ]) {
throw new NotFoundException ( 'You don\'t have permission to do that.' );
}
2019-07-08 11:39:41 +02:00
$localFlag = ! empty ( $this -> params [ 'named' ][ 'local' ]) ? '/local:1' : '' ;
2019-01-10 14:40:30 +01:00
$items = array ();
2018-07-19 11:48:22 +02:00
$favourites = $this -> Tag -> FavouriteTag -> find ( 'count' , array ( 'conditions' => array ( 'FavouriteTag.user_id' => $this -> Auth -> user ( 'id' ))));
2019-01-09 10:10:14 +01:00
if ( $favourites ) {
2019-01-18 15:03:24 +01:00
$items [] = array (
'name' => __ ( 'Favourite Tags' ),
2019-07-08 11:39:41 +02:00
'value' => " /tags/selectTag/ " . h ( $id ) . " /favourites/ " . h ( $scope ) . $localFlag
2019-01-18 15:03:24 +01:00
);
2019-01-09 10:10:14 +01:00
}
2019-01-09 16:09:05 +01:00
if ( $scope !== 'tag_collection' ) {
2019-01-18 15:03:24 +01:00
$items [] = array (
'name' => __ ( 'Tag Collections' ),
2019-07-08 11:39:41 +02:00
'value' => " /tags/selectTag/ " . h ( $id ) . " /collections/ " . h ( $scope ) . $localFlag
2019-01-18 15:03:24 +01:00
);
2019-01-09 16:09:05 +01:00
}
2019-04-03 15:52:05 +02:00
$items [] = array (
'name' => __ ( 'Custom Tags' ),
2019-07-08 11:39:41 +02:00
'value' => " /tags/selectTag/ " . h ( $id ) . " /0/ " . h ( $scope ) . $localFlag
2019-04-03 15:52:05 +02:00
);
2019-01-18 15:03:24 +01:00
$items [] = array (
'name' => __ ( 'All Tags' ),
2019-07-08 11:39:41 +02:00
'value' => " /tags/selectTag/ " . h ( $id ) . " /all/ " . h ( $scope ) . $localFlag
2019-01-18 15:03:24 +01:00
);
2019-01-09 10:10:14 +01:00
2018-07-19 11:48:22 +02:00
$this -> loadModel ( 'Taxonomy' );
$options = $this -> Taxonomy -> find ( 'list' , array ( 'conditions' => array ( 'enabled' => true ), 'fields' => array ( 'namespace' ), 'order' => array ( 'Taxonomy.namespace ASC' )));
foreach ( $options as $k => $option ) {
2019-04-03 10:50:52 +02:00
$items [] = array (
'name' => __ ( 'Taxonomy Library' ) . " : " . h ( $option ),
2019-07-08 11:39:41 +02:00
'value' => " /tags/selectTag/ " . h ( $id ) . " / " . h ( $k ) . " / " . h ( $scope . $localFlag )
2019-04-03 10:50:52 +02:00
);
2018-07-19 11:48:22 +02:00
}
2019-01-10 14:40:30 +01:00
$this -> set ( 'items' , $items );
$this -> set ( 'options' , array ( // set chosen (select picker) options
'select_options' => array (
'multiple' => 0 ,
2019-04-03 10:50:52 +02:00
)
2019-01-10 14:40:30 +01:00
));
$this -> render ( '/Elements/generic_picker' );
2018-07-19 11:48:22 +02:00
}
2015-11-27 17:41:41 +01:00
2018-12-20 21:48:04 +01:00
public function selectTag ( $id , $taxonomy_id , $scope = 'event' , $filterData = '' )
2018-07-19 11:48:22 +02:00
{
if ( ! $this -> _isSiteAdmin () && ! $this -> userRole [ 'perm_tagger' ]) {
throw new NotFoundException ( 'You don\'t have permission to do that.' );
}
$this -> loadModel ( 'Taxonomy' );
$expanded = array ();
2018-12-23 22:45:25 +01:00
$banned_tags = $this -> Tag -> find ( 'list' , array (
'conditions' => array (
'NOT' => array (
'Tag.org_id' => array (
0 ,
$this -> Auth -> user ( 'org_id' )
),
'Tag.user_id' => array (
0 ,
$this -> Auth -> user ( 'id' )
)
)
),
'fields' => array ( 'Tag.id' )
));
2019-01-01 16:38:57 +01:00
$this -> set ( 'taxonomy_id' , $taxonomy_id );
2018-12-23 22:45:25 +01:00
if ( $taxonomy_id === 'collections' ) {
$this -> loadModel ( 'TagCollection' );
2019-01-01 16:38:57 +01:00
$tagCollections = $this -> TagCollection -> fetchTagCollection ( $this -> Auth -> user ());
2019-01-11 16:32:19 +01:00
$tags = array ();
$inludedTagListString = array ();
2018-12-23 22:45:25 +01:00
$expanded = array ();
foreach ( $tagCollections as & $tagCollection ) {
2019-01-11 16:32:19 +01:00
$tags [ $tagCollection [ 'TagCollection' ][ 'id' ]] = $tagCollection [ 'TagCollection' ];
2018-12-23 22:45:25 +01:00
$expanded [ $tagCollection [ 'TagCollection' ][ 'id' ]] = empty ( $tagCollection [ 'TagCollection' ][ 'description' ]) ? $tagCollection [ 'TagCollection' ][ 'name' ] : $tagCollection [ 'TagCollection' ][ 'description' ];
2018-12-26 23:48:49 +01:00
if ( ! empty ( $tagCollection [ 'TagCollectionTag' ])) {
2018-12-23 22:45:25 +01:00
$tagList = array ();
2018-12-26 23:48:49 +01:00
foreach ( $tagCollection [ 'TagCollectionTag' ] as $k => $tce ) {
2018-12-23 22:45:25 +01:00
if ( in_array ( $tce [ 'tag_id' ], $banned_tags )) {
2018-12-26 23:48:49 +01:00
unset ( $tagCollection [ 'TagCollectionTag' ][ $k ]);
2018-12-23 22:45:25 +01:00
} else {
$tagList [] = $tce [ 'Tag' ][ 'name' ];
}
2018-12-26 23:48:49 +01:00
$tagCollection [ 'TagCollectionTag' ] = array_values ( $tagCollection [ 'TagCollectionTag' ]);
2018-12-23 22:45:25 +01:00
}
$tagList = implode ( ', ' , $tagList );
2019-01-11 16:32:19 +01:00
$inludedTagListString [ $tagCollection [ 'TagCollection' ][ 'id' ]] = $tagList ;
2018-12-23 22:45:25 +01:00
$expanded [ $tagCollection [ 'TagCollection' ][ 'id' ]] .= sprintf ( ' (%s)' , $tagList );
}
2018-07-19 11:48:22 +02:00
}
} else {
2018-12-23 22:45:25 +01:00
if ( $taxonomy_id === '0' ) {
2019-04-03 15:52:05 +02:00
$temp = $this -> Taxonomy -> getAllTaxonomyTags ( true , false , true );
$tags = array ();
foreach ( $temp as $tag ) {
$tags [ $tag [ 'Tag' ][ 'id' ]] = $tag [ 'Tag' ];
}
unset ( $temp );
2019-01-11 16:32:19 +01:00
$expanded = $tags ;
2018-12-23 22:45:25 +01:00
} elseif ( $taxonomy_id === 'favourites' ) {
2019-01-11 16:32:19 +01:00
$tags = array ();
2018-12-23 22:45:25 +01:00
$conditions = array ( 'FavouriteTag.user_id' => $this -> Auth -> user ( 'id' ));
2019-01-11 16:32:19 +01:00
$favTags = $this -> Tag -> FavouriteTag -> find ( 'all' , array (
2018-12-23 22:45:25 +01:00
'conditions' => $conditions ,
'recursive' => - 1 ,
2019-01-18 16:05:25 +01:00
'contain' => array ( 'Tag' ),
'order' => array ( 'Tag.name asc' )
2018-12-23 22:45:25 +01:00
));
2019-01-11 16:32:19 +01:00
foreach ( $favTags as $favTag ) {
$tags [ $favTag [ 'FavouriteTag' ][ 'tag_id' ]] = $favTag [ 'Tag' ];
$expanded = $tags ;
2018-12-23 22:45:25 +01:00
}
} elseif ( $taxonomy_id === 'all' ) {
2019-04-03 15:52:05 +02:00
if ( ! $this -> _isSiteAdmin ()) {
$conditions = array ( 'Tag.org_id' => array ( 0 , $this -> Auth -> user ( 'org_id' )));
$conditions = array ( 'Tag.user_id' => array ( 0 , $this -> Auth -> user ( 'id' )));
}
2018-12-23 22:45:25 +01:00
$conditions [ 'Tag.hide_tag' ] = 0 ;
2019-04-03 15:52:05 +02:00
$allTags = $this -> Tag -> find ( 'all' , array (
'conditions' => $conditions ,
'recursive' => - 1 ,
'order' => array ( 'name asc' ),
'fields' => array ( 'Tag.id' , 'Tag.name' , 'Tag.colour' )
));
2019-01-11 16:32:19 +01:00
$tags = array ();
2019-04-03 15:52:05 +02:00
foreach ( $allTags as $k => $tag ) {
$temp = explode ( ':' , $tag [ 'Tag' ][ 'name' ]);
if ( count ( $temp ) > 1 ) {
if ( $temp [ 0 ] !== 'misp-galaxy' ) {
$tags [ $tag [ 'Tag' ][ 'id' ]] = $tag [ 'Tag' ];
}
} else {
2019-01-18 13:59:33 +01:00
$tags [ $tag [ 'Tag' ][ 'id' ]] = $tag [ 'Tag' ];
}
2019-01-11 16:32:19 +01:00
}
unset ( $allTags );
$expanded = $tags ;
2018-12-23 22:45:25 +01:00
} else {
$taxonomies = $this -> Taxonomy -> getTaxonomy ( $taxonomy_id );
2019-01-11 16:32:19 +01:00
$tags = array ();
2018-12-23 22:45:25 +01:00
if ( ! empty ( $taxonomies [ 'entries' ])) {
foreach ( $taxonomies [ 'entries' ] as $entry ) {
if ( ! empty ( $entry [ 'existing_tag' ][ 'Tag' ])) {
2019-01-11 16:32:19 +01:00
$tags [ $entry [ 'existing_tag' ][ 'Tag' ][ 'id' ]] = $entry [ 'existing_tag' ][ 'Tag' ];
2018-12-23 22:45:25 +01:00
$expanded [ $entry [ 'existing_tag' ][ 'Tag' ][ 'id' ]] = $entry [ 'expanded' ];
}
2018-12-20 21:48:04 +01:00
}
2018-07-19 11:48:22 +02:00
}
}
2018-12-23 22:45:25 +01:00
// Unset all tags that this user cannot use for tagging, determined by the org restriction on tags
if ( ! $this -> _isSiteAdmin ()) {
foreach ( $banned_tags as $banned_tag ) {
2019-01-11 16:32:19 +01:00
unset ( $tags [ $banned_tag ]);
2018-12-23 22:45:25 +01:00
unset ( $expanded [ $banned_tag ]);
}
}
$hidden_tags = $this -> Tag -> find ( 'list' , array (
'conditions' => array ( 'Tag.hide_tag' => 1 ),
2018-07-19 11:48:22 +02:00
'fields' => array ( 'Tag.id' )
));
2018-12-23 22:45:25 +01:00
foreach ( $hidden_tags as $hidden_tag ) {
2019-01-11 16:32:19 +01:00
unset ( $tags [ $hidden_tag ]);
2018-12-23 22:45:25 +01:00
unset ( $expanded [ $hidden_tag ]);
}
2019-01-09 14:19:14 +01:00
}
$this -> set ( 'scope' , $scope );
$this -> set ( 'object_id' , $id );
2019-01-22 16:02:01 +01:00
if ( $scope === 'attribute' ) {
$onClickForm = 'quickSubmitAttributeTagForm' ;
} elseif ( $scope === 'tag_collection' ) {
$onClickForm = 'quickSubmitTagCollectionTagForm' ;
} else {
$onClickForm = 'quickSubmitTagForm' ;
}
2019-01-09 14:19:14 +01:00
$items = array ();
2019-01-11 16:32:19 +01:00
foreach ( $tags as $k => $tag ) {
$tagName = $tag [ 'name' ];
2019-01-09 14:19:14 +01:00
$choice_id = $k ;
if ( $taxonomy_id === 'collections' ) {
$choice_id = 'collection_' . $choice_id ;
}
2019-01-18 15:03:24 +01:00
$itemParam = array (
2019-01-22 16:02:01 +01:00
'name' => $tagName ,
'value' => $choice_id ,
'template' => array (
'name' => array (
'name' => $tagName ,
'label' => array (
'background' => isset ( $tag [ 'colour' ]) ? $tag [ 'colour' ] : '#ffffff'
)
),
'infoExtra' => $expanded [ $tag [ 'id' ]]
2019-01-09 14:19:14 +01:00
)
);
2019-01-11 16:32:19 +01:00
if ( $taxonomy_id === 'collections' ) {
2019-01-22 16:02:01 +01:00
$itemParam [ 'template' ][ 'infoContextual' ] = __ ( 'Includes: ' ) . $inludedTagListString [ $tag [ 'id' ]];
2019-01-11 16:32:19 +01:00
}
2019-01-18 15:03:24 +01:00
$items [] = $itemParam ;
2018-07-19 11:48:22 +02:00
}
2019-01-09 14:19:14 +01:00
$this -> set ( 'items' , $items );
$this -> set ( 'options' , array ( // set chosen (select picker) options
'functionName' => $onClickForm ,
2019-01-10 14:40:30 +01:00
'multiple' => - 1 ,
2019-01-28 14:47:49 +01:00
'select_options' => array (
'additionalData' => array (
2019-07-08 11:39:41 +02:00
'id' => $id ,
'local' => ! empty ( $this -> params [ 'named' ][ 'local' ])
2019-01-28 14:47:49 +01:00
),
),
2019-01-09 14:19:14 +01:00
));
2019-07-08 11:39:41 +02:00
$this -> set ( 'local' , ! empty ( $this -> params [ 'named' ][ 'local' ]));
2019-01-09 14:19:14 +01:00
$this -> render ( 'ajax/select_tag' );
2018-07-19 11:48:22 +02:00
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function tagStatistics ( $percentage = false , $keysort = false )
{
$result = $this -> Tag -> EventTag -> find ( 'all' , array (
'recursive' => - 1 ,
'fields' => array ( 'count(EventTag.id) as count' , 'tag_id' ),
'contain' => array ( 'Tag' => array ( 'fields' => array ( 'Tag.name' ))),
'group' => array ( 'tag_id' )
));
$tags = array ();
$taxonomies = array ();
$totalCount = 0 ;
$this -> loadModel ( 'Taxonomy' );
$temp = $this -> Taxonomy -> listTaxonomies ( array ( 'enabled' => true ));
foreach ( $temp as $t ) {
if ( $t [ 'enabled' ]) {
$taxonomies [ $t [ 'namespace' ]] = 0 ;
}
}
foreach ( $result as $r ) {
if ( $r [ 'Tag' ][ 'name' ] == null ) {
continue ;
}
$tags [ $r [ 'Tag' ][ 'name' ]] = $r [ 0 ][ 'count' ];
$totalCount += $r [ 0 ][ 'count' ];
foreach ( $taxonomies as $taxonomy => $count ) {
if ( substr ( strtolower ( $r [ 'Tag' ][ 'name' ]), 0 , strlen ( $taxonomy )) === strtolower ( $taxonomy )) {
$taxonomies [ $taxonomy ] += $r [ 0 ][ 'count' ];
}
}
}
if ( $keysort === 'true' ) {
ksort ( $tags , SORT_NATURAL | SORT_FLAG_CASE );
ksort ( $taxonomies , SORT_NATURAL | SORT_FLAG_CASE );
} else {
arsort ( $tags );
arsort ( $taxonomies );
}
if ( $percentage === 'true' ) {
foreach ( $tags as $tag => $count ) {
$tags [ $tag ] = round ( 100 * $count / $totalCount , 3 ) . '%' ;
}
foreach ( $taxonomies as $taxonomy => $count ) {
$taxonomies [ $taxonomy ] = round ( 100 * $count / $totalCount , 3 ) . '%' ;
}
}
$results = array ( 'tags' => $tags , 'taxonomies' => $taxonomies );
$this -> autoRender = false ;
$this -> layout = false ;
$this -> set ( 'data' , $results );
$this -> set ( 'flags' , JSON_PRETTY_PRINT );
$this -> response -> type ( 'json' );
$this -> render ( '/Servers/json/simple' );
}
2016-06-04 01:08:16 +02:00
2019-11-26 11:36:49 +01:00
private function __findObjectByUuid ( $object_uuid , & $type , $scope = 'modify' )
2018-07-19 11:48:22 +02:00
{
$this -> loadModel ( 'Event' );
2019-11-26 11:36:49 +01:00
if ( ! $this -> userRole [ 'perm_tagger' ]) {
throw new MethodNotAllowedException ( __ ( 'This functionality requires tagging permission.' ));
}
$object = $this -> Event -> fetchEvent ( $this -> Auth -> user (), array (
'event_uuid' => $object_uuid ,
'metadata' => 1
2018-07-19 11:48:22 +02:00
));
$type = 'Event' ;
if ( ! empty ( $object )) {
2019-11-26 11:36:49 +01:00
$object = $object [ 0 ];
2018-07-19 11:48:22 +02:00
if (
2019-11-26 11:36:49 +01:00
$scope !== 'view' &&
2018-07-19 11:48:22 +02:00
! $this -> _isSiteAdmin () &&
2019-11-29 13:40:22 +01:00
$object [ 'Event' ][ 'orgc_id' ] != $this -> Auth -> user ( 'org_id' )
2018-07-19 11:48:22 +02:00
) {
2019-11-29 13:31:29 +01:00
$message = __ ( 'Cannot alter the tags of this data, only the organisation that has created the data (orgc) can modify global tags.' );
if ( $this -> Auth -> user ( 'org_id' ) === Configure :: read ( 'MISP.host_org_id' )) {
$message .= ' ' . __ ( 'Please consider using local tags if you are in the host organisation of the instance.' );
}
throw new MethodNotAllowedException ( $message );
2018-07-19 11:48:22 +02:00
}
} else {
$type = 'Attribute' ;
2019-11-26 11:36:49 +01:00
$object = $this -> Event -> Attribute -> fetchAttributes (
$this -> Auth -> user (),
array (
'conditions' => array (
'Attribute.uuid' => $object_uuid
),
'flatten' => 1
)
);
2018-07-19 11:48:22 +02:00
if ( ! empty ( $object )) {
2019-11-26 11:36:49 +01:00
$object = $object [ 0 ];
if (
$scope !== 'view' &&
! $this -> _isSiteAdmin () &&
2019-11-29 13:40:22 +01:00
$object [ 'Event' ][ 'orgc_id' ] != $this -> Auth -> user ( 'org_id' )
2019-11-26 11:36:49 +01:00
) {
2019-11-29 13:31:29 +01:00
$message = __ ( 'Cannot alter the tags of this data, only the organisation that has created the data (orgc) can modify global tags.' );
if ( $this -> Auth -> user ( 'org_id' ) === Configure :: read ( 'MISP.host_org_id' )) {
$message .= ' ' . __ ( 'Please consider using local tags if you are in the host organisation of the instance.' );
}
throw new MethodNotAllowedException ( $message );
2018-07-19 11:48:22 +02:00
}
} else {
2019-11-26 11:36:49 +01:00
throw new MethodNotAllowedException ( __ ( 'Invalid Target.' ));
2018-07-19 11:48:22 +02:00
}
}
return $object ;
}
2017-01-27 19:05:43 +01:00
2019-09-24 17:55:36 +02:00
public function attachTagToObject ( $uuid = false , $tag = false , $local = false )
2018-07-19 11:48:22 +02:00
{
if ( ! $this -> request -> is ( 'post' )) {
throw new MethodNotAllowedException ( 'This method is only accessible via POST requests.' );
}
if ( empty ( $uuid )) {
if ( ! empty ( $this -> request -> data [ 'uuid' ])) {
$uuid = $this -> request -> data [ 'uuid' ];
} else {
throw new MethodNotAllowedException ( 'Invalid object uuid' );
}
}
if ( ! Validation :: uuid ( $uuid )) {
throw new InvalidArgumentException ( 'Invalid UUID' );
}
if ( empty ( $tag )) {
if ( ! empty ( $this -> request -> data [ 'tag' ])) {
$tag = $this -> request -> data [ 'tag' ];
} else {
throw new MethodNotAllowedException ( 'Invalid tag' );
}
}
if ( is_numeric ( $tag )) {
$conditions = array ( 'Tag.id' => $tag );
} else {
$conditions = array ( 'LOWER(Tag.name) LIKE' => strtolower ( trim ( $tag )));
}
2019-09-24 17:55:36 +02:00
if ( empty ( $local )) {
if ( ! empty ( $this -> request -> data [ 'local' ])) {
$local = $this -> request -> data [ 'local' ];
}
}
2019-11-26 11:36:49 +01:00
if ( ! empty ( $local ) && $this -> Auth -> user ( 'org_id' ) != Configure :: read ( 'MISP.host_org_id' )) {
throw new MethodNotAllowedException ( __ ( 'Local tags can only be added by users of the host organisation.' ));
2019-09-24 17:55:36 +02:00
}
2018-07-19 11:48:22 +02:00
$objectType = '' ;
2019-11-26 11:36:49 +01:00
$object = $this -> __findObjectByUuid ( $uuid , $objectType , $local ? 'view' : 'modify' );
2018-07-19 11:48:22 +02:00
$existingTag = $this -> Tag -> find ( 'first' , array ( 'conditions' => $conditions , 'recursive' => - 1 ));
if ( empty ( $existingTag )) {
if ( ! is_numeric ( $tag )) {
if ( ! $this -> userRole [ 'perm_tag_editor' ]) {
throw new MethodNotAllowedException ( 'Tag not found and insufficient privileges to create it.' );
}
$this -> Tag -> create ();
2019-11-11 15:33:15 +01:00
$result = $this -> Tag -> save ( array ( 'Tag' => array ( 'name' => $tag , 'colour' => $this -> Tag -> random_color ())));
if ( ! $result ) {
return $this -> RestResponse -> saveFailResponse ( 'Tags' , 'attachTagToObject' , false , __ ( 'Unable to create tag. Reason: ' . json_encode ( $this -> Tag -> validationErrors )), $this -> response -> type ());
}
2018-07-19 11:48:22 +02:00
$existingTag = $this -> Tag -> find ( 'first' , array ( 'recursive' => - 1 , 'conditions' => array ( 'Tag.id' => $this -> Tag -> id )));
} else {
throw new NotFoundException ( 'Invalid Tag.' );
}
}
if ( ! $this -> _isSiteAdmin ()) {
if ( ! in_array ( $existingTag [ 'Tag' ][ 'org_id' ], array ( 0 , $this -> Auth -> user ( 'org_id' )))) {
2019-11-11 16:11:39 +01:00
throw new MethodNotAllowedException ( 'Invalid Tag. This tag can only be set by a fixed organisation.' );
2018-07-19 11:48:22 +02:00
}
if ( ! in_array ( $existingTag [ 'Tag' ][ 'user_id' ], array ( 0 , $this -> Auth -> user ( 'id' )))) {
2019-11-11 16:11:39 +01:00
throw new MethodNotAllowedException ( 'Invalid Tag. This tag can only be set by a fixed user.' );
2018-07-19 11:48:22 +02:00
}
}
$this -> loadModel ( $objectType );
$connectorObject = $objectType . 'Tag' ;
$conditions = array (
strtolower ( $objectType ) . '_id' => $object [ $objectType ][ 'id' ],
2019-09-24 17:55:36 +02:00
'tag_id' => $existingTag [ 'Tag' ][ 'id' ],
'local' => ( $local ? 1 : 0 )
2018-07-19 11:48:22 +02:00
);
$existingAssociation = $this -> $objectType -> $connectorObject -> find ( 'first' , array (
2019-09-24 17:55:36 +02:00
'conditions' => $conditions
2018-07-19 11:48:22 +02:00
));
if ( ! empty ( $existingAssociation )) {
return $this -> RestResponse -> saveSuccessResponse ( 'Tags' , 'attachTagToObject' , false , $this -> response -> type (), $objectType . ' already has the requested tag attached, no changes had to be made.' );
}
$this -> $objectType -> $connectorObject -> create ();
$data = array (
2019-09-24 17:55:36 +02:00
$connectorObject => $conditions
2018-07-19 11:48:22 +02:00
);
if ( $objectType == 'Attribute' ) {
$data [ $connectorObject ][ 'event_id' ] = $object [ 'Event' ][ 'id' ];
}
$result = $this -> $objectType -> $connectorObject -> save ( $data );
if ( $result ) {
2019-01-29 18:11:28 +01:00
$tempObject = $this -> $objectType -> find ( 'first' , array (
'recursive' => - 1 ,
'conditions' => array ( $objectType . '.id' => $object [ $objectType ][ 'id' ])
));
$date = new DateTime ();
$tempObject [ $objectType ][ 'timestamp' ] = $date -> getTimestamp ();
$this -> $objectType -> save ( $tempObject );
2019-09-24 17:55:36 +02:00
if ( $local ) {
$message = 'Local tag ' . $existingTag [ 'Tag' ][ 'name' ] . '(' . $existingTag [ 'Tag' ][ 'id' ] . ') successfully attached to ' . $objectType . '(' . $object [ $objectType ][ 'id' ] . ').' ;
} else {
2019-09-30 21:24:46 +02:00
if ( $objectType === 'Attribute' ) {
$this -> $objectType -> Event -> unpublishEvent ( $object [ 'Event' ][ 'id' ]);
} else if ( $objectType === 'Event' ) {
$this -> Event -> unpublishEvent ( $object [ 'Event' ][ 'id' ]);
}
2019-09-24 17:55:36 +02:00
$message = 'Global tag ' . $existingTag [ 'Tag' ][ 'name' ] . '(' . $existingTag [ 'Tag' ][ 'id' ] . ') successfully attached to ' . $objectType . '(' . $object [ $objectType ][ 'id' ] . ').' ;
}
2018-07-19 11:48:22 +02:00
return $this -> RestResponse -> saveSuccessResponse ( 'Tags' , 'attachTagToObject' , false , $this -> response -> type (), $message );
} else {
return $this -> RestResponse -> saveFailResponse ( 'Tags' , 'attachTagToObject' , false , 'Failed to attach tag to object.' , $this -> response -> type ());
}
}
2017-01-27 19:05:43 +01:00
2018-07-19 11:48:22 +02:00
public function removeTagFromObject ( $uuid = false , $tag = false )
{
if ( ! $this -> request -> is ( 'post' )) {
throw new MethodNotAllowedException ( 'This method is only accessible via POST requests.' );
}
if ( empty ( $uuid )) {
if ( ! empty ( $this -> request -> data [ 'uuid' ])) {
$uuid = $this -> request -> data [ 'uuid' ];
} else {
throw new MethodNotAllowedException ( 'Invalid object uuid' );
}
}
if ( ! Validation :: uuid ( $uuid )) {
throw new InvalidArgumentException ( 'Invalid UUID' );
}
if ( empty ( $tag )) {
if ( ! empty ( $this -> request -> data [ 'tag' ])) {
$tag = $this -> request -> data [ 'tag' ];
} else {
throw new MethodNotAllowedException ( 'Invalid tag' );
}
}
if ( is_numeric ( $tag )) {
$conditions = array ( 'Tag.id' => $tag );
} else {
$conditions = array ( 'LOWER(Tag.name) LIKE' => strtolower ( trim ( $tag )));
}
$existingTag = $this -> Tag -> find ( 'first' , array ( 'conditions' => $conditions , 'recursive' => - 1 ));
if ( empty ( $existingTag )) {
throw new MethodNotAllowedException ( 'Invalid Tag.' );
}
$objectType = '' ;
2019-11-26 11:36:49 +01:00
$object = $this -> __findObjectByUuid ( $uuid , $objectType , 'view' );
2018-07-19 11:48:22 +02:00
if ( empty ( $object )) {
2019-11-26 11:36:49 +01:00
throw new MethodNotAllowedException ( __ ( 'Invalid Target.' ));
2018-07-19 11:48:22 +02:00
}
$connectorObject = $objectType . 'Tag' ;
$this -> loadModel ( $objectType );
$existingAssociation = $this -> $objectType -> $connectorObject -> find ( 'first' , array (
'conditions' => array (
strtolower ( $objectType ) . '_id' => $object [ $objectType ][ 'id' ],
'tag_id' => $existingTag [ 'Tag' ][ 'id' ]
)
));
if ( empty ( $existingAssociation )) {
throw new MethodNotAllowedException ( 'Could not remove tag as it is not attached to the target ' . $objectType );
2019-11-26 11:36:49 +01:00
} else {
if ( empty ( $existingAssociation [ $objectType . 'Tag' ][ 'local' ])) {
$object = $this -> __findObjectByUuid ( $uuid , $objectType );
} else {
if ( $object [ 'Event' ][ 'orgc_id' ] !== $this -> Auth -> user ( 'org_id' ) && $this -> Auth -> user ( 'org_id' ) != Configure :: read ( 'MISP.host_org_id' )) {
throw new MethodNotAllowedException ( __ ( 'Insufficient privileges to remove local tags from events you do not own.' ));
}
}
2018-07-19 11:48:22 +02:00
}
$result = $this -> $objectType -> $connectorObject -> delete ( $existingAssociation [ $connectorObject ][ 'id' ]);
if ( $result ) {
$message = 'Tag ' . $existingTag [ 'Tag' ][ 'name' ] . '(' . $existingTag [ 'Tag' ][ 'id' ] . ') successfully removed from ' . $objectType . '(' . $object [ $objectType ][ 'id' ] . ').' ;
return $this -> RestResponse -> saveSuccessResponse ( 'Tags' , 'removeTagFromObject' , false , $this -> response -> type (), $message );
} else {
return $this -> RestResponse -> saveFailResponse ( 'Tags' , 'removeTagFromObject' , false , 'Failed to remove tag from object.' , $this -> response -> type ());
}
}
2017-01-27 19:05:43 +01:00
2018-07-19 11:48:22 +02:00
public function viewGraph ( $id )
{
$tag = $this -> Tag -> find ( 'first' , array (
'conditions' => array ( 'Tag.id' => $id ),
'recursive' => - 1
));
if ( empty ( $tag )) {
throw new MethodNotAllowedException ( 'Invalid Tag.' );
}
$this -> loadModel ( 'Taxonomy' );
$taxonomy = $this -> Taxonomy -> getTaxonomyForTag ( $tag [ 'Tag' ][ 'name' ]);
if ( ! empty ( $taxonomy )) {
$this -> set ( 'taxonomy' , $taxonomy );
}
$this -> set ( 'scope' , 'tag' );
$this -> set ( 'id' , $id );
$this -> render ( '/Events/view_graph' );
}
2019-02-10 21:33:03 +01:00
public function search ( $tag = false )
{
if ( isset ( $this -> request -> data [ 'Tag' ])) {
$this -> request -> data = $this -> request -> data [ 'Tag' ];
}
if ( ! empty ( $this -> request -> data [ 'tag' ])) {
$tag = $this -> request -> data [ 'tag' ];
} else if ( ! empty ( $this -> request -> data )) {
$tag = $this -> request -> data ;
}
2019-02-10 22:20:54 +01:00
if ( ! is_array ( $tag )) {
$tag = array ( $tag );
}
2019-04-16 21:54:55 +02:00
$conditions = array ();
2019-02-10 22:20:54 +01:00
foreach ( $tag as $k => $t ) {
$tag [ $k ] = strtolower ( $t );
2019-04-16 21:54:55 +02:00
$conditions [ 'OR' ][] = array ( 'LOWER(GalaxyCluster.value)' => $tag [ $k ]);
2019-02-10 22:20:54 +01:00
}
foreach ( $tag as $k => $t ) {
2019-04-16 21:54:55 +02:00
$conditions [ 'OR' ][] = array ( 'AND' => array ( 'GalaxyElement.key' => 'synonyms' , 'LOWER(GalaxyElement.value) LIKE' => $t ));
2019-02-10 22:20:54 +01:00
}
2019-04-16 21:54:55 +02:00
$this -> loadModel ( 'GalaxyCluster' );
2019-02-10 22:20:54 +01:00
$elements = $this -> GalaxyCluster -> GalaxyElement -> find ( 'all' , array (
'recursive' => - 1 ,
'conditions' => $conditions ,
'contain' => array ( 'GalaxyCluster.tag_name' )
));
foreach ( $elements as $element ) {
$tag [] = strtolower ( $element [ 'GalaxyCluster' ][ 'tag_name' ]);
}
$conditions = array ();
foreach ( $tag as $k => $t ) {
$conditions [ 'OR' ][] = array ( 'LOWER(Tag.name) LIKE' => $t );
2019-02-10 21:33:03 +01:00
}
$tags = $this -> Tag -> find ( 'all' , array (
'conditions' => $conditions ,
'recursive' => - 1
));
$this -> loadModel ( 'Taxonomy' );
foreach ( $tags as $k => $t ) {
$taxonomy = $this -> Taxonomy -> getTaxonomyForTag ( $t [ 'Tag' ][ 'name' ], true );
if ( ! empty ( $taxonomy )) {
$tags [ $k ][ 'Taxonomy' ] = $taxonomy [ 'Taxonomy' ];
}
2020-03-12 10:26:09 +01:00
$cluster = $this -> GalaxyCluster -> getCluster ( $t [ 'Tag' ][ 'name' ], $this -> Auth -> user );
2019-02-10 21:33:03 +01:00
if ( ! empty ( $cluster )) {
$tags [ $k ][ 'GalaxyCluster' ] = $cluster [ 'GalaxyCluster' ];
}
}
return $this -> RestResponse -> viewData ( $tags , $this -> response -> type ());
}
2015-01-27 10:10:22 +01:00
}