2013-11-06 10:52:18 +01: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
//App::uses('AppShell', 'Console/Command');
require_once 'AppShell.php' ;
class EventShell extends AppShell
{
2015-01-28 12:45:16 +01:00
public $uses = array ( 'Event' , 'Post' , 'Attribute' , 'Job' , 'User' , 'Task' , 'Whitelist' );
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 );
2013-11-07 15:58:29 +01:00
//$jobID = $this->Job->id;
2013-11-06 10:52:18 +01:00
//$this->Job->add('default', 'Publish', 'Event published: ' . $id);
// 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.' );
}
2013-11-07 15:58:29 +01:00
public function cachexml () {
$org = $this -> args [ 0 ];
$isSiteAdmin = $this -> args [ 1 ];
2013-11-15 15:39:34 +01:00
$id = $this -> args [ 2 ];
$this -> Job -> id = $id ;
2013-11-07 15:58:29 +01:00
$eventIds = $this -> Event -> fetchEventIds ( $org , $isSiteAdmin );
2014-06-24 13:19:40 +02:00
$result = array ();
2013-11-07 15:58:29 +01:00
$eventCount = count ( $eventIds );
2015-04-08 22:47:28 +02:00
$dir = new Folder ( APP . 'tmp/cached_exports/xml' );
2014-06-24 13:19:40 +02:00
if ( $isSiteAdmin ) {
$file = new File ( $dir -> pwd () . DS . 'misp.xml' . '.ADMIN.xml' );
} else {
$file = new File ( $dir -> pwd () . DS . 'misp.xml' . '.' . $org . '.xml' );
}
2014-11-14 11:05:37 +01:00
$toEscape = array ( " & " , " < " , " > " , " \" " , " ' " );
$escapeWith = array ( '&' , '<' , '>' , '"' , ''' );
2014-09-02 15:56:28 +02:00
$file -> write ( '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL . '<response>' );
2013-11-07 15:58:29 +01:00
foreach ( $eventIds as $k => $eventId ) {
$temp = $this -> Event -> fetchEvent ( $eventId [ 'Event' ][ 'id' ], null , $org , $isSiteAdmin , $this -> Job -> id );
2014-06-24 13:19:40 +02:00
$result = $temp [ 0 ];
2013-11-07 15:58:29 +01:00
$result [ 'Event' ][ 'Attribute' ] = $result [ 'Attribute' ];
$result [ 'Event' ][ 'ShadowAttribute' ] = $result [ 'ShadowAttribute' ];
$result [ 'Event' ][ 'RelatedEvent' ] = $result [ 'RelatedEvent' ];
2014-11-14 11:05:37 +01:00
$result [ 'Event' ][ 'info' ] = preg_replace ( '/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u' , ' ' , $result [ 'Event' ][ 'info' ]);
$result [ 'Event' ][ 'info' ] = str_replace ( $toEscape , $escapeWith , $result [ 'Event' ][ 'info' ]);
2014-06-24 13:19:40 +02:00
2013-11-07 15:58:29 +01:00
//
// cleanup the array from things we do not want to expose
//
unset ( $result [ 'Event' ][ 'user_id' ]);
// hide the org field is we are not in showorg mode
2014-02-05 15:01:26 +01:00
if ( 'true' != Configure :: read ( 'MISP.showorg' ) && ! $isSiteAdmin ) {
2013-11-07 15:58:29 +01:00
unset ( $result [ 'Event' ][ 'org' ]);
unset ( $result [ 'Event' ][ 'orgc' ]);
unset ( $result [ 'Event' ][ 'from' ]);
}
2014-09-02 15:56:28 +02:00
2013-11-07 15:58:29 +01:00
// remove value1 and value2 from the output and remove invalid utf8 characters for the xml parser
foreach ( $result [ 'Event' ][ 'Attribute' ] as $key => $value ) {
2014-09-02 15:56:28 +02:00
if ( Configure :: read ( 'MISP.cached_attachments' ) && $this -> Event -> Attribute -> typeIsAttachment ( $result [ 'Event' ][ 'Attribute' ][ $key ][ 'type' ])) {
$encodedFile = $this -> Event -> Attribute -> base64EncodeAttachment ( $result [ 'Event' ][ 'Attribute' ][ $key ]);
$result [ 'Event' ][ 'Attribute' ][ $key ][ 'data' ] = $encodedFile ;
}
2013-11-07 15:58:29 +01:00
$result [ 'Event' ][ 'Attribute' ][ $key ][ 'value' ] = preg_replace ( '/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u' , ' ' , $result [ 'Event' ][ 'Attribute' ][ $key ][ 'value' ]);
2014-11-14 11:28:42 +01:00
$result [ 'Event' ][ 'Attribute' ][ $key ][ 'comment' ] = preg_replace ( '/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u' , ' ' , $result [ 'Event' ][ 'Attribute' ][ $key ][ 'comment' ]);
2013-11-07 15:58:29 +01:00
unset ( $result [ 'Event' ][ 'Attribute' ][ $key ][ 'value1' ]);
unset ( $result [ 'Event' ][ 'Attribute' ][ $key ][ 'value2' ]);
unset ( $result [ 'Event' ][ 'Attribute' ][ $key ][ 'category_order' ]);
2014-09-02 15:56:28 +02:00
$result [ 'Event' ][ 'Attribute' ][ $key ][ 'value' ] = str_replace ( $toEscape , $escapeWith , $result [ 'Event' ][ 'Attribute' ][ $key ][ 'value' ]);
2014-11-14 11:28:42 +01:00
$result [ 'Event' ][ 'Attribute' ][ $key ][ 'comment' ] = str_replace ( $toEscape , $escapeWith , $result [ 'Event' ][ 'Attribute' ][ $key ][ 'comment' ]);
2014-11-14 11:59:38 +01:00
foreach ( $result [ 'Event' ][ 'Attribute' ][ $key ][ 'ShadowAttribute' ] as $skey => $svalue ) {
if ( Configure :: read ( 'MISP.cached_attachments' ) && $this -> Event -> ShadowAttribute -> typeIsAttachment ( $result [ 'Event' ][ 'Attribute' ][ $key ][ 'ShadowAttribute' ][ $skey ][ 'type' ])) {
$encodedFile = $this -> Event -> ShadowAttribute -> base64EncodeAttachment ( $result [ 'Event' ][ 'Attribute' ][ $key ][ 'ShadowAttribute' ][ $skey ]);
$result [ 'Event' ][ 'Attribute' ][ $key ][ 'ShadowAttribute' ][ $skey ][ 'data' ] = $encodedFile ;
}
$result [ 'Event' ][ 'Attribute' ][ $key ][ 'ShadowAttribute' ][ $skey ][ 'value' ] = preg_replace ( '/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u' , ' ' , $result [ 'Event' ][ 'Attribute' ][ $key ][ 'ShadowAttribute' ][ $skey ][ 'value' ]);
$result [ 'Event' ][ 'Attribute' ][ $key ][ 'ShadowAttribute' ][ $skey ][ 'value' ] = str_replace ( $toEscape , $escapeWith , $result [ 'Event' ][ 'Attribute' ][ $key ][ 'ShadowAttribute' ][ $skey ][ 'value' ]);
$result [ 'Event' ][ 'Attribute' ][ $key ][ 'ShadowAttribute' ][ $skey ][ 'comment' ] = preg_replace ( '/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u' , ' ' , $result [ 'Event' ][ 'Attribute' ][ $key ][ 'ShadowAttribute' ][ $skey ][ 'comment' ]);
$result [ 'Event' ][ 'Attribute' ][ $key ][ 'ShadowAttribute' ][ $skey ][ 'comment' ] = str_replace ( $toEscape , $escapeWith , $result [ 'Event' ][ 'Attribute' ][ $key ][ 'ShadowAttribute' ][ $skey ][ 'comment' ]);
}
2013-11-07 15:58:29 +01:00
}
// remove invalid utf8 characters for the xml parser
foreach ( $result [ 'Event' ][ 'ShadowAttribute' ] as $key => $value ) {
2014-09-02 15:56:28 +02:00
if ( Configure :: read ( 'MISP.cached_attachments' ) && $this -> Event -> ShadowAttribute -> typeIsAttachment ( $result [ 'Event' ][ 'ShadowAttribute' ][ $key ][ 'type' ])) {
$encodedFile = $this -> Event -> ShadowAttribute -> base64EncodeAttachment ( $result [ 'Event' ][ 'ShadowAttribute' ][ $key ]);
$result [ 'Event' ][ 'ShadowAttribute' ][ $key ][ 'data' ] = $encodedFile ;
}
2013-11-07 15:58:29 +01:00
$result [ 'Event' ][ 'ShadowAttribute' ][ $key ][ 'value' ] = preg_replace ( '/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u' , ' ' , $result [ 'Event' ][ 'ShadowAttribute' ][ $key ][ 'value' ]);
2014-09-02 15:56:28 +02:00
$result [ 'Event' ][ 'ShadowAttribute' ][ $key ][ 'value' ] = str_replace ( $toEscape , $escapeWith , $result [ 'Event' ][ 'ShadowAttribute' ][ $key ][ 'value' ]);
2014-11-14 11:28:42 +01:00
$result [ 'Event' ][ 'ShadowAttribute' ][ $key ][ 'comment' ] = preg_replace ( '/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u' , ' ' , $result [ 'Event' ][ 'ShadowAttribute' ][ $key ][ 'comment' ]);
$result [ 'Event' ][ 'ShadowAttribute' ][ $key ][ 'comment' ] = str_replace ( $toEscape , $escapeWith , $result [ 'Event' ][ 'ShadowAttribute' ][ $key ][ 'comment' ]);
2013-11-07 15:58:29 +01:00
}
2014-06-24 13:19:40 +02:00
2013-11-07 15:58:29 +01:00
if ( isset ( $result [ 'Event' ][ 'RelatedEvent' ])) {
foreach ( $result [ 'Event' ][ 'RelatedEvent' ] as $key => $value ) {
2014-09-02 15:56:28 +02:00
$temp = $value [ 'Event' ];
unset ( $result [ 'Event' ][ 'RelatedEvent' ][ $key ][ 'Event' ]);
$result [ 'Event' ][ 'RelatedEvent' ][ $key ][ 'Event' ][ 0 ] = $temp ;
unset ( $result [ 'Event' ][ 'RelatedEvent' ][ $key ][ 'Event' ][ 0 ][ 'user_id' ]);
2014-11-14 11:15:52 +01:00
$result [ 'Event' ][ 'RelatedEvent' ][ $key ][ 'Event' ][ 0 ][ 'info' ] = preg_replace ( '/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u' , ' ' , $result [ 'Event' ][ 'RelatedEvent' ][ $key ][ 'Event' ][ 0 ][ 'info' ]);
$result [ 'Event' ][ 'RelatedEvent' ][ $key ][ 'Event' ][ 0 ][ 'info' ] = str_replace ( $toEscape , $escapeWith , $result [ 'Event' ][ 'RelatedEvent' ][ $key ][ 'Event' ][ 0 ][ 'info' ]);
2014-02-05 15:01:26 +01:00
if ( 'true' != Configure :: read ( 'MISP.showorg' ) && ! $isAdmin ) {
2014-09-02 15:56:28 +02:00
unset ( $result [ 'Event' ][ 'RelatedEvent' ][ $key ][ 'Event' ][ 0 ][ 'org' ]);
unset ( $result [ 'Event' ][ 'RelatedEvent' ][ $key ][ 'Event' ][ 0 ][ 'orgc' ]);
2013-11-07 15:58:29 +01:00
}
2014-09-02 15:56:28 +02:00
unset ( $temp );
2013-11-07 15:58:29 +01:00
}
}
2014-09-02 15:56:28 +02:00
$text = $this -> __recursiveEcho ( array ( 'Event' => array ( 0 => $result [ 'Event' ])));
$file -> append ( $text );
2014-06-24 13:19:40 +02: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 ();
2013-11-15 15:39:34 +01: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 ;
}
2013-11-15 15:39:34 +01:00
public function cachehids () {
$org = $this -> args [ 0 ];
$isSiteAdmin = $this -> args [ 1 ];
$id = $this -> args [ 2 ];
$this -> Job -> id = $id ;
$extra = $this -> args [ 3 ];
$this -> Job -> saveField ( 'progress' , 1 );
2013-11-18 11:35:02 +01:00
$rules = $this -> Attribute -> hids ( $isSiteAdmin , $org , $extra );
2013-11-15 15:39:34 +01:00
$this -> Job -> saveField ( 'progress' , 80 );
$dir = new Folder ( APP . DS . '/tmp/cached_exports/' . $extra );
2013-12-04 11:58:01 +01:00
if ( $isSiteAdmin ) {
$file = new File ( $dir -> pwd () . DS . 'misp.' . $extra . '.ADMIN.txt' );
} else {
$file = new File ( $dir -> pwd () . DS . 'misp.' . $extra . '.' . $org . '.txt' );
}
2013-11-15 15:39:34 +01:00
$file -> write ( '' );
foreach ( $rules as $rule ) {
$file -> append ( $rule . PHP_EOL );
}
$file -> close ();
$this -> Job -> saveField ( 'progress' , '100' );
}
public function cachecsv () {
$org = $this -> args [ 0 ];
$isSiteAdmin = $this -> args [ 1 ];
$id = $this -> args [ 2 ];
$this -> Job -> id = $id ;
$extra = $this -> args [ 3 ];
2014-03-24 16:33:40 +01:00
if ( $extra == 'csv_all' ) $ignore = 1 ;
else $ignore = 0 ;
2013-11-15 15:39:34 +01:00
$eventIds = $this -> Event -> fetchEventIds ( $org , $isSiteAdmin );
$eventCount = count ( $eventIds );
2014-09-02 15:56:28 +02:00
$attributes = array ();
2015-04-08 22:47:28 +02:00
$dir = new Folder ( APP . 'tmp/cached_exports/' . $extra );
2013-12-04 11:58:01 +01:00
if ( $isSiteAdmin ) {
$file = new File ( $dir -> pwd () . DS . 'misp.' . $extra . '.ADMIN.csv' );
} else {
$file = new File ( $dir -> pwd () . DS . 'misp.' . $extra . '.' . $org . '.csv' );
}
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 = " " ;
2014-09-02 15:56:28 +02:00
$attributes = $this -> Event -> csv ( $org , $isSiteAdmin , $eventId [ 'Event' ][ 'id' ], $ignore );
$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 ();
$this -> Job -> saveField ( 'progress' , '100' );
}
public function cachetext () {
$org = $this -> args [ 0 ];
$isSiteAdmin = $this -> args [ 1 ];
$id = $this -> args [ 2 ];
$this -> Job -> id = $id ;
$extra = $this -> args [ 3 ];
$types = array_keys ( $this -> Attribute -> typeDefinitions );
$typeCount = count ( $types );
$dir = new Folder ( APP . DS . '/tmp/cached_exports/text' );
foreach ( $types as $k => $type ) {
$final = $this -> Attribute -> text ( $org , $isSiteAdmin , $type );
2013-12-04 11:58:01 +01:00
if ( $isSiteAdmin ) {
$file = new File ( $dir -> pwd () . DS . 'misp.text_' . $type . '.ADMIN.txt' );
} else {
$file = new File ( $dir -> pwd () . DS . 'misp.text_' . $type . '.' . $org . '.txt' );
}
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
}
2013-11-19 11:03:30 +01:00
$this -> Job -> saveField ( 'progress' , 100 );
}
public function cachenids () {
$org = $this -> args [ 0 ];
$isSiteAdmin = $this -> args [ 1 ];
$id = $this -> args [ 2 ];
$this -> Job -> id = $id ;
$format = $this -> args [ 3 ];
$sid = $this -> args [ 4 ];
$eventIds = $this -> Event -> fetchEventIds ( $org , $isSiteAdmin );
$eventCount = count ( $eventIds );
$dir = new Folder ( APP . DS . '/tmp/cached_exports/' . $format );
2013-12-04 11:58:01 +01:00
if ( $isSiteAdmin ) {
$file = new File ( $dir -> pwd () . DS . 'misp.' . $format . '.ADMIN.rules' );
} else {
$file = new File ( $dir -> pwd () . DS . 'misp.' . $format . '.' . $org . '.rules' );
}
2013-11-19 11:03:30 +01:00
$file -> write ( '' );
foreach ( $eventIds as $k => $eventId ) {
if ( $k == 0 ) {
$temp = $this -> Attribute -> nids ( $isSiteAdmin , $org , $format , $sid , $eventId [ 'Event' ][ 'id' ]);
} else {
$temp = $this -> Attribute -> nids ( $isSiteAdmin , $org , $format , $sid , $eventId [ 'Event' ][ 'id' ], true );
}
foreach ( $temp as $line ) {
$file -> append ( $line . PHP_EOL );
}
if ( $k % 10 == 0 ) {
$this -> Job -> saveField ( 'progress' , $k / $eventCount * 80 );
}
}
$file -> close ();
2013-11-15 15:39:34 +01:00
$this -> Job -> saveField ( 'progress' , '100' );
2013-11-07 15:58:29 +01:00
}
2013-12-04 11:58:01 +01:00
public function alertemail () {
$org = $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 ];
$result = $this -> Event -> sendAlertEmail ( $eventId , $org , $processId );
2014-02-10 00:29:46 +01:00
$job [ 'Job' ][ 'progress' ] = 100 ;
$job [ 'Job' ][ 'message' ] = 'Emails sent.' ;
$this -> Job -> save ( $job );
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 ;
2014-01-06 05:15:47 +01:00
$user = $this -> User -> read ( null , $userId );
$eventId = $this -> args [ 2 ];
$result = $this -> Event -> sendContactEmail ( $id , $message , $all , $user , $isSiteAdmin );
2013-12-04 11:58:01 +01:00
$this -> Job -> saveField ( 'progress' , '100' );
if ( $result != true ) $this -> Job -> saveField ( 'message' , 'Job done.' );
}
2015-01-27 17:56:50 +01:00
public function postsemail () {
$user_id = $this -> args [ 0 ];
$post_id = $this -> args [ 1 ];
$event_id = $this -> args [ 2 ];
$title = $this -> args [ 3 ];
$message = $this -> args [ 4 ];
$processId = $this -> args [ 5 ];
$this -> Job -> id = $processId ;
$user = $this -> User -> read ( null , $user_id );
$eventId = $this -> args [ 2 ];
$result = $this -> Post -> sendPostsEmail ( $user_id , $post_id , $event_id , $title , $message );
2015-01-28 12:45:16 +01:00
$job [ 'Job' ][ 'progress' ] = 100 ;
$job [ 'Job' ][ 'message' ] = 'Emails sent.' ;
$this -> Job -> save ( $job );
2015-01-27 17:56:50 +01:00
}
2014-01-03 15:26:35 +01:00
public function enqueueCaching () {
$timestamp = $this -> args [ 0 ];
$task = $this -> Task -> findByType ( 'cache_exports' );
// 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
// been updated.
if ( $task [ 'Task' ][ 'next_execution_time' ] != $timestamp ) return ;
2014-01-29 15:52:09 +01:00
$task [ 'Task' ][ 'scheduled_time' ] = date ( 'H:i' , $task [ 'Task' ][ 'next_execution_time' ]);
$this -> Task -> save ( $task );
2014-01-03 15:26:35 +01:00
$orgs = $this -> User -> getOrgs ();
// 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 ;
foreach ( $this -> Event -> export_types as $k => $type ) {
foreach ( $orgs as $org ) {
$this -> Job -> cache ( $k , false , $org , 'Events visible to: ' . $org , $org );
$i ++ ;
}
$this -> Job -> cache ( $k , true , 'ADMIN' , 'All events.' , 'ADMIN' );
$i ++ ;
}
$task [ 'Task' ][ 'message' ] = $i . ' jobs started at ' . date ( 'd/m/Y - H:i:s' ) . '.' ;
if ( $task [ 'Task' ][ 'timer' ] > 0 ) {
2014-01-29 15:52:09 +01:00
$time = time ();
// Keep adding the timer's time interval until we get a date that is in the future! We don't want to keep queuing tasks in the past since they will execute until it catches up.
while ( $task [ 'Task' ][ 'next_execution_time' ] < $time ) {
$task [ 'Task' ][ 'next_execution_time' ] = strtotime ( '+' . $task [ 'Task' ][ 'timer' ] . ' hours' , $task [ 'Task' ][ 'next_execution_time' ]);
}
2014-01-03 15:26:35 +01:00
$task [ 'Task' ][ 'scheduled_time' ] = $this -> Task -> breakTime ( $task [ 'Task' ][ 'scheduled_time' ], $task [ 'Task' ][ 'timer' ]);
2015-01-12 17:35:08 +01:00
$task [ 'Task' ][ 'scheduled_time' ] = date ( 'H:i' , $task [ 'Task' ][ 'next_execution_time' ]);
// Now that we have figured out when the next execution should happen, it's time to enqueue it.
$process_id = CakeResque :: enqueueAt (
$task [ 'Task' ][ 'next_execution_time' ],
2015-03-23 17:58:41 +01:00
'cache' ,
2015-01-12 17:35:08 +01:00
'EventShell' ,
array ( 'enqueueCaching' , $task [ 'Task' ][ 'next_execution_time' ]),
true
);
$task [ 'Task' ][ 'job_id' ] = $process_id ;
2014-01-29 15:52:09 +01:00
$this -> Task -> save ( $task );
2014-01-03 15:26:35 +01: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 ];
2014-02-10 00:29:46 +01:00
$org = $this -> args [ 3 ];
$email = $this -> args [ 4 ];
$user = $this -> User -> find ( 'first' , array (
'conditions' => array ( 'email' => $email ),
'fields' => array ( 'email' , 'org' , 'id' ),
'recursive' => - 1 ,
));
$job = $this -> Job -> read ( null , $jobId );
2014-01-06 05:15:47 +01:00
$eventId = $this -> args [ 2 ];
2014-02-10 00:29:46 +01:00
$this -> Event -> Behaviors -> unload ( 'SysLogLogable.SysLogLogable' );
$result = $this -> Event -> publish ( $id , $passAlong );
$job [ 'Job' ][ 'progress' ] = 100 ;
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 ();
$log -> save ( array (
'org' => $user [ 'User' ][ 'org' ],
'email' => $user [ 'User' ][ 'email' ],
'user_id' => $user [ 'User' ][ 'id' ],
'action' => 'publish' ,
'title' => 'Event (' . $id . '): published.' ,
'change' => 'published () => (1)' ));
2014-01-06 05:15:47 +01:00
}
2014-02-10 00:29:46 +01:00
2013-11-06 10:52:18 +01:00
}