HEX
Server: nginx/1.24.0
System: Linux webserver-one 6.8.0-101-generic #101-Ubuntu SMP PREEMPT_DYNAMIC Mon Feb 9 10:15:05 UTC 2026 x86_64
User: www-data (33)
PHP: 8.4.18
Disabled: NONE
Upload Files
File: /srv/www/rectt-csmcri.res.in/public_html/mail_pdf.php
<?php

function generateApplicationPDF($user_id, $application_id, $conn): string
{
    require_once "sites/config/config.php";
    require_once dirname(__DIR__) . "/vendor/autoload.php";


    $user_id = $_SESSION["user_id"];

    $application_id = $_SESSION["application_id"];


    function esc($string)
    {
        return htmlspecialchars($string, ENT_QUOTES, "UTF-8");
    }

    $primary = [];

    $result = $conn->query(
        "SELECT * FROM primary_details WHERE user_id = $user_id"
    );

    if ($result && $result->num_rows > 0) {
        $primary = $result->fetch_assoc();
    }

    $application_creation_date = null; // Initialize as null

    $result = $conn->query(
        "SELECT created_at FROM applications WHERE user_id = $user_id LIMIT 1"
    );

    if ($result && $result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $application_creation_date = $row['created_at'];
    }

    $relatives = [];

    $result = $conn->query("SELECT * FROM relatives WHERE user_id = $user_id");

    if ($result && $result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $relatives[] = $row;
        }
    }

    $application = [];
    $advertisement_id = null;
    $result = $conn->query("SELECT * FROM applications WHERE id = $application_id");

    if ($result && $result->num_rows > 0) {
        $application = $result->fetch_assoc();
        $advertisement_id = $application['advertisement_id'];
    }

    // Fetch posts applied

    $posts = [];

    $result = $conn->query("

SELECT p.post_title, p.category

FROM application_posts ap

JOIN posts p ON ap.post_id = p.id

WHERE ap.application_id = $application_id

");

    if ($result && $result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $posts[] = $row;
        }
    }

    // Fetch qualifications

    $qualifications = [];

    $result = $conn->query(
        "SELECT * FROM qualifications WHERE application_id = $application_id"
    );

    if ($result && $result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $qualifications[] = $row;
        }
    }

    // Fetch uploads (photo and signature)

    $uploads = [
        "photograph" => "",

        "signature" => "",
    ];

    $result = $conn->query("

SELECT document_type, file_path

FROM uploads

WHERE application_id = $application_id

AND document_type IN ('photograph', 'signature')

");

    if ($result && $result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $uploads[$row["document_type"]] = $row["file_path"];
        }
    }
    $raw_date = $application_creation_date;
    $date_only = (new DateTime($raw_date))->format('Ymd');
    $registration_id = $date_only  . $user_id . $application_id . "A" . $advertisement_id;
    $_SESSION["registration_id"] = $registration_id;

    // Build HTML content
    $data = <<<HTML

<style>

