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 = $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_notified", false, PDO::PARAM_BOOL);
$statement->bindValue(":importjob_creator", $this->current_user_name, PDO::PARAM_STR); $statement->bindValue(":importjob_creator", $this->current_user_name, PDO::PARAM_STR);
$statement->bindValue(":importjob_created", $this->getTimestamp()); $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);
}
}
?>
+115 -100
View File
@@ -16,8 +16,8 @@ class Process_ImportRequest {
// Variables // Variables
private $log = ""; private $log = "";
private $settings = ""; private $settings = "";
private $connection = ""; private $connection = "";
// ================== // ==================
@@ -27,7 +27,7 @@ class Process_ImportRequest {
public function __construct() { public function __construct() {
$basePath = realpath(__DIR__ . '/..'); $basePath = realpath(__DIR__ . '/..');
$logfile = $basePath . "/scripts/importrequests.log"; $logfile = $basePath . "/scripts/importrequests.log";
// ========================== // ==========================
// Log Rotation (50 MB limit) // Log Rotation (50 MB limit)
@@ -80,9 +80,9 @@ class Process_ImportRequest {
try { try {
$database = (string) $this->settings->DatabaseName; $database = (string) $this->settings->DatabaseName;
$user = (string) $this->settings->DatabaseUser; $user = (string) $this->settings->DatabaseUser;
$password = (string) $this->settings->DatabasePassword; $password = (string) $this->settings->DatabasePassword;
$connection = new PDO("mysql:host={$host};dbname={$database};charset=utf8", $user, $password); $connection = new PDO("mysql:host={$host};dbname={$database};charset=utf8", $user, $password);
@@ -140,7 +140,7 @@ class Process_ImportRequest {
try { try {
$success = $this->parseCSVFile($importjob_file_path); $this->parseCSVFile($importjob_file_path);
$SQL = "UPDATE importjobs $SQL = "UPDATE importjobs
SET importjob_status = 'complete', SET importjob_status = 'complete',
@@ -150,6 +150,9 @@ class Process_ImportRequest {
$update = $this->connect()->prepare($SQL); $update = $this->connect()->prepare($SQL);
$update->execute([':importjob_serial' => $importjob_serial]); $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) { } catch (Throwable $e) {
$SQL = "UPDATE importjobs $SQL = "UPDATE importjobs
@@ -238,24 +241,29 @@ class Process_ImportRequest {
$filename = $files['importjob_file_path']; $filename = $files['importjob_file_path'];
unlink($filename); if (is_file($filename)) {
unlink($filename);
}
} }
$baseDir = './imports'; $baseDir = "./uploads/Production/";
foreach (new DirectoryIterator($baseDir) as $item) { 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;
}
if ($item->isDot() || !$item->isDir()) { $folderPath = $item->getPathname();
continue;
}
$folderPath = $item->getPathname(); // Check if directory is empty
$files = scandir($folderPath);
// Check if directory is empty if ($files !== false && count($files) === 2) {
$files = scandir($folderPath); rmdir($folderPath);
}
if ($files !== false && count($files) === 2) {
rmdir($folderPath);
} }
} }
@@ -291,42 +299,51 @@ class Process_ImportRequest {
$csvfile = fopen($file, 'r'); $csvfile = fopen($file, 'r');
if ($csvfile === false) {
throw new RuntimeException("Unable to open CSV file: {$file}");
}
$RecordCount = 0; $RecordCount = 0;
while (($data = fgetcsv($csvfile, 0, ",", '"', '\\')) !== FALSE) { while (($data = fgetcsv($csvfile, 0, ",", '"', '\\')) !== FALSE) {
$PermitFactsId = $data[1]; if (count($data) < 36) {
$EntryDate = $this->fixDate($data[2]); $this->log("Skipping malformed CSV row with " . count($data) . " columns.");
$PermitNumber = $data[4]; continue;
$PermitDate = $this->fixDate($data[5]); }
$ProjectAddress = $data[7];
$SubdivisionName = $data[8]; $PermitFactsId = $data[1];
$ProjectType = $data[9]; $EntryDate = $this->fixDate($data[2]);
$Company = substr($data[10], 0, 30); $PermitNumber = $data[4];
$Address = substr($data[11], 0, 30); $PermitDate = $this->fixDate($data[5]);
$City = $data[12]; $ProjectAddress = $data[7];
$State = $data[13]; $SubdivisionName = $data[8];
$Zip = $data[14]; $ProjectType = $data[9];
$Phone = $data[15]; $Company = substr($data[10], 0, 30);
$ProjectCity = $data[16]; $Address = substr($data[11], 0, 30);
$ProjectState = $data[17]; $City = $data[12];
$Size = $data[18] ?: 0; $State = $data[13];
$Units = $data[19]; $Zip = $data[14];
$BuildingType = $data[20]; $Phone = $data[15];
$Value = $data[21] ?: 0; $ProjectCity = $data[16];
$WorkType = $data[22]; $ProjectState = $data[17];
$County = $data[23] ?: 0; $Size = $data[18] ?: 0;
$Lot = $data[24]; $Units = $data[19];
$DistrictLL = $data[25]; $BuildingType = $data[20];
$Ctl = $data[26]; $Value = $data[21] ?: 0;
$OwnerName = substr($data[27], 0, 30); $WorkType = $data[22];
$OwnerAddress = substr($data[28], 0, 30); $County = $data[23] ?: 0;
$OwnerCity = $data[29]; $Lot = $data[24];
$OwnerState = substr($data[30], 0, 2); $DistrictLL = $data[25];
$OwnerZip = $data[31]; $Ctl = $data[26];
$OwnerPhone = $data[32]; $OwnerName = substr($data[27], 0, 30);
$YTDPermits = $data[33] ?: 0; $OwnerAddress = substr($data[28], 0, 30);
$YTDValue = $data[34] ?: 0; $OwnerCity = $data[29];
$OwnerState = substr($data[30], 0, 2);
$OwnerZip = $data[31];
$OwnerPhone = $data[32];
$YTDPermits = $data[33] ?: 0;
$YTDValue = $data[34] ?: 0;
$TwelveMonthPreviousPermits = $data[35] ?: 0; $TwelveMonthPreviousPermits = $data[35] ?: 0;
$County = $this->getCountiesByName($County)['county_serial'] ?? 0; $County = $this->getCountiesByName($County)['county_serial'] ?? 0;
@@ -403,38 +420,38 @@ class Process_ImportRequest {
$statement = $this->connection->prepare($SQL); $statement = $this->connection->prepare($SQL);
$statement->bindValue(":permit_permit_facts_id", $PermitFactsId); $statement->bindValue(":permit_permit_facts_id", $PermitFactsId);
$statement->bindValue(":permit_permit_number", $PermitNumber); $statement->bindValue(":permit_permit_number", $PermitNumber);
$statement->bindValue(":permit_entry_date", $EntryDate); $statement->bindValue(":permit_entry_date", $EntryDate);
$statement->bindValue(":permit_permit_date", $PermitDate); $statement->bindValue(":permit_permit_date", $PermitDate);
$statement->bindValue(":permit_project_addr", $ProjectAddress); $statement->bindValue(":permit_project_addr", $ProjectAddress);
$statement->bindValue(":permit_sub_div_name", $SubdivisionName); $statement->bindValue(":permit_sub_div_name", $SubdivisionName);
$statement->bindValue(":permit_project_type", $ProjectType); $statement->bindValue(":permit_project_type", $ProjectType);
$statement->bindValue(":permit_company", $Company); $statement->bindValue(":permit_company", $Company);
$statement->bindValue(":permit_address", $Address); $statement->bindValue(":permit_address", $Address);
$statement->bindValue(":permit_city", $City); $statement->bindValue(":permit_city", $City);
$statement->bindValue(":permit_state", $State); $statement->bindValue(":permit_state", $State);
$statement->bindValue(":permit_zip", $Zip); $statement->bindValue(":permit_zip", $Zip);
$statement->bindValue(":permit_phone", $Phone); $statement->bindValue(":permit_phone", $Phone);
$statement->bindValue(":permit_project_city", $ProjectCity); $statement->bindValue(":permit_project_city", $ProjectCity);
$statement->bindValue(":permit_project_state", $ProjectState); $statement->bindValue(":permit_project_state", $ProjectState);
$statement->bindValue(":permit_size", $Size); $statement->bindValue(":permit_size", $Size);
$statement->bindValue(":permit_units", $Units); $statement->bindValue(":permit_units", $Units);
$statement->bindValue(":permit_building_type", $BuildingType); $statement->bindValue(":permit_building_type", $BuildingType);
$statement->bindValue(":permit_value", $Value); $statement->bindValue(":permit_value", $Value);
$statement->bindValue(":permit_work_type", $WorkType); $statement->bindValue(":permit_work_type", $WorkType);
$statement->bindValue(":permit_county", $County); $statement->bindValue(":permit_county", $County);
$statement->bindValue(":permit_lot", $Lot); $statement->bindValue(":permit_lot", $Lot);
$statement->bindValue(":permit_dist_ll", $DistrictLL); $statement->bindValue(":permit_dist_ll", $DistrictLL);
$statement->bindValue(":permit_ct1", $Ctl); $statement->bindValue(":permit_ct1", $Ctl);
$statement->bindValue(":permit_owner_name", $OwnerName); $statement->bindValue(":permit_owner_name", $OwnerName);
$statement->bindValue(":permit_owner_address", $OwnerAddress); $statement->bindValue(":permit_owner_address", $OwnerAddress);
$statement->bindValue(":permit_owner_city", $OwnerCity); $statement->bindValue(":permit_owner_city", $OwnerCity);
$statement->bindValue(":permit_owner_state", $OwnerState); $statement->bindValue(":permit_owner_state", $OwnerState);
$statement->bindValue(":permit_owner_zip", $OwnerZip); $statement->bindValue(":permit_owner_zip", $OwnerZip);
$statement->bindValue(":permit_owner_phone", $OwnerPhone); $statement->bindValue(":permit_owner_phone", $OwnerPhone);
$statement->bindValue(":permit_ytd_permits", $YTDPermits); $statement->bindValue(":permit_ytd_permits", $YTDPermits);
$statement->bindValue(":permit_ytd_value", $YTDValue); $statement->bindValue(":permit_ytd_value", $YTDValue);
$statement->bindValue(":permit_12mth_prev_permits", $TwelveMonthPreviousPermits); $statement->bindValue(":permit_12mth_prev_permits", $TwelveMonthPreviousPermits);
$statement->execute(); $statement->execute();
@@ -453,11 +470,11 @@ class Process_ImportRequest {
$SQL = "select view_counties.* $SQL = "select view_counties.*
from 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 = $this->connection->prepare($SQL);
$statement->execute(); $statement->execute([':county_name' => $county_name]);
$county_name = $statement->fetch(PDO::FETCH_ASSOC); $county_name = $statement->fetch(PDO::FETCH_ASSOC);
@@ -468,7 +485,7 @@ class Process_ImportRequest {
// Send an Alert/Notification // 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; $webhookUrl = (string) $this->settings->DiscordWebHookURL;
@@ -512,40 +529,41 @@ class Process_ImportRequest {
$settings = new SimpleXMLElement((file_exists("./xml/settings.xml") ? file_get_contents("./xml/settings.xml") : "<settings/>")); $settings = new SimpleXMLElement((file_exists("./xml/settings.xml") ? file_get_contents("./xml/settings.xml") : "<settings/>"));
print_r($settings);
die();
// Server Variables // Server Variables
$host = (string) $settings->SmtpServer; $host = (string) $settings->SmtpServer;
$username = (string) $settings->SmtpUserName; $username = (string) $settings->SmtpUserName;
$password = (string) $settings->SmtpPassword; $password = (string) $settings->SmtpPassword;
// Recipient Variables // Recipient Variables
$from_email = (string) $settings->FromEmail; $from_email = (string) $settings->FromEmail;
$from_name = (string) $settings->FromName; $from_name = (string) $settings->FromName;
$support_email = (string) $settings->SupportEmail; $support_email = (string) $settings->SupportEmail;
$support_name = (string) $settings->SupportName; $support_name = (string) $settings->SupportName;
if ((string) $settings->Environment == 'Production') { if ((string) $settings->Environment == 'Production') {
$admin_email = (string) $settings->AdminEmail; $admin_email = (string) $settings->AdminEmail;
$admin_name = (string) $settings->AdminName; $admin_name = (string) $settings->AdminName;
} }
// Content Variables // Content Variables
$attachment_name = "";
$mail = new PHPMailer(true); $mail = new PHPMailer(true);
try { try {
//Server settings //Server settings
$mail->isSMTP(); $mail->isSMTP();
$mail->Host = $host; $mail->Host = $host;
$mail->SMTPAuth = true; $mail->SMTPAuth = true;
$mail->Username = $username; $mail->Username = $username;
$mail->Password = $password; $mail->Password = $password;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465; $mail->Port = 465;
//Recipients //Recipients
$mail->setFrom($from_email, $from_name); $mail->setFrom($from_email, $from_name);
@@ -555,9 +573,6 @@ class Process_ImportRequest {
$mail->addAddress($admin_email, $admin_name); $mail->addAddress($admin_email, $admin_name);
} }
//Attachments
$mail->addAttachment($attachment_name);
//Content //Content
$mail->isHTML(true); $mail->isHTML(true);
$mail->Subject = $subject; $mail->Subject = $subject;