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