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