<?php
session_start();
set_time_limit(0);

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Access-Control-Allow-Origin: *");

class UchihaMailer {
    private $password = "MaDaRa";
    private $config = [
        'version' => "1.0",
        'clan' => "Uchiha"
    ];

    public function __construct() {
        $this->authenticate();
    }

    private function authenticate() {
        $sessioncode = md5(__FILE__);
        
        if (!isset($_SESSION[$sessioncode])) {
            $_SESSION[$sessioncode] = '';
        }

        if (!empty($this->password) && $_SESSION[$sessioncode] != $this->password) {
            if (isset($_REQUEST['pass']) && $_REQUEST['pass'] == $this->password) {
                $_SESSION[$sessioncode] = $this->password;
            } else {
                $this->showPasswordForm();
                exit;
            }
        }
    }

    private function showPasswordForm() {
        echo '<!DOCTYPE html>
        <html>
        <head>
            <title>Sharingan Access Required</title>
            <style>
                body { 
                    font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; 
                    max-width: 400px; 
                    margin: 100px auto; 
                    padding: 20px; 
                    background: linear-gradient(135deg, #0c0c0c 0%, #1a1a1a 100%);
                    color: #c10000;
                }
                .login-form { 
                    background: rgba(0, 0, 0, 0.8); 
                    padding: 40px; 
                    border-radius: 10px; 
                    border: 2px solid #c10000;
                    box-shadow: 0 0 20px rgba(193, 0, 0, 0.3);
                    text-align: center;
                }
                .sharingan {
                    width: 80px;
                    height: 80px;
                    background: radial-gradient(circle, #c10000 0%, #8b0000 100%);
                    border: 3px solid #ff4444;
                    border-radius: 50%;
                    margin: 0 auto 20px;
                    position: relative;
                    box-shadow: 0 0 15px #c10000;
                }
                .sharingan::before {
                    content: "";
                    position: absolute;
                    top: 20px;
                    left: 20px;
                    width: 40px;
                    height: 40px;
                    border: 2px solid #000;
                    border-radius: 50%;
                    background: #000;
                }
                .sharingan::after {
                    content: "●";
                    position: absolute;
                    top: 35px;
                    left: 35px;
                    color: #000;
                    font-size: 10px;
                }
                input[type="password"] { 
                    width: 100%; 
                    padding: 12px; 
                    margin: 20px 0; 
                    border: 1px solid #c10000; 
                    border-radius: 5px; 
                    background: #1a1a1a;
                    color: #fff;
                    text-align: center;
                }
                input[type="submit"] { 
                    background: linear-gradient(135deg, #c10000 0%, #8b0000 100%); 
                    color: #000; 
                    padding: 12px 30px; 
                    border: none; 
                    border-radius: 5px; 
                    cursor: pointer; 
                    font-weight: bold;
                    border: 1px solid #ff4444;
                }
                input[type="submit"]:hover { 
                    background: linear-gradient(135deg, #ff0000 0%, #c10000 100%);
                    box-shadow: 0 0 10px #ff4444;
                }
                h2 {
                    color: #c10000;
                    text-shadow: 0 0 10px rgba(193, 0, 0, 0.5);
                    margin-bottom: 20px;
                }
            </style>
        </head>
        <body>
            <div class="login-form">
                <div class="sharingan"></div>
                <h2>Sharingan Access Required</h2>
                <form method="post">
                    <input type="password" name="pass" placeholder="Enter Mangekyou Password" required>
                    <input type="submit" value="Activate Sharingan">
                </form>
            </div>
        </body>
        </html>';
        exit;
    }

    public function handleRequest() {
        if (isset($_POST['action']) && $_POST['action'] == "send") {
            return $this->processEmail();
        } else {
            return $this->showEmailForm();
        }
    }

    private function processEmail() {
        error_reporting(0);
        
        $redirectlist = array_unique(explode("\n", $_POST['redirectlist'] ?? ''));
        $message = $_POST['messageLetter'] ?? '';
        $subject = $_POST['messageSubject'] ?? '';
        $realname = $_POST['senderName'] ?? '';
        $from = $_POST['senderEmail'] ?? '';
        $to = $_POST['recipient'] ?? '';

        $headers  = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=UTF-8\r\n";

        $email = explode("\n", $to);
        $headers .= "From: ".$realname." <".$from.">\r\n";
        $message = stripslashes($message);

        $i = 0;
        $count = 1;
        $redi = 0;
        $countred = count($redirectlist);
        $output = "";
        $successCount = 0;
        
        while(isset($email[$i]) && !empty(trim($email[$i]))) {
            if($redi >= $countred)
                $redi = 0;
            
            $redirect = '';
            if (!empty($redirectlist) && isset($redirectlist[$redi])) {
                $redirect = $redirectlist[$redi].'/'.md5(rand(0,99999)+rand(0,9999));
            }
            
            $ok = "ok";
            $messgb = "\r\n<!-- \r\n Here we go some texte here to fuck security ".md5(rand(0,99999)+rand(0,99999))." \r\n what do you think about it".md5(rand(0,99999)+rand(0,99999))."\r\n -->";
            
            if (!empty($redirect)) {
                $messgb .= str_replace('[red]', $redirect, $message);
            } else {
                $messgb .= $message;
            }

            if(mail(trim($email[$i]), $subject, $messgb, $headers)) {
                $output .= "* Number: $count <b>".trim($email[$i])."</b> <font color=green>OK</font><br>";
                $successCount++;
            } else {
                $output .= "* Number: $count <b>".trim($email[$i])."</b> <font color=red>SEND ERROR</font><br>";
            }
            $i++;
            $count++;
            $redi++;
        }
        
        $count--;
        if($successCount > 0) {
            $output .= "<br><strong>AMATERASU COMPLETED - Sent to $successCount targets</strong>";
        }
        
        return $this->showEmailForm($output, $successCount > 0 ? "success" : "error");
    }

    private function showEmailForm($message = "", $messageType = "error") {
        echo '<!DOCTYPE html>
        <html>
        <head>
            <title>Uchiha Clan Mailer</title>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <style>
                * { 
                    box-sizing: border-box; 
                    margin: 0; 
                    padding: 0; 
                }
                body { 
                    font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; 
                    background: linear-gradient(135deg, #0c0c0c 0%, #1a1a1a 50%, #0c0c0c 100%);
                    color: #c10000;
                    min-height: 100vh;
                    padding: 20px;
                }
                .container { 
                    max-width: 1200px; 
                    margin: 0 auto; 
                    background: rgba(0, 0, 0, 0.85);
                    border-radius: 15px;
                    border: 2px solid #c10000;
                    box-shadow: 0 0 30px rgba(193, 0, 0, 0.3);
                    overflow: hidden;
                }
                .header {
                    background: linear-gradient(135deg, #1a0000 0%, #330000 100%);
                    padding: 30px;
                    text-align: center;
                    border-bottom: 3px solid #c10000;
                    position: relative;
                }
                .sharingan-header {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    gap: 20px;
                    margin-bottom: 15px;
                }
                .sharingan {
                    width: 60px;
                    height: 60px;
                    background: radial-gradient(circle, #c10000 0%, #8b0000 100%);
                    border: 2px solid #ff4444;
                    border-radius: 50%;
                    position: relative;
                    box-shadow: 0 0 15px #c10000;
                    animation: pulse 2s infinite;
                }
                .sharingan::before {
                    content: "";
                    position: absolute;
                    top: 15px;
                    left: 15px;
                    width: 30px;
                    height: 30px;
                    border: 2px solid #000;
                    border-radius: 50%;
                    background: #000;
                }
                .sharingan::after {
                    content: "●";
                    position: absolute;
                    top: 25px;
                    left: 25px;
                    color: #000;
                    font-size: 8px;
                }
                @keyframes pulse {
                    0% { box-shadow: 0 0 15px #c10000; }
                    50% { box-shadow: 0 0 25px #ff0000; }
                    100% { box-shadow: 0 0 15px #c10000; }
                }
                h1 {
                    color: #ff4444;
                    text-shadow: 0 0 10px rgba(255, 0, 0, 0.7);
                    font-size: 2.5em;
                    margin: 0;
                }
                .subtitle {
                    color: #ff8888;
                    font-size: 1.2em;
                    margin-top: 10px;
                }
                .content {
                    padding: 30px;
                }
                .form-group { 
                    margin-bottom: 25px; 
                }
                label { 
                    display: block; 
                    margin-bottom: 8px; 
                    font-weight: bold;
                    color: #ff4444;
                    text-shadow: 0 0 5px rgba(255, 0, 0, 0.3);
                }
                input[type="text"], input[type="email"], input[type="password"], select, textarea { 
                    width: 100%; 
                    padding: 12px 15px; 
                    margin: 5px 0; 
                    border: 1px solid #c10000; 
                    border-radius: 8px; 
                    background: rgba(26, 26, 26, 0.8);
                    color: #fff;
                    font-size: 14px;
                    transition: all 0.3s ease;
                }
                input:focus, select:focus, textarea:focus {
                    outline: none;
                    border-color: #ff4444;
                    box-shadow: 0 0 10px rgba(255, 68, 68, 0.3);
                    background: rgba(40, 40, 40, 0.8);
                }
                textarea { 
                    min-height: 120px; 
                    resize: vertical;
                }
                .recipients-textarea {
                    min-height: 150px;
                    font-family: monospace;
                }
                .btn { 
                    background: linear-gradient(135deg, #c10000 0%, #8b0000 100%); 
                    color: #000; 
                    padding: 15px 30px; 
                    border: none; 
                    border-radius: 8px; 
                    cursor: pointer; 
                    font-size: 16px;
                    font-weight: bold;
                    margin-right: 10px;
                    border: 1px solid #ff4444;
                    transition: all 0.3s ease;
                    text-transform: uppercase;
                    letter-spacing: 1px;
                }
                .btn:hover { 
                    background: linear-gradient(135deg, #ff0000 0%, #c10000 100%);
                    box-shadow: 0 0 15px rgba(255, 68, 68, 0.5);
                    transform: translateY(-2px);
                }
                .btn-secondary {
                    background: linear-gradient(135deg, #333 0%, #555 100%);
                    border: 1px solid #666;
                }
                .btn-secondary:hover {
                    background: linear-gradient(135deg, #555 0%, #777 100%);
                    box-shadow: 0 0 15px rgba(102, 102, 102, 0.5);
                }
                .message { 
                    padding: 20px; 
                    margin: 20px 0; 
                    border-radius: 8px; 
                    font-weight: bold;
                    text-align: center;
                    border: 1px solid;
                    background: rgba(10, 10, 10, 0.9);
                }
                .success { 
                    color: #00ff00; 
                    border-color: #00ff00;
                    text-shadow: 0 0 10px rgba(0, 255, 0, 0.5);
                }
                .error { 
                    color: #ff4444; 
                    border-color: #ff4444;
                    text-shadow: 0 0 10px rgba(255, 68, 68, 0.5);
                }
                .tabs {
                    display: flex;
                    margin-bottom: 25px;
                    border-bottom: 2px solid #c10000;
                    background: rgba(26, 0, 0, 0.5);
                    border-radius: 8px 8px 0 0;
                }
                .tab {
                    padding: 15px 25px;
                    cursor: pointer;
                    background: transparent;
                    color: #ff8888;
                    border: none;
                    border-bottom: 3px solid transparent;
                    transition: all 0.3s ease;
                    font-weight: bold;
                }
                .tab.active {
                    color: #ff4444;
                    border-bottom: 3px solid #ff4444;
                    background: rgba(193, 0, 0, 0.1);
                }
                .tab:hover {
                    color: #ff4444;
                    background: rgba(193, 0, 0, 0.1);
                }
                .tab-content {
                    display: none;
                    padding: 20px;
                    background: rgba(10, 10, 10, 0.5);
                    border-radius: 0 0 8px 8px;
                    border: 1px solid #333;
                    border-top: none;
                }
                .tab-content.active {
                    display: block;
                }
                .help-text {
                    font-size: 12px;
                    color: #ff8888;
                    margin-top: 5px;
                    font-style: italic;
                }
                .form-row {
                    display: flex;
                    gap: 20px;
                }
                .form-row .form-group {
                    flex: 1;
                }
                .variable-buttons {
                    display: flex;
                    gap: 10px;
                    margin: 10px 0;
                    flex-wrap: wrap;
                }
                .var-btn {
                    background: rgba(193, 0, 0, 0.3);
                    color: #ff8888;
                    border: 1px solid #c10000;
                    padding: 8px 15px;
                    border-radius: 5px;
                    cursor: pointer;
                    font-size: 12px;
                    transition: all 0.3s ease;
                }
                .var-btn:hover {
                    background: rgba(255, 68, 68, 0.5);
                    color: #fff;
                }
                .uchiha-note {
                    text-align: center;
                    margin-top: 30px;
                    padding: 15px;
                    background: rgba(193, 0, 0, 0.1);
                    border-radius: 8px;
                    border: 1px solid #c10000;
                    font-style: italic;
                }
                .results {
                    background: rgba(0, 0, 0, 0.8);
                    padding: 15px;
                    border-radius: 8px;
                    border: 1px solid #c10000;
                    margin: 20px 0;
                    max-height: 300px;
                    overflow-y: auto;
                    font-family: monospace;
                    font-size: 12px;
                }
                @media (max-width: 768px) {
                    .form-row {
                        flex-direction: column;
                        gap: 0;
                    }
                    .sharingan-header {
                        flex-direction: column;
                        gap: 10px;
                    }
                }
            </style>
            <script>
                function showTab(tabName) {
                    var tabContents = document.getElementsByClassName("tab-content");
                    for (var i = 0; i < tabContents.length; i++) {
                        tabContents[i].classList.remove("active");
                    }
                    
                    var tabs = document.getElementsByClassName("tab");
                    for (var i = 0; i < tabs.length; i++) {
                        tabs[i].classList.remove("active");
                    }
                    
                    document.getElementById(tabName).classList.add("active");
                    event.currentTarget.classList.add("active");
                }

                function insertVariable(variable) {
                    var textarea = document.getElementById("messageLetter");
                    var start = textarea.selectionStart;
                    var end = textarea.selectionEnd;
                    var text = textarea.value;
                    var newText = text.substring(0, start) + variable + text.substring(end);
                    textarea.value = newText;
                    textarea.focus();
                    textarea.setSelectionRange(start + variable.length, start + variable.length);
                }

                function showExampleRecipients() {
                    var textarea = document.getElementById("recipient");
                    if (textarea.value.trim() === "") {
                        textarea.value = "target1@gmail.com\ntarget2@yahoo.com\ntarget3@hotmail.com";
                    }
                }

                function showExampleRedirects() {
                    var textarea = document.getElementById("redirectlist");
                    if (textarea.value.trim() === "") {
                        textarea.value = "https://example.com/redirect1\nhttps://example.com/redirect2\nhttps://example.com/redirect3";
                    }
                }
            </script>
        </head>
        <body>
            <div class="container">
                <div class="header">
                    <div class="sharingan-header">
                        <div class="sharingan"></div>
                        <h1>UCHIHA CLAN MAILER</h1>
                        <div class="sharingan"></div>
                    </div>
                    <div class="subtitle">Amaterasu Fire Style Email Technique</div>
                </div>
                
                <div class="content">';
                
                if (!empty($message)) {
                    echo '<div class="message ' . $messageType . '">' . $message . '</div>';
                }
                
                echo '<form method="post" enctype="multipart/form-data">
                    <input type="hidden" name="action" value="send">
                    
                    <div class="tabs">
                        <div class="tab active" onclick="showTab(\'targets\')">Target Selection</div>
                        <div class="tab" onclick="showTab(\'message\')">Fire Message</div>
                        <div class="tab" onclick="showTab(\'redirects\')">Redirect Links</div>
                    </div>
                    
                    <div id="targets" class="tab-content active">
                        <div class="form-group">
                            <label for="recipient">Target Emails (One per line)</label>
                            <textarea id="recipient" name="recipient" class="recipients-textarea" placeholder="target1@domain.com&#10;target2@domain.com&#10;target3@domain.com" onfocus="showExampleRecipients()">' . htmlspecialchars($_POST['recipient'] ?? '') . '</textarea>
                            <div class="help-text">Enter multiple email addresses, one per line.</div>
                        </div>
                        
                        <div class="form-row">
                            <div class="form-group">
                                <label for="senderEmail">Sender Email</label>
                                <input type="email" id="senderEmail" name="senderEmail" value="' . htmlspecialchars($_POST['senderEmail'] ?? '') . '" placeholder="uchiha@konoha.com">
                            </div>
                            <div class="form-group">
                                <label for="senderName">Sender Name</label>
                                <input type="text" id="senderName" name="senderName" value="' . htmlspecialchars($_POST['senderName'] ?? '') . '" placeholder="Madara Uchiha">
                            </div>
                        </div>
                    </div>
                    
                    <div id="message" class="tab-content">
                        <div class="form-group">
                            <label for="messageSubject">Message Subject</label>
                            <input type="text" id="messageSubject" name="messageSubject" value="' . htmlspecialchars($_POST['messageSubject'] ?? '') . '" placeholder="The Power of the Sharingan">
                        </div>
                        
                        <div class="form-group">
                            <label>Sharingan Variables:</label>
                            <div class="variable-buttons">
                                <button type="button" class="var-btn" onclick="insertVariable(\'[-time-]\')">Current Time</button>
                                <button type="button" class="var-btn" onclick="insertVariable(\'[-email-]\')">Target Email</button>
                                <button type="button" class="var-btn" onclick="insertVariable(\'[-emailuser-]\')">Username</button>
                                <button type="button" class="var-btn" onclick="insertVariable(\'[-randomstring-]\')">Random String</button>
                                <button type="button" class="var-btn" onclick="insertVariable(\'[-randomnumber-]\')">Random Number</button>
                                <button type="button" class="var-btn" onclick="insertVariable(\'[-randommd5-]\')">Random MD5</button>
                                <button type="button" class="var-btn" onclick="insertVariable(\'[red]\')">Redirect URL</button>
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label for="messageLetter">Message Body (HTML)</label>
                            <textarea id="messageLetter" name="messageLetter" placeholder="<html><body>We, the Uchiha clan, possess the most powerful ocular jutsu... Use [red] for redirect URLs</body></html>">' . htmlspecialchars($_POST['messageLetter'] ?? '') . '</textarea>
                            <div class="help-text">Use [red] in your message to insert redirect URLs. Each recipient will get a unique redirect URL.</div>
                        </div>
                    </div>
                    
                    <div id="redirects" class="tab-content">
                        <div class="form-group">
                            <label for="redirectlist">Redirect URLs (One per line)</label>
                            <textarea id="redirectlist" name="redirectlist" class="recipients-textarea" placeholder="https://your-domain.com/path1&#10;https://your-domain.com/path2&#10;https://your-domain.com/path3" onfocus="showExampleRedirects()">' . htmlspecialchars($_POST['redirectlist'] ?? '') . '</textarea>
                            <div class="help-text">Enter redirect URLs where targets will be sent when they click links containing [red]. URLs will be rotated among recipients.</div>
                        </div>
                        
                        <div class="uchiha-note">
                            "The Redirect Jutsu: When a target clicks a link containing [red], they will be sent to one of these URLs with a unique token appended."
                        </div>
                    </div>
                    
                    <div class="uchiha-note">
                        "In this world, wherever there is light, there are also shadows. As long as the concept of winners exists, there must also be losers."
                    </div>
                    
                    <div style="margin-top: 30px; text-align: center;">
                        <button type="submit" class="btn">🔥 Cast Amaterasu</button>
                        <button type="reset" class="btn btn-secondary">Reset Form</button>
                    </div>
                </form>
            </div>
        </div>
        </body>
        </html>';
    }
}

$mailHandler = new UchihaMailer();
$mailHandler->handleRequest();

?>