2016-06-04 01:10:45 +02:00
< ? php
2013-11-07 15:58:29 +01:00
App :: uses ( 'Folder' , 'Utility' );
App :: uses ( 'File' , 'Utility' );
2013-11-06 10:52:18 +01:00
require_once 'AppShell.php' ;
class EventShell extends AppShell
{
2016-01-10 19:45:33 +01:00
public $uses = array ( 'Event' , 'Post' , 'Attribute' , 'Job' , 'User' , 'Task' , 'Whitelist' , 'Server' , 'Organisation' );
2015-06-23 17:19:08 +02:00
2013-11-06 10:52:18 +01:00
public function doPublish () {
$id = $this -> args [ 0 ];
$this -> Event -> id = $id ;
if ( ! $this -> Event -> exists ()) {
throw new NotFoundException ( __ ( 'Invalid event' ));
}
$this -> Job -> create ();
$data = array (
'worker' => 'default' ,
'job_type' => 'doPublish' ,
'job_input' => $id ,
'status' => 0 ,
'retries' => 0 ,
2013-11-07 15:58:29 +01:00
//'org' => $jobOrg,
2013-11-06 10:52:18 +01:00
'message' => 'Job created.' ,
);
$this -> Job -> save ( $data );
// update the event and set the from field to the current instance's organisation from the bootstrap. We also need to save id and info for the logs.
$this -> Event -> recursive = - 1 ;
$event = $this -> Event -> read ( null , $id );
$event [ 'Event' ][ 'published' ] = 1 ;
$fieldList = array ( 'published' , 'id' , 'info' );
$this -> Event -> save ( $event , array ( 'fieldList' => $fieldList ));
// only allow form submit CSRF protection.
$this -> Job -> saveField ( 'status' , 1 );
$this -> Job -> saveField ( 'message' , 'Job done.' );
}
2016-06-04 01:08:16 +02:00
2013-11-07 15:58:29 +01:00
public function cachexml () {
2016-07-01 14:52:02 +02:00
$timeStart = time ();
2016-05-20 19:00:03 +02:00
$userId = $this -> args [ 0 ];
2015-04-13 12:42:26 +02:00
$id = $this -> args [ 1 ];
2016-05-20 19:00:03 +02:00
$user = $this -> User -> getAuthUser ( $userId );
2013-11-15 15:39:34 +01:00
$this -> Job -> id = $id ;
2015-04-13 12:42:26 +02:00
// TEMP: change to passing an options array with the user!!
$eventIds = $this -> Event -> fetchEventIds ( $user );
2013-11-07 15:58:29 +01:00
$eventCount = count ( $eventIds );
2015-12-28 00:03:52 +01:00
$dir = new Folder ( APP . 'tmp/cached_exports/xml' , true , 0750 );
2015-04-18 07:53:18 +02:00
if ( $user [ 'Role' ][ 'perm_site_admin' ]) {
2014-06-24 13:19:40 +02:00
$file = new File ( $dir -> pwd () . DS . 'misp.xml' . '.ADMIN.xml' );
} else {
2015-04-18 07:53:18 +02:00
$file = new File ( $dir -> pwd () . DS . 'misp.xml' . '.' . $user [ 'Organisation' ][ 'name' ] . '.xml' );
2014-06-24 13:19:40 +02:00
}
2015-04-18 07:53:18 +02:00
App :: uses ( 'XMLConverterTool' , 'Tools' );
$converter = new XMLConverterTool ();
2014-09-02 15:56:28 +02:00
$file -> write ( '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL . '<response>' );
2016-01-10 19:45:33 +01:00
if ( ! empty ( $eventIds )) {
foreach ( $eventIds as $k => $eventId ) {
2016-07-30 18:08:19 +02:00
$temp = $this -> Event -> fetchEvent ( $user , array ( 'eventid' => $eventId [ 'Event' ][ 'id' ], 'includeAttachments' => Configure :: read ( 'MISP.cached_attachments' )));
2017-03-31 19:27:34 +02:00
$file -> append ( $converter -> convert ( $temp [ 0 ], $user [ 'Role' ][ 'perm_site_admin' ]) . PHP_EOL );
2016-01-10 19:45:33 +01:00
$this -> Job -> saveField ( 'progress' , ( $k + 1 ) / $eventCount * 100 );
}
2013-12-04 11:58:01 +01:00
}
2014-09-02 15:56:28 +02:00
$file -> append ( '<xml_version>' . $this -> Event -> mispVersion . '</xml_version>' );
$file -> append ( '</response>' . PHP_EOL );
2013-11-07 15:58:29 +01:00
$file -> close ();
2016-07-01 14:52:02 +02:00
$timeDelta = ( time () - $timeStart );
$this -> Job -> saveField ( 'progress' , 100 );
$this -> Job -> saveField ( 'message' , 'Job done. (in ' . $timeDelta . 's)' );
$this -> Job -> saveField ( 'date_modified' , date ( " y-m-d H:i:s " ));
2013-11-15 15:39:34 +01:00
}
2016-08-17 09:43:57 +02:00
2016-07-30 18:08:19 +02:00
public function cachejson () {
$timeStart = time ();
$userId = $this -> args [ 0 ];
$id = $this -> args [ 1 ];
$user = $this -> User -> getAuthUser ( $userId );
$this -> Job -> id = $id ;
// TEMP: change to passing an options array with the user!!
$eventIds = $this -> Event -> fetchEventIds ( $user );
$eventCount = count ( $eventIds );
$dir = new Folder ( APP . 'tmp/cached_exports/json' , true , 0750 );
if ( $user [ 'Role' ][ 'perm_site_admin' ]) {
$file = new File ( $dir -> pwd () . DS . 'misp.json' . '.ADMIN.json' );
} else {
$file = new File ( $dir -> pwd () . DS . 'misp.json' . '.' . $user [ 'Organisation' ][ 'name' ] . '.json' );
}
App :: uses ( 'JSONConverterTool' , 'Tools' );
$converter = new JSONConverterTool ();
2016-08-16 14:49:52 +02:00
$file -> write ( '{"response":[' );
2016-07-30 18:08:19 +02:00
foreach ( $eventIds as $k => $eventId ) {
$result = $this -> Event -> fetchEvent ( $user , array ( 'eventid' => $eventId [ 'Event' ][ 'id' ], 'includeAttachments' => Configure :: read ( 'MISP.cached_attachments' )));
2017-03-31 19:27:34 +02:00
$file -> append ( $converter -> convert ( $result [ 0 ]));
2016-07-30 18:08:19 +02:00
if ( $k < count ( $eventIds ) - 1 ) $file -> append ( ',' );
2016-07-30 19:00:02 +02:00
$this -> Job -> saveField ( 'progress' , ( $k + 1 ) / $eventCount * 100 );
2016-07-30 18:08:19 +02:00
}
$file -> append ( ']}' );
$file -> close ();
$timeDelta = ( time () - $timeStart );
$this -> Job -> saveField ( 'progress' , 100 );
$this -> Job -> saveField ( 'message' , 'Job done. (in ' . $timeDelta . 's)' );
$this -> Job -> saveField ( 'date_modified' , date ( " y-m-d H:i:s " ));
}
2016-06-04 01:08:16 +02:00
2016-08-21 22:57:11 +02:00
public function cachestix () {
$timeStart = time ();
$userId = $this -> args [ 0 ];
$id = $this -> args [ 1 ];
$user = $this -> User -> getAuthUser ( $userId );
$this -> Job -> id = $id ;
$dir = new Folder ( APP . 'tmp/cached_exports/stix' , true , 0750 );
if ( $user [ 'Role' ][ 'perm_site_admin' ]) {
2016-11-01 17:57:56 +01:00
$stixFilePath = $dir -> pwd () . DS . 'misp.stix' . '.ADMIN.xml' ;
2016-08-21 22:57:11 +02:00
} else {
2016-11-01 17:57:56 +01:00
$stixFilePath = $dir -> pwd () . DS . 'misp.stix' . '.' . $user [ 'Organisation' ][ 'name' ] . '.xml' ;
2016-08-21 22:57:11 +02:00
}
2016-11-01 17:57:56 +01:00
$result = $this -> Event -> stix ( false , false , Configure :: read ( 'MISP.cached_attachments' ), $user , 'xml' , false , false , false , $id , true );
2016-08-21 22:57:11 +02:00
$timeDelta = ( time () - $timeStart );
$this -> Job -> saveField ( 'date_modified' , date ( " y-m-d H:i:s " ));
2016-08-23 00:24:54 +02:00
if ( $result [ 'success' ]) {
2016-11-01 17:57:56 +01:00
rename ( $result [ 'data' ], $stixFilePath );
unlink ( $result [ 'data' ]);
2016-08-23 00:24:54 +02:00
$this -> Job -> saveField ( 'progress' , 100 );
$this -> Job -> saveField ( 'message' , 'Job done. (in ' . $timeDelta . 's)' );
} else {
$log = ClassRegistry :: init ( 'Log' );
$log -> create ();
$log -> createLogEntry ( $user , 'export' , 'STIX export failed' , $result [ 'message' ]);
throw new InternalErrorException ();
}
2016-08-21 22:57:11 +02:00
}
2014-09-02 15:56:28 +02:00
private function __recursiveEcho ( $array ) {
$text = " " ;
foreach ( $array as $k => $v ) {
if ( is_array ( $v )) {
if ( empty ( $v )) $text .= '<' . $k . '/>' ;
else {
foreach ( $v as $element ) {
$text .= '<' . $k . '>' ;
$text .= $this -> __recursiveEcho ( $element );
$text .= '</' . $k . '>' ;
}
}
} else {
if ( $v === false ) $v = 0 ;
if ( $v === " " || $v === null ) $text .= '<' . $k . '/>' ;
else {
$text .= '<' . $k . '>' . $v . '</' . $k . '>' ;
}
}
}
return $text ;
}
2016-06-04 01:08:16 +02:00
2013-11-15 15:39:34 +01:00
public function cachehids () {
2016-07-01 14:52:02 +02:00
$timeStart = time ();
2016-05-20 19:00:03 +02:00
$userId = $this -> args [ 0 ];
$user = $this -> User -> getAuthUser ( $userId );
2015-04-13 12:42:26 +02:00
$id = $this -> args [ 1 ];
2013-11-15 15:39:34 +01:00
$this -> Job -> id = $id ;
2015-04-13 12:42:26 +02:00
$extra = $this -> args [ 2 ];
2013-11-15 15:39:34 +01:00
$this -> Job -> saveField ( 'progress' , 1 );
2016-07-30 18:08:19 +02:00
$rules = $this -> Attribute -> hids ( $user , $extra , '' , false , false , false , $id );
2013-11-15 15:39:34 +01:00
$this -> Job -> saveField ( 'progress' , 80 );
2015-12-28 00:03:52 +01:00
$dir = new Folder ( APP . DS . '/tmp/cached_exports/' . $extra , true , 0750 );
2015-04-13 12:42:26 +02:00
if ( $user [ 'Role' ][ 'perm_site_admin' ]) {
2013-12-04 11:58:01 +01:00
$file = new File ( $dir -> pwd () . DS . 'misp.' . $extra . '.ADMIN.txt' );
} else {
2015-04-20 11:46:55 +02:00
$file = new File ( $dir -> pwd () . DS . 'misp.' . $extra . '.' . $user [ 'Organisation' ][ 'name' ] . '.txt' );
2013-12-04 11:58:01 +01:00
}
2013-11-15 15:39:34 +01:00
$file -> write ( '' );
2016-07-30 18:08:19 +02:00
if ( ! empty ( $rules )) {
foreach ( $rules as $rule ) {
2016-07-31 22:56:19 +02:00
$file -> append ( $rule . PHP_EOL );
2016-07-30 18:08:19 +02:00
}
} else {
2016-07-31 22:56:19 +02:00
$file -> append ( " No exportable " . $type . " s found. " . PHP_EOL );
2013-11-15 15:39:34 +01:00
}
$file -> close ();
2016-07-01 14:52:02 +02:00
$timeDelta = ( time () - $timeStart );
2013-11-15 15:39:34 +01:00
$this -> Job -> saveField ( 'progress' , '100' );
2016-07-01 14:52:02 +02:00
$this -> Job -> saveField ( 'message' , 'Job done. (in ' . $timeDelta . 's)' );
2016-06-29 22:46:48 +02:00
$this -> Job -> saveField ( 'date_modified' , date ( " y-m-d H:i:s " ));
2013-11-15 15:39:34 +01:00
}
2016-06-04 01:08:16 +02:00
2015-05-20 16:23:42 +02:00
public function cacherpz () {
2016-07-01 14:52:02 +02:00
$timeStart = time ();
2016-05-20 19:00:03 +02:00
$userId = $this -> args [ 0 ];
$user = $this -> User -> getAuthUser ( $userId );
2015-11-17 22:01:22 +01:00
$id = $this -> args [ 1 ];
2015-05-20 16:23:42 +02:00
$this -> Job -> id = $id ;
2015-11-17 22:01:22 +01:00
$extra = $this -> args [ 2 ];
2015-05-20 16:23:42 +02:00
$this -> Job -> saveField ( 'progress' , 1 );
2015-12-26 02:50:50 +01:00
$eventIds = $this -> Attribute -> Event -> fetchEventIds ( $user , false , false , false , true );
$values = array ();
$eventCount = count ( $eventIds );
if ( $eventCount ) {
foreach ( $eventIds as $k => $eventId ) {
$values = array_merge_recursive ( $values , $this -> Attribute -> rpz ( $user , false , $eventId ));
if ( $k % 10 == 0 ) $this -> Job -> saveField ( 'progress' , $k * 80 / $eventCount );
}
}
2015-05-20 16:23:42 +02:00
$this -> Job -> saveField ( 'progress' , 80 );
2015-12-28 00:03:52 +01:00
$dir = new Folder ( APP . DS . '/tmp/cached_exports/' . $extra , true , 0750 );
2015-12-26 01:35:34 +01:00
if ( $user [ 'Role' ][ 'perm_site_admin' ]) {
2015-05-20 16:23:42 +02:00
$file = new File ( $dir -> pwd () . DS . 'misp.rpz.ADMIN.txt' );
} else {
2015-12-26 01:35:34 +01:00
$file = new File ( $dir -> pwd () . DS . 'misp.rpz.' . $user [ 'Organisation' ][ 'name' ] . '.txt' );
2015-05-20 16:23:42 +02:00
}
App :: uses ( 'RPZExport' , 'Export' );
$rpzExport = new RPZExport ();
$rpzSettings = array ();
2017-09-21 17:11:30 +02:00
$lookupData = array ( 'policy' , 'walled_garden' , 'ns' , 'email' , 'serial' , 'refresh' , 'retry' , 'expiry' , 'minimum_ttl' , 'ttl' , 'ns_alt' );
2015-05-20 16:23:42 +02:00
foreach ( $lookupData as $v ) {
$tempSetting = Configure :: read ( 'Plugin.RPZ_' . $v );
if ( isset ( $tempSetting )) $rpzSettings [ $v ] = Configure :: read ( 'Plugin.RPZ_' . $v );
else $rpzSettings [ $v ] = $this -> Server -> serverSettings [ 'Plugin' ][ 'RPZ_' . $v ][ 'value' ];
}
$file -> write ( $rpzExport -> export ( $values , $rpzSettings ));
$file -> close ();
2016-07-01 14:52:02 +02:00
$timeDelta = ( time () - $timeStart );
2015-05-20 16:23:42 +02:00
$this -> Job -> saveField ( 'progress' , '100' );
2016-07-01 14:52:02 +02:00
$this -> Job -> saveField ( 'message' , 'Job done. (in ' . $timeDelta . 's)' );
2016-06-29 22:46:48 +02:00
$this -> Job -> saveField ( 'date_modified' , date ( " y-m-d H:i:s " ));
2015-05-20 16:23:42 +02:00
}
2016-06-04 01:08:16 +02:00
2013-11-15 15:39:34 +01:00
public function cachecsv () {
2016-07-01 14:52:02 +02:00
$timeStart = time ();
2016-05-20 19:00:03 +02:00
$userId = $this -> args [ 0 ];
$user = $this -> User -> getAuthUser ( $userId );
2015-04-13 12:42:26 +02:00
$id = $this -> args [ 1 ];
2013-11-15 15:39:34 +01:00
$this -> Job -> id = $id ;
2015-04-13 12:42:26 +02:00
$extra = $this -> args [ 2 ];
2014-03-24 16:33:40 +01:00
if ( $extra == 'csv_all' ) $ignore = 1 ;
else $ignore = 0 ;
2015-04-13 12:42:26 +02:00
// TEMP: change to passing an options array with the user!!
$eventIds = $this -> Event -> fetchEventIds ( $user );
2013-11-15 15:39:34 +01:00
$eventCount = count ( $eventIds );
2014-09-02 15:56:28 +02:00
$attributes = array ();
2015-12-28 00:03:52 +01:00
$dir = new Folder ( APP . 'tmp/cached_exports/' . $extra , true , 0750 );
2015-04-13 12:42:26 +02:00
if ( $user [ 'Role' ][ 'perm_site_admin' ]) {
2013-12-04 11:58:01 +01:00
$file = new File ( $dir -> pwd () . DS . 'misp.' . $extra . '.ADMIN.csv' );
} else {
2015-04-13 12:42:26 +02:00
$file = new File ( $dir -> pwd () . DS . 'misp.' . $extra . '.' . $user [ 'Organisation' ][ 'name' ] . '.csv' );
2013-12-04 11:58:01 +01:00
}
2014-11-12 15:36:07 +01:00
$file -> write ( 'uuid,event_id,category,type,value,to_ids,date' . PHP_EOL );
2014-09-02 15:56:28 +02:00
foreach ( $eventIds as $k => $eventId ) {
2015-04-08 22:47:28 +02:00
$chunk = " " ;
2015-04-13 12:42:26 +02:00
$attributes = $this -> Event -> csv ( $user , $eventId [ 'Event' ][ 'id' ], $ignore );
2014-09-02 15:56:28 +02:00
$attributes = $this -> Whitelist -> removeWhitelistedFromArray ( $attributes , true );
foreach ( $attributes as $attribute ) {
2015-04-08 22:47:28 +02:00
$chunk .= $attribute [ 'Attribute' ][ 'uuid' ] . ',' . $attribute [ 'Attribute' ][ 'event_id' ] . ',' . $attribute [ 'Attribute' ][ 'category' ] . ',' . $attribute [ 'Attribute' ][ 'type' ] . ',' . $attribute [ 'Attribute' ][ 'value' ] . ',' . intval ( $attribute [ 'Attribute' ][ 'to_ids' ]) . ',' . $attribute [ 'Attribute' ][ 'timestamp' ] . PHP_EOL ;
2014-09-02 15:56:28 +02:00
}
2015-04-08 22:47:28 +02:00
$file -> append ( $chunk );
2014-09-02 15:56:28 +02:00
if ( $k % 10 == 0 ) {
$this -> Job -> saveField ( 'progress' , $k / $eventCount * 80 );
}
2013-11-15 15:39:34 +01:00
}
$file -> close ();
2016-07-01 14:52:02 +02:00
$timeDelta = ( time () - $timeStart );
2013-11-15 15:39:34 +01:00
$this -> Job -> saveField ( 'progress' , '100' );
2016-07-01 14:52:02 +02:00
$this -> Job -> saveField ( 'message' , 'Job done. (in ' . $timeDelta . 's)' );
2016-06-29 22:46:48 +02:00
$this -> Job -> saveField ( 'date_modified' , date ( " y-m-d H:i:s " ));
2013-11-15 15:39:34 +01:00
}
2016-06-04 01:08:16 +02:00
2013-11-15 15:39:34 +01:00
public function cachetext () {
2016-07-01 14:52:02 +02:00
$timeStart = time ();
2016-05-20 19:00:03 +02:00
$userId = $this -> args [ 0 ];
$user = $this -> User -> getAuthUser ( $userId );
2015-04-13 12:42:26 +02:00
$id = $this -> args [ 1 ];
2013-11-15 15:39:34 +01:00
$this -> Job -> id = $id ;
$types = array_keys ( $this -> Attribute -> typeDefinitions );
$typeCount = count ( $types );
2015-12-28 00:03:52 +01:00
$dir = new Folder ( APP . DS . '/tmp/cached_exports/text' , true , 0750 );
2013-11-15 15:39:34 +01:00
foreach ( $types as $k => $type ) {
2015-04-13 12:42:26 +02:00
$final = $this -> Attribute -> text ( $user , $type );
if ( $user [ 'Role' ][ 'perm_site_admin' ]) {
2013-12-04 11:58:01 +01:00
$file = new File ( $dir -> pwd () . DS . 'misp.text_' . $type . '.ADMIN.txt' );
} else {
2015-04-13 12:42:26 +02:00
$file = new File ( $dir -> pwd () . DS . 'misp.text_' . $type . '.' . $user [ 'Organisation' ][ 'name' ] . '.txt' );
2013-12-04 11:58:01 +01:00
}
2013-11-15 15:39:34 +01:00
$file -> write ( '' );
foreach ( $final as $attribute ) {
$file -> append ( $attribute [ 'Attribute' ][ 'value' ] . PHP_EOL );
}
$file -> close ();
2013-11-19 11:03:30 +01:00
$this -> Job -> saveField ( 'progress' , $k / $typeCount * 100 );
2013-11-15 15:39:34 +01:00
}
2016-07-01 14:52:02 +02:00
$timeDelta = ( time () - $timeStart );
2013-11-19 11:03:30 +01:00
$this -> Job -> saveField ( 'progress' , 100 );
2016-07-01 14:52:02 +02:00
$this -> Job -> saveField ( 'message' , 'Job done. (in ' . $timeDelta . 's)' );
2016-06-29 22:46:48 +02:00
$this -> Job -> saveField ( 'date_modified' , date ( " y-m-d H:i:s " ));
2013-11-19 11:03:30 +01:00
}
2016-06-04 01:08:16 +02:00
2013-11-19 11:03:30 +01:00
public function cachenids () {
2016-07-01 14:52:02 +02:00
$timeStart = time ();
2016-05-20 19:00:03 +02:00
$userId = $this -> args [ 0 ];
$user = $this -> User -> getAuthUser ( $userId );
2015-04-13 12:42:26 +02:00
$id = $this -> args [ 1 ];
2013-11-19 11:03:30 +01:00
$this -> Job -> id = $id ;
2015-04-13 12:42:26 +02:00
$format = $this -> args [ 2 ];
2015-12-23 00:51:57 +01:00
$eventIds = array_values ( $this -> Event -> fetchEventIds ( $user , false , false , false , true ));
2013-11-19 11:03:30 +01:00
$eventCount = count ( $eventIds );
2015-12-28 00:03:52 +01:00
$dir = new Folder ( APP . DS . '/tmp/cached_exports/' . $format , true , 0750 );
2015-04-13 12:42:26 +02:00
if ( $user [ 'Role' ][ 'perm_site_admin' ]) {
2013-12-04 11:58:01 +01:00
$file = new File ( $dir -> pwd () . DS . 'misp.' . $format . '.ADMIN.rules' );
} else {
2015-04-13 12:42:26 +02:00
$file = new File ( $dir -> pwd () . DS . 'misp.' . $format . '.' . $user [ 'Organisation' ][ 'name' ] . '.rules' );
2013-12-04 11:58:01 +01:00
}
2013-11-19 11:03:30 +01:00
$file -> write ( '' );
foreach ( $eventIds as $k => $eventId ) {
if ( $k == 0 ) {
2015-12-23 00:51:57 +01:00
$temp = $this -> Attribute -> nids ( $user , $format , $eventId );
2013-11-19 11:03:30 +01:00
} else {
2015-12-23 00:51:57 +01:00
$temp = $this -> Attribute -> nids ( $user , $format , $eventId , true );
2013-11-19 11:03:30 +01:00
}
foreach ( $temp as $line ) {
$file -> append ( $line . PHP_EOL );
}
if ( $k % 10 == 0 ) {
$this -> Job -> saveField ( 'progress' , $k / $eventCount * 80 );
}
}
$file -> close ();
2016-07-01 14:52:02 +02:00
$timeDelta = time () - $timeStart ;
2013-11-15 15:39:34 +01:00
$this -> Job -> saveField ( 'progress' , '100' );
2016-07-01 14:52:02 +02:00
$this -> Job -> saveField ( 'message' , 'Job done. (in ' . $timeDelta . 's)' );
2016-06-29 22:46:48 +02:00
$this -> Job -> saveField ( 'date_modified' , date ( " y-m-d H:i:s " ));
2013-11-07 15:58:29 +01:00
}
2016-06-04 01:08:16 +02:00
2016-08-23 14:06:06 +02:00
public function cachebro ()
{
2016-12-07 16:33:17 +01:00
$timeStart = time ();
$broHeader = " #fields \t indicator \t indicator_type \t meta.source \t meta.desc \t meta.url \t meta.do_notice \t meta.if_in \n " ;
2016-08-23 14:06:06 +02:00
$userId = $this -> args [ 0 ];
$user = $this -> User -> getAuthUser ( $userId );
$id = $this -> args [ 1 ];
$this -> Job -> id = $id ;
$this -> Job -> saveField ( 'progress' , 1 );
2016-12-07 16:33:17 +01:00
App :: uses ( 'BroExport' , 'Export' );
$export = new BroExport ();
$types = array_keys ( $export -> mispTypes );
2016-08-23 14:06:06 +02:00
$typeCount = count ( $types );
2016-12-07 16:33:17 +01:00
$dir = new Folder ( APP . DS . '/tmp/cached_exports/bro' , true , 0750 );
2016-08-23 14:06:06 +02:00
if ( $user [ 'Role' ][ 'perm_site_admin' ]) {
2016-12-21 19:10:55 +01:00
$file = new File ( $dir -> pwd () . DS . 'misp.bro.ADMIN.intel' );
2016-08-23 14:06:06 +02:00
} else {
2016-12-21 19:10:55 +01:00
$file = new File ( $dir -> pwd () . DS . 'misp.bro.' . $user [ 'Organisation' ][ 'name' ] . '.intel' );
2016-12-07 16:33:17 +01:00
}
2017-01-23 13:32:06 +01:00
$file -> write ( '' );
2016-12-07 16:33:17 +01:00
foreach ( $types as $k => $type ) {
$final = $this -> Attribute -> bro ( $user , $type );
foreach ( $final as $attribute ) {
$file -> append ( $attribute . PHP_EOL );
}
$this -> Job -> saveField ( 'progress' , $k / $typeCount * 100 );
2016-08-23 14:06:06 +02:00
}
2016-12-07 16:33:17 +01:00
$file -> close ();
$timeDelta = ( time () - $timeStart );
2016-08-23 14:06:06 +02:00
$this -> Job -> saveField ( 'progress' , 100 );
2016-12-07 16:33:17 +01:00
$this -> Job -> saveField ( 'message' , 'Job done. (in ' . $timeDelta . 's)' );
$this -> Job -> saveField ( 'date_modified' , date ( " y-m-d H:i:s " ));
2016-08-23 14:06:06 +02:00
}
2013-12-04 11:58:01 +01:00
public function alertemail () {
2015-04-20 11:46:55 +02:00
$userId = $this -> args [ 0 ];
2014-01-06 05:15:47 +01:00
$processId = $this -> args [ 1 ];
2014-02-10 00:29:46 +01:00
$job = $this -> Job -> read ( null , $processId );
2014-01-06 05:15:47 +01:00
$eventId = $this -> args [ 2 ];
2016-10-02 14:46:51 +02:00
$oldpublish = $this -> args [ 3 ];
2015-04-20 11:46:55 +02:00
$user = $this -> User -> getAuthUser ( $userId );
2016-10-02 14:46:51 +02:00
$result = $this -> Event -> sendAlertEmail ( $eventId , $user , $oldpublish , $processId );
2014-02-10 00:29:46 +01:00
$job [ 'Job' ][ 'progress' ] = 100 ;
$job [ 'Job' ][ 'message' ] = 'Emails sent.' ;
2016-06-29 22:46:48 +02:00
//$job['Job']['date_modified'] = date("y-m-d H:i:s");
2014-02-10 00:29:46 +01:00
$this -> Job -> save ( $job );
2014-01-06 05:15:47 +01:00
}
2016-06-04 01:08:16 +02:00
2014-01-06 05:15:47 +01:00
public function contactemail () {
$id = $this -> args [ 0 ];
$message = $this -> args [ 1 ];
$all = $this -> args [ 2 ];
$userId = $this -> args [ 3 ];
$isSiteAdmin = $this -> args [ 4 ];
$processId = $this -> args [ 5 ];
2014-02-14 13:32:19 +01:00
$this -> Job -> id = $processId ;
2015-04-20 13:32:34 +02:00
$user = $this -> User -> getAuthUser ( $userId );
2016-02-02 18:19:09 +01:00
$result = $this -> Event -> sendContactEmail ( $id , $message , $all , array ( 'User' => $user ), $isSiteAdmin );
2013-12-04 11:58:01 +01:00
$this -> Job -> saveField ( 'progress' , '100' );
2016-06-30 16:16:16 +02:00
$this -> Job -> saveField ( 'date_modified' , date ( " y-m-d H:i:s " ));
2013-12-04 11:58:01 +01:00
if ( $result != true ) $this -> Job -> saveField ( 'message' , 'Job done.' );
}
2015-01-27 17:56:50 +01:00
public function postsemail () {
2016-05-20 19:00:03 +02:00
$userId = $this -> args [ 0 ];
$postId = $this -> args [ 1 ];
$eventId = $this -> args [ 2 ];
2015-01-27 17:56:50 +01:00
$title = $this -> args [ 3 ];
$message = $this -> args [ 4 ];
$processId = $this -> args [ 5 ];
$this -> Job -> id = $processId ;
2016-05-20 19:00:03 +02:00
$result = $this -> Post -> sendPostsEmail ( $userId , $postId , $eventId , $title , $message );
2015-01-28 12:45:16 +01:00
$job [ 'Job' ][ 'progress' ] = 100 ;
$job [ 'Job' ][ 'message' ] = 'Emails sent.' ;
2016-06-30 16:16:16 +02:00
$job [ 'Job' ][ 'date_modified' ] = date ( " y-m-d H:i:s " );
2015-01-28 12:45:16 +01:00
$this -> Job -> save ( $job );
2015-01-27 17:56:50 +01:00
}
2016-06-04 01:08:16 +02:00
2014-01-03 15:26:35 +01:00
public function enqueueCaching () {
$timestamp = $this -> args [ 0 ];
$task = $this -> Task -> findByType ( 'cache_exports' );
2016-06-04 01:08:16 +02:00
2014-01-03 15:26:35 +01:00
// If the next execution time and the timestamp don't match, it means that this task is no longer valid as the time for the execution has since being scheduled
2016-06-04 01:10:45 +02:00
// been updated.
2014-01-03 15:26:35 +01:00
if ( $task [ 'Task' ][ 'next_execution_time' ] != $timestamp ) return ;
2015-06-03 23:49:37 +02:00
2016-01-10 19:45:33 +01:00
$users = $this -> User -> find ( 'all' , array (
'recursive' => - 1 ,
'conditions' => array (
'Role.perm_site_admin' => 0 ,
'User.disabled' => 0 ,
),
'contain' => array (
'Organisation' => array ( 'fields' => array ( 'name' )),
2016-06-04 01:10:45 +02:00
'Role' => array ( 'fields' => array ( 'perm_site_admin' ))
2016-01-10 19:45:33 +01:00
),
'fields' => array ( 'User.org_id' , 'User.id' ),
'group' => array ( 'User.org_id' )
));
$site_admin = $this -> User -> find ( 'first' , array (
'recursive' => - 1 ,
'conditions' => array (
'Role.perm_site_admin' => 1 ,
'User.disabled' => 0
),
'contain' => array (
'Organisation' => array ( 'fields' => array ( 'name' )),
2016-06-04 01:10:45 +02:00
'Role' => array ( 'fields' => array ( 'perm_site_admin' ))
2016-01-10 19:45:33 +01:00
),
'fields' => array ( 'User.org_id' , 'User.id' )
));
$users [] = $site_admin ;
2016-06-04 01:08:16 +02:00
2015-06-03 23:49:37 +02:00
if ( $task [ 'Task' ][ 'timer' ] > 0 ) $this -> Task -> reQueue ( $task , 'cache' , 'EventShell' , 'enqueueCaching' , false , false );
2016-06-04 01:08:16 +02:00
2014-01-03 15:26:35 +01:00
// Queue a set of exports for admins. This "ADMIN" organisation. The organisation of the admin users doesn't actually matter, it is only used to indentify
// the special cache files containing all events
$i = 0 ;
2016-01-10 19:45:33 +01:00
foreach ( $users as $user ) {
2016-06-04 15:45:39 +02:00
foreach ( $this -> Event -> export_types as $k => $type ) {
2016-08-23 00:24:54 +02:00
if ( $k == 'stix' ) continue ;
2016-08-17 09:43:57 +02:00
$this -> Job -> cache ( $k , $user [ 'User' ]);
2014-01-03 15:26:35 +01:00
$i ++ ;
}
}
2015-06-03 23:49:37 +02:00
$this -> Task -> id = $task [ 'Task' ][ 'id' ];
$this -> Task -> saveField ( 'message' , $i . ' job(s) started at ' . date ( 'd/m/Y - H:i:s' ) . '.' );
2014-01-03 15:26:35 +01:00
}
2016-06-04 01:08:16 +02:00
2014-01-06 05:15:47 +01:00
public function publish () {
$id = $this -> args [ 0 ];
$passAlong = $this -> args [ 1 ];
2014-02-09 22:09:29 +01:00
$jobId = $this -> args [ 2 ];
2015-04-20 11:46:55 +02:00
$userId = $this -> args [ 3 ];
2015-04-20 13:32:34 +02:00
$user = $this -> User -> getAuthUser ( $userId );
2014-02-10 00:29:46 +01:00
$job = $this -> Job -> read ( null , $jobId );
$this -> Event -> Behaviors -> unload ( 'SysLogLogable.SysLogLogable' );
$result = $this -> Event -> publish ( $id , $passAlong );
$job [ 'Job' ][ 'progress' ] = 100 ;
2016-06-30 16:16:16 +02:00
$job [ 'Job' ][ 'date_modified' ] = date ( " y-m-d H:i:s " );
2014-02-10 00:29:46 +01:00
if ( $result ) {
$job [ 'Job' ][ 'message' ] = 'Event published.' ;
} else {
$job [ 'Job' ][ 'message' ] = 'Event published, but the upload to other instances may have failed.' ;
}
$this -> Job -> save ( $job );
$log = ClassRegistry :: init ( 'Log' );
$log -> create ();
2018-04-04 12:26:12 +02:00
$log -> createLogEntry ( $user , 'publish' , 'Event' , $id , 'Event (' . $id . '): published.' , 'published () => (1)' );
2014-01-06 05:15:47 +01:00
}
2014-02-10 00:29:46 +01:00
2018-04-24 16:41:09 +02:00
public function enrichment () {
if ( empty ( $this -> args [ 0 ]) || empty ( $this -> args [ 1 ]) || empty ( $this -> args [ 2 ])) {
die ( 'Usage: ' . $this -> Server -> command_line_functions [ 'enrichment' ] . PHP_EOL );
}
$userId = $this -> args [ 0 ];
$user = $this -> User -> getAuthUser ( $userId );
if ( empty ( $user )) die ( 'Invalid user.' );
$eventId = $this -> args [ 1 ];
$modules = $this -> args [ 2 ];
try {
$modules = json_decode ( $modules );
} catch ( Exception $e ) {
die ( 'Invalid module JSON' );
}
if ( ! empty ( $this -> args [ 3 ])) {
$jobId = $this -> args [ 3 ];
} else {
$this -> Job -> create ();
$data = array (
'worker' => 'default' ,
'job_type' => 'enrichment' ,
'job_input' => 'Event: ' . $eventId . ' modules: ' . $modules ,
'status' => 0 ,
'retries' => 0 ,
'org' => $user [ 'Organisation' ][ 'name' ],
'message' => 'Enriching event.' ,
);
$this -> Job -> save ( $data );
$jobId = $this -> Job -> id ;
}
$options = array (
'user' => $user ,
'event_id' => $eventId ,
'modules' => $modules
);
$result = $this -> Event -> enrichment ( $options );
}
2018-09-23 17:44:23 +02:00
public function processfreetext () {
$inputFile = $this -> args [ 0 ];
$tempdir = new Folder ( APP . 'tmp/cache/ingest' , true , 0750 );
$tempFile = new File ( APP . 'tmp/cache/ingest' . DS . $inputFile );
$inputData = $tempFile -> read ();
$inputData = json_decode ( $inputData , true );
$tempFile -> delete ();
$this -> Event -> processFreeTextData (
$inputData [ 'user' ],
$inputData [ 'attributes' ],
$inputData [ 'id' ],
$inputData [ 'default_comment' ],
$inputData [ 'force' ],
$inputData [ 'adhereToWarninglists' ],
$inputData [ 'jobId' ]
);
return true ;
}
2013-11-06 10:52:18 +01:00
}