60 lines
2.4 KiB
PHP
60 lines
2.4 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');
|
|
}
|
|
|
|
$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', '')));
|
|
|
|
if ($phoneInput === '') {
|
|
set_flash('danger', tr('أدخل رقم واتساب تجريبي صالحاً من 8 خانات.', 'Enter a valid 8-digit test WhatsApp number.'));
|
|
header('Location: ' . ($_SERVER['HTTP_REFERER'] ?? '../index.php'));
|
|
exit;
|
|
}
|
|
|
|
$phone = normalize_oman_phone($phoneInput);
|
|
if ($phone === '') {
|
|
set_flash('danger', tr('رقم الاختبار يجب أن يكون عمانياً من 8 خانات.', 'The test phone must be a valid 8-digit Oman number.'));
|
|
header('Location: ' . ($_SERVER['HTTP_REFERER'] ?? '../index.php'));
|
|
exit;
|
|
}
|
|
|
|
if ($message === '') {
|
|
set_flash('danger', tr('اكتب رسالة الاختبار أولاً.', 'Write the test message first.'));
|
|
header('Location: ' . ($_SERVER['HTTP_REFERER'] ?? '../index.php'));
|
|
exit;
|
|
}
|
|
|
|
if (!wablas_has_credentials($token, $secretKey)) {
|
|
set_flash('danger', tr('أدخل Wablas Token و Secret Key قبل إرسال الاختبار.', 'Enter the Wablas token and secret key before sending a test message.'));
|
|
header('Location: ' . ($_SERVER['HTTP_REFERER'] ?? '../index.php'));
|
|
exit;
|
|
}
|
|
|
|
$result = wablas_send_message($phone, $message, [
|
|
'token' => $token,
|
|
'secret_key' => $secretKey,
|
|
'ignore_enabled' => true,
|
|
]);
|
|
|
|
if (!empty($result['success'])) {
|
|
set_flash('success', tr('تم إرسال رسالة الاختبار إلى 968 ', 'Test message sent to 968 ') . $phone . '.');
|
|
} else {
|
|
$status = isset($result['status']) ? (' (' . (int) $result['status'] . ')') : '';
|
|
set_flash('danger', tr('فشل إرسال رسالة الاختبار.', 'Failed to send the test message.') . ' ' . (string) ($result['error'] ?? ('Wablas error' . $status)));
|
|
}
|
|
|
|
header('Location: ' . ($_SERVER['HTTP_REFERER'] ?? '../index.php'));
|
|
exit;
|