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