fixed filepath issue in import request script.

This commit is contained in:
James Richie
2026-05-21 20:43:16 -04:00
parent 8a3cb15e55
commit 3f3900fef6
3 changed files with 116 additions and 356 deletions
+1 -1
View File
@@ -312,7 +312,7 @@ class Import extends Base {
$statement = $this->connect()->prepare($SQL);
$statement->bindValue(":importjob_file_path", "../{$filepath}", PDO::PARAM_STR);
$statement->bindValue(":importjob_file_path", $filepath, PDO::PARAM_STR);
$statement->bindValue(":importjob_notified", false, PDO::PARAM_BOOL);
$statement->bindValue(":importjob_creator", $this->current_user_name, PDO::PARAM_STR);
$statement->bindValue(":importjob_created", $this->getTimestamp());
-255
View File
@@ -1,255 +0,0 @@
<?php
// ======================
// Import Large CSV Class
// ======================
class ImportLargeCSV extends Base {
// Variables
private $current_user_serial = 0;
private $current_user_id = "";
private $current_user_name = "";
private $current_user_email = "";
private $pagination_size = 20;
private $pagination_page = 1;
// =================
// Class Constructor
// =================
public function __construct() {
set_error_handler(array($this, "displayError"));
date_default_timezone_set(self::TIMEZONE);
$this->current_user_serial = $_SESSION[self::USER_SERIAL] ?? 0;
$this->current_user_id = $_SESSION[self::USER_ID] ?? "";
$this->current_user_name = $_SESSION[self::USER_NAME] ?? "";
$this->current_user_email = $_SESSION[self::USER_EMAIL] ?? "";
$this->pagination_page = $_SESSION[self::PAGINATION_PAGE] ?? 1;
}
// =====================
// Create Import Request
// =====================
public function createImportRequest() {
$subscription_serial = filter_input(INPUT_POST, "subscription_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;
$subscription = (new Subscriptions())->getSubscription($subscription_serial);
if ($subscription->count() === 0) {
$this->logError("Invalid Subscription ({$subscription_serial}): " . __METHOD__);
}
$import_type = filter_input(INPUT_POST, "import_type") ?: "";
$report_type = filter_input(INPUT_POST, "report_type") ?: "";
$importjob_import_type = str_replace(' ', '', $subscription->record->subscription_full_title) . "$import_type";
$importjob_file_name = str_replace(' ', '_', $subscription->record->subscription_full_title) . "_all_records.{$report_type}";
// $importjob_file_name = str_replace('&', 'And', $subscription->record->subscription_full_title) . "_all_records.{$report_type}";
$SQL = "insert into importjobs
( importjob_subscription,
importjob_import_type,
importjob_file_name,
importjob_file_type,
importjob_creator,
importjob_created )
VALUES ( :importjob_subscription,
:importjob_import_type,
:importjob_file_name,
:importjob_file_type,
:importjob_creator,
:importjob_created )";
$statement = $this->connect()->prepare($SQL);
$statement->bindValue(":importjob_subscription", $subscription_serial);
$statement->bindValue(":importjob_import_type", $importjob_import_type);
$statement->bindValue(":importjob_file_name", $importjob_file_name);
$statement->bindValue(":importjob_file_type", strtoupper($report_type));
$statement->bindValue(":importjob_creator", $this->current_user_serial);
$statement->bindValue(":importjob_created", $this->getTimestamp());
$statement->execute();
$this->displayNotice("Your import has been queued. You will receive an notification when it is ready.");
}
// ============
// User Imports
// ============
public function usersImports() {
$imports = $this->getUsersImports();
$popovers = (new Popovers())->getPopovers();
$this->markImportsAsSeen();
$XML = $this->mergeXML($imports, "<XML/>");
$XML = $this->mergeXML($popovers, $XML);
$html = $this->applyXSL($XML, $this->getXSL("viewImports"));
$this->displayContent($html);
}
// =========================
// Mark User Imports as Seen
// =========================
public function markImportsAsSeen() {
$SQL = "update importjobs
set importjob_notified = true
where importjobs.importjob_creator = {$this->current_user_serial}";
$statement = $this->connect()->prepare($SQL);
$statement->execute();
}
// =======================
// Return a Imports Object
// =======================
public function getUsersImports() {
// Find Criteria
$find_importjob = filter_input(INPUT_POST, "find_importjob") ?: "";
$find_words = $this->sanitizeString($find_importjob);
// Show Per Page
$this->pagination_size = filter_input(INPUT_POST, "importjobs_per_page", FILTER_SANITIZE_NUMBER_INT) ?: $this->pagination_size;
// Record Count
$connection = $this->connect();
$SQL = "select count(*) as count
from view_importjobs
where view_importjobs.importjob_creator = {$this->current_user_serial}
and view_importjobs.subscription_full_title regexp :subscription_full_title";
$statement = $connection->prepare($SQL);
$statement->bindValue(":subscription_full_title", $find_words);
$statement->execute();
$recordset = $statement->fetch(PDO::FETCH_ASSOC);
$count = (count($recordset) > 0) ? (integer) $recordset["count"] : (integer) 0;
// Pagination
$pagination = filter_input(INPUT_POST, "pagination") ?: "";
switch ($pagination) {
case "first" : $this->pagination_page = 1;
break;
case "previous" : $this->pagination_page = max(($this->pagination_page - 1), 1);
break;
case "next" : $this->pagination_page = ($this->pagination_page + 1);
break;
case "last" : $this->pagination_page = ($count / $this->pagination_size);
break;
}
$offset = (($this->pagination_page * $this->pagination_size) - $this->pagination_size);
$start = ($offset + 1);
$end = min($count, (($start + $this->pagination_size) - 1));
// Data
$SQL = "select view_importjobs.*
from view_importjobs
where view_importjobs.importjob_creator = {$this->current_user_serial}
and view_importjobs.subscription_full_title regexp :subscription_full_title
limit {$this->pagination_size}
offset {$offset}";
$statement = $connection->prepare($SQL);
$statement->bindValue(":subscription_full_title", $find_words);
$statement->execute();
$importjobs = $this->getTableXML("importjobs", $statement);
// Insert Pagination Attributes
$importjobs->addAttribute("count", $count);
$importjobs->addAttribute("start", $start);
$importjobs->addAttribute("end", $end);
$importjobs->addAttribute("page", $this->pagination_page);
$_SESSION[self::PAGINATION_PAGE] = $this->pagination_page;
return $importjobs;
}
// =====================================
// Return if users imports are completed
// =====================================
public function checkUsersCompletedImports_AJAX() {
$SQL = "select count(*) as notify
from view_importjobs
where view_importjobs.importjob_creator = :current_user_serial
and view_importjobs.importjob_status = 'complete'
and view_importjobs.importjob_notified = 0";
$importjobs = $this->getTable("importjobs", $SQL, array('current_user_serial' => $this->current_user_serial));
// Create the User Object.
$users_imports = array();
$users_imports["notify"] = ($importjobs->record->notify == 0) ? false : true;
if ($_SESSION[self::AJAX]) {
echo json_encode($users_imports);
} else {
return $users_imports;
}
}
// ===============
// Download Import
// ===============
public function downloadImport() {
$import_code = filter_input(INPUT_POST, "import_code") ?: "";
$SQL = "select view_importjobs.*
from view_importjobs
where view_importjobs.importjob_download_token = :import_code
and view_importjobs.importjob_status = 'complete' limit 1";
$importjob = $this->getTable("importjob", $SQL, array("import_code" => $import_code));
$file_path = str_replace('../imports/', './imports/', $importjob->record->importjob_file_path);
$file_name = $importjob->record->importjob_file_name;
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"$file_name\"");
readfile($file_path);
}
}
?>
+26 -11
View File
@@ -140,7 +140,7 @@ class Process_ImportRequest {
try {
$success = $this->parseCSVFile($importjob_file_path);
$this->parseCSVFile($importjob_file_path);
$SQL = "UPDATE importjobs
SET importjob_status = 'complete',
@@ -150,6 +150,9 @@ class Process_ImportRequest {
$update = $this->connect()->prepare($SQL);
$update->execute([':importjob_serial' => $importjob_serial]);
$this->sendWebHookAlert("Import Job completed successfully.");
$this->email_admins("Import Job Success.", "Import Job completed successfully.");
} catch (Throwable $e) {
$SQL = "UPDATE importjobs
@@ -238,13 +241,17 @@ class Process_ImportRequest {
$filename = $files['importjob_file_path'];
if (is_file($filename)) {
unlink($filename);
}
}
$baseDir = './imports';
$baseDir = "./uploads/Production/";
if (!is_dir($baseDir)) {
$this->log("Imports directory does not exist: {$baseDir}");
} else {
foreach (new DirectoryIterator($baseDir) as $item) {
if ($item->isDot() || !$item->isDir()) {
continue;
}
@@ -258,6 +265,7 @@ class Process_ImportRequest {
rmdir($folderPath);
}
}
}
$this->log("Purging Import Jobs Database Records");
@@ -291,10 +299,19 @@ class Process_ImportRequest {
$csvfile = fopen($file, 'r');
if ($csvfile === false) {
throw new RuntimeException("Unable to open CSV file: {$file}");
}
$RecordCount = 0;
while (($data = fgetcsv($csvfile, 0, ",", '"', '\\')) !== FALSE) {
if (count($data) < 36) {
$this->log("Skipping malformed CSV row with " . count($data) . " columns.");
continue;
}
$PermitFactsId = $data[1];
$EntryDate = $this->fixDate($data[2]);
$PermitNumber = $data[4];
@@ -453,11 +470,11 @@ class Process_ImportRequest {
$SQL = "select view_counties.*
from view_counties
where view_counties.county_name = '{$county_name}'";
where view_counties.county_name = :county_name";
$statement = $this->connection->prepare($SQL);
$statement->execute();
$statement->execute([':county_name' => $county_name]);
$county_name = $statement->fetch(PDO::FETCH_ASSOC);
@@ -468,7 +485,7 @@ class Process_ImportRequest {
// Send an Alert/Notification
// ==========================
public function sendWebHookAlert($message, $title = "🚨 DEC Application Alert", $alerttype = "1fff00") {
public function sendWebHookAlert($message, $title = "DEC Application Alert", $alerttype = "1fff00") {
$webhookUrl = (string) $this->settings->DiscordWebHookURL;
@@ -512,6 +529,9 @@ class Process_ImportRequest {
$settings = new SimpleXMLElement((file_exists("./xml/settings.xml") ? file_get_contents("./xml/settings.xml") : "<settings/>"));
print_r($settings);
die();
// Server Variables
$host = (string) $settings->SmtpServer;
$username = (string) $settings->SmtpUserName;
@@ -532,8 +552,6 @@ class Process_ImportRequest {
// Content Variables
$attachment_name = "";
$mail = new PHPMailer(true);
try {
@@ -555,9 +573,6 @@ class Process_ImportRequest {
$mail->addAddress($admin_email, $admin_name);
}
//Attachments
$mail->addAttachment($attachment_name);
//Content
$mail->isHTML(true);
$mail->Subject = $subject;