body { font-family: Arial, sans-serif; font-size: 11pt; color: #333; }

h2 { text-align: center; font-size: 18pt; color: #003366; margin-bottom: 20px; }

table { width: 100%; border-collapse: collapse; margin-bottom: 25px; }

th, td { padding: 8px 10px; vertical-align: top; }

tr:nth-child(even) td { background-color: #f9f9f9; }

.section-header {

background: #e0ebf5;

color: #003366;

font-size: 13pt;

font-weight: bold;

padding: 10px;

border: 1px solid #c0d3eb;

margin-top: 20px;

}

.label { width: 35%; font-weight: bold; color: #333; }

.value { width: 65%; }

.photo-sign-container {

display: flex;

justify-content: center;

gap: 60px;

margin-top: 25px;

}

/* Simplified CSS for images, relying on HTML attributes for size */

.photo-box img,

.signature-box img {

border: 1px solid #aaa;

margin-top: 5px;

box-shadow: 1px 1px 3px rgba(0,0,0,0.05);


}



.qual-box {

border: 1px solid #ccc;

margin-bottom: 15px;

padding: 8px;

border-radius: 4px;

}

.qual-box table { margin-bottom: 0; }

</style>


<div style="text-align: center; margin-bottom: 10px;">
    <img src="sites/images/Header-logo-CSMCRI-New.png" alt="Header Logo" style="max-width: 100%; height: auto;">
</div>

<div style="text-align: center; margin-bottom: 10px;">
    <p><strong>Registration ID:</strong> {$registration_id}</p>
    <p><strong>Advertisement ID:</strong> {$advertisement_id}</p>
    <p><strong>Lab Name:</strong> CSIR-CSMCRI</p>
</div>

<h2>Application Summary</h2>


<div class='section-header'>Posts Applied For</div>

<table border='0'>

HTML;

    foreach ($posts as $post) {
        $data .=
            "<tr><td colspan='2'>" .
            esc($post["post_title"]) .
            " (" .
            esc($post["category"]) .
            ")</td></tr>";
    }

    $data .= "</table>";

    $data .= "<div class='section-header'>Primary Details</div><table border='0'>";

    foreach ($primary as $key => $value) {
        if (!in_array($key, ["id", "user_id"])) {
            $label = ucwords(str_replace("_", " ", $key));

            $data .=
                "<tr><td class='label'>" .
                esc($label) .
                "</td><td class='value'>" .
                esc($value) .
                "</td></tr>";
        }
    }

    $data .= "</table>";

    $data .=
        "<div class='section-header'>Relatives in CSIR</div><table border='0'>";

    if (!empty($relatives)) {
        foreach ($relatives as $index => $rel) {
            $data .=
                "<tr><td colspan='2'><strong>Relative " .
                ($index + 1) .
                "</strong></td></tr>";

            foreach ($rel as $key => $value) {
                if (!in_array($key, ["id", "user_id"])) {
                    $label = ucwords(str_replace("_", " ", $key));

                    $data .=
                        "<tr><td class='label' style='padding-left:20px;'>" .
                        esc($label) .
                        "</td><td class='value'>" .
                        esc($value) .
                        "</td></tr>";
                }
            }
        }
    } else {
        $data .= "<tr><td colspan='2'>No relatives in CSIR.</td></tr>";
    }

    $data .= "</table>";

    $data .=
        "<div class='section-header'>Application Information</div><table border='0'>";

    $data .=
        "<tr><td class='label'>Computer Knowledge</td><td class='value'>" .
        esc($application["computer_knowledge"]) .
        "</td></tr>";

    $data .=
        "<tr><td class='label'>Payment Reference No</td><td class='value'>" .
        esc($application["payment_reference_no"]) .
        "</td></tr>";

    $data .= "</table>";

    $data .= "<div class='section-header'>Educational Qualifications</div>";

    foreach ($qualifications as $qual) {
        $data .=
            "<div class='qual-box'><table border='0'>

<tr><td class='label'>Qualification Type</td><td class='value'>" .
            esc($qual["qualification_type"]) .
            "</td></tr>

<tr><td class='label'>School / College</td><td class='value'>" .
            esc($qual["school_name"]) .
            "</td></tr>

<tr><td class='label'>Board / University</td><td class='value'>" .
            esc($qual["board_or_university"]) .
            "</td></tr>

<tr><td class='label'>Subjects</td><td class='value'>" .
            esc($qual["subjects"]) .
            "</td></tr>

<tr><td class='label'>Marks</td><td class='value'>" .
            esc($qual["marks"]) .
            " (" .
            esc($qual["scale"]) .
            ")</td></tr>

<tr><td class='label'>Passing Date</td><td class='value'>" .
            esc($qual["pass_date"]) .
            "</td></tr>

</table></div>";
    }

    $data .= <<<HTML

<div class='section-header'>Declaration</div>

<table border="0">

<tr><td colspan="2">

I, hereby declare that the particulars given in the application form are true, complete and appropriate to the best of my knowledge and belief.

If any information is found false or inappropriate, or any ineligibility is detected before or after selection, my candidature is liable to be cancelled.

<br><br>

☑ <strong>I agree</strong>

</td></tr>

</table>


<div class='section-header'>Photograph and Signature</div>

<div class='photo-sign-container'> <div class='photo-box'> <img src="{$uploads["photograph"]}" alt="Photograph" width="120" height="auto">

</div>

<div class='signature-box'> <img src="{$uploads["signature"]}" alt="Signature" width="150" height="60"> </div>

</div>

HTML;


    // Use the custom temporary directory setting
    $mpdf = new \Mpdf\Mpdf([
        "tempDir" => dirname(__DIR__) . "/tmp",
    ]);
    $outputDir = "sites/upload/mail";
    $outputFilename = "application_summary_" . $registration_id . ".pdf";
    $outputPath = $outputDir . "/" . $outputFilename;

    if (!is_dir($outputDir)) {
        if (!mkdir($outputDir, 0777, true)) {
            die("Failed to create output directory: " . $outputDir);
        }
    }
    if (!is_writable($outputDir)) {
        die("Output directory is not writable: " . $outputDir);
    }

    $mpdf->WriteHTML($data);
    $mpdf->Output($outputPath, "F"); // Save instead of download


    // Adding the files to the document folder
    $uploads = [];

    $result = $conn->query("
    SELECT document_type, file_path
    FROM uploads
    WHERE application_id = $application_id
");

    if ($result && $result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $uploads[$row["document_type"]] = $row["file_path"];
        }
    }


    // Create documents directory if it doesn't exist
    $targetDir = "sites/documents/" . $registration_id . "/";
    if (!is_dir($targetDir)) {
        mkdir($targetDir, 0777, true);
    }
    // Move files to the new directory
    foreach ($uploads as $docType => $filePathRelative) {
        $sourcePath = realpath(__DIR__ . "/" . $filePathRelative);
        if ($sourcePath && file_exists($sourcePath)) {
            $originalFileName = basename($sourcePath);
            $destinationPath = $targetDir . $originalFileName;
            // Copy file to documents directory
            if (!copy($sourcePath, $destinationPath)) {
                throw new Exception("Failed to copy file for {$docType} to {$destinationPath}");
            }
        } else {
            error_log("Missing file for {$docType}: {$sourcePath}");
        }
    }
    // Also copy the file at $outputPath into the same folder
    if (isset($outputPath) && file_exists($outputPath)) {
        $finalFileName = basename($outputPath);
        $finalDestination = $targetDir . $finalFileName;

        if (!copy($outputPath, $finalDestination)) {
            throw new Exception("Failed to copy final file to {$finalDestination}");
        }
    } else {
        error_log("Output file not found or not set: {$outputPath}");
    }

    return $outputPath;
}