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.

243 lines
6.0KB

  1. <?php
  2. // send a private message
  3. use Friendica\App;
  4. function send_message($recipient=0, $body='', $subject='', $replyto=''){
  5. $a = get_app();
  6. if (! $recipient) return -1;
  7. if (! strlen($subject))
  8. $subject = t('[no subject]');
  9. $me = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
  10. intval(local_user())
  11. );
  12. $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  13. intval($recipient),
  14. intval(local_user())
  15. );
  16. if (! (count($me) && (count($contact)))) {
  17. return -2;
  18. }
  19. $guid = get_guid(32);
  20. $uri = 'urn:X-dfrn:' . App::get_baseurl() . ':' . local_user() . ':' . $guid;
  21. $convid = 0;
  22. $reply = false;
  23. // look for any existing conversation structure
  24. if (strlen($replyto)) {
  25. $reply = true;
  26. $r = q("select convid from mail where uid = %d and ( uri = '%s' or `parent-uri` = '%s' ) limit 1",
  27. intval(local_user()),
  28. dbesc($replyto),
  29. dbesc($replyto)
  30. );
  31. if (dbm::is_result($r))
  32. $convid = $r[0]['convid'];
  33. }
  34. if (! $convid) {
  35. // create a new conversation
  36. $recip_host = substr($contact[0]['url'],strpos($contact[0]['url'],'://')+3);
  37. $recip_host = substr($recip_host,0,strpos($recip_host,'/'));
  38. $recip_handle = (($contact[0]['addr']) ? $contact[0]['addr'] : $contact[0]['nick'] . '@' . $recip_host);
  39. $sender_handle = $a->user['nickname'] . '@' . substr(App::get_baseurl(), strpos(App::get_baseurl(),'://') + 3);
  40. $conv_guid = get_guid(32);
  41. $convuri = $recip_handle.':'.$conv_guid;
  42. $handles = $recip_handle . ';' . $sender_handle;
  43. $r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ",
  44. intval(local_user()),
  45. dbesc($conv_guid),
  46. dbesc($sender_handle),
  47. dbesc(datetime_convert()),
  48. dbesc(datetime_convert()),
  49. dbesc($subject),
  50. dbesc($handles)
  51. );
  52. $r = q("select * from conv where guid = '%s' and uid = %d limit 1",
  53. dbesc($conv_guid),
  54. intval(local_user())
  55. );
  56. if (dbm::is_result($r))
  57. $convid = $r[0]['id'];
  58. }
  59. if (! $convid) {
  60. logger('send message: conversation not found.');
  61. return -4;
  62. }
  63. if (! strlen($replyto)) {
  64. $replyto = $convuri;
  65. }
  66. $r = q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`,
  67. `contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`)
  68. VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s' )",
  69. intval(local_user()),
  70. dbesc($guid),
  71. intval($convid),
  72. dbesc($me[0]['name']),
  73. dbesc($me[0]['thumb']),
  74. dbesc($me[0]['url']),
  75. intval($recipient),
  76. dbesc($subject),
  77. dbesc($body),
  78. 1,
  79. intval($reply),
  80. 0,
  81. dbesc($uri),
  82. dbesc($replyto),
  83. datetime_convert()
  84. );
  85. $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
  86. dbesc($uri),
  87. intval(local_user())
  88. );
  89. if (dbm::is_result($r))
  90. $post_id = $r[0]['id'];
  91. /**
  92. *
  93. * When a photo was uploaded into the message using the (profile wall) ajax
  94. * uploader, The permissions are initially set to disallow anybody but the
  95. * owner from seeing it. This is because the permissions may not yet have been
  96. * set for the post. If it's private, the photo permissions should be set
  97. * appropriately. But we didn't know the final permissions on the post until
  98. * now. So now we'll look for links of uploaded messages that are in the
  99. * post and set them to the same permissions as the post itself.
  100. *
  101. */
  102. $match = null;
  103. if (preg_match_all("/\[img\](.*?)\[\/img\]/",$body,$match)) {
  104. $images = $match[1];
  105. if (count($images)) {
  106. foreach ($images as $image) {
  107. if (! stristr($image,App::get_baseurl() . '/photo/')) {
  108. continue;
  109. }
  110. $image_uri = substr($image,strrpos($image,'/') + 1);
  111. $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
  112. $r = q("UPDATE `photo` SET `allow_cid` = '%s'
  113. WHERE `resource-id` = '%s' AND `album` = '%s' AND `uid` = %d ",
  114. dbesc('<' . $recipient . '>'),
  115. dbesc($image_uri),
  116. dbesc( t('Wall Photos')),
  117. intval(local_user())
  118. );
  119. }
  120. }
  121. }
  122. if ($post_id) {
  123. proc_run(PRIORITY_HIGH, "include/notifier.php", "mail", $post_id);
  124. return intval($post_id);
  125. } else {
  126. return -3;
  127. }
  128. }
  129. function send_wallmessage($recipient='', $body='', $subject='', $replyto=''){
  130. if (! $recipient) {
  131. return -1;
  132. }
  133. if (! strlen($subject)) {
  134. $subject = t('[no subject]');
  135. }
  136. $guid = get_guid(32);
  137. $uri = 'urn:X-dfrn:' . App::get_baseurl() . ':' . local_user() . ':' . $guid;
  138. $convid = 0;
  139. $reply = false;
  140. require_once 'include/probe.php';
  141. $me = probe_url($replyto);
  142. if (! $me['name']) {
  143. return -2;
  144. }
  145. $conv_guid = get_guid(32);
  146. $recip_handle = $recipient['nickname'] . '@' . substr(App::get_baseurl(), strpos(App::get_baseurl(),'://') + 3);
  147. $sender_nick = basename($replyto);
  148. $sender_host = substr($replyto,strpos($replyto,'://')+3);
  149. $sender_host = substr($sender_host,0,strpos($sender_host,'/'));
  150. $sender_handle = $sender_nick . '@' . $sender_host;
  151. $handles = $recip_handle . ';' . $sender_handle;
  152. $r = q("INSERT INTO `conv` (`uid`,`guid`,`creator`,`created`,`updated`,`subject`,`recips`) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ",
  153. intval($recipient['uid']),
  154. dbesc($conv_guid),
  155. dbesc($sender_handle),
  156. dbesc(datetime_convert()),
  157. dbesc(datetime_convert()),
  158. dbesc($subject),
  159. dbesc($handles)
  160. );
  161. $r = q("SELECT * FROM `conv` WHERE `guid` = '%s' AND `uid` = %d LIMIT 1",
  162. dbesc($conv_guid),
  163. intval($recipient['uid'])
  164. );
  165. if (! dbm::is_result($r)) {
  166. logger('send message: conversation not found.');
  167. return -4;
  168. }
  169. $convid = $r[0]['id'];
  170. $r = q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`,
  171. `contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`, `unknown`)
  172. VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s', %d )",
  173. intval($recipient['uid']),
  174. dbesc($guid),
  175. intval($convid),
  176. dbesc($me['name']),
  177. dbesc($me['photo']),
  178. dbesc($me['url']),
  179. 0,
  180. dbesc($subject),
  181. dbesc($body),
  182. 0,
  183. 0,
  184. 0,
  185. dbesc($uri),
  186. dbesc($replyto),
  187. datetime_convert(),
  188. 1
  189. );
  190. return 0;
  191. }