messages_send.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * ElggPG -- Messaging library
  4. *
  5. * Override from mod/messages/start.php:messages_send Allows to
  6. * control saving of the body for each user, and later to avoid double
  7. * encryption when setting messages as encrypted.
  8. *
  9. * @package Lorea
  10. * @subpackage ElggPG
  11. *
  12. * Copyright 2011-2013 Lorea Faeries <federation@lorea.org>
  13. *
  14. * This file is part of the ElggPG plugin for Elgg.
  15. *
  16. * ElggPG is free software: you can redistribute it and/or modify it
  17. * under the terms of the GNU Affero General Public License as
  18. * published by the Free Software Foundation, either version 3 of the
  19. * License, or (at your option) any later version.
  20. *
  21. * ElggPG is distributed in the hope that it will be useful, but
  22. * WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. * Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public
  27. * License along with this program. If not, see
  28. * <http://www.gnu.org/licenses/>.
  29. */
  30. /**
  31. * Send an internal message
  32. *
  33. * @param string $subject The subject line of the message
  34. * @param string $body The body of the mesage
  35. * @param string $body_sent The body of the mesage encrypted for the sender
  36. * @param int $recipient_guid The GUID of the user to send to
  37. * @param int $sender_guid Optionally, the GUID of the user to send from
  38. * @param int $original_msg_guid The GUID of the message to reply from (default: none)
  39. * @param bool $notify Send a notification (default: true)
  40. * @param bool $add_to_sent If true (default), will add a message to the sender's 'sent' tray
  41. * @return bool
  42. */
  43. function messages_send_override($subject, $body, $body_sent, $recipient_guid, $sender_guid = 0, $original_msg_guid = 0, $notify = true, $add_to_sent = true) {
  44. // @todo remove globals
  45. global $messagesendflag;
  46. $messagesendflag = 1;
  47. // @todo remove globals
  48. global $messages_pm;
  49. if ($notify) {
  50. $messages_pm = 1;
  51. } else {
  52. $messages_pm = 0;
  53. }
  54. // If $sender_guid == 0, set to current user
  55. if ($sender_guid == 0) {
  56. $sender_guid = (int) elgg_get_logged_in_user_guid();
  57. }
  58. // Initialise 2 new ElggObject
  59. $message_to = new ElggObject();
  60. $message_sent = new ElggObject();
  61. $message_to->subtype = "messages";
  62. $message_sent->subtype = "messages";
  63. $message_to->owner_guid = $recipient_guid;
  64. $message_to->container_guid = $recipient_guid;
  65. $message_sent->owner_guid = $sender_guid;
  66. $message_sent->container_guid = $sender_guid;
  67. $message_to->access_id = ACCESS_PUBLIC;
  68. $message_sent->access_id = ACCESS_PUBLIC;
  69. $message_to->title = $subject;
  70. $message_to->description = $body;
  71. $message_sent->title = $subject;
  72. $message_sent->description = $body_sent;
  73. $message_to->toId = $recipient_guid; // the user receiving the message
  74. $message_to->fromId = $sender_guid; // the user receiving the message
  75. $message_to->readYet = 0; // this is a toggle between 0 / 1 (1 = read)
  76. $message_to->hiddenFrom = 0; // this is used when a user deletes a message in their sentbox, it is a flag
  77. $message_to->hiddenTo = 0; // this is used when a user deletes a message in their inbox
  78. $message_sent->toId = $recipient_guid; // the user receiving the message
  79. $message_sent->fromId = $sender_guid; // the user receiving the message
  80. $message_sent->readYet = 0; // this is a toggle between 0 / 1 (1 = read)
  81. $message_sent->hiddenFrom = 0; // this is used when a user deletes a message in their sentbox, it is a flag
  82. $message_sent->hiddenTo = 0; // this is used when a user deletes a message in their inbox
  83. $message_to->msg = 1;
  84. $message_sent->msg = 1;
  85. // Save the copy of the message that goes to the recipient
  86. $success = $message_to->save();
  87. // Save the copy of the message that goes to the sender
  88. if ($add_to_sent) {
  89. $message_sent->save();
  90. }
  91. $message_to->access_id = ACCESS_PRIVATE;
  92. $message_to->save();
  93. if ($add_to_sent) {
  94. $message_sent->access_id = ACCESS_PRIVATE;
  95. $message_sent->save();
  96. }
  97. // if the new message is a reply then create a relationship link between the new message
  98. // and the message it is in reply to
  99. if ($original_msg_guid && $success) {
  100. add_entity_relationship($message_sent->guid, "reply", $original_msg_guid);
  101. }
  102. $message_contents = strip_tags($body);
  103. if (($recipient_guid != elgg_get_logged_in_user_guid()) && $notify) {
  104. $recipient = get_user($recipient_guid);
  105. $sender = get_user($sender_guid);
  106. $subject = elgg_echo('messages:email:subject');
  107. $body = elgg_echo('messages:email:body', array(
  108. $sender->name,
  109. $message_contents,
  110. elgg_get_site_url() . "messages/inbox/" . $recipient->username,
  111. $sender->name,
  112. elgg_get_site_url() . "messages/compose?send_to=" . $sender_guid
  113. ));
  114. notify_user($recipient_guid, $sender_guid, $subject, $body);
  115. }
  116. $messagesendflag = 0;
  117. return $success;
  118. }