sms_settings.php 12 KB

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