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'],
36 '$url' => ($a->data['contact']['network'] == NETWORK_DFRN) ? $a->get_baseurl()."/redir/".$a->data['contact']['id'] : $a->data['contact']['url']
42 if (isset($_GET['add']))
43 $follow_widget = follow_widget($_GET['add']);
45 $follow_widget = follow_widget();
48 if ($_GET['nets'] == "all")
51 $groups_widget .= group_side('contacts','group',false,0,$contact_id);
52 $findpeople_widget .= findpeople_widget();
53 $networks_widget .= networks_widget('contacts',$_GET['nets']);
54 $a->page['aside'] .= replace_macros(get_markup_template("contacts-widget-sidebar.tpl"),array(
55 '$vcard_widget' => $vcard_widget,
56 '$follow_widget' => $follow_widget,
57 '$groups_widget' => $groups_widget,
58 '$findpeople_widget' => $findpeople_widget,
59 '$networks_widget' => $networks_widget
62 $base = $a->get_baseurl();
63 $tpl = get_markup_template("contacts-head.tpl");
64 $a->page['htmlhead'] .= replace_macros($tpl,array(
65 '$baseurl' => $a->get_baseurl(true),
69 $tpl = get_markup_template("contacts-end.tpl");
70 $a->page['end'] .= replace_macros($tpl,array(
71 '$baseurl' => $a->get_baseurl(true),
78 function contacts_batch_actions(&$a){
79 $contacts_id = $_POST['contact_batch'];
80 if (!is_array($contacts_id)) return;
82 $orig_records = q("SELECT * FROM `contact` WHERE `id` IN (%s) AND `uid` = %d AND `self` = 0",
83 implode(",", $contacts_id),
88 foreach($orig_records as $orig_record) {
89 $contact_id = $orig_record['id'];
90 if (x($_POST, 'contacts_batch_update')) {
91 _contact_update($contact_id);
94 if (x($_POST, 'contacts_batch_block')) {
95 $r = _contact_block($contact_id, $orig_record);
96 if ($r) $count_actions++;
98 if (x($_POST, 'contacts_batch_ignore')) {
99 $r = _contact_ignore($contact_id, $orig_record);
100 if ($r) $count_actions++;
102 if (x($_POST, 'contacts_batch_archive')) {
103 $r = _contact_archive($contact_id, $orig_record);
104 if ($r) $count_actions++;
106 if (x($_POST, 'contacts_batch_drop')) {
107 _contact_drop($contact_id, $orig_record);
111 if ($count_actions>0) {
112 info ( sprintf( tt("%d contact edited.", "%d contacts edited", $count_actions), $count_actions) );
115 if(x($_SESSION,'return_url'))
116 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
118 goaway($a->get_baseurl(true) . '/contacts');
123 function contacts_post(&$a) {
128 if ($a->argv[1]==="batch") {
129 contacts_batch_actions($a);
133 $contact_id = intval($a->argv[1]);
137 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
142 if(! count($orig_record)) {
143 notice( t('Could not access contact record.') . EOL);
144 goaway($a->get_baseurl(true) . '/contacts');
145 return; // NOTREACHED
148 call_hooks('contact_edit_post', $_POST);
150 $profile_id = intval($_POST['profile-assign']);
152 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
157 notice( t('Could not locate selected profile.') . EOL);
162 $hidden = intval($_POST['hidden']);
164 $notify = intval($_POST['notify']);
166 $fetch_further_information = intval($_POST['fetch_further_information']);
168 $ffi_keyword_blacklist = fix_mce_lf(escape_tags(trim($_POST['ffi_keyword_blacklist'])));
170 $priority = intval($_POST['poll']);
171 if($priority > 5 || $priority < 0)
174 $info = fix_mce_lf(escape_tags(trim($_POST['info'])));
176 $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `info` = '%s',
177 `hidden` = %d, `notify_new_posts` = %d, `fetch_further_information` = %d,
178 `ffi_keyword_blacklist` = '%s' WHERE `id` = %d AND `uid` = %d",
184 intval($fetch_further_information),
185 dbesc($ffi_keyword_blacklist),
190 info( t('Contact updated.') . EOL);
192 notice( t('Failed to update contact record.') . EOL);
194 $r = q("select * from contact where id = %d and uid = %d limit 1",
199 $a->data['contact'] = $r[0];
206 function _contact_update($contact_id) {
207 // pull feed and consume it, which should subscribe to the hub.
208 proc_run('php',"include/poller.php","$contact_id");
210 function _contact_block($contact_id, $orig_record) {
211 $blocked = (($orig_record['blocked']) ? 0 : 1);
212 $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d",
220 function _contact_ignore($contact_id, $orig_record) {
221 $readonly = (($orig_record['readonly']) ? 0 : 1);
222 $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d",
229 function _contact_archive($contact_id, $orig_record) {
230 $archived = (($orig_record['archive']) ? 0 : 1);
231 $r = q("UPDATE `contact` SET `archive` = %d WHERE `id` = %d AND `uid` = %d",
237 q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact_id), intval(local_user()));
241 function _contact_drop($contact_id, $orig_record) {
242 require_once('include/Contact.php');
245 terminate_friendship($a->user,$a->contact,$orig_record);
246 contact_remove($orig_record['id']);
250 function contacts_content(&$a) {
254 nav_set_selected('contacts');
258 notice( t('Permission denied.') . EOL);
264 $contact_id = intval($a->argv[1]);
270 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1",
275 if(! count($orig_record)) {
276 notice( t('Could not access contact record.') . EOL);
277 goaway($a->get_baseurl(true) . '/contacts');
278 return; // NOTREACHED
281 if($cmd === 'update') {
282 _contact_update($contact_id);
283 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
287 if($cmd === 'block') {
288 $r = _contact_block($contact_id, $orig_record[0]);
290 $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
291 info((($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')).EOL);
294 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
295 return; // NOTREACHED
298 if($cmd === 'ignore') {
299 $r = _contact_ignore($contact_id, $orig_record[0]);
301 $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
302 info((($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')).EOL);
305 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
306 return; // NOTREACHED
310 if($cmd === 'archive') {
311 $r = _contact_archive($contact_id, $orig_record[0]);
313 $archived = (($orig_record[0]['archive']) ? 0 : 1);
314 info((($archived) ? t('Contact has been archived') : t('Contact has been unarchived')).EOL);
317 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
318 return; // NOTREACHED
321 if($cmd === 'drop') {
323 // Check if we should do HTML-based delete confirmation
324 if($_REQUEST['confirm']) {
325 // <form> can't take arguments in its "action" parameter
326 // so add any arguments as hidden inputs
327 $query = explode_querystring($a->query_string);
329 foreach($query['args'] as $arg) {
330 if(strpos($arg, 'confirm=') === false) {
331 $arg_parts = explode('=', $arg);
332 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
336 $a->page['aside'] = '';
337 return replace_macros(get_markup_template('confirm.tpl'), array(
339 '$message' => t('Do you really want to delete this contact?'),
340 '$extra_inputs' => $inputs,
341 '$confirm' => t('Yes'),
342 '$confirm_url' => $query['base'],
343 '$confirm_name' => 'confirmed',
344 '$cancel' => t('Cancel'),
347 // Now check how the user responded to the confirmation query
348 if($_REQUEST['canceled']) {
349 if(x($_SESSION,'return_url'))
350 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
352 goaway($a->get_baseurl(true) . '/contacts');
355 _contact_drop($contact_id, $orig_record[0]);
356 info( t('Contact has been removed.') . EOL );
357 if(x($_SESSION,'return_url'))
358 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
360 goaway($a->get_baseurl(true) . '/contacts');
361 return; // NOTREACHED
367 $_SESSION['return_url'] = $a->query_string;
369 if((x($a->data,'contact')) && (is_array($a->data['contact']))) {
371 $contact_id = $a->data['contact']['id'];
372 $contact = $a->data['contact'];
374 $editselect = 'none';
375 if( feature_enabled(local_user(),'richtext') )
376 $editselect = 'exact';
378 $a->page['htmlhead'] .= replace_macros(get_markup_template('contact_head.tpl'), array(
379 '$baseurl' => $a->get_baseurl(true),
380 '$editselect' => $editselect,
382 $a->page['end'] .= replace_macros(get_markup_template('contact_end.tpl'), array(
383 '$baseurl' => $a->get_baseurl(true),
384 '$editselect' => $editselect,
387 require_once('include/contact_selectors.php');
389 $tpl = get_markup_template("contact_edit.tpl");
391 switch($contact['rel']) {
392 case CONTACT_IS_FRIEND:
393 $dir_icon = 'images/lrarrow.gif';
394 $relation_text = t('You are mutual friends with %s');
396 case CONTACT_IS_FOLLOWER;
397 $dir_icon = 'images/larrow.gif';
398 $relation_text = t('You are sharing with %s');
401 case CONTACT_IS_SHARING;
402 $dir_icon = 'images/rarrow.gif';
403 $relation_text = t('%s is sharing with you');
409 if(!in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
412 $relation_text = sprintf($relation_text,$contact['name']);
414 if(($contact['network'] === NETWORK_DFRN) && ($contact['rel'])) {
415 $url = "redir/{$contact['id']}";
416 $sparkle = ' class="sparkle" ';
419 $url = $contact['url'];
423 $insecure = t('Private communications are not available for this contact.');
425 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00')
427 : datetime_convert('UTC',date_default_timezone_get(),$contact['last-update'],'D, j M Y, g:i A'));
429 if($contact['last-update'] !== '0000-00-00 00:00:00')
430 $last_update .= ' ' . (($contact['last-update'] <= $contact['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
432 $lblsuggest = (($contact['network'] === NETWORK_DFRN) ? t('Suggest friends') : '');
434 $poll_enabled = in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2));
436 $nettype = sprintf( t('Network type: %s'),network_to_name($contact['network']));
438 $common = count_common_friends(local_user(),$contact['id']);
439 $common_text = (($common) ? sprintf( tt('%d contact in common','%d contacts in common', $common),$common) : '');
441 $polling = (($contact['network'] === NETWORK_MAIL | $contact['network'] === NETWORK_FEED) ? 'polling' : '');
443 $x = count_all_friends(local_user(), $contact['id']);
444 $all_friends = (($x) ? t('View all contacts') : '');
449 'label' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
450 'url' => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/block',
452 'title' => t('Toggle Blocked status'),
455 'label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
456 'url' => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/ignore',
458 'title' => t('Toggle Ignored status'),
462 'label' => (($contact['archive']) ? t('Unarchive') : t('Archive') ),
463 'url' => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/archive',
465 'title' => t('Toggle Archive status'),
468 'label' => t('Repair'),
469 'url' => $a->get_baseurl(true) . '/crepair/' . $contact_id,
471 'title' => t('Advanced Contact Settings'),
474 $tab_tpl = get_markup_template('common_tabs.tpl');
475 $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
477 $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!') : '');
479 if ($contact['network'] == NETWORK_FEED)
480 $fetch_further_information = array('fetch_further_information', t('Fetch further information for feeds'), $contact['fetch_further_information'], t('Fetch further information for feeds'),
481 array('0'=>t('Disabled'), '1'=>t('Fetch information'), '2'=>t('Fetch information and keywords')));
483 if (in_array($contact['network'], array(NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2)))
484 $poll_interval = contact_poll_interval($contact['priority'],(! $poll_enabled));
486 if ($contact['network'] == NETWORK_DFRN)
487 $profile_select = contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false));
489 $o .= replace_macros($tpl, array(
490 '$header' => t('Contact Editor'),
491 '$tab_str' => $tab_str,
492 '$submit' => t('Submit'),
493 '$lbl_vis1' => t('Profile Visibility'),
494 '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
495 '$lbl_info1' => t('Contact Information / Notes'),
496 '$infedit' => t('Edit contact notes'),
497 '$common_text' => $common_text,
498 '$common_link' => $a->get_baseurl(true) . '/common/loc/' . local_user() . '/' . $contact['id'],
499 '$all_friends' => $all_friends,
500 '$relation_text' => $relation_text,
501 '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
502 '$blockunblock' => t('Block/Unblock contact'),
503 '$ignorecont' => t('Ignore contact'),
504 '$lblcrepair' => t("Repair URL settings"),
505 '$lblrecent' => t('View conversations'),
506 '$lblsuggest' => $lblsuggest,
507 '$delete' => t('Delete contact'),
508 '$nettype' => $nettype,
509 '$poll_interval' => $poll_interval,
510 '$poll_enabled' => $poll_enabled,
511 '$lastupdtext' => t('Last update:'),
512 '$lost_contact' => $lost_contact,
513 '$updpub' => t('Update public posts'),
514 '$last_update' => $last_update,
515 '$udnow' => t('Update now'),
516 '$profile_select' => $profile_select,
517 '$contact_id' => $contact['id'],
518 '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
519 '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
520 '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
521 '$info' => $contact['info'],
522 '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
523 '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
524 '$archived' => (($contact['archive']) ? t('Currently archived') : ''),
525 '$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')),
526 '$notify' => array('notify', t('Notification for new posts'), ($contact['notify_new_posts'] == 1), t('Send a notification of every new post of this contact')),
527 '$fetch_further_information' => $fetch_further_information,
528 '$ffi_keyword_blacklist' => $contact['ffi_keyword_blacklist'],
529 '$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')),
530 '$photo' => $contact['photo'],
531 '$name' => $contact['name'],
532 '$dir_icon' => $dir_icon,
533 '$alt_text' => $alt_text,
534 '$sparkle' => $sparkle,
539 $arr = array('contact' => $contact,'output' => $o);
541 call_hooks('contact_edit', $arr);
543 return $arr['output'];
552 if(($a->argc == 2) && ($a->argv[1] === 'all')) {
556 elseif(($a->argc == 2) && ($a->argv[1] === 'blocked')) {
557 $sql_extra = " AND `blocked` = 1 ";
560 elseif(($a->argc == 2) && ($a->argv[1] === 'hidden')) {
561 $sql_extra = " AND `hidden` = 1 ";
564 elseif(($a->argc == 2) && ($a->argv[1] === 'ignored')) {
565 $sql_extra = " AND `readonly` = 1 ";
568 elseif(($a->argc == 2) && ($a->argv[1] === 'archived')) {
569 $sql_extra = " AND `archive` = 1 ";
573 $sql_extra = " AND `blocked` = 0 ";
575 $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
576 $nets = ((x($_GET,'nets')) ? notags(trim($_GET['nets'])) : '');
580 'label' => t('Suggestions'),
581 'url' => $a->get_baseurl(true) . '/suggest',
583 'title' => t('Suggest potential friends'),
586 'label' => t('All Contacts'),
587 'url' => $a->get_baseurl(true) . '/contacts/all',
588 'sel' => ($all) ? 'active' : '',
589 'title' => t('Show all contacts'),
592 'label' => t('Unblocked'),
593 'url' => $a->get_baseurl(true) . '/contacts',
594 'sel' => ((! $all) && (! $blocked) && (! $hidden) && (! $search) && (! $nets) && (! $ignored) && (! $archived)) ? 'active' : '',
595 'title' => t('Only show unblocked contacts'),
599 'label' => t('Blocked'),
600 'url' => $a->get_baseurl(true) . '/contacts/blocked',
601 'sel' => ($blocked) ? 'active' : '',
602 'title' => t('Only show blocked contacts'),
606 'label' => t('Ignored'),
607 'url' => $a->get_baseurl(true) . '/contacts/ignored',
608 'sel' => ($ignored) ? 'active' : '',
609 'title' => t('Only show ignored contacts'),
613 'label' => t('Archived'),
614 'url' => $a->get_baseurl(true) . '/contacts/archived',
615 'sel' => ($archived) ? 'active' : '',
616 'title' => t('Only show archived contacts'),
620 'label' => t('Hidden'),
621 'url' => $a->get_baseurl(true) . '/contacts/hidden',
622 'sel' => ($hidden) ? 'active' : '',
623 'title' => t('Only show hidden contacts'),
628 $tab_tpl = get_markup_template('common_tabs.tpl');
629 $t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
635 $search_hdr = $search;
636 $search_txt = dbesc(protect_sprintf(preg_quote($search)));
639 $sql_extra .= (($searching) ? " AND (name REGEXP '$search_txt' OR url REGEXP '$search_txt' OR nick REGEXP '$search_txt') " : "");
642 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
644 $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : '');
647 $r = q("SELECT COUNT(*) AS `total` FROM `contact`
648 WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ",
649 intval($_SESSION['uid']));
651 $a->set_pager_total($r[0]['total']);
652 $total = $r[0]['total'];
656 $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 ",
657 intval($_SESSION['uid']),
658 intval($a->pager['start']),
659 intval($a->pager['itemspage'])
669 case CONTACT_IS_FRIEND:
670 $dir_icon = 'images/lrarrow.gif';
671 $alt_text = t('Mutual Friendship');
673 case CONTACT_IS_FOLLOWER;
674 $dir_icon = 'images/larrow.gif';
675 $alt_text = t('is a fan of yours');
677 case CONTACT_IS_SHARING;
678 $dir_icon = 'images/rarrow.gif';
679 $alt_text = t('you are a fan of');
684 if(($rr['network'] === NETWORK_DFRN) && ($rr['rel'])) {
685 $url = "redir/{$rr['id']}";
686 $sparkle = ' class="sparkle" ';
695 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
696 'edit_hover' => t('Edit contact'),
697 'photo_menu' => contact_photo_menu($rr),
699 'alt_text' => $alt_text,
700 'dir_icon' => $dir_icon,
701 'thumb' => proxy_url($rr['thumb']),
702 'name' => $rr['name'],
703 'username' => $rr['name'],
704 'sparkle' => $sparkle,
705 'itemurl' => $rr['url'],
707 'network' => network_to_name($rr['network']),
715 $tpl = get_markup_template("contacts-template.tpl");
716 $o .= replace_macros($tpl, array(
717 '$baseurl' => $a->get_baseurl(),
718 '$header' => t('Contacts') . (($nets) ? ' - ' . network_to_name($nets) : ''),
721 '$search' => $search_hdr,
722 '$desc' => t('Search your contacts'),
723 '$finding' => (($searching) ? t('Finding: ') . "'" . $search . "'" : ""),
724 '$submit' => t('Find'),
726 '$contacts' => $contacts,
727 '$contact_drop_confirm' => t('Do you really want to delete this contact?'),
728 '$batch_actions' => array(
729 'contacts_batch_update' => t('Update'),
730 'contacts_batch_block' => t('Block')."/".t("Unblock"),
731 "contacts_batch_ignore" => t('Ignore')."/".t("Unignore"),
732 "contacts_batch_archive" => t('Archive')."/".t("Unarchive"),
733 "contacts_batch_drop" => t('Delete'),
735 '$paginate' => paginate($a),