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