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 $o .= replace_macros($tpl, array(
486 '$header' => t('Contact Editor'),
487 '$tab_str' => $tab_str,
488 '$submit' => t('Submit'),
489 '$lbl_vis1' => t('Profile Visibility'),
490 '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
491 '$lbl_info1' => t('Contact Information / Notes'),
492 '$infedit' => t('Edit contact notes'),
493 '$common_text' => $common_text,
494 '$common_link' => $a->get_baseurl(true) . '/common/loc/' . local_user() . '/' . $contact['id'],
495 '$all_friends' => $all_friends,
496 '$relation_text' => $relation_text,
497 '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
498 '$blockunblock' => t('Block/Unblock contact'),
499 '$ignorecont' => t('Ignore contact'),
500 '$lblcrepair' => t("Repair URL settings"),
501 '$lblrecent' => t('View conversations'),
502 '$lblsuggest' => $lblsuggest,
503 '$delete' => t('Delete contact'),
504 '$nettype' => $nettype,
505 '$poll_interval' => $poll_interval,
506 '$poll_enabled' => $poll_enabled,
507 '$lastupdtext' => t('Last update:'),
508 '$lost_contact' => $lost_contact,
509 '$updpub' => t('Update public posts'),
510 '$last_update' => $last_update,
511 '$udnow' => t('Update now'),
512 '$profile_select' => contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false)),
513 '$contact_id' => $contact['id'],
514 '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
515 '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
516 '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
517 '$info' => $contact['info'],
518 '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
519 '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
520 '$archived' => (($contact['archive']) ? t('Currently archived') : ''),
521 '$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')),
522 '$notify' => array('notify', t('Notification for new posts'), ($contact['notify_new_posts'] == 1), t('Send a notification of every new post of this contact')),
523 '$fetch_further_information' => $fetch_further_information,
524 '$ffi_keyword_blacklist' => $contact['ffi_keyword_blacklist'],
525 '$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')),
526 '$photo' => $contact['photo'],
527 '$name' => $contact['name'],
528 '$dir_icon' => $dir_icon,
529 '$alt_text' => $alt_text,
530 '$sparkle' => $sparkle,
535 $arr = array('contact' => $contact,'output' => $o);
537 call_hooks('contact_edit', $arr);
539 return $arr['output'];
548 if(($a->argc == 2) && ($a->argv[1] === 'all')) {
552 elseif(($a->argc == 2) && ($a->argv[1] === 'blocked')) {
553 $sql_extra = " AND `blocked` = 1 ";
556 elseif(($a->argc == 2) && ($a->argv[1] === 'hidden')) {
557 $sql_extra = " AND `hidden` = 1 ";
560 elseif(($a->argc == 2) && ($a->argv[1] === 'ignored')) {
561 $sql_extra = " AND `readonly` = 1 ";
564 elseif(($a->argc == 2) && ($a->argv[1] === 'archived')) {
565 $sql_extra = " AND `archive` = 1 ";
569 $sql_extra = " AND `blocked` = 0 ";
571 $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
572 $nets = ((x($_GET,'nets')) ? notags(trim($_GET['nets'])) : '');
576 'label' => t('Suggestions'),
577 'url' => $a->get_baseurl(true) . '/suggest',
579 'title' => t('Suggest potential friends'),
582 'label' => t('All Contacts'),
583 'url' => $a->get_baseurl(true) . '/contacts/all',
584 'sel' => ($all) ? 'active' : '',
585 'title' => t('Show all contacts'),
588 'label' => t('Unblocked'),
589 'url' => $a->get_baseurl(true) . '/contacts',
590 'sel' => ((! $all) && (! $blocked) && (! $hidden) && (! $search) && (! $nets) && (! $ignored) && (! $archived)) ? 'active' : '',
591 'title' => t('Only show unblocked contacts'),
595 'label' => t('Blocked'),
596 'url' => $a->get_baseurl(true) . '/contacts/blocked',
597 'sel' => ($blocked) ? 'active' : '',
598 'title' => t('Only show blocked contacts'),
602 'label' => t('Ignored'),
603 'url' => $a->get_baseurl(true) . '/contacts/ignored',
604 'sel' => ($ignored) ? 'active' : '',
605 'title' => t('Only show ignored contacts'),
609 'label' => t('Archived'),
610 'url' => $a->get_baseurl(true) . '/contacts/archived',
611 'sel' => ($archived) ? 'active' : '',
612 'title' => t('Only show archived contacts'),
616 'label' => t('Hidden'),
617 'url' => $a->get_baseurl(true) . '/contacts/hidden',
618 'sel' => ($hidden) ? 'active' : '',
619 'title' => t('Only show hidden contacts'),
624 $tab_tpl = get_markup_template('common_tabs.tpl');
625 $t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
631 $search_hdr = $search;
632 $search_txt = dbesc(protect_sprintf(preg_quote($search)));
635 $sql_extra .= (($searching) ? " AND (name REGEXP '$search_txt' OR url REGEXP '$search_txt' OR nick REGEXP '$search_txt') " : "");
638 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
640 $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : '');
643 $r = q("SELECT COUNT(*) AS `total` FROM `contact`
644 WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ",
645 intval($_SESSION['uid']));
647 $a->set_pager_total($r[0]['total']);
648 $total = $r[0]['total'];
652 $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 ",
653 intval($_SESSION['uid']),
654 intval($a->pager['start']),
655 intval($a->pager['itemspage'])
665 case CONTACT_IS_FRIEND:
666 $dir_icon = 'images/lrarrow.gif';
667 $alt_text = t('Mutual Friendship');
669 case CONTACT_IS_FOLLOWER;
670 $dir_icon = 'images/larrow.gif';
671 $alt_text = t('is a fan of yours');
673 case CONTACT_IS_SHARING;
674 $dir_icon = 'images/rarrow.gif';
675 $alt_text = t('you are a fan of');
680 if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
681 $url = "redir/{$rr['id']}";
682 $sparkle = ' class="sparkle" ';
691 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
692 'edit_hover' => t('Edit contact'),
693 'photo_menu' => contact_photo_menu($rr),
695 'alt_text' => $alt_text,
696 'dir_icon' => $dir_icon,
697 'thumb' => proxy_url($rr['thumb']),
698 'name' => $rr['name'],
699 'username' => $rr['name'],
700 'sparkle' => $sparkle,
701 'itemurl' => $rr['url'],
703 'network' => network_to_name($rr['network']),
711 $tpl = get_markup_template("contacts-template.tpl");
712 $o .= replace_macros($tpl, array(
713 '$baseurl' => $a->get_baseurl(),
714 '$header' => t('Contacts') . (($nets) ? ' - ' . network_to_name($nets) : ''),
717 '$search' => $search_hdr,
718 '$desc' => t('Search your contacts'),
719 '$finding' => (($searching) ? t('Finding: ') . "'" . $search . "'" : ""),
720 '$submit' => t('Find'),
722 '$contacts' => $contacts,
723 '$contact_drop_confirm' => t('Do you really want to delete this contact?'),
724 '$batch_actions' => array(
725 'contacts_batch_update' => t('Update'),
726 'contacts_batch_block' => t('Block')."/".t("Unblock"),
727 "contacts_batch_ignore" => t('Ignore')."/".t("Unignore"),
728 "contacts_batch_archive" => t('Archive')."/".t("Unarchive"),
729 "contacts_batch_drop" => t('Delete'),
731 '$paginate' => paginate($a),