Jakweb.ch stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
clouddesk/cron/tickets.php

506 lines
26 KiB

1 year ago
<?php
/*===============================================*\
|| ############################################# ||
|| # JAKWEB.CH / Version 2.1.1 # ||
|| # ----------------------------------------- # ||
|| # Copyright 2023 JAKWEB All Rights Reserved # ||
|| ############################################# ||
\*===============================================*/
$cron_url_orig = dirname(__FILE__) . DIRECTORY_SEPARATOR;
$cron_url = str_replace("cron" . DIRECTORY_SEPARATOR, "", $cron_url_orig);
if (!file_exists($cron_url . "include/db.php"))
{
die("cron/[cron.php] db.php not exist");
}
require_once $cron_url . "include/db.php";
if (!file_exists($cron_url . "class/class.db.php"))
{
die("cron/[cron.php] class.db.php not exist");
}
require_once $cron_url . "class/class.db.php";
if (!file_exists($cron_url . "class/class.jakbase.php"))
{
die("cron/[cron.php] class.jakbase.php not exist");
}
require_once $cron_url . "class/class.jakbase.php";
use JAKWEB\JAKsql;
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\OAuth;
//@see https://github.com/thephpleague/oauth2-google
use League\OAuth2\Client\Provider\Google;
//@see https://github.com/stevenmaguire/oauth2-microsoft
use Stevenmaguire\OAuth2\Client\Provider\Microsoft;
//@see https://github.com/greew/oauth2-azure-provider
use Greew\OAuth2\Client\Provider\Azure;
//@see https://packagist.org/packages/hayageek/oauth2-yahoo
use Hayageek\OAuth2\Client\Provider\Yahoo;
// Database connection
$jakdb = new JAKsql([
// required
"database_type" => JAKDB_DBTYPE, "database_name" => JAKDB_NAME, "server" => JAKDB_HOST, "username" => JAKDB_USER, "password" => JAKDB_PASS, "charset" => "utf8", "port" => JAKDB_PORT, "prefix" => JAKDB_PREFIX,
// [optional] driver_option for connection, read more from http://www.php.net/manual/en/pdo.setattribute.php
"option" => [PDO::ATTR_CASE => PDO::CASE_NATURAL], ]);
// Get the necessary classes
include_once str_replace("cron/", "", $cron_url . "include/functions.php");
include_once str_replace("cron/", "", $cron_url . "class/class.browser.php");
require_once str_replace("cron/", "", $cron_url . "vendor/autoload.php");
// We need the correct url to filter either from web or cron
$sapi_type = php_sapi_name();
if (substr($sapi_type, 0, 3) == "cli" || empty($_SERVER["REMOTE_ADDR"]))
{
$path_parts = pathinfo($cron_url);
$url_filter = $cron_url;
$url_replace = "/" . basename($path_parts["dirname"]) . "/";
}
else
{
$url_filter = "/cron/";
$url_replace = "/";
}
// Tables
$jaktable = "support_tickets";
$jaktable1 = "tickets_answer";
$jaktable2 = "php_imap";
$ops = [];
// Write the log file each time we run the ticket cron job
JAK_base::jakWhatslog("System", 0, 0, 0, 38, 0, "", "Cron Job - Tickets", "cron/tickets.php", 0, "tickets");
// Get the BASE_URL if not wildcard
if (!JAK_WILDCARD_SUBDOMAIN)
{
// We have no wildcard subdomain, just go normal
$BASE_URL = (JAK_SITEHTTPS ? "https://" : "http://") . FULL_SITE_DOMAIN . SITE_SUBFOLDER . "/";
}
// Select all accounts that need a welcome email.
$upduser = $jakdb->select("user", ["id", "email", "username"], ["AND" => ["opid" => 0, "access" => 1]]);
if (isset($upduser) && !empty($upduser) && is_array($upduser))
{
foreach ($upduser as $usr)
{
$datasett = $jakdb->select("settings", ["varname", "used_value"], ["opid" => $usr["id"]]);
foreach ($datasett as $row)
{
// Now check if sting contains html and do something about it!
if (!empty($row["used_value"]) && strlen($row["used_value"]) != strlen(filter_var($row["used_value"], FILTER_SANITIZE_FULL_SPECIAL_CHARS)))
{
$defvar = htmlspecialchars($row["used_value"]);
}
else
{
$defvar = $row["used_value"];
}
$ops["JAK_" . strtoupper($row["varname"]) ] = $defvar;
}
// Now we will need the correct URL if wildcard
if (JAK_WILDCARD_SUBDOMAIN)
{
$subdomain = $jakdb->get("subscriptions", "business", ["opid" => $usr["id"], ]);
if (isset($subdomain) && !empty($subdomain))
{
// We will need to define the URL
$BASE_URL = (JAK_SITEHTTPS ? "https://" : "http://") . $subdomain . "." . FULL_SITE_DOMAIN . SITE_SUBFOLDER . "/";
}
else
{
// We have no wildcard subdomain or could not find it, just go normal
$BASE_URL = (JAK_SITEHTTPS ? "https://" : "http://") . FULL_SITE_DOMAIN . SITE_SUBFOLDER . "/";
}
}
// Connect to the correct language
include str_replace("cron/", "", $cron_url . "lang/" . $ops["JAK_LANG"] . ".php");
// Calculate which tickets we have to reminder
$ticketreminder = time() - $ops["JAK_TICKET_REMINDER"] * 86400;
// First check if we need to send a ticket reminder
$result = $jakdb->select($jaktable, ["[>]clients" => ["clientid" => "id"]], ["support_tickets.id", "support_tickets.depid", "support_tickets.name", "support_tickets.email", "support_tickets.subject", "clients.credits", "clients.paid_until", ], ["AND" => ["support_tickets.ended" => 0, "support_tickets.reminder" => 0, "support_tickets.updated[<]" => $ticketreminder, ], ]);
if (isset($result) && !empty($result))
{
foreach ($result as $row)
{
// Dashboard URL
$ticketurl = str_replace($url_filter, $url_replace, JAK_rewrite::jakParseurl($ops["JAK_SUPPORT_URL"], "t", $row["id"], JAK_rewrite::jakCleanurl($row["subject"])));
// Let's check if we have an imap
$answeremail = $ticktext = "";
$check_imap = $jakdb->get($jaktable2, "emailanswer", ["depid" => $row["depid"], ]);
if ($check_imap)
{
$answeremail = $check_imap;
}
// Get the ticket answer template
if (!empty($HD_ANSWERS) && is_array($HD_ANSWERS))
{
foreach ($HD_ANSWERS as $v)
{
if ($v["msgtype"] == 22 && $v["lang"] == $ops["JAK_LANG"])
{
$phold = ["{url}", "{title}", "{cemail}", "{cname}", "{credits}", "{paid_until}", "{ticket}", "{subject}", "{ticketurl}", "{email}", ];
$replace = [str_replace($cron_url, "", $BASE_URL) , $ops["JAK_TITLE"], $row["email"], $row["name"], $row["credits"], $row["paid_until"], "#" . $row["id"], $row["subject"], $ticketurl, $answeremail, ];
$ticktext = str_replace($phold, $replace, $v["message"]);
break;
}
};
}
if (!empty($ticktext))
{
$ticktext = '<p style="color:#c1c1c1;">-------------## Do Not Remove ##-------------</p>' . $ticktext;
// Get the email template
$nlhtml = file_get_contents(str_replace("cron/", "", $cron_url . "template/" . $ops["JAK_FRONT_TEMPLATE"] . "/email/index.html"));
// Change fake vars into real ones.
$cssAtt = ["{emailcontent}", "{weburl}", "{title}", "{emailtpllogo}", "{emailtplcopy}", ];
$cssUrl = [$ticktext, str_replace($cron_url, "", $BASE_URL) , $ops["JAK_TITLE"], $ops["JAK_EMAILTPLLOGO"], $ops["JAK_EMAILTPLCOPY"], ];
$nlcontent = str_replace($cssAtt, $cssUrl, $nlhtml);
$body = str_ireplace("[\]", "", $nlcontent);
$mail = new PHPMailer(); // defaults to using php "mail()" or optional SMTP
if ($ops["JAK_SMTP_MAIL"] == 1)
{
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = $ops["JAK_SMTPHOST"];
$mail->SMTPAuth = $ops["JAK_SMTP_AUTH"] ? true : false; // enable SMTP authentication
$mail->SMTPSecure = $ops["JAK_SMTP_PREFIX"]; // sets the prefix to the server
$mail->SMTPAutoTLS = false;
$mail->SMTPKeepAlive = $ops["JAK_SMTP_ALIVE"] ? true : false; // SMTP connection will not close after each email sent
$mail->Port = $ops["JAK_SMTPPORT"]; // set the SMTP port for the GMAIL server
$mail->Username = $ops["JAK_SMTPUSERNAME"]; // SMTP account username
$mail->Password = $ops["JAK_SMTPPASSWORD"]; // SMTP account password
}
elseif ($ops["JAK_SMTP_MAIL"] == 2)
{
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Set AuthType to use XOAUTH2
$mail->AuthType = "XOAUTH2";
$oauth_params = ["clientId" => $ops["JAK_OAUTH_CLIENTID"], "clientSecret" => $ops["JAK_OAUTH_SECRET"], ];
switch ($ops["JAK_OAUTH_PROVIDER"])
{
case "Google":
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$provider = new Google($oauth_params);
break;
case "Yahoo":
$mail->Host = "smtp.mail.yahoo.com";
$mail->Port = 465;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$provider = new Yahoo($oauth_params);
break;
case "Microsoft":
$mail->Host = "smtp.office365.com";
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$provider = new Microsoft($oauth_params);
break;
case "Azure":
$mail->Host = "smtp.office365.com";
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$params["tenantId"] = $ops["JAK_OAUTH_TENANTID"];
$provider = new Azure($oauth_params);
break;
}
if ($provider)
{
$mail->setOAuth(new OAuth(["provider" => $provider, "clientId" => $ops["JAK_OAUTH_CLIENTID"], "clientSecret" => $ops["JAK_OAUTH_SECRET"], "refreshToken" => $ops["JAK_OAUTH_REFRESH"], "userName" => $ops["JAK_SMTP_SENDER"], ]));
}
}
// Finally send the email
$mail->SetFrom($ops["JAK_SMTP_SENDER"]);
$mail->addAddress($row["email"]);
$mail->Subject = $ops["JAK_TITLE"] . " - RE:" . $row["subject"];
$mail->MsgHTML($body);
$mail->Send();
}
// Now we update the ticket table
$jakdb->update($jaktable, ["reminder" => 1], ["id" => $row["id"]]);
};
}
// Calculate which tickets we have to close
if ($ops["JAK_TICKET_CLOSE_C"] != 0)
{
$ticketclose = time() - $ops["JAK_TICKET_CLOSE_C"] * 86400;
// First check if we need to send a ticket reminder
$result = $jakdb->select($jaktable, ["[>]clients" => ["clientid" => "id"]], ["support_tickets.id", "support_tickets.depid", "support_tickets.name", "support_tickets.email", "support_tickets.subject", "clients.credits", "clients.paid_until", ], ["AND" => ["support_tickets.ended" => 0, "support_tickets.updated[<]" => $ticketclose, ], ]);
if (isset($result) && !empty($result))
{
foreach ($result as $row)
{
// Send email to customers if set so.
if ($ops["JAK_TICKET_CLOSE_R"] == 1)
{
// Dashboard URL
$ticketurl = str_replace($url_filter, $url_replace, JAK_rewrite::jakParseurl($ops["JAK_SUPPORT_URL"], "t", $row["id"], JAK_rewrite::jakCleanurl($row["subject"])));
// Let's check if we have an imap
$answeremail = $ticktext = "";
$check_imap = $jakdb->get($jaktable2, "emailanswer", ["depid" => $row["depid"], ]);
if ($check_imap)
{
$answeremail = $check_imap;
}
// Get the ticket answer template
if (!empty($HD_ANSWERS) && is_array($HD_ANSWERS))
{
foreach ($HD_ANSWERS as $v)
{
if ($v["msgtype"] == 23 && $v["lang"] == $ops["JAK_LANG"])
{
$phold = ["{url}", "{title}", "{cemail}", "{cname}", "{credits}", "{paid_until}", "{ticket}", "{subject}", "{ticketurl}", "{email}", ];
$replace = [str_replace($cron_url, "", $BASE_URL) , $ops["JAK_TITLE"], $row["email"], $row["name"], $row["credits"], $row["paid_until"], "#" . $row["id"], $row["subject"], $ticketurl, $answeremail, ];
$ticktext = str_replace($phold, $replace, $v["message"]);
break;
}
};
}
// Get the email template
if (!empty($ticktext))
{
$nlhtml = file_get_contents(str_replace("cron/", "", $cron_url . "template/" . $ops["JAK_FRONT_TEMPLATE"] . "/email/index.html"));
// Change fake vars into real ones.
$cssAtt = ["{emailcontent}", "{weburl}", "{title}", "{emailtpllogo}", "{emailtplcopy}", ];
$cssUrl = [$ticktext, str_replace($cron_url, "", $BASE_URL) , $ops["JAK_TITLE"], $ops["JAK_EMAILTPLLOGO"], $ops["JAK_EMAILTPLCOPY"], ];
$nlcontent = str_replace($cssAtt, $cssUrl, $nlhtml);
$body = str_ireplace("[\]", "", $nlcontent);
$mail = new PHPMailer(); // defaults to using php "mail()" or optional SMTP
if ($ops["JAK_SMTP_MAIL"] == 1)
{
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = $ops["JAK_SMTPHOST"];
$mail->SMTPAuth = $ops["JAK_SMTP_AUTH"] ? true : false; // enable SMTP authentication
$mail->SMTPSecure = $ops["JAK_SMTP_PREFIX"]; // sets the prefix to the server
$mail->SMTPAutoTLS = false;
$mail->SMTPKeepAlive = $ops["JAK_SMTP_ALIVE"] ? true : false; // SMTP connection will not close after each email sent
$mail->Port = $ops["JAK_SMTPPORT"]; // set the SMTP port for the GMAIL server
$mail->Username = $ops["JAK_SMTPUSERNAME"]; // SMTP account username
$mail->Password = $ops["JAK_SMTPPASSWORD"]; // SMTP account password
}
elseif ($ops["JAK_SMTP_MAIL"] == 2)
{
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Set AuthType to use XOAUTH2
$mail->AuthType = "XOAUTH2";
$oauth_params = ["clientId" => $ops["JAK_OAUTH_CLIENTID"], "clientSecret" => $ops["JAK_OAUTH_SECRET"], ];
switch ($ops["JAK_OAUTH_PROVIDER"])
{
case "Google":
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$provider = new Google($oauth_params);
break;
case "Yahoo":
$mail->Host = "smtp.mail.yahoo.com";
$mail->Port = 465;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$provider = new Yahoo($oauth_params);
break;
case "Microsoft":
$mail->Host = "smtp.office365.com";
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$provider = new Microsoft($oauth_params);
break;
case "Azure":
$mail->Host = "smtp.office365.com";
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$params["tenantId"] = $ops["JAK_OAUTH_TENANTID"];
$provider = new Azure($oauth_params);
break;
}
if ($provider)
{
$mail->setOAuth(new OAuth(["provider" => $provider, "clientId" => $ops["JAK_OAUTH_CLIENTID"], "clientSecret" => $ops["JAK_OAUTH_SECRET"], "refreshToken" => $ops["JAK_OAUTH_REFRESH"], "userName" => $ops["JAK_SMTP_SENDER"], ]));
}
}
// Finally send the email
$mail->SetFrom($ops["JAK_SMTP_SENDER"]);
$mail->addAddress($row["email"]);
$mail->Subject = $ops["JAK_TITLE"] . " - " . sprintf($jkl["hd101"], $row["subject"]);
$mail->MsgHTML($body);
// Send email to customer
$mail->Send();
}
} // end sending closed message
// Now we update the ticket table
$jakdb->update($jaktable, ["ended" => time() , "status" => 3], ["id" => $row["id"]]);
};
}
}
// Calculate which tickets we will send a rating email
$ticketrating = time() - $ops["JAK_TICKET_REOPEN"] * 86400;
// Send some ticket ratings emails which cannot be reopen again.
$restr = $jakdb->select($jaktable, ["[>]clients" => ["clientid" => "id"]], ["support_tickets.id", "support_tickets.depid", "support_tickets.name", "support_tickets.email", "support_tickets.subject", "support_tickets.initiated", "clients.credits", "clients.paid_until", ], ["AND" => ["support_tickets.status[>]" => 2, "support_tickets.reminder" => 1, "support_tickets.ended[<]" => $ticketrating, ], ]);
if (isset($restr) && !empty($restr))
{
foreach ($restr as $rowtr)
{
// Dashboard URL
$ticketratingurl = str_replace($url_filter, $url_replace, JAK_rewrite::jakParseurl($ops["JAK_CLIENT_URL"], "rt", $rowtr["id"], $rowtr["initiated"]));
// Let's check if we have an imap
$answeremail = $ticktext = "";
$check_imap = $jakdb->get($jaktable2, "emailanswer", ["depid" => $rowtr["depid"], ]);
if ($check_imap)
{
$answeremail = $check_imap;
}
// Get the ticket answer template
if (!empty($HD_ANSWERS) && is_array($HD_ANSWERS))
{
foreach ($HD_ANSWERS as $v)
{
if ($v["msgtype"] == 25 && $v["lang"] == $ops["JAK_LANG"])
{
$phold = ["{url}", "{title}", "{cemail}", "{cname}", "{credits}", "{paid_until}", "{ticket}", "{subject}", "{ticketurl}", "{email}", ];
$replace = [str_replace($cron_url, "", $BASE_URL) , $ops["JAK_TITLE"], $rowtr["email"], $rowtr["name"], $rowtr["credits"], $rowtr["paid_until"], "#" . $rowtr["id"], $rowtr["subject"], $ticketratingurl, $answeremail, ];
$ticktext = str_replace($phold, $replace, $v["message"]);
break;
}
};
}
if (!empty($ticktext))
{
// Get the email template
$nlhtml = file_get_contents(str_replace("cron/", "", $cron_url . "template/" . $ops["JAK_FRONT_TEMPLATE"] . "/email/index.html"));
// Change fake vars into real ones.
$cssAtt = ["{emailcontent}", "{weburl}", "{title}", "{emailtpllogo}", "{emailtplcopy}", ];
$cssUrl = [$ticktext, str_replace($cron_url, "", $BASE_URL) , $ops["JAK_TITLE"], $ops["JAK_EMAILTPLLOGO"], $ops["JAK_EMAILTPLCOPY"], ];
$nlcontent = str_replace($cssAtt, $cssUrl, $nlhtml);
$body = str_ireplace("[\]", "", $nlcontent);
$mail = new PHPMailer(); // defaults to using php "mail()" or optional SMTP
if ($ops["JAK_SMTP_MAIL"] == 1)
{
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = $ops["JAK_SMTPHOST"];
$mail->SMTPAuth = $ops["JAK_SMTP_AUTH"] ? true : false; // enable SMTP authentication
$mail->SMTPSecure = $ops["JAK_SMTP_PREFIX"]; // sets the prefix to the server
$mail->SMTPAutoTLS = false;
$mail->SMTPKeepAlive = $ops["JAK_SMTP_ALIVE"] ? true : false; // SMTP connection will not close after each email sent
$mail->Port = $ops["JAK_SMTPPORT"]; // set the SMTP port for the GMAIL server
$mail->Username = $ops["JAK_SMTPUSERNAME"]; // SMTP account username
$mail->Password = $ops["JAK_SMTPPASSWORD"]; // SMTP account password
}
elseif ($ops["JAK_SMTP_MAIL"] == 2)
{
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Set AuthType to use XOAUTH2
$mail->AuthType = "XOAUTH2";
$oauth_params = ["clientId" => $ops["JAK_OAUTH_CLIENTID"], "clientSecret" => $ops["JAK_OAUTH_SECRET"], ];
switch ($ops["JAK_OAUTH_PROVIDER"])
{
case "Google":
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$provider = new Google($oauth_params);
break;
case "Yahoo":
$mail->Host = "smtp.mail.yahoo.com";
$mail->Port = 465;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$provider = new Yahoo($oauth_params);
break;
case "Microsoft":
$mail->Host = "smtp.office365.com";
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$provider = new Microsoft($oauth_params);
break;
case "Azure":
$mail->Host = "smtp.office365.com";
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$params["tenantId"] = $ops["JAK_OAUTH_TENANTID"];
$provider = new Azure($oauth_params);
break;
}
if ($provider)
{
$mail->setOAuth(new OAuth(["provider" => $provider, "clientId" => $ops["JAK_OAUTH_CLIENTID"], "clientSecret" => $ops["JAK_OAUTH_SECRET"], "refreshToken" => $ops["JAK_OAUTH_REFRESH"], "userName" => $ops["JAK_SMTP_SENDER"], ]));
}
}
// Finally send the email
$mail->SetFrom($ops["JAK_SMTP_SENDER"]);
$mail->addAddress($rowtr["email"]);
$mail->Subject = $ops["JAK_TITLE"] . " - " . $jkl["g29"] . ": " . $rowtr["subject"];
$mail->MsgHTML($body);
$mail->Send();
}
// Now we update the ticket table
$jakdb->update($jaktable, ["reminder" => 2], ["id" => $rowtr["id"]]);
};
}
};
}
?>