register.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. require_once __DIR__ . '/../config/database.php';
  3. require_once __DIR__ . '/../lib/auth.php';
  4. require_once __DIR__ . '/../lib/csrf.php';
  5. require_once __DIR__ . '/../lib/validation.php';
  6. if (isLoggedIn()) {
  7. header('Location: /dashboard/dashboard.php');
  8. exit;
  9. }
  10. $errors = [];
  11. $old = []; // repopulate form fields on error
  12. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  13. if (!verifyCsrfToken($_POST['csrf_token'] ?? '')) {
  14. $errors['general'] = 'Invalid request. Please try again.';
  15. } else {
  16. $old = $_POST;
  17. // --- Validation ---
  18. $fullname = sanitizeString($_POST['fullname'] ?? '', 255);
  19. $email = sanitizeString($_POST['email'] ?? '', 255);
  20. $company = sanitizeString($_POST['company'] ?? '', 255);
  21. $mobilephone = sanitizeString($_POST['mobilephone'] ?? '', 50);
  22. $industry = $_POST['industry'] ?? '';
  23. $role = $_POST['role'] ?? '';
  24. $city = sanitizeString($_POST['city'] ?? '', 100);
  25. $state = $_POST['state'] ?? '';
  26. $postcode = sanitizeString($_POST['postcode'] ?? '', 20);
  27. $country = $_POST['country'] ?? 'Australia';
  28. $password = $_POST['password'] ?? '';
  29. $password2 = $_POST['password_confirm'] ?? '';
  30. if ($fullname === '') $errors['fullname'] = 'Full name is required.';
  31. if ($email === '') $errors['email'] = 'Email is required.';
  32. elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors['email'] = 'Please enter a valid email.';
  33. if ($mobilephone === '') $errors['mobilephone'] = 'Mobile phone is required.';
  34. if ($password === '') $errors['password'] = 'Password is required.';
  35. elseif (strlen($password) < 8) $errors['password'] = 'Password must be at least 8 characters.';
  36. if ($password !== $password2) $errors['password_confirm'] = 'Passwords do not match.';
  37. $allowedIndustries = ['Broadacre','Viticulture','Horticulture','Permaculture','Dairy'];
  38. $allowedRoles = ['Manager','Viticulturist','Horticulturist','Permaculturist','Irrigation Manager'];
  39. $allowedStates = ['New South Wales','Victoria','Queensland','Western Australia','South Australia','Tasmania','Australian Capital Territory','Northern Territory','Other'];
  40. $allowedCountries = ['Australia','New Zealand'];
  41. if (!in_array($industry, $allowedIndustries, true)) $errors['industry'] = 'Please select an industry.';
  42. if (!in_array($role, $allowedRoles, true)) $errors['role'] = 'Please select a role.';
  43. if (!in_array($state, $allowedStates, true)) $errors['state'] = 'Please select a state.';
  44. if (!in_array($country, $allowedCountries, true)) $country = 'Australia';
  45. if (empty($errors)) {
  46. $result = registerUser([
  47. 'fullname' => $fullname,
  48. 'email' => $email,
  49. 'company' => $company,
  50. 'mobilephone' => $mobilephone,
  51. 'industry' => $industry,
  52. 'role' => $role,
  53. 'city' => $city,
  54. 'state' => $state,
  55. 'postcode' => $postcode,
  56. 'country' => $country,
  57. 'password' => $password,
  58. ]);
  59. if ($result['success']) {
  60. // Auto-login after registration
  61. loginUser($email, $password);
  62. header('Location: /dashboard/dashboard.php?registered=1');
  63. exit;
  64. } else {
  65. $errors['email'] = $result['error'];
  66. }
  67. }
  68. }
  69. }
  70. $pageTitle = 'Create an Account';
  71. include __DIR__ . '/_head.php';
  72. // Helper: old value for text inputs
  73. $v = fn(string $key) => htmlspecialchars($old[$key] ?? '', ENT_QUOTES, 'UTF-8');
  74. // Helper: re-select dropdown option
  75. $sel = fn(string $key, string $val) => (($old[$key] ?? '') === $val) ? 'selected' : '';
  76. ?>
  77. <div class="container">
  78. <div class="card o-hidden border-0 shadow-lg my-5">
  79. <div class="card-body p-0">
  80. <div class="row">
  81. <div class="col-lg-5 d-none d-lg-block bg-register-image"></div>
  82. <div class="col-lg-7">
  83. <div class="p-5">
  84. <div class="text-center mb-4">
  85. <h1 class="h4 text-gray-900">Create an Account</h1>
  86. </div>
  87. <?php if (!empty($errors['general'])): ?>
  88. <div class="alert alert-danger"><?= htmlspecialchars($errors['general'], ENT_QUOTES, 'UTF-8') ?></div>
  89. <?php endif; ?>
  90. <form method="POST" action="/login/register.php" novalidate>
  91. <input type="hidden" name="csrf_token" value="<?= generateCsrfToken() ?>">
  92. <!-- Name + Company -->
  93. <div class="row mb-3">
  94. <div class="col-md-6 mb-2 mb-md-0">
  95. <input type="text" name="fullname" class="form-control form-control-user <?= isset($errors['fullname']) ? 'is-invalid' : '' ?>"
  96. placeholder="Full Name *" value="<?= $v('fullname') ?>" required>
  97. <?php if (isset($errors['fullname'])): ?>
  98. <span class="error"><?= htmlspecialchars($errors['fullname'], ENT_QUOTES, 'UTF-8') ?></span>
  99. <?php endif; ?>
  100. </div>
  101. <div class="col-md-6">
  102. <input type="text" name="company" class="form-control form-control-user"
  103. placeholder="Company Name" value="<?= $v('company') ?>">
  104. </div>
  105. </div>
  106. <!-- Email -->
  107. <div class="mb-3">
  108. <input type="email" name="email" class="form-control form-control-user <?= isset($errors['email']) ? 'is-invalid' : '' ?>"
  109. placeholder="Email Address *" value="<?= $v('email') ?>" required>
  110. <?php if (isset($errors['email'])): ?>
  111. <span class="error"><?= htmlspecialchars($errors['email'], ENT_QUOTES, 'UTF-8') ?></span>
  112. <?php endif; ?>
  113. </div>
  114. <!-- Mobile -->
  115. <div class="mb-3">
  116. <input type="tel" name="mobilephone" class="form-control form-control-user <?= isset($errors['mobilephone']) ? 'is-invalid' : '' ?>"
  117. placeholder="Mobile Phone *" value="<?= $v('mobilephone') ?>">
  118. <?php if (isset($errors['mobilephone'])): ?>
  119. <span class="error"><?= htmlspecialchars($errors['mobilephone'], ENT_QUOTES, 'UTF-8') ?></span>
  120. <?php endif; ?>
  121. </div>
  122. <!-- Industry + Role -->
  123. <div class="row mb-3">
  124. <div class="col-md-6 mb-2 mb-md-0">
  125. <select name="industry" class="form-control form-control-user <?= isset($errors['industry']) ? 'is-invalid' : '' ?>">
  126. <option value="">Choose your industry *</option>
  127. <option value="Broadacre" <?= $sel('industry','Broadacre') ?>>Broadacre</option>
  128. <option value="Viticulture" <?= $sel('industry','Viticulture') ?>>Viticulture</option>
  129. <option value="Horticulture" <?= $sel('industry','Horticulture') ?>>Horticulture</option>
  130. <option value="Permaculture" <?= $sel('industry','Permaculture') ?>>Permaculture</option>
  131. <option value="Dairy" <?= $sel('industry','Dairy') ?>>Dairy</option>
  132. </select>
  133. <?php if (isset($errors['industry'])): ?>
  134. <span class="error"><?= htmlspecialchars($errors['industry'], ENT_QUOTES, 'UTF-8') ?></span>
  135. <?php endif; ?>
  136. </div>
  137. <div class="col-md-6">
  138. <select name="role" class="form-control form-control-user <?= isset($errors['role']) ? 'is-invalid' : '' ?>">
  139. <option value="">Choose your role *</option>
  140. <option value="Manager" <?= $sel('role','Manager') ?>>Manager</option>
  141. <option value="Viticulturist" <?= $sel('role','Viticulturist') ?>>Viticulturist</option>
  142. <option value="Horticulturist" <?= $sel('role','Horticulturist') ?>>Horticulturist</option>
  143. <option value="Permaculturist" <?= $sel('role','Permaculturist') ?>>Permaculturist</option>
  144. <option value="Irrigation Manager" <?= $sel('role','Irrigation Manager') ?>>Irrigation Manager</option>
  145. </select>
  146. <?php if (isset($errors['role'])): ?>
  147. <span class="error"><?= htmlspecialchars($errors['role'], ENT_QUOTES, 'UTF-8') ?></span>
  148. <?php endif; ?>
  149. </div>
  150. </div>
  151. <!-- City + State -->
  152. <div class="row mb-3">
  153. <div class="col-md-6 mb-2 mb-md-0">
  154. <input type="text" name="city" class="form-control form-control-user"
  155. placeholder="City" value="<?= $v('city') ?>">
  156. </div>
  157. <div class="col-md-6">
  158. <select name="state" class="form-control form-control-user <?= isset($errors['state']) ? 'is-invalid' : '' ?>">
  159. <option value="">State *</option>
  160. <option value="New South Wales" <?= $sel('state','New South Wales') ?>>New South Wales</option>
  161. <option value="Victoria" <?= $sel('state','Victoria') ?>>Victoria</option>
  162. <option value="Queensland" <?= $sel('state','Queensland') ?>>Queensland</option>
  163. <option value="Western Australia" <?= $sel('state','Western Australia') ?>>Western Australia</option>
  164. <option value="South Australia" <?= $sel('state','South Australia') ?>>South Australia</option>
  165. <option value="Tasmania" <?= $sel('state','Tasmania') ?>>Tasmania</option>
  166. <option value="Australian Capital Territory" <?= $sel('state','Australian Capital Territory') ?>>ACT</option>
  167. <option value="Northern Territory" <?= $sel('state','Northern Territory') ?>>Northern Territory</option>
  168. <option value="Other" <?= $sel('state','Other') ?>>Other</option>
  169. </select>
  170. <?php if (isset($errors['state'])): ?>
  171. <span class="error"><?= htmlspecialchars($errors['state'], ENT_QUOTES, 'UTF-8') ?></span>
  172. <?php endif; ?>
  173. </div>
  174. </div>
  175. <!-- Postcode + Country -->
  176. <div class="row mb-3">
  177. <div class="col-md-6 mb-2 mb-md-0">
  178. <input type="text" name="postcode" class="form-control form-control-user"
  179. placeholder="Post Code" value="<?= $v('postcode') ?>">
  180. </div>
  181. <div class="col-md-6">
  182. <select name="country" class="form-control form-control-user">
  183. <option value="Australia" <?= $sel('country','Australia') ?>>Australia</option>
  184. <option value="New Zealand" <?= $sel('country','New Zealand') ?>>New Zealand</option>
  185. </select>
  186. </div>
  187. </div>
  188. <!-- Password -->
  189. <div class="row mb-3">
  190. <div class="col-md-6 mb-2 mb-md-0">
  191. <input type="password" name="password" class="form-control form-control-user <?= isset($errors['password']) ? 'is-invalid' : '' ?>"
  192. placeholder="Password * (min 8 chars)" required>
  193. <?php if (isset($errors['password'])): ?>
  194. <span class="error"><?= htmlspecialchars($errors['password'], ENT_QUOTES, 'UTF-8') ?></span>
  195. <?php endif; ?>
  196. </div>
  197. <div class="col-md-6">
  198. <input type="password" name="password_confirm" class="form-control form-control-user <?= isset($errors['password_confirm']) ? 'is-invalid' : '' ?>"
  199. placeholder="Repeat Password *" required>
  200. <?php if (isset($errors['password_confirm'])): ?>
  201. <span class="error"><?= htmlspecialchars($errors['password_confirm'], ENT_QUOTES, 'UTF-8') ?></span>
  202. <?php endif; ?>
  203. </div>
  204. </div>
  205. <button type="submit" class="btn btn-success btn-user btn-block w-100 mb-3">
  206. Register Account
  207. </button>
  208. </form>
  209. <hr>
  210. <div class="text-center">
  211. <a class="small" href="/login/forgot-password.php">Forgot Password?</a>
  212. </div>
  213. <div class="text-center">
  214. <a class="small" href="/login/login.php">Already have an account? Login!</a>
  215. </div>
  216. </div>
  217. </div>
  218. </div>
  219. </div>
  220. </div>
  221. </div>
  222. <?php include __DIR__ . '/_foot.php'; ?>