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