From 8c166a16365a13cfdc2083da98e2fd0339b38675 Mon Sep 17 00:00:00 2001 From: Pinga <121483313+getpinga@users.noreply.github.com> Date: Mon, 28 Oct 2024 13:00:22 +0200 Subject: [PATCH] Completed the URS script, fixed #6 --- automation/config.php.dist | 2 + automation/urs.php | 102 +++++++++++++++++++++++-------------- install.md | 2 +- 3 files changed, 66 insertions(+), 40 deletions(-) diff --git a/automation/config.php.dist b/automation/config.php.dist index 2a5f780..79a83bf 100644 --- a/automation/config.php.dist +++ b/automation/config.php.dist @@ -48,4 +48,6 @@ $config = array( 'urs_imap_host' => '{your_imap_server:993/imap/ssl}INBOX', 'urs_imap_username' => 'your_username', 'urs_imap_password' => 'your_password', + // WHMCS Configuration + 'whmcs_department_id' => 1, ); diff --git a/automation/urs.php b/automation/urs.php index b9e68df..3b41d59 100644 --- a/automation/urs.php +++ b/automation/urs.php @@ -37,58 +37,82 @@ $subject = $header->subject; $date = date('Y-m-d H:i:s', strtotime($header->date)) . '.000'; - // Determine the URS provider based on the email sender - $providerAEmail = 'urs@adrforum.com'; - $providerBEmail = 'urs@adndrc.org'; - $providerCEmail = 'urs@mfsd.it'; - - // Determine the URS provider based on the email sender - if ($from == $providerAEmail) { - $ursProvider = 'FORUM'; - } elseif ($from == $providerBEmail) { - $ursProvider = 'ADNDRC'; - } elseif ($from == $providerCEmail) { - $ursProvider = 'MFSD'; - } else { - $ursProvider = 'Unknown'; - } + // Determine the URS provider + $provider = match ($from) { + 'urs@adrforum.com' => 'FORUM', + 'urs@adndrc.org' => 'ADNDRC', + 'urs@mfsd.it' => 'MFSD', + default => 'Unknown', + }; // Extract domain name or relevant info from the email (you'd need more specific code here based on the email content) $body = imap_fetchbody($inbox, $emailId, 1); $domain = extractDomainNameFromEmail($body); - // Extract TLD from the domain and prepend a dot - $parts = explode('.', $domain); - $domainName = $parts[0]; - $tld = "." . end($parts); + if (!$domain) { + error_log("No domain found in email body for email ID $emailId"); + continue; + } // Insert into the database - $stmt = $dbh->prepare("SELECT sld, tld, client_id FROM service_domain WHERE sld = ?"); - $stmt->execute([$domainName]); + $stmt = $dbh->prepare("SELECT id, userid FROM tbldomains WHERE domain = ?"); + $stmt->execute([$domain]); $domainResult = $stmt->fetch(PDO::FETCH_ASSOC); if ($domainResult) { - $domainName = $domain; - $clientId = $domainResult['client_id']; + $userId = $domainResult['userid']; - // Prepare the current date and time in the required format + // Insert ticket in WHMCS tbltickets table $currentDateTime = date('Y-m-d H:i:s'); - - // Insert into the support_ticket table - $stmt = $dbh->prepare("INSERT INTO support_ticket (support_helpdesk_id, client_id, priority, subject, status, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)"); - $stmt->execute([1, $clientId, 100, 'New URS case for ' . $domainName, 'on_hold', $currentDateTime, $currentDateTime]); - - // Get the last inserted ID from the support_ticket table - $supportTicketId = $dbh->lastInsertId(); - - // Get the client IP address, default to '127.0.0.1' if not available - $clientIp = $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1'; - - // Insert into the support_ticket_message table using the last inserted ID - $stmt = $dbh->prepare("INSERT INTO support_ticket_message (support_ticket_id, client_id, admin_id, content, attachment, ip, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); - $stmt->execute([$supportTicketId, null, 1, 'New URS case for ' . $domainName . ' submitted by ' . $ursProvider . ' on ' . $date . '. Please act accordingly', null, $clientIp, $currentDateTime, $currentDateTime]); + // Generate a unique TID (Ticket ID) similar to WHMCS format, e.g., "SCT-441641" + $tid = 'SCT-' . str_pad(mt_rand(1, 999999), 6, '0', STR_PAD_LEFT); + // Generate a unique alphanumeric code for `c` (Ticket Hash), e.g., "tH7B0ple" + $c = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 8); + + $stmt = $dbh->prepare(" + INSERT INTO tbltickets ( + tid, userid, did, cc, c, title, message, status, urgency, date, lastreply, ipaddress, + flag, name, email, contactid, requestor_id, admin, attachment, attachments_removed, merged_ticket_id, clientunread, replyingadmin, adminunread, replyingtime, service, editor, updated_at + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + "); + + // Execute with appropriate values for each field + $stmt->execute([ + $tid, // tid: Unique Ticket ID + $userId, // userid: User associated with the domain + $config['whmcs_department_id'], // did: Department ID + '', // cc: empty + $c, // c: Unique Ticket Hash + 'New URS case for ' . $domain, // title: Subject line of the ticket + 'New URS case for ' . $domain . ' submitted by ' . $provider . ' on ' . $date . '. Please review and act accordingly.', // message: Ticket content + 'Open', // status: Ticket status + 'Medium', // urgency: Priority level + $currentDateTime, // date: Ticket creation date + $currentDateTime, // lastreply: Date of last reply (set to creation date initially) + '127.0.0.1', // ipaddress: Default IP address if not available + 0, // flag: 0 + 'Automated URS System', // name: Requestor’s name (can use a generic identifier for automated tickets) + $config['email']['sender'], // email: Default email if not available + 0, // contactid: Set to 0 if no specific contact is associated + 0, // requestor_id: Default to 0 if not specified + '', // admin: empty + '', // attachment: empty + 0, // attachments_removed: Set to 0, assuming no attachments are initially removed + 0, // merged_ticket_id: Default to 0 if this ticket is standalone + 1, // clientunread: 1 + 0, // replyingadmin: 0 + '', // adminunread: empty + '0000-00-00 00:00:00', // replyingtime: 0 + '', // service: empty + 'plain', // editor: Default to 'plain' for message formatting + $currentDateTime // updated_at: Set to current date/time + ]); + + // Log insertion and retrieve last inserted ticket ID + $ticketId = $dbh->lastInsertId(); + error_log("Created support ticket ID $ticketId for domain $domain."); } else { - error_log('Domain ' . $domainName . ' does not exists in registry'); + error_log('Domain ' . $domain . ' does not exists in registry'); } } diff --git a/install.md b/install.md index 62dce05..854137e 100644 --- a/install.md +++ b/install.md @@ -17,7 +17,7 @@ systemctl enable apache2 systemctl start apache2 add-apt-repository ppa:ondrej/php -y apt update -apt install -y php8.2 php8.2-{curl,gd,mbstring,mysql,xml,zip,bcmath,intl,swoole} +apt install -y php8.2 php8.2-{curl,gd,mbstring,mysql,xml,zip,imap,bcmath,intl,swoole} ``` ## 2. Install ionCube Loader: