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