RestApi
1 month ago
LogHandler.php
10 months ago
LogListingRepository.php
1 month ago
LogRepository.php
2 weeks ago
LoggerFactory.php
10 months ago
LogsDownload.php
2 weeks ago
PluginVersionProcessor.php
3 years ago
index.php
3 years ago
LogsDownload.php
157 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Logging; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\AccessControl; |
| 9 | use MailPoet\WP\Functions as WPFunctions; |
| 10 | |
| 11 | class LogsDownload { |
| 12 | private const ACTION = 'mailpoet_download_logs'; |
| 13 | private const NONCE_ACTION = 'mailpoet_download_logs_nonce'; |
| 14 | private const MAX_LOGS = 50000; |
| 15 | |
| 16 | /** @var LogRepository */ |
| 17 | private $logRepository; |
| 18 | |
| 19 | /** @var WPFunctions */ |
| 20 | private $wp; |
| 21 | |
| 22 | public function __construct( |
| 23 | LogRepository $logRepository, |
| 24 | WPFunctions $wp |
| 25 | ) { |
| 26 | $this->logRepository = $logRepository; |
| 27 | $this->wp = $wp; |
| 28 | } |
| 29 | |
| 30 | public function initialize(): void { |
| 31 | $this->wp->addAction('admin_post_' . self::ACTION, [$this, 'handle']); |
| 32 | } |
| 33 | |
| 34 | public function handle(): void { |
| 35 | if (!$this->wp->currentUserCan(AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN)) { |
| 36 | $this->wp->wpDie(esc_html__('You do not have permission to download logs.', 'mailpoet'), '', ['response' => 403]); |
| 37 | } |
| 38 | |
| 39 | $nonce = $this->getStringParam('_wpnonce') ?? ''; |
| 40 | if (!$this->wp->wpVerifyNonce($nonce, self::NONCE_ACTION)) { |
| 41 | $this->wp->wpDie(esc_html__('Security check failed.', 'mailpoet'), '', ['response' => 403]); |
| 42 | } |
| 43 | |
| 44 | $from = $this->getStringParam('from'); |
| 45 | $to = $this->getStringParam('to'); |
| 46 | $search = $this->getStringParam('search'); |
| 47 | |
| 48 | $filter = []; |
| 49 | $dateFrom = $this->parseDateParam($from); |
| 50 | if ($dateFrom instanceof \DateTimeImmutable) { |
| 51 | $filter['from'] = $dateFrom->format('Y-m-d'); |
| 52 | } |
| 53 | $dateTo = $this->parseDateParam($to); |
| 54 | if ($dateTo instanceof \DateTimeImmutable) { |
| 55 | $filter['to'] = $dateTo->format('Y-m-d'); |
| 56 | } |
| 57 | $names = $this->parseStringArrayParam('name'); |
| 58 | if ($names) { |
| 59 | $filter['name'] = $names; |
| 60 | } |
| 61 | $levels = $this->parseIntArrayParam('level'); |
| 62 | if ($levels) { |
| 63 | $filter['level'] = $levels; |
| 64 | } |
| 65 | if ($search === '') { |
| 66 | $search = null; |
| 67 | } |
| 68 | |
| 69 | $logs = $this->logRepository->getLogsForExport($filter, $search, self::MAX_LOGS); |
| 70 | |
| 71 | $filename = 'mailpoet-logs'; |
| 72 | if (isset($filter['from'])) { |
| 73 | $filename .= '-from-' . $filter['from']; |
| 74 | } |
| 75 | if (isset($filter['to'])) { |
| 76 | $filename .= '-to-' . $filter['to']; |
| 77 | } |
| 78 | $filename .= '.txt'; |
| 79 | |
| 80 | header('Content-Type: text/plain; charset=utf-8'); |
| 81 | header('Content-Disposition: attachment; filename="' . $filename . '"'); |
| 82 | header('Cache-Control: no-cache, no-store, must-revalidate'); |
| 83 | header('Pragma: no-cache'); |
| 84 | header('Expires: 0'); |
| 85 | |
| 86 | $count = 0; |
| 87 | foreach ($logs as $log) { |
| 88 | $createdAt = $log['created_at'] ?? 'N/A'; |
| 89 | $name = $log['name'] ?? ''; |
| 90 | $message = $log['message'] ?? ''; |
| 91 | // Output is a text/plain attachment download, not HTML; HTML-escaping would corrupt the log content. |
| 92 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 93 | echo '[' . $createdAt . '] [' . $name . '] ' . $message . "\n"; |
| 94 | // Stream to the client as we go so the output buffer doesn't accumulate |
| 95 | // the whole file in memory; the repository already pages the DB rows. |
| 96 | $count++; |
| 97 | if (($count % 1000) === 0) { |
| 98 | flush(); |
| 99 | } |
| 100 | } |
| 101 | exit; |
| 102 | } |
| 103 | |
| 104 | private function getStringParam(string $key): ?string { |
| 105 | if (!isset($_GET[$key]) || !is_string($_GET[$key])) { |
| 106 | return null; |
| 107 | } |
| 108 | return sanitize_text_field(wp_unslash($_GET[$key])); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @return string[] |
| 113 | */ |
| 114 | private function parseStringArrayParam(string $key): array { |
| 115 | if (!isset($_GET[$key]) || !is_array($_GET[$key])) { |
| 116 | return []; |
| 117 | } |
| 118 | $values = array_filter( |
| 119 | array_map('sanitize_text_field', wp_unslash($_GET[$key])), |
| 120 | static function (string $value): bool { |
| 121 | return $value !== ''; |
| 122 | } |
| 123 | ); |
| 124 | return array_values(array_unique($values)); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @return int[] |
| 129 | */ |
| 130 | private function parseIntArrayParam(string $key): array { |
| 131 | if (!isset($_GET[$key]) || !is_array($_GET[$key])) { |
| 132 | return []; |
| 133 | } |
| 134 | $values = array_map('intval', wp_unslash($_GET[$key])); |
| 135 | return array_values(array_unique($values)); |
| 136 | } |
| 137 | |
| 138 | public static function createNonce(WPFunctions $wp): string { |
| 139 | return $wp->wpCreateNonce(self::NONCE_ACTION); |
| 140 | } |
| 141 | |
| 142 | private function parseDateParam(?string $value): ?\DateTimeImmutable { |
| 143 | if ($value === null || $value === '') { |
| 144 | return null; |
| 145 | } |
| 146 | $date = \DateTimeImmutable::createFromFormat('!Y-m-d', $value); |
| 147 | if (!$date) { |
| 148 | return null; |
| 149 | } |
| 150 | $errors = \DateTimeImmutable::getLastErrors(); |
| 151 | if (is_array($errors) && ($errors['warning_count'] > 0 || $errors['error_count'] > 0)) { |
| 152 | return null; |
| 153 | } |
| 154 | return $date; |
| 155 | } |
| 156 | } |
| 157 |