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