212 lines
8.2 KiB
PHP
212 lines
8.2 KiB
PHP
<?php
|
|
/**
|
|
* SMS Center Notify: WHMCS hooks. Each hook is wrapped so that a failure never stops WHMCS.
|
|
*/
|
|
|
|
use MozoutMzSmsCenter\Core;
|
|
use WHMCS\Database\Capsule;
|
|
|
|
if (!defined('WHMCS')) {
|
|
die('This file cannot be accessed directly');
|
|
}
|
|
|
|
require_once __DIR__ . '/lib/Core.php';
|
|
|
|
if (!function_exists('mozout_mz_smsc_safe')) {
|
|
function mozout_mz_smsc_safe(callable $fn)
|
|
{
|
|
try {
|
|
if (!Capsule::schema()->hasTable(Core::T_LOG)) {
|
|
return;
|
|
}
|
|
$fn();
|
|
} catch (\Throwable $e) {
|
|
try {
|
|
logActivity('SMS Center Notify: ' . $e->getMessage());
|
|
} catch (\Throwable $e2) {
|
|
}
|
|
}
|
|
}
|
|
|
|
function mozout_mz_smsc_invoice_vars($invoiceId)
|
|
{
|
|
$inv = Capsule::table('tblinvoices')->where('id', (int) $invoiceId)->first();
|
|
if (!$inv) {
|
|
return null;
|
|
}
|
|
return [
|
|
'inv' => $inv,
|
|
'vars' => [
|
|
'invoice_id' => (string) ($inv->invoicenum !== '' && $inv->invoicenum !== null ? $inv->invoicenum : $inv->id),
|
|
'total' => Core::money($inv->total, $inv->userid),
|
|
'due_date' => Core::date($inv->duedate),
|
|
],
|
|
];
|
|
}
|
|
|
|
function mozout_mz_smsc_service_vars($serviceId)
|
|
{
|
|
$s = Capsule::table('tblhosting')->where('id', (int) $serviceId)->first();
|
|
if (!$s) {
|
|
return null;
|
|
}
|
|
$product = (string) Capsule::table('tblproducts')->where('id', $s->packageid)->value('name');
|
|
return ['svc' => $s, 'vars' => ['service' => $product, 'domain' => (string) $s->domain]];
|
|
}
|
|
}
|
|
|
|
/* Order paid (client) */
|
|
add_hook('OrderPaid', 1, function ($vars) {
|
|
mozout_mz_smsc_safe(function () use ($vars) {
|
|
$orderId = (int) ($vars['orderId'] ?? 0);
|
|
$userId = (int) ($vars['userId'] ?? 0);
|
|
$invoiceId = (int) ($vars['invoiceId'] ?? 0);
|
|
$order = Capsule::table('tblorders')->where('id', $orderId)->first();
|
|
if (!$order) {
|
|
return;
|
|
}
|
|
$total = $invoiceId ? Capsule::table('tblinvoices')->where('id', $invoiceId)->value('total') : $order->amount;
|
|
Core::notifyClient('order_paid', $userId ?: (int) $order->userid, [
|
|
'order_id' => (string) ($order->ordernum ?: $order->id),
|
|
'invoice_id' => (string) $invoiceId,
|
|
'total' => Core::money($total, $order->userid),
|
|
], $orderId, 'order_paid|' . $orderId);
|
|
});
|
|
});
|
|
|
|
/* Invoice created (client) */
|
|
add_hook('InvoiceCreated', 1, function ($vars) {
|
|
mozout_mz_smsc_safe(function () use ($vars) {
|
|
$x = mozout_mz_smsc_invoice_vars($vars['invoiceid'] ?? 0);
|
|
if (!$x || $x['inv']->status !== 'Unpaid' || (float) $x['inv']->total <= 0) {
|
|
return;
|
|
}
|
|
Core::notifyClient('invoice_created', (int) $x['inv']->userid, $x['vars'], (int) $x['inv']->id, 'invoice_created|' . $x['inv']->id);
|
|
});
|
|
});
|
|
|
|
/* Invoice reminder and overdue notices (client) */
|
|
add_hook('InvoicePaymentReminder', 1, function ($vars) {
|
|
mozout_mz_smsc_safe(function () use ($vars) {
|
|
$x = mozout_mz_smsc_invoice_vars($vars['invoiceid'] ?? 0);
|
|
if (!$x || $x['inv']->status !== 'Unpaid') {
|
|
return;
|
|
}
|
|
$type = (string) ($vars['type'] ?? 'reminder');
|
|
$event = $type === 'reminder' ? 'invoice_reminder' : 'invoice_overdue';
|
|
Core::notifyClient($event, (int) $x['inv']->userid, $x['vars'], (int) $x['inv']->id, $event . '|' . $x['inv']->id . '|' . $type . '|' . date('Y-m-d'));
|
|
});
|
|
});
|
|
|
|
/* Service activated, suspended, unsuspended (client) */
|
|
foreach (['AfterModuleCreate' => 'service_active', 'AfterModuleSuspend' => 'service_suspended', 'AfterModuleUnsuspend' => 'service_unsuspended'] as $hook => $event) {
|
|
add_hook($hook, 1, function ($vars) use ($event) {
|
|
mozout_mz_smsc_safe(function () use ($vars, $event) {
|
|
$p = $vars['params'] ?? [];
|
|
$sid = (int) ($p['serviceid'] ?? 0);
|
|
if ($sid <= 0) {
|
|
return;
|
|
}
|
|
$x = mozout_mz_smsc_service_vars($sid);
|
|
if (!$x) {
|
|
return;
|
|
}
|
|
Core::notifyClient($event, (int) $x['svc']->userid, $x['vars'], $sid, $event . '|' . $sid . '|' . date('Y-m-d H'));
|
|
});
|
|
});
|
|
}
|
|
|
|
/* Domain registered (client) */
|
|
add_hook('AfterRegistrarRegistration', 1, function ($vars) {
|
|
mozout_mz_smsc_safe(function () use ($vars) {
|
|
$p = $vars['params'] ?? [];
|
|
$did = (int) ($p['domainid'] ?? 0);
|
|
$d = $did ? Capsule::table('tbldomains')->where('id', $did)->first() : null;
|
|
if (!$d) {
|
|
return;
|
|
}
|
|
Core::notifyClient('domain_registered', (int) $d->userid, [
|
|
'domain' => (string) $d->domain,
|
|
'expiry_date' => Core::date($d->expirydate),
|
|
], $did, 'domain_registered|' . $did);
|
|
});
|
|
});
|
|
|
|
/* Ticket reply by staff (client) */
|
|
add_hook('TicketAdminReply', 1, function ($vars) {
|
|
mozout_mz_smsc_safe(function () use ($vars) {
|
|
$tid = (int) ($vars['ticketid'] ?? 0);
|
|
$t = Capsule::table('tbltickets')->where('id', $tid)->first();
|
|
if (!$t || (int) $t->userid <= 0) {
|
|
return;
|
|
}
|
|
Core::notifyClient('ticket_reply', (int) $t->userid, [
|
|
'ticket_id' => (string) $t->tid,
|
|
'ticket_subject' => mb_substr((string) $t->title, 0, 60),
|
|
], $tid, 'ticket_reply|' . $tid . '|' . (int) ($vars['replyid'] ?? 0));
|
|
});
|
|
});
|
|
|
|
/* New order (admin) */
|
|
add_hook('AfterShoppingCartCheckout', 1, function ($vars) {
|
|
mozout_mz_smsc_safe(function () use ($vars) {
|
|
$orderId = (int) ($vars['OrderID'] ?? 0);
|
|
$order = Capsule::table('tblorders')->where('id', $orderId)->first();
|
|
if (!$order) {
|
|
return;
|
|
}
|
|
$c = Capsule::table('tblclients')->where('id', $order->userid)->first();
|
|
Core::notifyAdmins('admin_new_order', [
|
|
'order_id' => (string) ($order->ordernum ?: $order->id),
|
|
'client_name' => $c ? trim($c->firstname . ' ' . $c->lastname . ($c->companyname ? ' (' . $c->companyname . ')' : '')) : '',
|
|
'total' => Core::money($vars['TotalDue'] ?? $order->amount, $order->userid),
|
|
], $orderId, 'admin_new_order|' . $orderId);
|
|
});
|
|
});
|
|
|
|
/* New ticket opened by a client (admin) */
|
|
add_hook('TicketOpen', 1, function ($vars) {
|
|
mozout_mz_smsc_safe(function () use ($vars) {
|
|
$tid = (int) ($vars['ticketid'] ?? 0);
|
|
$t = Capsule::table('tbltickets')->where('id', $tid)->first();
|
|
if (!$t) {
|
|
return;
|
|
}
|
|
$name = (string) $t->name;
|
|
if ((int) $t->userid > 0) {
|
|
$c = Capsule::table('tblclients')->where('id', $t->userid)->first();
|
|
if ($c) {
|
|
$name = trim($c->firstname . ' ' . $c->lastname);
|
|
}
|
|
}
|
|
Core::notifyAdmins('admin_new_ticket', [
|
|
'ticket_id' => (string) $t->tid,
|
|
'ticket_subject' => mb_substr((string) $t->title, 0, 60),
|
|
'client_name' => $name,
|
|
'department' => (string) ($vars['deptname'] ?? Capsule::table('tblticketdepartments')->where('id', $t->did)->value('name')),
|
|
], $tid, 'admin_new_ticket|' . $tid);
|
|
});
|
|
});
|
|
|
|
/* Domains about to expire (client), once a day, on the days chosen in the settings */
|
|
add_hook('DailyCronJob', 1, function ($vars) {
|
|
mozout_mz_smsc_safe(function () {
|
|
if (!Core::eventEnabled('domain_expiring')) {
|
|
return;
|
|
}
|
|
$days = array_unique(array_filter(array_map('intval', explode(',', Core::get('expiry_days', '30,7,1'))), function ($d) {
|
|
return $d > 0 && $d <= 365;
|
|
}));
|
|
foreach ($days as $n) {
|
|
$date = date('Y-m-d', strtotime('+' . $n . ' days'));
|
|
$list = Capsule::table('tbldomains')->where('status', 'Active')->where('expirydate', $date)->where('donotrenew', '!=', 1)->limit(500)->get();
|
|
foreach ($list as $d) {
|
|
Core::notifyClient('domain_expiring', (int) $d->userid, [
|
|
'domain' => (string) $d->domain,
|
|
'expiry_date' => Core::date($d->expirydate),
|
|
'days' => (string) $n,
|
|
], (int) $d->id, 'domain_expiring|' . $d->id . '|' . $d->expirydate . '|' . $n);
|
|
}
|
|
}
|
|
});
|
|
});
|