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 $priority = intval($_POST['poll']);
157 if($priority > 5 || $priority < 0)
160 $info = fix_mce_lf(escape_tags(trim($_POST['info'])));
162 $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `info` = '%s',
163 `hidden` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
172 info( t('Contact updated.') . EOL);
174 notice( t('Failed to update contact record.') . EOL);
176 $r = q("select * from contact where id = %d and uid = %d limit 1",
181 $a->data['contact'] = $r[0];
188 function _contact_update($contact_id) {
189 // pull feed and consume it, which should subscribe to the hub.
190 proc_run('php',"include/poller.php","$contact_id");
192 function _contact_block($contact_id, $orig_record) {
193 $blocked = (($orig_record['blocked']) ? 0 : 1);
194 $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
202 function _contact_ignore($contact_id, $orig_record) {
203 $readonly = (($orig_record['readonly']) ? 0 : 1);
204 $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
211 function _contact_archive($contact_id, $orig_record) {
212 $archived = (($orig_record['archive']) ? 0 : 1);
213 $r = q("UPDATE `contact` SET `archive` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
219 q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact_id), intval(local_user()));
223 function _contact_drop($contact_id, $orig_record) {
224 require_once('include/Contact.php');
227 terminate_friendship($a->user,$a->contact,$orig_record);
228 contact_remove($orig_record['id']);
232 function contacts_content(&$a) {
236 nav_set_selected('contacts');
240 notice( t('Permission denied.') . EOL);
246 $contact_id = intval($a->argv[1]);
252 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1",
257 if(! count($orig_record)) {
258 notice( t('Could not access contact record.') . EOL);
259 goaway($a->get_baseurl(true) . '/contacts');
260 return; // NOTREACHED
263 if($cmd === 'update') {
264 _contact_update($contact_id);
265 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
269 if($cmd === 'block') {
270 $r = _contact_block($contact_id, $orig_record[0]);
272 info( (($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')) . EOL );
275 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
276 return; // NOTREACHED
279 if($cmd === 'ignore') {
280 $r = _contact_ignore($contact_id, $orig_record[0]);
282 info( (($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')) . EOL );
285 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
286 return; // NOTREACHED
290 if($cmd === 'archive') {
291 $r = _contact_archive($contact_id, $orig_record[0]);
293 info( (($archived) ? t('Contact has been archived') : t('Contact has been unarchived')) . EOL );
296 goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
297 return; // NOTREACHED
300 if($cmd === 'drop') {
302 // Check if we should do HTML-based delete confirmation
303 if($_REQUEST['confirm']) {
304 // <form> can't take arguments in its "action" parameter
305 // so add any arguments as hidden inputs
306 $query = explode_querystring($a->query_string);
308 foreach($query['args'] as $arg) {
309 if(strpos($arg, 'confirm=') === false) {
310 $arg_parts = explode('=', $arg);
311 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
315 $a->page['aside'] = '';
316 return replace_macros(get_markup_template('confirm.tpl'), array(
318 '$message' => t('Do you really want to delete this contact?'),
319 '$extra_inputs' => $inputs,
320 '$confirm' => t('Yes'),
321 '$confirm_url' => $query['base'],
322 '$confirm_name' => 'confirmed',
323 '$cancel' => t('Cancel'),
326 // Now check how the user responded to the confirmation query
327 if($_REQUEST['canceled']) {
328 if(x($_SESSION,'return_url'))
329 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
331 goaway($a->get_baseurl(true) . '/contacts');
334 _contact_drop($contact_id, $orig_record[0]);
335 info( t('Contact has been removed.') . EOL );
336 if(x($_SESSION,'return_url'))
337 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
339 goaway($a->get_baseurl(true) . '/contacts');
340 return; // NOTREACHED
346 $_SESSION['return_url'] = $a->query_string;
348 if((x($a->data,'contact')) && (is_array($a->data['contact']))) {
350 $contact_id = $a->data['contact']['id'];
351 $contact = $a->data['contact'];
353 $editselect = 'none';
354 if( feature_enabled(local_user(),'richtext') )
355 $editselect = 'exact';
357 $a->page['htmlhead'] .= replace_macros(get_markup_template('contact_head.tpl'), array(
358 '$baseurl' => $a->get_baseurl(true),
359 '$editselect' => $editselect,
361 $a->page['end'] .= replace_macros(get_markup_template('contact_end.tpl'), array(
362 '$baseurl' => $a->get_baseurl(true),
363 '$editselect' => $editselect,
366 require_once('include/contact_selectors.php');
368 $tpl = get_markup_template("contact_edit.tpl");
370 switch($contact['rel']) {
371 case CONTACT_IS_FRIEND:
372 $dir_icon = 'images/lrarrow.gif';
373 $relation_text = t('You are mutual friends with %s');
375 case CONTACT_IS_FOLLOWER;
376 $dir_icon = 'images/larrow.gif';
377 $relation_text = t('You are sharing with %s');
380 case CONTACT_IS_SHARING;
381 $dir_icon = 'images/rarrow.gif';
382 $relation_text = t('%s is sharing with you');
388 $relation_text = sprintf($relation_text,$contact['name']);
390 if(($contact['network'] === NETWORK_DFRN) && ($contact['rel'])) {
391 $url = "redir/{$contact['id']}";
392 $sparkle = ' class="sparkle" ';
395 $url = $contact['url'];
399 $insecure = t('Private communications are not available for this contact.');
401 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00')
403 : datetime_convert('UTC',date_default_timezone_get(),$contact['last-update'],'D, j M Y, g:i A'));
405 if($contact['last-update'] !== '0000-00-00 00:00:00')
406 $last_update .= ' ' . (($contact['last-update'] == $contact['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
408 $lblsuggest = (($contact['network'] === NETWORK_DFRN) ? t('Suggest friends') : '');
410 $poll_enabled = (($contact['network'] !== NETWORK_DIASPORA) ? true : false);
412 $nettype = sprintf( t('Network type: %s'),network_to_name($contact['network']));
414 $common = count_common_friends(local_user(),$contact['id']);
415 $common_text = (($common) ? sprintf( tt('%d contact in common','%d contacts in common', $common),$common) : '');
417 $polling = (($contact['network'] === NETWORK_MAIL | $contact['network'] === NETWORK_FEED) ? 'polling' : '');
419 $x = count_all_friends(local_user(), $contact['id']);
420 $all_friends = (($x) ? t('View all contacts') : '');
425 'label' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
426 'url' => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/block',
428 'title' => t('Toggle Blocked status'),
431 'label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
432 'url' => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/ignore',
434 'title' => t('Toggle Ignored status'),
438 'label' => (($contact['archive']) ? t('Unarchive') : t('Archive') ),
439 'url' => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/archive',
441 'title' => t('Toggle Archive status'),
444 'label' => t('Repair'),
445 'url' => $a->get_baseurl(true) . '/crepair/' . $contact_id,
447 'title' => t('Advanced Contact Settings'),
450 $tab_tpl = get_markup_template('common_tabs.tpl');
451 $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
453 $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!') : '');
455 $o .= replace_macros($tpl, array(
456 '$header' => t('Contact Editor'),
457 '$tab_str' => $tab_str,
458 '$submit' => t('Submit'),
459 '$lbl_vis1' => t('Profile Visibility'),
460 '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
461 '$lbl_info1' => t('Contact Information / Notes'),
462 '$infedit' => t('Edit contact notes'),
463 '$common_text' => $common_text,
464 '$common_link' => $a->get_baseurl(true) . '/common/loc/' . local_user() . '/' . $contact['id'],
465 '$all_friends' => $all_friends,
466 '$relation_text' => $relation_text,
467 '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
468 '$blockunblock' => t('Block/Unblock contact'),
469 '$ignorecont' => t('Ignore contact'),
470 '$lblcrepair' => t("Repair URL settings"),
471 '$lblrecent' => t('View conversations'),
472 '$lblsuggest' => $lblsuggest,
473 '$delete' => t('Delete contact'),
474 '$nettype' => $nettype,
475 '$poll_interval' => contact_poll_interval($contact['priority'],(! $poll_enabled)),
476 '$poll_enabled' => $poll_enabled,
477 '$lastupdtext' => t('Last update:'),
478 '$lost_contact' => $lost_contact,
479 '$updpub' => t('Update public posts'),
480 '$last_update' => $last_update,
481 '$udnow' => t('Update now'),
482 '$profile_select' => contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false)),
483 '$contact_id' => $contact['id'],
484 '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
485 '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
486 '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
487 '$info' => $contact['info'],
488 '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
489 '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
490 '$archived' => (($contact['archive']) ? t('Currently archived') : ''),
491 '$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')),
492 '$photo' => $contact['photo'],
493 '$name' => $contact['name'],
494 '$dir_icon' => $dir_icon,
495 '$alt_text' => $alt_text,
496 '$sparkle' => $sparkle,
501 $arr = array('contact' => $contact,'output' => $o);
503 call_hooks('contact_edit', $arr);
505 return $arr['output'];
514 if(($a->argc == 2) && ($a->argv[1] === 'all')) {
518 elseif(($a->argc == 2) && ($a->argv[1] === 'blocked')) {
519 $sql_extra = " AND `blocked` = 1 ";
522 elseif(($a->argc == 2) && ($a->argv[1] === 'hidden')) {
523 $sql_extra = " AND `hidden` = 1 ";
526 elseif(($a->argc == 2) && ($a->argv[1] === 'ignored')) {
527 $sql_extra = " AND `readonly` = 1 ";
530 elseif(($a->argc == 2) && ($a->argv[1] === 'archived')) {
531 $sql_extra = " AND `archive` = 1 ";
535 $sql_extra = " AND `blocked` = 0 ";
537 $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
538 $nets = ((x($_GET,'nets')) ? notags(trim($_GET['nets'])) : '');
542 'label' => t('Suggestions'),
543 'url' => $a->get_baseurl(true) . '/suggest',
545 'title' => t('Suggest potential friends'),
548 'label' => t('All Contacts'),
549 'url' => $a->get_baseurl(true) . '/contacts/all',
550 'sel' => ($all) ? 'active' : '',
551 'title' => t('Show all contacts'),
554 'label' => t('Unblocked'),
555 'url' => $a->get_baseurl(true) . '/contacts',
556 'sel' => ((! $all) && (! $blocked) && (! $hidden) && (! $search) && (! $nets) && (! $ignored) && (! $archived)) ? 'active' : '',
557 'title' => t('Only show unblocked contacts'),
561 'label' => t('Blocked'),
562 'url' => $a->get_baseurl(true) . '/contacts/blocked',
563 'sel' => ($blocked) ? 'active' : '',
564 'title' => t('Only show blocked contacts'),
568 'label' => t('Ignored'),
569 'url' => $a->get_baseurl(true) . '/contacts/ignored',
570 'sel' => ($ignored) ? 'active' : '',
571 'title' => t('Only show ignored contacts'),
575 'label' => t('Archived'),
576 'url' => $a->get_baseurl(true) . '/contacts/archived',
577 'sel' => ($archived) ? 'active' : '',
578 'title' => t('Only show archived contacts'),
582 'label' => t('Hidden'),
583 'url' => $a->get_baseurl(true) . '/contacts/hidden',
584 'sel' => ($hidden) ? 'active' : '',
585 'title' => t('Only show hidden contacts'),
590 $tab_tpl = get_markup_template('common_tabs.tpl');
591 $t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
597 $search_hdr = $search;
598 $search_txt = dbesc(protect_sprintf(preg_quote($search)));
601 $sql_extra .= (($searching) ? " AND (name REGEXP '$search_txt' OR url REGEXP '$search_txt' OR nick REGEXP '$search_txt') " : "");
604 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
606 $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : '');
609 $r = q("SELECT COUNT(*) AS `total` FROM `contact`
610 WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ",
611 intval($_SESSION['uid']));
613 $a->set_pager_total($r[0]['total']);
614 $total = $r[0]['total'];
618 $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 ",
619 intval($_SESSION['uid']),
620 intval($a->pager['start']),
621 intval($a->pager['itemspage'])
631 case CONTACT_IS_FRIEND:
632 $dir_icon = 'images/lrarrow.gif';
633 $alt_text = t('Mutual Friendship');
635 case CONTACT_IS_FOLLOWER;
636 $dir_icon = 'images/larrow.gif';
637 $alt_text = t('is a fan of yours');
639 case CONTACT_IS_SHARING;
640 $dir_icon = 'images/rarrow.gif';
641 $alt_text = t('you are a fan of');
646 if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
647 $url = "redir/{$rr['id']}";
648 $sparkle = ' class="sparkle" ';
657 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
658 'edit_hover' => t('Edit contact'),
659 'photo_menu' => contact_photo_menu($rr),
661 'alt_text' => $alt_text,
662 'dir_icon' => $dir_icon,
663 'thumb' => $rr['thumb'],
664 'name' => $rr['name'],
665 'username' => $rr['name'],
666 'sparkle' => $sparkle,
667 'itemurl' => $rr['url'],
669 'network' => network_to_name($rr['network']),
677 $tpl = get_markup_template("contacts-template.tpl");
678 $o .= replace_macros($tpl, array(
679 '$baseurl' => $a->get_baseurl(),
680 '$header' => t('Contacts') . (($nets) ? ' - ' . network_to_name($nets) : ''),
683 '$search' => $search_hdr,
684 '$desc' => t('Search your contacts'),
685 '$finding' => (($searching) ? t('Finding: ') . "'" . $search . "'" : ""),
686 '$submit' => t('Find'),
688 '$contacts' => $contacts,
689 '$contact_drop_confirm' => t('Do you really want to delete this contact?'),
690 '$batch_actions' => array(
691 'contacts_batch_update' => t('Update'),
692 'contacts_batch_block' => t('Block')."/".t("Unblock"),
693 "contacts_batch_ignore" => t('Ignore')."/".t("Unignore"),
694 "contacts_batch_archive" => t('Archive')."/".t("Unarchive"),
695 "contacts_batch_drop" => t('Delete'),
697 '$paginate' => paginate($a),