80 lines
3.3 KiB
PHP
80 lines
3.3 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../includes/app.php';
|
|
require_permission('settings', 'edit');
|
|
|
|
$user = current_user();
|
|
if (!in_array($user['role'], ['owner', 'manager'], true)) {
|
|
set_flash('danger', tr('غير مصرح لك.', 'Unauthorized.'));
|
|
redirect_to('../index.php');
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
redirect_to('../index.php');
|
|
}
|
|
|
|
$isAjax = strtolower((string) ($_SERVER['HTTP_X_REQUESTED_WITH'] ?? '')) === 'xmlhttprequest';
|
|
$respond = static function (bool $success, string $type, string $message, ?string $redirect = null) use ($isAjax): void {
|
|
if ($isAjax) {
|
|
header('Content-Type: application/json; charset=UTF-8');
|
|
echo json_encode([
|
|
'success' => $success,
|
|
'type' => $type,
|
|
'message' => $message,
|
|
'redirect' => $redirect,
|
|
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
exit;
|
|
}
|
|
|
|
set_flash($type, $message);
|
|
header('Location: ' . ($redirect ?: '../index.php'));
|
|
exit;
|
|
};
|
|
|
|
$redirectBack = static function (): string {
|
|
$referer = $_SERVER['HTTP_REFERER'] ?? '../index.php';
|
|
$returnModal = trim((string) ($_POST['return_modal'] ?? ''));
|
|
if ($returnModal === 'wablas') {
|
|
return append_query_params($referer, ['open_modal' => 'wablas']);
|
|
}
|
|
return $referer;
|
|
};
|
|
|
|
$phoneInput = trim((string) ($_POST['wablas_test_phone'] ?? ''));
|
|
$message = trim((string) ($_POST['wablas_test_message'] ?? ''));
|
|
$token = trim((string) ($_POST['wablas_token'] ?? get_setting('wablas_token', '')));
|
|
$secretKey = trim((string) ($_POST['wablas_secret_key'] ?? get_setting('wablas_secret_key', '')));
|
|
$apiUrl = trim((string) ($_POST['wablas_api_url'] ?? get_setting('wablas_api_url', '')));
|
|
|
|
if ($phoneInput === '') {
|
|
$respond(false, 'danger', tr('أدخل رقم واتساب تجريبي صالحاً من 8 خانات.', 'Enter a valid 8-digit test WhatsApp number.'), $redirectBack());
|
|
}
|
|
|
|
$phone = normalize_oman_phone($phoneInput);
|
|
if ($phone === '') {
|
|
$respond(false, 'danger', tr('رقم الاختبار يجب أن يكون عمانياً من 8 خانات.', 'The test phone must be a valid 8-digit Oman number.'), $redirectBack());
|
|
}
|
|
|
|
if ($message === '') {
|
|
$respond(false, 'danger', tr('اكتب رسالة الاختبار أولاً.', 'Write the test message first.'), $redirectBack());
|
|
}
|
|
|
|
if (!wablas_has_credentials($token, $secretKey)) {
|
|
$respond(false, 'danger', tr('أدخل Wablas Token و Secret Key قبل إرسال الاختبار.', 'Enter the Wablas token and secret key before sending a test message.'), $redirectBack());
|
|
}
|
|
|
|
$result = wablas_send_message($phone, $message, [
|
|
'token' => $token,
|
|
'secret_key' => $secretKey,
|
|
'api_url' => $apiUrl,
|
|
'ignore_enabled' => true,
|
|
]);
|
|
|
|
if (!empty($result['success'])) {
|
|
$respond(true, 'success', tr('تم إرسال رسالة الاختبار إلى 968 ', 'Test message sent to 968 ') . $phone . '.', $redirectBack());
|
|
} else {
|
|
$status = isset($result['status']) ? (' (' . (int) $result['status'] . ')') : '';
|
|
$respond(false, 'danger', tr('فشل إرسال رسالة الاختبار.', 'Failed to send the test message.') . ' ' . (string) ($result['error'] ?? ('Wablas error' . $status)), $redirectBack());
|
|
}
|
|
|
|
$respond(false, 'danger', tr('فشل إرسال رسالة الاختبار.', 'Failed to send the test message.'), $redirectBack());
|