*/ function sp_sendpier_blocked_attachment_extensions(): array { return [ 'ade', 'adp', 'apk', 'app', 'appx', 'asp', 'aspx', 'bat', 'cab', 'cer', 'chm', 'cmd', 'com', 'cpl', 'dll', 'dmg', 'exe', 'gadget', 'hta', 'img', 'ins', 'iso', 'isp', 'jar', 'jnlp', 'js', 'jse', 'lib', 'lnk', 'mde', 'msc', 'msi', 'msix', 'msp', 'mst', 'nsh', 'pif', 'ps1', 'ps1xml', 'ps2', 'ps2xml', 'psc1', 'psc2', 'psm1', 'reg', 'scf', 'scr', 'sct', 'shb', 'sys', 'vb', 'vbe', 'vbs', 'vhd', 'vxd', 'wsc', 'wsf', 'wsh', ]; } /** * Houd van een opgegeven naam alleen een veilige bestandsnaam over. * * Zelfde regels als `sanitize_attachment_filename()` in de Send API: * padscheidingstekens, aanhalingstekens en stuurtekens verdwijnen, en punten of * spaties achteraan gaan eraf — `factuur.exe.` omzeilt op Windows anders de * extensiecontrole. Blijft er niets over, dan is de naam ongeldig. */ function sp_sendpier_attachment_filename(string $value): string { $name = str_replace('\\', '/', $value); $parts = explode('/', $name); $name = (string) end($parts); // Stuurtekens en het aanhalingsteken breken de Content-Disposition-header. $name = (string) preg_replace('#["\x00-\x1f\x7f]#', '', $name); $name = trim($name); return rtrim($name, ". \t"); } /** * De laatste extensie in kleine letters, of een lege string. * * Bewust de láátste: `factuur.pdf.exe` is een .exe, geen .pdf. */ function sp_sendpier_attachment_extension(string $filename): string { $base = sp_sendpier_attachment_filename($filename); if (strpos($base, '.') === false) { return ''; } $parts = explode('.', $base); return strtolower((string) end($parts)); } /** * Wordt dit bestandstype door de Send API geweigerd? */ function sp_sendpier_attachment_type_is_blocked(string $filename): bool { $extension = sp_sendpier_attachment_extension($filename); return $extension !== '' && in_array($extension, sp_sendpier_blocked_attachment_extensions(), true); } /** * Raad het content-type uit de extensie. * * De Send API doet dit desnoods zelf, maar een expliciet content_type maakt de * mail voorspelbaar: de ontvangende client hoeft dan niet te gokken en het staat * ook in de wachtrij met niet-verzonden berichten. */ function sp_sendpier_attachment_content_type(string $filename): string { $types = [ 'pdf' => 'application/pdf', 'csv' => 'text/csv', 'txt' => 'text/plain', 'ics' => 'text/calendar', 'html' => 'text/html', 'htm' => 'text/html', 'xml' => 'text/xml', 'json' => 'application/json', 'zip' => 'application/zip', 'gz' => 'application/gzip', 'doc' => 'application/msword', 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'xls' => 'application/vnd.ms-excel', 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'ppt' => 'application/vnd.ms-powerpoint', 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'odt' => 'application/vnd.oasis.opendocument.text', 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', 'rtf' => 'application/rtf', 'png' => 'image/png', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'gif' => 'image/gif', 'webp' => 'image/webp', 'svg' => 'image/svg+xml', 'bmp' => 'image/bmp', 'mp3' => 'audio/mpeg', 'mp4' => 'video/mp4', ]; return $types[sp_sendpier_attachment_extension($filename)] ?? 'application/octet-stream'; } /** * Zet de bijlagen uit `wp_mail()` om naar een vaste vorm. * * WordPress accepteert drie schrijfwijzen: één pad als string, een lijst paden, * en (sinds 5.8) een lijst met `bestandsnaam => pad`. Die laatste bepaalt hoe de * bijlage in de mail heet — het pad kan een tijdelijk bestand met een * nietszeggende naam zijn. * * @param mixed $attachments * @return array */ function sp_sendpier_normalize_wp_attachments($attachments): array { if (is_string($attachments)) { $attachments = [$attachments]; } if (!is_array($attachments)) { return []; } $normalized = []; foreach ($attachments as $key => $path) { if (!is_string($path)) { continue; } $path = trim($path); if ($path === '') { continue; } $filename = is_string($key) ? sp_sendpier_attachment_filename($key) : ''; if ($filename === '') { $filename = sp_sendpier_attachment_filename($path); } $normalized[] = ['path' => $path, 'filename' => $filename]; } return $normalized; } /** * Zet genormaliseerde bijlagen terug naar de vorm die `wp_mail()` wil. * * WordPress leest `bestandsnaam => pad` sinds 5.8 (de plugin vraagt 6.0), zodat * de naam in de mail dezelfde blijft als die in de API-payload. Twee bijlagen * met dezelfde naam zouden elkaar in zo'n lijst overschrijven; die gaan op hun * pad mee. * * @param array $files * @return array */ function sp_sendpier_wp_mail_attachment_list(array $files): array { $list = []; foreach ($files as $file) { $path = (string) ($file['path'] ?? ''); $filename = (string) ($file['filename'] ?? ''); if ($path === '') { continue; } if ($filename === '' || isset($list[$filename])) { $list[] = $path; continue; } $list[$filename] = $path; } return $list; } /** * Bouw één bijlage voor de payload van `POST /v1/messages`. * * @param string $filename de naam zoals de ontvanger hem ziet * @param string $contents de ruwe inhoud van het bestand * @return array{filename: string, content: string, content_type: string} */ function sp_sendpier_build_attachment(string $filename, string $contents): array { $filename = sp_sendpier_attachment_filename($filename); return [ 'filename' => $filename, 'content' => base64_encode($contents), 'content_type' => sp_sendpier_attachment_content_type($filename), ]; } /** * Controleer een set bijlagen tegen de grenzen van de Send API. * * Bewust op naam en omvang, vóór het inlezen: een bestand van dertig megabyte * hoeft niet eerst het geheugen in om daarna alsnog geweigerd te worden. * * `$body_bytes` is de omvang van het tekst- en html-deel, zodat de 25 MB over * het hele bericht gaat en niet alleen over de bijlagen — precies zoals * `prepare_attachments()` in de API het meet. * * @param array $items * @return array{ok: bool, reason: string, message: string, filename: string, bytes: int} */ function sp_sendpier_check_attachments(array $items, int $body_bytes = 0): array { $ok = ['ok' => true, 'reason' => '', 'message' => '', 'filename' => '', 'bytes' => 0]; if (empty($items)) { return $ok; } if (count($items) > sp_sendpier_attachment_max_count()) { return [ 'ok' => false, 'reason' => 'attachment_count', 'message' => sprintf( /* translators: 1: het maximum aantal bijlagen, 2: het aantal bijlagen in dit bericht. */ __('SendPier neemt maximaal %1$d bijlagen per bericht aan; dit bericht heeft er %2$d. Verstuur de rest als downloadlink.', 'sendpier'), sp_sendpier_attachment_max_count(), count($items) ), 'filename' => '', 'bytes' => 0, ]; } $total = max(0, $body_bytes); foreach ($items as $item) { $filename = sp_sendpier_attachment_filename((string) ($item['filename'] ?? '')); $bytes = (int) ($item['bytes'] ?? 0); if ($filename === '') { return [ 'ok' => false, 'reason' => 'attachment_unreadable', 'message' => __('Een bijlage heeft geen bruikbare bestandsnaam; SendPier zou het bericht daarom weigeren. Geef het bestand een gewone naam met een extensie, zoals factuur.pdf.', 'sendpier'), 'filename' => '', 'bytes' => 0, ]; } if ($bytes < 0) { return [ 'ok' => false, 'reason' => 'attachment_unreadable', 'message' => sprintf( /* translators: %s is de bestandsnaam van de bijlage. */ __('De bijlage %s kon niet gelezen worden. Controleer of het bestand nog op de server staat.', 'sendpier'), $filename ), 'filename' => $filename, 'bytes' => 0, ]; } if (sp_sendpier_attachment_type_is_blocked($filename)) { return [ 'ok' => false, 'reason' => 'attachment_type', 'message' => sprintf( /* translators: 1: de bestandsnaam van de bijlage, 2: de extensie zonder punt. */ __('SendPier verstuurt geen uitvoerbare bestanden; %1$s is een .%2$s-bestand. Verstuur het als downloadlink.', 'sendpier'), $filename, sp_sendpier_attachment_extension($filename) ), 'filename' => $filename, 'bytes' => $bytes, ]; } $total += $bytes; if ($total > sp_sendpier_attachment_max_bytes()) { return [ 'ok' => false, 'reason' => 'attachment_too_large', 'message' => sprintf( /* translators: 1: het maximum in megabytes, 2: de bestandsnaam waarbij de grens overschreden werd. */ __('Het bericht is met de bijlagen groter dan %1$d MB; daarboven neemt SendPier het niet aan (bij %2$s). Verstuur grote bestanden via een downloadlink.', 'sendpier'), (int) (sp_sendpier_attachment_max_bytes() / (1024 * 1024)), $filename ), 'filename' => $filename, 'bytes' => $bytes, ]; } } return ['ok' => true, 'reason' => '', 'message' => '', 'filename' => '', 'bytes' => $total - max(0, $body_bytes)]; } /** * De omvang van de bijlagen in een payload, ná decodering. * * @param array $attachments */ function sp_sendpier_attachments_decoded_bytes(array $attachments): int { $bytes = 0; foreach ($attachments as $attachment) { if (!is_array($attachment) || !isset($attachment['content']) || !is_string($attachment['content'])) { continue; } // De omvang volgt uit de base64-lengte; decoderen om te tellen zou het // hele bestand nog een keer in het geheugen zetten. $encoded = (string) preg_replace('#\s+#', '', $attachment['content']); $padding = substr_count(substr($encoded, -2), '='); $bytes += max(0, (int) (strlen($encoded) / 4) * 3 - $padding); } return $bytes; } /** * Leesbare opsomming van de bijlagen, voor de wachtrij en de diagnose. * * @param array $attachments */ function sp_sendpier_attachments_summary(array $attachments): string { $parts = []; foreach ($attachments as $attachment) { if (!is_array($attachment)) { continue; } $filename = trim((string) ($attachment['filename'] ?? '')); if ($filename === '') { continue; } $bytes = isset($attachment['bytes']) ? (int) $attachment['bytes'] : sp_sendpier_attachments_decoded_bytes([$attachment]); $parts[] = $bytes > 0 ? $filename . ' (' . sp_sendpier_attachment_size($bytes) . ')' : $filename; } return implode(', ', $parts); } /** * Leesbare omvang van een bijlage. * * Zelfde schrijfwijze als `sp_sendpier_wachtrij_body_size()`, met megabytes * erbij: een bijlage van tien megabyte als "10240 kB" leest niemand. */ function sp_sendpier_attachment_size(int $bytes): string { $bytes = max(0, $bytes); if ($bytes < 1024) { return $bytes . ' B'; } if ($bytes < 1024 * 1024) { return round($bytes / 1024, 1) . ' kB'; } return round($bytes / (1024 * 1024), 1) . ' MB'; }