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