2016-02-28 22:54:09 +01:00
< ? php
App :: uses ( 'AppController' , 'Controller' );
2020-07-27 09:37:08 +02:00
/**
* @ property Feed $Feed
*/
2018-07-19 11:48:22 +02:00
class FeedsController extends AppController
{
2021-06-17 17:14:22 +02:00
public $components = array (
'Security' ,
'CRUD' ,
'RequestHandler'
); // XXX ACL component
2016-02-28 22:54:09 +01:00
2018-07-19 11:48:22 +02:00
public $paginate = array (
2021-06-17 17:14:22 +02:00
'limit' => 60 ,
'recursive' => - 1 ,
'contain' => array (
'Tag' ,
'SharingGroup' ,
'Orgc' => array (
'fields' => array (
'Orgc.id' ,
'Orgc.uuid' ,
'Orgc.name' ,
'Orgc.local'
2020-04-17 14:17:54 +02:00
)
2021-06-17 17:14:22 +02:00
)
),
'maxLimit' => 9999 , // LATER we will bump here on a problem once we have more than 9999 events
'order' => array (
'Feed.default' => 'DESC' ,
'Feed.id' => 'ASC'
),
2018-07-19 11:48:22 +02:00
);
2016-02-28 22:54:09 +01:00
2018-07-19 11:48:22 +02:00
public $uses = array ( 'Feed' );
2016-02-28 22:54:09 +01:00
2018-07-19 11:48:22 +02:00
public function beforeFilter ()
{
parent :: beforeFilter ();
2019-02-11 22:28:59 +01:00
$this -> Security -> unlockedActions [] = 'previewIndex' ;
2019-03-10 18:09:46 +01:00
$this -> Security -> unlockedActions [] = 'feedCoverage' ;
2019-01-20 10:19:05 +01:00
if ( ! $this -> _isSiteAdmin () && $this -> Auth -> user ( 'org_id' ) != Configure :: read ( 'MISP.host_org_id' )) {
2018-07-19 11:48:22 +02:00
throw new MethodNotAllowedException ( __ ( 'You don\'t have the required privileges to do that.' ));
}
}
2016-02-28 22:54:09 +01:00
2020-04-17 14:17:54 +02:00
public function loadDefaultFeeds ()
{
if ( $this -> request -> is ( 'post' )) {
$this -> Feed -> load_default_feeds ();
$message = __ ( 'Default feed metadata loaded.' );
if ( $this -> _isRest ()) {
return $this -> RestResponse -> saveSuccessResponse ( 'Feed' , 'loadDefaultFeeds' , false , $this -> response -> type (), $message );
} else {
$this -> Flash -> success ( $message );
$this -> redirect ( array ( 'controller' => 'Feeds' , 'action' => 'index' ));
}
}
}
2018-07-19 11:48:22 +02:00
public function index ()
{
2021-06-17 17:14:22 +02:00
$conditions = [];
2018-07-19 11:48:22 +02:00
$scope = isset ( $this -> passedArgs [ 'scope' ]) ? $this -> passedArgs [ 'scope' ] : 'all' ;
if ( $scope !== 'all' ) {
if ( $scope == 'enabled' ) {
2021-06-17 17:14:22 +02:00
$conditions [] = array (
2018-07-19 11:48:22 +02:00
'OR' => array (
'Feed.enabled' => 1 ,
'Feed.caching_enabled' => 1
)
);
} else {
2021-06-17 17:14:22 +02:00
$conditions [] = array (
2018-07-19 11:48:22 +02:00
'Feed.default' => $scope == 'custom' ? 0 : 1
);
}
}
2019-04-01 14:20:09 +02:00
2021-06-17 17:14:22 +02:00
$this -> CRUD -> index ([
'filters' => [
'Feed.name' ,
'url' ,
'provider' ,
'source_format' ,
'enabled' ,
'caching_enabled' ,
'default'
],
'quickFilters' => [
'Feed.name' ,
'url' ,
'provider' ,
'source_format'
],
'conditions' => $conditions ,
'afterFind' => function ( array $feeds ) {
if ( $this -> _isSiteAdmin ()) {
$feeds = $this -> Feed -> attachFeedCacheTimestamps ( $feeds );
2018-07-19 11:48:22 +02:00
}
2021-06-17 17:14:22 +02:00
if ( $this -> IndexFilter -> isRest ()) {
foreach ( $feeds as & $feed ) {
unset ( $feed [ 'SharingGroup' ]);
if ( empty ( $feed [ 'Tag' ][ 'id' ])) {
unset ( $feed [ 'Tag' ]);
}
}
2018-07-19 11:48:22 +02:00
}
2021-06-17 17:14:22 +02:00
return $feeds ;
2018-07-19 11:48:22 +02:00
}
2021-06-17 17:14:22 +02:00
]);
if ( $this -> IndexFilter -> isRest ()) {
return $this -> restResponsePayload ;
2018-07-19 11:48:22 +02:00
}
2021-06-17 17:14:22 +02:00
$this -> set ( 'title_for_layout' , __ ( 'Feeds' ));
$this -> set ( 'menuData' , [
'menuList' => 'feeds' ,
'menuItem' => 'index'
]);
2018-07-19 11:48:22 +02:00
$this -> loadModel ( 'Event' );
$this -> set ( 'distributionLevels' , $this -> Event -> distributionLevels );
2021-06-17 17:14:22 +02:00
$this -> set ( 'scope' , $scope );
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 view ( $feedId )
{
2021-06-17 17:14:22 +02:00
$this -> CRUD -> view ( $feedId , [
'contain' => [ 'Tag' ],
'afterFind' => function ( array $feed ) {
if ( ! $this -> _isSiteAdmin ()) {
unset ( $feed [ 'Feed' ][ 'headers' ]);
}
$feed [ 'Feed' ][ 'cached_elements' ] = $this -> Feed -> getCachedElements ( $feed [ 'Feed' ][ 'id' ]);
$feed [ 'Feed' ][ 'coverage_by_other_feeds' ] = $this -> Feed -> getFeedCoverage ( $feed [ 'Feed' ][ 'id' ], 'feed' , 'all' ) . '%' ;
if ( $this -> _isRest ()) {
if ( empty ( $feed [ 'Tag' ][ 'id' ])) {
unset ( $feed [ 'Tag' ]);
}
}
return $feed ;
2018-07-19 11:48:22 +02:00
}
2021-06-17 17:14:22 +02:00
]);
if ( $this -> IndexFilter -> isRest ()) {
return $this -> restResponsePayload ;
2018-07-19 11:48:22 +02:00
}
2021-06-17 17:14:22 +02:00
$otherFeeds = $this -> Feed -> getAllCachingEnabledFeeds ( $feedId , true );
$this -> set ( 'other_feeds' , $otherFeeds );
2019-03-10 18:09:46 +01:00
}
2019-04-01 16:09:24 +02:00
2019-03-10 18:09:46 +01:00
public function feedCoverage ( $feedId )
{
$feed = $this -> Feed -> find ( 'first' , array (
'conditions' => array ( 'Feed.id' => $feedId ),
'recursive' => - 1 ,
'contain' => array ( 'Tag' )
));
$result = $this -> Feed -> getFeedCoverage ( $feed [ 'Feed' ][ 'id' ], 'feed' , $this -> request -> data );
return $this -> RestResponse -> viewData ( $result , $this -> response -> type ());
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 importFeeds ()
{
if ( $this -> request -> is ( 'post' )) {
2019-10-02 16:41:03 +02:00
if ( isset ( $this -> request -> data [ 'Feed' ][ 'json' ])) {
$this -> request -> data = $this -> request -> data [ 'Feed' ][ 'json' ];
}
$results = $this -> Feed -> importFeeds ( $this -> request -> data , $this -> Auth -> user ());
2018-07-19 11:48:22 +02:00
if ( $results [ 'successes' ] > 0 ) {
$flashType = 'success' ;
$message = $results [ 'successes' ] . ' new feeds added.' ;
} else {
$flashType = 'info' ;
$message = 'No new feeds to add.' ;
}
if ( $results [ 'fails' ]) {
$message .= ' ' . $results [ 'fails' ] . ' feeds could not be added (possibly because they already exist)' ;
}
if ( $this -> _isRest ()) {
return $this -> RestResponse -> saveSuccessResponse ( 'Feed' , 'importFeeds' , false , $this -> response -> type (), $message );
} else {
$this -> Flash -> { $flashType }( $message );
$this -> redirect ( array ( 'controller' => 'Feeds' , 'action' => 'index' , 'all' ));
}
}
}
2016-12-31 09:04:46 +01:00
2018-07-19 11:48:22 +02:00
public function add ()
{
2021-06-17 17:14:22 +02:00
$params = [
'beforeSave' => function ( array $feed ) {
if ( $this -> _isRest ()) {
if ( empty ( $feed [ 'Feed' ][ 'source_format' ])) {
$feed [ 'Feed' ][ 'source_format' ] = 'freetext' ;
2018-07-19 11:48:22 +02:00
}
2021-06-17 17:14:22 +02:00
if ( empty ( $feed [ 'Feed' ][ 'fixed_event' ])) {
$feed [ 'Feed' ][ 'source_format' ] = 1 ;
2018-07-19 11:48:22 +02:00
}
}
2021-06-17 17:14:22 +02:00
if ( isset ( $feed [ 'Feed' ][ 'pull_rules' ])) {
$feed [ 'Feed' ][ 'rules' ] = $feed [ 'Feed' ][ 'pull_rules' ];
2020-04-17 14:17:54 +02:00
}
2021-06-17 17:14:22 +02:00
if ( ! isset ( $feed [ 'Feed' ][ 'distribution' ])) {
$feed [ 'Feed' ][ 'distribution' ] = 0 ;
2018-07-19 11:48:22 +02:00
}
2021-06-17 17:14:22 +02:00
if ( $feed [ 'Feed' ][ 'distribution' ] != 4 ) {
$feed [ 'Feed' ][ 'sharing_group_id' ] = 0 ;
2018-07-19 11:48:22 +02:00
}
2021-06-17 17:14:22 +02:00
$feed [ 'Feed' ][ 'default' ] = 0 ;
if ( ! isset ( $feed [ 'Feed' ][ 'source_format' ])) {
$feed [ 'Feed' ][ 'source_format' ] = 'freetext' ;
}
if ( ! empty ( $feed [ 'Feed' ][ 'source_format' ]) && ( $feed [ 'Feed' ][ 'source_format' ] == 'misp' )) {
if ( ! empty ( $feed [ 'Feed' ][ 'orgc_id' ])) {
$feed [ 'Feed' ][ 'orgc_id' ] = 0 ;
}
}
if ( $feed [ 'Feed' ][ 'source_format' ] == 'freetext' ) {
if ( $feed [ 'Feed' ][ 'fixed_event' ] == 1 ) {
if ( ! empty ( $feed [ 'Feed' ][ 'target_event' ]) && is_numeric ( $feed [ 'Feed' ][ 'target_event' ])) {
$feed [ 'Feed' ][ 'event_id' ] = $feed [ 'Feed' ][ 'target_event' ];
}
2018-07-19 11:48:22 +02:00
}
2021-06-17 17:14:22 +02:00
}
if ( ! isset ( $feed [ 'Feed' ][ 'settings' ])) {
$feed [ 'Feed' ][ 'settings' ] = array ();
2018-07-19 11:48:22 +02:00
} else {
2021-06-17 17:14:22 +02:00
if ( ! empty ( $feed [ 'Feed' ][ 'settings' ][ 'common' ][ 'excluderegex' ]) && ! $this -> __checkRegex ( $feed [ 'Feed' ][ 'settings' ][ 'common' ][ 'excluderegex' ])) {
$regexErrorMessage = __ ( 'Invalid exclude regex. Make sure it\'s a delimited PCRE regex pattern.' );
if ( ! $this -> _isRest ()) {
$this -> Flash -> error ( $regexErrorMessage );
return true ;
} else {
return new CakeResponse ( array (
'body' => json_encode ( array ( 'saved' => false , 'errors' => $regexErrorMessage )),
'status' => 200 ,
'type' => 'json'
));
}
2018-07-19 11:48:22 +02:00
}
}
2021-06-17 17:14:22 +02:00
if ( isset ( $feed [ 'Feed' ][ 'settings' ][ 'delimiter' ]) && empty ( $feed [ 'Feed' ][ 'settings' ][ 'delimiter' ])) {
$feed [ 'Feed' ][ 'settings' ][ 'delimiter' ] = ',' ;
}
if ( empty ( $feed [ 'Feed' ][ 'target_event' ])) {
$feed [ 'Feed' ][ 'target_event' ] = 0 ;
}
if ( empty ( $feed [ 'Feed' ][ 'lookup_visible' ])) {
$feed [ 'Feed' ][ 'lookup_visible' ] = 0 ;
}
if ( empty ( $feed [ 'Feed' ][ 'input_source' ])) {
$feed [ 'Feed' ][ 'input_source' ] = 'network' ;
} else {
$feed [ 'Feed' ][ 'input_source' ] = strtolower ( $feed [ 'Feed' ][ 'input_source' ]);
}
if ( ! in_array ( $feed [ 'Feed' ][ 'input_source' ], array ( 'network' , 'local' ))) {
$feed [ 'Feed' ][ 'input_source' ] = 'network' ;
}
if ( ! isset ( $feed [ 'Feed' ][ 'delete_local_file' ])) {
$feed [ 'Feed' ][ 'delete_local_file' ] = 0 ;
}
$feed [ 'Feed' ][ 'settings' ] = json_encode ( $feed [ 'Feed' ][ 'settings' ]);
$feed [ 'Feed' ][ 'event_id' ] = ! empty ( $feed [ 'Feed' ][ 'fixed_event' ]) ? $feed [ 'Feed' ][ 'target_event' ] : 0 ;
return $feed ;
2018-07-19 11:48:22 +02:00
}
2021-06-17 17:14:22 +02:00
];
$this -> CRUD -> add ( $params );
if ( $this -> IndexFilter -> isRest ()) {
return $this -> restResponsePayload ;
2018-07-19 11:48:22 +02:00
}
2021-06-17 17:14:22 +02:00
$this -> loadModel ( 'Event' );
$sharingGroups = $this -> Event -> SharingGroup -> fetchAllAuthorised ( $this -> Auth -> user (), 'name' , 1 );
$distributionLevels = $this -> Event -> distributionLevels ;
if ( empty ( $sharingGroups )) {
unset ( $distributionLevels [ 4 ]);
}
$inputSources = array ( 'network' => 'Network' );
if ( empty ( Configure :: read ( 'Security.disable_local_feed_access' ))) {
$inputSources [ 'local' ] = 'Local' ;
}
$tags = $this -> Event -> EventTag -> Tag -> find ( 'list' , array ( 'fields' => array ( 'Tag.name' ), 'order' => array ( 'lower(Tag.name) asc' )));
$tags [ 0 ] = 'None' ;
$dropdownData = [
'orgs' => $this -> Event -> Orgc -> find ( 'list' , array (
'fields' => array ( 'id' , 'name' ),
'order' => 'LOWER(name)'
)),
'tags' => $tags ,
'feedTypes' => $this -> Feed -> getFeedTypesOptions (),
'sharingGroups' => $sharingGroups ,
'distributionLevels' => $distributionLevels ,
'inputSources' => $inputSources
];
$this -> set ( compact ( 'dropdownData' ));
$this -> set ( 'menuData' , array ( 'menuList' => 'feeds' , 'menuItem' => 'add' ));
2018-07-19 11:48:22 +02:00
}
2017-01-01 16:25:08 +01:00
2018-07-19 11:48:22 +02:00
private function __checkRegex ( $pattern )
{
2021-06-17 17:14:22 +02:00
if ( @ preg_match ( $pattern , '' ) === false ) {
2018-07-19 11:48:22 +02:00
return false ;
}
return true ;
}
2017-01-01 16:25:08 +01:00
2018-07-19 11:48:22 +02:00
public function edit ( $feedId )
{
2021-06-17 17:14:22 +02:00
$this -> CRUD -> edit ( $feedId , [
'fields' => [
'name' ,
'provider' ,
'enabled' ,
'caching_enabled' ,
'rules' ,
'url' ,
'distribution' ,
'sharing_group_id' ,
'tag_id' ,
'fixed_event' ,
'event_id' ,
'publish' ,
'delta_merge' ,
'source_format' ,
'override_ids' ,
'settings' ,
'input_source' ,
'delete_local_file' ,
'lookup_visible' ,
'headers' ,
'orgc_id'
],
'beforeSave' => function ( array $feed ) use ( $feedId ) {
if ( ! empty ( $feed [ 'Feed' ][ 'settings' ])) {
$feed [ 'Feed' ][ 'settings' ] = json_decode ( $feed [ 'Feed' ][ 'settings' ], true );
2018-07-19 11:48:22 +02:00
}
2021-06-17 17:14:22 +02:00
if ( isset ( $feed [ 'Feed' ][ 'pull_rules' ])) {
$feed [ 'Feed' ][ 'rules' ] = $feed [ 'Feed' ][ 'pull_rules' ];
2020-04-17 14:17:54 +02:00
}
2021-06-17 17:14:22 +02:00
if ( isset ( $feed [ 'Feed' ][ 'distribution' ]) && $feed [ 'Feed' ][ 'distribution' ] != 4 ) {
$feed [ 'Feed' ][ 'sharing_group_id' ] = 0 ;
}
$feed [ 'Feed' ][ 'id' ] = $feedId ;
if ( ! empty ( $feed [ 'Feed' ][ 'source_format' ]) && ( $feed [ 'Feed' ][ 'source_format' ] == 'misp' )) {
if ( ! empty ( $feed [ 'Feed' ][ 'orgc_id' ])) {
$feed [ 'Feed' ][ 'orgc_id' ] = 0 ;
2018-07-19 11:48:22 +02:00
}
}
2021-06-17 17:14:22 +02:00
if ( ! empty ( $feed [ 'Feed' ][ 'source_format' ]) && ( $feed [ 'Feed' ][ 'source_format' ] == 'freetext' || $feed [ 'Feed' ][ 'source_format' ] == 'csv' )) {
if ( $feed [ 'Feed' ][ 'fixed_event' ] == 1 ) {
if ( isset ( $feed [ 'Feed' ][ 'target_event' ]) && is_numeric ( $feed [ 'Feed' ][ 'target_event' ])) {
$feed [ 'Feed' ][ 'event_id' ] = $feed [ 'Feed' ][ 'target_event' ];
} else if ( ! empty ( $feed [ 'Feed' ][ 'event_id' ])) {
$feed [ 'Feed' ][ 'event_id' ] = $feed [ 'Feed' ][ 'event_id' ];
} else {
$feed [ 'Feed' ][ 'event_id' ] = 0 ;
}
}
2020-05-14 15:50:08 +02:00
}
2021-06-17 17:14:22 +02:00
if ( ! isset ( $feed [ 'Feed' ][ 'settings' ])) {
if ( ! empty ( $feed [ 'Feed' ][ 'settings' ])) {
$feed [ 'Feed' ][ 'settings' ] = $feed [ 'Feed' ][ 'settings' ];
} else {
$feed [ 'Feed' ][ 'settings' ] = array ();
}
} else {
if ( ! empty ( $feed [ 'Feed' ][ 'settings' ][ 'common' ][ 'excluderegex' ]) && ! $this -> __checkRegex ( $feed [ 'Feed' ][ 'settings' ][ 'common' ][ 'excluderegex' ])) {
$regexErrorMessage = __ ( 'Invalid exclude regex. Make sure it\'s a delimited PCRE regex pattern.' );
if ( ! $this -> _isRest ()) {
$this -> Flash -> error ( $regexErrorMessage );
return true ;
} else {
return new CakeResponse ( array (
'body' => json_encode ( array ( 'saved' => false , 'errors' => $regexErrorMessage )),
'status' => 200 ,
'type' => 'json'
));
}
}
2018-07-19 11:48:22 +02:00
}
2021-06-17 17:14:22 +02:00
if ( isset ( $feed [ 'Feed' ][ 'settings' ][ 'delimiter' ]) && empty ( $feed [ 'Feed' ][ 'settings' ][ 'delimiter' ])) {
$feed [ 'Feed' ][ 'settings' ][ 'delimiter' ] = ',' ;
2018-07-19 11:48:22 +02:00
}
2021-06-17 17:14:22 +02:00
$feed [ 'Feed' ][ 'settings' ] = json_encode ( $feed [ 'Feed' ][ 'settings' ]);
return $feed ;
},
'afterSave' => function ( array $feed ) {
$feedCache = APP . 'tmp' . DS . 'cache' . DS . 'misp_feed_' . intval ( $feed [ 'Feed' ][ 'id' ]) . '.cache' ;
2018-07-19 11:48:22 +02:00
if ( file_exists ( $feedCache )) {
unlink ( $feedCache );
}
2021-06-17 17:14:22 +02:00
return $feed ;
2018-07-19 11:48:22 +02:00
}
2021-06-17 17:14:22 +02:00
]);
if ( $this -> IndexFilter -> isRest ()) {
return $this -> restResponsePayload ;
}
$this -> loadModel ( 'Event' );
$sharingGroups = $this -> Event -> SharingGroup -> fetchAllAuthorised ( $this -> Auth -> user (), 'name' , 1 );
$distributionLevels = $this -> Event -> distributionLevels ;
if ( empty ( $sharingGroups )) {
unset ( $distributionLevels [ 4 ]);
2018-07-19 11:48:22 +02:00
}
2021-06-17 17:14:22 +02:00
$inputSources = array ( 'network' => 'Network' );
if ( empty ( Configure :: read ( 'Security.disable_local_feed_access' ))) {
$inputSources [ 'local' ] = 'Local' ;
}
$tags = $this -> Event -> EventTag -> Tag -> find ( 'list' , array ( 'fields' => array ( 'Tag.name' ), 'order' => array ( 'lower(Tag.name) asc' )));
$tags [ 0 ] = 'None' ;
$dropdownData = [
'orgs' => $this -> Event -> Orgc -> find ( 'list' , array (
'fields' => array ( 'id' , 'name' ),
'order' => 'LOWER(name)'
)),
'tags' => $tags ,
'feedTypes' => $this -> Feed -> getFeedTypesOptions (),
'sharingGroups' => $sharingGroups ,
'distributionLevels' => $distributionLevels ,
'inputSources' => $inputSources
];
$this -> set ( compact ( 'dropdownData' ));
$this -> set ( 'menuData' , [
'menuList' => 'feeds' ,
'menuItem' => 'edit' ,
]);
$this -> set ( 'edit' , true );
$this -> render ( 'add' );
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 delete ( $feedId )
{
2021-06-17 17:14:22 +02:00
$this -> CRUD -> delete ( $feedId );
if ( $this -> IndexFilter -> isRest ()) {
return $this -> restResponsePayload ;
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 fetchFromFeed ( $feedId )
{
$this -> Feed -> id = $feedId ;
if ( ! $this -> Feed -> exists ()) {
throw new NotFoundException ( __ ( 'Invalid feed.' ));
}
$this -> Feed -> read ();
if ( ! empty ( $this -> Feed -> data [ 'Feed' ][ 'settings' ])) {
$this -> Feed -> data [ 'Feed' ][ 'settings' ] = json_decode ( $this -> Feed -> data [ 'Feed' ][ 'settings' ], true );
}
if ( ! $this -> Feed -> data [ 'Feed' ][ 'enabled' ]) {
2021-05-25 11:51:16 +02:00
if ( $this -> _isRest ()) {
return $this -> RestResponse -> viewData (
array ( 'result' => __ ( 'Feed is currently not enabled. Make sure you enable it.' )),
$this -> response -> type ()
);
} else {
$this -> Flash -> error ( __ ( 'Feed is currently not enabled. Make sure you enable it.' ));
$this -> redirect ( array ( 'action' => 'index' ));
}
2018-07-19 11:48:22 +02:00
}
if ( Configure :: read ( 'MISP.background_jobs' )) {
$this -> loadModel ( 'Job' );
$this -> Job -> create ();
$data = array (
2021-06-17 17:14:22 +02:00
'worker' => 'default' ,
'job_type' => 'fetch_feeds' ,
'job_input' => 'Feed: ' . $feedId ,
'status' => 0 ,
'retries' => 0 ,
'org' => $this -> Auth -> user ( 'Organisation' )[ 'name' ],
'message' => __ ( 'Starting fetch from Feed.' ),
2018-07-19 11:48:22 +02:00
);
$this -> Job -> save ( $data );
$jobId = $this -> Job -> id ;
$process_id = CakeResque :: enqueue (
2021-06-17 17:14:22 +02:00
'default' ,
'ServerShell' ,
array ( 'fetchFeed' , $this -> Auth -> user ( 'id' ), $feedId , $jobId ),
true
2018-07-19 11:48:22 +02:00
);
$this -> Job -> saveField ( 'process_id' , $process_id );
$message = __ ( 'Pull queued for background execution.' );
} else {
$result = $this -> Feed -> downloadFromFeedInitiator ( $feedId , $this -> Auth -> user ());
if ( ! $result ) {
if ( $this -> _isRest ()) {
return $this -> RestResponse -> viewData ( array ( 'result' => __ ( 'Fetching the feed has failed.' )), $this -> response -> type ());
} else {
$this -> Flash -> error ( __ ( 'Fetching the feed has failed.' ));
$this -> redirect ( array ( 'action' => 'index' ));
}
}
2019-04-29 15:12:55 +02:00
$message = __ ( 'Fetching the feed has successfully completed.' );
2018-07-19 11:48:22 +02:00
if ( $this -> Feed -> data [ 'Feed' ][ 'source_format' ] == 'misp' ) {
if ( isset ( $result [ 'add' ])) {
2018-12-12 17:30:58 +01:00
$message .= ' Downloaded ' . count ( $result [ 'add' ]) . ' new event(s).' ;
2018-07-19 11:48:22 +02:00
}
if ( isset ( $result [ 'edit' ])) {
2018-12-12 17:30:58 +01:00
$message .= ' Updated ' . count ( $result [ 'edit' ]) . ' event(s).' ;
2018-07-19 11:48:22 +02:00
}
}
}
if ( $this -> _isRest ()) {
return $this -> RestResponse -> viewData ( array ( 'result' => $message ), $this -> response -> type ());
} else {
$this -> Flash -> success ( $message );
$this -> redirect ( array ( 'action' => 'index' ));
}
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function fetchFromAllFeeds ()
{
$feeds = $this -> Feed -> find ( 'all' , array (
'recursive' => - 1 ,
'fields' => array ( 'id' )
));
foreach ( $feeds as $feed ) {
$feedId = $feed [ 'Feed' ][ 'id' ];
2017-08-16 18:08:30 +02:00
$this -> Feed -> id = $feedId ;
$this -> Feed -> read ();
if ( ! empty ( $this -> Feed -> data [ 'Feed' ][ 'settings' ])) {
$this -> Feed -> data [ 'Feed' ][ 'settings' ] = json_decode ( $this -> Feed -> data [ 'Feed' ][ 'settings' ], true );
}
if ( ! $this -> Feed -> data [ 'Feed' ][ 'enabled' ]) {
continue ;
}
if ( Configure :: read ( 'MISP.background_jobs' )) {
$this -> loadModel ( 'Job' );
$this -> Job -> create ();
$data = array (
'worker' => 'default' ,
'job_type' => 'fetch_feed' ,
'job_input' => 'Feed: ' . $feedId ,
'status' => 0 ,
'retries' => 0 ,
'org' => $this -> Auth -> user ( 'Organisation' )[ 'name' ],
2018-07-12 23:36:47 +02:00
'message' => __ ( 'Starting fetch from Feed.' ),
2017-08-16 18:08:30 +02:00
);
$this -> Job -> save ( $data );
$jobId = $this -> Job -> id ;
$process_id = CakeResque :: enqueue (
'default' ,
'ServerShell' ,
array ( 'fetchFeed' , $this -> Auth -> user ( 'id' ), $feedId , $jobId ),
true
);
$this -> Job -> saveField ( 'process_id' , $process_id );
$message = 'Pull queued for background execution.' ;
} else {
$result = $this -> Feed -> downloadFromFeedInitiator ( $feedId , $this -> Auth -> user ());
if ( ! $result ) {
continue ;
}
2018-07-12 23:36:47 +02:00
$message = __ ( 'Fetching the feed has successfully completed.' );
2017-08-16 18:08:30 +02:00
if ( $this -> Feed -> data [ 'Feed' ][ 'source_format' ] == 'misp' ) {
2018-07-19 11:48:22 +02:00
if ( isset ( $result [ 'add' ])) {
$message [ 'result' ] .= ' Downloaded ' . count ( $result [ 'add' ]) . ' new event(s).' ;
}
if ( isset ( $result [ 'edit' ])) {
$message [ 'result' ] .= ' Updated ' . count ( $result [ 'edit' ]) . ' event(s).' ;
}
2017-08-16 18:08:30 +02:00
}
}
}
2019-10-02 15:29:06 +02:00
if ( ! isset ( $message )) {
$message = __ ( 'No feed enabled.' );
}
2019-10-02 16:41:03 +02:00
if ( $this -> _isRest ()) {
return $this -> RestResponse -> viewData ( array ( 'result' => $message ), $this -> response -> type ());
} else {
$this -> Flash -> success ( $message );
$this -> redirect ( array ( 'action' => 'index' ));
}
2018-07-19 11:48:22 +02:00
}
2017-08-16 18:08:30 +02:00
2018-07-19 11:48:22 +02:00
public function getEvent ( $feedId , $eventUuid , $all = false )
{
$this -> Feed -> id = $feedId ;
if ( ! $this -> Feed -> exists ()) {
throw new NotFoundException ( __ ( 'Invalid feed.' ));
}
$this -> Feed -> read ();
if ( ! $this -> Feed -> data [ 'Feed' ][ 'enabled' ]) {
2020-06-26 15:52:49 +02:00
$this -> Flash -> error ( __ ( 'Feed is currently not enabled. Make sure you enable it.' ));
2018-07-19 11:48:22 +02:00
$this -> redirect ( array ( 'action' => 'previewIndex' , $feedId ));
}
2019-08-25 12:23:33 +02:00
try {
$result = $this -> Feed -> downloadAndSaveEventFromFeed ( $this -> Feed -> data , $eventUuid , $this -> Auth -> user ());
} catch ( Exception $e ) {
$this -> Flash -> error ( __ ( 'Download failed.' ) . ' ' . $e -> getMessage ());
$this -> redirect ( array ( 'action' => 'previewIndex' , $feedId ));
}
2018-07-19 11:48:22 +02:00
if ( isset ( $result [ 'action' ])) {
if ( $result [ 'result' ]) {
if ( $result [ 'action' ] == 'add' ) {
$this -> Flash -> success ( __ ( 'Event added.' ));
} else {
if ( $result [ 'result' ] === 'No change' ) {
$this -> Flash -> info ( __ ( 'Event already up to date.' ));
} else {
$this -> Flash -> success ( __ ( 'Event updated.' ));
}
}
} else {
$this -> Flash -> error ( __ ( 'Could not %s event.' , $result [ 'action' ]));
}
} else {
$this -> Flash -> error ( __ ( 'Download failed.' ));
}
$this -> redirect ( array ( 'action' => 'previewIndex' , $feedId ));
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function previewIndex ( $feedId )
{
2021-03-09 23:01:52 +01:00
$feed = $this -> Feed -> find ( 'first' , [
'conditions' => [ 'id' => $feedId ],
'recursive' => - 1 ,
]);
if ( empty ( $feed )) {
2018-07-19 11:48:22 +02:00
throw new NotFoundException ( __ ( 'Invalid feed.' ));
}
2021-03-09 23:01:52 +01:00
if ( ! empty ( $feed [ 'Feed' ][ 'settings' ])) {
$feed [ 'Feed' ][ 'settings' ] = json_decode ( $feed [ 'Feed' ][ 'settings' ], true );
2018-07-19 11:48:22 +02:00
}
$params = array ();
if ( $this -> request -> is ( 'post' )) {
$params = $this -> request -> data [ 'Feed' ];
}
2021-03-09 23:01:52 +01:00
if ( $feed [ 'Feed' ][ 'source_format' ] === 'misp' ) {
return $this -> __previewIndex ( $feed , $params );
} elseif ( in_array ( $feed [ 'Feed' ][ 'source_format' ], [ 'freetext' , 'csv' ], true )) {
return $this -> __previewFreetext ( $feed );
} else {
throw new Exception ( " Invalid feed format ` { $feed [ 'Feed' ][ 'source_format' ] } `. " );
2018-07-19 11:48:22 +02:00
}
}
2016-12-26 14:41:24 +01:00
2021-03-09 23:01:52 +01:00
private function __previewIndex ( array $feed , $filterParams = array ())
2018-07-19 11:48:22 +02:00
{
$urlparams = '' ;
App :: uses ( 'CustomPaginationTool' , 'Tools' );
$customPagination = new CustomPaginationTool ();
$passedArgs = array ();
App :: uses ( 'SyncTool' , 'Tools' );
$syncTool = new SyncTool ();
$HttpSocket = $syncTool -> setupHttpSocketFeed ( $feed );
2019-08-25 10:17:50 +02:00
try {
$events = $this -> Feed -> getManifest ( $feed , $HttpSocket );
} catch ( Exception $e ) {
$this -> Flash -> error ( " Could not fetch manifest for feed: { $e -> getMessage () } " );
2018-07-19 11:48:22 +02:00
$this -> redirect ( array ( 'controller' => 'feeds' , 'action' => 'index' ));
}
2019-08-25 10:17:50 +02:00
2019-02-23 12:57:00 +01:00
if ( ! empty ( $this -> params [ 'named' ][ 'searchall' ])) {
2020-07-27 09:37:08 +02:00
$searchAll = trim ( strtolower ( $this -> params [ 'named' ][ 'searchall' ]));
2019-02-23 12:57:00 +01:00
foreach ( $events as $uuid => $event ) {
$found = false ;
2020-07-27 09:37:08 +02:00
if ( $uuid === $searchAll ) {
2019-02-23 12:57:00 +01:00
$found = true ;
}
2020-07-27 09:37:08 +02:00
if ( ! $found && strpos ( strtolower ( $event [ 'info' ]), $searchAll ) !== false ) {
2019-02-23 12:57:00 +01:00
$found = true ;
}
2020-07-27 09:37:08 +02:00
if ( ! $found && strpos ( strtolower ( $event [ 'Orgc' ][ 'name' ]), $searchAll ) !== false ) {
$found = true ;
}
if ( ! $found && ! empty ( $event [ 'Tag' ])) {
2019-02-23 12:57:00 +01:00
foreach ( $event [ 'Tag' ] as $tag ) {
2020-07-27 09:37:08 +02:00
if ( strpos ( strtolower ( $tag [ 'name' ]), $searchAll ) !== false ) {
2019-02-23 12:57:00 +01:00
$found = true ;
}
}
}
if ( ! $found ) {
unset ( $events [ $uuid ]);
}
}
}
2018-07-19 11:48:22 +02:00
foreach ( $filterParams as $k => $filter ) {
if ( ! empty ( $filter )) {
$filterParams [ $k ] = json_decode ( $filter );
}
}
if ( ! empty ( $filterParams [ 'eventid' ])) {
foreach ( $events as $k => $event ) {
if ( ! in_array ( $k , $filterParams [ 'eventid' ])) {
unset ( $events [ $k ]);
continue ;
}
}
}
$params = $customPagination -> createPaginationRules ( $events , $this -> passedArgs , $this -> alias );
$this -> params -> params [ 'paging' ] = array ( $this -> modelClass => $params );
$events = $customPagination -> sortArray ( $events , $params , true );
if ( is_array ( $events )) {
$customPagination -> truncateByPagination ( $events , $params );
}
if ( $this -> _isRest ()) {
return $this -> RestResponse -> viewData ( $events , $this -> response -> type ());
}
$this -> set ( 'events' , $events );
$this -> loadModel ( 'Event' );
2021-03-30 09:38:41 +02:00
$this -> set ( 'threatLevels' , $this -> Event -> ThreatLevel -> listThreatLevels ());
2018-07-19 11:48:22 +02:00
$this -> set ( 'eventDescriptions' , $this -> Event -> fieldDescriptions );
$this -> set ( 'analysisLevels' , $this -> Event -> analysisLevels );
$this -> set ( 'distributionLevels' , $this -> Event -> distributionLevels );
$shortDist = array ( 0 => 'Organisation' , 1 => 'Community' , 2 => 'Connected' , 3 => 'All' , 4 => ' sharing Group' );
$this -> set ( 'shortDist' , $shortDist );
$this -> set ( 'id' , $feed [ 'Feed' ][ 'id' ]);
$this -> set ( 'feed' , $feed );
$this -> set ( 'urlparams' , $urlparams );
$this -> set ( 'passedArgs' , json_encode ( $passedArgs ));
$this -> set ( 'passedArgsArray' , $passedArgs );
}
2016-12-26 14:41:24 +01:00
2021-03-09 23:01:52 +01:00
private function __previewFreetext ( array $feed )
2018-07-19 11:48:22 +02:00
{
if ( isset ( $this -> passedArgs [ 'page' ])) {
$currentPage = $this -> passedArgs [ 'page' ];
} elseif ( isset ( $this -> passedArgs [ 'page' ])) {
$currentPage = $this -> passedArgs [ 'page' ];
} else {
$currentPage = 1 ;
}
$urlparams = '' ;
App :: uses ( 'SyncTool' , 'Tools' );
$syncTool = new SyncTool ();
if ( ! in_array ( $feed [ 'Feed' ][ 'source_format' ], array ( 'freetext' , 'csv' ))) {
throw new MethodNotAllowedException ( __ ( 'Invalid feed type.' ));
}
$HttpSocket = $syncTool -> setupHttpSocketFeed ( $feed );
$params = array ();
// params is passed as reference here, the pagination happens in the method, which isn't ideal but considering the performance gains here it's worth it
2019-08-25 11:22:36 +02:00
try {
$resultArray = $this -> Feed -> getFreetextFeed ( $feed , $HttpSocket , $feed [ 'Feed' ][ 'source_format' ], $currentPage , 60 , $params );
} catch ( Exception $e ) {
$this -> Flash -> error ( " Could not fetch feed: { $e -> getMessage () } " );
2018-07-19 11:48:22 +02:00
$this -> redirect ( array ( 'controller' => 'feeds' , 'action' => 'index' ));
}
$this -> params -> params [ 'paging' ] = array ( $this -> modelClass => $params );
$resultArray = $this -> Feed -> getFreetextFeedCorrelations ( $resultArray , $feed [ 'Feed' ][ 'id' ]);
// remove all duplicates
$correlatingEvents = array ();
foreach ( $resultArray as $k => $v ) {
if ( ! empty ( $resultArray [ $k ][ 'correlations' ])) {
foreach ( $resultArray [ $k ][ 'correlations' ] as $correlatingEvent ) {
if ( ! in_array ( $correlatingEvent , $correlatingEvents )) {
$correlatingEvents [] = $correlatingEvent ;
}
}
}
}
$resultArray = array_values ( $resultArray );
$this -> loadModel ( 'Attribute' );
$correlatingEventInfos = $this -> Attribute -> Event -> find ( 'list' , array (
'fields' => array ( 'Event.id' , 'Event.info' ),
'conditions' => array ( 'Event.id' => $correlatingEvents )
));
$this -> set ( 'correlatingEventInfos' , $correlatingEventInfos );
$this -> set ( 'distributionLevels' , $this -> Attribute -> distributionLevels );
$this -> set ( 'feed' , $feed );
if ( $this -> _isRest ()) {
return $this -> RestResponse -> viewData ( $resultArray , $this -> response -> type ());
}
$this -> set ( 'attributes' , $resultArray );
$this -> render ( 'freetext_index' );
}
2016-12-26 14:41:24 +01:00
2018-07-19 11:48:22 +02:00
public function previewEvent ( $feedId , $eventUuid , $all = false )
{
2021-03-09 23:01:52 +01:00
$feed = $this -> Feed -> find ( 'first' , [
'conditions' => [ 'id' => $feedId ],
'recursive' => - 1 ,
]);
if ( empty ( $feed )) {
2018-07-19 11:48:22 +02:00
throw new NotFoundException ( __ ( 'Invalid feed.' ));
}
2019-08-25 12:23:33 +02:00
try {
2021-03-09 23:01:52 +01:00
$event = $this -> Feed -> downloadEventFromFeed ( $feed , $eventUuid );
2019-08-25 12:23:33 +02:00
} catch ( Exception $e ) {
throw new Exception ( __ ( 'Could not download the selected Event' ), 0 , $e );
}
2018-07-19 11:48:22 +02:00
if ( $this -> _isRest ()) {
return $this -> RestResponse -> viewData ( $event , $this -> response -> type ());
}
if ( is_array ( $event )) {
2021-01-02 18:09:09 +01:00
if ( isset ( $event [ 'Event' ][ 'Attribute' ])) {
$this -> loadModel ( 'Warninglist' );
$this -> Warninglist -> attachWarninglistToAttributes ( $event [ 'Event' ][ 'Attribute' ]);
}
2020-10-01 12:11:45 +02:00
2018-07-19 11:48:22 +02:00
$this -> loadModel ( 'Event' );
$params = $this -> Event -> rearrangeEventForView ( $event , $this -> passedArgs , $all );
$this -> params -> params [ 'paging' ] = array ( 'Feed' => $params );
$this -> set ( 'event' , $event );
2021-03-09 23:01:52 +01:00
$this -> set ( 'feed' , $feed );
2018-07-19 11:48:22 +02:00
$this -> loadModel ( 'Event' );
$dataForView = array (
2021-03-09 23:01:52 +01:00
'Attribute' => array ( 'attrDescriptions' => 'fieldDescriptions' , 'distributionDescriptions' => 'distributionDescriptions' , 'distributionLevels' => 'distributionLevels' ),
'Event' => array ( 'eventDescriptions' => 'fieldDescriptions' , 'analysisLevels' => 'analysisLevels' )
2018-07-19 11:48:22 +02:00
);
foreach ( $dataForView as $m => $variables ) {
if ( $m === 'Event' ) {
$currentModel = $this -> Event ;
} elseif ( $m === 'Attribute' ) {
$currentModel = $this -> Event -> Attribute ;
}
foreach ( $variables as $alias => $variable ) {
$this -> set ( $alias , $currentModel -> { $variable });
}
}
2021-05-05 16:37:57 +02:00
$this -> set ( 'threatLevels' , $this -> Event -> ThreatLevel -> find ( 'list' ));
2018-07-19 11:48:22 +02:00
} else {
if ( $event === 'blocked' ) {
throw new MethodNotAllowedException ( __ ( 'This event is blocked by the Feed filters.' ));
} else {
throw new NotFoundException ( __ ( 'Could not download the selected Event' ));
}
}
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function enable ( $id )
{
$result = $this -> __toggleEnable ( $id , true );
$this -> set ( 'name' , $result [ 'message' ]);
$this -> set ( 'message' , $result [ 'message' ]);
$this -> set ( 'url' , $this -> here );
if ( $result ) {
$this -> set ( '_serialize' , array ( 'name' , 'message' , 'url' ));
} else {
$this -> set ( 'errors' , $result );
$this -> set ( '_serialize' , array ( 'name' , 'message' , 'url' , 'errors' ));
}
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function disable ( $id )
{
$result = $this -> __toggleEnable ( $id , false );
$this -> set ( 'name' , $result [ 'message' ]);
$this -> set ( 'message' , $result [ 'message' ]);
$this -> set ( 'url' , $this -> here );
if ( $result [ 'result' ]) {
$this -> set ( '_serialize' , array ( 'name' , 'message' , 'url' ));
} else {
$this -> set ( 'errors' , $result );
$this -> set ( '_serialize' , array ( 'name' , 'message' , 'url' , 'errors' ));
}
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
private function __toggleEnable ( $id , $enable = true )
{
if ( ! is_numeric ( $id )) {
throw new MethodNotAllowedException ( __ ( 'Invalid Feed.' ));
}
$this -> Feed -> id = $id ;
if ( ! $this -> Feed -> exists ()) {
throw new MethodNotAllowedException ( __ ( 'Invalid Feed.' ));
}
$feed = $this -> Feed -> find ( 'first' , array (
2021-06-17 17:14:22 +02:00
'conditions' => array ( 'Feed.id' => $id ),
'recursive' => - 1
2018-07-19 11:48:22 +02:00
));
$feed [ 'Feed' ][ 'enabled' ] = $enable ;
$result = array ( 'result' => $this -> Feed -> save ( $feed ));
$fail = false ;
if ( ! $result [ 'result' ]) {
$fail = true ;
$result [ 'result' ] = $this -> Feed -> validationErrors ;
}
$action = $enable ? 'enable' : 'disable' ;
$result [ 'message' ] = $fail ? 'Could not ' . $action . ' feed.' : 'Feed ' . $action . 'd.' ;
return $result ;
}
2016-12-26 14:41:24 +01:00
2018-07-19 11:48:22 +02:00
public function fetchSelectedFromFreetextIndex ( $id )
{
if ( ! $this -> request -> is ( 'Post' )) {
throw new MethodNotAllowedException ( __ ( 'Only POST requests are allowed.' ));
}
$this -> Feed -> id = $id ;
if ( ! $this -> Feed -> exists ()) {
throw new NotFoundException ( __ ( 'Feed not found.' ));
}
$feed = $this -> Feed -> read ();
if ( ! empty ( $feed [ 'Feed' ][ 'settings' ])) {
$feed [ 'Feed' ][ 'settings' ] = json_decode ( $feed [ 'Feed' ][ 'settings' ], true );
}
$data = json_decode ( $this -> request -> data [ 'Feed' ][ 'data' ], true );
2019-09-03 17:24:06 +02:00
try {
$this -> Feed -> saveFreetextFeedData ( $feed , $data , $this -> Auth -> user ());
2018-07-19 11:48:22 +02:00
$this -> Flash -> success ( __ ( 'Data pulled.' ));
2019-09-03 17:24:06 +02:00
} catch ( Exception $e ) {
$this -> Flash -> error ( __ ( 'Could not pull the selected data. Reason: %s' , $e -> getMessage ()));
2018-07-19 11:48:22 +02:00
}
$this -> redirect ( array ( 'controller' => 'feeds' , 'action' => 'index' ));
}
2017-05-08 14:22:27 +02:00
2018-07-19 11:48:22 +02:00
public function cacheFeeds ( $scope = 'freetext' )
{
if ( Configure :: read ( 'MISP.background_jobs' )) {
$this -> loadModel ( 'Job' );
$this -> Job -> create ();
$data = array (
2021-06-17 17:14:22 +02:00
'worker' => 'default' ,
'job_type' => 'cache_feeds' ,
'job_input' => $scope ,
'status' => 0 ,
'retries' => 0 ,
'org' => $this -> Auth -> user ( 'Organisation' )[ 'name' ],
'message' => __ ( 'Starting feed caching.' ),
2018-07-19 11:48:22 +02:00
);
$this -> Job -> save ( $data );
$jobId = $this -> Job -> id ;
$process_id = CakeResque :: enqueue (
2021-06-17 17:14:22 +02:00
'default' ,
'ServerShell' ,
array ( 'cacheFeed' , $this -> Auth -> user ( 'id' ), $scope , $jobId ),
true
2018-07-19 11:48:22 +02:00
);
$this -> Job -> saveField ( 'process_id' , $process_id );
$message = 'Feed caching job initiated.' ;
} else {
$result = $this -> Feed -> cacheFeedInitiator ( $this -> Auth -> user (), false , $scope );
2019-10-02 18:55:37 +02:00
if ( $result [ 'fails' ] > 0 ) {
2018-07-19 11:48:22 +02:00
$this -> Flash -> error ( __ ( 'Caching the feeds has failed.' ));
$this -> redirect ( array ( 'action' => 'index' ));
}
$message = __ ( 'Caching the feeds has successfully completed.' );
}
if ( $this -> _isRest ()) {
return $this -> RestResponse -> saveSuccessResponse ( 'Feed' , 'cacheFeed' , false , $this -> response -> type (), $message );
} else {
2019-08-23 19:40:20 +02:00
$this -> Flash -> info ( $message );
2018-07-19 11:48:22 +02:00
$this -> redirect ( array ( 'controller' => 'feeds' , 'action' => 'index' ));
}
}
2017-05-08 14:22:27 +02:00
2018-07-19 11:48:22 +02:00
public function compareFeeds ( $id = false )
{
$feeds = $this -> Feed -> compareFeeds ( $id );
if ( $this -> _isRest ()) {
return $this -> RestResponse -> viewData ( $feeds , $this -> response -> type ());
} else {
$this -> set ( 'feeds' , $feeds );
}
}
2018-01-15 17:25:11 +01:00
2018-07-19 11:48:22 +02:00
public function toggleSelected ( $enable = false , $cache = false , $feedList = false )
{
$field = $cache ? 'caching_enabled' : 'enabled' ;
if ( ! empty ( $enable )) {
$enable = 1 ;
} else {
$enable = 0 ;
}
try {
$feedIds = json_decode ( $feedList , true );
} catch ( Exception $e ) {
$this -> Flash -> error ( __ ( 'Invalid feed list received.' ));
$this -> redirect ( array ( 'controller' => 'feeds' , 'action' => 'index' ));
}
if ( $this -> request -> is ( 'post' )) {
$feeds = $this -> Feed -> find ( 'all' , array (
'conditions' => array ( 'Feed.id' => $feedIds ),
'recursive' => - 1
));
$count = 0 ;
foreach ( $feeds as $feed ) {
if ( $feed [ 'Feed' ][ $field ] != $enable ) {
$feed [ 'Feed' ][ $field ] = $enable ;
$this -> Feed -> save ( $feed );
$count ++ ;
}
}
if ( $count > 0 ) {
$this -> Flash -> success ( $count . ' feeds ' . array ( 'disabled' , 'enabled' )[ $enable ] . '.' );
$this -> redirect ( array ( 'controller' => 'feeds' , 'action' => 'index' ));
} else {
$this -> Flash -> info ( 'All selected feeds are already ' . array ( 'disabled' , 'enabled' )[ $enable ] . ', nothing to update.' );
$this -> redirect ( array ( 'controller' => 'feeds' , 'action' => 'index' ));
}
} else {
$this -> set ( 'feedList' , $feedList );
$this -> set ( 'enable' , $enable );
$this -> render ( 'ajax/feedToggleConfirmation' );
}
}
2019-04-01 16:09:24 +02:00
public function searchCaches ()
{
if ( isset ( $this -> passedArgs [ 'pages' ])) {
$currentPage = $this -> passedArgs [ 'pages' ];
} else {
$currentPage = 1 ;
}
$urlparams = '' ;
App :: uses ( 'CustomPaginationTool' , 'Tools' );
$customPagination = new CustomPaginationTool ();
$passedArgs = array ();
$hits = array ();
$value = false ;
if ( $this -> request -> is ( 'post' )) {
if ( isset ( $this -> request -> data [ 'Feed' ])) {
$this -> request -> data = $this -> request -> data [ 'Feed' ];
}
if ( isset ( $this -> request -> data [ 'value' ])) {
$this -> request -> data = $this -> request -> data [ 'value' ];
}
$value = $this -> request -> data ;
}
if ( ! empty ( $this -> params [ 'named' ][ 'value' ])) {
$value = $this -> params [ 'named' ][ 'value' ];
}
$hits = $this -> Feed -> searchCaches ( $value );
if ( $this -> _isRest ()) {
return $this -> RestResponse -> viewData ( $hits , $this -> response -> type ());
} else {
$this -> set ( 'hits' , $hits );
}
$params = $customPagination -> createPaginationRules ( $hits , $this -> passedArgs , $this -> alias );
$this -> params -> params [ 'paging' ] = array ( 'Feed' => $params );
$hits = $customPagination -> sortArray ( $hits , $params , true );
if ( is_array ( $hits )) {
$customPagination -> truncateByPagination ( $hits , $params );
}
$pageCount = count ( $hits );
$this -> set ( 'urlparams' , $urlparams );
$this -> set ( 'passedArgs' , json_encode ( $passedArgs ));
$this -> set ( 'passedArgsArray' , $passedArgs );
}
2016-06-06 10:09:55 +02:00
}