2019-11-06 21:16:50 +01:00
< ? php
/**
* Get filter parameters from index searches
*/
class IndexFilterComponent extends Component
{
2020-12-02 19:35:31 +01:00
/** @var Controller */
public $Controller ;
2024-01-09 15:56:30 +01:00
/** @var bool|null */
private $isRest = null ;
2019-11-06 21:16:50 +01:00
2022-09-22 14:48:50 +02:00
// Used for isApiFunction(), a check that returns true if the controller & action combo matches an action that is a non-xml and non-json automation method
// This is used to allow authentication via headers for methods not covered by _isRest() - as that only checks for JSON and XML formats
const AUTOMATION_ARRAY = array (
'events' => array ( 'csv' , 'nids' , 'hids' , 'xml' , 'restSearch' , 'stix' , 'updateGraph' , 'downloadOpenIOCEvent' ),
'attributes' => array ( 'text' , 'downloadAttachment' , 'returnAttributes' , 'restSearch' , 'rpz' , 'bro' ),
'objects' => array ( 'restSearch' ),
);
2022-05-06 20:56:40 +02:00
public function initialize ( Controller $controller )
{
2020-10-20 01:50:29 +02:00
$this -> Controller = $controller ;
2019-11-06 21:16:50 +01:00
}
// generic function to standardise on the collection of parameters. Accepts posted request objects, url params, named url params
2022-05-06 20:56:40 +02:00
public function harvestParameters ( $paramArray , & $exception = [])
2019-11-06 21:16:50 +01:00
{
2022-05-06 20:56:40 +02:00
$request = $this -> Controller -> request ;
$data = [];
if ( $request -> is ( 'post' )) {
if ( empty ( $request -> data )) {
2020-10-20 01:50:29 +02:00
$exception = $this -> Controller -> RestResponse -> throwException (
2019-11-06 21:16:50 +01:00
400 ,
__ ( 'Either specify the search terms in the url, or POST a json with the filter parameters.' ),
2022-05-06 20:56:40 +02:00
'/' . $request -> params [ 'controller' ] . '/' . $this -> Controller -> action
2019-11-06 21:16:50 +01:00
);
return false ;
} else {
2022-05-06 20:56:40 +02:00
if ( isset ( $request -> data [ 'request' ])) {
$data = $request -> data [ 'request' ];
2019-11-06 21:16:50 +01:00
} else {
2022-05-06 20:56:40 +02:00
$data = $request -> data ;
2019-11-06 21:16:50 +01:00
}
}
}
2022-12-22 13:11:57 +01:00
2022-12-22 15:35:30 +01:00
$data = $this -> __massageData ( $data , $request , $paramArray );
$this -> Controller -> set ( 'passedArgs' , json_encode ( $this -> Controller -> passedArgs ));
return $data ;
}
private function __massageData ( $data , $request , $paramArray )
{
$data = array_filter ( $data , function ( $paramName ) use ( $paramArray ) {
2023-09-13 08:18:04 +02:00
return in_array ( $paramName , $paramArray , true );
2022-12-22 15:35:30 +01:00
}, ARRAY_FILTER_USE_KEY );
2019-11-06 21:16:50 +01:00
if ( ! empty ( $paramArray )) {
foreach ( $paramArray as $p ) {
2022-05-06 20:56:40 +02:00
if ( isset ( $request -> params [ 'named' ][ $p ])) {
$data [ $p ] = str_replace ( ';' , ':' , $request -> params [ 'named' ][ $p ]);
2019-11-06 21:16:50 +01:00
}
}
}
2022-05-06 20:56:40 +02:00
foreach ( $data as & $v ) {
if ( is_string ( $v )) {
$v = trim ( $v );
if ( strpos ( $v , '||' )) {
$v = explode ( '||' , $v );
2019-11-06 21:16:50 +01:00
}
}
}
2022-05-06 20:56:40 +02:00
unset ( $v );
2019-11-06 21:16:50 +01:00
return $data ;
2022-12-22 15:35:30 +01:00
2019-11-06 21:16:50 +01:00
}
2020-10-20 01:50:29 +02:00
public function isRest ()
{
// This method is surprisingly slow and called many times for one request, so it make sense to cache the result.
if ( $this -> isRest !== null ) {
return $this -> isRest ;
}
$api = $this -> isApiFunction ( $this -> Controller -> request -> params [ 'controller' ], $this -> Controller -> request -> params [ 'action' ]);
2020-12-02 19:35:31 +01:00
if ( isset ( $this -> Controller -> RequestHandler ) && ( $api || $this -> isJson () || $this -> Controller -> RequestHandler -> isXml () || $this -> isCsv ())) {
2020-10-20 01:50:29 +02:00
$this -> isRest = true ;
return true ;
} else {
$this -> isRest = false ;
return false ;
}
}
2024-01-09 15:56:30 +01:00
public function isXhr ()
{
return $this -> Controller -> request -> header ( 'X-Requested-With' ) === 'XMLHttpRequest' ;
}
2021-01-01 22:17:57 +01:00
public function isJson ()
2020-10-20 01:50:29 +02:00
{
return $this -> Controller -> request -> header ( 'Accept' ) === 'application/json' || $this -> Controller -> RequestHandler -> prefers () === 'json' ;
}
public function isCsv ()
{
2021-06-02 15:26:20 +02:00
return $this -> Controller -> request -> header ( 'Accept' ) === 'text/csv' || $this -> Controller -> RequestHandler -> prefers () === 'csv' ;
2020-10-20 01:50:29 +02:00
}
2020-12-02 19:35:31 +01:00
/**
* @ param string $controller
* @ param string $action
* @ return bool
*/
2020-10-20 01:50:29 +02:00
public function isApiFunction ( $controller , $action )
{
2022-09-22 14:48:50 +02:00
return isset ( self :: AUTOMATION_ARRAY [ $controller ]) && in_array ( $action , self :: AUTOMATION_ARRAY [ $controller ], true );
2020-10-20 01:50:29 +02:00
}
2019-11-06 21:16:50 +01:00
}