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