whatsapp_settings.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. // Αρχείο: /var/www/html/hockey-cms/admin/whatsapp_settings.php
  3. require_once 'admin_auth.php';
  4. require_once '../config/database.php';
  5. if (!isset($_SESSION['user_id']) or$_SESSION['role_id'] != 1) {
  6. header("Location: ../index.php");
  7. exit;
  8. }
  9. // --- Μηχανισμός Αλλαγής Γλώσσας ---
  10. if (isset($_GET['lang']) && in_array($_GET['lang'], ['el', 'en'])) {
  11. $_SESSION['lang'] =$_GET['lang'];
  12. }
  13. $current_lang =$_SESSION['lang'] ?? 'el';
  14. $translations = [
  15. 'el' => [
  16. 'page_title' => 'Ρυθμίσεις WhatsApp',
  17. 'header_title' => 'Ρυθμίσεις WhatsApp Cloud API',
  18. 'api_details' => 'Στοιχεία Σύνδεσης Meta for Developers',
  19. 'api_instructions' => 'Οδηγίες: Για να συνδέσετε το WhatsApp, δημιουργήστε λογαριασμό στο Meta for Developers, προσθέστε προϊόν WhatsApp σε μια εφαρμογή (App) και αντιγράψτε τα στοιχεία.',
  20. 'phone_id' => 'Phone Number ID *',
  21. 'access_token' => 'Temporary / Permanent Access Token *',
  22. 'business_id' => 'WhatsApp Business Account ID (Προαιρετικό)',
  23. 'btn_save' => '💾 Αποθήκευση Ρυθμίσεων',
  24. 'test_title' => 'Δοκιμή Μηνύματος (Test Message)',
  25. 'test_desc' => 'Στείλτε ένα δοκιμαστικό μήνυμα σε έναν αριθμό κινητού (π.χ. ο δικός σας αριθμός).',
  26. 'test_phone' => 'Αριθμός Κινητού (με κωδικό χώρας)',
  27. 'btn_test' => '💬 Αποστολή WhatsApp',
  28. 'msg_save_ok' => 'Οι ρυθμίσεις WhatsApp αποθηκεύτηκαν επιτυχώς!',
  29. 'msg_err_empty' => 'Παρακαλώ συμπληρώστε το τηλέφωνο παραλήπτη και τις ρυθμίσεις API παραπάνω!',
  30. 'msg_test_ok' => 'Το δοκιμαστικό μήνυμα WhatsApp στάλθηκε επιτυχώς στον αριθμό',
  31. 'msg_err_api' => 'Αποτυχία αποστολής WhatsApp.'
  32. ],
  33. 'en' => [
  34. 'page_title' => 'WhatsApp Settings',
  35. 'header_title' => 'WhatsApp Cloud API Settings',
  36. 'api_details' => 'Meta for Developers Credentials',
  37. 'api_instructions' => 'Instructions: To connect WhatsApp, create an account on Meta for Developers, add WhatsApp product to an App and copy the credentials.',
  38. 'phone_id' => 'Phone Number ID *',
  39. 'access_token' => 'Temporary / Permanent Access Token *',
  40. 'business_id' => 'WhatsApp Business Account ID (Optional)',
  41. 'btn_save' => '💾 Save Settings',
  42. 'test_title' => 'Test Message',
  43. 'test_desc' => 'Send a test message to a mobile number (e.g. your own number).',
  44. 'test_phone' => 'Mobile Number (with country code)',
  45. 'btn_test' => '💬 Send WhatsApp',
  46. 'msg_save_ok' => 'WhatsApp settings saved successfully!',
  47. 'msg_err_empty' => 'Please fill in the recipient phone and API settings above!',
  48. 'msg_test_ok' => 'Test WhatsApp message sent successfully to',
  49. 'msg_err_api' => 'WhatsApp Delivery Failed.'
  50. ]
  51. ];
  52. $t = $translations[$current_lang];
  53. header('Content-Type: text/html; charset=utf-8');
  54. $message = '';
  55. $setting_keys = ['whatsapp_phone_number_id', 'whatsapp_access_token', 'whatsapp_business_account_id'];
  56. // --- 1. ΑΠΟΘΗΚΕΥΣΗ ΡΥΘΜΙΣΕΩΝ ---
  57. if ($_SERVER['REQUEST_METHOD'] === 'POST' and isset($_POST['action']) and$_POST['action'] == 'save_settings') {
  58. try {
  59. $pdo->beginTransaction();
  60. foreach ($setting_keys as $key) {$value = trim($_POST[$key] ?? '');
  61. $check =$pdo->prepare("SELECT COUNT(*) FROM settings WHERE `key` = ?");
  62. $check->execute([$key]);
  63. if ($check->fetchColumn() > 0) {
  64. $stmt =$pdo->prepare("UPDATE settings SET `value` = ? WHERE `key` = ?");
  65. $stmt->execute([$value,$key]);
  66. } else {
  67. $stmt =$pdo->prepare("INSERT INTO settings (`key`, `value`) VALUES (?, ?)");
  68. $stmt->execute([$key,$value]);
  69. }
  70. }
  71. $pdo->commit();$message = "<div class='alert success'>{$t['msg_save_ok']}</div>";
  72. } catch (PDOException $e) {
  73. $pdo->rollBack();$message = "<div class='alert error'>Σφάλμα: " . $e->getMessage() . "</div>";
  74. }
  75. }
  76. // --- Ανάγνωση υφιστάμενων ρυθμίσεων ---
  77. $current_settings = [];
  78. $stmt =$pdo->query("SELECT `key`, `value` FROM settings WHERE `key` IN ('" . implode("','", $setting_keys) . "')");
  79. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {$current_settings[$row['key']] =$row['value'];
  80. }
  81. // --- 2. ΑΠΟΣΤΟΛΗ ΔΟΚΙΜΑΣΤΙΚΟΥ ΜΗΝΥΜΑΤΟΣ ---
  82. if ($_SERVER['REQUEST_METHOD'] === 'POST' and isset($_POST['action']) and$_POST['action'] == 'test_whatsapp') {
  83. $test_phone = trim($_POST['test_phone']);
  84. $phone_number_id =$current_settings['whatsapp_phone_number_id'] ?? '';
  85. $access_token =$current_settings['whatsapp_access_token'] ?? '';
  86. if (empty($test_phone) or empty($phone_number_id) or empty($access_token)) {$message = "<div class='alert error'>{$t['msg_err_empty']}</div>";
  87. } else {
  88. $url = "https://graph.facebook.com/v17.0/{$phone_number_id}/messages";
  89. $data = [
  90. "messaging_product" => "whatsapp",
  91. "to" => $test_phone,
  92. "type" => "text",
  93. "text" => ["body" => "🎉 Γεια σας! Αυτό είναι ένα δοκιμαστικό μήνυμα επιβεβαίωσης από το Hockey CMS."]
  94. ];
  95. $ch = curl_init($url);
  96. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  97. curl_setopt($ch, CURLOPT_POST, true);
  98. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  99. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  100. "Authorization: Bearer " . $access_token,
  101. "Content-Type: application/json"
  102. ]);
  103. $response = curl_exec($ch);
  104. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  105. curl_close($ch);
  106. if ($http_code === 200) {$message = "<div class='alert success'>{$t['msg_test_ok']} <strong>{$test_phone}</strong>!</div>";
  107. } else {
  108. $message = "<div class='alert error'>{$t['msg_err_api']} (HTTP Code: {$http_code}). <br><small>{$response}</small></div>";
  109. }
  110. }
  111. }
  112. ?>
  113. <!DOCTYPE html>
  114. <html lang="<?php echo $current_lang; ?>">
  115. <head>
  116. <meta charset="UTF-8">
  117. <title><?php echo $t['page_title']; ?> - Hockey CMS</title>
  118. <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
  119. <style>
  120. :root {
  121. --primary: #f97316;
  122. --primary-hover: #ea580c;
  123. --bg-color: #0b1120;
  124. --panel-bg: #1e293b;
  125. --border-color: #334155;
  126. --text-main: #f8fafc;
  127. --text-muted: #94a3b8;
  128. }
  129. body { font-family: 'Inter', sans-serif; background-color: var(--bg-color); color: var(--text-main); margin: 0; }
  130. .content { margin-left: 250px; padding: 30px; }
  131. .content h2 { margin-top: 0; color: var(--primary); font-size: 26px; text-transform: uppercase; letter-spacing: 0.5px; }
  132. .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; }
  133. .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; }
  134. .form-group { margin-bottom: 20px; }
  135. .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;}
  136. .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; }
  137. .form-group input:focus { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(249, 115, 22, 0.2); }
  138. .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; }
  139. .btn:hover { background: var(--primary-hover); transform: translateY(-1px); }
  140. .btn-test { background: rgba(16, 185, 129, 0.1); color: #10b981; border: 1px solid rgba(16, 185, 129, 0.2); }
  141. .btn-test:hover { background: #10b981; color: white; }
  142. .alert { padding: 15px; border-radius: 6px; margin-bottom: 20px; font-weight: 600; font-size: 14px;}
  143. .success { background: rgba(16, 185, 129, 0.1); color: #10b981; border: 1px solid rgba(16, 185, 129, 0.2); }
  144. .error { background: rgba(239, 68, 68, 0.1); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.2); }
  145. .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; }
  146. </style>
  147. </head>
  148. <body>
  149. <?php include 'sidebar.php'; ?>
  150. <div class="content">
  151. <h2><?php echo $t['header_title']; ?></h2>
  152. <?php echo $message; ?>
  153. <div style="display: grid; grid-template-columns: 2fr 1fr; gap: 25px;">
  154. <div class="card">
  155. <h3><?php echo $t['api_details']; ?></h3>
  156. <div class="info-box">
  157. <?php echo $t['api_instructions']; ?>
  158. </div>
  159. <form method="POST" action="whatsapp_settings.php">
  160. <input type="hidden" name="action" value="save_settings">
  161. <div class="form-group">
  162. <label><?php echo $t['phone_id']; ?></label>
  163. <input type="text" name="whatsapp_phone_number_id" value="<?php echo htmlspecialchars($current_settings['whatsapp_phone_number_id'] ?? ''); ?>" placeholder="e.g. 1059384820..." required>
  164. </div>
  165. <div class="form-group">
  166. <label><?php echo $t['access_token']; ?></label>
  167. <input type="password" name="whatsapp_access_token" value="<?php echo htmlspecialchars($current_settings['whatsapp_access_token'] ?? ''); ?>" placeholder="EAAG..." required>
  168. </div>
  169. <div class="form-group">
  170. <label><?php echo $t['business_id']; ?></label>
  171. <input type="text" name="whatsapp_business_account_id" value="<?php echo htmlspecialchars($current_settings['whatsapp_business_account_id'] ?? ''); ?>" placeholder="e.g. 104928374...">
  172. </div>
  173. <button type="submit" class="btn"><?php echo $t['btn_save']; ?></button>
  174. </form>
  175. </div>
  176. <div class="card" style="height: fit-content;">
  177. <h3><?php echo $t['test_title']; ?></h3>
  178. <p style="font-size: 13px; color: var(--text-muted); margin-bottom: 20px;"><?php echo $t['test_desc']; ?></p>
  179. <form method="POST" action="whatsapp_settings.php">
  180. <input type="hidden" name="action" value="test_whatsapp">
  181. <div class="form-group">
  182. <label><?php echo $t['test_phone']; ?></label>
  183. <input type="text" name="test_phone" placeholder="306912345678" required>
  184. </div>
  185. <button type="submit" class="btn btn-test"><?php echo $t['btn_test']; ?></button>
  186. </form>
  187. </div>
  188. </div>
  189. </div>
  190. </body>
  191. </html>