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.

1121 lines
36 KiB

2 years ago
2 years ago
2 years ago
  1. <?php
  2. namespace Friendica\Module;
  3. use Friendica\App;
  4. use Friendica\BaseModule;
  5. use Friendica\Content\ContactSelector;
  6. use Friendica\Content\Nav;
  7. use Friendica\Content\Pager;
  8. use Friendica\Content\Text\BBCode;
  9. use Friendica\Content\Widget;
  10. use Friendica\Core\ACL;
  11. use Friendica\Core\Hook;
  12. use Friendica\Core\L10n;
  13. use Friendica\Core\Protocol;
  14. use Friendica\Core\Renderer;
  15. use Friendica\Core\Worker;
  16. use Friendica\Database\DBA;
  17. use Friendica\DI;
  18. use Friendica\Model;
  19. use Friendica\Module\Security\Login;
  20. use Friendica\Network\HTTPException\BadRequestException;
  21. use Friendica\Network\HTTPException\NotFoundException;
  22. use Friendica\Util\DateTimeFormat;
  23. use Friendica\Util\Proxy as ProxyUtils;
  24. use Friendica\Util\Strings;
  25. /**
  26. * Manages and show Contacts and their content
  27. *
  28. * @brief manages contacts
  29. */
  30. class Contact extends BaseModule
  31. {
  32. private static function batchActions()
  33. {
  34. if (empty($_POST['contact_batch']) || !is_array($_POST['contact_batch'])) {
  35. return;
  36. }
  37. $contacts_id = $_POST['contact_batch'];
  38. $stmt = DBA::select('contact', ['id', 'archive'], ['id' => $contacts_id, 'uid' => local_user(), 'self' => false, 'deleted' => false]);
  39. $orig_records = DBA::toArray($stmt);
  40. $count_actions = 0;
  41. foreach ($orig_records as $orig_record) {
  42. $contact_id = $orig_record['id'];
  43. if (!empty($_POST['contacts_batch_update'])) {
  44. self::updateContactFromPoll($contact_id);
  45. $count_actions++;
  46. }
  47. if (!empty($_POST['contacts_batch_block'])) {
  48. self::blockContact($contact_id);
  49. $count_actions++;
  50. }
  51. if (!empty($_POST['contacts_batch_ignore'])) {
  52. self::ignoreContact($contact_id);
  53. $count_actions++;
  54. }
  55. if (!empty($_POST['contacts_batch_archive'])
  56. && self::archiveContact($contact_id, $orig_record)
  57. ) {
  58. $count_actions++;
  59. }
  60. if (!empty($_POST['contacts_batch_drop'])) {
  61. self::dropContact($orig_record);
  62. $count_actions++;
  63. }
  64. }
  65. if ($count_actions > 0) {
  66. info(L10n::tt('%d contact edited.', '%d contacts edited.', $count_actions));
  67. }
  68. DI::baseUrl()->redirect('contact');
  69. }
  70. public static function post(array $parameters = [])
  71. {
  72. $a = DI::app();
  73. if (!local_user()) {
  74. return;
  75. }
  76. // @TODO: Replace with parameter from router
  77. if ($a->argv[1] === 'batch') {
  78. self::batchActions();
  79. return;
  80. }
  81. // @TODO: Replace with parameter from router
  82. $contact_id = intval($a->argv[1]);
  83. if (!$contact_id) {
  84. return;
  85. }
  86. if (!DBA::exists('contact', ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false])) {
  87. notice(L10n::t('Could not access contact record.') . EOL);
  88. DI::baseUrl()->redirect('contact');
  89. return; // NOTREACHED
  90. }
  91. Hook::callAll('contact_edit_post', $_POST);
  92. $profile_id = intval($_POST['profile-assign'] ?? 0);
  93. if ($profile_id) {
  94. if (!DBA::exists('profile', ['id' => $profile_id, 'uid' => local_user()])) {
  95. notice(L10n::t('Could not locate selected profile.') . EOL);
  96. return;
  97. }
  98. }
  99. $hidden = !empty($_POST['hidden']);
  100. $notify = !empty($_POST['notify']);
  101. $fetch_further_information = intval($_POST['fetch_further_information'] ?? 0);
  102. $ffi_keyword_blacklist = Strings::escapeHtml(trim($_POST['ffi_keyword_blacklist'] ?? ''));
  103. $priority = intval($_POST['poll'] ?? 0);
  104. if ($priority > 5 || $priority < 0) {
  105. $priority = 0;
  106. }
  107. $info = Strings::escapeHtml(trim($_POST['info'] ?? ''));
  108. $r = DBA::update('contact', [
  109. 'profile-id' => $profile_id,
  110. 'priority' => $priority,
  111. 'info' => $info,
  112. 'hidden' => $hidden,
  113. 'notify_new_posts' => $notify,
  114. 'fetch_further_information' => $fetch_further_information,
  115. 'ffi_keyword_blacklist' => $ffi_keyword_blacklist],
  116. ['id' => $contact_id, 'uid' => local_user()]
  117. );
  118. if (DBA::isResult($r)) {
  119. info(L10n::t('Contact updated.') . EOL);
  120. } else {
  121. notice(L10n::t('Failed to update contact record.') . EOL);
  122. }
  123. $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
  124. if (DBA::isResult($contact)) {
  125. $a->data['contact'] = $contact;
  126. }
  127. return;
  128. }
  129. /* contact actions */
  130. private static function updateContactFromPoll($contact_id)
  131. {
  132. $contact = DBA::selectFirst('contact', ['uid', 'url', 'network'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
  133. if (!DBA::isResult($contact)) {
  134. return;
  135. }
  136. $uid = $contact['uid'];
  137. if ($contact['network'] == Protocol::OSTATUS) {
  138. $result = Model\Contact::createFromProbe($uid, $contact['url'], false, $contact['network']);
  139. if ($result['success']) {
  140. DBA::update('contact', ['subhub' => 1], ['id' => $contact_id]);
  141. }
  142. } else {
  143. // pull feed and consume it, which should subscribe to the hub.
  144. Worker::add(PRIORITY_HIGH, 'OnePoll', $contact_id, 'force');
  145. }
  146. }
  147. private static function updateContactFromProbe($contact_id)
  148. {
  149. $contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
  150. if (!DBA::isResult($contact)) {
  151. return;
  152. }
  153. // Update the entry in the contact table
  154. Model\Contact::updateFromProbe($contact_id, '', true);
  155. // Update the entry in the gcontact table
  156. Model\GContact::updateFromProbe($contact['url']);
  157. }
  158. /**
  159. * Toggles the blocked status of a contact identified by id.
  160. *
  161. * @param $contact_id
  162. * @throws \Exception
  163. */
  164. private static function blockContact($contact_id)
  165. {
  166. $blocked = !Model\Contact::isBlockedByUser($contact_id, local_user());
  167. Model\Contact::setBlockedForUser($contact_id, local_user(), $blocked);
  168. }
  169. /**
  170. * Toggles the ignored status of a contact identified by id.
  171. *
  172. * @param $contact_id
  173. * @throws \Exception
  174. */
  175. private static function ignoreContact($contact_id)
  176. {
  177. $ignored = !Model\Contact::isIgnoredByUser($contact_id, local_user());
  178. Model\Contact::setIgnoredForUser($contact_id, local_user(), $ignored);
  179. }
  180. /**
  181. * Toggles the archived status of a contact identified by id.
  182. * If the current status isn't provided, this will always archive the contact.
  183. *
  184. * @param $contact_id
  185. * @param $orig_record
  186. * @return bool
  187. * @throws \Exception
  188. */
  189. private static function archiveContact($contact_id, $orig_record)
  190. {
  191. $archived = empty($orig_record['archive']);
  192. $r = DBA::update('contact', ['archive' => $archived], ['id' => $contact_id, 'uid' => local_user()]);
  193. return DBA::isResult($r);
  194. }
  195. private static function dropContact($orig_record)
  196. {
  197. $owner = Model\User::getOwnerDataById(local_user());
  198. if (!DBA::isResult($owner)) {
  199. return;
  200. }
  201. Model\Contact::terminateFriendship($owner, $orig_record, true);
  202. Model\Contact::remove($orig_record['id']);
  203. }
  204. public static function content(array $parameters = [], $update = 0)
  205. {
  206. if (!local_user()) {
  207. return Login::form($_SERVER['REQUEST_URI']);
  208. }
  209. $a = DI::app();
  210. $nets = $_GET['nets'] ?? '';
  211. $rel = $_GET['rel'] ?? '';
  212. if (empty(DI::page()['aside'])) {
  213. DI::page()['aside'] = '';
  214. }
  215. $contact_id = null;
  216. $contact = null;
  217. // @TODO: Replace with parameter from router
  218. if ($a->argc == 2 && intval($a->argv[1])
  219. || $a->argc == 3 && intval($a->argv[1]) && in_array($a->argv[2], ['posts', 'conversations'])
  220. ) {
  221. $contact_id = intval($a->argv[1]);
  222. $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
  223. if (!DBA::isResult($contact)) {
  224. $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => 0, 'deleted' => false]);
  225. }
  226. // Don't display contacts that are about to be deleted
  227. if ($contact['network'] == Protocol::PHANTOM) {
  228. $contact = false;
  229. }
  230. }
  231. if (DBA::isResult($contact)) {
  232. if ($contact['self']) {
  233. // @TODO: Replace with parameter from router
  234. if (($a->argc == 3) && intval($a->argv[1]) && in_array($a->argv[2], ['posts', 'conversations'])) {
  235. DI::baseUrl()->redirect('profile/' . $contact['nick']);
  236. } else {
  237. DI::baseUrl()->redirect('profile/' . $contact['nick'] . '?tab=profile');
  238. }
  239. }
  240. $a->data['contact'] = $contact;
  241. if (($contact['network'] != '') && ($contact['network'] != Protocol::DFRN)) {
  242. $network_link = Strings::formatNetworkName($contact['network'], $contact['url']);
  243. } else {
  244. $network_link = '';
  245. }
  246. $follow_link = '';
  247. $unfollow_link = '';
  248. if (in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
  249. if ($contact['uid'] && in_array($contact['rel'], [Model\Contact::SHARING, Model\Contact::FRIEND])) {
  250. $unfollow_link = 'unfollow?url=' . urlencode($contact['url']);
  251. } elseif(!$contact['pending']) {
  252. $follow_link = 'follow?url=' . urlencode($contact['url']);
  253. }
  254. }
  255. $wallmessage_link = '';
  256. if ($contact['uid'] && Model\Contact::canReceivePrivateMessages($contact)) {
  257. $wallmessage_link = 'message/new/' . $contact['id'];
  258. }
  259. $vcard_widget = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/vcard.tpl'), [
  260. '$name' => $contact['name'],
  261. '$photo' => $contact['photo'],
  262. '$url' => Model\Contact::magicLinkByContact($contact, $contact['url']),
  263. '$addr' => $contact['addr'] ?? '',
  264. '$network_link' => $network_link,
  265. '$network' => L10n::t('Network:'),
  266. '$account_type' => Model\Contact::getAccountType($contact),
  267. '$follow' => L10n::t('Follow'),
  268. '$follow_link' => $follow_link,
  269. '$unfollow' => L10n::t('Unfollow'),
  270. '$unfollow_link' => $unfollow_link,
  271. '$wallmessage' => L10n::t('Message'),
  272. '$wallmessage_link' => $wallmessage_link,
  273. ]);
  274. $findpeople_widget = '';
  275. $follow_widget = '';
  276. $networks_widget = '';
  277. $rel_widget = '';
  278. } else {
  279. $vcard_widget = '';
  280. $findpeople_widget = Widget::findPeople();
  281. if (isset($_GET['add'])) {
  282. $follow_widget = Widget::follow($_GET['add']);
  283. } else {
  284. $follow_widget = Widget::follow();
  285. }
  286. $networks_widget = Widget::networks($_SERVER['REQUEST_URI'], $nets);
  287. $rel_widget = Widget::contactRels($_SERVER['REQUEST_URI'], $rel);
  288. }
  289. if ($contact['uid'] != 0) {
  290. $groups_widget = Model\Group::sidebarWidget('contact', 'group', 'full', 'everyone', $contact_id);
  291. } else {
  292. $groups_widget = null;
  293. }
  294. DI::page()['aside'] .= $vcard_widget . $findpeople_widget . $follow_widget . $groups_widget . $networks_widget . $rel_widget;
  295. $tpl = Renderer::getMarkupTemplate('contacts-head.tpl');
  296. DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
  297. '$baseurl' => DI::baseUrl()->get(true),
  298. ]);
  299. $sort_type = 0;
  300. $o = '';
  301. Nav::setSelected('contact');
  302. if (!local_user()) {
  303. notice(L10n::t('Permission denied.') . EOL);
  304. return Login::form();
  305. }
  306. if ($a->argc == 3) {
  307. $contact_id = intval($a->argv[1]);
  308. if (!$contact_id) {
  309. throw new BadRequestException();
  310. }
  311. // @TODO: Replace with parameter from router
  312. $cmd = $a->argv[2];
  313. $orig_record = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => [0, local_user()], 'self' => false, 'deleted' => false]);
  314. if (!DBA::isResult($orig_record)) {
  315. throw new NotFoundException(L10n::t('Contact not found'));
  316. }
  317. if ($cmd === 'update' && ($orig_record['uid'] != 0)) {
  318. self::updateContactFromPoll($contact_id);
  319. DI::baseUrl()->redirect('contact/' . $contact_id);
  320. // NOTREACHED
  321. }
  322. if ($cmd === 'updateprofile' && ($orig_record['uid'] != 0)) {
  323. self::updateContactFromProbe($contact_id);
  324. DI::baseUrl()->redirect('crepair/' . $contact_id);
  325. // NOTREACHED
  326. }
  327. if ($cmd === 'block') {
  328. self::blockContact($contact_id);
  329. $blocked = Model\Contact::isBlockedByUser($contact_id, local_user());
  330. info(($blocked ? L10n::t('Contact has been blocked') : L10n::t('Contact has been unblocked')) . EOL);
  331. DI::baseUrl()->redirect('contact/' . $contact_id);
  332. // NOTREACHED
  333. }
  334. if ($cmd === 'ignore') {
  335. self::ignoreContact($contact_id);
  336. $ignored = Model\Contact::isIgnoredByUser($contact_id, local_user());
  337. info(($ignored ? L10n::t('Contact has been ignored') : L10n::t('Contact has been unignored')) . EOL);
  338. DI::baseUrl()->redirect('contact/' . $contact_id);
  339. // NOTREACHED
  340. }
  341. if ($cmd === 'archive' && ($orig_record['uid'] != 0)) {
  342. $r = self::archiveContact($contact_id, $orig_record);
  343. if ($r) {
  344. $archived = (($orig_record['archive']) ? 0 : 1);
  345. info((($archived) ? L10n::t('Contact has been archived') : L10n::t('Contact has been unarchived')) . EOL);
  346. }
  347. DI::baseUrl()->redirect('contact/' . $contact_id);
  348. // NOTREACHED
  349. }
  350. if ($cmd === 'drop' && ($orig_record['uid'] != 0)) {
  351. // Check if we should do HTML-based delete confirmation
  352. if (!empty($_REQUEST['confirm'])) {
  353. // <form> can't take arguments in its 'action' parameter
  354. // so add any arguments as hidden inputs
  355. $query = explode_querystring(DI::args()->getQueryString());
  356. $inputs = [];
  357. foreach ($query['args'] as $arg) {
  358. if (strpos($arg, 'confirm=') === false) {
  359. $arg_parts = explode('=', $arg);
  360. $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
  361. }
  362. }
  363. DI::page()['aside'] = '';
  364. return Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_drop_confirm.tpl'), [
  365. '$header' => L10n::t('Drop contact'),
  366. '$contact' => self::getContactTemplateVars($orig_record),
  367. '$method' => 'get',
  368. '$message' => L10n::t('Do you really want to delete this contact?'),
  369. '$extra_inputs' => $inputs,
  370. '$confirm' => L10n::t('Yes'),
  371. '$confirm_url' => $query['base'],
  372. '$confirm_name' => 'confirmed',
  373. '$cancel' => L10n::t('Cancel'),
  374. ]);
  375. }
  376. // Now check how the user responded to the confirmation query
  377. if (!empty($_REQUEST['canceled'])) {
  378. DI::baseUrl()->redirect('contact');
  379. }
  380. self::dropContact($orig_record);
  381. info(L10n::t('Contact has been removed.') . EOL);
  382. DI::baseUrl()->redirect('contact');
  383. // NOTREACHED
  384. }
  385. if ($cmd === 'posts') {
  386. return self::getPostsHTML($a, $contact_id);
  387. }
  388. if ($cmd === 'conversations') {
  389. return self::getConversationsHMTL($a, $contact_id, $update);
  390. }
  391. }
  392. $_SESSION['return_path'] = DI::args()->getQueryString();
  393. if (!empty($a->data['contact']) && is_array($a->data['contact'])) {
  394. $contact = $a->data['contact'];
  395. DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('contact_head.tpl'), [
  396. '$baseurl' => DI::baseUrl()->get(true),
  397. ]);
  398. $contact['blocked'] = Model\Contact::isBlockedByUser($contact['id'], local_user());
  399. $contact['readonly'] = Model\Contact::isIgnoredByUser($contact['id'], local_user());
  400. $dir_icon = '';
  401. $relation_text = '';
  402. switch ($contact['rel']) {
  403. case Model\Contact::FRIEND:
  404. $dir_icon = 'images/lrarrow.gif';
  405. $relation_text = L10n::t('You are mutual friends with %s');
  406. break;
  407. case Model\Contact::FOLLOWER;
  408. $dir_icon = 'images/larrow.gif';
  409. $relation_text = L10n::t('You are sharing with %s');
  410. break;
  411. case Model\Contact::SHARING;
  412. $dir_icon = 'images/rarrow.gif';
  413. $relation_text = L10n::t('%s is sharing with you');
  414. break;
  415. default:
  416. break;
  417. }
  418. if ($contact['uid'] == 0) {
  419. $relation_text = '';
  420. }
  421. if (!in_array($contact['network'], Protocol::FEDERATED)) {
  422. $relation_text = '';
  423. }
  424. $relation_text = sprintf($relation_text, $contact['name']);
  425. $url = Model\Contact::magicLink($contact['url']);
  426. if (strpos($url, 'redir/') === 0) {
  427. $sparkle = ' class="sparkle" ';
  428. } else {
  429. $sparkle = '';
  430. }
  431. $insecure = L10n::t('Private communications are not available for this contact.');
  432. $last_update = (($contact['last-update'] <= DBA::NULL_DATETIME) ? L10n::t('Never') : DateTimeFormat::local($contact['last-update'], 'D, j M Y, g:i A'));
  433. if ($contact['last-update'] > DBA::NULL_DATETIME) {
  434. $last_update .= ' ' . (($contact['last-update'] <= $contact['success_update']) ? L10n::t('(Update was successful)') : L10n::t('(Update was not successful)'));
  435. }
  436. $lblsuggest = (($contact['network'] === Protocol::DFRN) ? L10n::t('Suggest friends') : '');
  437. $poll_enabled = in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL]);
  438. $nettype = L10n::t('Network type: %s', ContactSelector::networkToName($contact['network'], $contact['url'], $contact['protocol']));
  439. // tabs
  440. $tab_str = self::getTabsHTML($a, $contact, 3);
  441. $lost_contact = (($contact['archive'] && $contact['term-date'] > DBA::NULL_DATETIME && $contact['term-date'] < DateTimeFormat::utcNow()) ? L10n::t('Communications lost with this contact!') : '');
  442. $fetch_further_information = null;
  443. if ($contact['network'] == Protocol::FEED) {
  444. $fetch_further_information = [
  445. 'fetch_further_information',
  446. L10n::t('Fetch further information for feeds'),
  447. $contact['fetch_further_information'],
  448. L10n::t('Fetch information like preview pictures, title and teaser from the feed item. You can activate this if the feed doesn\'t contain much text. Keywords are taken from the meta header in the feed item and are posted as hash tags.'),
  449. [
  450. '0' => L10n::t('Disabled'),
  451. '1' => L10n::t('Fetch information'),
  452. '3' => L10n::t('Fetch keywords'),
  453. '2' => L10n::t('Fetch information and keywords')
  454. ]
  455. ];
  456. }
  457. $poll_interval = null;
  458. if (in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
  459. $poll_interval = ContactSelector::pollInterval($contact['priority'], !$poll_enabled);
  460. }
  461. $profile_select = null;
  462. if ($contact['network'] == Protocol::DFRN) {
  463. $profile_select = ContactSelector::profileAssign($contact['profile-id'], $contact['network'] !== Protocol::DFRN);
  464. }
  465. // Load contactact related actions like hide, suggest, delete and others
  466. $contact_actions = self::getContactActions($contact);
  467. if ($contact['uid'] != 0) {
  468. $lbl_vis1 = L10n::t('Profile Visibility');
  469. $lbl_info1 = L10n::t('Contact Information / Notes');
  470. $contact_settings_label = L10n::t('Contact Settings');
  471. } else {
  472. $lbl_vis1 = null;
  473. $lbl_info1 = null;
  474. $contact_settings_label = null;
  475. }
  476. $tpl = Renderer::getMarkupTemplate('contact_edit.tpl');
  477. $o .= Renderer::replaceMacros($tpl, [
  478. '$header' => L10n::t('Contact'),
  479. '$tab_str' => $tab_str,
  480. '$submit' => L10n::t('Submit'),
  481. '$lbl_vis1' => $lbl_vis1,
  482. '$lbl_vis2' => L10n::t('Please choose the profile you would like to display to %s when viewing your profile securely.', $contact['name']),
  483. '$lbl_info1' => $lbl_info1,
  484. '$lbl_info2' => L10n::t('Their personal note'),
  485. '$reason' => trim(Strings::escapeTags($contact['reason'])),
  486. '$infedit' => L10n::t('Edit contact notes'),
  487. '$common_link' => 'common/loc/' . local_user() . '/' . $contact['id'],
  488. '$relation_text' => $relation_text,
  489. '$visit' => L10n::t('Visit %s\'s profile [%s]', $contact['name'], $contact['url']),
  490. '$blockunblock' => L10n::t('Block/Unblock contact'),
  491. '$ignorecont' => L10n::t('Ignore contact'),
  492. '$lblcrepair' => L10n::t('Repair URL settings'),
  493. '$lblrecent' => L10n::t('View conversations'),
  494. '$lblsuggest' => $lblsuggest,
  495. '$nettype' => $nettype,
  496. '$poll_interval' => $poll_interval,
  497. '$poll_enabled' => $poll_enabled,
  498. '$lastupdtext' => L10n::t('Last update:'),
  499. '$lost_contact' => $lost_contact,
  500. '$updpub' => L10n::t('Update public posts'),
  501. '$last_update' => $last_update,
  502. '$udnow' => L10n::t('Update now'),
  503. '$profile_select' => $profile_select,
  504. '$contact_id' => $contact['id'],
  505. '$block_text' => ($contact['blocked'] ? L10n::t('Unblock') : L10n::t('Block')),
  506. '$ignore_text' => ($contact['readonly'] ? L10n::t('Unignore') : L10n::t('Ignore')),
  507. '$insecure' => (in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA]) ? '' : $insecure),
  508. '$info' => $contact['info'],
  509. '$cinfo' => ['info', '', $contact['info'], ''],
  510. '$blocked' => ($contact['blocked'] ? L10n::t('Currently blocked') : ''),
  511. '$ignored' => ($contact['readonly'] ? L10n::t('Currently ignored') : ''),
  512. '$archived' => ($contact['archive'] ? L10n::t('Currently archived') : ''),
  513. '$pending' => ($contact['pending'] ? L10n::t('Awaiting connection acknowledge') : ''),
  514. '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($contact['hidden'] == 1), L10n::t('Replies/likes to your public posts <strong>may</strong> still be visible')],
  515. '$notify' => ['notify', L10n::t('Notification for new posts'), ($contact['notify_new_posts'] == 1), L10n::t('Send a notification of every new post of this contact')],
  516. '$fetch_further_information' => $fetch_further_information,
  517. '$ffi_keyword_blacklist' => ['ffi_keyword_blacklist', L10n::t('Blacklisted keywords'), $contact['ffi_keyword_blacklist'], L10n::t('Comma separated list of keywords that should not be converted to hashtags, when "Fetch information and keywords" is selected')],
  518. '$photo' => $contact['photo'],
  519. '$name' => $contact['name'],
  520. '$dir_icon' => $dir_icon,
  521. '$sparkle' => $sparkle,
  522. '$url' => $url,
  523. '$profileurllabel'=> L10n::t('Profile URL'),
  524. '$profileurl' => $contact['url'],
  525. '$account_type' => Model\Contact::getAccountType($contact),
  526. '$location' => BBCode::convert($contact['location']),
  527. '$location_label' => L10n::t('Location:'),
  528. '$xmpp' => BBCode::convert($contact['xmpp']),
  529. '$xmpp_label' => L10n::t('XMPP:'),
  530. '$about' => BBCode::convert($contact['about'], false),
  531. '$about_label' => L10n::t('About:'),
  532. '$keywords' => $contact['keywords'],
  533. '$keywords_label' => L10n::t('Tags:'),
  534. '$contact_action_button' => L10n::t('Actions'),
  535. '$contact_actions'=> $contact_actions,
  536. '$contact_status' => L10n::t('Status'),
  537. '$contact_settings_label' => $contact_settings_label,
  538. '$contact_profile_label' => L10n::t('Profile'),
  539. ]);
  540. $arr = ['contact' => $contact, 'output' => $o];
  541. Hook::callAll('contact_edit', $arr);
  542. return $arr['output'];
  543. }
  544. $select_uid = local_user();
  545. // @TODO: Replace with parameter from router
  546. $type = $a->argv[1] ?? '';
  547. switch ($type) {
  548. case 'blocked':
  549. $sql_extra = sprintf(" AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = %d and `user-contact`.`blocked`)", intval(local_user()));
  550. $select_uid = 0;
  551. break;
  552. case 'hidden':
  553. $sql_extra = " AND `hidden` AND NOT `blocked` AND NOT `pending`";
  554. break;
  555. case 'ignored':
  556. $sql_extra = sprintf(" AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = %d and `user-contact`.`ignored`)", intval(local_user()));
  557. $select_uid = 0;
  558. break;
  559. case 'archived':
  560. $sql_extra = " AND `archive` AND NOT `blocked` AND NOT `pending`";
  561. break;
  562. case 'pending':
  563. $sql_extra = sprintf(" AND `pending` AND NOT `archive` AND ((`rel` = %d)
  564. OR EXISTS (SELECT `id` FROM `intro` WHERE `contact-id` = `contact`.`id` AND NOT `ignore`))", Model\Contact::SHARING);
  565. break;
  566. default:
  567. $sql_extra = " AND NOT `archive` AND NOT `blocked` AND NOT `pending`";
  568. }
  569. $sql_extra .= sprintf(" AND `network` != '%s' ", Protocol::PHANTOM);
  570. $search = Strings::escapeTags(trim($_GET['search'] ?? ''));
  571. $nets = Strings::escapeTags(trim($_GET['nets'] ?? ''));
  572. $rel = Strings::escapeTags(trim($_GET['rel'] ?? ''));
  573. $tabs = [
  574. [
  575. 'label' => L10n::t('All Contacts'),
  576. 'url' => 'contact',
  577. 'sel' => !$type ? 'active' : '',
  578. 'title' => L10n::t('Show all contacts'),
  579. 'id' => 'showall-tab',
  580. 'accesskey' => 'l',
  581. ],
  582. [
  583. 'label' => L10n::t('Pending'),
  584. 'url' => 'contact/pending',
  585. 'sel' => $type == 'pending' ? 'active' : '',
  586. 'title' => L10n::t('Only show pending contacts'),
  587. 'id' => 'showpending-tab',
  588. 'accesskey' => 'p',
  589. ],
  590. [
  591. 'label' => L10n::t('Blocked'),
  592. 'url' => 'contact/blocked',
  593. 'sel' => $type == 'blocked' ? 'active' : '',
  594. 'title' => L10n::t('Only show blocked contacts'),
  595. 'id' => 'showblocked-tab',
  596. 'accesskey' => 'b',
  597. ],
  598. [
  599. 'label' => L10n::t('Ignored'),
  600. 'url' => 'contact/ignored',
  601. 'sel' => $type == 'ignored' ? 'active' : '',
  602. 'title' => L10n::t('Only show ignored contacts'),
  603. 'id' => 'showignored-tab',
  604. 'accesskey' => 'i',
  605. ],
  606. [
  607. 'label' => L10n::t('Archived'),
  608. 'url' => 'contact/archived',
  609. 'sel' => $type == 'archived' ? 'active' : '',
  610. 'title' => L10n::t('Only show archived contacts'),
  611. 'id' => 'showarchived-tab',
  612. 'accesskey' => 'y',
  613. ],
  614. [
  615. 'label' => L10n::t('Hidden'),
  616. 'url' => 'contact/hidden',
  617. 'sel' => $type == 'hidden' ? 'active' : '',
  618. 'title' => L10n::t('Only show hidden contacts'),
  619. 'id' => 'showhidden-tab',
  620. 'accesskey' => 'h',
  621. ],
  622. [
  623. 'label' => L10n::t('Groups'),
  624. 'url' => 'group',
  625. 'sel' => '',
  626. 'title' => L10n::t('Organize your contact groups'),
  627. 'id' => 'contactgroups-tab',
  628. 'accesskey' => 'e',
  629. ],
  630. ];
  631. $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
  632. $t = Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
  633. $total = 0;
  634. $searching = false;
  635. $search_hdr = null;
  636. if ($search) {
  637. $searching = true;
  638. $search_hdr = $search;
  639. $search_txt = DBA::escape(Strings::protectSprintf(preg_quote($search)));
  640. $sql_extra .= " AND (name REGEXP '$search_txt' OR url REGEXP '$search_txt' OR nick REGEXP '$search_txt') ";
  641. }
  642. if ($nets) {
  643. $sql_extra .= sprintf(" AND network = '%s' ", DBA::escape($nets));
  644. }
  645. switch ($rel) {
  646. case 'followers': $sql_extra .= " AND `rel` IN (1, 3)"; break;
  647. case 'following': $sql_extra .= " AND `rel` IN (2, 3)"; break;
  648. case 'mutuals': $sql_extra .= " AND `rel` = 3"; break;
  649. }
  650. $sql_extra .= " AND NOT `deleted` ";
  651. $sql_extra2 = ((($sort_type > 0) && ($sort_type <= Model\Contact::FRIEND)) ? sprintf(" AND `rel` = %d ", intval($sort_type)) : '');
  652. $sql_extra3 = Widget::unavailableNetworks();
  653. $r = q("SELECT COUNT(*) AS `total` FROM `contact`
  654. WHERE `uid` = %d AND `self` = 0 $sql_extra $sql_extra2 $sql_extra3",
  655. intval($select_uid)
  656. );
  657. if (DBA::isResult($r)) {
  658. $total = $r[0]['total'];
  659. }
  660. $pager = new Pager(DI::args()->getQueryString());
  661. $contacts = [];
  662. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 $sql_extra $sql_extra2 $sql_extra3 ORDER BY `name` ASC LIMIT %d , %d ",
  663. intval($select_uid),
  664. $pager->getStart(),
  665. $pager->getItemsPerPage()
  666. );
  667. if (DBA::isResult($r)) {
  668. foreach ($r as $rr) {
  669. $rr['blocked'] = Model\Contact::isBlockedByUser($rr['id'], local_user());
  670. $rr['readonly'] = Model\Contact::isIgnoredByUser($rr['id'], local_user());
  671. $contacts[] = self::getContactTemplateVars($rr);
  672. }
  673. }
  674. switch ($rel) {
  675. case 'followers': $header = L10n::t('Followers'); break;
  676. case 'following': $header = L10n::t('Following'); break;
  677. case 'mutuals': $header = L10n::t('Mutual friends'); break;
  678. default: $header = L10n::t('Contacts');
  679. }
  680. switch ($type) {
  681. case 'pending': $header .= ' - ' . L10n::t('Pending'); break;
  682. case 'blocked': $header .= ' - ' . L10n::t('Blocked'); break;
  683. case 'hidden': $header .= ' - ' . L10n::t('Hidden'); break;
  684. case 'ignored': $header .= ' - ' . L10n::t('Ignored'); break;
  685. case 'archived': $header .= ' - ' . L10n::t('Archived'); break;
  686. }
  687. $header .= $nets ? ' - ' . ContactSelector::networkToName($nets) : '';
  688. $tpl = Renderer::getMarkupTemplate('contacts-template.tpl');
  689. $o .= Renderer::replaceMacros($tpl, [
  690. '$header' => $header,
  691. '$tabs' => $t,
  692. '$total' => $total,
  693. '$search' => $search_hdr,
  694. '$desc' => L10n::t('Search your contacts'),
  695. '$finding' => $searching ? L10n::t('Results for: %s', $search) : '',
  696. '$submit' => L10n::t('Find'),
  697. '$cmd' => DI::args()->getCommand(),
  698. '$contacts' => $contacts,
  699. '$contact_drop_confirm' => L10n::t('Do you really want to delete this contact?'),
  700. 'multiselect' => 1,
  701. '$batch_actions' => [
  702. 'contacts_batch_update' => L10n::t('Update'),
  703. 'contacts_batch_block' => L10n::t('Block') . '/' . L10n::t('Unblock'),
  704. 'contacts_batch_ignore' => L10n::t('Ignore') . '/' . L10n::t('Unignore'),
  705. 'contacts_batch_archive' => L10n::t('Archive') . '/' . L10n::t('Unarchive'),
  706. 'contacts_batch_drop' => L10n::t('Delete'),
  707. ],
  708. '$h_batch_actions' => L10n::t('Batch Actions'),
  709. '$paginate' => $pager->renderFull($total),
  710. ]);
  711. return $o;
  712. }
  713. /**
  714. * @brief List of pages for the Contact TabBar
  715. *
  716. * Available Pages are 'Status', 'Profile', 'Contacts' and 'Common Friends'
  717. *
  718. * @param App $a
  719. * @param array $contact The contact array
  720. * @param int $active_tab 1 if tab should be marked as active
  721. *
  722. * @return string HTML string of the contact page tabs buttons.
  723. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  724. */
  725. public static function getTabsHTML($a, $contact, $active_tab)
  726. {
  727. // tabs
  728. $tabs = [
  729. [
  730. 'label' => L10n::t('Status'),
  731. 'url' => "contact/" . $contact['id'] . "/conversations",
  732. 'sel' => (($active_tab == 1) ? 'active' : ''),
  733. 'title' => L10n::t('Conversations started by this contact'),
  734. 'id' => 'status-tab',
  735. 'accesskey' => 'm',
  736. ],
  737. [
  738. 'label' => L10n::t('Posts and Comments'),
  739. 'url' => "contact/" . $contact['id'] . "/posts",
  740. 'sel' => (($active_tab == 2) ? 'active' : ''),
  741. 'title' => L10n::t('Status Messages and Posts'),
  742. 'id' => 'posts-tab',
  743. 'accesskey' => 'p',
  744. ],
  745. [
  746. 'label' => L10n::t('Profile'),
  747. 'url' => "contact/" . $contact['id'],
  748. 'sel' => (($active_tab == 3) ? 'active' : ''),
  749. 'title' => L10n::t('Profile Details'),
  750. 'id' => 'profile-tab',
  751. 'accesskey' => 'o',
  752. ]
  753. ];
  754. // Show this tab only if there is visible friend list
  755. $x = Model\GContact::countAllFriends(local_user(), $contact['id']);
  756. if ($x) {
  757. $tabs[] = ['label' => L10n::t('Contacts'),
  758. 'url' => "allfriends/" . $contact['id'],
  759. 'sel' => (($active_tab == 4) ? 'active' : ''),
  760. 'title' => L10n::t('View all contacts'),
  761. 'id' => 'allfriends-tab',
  762. 'accesskey' => 't'];
  763. }
  764. // Show this tab only if there is visible common friend list
  765. $common = Model\GContact::countCommonFriends(local_user(), $contact['id']);
  766. if ($common) {
  767. $tabs[] = ['label' => L10n::t('Common Friends'),
  768. 'url' => "common/loc/" . local_user() . "/" . $contact['id'],
  769. 'sel' => (($active_tab == 5) ? 'active' : ''),
  770. 'title' => L10n::t('View all common friends'),
  771. 'id' => 'common-loc-tab',
  772. 'accesskey' => 'd'
  773. ];
  774. }
  775. if (!empty($contact['uid'])) {
  776. $tabs[] = ['label' => L10n::t('Advanced'),
  777. 'url' => 'crepair/' . $contact['id'],
  778. 'sel' => (($active_tab == 6) ? 'active' : ''),
  779. 'title' => L10n::t('Advanced Contact Settings'),
  780. 'id' => 'advanced-tab',
  781. 'accesskey' => 'r'
  782. ];
  783. }
  784. $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
  785. $tab_str = Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
  786. return $tab_str;
  787. }
  788. private static function getConversationsHMTL($a, $contact_id, $update)
  789. {
  790. $o = '';
  791. if (!$update) {
  792. // We need the editor here to be able to reshare an item.
  793. if (local_user()) {
  794. $x = [
  795. 'is_owner' => true,
  796. 'allow_location' => $a->user['allow_location'],
  797. 'default_location' => $a->user['default-location'],
  798. 'nickname' => $a->user['nickname'],
  799. 'lockstate' => (is_array($a->user) && (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) || strlen($a->user['deny_cid']) || strlen($a->user['deny_gid'])) ? 'lock' : 'unlock'),
  800. 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true),
  801. 'bang' => '',
  802. 'visitor' => 'block',
  803. 'profile_uid' => local_user(),
  804. ];
  805. $o = status_editor($a, $x, 0, true);
  806. }
  807. }
  808. $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]);
  809. if (!$update) {
  810. $o .= self::getTabsHTML($a, $contact, 1);
  811. }
  812. if (DBA::isResult($contact)) {
  813. DI::page()['aside'] = '';
  814. $profiledata = Model\Contact::getDetailsByURL($contact['url']);
  815. Model\Profile::load($a, '', 0, $profiledata, true);
  816. $o .= Model\Contact::getPostsFromUrl($contact['url'], true, $update);
  817. }
  818. return $o;
  819. }
  820. private static function getPostsHTML($a, $contact_id)
  821. {
  822. $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]);
  823. $o = self::getTabsHTML($a, $contact, 2);
  824. if (DBA::isResult($contact)) {
  825. DI::page()['aside'] = '';
  826. $profiledata = Model\Contact::getDetailsByURL($contact['url']);
  827. if (local_user() && in_array($profiledata['network'], Protocol::FEDERATED)) {
  828. $profiledata['remoteconnect'] = DI::baseUrl() . '/follow?url=' . urlencode($profiledata['url']);
  829. }
  830. Model\Profile::load($a, '', 0, $profiledata, true);
  831. $o .= Model\Contact::getPostsFromUrl($contact['url']);
  832. }
  833. return $o;
  834. }
  835. public static function getContactTemplateVars(array $rr)
  836. {
  837. $dir_icon = '';
  838. $alt_text = '';
  839. if (!empty($rr['uid']) && !empty($rr['rel'])) {
  840. switch ($rr['rel']) {
  841. case Model\Contact::FRIEND:
  842. $dir_icon = 'images/lrarrow.gif';
  843. $alt_text = L10n::t('Mutual Friendship');
  844. break;
  845. case Model\Contact::FOLLOWER;
  846. $dir_icon = 'images/larrow.gif';
  847. $alt_text = L10n::t('is a fan of yours');
  848. break;
  849. case Model\Contact::SHARING;
  850. $dir_icon = 'images/rarrow.gif';
  851. $alt_text = L10n::t('you are a fan of');
  852. break;
  853. default:
  854. break;
  855. }
  856. }
  857. $url = Model\Contact::magicLink($rr['url']);
  858. if (strpos($url, 'redir/') === 0) {
  859. $sparkle = ' class="sparkle" ';
  860. } else {
  861. $sparkle = '';
  862. }
  863. if ($rr['pending']) {
  864. if (in_array($rr['rel'], [Model\Contact::FRIEND, Model\Contact::SHARING])) {
  865. $alt_text = L10n::t('Pending outgoing contact request');
  866. } else {
  867. $alt_text = L10n::t('Pending incoming contact request');
  868. }
  869. }
  870. if ($rr['self']) {
  871. $dir_icon = 'images/larrow.gif';
  872. $alt_text = L10n::t('This is you');
  873. $url = $rr['url'];
  874. $sparkle = '';
  875. }
  876. return [
  877. 'img_hover' => L10n::t('Visit %s\'s profile [%s]', $rr['name'], $rr['url']),
  878. 'edit_hover'=> L10n::t('Edit contact'),
  879. 'photo_menu'=> Model\Contact::photoMenu($rr),
  880. 'id' => $rr['id'],
  881. 'alt_text' => $alt_text,
  882. 'dir_icon' => $dir_icon,
  883. 'thumb' => ProxyUtils::proxifyUrl($rr['thumb'], false, ProxyUtils::SIZE_THUMB),
  884. 'name' => $rr['name'],
  885. 'username' => $rr['name'],
  886. 'account_type' => Model\Contact::getAccountType($rr),
  887. 'sparkle' => $sparkle,
  888. 'itemurl' => ($rr['addr'] ?? '') ?: $rr['url'],
  889. 'url' => $url,
  890. 'network' => ContactSelector::networkToName($rr['network'], $rr['url'], $rr['protocol']),
  891. 'nick' => $rr['nick'],
  892. ];
  893. }
  894. /**
  895. * @brief Gives a array with actions which can performed to a given contact
  896. *
  897. * This includes actions like e.g. 'block', 'hide', 'archive', 'delete' and others
  898. *
  899. * @param array $contact Data about the Contact
  900. * @return array with contact related actions
  901. */
  902. private static function getContactActions($contact)
  903. {
  904. $poll_enabled = in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL]);
  905. $contact_actions = [];
  906. // Provide friend suggestion only for Friendica contacts
  907. if ($contact['network'] === Protocol::DFRN) {
  908. $contact_actions['suggest'] = [
  909. 'label' => L10n::t('Suggest friends'),
  910. 'url' => 'fsuggest/' . $contact['id'],
  911. 'title' => '',
  912. 'sel' => '',
  913. 'id' => 'suggest',
  914. ];
  915. }
  916. if ($poll_enabled) {
  917. $contact_actions['update'] = [
  918. 'label' => L10n::t('Update now'),
  919. 'url' => 'contact/' . $contact['id'] . '/update',
  920. 'title' => '',
  921. 'sel' => '',
  922. 'id' => 'update',
  923. ];
  924. }
  925. $contact_actions['block'] = [
  926. 'label' => (intval($contact['blocked']) ? L10n::t('Unblock') : L10n::t('Block')),
  927. 'url' => 'contact/' . $contact['id'] . '/block',
  928. 'title' => L10n::t('Toggle Blocked status'),
  929. 'sel' => (intval($contact['blocked']) ? 'active' : ''),
  930. 'id' => 'toggle-block',
  931. ];
  932. $contact_actions['ignore'] = [
  933. 'label' => (intval($contact['readonly']) ? L10n::t('Unignore') : L10n::t('Ignore')),
  934. 'url' => 'contact/' . $contact['id'] . '/ignore',
  935. 'title' => L10n::t('Toggle Ignored status'),
  936. 'sel' => (intval($contact['readonly']) ? 'active' : ''),
  937. 'id' => 'toggle-ignore',
  938. ];
  939. if ($contact['uid'] != 0) {
  940. $contact_actions['archive'] = [
  941. 'label' => (intval($contact['archive']) ? L10n::t('Unarchive') : L10n::t('Archive')),
  942. 'url' => 'contact/' . $contact['id'] . '/archive',
  943. 'title' => L10n::t('Toggle Archive status'),
  944. 'sel' => (intval($contact['archive']) ? 'active' : ''),
  945. 'id' => 'toggle-archive',
  946. ];
  947. $contact_actions['delete'] = [
  948. 'label' => L10n::t('Delete'),
  949. 'url' => 'contact/' . $contact['id'] . '/drop',
  950. 'title' => L10n::t('Delete contact'),
  951. 'sel' => '',
  952. 'id' => 'delete',
  953. ];
  954. }
  955. return $contact_actions;
  956. }
  957. }