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