register.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. // Αρχείο: /var/www/html/hockey-cms/register.php
  3. if (session_status() === PHP_SESSION_NONE) { session_start(); }
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. require_once __DIR__ . '/config/database.php';
  6. // Αν είναι ήδη συνδεδεμένος, πάει αρχική
  7. if (isset($_SESSION['user_id'])) {
  8. header("Location: index.php");
  9. exit;
  10. }
  11. // --- Μηχανισμός Αλλαγής Γλώσσας ---
  12. if (isset($_GET['lang']) && in_array($_GET['lang'], ['el', 'en'])) {
  13. $_SESSION['lang'] = $_GET['lang'];
  14. }
  15. $current_lang = $_SESSION['lang'] ?? 'el';
  16. // --- Εύρεση Ενεργού Theme ---
  17. $active_theme = 'default';
  18. try {
  19. $stmt = $pdo->query("SELECT * FROM settings");
  20. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  21. if (isset($row['key']) && $row['key'] === 'active_theme' && !empty($row['value'])) {
  22. $active_theme = trim($row['value']);
  23. }
  24. }
  25. } catch (Exception $e) {}
  26. $theme_dir = __DIR__ . '/themes/' . $active_theme;
  27. if (!is_dir($theme_dir)) {
  28. $active_theme = 'default';
  29. $theme_dir = __DIR__ . '/themes/default';
  30. }
  31. // --- Αρχικοποίηση Twig ---
  32. $loader = new \Twig\Loader\FilesystemLoader($theme_dir);
  33. $twig = new \Twig\Environment($loader, ['cache' => false]);
  34. $twig->addGlobal('session', $_SESSION);
  35. $twig->addGlobal('current_lang', $current_lang);
  36. // --- Φόρτωση Δυναμικών Κειμένων Site ---
  37. $siteTexts = [];
  38. try {
  39. $text_column = ($current_lang === 'en') ? 'text_value_en' : 'text_value_el';
  40. $textsStmt = $pdo->query("SELECT text_key, {$text_column} as text_value FROM site_texts");
  41. while ($row = $textsStmt->fetch(PDO::FETCH_ASSOC)) {
  42. $siteTexts[$row['text_key']] = $row['text_value'];
  43. }
  44. } catch (Exception $e) {}
  45. $twig->addGlobal('text', $siteTexts);
  46. // --- Φόρτωση Logo Site ---
  47. $site_logo = '';
  48. try {
  49. $stmtSettings = $pdo->query("SELECT * FROM settings");
  50. while ($row = $stmtSettings->fetch(PDO::FETCH_ASSOC)) {
  51. if (isset($row['key']) && $row['key'] === 'site_logo') {
  52. $site_logo = $row['value'];
  53. }
  54. }
  55. } catch (Exception $e) {}
  56. $twig->addGlobal('site_logo', $site_logo);
  57. // --- Μεταφράσεις ---
  58. $translations = [
  59. 'el' => [
  60. 'title' => 'Νέα Εγγραφή',
  61. 'fname' => 'Όνομα *',
  62. 'lname' => 'Επίθετο *',
  63. 'username' => 'Διάλεξε Username *',
  64. 'email' => 'Το Email σου *',
  65. 'phone' => 'Τηλέφωνο (Προαιρετικό)',
  66. 'password' => 'Κωδικός *',
  67. 'btn_register' => 'Δημιουργία Λογαριασμού',
  68. 'btn_google' => 'Εγγραφή με Google',
  69. 'already_have' => 'Έχετε ήδη λογαριασμό;',
  70. 'login' => 'Σύνδεση',
  71. 'err_exists' => 'Το Όνομα Χρήστη ή το Email υπάρχει ήδη!',
  72. 'err_general' => 'Σφάλμα κατά την εγγραφή. Δοκιμάστε ξανά.',
  73. 'err_req_fields' => 'Παρακαλώ συμπληρώστε όλα τα υποχρεωτικά πεδία!',
  74. 'err_captcha' => 'Λάθος αποτέλεσμα στην επαλήθευση (CAPTCHA)!',
  75. 'success' => 'Η εγγραφή ολοκληρώθηκε! Μπορείτε να συνδεθείτε.',
  76. 'captcha_label' => 'Επαλήθευση: Πόσο κάνει ',
  77. 'captcha_ph' => 'Αποτέλεσμα...'
  78. ],
  79. 'en' => [
  80. 'title' => 'Create Account',
  81. 'fname' => 'First Name *',
  82. 'lname' => 'Last Name *',
  83. 'username' => 'Choose Username *',
  84. 'email' => 'Email Address *',
  85. 'phone' => 'Phone (Optional)',
  86. 'password' => 'Password *',
  87. 'btn_register' => 'Register',
  88. 'btn_google' => 'Sign up with Google',
  89. 'already_have' => 'Already have an account?',
  90. 'login' => 'Login',
  91. 'err_exists' => 'Username or Email already exists!',
  92. 'err_general' => 'Registration error. Please try again.',
  93. 'err_req_fields' => 'Please fill in all required fields!',
  94. 'err_captcha' => 'Incorrect verification result (CAPTCHA)!',
  95. 'success' => 'Registration complete! You can now login.',
  96. 'captcha_label' => 'Verification: How much is ',
  97. 'captcha_ph' => 'Result...'
  98. ]
  99. ];
  100. $t = $translations[$current_lang];
  101. $message = '';
  102. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  103. $first_name = trim($_POST['reg_first_name'] ?? '');
  104. $last_name = trim($_POST['reg_last_name'] ?? '');
  105. $username = trim($_POST['reg_username'] ?? '');
  106. $email = trim($_POST['reg_email'] ?? '');
  107. $phone = trim($_POST['reg_phone'] ?? '');
  108. $password = $_POST['reg_password'] ?? '';
  109. $captcha_answer = isset($_POST['reg_captcha']) ? (int)$_POST['reg_captcha'] : -1;
  110. $expected_captcha = $_SESSION['captcha_result'] ?? -2;
  111. if (empty($first_name) || empty($last_name) || empty($username) || empty($email) || empty($password)) {
  112. $message = "<div style='padding: 12px; border-radius: 6px; margin-bottom: 15px; font-size: 13px; text-align: center; font-weight: 600; background: rgba(239, 68, 68, 0.1); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.2);'>" . $t['err_req_fields'] . "</div>";
  113. } elseif ($captcha_answer !== $expected_captcha) {
  114. $message = "<div style='padding: 12px; border-radius: 6px; margin-bottom: 15px; font-size: 13px; text-align: center; font-weight: 600; background: rgba(239, 68, 68, 0.1); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.2);'>" . $t['err_captcha'] . "</div>";
  115. } else {
  116. $check = $pdo->prepare("SELECT id FROM users WHERE username = ? OR email = ?");
  117. $check->execute([$username, $email]);
  118. if ($check->fetch()) {
  119. $message = "<div style='padding: 12px; border-radius: 6px; margin-bottom: 15px; font-size: 13px; text-align: center; font-weight: 600; background: rgba(239, 68, 68, 0.1); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.2);'>" . $t['err_exists'] . "</div>";
  120. } else {
  121. $hashed = password_hash($password, PASSWORD_DEFAULT);
  122. $stmt = $pdo->prepare("INSERT INTO users (first_name, last_name, username, email, phone, password, role_id, is_seller) VALUES (?, ?, ?, ?, ?, ?, 3, 0)");
  123. if ($stmt->execute([$first_name, $last_name, $username, $email, $phone, $hashed])) {
  124. $message = "<div style='padding: 12px; border-radius: 6px; margin-bottom: 15px; font-size: 13px; text-align: center; font-weight: 600; background: rgba(16, 185, 129, 0.1); color: #10b981; border: 1px solid rgba(16, 185, 129, 0.2);'>" . $t['success'] . "</div>";
  125. } else {
  126. $message = "<div style='padding: 12px; border-radius: 6px; margin-bottom: 15px; font-size: 13px; text-align: center; font-weight: 600; background: rgba(239, 68, 68, 0.1); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.2);'>" . $t['err_general'] . "</div>";
  127. }
  128. }
  129. }
  130. }
  131. // CAPTCHA
  132. $num1 = rand(1, 10);
  133. $num2 = rand(1, 10);
  134. $_SESSION['captcha_result'] = $num1 + $num2;
  135. $captcha_text = $num1 . " + " . $num2;
  136. echo $twig->render('register.html.twig', [
  137. 't' => $t,
  138. 'current_lang' => $current_lang,
  139. 'message' => $message,
  140. 'captcha_text' => $captcha_text
  141. ]);