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.

533 lines
15KB

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