| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
- // Αρχείο: /var/www/html/hockey-cms/admin/whatsapp_settings.php
- require_once 'admin_auth.php';
- require_once '../config/database.php';
- if (!isset($_SESSION['user_id']) or$_SESSION['role_id'] != 1) {
- header("Location: ../index.php");
- exit;
- }
- // --- Μηχανισμός Αλλαγής Γλώσσας ---
- if (isset($_GET['lang']) && in_array($_GET['lang'], ['el', 'en'])) {
- $_SESSION['lang'] =$_GET['lang'];
- }
- $current_lang =$_SESSION['lang'] ?? 'el';
- $translations = [
- 'el' => [
- 'page_title' => 'Ρυθμίσεις WhatsApp',
- 'header_title' => 'Ρυθμίσεις WhatsApp Cloud API',
- 'api_details' => 'Στοιχεία Σύνδεσης Meta for Developers',
- 'api_instructions' => 'Οδηγίες: Για να συνδέσετε το WhatsApp, δημιουργήστε λογαριασμό στο Meta for Developers, προσθέστε προϊόν WhatsApp σε μια εφαρμογή (App) και αντιγράψτε τα στοιχεία.',
- 'phone_id' => 'Phone Number ID *',
- 'access_token' => 'Temporary / Permanent Access Token *',
- 'business_id' => 'WhatsApp Business Account ID (Προαιρετικό)',
- 'btn_save' => '💾 Αποθήκευση Ρυθμίσεων',
- 'test_title' => 'Δοκιμή Μηνύματος (Test Message)',
- 'test_desc' => 'Στείλτε ένα δοκιμαστικό μήνυμα σε έναν αριθμό κινητού (π.χ. ο δικός σας αριθμός).',
- 'test_phone' => 'Αριθμός Κινητού (με κωδικό χώρας)',
- 'btn_test' => '💬 Αποστολή WhatsApp',
- 'msg_save_ok' => 'Οι ρυθμίσεις WhatsApp αποθηκεύτηκαν επιτυχώς!',
- 'msg_err_empty' => 'Παρακαλώ συμπληρώστε το τηλέφωνο παραλήπτη και τις ρυθμίσεις API παραπάνω!',
- 'msg_test_ok' => 'Το δοκιμαστικό μήνυμα WhatsApp στάλθηκε επιτυχώς στον αριθμό',
- 'msg_err_api' => 'Αποτυχία αποστολής WhatsApp.'
- ],
- 'en' => [
- 'page_title' => 'WhatsApp Settings',
- 'header_title' => 'WhatsApp Cloud API Settings',
- 'api_details' => 'Meta for Developers Credentials',
- 'api_instructions' => 'Instructions: To connect WhatsApp, create an account on Meta for Developers, add WhatsApp product to an App and copy the credentials.',
- 'phone_id' => 'Phone Number ID *',
- 'access_token' => 'Temporary / Permanent Access Token *',
- 'business_id' => 'WhatsApp Business Account ID (Optional)',
- 'btn_save' => '💾 Save Settings',
- 'test_title' => 'Test Message',
- 'test_desc' => 'Send a test message to a mobile number (e.g. your own number).',
- 'test_phone' => 'Mobile Number (with country code)',
- 'btn_test' => '💬 Send WhatsApp',
- 'msg_save_ok' => 'WhatsApp settings saved successfully!',
- 'msg_err_empty' => 'Please fill in the recipient phone and API settings above!',
- 'msg_test_ok' => 'Test WhatsApp message sent successfully to',
- 'msg_err_api' => 'WhatsApp Delivery Failed.'
- ]
- ];
- $t = $translations[$current_lang];
- header('Content-Type: text/html; charset=utf-8');
- $message = '';
- $setting_keys = ['whatsapp_phone_number_id', 'whatsapp_access_token', 'whatsapp_business_account_id'];
- // --- 1. ΑΠΟΘΗΚΕΥΣΗ ΡΥΘΜΙΣΕΩΝ ---
- if ($_SERVER['REQUEST_METHOD'] === 'POST' and isset($_POST['action']) and$_POST['action'] == 'save_settings') {
- try {
- $pdo->beginTransaction();
- foreach ($setting_keys as $key) {$value = trim($_POST[$key] ?? '');
- $check =$pdo->prepare("SELECT COUNT(*) FROM settings WHERE `key` = ?");
- $check->execute([$key]);
- if ($check->fetchColumn() > 0) {
- $stmt =$pdo->prepare("UPDATE settings SET `value` = ? WHERE `key` = ?");
- $stmt->execute([$value,$key]);
- } else {
- $stmt =$pdo->prepare("INSERT INTO settings (`key`, `value`) VALUES (?, ?)");
- $stmt->execute([$key,$value]);
- }
- }
- $pdo->commit();$message = "<div class='alert success'>{$t['msg_save_ok']}</div>";
- } catch (PDOException $e) {
- $pdo->rollBack();$message = "<div class='alert error'>Σφάλμα: " . $e->getMessage() . "</div>";
- }
- }
- // --- Ανάγνωση υφιστάμενων ρυθμίσεων ---
- $current_settings = [];
- $stmt =$pdo->query("SELECT `key`, `value` FROM settings WHERE `key` IN ('" . implode("','", $setting_keys) . "')");
- while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {$current_settings[$row['key']] =$row['value'];
- }
- // --- 2. ΑΠΟΣΤΟΛΗ ΔΟΚΙΜΑΣΤΙΚΟΥ ΜΗΝΥΜΑΤΟΣ ---
- if ($_SERVER['REQUEST_METHOD'] === 'POST' and isset($_POST['action']) and$_POST['action'] == 'test_whatsapp') {
- $test_phone = trim($_POST['test_phone']);
- $phone_number_id =$current_settings['whatsapp_phone_number_id'] ?? '';
- $access_token =$current_settings['whatsapp_access_token'] ?? '';
- if (empty($test_phone) or empty($phone_number_id) or empty($access_token)) {$message = "<div class='alert error'>{$t['msg_err_empty']}</div>";
- } else {
- $url = "https://graph.facebook.com/v17.0/{$phone_number_id}/messages";
- $data = [
- "messaging_product" => "whatsapp",
- "to" => $test_phone,
- "type" => "text",
- "text" => ["body" => "🎉 Γεια σας! Αυτό είναι ένα δοκιμαστικό μήνυμα επιβεβαίωσης από το Hockey CMS."]
- ];
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
- curl_setopt($ch, CURLOPT_HTTPHEADER, [
- "Authorization: Bearer " . $access_token,
- "Content-Type: application/json"
- ]);
- $response = curl_exec($ch);
- $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- curl_close($ch);
- if ($http_code === 200) {$message = "<div class='alert success'>{$t['msg_test_ok']} <strong>{$test_phone}</strong>!</div>";
- } else {
- $message = "<div class='alert error'>{$t['msg_err_api']} (HTTP Code: {$http_code}). <br><small>{$response}</small></div>";
- }
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="<?php echo $current_lang; ?>">
- <head>
- <meta charset="UTF-8">
- <title><?php echo $t['page_title']; ?> - Hockey CMS</title>
- <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
- <style>
- :root {
- --primary: #f97316;
- --primary-hover: #ea580c;
- --bg-color: #0b1120;
- --panel-bg: #1e293b;
- --border-color: #334155;
- --text-main: #f8fafc;
- --text-muted: #94a3b8;
- }
- body { font-family: 'Inter', sans-serif; background-color: var(--bg-color); color: var(--text-main); margin: 0; }
- .content { margin-left: 250px; padding: 30px; }
- .content h2 { margin-top: 0; color: var(--primary); font-size: 26px; text-transform: uppercase; letter-spacing: 0.5px; }
-
- .card { background: var(--panel-bg); padding: 25px; border-radius: 8px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.3); border: 1px solid var(--border-color); margin-bottom: 25px; }
- .card h3 { margin-top: 0; color: var(--text-main); border-bottom: 1px solid rgba(255,255,255,0.05); padding-bottom: 15px; font-size: 18px; text-transform: uppercase; letter-spacing: 0.5px; }
-
- .form-group { margin-bottom: 20px; }
- .form-group label { display: block; margin-bottom: 8px; font-weight: 700; font-size: 13px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px;}
- .form-group input { width: 100%; padding: 12px; background: #0f172a; border: 1px solid var(--border-color); color: var(--text-main); border-radius: 6px; box-sizing: border-box; font-family: 'Inter', sans-serif; outline: none; transition: 0.2s; }
- .form-group input:focus { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(249, 115, 22, 0.2); }
-
- .btn { padding: 12px 20px; background: var(--primary); color: white; border: none; border-radius: 6px; cursor: pointer; font-weight: 800; font-size: 14px; text-transform: uppercase; transition: 0.2s; }
- .btn:hover { background: var(--primary-hover); transform: translateY(-1px); }
- .btn-test { background: rgba(16, 185, 129, 0.1); color: #10b981; border: 1px solid rgba(16, 185, 129, 0.2); }
- .btn-test:hover { background: #10b981; color: white; }
-
- .alert { padding: 15px; border-radius: 6px; margin-bottom: 20px; font-weight: 600; font-size: 14px;}
- .success { background: rgba(16, 185, 129, 0.1); color: #10b981; border: 1px solid rgba(16, 185, 129, 0.2); }
- .error { background: rgba(239, 68, 68, 0.1); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.2); }
-
- .info-box { background: rgba(59, 130, 246, 0.1); border-left: 4px solid #3b82f6; padding: 15px; border-radius: 4px; font-size: 13px; color: #60a5fa; margin-bottom: 20px; line-height: 1.5; }
- </style>
- </head>
- <body>
-
- <?php include 'sidebar.php'; ?>
- <div class="content">
- <h2><?php echo $t['header_title']; ?></h2>
- <?php echo $message; ?>
- <div style="display: grid; grid-template-columns: 2fr 1fr; gap: 25px;">
-
- <div class="card">
- <h3><?php echo $t['api_details']; ?></h3>
-
- <div class="info-box">
- <?php echo $t['api_instructions']; ?>
- </div>
- <form method="POST" action="whatsapp_settings.php">
- <input type="hidden" name="action" value="save_settings">
-
- <div class="form-group">
- <label><?php echo $t['phone_id']; ?></label>
- <input type="text" name="whatsapp_phone_number_id" value="<?php echo htmlspecialchars($current_settings['whatsapp_phone_number_id'] ?? ''); ?>" placeholder="e.g. 1059384820..." required>
- </div>
- <div class="form-group">
- <label><?php echo $t['access_token']; ?></label>
- <input type="password" name="whatsapp_access_token" value="<?php echo htmlspecialchars($current_settings['whatsapp_access_token'] ?? ''); ?>" placeholder="EAAG..." required>
- </div>
- <div class="form-group">
- <label><?php echo $t['business_id']; ?></label>
- <input type="text" name="whatsapp_business_account_id" value="<?php echo htmlspecialchars($current_settings['whatsapp_business_account_id'] ?? ''); ?>" placeholder="e.g. 104928374...">
- </div>
-
- <button type="submit" class="btn"><?php echo $t['btn_save']; ?></button>
- </form>
- </div>
- <div class="card" style="height: fit-content;">
- <h3><?php echo $t['test_title']; ?></h3>
- <p style="font-size: 13px; color: var(--text-muted); margin-bottom: 20px;"><?php echo $t['test_desc']; ?></p>
-
- <form method="POST" action="whatsapp_settings.php">
- <input type="hidden" name="action" value="test_whatsapp">
- <div class="form-group">
- <label><?php echo $t['test_phone']; ?></label>
- <input type="text" name="test_phone" placeholder="306912345678" required>
- </div>
- <button type="submit" class="btn btn-test"><?php echo $t['btn_test']; ?></button>
- </form>
- </div>
-
- </div>
- </div>
- </body>
- </html>
|