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