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.

244 lines
6.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
  1. <?php
  2. // send a private message
  3. function send_message($recipient=0, $body='', $subject='', $replyto=''){
  4. $a = get_app();
  5. if(! $recipient) return -1;
  6. if(! strlen($subject))
  7. $subject = t('[no subject]');
  8. $me = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
  9. intval(local_user())
  10. );
  11. $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  12. intval($recipient),
  13. intval(local_user())
  14. );
  15. if(! (count($me) && (count($contact)))) {
  16. return -2;
  17. }
  18. $guid = get_guid(32);
  19. $uri = 'urn:X-dfrn:' . App::get_baseurl() . ':' . local_user() . ':' . $guid;
  20. $convid = 0;
  21. $reply = false;
  22. // look for any existing conversation structure
  23. if(strlen($replyto)) {
  24. $reply = true;
  25. $r = q("select convid from mail where uid = %d and ( uri = '%s' or `parent-uri` = '%s' ) limit 1",
  26. intval(local_user()),
  27. dbesc($replyto),
  28. dbesc($replyto)
  29. );
  30. if (dbm::is_result($r))
  31. $convid = $r[0]['convid'];
  32. }
  33. if(! $convid) {
  34. // create a new conversation
  35. $recip_host = substr($contact[0]['url'],strpos($contact[0]['url'],'://')+3);
  36. $recip_host = substr($recip_host,0,strpos($recip_host,'/'));
  37. $recip_handle = (($contact[0]['addr']) ? $contact[0]['addr'] : $contact[0]['nick'] . '@' . $recip_host);
  38. $sender_handle = $a->user['nickname'] . '@' . substr(App::get_baseurl(), strpos(App::get_baseurl(),'://') + 3);
  39. $conv_guid = get_guid(32);
  40. $convuri = $recip_handle.':'.$conv_guid;
  41. $handles = $recip_handle . ';' . $sender_handle;
  42. $r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ",
  43. intval(local_user()),
  44. dbesc($conv_guid),
  45. dbesc($sender_handle),
  46. dbesc(datetime_convert()),
  47. dbesc(datetime_convert()),
  48. dbesc($subject),
  49. dbesc($handles)
  50. );
  51. $r = q("select * from conv where guid = '%s' and uid = %d limit 1",
  52. dbesc($conv_guid),
  53. intval(local_user())
  54. );
  55. if (dbm::is_result($r))
  56. $convid = $r[0]['id'];
  57. }
  58. if(! $convid) {
  59. logger('send message: conversation not found.');
  60. return -4;
  61. }
  62. if(! strlen($replyto)) {
  63. $replyto = $convuri;
  64. }
  65. $r = q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`,
  66. `contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`)
  67. VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s' )",
  68. intval(local_user()),
  69. dbesc($guid),
  70. intval($convid),
  71. dbesc($me[0]['name']),
  72. dbesc($me[0]['thumb']),
  73. dbesc($me[0]['url']),
  74. intval($recipient),
  75. dbesc($subject),
  76. dbesc($body),
  77. 1,
  78. intval($reply),
  79. 0,
  80. dbesc($uri),
  81. dbesc($replyto),
  82. datetime_convert()
  83. );
  84. $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' and `uid` = %d LIMIT 1",
  85. dbesc($uri),
  86. intval(local_user())
  87. );
  88. if (dbm::is_result($r))
  89. $post_id = $r[0]['id'];
  90. /**
  91. *
  92. * When a photo was uploaded into the message using the (profile wall) ajax
  93. * uploader, The permissions are initially set to disallow anybody but the
  94. * owner from seeing it. This is because the permissions may not yet have been
  95. * set for the post. If it's private, the photo permissions should be set
  96. * appropriately. But we didn't know the final permissions on the post until
  97. * now. So now we'll look for links of uploaded messages that are in the
  98. * post and set them to the same permissions as the post itself.
  99. *
  100. */
  101. $match = null;
  102. if (preg_match_all("/\[img\](.*?)\[\/img\]/",$body,$match)) {
  103. $images = $match[1];
  104. if (count($images)) {
  105. foreach ($images as $image) {
  106. if (! stristr($image,App::get_baseurl() . '/photo/')) {
  107. continue;
  108. }
  109. $image_uri = substr($image,strrpos($image,'/') + 1);
  110. $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
  111. $r = q("UPDATE `photo` SET `allow_cid` = '%s'
  112. WHERE `resource-id` = '%s' AND `album` = '%s' AND `uid` = %d ",
  113. dbesc('<' . $recipient . '>'),
  114. dbesc($image_uri),
  115. dbesc( t('Wall Photos')),
  116. intval(local_user())
  117. );
  118. }
  119. }
  120. }
  121. if ($post_id) {
  122. proc_run(PRIORITY_HIGH, "include/notifier.php", "mail", $post_id);
  123. return intval($post_id);
  124. }
  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/Scrape.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. }