From 3f3900fef615f2da7dcb5be37fafc2e7cf40db1c Mon Sep 17 00:00:00 2001 From: James Richie Date: Thu, 21 May 2026 20:43:16 -0400 Subject: [PATCH] fixed filepath issue in import request script. --- classes/Import.php | 2 +- classes/ImportLargeCSV.php | 255 ------------------------------ classes/Process_ImportRequest.php | 215 +++++++++++++------------ 3 files changed, 116 insertions(+), 356 deletions(-) delete mode 100644 classes/ImportLargeCSV.php diff --git a/classes/Import.php b/classes/Import.php index 5e4539a..2784746 100644 --- a/classes/Import.php +++ b/classes/Import.php @@ -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()); diff --git a/classes/ImportLargeCSV.php b/classes/ImportLargeCSV.php deleted file mode 100644 index 048840d..0000000 --- a/classes/ImportLargeCSV.php +++ /dev/null @@ -1,255 +0,0 @@ -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 = $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); - } -} - -?> diff --git a/classes/Process_ImportRequest.php b/classes/Process_ImportRequest.php index 53a2096..d6ccad0 100644 --- a/classes/Process_ImportRequest.php +++ b/classes/Process_ImportRequest.php @@ -16,8 +16,8 @@ class Process_ImportRequest { // Variables - private $log = ""; - private $settings = ""; + private $log = ""; + private $settings = ""; private $connection = ""; // ================== @@ -27,7 +27,7 @@ class Process_ImportRequest { public function __construct() { $basePath = realpath(__DIR__ . '/..'); - $logfile = $basePath . "/scripts/importrequests.log"; + $logfile = $basePath . "/scripts/importrequests.log"; // ========================== // Log Rotation (50 MB limit) @@ -80,9 +80,9 @@ class Process_ImportRequest { try { - $database = (string) $this->settings->DatabaseName; - $user = (string) $this->settings->DatabaseUser; - $password = (string) $this->settings->DatabasePassword; + $database = (string) $this->settings->DatabaseName; + $user = (string) $this->settings->DatabaseUser; + $password = (string) $this->settings->DatabasePassword; $connection = new PDO("mysql:host={$host};dbname={$database};charset=utf8", $user, $password); @@ -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,24 +241,29 @@ class Process_ImportRequest { $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()) { - continue; - } + $folderPath = $item->getPathname(); - $folderPath = $item->getPathname(); + // Check if directory is empty + $files = scandir($folderPath); - // Check if directory is empty - $files = scandir($folderPath); - - if ($files !== false && count($files) === 2) { - rmdir($folderPath); + if ($files !== false && count($files) === 2) { + rmdir($folderPath); + } } } @@ -291,42 +299,51 @@ 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) { - $PermitFactsId = $data[1]; - $EntryDate = $this->fixDate($data[2]); - $PermitNumber = $data[4]; - $PermitDate = $this->fixDate($data[5]); - $ProjectAddress = $data[7]; - $SubdivisionName = $data[8]; - $ProjectType = $data[9]; - $Company = substr($data[10], 0, 30); - $Address = substr($data[11], 0, 30); - $City = $data[12]; - $State = $data[13]; - $Zip = $data[14]; - $Phone = $data[15]; - $ProjectCity = $data[16]; - $ProjectState = $data[17]; - $Size = $data[18] ?: 0; - $Units = $data[19]; - $BuildingType = $data[20]; - $Value = $data[21] ?: 0; - $WorkType = $data[22]; - $County = $data[23] ?: 0; - $Lot = $data[24]; - $DistrictLL = $data[25]; - $Ctl = $data[26]; - $OwnerName = substr($data[27], 0, 30); - $OwnerAddress = substr($data[28], 0, 30); - $OwnerCity = $data[29]; - $OwnerState = substr($data[30], 0, 2); - $OwnerZip = $data[31]; - $OwnerPhone = $data[32]; - $YTDPermits = $data[33] ?: 0; - $YTDValue = $data[34] ?: 0; + 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]; + $PermitDate = $this->fixDate($data[5]); + $ProjectAddress = $data[7]; + $SubdivisionName = $data[8]; + $ProjectType = $data[9]; + $Company = substr($data[10], 0, 30); + $Address = substr($data[11], 0, 30); + $City = $data[12]; + $State = $data[13]; + $Zip = $data[14]; + $Phone = $data[15]; + $ProjectCity = $data[16]; + $ProjectState = $data[17]; + $Size = $data[18] ?: 0; + $Units = $data[19]; + $BuildingType = $data[20]; + $Value = $data[21] ?: 0; + $WorkType = $data[22]; + $County = $data[23] ?: 0; + $Lot = $data[24]; + $DistrictLL = $data[25]; + $Ctl = $data[26]; + $OwnerName = substr($data[27], 0, 30); + $OwnerAddress = substr($data[28], 0, 30); + $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; $County = $this->getCountiesByName($County)['county_serial'] ?? 0; @@ -403,38 +420,38 @@ class Process_ImportRequest { $statement = $this->connection->prepare($SQL); - $statement->bindValue(":permit_permit_facts_id", $PermitFactsId); - $statement->bindValue(":permit_permit_number", $PermitNumber); - $statement->bindValue(":permit_entry_date", $EntryDate); - $statement->bindValue(":permit_permit_date", $PermitDate); - $statement->bindValue(":permit_project_addr", $ProjectAddress); - $statement->bindValue(":permit_sub_div_name", $SubdivisionName); - $statement->bindValue(":permit_project_type", $ProjectType); - $statement->bindValue(":permit_company", $Company); - $statement->bindValue(":permit_address", $Address); - $statement->bindValue(":permit_city", $City); - $statement->bindValue(":permit_state", $State); - $statement->bindValue(":permit_zip", $Zip); - $statement->bindValue(":permit_phone", $Phone); - $statement->bindValue(":permit_project_city", $ProjectCity); - $statement->bindValue(":permit_project_state", $ProjectState); - $statement->bindValue(":permit_size", $Size); - $statement->bindValue(":permit_units", $Units); - $statement->bindValue(":permit_building_type", $BuildingType); - $statement->bindValue(":permit_value", $Value); - $statement->bindValue(":permit_work_type", $WorkType); - $statement->bindValue(":permit_county", $County); - $statement->bindValue(":permit_lot", $Lot); - $statement->bindValue(":permit_dist_ll", $DistrictLL); - $statement->bindValue(":permit_ct1", $Ctl); - $statement->bindValue(":permit_owner_name", $OwnerName); - $statement->bindValue(":permit_owner_address", $OwnerAddress); - $statement->bindValue(":permit_owner_city", $OwnerCity); - $statement->bindValue(":permit_owner_state", $OwnerState); - $statement->bindValue(":permit_owner_zip", $OwnerZip); - $statement->bindValue(":permit_owner_phone", $OwnerPhone); - $statement->bindValue(":permit_ytd_permits", $YTDPermits); - $statement->bindValue(":permit_ytd_value", $YTDValue); + $statement->bindValue(":permit_permit_facts_id", $PermitFactsId); + $statement->bindValue(":permit_permit_number", $PermitNumber); + $statement->bindValue(":permit_entry_date", $EntryDate); + $statement->bindValue(":permit_permit_date", $PermitDate); + $statement->bindValue(":permit_project_addr", $ProjectAddress); + $statement->bindValue(":permit_sub_div_name", $SubdivisionName); + $statement->bindValue(":permit_project_type", $ProjectType); + $statement->bindValue(":permit_company", $Company); + $statement->bindValue(":permit_address", $Address); + $statement->bindValue(":permit_city", $City); + $statement->bindValue(":permit_state", $State); + $statement->bindValue(":permit_zip", $Zip); + $statement->bindValue(":permit_phone", $Phone); + $statement->bindValue(":permit_project_city", $ProjectCity); + $statement->bindValue(":permit_project_state", $ProjectState); + $statement->bindValue(":permit_size", $Size); + $statement->bindValue(":permit_units", $Units); + $statement->bindValue(":permit_building_type", $BuildingType); + $statement->bindValue(":permit_value", $Value); + $statement->bindValue(":permit_work_type", $WorkType); + $statement->bindValue(":permit_county", $County); + $statement->bindValue(":permit_lot", $Lot); + $statement->bindValue(":permit_dist_ll", $DistrictLL); + $statement->bindValue(":permit_ct1", $Ctl); + $statement->bindValue(":permit_owner_name", $OwnerName); + $statement->bindValue(":permit_owner_address", $OwnerAddress); + $statement->bindValue(":permit_owner_city", $OwnerCity); + $statement->bindValue(":permit_owner_state", $OwnerState); + $statement->bindValue(":permit_owner_zip", $OwnerZip); + $statement->bindValue(":permit_owner_phone", $OwnerPhone); + $statement->bindValue(":permit_ytd_permits", $YTDPermits); + $statement->bindValue(":permit_ytd_value", $YTDValue); $statement->bindValue(":permit_12mth_prev_permits", $TwelveMonthPreviousPermits); $statement->execute(); @@ -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,40 +529,41 @@ class Process_ImportRequest { $settings = new SimpleXMLElement((file_exists("./xml/settings.xml") ? file_get_contents("./xml/settings.xml") : "")); + print_r($settings); + die(); + // Server Variables - $host = (string) $settings->SmtpServer; + $host = (string) $settings->SmtpServer; $username = (string) $settings->SmtpUserName; $password = (string) $settings->SmtpPassword; // Recipient Variables $from_email = (string) $settings->FromEmail; - $from_name = (string) $settings->FromName; + $from_name = (string) $settings->FromName; $support_email = (string) $settings->SupportEmail; - $support_name = (string) $settings->SupportName; + $support_name = (string) $settings->SupportName; if ((string) $settings->Environment == 'Production') { $admin_email = (string) $settings->AdminEmail; - $admin_name = (string) $settings->AdminName; + $admin_name = (string) $settings->AdminName; } // Content Variables - $attachment_name = ""; - $mail = new PHPMailer(true); try { //Server settings $mail->isSMTP(); - $mail->Host = $host; - $mail->SMTPAuth = true; - $mail->Username = $username; - $mail->Password = $password; + $mail->Host = $host; + $mail->SMTPAuth = true; + $mail->Username = $username; + $mail->Password = $password; $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; - $mail->Port = 465; + $mail->Port = 465; //Recipients $mail->setFrom($from_email, $from_name); @@ -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;