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.

231 lines
6.1KB

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