3 require_once('include/Contact.php');
4 require_once('include/socgraph.php');
5 require_once('include/contact_selectors.php');
7 function contacts_init(&$a) {
13 if(($a->argc == 2) && intval($a->argv[1])) {
14 $contact_id = intval($a->argv[1]);
15 $r = q("SELECT * FROM `contact` WHERE `uid` = %d and `id` = %d LIMIT 1",
24 require_once('include/group.php');
25 require_once('include/contact_widgets.php');
27 if(! x($a->page,'aside'))
28 $a->page['aside'] = '';
31 $a->data['contact'] = $r[0];
32 $vcard_widget = replace_macros(get_markup_template("vcard-widget.tpl"),array(
33 '$name' => $a->data['contact']['name'],
34 '$photo' => $a->data['contact']['photo']
40 $follow_widget = follow_widget();
43 $groups_widget .= group_side('contacts','group',false,0,$contact_id);
44 $findpeople_widget .= findpeople_widget();
45 $networks_widget .= networks_widget('contacts',$_GET['nets']);
46 $a->page['aside'] .= replace_macros(get_markup_template("contacts-widget-sidebar.tpl"),array(
47 '$vcard_widget' => $vcard_widget,
48 '$follow_widget' => $follow_widget,
49 '$groups_widget' => $groups_widget,
50 '$findpeople_widget' => $findpeople_widget,
51 '$networks_widget' => $networks_widget
54 $base = $a->get_baseurl();
55 $tpl = get_markup_template("contacts-head.tpl");
56 $a->page['htmlhead'] .= replace_macros($tpl,array(
57 '$baseurl' => $a->get_baseurl(true),
61 $tpl = get_markup_template("contacts-end.tpl");
62 $a->page['end'] .= replace_macros($tpl,array(
63 '$baseurl' => $a->get_baseurl(true),
70 function contacts_batch_actions(&$a){
71 $contacts_id = $_POST['contact_batch'];
72 if (!is_array($contacts_id)) return;
74 $orig_records = q("SELECT * FROM `contact` WHERE `id` IN (%s) AND `uid` = %d AND `self` = 0",
75 implode(",", $contacts_id),
80 foreach($orig_records as $orig_record) {
81 $contact_id = $orig_record['id'];
82 if (x($_POST, 'contacts_batch_update')) {
83 _contact_update($contact_id);
86 if (x($_POST, 'contacts_batch_block')) {
87 $r = _contact_block($contact_id, $orig_record);
88 if ($r) $count_actions++;
90 if (x($_POST, 'contacts_batch_ignore')) {
91 $r = _contact_ignore($contact_id, $orig_record);
92 if ($r) $count_actions++;
94 if (x($_POST, 'contacts_batch_archive')) {
95 $r = _contact_archive($contact_id, $orig_record);
96 if ($r) $count_actions++;
98 if (x($_POST, 'contacts_batch_drop')) {
99 _contact_drop($contact_id, $orig_record);
103 if ($count_actions>0) {
104 info ( sprintf( tt("%d contact edited.", "%d contacts edited", $count_actions), $count_actions) );
107 if(x($_SESSION,'return_url'))
108 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
110 goaway($a->get_baseurl(true) . '/contacts');
115 function contacts_post(&$a) {
120 if ($a->argv[1]==="batch") {
121 contacts_batch_actions($a);
125 $contact_id = intval($a->argv[1]);
129 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
134 if(! count($orig_record)) {
135 notice( t('Could not access contact record.') . EOL);
136 goaway($a->get_baseurl(true) . '/contacts');
137 return; // NOTREACHED
140 call_hooks('contact_edit_post', $_POST);
142 $profile_id = intval($_POST['profile-assign']);
144 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
149 notice( t('Could not locate selected profile.') . EOL);
154 $hidden = intval($_POST['hidden']);
156 $notify = intval($_POST['notify']);
158 $fetch_further_information = intval($_POST['fetch_further_information']);
160 $priority = intval($_POST['poll']);
161 if($priority > 5 || $priority < 0)
164 $info = fix_mce_lf(escape_tags(trim($_POST['info'])));
166 $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `info` = '%s',
167 `hidden` = %d, `notify_new_posts` = %d, `fetch_further_information` = %d WHERE `id` = %d AND `uid` = %d",
173 intval($fetch_further_information),
178 info( t('Contact updated.') . EOL);
180 notice( t('Failed to update contact record.') . EOL);
182 $r = q("select * from contact where id = %d and uid = %d limit 1",
187 $a->data['contact'] = $r[0];
194 function _contact_update($contact_id) {
195 // pull feed and consume it, which should subscribe to the hub.
196 proc_run('php',"include/poller.php","$contact_id");
198 function _contact_block($contact_id, $orig_record) {
199 $blocked = (($orig_record['blocked']) ? 0 : 1);
200 $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d",
208 function _contact_ignore($contact_id, $orig_record) {
209 $readonly = (($orig_record['readonly']) ? 0 : 1);
210 $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d",
217 function _contact_archive($contact_id, $orig_record) {
218 $archived = (($orig_record['archive']) ? 0 : 1);
219 $r = q("UPDATE `contact` SET `archive` = %d WHERE `id` = %d AND `uid` = %d",
225 q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact_id), intval(local_user()));
229 function _contact_drop($contact_id, $orig_record) {
230 require_once('include/Contact.php');
233 terminate_friendship($a->user,$a->contact,$orig_record);
234 contact_remove($orig_record['id']);
238 function contacts_content(&$a) {
242 nav_set_selected('contacts');
246 notice( t('Permission denied.') . EOL);
252 $contact_id = intval($a->argv[1]);
258 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1",
263 if(! count($orig_record)) {
264 notice( t('Could not access contact record.') . EOL);
265 goaway($a->get_baseurl(true) . '/contacts');
266 return; // NOTREACHED
269 if($cmd === 'update') {
270 _contact_update($contact_id);
271 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
275 if($cmd === 'block') {
276 $r = _contact_block($contact_id, $orig_record[0]);
278 info( (($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')) . EOL );
281 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
282 return; // NOTREACHED
285 if($cmd === 'ignore') {
286 $r = _contact_ignore($contact_id, $orig_record[0]);
288 info( (($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')) . EOL );
291 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
292 return; // NOTREACHED
296 if($cmd === 'archive') {
297 $r = _contact_archive($contact_id, $orig_record[0]);
299 info( (($archived) ? t('Contact has been archived') : t('Contact has been unarchived')) . EOL );
302 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
303 return; // NOTREACHED
306 if($cmd === 'drop') {
308 // Check if we should do HTML-based delete confirmation
309 if($_REQUEST['confirm']) {
310 // <form> can't take arguments in its "action" parameter
311 // so add any arguments as hidden inputs
312 $query = explode_querystring($a->query_string);
314 foreach($query['args'] as $arg) {
315 if(strpos($arg, 'confirm=') === false) {
316 $arg_parts = explode('=', $arg);
317 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
321 $a->page['aside'] = '';
322 return replace_macros(get_markup_template('confirm.tpl'), array(
324 '$message' => t('Do you really want to delete this contact?'),
325 '$extra_inputs' => $inputs,
326 '$confirm' => t('Yes'),
327 '$confirm_url' => $query['base'],
328 '$confirm_name' => 'confirmed',
329 '$cancel' => t('Cancel'),
332 // Now check how the user responded to the confirmation query
333 if($_REQUEST['canceled']) {
334 if(x($_SESSION,'return_url'))
335 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
337 goaway($a->get_baseurl(true) . '/contacts');
340 _contact_drop($contact_id, $orig_record[0]);
341 info( t('Contact has been removed.') . EOL );
342 if(x($_SESSION,'return_url'))
343 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
345 goaway($a->get_baseurl(true) . '/contacts');
346 return; // NOTREACHED
352 $_SESSION['return_url'] = $a->query_string;
354 if((x($a->data,'contact')) && (is_array($a->data['contact']))) {
356 $contact_id = $a->data['contact']['id'];
357 $contact = $a->data['contact'];
359 $editselect = 'none';
360 if( feature_enabled(local_user(),'richtext') )
361 $editselect = 'exact';
363 $a->page['htmlhead'] .= replace_macros(get_markup_template('contact_head.tpl'), array(
364 '$baseurl' => $a->get_baseurl(true),
365 '$editselect' => $editselect,
367 $a->page['end'] .= replace_macros(get_markup_template('contact_end.tpl'), array(
368 '$baseurl' => $a->get_baseurl(true),
369 '$editselect' => $editselect,
372 require_once('include/contact_selectors.php');
374 $tpl = get_markup_template("contact_edit.tpl");
376 switch($contact['rel']) {
377 case CONTACT_IS_FRIEND:
378 $dir_icon = 'images/lrarrow.gif';
379 $relation_text = t('You are mutual friends with %s');
381 case CONTACT_IS_FOLLOWER;
382 $dir_icon = 'images/larrow.gif';
383 $relation_text = t('You are sharing with %s');
386 case CONTACT_IS_SHARING;
387 $dir_icon = 'images/rarrow.gif';
388 $relation_text = t('%s is sharing with you');
394 $relation_text = sprintf($relation_text,$contact['name']);
396 if(($contact['network'] === NETWORK_DFRN) && ($contact['rel'])) {
397 $url = "redir/{$contact['id']}";
398 $sparkle = ' class="sparkle" ';
401 $url = $contact['url'];
405 $insecure = t('Private communications are not available for this contact.');
407 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00')
409 : datetime_convert('UTC',date_default_timezone_get(),$contact['last-update'],'D, j M Y, g:i A'));
411 if($contact['last-update'] !== '0000-00-00 00:00:00')
412 $last_update .= ' ' . (($contact['last-update'] == $contact['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
414 $lblsuggest = (($contact['network'] === NETWORK_DFRN) ? t('Suggest friends') : '');
416 $poll_enabled = (($contact['network'] !== NETWORK_DIASPORA) ? true : false);
418 $nettype = sprintf( t('Network type: %s'),network_to_name($contact['network']));
420 $common = count_common_friends(local_user(),$contact['id']);
421 $common_text = (($common) ? sprintf( tt('%d contact in common','%d contacts in common', $common),$common) : '');
423 $polling = (($contact['network'] === NETWORK_MAIL | $contact['network'] === NETWORK_FEED) ? 'polling' : '');
425 $x = count_all_friends(local_user(), $contact['id']);
426 $all_friends = (($x) ? t('View all contacts') : '');
431 'label' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
432 'url' => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/block',
434 'title' => t('Toggle Blocked status'),
437 'label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
438 'url' => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/ignore',
440 'title' => t('Toggle Ignored status'),
444 'label' => (($contact['archive']) ? t('Unarchive') : t('Archive') ),
445 'url' => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/archive',
447 'title' => t('Toggle Archive status'),
450 'label' => t('Repair'),
451 'url' => $a->get_baseurl(true) . '/crepair/' . $contact_id,
453 'title' => t('Advanced Contact Settings'),
456 $tab_tpl = get_markup_template('common_tabs.tpl');
457 $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
459 $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!') : '');
461 $o .= replace_macros($tpl, array(
462 '$header' => t('Contact Editor'),
463 '$tab_str' => $tab_str,
464 '$submit' => t('Submit'),
465 '$lbl_vis1' => t('Profile Visibility'),
466 '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
467 '$lbl_info1' => t('Contact Information / Notes'),
468 '$infedit' => t('Edit contact notes'),
469 '$common_text' => $common_text,
470 '$common_link' => $a->get_baseurl(true) . '/common/loc/' . local_user() . '/' . $contact['id'],
471 '$all_friends' => $all_friends,
472 '$relation_text' => $relation_text,
473 '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
474 '$blockunblock' => t('Block/Unblock contact'),
475 '$ignorecont' => t('Ignore contact'),
476 '$lblcrepair' => t("Repair URL settings"),
477 '$lblrecent' => t('View conversations'),
478 '$lblsuggest' => $lblsuggest,
479 '$delete' => t('Delete contact'),
480 '$nettype' => $nettype,
481 '$poll_interval' => contact_poll_interval($contact['priority'],(! $poll_enabled)),
482 '$poll_enabled' => $poll_enabled,
483 '$lastupdtext' => t('Last update:'),
484 '$lost_contact' => $lost_contact,
485 '$updpub' => t('Update public posts'),
486 '$last_update' => $last_update,
487 '$udnow' => t('Update now'),
488 '$profile_select' => contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false)),
489 '$contact_id' => $contact['id'],
490 '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
491 '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
492 '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
493 '$info' => $contact['info'],
494 '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
495 '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
496 '$archived' => (($contact['archive']) ? t('Currently archived') : ''),
497 '$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')),
498 '$notify' => array('notify', t('Notification for new posts'), ($contact['notify_new_posts'] == 1), t('Send a notification of every new post of this contact')),
499 '$fetch_further_information' => array('fetch_further_information', t('Fetch further information for feeds'), ($contact['fetch_further_information'] == 1), t('Fetch further information for feeds')),
500 '$photo' => $contact['photo'],
501 '$name' => $contact['name'],
502 '$dir_icon' => $dir_icon,
503 '$alt_text' => $alt_text,
504 '$sparkle' => $sparkle,
509 $arr = array('contact' => $contact,'output' => $o);
511 call_hooks('contact_edit', $arr);
513 return $arr['output'];
522 if(($a->argc == 2) && ($a->argv[1] === 'all')) {
526 elseif(($a->argc == 2) && ($a->argv[1] === 'blocked')) {
527 $sql_extra = " AND `blocked` = 1 ";
530 elseif(($a->argc == 2) && ($a->argv[1] === 'hidden')) {
531 $sql_extra = " AND `hidden` = 1 ";
534 elseif(($a->argc == 2) && ($a->argv[1] === 'ignored')) {
535 $sql_extra = " AND `readonly` = 1 ";
538 elseif(($a->argc == 2) && ($a->argv[1] === 'archived')) {
539 $sql_extra = " AND `archive` = 1 ";
543 $sql_extra = " AND `blocked` = 0 ";
545 $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
546 $nets = ((x($_GET,'nets')) ? notags(trim($_GET['nets'])) : '');
550 'label' => t('Suggestions'),
551 'url' => $a->get_baseurl(true) . '/suggest',
553 'title' => t('Suggest potential friends'),
556 'label' => t('All Contacts'),
557 'url' => $a->get_baseurl(true) . '/contacts/all',
558 'sel' => ($all) ? 'active' : '',
559 'title' => t('Show all contacts'),
562 'label' => t('Unblocked'),
563 'url' => $a->get_baseurl(true) . '/contacts',
564 'sel' => ((! $all) && (! $blocked) && (! $hidden) && (! $search) && (! $nets) && (! $ignored) && (! $archived)) ? 'active' : '',
565 'title' => t('Only show unblocked contacts'),
569 'label' => t('Blocked'),
570 'url' => $a->get_baseurl(true) . '/contacts/blocked',
571 'sel' => ($blocked) ? 'active' : '',
572 'title' => t('Only show blocked contacts'),
576 'label' => t('Ignored'),
577 'url' => $a->get_baseurl(true) . '/contacts/ignored',
578 'sel' => ($ignored) ? 'active' : '',
579 'title' => t('Only show ignored contacts'),
583 'label' => t('Archived'),
584 'url' => $a->get_baseurl(true) . '/contacts/archived',
585 'sel' => ($archived) ? 'active' : '',
586 'title' => t('Only show archived contacts'),
590 'label' => t('Hidden'),
591 'url' => $a->get_baseurl(true) . '/contacts/hidden',
592 'sel' => ($hidden) ? 'active' : '',
593 'title' => t('Only show hidden contacts'),
598 $tab_tpl = get_markup_template('common_tabs.tpl');
599 $t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
605 $search_hdr = $search;
606 $search_txt = dbesc(protect_sprintf(preg_quote($search)));
609 $sql_extra .= (($searching) ? " AND (name REGEXP '$search_txt' OR url REGEXP '$search_txt' OR nick REGEXP '$search_txt') " : "");
612 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
614 $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : '');
617 $r = q("SELECT COUNT(*) AS `total` FROM `contact`
618 WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ",
619 intval($_SESSION['uid']));
621 $a->set_pager_total($r[0]['total']);
622 $total = $r[0]['total'];
626 $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 ",
627 intval($_SESSION['uid']),
628 intval($a->pager['start']),
629 intval($a->pager['itemspage'])
639 case CONTACT_IS_FRIEND:
640 $dir_icon = 'images/lrarrow.gif';
641 $alt_text = t('Mutual Friendship');
643 case CONTACT_IS_FOLLOWER;
644 $dir_icon = 'images/larrow.gif';
645 $alt_text = t('is a fan of yours');
647 case CONTACT_IS_SHARING;
648 $dir_icon = 'images/rarrow.gif';
649 $alt_text = t('you are a fan of');
654 if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
655 $url = "redir/{$rr['id']}";
656 $sparkle = ' class="sparkle" ';
665 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
666 'edit_hover' => t('Edit contact'),
667 'photo_menu' => contact_photo_menu($rr),
669 'alt_text' => $alt_text,
670 'dir_icon' => $dir_icon,
671 'thumb' => $rr['thumb'],
672 'name' => $rr['name'],
673 'username' => $rr['name'],
674 'sparkle' => $sparkle,
675 'itemurl' => $rr['url'],
677 'network' => network_to_name($rr['network']),
685 $tpl = get_markup_template("contacts-template.tpl");
686 $o .= replace_macros($tpl, array(
687 '$baseurl' => $a->get_baseurl(),
688 '$header' => t('Contacts') . (($nets) ? ' - ' . network_to_name($nets) : ''),
691 '$search' => $search_hdr,
692 '$desc' => t('Search your contacts'),
693 '$finding' => (($searching) ? t('Finding: ') . "'" . $search . "'" : ""),
694 '$submit' => t('Find'),
696 '$contacts' => $contacts,
697 '$contact_drop_confirm' => t('Do you really want to delete this contact?'),
698 '$batch_actions' => array(
699 'contacts_batch_update' => t('Update'),
700 'contacts_batch_block' => t('Block')."/".t("Unblock"),
701 "contacts_batch_ignore" => t('Ignore')."/".t("Unignore"),
702 "contacts_batch_archive" => t('Archive')."/".t("Unarchive"),
703 "contacts_batch_drop" => t('Delete'),
705 '$paginate' => paginate($a),