| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Component; |
| 4 |
|
| 5 |
use WP_Defender\Behavior\WPMUDEV; |
| 6 |
use WP_Defender\Component; |
| 7 |
use WP_Defender\Model\Notification as Abstract_Notification; |
| 8 |
use WP_Defender\Model\Notification\Audit_Report; |
| 9 |
use WP_Defender\Model\Notification\Firewall_Notification; |
| 10 |
use WP_Defender\Model\Notification\Firewall_Report; |
| 11 |
use WP_Defender\Model\Notification\Malware_Notification; |
| 12 |
use WP_Defender\Model\Notification\Malware_Report; |
| 13 |
use WP_Defender\Model\Notification\Tweak_Reminder; |
| 14 |
use WP_Defender\Model\Setting\Audit_Logging; |
| 15 |
use WP_Defender\Traits\IO; |
| 16 |
use WP_Defender\Traits\User; |
| 17 |
|
| 18 |
class Notification extends Component { |
| 19 |
use User, IO; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var bool |
| 23 |
*/ |
| 24 |
private $is_pro; |
| 25 |
|
| 26 |
public function __construct() { |
| 27 |
$this->attach_behavior( WPMUDEV::class, WPMUDEV::class ); |
| 28 |
$this->is_pro = ( new WPMUDEV() )->is_pro(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @param array $exclude |
| 33 |
* @param string $role |
| 34 |
* @param string $username |
| 35 |
* @param string $order_by |
| 36 |
* @param string $order |
| 37 |
* @param int $limit |
| 38 |
* |
| 39 |
* @return array |
| 40 |
*/ |
| 41 |
public function get_users_pool( |
| 42 |
$exclude = array(), |
| 43 |
$role = '', |
| 44 |
$username = '', |
| 45 |
$order_by = 'ID', |
| 46 |
$order = 'ASC', |
| 47 |
$limit = 15, |
| 48 |
$paged = 1 |
| 49 |
) { |
| 50 |
$params = array( |
| 51 |
'site_id' => 0, |
| 52 |
'role' => $role, |
| 53 |
'orderby' => $order_by, |
| 54 |
'order' => $order, |
| 55 |
'number' => $limit, |
| 56 |
'paged' => $paged, |
| 57 |
'exclude' => $exclude, |
| 58 |
); |
| 59 |
|
| 60 |
if ( ! empty( $username ) ) { |
| 61 |
$params['search'] = strtolower( $username ); |
| 62 |
$params['search_columns'] = array( |
| 63 |
'user_login', |
| 64 |
'user_email', |
| 65 |
'user_nicename', |
| 66 |
'display_name', |
| 67 |
); |
| 68 |
} |
| 69 |
$user_query = new \WP_User_Query( $params ); |
| 70 |
|
| 71 |
$pools = array(); |
| 72 |
foreach ( $user_query->get_results() as $user ) { |
| 73 |
$pools[] = array( |
| 74 |
'name' => $this->get_user_display( $user ), |
| 75 |
'email' => $this->get_current_user_email( $user ), |
| 76 |
'role' => $this->get_current_user_role( $user ), |
| 77 |
'avatar' => get_avatar_url( $this->get_current_user_email( $user ) ), |
| 78 |
'id' => $user->ID, |
| 79 |
'status' => Abstract_Notification::USER_SUBSCRIBE_NA, |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
return $pools; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Dispatch Firewall and Scan notifications. |
| 88 |
* |
| 89 |
* @param string $slug |
| 90 |
* @param object $args |
| 91 |
* |
| 92 |
* @throws \DI\DependencyException |
| 93 |
* @throws \DI\NotFoundException |
| 94 |
*/ |
| 95 |
public function dispatch_notification( $slug, $args ) { |
| 96 |
$module = $this->find_module_by_slug( $slug ); |
| 97 |
if ( ! is_object( $module ) ) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
if ( 'malware-notification' === $module->slug && $module->check_options() ) { |
| 102 |
// Case report. |
| 103 |
$module->send( $args ); |
| 104 |
} elseif ( 'firewall-notification' === $module->slug && $module->check_options( $args ) ) { |
| 105 |
$module->send( $args ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @param string $slug |
| 111 |
* |
| 112 |
* @return mixed |
| 113 |
* @throws \DI\DependencyException |
| 114 |
* @throws \DI\NotFoundException |
| 115 |
*/ |
| 116 |
public function find_module_by_slug( $slug ) { |
| 117 |
switch ( $slug ) { |
| 118 |
case 'tweak-reminder': |
| 119 |
return wd_di()->get( Tweak_Reminder::class ); |
| 120 |
case 'malware-notification': |
| 121 |
return wd_di()->get( Malware_Notification::class ); |
| 122 |
case 'firewall-notification': |
| 123 |
return wd_di()->get( Firewall_Notification::class ); |
| 124 |
case 'malware-report': |
| 125 |
return wd_di()->get( Malware_Report::class ); |
| 126 |
case 'firewall-report': |
| 127 |
return wd_di()->get( Firewall_Report::class ); |
| 128 |
case 'audit-report': |
| 129 |
default: |
| 130 |
return wd_di()->get( Audit_Report::class ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Send a verification email to users. |
| 136 |
* |
| 137 |
* @param Abstract_Notification $model |
| 138 |
*/ |
| 139 |
public function send_subscription_confirm_email( Abstract_Notification $model ) { |
| 140 |
foreach ( $model->in_house_recipients as &$subscriber ) { |
| 141 |
if ( empty( $subscriber['status'] ) ) { |
| 142 |
continue; |
| 143 |
} |
| 144 |
if ( Abstract_Notification::USER_SUBSCRIBE_NA !== $subscriber['status'] ) { |
| 145 |
continue; |
| 146 |
} |
| 147 |
$ret = $this->send_email( $subscriber, $model ); |
| 148 |
|
| 149 |
if ( $ret ) { |
| 150 |
$subscriber['status'] = Abstract_Notification::USER_SUBSCRIBE_WAITING; |
| 151 |
} |
| 152 |
} |
| 153 |
foreach ( $model->out_house_recipients as &$subscriber ) { |
| 154 |
if ( empty( $subscriber['status'] ) ) { |
| 155 |
continue; |
| 156 |
} |
| 157 |
if ( Abstract_Notification::USER_SUBSCRIBE_NA !== $subscriber['status'] ) { |
| 158 |
continue; |
| 159 |
} |
| 160 |
$ret = $this->send_email( $subscriber, $model ); |
| 161 |
|
| 162 |
if ( $ret ) { |
| 163 |
$subscriber['status'] = Abstract_Notification::USER_SUBSCRIBE_WAITING; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
$model->save(); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* @param array $subscriber |
| 172 |
* @param Abstract_Notification $model |
| 173 |
* |
| 174 |
* @return bool |
| 175 |
* @throws \DI\DependencyException |
| 176 |
* @throws \DI\NotFoundException |
| 177 |
*/ |
| 178 |
public function send_email( $subscriber, Abstract_Notification $model ) { |
| 179 |
$headers = defender_noreply_html_header( |
| 180 |
defender_noreply_email( 'wd_confirm_noreply_email' ) |
| 181 |
); |
| 182 |
$email = $subscriber['email']; |
| 183 |
$name = $subscriber['name'] ?? ''; |
| 184 |
$inhouse = false; |
| 185 |
if ( isset( $subscriber['id'] ) ) { |
| 186 |
$inhouse = true; |
| 187 |
} |
| 188 |
$url = $this->create_subscribe_url( $model->slug, $email, $inhouse ); |
| 189 |
$subject = sprintf( 'Subscribe to %s', $model->title ); |
| 190 |
// Renders emails. |
| 191 |
$notification = wd_di()->get( \WP_Defender\Controller\Notification::class ); |
| 192 |
$content_body = $notification->render_partial( |
| 193 |
'email/confirm', |
| 194 |
array( |
| 195 |
'subject' => $subject, |
| 196 |
'email' => $email, |
| 197 |
'notification_name' => $model->title, |
| 198 |
'url' => $url, |
| 199 |
'site_url' => network_site_url(), |
| 200 |
'name' => $name, |
| 201 |
), |
| 202 |
false |
| 203 |
); |
| 204 |
$content = $notification->render_partial( |
| 205 |
'email/index', |
| 206 |
array( |
| 207 |
'title' => preg_replace('/ - Notification$/', '', $model->title ), |
| 208 |
'content_body' => $content_body, |
| 209 |
), |
| 210 |
false |
| 211 |
); |
| 212 |
|
| 213 |
// We send email here. |
| 214 |
return wp_mail( $email, $subject, $content, $headers ); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* @param string $email |
| 219 |
* @param object $m |
| 220 |
* @param string $name |
| 221 |
* |
| 222 |
* @throws \DI\DependencyException |
| 223 |
* @throws \DI\NotFoundException |
| 224 |
*/ |
| 225 |
public function send_subscribed_email( $email, $m, $name ) { |
| 226 |
$headers = defender_noreply_html_header( |
| 227 |
defender_noreply_email( 'wd_subscribe_noreply_email' ) |
| 228 |
); |
| 229 |
|
| 230 |
$notification = wd_di()->get( \WP_Defender\Controller\Notification::class ); |
| 231 |
$subject = __( 'Confirmed', 'wpdef' ); |
| 232 |
$content_body = $notification->render_partial( |
| 233 |
'email/subscribed', |
| 234 |
array( |
| 235 |
'subject' => __( 'Subscription Confirmed', 'wpdef' ), |
| 236 |
'notification_name' => $m->title, |
| 237 |
'url' => $this->create_unsubscribe_url( $m->slug, $email ), |
| 238 |
'name' => $name, |
| 239 |
) |
| 240 |
); |
| 241 |
$content = $notification->render_partial( |
| 242 |
'email/index', |
| 243 |
array( |
| 244 |
'title' => preg_replace( '/ - Notification$/', '', $m->title ), |
| 245 |
'content_body' => $content_body, |
| 246 |
), |
| 247 |
false |
| 248 |
); |
| 249 |
|
| 250 |
wp_mail( $email, $subject, $content, $headers ); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Send unsubscribe email. |
| 255 |
* |
| 256 |
* @param object $m |
| 257 |
* @param string $email |
| 258 |
* @param bool $inhouse |
| 259 |
* @param string $name |
| 260 |
*/ |
| 261 |
public function send_unsubscribe_email( $m, $email, $inhouse, $name ) { |
| 262 |
$subject = __( 'Unsubscribed', 'wpdef' ); |
| 263 |
$url = $this->create_subscribe_url( $m->slug, $email, $inhouse ); |
| 264 |
// Render emails. |
| 265 |
$notification = wd_di()->get( \WP_Defender\Controller\Notification::class ); |
| 266 |
$content_body = $notification->render_partial( |
| 267 |
'email/unsubscribe', |
| 268 |
array( |
| 269 |
'subject' => __( 'Unsubscribed', 'wpdef' ), |
| 270 |
'notification_name' => $m->title, |
| 271 |
'url' => $url, |
| 272 |
'name' => $name, |
| 273 |
) |
| 274 |
); |
| 275 |
$title = preg_replace( '/ - Notification$/', '', $m->title ); |
| 276 |
$title = preg_replace( '/ - Reporting$/', '', $title ); |
| 277 |
$content = $notification->render_partial( |
| 278 |
'email/index', |
| 279 |
array( |
| 280 |
'title' => $title, |
| 281 |
'content_body' => $content_body, |
| 282 |
), |
| 283 |
false |
| 284 |
); |
| 285 |
|
| 286 |
$headers = defender_noreply_html_header( |
| 287 |
defender_noreply_email( 'wd_unsubscribe_noreply_email' ) |
| 288 |
); |
| 289 |
|
| 290 |
wp_mail( $email, $subject, $content, $headers ); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* @param string $slug |
| 295 |
* @param string $email |
| 296 |
* |
| 297 |
* @return string |
| 298 |
*/ |
| 299 |
public function create_unsubscribe_url( $slug, $email ) { |
| 300 |
return add_query_arg( |
| 301 |
array( |
| 302 |
'action' => 'defender_listen_user_unsubscribe', |
| 303 |
'hash' => hash( 'sha256', $email . AUTH_SALT ), |
| 304 |
'slug' => $slug, |
| 305 |
), |
| 306 |
admin_url( 'admin-ajax.php' ) |
| 307 |
); |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* @param string $slug |
| 312 |
* @param string $email |
| 313 |
* @param bool $inhouse User is In-house or not. |
| 314 |
* |
| 315 |
* @return string |
| 316 |
*/ |
| 317 |
public function create_subscribe_url( $slug, $email, $inhouse ) { |
| 318 |
return add_query_arg( |
| 319 |
array( |
| 320 |
'action' => 'defender_listen_user_subscribe', |
| 321 |
'hash' => hash( 'sha256', $email . AUTH_SALT ), |
| 322 |
'uid' => $slug, |
| 323 |
'inhouse' => $inhouse, |
| 324 |
), |
| 325 |
admin_url( 'admin-ajax.php' ) |
| 326 |
); |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Get all modules as array of arrays. |
| 331 |
* |
| 332 |
* @return array |
| 333 |
*/ |
| 334 |
public function get_modules() { |
| 335 |
$modules = array( |
| 336 |
wd_di()->get( Tweak_Reminder::class )->export(), |
| 337 |
wd_di()->get( Malware_Notification::class )->export(), |
| 338 |
wd_di()->get( Firewall_Notification::class )->export(), |
| 339 |
); |
| 340 |
|
| 341 |
if ( true === $this->is_pro ) { |
| 342 |
return array_merge( $modules, $this->get_active_pro_reports() ); |
| 343 |
} else { |
| 344 |
return $modules; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* Get all modules as array of objects. |
| 350 |
* |
| 351 |
* @return array |
| 352 |
* @throws \DI\DependencyException |
| 353 |
* @throws \DI\NotFoundException |
| 354 |
*/ |
| 355 |
public function get_modules_as_objects() { |
| 356 |
$modules = array( |
| 357 |
wd_di()->get( Tweak_Reminder::class ), |
| 358 |
wd_di()->get( Malware_Notification::class ), |
| 359 |
wd_di()->get( Firewall_Notification::class ), |
| 360 |
); |
| 361 |
|
| 362 |
if ( true === $this->is_pro ) { |
| 363 |
$modules = array_merge( |
| 364 |
$modules, |
| 365 |
array( |
| 366 |
wd_di()->get( Malware_Report::class ), |
| 367 |
wd_di()->get( Firewall_Report::class ), |
| 368 |
wd_di()->get( Audit_Report::class ), |
| 369 |
) |
| 370 |
); |
| 371 |
} |
| 372 |
|
| 373 |
return $modules; |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Return the time that next report will be trigger. |
| 378 |
* |
| 379 |
* @return mixed |
| 380 |
*/ |
| 381 |
public function get_next_run() { |
| 382 |
if ( false === $this->is_pro ) { |
| 383 |
return __( 'Never', 'wpdef' ); |
| 384 |
} |
| 385 |
$modules = $this->get_active_pro_reports_as_objects(); |
| 386 |
$next_run = null; |
| 387 |
foreach ( $modules as $module ) { |
| 388 |
if ( Abstract_Notification::STATUS_ACTIVE !== $module->status ) { |
| 389 |
continue; |
| 390 |
} |
| 391 |
if ( is_null( $next_run ) ) { |
| 392 |
$next_run = $module; |
| 393 |
} elseif ( $module->est_timestamp < $next_run->est_timestamp ) { |
| 394 |
$next_run = $module; |
| 395 |
} |
| 396 |
} |
| 397 |
if ( is_null( $next_run ) ) { |
| 398 |
return __( 'Never', 'wpdef' ); |
| 399 |
} |
| 400 |
|
| 401 |
return $next_run->get_next_run_as_string(); |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* Get inactive modules. |
| 406 |
* |
| 407 |
* @return array |
| 408 |
* @since 2.7.0 Malware Scanning - Reporting may be inactive. |
| 409 |
*/ |
| 410 |
public function get_inactive_modules() { |
| 411 |
if ( false === $this->is_pro ) { |
| 412 |
return array(); |
| 413 |
} |
| 414 |
$modules = array(); |
| 415 |
if ( false === wd_di()->get( \WP_Defender\Model\Setting\Scan::class )->scheduled_scanning ) { |
| 416 |
$module = wd_di()->get( Malware_Report::class )->export(); |
| 417 |
$module['link'] = network_admin_url( 'admin.php?page=wdf-scan&view=settings&enable=scheduled_scanning#setting_scheduled_scanning' ); |
| 418 |
$modules[] = $module; |
| 419 |
} |
| 420 |
if ( false === wd_di()->get( Audit_Logging::class )->is_active() ) { |
| 421 |
$module = wd_di()->get( Audit_Report::class )->export(); |
| 422 |
$module['link'] = network_admin_url( 'admin.php?page=wdf-logging&view=logs' ); |
| 423 |
$modules[] = $module; |
| 424 |
} |
| 425 |
|
| 426 |
return $modules; |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* Get active modules of Pro reports as array of arrays. |
| 431 |
* |
| 432 |
* @return array |
| 433 |
*/ |
| 434 |
public function get_active_pro_reports() { |
| 435 |
$modules = array(); |
| 436 |
// Malware_Report. |
| 437 |
if ( true === wd_di()->get( \WP_Defender\Model\Setting\Scan::class )->scheduled_scanning ) { |
| 438 |
$modules[] = wd_di()->get( Malware_Report::class )->export(); |
| 439 |
} |
| 440 |
// Firewall_Report. |
| 441 |
$modules[] = wd_di()->get( Firewall_Report::class )->export(); |
| 442 |
// Audit_Report. |
| 443 |
if ( true === wd_di()->get( Audit_Logging::class )->is_active() ) { |
| 444 |
$modules[] = wd_di()->get( Audit_Report::class )->export(); |
| 445 |
} |
| 446 |
|
| 447 |
return $modules; |
| 448 |
} |
| 449 |
|
| 450 |
/** |
| 451 |
* Get active modules of Pro reports as array of objects. |
| 452 |
* |
| 453 |
* @return array |
| 454 |
*/ |
| 455 |
public function get_active_pro_reports_as_objects() { |
| 456 |
$modules = array(); |
| 457 |
// Malware_Report. |
| 458 |
if ( true === wd_di()->get( \WP_Defender\Model\Setting\Scan::class )->scheduled_scanning ) { |
| 459 |
$modules[] = wd_di()->get( Malware_Report::class ); |
| 460 |
} |
| 461 |
// Firewall_Report. |
| 462 |
$modules[] = wd_di()->get( Firewall_Report::class ); |
| 463 |
// Audit_Report. |
| 464 |
if ( true === wd_di()->get( Audit_Logging::class )->is_active() ) { |
| 465 |
$modules[] = wd_di()->get( Audit_Report::class ); |
| 466 |
} |
| 467 |
|
| 468 |
return $modules; |
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* @return int |
| 473 |
*/ |
| 474 |
public function count_active() { |
| 475 |
$count = 0; |
| 476 |
foreach ( $this->get_modules() as $module ) { |
| 477 |
if ( Abstract_Notification::STATUS_ACTIVE === $module['status'] ) { |
| 478 |
++$count; |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
return $count; |
| 483 |
} |
| 484 |
|
| 485 |
public function maybe_dispatch_report() { |
| 486 |
$modules = array( wd_di()->get( Tweak_Reminder::class ) ); |
| 487 |
if ( true === $this->is_pro ) { |
| 488 |
$modules = array_merge( |
| 489 |
$modules, |
| 490 |
$this->get_active_pro_reports_as_objects() |
| 491 |
); |
| 492 |
} |
| 493 |
|
| 494 |
foreach ( $modules as $module ) { |
| 495 |
if ( $module->maybe_send() ) { |
| 496 |
$module->send(); |
| 497 |
} |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* Get available user roles with user count. |
| 503 |
* |
| 504 |
* @return array Return user roles with user count. |
| 505 |
*/ |
| 506 |
public function get_user_roles() { |
| 507 |
$user_roles = count_users(); |
| 508 |
|
| 509 |
if ( isset( $user_roles['avail_roles'] ) ) { |
| 510 |
foreach ( $user_roles['avail_roles'] as $key => $value ) { |
| 511 |
if ( 0 === $value ) { |
| 512 |
unset( $user_roles['avail_roles'][ $key ] ); |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
return $user_roles; |
| 518 |
} |
| 519 |
} |
| 520 |
|