diff --git a/include/class-Log.php b/include/class-Log.php index f9b8b60..a460b14 100644 --- a/include/class-Log.php +++ b/include/class-Log.php @@ -1,366 +1,368 @@ . // make sure that this class is loaded at startup class_exists( User::class ); class_exists( Domain::class ); class_exists( Mailbox::class ); class_exists( Mailforwardfrom::class ); trait LogTrait { /** * Get the log actor name * * @return string */ public function getLogActorFirm() { return User::firm( $this->get( 'actor_uid' ) ); } /** * Get the action family * * @return string */ public function getLogFamily() { return $this->get( 'log_family' ); } /** * Get the action */ public function getLogAction() { return $this->get( 'log_action' ); } /** * Get the action */ public function getLogDate() { return $this->get( 'log_timestamp' ); } /** * Get the UID of the User Marionette (user passively involved) * + * This is not a real column but a useful alias provided by the class 'QueryLog'. + * * @return string */ public function getLogMarionetteUID() { return $this->get( 'marionette_uid' ); } /** * Get the log message * * @param array $args Arguments * @return self */ public function getLogMessage( $args ) { $family = $this->getLogFamily(); $action = $this->getLogAction(); // trigger the right message family switch( $family ) { case 'domain': return self::domainMessage( $action, $this, $args ); case 'mailbox': return self::mailboxMessage( $action, $this, $args ); case 'mailforward': return self::mailforwardMessage( $action, $this, $args ); case 'user': return self::userMessage( $action, $this, $args ); } return self::unknownAction( $family, $action ); } /** * Get the log message alongside the date and the actor name * * @param array $args Arguments * @return self */ public function getLogMessageWithDateAndUser( $args ) { $actor = $args['actor'] ?? $this; // create the Actor firm from the passed User object or from the Log $actor_firm = $actor instanceof User ? $actor->getUserFirm() : $actor->getLogActorFirm(); return sprintf( "%s - %s %s", $this->getLogDate()->format( __( "Y-m-d H:i" ) ), $actor_firm, $this->getLogMessage( $args ) ); } protected function normalizeLog() { $this->datetimes( 'log_timestamp' ); } } /** * A generic log of an action * * Something happened. Dunno what. */ class Log extends Queried { use LogTrait; use UserTrait; use DomainTrait; use MailboxTrait; use MailforwardfromTrait; public function __construct() { $this->normalizeLog(); } /** * Database table name */ const T = 'log'; /** * Generate a Domain-related message * * @param string $action The related action name * @param object $log * @param array $args Arguments * @return string Message */ public static function domainMessage( $action, $log, $args ) { /** * You can pass some objects to build the message: * * A complete 'actor' User object * A complete 'domain' Domain object */ $domain = $args['domain'] ?? $log; $plan = $args['plan'] ?? $log; switch( $action ) { // an administrator created the Domain case 'create': return sprintf( __( "created the domain %s" ), $domain->getDomainFirm() ); // an administrator has changed the Plan for a Domain case 'plan.change': return sprintf( __( "changed the Plan for %s to %s" ), $domain->getDomainFirm(), esc_html( $plan->getPlanName() ) ); } // default dummy message return self::unknownAction( 'domain', $action ); } /** * Generate a Mailbox-related message * * @param string $action The related action name * @param object $log * @param array $args Arguments * @return string Message */ public static function mailboxMessage( $action, $log, $args ) { /** * You can pass some objects to build the message: * * A complete 'actor' User object * A complete 'domain' Domain object * A complete 'mailbox' Mailbox object */ $actor = $args['actor'] ?? $log; $domain = $args['domain'] ?? $log; $mailbox = $args['mailbox'] ?? $log; $mailbox_firm = Mailbox::firm( $domain->getDomainName(), $mailbox->getMailboxUsername() ); // trigger the right action message switch( $action ) { // the mailbox was created case 'create': return sprintf( __( "created the mailbox %s" ), $mailbox_firm ); // the description was changed case 'description.change': return sprintf( __( "edited description of %s" ), $mailbox_firm ); case 'newpassword': return sprintf( __( "reset password of %s" ), $mailbox_firm ); } // default dummy message return self::unknownAction( 'mailbox', $action ); } /** * Generate a Mailforward-related message * * @param string $action The related action name * @param object $log * @param array $args Arguments * @return string Message */ public static function mailforwardMessage( $action, $log, $args ) { /** * You can pass some objects to build the message: * * A complete 'domain' Domain object * A complete 'mailforwardfrom' Mailforwardfrom object */ $domain = $args['domain'] ?? $log; $mailforwardfrom = $args['mailforwardfrom'] ?? $log; $domain_name = '?'; if( $domain ) { $domain_name = $domain->getDomainName(); } $mailforwardfrom_username = '?'; if( $mailforwardfrom ) { $mailforwardfrom_username = $mailforwardfrom->getMailforwardfromUsername(); } $firm = Mailforwardfrom::firm( $domain_name, $mailforwardfrom_username ); // trigger the right action message switch( $action ) { // a Mailforward destination was added case 'add.destination': return sprintf( __( "added a destination for %s" ), $firm ); // a Mailforward destination was removed case 'remove.destination': return sprintf( __( "removed a destination for %s" ), $firm ); // a Mailforward mailbox was created case 'create': return sprintf( __( "created %s" ), $firm ); case 'delete': return sprintf( __( "deleted an %s mail forwarding" ), $firm ); } // default dummy message return self::unknownAction( 'mailforward', $action ); } /** * Generate a User-related message * * @param string $action The related action name * @param object $log * @param array $args Arguments * @return string Message */ public static function userMessage( $action, $log, $args ) { /** * You can pass some objects to build the message: * * A complete 'marionette' User object */ $marionette = $args['marionette'] ?? null; $marionette_uid = '?'; if( $args['marionette'] ) { $marionette_uid = $marionette->getUserUID(); } else { $marionette_uid = $log->getLogMarionetteUID(); } $firm = '?'; if( $marionette_uid ) { $firm = User::firm( $marionette_uid ); } // trigger the right action message switch( $action ) { // an User creation case 'create': return sprintf( __( "gave birth to %s" ), $firm ); // a password reset case 'password.reset': return sprintf( __( "reset password for %s" ), $firm ); } // default dummy message return self::unknownAction( 'user', $action ); } private static function unknownAction( $family, $action ) { return esc_html( sprintf( __( "misterious action about %s (%s)" ), $family, $action ) ); } }