]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
Only show the relation on native networks.
[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('mod/proxy.php');
7
8 function contacts_init(&$a) {
9         if(! local_user())
10                 return;
11
12         $contact_id = 0;
13
14         if(($a->argc == 2) && intval($a->argv[1])) {
15                 $contact_id = intval($a->argv[1]);
16                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d and `id` = %d LIMIT 1",
17                         intval(local_user()),
18                         intval($contact_id)
19                 );
20                 if(! count($r)) {
21                         $contact_id = 0;
22                 }
23         }
24
25         require_once('include/group.php');
26         require_once('include/contact_widgets.php');
27
28         if(! x($a->page,'aside'))
29                 $a->page['aside'] = '';
30
31         if($contact_id) {
32                         $a->data['contact'] = $r[0];
33                         $vcard_widget = replace_macros(get_markup_template("vcard-widget.tpl"),array(
34                                 '$name' => $a->data['contact']['name'],
35                                 '$photo' => $a->data['contact']['photo']
36                         ));
37                         $follow_widget = '';
38         }
39         else {
40                 $vcard_widget = '';
41                 if (isset($_GET['add']))
42                         $follow_widget = follow_widget($_GET['add']);
43                 else
44                         $follow_widget = follow_widget();
45         }
46
47         if ($_GET['nets'] == "all")
48                 $_GET['nets'] = "";
49
50         $groups_widget .= group_side('contacts','group',false,0,$contact_id);
51         $findpeople_widget .= findpeople_widget();
52         $networks_widget .= networks_widget('contacts',$_GET['nets']);
53         $a->page['aside'] .= replace_macros(get_markup_template("contacts-widget-sidebar.tpl"),array(
54                 '$vcard_widget' => $vcard_widget,
55                 '$follow_widget' => $follow_widget,
56                 '$groups_widget' => $groups_widget,
57                 '$findpeople_widget' => $findpeople_widget,
58                 '$networks_widget' => $networks_widget
59         ));
60
61         $base = $a->get_baseurl();
62         $tpl = get_markup_template("contacts-head.tpl");
63         $a->page['htmlhead'] .= replace_macros($tpl,array(
64                 '$baseurl' => $a->get_baseurl(true),
65                 '$base' => $base
66         ));
67
68         $tpl = get_markup_template("contacts-end.tpl");
69         $a->page['end'] .= replace_macros($tpl,array(
70                 '$baseurl' => $a->get_baseurl(true),
71                 '$base' => $base
72         ));
73
74
75 }
76
77 function contacts_batch_actions(&$a){
78         $contacts_id = $_POST['contact_batch'];
79         if (!is_array($contacts_id)) return;
80
81         $orig_records = q("SELECT * FROM `contact` WHERE `id` IN (%s) AND `uid` = %d AND `self` = 0",
82                 implode(",", $contacts_id),
83                 intval(local_user())
84         );
85
86         $count_actions=0;
87         foreach($orig_records as $orig_record) {
88                 $contact_id = $orig_record['id'];
89                 if (x($_POST, 'contacts_batch_update')) {
90                         _contact_update($contact_id);
91                         $count_actions++;
92                 }
93                 if (x($_POST, 'contacts_batch_block')) {
94                         $r  = _contact_block($contact_id, $orig_record);
95                         if ($r) $count_actions++;
96                 }
97                 if (x($_POST, 'contacts_batch_ignore')) {
98                         $r = _contact_ignore($contact_id, $orig_record);
99                         if ($r) $count_actions++;
100                 }
101                 if (x($_POST, 'contacts_batch_archive')) {
102                         $r = _contact_archive($contact_id, $orig_record);
103                         if ($r) $count_actions++;
104                 }
105                 if (x($_POST, 'contacts_batch_drop')) {
106                         _contact_drop($contact_id, $orig_record);
107                         $count_actions++;
108                 }
109         }
110         if ($count_actions>0) {
111                 info ( sprintf( tt("%d contact edited.", "%d contacts edited", $count_actions), $count_actions) );
112         }
113
114         if(x($_SESSION,'return_url'))
115                 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
116         else
117                 goaway($a->get_baseurl(true) . '/contacts');
118
119 }
120
121
122 function contacts_post(&$a) {
123
124         if(! local_user())
125                 return;
126
127         if ($a->argv[1]==="batch") {
128                 contacts_batch_actions($a);
129                 return;
130         }
131
132         $contact_id = intval($a->argv[1]);
133         if(! $contact_id)
134                 return;
135
136         $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
137                 intval($contact_id),
138                 intval(local_user())
139         );
140
141         if(! count($orig_record)) {
142                 notice( t('Could not access contact record.') . EOL);
143                 goaway($a->get_baseurl(true) . '/contacts');
144                 return; // NOTREACHED
145         }
146
147         call_hooks('contact_edit_post', $_POST);
148
149         $profile_id = intval($_POST['profile-assign']);
150         if($profile_id) {
151                 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
152                         intval($profile_id),
153                         intval(local_user())
154                 );
155                 if(! count($r)) {
156                         notice( t('Could not locate selected profile.') . EOL);
157                         return;
158                 }
159         }
160
161         $hidden = intval($_POST['hidden']);
162
163         $notify = intval($_POST['notify']);
164
165         $fetch_further_information = intval($_POST['fetch_further_information']);
166
167         $ffi_keyword_blacklist = fix_mce_lf(escape_tags(trim($_POST['ffi_keyword_blacklist'])));
168
169         $priority = intval($_POST['poll']);
170         if($priority > 5 || $priority < 0)
171                 $priority = 0;
172
173         $info = fix_mce_lf(escape_tags(trim($_POST['info'])));
174
175         $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `info` = '%s',
176                 `hidden` = %d, `notify_new_posts` = %d, `fetch_further_information` = %d,
177                 `ffi_keyword_blacklist` = '%s' WHERE `id` = %d AND `uid` = %d",
178                 intval($profile_id),
179                 intval($priority),
180                 dbesc($info),
181                 intval($hidden),
182                 intval($notify),
183                 intval($fetch_further_information),
184                 dbesc($ffi_keyword_blacklist),
185                 intval($contact_id),
186                 intval(local_user())
187         );
188         if($r)
189                 info( t('Contact updated.') . EOL);
190         else
191                 notice( t('Failed to update contact record.') . EOL);
192
193         $r = q("select * from contact where id = %d and uid = %d limit 1",
194                 intval($contact_id),
195                 intval(local_user())
196         );
197         if($r && count($r))
198                 $a->data['contact'] = $r[0];
199
200         return;
201
202 }
203
204 /*contact actions*/
205 function _contact_update($contact_id) {
206         // pull feed and consume it, which should subscribe to the hub.
207         proc_run('php',"include/poller.php","$contact_id"); 
208 }
209 function _contact_block($contact_id, $orig_record) {
210         $blocked = (($orig_record['blocked']) ? 0 : 1);
211         $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d",
212                 intval($blocked),
213                 intval($contact_id),
214                 intval(local_user())
215         );
216         return $r;
217
218 }
219 function _contact_ignore($contact_id, $orig_record) {
220         $readonly = (($orig_record['readonly']) ? 0 : 1);
221         $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d",
222                 intval($readonly),
223                 intval($contact_id),
224                 intval(local_user())
225         );
226         return $r;
227 }
228 function _contact_archive($contact_id, $orig_record) {
229         $archived = (($orig_record['archive']) ? 0 : 1);
230         $r = q("UPDATE `contact` SET `archive` = %d WHERE `id` = %d AND `uid` = %d",
231                 intval($archived),
232                 intval($contact_id),
233                 intval(local_user())
234         );
235         if ($archived) {
236                 q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact_id), intval(local_user()));
237         }
238         return $r;
239 }
240 function _contact_drop($contact_id, $orig_record) {
241         require_once('include/Contact.php');
242         $a = get_app();
243
244         terminate_friendship($a->user,$a->contact,$orig_record);
245         contact_remove($orig_record['id']);
246 }
247
248
249 function contacts_content(&$a) {
250
251         $sort_type = 0;
252         $o = '';
253         nav_set_selected('contacts');
254
255
256         if(! local_user()) {
257                 notice( t('Permission denied.') . EOL);
258                 return;
259         }
260
261         if($a->argc == 3) {
262
263                 $contact_id = intval($a->argv[1]);
264                 if(! $contact_id)
265                         return;
266
267                 $cmd = $a->argv[2];
268
269                 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1",
270                         intval($contact_id),
271                         intval(local_user())
272                 );
273
274                 if(! count($orig_record)) {
275                         notice( t('Could not access contact record.') . EOL);
276                         goaway($a->get_baseurl(true) . '/contacts');
277                         return; // NOTREACHED
278                 }
279
280                 if($cmd === 'update') {
281                         _contact_update($contact_id);
282                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
283                         // NOTREACHED
284                 }
285
286                 if($cmd === 'block') {
287                         $r = _contact_block($contact_id, $orig_record[0]);
288                         if($r) {
289                                 $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
290                                 info((($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')).EOL);
291                         }
292
293                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
294                         return; // NOTREACHED
295                 }
296
297                 if($cmd === 'ignore') {
298                         $r = _contact_ignore($contact_id, $orig_record[0]);
299                         if($r) {
300                                 $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
301                                 info((($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')).EOL);
302                         }
303
304                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
305                         return; // NOTREACHED
306                 }
307
308
309                 if($cmd === 'archive') {
310                         $r = _contact_archive($contact_id, $orig_record[0]);
311                         if($r) {
312                                 $archived = (($orig_record[0]['archive']) ? 0 : 1);
313                                 info((($archived) ? t('Contact has been archived') : t('Contact has been unarchived')).EOL);
314                         }
315
316                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
317                         return; // NOTREACHED
318                 }
319
320                 if($cmd === 'drop') {
321
322                         // Check if we should do HTML-based delete confirmation
323                         if($_REQUEST['confirm']) {
324                                 // <form> can't take arguments in its "action" parameter
325                                 // so add any arguments as hidden inputs
326                                 $query = explode_querystring($a->query_string);
327                                 $inputs = array();
328                                 foreach($query['args'] as $arg) {
329                                         if(strpos($arg, 'confirm=') === false) {
330                                                 $arg_parts = explode('=', $arg);
331                                                 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
332                                         }
333                                 }
334
335                                 $a->page['aside'] = '';
336                                 return replace_macros(get_markup_template('confirm.tpl'), array(
337                                         '$method' => 'get',
338                                         '$message' => t('Do you really want to delete this contact?'),
339                                         '$extra_inputs' => $inputs,
340                                         '$confirm' => t('Yes'),
341                                         '$confirm_url' => $query['base'],
342                                         '$confirm_name' => 'confirmed',
343                                         '$cancel' => t('Cancel'),
344                                 ));
345                         }
346                         // Now check how the user responded to the confirmation query
347                         if($_REQUEST['canceled']) {
348                                 if(x($_SESSION,'return_url'))
349                                         goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
350                                 else
351                                         goaway($a->get_baseurl(true) . '/contacts');
352                         }
353
354                         _contact_drop($contact_id, $orig_record[0]);
355                         info( t('Contact has been removed.') . EOL );
356                         if(x($_SESSION,'return_url'))
357                                 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
358                         else
359                                 goaway($a->get_baseurl(true) . '/contacts');
360                         return; // NOTREACHED
361                 }
362         }
363
364
365
366         $_SESSION['return_url'] = $a->query_string;
367
368         if((x($a->data,'contact')) && (is_array($a->data['contact']))) {
369
370                 $contact_id = $a->data['contact']['id'];
371                 $contact = $a->data['contact'];
372
373                 $editselect = 'none';
374                 if( feature_enabled(local_user(),'richtext') )
375                         $editselect = 'exact';
376
377                 $a->page['htmlhead'] .= replace_macros(get_markup_template('contact_head.tpl'), array(
378                         '$baseurl' => $a->get_baseurl(true),
379                         '$editselect' => $editselect,
380                 ));
381                 $a->page['end'] .= replace_macros(get_markup_template('contact_end.tpl'), array(
382                         '$baseurl' => $a->get_baseurl(true),
383                         '$editselect' => $editselect,
384                 ));
385
386                 require_once('include/contact_selectors.php');
387
388                 $tpl = get_markup_template("contact_edit.tpl");
389
390                 switch($contact['rel']) {
391                         case CONTACT_IS_FRIEND:
392                                 $dir_icon = 'images/lrarrow.gif';
393                                 $relation_text = t('You are mutual friends with %s');
394                                 break;
395                         case CONTACT_IS_FOLLOWER;
396                                 $dir_icon = 'images/larrow.gif';
397                                 $relation_text = t('You are sharing with %s');
398                                 break;
399
400                         case CONTACT_IS_SHARING;
401                                 $dir_icon = 'images/rarrow.gif';
402                                 $relation_text = t('%s is sharing with you');
403                                 break;
404                         default:
405                                 break;
406                 }
407
408                 if(!in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
409                                 $relation_text = "";
410
411                 $relation_text = sprintf($relation_text,$contact['name']);
412
413                 if(($contact['network'] === NETWORK_DFRN) && ($contact['rel'])) {
414                         $url = "redir/{$contact['id']}";
415                         $sparkle = ' class="sparkle" ';
416                 }
417                 else {
418                         $url = $contact['url'];
419                         $sparkle = '';
420                 }
421
422                 $insecure = t('Private communications are not available for this contact.');
423
424                 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00')
425                                 ? t('Never')
426                                 : datetime_convert('UTC',date_default_timezone_get(),$contact['last-update'],'D, j M Y, g:i A'));
427
428                 if($contact['last-update'] !== '0000-00-00 00:00:00')
429                         $last_update .= ' ' . (($contact['last-update'] <= $contact['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
430
431                 $lblsuggest = (($contact['network'] === NETWORK_DFRN) ? t('Suggest friends') : '');
432
433                 $poll_enabled = in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2));
434
435                 $nettype = sprintf( t('Network type: %s'),network_to_name($contact['network']));
436
437                 $common = count_common_friends(local_user(),$contact['id']);
438                 $common_text = (($common) ? sprintf( tt('%d contact in common','%d contacts in common', $common),$common) : '');
439
440                 $polling = (($contact['network'] === NETWORK_MAIL | $contact['network'] === NETWORK_FEED) ? 'polling' : '');
441
442                 $x = count_all_friends(local_user(), $contact['id']);
443                 $all_friends = (($x) ? t('View all contacts') : '');
444
445                 // tabs
446                 $tabs = array(
447                         array(
448                                 'label' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
449                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/block',
450                                 'sel'   => '',
451                                 'title' => t('Toggle Blocked status'),
452                         ),
453                         array(
454                                 'label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
455                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/ignore',
456                                 'sel'   => '',
457                                 'title' => t('Toggle Ignored status'),
458                         ),
459
460                         array(
461                                 'label' => (($contact['archive']) ? t('Unarchive') : t('Archive') ),
462                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/archive',
463                                 'sel'   => '',
464                                 'title' => t('Toggle Archive status'),
465                         ),
466                         array(
467                                 'label' => t('Repair'),
468                                 'url'   => $a->get_baseurl(true) . '/crepair/' . $contact_id,
469                                 'sel'   => '',
470                                 'title' => t('Advanced Contact Settings'),
471                         )
472                 );
473                 $tab_tpl = get_markup_template('common_tabs.tpl');
474                 $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
475
476                 $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!') : '');
477
478                 if ($contact['network'] == NETWORK_FEED)
479                         $fetch_further_information = array('fetch_further_information', t('Fetch further information for feeds'), $contact['fetch_further_information'], t('Fetch further information for feeds'),
480                                                                         array('0'=>t('Disabled'), '1'=>t('Fetch information'), '2'=>t('Fetch information and keywords')));
481
482                 if (in_array($contact['network'], array(NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2)))
483                         $poll_interval = contact_poll_interval($contact['priority'],(! $poll_enabled));
484
485                 $o .= replace_macros($tpl, array(
486                         '$header' => t('Contact Editor'),
487                         '$tab_str' => $tab_str,
488                         '$submit' => t('Submit'),
489                         '$lbl_vis1' => t('Profile Visibility'),
490                         '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
491                         '$lbl_info1' => t('Contact Information / Notes'),
492                         '$infedit' => t('Edit contact notes'),
493                         '$common_text' => $common_text,
494                         '$common_link' => $a->get_baseurl(true) . '/common/loc/' . local_user() . '/' . $contact['id'],
495                         '$all_friends' => $all_friends,
496                         '$relation_text' => $relation_text,
497                         '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
498                         '$blockunblock' => t('Block/Unblock contact'),
499                         '$ignorecont' => t('Ignore contact'),
500                         '$lblcrepair' => t("Repair URL settings"),
501                         '$lblrecent' => t('View conversations'),
502                         '$lblsuggest' => $lblsuggest,
503                         '$delete' => t('Delete contact'),
504                         '$nettype' => $nettype,
505                         '$poll_interval' => $poll_interval,
506                         '$poll_enabled' => $poll_enabled,
507                         '$lastupdtext' => t('Last update:'),
508                         '$lost_contact' => $lost_contact,
509                         '$updpub' => t('Update public posts'),
510                         '$last_update' => $last_update,
511                         '$udnow' => t('Update now'),
512                         '$profile_select' => contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false)),
513                         '$contact_id' => $contact['id'],
514                         '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
515                         '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
516                         '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
517                         '$info' => $contact['info'],
518                         '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
519                         '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
520                         '$archived' => (($contact['archive']) ? t('Currently archived') : ''),
521                         '$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')),
522                         '$notify' => array('notify', t('Notification for new posts'), ($contact['notify_new_posts'] == 1), t('Send a notification of every new post of this contact')),
523                         '$fetch_further_information' => $fetch_further_information,
524                         '$ffi_keyword_blacklist' => $contact['ffi_keyword_blacklist'],
525                         '$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')),
526                         '$photo' => $contact['photo'],
527                         '$name' => $contact['name'],
528                         '$dir_icon' => $dir_icon,
529                         '$alt_text' => $alt_text,
530                         '$sparkle' => $sparkle,
531                         '$url' => $url,
532
533                 ));
534
535                 $arr = array('contact' => $contact,'output' => $o);
536
537                 call_hooks('contact_edit', $arr);
538
539                 return $arr['output'];
540
541         }
542
543         $blocked = false;
544         $hidden = false;
545         $ignored = false;
546         $all = false;
547
548         if(($a->argc == 2) && ($a->argv[1] === 'all')) {
549                 $sql_extra = '';
550                 $all = true;
551         }
552         elseif(($a->argc == 2) && ($a->argv[1] === 'blocked')) {
553                 $sql_extra = " AND `blocked` = 1 ";
554                 $blocked = true;
555         }
556         elseif(($a->argc == 2) && ($a->argv[1] === 'hidden')) {
557                 $sql_extra = " AND `hidden` = 1 ";
558                 $hidden = true;
559         }
560         elseif(($a->argc == 2) && ($a->argv[1] === 'ignored')) {
561                 $sql_extra = " AND `readonly` = 1 ";
562                 $ignored = true;
563         }
564         elseif(($a->argc == 2) && ($a->argv[1] === 'archived')) {
565                 $sql_extra = " AND `archive` = 1 ";
566                 $archived = true;
567         }
568         else
569                 $sql_extra = " AND `blocked` = 0 ";
570
571         $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
572         $nets = ((x($_GET,'nets')) ? notags(trim($_GET['nets'])) : '');
573
574         $tabs = array(
575                 array(
576                         'label' => t('Suggestions'),
577                         'url'   => $a->get_baseurl(true) . '/suggest', 
578                         'sel'   => '',
579                         'title' => t('Suggest potential friends'),
580                 ),
581                 array(
582                         'label' => t('All Contacts'),
583                         'url'   => $a->get_baseurl(true) . '/contacts/all', 
584                         'sel'   => ($all) ? 'active' : '',
585                         'title' => t('Show all contacts'),
586                 ),
587                 array(
588                         'label' => t('Unblocked'),
589                         'url'   => $a->get_baseurl(true) . '/contacts',
590                         'sel'   => ((! $all) && (! $blocked) && (! $hidden) && (! $search) && (! $nets) && (! $ignored) && (! $archived)) ? 'active' : '',
591                         'title' => t('Only show unblocked contacts'),
592                 ),
593
594                 array(
595                         'label' => t('Blocked'),
596                         'url'   => $a->get_baseurl(true) . '/contacts/blocked',
597                         'sel'   => ($blocked) ? 'active' : '',
598                         'title' => t('Only show blocked contacts'),
599                 ),
600
601                 array(
602                         'label' => t('Ignored'),
603                         'url'   => $a->get_baseurl(true) . '/contacts/ignored',
604                         'sel'   => ($ignored) ? 'active' : '',
605                         'title' => t('Only show ignored contacts'),
606                 ),
607
608                 array(
609                         'label' => t('Archived'),
610                         'url'   => $a->get_baseurl(true) . '/contacts/archived',
611                         'sel'   => ($archived) ? 'active' : '',
612                         'title' => t('Only show archived contacts'),
613                 ),
614
615                 array(
616                         'label' => t('Hidden'),
617                         'url'   => $a->get_baseurl(true) . '/contacts/hidden',
618                         'sel'   => ($hidden) ? 'active' : '',
619                         'title' => t('Only show hidden contacts'),
620                 ),
621
622         );
623
624         $tab_tpl = get_markup_template('common_tabs.tpl');
625         $t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
626
627
628
629         $searching = false;
630         if($search) {
631                 $search_hdr = $search;
632                 $search_txt = dbesc(protect_sprintf(preg_quote($search)));
633                 $searching = true;
634         }
635         $sql_extra .= (($searching) ? " AND (name REGEXP '$search_txt' OR url REGEXP '$search_txt'  OR nick REGEXP '$search_txt') " : "");
636
637         if($nets)
638                 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
639
640         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : '');
641
642
643         $r = q("SELECT COUNT(*) AS `total` FROM `contact`
644                 WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ",
645                 intval($_SESSION['uid']));
646         if(count($r)) {
647                 $a->set_pager_total($r[0]['total']);
648                 $total = $r[0]['total'];
649         }
650
651
652         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ORDER BY `name` ASC LIMIT %d , %d ",
653                 intval($_SESSION['uid']),
654                 intval($a->pager['start']),
655                 intval($a->pager['itemspage'])
656         );
657
658         $contacts = array();
659
660         if(count($r)) {
661
662                 foreach($r as $rr) {
663
664                         switch($rr['rel']) {
665                                 case CONTACT_IS_FRIEND:
666                                         $dir_icon = 'images/lrarrow.gif';
667                                         $alt_text = t('Mutual Friendship');
668                                         break;
669                                 case  CONTACT_IS_FOLLOWER;
670                                         $dir_icon = 'images/larrow.gif';
671                                         $alt_text = t('is a fan of yours');
672                                         break;
673                                 case CONTACT_IS_SHARING;
674                                         $dir_icon = 'images/rarrow.gif';
675                                         $alt_text = t('you are a fan of');
676                                         break;
677                                 default:
678                                         break;
679                         }
680                         if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
681                                 $url = "redir/{$rr['id']}";
682                                 $sparkle = ' class="sparkle" ';
683                         }
684                         else {
685                                 $url = $rr['url'];
686                                 $sparkle = '';
687                         }
688
689
690                         $contacts[] = array(
691                                 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
692                                 'edit_hover' => t('Edit contact'),
693                                 'photo_menu' => contact_photo_menu($rr),
694                                 'id' => $rr['id'],
695                                 'alt_text' => $alt_text,
696                                 'dir_icon' => $dir_icon,
697                                 'thumb' => proxy_url($rr['thumb']),
698                                 'name' => $rr['name'],
699                                 'username' => $rr['name'],
700                                 'sparkle' => $sparkle,
701                                 'itemurl' => $rr['url'],
702                                 'url' => $url,
703                                 'network' => network_to_name($rr['network']),
704                         );
705                 }
706
707
708
709         }
710
711         $tpl = get_markup_template("contacts-template.tpl");
712         $o .= replace_macros($tpl, array(
713                 '$baseurl' => $a->get_baseurl(),
714                 '$header' => t('Contacts') . (($nets) ? ' - ' . network_to_name($nets) : ''),
715                 '$tabs' => $t,
716                 '$total' => $total,
717                 '$search' => $search_hdr,
718                 '$desc' => t('Search your contacts'),
719                 '$finding' => (($searching) ? t('Finding: ') . "'" . $search . "'" : ""),
720                 '$submit' => t('Find'),
721                 '$cmd' => $a->cmd,
722                 '$contacts' => $contacts,
723                 '$contact_drop_confirm' => t('Do you really want to delete this contact?'),
724                 '$batch_actions' => array(
725                         'contacts_batch_update' => t('Update'),
726                         'contacts_batch_block' => t('Block')."/".t("Unblock"),
727                         "contacts_batch_ignore" => t('Ignore')."/".t("Unignore"),
728                         "contacts_batch_archive" => t('Archive')."/".t("Unarchive"),
729                         "contacts_batch_drop" => t('Delete'),
730                 ),
731                 '$paginate' => paginate($a),
732
733         )); 
734         
735         return $o;
736 }