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