]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
Merge remote-tracking branch 'upstream/develop' into develop
[friendica.git] / mod / contacts.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\System;
5 use Friendica\Network\Probe;
6
7 require_once 'include/Contact.php';
8 require_once 'include/socgraph.php';
9 require_once 'include/contact_selectors.php';
10 require_once 'include/probe.php';
11 require_once 'mod/proxy.php';
12 require_once 'include/Photo.php';
13
14 function contacts_init(App $a) {
15         if (! local_user()) {
16                 return;
17         }
18
19         $contact_id = 0;
20
21         if((($a->argc == 2) && intval($a->argv[1])) || (($a->argc == 3) && intval($a->argv[1]) && ($a->argv[2] == "posts"))) {
22                 $contact_id = intval($a->argv[1]);
23                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d and `id` = %d LIMIT 1",
24                         intval(local_user()),
25                         intval($contact_id)
26                 );
27                 if (! dbm::is_result($r)) {
28                         $contact_id = 0;
29                 }
30         }
31
32         require_once 'include/group.php';
33         require_once 'include/contact_widgets.php';
34
35         if ($_GET['nets'] == "all") {
36                 $_GET['nets'] = "";
37         }
38
39         if (! x($a->page,'aside')) {
40                 $a->page['aside'] = '';
41         }
42
43         if ($contact_id) {
44                 $a->data['contact'] = $r[0];
45
46                 if (($a->data['contact']['network'] != "") && ($a->data['contact']['network'] != NETWORK_DFRN)) {
47                         $networkname = format_network_name($a->data['contact']['network'],$a->data['contact']['url']);
48                 } else {
49                         $networkname = '';
50                 }
51
52                 /// @TODO Add nice spaces
53                 $vcard_widget = replace_macros(get_markup_template("vcard-widget.tpl"),array(
54                         '$name' => htmlentities($a->data['contact']['name']),
55                         '$photo' => $a->data['contact']['photo'],
56                         '$url' => ($a->data['contact']['network'] == NETWORK_DFRN) ? "redir/".$a->data['contact']['id'] : $a->data['contact']['url'],
57                         '$addr' => (($a->data['contact']['addr'] != "") ? ($a->data['contact']['addr']) : ""),
58                         '$network_name' => $networkname,
59                         '$network' => t('Network:'),
60                         '$account_type' => account_type($a->data['contact'])
61                 ));
62
63                 $finpeople_widget = '';
64                 $follow_widget = '';
65                 $networks_widget = '';
66         } else {
67                 $vcard_widget = '';
68                 $networks_widget .= networks_widget('contacts',$_GET['nets']);
69                 if (isset($_GET['add'])) {
70                         $follow_widget = follow_widget($_GET['add']);
71                 } else {
72                         $follow_widget = follow_widget();
73                 }
74
75                 $findpeople_widget .= findpeople_widget();
76         }
77
78         $groups_widget .= group_side('contacts','group','full',0,$contact_id);
79
80         $a->page['aside'] .= replace_macros(get_markup_template("contacts-widget-sidebar.tpl"),array(
81                 '$vcard_widget' => $vcard_widget,
82                 '$findpeople_widget' => $findpeople_widget,
83                 '$follow_widget' => $follow_widget,
84                 '$groups_widget' => $groups_widget,
85                 '$networks_widget' => $networks_widget
86         ));
87
88         $base = System::baseUrl();
89         $tpl = get_markup_template("contacts-head.tpl");
90         $a->page['htmlhead'] .= replace_macros($tpl,array(
91                 '$baseurl' => System::baseUrl(true),
92                 '$base' => $base
93         ));
94
95         $tpl = get_markup_template("contacts-end.tpl");
96         $a->page['end'] .= replace_macros($tpl,array(
97                 '$baseurl' => System::baseUrl(true),
98                 '$base' => $base
99         ));
100
101
102 }
103
104 function contacts_batch_actions(App $a) {
105         $contacts_id = $_POST['contact_batch'];
106         if (!is_array($contacts_id)) return;
107
108         $orig_records = q("SELECT * FROM `contact` WHERE `id` IN (%s) AND `uid` = %d AND `self` = 0",
109                 implode(",", $contacts_id),
110                 intval(local_user())
111         );
112
113         $count_actions=0;
114         foreach($orig_records as $orig_record) {
115                 $contact_id = $orig_record['id'];
116                 if (x($_POST, 'contacts_batch_update')) {
117                         _contact_update($contact_id);
118                         $count_actions++;
119                 }
120                 if (x($_POST, 'contacts_batch_block')) {
121                         $r  = _contact_block($contact_id, $orig_record);
122                         if ($r) $count_actions++;
123                 }
124                 if (x($_POST, 'contacts_batch_ignore')) {
125                         $r = _contact_ignore($contact_id, $orig_record);
126                         if ($r) $count_actions++;
127                 }
128                 if (x($_POST, 'contacts_batch_archive')) {
129                         $r = _contact_archive($contact_id, $orig_record);
130                         if ($r) $count_actions++;
131                 }
132                 if (x($_POST, 'contacts_batch_drop')) {
133                         _contact_drop($contact_id, $orig_record);
134                         $count_actions++;
135                 }
136         }
137         if ($count_actions>0) {
138                 info ( sprintf( tt("%d contact edited.", "%d contacts edited.", $count_actions), $count_actions) );
139         }
140
141         if (x($_SESSION,'return_url')) {
142                 goaway('' . $_SESSION['return_url']);
143         }
144         else {
145                 goaway('contacts');
146         }
147
148 }
149
150
151 function contacts_post(App $a) {
152
153         if (! local_user()) {
154                 return;
155         }
156
157         if ($a->argv[1]==="batch") {
158                 contacts_batch_actions($a);
159                 return;
160         }
161
162         $contact_id = intval($a->argv[1]);
163         if (! $contact_id) {
164                 return;
165         }
166
167         $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
168                 intval($contact_id),
169                 intval(local_user())
170         );
171
172         if (! count($orig_record)) {
173                 notice( t('Could not access contact record.') . EOL);
174                 goaway('contacts');
175                 return; // NOTREACHED
176         }
177
178         call_hooks('contact_edit_post', $_POST);
179
180         $profile_id = intval($_POST['profile-assign']);
181         if ($profile_id) {
182                 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
183                         intval($profile_id),
184                         intval(local_user())
185                 );
186                 if (! dbm::is_result($r)) {
187                         notice( t('Could not locate selected profile.') . EOL);
188                         return;
189                 }
190         }
191
192         $hidden = intval($_POST['hidden']);
193
194         $notify = intval($_POST['notify']);
195
196         $fetch_further_information = intval($_POST['fetch_further_information']);
197
198         $ffi_keyword_blacklist = escape_tags(trim($_POST['ffi_keyword_blacklist']));
199
200         $priority = intval($_POST['poll']);
201         if($priority > 5 || $priority < 0)
202                 $priority = 0;
203
204         $info = escape_tags(trim($_POST['info']));
205
206         $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `info` = '%s',
207                 `hidden` = %d, `notify_new_posts` = %d, `fetch_further_information` = %d,
208                 `ffi_keyword_blacklist` = '%s' WHERE `id` = %d AND `uid` = %d",
209                 intval($profile_id),
210                 intval($priority),
211                 dbesc($info),
212                 intval($hidden),
213                 intval($notify),
214                 intval($fetch_further_information),
215                 dbesc($ffi_keyword_blacklist),
216                 intval($contact_id),
217                 intval(local_user())
218         );
219         if($r)
220                 info( t('Contact updated.') . EOL);
221         else
222                 notice( t('Failed to update contact record.') . EOL);
223
224         $r = q("select * from contact where id = %d and uid = %d limit 1",
225                 intval($contact_id),
226                 intval(local_user())
227         );
228         if($r && dbm::is_result($r))
229                 $a->data['contact'] = $r[0];
230
231         return;
232
233 }
234
235 /*contact actions*/
236 function _contact_update($contact_id) {
237         $r = q("SELECT `uid`, `url`, `network` FROM `contact` WHERE `id` = %d", intval($contact_id));
238         if (!$r)
239                 return;
240
241         $uid = $r[0]["uid"];
242
243         if ($uid != local_user())
244                 return;
245
246         if ($r[0]["network"] == NETWORK_OSTATUS) {
247                 $result = new_contact($uid, $r[0]["url"], false);
248
249                 if ($result['success'])
250                         $r = q("UPDATE `contact` SET `subhub` = 1 WHERE `id` = %d",
251                                 intval($contact_id));
252         } else
253                 // pull feed and consume it, which should subscribe to the hub.
254                 proc_run(PRIORITY_HIGH, "include/onepoll.php", $contact_id, "force");
255 }
256
257 function _contact_update_profile($contact_id) {
258         $r = q("SELECT `uid`, `url`, `network` FROM `contact` WHERE `id` = %d", intval($contact_id));
259         if (!$r)
260                 return;
261
262         $uid = $r[0]["uid"];
263
264         if ($uid != local_user())
265                 return;
266
267         $data = Probe::uri($r[0]["url"], "", 0, false);
268
269         // "Feed" or "Unknown" is mostly a sign of communication problems
270         if ((in_array($data["network"], array(NETWORK_FEED, NETWORK_PHANTOM))) && ($data["network"] != $r[0]["network"]))
271                 return;
272
273         $updatefields = array("name", "nick", "url", "addr", "batch", "notify", "poll", "request", "confirm",
274                                 "poco", "network", "alias");
275         $update = array();
276
277         if ($data["network"] == NETWORK_OSTATUS) {
278                 $result = new_contact($uid, $data["url"], false);
279
280                 if ($result['success'])
281                         $update["subhub"] = true;
282         }
283
284         foreach($updatefields AS $field)
285                 if (isset($data[$field]) && ($data[$field] != ""))
286                         $update[$field] = $data[$field];
287
288         $update["nurl"] = normalise_link($data["url"]);
289
290         $query = "";
291
292         if (isset($data["priority"]) && ($data["priority"] != 0))
293                 $query = "`priority` = ".intval($data["priority"]);
294
295         foreach($update AS $key => $value) {
296                 if ($query != "")
297                         $query .= ", ";
298
299                 $query .= "`".$key."` = '".dbesc($value)."'";
300         }
301
302         if ($query == "")
303                 return;
304
305         $r = q("UPDATE `contact` SET $query WHERE `id` = %d AND `uid` = %d",
306                 intval($contact_id),
307                 intval(local_user())
308         );
309
310         // Update the entry in the contact table
311         update_contact_avatar($data['photo'], local_user(), $contact_id, true);
312
313         // Update the entry in the gcontact table
314         update_gcontact_from_probe($data["url"]);
315 }
316
317 function _contact_block($contact_id, $orig_record) {
318         $blocked = (($orig_record['blocked']) ? 0 : 1);
319         $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d",
320                 intval($blocked),
321                 intval($contact_id),
322                 intval(local_user())
323         );
324         return $r;
325
326 }
327 function _contact_ignore($contact_id, $orig_record) {
328         $readonly = (($orig_record['readonly']) ? 0 : 1);
329         $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d",
330                 intval($readonly),
331                 intval($contact_id),
332                 intval(local_user())
333         );
334         return $r;
335 }
336 function _contact_archive($contact_id, $orig_record) {
337         $archived = (($orig_record['archive']) ? 0 : 1);
338         $r = q("UPDATE `contact` SET `archive` = %d WHERE `id` = %d AND `uid` = %d",
339                 intval($archived),
340                 intval($contact_id),
341                 intval(local_user())
342         );
343         if ($archived) {
344                 q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact_id), intval(local_user()));
345         }
346         return $r;
347 }
348 function _contact_drop($contact_id, $orig_record) {
349         $a = get_app();
350
351         terminate_friendship($a->user,$a->contact,$orig_record);
352         contact_remove($orig_record['id']);
353 }
354
355
356 function contacts_content(App $a) {
357
358         $sort_type = 0;
359         $o = '';
360         nav_set_selected('contacts');
361
362
363         if (! local_user()) {
364                 notice( t('Permission denied.') . EOL);
365                 return;
366         }
367
368         if($a->argc == 3) {
369
370                 $contact_id = intval($a->argv[1]);
371                 if(! $contact_id)
372                         return;
373
374                 $cmd = $a->argv[2];
375
376                 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1",
377                         intval($contact_id),
378                         intval(local_user())
379                 );
380
381                 if(! count($orig_record)) {
382                         notice( t('Could not access contact record.') . EOL);
383                         goaway('contacts');
384                         return; // NOTREACHED
385                 }
386
387                 if($cmd === 'update') {
388                         _contact_update($contact_id);
389                         goaway('contacts/' . $contact_id);
390                         // NOTREACHED
391                 }
392
393                 if($cmd === 'updateprofile') {
394                         _contact_update_profile($contact_id);
395                         goaway('crepair/' . $contact_id);
396                         // NOTREACHED
397                 }
398
399                 if($cmd === 'block') {
400                         $r = _contact_block($contact_id, $orig_record[0]);
401                         if ($r) {
402                                 $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
403                                 info((($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')).EOL);
404                         }
405
406                         goaway('contacts/' . $contact_id);
407                         return; // NOTREACHED
408                 }
409
410                 if($cmd === 'ignore') {
411                         $r = _contact_ignore($contact_id, $orig_record[0]);
412                         if ($r) {
413                                 $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
414                                 info((($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')).EOL);
415                         }
416
417                         goaway('contacts/' . $contact_id);
418                         return; // NOTREACHED
419                 }
420
421
422                 if($cmd === 'archive') {
423                         $r = _contact_archive($contact_id, $orig_record[0]);
424                         if ($r) {
425                                 $archived = (($orig_record[0]['archive']) ? 0 : 1);
426                                 info((($archived) ? t('Contact has been archived') : t('Contact has been unarchived')).EOL);
427                         }
428
429                         goaway('contacts/' . $contact_id);
430                         return; // NOTREACHED
431                 }
432
433                 if($cmd === 'drop') {
434
435                         // Check if we should do HTML-based delete confirmation
436                         if($_REQUEST['confirm']) {
437                                 // <form> can't take arguments in its "action" parameter
438                                 // so add any arguments as hidden inputs
439                                 $query = explode_querystring($a->query_string);
440                                 $inputs = array();
441                                 foreach($query['args'] as $arg) {
442                                         if(strpos($arg, 'confirm=') === false) {
443                                                 $arg_parts = explode('=', $arg);
444                                                 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
445                                         }
446                                 }
447
448                                 $a->page['aside'] = '';
449
450                                 return replace_macros(get_markup_template('contact_drop_confirm.tpl'), array(
451                                         '$header' => t('Drop contact'),
452                                         '$contact' => _contact_detail_for_template($orig_record[0]),
453                                         '$method' => 'get',
454                                         '$message' => t('Do you really want to delete this contact?'),
455                                         '$extra_inputs' => $inputs,
456                                         '$confirm' => t('Yes'),
457                                         '$confirm_url' => $query['base'],
458                                         '$confirm_name' => 'confirmed',
459                                         '$cancel' => t('Cancel'),
460                                 ));
461                         }
462                         // Now check how the user responded to the confirmation query
463                         if ($_REQUEST['canceled']) {
464                                 if (x($_SESSION,'return_url')) {
465                                         goaway('' . $_SESSION['return_url']);
466                                 }
467                                 else {
468                                         goaway('contacts');
469                                 }
470                         }
471
472                         _contact_drop($contact_id, $orig_record[0]);
473                         info( t('Contact has been removed.') . EOL );
474                         if (x($_SESSION,'return_url')) {
475                                 goaway('' . $_SESSION['return_url']);
476                         }
477                         else {
478                                 goaway('contacts');
479                         }
480                         return; // NOTREACHED
481                 }
482                 if ($cmd === 'posts') {
483                         return contact_posts($a, $contact_id);
484                 }
485         }
486
487
488
489         $_SESSION['return_url'] = $a->query_string;
490
491         if((x($a->data,'contact')) && (is_array($a->data['contact']))) {
492
493                 $contact_id = $a->data['contact']['id'];
494                 $contact = $a->data['contact'];
495
496                 $a->page['htmlhead'] .= replace_macros(get_markup_template('contact_head.tpl'), array(
497                         '$baseurl' => System::baseUrl(true),
498                 ));
499                 $a->page['end'] .= replace_macros(get_markup_template('contact_end.tpl'), array(
500                         '$baseurl' => System::baseUrl(true),
501                 ));
502
503                 require_once 'include/contact_selectors.php';
504
505                 $tpl = get_markup_template("contact_edit.tpl");
506
507                 switch($contact['rel']) {
508                         case CONTACT_IS_FRIEND:
509                                 $dir_icon = 'images/lrarrow.gif';
510                                 $relation_text = t('You are mutual friends with %s');
511                                 break;
512                         case CONTACT_IS_FOLLOWER;
513                                 $dir_icon = 'images/larrow.gif';
514                                 $relation_text = t('You are sharing with %s');
515                                 break;
516
517                         case CONTACT_IS_SHARING;
518                                 $dir_icon = 'images/rarrow.gif';
519                                 $relation_text = t('%s is sharing with you');
520                                 break;
521                         default:
522                                 break;
523                 }
524
525                 if(!in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
526                                 $relation_text = "";
527
528                 $relation_text = sprintf($relation_text,htmlentities($contact['name']));
529
530                 if(($contact['network'] === NETWORK_DFRN) && ($contact['rel'])) {
531                         $url = "redir/{$contact['id']}";
532                         $sparkle = ' class="sparkle" ';
533                 }
534                 else {
535                         $url = $contact['url'];
536                         $sparkle = '';
537                 }
538
539                 $insecure = t('Private communications are not available for this contact.');
540
541                 $last_update = (($contact['last-update'] <= NULL_DATE)
542                                 ? t('Never')
543                                 : datetime_convert('UTC',date_default_timezone_get(),$contact['last-update'],'D, j M Y, g:i A'));
544
545                 if ($contact['last-update'] > NULL_DATE) {
546                         $last_update .= ' ' . (($contact['last-update'] <= $contact['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
547                 }
548                 $lblsuggest = (($contact['network'] === NETWORK_DFRN) ? t('Suggest friends') : '');
549
550                 $poll_enabled = in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2));
551
552                 $nettype = sprintf( t('Network type: %s'),network_to_name($contact['network'], $contact["url"]));
553
554                 //$common = count_common_friends(local_user(),$contact['id']);
555                 //$common_text = (($common) ? sprintf( tt('%d contact in common','%d contacts in common', $common),$common) : '');
556
557                 $polling = (($contact['network'] === NETWORK_MAIL | $contact['network'] === NETWORK_FEED) ? 'polling' : '');
558
559                 //$x = count_all_friends(local_user(), $contact['id']);
560                 //$all_friends = (($x) ? t('View all contacts') : '');
561
562                 // tabs
563                 $tab_str = contacts_tab($a, $contact_id, 2);
564
565                 $lost_contact = (($contact['archive'] && $contact['term-date'] > NULL_DATE && $contact['term-date'] < datetime_convert('','','now')) ? t('Communications lost with this contact!') : '');
566
567                 if ($contact['network'] == NETWORK_FEED) {
568                         $fetch_further_information = array('fetch_further_information', t('Fetch further information for feeds'), $contact['fetch_further_information'], t('Fetch further information for feeds'),
569                                                                         array('0'=>t('Disabled'), '1'=>t('Fetch information'), '2'=>t('Fetch information and keywords')));
570                 }
571                 if (in_array($contact['network'], array(NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2)))
572                         $poll_interval = contact_poll_interval($contact['priority'],(! $poll_enabled));
573
574                 if ($contact['network'] == NETWORK_DFRN)
575                         $profile_select = contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false));
576
577                 if (in_array($contact['network'], array(NETWORK_DIASPORA, NETWORK_OSTATUS))) {
578                         if ($contact['rel'] == CONTACT_IS_FOLLOWER) {
579                                 $follow = System::baseUrl(true)."/follow?url=".urlencode($contact["url"]);
580                                 $follow_text = t("Connect/Follow");
581                         } elseif ($contact['rel'] == CONTACT_IS_FRIEND) {
582                                 $follow = System::baseUrl(true)."/unfollow?url=".urlencode($contact["url"]);
583                                 $follow_text = t("Disconnect/Unfollow");
584                         }
585                 }
586
587                 // Load contactact related actions like hide, suggest, delete and others
588                 $contact_actions = contact_actions($contact);
589
590
591                 $o .= replace_macros($tpl, array(
592                         //'$header' => t('Contact Editor'),
593                         '$header' => t("Contact"),
594                         '$tab_str' => $tab_str,
595                         '$submit' => t('Submit'),
596                         '$lbl_vis1' => t('Profile Visibility'),
597                         '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
598                         '$lbl_info1' => t('Contact Information / Notes'),
599                         '$lbl_info2' => t('Their personal note'),
600                         '$reason' => trim(notags($contact['reason'])),
601                         '$infedit' => t('Edit contact notes'),
602                         '$common_text' => $common_text,
603                         '$common_link' => 'common/loc/' . local_user() . '/' . $contact['id'],
604                         '$all_friends' => $all_friends,
605                         '$relation_text' => $relation_text,
606                         '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
607                         '$blockunblock' => t('Block/Unblock contact'),
608                         '$ignorecont' => t('Ignore contact'),
609                         '$lblcrepair' => t("Repair URL settings"),
610                         '$lblrecent' => t('View conversations'),
611                         '$lblsuggest' => $lblsuggest,
612                         //'$delete' => t('Delete contact'),
613                         '$nettype' => $nettype,
614                         '$poll_interval' => $poll_interval,
615                         '$poll_enabled' => $poll_enabled,
616                         '$lastupdtext' => t('Last update:'),
617                         '$lost_contact' => $lost_contact,
618                         '$updpub' => t('Update public posts'),
619                         '$last_update' => $last_update,
620                         '$udnow' => t('Update now'),
621                         '$follow' => $follow,
622                         '$follow_text' => $follow_text,
623                         '$profile_select' => $profile_select,
624                         '$contact_id' => $contact['id'],
625                         '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
626                         '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
627                         '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
628                         '$info' => $contact['info'],
629                         '$cinfo' => array('info', '', $contact['info'], ''),
630                         '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
631                         '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
632                         '$archived' => (($contact['archive']) ? t('Currently archived') : ''),
633                         '$hidden' => array('hidden', t('Hide this contact from others'), ($contact['hidden'] == 1), t('Replies/likes to your public posts <strong>may</strong> still be visible')),
634                         '$notify' => array('notify', t('Notification for new posts'), ($contact['notify_new_posts'] == 1), t('Send a notification of every new post of this contact')),
635                         '$fetch_further_information' => $fetch_further_information,
636                         '$ffi_keyword_blacklist' => $contact['ffi_keyword_blacklist'],
637                         '$ffi_keyword_blacklist' => array('ffi_keyword_blacklist', t('Blacklisted keywords'), $contact['ffi_keyword_blacklist'], t('Comma separated list of keywords that should not be converted to hashtags, when "Fetch information and keywords" is selected')),
638                         '$photo' => $contact['photo'],
639                         '$name' => htmlentities($contact['name']),
640                         '$dir_icon' => $dir_icon,
641                         '$alt_text' => $alt_text,
642                         '$sparkle' => $sparkle,
643                         '$url' => $url,
644                         '$profileurllabel' => t('Profile URL'),
645                         '$profileurl' => $contact['url'],
646                         '$account_type' => account_type($contact),
647                         '$location' => bbcode($contact["location"]),
648                         '$location_label' => t("Location:"),
649                         '$xmpp' => bbcode($contact["xmpp"]),
650                         '$xmpp_label' => t("XMPP:"),
651                         '$about' => bbcode($contact["about"], false, false),
652                         '$about_label' => t("About:"),
653                         '$keywords' => $contact["keywords"],
654                         '$keywords_label' => t("Tags:"),
655                         '$contact_action_button' => t("Actions"),
656                         '$contact_actions' => $contact_actions,
657                         '$contact_status' => t("Status"),
658                         '$contact_settings_label' => t('Contact Settings'),
659                         '$contact_profile_label' => t("Profile"),
660
661                 ));
662
663                 $arr = array('contact' => $contact,'output' => $o);
664
665                 call_hooks('contact_edit', $arr);
666
667                 return $arr['output'];
668
669         }
670
671         $blocked = false;
672         $hidden = false;
673         $ignored = false;
674         $all = false;
675
676         if(($a->argc == 2) && ($a->argv[1] === 'all')) {
677                 $sql_extra = '';
678                 $all = true;
679         }
680         elseif(($a->argc == 2) && ($a->argv[1] === 'blocked')) {
681                 $sql_extra = " AND `blocked` = 1 ";
682                 $blocked = true;
683         }
684         elseif(($a->argc == 2) && ($a->argv[1] === 'hidden')) {
685                 $sql_extra = " AND `hidden` = 1 ";
686                 $hidden = true;
687         }
688         elseif(($a->argc == 2) && ($a->argv[1] === 'ignored')) {
689                 $sql_extra = " AND `readonly` = 1 ";
690                 $ignored = true;
691         }
692         elseif(($a->argc == 2) && ($a->argv[1] === 'archived')) {
693                 $sql_extra = " AND `archive` = 1 ";
694                 $archived = true;
695         }
696         else
697                 $sql_extra = " AND `blocked` = 0 ";
698
699         $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
700         $nets = ((x($_GET,'nets')) ? notags(trim($_GET['nets'])) : '');
701
702         $tabs = array(
703                 array(
704                         'label' => t('Suggestions'),
705                         'url'   => 'suggest',
706                         'sel'   => '',
707                         'title' => t('Suggest potential friends'),
708                         'id'    => 'suggestions-tab',
709                         'accesskey' => 'g',
710                 ),
711                 array(
712                         'label' => t('All Contacts'),
713                         'url'   => 'contacts/all',
714                         'sel'   => ($all) ? 'active' : '',
715                         'title' => t('Show all contacts'),
716                         'id'    => 'showall-tab',
717                         'accesskey' => 'l',
718                 ),
719                 array(
720                         'label' => t('Unblocked'),
721                         'url'   => 'contacts',
722                         'sel'   => ((! $all) && (! $blocked) && (! $hidden) && (! $search) && (! $nets) && (! $ignored) && (! $archived)) ? 'active' : '',
723                         'title' => t('Only show unblocked contacts'),
724                         'id'    => 'showunblocked-tab',
725                         'accesskey' => 'o',
726                 ),
727
728                 array(
729                         'label' => t('Blocked'),
730                         'url'   => 'contacts/blocked',
731                         'sel'   => ($blocked) ? 'active' : '',
732                         'title' => t('Only show blocked contacts'),
733                         'id'    => 'showblocked-tab',
734                         'accesskey' => 'b',
735                 ),
736
737                 array(
738                         'label' => t('Ignored'),
739                         'url'   => 'contacts/ignored',
740                         'sel'   => ($ignored) ? 'active' : '',
741                         'title' => t('Only show ignored contacts'),
742                         'id'    => 'showignored-tab',
743                         'accesskey' => 'i',
744                 ),
745
746                 array(
747                         'label' => t('Archived'),
748                         'url'   => 'contacts/archived',
749                         'sel'   => ($archived) ? 'active' : '',
750                         'title' => t('Only show archived contacts'),
751                         'id'    => 'showarchived-tab',
752                         'accesskey' => 'y',
753                 ),
754
755                 array(
756                         'label' => t('Hidden'),
757                         'url'   => 'contacts/hidden',
758                         'sel'   => ($hidden) ? 'active' : '',
759                         'title' => t('Only show hidden contacts'),
760                         'id'    => 'showhidden-tab',
761                         'accesskey' => 'h',
762                 ),
763
764         );
765
766         $tab_tpl = get_markup_template('common_tabs.tpl');
767         $t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
768
769
770
771         $searching = false;
772         if($search) {
773                 $search_hdr = $search;
774                 $search_txt = dbesc(protect_sprintf(preg_quote($search)));
775                 $searching = true;
776         }
777         $sql_extra .= (($searching) ? " AND (name REGEXP '$search_txt' OR url REGEXP '$search_txt'  OR nick REGEXP '$search_txt') " : "");
778
779         if($nets)
780                 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
781
782         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : '');
783
784
785         $r = q("SELECT COUNT(*) AS `total` FROM `contact`
786                 WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ",
787                 intval($_SESSION['uid']));
788         if (dbm::is_result($r)) {
789                 $a->set_pager_total($r[0]['total']);
790                 $total = $r[0]['total'];
791         }
792
793         $sql_extra3 = unavailable_networks();
794
795         $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 ",
796                 intval($_SESSION['uid']),
797                 intval($a->pager['start']),
798                 intval($a->pager['itemspage'])
799         );
800
801         $contacts = array();
802
803         if (dbm::is_result($r)) {
804                 foreach ($r as $rr) {
805                         $contacts[] = _contact_detail_for_template($rr);
806                 }
807         }
808
809         $tpl = get_markup_template("contacts-template.tpl");
810         $o .= replace_macros($tpl, array(
811                 '$baseurl' => System::baseUrl(),
812                 '$header' => t('Contacts') . (($nets) ? ' - ' . network_to_name($nets) : ''),
813                 '$tabs' => $t,
814                 '$total' => $total,
815                 '$search' => $search_hdr,
816                 '$desc' => t('Search your contacts'),
817                 '$finding' => (($searching) ? sprintf(t('Results for: %s'),$search) : ""),
818                 '$submit' => t('Find'),
819                 '$cmd' => $a->cmd,
820                 '$contacts' => $contacts,
821                 '$contact_drop_confirm' => t('Do you really want to delete this contact?'),
822                 'multiselect' => 1,
823                 '$batch_actions' => array(
824                         'contacts_batch_update' => t('Update'),
825                         'contacts_batch_block' => t('Block')."/".t("Unblock"),
826                         "contacts_batch_ignore" => t('Ignore')."/".t("Unignore"),
827                         "contacts_batch_archive" => t('Archive')."/".t("Unarchive"),
828                         "contacts_batch_drop" => t('Delete'),
829                 ),
830                 '$h_batch_actions' => t('Batch Actions'),
831                 '$paginate' => paginate($a),
832
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 int $contact_id The ID of the contact
845  * @param int $active_tab 1 if tab should be marked as active
846  *
847  * @return array with with contact TabBar data
848  */
849 function contacts_tab($a, $contact_id, $active_tab) {
850         // tabs
851         $tabs = array(
852                 array(
853                         'label'=>t('Status'),
854                         'url' => "contacts/".$contact_id."/posts",
855                         'sel' => (($active_tab == 1)?'active':''),
856                         'title' => t('Status Messages and Posts'),
857                         'id' => 'status-tab',
858                         'accesskey' => 'm',
859                 ),
860                 array(
861                         'label'=>t('Profile'),
862                         'url' => "contacts/".$contact_id,
863                         'sel' => (($active_tab == 2)?'active':''),
864                         'title' => t('Profile Details'),
865                         'id' => 'status-tab',
866                         'accesskey' => 'o',
867                 )
868         );
869
870         // Show this tab only if there is visible friend list
871         $x = count_all_friends(local_user(), $contact_id);
872         if ($x)
873                 $tabs[] = array('label'=>t('Contacts'),
874                                 'url' => "allfriends/".$contact_id,
875                                 'sel' => (($active_tab == 3)?'active':''),
876                                 'title' => t('View all contacts'),
877                                 'id' => 'allfriends-tab',
878                                 'accesskey' => 't');
879
880         // Show this tab only if there is visible common friend list
881         $common = count_common_friends(local_user(),$contact_id);
882         if ($common)
883                 $tabs[] = array('label'=>t('Common Friends'),
884                                 'url' => "common/loc/".local_user()."/".$contact_id,
885                                 'sel' => (($active_tab == 4)?'active':''),
886                                 'title' => t('View all common friends'),
887                                 'id' => 'common-loc-tab',
888                                 'accesskey' => 'd');
889
890         $tabs[] = array('label' => t('Advanced'),
891                         'url'   => 'crepair/' . $contact_id,
892                         'sel' => (($active_tab == 5)?'active':''),
893                         'title' => t('Advanced Contact Settings'),
894                         'id'    => 'advanced-tab',
895                         'accesskey' => 'r');
896
897         $tab_tpl = get_markup_template('common_tabs.tpl');
898         $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
899
900         return $tab_str;
901 }
902
903 function contact_posts($a, $contact_id) {
904
905         $r = q("SELECT `url` FROM `contact` WHERE `id` = %d", intval($contact_id));
906         if ($r) {
907                 $contact = $r[0];
908                 $a->page['aside'] = "";
909                 profile_load($a, "", 0, get_contact_details_by_url($contact["url"]));
910         } else
911                 $profile = "";
912
913         $tab_str = contacts_tab($a, $contact_id, 1);
914
915         $o .= $tab_str;
916
917         $o .= posts_from_contact_url($a, $contact["url"]);
918
919         return $o;
920 }
921
922 function _contact_detail_for_template($rr){
923
924         switch($rr['rel']) {
925                 case CONTACT_IS_FRIEND:
926                         $dir_icon = 'images/lrarrow.gif';
927                         $alt_text = t('Mutual Friendship');
928                         break;
929                 case  CONTACT_IS_FOLLOWER;
930                         $dir_icon = 'images/larrow.gif';
931                         $alt_text = t('is a fan of yours');
932                         break;
933                 case CONTACT_IS_SHARING;
934                         $dir_icon = 'images/rarrow.gif';
935                         $alt_text = t('you are a fan of');
936                         break;
937                 default:
938                         break;
939         }
940         if(($rr['network'] === NETWORK_DFRN) && ($rr['rel'])) {
941                 $url = "redir/{$rr['id']}";
942                 $sparkle = ' class="sparkle" ';
943         }
944         else {
945                 $url = $rr['url'];
946                 $sparkle = '';
947         }
948
949         return array(
950                 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
951                 'edit_hover' => t('Edit contact'),
952                 'photo_menu' => contact_photo_menu($rr),
953                 'id' => $rr['id'],
954                 'alt_text' => $alt_text,
955                 'dir_icon' => $dir_icon,
956                 'thumb' => proxy_url($rr['thumb'], false, PROXY_SIZE_THUMB),
957                 'name' => htmlentities($rr['name']),
958                 'username' => htmlentities($rr['name']),
959                 'account_type' => account_type($rr),
960                 'sparkle' => $sparkle,
961                 'itemurl' => (($rr['addr'] != "") ? $rr['addr'] : $rr['url']),
962                 'url' => $url,
963                 'network' => network_to_name($rr['network'], $rr['url']),
964         );
965
966 }
967
968 /**
969  * @brief Gives a array with actions which can performed to a given contact
970  *
971  * This includes actions like e.g. 'block', 'hide', 'archive', 'delete' and others
972  *
973  * @param array $contact Data about the Contact
974  * @return array with contact related actions
975  */
976 function contact_actions($contact) {
977
978         $poll_enabled = in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2));
979         $contact_action = array();
980
981         // Provide friend suggestion only for Friendica contacts
982         if($contact['network'] === NETWORK_DFRN) {
983                 $contact_actions['suggest'] = array(
984                                                         'label' => t('Suggest friends'),
985                                                         'url'   => 'fsuggest/' . $contact['id'],
986                                                         'title' => '',
987                                                         'sel'   => '',
988                                                         'id'    =>  'suggest',
989                                         );
990         }
991
992         if($poll_enabled) {
993                 $contact_actions['update'] = array(
994                                                         'label' => t('Update now'),
995                                                         'url'   => 'contacts/' . $contact['id'] . '/update',
996                                                         'title' => '',
997                                                         'sel'   => '',
998                                                         'id'    => 'update',
999                                         );
1000         }
1001
1002         $contact_actions['block'] = array(
1003                                                 'label' => (intval($contact['blocked']) ? t('Unblock') : t('Block') ),
1004                                                 'url'   => 'contacts/' . $contact['id'] . '/block',
1005                                                 'title' => t('Toggle Blocked status'),
1006                                                 'sel'   => (intval($contact['blocked']) ? 'active' : ''),
1007                                                 'id'    => 'toggle-block',
1008                                 );
1009
1010         $contact_actions['ignore'] = array(
1011                                                 'label' => (intval($contact['readonly']) ? t('Unignore') : t('Ignore') ),
1012                                                 'url'   => 'contacts/' . $contact['id'] . '/ignore',
1013                                                 'title' => t('Toggle Ignored status'),
1014                                                 'sel'   => (intval($contact['readonly']) ? 'active' : ''),
1015                                                 'id'    => 'toggle-ignore',
1016                                 );
1017
1018         $contact_actions['archive'] = array(
1019                                                 'label' => (intval($contact['archive']) ? t('Unarchive') : t('Archive') ),
1020                                                 'url'   => 'contacts/' . $contact['id'] . '/archive',
1021                                                 'title' => t('Toggle Archive status'),
1022                                                 'sel'   => (intval($contact['archive']) ? 'active' : ''),
1023                                                 'id'    => 'toggle-archive',
1024                                 );
1025
1026         $contact_actions['delete'] = array(
1027                                                 'label' => t('Delete'),
1028                                                 'url'   => 'contacts/' . $contact['id'] . '/drop',
1029                                                 'title' => t('Delete contact'),
1030                                                 'sel'   => '',
1031                                                 'id'    => 'delete',
1032                                 );
1033
1034         return $contact_actions;
1035 }