PHP game server for TerranQuest. https://terranquest.net

privmsgs.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. require 'required.php';
  3. require 'onlyloggedin.php';
  4. /*
  5. * Handle everything with private messages.
  6. * Figures out what you want based on context.
  7. */
  8. if (!is_empty($VARS['msg'])) {
  9. if (!is_empty($VARS['to'])) {
  10. $touuid = $database->select("players", ['uuid'], ['nickname' => $VARS['to']])[0]['uuid'];
  11. $database->insert("private_messages", ["from_uuid" => $_SESSION['uuid'], "to_uuid" => $touuid, "message" => $VARS['msg'], "msg_read" => 0, "#time" => "NOW()"]);
  12. sendOK();
  13. } else {
  14. sendError("Missing required arguments.", true);
  15. }
  16. } else if (!is_empty($VARS['markread'])) {
  17. if (preg_match("/[0-9]+/", $VARS['markread'])) {
  18. $database->update('private_messages', ['msg_read' => 1], [
  19. 'AND' => [
  20. "id" => $VARS['markread'],
  21. "to_uuid" => $_SESSION['uuid']
  22. ]]);
  23. sendOK();
  24. } else {
  25. sendError("Malformed input.", true);
  26. }
  27. } else if (!is_empty($VARS['delete'])) {
  28. if (preg_match("/[0-9]+/", $VARS['delete'])) {
  29. $database->delete('private_messages', [
  30. 'AND' => [
  31. "id" => $VARS['delete'],
  32. "to_uuid" => $_SESSION['uuid']
  33. ]]);
  34. sendOK();
  35. } else {
  36. sendError("Malformed input.", true);
  37. }
  38. } else {
  39. $where = [
  40. "private_messages.to_uuid" => $_SESSION['uuid']
  41. ];
  42. if ($VARS['filter'] == 'read') {
  43. $where["msg_read"] = 1;
  44. } else if ($VARS['filter'] == 'unread') {
  45. $where["msg_read"] = 0;
  46. }
  47. $out = ["status" => "OK", "msgs" => []];
  48. $out['msgs'] = $database->select('private_messages', [
  49. "[>]players" => [
  50. "from_uuid" => "uuid"
  51. ]
  52. ], [
  53. 'private_messages.id', 'private_messages.message', 'private_messages.time', 'players.nickname', 'private_messages.msg_read'
  54. ], [
  55. 'AND' => $where,
  56. "ORDER" => "private_messages.time DESC"]
  57. );
  58. echo json_encode($out);
  59. }