]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
Merge pull request #2298 from annando/1601-contact-posts
[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 `photo` = '%s',
299                         `thumb` = '%s',
300                         `micro` = '%s',
301                         `name-date` = '%s',
302                         `uri-date` = '%s',
303                         `avatar-date` = '%s'
304                         WHERE `id` = %d",
305                         dbesc($data["photo"]),
306                         dbesc($photos[1]),
307                         dbesc($photos[2]),
308                         dbesc(datetime_convert()),
309                         dbesc(datetime_convert()),
310                         dbesc(datetime_convert()),
311                         intval($contact_id)
312                 );
313
314         // Update the entry in the gcontact table
315         update_gcontact_from_probe($data["url"]);
316 }
317
318 function _contact_block($contact_id, $orig_record) {
319         $blocked = (($orig_record['blocked']) ? 0 : 1);
320         $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d",
321                 intval($blocked),
322                 intval($contact_id),
323                 intval(local_user())
324         );
325         return $r;
326
327 }
328 function _contact_ignore($contact_id, $orig_record) {
329         $readonly = (($orig_record['readonly']) ? 0 : 1);
330         $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d",
331                 intval($readonly),
332                 intval($contact_id),
333                 intval(local_user())
334         );
335         return $r;
336 }
337 function _contact_archive($contact_id, $orig_record) {
338         $archived = (($orig_record['archive']) ? 0 : 1);
339         $r = q("UPDATE `contact` SET `archive` = %d WHERE `id` = %d AND `uid` = %d",
340                 intval($archived),
341                 intval($contact_id),
342                 intval(local_user())
343         );
344         if ($archived) {
345                 q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact_id), intval(local_user()));
346         }
347         return $r;
348 }
349 function _contact_drop($contact_id, $orig_record) {
350         $a = get_app();
351
352         terminate_friendship($a->user,$a->contact,$orig_record);
353         contact_remove($orig_record['id']);
354 }
355
356
357 function contacts_content(&$a) {
358
359         $sort_type = 0;
360         $o = '';
361         nav_set_selected('contacts');
362
363
364         if(! local_user()) {
365                 notice( t('Permission denied.') . EOL);
366                 return;
367         }
368
369         if($a->argc == 3) {
370
371                 $contact_id = intval($a->argv[1]);
372                 if(! $contact_id)
373                         return;
374
375                 $cmd = $a->argv[2];
376
377                 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1",
378                         intval($contact_id),
379                         intval(local_user())
380                 );
381
382                 if(! count($orig_record)) {
383                         notice( t('Could not access contact record.') . EOL);
384                         goaway($a->get_baseurl(true) . '/contacts');
385                         return; // NOTREACHED
386                 }
387
388                 if($cmd === 'update') {
389                         _contact_update($contact_id);
390                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
391                         // NOTREACHED
392                 }
393
394                 if($cmd === 'updateprofile') {
395                         _contact_update_profile($contact_id);
396                         goaway($a->get_baseurl(true) . '/crepair/' . $contact_id);
397                         // NOTREACHED
398                 }
399
400                 if($cmd === 'block') {
401                         $r = _contact_block($contact_id, $orig_record[0]);
402                         if($r) {
403                                 $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
404                                 info((($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')).EOL);
405                         }
406
407                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
408                         return; // NOTREACHED
409                 }
410
411                 if($cmd === 'ignore') {
412                         $r = _contact_ignore($contact_id, $orig_record[0]);
413                         if($r) {
414                                 $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
415                                 info((($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')).EOL);
416                         }
417
418                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
419                         return; // NOTREACHED
420                 }
421
422
423                 if($cmd === 'archive') {
424                         $r = _contact_archive($contact_id, $orig_record[0]);
425                         if($r) {
426                                 $archived = (($orig_record[0]['archive']) ? 0 : 1);
427                                 info((($archived) ? t('Contact has been archived') : t('Contact has been unarchived')).EOL);
428                         }
429
430                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
431                         return; // NOTREACHED
432                 }
433
434                 if($cmd === 'drop') {
435
436                         // Check if we should do HTML-based delete confirmation
437                         if($_REQUEST['confirm']) {
438                                 // <form> can't take arguments in its "action" parameter
439                                 // so add any arguments as hidden inputs
440                                 $query = explode_querystring($a->query_string);
441                                 $inputs = array();
442                                 foreach($query['args'] as $arg) {
443                                         if(strpos($arg, 'confirm=') === false) {
444                                                 $arg_parts = explode('=', $arg);
445                                                 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
446                                         }
447                                 }
448
449                                 $a->page['aside'] = '';
450
451                                 return replace_macros(get_markup_template('contact_drop_confirm.tpl'), array(
452                                         '$contact' =>  _contact_detail_for_template($orig_record[0]),
453                                         '$method' => 'get',
454                                         '$message' => t('Do you really want to delete this contact?'),
455                                         '$extra_inputs' => $inputs,
456                                         '$confirm' => t('Yes'),
457                                         '$confirm_url' => $query['base'],
458                                         '$confirm_name' => 'confirmed',
459                                         '$cancel' => t('Cancel'),
460                                 ));
461                         }
462                         // Now check how the user responded to the confirmation query
463                         if($_REQUEST['canceled']) {
464                                 if(x($_SESSION,'return_url'))
465                                         goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
466                                 else
467                                         goaway($a->get_baseurl(true) . '/contacts');
468                         }
469
470                         _contact_drop($contact_id, $orig_record[0]);
471                         info( t('Contact has been removed.') . EOL );
472                         if(x($_SESSION,'return_url'))
473                                 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
474                         else
475                                 goaway($a->get_baseurl(true) . '/contacts');
476                         return; // NOTREACHED
477                 }
478                 if($cmd === 'posts') {
479                         return contact_posts($a, $contact_id);
480                 }
481         }
482
483
484
485         $_SESSION['return_url'] = $a->query_string;
486
487         if((x($a->data,'contact')) && (is_array($a->data['contact']))) {
488
489                 $contact_id = $a->data['contact']['id'];
490                 $contact = $a->data['contact'];
491
492                 $editselect = 'none';
493                 if( feature_enabled(local_user(),'richtext') )
494                         $editselect = 'exact';
495
496                 $a->page['htmlhead'] .= replace_macros(get_markup_template('contact_head.tpl'), array(
497                         '$baseurl' => $a->get_baseurl(true),
498                         '$editselect' => $editselect,
499                 ));
500                 $a->page['end'] .= replace_macros(get_markup_template('contact_end.tpl'), array(
501                         '$baseurl' => $a->get_baseurl(true),
502                         '$editselect' => $editselect,
503                 ));
504
505                 require_once('include/contact_selectors.php');
506
507                 $tpl = get_markup_template("contact_edit.tpl");
508
509                 switch($contact['rel']) {
510                         case CONTACT_IS_FRIEND:
511                                 $dir_icon = 'images/lrarrow.gif';
512                                 $relation_text = t('You are mutual friends with %s');
513                                 break;
514                         case CONTACT_IS_FOLLOWER;
515                                 $dir_icon = 'images/larrow.gif';
516                                 $relation_text = t('You are sharing with %s');
517                                 break;
518
519                         case CONTACT_IS_SHARING;
520                                 $dir_icon = 'images/rarrow.gif';
521                                 $relation_text = t('%s is sharing with you');
522                                 break;
523                         default:
524                                 break;
525                 }
526
527                 if(!in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
528                                 $relation_text = "";
529
530                 $relation_text = sprintf($relation_text,htmlentities($contact['name']));
531
532                 if(($contact['network'] === NETWORK_DFRN) && ($contact['rel'])) {
533                         $url = "redir/{$contact['id']}";
534                         $sparkle = ' class="sparkle" ';
535                 }
536                 else {
537                         $url = $contact['url'];
538                         $sparkle = '';
539                 }
540
541                 $insecure = t('Private communications are not available for this contact.');
542
543                 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00')
544                                 ? t('Never')
545                                 : datetime_convert('UTC',date_default_timezone_get(),$contact['last-update'],'D, j M Y, g:i A'));
546
547                 if($contact['last-update'] !== '0000-00-00 00:00:00')
548                         $last_update .= ' ' . (($contact['last-update'] <= $contact['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
549
550                 $lblsuggest = (($contact['network'] === NETWORK_DFRN) ? t('Suggest friends') : '');
551
552                 $poll_enabled = in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2));
553
554                 $nettype = sprintf( t('Network type: %s'),network_to_name($contact['network'], $contact["url"]));
555
556                 //$common = count_common_friends(local_user(),$contact['id']);
557                 //$common_text = (($common) ? sprintf( tt('%d contact in common','%d contacts in common', $common),$common) : '');
558
559                 $polling = (($contact['network'] === NETWORK_MAIL | $contact['network'] === NETWORK_FEED) ? 'polling' : '');
560
561                 //$x = count_all_friends(local_user(), $contact['id']);
562                 //$all_friends = (($x) ? t('View all contacts') : '');
563
564                 // tabs
565                 $tab_str = contacts_tab($a, $contact_id, 2);
566
567                 $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!') : '');
568
569                 if ($contact['network'] == NETWORK_FEED)
570                         $fetch_further_information = array('fetch_further_information', t('Fetch further information for feeds'), $contact['fetch_further_information'], t('Fetch further information for feeds'),
571                                                                         array('0'=>t('Disabled'), '1'=>t('Fetch information'), '2'=>t('Fetch information and keywords')));
572
573                 if (in_array($contact['network'], array(NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2)))
574                         $poll_interval = contact_poll_interval($contact['priority'],(! $poll_enabled));
575
576                 if ($contact['network'] == NETWORK_DFRN)
577                         $profile_select = contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false));
578
579                 if (in_array($contact['network'], array(NETWORK_DIASPORA, NETWORK_OSTATUS)) AND
580                         ($contact['rel'] == CONTACT_IS_FOLLOWER))
581                         $follow = $a->get_baseurl(true)."/follow?url=".urlencode($contact["url"]);
582
583
584                 $o .= replace_macros($tpl, array(
585                         //'$header' => t('Contact Editor'),
586                         '$tab_str' => $tab_str,
587                         '$submit' => t('Submit'),
588                         '$lbl_vis1' => t('Profile Visibility'),
589                         '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
590                         '$lbl_info1' => t('Contact Information / Notes'),
591                         '$infedit' => t('Edit contact notes'),
592                         '$common_text' => $common_text,
593                         '$common_link' => $a->get_baseurl(true) . '/common/loc/' . local_user() . '/' . $contact['id'],
594                         '$all_friends' => $all_friends,
595                         '$relation_text' => $relation_text,
596                         '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
597                         '$blockunblock' => t('Block/Unblock contact'),
598                         '$ignorecont' => t('Ignore contact'),
599                         '$lblcrepair' => t("Repair URL settings"),
600                         '$lblrecent' => t('View conversations'),
601                         '$lblsuggest' => $lblsuggest,
602                         '$delete' => t('Delete contact'),
603                         '$nettype' => $nettype,
604                         '$poll_interval' => $poll_interval,
605                         '$poll_enabled' => $poll_enabled,
606                         '$lastupdtext' => t('Last update:'),
607                         '$lost_contact' => $lost_contact,
608                         '$updpub' => t('Update public posts'),
609                         '$last_update' => $last_update,
610                         '$udnow' => t('Update now'),
611                         '$follow' => $follow,
612                         '$follow_text' => t("Connect/Follow"),
613                         '$profile_select' => $profile_select,
614                         '$contact_id' => $contact['id'],
615                         '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
616                         '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
617                         '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
618                         '$info' => $contact['info'],
619                         '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
620                         '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
621                         '$archived' => (($contact['archive']) ? t('Currently archived') : ''),
622                         '$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')),
623                         '$notify' => array('notify', t('Notification for new posts'), ($contact['notify_new_posts'] == 1), t('Send a notification of every new post of this contact')),
624                         '$fetch_further_information' => $fetch_further_information,
625                         '$ffi_keyword_blacklist' => $contact['ffi_keyword_blacklist'],
626                         '$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')),
627                         '$photo' => $contact['photo'],
628                         '$name' => htmlentities($contact['name']),
629                         '$dir_icon' => $dir_icon,
630                         '$alt_text' => $alt_text,
631                         '$sparkle' => $sparkle,
632                         '$url' => $url,
633                         '$profileurllabel' => t('Profile URL'),
634                         '$profileurl' => $contact['url'],
635                         '$location' => bbcode($contact["location"]),
636                         '$location_label' => t("Location:"),
637                         '$about' => bbcode($contact["about"], false, false),
638                         '$about_label' => t("About:"),
639                         '$keywords' => $contact["keywords"],
640                         '$keywords_label' => t("Tags:")
641
642                 ));
643
644                 $arr = array('contact' => $contact,'output' => $o);
645
646                 call_hooks('contact_edit', $arr);
647
648                 return $arr['output'];
649
650         }
651
652         $blocked = false;
653         $hidden = false;
654         $ignored = false;
655         $all = false;
656
657         if(($a->argc == 2) && ($a->argv[1] === 'all')) {
658                 $sql_extra = '';
659                 $all = true;
660         }
661         elseif(($a->argc == 2) && ($a->argv[1] === 'blocked')) {
662                 $sql_extra = " AND `blocked` = 1 ";
663                 $blocked = true;
664         }
665         elseif(($a->argc == 2) && ($a->argv[1] === 'hidden')) {
666                 $sql_extra = " AND `hidden` = 1 ";
667                 $hidden = true;
668         }
669         elseif(($a->argc == 2) && ($a->argv[1] === 'ignored')) {
670                 $sql_extra = " AND `readonly` = 1 ";
671                 $ignored = true;
672         }
673         elseif(($a->argc == 2) && ($a->argv[1] === 'archived')) {
674                 $sql_extra = " AND `archive` = 1 ";
675                 $archived = true;
676         }
677         else
678                 $sql_extra = " AND `blocked` = 0 ";
679
680         $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
681         $nets = ((x($_GET,'nets')) ? notags(trim($_GET['nets'])) : '');
682
683         $tabs = array(
684                 array(
685                         'label' => t('Suggestions'),
686                         'url'   => $a->get_baseurl(true) . '/suggest',
687                         'sel'   => '',
688                         'title' => t('Suggest potential friends'),
689                         'id'    => 'suggestions-tab',
690                         'accesskey' => 'g',
691                 ),
692                 array(
693                         'label' => t('All Contacts'),
694                         'url'   => $a->get_baseurl(true) . '/contacts/all',
695                         'sel'   => ($all) ? 'active' : '',
696                         'title' => t('Show all contacts'),
697                         'id'    => 'showall-tab',
698                         'accesskey' => 'l',
699                 ),
700                 array(
701                         'label' => t('Unblocked'),
702                         'url'   => $a->get_baseurl(true) . '/contacts',
703                         'sel'   => ((! $all) && (! $blocked) && (! $hidden) && (! $search) && (! $nets) && (! $ignored) && (! $archived)) ? 'active' : '',
704                         'title' => t('Only show unblocked contacts'),
705                         'id'    => 'showunblocked-tab',
706                         'accesskey' => 'o',
707                 ),
708
709                 array(
710                         'label' => t('Blocked'),
711                         'url'   => $a->get_baseurl(true) . '/contacts/blocked',
712                         'sel'   => ($blocked) ? 'active' : '',
713                         'title' => t('Only show blocked contacts'),
714                         'id'    => 'showblocked-tab',
715                         'accesskey' => 'b',
716                 ),
717
718                 array(
719                         'label' => t('Ignored'),
720                         'url'   => $a->get_baseurl(true) . '/contacts/ignored',
721                         'sel'   => ($ignored) ? 'active' : '',
722                         'title' => t('Only show ignored contacts'),
723                         'id'    => 'showignored-tab',
724                         'accesskey' => 'i',
725                 ),
726
727                 array(
728                         'label' => t('Archived'),
729                         'url'   => $a->get_baseurl(true) . '/contacts/archived',
730                         'sel'   => ($archived) ? 'active' : '',
731                         'title' => t('Only show archived contacts'),
732                         'id'    => 'showarchived-tab',
733                         'accesskey' => 'y',
734                 ),
735
736                 array(
737                         'label' => t('Hidden'),
738                         'url'   => $a->get_baseurl(true) . '/contacts/hidden',
739                         'sel'   => ($hidden) ? 'active' : '',
740                         'title' => t('Only show hidden contacts'),
741                         'id'    => 'showhidden-tab',
742                         'accesskey' => 'h',
743                 ),
744
745         );
746
747         $tab_tpl = get_markup_template('common_tabs.tpl');
748         $t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
749
750
751
752         $searching = false;
753         if($search) {
754                 $search_hdr = $search;
755                 $search_txt = dbesc(protect_sprintf(preg_quote($search)));
756                 $searching = true;
757         }
758         $sql_extra .= (($searching) ? " AND (name REGEXP '$search_txt' OR url REGEXP '$search_txt'  OR nick REGEXP '$search_txt') " : "");
759
760         if($nets)
761                 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
762
763         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : '');
764
765
766         $r = q("SELECT COUNT(*) AS `total` FROM `contact`
767                 WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ",
768                 intval($_SESSION['uid']));
769         if(count($r)) {
770                 $a->set_pager_total($r[0]['total']);
771                 $total = $r[0]['total'];
772         }
773
774         $sql_extra3 = unavailable_networks();
775
776         $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 ",
777                 intval($_SESSION['uid']),
778                 intval($a->pager['start']),
779                 intval($a->pager['itemspage'])
780         );
781
782         $contacts = array();
783
784         if(count($r)) {
785                 foreach($r as $rr) {
786                         $contacts[] = _contact_detail_for_template($rr);
787                 }
788         }
789
790         $tpl = get_markup_template("contacts-template.tpl");
791         $o .= replace_macros($tpl, array(
792                 '$baseurl' => z_root(),
793                 '$header' => t('Contacts') . (($nets) ? ' - ' . network_to_name($nets) : ''),
794                 '$tabs' => $t,
795                 '$total' => $total,
796                 '$search' => $search_hdr,
797                 '$desc' => t('Search your contacts'),
798                 '$finding' => (($searching) ? t('Finding: ') . "'" . $search . "'" : ""),
799                 '$submit' => t('Find'),
800                 '$cmd' => $a->cmd,
801                 '$contacts' => $contacts,
802                 '$contact_drop_confirm' => t('Do you really want to delete this contact?'),
803                 'multiselect' => 1,
804                 '$batch_actions' => array(
805                         'contacts_batch_update' => t('Update'),
806                         'contacts_batch_block' => t('Block')."/".t("Unblock"),
807                         "contacts_batch_ignore" => t('Ignore')."/".t("Unignore"),
808                         "contacts_batch_archive" => t('Archive')."/".t("Unarchive"),
809                         "contacts_batch_drop" => t('Delete'),
810                 ),
811                 '$paginate' => paginate($a),
812
813         ));
814
815         return $o;
816 }
817
818 function contacts_tab($a, $contact_id, $active_tab) {
819         // tabs
820         $tabs = array(
821                 array(
822                         'label'=>t('Status'),
823                         'url' => "contacts/".$contact_id."/posts",
824                         'sel' => (($active_tab == 1)?'active':''),
825                         'title' => t('Status Messages and Posts'),
826                         'id' => 'status-tab',
827                         'accesskey' => 'm',
828                 ),
829                 array(
830                         'label'=>t('Profile'),
831                         'url' => "contacts/".$contact_id,
832                         'sel' => (($active_tab == 2)?'active':''),
833                         'title' => t('Profile Details'),
834                         'id' => 'status-tab',
835                         'accesskey' => 'o',
836                 )
837         );
838
839         $x = count_all_friends(local_user(), $contact_id);
840         if ($x)
841                 $tabs[] = array('label'=>t('Contacts'),
842                                 'url' => "allfriends/".$contact_id,
843                                 'sel' => (($active_tab == 3)?'active':''),
844                                 'title' => t('View all contacts'),
845                                 'id' => 'allfriends-tab',
846                                 'accesskey' => 't');
847
848         $common = count_common_friends(local_user(),$contact_id);
849         if ($common)
850                 $tabs[] = array('label'=>t('Common Friends'),
851                                 'url' => "common/loc/".local_user()."/".$contact_id,
852                                 'sel' => (($active_tab == 4)?'active':''),
853                                 'title' => t('View all common friends'),
854                                 'id' => 'common-loc-tab',
855                                 'accesskey' => 'd');
856
857         $tabs[] = array('label' => t('Repair'),
858                         'url'   => $a->get_baseurl(true) . '/crepair/' . $contact_id,
859                         'sel' => (($active_tab == 5)?'active':''),
860                         'title' => t('Advanced Contact Settings'),
861                         'id'    => 'repair-tab',
862                         'accesskey' => 'r');
863
864
865         $tabs[] = array('label' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
866                         'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/block',
867                         'sel'   => '',
868                         'title' => t('Toggle Blocked status'),
869                         'id'    => 'toggle-block-tab',
870                         'accesskey' => 'b');
871
872         $tabs[] = array('label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
873                         'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/ignore',
874                         'sel'   => '',
875                         'title' => t('Toggle Ignored status'),
876                         'id'    => 'toggle-ignore-tab',
877                         'accesskey' => 'i');
878
879         $tabs[] = array('label' => (($contact['archive']) ? t('Unarchive') : t('Archive') ),
880                         'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/archive',
881                         'sel'   => '',
882                         'title' => t('Toggle Archive status'),
883                         'id'    => 'toggle-archive-tab',
884                         'accesskey' => 'v');
885
886         $tab_tpl = get_markup_template('common_tabs.tpl');
887         $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
888
889         return $tab_str;
890 }
891
892 function contact_posts($a, $contact_id) {
893
894         $r = q("SELECT `url` FROM `contact` WHERE `id` = %d", intval($contact_id));
895         if ($r) {
896                 $contact = $r[0];
897                 $a->page['aside'] = "";
898                 profile_load($a, "", 0, get_contact_details_by_url($contact["url"]));
899         } else
900                 $profile = "";
901
902         $tab_str = contacts_tab($a, $contact_id, 1);
903
904         $o .= $tab_str;
905
906         $r = q("SELECT `id` FROM `item` WHERE `contact-id` = %d LIMIT 1", intval($contact_id));
907         if ($r)
908                 $o .= posts_from_contact($a, $contact_id);
909         elseif ($contact["url"]) {
910                 $r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
911                         dbesc(normalise_link($contact["url"])));
912
913                 if ($r[0]["id"] <> 0)
914                         $o .= posts_from_gcontact($a, $r[0]["id"]);
915         }
916
917         return $o;
918 }
919
920 function _contact_detail_for_template($rr){
921
922         $community = '';
923
924         switch($rr['rel']) {
925                 case CONTACT_IS_FRIEND:
926                         $dir_icon = 'images/lrarrow.gif';
927                         $alt_text = t('Mutual Friendship');
928                         break;
929                 case  CONTACT_IS_FOLLOWER;
930                         $dir_icon = 'images/larrow.gif';
931                         $alt_text = t('is a fan of yours');
932                         break;
933                 case CONTACT_IS_SHARING;
934                         $dir_icon = 'images/rarrow.gif';
935                         $alt_text = t('you are a fan of');
936                         break;
937                 default:
938                         break;
939         }
940         if(($rr['network'] === NETWORK_DFRN) && ($rr['rel'])) {
941                 $url = "redir/{$rr['id']}";
942                 $sparkle = ' class="sparkle" ';
943         }
944         else {
945                 $url = $rr['url'];
946                 $sparkle = '';
947         }
948
949         //test if contact is a forum page
950         if (isset($rr['forum']) OR isset($rr['prv']))
951                                 $community = ($rr['forum'] OR $rr['prv']);
952
953
954         return array(
955                 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
956                 'edit_hover' => t('Edit contact'),
957                 'photo_menu' => contact_photo_menu($rr),
958                 'id' => $rr['id'],
959                 'alt_text' => $alt_text,
960                 'dir_icon' => $dir_icon,
961                 'thumb' => proxy_url($rr['thumb'], false, PROXY_SIZE_THUMB),
962                 'name' => htmlentities($rr['name']),
963                 'username' => htmlentities($rr['name']),
964                 'account_type' => ($community ? t('Forum') : ''),
965                 'sparkle' => $sparkle,
966                 'itemurl' => (($rr['addr'] != "") ? $rr['addr'] : $rr['url']),
967                 'url' => $url,
968                 'network' => network_to_name($rr['network'], $rr['url']),
969         );
970
971 }