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 if (isset($_GET['add']))
41 $follow_widget = follow_widget($_GET['add']);
43 $follow_widget = follow_widget();
46 $groups_widget .= group_side('contacts','group',false,0,$contact_id);
47 $findpeople_widget .= findpeople_widget();
48 $networks_widget .= networks_widget('contacts',$_GET['nets']);
49 $a->page['aside'] .= replace_macros(get_markup_template("contacts-widget-sidebar.tpl"),array(
50 '$vcard_widget' => $vcard_widget,
51 '$follow_widget' => $follow_widget,
52 '$groups_widget' => $groups_widget,
53 '$findpeople_widget' => $findpeople_widget,
54 '$networks_widget' => $networks_widget
57 $base = $a->get_baseurl();
58 $tpl = get_markup_template("contacts-head.tpl");
59 $a->page['htmlhead'] .= replace_macros($tpl,array(
60 '$baseurl' => $a->get_baseurl(true),
64 $tpl = get_markup_template("contacts-end.tpl");
65 $a->page['end'] .= replace_macros($tpl,array(
66 '$baseurl' => $a->get_baseurl(true),
73 function contacts_batch_actions(&$a){
74 $contacts_id = $_POST['contact_batch'];
75 if (!is_array($contacts_id)) return;
77 $orig_records = q("SELECT * FROM `contact` WHERE `id` IN (%s) AND `uid` = %d AND `self` = 0",
78 implode(",", $contacts_id),
83 foreach($orig_records as $orig_record) {
84 $contact_id = $orig_record['id'];
85 if (x($_POST, 'contacts_batch_update')) {
86 _contact_update($contact_id);
89 if (x($_POST, 'contacts_batch_block')) {
90 $r = _contact_block($contact_id, $orig_record);
91 if ($r) $count_actions++;
93 if (x($_POST, 'contacts_batch_ignore')) {
94 $r = _contact_ignore($contact_id, $orig_record);
95 if ($r) $count_actions++;
97 if (x($_POST, 'contacts_batch_archive')) {
98 $r = _contact_archive($contact_id, $orig_record);
99 if ($r) $count_actions++;
101 if (x($_POST, 'contacts_batch_drop')) {
102 _contact_drop($contact_id, $orig_record);
106 if ($count_actions>0) {
107 info ( sprintf( tt("%d contact edited.", "%d contacts edited", $count_actions), $count_actions) );
110 if(x($_SESSION,'return_url'))
111 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
113 goaway($a->get_baseurl(true) . '/contacts');
118 function contacts_post(&$a) {
123 if ($a->argv[1]==="batch") {
124 contacts_batch_actions($a);
128 $contact_id = intval($a->argv[1]);
132 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
137 if(! count($orig_record)) {
138 notice( t('Could not access contact record.') . EOL);
139 goaway($a->get_baseurl(true) . '/contacts');
140 return; // NOTREACHED
143 call_hooks('contact_edit_post', $_POST);
145 $profile_id = intval($_POST['profile-assign']);
147 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
152 notice( t('Could not locate selected profile.') . EOL);
157 $hidden = intval($_POST['hidden']);
159 $notify = intval($_POST['notify']);
161 $fetch_further_information = intval($_POST['fetch_further_information']);
163 $priority = intval($_POST['poll']);
164 if($priority > 5 || $priority < 0)
167 $info = fix_mce_lf(escape_tags(trim($_POST['info'])));
169 $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `info` = '%s',
170 `hidden` = %d, `notify_new_posts` = %d, `fetch_further_information` = %d WHERE `id` = %d AND `uid` = %d",
176 intval($fetch_further_information),
181 info( t('Contact updated.') . EOL);
183 notice( t('Failed to update contact record.') . EOL);
185 $r = q("select * from contact where id = %d and uid = %d limit 1",
190 $a->data['contact'] = $r[0];
197 function _contact_update($contact_id) {
198 // pull feed and consume it, which should subscribe to the hub.
199 proc_run('php',"include/poller.php","$contact_id");
201 function _contact_block($contact_id, $orig_record) {
202 $blocked = (($orig_record['blocked']) ? 0 : 1);
203 $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d",
211 function _contact_ignore($contact_id, $orig_record) {
212 $readonly = (($orig_record['readonly']) ? 0 : 1);
213 $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d",
220 function _contact_archive($contact_id, $orig_record) {
221 $archived = (($orig_record['archive']) ? 0 : 1);
222 $r = q("UPDATE `contact` SET `archive` = %d WHERE `id` = %d AND `uid` = %d",
228 q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact_id), intval(local_user()));
232 function _contact_drop($contact_id, $orig_record) {
233 require_once('include/Contact.php');
236 terminate_friendship($a->user,$a->contact,$orig_record);
237 contact_remove($orig_record['id']);
241 function contacts_content(&$a) {
245 nav_set_selected('contacts');
249 notice( t('Permission denied.') . EOL);
255 $contact_id = intval($a->argv[1]);
261 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1",
266 if(! count($orig_record)) {
267 notice( t('Could not access contact record.') . EOL);
268 goaway($a->get_baseurl(true) . '/contacts');
269 return; // NOTREACHED
272 if($cmd === 'update') {
273 _contact_update($contact_id);
274 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
278 if($cmd === 'block') {
279 $r = _contact_block($contact_id, $orig_record[0]);
281 info( (($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')) . EOL );
284 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
285 return; // NOTREACHED
288 if($cmd === 'ignore') {
289 $r = _contact_ignore($contact_id, $orig_record[0]);
291 info( (($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')) . EOL );
294 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
295 return; // NOTREACHED
299 if($cmd === 'archive') {
300 $r = _contact_archive($contact_id, $orig_record[0]);
302 info( (($archived) ? t('Contact has been archived') : t('Contact has been unarchived')) . EOL );
305 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
306 return; // NOTREACHED
309 if($cmd === 'drop') {
311 // Check if we should do HTML-based delete confirmation
312 if($_REQUEST['confirm']) {
313 // <form> can't take arguments in its "action" parameter
314 // so add any arguments as hidden inputs
315 $query = explode_querystring($a->query_string);
317 foreach($query['args'] as $arg) {
318 if(strpos($arg, 'confirm=') === false) {
319 $arg_parts = explode('=', $arg);
320 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
324 $a->page['aside'] = '';
325 return replace_macros(get_markup_template('confirm.tpl'), array(
327 '$message' => t('Do you really want to delete this contact?'),
328 '$extra_inputs' => $inputs,
329 '$confirm' => t('Yes'),
330 '$confirm_url' => $query['base'],
331 '$confirm_name' => 'confirmed',
332 '$cancel' => t('Cancel'),
335 // Now check how the user responded to the confirmation query
336 if($_REQUEST['canceled']) {
337 if(x($_SESSION,'return_url'))
338 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
340 goaway($a->get_baseurl(true) . '/contacts');
343 _contact_drop($contact_id, $orig_record[0]);
344 info( t('Contact has been removed.') . EOL );
345 if(x($_SESSION,'return_url'))
346 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
348 goaway($a->get_baseurl(true) . '/contacts');
349 return; // NOTREACHED
355 $_SESSION['return_url'] = $a->query_string;
357 if((x($a->data,'contact')) && (is_array($a->data['contact']))) {
359 $contact_id = $a->data['contact']['id'];
360 $contact = $a->data['contact'];
362 $editselect = 'none';
363 if( feature_enabled(local_user(),'richtext') )
364 $editselect = 'exact';
366 $a->page['htmlhead'] .= replace_macros(get_markup_template('contact_head.tpl'), array(
367 '$baseurl' => $a->get_baseurl(true),
368 '$editselect' => $editselect,
370 $a->page['end'] .= replace_macros(get_markup_template('contact_end.tpl'), array(
371 '$baseurl' => $a->get_baseurl(true),
372 '$editselect' => $editselect,
375 require_once('include/contact_selectors.php');
377 $tpl = get_markup_template("contact_edit.tpl");
379 switch($contact['rel']) {
380 case CONTACT_IS_FRIEND:
381 $dir_icon = 'images/lrarrow.gif';
382 $relation_text = t('You are mutual friends with %s');
384 case CONTACT_IS_FOLLOWER;
385 $dir_icon = 'images/larrow.gif';
386 $relation_text = t('You are sharing with %s');
389 case CONTACT_IS_SHARING;
390 $dir_icon = 'images/rarrow.gif';
391 $relation_text = t('%s is sharing with you');
397 $relation_text = sprintf($relation_text,$contact['name']);
399 if(($contact['network'] === NETWORK_DFRN) && ($contact['rel'])) {
400 $url = "redir/{$contact['id']}";
401 $sparkle = ' class="sparkle" ';
404 $url = $contact['url'];
408 $insecure = t('Private communications are not available for this contact.');
410 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00')
412 : datetime_convert('UTC',date_default_timezone_get(),$contact['last-update'],'D, j M Y, g:i A'));
414 if($contact['last-update'] !== '0000-00-00 00:00:00')
415 $last_update .= ' ' . (($contact['last-update'] == $contact['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
417 $lblsuggest = (($contact['network'] === NETWORK_DFRN) ? t('Suggest friends') : '');
419 $poll_enabled = (($contact['network'] !== NETWORK_DIASPORA) ? true : false);
421 $nettype = sprintf( t('Network type: %s'),network_to_name($contact['network']));
423 $common = count_common_friends(local_user(),$contact['id']);
424 $common_text = (($common) ? sprintf( tt('%d contact in common','%d contacts in common', $common),$common) : '');
426 $polling = (($contact['network'] === NETWORK_MAIL | $contact['network'] === NETWORK_FEED) ? 'polling' : '');
428 $x = count_all_friends(local_user(), $contact['id']);
429 $all_friends = (($x) ? t('View all contacts') : '');
434 'label' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
435 'url' => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/block',
437 'title' => t('Toggle Blocked status'),
440 'label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
441 'url' => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/ignore',
443 'title' => t('Toggle Ignored status'),
447 'label' => (($contact['archive']) ? t('Unarchive') : t('Archive') ),
448 'url' => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/archive',
450 'title' => t('Toggle Archive status'),
453 'label' => t('Repair'),
454 'url' => $a->get_baseurl(true) . '/crepair/' . $contact_id,
456 'title' => t('Advanced Contact Settings'),
459 $tab_tpl = get_markup_template('common_tabs.tpl');
460 $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
462 $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!') : '');
464 $o .= replace_macros($tpl, array(
465 '$header' => t('Contact Editor'),
466 '$tab_str' => $tab_str,
467 '$submit' => t('Submit'),
468 '$lbl_vis1' => t('Profile Visibility'),
469 '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
470 '$lbl_info1' => t('Contact Information / Notes'),
471 '$infedit' => t('Edit contact notes'),
472 '$common_text' => $common_text,
473 '$common_link' => $a->get_baseurl(true) . '/common/loc/' . local_user() . '/' . $contact['id'],
474 '$all_friends' => $all_friends,
475 '$relation_text' => $relation_text,
476 '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
477 '$blockunblock' => t('Block/Unblock contact'),
478 '$ignorecont' => t('Ignore contact'),
479 '$lblcrepair' => t("Repair URL settings"),
480 '$lblrecent' => t('View conversations'),
481 '$lblsuggest' => $lblsuggest,
482 '$delete' => t('Delete contact'),
483 '$nettype' => $nettype,
484 '$poll_interval' => contact_poll_interval($contact['priority'],(! $poll_enabled)),
485 '$poll_enabled' => $poll_enabled,
486 '$lastupdtext' => t('Last update:'),
487 '$lost_contact' => $lost_contact,
488 '$updpub' => t('Update public posts'),
489 '$last_update' => $last_update,
490 '$udnow' => t('Update now'),
491 '$profile_select' => contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false)),
492 '$contact_id' => $contact['id'],
493 '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
494 '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
495 '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
496 '$info' => $contact['info'],
497 '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
498 '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
499 '$archived' => (($contact['archive']) ? t('Currently archived') : ''),
500 '$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')),
501 '$notify' => array('notify', t('Notification for new posts'), ($contact['notify_new_posts'] == 1), t('Send a notification of every new post of this contact')),
502 '$fetch_further_information' => array('fetch_further_information', t('Fetch further information for feeds'), ($contact['fetch_further_information'] == 1), t('Fetch further information for feeds')),
503 '$photo' => $contact['photo'],
504 '$name' => $contact['name'],
505 '$dir_icon' => $dir_icon,
506 '$alt_text' => $alt_text,
507 '$sparkle' => $sparkle,
512 $arr = array('contact' => $contact,'output' => $o);
514 call_hooks('contact_edit', $arr);
516 return $arr['output'];
525 if(($a->argc == 2) && ($a->argv[1] === 'all')) {
529 elseif(($a->argc == 2) && ($a->argv[1] === 'blocked')) {
530 $sql_extra = " AND `blocked` = 1 ";
533 elseif(($a->argc == 2) && ($a->argv[1] === 'hidden')) {
534 $sql_extra = " AND `hidden` = 1 ";
537 elseif(($a->argc == 2) && ($a->argv[1] === 'ignored')) {
538 $sql_extra = " AND `readonly` = 1 ";
541 elseif(($a->argc == 2) && ($a->argv[1] === 'archived')) {
542 $sql_extra = " AND `archive` = 1 ";
546 $sql_extra = " AND `blocked` = 0 ";
548 $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
549 $nets = ((x($_GET,'nets')) ? notags(trim($_GET['nets'])) : '');
553 'label' => t('Suggestions'),
554 'url' => $a->get_baseurl(true) . '/suggest',
556 'title' => t('Suggest potential friends'),
559 'label' => t('All Contacts'),
560 'url' => $a->get_baseurl(true) . '/contacts/all',
561 'sel' => ($all) ? 'active' : '',
562 'title' => t('Show all contacts'),
565 'label' => t('Unblocked'),
566 'url' => $a->get_baseurl(true) . '/contacts',
567 'sel' => ((! $all) && (! $blocked) && (! $hidden) && (! $search) && (! $nets) && (! $ignored) && (! $archived)) ? 'active' : '',
568 'title' => t('Only show unblocked contacts'),
572 'label' => t('Blocked'),
573 'url' => $a->get_baseurl(true) . '/contacts/blocked',
574 'sel' => ($blocked) ? 'active' : '',
575 'title' => t('Only show blocked contacts'),
579 'label' => t('Ignored'),
580 'url' => $a->get_baseurl(true) . '/contacts/ignored',
581 'sel' => ($ignored) ? 'active' : '',
582 'title' => t('Only show ignored contacts'),
586 'label' => t('Archived'),
587 'url' => $a->get_baseurl(true) . '/contacts/archived',
588 'sel' => ($archived) ? 'active' : '',
589 'title' => t('Only show archived contacts'),
593 'label' => t('Hidden'),
594 'url' => $a->get_baseurl(true) . '/contacts/hidden',
595 'sel' => ($hidden) ? 'active' : '',
596 'title' => t('Only show hidden contacts'),
601 $tab_tpl = get_markup_template('common_tabs.tpl');
602 $t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
608 $search_hdr = $search;
609 $search_txt = dbesc(protect_sprintf(preg_quote($search)));
612 $sql_extra .= (($searching) ? " AND (name REGEXP '$search_txt' OR url REGEXP '$search_txt' OR nick REGEXP '$search_txt') " : "");
615 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
617 $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : '');
620 $r = q("SELECT COUNT(*) AS `total` FROM `contact`
621 WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ",
622 intval($_SESSION['uid']));
624 $a->set_pager_total($r[0]['total']);
625 $total = $r[0]['total'];
629 $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 ",
630 intval($_SESSION['uid']),
631 intval($a->pager['start']),
632 intval($a->pager['itemspage'])
642 case CONTACT_IS_FRIEND:
643 $dir_icon = 'images/lrarrow.gif';
644 $alt_text = t('Mutual Friendship');
646 case CONTACT_IS_FOLLOWER;
647 $dir_icon = 'images/larrow.gif';
648 $alt_text = t('is a fan of yours');
650 case CONTACT_IS_SHARING;
651 $dir_icon = 'images/rarrow.gif';
652 $alt_text = t('you are a fan of');
657 if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
658 $url = "redir/{$rr['id']}";
659 $sparkle = ' class="sparkle" ';
668 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
669 'edit_hover' => t('Edit contact'),
670 'photo_menu' => contact_photo_menu($rr),
672 'alt_text' => $alt_text,
673 'dir_icon' => $dir_icon,
674 'thumb' => $rr['thumb'],
675 'name' => $rr['name'],
676 'username' => $rr['name'],
677 'sparkle' => $sparkle,
678 'itemurl' => $rr['url'],
680 'network' => network_to_name($rr['network']),
688 $tpl = get_markup_template("contacts-template.tpl");
689 $o .= replace_macros($tpl, array(
690 '$baseurl' => $a->get_baseurl(),
691 '$header' => t('Contacts') . (($nets) ? ' - ' . network_to_name($nets) : ''),
694 '$search' => $search_hdr,
695 '$desc' => t('Search your contacts'),
696 '$finding' => (($searching) ? t('Finding: ') . "'" . $search . "'" : ""),
697 '$submit' => t('Find'),
699 '$contacts' => $contacts,
700 '$contact_drop_confirm' => t('Do you really want to delete this contact?'),
701 '$batch_actions' => array(
702 'contacts_batch_update' => t('Update'),
703 'contacts_batch_block' => t('Block')."/".t("Unblock"),
704 "contacts_batch_ignore" => t('Ignore')."/".t("Unignore"),
705 "contacts_batch_archive" => t('Archive')."/".t("Unarchive"),
706 "contacts_batch_drop" => t('Delete'),
708 '$paginate' => paginate($a),