initial commit
This commit is contained in:
54
db.php
Normal file
54
db.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?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 notify_user($user_id, $message) {
|
||||
global $pdo;
|
||||
$stmt = $pdo->prepare("INSERT INTO notifications (user_id, message) VALUES (?, ?)");
|
||||
$stmt->execute([$user_id, $message]);
|
||||
|
||||
// Virtual hook for Email/Telegram/WhatsApp based on user features
|
||||
$user = get_user($user_id);
|
||||
if($user && isset($user['notify_channels'])) {
|
||||
$channels = explode(',', $user['notify_channels']);
|
||||
// Here we would actually send the message to the third party API.
|
||||
// For MVP we just assume it's sent.
|
||||
}
|
||||
}
|
||||
|
||||
function notify_group($group_id, $message) {
|
||||
global $pdo;
|
||||
$stmt = $pdo->prepare("SELECT user_id FROM user_groups WHERE group_id = ?");
|
||||
$stmt->execute([$group_id]);
|
||||
$users = $stmt->fetchAll();
|
||||
foreach ($users as $u) {
|
||||
notify_user($u['user_id'], $message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user