137 lines
5.5 KiB
PHP
137 lines
5.5 KiB
PHP
<?php
|
|
require_once 'config.php';
|
|
try {
|
|
$pdo = new PDO('sqlite:' . DB_FILE);
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
|
} catch (PDOException $e) {
|
|
die("Database connection failed: " . $e->getMessage());
|
|
}
|
|
|
|
function query($sql, $params = []) {
|
|
global $pdo;
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
return $stmt;
|
|
}
|
|
|
|
function get_user($id) {
|
|
global $pdo;
|
|
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
return $stmt->fetch();
|
|
}
|
|
|
|
function get_user_groups($user_id) {
|
|
global $pdo;
|
|
$stmt = $pdo->prepare("SELECT g.* FROM groups g JOIN user_groups ug ON g.id = ug.group_id WHERE ug.user_id = ?");
|
|
$stmt->execute([$user_id]);
|
|
return $stmt->fetchAll();
|
|
}
|
|
|
|
function compile_waha_template($template, $context) {
|
|
if (!$template) return "";
|
|
$action = $context['action'] ?? 'Update';
|
|
$module = $context['module'] ?? 'System';
|
|
$item = $context['item'] ?? '';
|
|
|
|
$user = $context['user'] ?? null;
|
|
if (!$user && isset($_SESSION['user_id'])) {
|
|
$u = get_user($_SESSION['user_id']);
|
|
$user = $u ? $u['username'] : 'System';
|
|
} elseif (!$user) {
|
|
$user = 'System';
|
|
}
|
|
|
|
$attachment_count = $context['attachment_count'] ?? '0';
|
|
$due_date = $context['due_date'] ?? 'None';
|
|
$created_date = $context['created_date'] ?? date('Y-m-d H:i');
|
|
$updated_date = $context['updated_date'] ?? date('Y-m-d H:i');
|
|
$assignor = $context['assignor'] ?? 'N/A';
|
|
$assignee = $context['assignee'] ?? 'N/A';
|
|
|
|
return str_replace(
|
|
['{action}', '{module}', '{item}', '{user}', '{attachment_count}', '{due_date}', '{created_date}', '{updated_date}', '{assignor}', '{assignee}'],
|
|
[$action, $module, $item, $user, $attachment_count, $due_date, $created_date, $updated_date, $assignor, $assignee],
|
|
$template
|
|
);
|
|
}
|
|
|
|
function dispatch_waha_message($endpoint, $chat_id, $session, $api_key, $text) {
|
|
try {
|
|
$payload = json_encode([
|
|
"chatId" => $chat_id,
|
|
"text" => $text,
|
|
"session" => $session ?: 'default'
|
|
]);
|
|
$ch = curl_init($endpoint);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
|
$headers = ['Content-Type: application/json'];
|
|
if (!empty($api_key)) $headers[] = 'X-Api-Key: ' . $api_key;
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
|
|
curl_exec($ch);
|
|
curl_close($ch);
|
|
} catch(Exception $e) {}
|
|
}
|
|
|
|
function notify_user($user_id, $message, $context = []) {
|
|
global $pdo;
|
|
$stmt = $pdo->prepare("INSERT INTO notifications (user_id, message) VALUES (?, ?)");
|
|
$stmt->execute([$user_id, $message]);
|
|
|
|
$user = get_user($user_id);
|
|
if($user && isset($user['notify_channels'])) {
|
|
$channels = explode(',', $user['notify_channels']);
|
|
if (in_array('whatsapp', $channels) && $user['waha_endpoint'] && $user['waha_chat_id']) {
|
|
$module = $context['module'] ?? '';
|
|
$template = '';
|
|
if (stripos($module, 'Note') !== false) $template = $user['waha_template_note'];
|
|
elseif (stripos($module, 'Password') !== false) $template = $user['waha_template_password'];
|
|
elseif (stripos($module, 'File') !== false || stripos($module, 'Folder') !== false) $template = $user['waha_template_file'];
|
|
else $template = $user['waha_template_task'];
|
|
|
|
if (!$template) $template = "*Alert:* {action} on {module} '{item}' by {user}";
|
|
|
|
$text = compile_waha_template($template, $context);
|
|
dispatch_waha_message($user['waha_endpoint'], $user['waha_chat_id'], $user['waha_session'], $user['waha_api_key'], $text);
|
|
}
|
|
}
|
|
}
|
|
|
|
function notify_group($group_id, $message, $context = []) {
|
|
global $pdo;
|
|
// Notify all individual group members in their UI notifications
|
|
$stmt = $pdo->prepare("SELECT user_id FROM user_groups WHERE group_id = ?");
|
|
$stmt->execute([$group_id]);
|
|
$users = $stmt->fetchAll();
|
|
foreach ($users as $u) {
|
|
$s = $pdo->prepare("INSERT INTO notifications (user_id, message) VALUES (?, ?)");
|
|
$s->execute([$u['user_id'], $message]);
|
|
}
|
|
|
|
// Check if the group itself has WAHA configured
|
|
$stmt = $pdo->prepare("SELECT * FROM groups WHERE id = ?");
|
|
$stmt->execute([$group_id]);
|
|
$group = $stmt->fetch();
|
|
|
|
if($group && isset($group['notify_channels'])) {
|
|
$channels = explode(',', $group['notify_channels']);
|
|
if (in_array('whatsapp', $channels) && $group['waha_endpoint'] && $group['waha_chat_id']) {
|
|
$module = $context['module'] ?? '';
|
|
$template = '';
|
|
if (stripos($module, 'Note') !== false) $template = $group['waha_template_note'];
|
|
elseif (stripos($module, 'Password') !== false) $template = $group['waha_template_password'];
|
|
elseif (stripos($module, 'File') !== false || stripos($module, 'Folder') !== false) $template = $group['waha_template_file'];
|
|
else $template = $group['waha_template_task'];
|
|
|
|
if (!$template) $template = "*Group Alert:* {action} on {module} '{item}' by {user}";
|
|
|
|
$text = compile_waha_template($template, $context);
|
|
dispatch_waha_message($group['waha_endpoint'], $group['waha_chat_id'], $group['waha_session'], $group['waha_api_key'], $text);
|
|
}
|
|
}
|
|
}
|