Files
html/api/spam-score.php
2026-04-12 22:57:03 +02:00

30 lines
2.1 KiB
PHP

<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') die(json_encode(['error'=>'POST']));
$data = json_decode(file_get_contents('php://input'), true) ?: $_POST;
$subject = $data['subject'] ?? '';
$html = $data['html'] ?? '';
$score = 0; $issues = [];
// Subject checks
if (preg_match('/[A-Z]{5,}/', $subject)) { $score += 15; $issues[] = 'Subject has ALL CAPS'; }
if (preg_match('/!{2,}/', $subject)) { $score += 10; $issues[] = 'Multiple exclamation marks'; }
if (preg_match('/free|winner|congratulations|urgent|act now|limited time/i', $subject)) { $score += 20; $issues[] = 'Spam trigger words in subject'; }
if (strlen($subject) > 80) { $score += 5; $issues[] = 'Subject too long (>80 chars)'; }
if (strlen($subject) < 10) { $score += 10; $issues[] = 'Subject too short (<10 chars)'; }
// HTML checks
if (preg_match('/color:\s*red|font-size:\s*[3-9]\d/i', $html)) { $score += 10; $issues[] = 'Aggressive styling (red/large fonts)'; }
if (substr_count(strtolower($html), 'click here') > 0) { $score += 10; $issues[] = '"Click here" link text'; }
if (preg_match_all('/<a\s/i', $html, $m) && count($m[0]) > 5) { $score += 10; $issues[] = 'Too many links (>5)'; }
if (preg_match_all('/<img/i', $html, $m) && count($m[0]) > 10) { $score += 5; $issues[] = 'Too many images (>10)'; }
if (stripos($html, 'unsubscribe') === false) { $score += 15; $issues[] = 'No unsubscribe link'; }
$textRatio = strlen(strip_tags($html)) / max(strlen($html), 1);
if ($textRatio < 0.3 && strlen($html) > 100) { $score += 10; $issues[] = 'Low text-to-HTML ratio (<30%)'; }
if (preg_match('/viagra|casino|lottery|bitcoin|crypto.*invest/i', $html)) { $score += 30; $issues[] = 'High-risk spam content detected'; }
$score = min($score, 100);
$verdict = $score <= 15 ? 'clean' : ($score <= 40 ? 'warning' : 'danger');
echo json_encode(['ok'=>1, 'score'=>$score, 'verdict'=>$verdict, 'issues'=>$issues, 'tips'=>$score > 15 ? ['Add unsubscribe link','Avoid ALL CAPS','Use natural language','Limit links to 3-5','Include plain text version'] : ['Email looks good!']]);