/** * Log activity for audit trail */ function logActivity($activity_type, $description, $reference_id = null, $user_id = null) { require_once 'config/database.php'; try { $pdo = getDBConnection(); if ($pdo) { $stmt = $pdo->prepare(" INSERT INTO activity_logs (activity_type, description, user_id, reference_id, ip_address, user_agent) VALUES (?, ?, ?, ?, ?, ?) "); $user_id = $user_id ?: ($_SESSION['user_id'] ?? null); $ip_address = $_SERVER['REMOTE_ADDR'] ?? null; $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? null; return $stmt->execute([ $activity_type, $description, $user_id, $reference_id, $ip_address, $user_agent ]); } } catch (Exception $e) { error_log("Error logging activity: " . $e->getMessage()); } return false; } /** * Send SMS using SmartSMS API */ function sendSMS($phone, $message, $sender_id = null) { $api_key = getSiteSetting('smartsms_api_key'); $default_sender = getSiteSetting('smartsms_sender_id', 'BCBC'); if (empty($api_key)) { return ['success' => false, 'message' => 'SMS API not configured']; } $sender_id = $sender_id ?: $default_sender; $url = 'https://smartsmssolutions.com/api/json.php'; $data = [ 'username' => $api_key, 'password' => '', // SmartSMS uses API key as username 'message' => $message, 'sender' => $sender_id, 'mobiles' => $phone ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($http_code == 200) { $result = json_decode($response, true); return [ 'success' => true, 'message' => 'SMS sent successfully', 'response' => $result ]; } else { return [ 'success' => false, 'message' => 'Failed to send SMS', 'response' => $response ]; } } /** * Send bulk SMS to multiple recipients */ function sendBulkSMS($recipients, $message, $sender_id = null) { $results = []; foreach ($recipients as $phone) { $result = sendSMS($phone, $message, $sender_id); $results[] = [ 'phone' => $phone, 'success' => $result['success'], 'message' => $result['message'] ]; // Add small delay to avoid rate limiting usleep(100000); // 0.1 second delay } return $results; } /** * Get members by department */ function getMembersByDepartment($department = null) { require_once 'config/database.php'; try { $pdo = getDBConnection(); if ($pdo) { if ($department) { $stmt = $pdo->prepare("SELECT * FROM members WHERE department = ? AND status = 'active' ORDER BY first_name, last_name"); $stmt->execute([$department]); } else { $stmt = $pdo->prepare("SELECT * FROM members WHERE status = 'active' ORDER BY first_name, last_name"); $stmt->execute(); } return $stmt->fetchAll(); } } catch (Exception $e) { error_log("Error getting members: " . $e->getMessage()); } return []; } /** * Process Paystack payment */ function processPaystackPayment($amount, $email, $reference = null) { $public_key = getSiteSetting('paystack_public_key'); $secret_key = getSiteSetting('paystack_secret_key'); if (empty($secret_key)) { return ['success' => false, 'message' => 'Paystack not configured']; } $reference = $reference ?: 'BCBC_' . time() . '_' . rand(1000, 9999); $url = 'https://api.paystack.co/transaction/initialize'; $data = [ 'amount' => $amount * 100, // Convert to kobo 'email' => $email, 'reference' => $reference, 'callback_url' => getSiteSetting('site_url', 'http://localhost') . '/payment-callback.php' ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Authorization: Bearer ' . $secret_key, 'Content-Type: application/json' ]); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($http_code == 200) { $result = json_decode($response, true); if ($result['status']) { return [ 'success' => true, 'authorization_url' => $result['data']['authorization_url'], 'reference' => $result['data']['reference'] ]; } } return ['success' => false, 'message' => 'Payment initialization failed']; } /** * Verify Paystack payment */ function verifyPaystackPayment($reference) { $secret_key = getSiteSetting('paystack_secret_key'); if (empty($secret_key)) { return ['success' => false, 'message' => 'Paystack not configured']; } $url = 'https://api.paystack.co/transaction/verify/' . $reference; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Authorization: Bearer ' . $secret_key ]); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($http_code == 200) { $result = json_decode($response, true); if ($result['status'] && $result['data']['status'] === 'success') { return [ 'success' => true, 'amount' => $result['data']['amount'] / 100, // Convert from kobo 'customer' => $result['data']['customer'], 'paid_at' => $result['data']['paid_at'] ]; } } return ['success' => false, 'message' => 'Payment verification failed']; } /** * Send welcome email to new members */ function sendWelcomeEmail($email, $name) { $subject = 'Welcome to Better City Bible Church!'; $message = "
We are thrilled to have you join our church family. Your registration has been successfully completed.
Our church is located at:
Behind New Model Int'l College
Opposite Ologede Police Station
Off Ikere Road, Ado-Ekiti
If you have any questions, please don't hesitate to contact us.
Blessings,
Rev. Dr. & Lady Evang. Olusegun Akinloose
Lead Pastor & General Overseer
Better City Bible Church
We have received your volunteer application and are excited about your desire to serve with us at Better City Bible Church.
Our volunteer coordinator will review your application and contact you within the next few days to discuss next steps and available opportunities.
Thank you for your heart to serve God and our community!
Blessings,
Better City Bible Church
Volunteer Ministry Team
We have received your testimony titled \"{$title}\" and are blessed by how God has worked in your life.
Our team will review your testimony, and if you gave permission for publication, it may be featured on our website or in our church communications to encourage others.
Thank you for sharing how God has moved in your life!
Blessings,
Better City Bible Church
Ministry Team
We have received your request for a counseling appointment.
Requested Date: " . formatDate($date) . "
Requested Time: {$time}
Our pastoral team will review your request and contact you within 24-48 hours to confirm the appointment details.
We are here to support you and look forward to meeting with you.
Blessings,
Rev. Dr. & Lady Evang. Olusegun Akinloose
Better City Bible Church
A new urgent counseling appointment request has been submitted:
Name: {$name}
Type: {$type}
Appointment ID: {$appointment_id}
Please review and respond to this request as soon as possible.
"; return sendEmail($admin_email, $subject, $message); } /** * Generate random password */ function generateRandomPassword($length = 12) { $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*'; $password = ''; for ($i = 0; $i < $length; $i++) { $password .= $characters[rand(0, strlen($characters) - 1)]; } return $password; } /** * Export data to CSV */ function exportToCSV($data, $filename, $headers = []) { header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="' . $filename . '"'); $output = fopen('php://output', 'w'); if (!empty($headers)) { fputcsv($output, $headers); } elseif (!empty($data)) { fputcsv($output, array_keys($data[0])); } foreach ($data as $row) { fputcsv($output, $row); } fclose($output); exit(); } /** * Import CSV data */ function importFromCSV($file_path, $skip_header = true) { $data = []; if (($handle = fopen($file_path, 'r')) !== FALSE) { $row_count = 0; while (($row = fgetcsv($handle, 1000, ',')) !== FALSE) { if ($skip_header && $row_count == 0) { $row_count++; continue; } $data[] = $row; $row_count++; } fclose($handle); } return $data; }