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/phpimap.php

1398 lines
84 KiB

1 year ago
<?php
/*===============================================*\
|| ############################################# ||
|| # JAKWEB.CH / Version 2.1.1 # ||
|| # ----------------------------------------- # ||
|| # Copyright 2023 JAKWEB All Rights Reserved # ||
|| ############################################# ||
\*===============================================*/
$imap_url_orig = dirname(__FILE__) . DIRECTORY_SEPARATOR;
$imap_url = str_replace("cron" . DIRECTORY_SEPARATOR, "", $imap_url_orig);
if (!file_exists($imap_url . "include/db.php"))
{
die("[cron.php] db.php not exist");
}
require_once $imap_url . "include/db.php";
if (!file_exists($imap_url . "class/class.db.php"))
{
die("class/[cron.php] class.db.php not exist");
}
require_once $imap_url . "class/class.db.php";
if (!file_exists($imap_url . "class/class.jakbase.php"))
{
die("class/[cron.php] class.jakbase.php not exist");
}
require_once $imap_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/", "", $imap_url . "include/functions.php");
include_once str_replace("cron/", "", $imap_url . "class/class.browser.php");
require_once str_replace("cron/", "", $imap_url . "vendor/autoload.php");
// is php imap extension installed
if (!function_exists("imap_open"))
{
die("PHP IMAP extension is not installed");
}
if (!file_exists($imap_url . "class/class.imap.php"))
{
die("phpimap.php] class.imap.php not exist");
}
require_once $imap_url . "class/class.imap.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($imap_url);
$url_filter = $imap_url;
$url_replace = "/" . basename($path_parts["dirname"]) . "/";
}
else
{
$url_filter = "/cron/";
$url_replace = "/";
}
// 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 . "/";
}
// All the tables we need for this plugin
$jaktable = "clients";
$jaktable1 = "support_tickets";
$jaktable2 = "ticket_answers";
$jaktable3 = "support_departments";
$jaktable4 = "php_imap";
$jaktable5 = "ticketpriority";
$jaktable6 = "ticketoptions";
$jaktable7 = "answers";
// Write the log file each time someone tries to login before
JAK_base::jakWhatslog("System", 0, 0, 0, 39, 0, isset($_COOKIE["WIOgeoData"]) ? $_COOKIE["WIOgeoData"] : "", "Cron Job - PHP IMAP", isset($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"] : $imap_url, 0, "phpimap");
// Get the database and go through the accounts
$resacc = $jakdb->select($jaktable4, "*", ["active" => 1]);
if (isset($resacc) && !empty($resacc))
{
foreach ($resacc as $accounts)
{
// collect each record into $accounts
// open connection
$imap = new Imap($accounts["mailbox"], $accounts["username"], $accounts["password"], $accounts["encryption"]);
// proceed on success
if ($imap->isConnected())
{
// We need to reset the array
$ops = [];
// OK, now import the correct cache file
$datasett = $jakdb->select("settings", ["varname", "used_value"], ["opid" => $accounts["opid"]]);
foreach ($datasett as $row)
{
// Now check if sting contains html and do something about it!
if (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;
}
$setta = $jakdb->get("settings", ["varname", "used_value"], ["varname" => "allowed_files"]);
$ops["JAK_" . strtoupper($setta["varname"]) ] = $setta["used_value"];
// Connect to the correct language
include str_replace("cron/", "", $imap_url . "lang/" . $ops["JAK_LANG"] . ".php");
// Now we will need the correct URL if wildcard
if (JAK_WILDCARD_SUBDOMAIN)
{
$subdomain = $jakdb->get("subscriptions", "business", ["opid" => $accounts["opid"], ]);
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 . "/";
}
}
// select folder
$imap->selectFolder($accounts["scanfolder"]);
// fetch new emails only
$emails = $imap->getUnreadMessages();
// Go through each email
if (isset($emails) && !empty($emails))
{
foreach ($emails as $v)
{
// Set time for this email
$timeupdate = time();
//---------------------- GET EMAIL HEADER INFO -----------------------//
// Message ID
$msgID = $v["uid"];
// Reset some vars
$toName = "";
//get the name and email of the sender
$sname = filter_var($v["fromname"], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$semail = filter_var($v["from"], FILTER_SANITIZE_EMAIL);
//get the name and email of the recipient
$toEmail = filter_var($v["to"], FILTER_SANITIZE_EMAIL);
$toName = filter_var($v["to"], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
//get the subject
$subject = !empty($v["subject"]) ? utf8_decode(filter_var($v["subject"], FILTER_SANITIZE_FULL_SPECIAL_CHARS)) : $jkl["hd121"];
// get the body
$body = $v["body"];
// Referer
$referrer = "PHP IMAP";
// We don't load attachments
$loadattach = false;
// New black list check
if ($ops["JAK_EMAIL_BLOCK"])
{
$blockede = explode(",", $ops["JAK_EMAIL_BLOCK"]);
if (in_array($semail, $blockede) || in_array(strrchr($semail, "@") , $blockede))
{
// We do nothing with this email because it is blocked and just set it to read
if ($accounts["msgdel"])
{
$imap->setUnseenMessage($msgID, true);
}
else
{
$imap->deleteMessage($msgID);
}
break;
}
}
// Now we check if the mail is coming from a system email and stop the process as well
$blockedcc = explode(",", $ops["JAK_EMAILCC"]);
if ($accounts["emailanswer"] == $semail || in_array($semail, $blockedcc))
{
// We do nothing with this email because it is blocked and just set it to read
if ($accounts["msgdel"])
{
$imap->setUnseenMessage($msgID, true);
}
else
{
$imap->deleteMessage($msgID);
}
break;
}
// Check if body is not empty otherwise answer back so it uses the HelpDesk online form.
if (isset($msgID) && is_numeric($msgID) && isset($body) && !empty($body) && isset($semail) && !empty($semail))
{
// So far we have no error
$errorpipe = false;
//get rid of any quoted text in the email body
$body_array = explode("\n", $body);
$cleanmsg = $message = "";
foreach ($body_array as $key => $value)
{
//remove hotmail sig
if ($value == "________________________________")
{
break;
// Remove text underneath
}
elseif (preg_match("/-------------## Do Not Remove ##-------------/", $value, $matches))
{
break;
//original message quote
}
elseif (preg_match("/^-*(.*)Original Message(.*)-*/i", $value, $matches))
{
break;
//check for date wrote string
}
elseif (preg_match("/^On(.*)wrote:(.*)/i", $value, $matches))
{
break;
//check for To Name email section
}
elseif (preg_match("/^On(.*)$toName(.*)/i", $value, $matches))
{
break;
//check for To Email email section
}
elseif (preg_match("/^(.*)$toEmail(.*)wrote:(.*)/i", $value, $matches))
{
break;
//check for quoted ">" section
}
elseif (preg_match("/^>(.*)/i", $value, $matches))
{
break;
//check for date wrote string with dashes
}
elseif (preg_match("/^---(.*)On(.*)wrote:(.*)/i", $value, $matches))
{
break;
//add line to body
}
else
{
$message .= "$value\n";
}
}
// convert the text into a nice format
$message = trim($message);
$message = preg_replace("/(\R){2,}/", "$1", $message);
$message = "<p>" . nl2br($message) . "</p>";
// Clean the text
$cleanmsg = jak_clean_safe_userpost($message);
// Now we have the header and body information let's do some PHP/MySQL Magic
if (!empty($cleanmsg))
{
// First let's find out if the user exists already in our database.
$row = $jakdb->get($jaktable, ["id", "name", "email", "canupload", "credits", "paid_until", ], ["AND" => ["opid" => $accounts["opid"], "email" => $semail, ], ]);
// Now check if we have an existing client
if (isset($row["id"]) && is_numeric($row["id"]))
{
$semail = $row["email"];
$sname = $row["name"];
$clientid = $row["id"];
}
else
{
// Check if we can have a client if not mark the email
if ($ops["JAK_TICKET_GUEST"] == 0)
{
if ($accounts["msgdel"])
{
$imap->setUnseenMessage($msgID, true);
}
else
{
$imap->deleteMessage($msgID);
}
break;
}
// Do we want a new user?
if ($ops["JAK_TICKET_ACCOUNT"])
{
// create new password
$password = jak_password_creator();
$passcrypt = hash_hmac("sha256", $password, DB_PASS_HASH);
$jakdb->insert($jaktable, ["opid" => $accounts["opid"], "chat_dep" => $ops["JAK_STANDARD_CHAT_DEP"], "support_dep" => $ops["JAK_STANDARD_SUPPORT_DEP"], "faq_cat" => $ops["JAK_STANDARD_FAQ_CAT"], "name" => $sname, "email" => $semail, "password" => $passcrypt, "canupload" => 1, "access" => 1, "time" => $jakdb->raw("NOW()") , ]);
$uid = $jakdb->id();
// Create a folder
$newuserpath = str_replace("cron/", "", $imap_url . JAK_FILES_DIRECTORY . "/clients/" . $uid);
if (!is_dir($newuserpath))
{
mkdir($newuserpath, 0755);
copy(str_replace("cron/", "", $imap_url . JAK_FILES_DIRECTORY . "/clients/index.html") , $newuserpath . "/index.html");
}
// Write the log file each time someone tries to login before
JAK_base::jakWhatslog("", $accounts["opid"], 0, $uid, 12, 0, isset($_COOKIE["WIOgeoData"]) ? $_COOKIE["WIOgeoData"] : "", $semail, $_SERVER["REQUEST_URI"], 0, "phpimap");
// Now send the email to the customer
// Get the email template
$nlhtml = file_get_contents(str_replace("cron/", "", $imap_url . "template/" . $ops["JAK_FRONT_TEMPLATE"] . "/email/index.html"));
// Get the correct answer
$hda = $jakdb->get($jaktable7, ["id", "message"], ["AND" => ["opid" => $accounts["opid"], "msgtype" => 14, "lang" => $ops["JAK_LANG"], ], ]);
// Set the email Content
$phold = ["{url}", "{title}", "{cname}", "{cemail}", "{cpassword}", "{email}", ];
$replace = [str_replace($url_filter, $url_replace, $BASE_URL) , $ops["JAK_TITLE"], $sname, $semail, $password, $ops["JAK_EMAIL"], ];
$regtext = str_replace($phold, $replace, $hda["message"]);
// Change fake vars into real ones.
$cssAtt = ["{emailcontent}", "{weburl}", "{title}", "{emailtpllogo}", "{emailtplcopy}", ];
$cssUrl = [$regtext, str_replace($url_filter, $url_replace, $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
// OAuth2
}
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($semail);
$mail->Subject = $ops["JAK_TITLE"] . " - " . $jkl["hd33"];
$mail->MsgHTML($body);
$mail->Send();
// Now we know where to put it. $accounts["depid"]
$clientid = $uid;
}
else
{
$clientid = 0;
}
}
// We will now need the support status
$support_status_id = $jakdb->get("support_status", "id", ["opid" => $accounts["opid"], "ORDER" => ["dorder" => "ASC"], ]);
// let's check if that is an answer or if we have to create a new ticket
if (preg_match("/\[\#[0-9]{1,99}\]/", $subject, $ticket))
{
// Get the ticket id
$ticketid = filter_var($ticket[0], FILTER_SANITIZE_NUMBER_INT);
$row1 = $jakdb->get($jaktable1, ["id", "subject", "depid", "operatorid", "clientid", "name", "email", "subject", ], ["AND" => ["id" => $ticketid, "opid" => $accounts["opid"], "email" => $semail, ], ]);
if (isset($row1["id"]) && is_numeric($row1["id"]))
{
$jakdb->update($jaktable1, ["status" => $support_status_id, "ended" => 0, "updated" => $timeupdate, ], ["AND" => ["id" => $row1["id"], "opid" => $accounts["opid"], ], ]);
$jakdb->insert($jaktable2, ["ticketid" => $row1["id"], "clientid" => $row1["clientid"], "content" => $cleanmsg, "lastedit" => $jakdb->raw("NOW()") , "sent" => $jakdb->raw("NOW()") , ]);
// Get the ID from the answer
$lastid = $jakdb->id();
// Write the log file each time someone tries to login before
JAK_base::jakWhatslog("", $accounts["opid"], 0, $row1["clientid"], 32, $lastid, isset($_COOKIE["WIOgeoData"]) ? $_COOKIE["WIOgeoData"] : "", $row1["email"], $_SERVER["REQUEST_URI"], 0, "phpimap");
// Dashboard URL
$ticketurl = str_replace($url_filter, $url_replace, JAK_rewrite::jakParseurl($ops["JAK_SUPPORT_URL"], "t", $row1["id"], JAK_rewrite::jakCleanurl($row1["subject"])));
// Let's check if we have an imap
$ticktext = "";
// Get the correct answer
$hda = $jakdb->get($jaktable7, ["id", "message"], ["AND" => ["opid" => $accounts["opid"], "msgtype" => 21, "lang" => $ops["JAK_LANG"], ], ]);
// Get the ticket answer template
$phold = ["{url}", "{title}", "{cemail}", "{cname}", "{credits}", "{paid_until}", "{ticket}", "{subject}", "{ticketurl}", "{email}", "{ticketcontent}", ];
$replace = [str_replace($url_filter, $url_replace, $BASE_URL) , $ops["JAK_TITLE"], $row["email"], $row["name"], $row["credits"], $row["paid_until"], "#" . $row1["id"], $row1["subject"], $ticketurl, $accounts["emailanswer"], replace_urls_emails($cleanmsg, str_replace($url_filter, $url_replace, $BASE_URL) , JAK_FILES_DIRECTORY) , ];
$ticktext = str_replace($phold, $replace, $hda["message"]);
if (!empty($ticktext))
{
$ticktext = '<p style="color:#c1c1c1;">-------------## Do Not Remove ##-------------</p>' . $ticktext;
// Get the email template
$nlhtml = file_get_contents(str_replace("cron/", "", $imap_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($url_filter, $url_replace, $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
// OAuth2
}
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"], ]));
}
}
// Check if we have an imap
// Finally send the email
$mail->SetFrom($ops["JAK_SMTP_SENDER"]);
$mail->AddReplyTo($accounts["emailanswer"]);
$mail->addAddress($row["email"]);
$mail->Subject = $ops["JAK_TITLE"] . " - [#" . $row1["id"] . "] - RE:" . $row1["subject"];
$mail->MsgHTML($body);
$mail->Send();
}
// We will need to inform the operator if set so
if ($ops["JAK_TICKET_INFORM_R"])
{
// Get the email template
$nlhtml = file_get_contents(str_replace("cron/", "", $imap_url . "template/" . $ops["JAK_FRONT_TEMPLATE"] . "/email/index.html"));
// Operator URL
$ticketurl = str_replace($url_filter, $url_replace, JAK_rewrite::jakParseurl(JAK_OPERATOR_LOC, "support", "read", $page2));
// Change fake vars into real ones.
$cssAtt = ["{emailcontent}", "{weburl}", "{title}", "{emailtpllogo}", "{emailtplcopy}", ];
$cssUrl = [sprintf($jkl["hd93"], $ticketurl) , str_replace($url_filter, $url_replace, $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"])
{
$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
// OAuth2
}
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"], ]));
}
}
// We need to send it to the department as well
if (isset($HD_SUPPORT_DEPARTMENTS) && is_array($HD_SUPPORT_DEPARTMENTS))
{
foreach ($HD_SUPPORT_DEPARTMENTS as $hd)
{
if ($hd["id"] == $accounts["depid"])
{
if ($hd["email"])
{
$mail->AddCC($hd["email"]);
}
}
}
}
// CC? Yes it does, send it to following address
if (defined($ops["JAK_EMAILCC"]))
{
$emailarray = explode(",", $ops["JAK_EMAILCC"]);
if (is_array($emailarray))
{
foreach ($emailarray as $ea)
{
$mail->AddCC(trim($ea));
}
}
}
// operator is set.
$sendopemail = $ops["JAK_SMTP_SENDER"];
if ($row1["operatorid"] != 0)
{
$sendopemail = $jakdb->get("user", "email", ["id" => $row1["operatorid"]]);
}
// Finally send the email
$mail->SetFrom($ops["JAK_SMTP_SENDER"]);
$mail->addAddress($sendopemail);
$mail->AddReplyTo($semail);
$mail->Subject = $ops["JAK_TITLE"] . " - RE:" . $row1["subject"];
$mail->MsgHTML($body);
// We sent silently
$mail->Send();
}
}
// will create a new ticket
}
else
{
// Let check if that client has reached the limit to open a new ticket
if ($ops["JAK_TICKET_LIMIT"] != 0 && $ops["JAK_TICKET_ACCOUNT"])
{
$totaltickets = $jakdb->count($jaktable1, "id", ["AND" => ["status" => 2, "clientid" => $clientid, ], ]);
if ($ops["JAK_TICKET_LIMIT"] <= $totaltickets)
{
// inform the client
// Do we have legal call from a email client.
if (isset($semail) && !empty($semail))
{
// Ticket error inform the client
$ticketonline = sprintf($jkl["hd115"], str_replace($url_filter, $url_replace, JAK_rewrite::jakParseurl($ops["JAK_CLIENT_URL"])));
// Get the email template
$nlhtml = file_get_contents(str_replace("cron/", "", $imap_url . "template/" . $ops["JAK_FRONT_TEMPLATE"] . "/email/index.html"));
// Change fake vars into real ones.
$cssAtt = ["{emailcontent}", "{weburl}", "{title}", "{emailtpllogo}", "{emailtplcopy}", ];
$cssUrl = [$ticketonline, str_replace($url_filter, $url_replace, $BASE_URL) , $ops["JAK_TITLE"], $ops["JAK_EMAILTPLLOGO"], $ops["JAK_EMAILTPLCOPY"], ];
$nlcontent = str_replace($cssAtt, $cssUrl, $nlhtml);
$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
// OAuth2
}
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($semail);
$mail->Subject = $ops["JAK_TITLE"] . " - " . $jkl["hd116"];
$mail->MsgHTML($nlcontent);
$mail->Send();
//Clear all addresses and attachments for the next iteration
$mail->clearAddresses();
}
// and we mark the message as read
if (isset($msgID))
{
if ($accounts["msgdel"])
{
$imap->setUnseenMessage($msgID, true);
}
else
{
$imap->deleteMessage($msgID);
}
}
break;
}
}
// Now we know where to put it. $accounts["depid";
// Create the ticket
$result = $jakdb->insert($jaktable1, ["opid" => $accounts["opid"], "depid" => $accounts["depid"], "subject" => $subject, "content" => $cleanmsg, "clientid" => $clientid, "name" => $sname, "email" => $semail, "referrer" => "PHP IMAP", "private" => $ops["JAK_TICKET_PRIVATE"], "priorityid" => $ops["JAK_STANDARD_TICKET_PRIORITY"], "toptionid" => $ops["JAK_STANDARD_TICKET_OPTION"], "status" => $support_status_id, "updated" => $timeupdate, "initiated" => $timeupdate, ]);
$ticketid = $jakdb->id();
if ($result)
{
// Write the log file each time someone tries to login before
JAK_base::jakWhatslog("", $accounts["opid"], 0, $clientid, 8, $ticketid, isset($_COOKIE["WIOgeoData"]) ? $_COOKIE["WIOgeoData"] : "", $semail, $_SERVER["REQUEST_URI"], 0, "phpimap");
// Set the client ticket request +1
$jakdb->update($jaktable, ["supportrequests[+]" => 1], ["id" => $clientid]);
// We run on a credit based system?
if ($ops["JAK_BILLING_MODE"] == 1 && $totalc != 0)
{
$priocredit = $optcredit = 0;
if ($ops["JAK_STANDARD_TICKET_PRIORITY"])
{
$priocredit = $jakdb->get($jaktable5, "credits", ["AND" => ["id" => $ops["JAK_STANDARD_TICKET_PRIORITY"], "opid" => $accounts["opid"], ], ]);
}
if ($ops["JAK_STANDARD_TICKET_OPTION"])
{
$optcredit = $jakdb->get($jaktable6, "credits", ["AND" => ["id" => $ops["JAK_STANDARD_TICKET_OPTION"], "opid" => $accounts["opid"], ], ]);
}
$depcredit = 0;
if (isset($HD_SUPPORT_DEPARTMENTS) && is_array($HD_SUPPORT_DEPARTMENTS))
{
foreach ($HD_SUPPORT_DEPARTMENTS as $hd)
{
if ($hd["id"] == $accounts["depid"])
{
$depcredit = $hd["credits"];
}
}
}
$totalc = $depcredit + $priocredit + $optcredit;
// We need to get the credits
$jakdb->update($jaktable, ["credits[-]" => $totalc], ["id" => $clientid]);
}
// Finally we inform the customer about the new ticket
// Dashboard URL
$ticketurl = str_replace($url_filter, $url_replace, JAK_rewrite::jakParseurl($ops["JAK_SUPPORT_URL"], "t", $ticketid, JAK_rewrite::jakCleanurl($subject)));
// Get the correct answer
$hda = $jakdb->get($jaktable7, ["id", "message"], ["AND" => ["opid" => $accounts["opid"], "msgtype" => 20, "lang" => $ops["JAK_LANG"], ], ]);
// Get the ticket answer template
$phold = ["{url}", "{title}", "{cemail}", "{cname}", "{credits}", "{paid_until}", "{ticket}", "{subject}", "{ticketurl}", "{email}", "{ticketcontent}", ];
$replace = [str_replace($url_filter, $url_replace, $BASE_URL) , $ops["JAK_TITLE"], $semail, $sname, $row["credits"], $row["paid_until"], "#" . $ticketid, $subject, $ticketurl, $accounts["emailanswer"], replace_urls_emails($cleanmsg, str_replace($url_filter, $url_replace, $BASE_URL) , JAK_FILES_DIRECTORY) , ];
$ticktext = str_replace($phold, $replace, $hda["message"]);
if (!empty($ticktext))
{
$ticktext = '<p style="color:#c1c1c1;">-------------## Do Not Remove ##-------------</p>' . $ticktext;
// Get the email template
$nlhtml = file_get_contents(str_replace("cron/", "", $imap_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($url_filter, $url_replace, $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
// OAuth2
}
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($semail);
$mail->AddReplyTo($accounts["emailanswer"]);
$mail->Subject = $ops["JAK_TITLE"] . " - [#" . $ticketid . "] - RE:" . $subject;
$mail->MsgHTML($body);
$mail->Send();
//Clear all addresses and attachments for the next iteration
$mail->clearAddresses();
}
// We will need to inform the operator if set so
if ($ops["JAK_TICKET_INFORM_R"])
{
// Get the email template
$nlhtml = file_get_contents(str_replace("cron/", "", $imap_url . "template/" . $ops["JAK_FRONT_TEMPLATE"] . "/email/index.html"));
// Operator URL
$ticketurl = str_replace($url_filter, $url_replace, JAK_rewrite::jakParseurl(JAK_OPERATOR_LOC, "support", "read", $ticketid));
// Change fake vars into real ones.
$cssAtt = ["{emailcontent}", "{weburl}", "{title}", "{emailtpllogo}", "{emailtplcopy}", ];
$cssUrl = [sprintf($jkl["hd94"], $ticketurl) , str_replace($url_filter, $url_replace, $BASE_URL) , $ops["JAK_TITLE"], $ops["JAK_EMAILTPLLOGO"], $ops["JAK_EMAILTPLCOPY"], ];
$nlcontent = str_replace($cssAtt, $cssUrl, $nlhtml);
$bodya = str_ireplace("[\]", "", $nlcontent);
$maila = new PHPMailer(); // defaults to using php "mail()" or optional SMTP
if ($ops["JAK_SMTP_MAIL"] == 1)
{
$maila->IsSMTP(); // telling the class to use SMTP
$maila->Host = $ops["JAK_SMTPHOST"];
$maila->SMTPAuth = $ops["JAK_SMTP_AUTH"] ? true : false; // enable SMTP authentication
$maila->SMTPSecure = $ops["JAK_SMTP_PREFIX"]; // sets the prefix to the server
$mail->SMTPAutoTLS = false;
$maila->SMTPKeepAlive = $ops["JAK_SMTP_ALIVE"] ? true : false; // SMTP connection will not close after each email sent
$maila->Port = $ops["JAK_SMTPPORT"]; // set the SMTP port for the GMAIL server
$maila->Username = $ops["JAK_SMTPUSERNAME"]; // SMTP account username
$maila->Password = $ops["JAK_SMTPPASSWORD"]; // SMTP account password
// OAuth2
}
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"], ]));
}
}
// We need to send it to the department as well
if (isset($HD_SUPPORT_DEPARTMENTS) && is_array($HD_SUPPORT_DEPARTMENTS))
{
foreach ($HD_SUPPORT_DEPARTMENTS as $hd)
{
if ($hd["id"] == $accounts["depid"])
{
if ($hd["email"])
{
$maila->AddCC($hd["email"]);
}
}
}
}
// CC? Yes it does, send it to following address
if (defined($ops["JAK_EMAILCC"]))
{
$emailarray = explode(",", $ops["JAK_EMAILCC"]);
if (is_array($emailarray))
{
foreach ($emailarray as $ea)
{
$maila->AddCC(trim($ea));
}
}
}
// Finally send the email
$maila->SetFrom($ops["JAK_SMTP_SENDER"]);
$maila->AddReplyTo($semail);
$maila->addAddress($ops["JAK_EMAIL"]);
$maila->Subject = $ops["JAK_TITLE"] . " - " . $subject;
$maila->MsgHTML($body);
// We sent silently
$maila->Send();
// Clear all addresses and attachments for the next iteration
$maila->clearAddresses();
}
}
}
$loadattach = true;
}
if ($accounts["takeattach"] && $loadattach)
{
//------------------------ ATTACHMENTS ------------------------------------//
if (isset($msgID) && !empty($msgID))
{
$att = $imap->getAttachment($msgID);
}
if (isset($att) && !empty($att))
{
// Get first the general stuff
$rowa = $jakdb->get($jaktable1, ["id", "depid", "attachments"], ["AND" => ["id" => $ticketid, "opid" => $accounts["opid"], ], ]);
// Can we have more attachments
if ($ops["JAK_TICKET_ATTACH"] == 0 || ($ops["JAK_TICKET_ATTACH"] != 0 && $rowa["attachments"] <= $ops["JAK_TICKET_ATTACH"]))
{
// first get the target path
$targetPathd = CLIENT_UPLOAD_DIR . "/" . $accounts["opid"] . "/support/" . $rowa["id"] . "/";
$targetPath = str_replace("//", "/", $targetPathd);
foreach ($att as $f)
{
if ($f["is_attachment"])
{
$filename = $f["name"] ? $f["name"] : $f["filename"];
if (!preg_match("!!u", $filename))
{
$filename = utf8_decode($filename);
}
$jak_xtension = pathinfo($filename);
// Check if the extension is valid
$allowedf = explode(",", $ops["JAK_ALLOWED_FILES"]);
if (in_array("." . $jak_xtension["extension"], $allowedf))
{
// Create the target path
if (!is_dir($targetPath))
{
mkdir($targetPath, 0755, true);
}
// Keep the file name but sanitized
$fileName = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", "", $jak_xtension["filename"]);
$fileName = mb_ereg_replace("([\.]{2,})", "", $fileName);
$fileName = preg_replace("/\s+/", "_", $fileName);
$fileName = "u_" . str_replace(".", "_", microtime(true)) . "_" . $fileName . "." . $jak_xtension["extension"];
file_put_contents($targetPath . "/" . $fileName, $f["attachment"], LOCK_EX);
// Update counter on ticket
$jakdb->update($jaktable1, ["attachments[+]" => 1], ["AND" => ["id" => $rowa["id"], "opid" => $accounts["opid"], ], ]);
// Now we update the answer table so we can have it in the conversation.
$jakdb->insert("ticket_answers", ["ticketid" => $ticketid, "clientid" => $clientid, "content" => $fileName, "file" => 1, "lastedit" => $jakdb->raw("NOW()") , "sent" => $jakdb->raw("NOW()") , ]);
}
}
}
}
}
}
}
else
{
$errorpipe = true;
}
// and we mark the message as read
if (isset($msgID))
{
if ($accounts["msgdel"])
{
$imap->setUnseenMessage($msgID, true);
}
else
{
$imap->deleteMessage($msgID);
}
}
// Output the error means we send it by email
if (isset($errorpipe) && $errorpipe == true)
{
// We will need to inform the operator if set so
if ($ops["JAK_TICKET_INFORM_R"])
{
// Get the email template
$nlhtml = file_get_contents(str_replace("cron/", "", $imap_url . "template/" . $ops["JAK_FRONT_TEMPLATE"] . "/email/index.html"));
// Change fake vars into real ones.
$cssAtt = ["{emailcontent}", "{weburl}", "{title}", "{emailtpllogo}", "{emailtplcopy}", ];
$cssUrl = ["There has been an error to pull in a message from: " . $semail, str_replace($url_filter, $url_replace, $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
// OAuth2
}
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"], ]));
}
}
// We need to send it to the department as well
if (isset($HD_SUPPORT_DEPARTMENTS) && is_array($HD_SUPPORT_DEPARTMENTS))
{
foreach ($HD_SUPPORT_DEPARTMENTS as $v)
{
if ($v["id"] == $accounts["depid"])
{
if ($v["email"])
{
$mail->AddCC($v["email"]);
}
}
}
}
// CC? Yes it does, send it to following address
if (defined($ops["JAK_EMAILCC"]))
{
$emailarray = explode(",", $ops["JAK_EMAILCC"]);
if (is_array($emailarray))
{
foreach ($emailarray as $ea)
{
$mail->AddCC(trim($ea));
}
}
}
// Finally send the email
$mail->SetFrom($ops["JAK_SMTP_SENDER"]);
$mail->addAddress($ops["JAK_EMAIL"]);
$mail->AddReplyTo($semail);
$mail->Subject = $ops["JAK_TITLE"] . " - PHP IMAP ERROR";
$mail->MsgHTML($body);
// We sent silently
$mail->Send();
// Clear all addresses and attachments for the next iteration
$mail->clearAddresses();
}
}
}
}
else
{
// Do we have legal call from a email client or is there an email to fetch.
}
// On error inform the admin
}
else
{
// close connection
$imap->close();
// Ticket error inform the client
$imaperror = "Following error occured when trying to connect to the mailbox " . $accounts["mailbox"] . ", with username " . $accounts["username"] . ": " . $imap->getError();
// Get the email template
$nlhtml = file_get_contents(str_replace("cron/", "", $imap_url . "template/" . $ops["JAK_FRONT_TEMPLATE"] . "/email/index.html"));
// Change fake vars into real ones.
$cssAtt = ["{emailcontent}", "{weburl}", "{title}", "{emailtpllogo}", "{emailtplcopy}", ];
$cssUrl = [$imaperror, str_replace($url_filter, $url_replace, $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->AddReplyTo($ops["JAK_EMAIL"]);
$mail->Subject = $ops["JAK_TITLE"] . " - PHP IMAP ERROR";
$mail->MsgHTML($body);
// We sent silently
$mail->Send();
//Clear all addresses and attachments for the next iteration
$mail->clearAddresses();
}
}
}
?>