Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
6.3 KiB

  1. <?php
  2. /**
  3. * @file src/Model/Mail.php
  4. */
  5. namespace Friendica\Model;
  6. use Friendica\Core\L10n;
  7. use Friendica\Core\Logger;
  8. use Friendica\Core\System;
  9. use Friendica\Core\Worker;
  10. use Friendica\Database\DBA;
  11. use Friendica\Network\Probe;
  12. use Friendica\Util\DateTimeFormat;
  13. /**
  14. * Class to handle private messages
  15. */
  16. class Mail
  17. {
  18. /**
  19. * Send private message
  20. *
  21. * @param integer $recipient recipient id, default 0
  22. * @param string $body message body, default empty
  23. * @param string $subject message subject, default empty
  24. * @param string $replyto reply to
  25. * @return int
  26. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  27. */
  28. public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
  29. {
  30. $a = \get_app();
  31. if (!$recipient) {
  32. return -1;
  33. }
  34. if (!strlen($subject)) {
  35. $subject = L10n::t('[no subject]');
  36. }
  37. $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
  38. $contact = DBA::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
  39. if (!(count($me) && (count($contact)))) {
  40. return -2;
  41. }
  42. $guid = System::createUUID();
  43. $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
  44. $convid = 0;
  45. $reply = false;
  46. // look for any existing conversation structure
  47. if (strlen($replyto)) {
  48. $reply = true;
  49. $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
  50. local_user(), $replyto, $replyto];
  51. $mail = DBA::selectFirst('mail', ['convid'], $condition);
  52. if (DBA::isResult($mail)) {
  53. $convid = $mail['convid'];
  54. }
  55. }
  56. $convuri = '';
  57. if (!$convid) {
  58. // create a new conversation
  59. $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
  60. $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
  61. $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
  62. $sender_handle = $a->user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
  63. $conv_guid = System::createUUID();
  64. $convuri = $recip_handle . ':' . $conv_guid;
  65. $handles = $recip_handle . ';' . $sender_handle;
  66. $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
  67. 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
  68. 'subject' => $subject, 'recips' => $handles];
  69. if (DBA::insert('conv', $fields)) {
  70. $convid = DBA::lastInsertId();
  71. }
  72. }
  73. if (!$convid) {
  74. Logger::log('send message: conversation not found.');
  75. return -4;
  76. }
  77. if (!strlen($replyto)) {
  78. $replyto = $convuri;
  79. }
  80. $post_id = null;
  81. $success = DBA::insert(
  82. 'mail',
  83. [
  84. 'uid' => local_user(),
  85. 'guid' => $guid,
  86. 'convid' => $convid,
  87. 'from-name' => $me['name'],
  88. 'from-photo' => $me['thumb'],
  89. 'from-url' => $me['url'],
  90. 'contact-id' => $recipient,
  91. 'title' => $subject,
  92. 'body' => $body,
  93. 'seen' => 1,
  94. 'reply' => $reply,
  95. 'replied' => 0,
  96. 'uri' => $uri,
  97. 'parent-uri' => $replyto,
  98. 'created' => DateTimeFormat::utcNow()
  99. ]
  100. );
  101. if ($success) {
  102. $post_id = DBA::lastInsertId();
  103. }
  104. /**
  105. *
  106. * When a photo was uploaded into the message using the (profile wall) ajax
  107. * uploader, The permissions are initially set to disallow anybody but the
  108. * owner from seeing it. This is because the permissions may not yet have been
  109. * set for the post. If it's private, the photo permissions should be set
  110. * appropriately. But we didn't know the final permissions on the post until
  111. * now. So now we'll look for links of uploaded messages that are in the
  112. * post and set them to the same permissions as the post itself.
  113. *
  114. */
  115. $match = null;
  116. if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
  117. $images = $match[1];
  118. if (count($images)) {
  119. foreach ($images as $image) {
  120. if (!stristr($image, System::baseUrl() . '/photo/')) {
  121. continue;
  122. }
  123. $image_uri = substr($image, strrpos($image, '/') + 1);
  124. $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
  125. Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_uri, 'album' => 'Wall Photos', 'uid' => local_user()]);
  126. }
  127. }
  128. }
  129. if ($post_id) {
  130. Worker::add(PRIORITY_HIGH, "Notifier", "mail", $post_id);
  131. return intval($post_id);
  132. } else {
  133. return -3;
  134. }
  135. }
  136. /**
  137. * @param array $recipient recipient, default empty
  138. * @param string $body message body, default empty
  139. * @param string $subject message subject, default empty
  140. * @param string $replyto reply to, default empty
  141. * @return int
  142. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  143. * @throws \ImagickException
  144. */
  145. public static function sendWall(array $recipient = [], $body = '', $subject = '', $replyto = '')
  146. {
  147. if (!$recipient) {
  148. return -1;
  149. }
  150. if (!strlen($subject)) {
  151. $subject = L10n::t('[no subject]');
  152. }
  153. $guid = System::createUUID();
  154. $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
  155. $me = Probe::uri($replyto);
  156. if (!$me['name']) {
  157. return -2;
  158. }
  159. $conv_guid = System::createUUID();
  160. $recip_handle = $recipient['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
  161. $sender_nick = basename($replyto);
  162. $sender_host = substr($replyto, strpos($replyto, '://') + 3);
  163. $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
  164. $sender_handle = $sender_nick . '@' . $sender_host;
  165. $handles = $recip_handle . ';' . $sender_handle;
  166. $convid = null;
  167. $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
  168. 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
  169. 'subject' => $subject, 'recips' => $handles];
  170. if (DBA::insert('conv', $fields)) {
  171. $convid = DBA::lastInsertId();
  172. }
  173. if (!$convid) {
  174. Logger::log('send message: conversation not found.');
  175. return -4;
  176. }
  177. DBA::insert(
  178. 'mail',
  179. [
  180. 'uid' => $recipient['uid'],
  181. 'guid' => $guid,
  182. 'convid' => $convid,
  183. 'from-name' => $me['name'],
  184. 'from-photo' => $me['photo'],
  185. 'from-url' => $me['url'],
  186. 'contact-id' => 0,
  187. 'title' => $subject,
  188. 'body' => $body,
  189. 'seen' => 0,
  190. 'reply' => 0,
  191. 'replied' => 0,
  192. 'uri' => $uri,
  193. 'parent-uri' => $replyto,
  194. 'created' => DateTimeFormat::utcNow(),
  195. 'unknown' => 1
  196. ]
  197. );
  198. return 0;
  199. }
  200. }