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