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