3 require_once('include/Contact.php');
4 require_once('include/socgraph.php');
5 require_once('include/contact_selectors.php');
6 require_once('mod/proxy.php');
8 function contacts_init(&$a) {
14 if(($a->argc == 2) && intval($a->argv[1])) {
15 $contact_id = intval($a->argv[1]);
16 $r = q("SELECT * FROM `contact` WHERE `uid` = %d and `id` = %d LIMIT 1",
25 require_once('include/group.php');
26 require_once('include/contact_widgets.php');
28 if(! x($a->page,'aside'))
29 $a->page['aside'] = '';
32 $a->data['contact'] = $r[0];
33 $vcard_widget = replace_macros(get_markup_template("vcard-widget.tpl"),array(
34 '$name' => $a->data['contact']['name'],
35 '$photo' => $a->data['contact']['photo']
41 if (isset($_GET['add']))
42 $follow_widget = follow_widget($_GET['add']);
44 $follow_widget = follow_widget();
47 if ($_GET['nets'] == "all")
50 $groups_widget .= group_side('contacts','group',false,0,$contact_id);
51 $findpeople_widget .= findpeople_widget();
52 $networks_widget .= networks_widget('contacts',$_GET['nets']);
53 $a->page['aside'] .= replace_macros(get_markup_template("contacts-widget-sidebar.tpl"),array(
54 '$vcard_widget' => $vcard_widget,
55 '$follow_widget' => $follow_widget,
56 '$groups_widget' => $groups_widget,
57 '$findpeople_widget' => $findpeople_widget,
58 '$networks_widget' => $networks_widget
61 $base = $a->get_baseurl();
62 $tpl = get_markup_template("contacts-head.tpl");
63 $a->page['htmlhead'] .= replace_macros($tpl,array(
64 '$baseurl' => $a->get_baseurl(true),
68 $tpl = get_markup_template("contacts-end.tpl");
69 $a->page['end'] .= replace_macros($tpl,array(
70 '$baseurl' => $a->get_baseurl(true),
77 function contacts_batch_actions(&$a){
78 $contacts_id = $_POST['contact_batch'];
79 if (!is_array($contacts_id)) return;
81 $orig_records = q("SELECT * FROM `contact` WHERE `id` IN (%s) AND `uid` = %d AND `self` = 0",
82 implode(",", $contacts_id),
87 foreach($orig_records as $orig_record) {
88 $contact_id = $orig_record['id'];
89 if (x($_POST, 'contacts_batch_update')) {
90 _contact_update($contact_id);
93 if (x($_POST, 'contacts_batch_block')) {
94 $r = _contact_block($contact_id, $orig_record);
95 if ($r) $count_actions++;
97 if (x($_POST, 'contacts_batch_ignore')) {
98 $r = _contact_ignore($contact_id, $orig_record);
99 if ($r) $count_actions++;
101 if (x($_POST, 'contacts_batch_archive')) {
102 $r = _contact_archive($contact_id, $orig_record);
103 if ($r) $count_actions++;
105 if (x($_POST, 'contacts_batch_drop')) {
106 _contact_drop($contact_id, $orig_record);
110 if ($count_actions>0) {
111 info ( sprintf( tt("%d contact edited.", "%d contacts edited", $count_actions), $count_actions) );
114 if(x($_SESSION,'return_url'))
115 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
117 goaway($a->get_baseurl(true) . '/contacts');
122 function contacts_post(&$a) {
127 if ($a->argv[1]==="batch") {
128 contacts_batch_actions($a);
132 $contact_id = intval($a->argv[1]);
136 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
141 if(! count($orig_record)) {
142 notice( t('Could not access contact record.') . EOL);
143 goaway($a->get_baseurl(true) . '/contacts');
144 return; // NOTREACHED
147 call_hooks('contact_edit_post', $_POST);
149 $profile_id = intval($_POST['profile-assign']);
151 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
156 notice( t('Could not locate selected profile.') . EOL);
161 $hidden = intval($_POST['hidden']);
163 $notify = intval($_POST['notify']);
165 $fetch_further_information = intval($_POST['fetch_further_information']);
167 $ffi_keyword_blacklist = fix_mce_lf(escape_tags(trim($_POST['ffi_keyword_blacklist'])));
169 $priority = intval($_POST['poll']);
170 if($priority > 5 || $priority < 0)
173 $info = fix_mce_lf(escape_tags(trim($_POST['info'])));
175 $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `info` = '%s',
176 `hidden` = %d, `notify_new_posts` = %d, `fetch_further_information` = %d,
177 `ffi_keyword_blacklist` = '%s' WHERE `id` = %d AND `uid` = %d",
183 intval($fetch_further_information),
184 dbesc($ffi_keyword_blacklist),
189 info( t('Contact updated.') . EOL);
191 notice( t('Failed to update contact record.') . EOL);
193 $r = q("select * from contact where id = %d and uid = %d limit 1",
198 $a->data['contact'] = $r[0];
205 function _contact_update($contact_id) {
206 // pull feed and consume it, which should subscribe to the hub.
207 proc_run('php',"include/poller.php","$contact_id");
209 function _contact_block($contact_id, $orig_record) {
210 $blocked = (($orig_record['blocked']) ? 0 : 1);
211 $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d",
219 function _contact_ignore($contact_id, $orig_record) {
220 $readonly = (($orig_record['readonly']) ? 0 : 1);
221 $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d",
228 function _contact_archive($contact_id, $orig_record) {
229 $archived = (($orig_record['archive']) ? 0 : 1);
230 $r = q("UPDATE `contact` SET `archive` = %d WHERE `id` = %d AND `uid` = %d",
236 q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact_id), intval(local_user()));
240 function _contact_drop($contact_id, $orig_record) {
241 require_once('include/Contact.php');
244 terminate_friendship($a->user,$a->contact,$orig_record);
245 contact_remove($orig_record['id']);
249 function contacts_content(&$a) {
253 nav_set_selected('contacts');
257 notice( t('Permission denied.') . EOL);
263 $contact_id = intval($a->argv[1]);
269 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1",
274 if(! count($orig_record)) {
275 notice( t('Could not access contact record.') . EOL);
276 goaway($a->get_baseurl(true) . '/contacts');
277 return; // NOTREACHED
280 if($cmd === 'update') {
281 _contact_update($contact_id);
282 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
286 if($cmd === 'block') {
287 $r = _contact_block($contact_id, $orig_record[0]);
289 $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
290 info((($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')).EOL);
293 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
294 return; // NOTREACHED
297 if($cmd === 'ignore') {
298 $r = _contact_ignore($contact_id, $orig_record[0]);
300 $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
301 info((($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')).EOL);
304 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
305 return; // NOTREACHED
309 if($cmd === 'archive') {
310 $r = _contact_archive($contact_id, $orig_record[0]);
312 $archived = (($orig_record[0]['archive']) ? 0 : 1);
313 info((($archived) ? t('Contact has been archived') : t('Contact has been unarchived')).EOL);
316 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
317 return; // NOTREACHED
320 if($cmd === 'drop') {
322 // Check if we should do HTML-based delete confirmation
323 if($_REQUEST['confirm']) {
324 // <form> can't take arguments in its "action" parameter
325 // so add any arguments as hidden inputs
326 $query = explode_querystring($a->query_string);
328 foreach($query['args'] as $arg) {
329 if(strpos($arg, 'confirm=') === false) {
330 $arg_parts = explode('=', $arg);
331 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
335 $a->page['aside'] = '';
336 return replace_macros(get_markup_template('confirm.tpl'), array(
338 '$message' => t('Do you really want to delete this contact?'),
339 '$extra_inputs' => $inputs,
340 '$confirm' => t('Yes'),
341 '$confirm_url' => $query['base'],
342 '$confirm_name' => 'confirmed',
343 '$cancel' => t('Cancel'),
346 // Now check how the user responded to the confirmation query
347 if($_REQUEST['canceled']) {
348 if(x($_SESSION,'return_url'))
349 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
351 goaway($a->get_baseurl(true) . '/contacts');
354 _contact_drop($contact_id, $orig_record[0]);
355 info( t('Contact has been removed.') . EOL );
356 if(x($_SESSION,'return_url'))
357 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
359 goaway($a->get_baseurl(true) . '/contacts');
360 return; // NOTREACHED
366 $_SESSION['return_url'] = $a->query_string;
368 if((x($a->data,'contact')) && (is_array($a->data['contact']))) {
370 $contact_id = $a->data['contact']['id'];
371 $contact = $a->data['contact'];
373 $editselect = 'none';
374 if( feature_enabled(local_user(),'richtext') )
375 $editselect = 'exact';
377 $a->page['htmlhead'] .= replace_macros(get_markup_template('contact_head.tpl'), array(
378 '$baseurl' => $a->get_baseurl(true),
379 '$editselect' => $editselect,
381 $a->page['end'] .= replace_macros(get_markup_template('contact_end.tpl'), array(
382 '$baseurl' => $a->get_baseurl(true),
383 '$editselect' => $editselect,
386 require_once('include/contact_selectors.php');
388 $tpl = get_markup_template("contact_edit.tpl");
390 switch($contact['rel']) {
391 case CONTACT_IS_FRIEND:
392 $dir_icon = 'images/lrarrow.gif';
393 $relation_text = t('You are mutual friends with %s');
395 case CONTACT_IS_FOLLOWER;
396 $dir_icon = 'images/larrow.gif';
397 $relation_text = t('You are sharing with %s');
400 case CONTACT_IS_SHARING;
401 $dir_icon = 'images/rarrow.gif';
402 $relation_text = t('%s is sharing with you');
408 if(!in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
411 $relation_text = sprintf($relation_text,$contact['name']);
413 if(($contact['network'] === NETWORK_DFRN) && ($contact['rel'])) {
414 $url = "redir/{$contact['id']}";
415 $sparkle = ' class="sparkle" ';
418 $url = $contact['url'];
422 $insecure = t('Private communications are not available for this contact.');
424 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00')
426 : datetime_convert('UTC',date_default_timezone_get(),$contact['last-update'],'D, j M Y, g:i A'));
428 if($contact['last-update'] !== '0000-00-00 00:00:00')
429 $last_update .= ' ' . (($contact['last-update'] <= $contact['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
431 $lblsuggest = (($contact['network'] === NETWORK_DFRN) ? t('Suggest friends') : '');
433 $poll_enabled = in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2));
435 $nettype = sprintf( t('Network type: %s'),network_to_name($contact['network']));
437 $common = count_common_friends(local_user(),$contact['id']);
438 $common_text = (($common) ? sprintf( tt('%d contact in common','%d contacts in common', $common),$common) : '');
440 $polling = (($contact['network'] === NETWORK_MAIL | $contact['network'] === NETWORK_FEED) ? 'polling' : '');
442 $x = count_all_friends(local_user(), $contact['id']);
443 $all_friends = (($x) ? t('View all contacts') : '');
448 'label' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
449 'url' => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/block',
451 'title' => t('Toggle Blocked status'),
454 'label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
455 'url' => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/ignore',
457 'title' => t('Toggle Ignored status'),
461 'label' => (($contact['archive']) ? t('Unarchive') : t('Archive') ),
462 'url' => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/archive',
464 'title' => t('Toggle Archive status'),
467 'label' => t('Repair'),
468 'url' => $a->get_baseurl(true) . '/crepair/' . $contact_id,
470 'title' => t('Advanced Contact Settings'),
473 $tab_tpl = get_markup_template('common_tabs.tpl');
474 $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
476 $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!') : '');
478 if ($contact['network'] == NETWORK_FEED)
479 $fetch_further_information = array('fetch_further_information', t('Fetch further information for feeds'), $contact['fetch_further_information'], t('Fetch further information for feeds'),
480 array('0'=>t('Disabled'), '1'=>t('Fetch information'), '2'=>t('Fetch information and keywords')));
482 if (in_array($contact['network'], array(NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2)))
483 $poll_interval = contact_poll_interval($contact['priority'],(! $poll_enabled));
485 if ($contact['network'] == NETWORK_DFRN)
486 $profile_select = contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false));
488 $o .= replace_macros($tpl, array(
489 '$header' => t('Contact Editor'),
490 '$tab_str' => $tab_str,
491 '$submit' => t('Submit'),
492 '$lbl_vis1' => t('Profile Visibility'),
493 '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
494 '$lbl_info1' => t('Contact Information / Notes'),
495 '$infedit' => t('Edit contact notes'),
496 '$common_text' => $common_text,
497 '$common_link' => $a->get_baseurl(true) . '/common/loc/' . local_user() . '/' . $contact['id'],
498 '$all_friends' => $all_friends,
499 '$relation_text' => $relation_text,
500 '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
501 '$blockunblock' => t('Block/Unblock contact'),
502 '$ignorecont' => t('Ignore contact'),
503 '$lblcrepair' => t("Repair URL settings"),
504 '$lblrecent' => t('View conversations'),
505 '$lblsuggest' => $lblsuggest,
506 '$delete' => t('Delete contact'),
507 '$nettype' => $nettype,
508 '$poll_interval' => $poll_interval,
509 '$poll_enabled' => $poll_enabled,
510 '$lastupdtext' => t('Last update:'),
511 '$lost_contact' => $lost_contact,
512 '$updpub' => t('Update public posts'),
513 '$last_update' => $last_update,
514 '$udnow' => t('Update now'),
515 '$profile_select' => $profile_select,
516 '$contact_id' => $contact['id'],
517 '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
518 '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
519 '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
520 '$info' => $contact['info'],
521 '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
522 '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
523 '$archived' => (($contact['archive']) ? t('Currently archived') : ''),
524 '$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')),
525 '$notify' => array('notify', t('Notification for new posts'), ($contact['notify_new_posts'] == 1), t('Send a notification of every new post of this contact')),
526 '$fetch_further_information' => $fetch_further_information,
527 '$ffi_keyword_blacklist' => $contact['ffi_keyword_blacklist'],
528 '$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')),
529 '$photo' => $contact['photo'],
530 '$name' => $contact['name'],
531 '$dir_icon' => $dir_icon,
532 '$alt_text' => $alt_text,
533 '$sparkle' => $sparkle,
538 $arr = array('contact' => $contact,'output' => $o);
540 call_hooks('contact_edit', $arr);
542 return $arr['output'];
551 if(($a->argc == 2) && ($a->argv[1] === 'all')) {
555 elseif(($a->argc == 2) && ($a->argv[1] === 'blocked')) {
556 $sql_extra = " AND `blocked` = 1 ";
559 elseif(($a->argc == 2) && ($a->argv[1] === 'hidden')) {
560 $sql_extra = " AND `hidden` = 1 ";
563 elseif(($a->argc == 2) && ($a->argv[1] === 'ignored')) {
564 $sql_extra = " AND `readonly` = 1 ";
567 elseif(($a->argc == 2) && ($a->argv[1] === 'archived')) {
568 $sql_extra = " AND `archive` = 1 ";
572 $sql_extra = " AND `blocked` = 0 ";
574 $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
575 $nets = ((x($_GET,'nets')) ? notags(trim($_GET['nets'])) : '');
579 'label' => t('Suggestions'),
580 'url' => $a->get_baseurl(true) . '/suggest',
582 'title' => t('Suggest potential friends'),
585 'label' => t('All Contacts'),
586 'url' => $a->get_baseurl(true) . '/contacts/all',
587 'sel' => ($all) ? 'active' : '',
588 'title' => t('Show all contacts'),
591 'label' => t('Unblocked'),
592 'url' => $a->get_baseurl(true) . '/contacts',
593 'sel' => ((! $all) && (! $blocked) && (! $hidden) && (! $search) && (! $nets) && (! $ignored) && (! $archived)) ? 'active' : '',
594 'title' => t('Only show unblocked contacts'),
598 'label' => t('Blocked'),
599 'url' => $a->get_baseurl(true) . '/contacts/blocked',
600 'sel' => ($blocked) ? 'active' : '',
601 'title' => t('Only show blocked contacts'),
605 'label' => t('Ignored'),
606 'url' => $a->get_baseurl(true) . '/contacts/ignored',
607 'sel' => ($ignored) ? 'active' : '',
608 'title' => t('Only show ignored contacts'),
612 'label' => t('Archived'),
613 'url' => $a->get_baseurl(true) . '/contacts/archived',
614 'sel' => ($archived) ? 'active' : '',
615 'title' => t('Only show archived contacts'),
619 'label' => t('Hidden'),
620 'url' => $a->get_baseurl(true) . '/contacts/hidden',
621 'sel' => ($hidden) ? 'active' : '',
622 'title' => t('Only show hidden contacts'),
627 $tab_tpl = get_markup_template('common_tabs.tpl');
628 $t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
634 $search_hdr = $search;
635 $search_txt = dbesc(protect_sprintf(preg_quote($search)));
638 $sql_extra .= (($searching) ? " AND (name REGEXP '$search_txt' OR url REGEXP '$search_txt' OR nick REGEXP '$search_txt') " : "");
641 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
643 $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : '');
646 $r = q("SELECT COUNT(*) AS `total` FROM `contact`
647 WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ",
648 intval($_SESSION['uid']));
650 $a->set_pager_total($r[0]['total']);
651 $total = $r[0]['total'];
655 $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 ",
656 intval($_SESSION['uid']),
657 intval($a->pager['start']),
658 intval($a->pager['itemspage'])
668 case CONTACT_IS_FRIEND:
669 $dir_icon = 'images/lrarrow.gif';
670 $alt_text = t('Mutual Friendship');
672 case CONTACT_IS_FOLLOWER;
673 $dir_icon = 'images/larrow.gif';
674 $alt_text = t('is a fan of yours');
676 case CONTACT_IS_SHARING;
677 $dir_icon = 'images/rarrow.gif';
678 $alt_text = t('you are a fan of');
683 if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
684 $url = "redir/{$rr['id']}";
685 $sparkle = ' class="sparkle" ';
694 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
695 'edit_hover' => t('Edit contact'),
696 'photo_menu' => contact_photo_menu($rr),
698 'alt_text' => $alt_text,
699 'dir_icon' => $dir_icon,
700 'thumb' => proxy_url($rr['thumb']),
701 'name' => $rr['name'],
702 'username' => $rr['name'],
703 'sparkle' => $sparkle,
704 'itemurl' => $rr['url'],
706 'network' => network_to_name($rr['network']),
714 $tpl = get_markup_template("contacts-template.tpl");
715 $o .= replace_macros($tpl, array(
716 '$baseurl' => $a->get_baseurl(),
717 '$header' => t('Contacts') . (($nets) ? ' - ' . network_to_name($nets) : ''),
720 '$search' => $search_hdr,
721 '$desc' => t('Search your contacts'),
722 '$finding' => (($searching) ? t('Finding: ') . "'" . $search . "'" : ""),
723 '$submit' => t('Find'),
725 '$contacts' => $contacts,
726 '$contact_drop_confirm' => t('Do you really want to delete this contact?'),
727 '$batch_actions' => array(
728 'contacts_batch_update' => t('Update'),
729 'contacts_batch_block' => t('Block')."/".t("Unblock"),
730 "contacts_batch_ignore" => t('Ignore')."/".t("Unignore"),
731 "contacts_batch_archive" => t('Archive')."/".t("Unarchive"),
732 "contacts_batch_drop" => t('Delete'),
734 '$paginate' => paginate($a),