]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
a2287382a4c61d3726b5e62b7bc2c5d30d73e1ba
[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 if(! function_exists('contacts_init')) {
11 function contacts_init(&$a) {
12         if(! local_user())
13                 return;
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(! count($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) ? z_root()."/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' => (($a->data['contact']['forum'] || $a->data['contact']['prv']) ? t('Forum') : '')
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' => $a->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' => $a->get_baseurl(true),
89                 '$base' => $base
90         ));
91
92 }
93 }
94
95 if(! function_exists('contacts_batch_actions')) {
96 function contacts_batch_actions(&$a){
97         $contacts_id = $_POST['contact_batch'];
98         if (!is_array($contacts_id)) return;
99
100         $orig_records = q("SELECT * FROM `contact` WHERE `id` IN (%s) AND `uid` = %d AND `self` = 0",
101                 implode(",", $contacts_id),
102                 intval(local_user())
103         );
104
105         $count_actions=0;
106         foreach($orig_records as $orig_record) {
107                 $contact_id = $orig_record['id'];
108                 if (x($_POST, 'contacts_batch_update')) {
109                         _contact_update($contact_id);
110                         $count_actions++;
111                 }
112                 if (x($_POST, 'contacts_batch_block')) {
113                         $r  = _contact_block($contact_id, $orig_record);
114                         if ($r) $count_actions++;
115                 }
116                 if (x($_POST, 'contacts_batch_ignore')) {
117                         $r = _contact_ignore($contact_id, $orig_record);
118                         if ($r) $count_actions++;
119                 }
120                 if (x($_POST, 'contacts_batch_archive')) {
121                         $r = _contact_archive($contact_id, $orig_record);
122                         if ($r) $count_actions++;
123                 }
124                 if (x($_POST, 'contacts_batch_drop')) {
125                         _contact_drop($contact_id, $orig_record);
126                         $count_actions++;
127                 }
128         }
129         if ($count_actions>0) {
130                 info ( sprintf( tt("%d contact edited.", "%d contacts edited.", $count_actions), $count_actions) );
131         }
132
133         if(x($_SESSION,'return_url'))
134                 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
135         else
136                 goaway($a->get_baseurl(true) . '/contacts');
137 }
138 }
139
140 if(! function_exists('contacts_post')) {
141 function contacts_post(&$a) {
142
143         if(! local_user())
144                 return;
145
146         if ($a->argv[1]==="batch") {
147                 contacts_batch_actions($a);
148                 return;
149         }
150
151         $contact_id = intval($a->argv[1]);
152         if(! $contact_id)
153                 return;
154
155         $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
156                 intval($contact_id),
157                 intval(local_user())
158         );
159
160         if(! count($orig_record)) {
161                 notice( t('Could not access contact record.') . EOL);
162                 goaway($a->get_baseurl(true) . '/contacts');
163                 return; // NOTREACHED
164         }
165
166         call_hooks('contact_edit_post', $_POST);
167
168         $profile_id = intval($_POST['profile-assign']);
169         if($profile_id) {
170                 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
171                         intval($profile_id),
172                         intval(local_user())
173                 );
174                 if(! count($r)) {
175                         notice( t('Could not locate selected profile.') . EOL);
176                         return;
177                 }
178         }
179
180         $hidden = intval($_POST['hidden']);
181
182         $notify = intval($_POST['notify']);
183
184         $fetch_further_information = intval($_POST['fetch_further_information']);
185
186         $ffi_keyword_blacklist = fix_mce_lf(escape_tags(trim($_POST['ffi_keyword_blacklist'])));
187
188         $priority = intval($_POST['poll']);
189         if($priority > 5 || $priority < 0)
190                 $priority = 0;
191
192         $info = fix_mce_lf(escape_tags(trim($_POST['info'])));
193
194         $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `info` = '%s',
195                 `hidden` = %d, `notify_new_posts` = %d, `fetch_further_information` = %d,
196                 `ffi_keyword_blacklist` = '%s' WHERE `id` = %d AND `uid` = %d",
197                 intval($profile_id),
198                 intval($priority),
199                 dbesc($info),
200                 intval($hidden),
201                 intval($notify),
202                 intval($fetch_further_information),
203                 dbesc($ffi_keyword_blacklist),
204                 intval($contact_id),
205                 intval(local_user())
206         );
207         if($r)
208                 info( t('Contact updated.') . EOL);
209         else
210                 notice( t('Failed to update contact record.') . EOL);
211
212         $r = q("select * from contact where id = %d and uid = %d limit 1",
213                 intval($contact_id),
214                 intval(local_user())
215         );
216         if($r && count($r))
217                 $a->data['contact'] = $r[0];
218
219         return;
220 }
221 }
222
223 /*contact actions*/
224 if(! function_exists('_contact_update')) {
225 function _contact_update($contact_id) {
226         $r = q("SELECT `uid`, `url`, `network` FROM `contact` WHERE `id` = %d", intval($contact_id));
227         if (!$r)
228                 return;
229
230         $uid = $r[0]["uid"];
231
232         if ($uid != local_user())
233                 return;
234
235         if ($r[0]["network"] == NETWORK_OSTATUS) {
236                 $result = new_contact($uid, $r[0]["url"], false);
237
238                 if ($result['success'])
239                         $r = q("UPDATE `contact` SET `subhub` = 1 WHERE `id` = %d",
240                                 intval($contact_id));
241         } else
242                 // pull feed and consume it, which should subscribe to the hub.
243                 proc_run('php',"include/onepoll.php","$contact_id", "force");
244 }
245 }
246
247 if(! function_exists('_contact_update_profile')) {
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
309 if(! function_exists('_contact_block')) {
310 function _contact_block($contact_id, $orig_record) {
311         $blocked = (($orig_record['blocked']) ? 0 : 1);
312         $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d",
313                 intval($blocked),
314                 intval($contact_id),
315                 intval(local_user())
316         );
317         return $r;
318 }
319 }
320
321 if(! function_exists('_contact_ignore')) {
322 function _contact_ignore($contact_id, $orig_record) {
323         $readonly = (($orig_record['readonly']) ? 0 : 1);
324         $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d",
325                 intval($readonly),
326                 intval($contact_id),
327                 intval(local_user())
328         );
329         return $r;
330 }
331 }
332
333 if(! function_exists('_contact_archive')) {
334 function _contact_archive($contact_id, $orig_record) {
335         $archived = (($orig_record['archive']) ? 0 : 1);
336         $r = q("UPDATE `contact` SET `archive` = %d WHERE `id` = %d AND `uid` = %d",
337                 intval($archived),
338                 intval($contact_id),
339                 intval(local_user())
340         );
341         if ($archived) {
342                 q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact_id), intval(local_user()));
343         }
344         return $r;
345 }
346 }
347
348 if(! function_exists('_contact_drop')) {
349 function _contact_drop($contact_id, $orig_record) {
350         $a = get_app();
351
352         terminate_friendship($a->user,$a->contact,$orig_record);
353         contact_remove($orig_record['id']);
354 }
355 }
356
357 if(! function_exists('contacts_content')) {
358 function contacts_content(&$a) {
359
360         $sort_type = 0;
361         $o = '';
362         nav_set_selected('contacts');
363
364
365         if(! local_user()) {
366                 notice( t('Permission denied.') . EOL);
367                 return;
368         }
369
370         if($a->argc == 3) {
371
372                 $contact_id = intval($a->argv[1]);
373                 if(! $contact_id)
374                         return;
375
376                 $cmd = $a->argv[2];
377
378                 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1",
379                         intval($contact_id),
380                         intval(local_user())
381                 );
382
383                 if(! count($orig_record)) {
384                         notice( t('Could not access contact record.') . EOL);
385                         goaway($a->get_baseurl(true) . '/contacts');
386                         return; // NOTREACHED
387                 }
388
389                 if($cmd === 'update') {
390                         _contact_update($contact_id);
391                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
392                         // NOTREACHED
393                 }
394
395                 if($cmd === 'updateprofile') {
396                         _contact_update_profile($contact_id);
397                         goaway($a->get_baseurl(true) . '/crepair/' . $contact_id);
398                         // NOTREACHED
399                 }
400
401                 if($cmd === 'block') {
402                         $r = _contact_block($contact_id, $orig_record[0]);
403                         if($r) {
404                                 $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
405                                 info((($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')).EOL);
406                         }
407
408                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
409                         return; // NOTREACHED
410                 }
411
412                 if($cmd === 'ignore') {
413                         $r = _contact_ignore($contact_id, $orig_record[0]);
414                         if($r) {
415                                 $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
416                                 info((($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')).EOL);
417                         }
418
419                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
420                         return; // NOTREACHED
421                 }
422
423
424                 if($cmd === 'archive') {
425                         $r = _contact_archive($contact_id, $orig_record[0]);
426                         if($r) {
427                                 $archived = (($orig_record[0]['archive']) ? 0 : 1);
428                                 info((($archived) ? t('Contact has been archived') : t('Contact has been unarchived')).EOL);
429                         }
430
431                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
432                         return; // NOTREACHED
433                 }
434
435                 if($cmd === 'drop') {
436
437                         // Check if we should do HTML-based delete confirmation
438                         if($_REQUEST['confirm']) {
439                                 // <form> can't take arguments in its "action" parameter
440                                 // so add any arguments as hidden inputs
441                                 $query = explode_querystring($a->query_string);
442                                 $inputs = array();
443                                 foreach($query['args'] as $arg) {
444                                         if(strpos($arg, 'confirm=') === false) {
445                                                 $arg_parts = explode('=', $arg);
446                                                 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
447                                         }
448                                 }
449
450                                 $a->page['aside'] = '';
451
452                                 return replace_macros(get_markup_template('contact_drop_confirm.tpl'), array(
453                                         '$contact' =>  _contact_detail_for_template($orig_record[0]),
454                                         '$method' => 'get',
455                                         '$message' => t('Do you really want to delete this contact?'),
456                                         '$extra_inputs' => $inputs,
457                                         '$confirm' => t('Yes'),
458                                         '$confirm_url' => $query['base'],
459                                         '$confirm_name' => 'confirmed',
460                                         '$cancel' => t('Cancel'),
461                                 ));
462                         }
463                         // Now check how the user responded to the confirmation query
464                         if($_REQUEST['canceled']) {
465                                 if(x($_SESSION,'return_url'))
466                                         goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
467                                 else
468                                         goaway($a->get_baseurl(true) . '/contacts');
469                         }
470
471                         _contact_drop($contact_id, $orig_record[0]);
472                         info( t('Contact has been removed.') . EOL );
473                         if(x($_SESSION,'return_url'))
474                                 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
475                         else
476                                 goaway($a->get_baseurl(true) . '/contacts');
477                         return; // NOTREACHED
478                 }
479                 if($cmd === 'posts') {
480                         return contact_posts($a, $contact_id);
481                 }
482         }
483
484
485
486         $_SESSION['return_url'] = $a->query_string;
487
488         if((x($a->data,'contact')) && (is_array($a->data['contact']))) {
489
490                 $contact_id = $a->data['contact']['id'];
491                 $contact = $a->data['contact'];
492
493                 $editselect = 'none';
494                 if( feature_enabled(local_user(),'richtext') )
495                         $editselect = 'exact';
496
497                 $a->page['htmlhead'] .= replace_macros(get_markup_template('contact_head.tpl'), array(
498                         '$baseurl' => $a->get_baseurl(true),
499                         '$editselect' => $editselect,
500                 ));
501                 $a->page['end'] .= replace_macros(get_markup_template('contact_end.tpl'), array(
502                         '$baseurl' => $a->get_baseurl(true),
503                         '$editselect' => $editselect,
504                 ));
505
506                 require_once('include/contact_selectors.php');
507
508                 $tpl = get_markup_template("contact_edit.tpl");
509
510                 switch($contact['rel']) {
511                         case CONTACT_IS_FRIEND:
512                                 $dir_icon = 'images/lrarrow.gif';
513                                 $relation_text = t('You are mutual friends with %s');
514                                 break;
515                         case CONTACT_IS_FOLLOWER;
516                                 $dir_icon = 'images/larrow.gif';
517                                 $relation_text = t('You are sharing with %s');
518                                 break;
519
520                         case CONTACT_IS_SHARING;
521                                 $dir_icon = 'images/rarrow.gif';
522                                 $relation_text = t('%s is sharing with you');
523                                 break;
524                         default:
525                                 break;
526                 }
527
528                 if(!in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
529                                 $relation_text = "";
530
531                 $relation_text = sprintf($relation_text,htmlentities($contact['name']));
532
533                 if(($contact['network'] === NETWORK_DFRN) && ($contact['rel'])) {
534                         $url = "redir/{$contact['id']}";
535                         $sparkle = ' class="sparkle" ';
536                 }
537                 else {
538                         $url = $contact['url'];
539                         $sparkle = '';
540                 }
541
542                 $insecure = t('Private communications are not available for this contact.');
543
544                 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00')
545                                 ? t('Never')
546                                 : datetime_convert('UTC',date_default_timezone_get(),$contact['last-update'],'D, j M Y, g:i A'));
547
548                 if($contact['last-update'] !== '0000-00-00 00:00:00')
549                         $last_update .= ' ' . (($contact['last-update'] <= $contact['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
550
551                 $lblsuggest = (($contact['network'] === NETWORK_DFRN) ? t('Suggest friends') : '');
552
553                 $poll_enabled = in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2));
554
555                 $nettype = sprintf( t('Network type: %s'),network_to_name($contact['network'], $contact["url"]));
556
557                 //$common = count_common_friends(local_user(),$contact['id']);
558                 //$common_text = (($common) ? sprintf( tt('%d contact in common','%d contacts in common', $common),$common) : '');
559
560                 $polling = (($contact['network'] === NETWORK_MAIL | $contact['network'] === NETWORK_FEED) ? 'polling' : '');
561
562                 //$x = count_all_friends(local_user(), $contact['id']);
563                 //$all_friends = (($x) ? t('View all contacts') : '');
564
565                 // tabs
566                 $tab_str = contacts_tab($a, $contact_id, 2);
567
568                 $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!') : '');
569
570                 if ($contact['network'] == NETWORK_FEED)
571                         $fetch_further_information = array('fetch_further_information', t('Fetch further information for feeds'), $contact['fetch_further_information'], t('Fetch further information for feeds'),
572                                                                         array('0'=>t('Disabled'), '1'=>t('Fetch information'), '2'=>t('Fetch information and keywords')));
573
574                 if (in_array($contact['network'], array(NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2)))
575                         $poll_interval = contact_poll_interval($contact['priority'],(! $poll_enabled));
576
577                 if ($contact['network'] == NETWORK_DFRN)
578                         $profile_select = contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false));
579
580                 if (in_array($contact['network'], array(NETWORK_DIASPORA, NETWORK_OSTATUS)) AND
581                         ($contact['rel'] == CONTACT_IS_FOLLOWER))
582                         $follow = $a->get_baseurl(true)."/follow?url=".urlencode($contact["url"]);
583
584                 // Load contactact related actions like hide, suggest, delete and others
585                 $contact_actions = contact_actions($contact);
586
587
588                 $o .= replace_macros($tpl, array(
589                         //'$header' => t('Contact Editor'),
590                         '$tab_str' => $tab_str,
591                         '$submit' => t('Submit'),
592                         '$lbl_vis1' => t('Profile Visibility'),
593                         '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
594                         '$lbl_info1' => t('Contact Information / Notes'),
595                         '$infedit' => t('Edit contact notes'),
596                         //'$common_text' => $common_text,
597                         '$common_link' => $a->get_baseurl(true) . '/common/loc/' . local_user() . '/' . $contact['id'],
598                         '$all_friends' => $all_friends,
599                         '$relation_text' => $relation_text,
600                         '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
601                         '$blockunblock' => t('Block/Unblock contact'),
602                         '$ignorecont' => t('Ignore contact'),
603                         '$lblcrepair' => t("Repair URL settings"),
604                         '$lblrecent' => t('View conversations'),
605                         '$lblsuggest' => $lblsuggest,
606                         //'$delete' => t('Delete contact'),
607                         '$nettype' => $nettype,
608                         '$poll_interval' => $poll_interval,
609                         '$poll_enabled' => $poll_enabled,
610                         '$lastupdtext' => t('Last update:'),
611                         '$lost_contact' => $lost_contact,
612                         '$updpub' => t('Update public posts'),
613                         '$last_update' => $last_update,
614                         '$udnow' => t('Update now'),
615                         '$follow' => $follow,
616                         '$follow_text' => t("Connect/Follow"),
617                         '$profile_select' => $profile_select,
618                         '$contact_id' => $contact['id'],
619                         '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
620                         '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
621                         '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
622                         '$info' => $contact['info'],
623                         '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
624                         '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
625                         '$archived' => (($contact['archive']) ? t('Currently archived') : ''),
626                         '$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')),
627                         '$notify' => array('notify', t('Notification for new posts'), ($contact['notify_new_posts'] == 1), t('Send a notification of every new post of this contact')),
628                         '$fetch_further_information' => $fetch_further_information,
629                         '$ffi_keyword_blacklist' => $contact['ffi_keyword_blacklist'],
630                         '$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')),
631                         '$photo' => $contact['photo'],
632                         '$name' => htmlentities($contact['name']),
633                         '$dir_icon' => $dir_icon,
634                         '$alt_text' => $alt_text,
635                         '$sparkle' => $sparkle,
636                         '$url' => $url,
637                         '$profileurllabel' => t('Profile URL'),
638                         '$profileurl' => $contact['url'],
639                         '$location' => bbcode($contact["location"]),
640                         '$location_label' => t("Location:"),
641                         '$about' => bbcode($contact["about"], false, false),
642                         '$about_label' => t("About:"),
643                         '$keywords' => $contact["keywords"],
644                         '$keywords_label' => t("Tags:"),
645                         '$contact_action_button' => t("Actions"),
646                         '$contact_actions' => $contact_actions,
647                         '$contact_status' => t("Status"),
648                         '$contact_settings_label' => t('Contact Settings'),
649
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'   => $a->get_baseurl(true) . '/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'   => $a->get_baseurl(true) . '/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'   => $a->get_baseurl(true) . '/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'   => $a->get_baseurl(true) . '/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'   => $a->get_baseurl(true) . '/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'   => $a->get_baseurl(true) . '/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'   => $a->get_baseurl(true) . '/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(count($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(count($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) ? t('Finding: ') . "'" . $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                 '$paginate' => paginate($a),
820
821         ));
822
823         return $o;
824 }
825 }
826
827 if(! function_exists('contacts_tab')) {
828 function contacts_tab($a, $contact_id, $active_tab) {
829         // tabs
830         $tabs = array(
831                 array(
832                         'label'=>t('Status'),
833                         'url' => "contacts/".$contact_id."/posts",
834                         'sel' => (($active_tab == 1)?'active':''),
835                         'title' => t('Status Messages and Posts'),
836                         'id' => 'status-tab',
837                         'accesskey' => 'm',
838                 ),
839                 array(
840                         'label'=>t('Profile'),
841                         'url' => "contacts/".$contact_id,
842                         'sel' => (($active_tab == 2)?'active':''),
843                         'title' => t('Profile Details'),
844                         'id' => 'status-tab',
845                         'accesskey' => 'o',
846                 )
847         );
848
849         $x = count_all_friends(local_user(), $contact_id);
850         if ($x)
851                 $tabs[] = array('label'=>t('Contacts'),
852                                 'url' => "allfriends/".$contact_id,
853                                 'sel' => (($active_tab == 3)?'active':''),
854                                 'title' => t('View all contacts'),
855                                 'id' => 'allfriends-tab',
856                                 'accesskey' => 't');
857
858         $common = count_common_friends(local_user(),$contact_id);
859         if ($common)
860                 $tabs[] = array('label'=>t('Common Friends'),
861                                 'url' => "common/loc/".local_user()."/".$contact_id,
862                                 'sel' => (($active_tab == 4)?'active':''),
863                                 'title' => t('View all common friends'),
864                                 'id' => 'common-loc-tab',
865                                 'accesskey' => 'd');
866
867         $tabs[] = array('label' => t('Repair'),
868                         'url'   => $a->get_baseurl(true) . '/crepair/' . $contact_id,
869                         'sel' => (($active_tab == 5)?'active':''),
870                         'title' => t('Advanced Contact Settings'),
871                         'id'    => 'repair-tab',
872                         'accesskey' => 'r');
873
874
875         $tabs[] = array('label' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
876                         'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/block',
877                         'sel'   => '',
878                         'title' => t('Toggle Blocked status'),
879                         'id'    => 'toggle-block-tab',
880                         'accesskey' => 'b');
881
882         $tabs[] = array('label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
883                         'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/ignore',
884                         'sel'   => '',
885                         'title' => t('Toggle Ignored status'),
886                         'id'    => 'toggle-ignore-tab',
887                         'accesskey' => 'i');
888
889         $tabs[] = array('label' => (($contact['archive']) ? t('Unarchive') : t('Archive') ),
890                         'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/archive',
891                         'sel'   => '',
892                         'title' => t('Toggle Archive status'),
893                         'id'    => 'toggle-archive-tab',
894                         'accesskey' => 'v');
895
896         $tab_tpl = get_markup_template('common_tabs.tpl');
897         $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
898
899         return $tab_str;
900 }
901 }
902
903 if(! function_exists('contact_posts')) {
904 function contact_posts($a, $contact_id) {
905
906         $r = q("SELECT `url` FROM `contact` WHERE `id` = %d", intval($contact_id));
907         if ($r) {
908                 $contact = $r[0];
909                 $a->page['aside'] = "";
910                 profile_load($a, "", 0, get_contact_details_by_url($contact["url"]));
911         } else
912                 $profile = "";
913
914         $tab_str = contacts_tab($a, $contact_id, 1);
915
916         $o .= $tab_str;
917
918         $r = q("SELECT `id` FROM `item` WHERE `contact-id` = %d LIMIT 1", intval($contact_id));
919         if ($r)
920                 $o .= posts_from_contact($a, $contact_id);
921         elseif ($contact["url"]) {
922                 $r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
923                         dbesc(normalise_link($contact["url"])));
924
925                 if ($r[0]["id"] <> 0)
926                         $o .= posts_from_gcontact($a, $r[0]["id"]);
927         }
928
929         return $o;
930 }
931 }
932
933 if(! function_exists('_contact_detail_for_template')) {
934 function _contact_detail_for_template($rr){
935
936         $community = '';
937
938         switch($rr['rel']) {
939                 case CONTACT_IS_FRIEND:
940                         $dir_icon = 'images/lrarrow.gif';
941                         $alt_text = t('Mutual Friendship');
942                         break;
943                 case  CONTACT_IS_FOLLOWER;
944                         $dir_icon = 'images/larrow.gif';
945                         $alt_text = t('is a fan of yours');
946                         break;
947                 case CONTACT_IS_SHARING;
948                         $dir_icon = 'images/rarrow.gif';
949                         $alt_text = t('you are a fan of');
950                         break;
951                 default:
952                         break;
953         }
954         if(($rr['network'] === NETWORK_DFRN) && ($rr['rel'])) {
955                 $url = "redir/{$rr['id']}";
956                 $sparkle = ' class="sparkle" ';
957         }
958         else {
959                 $url = $rr['url'];
960                 $sparkle = '';
961         }
962
963         //test if contact is a forum page
964         if (isset($rr['forum']) OR isset($rr['prv']))
965                                 $community = ($rr['forum'] OR $rr['prv']);
966
967
968         return array(
969                 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
970                 'edit_hover' => t('Edit contact'),
971                 'photo_menu' => contact_photo_menu($rr),
972                 'id' => $rr['id'],
973                 'alt_text' => $alt_text,
974                 'dir_icon' => $dir_icon,
975                 'thumb' => proxy_url($rr['thumb'], false, PROXY_SIZE_THUMB),
976                 'name' => htmlentities($rr['name']),
977                 'username' => htmlentities($rr['name']),
978                 'account_type' => ($community ? t('Forum') : ''),
979                 'sparkle' => $sparkle,
980                 'itemurl' => (($rr['addr'] != "") ? $rr['addr'] : $rr['url']),
981                 'url' => $url,
982                 'network' => network_to_name($rr['network'], $rr['url']),
983         );
984 }
985 }
986
987 /**
988  * @brief Gives a array with actions which can performed to a given contact
989  * 
990  * This includes actions like e.g. 'block', 'hide', 'archive', 'delete' and others
991  * 
992  * @param array $contact Data about the Contact
993  * @return array with actions related actions
994  */
995 function contact_actions($contact) {
996
997         $poll_enabled = in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2));
998         $contact_action = array();
999
1000         if($contact['network'] === NETWORK_DFRN) {
1001                 $contact_actions['suggest'] = array(
1002                                                         'label' => t('Suggest friends'),
1003                                                         'url'   => app::get_baseurl(true) . '/fsuggest/' . $contact['id'],
1004                                                         'title' => '',
1005                                                         'sel'   => '',
1006                                                         'id'    =>  'suggest',
1007                                         );
1008         }
1009
1010         if($poll_enabled) {
1011                 $contact_actions['update'] = array(
1012                                                         'label' => t('Update now'),
1013                                                         'url'   => app::get_baseurl(true) . '/contacts/' . $contact['id'] . '/update',
1014                                                         'title' => '',
1015                                                         'sel'   => '',
1016                                                         'id'    => 'update',
1017                                         );
1018         }
1019
1020         $contact_actions['repair'] = array(
1021                                                 'label' => t('Repair'),
1022                                                 'url'   => app::get_baseurl(true) . '/crepair/' . $contact['id'],
1023                                                 'title' => t('Advanced Contact Settings'),
1024                                                 'sel'   => '',
1025                                                 'id'    => 'repair',
1026                                 );
1027
1028         $contact_actions['block'] = array(
1029                                                 'label' => (intval($contact['blocked']) ? t('Unblock') : t('Block') ),
1030                                                 'url'   => app::get_baseurl(true) . '/contacts/' . $contact['id'] . '/block',
1031                                                 'title' => t('Toggle Blocked status'),
1032                                                 'sel'   => (intval($contact['blocked']) ? 'active' : ''),
1033                                                 'id'    => 'toggle-block',
1034                                 );
1035
1036         $contact_actions['ignore'] = array(
1037                                                 'label' => (intval($contact['readonly']) ? t('Unignore') : t('Ignore') ),
1038                                                 'url'   => app::get_baseurl(true) . '/contacts/' . $contact['id'] . '/ignore',
1039                                                 'title' => t('Toggle Ignored status'),
1040                                                 'sel'   => (intval($contact['readonly']) ? 'active' : ''),
1041                                                 'id'    => 'toggle-ignore',
1042                                 );
1043
1044         $contact_actions['archive'] = array(
1045                                                 'label' => (intval($contact['archive']) ? t('Unarchive') : t('Archive') ),
1046                                                 'url'   => app::get_baseurl(true) . '/contacts/' . $contact['id'] . '/archive',
1047                                                 'title' => t('Toggle Archive status'),
1048                                                 'sel'   => (intval($contact['archive']) ? 'active' : ''),
1049                                                 'id'    => 'toggle-archive',
1050                                 );
1051
1052         $contact_actions['delete'] = array(
1053                                                 'label' => t('Delete'),
1054                                                 'url'   => app::get_baseurl(true) . '/contacts/' . $contact['id'] . '/drop', 
1055                                                 'title' => t('Delete contact'),
1056                                                 'sel'   => '',
1057                                                 'id'    => 'delete',
1058                                 );
1059
1060         return $contact_actions;
1061 }