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