| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles the scheduling and sending of audit logging reports. |
| 4 |
* |
| 5 |
* @package WP_Defender\Model\Notification |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Model\Notification; |
| 9 |
|
| 10 |
use DateTime; |
| 11 |
use Exception; |
| 12 |
use WP_Defender\Component\Mail; |
| 13 |
use WP_Defender\Model\Audit_Log; |
| 14 |
use WP_Defender\Component\Notification; |
| 15 |
use WP_Defender\Controller\Audit_Logging; |
| 16 |
|
| 17 |
/** |
| 18 |
* Model for audit logging settings. |
| 19 |
*/ |
| 20 |
class Audit_Report extends \WP_Defender\Model\Notification { |
| 21 |
|
| 22 |
/** |
| 23 |
* Table name. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $table = 'wd_audit_report'; |
| 28 |
/** |
| 29 |
* Slug identifier for the audit report. |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
public const SLUG = 'audit-report'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Constructor method. |
| 37 |
* Sets default values for the class. |
| 38 |
*/ |
| 39 |
protected function before_load(): void { |
| 40 |
$default = array( |
| 41 |
'title' => esc_html__( 'Audit Logging - Reporting', 'defender-security' ), |
| 42 |
'slug' => self::SLUG, |
| 43 |
'status' => self::STATUS_DISABLED, |
| 44 |
'description' => esc_html__( |
| 45 |
'Schedule Defender to automatically email you a summary of all your website events.', |
| 46 |
'defender-security' |
| 47 |
), |
| 48 |
// @since 3.0.0 Fix 'Guest'-line. |
| 49 |
'in_house_recipients' => is_user_logged_in() ? array( $this->get_default_user() ) : array(), |
| 50 |
'out_house_recipients' => array(), |
| 51 |
'type' => 'report', |
| 52 |
'frequency' => 'weekly', |
| 53 |
'day' => 'sunday', |
| 54 |
'time' => '4:00', |
| 55 |
'day_n' => 1, |
| 56 |
'dry_run' => false, |
| 57 |
'configs' => array(), |
| 58 |
); |
| 59 |
$this->import( $default ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Sends the email. |
| 64 |
* Retrieves data based on logic. |
| 65 |
* |
| 66 |
* @return void |
| 67 |
* @throws Exception When an error occurs. |
| 68 |
*/ |
| 69 |
public function send() { |
| 70 |
$date_to = new DateTime( 'now', wp_timezone() ); |
| 71 |
switch ( $this->frequency ) { |
| 72 |
case 'daily': |
| 73 |
$date_from = new DateTime( '-24 hours', wp_timezone() ); |
| 74 |
break; |
| 75 |
case 'weekly': |
| 76 |
$date_from = new DateTime( '-7 days', wp_timezone() ); |
| 77 |
break; |
| 78 |
case 'monthly': |
| 79 |
default: |
| 80 |
$date_from = new DateTime( '-30 days', wp_timezone() ); |
| 81 |
break; |
| 82 |
} |
| 83 |
$data = Audit_Log::query( $date_from->getTimestamp(), $date_to->getTimestamp(), array(), '', '', false ); |
| 84 |
|
| 85 |
$collection = array(); |
| 86 |
// Collect data. |
| 87 |
foreach ( $data as $item ) { |
| 88 |
if ( ! isset( $collection[ $item->event_type ][ $item->action_type ] ) ) { |
| 89 |
$collection[ $item->event_type ][ $item->action_type ] = 0; |
| 90 |
} |
| 91 |
++$collection[ $item->event_type ][ $item->action_type ]; |
| 92 |
} |
| 93 |
$service = wd_di()->get( Notification::class ); |
| 94 |
// Send data. |
| 95 |
foreach ( $this->in_house_recipients as $recipient ) { |
| 96 |
if ( self::USER_SUBSCRIBED !== $recipient['status'] ) { |
| 97 |
continue; |
| 98 |
} |
| 99 |
$this->send_to_user( $recipient['name'], $recipient['email'], $data, $collection, $service ); |
| 100 |
} |
| 101 |
foreach ( $this->out_house_recipients as $recipient ) { |
| 102 |
if ( self::USER_SUBSCRIBED !== $recipient['status'] ) { |
| 103 |
continue; |
| 104 |
} |
| 105 |
$this->send_to_user( $recipient['name'], $recipient['email'], $data, $collection, $service ); |
| 106 |
} |
| 107 |
// Last sent should be the previous timestamp. |
| 108 |
$this->last_sent = $this->est_timestamp; |
| 109 |
$this->est_timestamp = $this->get_next_run()->getTimestamp(); |
| 110 |
$this->save(); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Constructs and sends the email to the specified recipient. |
| 115 |
* |
| 116 |
* @param string $name The recipient's name. |
| 117 |
* @param string $email The recipient's email address. |
| 118 |
* @param array $data The audit log data. |
| 119 |
* @param array $collection The summarized list of events. |
| 120 |
* @param object $service The notification service object. |
| 121 |
*/ |
| 122 |
private function send_to_user( $name, $email, $data, $collection, $service ) { |
| 123 |
$site_url = network_site_url(); |
| 124 |
// Use smart quotes to display the email subject in HTML correctly and save escaping. |
| 125 |
$subject = sprintf( |
| 126 |
/* translators: %s: Site URL. */ |
| 127 |
esc_html__( 'Here`s what`s been happening at %s', 'defender-security' ), |
| 128 |
$site_url |
| 129 |
); |
| 130 |
$audit_logging = wd_di()->get( Audit_Logging::class ); |
| 131 |
$mail_object = wd_di()->get( Mail::class ); |
| 132 |
$plugin_label = $mail_object->get_sender_name( self::SLUG ); |
| 133 |
if ( count( $data ) > 0 ) { |
| 134 |
$logs_url = network_admin_url( 'admin.php?page=wdf-logging&view=logs' ); |
| 135 |
// Need for activated Mask Login feature. |
| 136 |
$logs_url = apply_filters( 'report_email_logs_link', $logs_url, $email ); |
| 137 |
$header = sprintf( |
| 138 |
/* translators: 1. Plugin label. 2. Site URL. */ |
| 139 |
esc_html__( 'Audit Update From %1$s! %2$s', 'defender-security' ), |
| 140 |
$plugin_label, |
| 141 |
$site_url |
| 142 |
); |
| 143 |
$message = $audit_logging->render_partial( |
| 144 |
'email/audit-report-table', |
| 145 |
array( |
| 146 |
'logs_url' => $logs_url, |
| 147 |
'name' => $name, |
| 148 |
'collection' => $collection, |
| 149 |
'header' => $header, |
| 150 |
), |
| 151 |
false |
| 152 |
); |
| 153 |
} else { |
| 154 |
$message = $audit_logging->render_partial( |
| 155 |
'email/audit-report-no-events', |
| 156 |
array( |
| 157 |
'name' => $name, |
| 158 |
'site_url' => $site_url, |
| 159 |
), |
| 160 |
false |
| 161 |
); |
| 162 |
} |
| 163 |
$unsubscribe_link = $service->create_unsubscribe_url( $this->slug, $email ); |
| 164 |
// Main email template. |
| 165 |
$content = $audit_logging->render_partial( |
| 166 |
'email/index', |
| 167 |
array( |
| 168 |
'title' => esc_html__( 'Audit Logging', 'defender-security' ), |
| 169 |
'content_body' => $message, |
| 170 |
'unsubscribe_link' => $unsubscribe_link, |
| 171 |
), |
| 172 |
false |
| 173 |
); |
| 174 |
|
| 175 |
$headers = $mail_object->get_headers( |
| 176 |
defender_noreply_email( 'wd_audit_noreply_email' ), |
| 177 |
self::SLUG |
| 178 |
); |
| 179 |
|
| 180 |
$ret = wp_mail( $email, $subject, $content, $headers ); |
| 181 |
if ( $ret ) { |
| 182 |
$this->save_log( $email ); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Define labels. |
| 188 |
* |
| 189 |
* @return array The array of labels. |
| 190 |
*/ |
| 191 |
public function labels(): array { |
| 192 |
return array( |
| 193 |
'report' => esc_html__( 'Audit Logging - Reporting', 'defender-security' ), |
| 194 |
'subscribers' => esc_html__( 'Recipients', 'defender-security' ), |
| 195 |
'day' => esc_html__( 'Day of', 'defender-security' ), |
| 196 |
'day_n' => esc_html__( 'Day of', 'defender-security' ), |
| 197 |
'time' => esc_html__( 'Time of day', 'defender-security' ), |
| 198 |
'frequency' => esc_html__( 'Frequency', 'defender-security' ), |
| 199 |
); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Additional converting rules. |
| 204 |
* |
| 205 |
* @param array $configs The configuration data. |
| 206 |
* |
| 207 |
* @return array The type-casted configuration data. |
| 208 |
* @since 3.1.0 |
| 209 |
*/ |
| 210 |
public function type_casting( $configs ): array { |
| 211 |
return is_array( $configs ) ? $configs : array(); |
| 212 |
} |
| 213 |
} |
| 214 |
|