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.

489 lines
14KB

  1. <?php
  2. /**
  3. * @file mod/message.php
  4. */
  5. use Friendica\App;
  6. use Friendica\Content\Nav;
  7. use Friendica\Content\Smilies;
  8. use Friendica\Content\Text\BBCode;
  9. use Friendica\Core\ACL;
  10. use Friendica\Core\L10n;
  11. use Friendica\Core\System;
  12. use Friendica\Database\DBM;
  13. use Friendica\Model\Contact;
  14. use Friendica\Model\Mail;
  15. use Friendica\Util\DateTimeFormat;
  16. use Friendica\Util\Temporal;
  17. require_once 'include/conversation.php';
  18. function message_init(App $a)
  19. {
  20. $tabs = '';
  21. if ($a->argc > 1 && is_numeric($a->argv[1])) {
  22. $tabs = render_messages(get_messages(local_user(), 0, 5), 'mail_list.tpl');
  23. }
  24. $new = [
  25. 'label' => L10n::t('New Message'),
  26. 'url' => 'message/new',
  27. 'sel' => $a->argc > 1 && $a->argv[1] == 'new',
  28. 'accesskey' => 'm',
  29. ];
  30. $tpl = get_markup_template('message_side.tpl');
  31. $a->page['aside'] = replace_macros($tpl, [
  32. '$tabs' => $tabs,
  33. '$new' => $new,
  34. ]);
  35. $base = System::baseUrl();
  36. $head_tpl = get_markup_template('message-head.tpl');
  37. $a->page['htmlhead'] .= replace_macros($head_tpl, [
  38. '$baseurl' => System::baseUrl(true),
  39. '$base' => $base
  40. ]);
  41. $end_tpl = get_markup_template('message-end.tpl');
  42. $a->page['end'] .= replace_macros($end_tpl, [
  43. '$baseurl' => System::baseUrl(true),
  44. '$base' => $base
  45. ]);
  46. }
  47. function message_post(App $a)
  48. {
  49. if (!local_user()) {
  50. notice(L10n::t('Permission denied.') . EOL);
  51. return;
  52. }
  53. $replyto = x($_REQUEST, 'replyto') ? notags(trim($_REQUEST['replyto'])) : '';
  54. $subject = x($_REQUEST, 'subject') ? notags(trim($_REQUEST['subject'])) : '';
  55. $body = x($_REQUEST, 'body') ? escape_tags(trim($_REQUEST['body'])) : '';
  56. $recipient = x($_REQUEST, 'messageto') ? intval($_REQUEST['messageto']) : 0;
  57. $ret = Mail::send($recipient, $body, $subject, $replyto);
  58. $norecip = false;
  59. switch ($ret) {
  60. case -1:
  61. notice(L10n::t('No recipient selected.') . EOL);
  62. $norecip = true;
  63. break;
  64. case -2:
  65. notice(L10n::t('Unable to locate contact information.') . EOL);
  66. break;
  67. case -3:
  68. notice(L10n::t('Message could not be sent.') . EOL);
  69. break;
  70. case -4:
  71. notice(L10n::t('Message collection failure.') . EOL);
  72. break;
  73. default:
  74. info(L10n::t('Message sent.') . EOL);
  75. }
  76. // fake it to go back to the input form if no recipient listed
  77. if ($norecip) {
  78. $a->argc = 2;
  79. $a->argv[1] = 'new';
  80. } else {
  81. goaway($_SESSION['return_url']);
  82. }
  83. }
  84. function message_content(App $a)
  85. {
  86. $o = '';
  87. Nav::setSelected('messages');
  88. if (!local_user()) {
  89. notice(L10n::t('Permission denied.') . EOL);
  90. return;
  91. }
  92. $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
  93. $tpl = get_markup_template('mail_head.tpl');
  94. $header = replace_macros($tpl, [
  95. '$messages' => L10n::t('Messages'),
  96. ]);
  97. if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
  98. if (!intval($a->argv[2])) {
  99. return;
  100. }
  101. // Check if we should do HTML-based delete confirmation
  102. if ($_REQUEST['confirm']) {
  103. // <form> can't take arguments in its "action" parameter
  104. // so add any arguments as hidden inputs
  105. $query = explode_querystring($a->query_string);
  106. $inputs = [];
  107. foreach ($query['args'] as $arg) {
  108. if (strpos($arg, 'confirm=') === false) {
  109. $arg_parts = explode('=', $arg);
  110. $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
  111. }
  112. }
  113. //$a->page['aside'] = '';
  114. return replace_macros(get_markup_template('confirm.tpl'), [
  115. '$method' => 'get',
  116. '$message' => L10n::t('Do you really want to delete this message?'),
  117. '$extra_inputs' => $inputs,
  118. '$confirm' => L10n::t('Yes'),
  119. '$confirm_url' => $query['base'],
  120. '$confirm_name' => 'confirmed',
  121. '$cancel' => L10n::t('Cancel'),
  122. ]);
  123. }
  124. // Now check how the user responded to the confirmation query
  125. if ($_REQUEST['canceled']) {
  126. goaway($_SESSION['return_url']);
  127. }
  128. $cmd = $a->argv[1];
  129. if ($cmd === 'drop') {
  130. if (dba::delete('mail', ['id' => $a->argv[2], 'uid' => local_user()])) {
  131. info(L10n::t('Message deleted.') . EOL);
  132. }
  133. //goaway(System::baseUrl(true) . '/message' );
  134. goaway($_SESSION['return_url']);
  135. } else {
  136. $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  137. intval($a->argv[2]),
  138. intval(local_user())
  139. );
  140. if (DBM::is_result($r)) {
  141. $parent = $r[0]['parent-uri'];
  142. $convid = $r[0]['convid'];
  143. if (dba::delete('mail', ['parent-uri' => $parent, 'uid' => local_user()])) {
  144. info(L10n::t('Conversation removed.') . EOL);
  145. }
  146. }
  147. //goaway(System::baseUrl(true) . '/message' );
  148. goaway($_SESSION['return_url']);
  149. }
  150. }
  151. if (($a->argc > 1) && ($a->argv[1] === 'new')) {
  152. $o .= $header;
  153. $tpl = get_markup_template('msg-header.tpl');
  154. $a->page['htmlhead'] .= replace_macros($tpl, [
  155. '$baseurl' => System::baseUrl(true),
  156. '$nickname' => $a->user['nickname'],
  157. '$linkurl' => L10n::t('Please enter a link URL:')
  158. ]);
  159. $tpl = get_markup_template('msg-end.tpl');
  160. $a->page['end'] .= replace_macros($tpl, [
  161. '$baseurl' => System::baseUrl(true),
  162. '$nickname' => $a->user['nickname'],
  163. '$linkurl' => L10n::t('Please enter a link URL:')
  164. ]);
  165. $preselect = isset($a->argv[2]) ? [$a->argv[2]] : [];
  166. $prename = $preurl = $preid = '';
  167. if ($preselect) {
  168. $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
  169. intval(local_user()),
  170. intval($a->argv[2])
  171. );
  172. if (!DBM::is_result($r)) {
  173. $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
  174. intval(local_user()),
  175. dbesc(normalise_link(base64_decode($a->argv[2])))
  176. );
  177. }
  178. if (!DBM::is_result($r)) {
  179. $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
  180. intval(local_user()),
  181. dbesc(base64_decode($a->argv[2]))
  182. );
  183. }
  184. if (DBM::is_result($r)) {
  185. $prename = $r[0]['name'];
  186. $preurl = $r[0]['url'];
  187. $preid = $r[0]['id'];
  188. $preselect = [$preid];
  189. } else {
  190. $preselect = [];
  191. }
  192. }
  193. $prefill = $preselect ? $prename : '';
  194. // the ugly select box
  195. $select = ACL::getMessageContactSelectHTML('messageto', 'message-to-select', $preselect, 4, 10);
  196. $tpl = get_markup_template('prv_message.tpl');
  197. $o .= replace_macros($tpl, [
  198. '$header' => L10n::t('Send Private Message'),
  199. '$to' => L10n::t('To:'),
  200. '$showinputs' => 'true',
  201. '$prefill' => $prefill,
  202. '$preid' => $preid,
  203. '$subject' => L10n::t('Subject:'),
  204. '$subjtxt' => x($_REQUEST, 'subject') ? strip_tags($_REQUEST['subject']) : '',
  205. '$text' => x($_REQUEST, 'body') ? escape_tags(htmlspecialchars($_REQUEST['body'])) : '',
  206. '$readonly' => '',
  207. '$yourmessage' => L10n::t('Your message:'),
  208. '$select' => $select,
  209. '$parent' => '',
  210. '$upload' => L10n::t('Upload photo'),
  211. '$insert' => L10n::t('Insert web link'),
  212. '$wait' => L10n::t('Please wait'),
  213. '$submit' => L10n::t('Submit')
  214. ]);
  215. return $o;
  216. }
  217. $_SESSION['return_url'] = $a->query_string;
  218. if ($a->argc == 1) {
  219. // List messages
  220. $o .= $header;
  221. $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
  222. WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
  223. intval(local_user())
  224. );
  225. if (DBM::is_result($r)) {
  226. $a->set_pager_total($r[0]['total']);
  227. }
  228. $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
  229. if (!DBM::is_result($r)) {
  230. info(L10n::t('No messages.') . EOL);
  231. return $o;
  232. }
  233. $o .= render_messages($r, 'mail_list.tpl');
  234. $o .= paginate($a);
  235. return $o;
  236. }
  237. if (($a->argc > 1) && (intval($a->argv[1]))) {
  238. $o .= $header;
  239. $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
  240. FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
  241. WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
  242. intval(local_user()),
  243. intval($a->argv[1])
  244. );
  245. if (DBM::is_result($r)) {
  246. $contact_id = $r[0]['contact-id'];
  247. $convid = $r[0]['convid'];
  248. $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
  249. if ($convid)
  250. $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
  251. dbesc($r[0]['parent-uri']),
  252. intval($convid)
  253. );
  254. $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
  255. FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
  256. WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
  257. intval(local_user())
  258. );
  259. }
  260. if (!count($messages)) {
  261. notice(L10n::t('Message not available.') . EOL);
  262. return $o;
  263. }
  264. $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
  265. dbesc($r[0]['parent-uri']),
  266. intval(local_user())
  267. );
  268. $tpl = get_markup_template('msg-header.tpl');
  269. $a->page['htmlhead'] .= replace_macros($tpl, [
  270. '$baseurl' => System::baseUrl(true),
  271. '$nickname' => $a->user['nickname'],
  272. '$linkurl' => L10n::t('Please enter a link URL:')
  273. ]);
  274. $tpl = get_markup_template('msg-end.tpl');
  275. $a->page['end'] .= replace_macros($tpl, [
  276. '$baseurl' => System::baseUrl(true),
  277. '$nickname' => $a->user['nickname'],
  278. '$linkurl' => L10n::t('Please enter a link URL:')
  279. ]);
  280. $mails = [];
  281. $seen = 0;
  282. $unknown = false;
  283. foreach ($messages as $message) {
  284. if ($message['unknown'])
  285. $unknown = true;
  286. if ($message['from-url'] == $myprofile) {
  287. $from_url = $myprofile;
  288. $sparkle = '';
  289. } else {
  290. $from_url = Contact::magicLink($message['from-url']);
  291. $sparkle = ' sparkle';
  292. }
  293. $extracted = item_extract_images($message['body']);
  294. if ($extracted['images']) {
  295. $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
  296. }
  297. $from_name_e = $message['from-name'];
  298. $subject_e = $message['title'];
  299. $body_e = Smilies::replace(BBCode::convert($message['body']));
  300. $to_name_e = $message['name'];
  301. $contact = Contact::getDetailsByURL($message['from-url']);
  302. if (isset($contact["thumb"])) {
  303. $from_photo = $contact["thumb"];
  304. } else {
  305. $from_photo = $message['from-photo'];
  306. }
  307. $mails[] = [
  308. 'id' => $message['id'],
  309. 'from_name' => $from_name_e,
  310. 'from_url' => $from_url,
  311. 'from_addr' => $contact['addr'],
  312. 'sparkle' => $sparkle,
  313. 'from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
  314. 'subject' => $subject_e,
  315. 'body' => $body_e,
  316. 'delete' => L10n::t('Delete message'),
  317. 'to_name' => $to_name_e,
  318. 'date' => DateTimeFormat::local($message['created'], L10n::t('D, d M Y - g:i A')),
  319. 'ago' => Temporal::getRelativeDate($message['created']),
  320. ];
  321. $seen = $message['seen'];
  322. }
  323. $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
  324. $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
  325. $tpl = get_markup_template('mail_display.tpl');
  326. $o = replace_macros($tpl, [
  327. '$thread_id' => $a->argv[1],
  328. '$thread_subject' => $message['title'],
  329. '$thread_seen' => $seen,
  330. '$delete' => L10n::t('Delete conversation'),
  331. '$canreply' => (($unknown) ? false : '1'),
  332. '$unknown_text' => L10n::t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
  333. '$mails' => $mails,
  334. // reply
  335. '$header' => L10n::t('Send Reply'),
  336. '$to' => L10n::t('To:'),
  337. '$showinputs' => '',
  338. '$subject' => L10n::t('Subject:'),
  339. '$subjtxt' => $message['title'],
  340. '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
  341. '$yourmessage' => L10n::t('Your message:'),
  342. '$text' => '',
  343. '$select' => $select,
  344. '$parent' => $parent,
  345. '$upload' => L10n::t('Upload photo'),
  346. '$insert' => L10n::t('Insert web link'),
  347. '$submit' => L10n::t('Submit'),
  348. '$wait' => L10n::t('Please wait')
  349. ]);
  350. return $o;
  351. }
  352. }
  353. function get_messages($user, $lstart, $lend)
  354. {
  355. //TODO: rewritte with a sub-query to get the first message of each private thread with certainty
  356. return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
  357. ANY_VALUE(`mail`.`id`) AS `id`, ANY_VALUE(`mail`.`uid`) AS `uid`, ANY_VALUE(`mail`.`guid`) AS `guid`,
  358. ANY_VALUE(`mail`.`from-name`) AS `from-name`, ANY_VALUE(`mail`.`from-photo`) AS `from-photo`,
  359. ANY_VALUE(`mail`.`from-url`) AS `from-url`, ANY_VALUE(`mail`.`contact-id`) AS `contact-id`,
  360. ANY_VALUE(`mail`.`convid`) AS `convid`, ANY_VALUE(`mail`.`title`) AS `title`, ANY_VALUE(`mail`.`body`) AS `body`,
  361. ANY_VALUE(`mail`.`seen`) AS `seen`, ANY_VALUE(`mail`.`reply`) AS `reply`, ANY_VALUE(`mail`.`replied`) AS `replied`,
  362. ANY_VALUE(`mail`.`unknown`) AS `unknown`, ANY_VALUE(`mail`.`uri`) AS `uri`,
  363. `mail`.`parent-uri`,
  364. ANY_VALUE(`mail`.`created`) AS `created`, ANY_VALUE(`contact`.`name`) AS `name`, ANY_VALUE(`contact`.`url`) AS `url`,
  365. ANY_VALUE(`contact`.`thumb`) AS `thumb`, ANY_VALUE(`contact`.`network`) AS `network`,
  366. count( * ) as `count`
  367. FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
  368. WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
  369. intval($user), intval($lstart), intval($lend)
  370. );
  371. }
  372. function render_messages(array $msg, $t)
  373. {
  374. $a = get_app();
  375. $tpl = get_markup_template($t);
  376. $rslt = '';
  377. $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
  378. foreach ($msg as $rr) {
  379. if ($rr['unknown']) {
  380. $participants = L10n::t("Unknown sender - %s", $rr['from-name']);
  381. } elseif (link_compare($rr['from-url'], $myprofile)) {
  382. $participants = L10n::t("You and %s", $rr['name']);
  383. } else {
  384. $participants = L10n::t("%s and You", $rr['from-name']);
  385. }
  386. $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
  387. $body_e = $rr['body'];
  388. $to_name_e = $rr['name'];
  389. $contact = Contact::getDetailsByURL($rr['url']);
  390. if (isset($contact["thumb"])) {
  391. $from_photo = $contact["thumb"];
  392. } else {
  393. $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
  394. }
  395. $rslt .= replace_macros($tpl, [
  396. '$id' => $rr['id'],
  397. '$from_name' => $participants,
  398. '$from_url' => Contact::magicLink($rr['url']),
  399. '$from_addr' => $contact['addr'],
  400. '$sparkle' => ' sparkle',
  401. '$from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
  402. '$subject' => $subject_e,
  403. '$delete' => L10n::t('Delete conversation'),
  404. '$body' => $body_e,
  405. '$to_name' => $to_name_e,
  406. '$date' => DateTimeFormat::local($rr['mailcreated'], L10n::t('D, d M Y - g:i A')),
  407. '$ago' => Temporal::getRelativeDate($rr['mailcreated']),
  408. '$seen' => $rr['mailseen'],
  409. '$count' => L10n::tt('%d message', '%d messages', $rr['count']),
  410. ]);
  411. }
  412. return $rslt;
  413. }