Skip to content

Commit

Permalink
Completed the URS script, fixed #6
Browse files Browse the repository at this point in the history
  • Loading branch information
getpinga committed Oct 28, 2024
1 parent 96e31f5 commit 8c166a1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 40 deletions.
2 changes: 2 additions & 0 deletions automation/config.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
102 changes: 63 additions & 39 deletions automation/urs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]';
$providerBEmail = '[email protected]';
$providerCEmail = '[email protected]';

// 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) {
'[email protected]' => 'FORUM',
'[email protected]' => 'ADNDRC',
'[email protected]' => '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');
}
}

Expand Down
2 changes: 1 addition & 1 deletion install.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 8c166a1

Please sign in to comment.