Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
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.

message.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. require_once('include/acl_selectors.php');
  3. require_once('include/message.php');
  4. function message_init(&$a) {
  5. $tabs = array();
  6. $new = array(
  7. 'label' => t('New Message'),
  8. 'url' => $a->get_baseurl(true) . '/message/new',
  9. 'sel'=> ($a->argv[1] == 'new'),
  10. );
  11. $tpl = get_markup_template('message_side.tpl');
  12. $a->page['aside'] = replace_macros($tpl, array(
  13. '$tabs'=>$tabs,
  14. '$new'=>$new,
  15. ));
  16. $base = $a->get_baseurl();
  17. $a->page['htmlhead'] .= '<script src="' . $a->get_baseurl(true) . '/library/jquery_ac/friendica.complete.js" ></script>';
  18. $a->page['htmlhead'] .= <<< EOT
  19. <script>$(document).ready(function() {
  20. var a;
  21. a = $("#recip").autocomplete({
  22. serviceUrl: '$base/acl',
  23. minChars: 2,
  24. width: 350,
  25. onSelect: function(value,data) {
  26. $("#recip-complete").val(data);
  27. }
  28. });
  29. });
  30. </script>
  31. EOT;
  32. }
  33. function message_post(&$a) {
  34. if(! local_user()) {
  35. notice( t('Permission denied.') . EOL);
  36. return;
  37. }
  38. $replyto = ((x($_REQUEST,'replyto')) ? notags(trim($_REQUEST['replyto'])) : '');
  39. $subject = ((x($_REQUEST,'subject')) ? notags(trim($_REQUEST['subject'])) : '');
  40. $body = ((x($_REQUEST,'body')) ? escape_tags(trim($_REQUEST['body'])) : '');
  41. $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto']) : 0 );
  42. // Work around doubled linefeeds in Tinymce 3.5b2
  43. $plaintext = intval(get_pconfig(local_user(),'system','plaintext'));
  44. if(! $plaintext) {
  45. $body = fix_mce_lf($body);
  46. }
  47. $ret = send_message($recipient, $body, $subject, $replyto);
  48. $norecip = false;
  49. switch($ret){
  50. case -1:
  51. notice( t('No recipient selected.') . EOL );
  52. $norecip = true;
  53. break;
  54. case -2:
  55. notice( t('Unable to locate contact information.') . EOL );
  56. break;
  57. case -3:
  58. notice( t('Message could not be sent.') . EOL );
  59. break;
  60. case -4:
  61. notice( t('Message collection failure.') . EOL );
  62. break;
  63. default:
  64. info( t('Message sent.') . EOL );
  65. }
  66. // fake it to go back to the input form if no recipient listed
  67. if($norecip) {
  68. $a->argc = 2;
  69. $a->argv[1] = 'new';
  70. }
  71. }
  72. function message_content(&$a) {
  73. $o = '';
  74. nav_set_selected('messages');
  75. if(! local_user()) {
  76. notice( t('Permission denied.') . EOL);
  77. return;
  78. }
  79. $myprofile = $a->get_baseurl(true) . '/profile/' . $a->user['nickname'];
  80. $tpl = get_markup_template('mail_head.tpl');
  81. $header = replace_macros($tpl, array(
  82. '$messages' => t('Messages'),
  83. '$tab_content' => $tab_content
  84. ));
  85. if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
  86. if(! intval($a->argv[2]))
  87. return;
  88. $cmd = $a->argv[1];
  89. if($cmd === 'drop') {
  90. $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  91. intval($a->argv[2]),
  92. intval(local_user())
  93. );
  94. if($r) {
  95. info( t('Message deleted.') . EOL );
  96. }
  97. goaway($a->get_baseurl(true) . '/message' );
  98. }
  99. else {
  100. $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  101. intval($a->argv[2]),
  102. intval(local_user())
  103. );
  104. if(count($r)) {
  105. $parent = $r[0]['parent-uri'];
  106. $convid = $r[0]['convid'];
  107. $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
  108. dbesc($parent),
  109. intval(local_user())
  110. );
  111. // remove diaspora conversation pointer
  112. // Actually if we do this, we can never receive another reply to that conversation,
  113. // as we will never again have the info we need to re-create it.
  114. // We'll just have to orphan it.
  115. //if($convid) {
  116. // q("delete from conv where id = %d limit 1",
  117. // intval($convid)
  118. // );
  119. //}
  120. if($r)
  121. info( t('Conversation removed.') . EOL );
  122. }
  123. goaway($a->get_baseurl(true) . '/message' );
  124. }
  125. }
  126. if(($a->argc > 1) && ($a->argv[1] === 'new')) {
  127. $o .= $header;
  128. $plaintext = false;
  129. if(intval(get_pconfig(local_user(),'system','plaintext')))
  130. $plaintext = true;
  131. $tpl = get_markup_template('msg-header.tpl');
  132. $a->page['htmlhead'] .= replace_macros($tpl, array(
  133. '$baseurl' => $a->get_baseurl(true),
  134. '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
  135. '$nickname' => $a->user['nickname'],
  136. '$linkurl' => t('Please enter a link URL:')
  137. ));
  138. $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
  139. $prename = $preurl = $preid = '';
  140. if($preselect) {
  141. $r = q("select name, url, id from contact where uid = %d and id = %d limit 1",
  142. intval(local_user()),
  143. intval($a->argv[2])
  144. );
  145. if(count($r)) {
  146. $prename = $r[0]['name'];
  147. $preurl = $r[0]['url'];
  148. $preid = $r[0]['id'];
  149. }
  150. }
  151. $prefill = (($preselect) ? $prename : '');
  152. // the ugly select box
  153. $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
  154. $tpl = get_markup_template('prv_message.tpl');
  155. $o .= replace_macros($tpl,array(
  156. '$header' => t('Send Private Message'),
  157. '$to' => t('To:'),
  158. '$showinputs' => 'true',
  159. '$prefill' => $prefill,
  160. '$autocomp' => $autocomp,
  161. '$preid' => $preid,
  162. '$subject' => t('Subject:'),
  163. '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
  164. '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
  165. '$readonly' => '',
  166. '$yourmessage' => t('Your message:'),
  167. '$select' => $select,
  168. '$parent' => '',
  169. '$upload' => t('Upload photo'),
  170. '$insert' => t('Insert web link'),
  171. '$wait' => t('Please wait'),
  172. '$submit' => t('Submit')
  173. ));
  174. return $o;
  175. }
  176. if($a->argc == 1) {
  177. // list messages
  178. $o .= $header;
  179. $r = q("SELECT count(*) AS `total` FROM `mail`
  180. WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
  181. intval(local_user()),
  182. dbesc($myprofile)
  183. );
  184. if(count($r))
  185. $a->set_pager_total($r[0]['total']);
  186. $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
  187. `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
  188. count( * ) as count
  189. FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
  190. WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
  191. intval(local_user()),
  192. //
  193. intval($a->pager['start']),
  194. intval($a->pager['itemspage'])
  195. );
  196. if(! count($r)) {
  197. info( t('No messages.') . EOL);
  198. return $o;
  199. }
  200. $tpl = get_markup_template('mail_list.tpl');
  201. foreach($r as $rr) {
  202. if($rr['unknown']) {
  203. $partecipants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
  204. }
  205. elseif (link_compare($rr['from-url'],$myprofile)){
  206. $partecipants = sprintf( t("You and %s"), $rr['name']);
  207. }
  208. else {
  209. $partecipants = sprintf( t("%s and You"), $rr['from-name']);
  210. }
  211. $o .= replace_macros($tpl, array(
  212. '$id' => $rr['id'],
  213. '$from_name' => $partecipants,
  214. '$from_url' => (($rr['network'] === NETWORK_DFRN) ? $a->get_baseurl(true) . '/redir/' . $rr['contact-id'] : $rr['url']),
  215. '$sparkle' => ' sparkle',
  216. '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
  217. '$subject' => template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>')),
  218. '$delete' => t('Delete conversation'),
  219. '$body' => template_escape($rr['body']),
  220. '$to_name' => template_escape($rr['name']),
  221. '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
  222. '$seen' => $rr['mailseen'],
  223. '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),
  224. ));
  225. }
  226. $o .= paginate($a);
  227. return $o;
  228. }
  229. if(($a->argc > 1) && (intval($a->argv[1]))) {
  230. $o .= $header;
  231. $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
  232. FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
  233. WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
  234. intval(local_user()),
  235. intval($a->argv[1])
  236. );
  237. if(count($r)) {
  238. $contact_id = $r[0]['contact-id'];
  239. $convid = $r[0]['convid'];
  240. $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
  241. if($convid)
  242. $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
  243. dbesc($r[0]['parent-uri']),
  244. intval($convid)
  245. );
  246. $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
  247. FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
  248. WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
  249. intval(local_user())
  250. );
  251. }
  252. if(! count($messages)) {
  253. notice( t('Message not available.') . EOL );
  254. return $o;
  255. }
  256. $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
  257. dbesc($r[0]['parent-uri']),
  258. intval(local_user())
  259. );
  260. require_once("include/bbcode.php");
  261. $tpl = get_markup_template('msg-header.tpl');
  262. $a->page['htmlhead'] .= replace_macros($tpl, array(
  263. '$nickname' => $a->user['nickname'],
  264. '$baseurl' => $a->get_baseurl(true)
  265. ));
  266. $mails = array();
  267. $seen = 0;
  268. $unknown = false;
  269. foreach($messages as $message) {
  270. if($message['unknown'])
  271. $unknown = true;
  272. if($message['from-url'] == $myprofile) {
  273. $from_url = $myprofile;
  274. $sparkle = '';
  275. }
  276. else {
  277. $from_url = $a->get_baseurl(true) . '/redir/' . $message['contact-id'];
  278. $sparkle = ' sparkle';
  279. }
  280. $Text = $message['body'];
  281. $saved_image = '';
  282. $img_start = strpos($Text,'[img]data:');
  283. $img_end = strpos($Text,'[/img]');
  284. if($img_start !== false && $img_end !== false && $img_end > $img_start) {
  285. $start_fragment = substr($Text,0,$img_start);
  286. $img_start += strlen('[img]');
  287. $saved_image = substr($Text,$img_start,$img_end - $img_start);
  288. $end_fragment = substr($Text,$img_end + strlen('[/img]'));
  289. $Text = $start_fragment . '[!#saved_image#!]' . $end_fragment;
  290. $search = '/\[url\=(.*?)\]\[!#saved_image#!\]\[\/url\]' . '/is';
  291. $replace = '[url=' . z_path() . '/redir/' . $message['contact-id']
  292. . '?f=1&url=' . '$1' . '][!#saved_image#!][/url]' ;
  293. $Text = preg_replace($search,$replace,$Text);
  294. if(strlen($saved_image))
  295. $message['body'] = str_replace('[!#saved_image#!]', '[img]' . $saved_image . '[/img]',$Text);
  296. }
  297. $mails[] = array(
  298. 'id' => $message['id'],
  299. 'from_name' => template_escape($message['from-name']),
  300. 'from_url' => $from_url,
  301. 'sparkle' => $sparkle,
  302. 'from_photo' => $message['from-photo'],
  303. 'subject' => template_escape($message['title']),
  304. 'body' => template_escape(smilies(bbcode($message['body']))),
  305. 'delete' => t('Delete message'),
  306. 'to_name' => template_escape($message['name']),
  307. 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
  308. );
  309. $seen = $message['seen'];
  310. }
  311. $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
  312. $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
  313. $tpl = get_markup_template('mail_display.tpl');
  314. $o = replace_macros($tpl, array(
  315. '$thread_id' => $a->argv[1],
  316. '$thread_subject' => $message['title'],
  317. '$thread_seen' => $seen,
  318. '$delete' => t('Delete conversation'),
  319. '$canreply' => (($unknown) ? false : '1'),
  320. '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
  321. '$mails' => $mails,
  322. // reply
  323. '$header' => t('Send Reply'),
  324. '$to' => t('To:'),
  325. '$showinputs' => '',
  326. '$subject' => t('Subject:'),
  327. '$subjtxt' => template_escape($message['title']),
  328. '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
  329. '$yourmessage' => t('Your message:'),
  330. '$text' => '',
  331. '$select' => $select,
  332. '$parent' => $parent,
  333. '$upload' => t('Upload photo'),
  334. '$insert' => t('Insert web link'),
  335. '$submit' => t('Submit'),
  336. '$wait' => t('Please wait')
  337. ));
  338. return $o;
  339. }
  340. }