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