]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
Merge pull request #2119 from annando/1511-contact-evolution
[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(&$a) {
11         if(! local_user())
12                 return;
13
14         $contact_id = 0;
15
16         if((($a->argc == 2) && intval($a->argv[1])) OR (($a->argc == 3) && intval($a->argv[1]) && ($a->argv[2] == "posts"))) {
17                 $contact_id = intval($a->argv[1]);
18                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d and `id` = %d LIMIT 1",
19                         intval(local_user()),
20                         intval($contact_id)
21                 );
22                 if(! count($r)) {
23                         $contact_id = 0;
24                 }
25         }
26
27         require_once('include/group.php');
28         require_once('include/contact_widgets.php');
29
30         if ($_GET['nets'] == "all")
31         $_GET['nets'] = "";
32
33         if(! x($a->page,'aside'))
34                 $a->page['aside'] = '';
35
36         if($contact_id) {
37                         $a->data['contact'] = $r[0];
38                         $vcard_widget = replace_macros(get_markup_template("vcard-widget.tpl"),array(
39                                 '$name' => htmlentities($a->data['contact']['name']),
40                                 '$photo' => $a->data['contact']['photo'],
41                                 '$url' => ($a->data['contact']['network'] == NETWORK_DFRN) ? $a->get_baseurl()."/redir/".$a->data['contact']['id'] : $a->data['contact']['url']
42                         ));
43                         $finpeople_widget = '';
44                         $follow_widget = '';
45                         $networks_widget = '';
46         }
47         else {
48                 $vcard_widget = '';
49                 $networks_widget .= networks_widget('contacts',$_GET['nets']);
50                 if (isset($_GET['add']))
51                         $follow_widget = follow_widget($_GET['add']);
52                 else
53                         $follow_widget = follow_widget();
54
55                 $findpeople_widget .= findpeople_widget();
56         }
57
58         $groups_widget .= group_side('contacts','group','full',0,$contact_id);
59
60         $a->page['aside'] .= replace_macros(get_markup_template("contacts-widget-sidebar.tpl"),array(
61                 '$vcard_widget' => $vcard_widget,
62                 '$findpeople_widget' => $findpeople_widget,
63                 '$follow_widget' => $follow_widget,
64                 '$groups_widget' => $groups_widget,
65                 '$networks_widget' => $networks_widget
66         ));
67
68         $base = $a->get_baseurl();
69         $tpl = get_markup_template("contacts-head.tpl");
70         $a->page['htmlhead'] .= replace_macros($tpl,array(
71                 '$baseurl' => $a->get_baseurl(true),
72                 '$base' => $base
73         ));
74
75         $tpl = get_markup_template("contacts-end.tpl");
76         $a->page['end'] .= replace_macros($tpl,array(
77                 '$baseurl' => $a->get_baseurl(true),
78                 '$base' => $base
79         ));
80
81
82 }
83
84 function contacts_batch_actions(&$a){
85         $contacts_id = $_POST['contact_batch'];
86         if (!is_array($contacts_id)) return;
87
88         $orig_records = q("SELECT * FROM `contact` WHERE `id` IN (%s) AND `uid` = %d AND `self` = 0",
89                 implode(",", $contacts_id),
90                 intval(local_user())
91         );
92
93         $count_actions=0;
94         foreach($orig_records as $orig_record) {
95                 $contact_id = $orig_record['id'];
96                 if (x($_POST, 'contacts_batch_update')) {
97                         _contact_update($contact_id);
98                         $count_actions++;
99                 }
100                 if (x($_POST, 'contacts_batch_block')) {
101                         $r  = _contact_block($contact_id, $orig_record);
102                         if ($r) $count_actions++;
103                 }
104                 if (x($_POST, 'contacts_batch_ignore')) {
105                         $r = _contact_ignore($contact_id, $orig_record);
106                         if ($r) $count_actions++;
107                 }
108                 if (x($_POST, 'contacts_batch_archive')) {
109                         $r = _contact_archive($contact_id, $orig_record);
110                         if ($r) $count_actions++;
111                 }
112                 if (x($_POST, 'contacts_batch_drop')) {
113                         _contact_drop($contact_id, $orig_record);
114                         $count_actions++;
115                 }
116         }
117         if ($count_actions>0) {
118                 info ( sprintf( tt("%d contact edited.", "%d contacts edited", $count_actions), $count_actions) );
119         }
120
121         if(x($_SESSION,'return_url'))
122                 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
123         else
124                 goaway($a->get_baseurl(true) . '/contacts');
125
126 }
127
128
129 function contacts_post(&$a) {
130
131         if(! local_user())
132                 return;
133
134         if ($a->argv[1]==="batch") {
135                 contacts_batch_actions($a);
136                 return;
137         }
138
139         $contact_id = intval($a->argv[1]);
140         if(! $contact_id)
141                 return;
142
143         $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
144                 intval($contact_id),
145                 intval(local_user())
146         );
147
148         if(! count($orig_record)) {
149                 notice( t('Could not access contact record.') . EOL);
150                 goaway($a->get_baseurl(true) . '/contacts');
151                 return; // NOTREACHED
152         }
153
154         call_hooks('contact_edit_post', $_POST);
155
156         $profile_id = intval($_POST['profile-assign']);
157         if($profile_id) {
158                 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
159                         intval($profile_id),
160                         intval(local_user())
161                 );
162                 if(! count($r)) {
163                         notice( t('Could not locate selected profile.') . EOL);
164                         return;
165                 }
166         }
167
168         $hidden = intval($_POST['hidden']);
169
170         $notify = intval($_POST['notify']);
171
172         $fetch_further_information = intval($_POST['fetch_further_information']);
173
174         $ffi_keyword_blacklist = fix_mce_lf(escape_tags(trim($_POST['ffi_keyword_blacklist'])));
175
176         $priority = intval($_POST['poll']);
177         if($priority > 5 || $priority < 0)
178                 $priority = 0;
179
180         $info = fix_mce_lf(escape_tags(trim($_POST['info'])));
181
182         $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `info` = '%s',
183                 `hidden` = %d, `notify_new_posts` = %d, `fetch_further_information` = %d,
184                 `ffi_keyword_blacklist` = '%s' WHERE `id` = %d AND `uid` = %d",
185                 intval($profile_id),
186                 intval($priority),
187                 dbesc($info),
188                 intval($hidden),
189                 intval($notify),
190                 intval($fetch_further_information),
191                 dbesc($ffi_keyword_blacklist),
192                 intval($contact_id),
193                 intval(local_user())
194         );
195         if($r)
196                 info( t('Contact updated.') . EOL);
197         else
198                 notice( t('Failed to update contact record.') . EOL);
199
200         $r = q("select * from contact where id = %d and uid = %d limit 1",
201                 intval($contact_id),
202                 intval(local_user())
203         );
204         if($r && count($r))
205                 $a->data['contact'] = $r[0];
206
207         return;
208
209 }
210
211 /*contact actions*/
212 function _contact_update($contact_id) {
213         $r = q("SELECT `uid`, `url`, `network` FROM `contact` WHERE `id` = %d", intval($contact_id));
214         if (!$r)
215                 return;
216
217         $uid = $r[0]["uid"];
218
219         if ($uid != local_user())
220                 return;
221
222         if ($r[0]["network"] == NETWORK_OSTATUS) {
223                 $result = new_contact($uid, $r[0]["url"], false);
224
225                 if ($result['success'])
226                         $r = q("UPDATE `contact` SET `subhub` = 1 WHERE `id` = %d",
227                                 intval($contact_id));
228         } else
229                 // pull feed and consume it, which should subscribe to the hub.
230                 proc_run('php',"include/onepoll.php","$contact_id", "force");
231 }
232
233 function _contact_update_profile($contact_id) {
234         $r = q("SELECT `uid`, `url`, `network` FROM `contact` WHERE `id` = %d", intval($contact_id));
235         if (!$r)
236                 return;
237
238         $uid = $r[0]["uid"];
239
240         if ($uid != local_user())
241                 return;
242
243         $data = probe_url($r[0]["url"]);
244
245         // "Feed" or "Unknown" is mostly a sign of communication problems
246         if ((in_array($data["network"], array(NETWORK_FEED, NETWORK_PHANTOM))) AND ($data["network"] != $r[0]["network"]))
247                 return;
248
249         $updatefields = array("name", "nick", "url", "addr", "batch", "notify", "poll", "request", "confirm",
250                                 "poco", "network", "alias");
251         $update = array();
252
253         if ($data["network"] == NETWORK_OSTATUS) {
254                 $result = new_contact($uid, $data["url"], false);
255
256                 if ($result['success'])
257                         $update["subhub"] = true;
258         }
259
260         foreach($updatefields AS $field)
261                 if (isset($data[$field]) AND ($data[$field] != ""))
262                         $update[$field] = $data[$field];
263
264         $update["nurl"] = normalise_link($data["url"]);
265
266         $query = "";
267
268         if (isset($data["priority"]) AND ($data["priority"] != 0))
269                 $query = "`priority` = ".intval($data["priority"]);
270
271         foreach($update AS $key => $value) {
272                 if ($query != "")
273                         $query .= ", ";
274
275                 $query .= "`".$key."` = '".dbesc($value)."'";
276         }
277
278         if ($query == "")
279                 return;
280
281         $r = q("UPDATE `contact` SET $query WHERE `id` = %d AND `uid` = %d",
282                 intval($contact_id),
283                 intval(local_user())
284         );
285
286         $photos = import_profile_photo($data['photo'], local_user(), $contact_id);
287
288         $r = q("UPDATE `contact` SET `photo` = '%s',
289                         `thumb` = '%s',
290                         `micro` = '%s',
291                         `name-date` = '%s',
292                         `uri-date` = '%s',
293                         `avatar-date` = '%s'
294                         WHERE `id` = %d",
295                         dbesc($photos[0]),
296                         dbesc($photos[1]),
297                         dbesc($photos[2]),
298                         dbesc(datetime_convert()),
299                         dbesc(datetime_convert()),
300                         dbesc(datetime_convert()),
301                         intval($contact_id)
302                 );
303
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         require_once('include/Contact.php');
339         $a = get_app();
340
341         terminate_friendship($a->user,$a->contact,$orig_record);
342         contact_remove($orig_record['id']);
343 }
344
345
346 function contacts_content(&$a) {
347
348         $sort_type = 0;
349         $o = '';
350         nav_set_selected('contacts');
351
352
353         if(! local_user()) {
354                 notice( t('Permission denied.') . EOL);
355                 return;
356         }
357
358         if($a->argc == 3) {
359
360                 $contact_id = intval($a->argv[1]);
361                 if(! $contact_id)
362                         return;
363
364                 $cmd = $a->argv[2];
365
366                 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1",
367                         intval($contact_id),
368                         intval(local_user())
369                 );
370
371                 if(! count($orig_record)) {
372                         notice( t('Could not access contact record.') . EOL);
373                         goaway($a->get_baseurl(true) . '/contacts');
374                         return; // NOTREACHED
375                 }
376
377                 if($cmd === 'update') {
378                         _contact_update($contact_id);
379                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
380                         // NOTREACHED
381                 }
382
383                 if($cmd === 'updateprofile') {
384                         _contact_update_profile($contact_id);
385                         goaway($a->get_baseurl(true) . '/crepair/' . $contact_id);
386                         // NOTREACHED
387                 }
388
389                 if($cmd === 'block') {
390                         $r = _contact_block($contact_id, $orig_record[0]);
391                         if($r) {
392                                 $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
393                                 info((($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')).EOL);
394                         }
395
396                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
397                         return; // NOTREACHED
398                 }
399
400                 if($cmd === 'ignore') {
401                         $r = _contact_ignore($contact_id, $orig_record[0]);
402                         if($r) {
403                                 $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
404                                 info((($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')).EOL);
405                         }
406
407                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
408                         return; // NOTREACHED
409                 }
410
411
412                 if($cmd === 'archive') {
413                         $r = _contact_archive($contact_id, $orig_record[0]);
414                         if($r) {
415                                 $archived = (($orig_record[0]['archive']) ? 0 : 1);
416                                 info((($archived) ? t('Contact has been archived') : t('Contact has been unarchived')).EOL);
417                         }
418
419                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
420                         return; // NOTREACHED
421                 }
422
423                 if($cmd === 'drop') {
424
425                         // Check if we should do HTML-based delete confirmation
426                         if($_REQUEST['confirm']) {
427                                 // <form> can't take arguments in its "action" parameter
428                                 // so add any arguments as hidden inputs
429                                 $query = explode_querystring($a->query_string);
430                                 $inputs = array();
431                                 foreach($query['args'] as $arg) {
432                                         if(strpos($arg, 'confirm=') === false) {
433                                                 $arg_parts = explode('=', $arg);
434                                                 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
435                                         }
436                                 }
437
438                                 $a->page['aside'] = '';
439
440                                 return replace_macros(get_markup_template('contact_drop_confirm.tpl'), array(
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($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
455                                 else
456                                         goaway($a->get_baseurl(true) . '/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($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
463                         else
464                                 goaway($a->get_baseurl(true) . '/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' => $a->get_baseurl(true),
487                         '$editselect' => $editselect,
488                 ));
489                 $a->page['end'] .= replace_macros(get_markup_template('contact_end.tpl'), array(
490                         '$baseurl' => $a->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 = contact_tabs($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 = $a->get_baseurl(true)."/follow?url=".urlencode($contact["url"]);
571
572
573                 $header = $contact["name"];
574
575                 if ($contact["addr"] != "")
576                         $header .= " <".$contact["addr"].">";
577
578                 $header .= " (".network_to_name($contact['network'], $contact['url']).")";
579
580                 $o .= replace_macros($tpl, array(
581                         //'$header' => t('Contact Editor'),
582                         '$header' => htmlentities($header),
583                         '$tab_str' => $tab_str,
584                         '$submit' => t('Submit'),
585                         '$lbl_vis1' => t('Profile Visibility'),
586                         '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
587                         '$lbl_info1' => t('Contact Information / Notes'),
588                         '$infedit' => t('Edit contact notes'),
589                         '$common_text' => $common_text,
590                         '$common_link' => $a->get_baseurl(true) . '/common/loc/' . local_user() . '/' . $contact['id'],
591                         '$all_friends' => $all_friends,
592                         '$relation_text' => $relation_text,
593                         '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
594                         '$blockunblock' => t('Block/Unblock contact'),
595                         '$ignorecont' => t('Ignore contact'),
596                         '$lblcrepair' => t("Repair URL settings"),
597                         '$lblrecent' => t('View conversations'),
598                         '$lblsuggest' => $lblsuggest,
599                         '$delete' => t('Delete contact'),
600                         '$nettype' => $nettype,
601                         '$poll_interval' => $poll_interval,
602                         '$poll_enabled' => $poll_enabled,
603                         '$lastupdtext' => t('Last update:'),
604                         '$lost_contact' => $lost_contact,
605                         '$updpub' => t('Update public posts'),
606                         '$last_update' => $last_update,
607                         '$udnow' => t('Update now'),
608                         '$follow' => $follow,
609                         '$follow_text' => t("Connect/Follow"),
610                         '$profile_select' => $profile_select,
611                         '$contact_id' => $contact['id'],
612                         '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
613                         '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
614                         '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
615                         '$info' => $contact['info'],
616                         '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
617                         '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
618                         '$archived' => (($contact['archive']) ? t('Currently archived') : ''),
619                         '$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')),
620                         '$notify' => array('notify', t('Notification for new posts'), ($contact['notify_new_posts'] == 1), t('Send a notification of every new post of this contact')),
621                         '$fetch_further_information' => $fetch_further_information,
622                         '$ffi_keyword_blacklist' => $contact['ffi_keyword_blacklist'],
623                         '$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')),
624                         '$photo' => $contact['photo'],
625                         '$name' => htmlentities($contact['name']),
626                         '$dir_icon' => $dir_icon,
627                         '$alt_text' => $alt_text,
628                         '$sparkle' => $sparkle,
629                         '$url' => $url,
630                         '$profileurllabel' => t('Profile URL'),
631                         '$profileurl' => $contact['url'],
632                         '$location' => bbcode($contact["location"]),
633                         '$location_label' => t("Location:"),
634                         '$about' => bbcode($contact["about"], false, false),
635                         '$about_label' => t("About:"),
636                         '$keywords' => $contact["keywords"],
637                         '$keywords_label' => t("Tags:")
638
639                 ));
640
641                 $arr = array('contact' => $contact,'output' => $o);
642
643                 call_hooks('contact_edit', $arr);
644
645                 return $arr['output'];
646
647         }
648
649         $blocked = false;
650         $hidden = false;
651         $ignored = false;
652         $all = false;
653
654         if(($a->argc == 2) && ($a->argv[1] === 'all')) {
655                 $sql_extra = '';
656                 $all = true;
657         }
658         elseif(($a->argc == 2) && ($a->argv[1] === 'blocked')) {
659                 $sql_extra = " AND `blocked` = 1 ";
660                 $blocked = true;
661         }
662         elseif(($a->argc == 2) && ($a->argv[1] === 'hidden')) {
663                 $sql_extra = " AND `hidden` = 1 ";
664                 $hidden = true;
665         }
666         elseif(($a->argc == 2) && ($a->argv[1] === 'ignored')) {
667                 $sql_extra = " AND `readonly` = 1 ";
668                 $ignored = true;
669         }
670         elseif(($a->argc == 2) && ($a->argv[1] === 'archived')) {
671                 $sql_extra = " AND `archive` = 1 ";
672                 $archived = true;
673         }
674         else
675                 $sql_extra = " AND `blocked` = 0 ";
676
677         $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
678         $nets = ((x($_GET,'nets')) ? notags(trim($_GET['nets'])) : '');
679
680         $tabs = array(
681                 array(
682                         'label' => t('Suggestions'),
683                         'url'   => $a->get_baseurl(true) . '/suggest',
684                         'sel'   => '',
685                         'title' => t('Suggest potential friends'),
686                         'id'    => 'suggestions-tab',
687                         'accesskey' => 'g',
688                 ),
689                 array(
690                         'label' => t('All Contacts'),
691                         'url'   => $a->get_baseurl(true) . '/contacts/all',
692                         'sel'   => ($all) ? 'active' : '',
693                         'title' => t('Show all contacts'),
694                         'id'    => 'showall-tab',
695                         'accesskey' => 'l',
696                 ),
697                 array(
698                         'label' => t('Unblocked'),
699                         'url'   => $a->get_baseurl(true) . '/contacts',
700                         'sel'   => ((! $all) && (! $blocked) && (! $hidden) && (! $search) && (! $nets) && (! $ignored) && (! $archived)) ? 'active' : '',
701                         'title' => t('Only show unblocked contacts'),
702                         'id'    => 'showunblocked-tab',
703                         'accesskey' => 'o',
704                 ),
705
706                 array(
707                         'label' => t('Blocked'),
708                         'url'   => $a->get_baseurl(true) . '/contacts/blocked',
709                         'sel'   => ($blocked) ? 'active' : '',
710                         'title' => t('Only show blocked contacts'),
711                         'id'    => 'showblocked-tab',
712                         'accesskey' => 'b',
713                 ),
714
715                 array(
716                         'label' => t('Ignored'),
717                         'url'   => $a->get_baseurl(true) . '/contacts/ignored',
718                         'sel'   => ($ignored) ? 'active' : '',
719                         'title' => t('Only show ignored contacts'),
720                         'id'    => 'showignored-tab',
721                         'accesskey' => 'i',
722                 ),
723
724                 array(
725                         'label' => t('Archived'),
726                         'url'   => $a->get_baseurl(true) . '/contacts/archived',
727                         'sel'   => ($archived) ? 'active' : '',
728                         'title' => t('Only show archived contacts'),
729                         'id'    => 'showarchived-tab',
730                         'accesskey' => 'y',
731                 ),
732
733                 array(
734                         'label' => t('Hidden'),
735                         'url'   => $a->get_baseurl(true) . '/contacts/hidden',
736                         'sel'   => ($hidden) ? 'active' : '',
737                         'title' => t('Only show hidden contacts'),
738                         'id'    => 'showhidden-tab',
739                         'accesskey' => 'h',
740                 ),
741
742         );
743
744         $tab_tpl = get_markup_template('common_tabs.tpl');
745         $t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
746
747
748
749         $searching = false;
750         if($search) {
751                 $search_hdr = $search;
752                 $search_txt = dbesc(protect_sprintf(preg_quote($search)));
753                 $searching = true;
754         }
755         $sql_extra .= (($searching) ? " AND (name REGEXP '$search_txt' OR url REGEXP '$search_txt'  OR nick REGEXP '$search_txt') " : "");
756
757         if($nets)
758                 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
759
760         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : '');
761
762
763         $r = q("SELECT COUNT(*) AS `total` FROM `contact`
764                 WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ",
765                 intval($_SESSION['uid']));
766         if(count($r)) {
767                 $a->set_pager_total($r[0]['total']);
768                 $total = $r[0]['total'];
769         }
770
771         $sql_extra3 = unavailable_networks();
772
773         $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 ",
774                 intval($_SESSION['uid']),
775                 intval($a->pager['start']),
776                 intval($a->pager['itemspage'])
777         );
778
779         $contacts = array();
780
781         if(count($r)) {
782                 foreach($r as $rr) {
783                         $contacts[] = _contact_detail_for_template($rr);
784                 }
785         }
786
787         $tpl = get_markup_template("contacts-template.tpl");
788         $o .= replace_macros($tpl, array(
789                 '$baseurl' => $a->get_baseurl(),
790                 '$header' => t('Contacts') . (($nets) ? ' - ' . network_to_name($nets) : ''),
791                 '$tabs' => $t,
792                 '$total' => $total,
793                 '$search' => $search_hdr,
794                 '$desc' => t('Search your contacts'),
795                 '$finding' => (($searching) ? t('Finding: ') . "'" . $search . "'" : ""),
796                 '$submit' => t('Find'),
797                 '$cmd' => $a->cmd,
798                 '$contacts' => $contacts,
799                 '$contact_drop_confirm' => t('Do you really want to delete this contact?'),
800                 'multiselect' => 1,
801                 '$batch_actions' => array(
802                         'contacts_batch_update' => t('Update'),
803                         'contacts_batch_block' => t('Block')."/".t("Unblock"),
804                         "contacts_batch_ignore" => t('Ignore')."/".t("Unignore"),
805                         "contacts_batch_archive" => t('Archive')."/".t("Unarchive"),
806                         "contacts_batch_drop" => t('Delete'),
807                 ),
808                 '$paginate' => paginate($a),
809
810         ));
811
812         return $o;
813 }
814
815 function contact_tabs($a, $contact_id, $active_tab) {
816         // tabs
817         $tabs = array(
818                 array(
819                         'label'=>t('Status'),
820                         'url' => "contacts/".$contact_id."/posts",
821                         'sel' => (($active_tab == 1)?'active':''),
822                         'title' => t('Status Messages and Posts'),
823                         'id' => 'status-tab',
824                         'accesskey' => 'm',
825                 ),
826                 array(
827                         'label'=>t('Profile'),
828                         'url' => "contacts/".$contact_id,
829                         'sel' => (($active_tab == 2)?'active':''),
830                         'title' => t('Profile Details'),
831                         'id' => 'status-tab',
832                         'accesskey' => 'r',
833                 ),
834                 array(
835                         'label' => t('Repair'),
836                         'url'   => $a->get_baseurl(true) . '/crepair/' . $contact_id,
837                         'sel' => (($active_tab == 3)?'active':''),
838                         'title' => t('Advanced Contact Settings'),
839                         'id'    => 'repair-tab',
840                         'accesskey' => 'r',
841                 ),
842                 array(
843                         'label' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
844                         'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/block',
845                         'sel'   => '',
846                         'title' => t('Toggle Blocked status'),
847                         'id'    => 'toggle-block-tab',
848                         'accesskey' => 'b',
849                 ),
850                 array(
851                         'label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
852                         'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/ignore',
853                         'sel'   => '',
854                         'title' => t('Toggle Ignored status'),
855                         'id'    => 'toggle-ignore-tab',
856                         'accesskey' => 'i',
857                 ),
858                 array(
859                         'label' => (($contact['archive']) ? t('Unarchive') : t('Archive') ),
860                         'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/archive',
861                         'sel'   => '',
862                         'title' => t('Toggle Archive status'),
863                         'id'    => 'toggle-archive-tab',
864                         'accesskey' => 'v',
865                 )
866         );
867         $tab_tpl = get_markup_template('common_tabs.tpl');
868         $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
869
870         return $tab_str;
871 }
872
873 function contact_posts($a, $contact_id) {
874
875         require_once('include/conversation.php');
876
877         $r = q("SELECT * FROM `contact` WHERE `id` = %d", intval($contact_id));
878         if ($r) {
879                 $contact = $r[0];
880                 $a->page['aside'] = "";
881                 profile_load($a, "", 0, get_contact_details_by_url($contact["url"]));
882         }
883
884         if(get_config('system', 'old_pager')) {
885                 $r = q("SELECT COUNT(*) AS `total` FROM `item`
886                         WHERE `item`.`uid` = %d AND (`author-link` = '%s')",
887                         intval(local_user()), dbesc($contact["url"]));
888
889                 $a->set_pager_total($r[0]['total']);
890         }
891
892         $r = q("SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
893                         `author-name` AS `name`, `owner-avatar` AS `photo`,
894                         `owner-link` AS `url`, `owner-avatar` AS `thumb`
895                 FROM `item` FORCE INDEX (uid_contactid_created)
896                 WHERE `item`.`uid` = %d AND `contact-id` = %d
897                         AND (`author-link` = '%s')
898                 ORDER BY `item`.`created` DESC LIMIT %d, %d",
899                 intval(local_user()),
900                 intval($contact_id),
901                 dbesc($contact["url"]),
902                 intval($a->pager['start']),
903                 intval($a->pager['itemspage'])
904         );
905
906         $tab_str = contact_tabs($a, $contact_id, 1);
907
908         $header = $contact["name"];
909
910         if ($contact["addr"] != "")
911                 $header .= " <".$contact["addr"].">";
912
913         $header .= " (".network_to_name($contact['network'], $contact['url']).")";
914
915         $tpl = get_markup_template("section_title.tpl");
916         $o = replace_macros($tpl,array(
917                 '$title' => htmlentities($header)
918         ));
919
920         $o .= $tab_str;
921
922         $o .= conversation($a,$r,'community',false);
923
924         if(!get_config('system', 'old_pager')) {
925                 $o .= alt_pager($a,count($r));
926         } else {
927                 $o .= paginate($a);
928         }
929
930         return $o;
931 }
932
933 function _contact_detail_for_template($rr){
934         switch($rr['rel']) {
935                 case CONTACT_IS_FRIEND:
936                         $dir_icon = 'images/lrarrow.gif';
937                         $alt_text = t('Mutual Friendship');
938                         break;
939                 case  CONTACT_IS_FOLLOWER;
940                         $dir_icon = 'images/larrow.gif';
941                         $alt_text = t('is a fan of yours');
942                         break;
943                 case CONTACT_IS_SHARING;
944                         $dir_icon = 'images/rarrow.gif';
945                         $alt_text = t('you are a fan of');
946                         break;
947                 default:
948                         break;
949         }
950         if(($rr['network'] === NETWORK_DFRN) && ($rr['rel'])) {
951                 $url = "redir/{$rr['id']}";
952                 $sparkle = ' class="sparkle" ';
953         }
954         else {
955                 $url = $rr['url'];
956                 $sparkle = '';
957         }
958
959
960         return array(
961                 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
962                 'edit_hover' => t('Edit contact'),
963                 'photo_menu' => contact_photo_menu($rr),
964                 'id' => $rr['id'],
965                 'alt_text' => $alt_text,
966                 'dir_icon' => $dir_icon,
967                 'thumb' => proxy_url($rr['thumb'], false, PROXY_SIZE_THUMB),
968                 'name' => htmlentities($rr['name']),
969                 'username' => htmlentities($rr['name']),
970                 'sparkle' => $sparkle,
971                 'itemurl' => (($rr['addr'] != "") ? $rr['addr'] : $rr['url']),
972                 'url' => $url,
973                 'network' => network_to_name($rr['network'], $rr['url']),
974         );
975
976 }