mitrobill/system/autoload/Message.php

262 lines
11 KiB
PHP
Raw Normal View History

2023-03-06 14:48:05 +07:00
<?php
/**
2023-10-12 15:55:42 +07:00
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
* by https://t.me/ibnux
2023-03-06 14:48:05 +07:00
**/
2024-03-27 14:32:55 +07:00
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
2024-06-05 17:19:24 +07:00
use PEAR2\Net\RouterOS;
2024-03-27 14:32:55 +07:00
require $root_path . 'system/autoload/mail/Exception.php';
require $root_path . 'system/autoload/mail/PHPMailer.php';
require $root_path . 'system/autoload/mail/SMTP.php';
2023-10-12 15:55:42 +07:00
2023-03-06 14:48:05 +07:00
class Message
{
public static function sendTelegram($txt)
{
global $config;
2024-06-26 17:39:15 +07:00
run_hook('send_telegram', [$txt]); #HOOK
2023-03-06 14:48:05 +07:00
if (!empty($config['telegram_bot']) && !empty($config['telegram_target_id'])) {
2024-01-24 14:02:58 +07:00
return Http::getData('https://api.telegram.org/bot' . $config['telegram_bot'] . '/sendMessage?chat_id=' . $config['telegram_target_id'] . '&text=' . urlencode($txt));
2023-03-06 14:48:05 +07:00
}
}
public static function sendSMS($phone, $txt)
{
global $config;
2024-07-22 15:19:29 +07:00
if (empty($txt)) {
2024-04-15 14:30:51 +07:00
return "";
}
2024-06-26 17:39:15 +07:00
run_hook('send_sms', [$phone, $txt]); #HOOK
2023-03-06 14:48:05 +07:00
if (!empty($config['sms_url'])) {
2023-12-18 16:47:08 +07:00
if (strlen($config['sms_url']) > 4 && substr($config['sms_url'], 0, 4) != "http") {
if (strlen($txt) > 160) {
$txts = str_split($txt, 160);
2024-01-24 14:02:58 +07:00
try {
foreach ($txts as $txt) {
2024-06-05 17:19:24 +07:00
self::sendSMS($config['sms_url'], $phone, $txt);
2023-12-12 13:36:00 +07:00
}
2024-01-24 14:02:58 +07:00
} catch (Exception $e) {
// ignore, add to logs
_log("Failed to send SMS using Mikrotik.\n" . $e->getMessage(), 'SMS', 0);
2023-12-12 13:36:00 +07:00
}
2024-01-24 14:02:58 +07:00
} else {
2023-12-12 13:36:00 +07:00
try {
2024-06-05 17:19:24 +07:00
self::sendSMS($config['sms_url'], $phone, $txt);
2023-12-12 13:36:00 +07:00
} catch (Exception $e) {
// ignore, add to logs
_log("Failed to send SMS using Mikrotik.\n" . $e->getMessage(), 'SMS', 0);
}
}
2023-12-18 16:47:08 +07:00
} else {
$smsurl = str_replace('[number]', urlencode($phone), $config['sms_url']);
$smsurl = str_replace('[text]', urlencode($txt), $smsurl);
2024-01-24 14:02:58 +07:00
return Http::getData($smsurl);
2023-12-11 14:12:12 +07:00
}
2023-03-06 14:48:05 +07:00
}
}
2024-06-05 17:19:24 +07:00
public static function MikrotikSendSMS($router_name, $to, $message)
{
global $_app_stage, $client_m;
if ($_app_stage == 'demo') {
return null;
}
2024-07-22 15:19:29 +07:00
if (!isset($client_m)) {
2024-06-05 17:19:24 +07:00
$mikrotik = ORM::for_table('tbl_routers')->where('name', $router_name)->find_one();
$iport = explode(":", $mikrotik['ip_address']);
$client_m = new RouterOS\Client($iport[0], $mikrotik['username'], $mikrotik['password'], ($iport[1]) ? $iport[1] : null);
}
$smsRequest = new RouterOS\Request('/tool sms send');
$smsRequest
->setArgument('phone-number', $to)
->setArgument('message', $message);
$client_m->sendSync($smsRequest);
}
2023-03-06 14:48:05 +07:00
public static function sendWhatsapp($phone, $txt)
{
global $config;
2024-07-22 15:19:29 +07:00
if (empty($txt)) {
2024-06-26 17:39:15 +07:00
return "kosong";
2024-04-15 14:30:51 +07:00
}
2024-06-26 17:39:15 +07:00
run_hook('send_whatsapp', [$phone, $txt]); #HOOK
2023-03-06 14:48:05 +07:00
if (!empty($config['wa_url'])) {
2024-03-04 14:26:36 +07:00
$waurl = str_replace('[number]', urlencode(Lang::phoneFormat($phone)), $config['wa_url']);
2023-03-06 14:48:05 +07:00
$waurl = str_replace('[text]', urlencode($txt), $waurl);
2024-01-16 15:16:59 +07:00
return Http::getData($waurl);
2023-03-06 14:48:05 +07:00
}
}
2024-03-27 14:32:55 +07:00
public static function sendEmail($to, $subject, $body)
{
2024-07-22 15:19:29 +07:00
global $config, $PAGES_PATH, $_app_stage;
if (empty($body)) {
return "";
}
if (empty($to)) {
2024-04-15 14:30:51 +07:00
return "";
}
2024-06-26 17:39:15 +07:00
run_hook('send_email', [$to, $subject, $body]); #HOOK
2024-03-27 14:32:55 +07:00
if (empty($config['smtp_host'])) {
$attr = "";
if (!empty($config['mail_from'])) {
$attr .= "From: " . $config['mail_from'] . "\r\n";
}
if (!empty($config['mail_reply_to'])) {
$attr .= "Reply-To: " . $config['mail_reply_to'] . "\r\n";
}
mail($to, $subject, $body, $attr);
} else {
$mail = new PHPMailer();
$mail->isSMTP();
2024-07-22 15:19:29 +07:00
if ($_app_stage == 'Dev') {
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
}
2024-03-27 14:32:55 +07:00
$mail->Host = $config['smtp_host'];
$mail->SMTPAuth = true;
$mail->Username = $config['smtp_user'];
$mail->Password = $config['smtp_pass'];
$mail->SMTPSecure = $config['smtp_ssltls'];
$mail->Port = $config['smtp_port'];
if (!empty($config['mail_from'])) {
$mail->setFrom($config['mail_from']);
}
if (!empty($config['mail_reply_to'])) {
$mail->addReplyTo($config['mail_reply_to']);
}
2024-07-22 15:19:29 +07:00
2024-03-27 14:32:55 +07:00
$mail->addAddress($to);
$mail->Subject = $subject;
2024-07-22 15:19:29 +07:00
if (!file_exists($PAGES_PATH . DIRECTORY_SEPARATOR . 'Email.html')) {
if (!copy($PAGES_PATH . '_template' . DIRECTORY_SEPARATOR . 'Email.html', $PAGES_PATH . DIRECTORY_SEPARATOR . 'Email.html')) {
file_put_contents($PAGES_PATH . DIRECTORY_SEPARATOR . 'Email.html', Http::getData('https://raw.githubusercontent.com/hotspotbilling/phpnuxbill/master/pages_template/Email.html'));
}
}
if (file_exists($PAGES_PATH . DIRECTORY_SEPARATOR . 'Email.html')) {
$html = file_get_contents($PAGES_PATH . DIRECTORY_SEPARATOR . 'Email.html');
$html = str_replace('[[Subject]]', $subject, $html);
$html = str_replace('[[Company_Address]]', nl2br($config['address']), $html);
$html = str_replace('[[Company_Name]]', nl2br($config['CompanyName']), $html);
$html = str_replace('[[Body]]', nl2br($body), $html);
$mail->isHTML(true);
$mail->Body = $html;
} else {
$mail->isHTML(false);
$mail->Body = $body;
}
2024-03-27 14:32:55 +07:00
$mail->send();
2024-07-22 15:19:29 +07:00
//<p style="font-family: Helvetica, sans-serif; font-size: 16px; font-weight: normal; margin: 0; margin-bottom: 16px;">
2024-03-27 14:32:55 +07:00
}
}
public static function sendPackageNotification($customer, $package, $price, $message, $via)
2023-03-06 14:48:05 +07:00
{
2024-07-22 15:19:29 +07:00
global $ds, $config;
if (empty($message)) {
2024-04-15 14:30:51 +07:00
return "";
}
$msg = str_replace('[[name]]', $customer['fullname'], $message);
2024-03-04 14:26:36 +07:00
$msg = str_replace('[[username]]', $customer['username'], $msg);
$msg = str_replace('[[plan]]', $package, $msg);
2024-01-16 15:16:59 +07:00
$msg = str_replace('[[package]]', $package, $msg);
2024-04-05 09:40:11 +07:00
$msg = str_replace('[[price]]', Lang::moneyFormat($price), $msg);
2024-04-04 15:01:45 +07:00
list($bills, $add_cost) = User::getBills($customer['id']);
2024-07-22 15:19:29 +07:00
if ($add_cost > 0) {
2024-04-04 15:01:45 +07:00
$note = "";
foreach ($bills as $k => $v) {
$note .= $k . " : " . Lang::moneyFormat($v) . "\n";
}
2024-07-22 15:19:29 +07:00
$note .= "Total : " . Lang::moneyFormat($add_cost + $price) . "\n";
2024-04-04 15:01:45 +07:00
$msg = str_replace('[[bills]]', $note, $msg);
2024-07-22 15:19:29 +07:00
} else {
$msg = str_replace('[[bills]]', '', $msg);
2024-04-04 15:01:45 +07:00
}
if ($ds) {
$msg = str_replace('[[expired_date]]', Lang::dateAndTimeFormat($ds['expiration'], $ds['time']), $msg);
2024-07-22 15:19:29 +07:00
} else {
2024-04-21 10:59:20 +07:00
$msg = str_replace('[[expired_date]]', "", $msg);
2024-02-28 11:31:36 +07:00
}
2023-03-06 14:48:05 +07:00
if (
2024-03-04 14:26:36 +07:00
!empty($customer['phonenumber']) && strlen($customer['phonenumber']) > 5
2023-08-14 14:16:03 +07:00
&& !empty($message) && in_array($via, ['sms', 'wa'])
2023-03-06 14:48:05 +07:00
) {
if ($via == 'sms') {
echo Message::sendSMS($customer['phonenumber'], $msg);
2024-07-22 15:19:29 +07:00
} else if ($via == 'email') {
self::sendEmail($customer['email'], '[' . $config['CompanyName'] . '] ' . Lang::T("Internet Plan Reminder"), $msg);
2023-03-06 14:48:05 +07:00
} else if ($via == 'wa') {
echo Message::sendWhatsapp($customer['phonenumber'], $msg);
2023-03-06 14:48:05 +07:00
}
}
2023-08-14 14:16:03 +07:00
return "$via: $msg";
2023-03-06 14:48:05 +07:00
}
2024-07-22 15:19:29 +07:00
public static function sendBalanceNotification($cust, $balance, $balance_now, $message, $via)
2023-08-24 11:35:23 +07:00
{
2024-07-22 15:19:29 +07:00
global $config;
$msg = str_replace('[[name]]', $cust['fullname'] . ' (' . $cust['username'] . ')', $message);
2023-09-13 16:23:51 +07:00
$msg = str_replace('[[current_balance]]', Lang::moneyFormat($balance_now), $msg);
2024-01-16 09:52:12 +07:00
$msg = str_replace('[[balance]]', Lang::moneyFormat($balance), $msg);
2024-07-22 15:19:29 +07:00
$phone = $cust['phonenumber'];
2023-08-24 11:35:23 +07:00
if (
!empty($phone) && strlen($phone) > 5
2024-07-22 15:19:29 +07:00
&& !empty($message) && in_array($via, ['sms', 'wa', 'email'])
2023-08-24 11:35:23 +07:00
) {
if ($via == 'sms') {
Message::sendSMS($phone, $msg);
2024-07-22 15:19:29 +07:00
} else if ($config['user_notification_payment'] == 'email') {
self::sendEmail($cust['email'], '[' . $config['CompanyName'] . '] ' . Lang::T("Balance Notification"), $msg);
2023-08-24 11:35:23 +07:00
} else if ($via == 'wa') {
Message::sendWhatsapp($phone, $msg);
}
}
return "$via: $msg";
}
2023-10-20 13:57:12 +07:00
2023-12-12 13:36:00 +07:00
public static function sendInvoice($cust, $trx)
{
2023-10-20 13:57:12 +07:00
global $config;
$textInvoice = Lang::getNotifText('invoice_paid');
$textInvoice = str_replace('[[company_name]]', $config['CompanyName'], $textInvoice);
$textInvoice = str_replace('[[address]]', $config['address'], $textInvoice);
$textInvoice = str_replace('[[phone]]', $config['phone'], $textInvoice);
$textInvoice = str_replace('[[invoice]]', $trx['invoice'], $textInvoice);
$textInvoice = str_replace('[[date]]', Lang::dateAndTimeFormat($trx['recharged_on'], $trx['recharged_time']), $textInvoice);
2024-03-18 11:48:15 +07:00
if (!empty($trx['note'])) {
$textInvoice = str_replace('[[note]]', $trx['note'], $textInvoice);
}
2024-02-07 10:11:30 +07:00
$gc = explode("-", $trx['method']);
$textInvoice = str_replace('[[payment_gateway]]', trim($gc[0]), $textInvoice);
$textInvoice = str_replace('[[payment_channel]]', trim($gc[1]), $textInvoice);
2023-10-20 13:57:12 +07:00
$textInvoice = str_replace('[[type]]', $trx['type'], $textInvoice);
$textInvoice = str_replace('[[plan_name]]', $trx['plan_name'], $textInvoice);
$textInvoice = str_replace('[[plan_price]]', Lang::moneyFormat($trx['price']), $textInvoice);
$textInvoice = str_replace('[[name]]', $cust['fullname'], $textInvoice);
2024-03-14 12:14:11 +07:00
$textInvoice = str_replace('[[note]]', $cust['note'], $textInvoice);
2023-10-20 13:57:12 +07:00
$textInvoice = str_replace('[[user_name]]', $trx['username'], $textInvoice);
$textInvoice = str_replace('[[user_password]]', $cust['password'], $textInvoice);
2024-03-14 12:14:11 +07:00
$textInvoice = str_replace('[[username]]', $trx['username'], $textInvoice);
$textInvoice = str_replace('[[password]]', $cust['password'], $textInvoice);
2023-10-20 13:57:12 +07:00
$textInvoice = str_replace('[[expired_date]]', Lang::dateAndTimeFormat($trx['expiration'], $trx['time']), $textInvoice);
$textInvoice = str_replace('[[footer]]', $config['note'], $textInvoice);
if ($config['user_notification_payment'] == 'sms') {
Message::sendSMS($cust['phonenumber'], $textInvoice);
2024-07-22 15:19:29 +07:00
} else if ($config['user_notification_payment'] == 'email') {
self::sendEmail($cust['email'], '[' . $config['CompanyName'] . '] ' . Lang::T("Invoice") . ' #' . $trx['invoice'], $textInvoice);
2023-10-20 13:57:12 +07:00
} else if ($config['user_notification_payment'] == 'wa') {
Message::sendWhatsapp($cust['phonenumber'], $textInvoice);
}
}
2023-03-06 14:48:05 +07:00
}