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