google_auth.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. // Αρχείο: /var/www/html/hockey-cms/google_auth.php
  3. session_start();
  4. require_once __DIR__ . '/config/database.php';
  5. require_once __DIR__ . '/vendor/autoload.php';
  6. // ============================================================
  7. // GOOGLE OAUTH SETTINGS
  8. // ============================================================
  9. $clientID = '1062187052171-is354osfmism8chdok6bqe25i0iccmdt.apps.googleusercontent.com';
  10. // ΒΑΛΕ ΕΔΩ ΤΟ ΝΕΟ CLIENT SECRET ΠΟΥ ΘΑ ΚΑΝΕΙΣ ROTATE
  11. $clientSecret = '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';
  12. $redirectUri = 'https://hockeycms.eparmak.eu/google_auth.php';
  13. // ============================================================
  14. // GOOGLE CLIENT
  15. // ============================================================
  16. $client = new Google_Client();
  17. $client->setClientId($clientID);
  18. $client->setClientSecret($clientSecret);
  19. $client->setRedirectUri($redirectUri);
  20. // OAuth scopes
  21. $client->addScope('openid');
  22. $client->addScope('email');
  23. $client->addScope('profile');
  24. // Ζητάμε offline access όπου χρειάζεται
  25. $client->setAccessType('online');
  26. // Για να παίρνουμε πάντα consent όταν χρειάζεται
  27. $client->setPrompt('select_account');
  28. // ============================================================
  29. // GOOGLE CALLBACK
  30. // ============================================================
  31. if (isset($_GET['code'])) {
  32. // --------------------------------------------------------
  33. // Έλεγχος OAuth state
  34. // --------------------------------------------------------
  35. if (
  36. !isset($_GET['state']) ||
  37. !isset($_SESSION['google_oauth_state']) ||
  38. !hash_equals($_SESSION['google_oauth_state'], $_GET['state'])
  39. ) {
  40. die('Μη έγκυρο OAuth state.');
  41. }
  42. unset($_SESSION['google_oauth_state']);
  43. // --------------------------------------------------------
  44. // Ανταλλαγή authorization code με access token
  45. // --------------------------------------------------------
  46. try {
  47. $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
  48. } catch (Exception $e) {
  49. die(
  50. 'Σφάλμα κατά τη σύνδεση με Google: ' .
  51. htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8')
  52. );
  53. }
  54. if (isset($token['error'])) {
  55. $errorDescription = $token['error_description'] ?? $token['error'];
  56. die(
  57. 'Google OAuth Error: ' .
  58. htmlspecialchars($errorDescription, ENT_QUOTES, 'UTF-8')
  59. );
  60. }
  61. if (!isset($token['access_token'])) {
  62. die('Δεν ελήφθη access token από τη Google.');
  63. }
  64. $client->setAccessToken($token['access_token']);
  65. // --------------------------------------------------------
  66. // Λήψη στοιχείων χρήστη από Google
  67. // --------------------------------------------------------
  68. try {
  69. $google_oauth = new Google_Service_Oauth2($client);
  70. $google_account_info = $google_oauth->userinfo->get();
  71. } catch (Exception $e) {
  72. die(
  73. 'Αδυναμία λήψης στοιχείων από Google: ' .
  74. htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8')
  75. );
  76. }
  77. // --------------------------------------------------------
  78. // User information
  79. // --------------------------------------------------------
  80. $email = trim((string) $google_account_info->email);
  81. $first_name = trim((string) ($google_account_info->givenName ?? ''));
  82. $last_name = trim((string) ($google_account_info->familyName ?? ''));
  83. if ($email === '') {
  84. die('Η Google δεν επέστρεψε email για τον χρήστη.');
  85. }
  86. // ========================================================
  87. // SEARCH USER IN DATABASE
  88. // ========================================================
  89. try {
  90. $stmt = $pdo->prepare(
  91. "SELECT *
  92. FROM users
  93. WHERE username = ? OR email = ?
  94. LIMIT 1"
  95. );
  96. $stmt->execute([$email, $email]);
  97. $user = $stmt->fetch(PDO::FETCH_ASSOC);
  98. } catch (PDOException $e) {
  99. die(
  100. 'Σφάλμα βάσης δεδομένων: ' .
  101. htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8')
  102. );
  103. }
  104. // ========================================================
  105. // EXISTING USER
  106. // ========================================================
  107. if ($user) {
  108. // Προαιρετικά ενημερώνουμε όνομα/επώνυμο
  109. // αν υπάρχουν διαθέσιμα από Google.
  110. if (empty($user['first_name']) && $first_name !== '') {
  111. $update = $pdo->prepare(
  112. "UPDATE users
  113. SET first_name = ?
  114. WHERE id = ?"
  115. );
  116. $update->execute([$first_name, $user['id']]);
  117. $user['first_name'] = $first_name;
  118. }
  119. if (empty($user['last_name']) && $last_name !== '') {
  120. $update = $pdo->prepare(
  121. "UPDATE users
  122. SET last_name = ?
  123. WHERE id = ?"
  124. );
  125. $update->execute([$last_name, $user['id']]);
  126. $user['last_name'] = $last_name;
  127. }
  128. // ----------------------------------------------------
  129. // Login
  130. // ----------------------------------------------------
  131. session_regenerate_id(true);
  132. $_SESSION['user_id'] = $user['id'];
  133. $_SESSION['username'] = $user['username'];
  134. $_SESSION['role_id'] = $user['role_id'];
  135. $_SESSION['first_name'] = $user['first_name'] ?? '';
  136. $_SESSION['is_seller'] = $user['is_seller'] ?? 0;
  137. }
  138. // ========================================================
  139. // NEW USER
  140. // ========================================================
  141. else {
  142. // Τυχαίος password επειδή ο χρήστης θα κάνει login μέσω Google.
  143. $random_password = password_hash(
  144. bin2hex(random_bytes(32)),
  145. PASSWORD_DEFAULT
  146. );
  147. // 3 = Απλός χρήστης / Πελάτης
  148. // 2 = Seller
  149. // 1 = Admin
  150. $role_id = 3;
  151. try {
  152. $insert = $pdo->prepare(
  153. "INSERT INTO users
  154. (
  155. username,
  156. email,
  157. password,
  158. first_name,
  159. last_name,
  160. role_id
  161. )
  162. VALUES (?, ?, ?, ?, ?, ?)"
  163. );
  164. $insert->execute([
  165. $email,
  166. $email,
  167. $random_password,
  168. $first_name,
  169. $last_name,
  170. $role_id
  171. ]);
  172. $new_user_id = $pdo->lastInsertId();
  173. } catch (PDOException $e) {
  174. die(
  175. 'Σφάλμα κατά την αυτόματη εγγραφή του χρήστη: ' .
  176. htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8')
  177. );
  178. }
  179. // ----------------------------------------------------
  180. // Login νέου χρήστη
  181. // ----------------------------------------------------
  182. session_regenerate_id(true);
  183. $_SESSION['user_id'] = $new_user_id;
  184. $_SESSION['username'] = $email;
  185. $_SESSION['role_id'] = $role_id;
  186. $_SESSION['first_name'] = $first_name;
  187. $_SESSION['is_seller'] = 0;
  188. }
  189. // ========================================================
  190. // SUCCESS
  191. // ========================================================
  192. header('Location: index.php');
  193. exit;
  194. }
  195. // ============================================================
  196. // INITIAL REQUEST - REDIRECT TO GOOGLE
  197. // ============================================================
  198. else {
  199. // Δημιουργία τυχαίου state για προστασία από CSRF
  200. $state = bin2hex(random_bytes(32));
  201. $_SESSION['google_oauth_state'] = $state;
  202. $client->setState($state);
  203. // Δημιουργία Google authorization URL
  204. $authUrl = $client->createAuthUrl();
  205. header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
  206. exit;
  207. }