|
324 | 324 | header('Location: index.php'); |
325 | 325 | } |
326 | 326 |
|
327 | | -if (isset($_POST['edit_contact'])) { |
328 | | - $contact_id = intval($_POST['contact_id']); |
| 327 | +if (isset($_POST['add_contact'])) { |
329 | 328 | $contact_name = sanitizeInput($_POST['contact_name']); |
330 | 329 | $contact_email = sanitizeInput($_POST['contact_email']); |
331 | 330 | $contact_technical = intval($_POST['contact_technical']); |
332 | 331 | $contact_billing = intval($_POST['contact_billing']); |
333 | 332 | $contact_auth_method = sanitizeInput($_POST['contact_auth_method']); |
334 | 333 |
|
335 | | - mysqli_query($mysqli, "UPDATE contacts SET contact_name = '$contact_name', contact_email = '$contact_email', contact_billing = $contact_billing, contact_technical = $contact_technical WHERE contact_id = $contact_id AND contact_client_id = $session_client_id AND contact_archived_at IS NULL AND contact_primary = 0"); |
| 334 | + // Check the email isn't already in use |
| 335 | + $sql = mysqli_query($mysqli, "SELECT user_id FROM users WHERE user_email = '$contact_email'"); |
| 336 | + if ($sql && mysqli_num_rows($sql) > 0) { |
| 337 | + $_SESSION['alert_type'] = "danger"; |
| 338 | + $_SESSION['alert_message'] = "Cannot add contact as that email address is already in use"; |
| 339 | + header('Location: contact_add.php'); |
| 340 | + exit(); |
| 341 | + } |
| 342 | + |
| 343 | + // Create user account with rand password for the contact |
| 344 | + $contact_user_id = 0; |
| 345 | + if ($contact_name && $contact_email && $contact_auth_method) { |
| 346 | + |
| 347 | + $password_hash = password_hash(randomString(), PASSWORD_DEFAULT); |
| 348 | + |
| 349 | + mysqli_query($mysqli, "INSERT INTO users SET user_name = '$contact_name', user_email = '$contact_email', user_password = '$password_hash', user_auth_method = '$contact_auth_method', user_type = 2"); |
| 350 | + |
| 351 | + $contact_user_id = mysqli_insert_id($mysqli); |
| 352 | + } |
| 353 | + |
| 354 | + // Create contact record |
| 355 | + mysqli_query($mysqli, "INSERT INTO contacts SET contact_name = '$contact_name', contact_email = '$contact_email', contact_billing = $contact_billing, contact_technical = $contact_technical, contact_client_id = $session_client_id, contact_user_id = $contact_user_id"); |
| 356 | + $contact_id = mysqli_insert_id($mysqli); |
336 | 357 |
|
337 | 358 | // Logging |
338 | | - logAction("Contact", "Edit", "Client contact $session_contact_name edited contact $contact_name in the client portal", $session_client_id, $contact_id); |
| 359 | + logAction("Contact", "Create", "Client contact $session_contact_name created contact $contact_name in the client portal", $session_client_id, $contact_id); |
339 | 360 |
|
340 | | - $_SESSION['alert_message'] = "Contact <strong>$contact_name</strong> updated"; |
341 | | - |
342 | | - header('Location: contacts.php'); |
| 361 | + customAction('contact_create', $contact_id); |
343 | 362 |
|
344 | | - customAction('contact_update', $contact_id); |
| 363 | + $_SESSION['alert_message'] = "Contact $contact_name created"; |
| 364 | + |
| 365 | + header('Location: contacts.php'); |
345 | 366 | } |
346 | 367 |
|
347 | | -if (isset($_POST['add_contact'])) { |
| 368 | +if (isset($_POST['edit_contact'])) { |
| 369 | + $contact_id = intval($_POST['contact_id']); |
348 | 370 | $contact_name = sanitizeInput($_POST['contact_name']); |
349 | 371 | $contact_email = sanitizeInput($_POST['contact_email']); |
350 | 372 | $contact_technical = intval($_POST['contact_technical']); |
351 | 373 | $contact_billing = intval($_POST['contact_billing']); |
352 | 374 | $contact_auth_method = sanitizeInput($_POST['contact_auth_method']); |
353 | 375 |
|
354 | | - mysqli_query($mysqli, "INSERT INTO contacts SET contact_name = '$contact_name', contact_email = '$contact_email', contact_billing = $contact_billing, contact_technical = $contact_technical, contact_client_id = $session_client_id"); |
| 376 | + // Get the existing contact_user_id - we look it up ourselves so the user can't just overwrite random users |
| 377 | + $sql = mysqli_query($mysqli,"SELECT contact_user_id FROM contacts WHERE contact_id = $contact_id AND contact_client_id = $session_client_id"); |
| 378 | + $row = mysqli_fetch_array($sql); |
| 379 | + $contact_user_id = intval($row['contact_user_id']); |
| 380 | + |
| 381 | + // Check the email isn't already in use |
| 382 | + $sql = mysqli_query($mysqli, "SELECT user_id FROM users WHERE user_email = '$contact_email' AND user_id != $contact_user_id"); |
| 383 | + if ($sql && mysqli_num_rows($sql) > 0) { |
| 384 | + $_SESSION['alert_type'] = "danger"; |
| 385 | + $_SESSION['alert_message'] = "Cannot update contact as that email address is already in use"; |
| 386 | + header('Location: contact_edit.php?id=' . $contact_id); |
| 387 | + exit(); |
| 388 | + } |
355 | 389 |
|
356 | | - $contact_id = mysqli_insert_id($mysqli); |
| 390 | + // Update Existing User |
| 391 | + if ($contact_user_id > 0) { |
| 392 | + mysqli_query($mysqli, "UPDATE users SET user_name = '$contact_name', user_email = '$contact_email', user_auth_method = '$contact_auth_method' WHERE user_id = $contact_user_id"); |
357 | 393 |
|
358 | | - // Logging |
359 | | - logAction("Contact", "Create", "Client contact $session_contact_name created contact $contact_name in the client portal", $session_client_id, $contact_id); |
| 394 | + // Else, create New User |
| 395 | + } elseif ($contact_user_id == 0 && $contact_name && $contact_email && $contact_auth_method) { |
| 396 | + $password_hash = password_hash(randomString(), PASSWORD_DEFAULT); |
| 397 | + mysqli_query($mysqli, "INSERT INTO users SET user_name = '$contact_name', user_email = '$contact_email', user_password = '$password_hash', user_auth_method = '$contact_auth_method', user_type = 2"); |
360 | 398 |
|
361 | | - customAction('contact_create', $contact_id); |
| 399 | + $contact_user_id = mysqli_insert_id($mysqli); |
| 400 | + } |
362 | 401 |
|
363 | | - $_SESSION['alert_message'] = "Contact <strong>$contact_name</strong> created"; |
| 402 | + // Update contact |
| 403 | + mysqli_query($mysqli, "UPDATE contacts SET contact_name = '$contact_name', contact_email = '$contact_email', contact_billing = $contact_billing, contact_technical = $contact_technical, contact_user_id = $contact_user_id WHERE contact_id = $contact_id AND contact_client_id = $session_client_id AND contact_archived_at IS NULL AND contact_primary = 0"); |
364 | 404 |
|
| 405 | + // Logging |
| 406 | + logAction("Contact", "Edit", "Client contact $session_contact_name edited contact $contact_name in the client portal", $session_client_id, $contact_id); |
| 407 | + |
| 408 | + $_SESSION['alert_message'] = "Contact $contact_name updated"; |
| 409 | + |
365 | 410 | header('Location: contacts.php'); |
| 411 | + |
| 412 | + customAction('contact_update', $contact_id); |
366 | 413 | } |
0 commit comments