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