Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

242 行
6.0KB

  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. } else {
  125. return -3;
  126. }
  127. }
  128. function send_wallmessage($recipient='', $body='', $subject='', $replyto=''){
  129. if (! $recipient) {
  130. return -1;
  131. }
  132. if (! strlen($subject)) {
  133. $subject = t('[no subject]');
  134. }
  135. $guid = get_guid(32);
  136. $uri = 'urn:X-dfrn:' . App::get_baseurl() . ':' . local_user() . ':' . $guid;
  137. $convid = 0;
  138. $reply = false;
  139. require_once('include/Scrape.php');
  140. $me = probe_url($replyto);
  141. if (! $me['name']) {
  142. return -2;
  143. }
  144. $conv_guid = get_guid(32);
  145. $recip_handle = $recipient['nickname'] . '@' . substr(App::get_baseurl(), strpos(App::get_baseurl(),'://') + 3);
  146. $sender_nick = basename($replyto);
  147. $sender_host = substr($replyto,strpos($replyto,'://')+3);
  148. $sender_host = substr($sender_host,0,strpos($sender_host,'/'));
  149. $sender_handle = $sender_nick . '@' . $sender_host;
  150. $handles = $recip_handle . ';' . $sender_handle;
  151. $r = q("INSERT INTO `conv` (`uid`,`guid`,`creator`,`created`,`updated`,`subject`,`recips`) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ",
  152. intval($recipient['uid']),
  153. dbesc($conv_guid),
  154. dbesc($sender_handle),
  155. dbesc(datetime_convert()),
  156. dbesc(datetime_convert()),
  157. dbesc($subject),
  158. dbesc($handles)
  159. );
  160. $r = q("SELECT * FROM `conv` WHERE `guid` = '%s' AND `uid` = %d LIMIT 1",
  161. dbesc($conv_guid),
  162. intval($recipient['uid'])
  163. );
  164. if (! dbm::is_result($r)) {
  165. logger('send message: conversation not found.');
  166. return -4;
  167. }
  168. $convid = $r[0]['id'];
  169. $r = q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`,
  170. `contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`, `unknown`)
  171. VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s', %d )",
  172. intval($recipient['uid']),
  173. dbesc($guid),
  174. intval($convid),
  175. dbesc($me['name']),
  176. dbesc($me['photo']),
  177. dbesc($me['url']),
  178. 0,
  179. dbesc($subject),
  180. dbesc($body),
  181. 0,
  182. 0,
  183. 0,
  184. dbesc($uri),
  185. dbesc($replyto),
  186. datetime_convert(),
  187. 1
  188. );
  189. return 0;
  190. }