2012-08-17 04:41:07 +00:00
< ? php
require_once ( 'include/Contact.php' );
require_once ( 'include/socgraph.php' );
require_once ( 'include/contact_selectors.php' );
2012-10-31 04:37:58 +00:00
require_once ( 'include/group.php' );
require_once ( 'include/contact_widgets.php' );
2013-01-04 00:18:52 +00:00
require_once ( 'include/zot.php' );
2013-12-09 04:08:50 +00:00
require_once ( 'include/widgets.php' );
2012-08-17 04:41:07 +00:00
2012-11-03 09:00:48 +00:00
function connections_init ( & $a ) {
2012-10-31 04:37:58 +00:00
2012-08-17 04:41:07 +00:00
if ( ! local_user ())
return ;
2013-07-08 00:22:40 +00:00
$channel = $a -> get_channel ();
if ( $channel )
head_set_icon ( $channel [ 'xchan_photo_s' ]);
2012-11-10 10:54:29 +00:00
}
2012-08-17 04:41:07 +00:00
2012-11-03 09:00:48 +00:00
function connections_post ( & $a ) {
2012-08-17 04:41:07 +00:00
if ( ! local_user ())
return ;
2012-11-09 01:33:38 +00:00
$contact_id = intval ( argv ( 1 ));
2012-08-17 04:41:07 +00:00
if ( ! $contact_id )
return ;
2012-11-09 01:33:38 +00:00
$orig_record = q ( " SELECT * FROM abook WHERE abook_id = %d AND abook_channel = %d LIMIT 1 " ,
2012-08-17 04:41:07 +00:00
intval ( $contact_id ),
intval ( local_user ())
);
2012-11-09 01:33:38 +00:00
if ( ! $orig_record ) {
2012-08-17 04:41:07 +00:00
notice ( t ( 'Could not access contact record.' ) . EOL );
2014-03-04 22:35:42 +00:00
goaway ( z_root () . '/connections' );
2012-08-17 04:41:07 +00:00
return ; // NOTREACHED
}
call_hooks ( 'contact_edit_post' , $_POST );
2014-02-12 00:56:39 +00:00
$profile_id = $_POST [ 'profile_assign' ];
2012-08-17 04:41:07 +00:00
if ( $profile_id ) {
2013-07-03 08:50:39 +00:00
$r = q ( " SELECT profile_guid FROM profile WHERE profile_guid = '%s' AND `uid` = %d LIMIT 1 " ,
dbesc ( $profile_id ),
2012-08-17 04:41:07 +00:00
intval ( local_user ())
);
if ( ! count ( $r )) {
notice ( t ( 'Could not locate selected profile.' ) . EOL );
return ;
}
}
$hidden = intval ( $_POST [ 'hidden' ]);
$priority = intval ( $_POST [ 'poll' ]);
if ( $priority > 5 || $priority < 0 )
$priority = 0 ;
$closeness = intval ( $_POST [ 'closeness' ]);
if ( $closeness < 0 )
$closeness = 99 ;
2012-11-09 01:33:38 +00:00
$abook_my_perms = 0 ;
2012-08-17 04:41:07 +00:00
2012-11-09 01:33:38 +00:00
foreach ( $_POST as $k => $v ) {
if ( strpos ( $k , 'perms_' ) === 0 ) {
$abook_my_perms += $v ;
}
}
2013-01-28 01:05:32 +00:00
$abook_flags = $orig_record [ 0 ][ 'abook_flags' ];
2013-08-28 04:06:11 +00:00
$new_friend = false ;
2013-01-28 01:05:32 +00:00
2014-02-03 22:03:43 +00:00
2013-01-28 01:05:32 +00:00
if (( $_REQUEST [ 'pending' ]) && ( $abook_flags & ABOOK_FLAG_PENDING )) {
$abook_flags = ( $abook_flags ^ ABOOK_FLAG_PENDING );
2013-08-28 04:06:11 +00:00
$new_friend = true ;
2013-01-28 01:05:32 +00:00
}
2013-07-03 08:50:39 +00:00
$r = q ( " UPDATE abook SET abook_profile = '%s', abook_my_perms = %d , abook_closeness = %d, abook_flags = %d
2012-11-09 01:33:38 +00:00
where abook_id = % d AND abook_channel = % d LIMIT 1 " ,
2013-07-03 08:50:39 +00:00
dbesc ( $profile_id ),
2012-11-09 01:33:38 +00:00
intval ( $abook_my_perms ),
2012-08-17 04:41:07 +00:00
intval ( $closeness ),
2013-01-28 01:05:32 +00:00
intval ( $abook_flags ),
2012-08-17 04:41:07 +00:00
intval ( $contact_id ),
intval ( local_user ())
);
2014-02-03 22:03:43 +00:00
2012-08-17 04:41:07 +00:00
if ( $r )
2012-11-09 01:33:38 +00:00
info ( t ( 'Connection updated.' ) . EOL );
2012-08-17 04:41:07 +00:00
else
2013-02-02 19:04:38 +00:00
notice ( t ( 'Failed to update connection record.' ) . EOL );
2012-08-17 04:41:07 +00:00
2013-01-24 05:15:40 +00:00
if (( x ( $a -> data , 'abook' )) && $a -> data [ 'abook' ][ 'abook_my_perms' ] != $abook_my_perms
&& ( ! ( $a -> data [ 'abook' ][ 'abook_flags' ] & ABOOK_FLAG_SELF ))) {
2012-11-10 11:01:07 +00:00
proc_run ( 'php' , 'include/notifier.php' , 'permission_update' , $contact_id );
}
2013-08-28 04:06:11 +00:00
if ( $new_friend ) {
2013-11-22 01:31:37 +00:00
$channel = $a -> get_channel ();
$default_group = $channel [ 'channel_default_group' ];
if ( $default_group ) {
require_once ( 'include/group.php' );
$g = group_rec_byhash ( local_user (), $default_group );
if ( $g )
group_add_member ( local_user (), '' , $a -> data [ 'abook_xchan' ], $g [ 'id' ]);
}
2013-11-22 01:49:14 +00:00
2013-08-28 04:06:11 +00:00
// Check if settings permit ("post new friend activity" is allowed, and
// friends in general or this friend in particular aren't hidden)
// and send out a new friend activity
// TODO
2013-11-18 00:50:32 +00:00
// pull in a bit of content if there is any to pull in
proc_run ( 'php' , 'include/onepoll.php' , $contact_id );
2013-11-22 01:49:14 +00:00
2013-08-28 04:06:11 +00:00
}
2012-11-09 01:33:38 +00:00
// Refresh the structure in memory with the new data
$r = q ( " SELECT abook.*, xchan.*
FROM abook left join xchan on abook_xchan = xchan_hash
WHERE abook_channel = % d and abook_id = % d LIMIT 1 " ,
intval ( local_user ()),
intval ( $contact_id )
2012-08-17 04:41:07 +00:00
);
2013-06-26 21:28:11 +00:00
if ( $r ) {
2012-11-09 01:33:38 +00:00
$a -> data [ 'abook' ] = $r [ 0 ];
2013-06-26 21:28:11 +00:00
}
2013-11-22 01:49:14 +00:00
if ( $new_friend ) {
$arr = array ( 'channel_id' => local_user (), 'abook' => $a -> data [ 'abook' ]);
call_hooks ( 'accept_follow' , $arr );
}
2013-07-26 05:57:06 +00:00
connections_clone ( $a );
2012-08-17 04:41:07 +00:00
return ;
}
2013-06-26 21:28:11 +00:00
function connections_clone ( & $a ) {
if ( ! array_key_exists ( 'abook' , $a -> data ))
return ;
$clone = $a -> data [ 'abook' ];
unset ( $clone [ 'abook_id' ]);
unset ( $clone [ 'abook_account' ]);
unset ( $clone [ 'abook_channel' ]);
2013-07-25 23:00:04 +00:00
2013-06-27 00:31:02 +00:00
build_sync_packet ( 0 /* use the current local_user */ , array ( 'abook' => array ( $clone )));
2013-06-26 21:28:11 +00:00
}
2012-08-17 04:41:07 +00:00
2012-11-03 09:00:48 +00:00
function connections_content ( & $a ) {
2012-08-17 04:41:07 +00:00
$sort_type = 0 ;
$o = '' ;
if ( ! local_user ()) {
notice ( t ( 'Permission denied.' ) . EOL );
2013-11-03 06:56:29 +00:00
return login ();
2012-08-17 04:41:07 +00:00
}
2014-03-04 22:35:42 +00:00
$blocked = false ;
$hidden = false ;
$ignored = false ;
$archived = false ;
$unblocked = false ;
$pending = false ;
$unconnected = false ;
$all = false ;
2012-08-17 04:41:07 +00:00
$_SESSION [ 'return_url' ] = $a -> query_string ;
2012-10-31 04:37:58 +00:00
$search_flags = 0 ;
2013-01-28 01:05:32 +00:00
$head = '' ;
2012-10-31 04:37:58 +00:00
if ( argc () == 2 ) {
switch ( argv ( 1 )) {
case 'blocked' :
$search_flags = ABOOK_FLAG_BLOCKED ;
2013-01-28 01:05:32 +00:00
$head = t ( 'Blocked' );
2012-10-31 04:37:58 +00:00
$blocked = true ;
break ;
case 'ignored' :
$search_flags = ABOOK_FLAG_IGNORED ;
2013-01-28 01:05:32 +00:00
$head = t ( 'Ignored' );
2012-10-31 04:37:58 +00:00
$ignored = true ;
break ;
case 'hidden' :
$search_flags = ABOOK_FLAG_HIDDEN ;
2013-01-28 01:05:32 +00:00
$head = t ( 'Hidden' );
2012-10-31 04:37:58 +00:00
$hidden = true ;
break ;
case 'archived' :
$search_flags = ABOOK_FLAG_ARCHIVED ;
2013-01-28 01:05:32 +00:00
$head = t ( 'Archived' );
2012-10-31 04:37:58 +00:00
$archived = true ;
break ;
2013-01-28 01:05:32 +00:00
case 'pending' :
$search_flags = ABOOK_FLAG_PENDING ;
$head = t ( 'New' );
$pending = true ;
2013-08-02 03:10:03 +00:00
nav_set_selected ( 'intros' );
2013-01-28 01:05:32 +00:00
break ;
2014-04-04 23:56:49 +00:00
case 'ifpending' :
2014-05-04 18:42:05 +00:00
$r = q ( " SELECT COUNT(abook.abook_id) AS total FROM abook left join xchan on abook.abook_xchan = xchan.xchan_hash where abook_channel = %d and (abook_flags & %d) and not ((abook_flags & %d) or (xchan_flags & %d)) " ,
2014-04-04 23:56:49 +00:00
intval ( local_user ()),
2014-05-04 09:41:31 +00:00
intval ( ABOOK_FLAG_PENDING ),
2014-05-04 10:11:28 +00:00
intval ( ABOOK_FLAG_SELF | ABOOK_FLAG_IGNORED ),
2014-04-15 00:29:19 +00:00
intval ( XCHAN_FLAGS_DELETED | XCHAN_FLAGS_ORPHAN )
2014-04-04 23:56:49 +00:00
);
if ( $r && $r [ 0 ][ 'total' ]) {
$search_flags = ABOOK_FLAG_PENDING ;
$head = t ( 'New' );
$pending = true ;
nav_set_selected ( 'intros' );
2014-04-05 00:43:44 +00:00
$a -> argv [ 1 ] = 'pending' ;
2014-04-04 23:56:49 +00:00
}
else {
$head = t ( 'All' );
$search_flags = 0 ;
$all = true ;
2014-04-05 00:43:44 +00:00
$a -> argc = 1 ;
unset ( $a -> argv [ 1 ]);
2014-04-04 23:56:49 +00:00
}
2014-06-14 13:49:57 +00:00
nav_set_selected ( 'intros' );
2014-04-04 23:56:49 +00:00
break ;
2014-07-18 03:54:30 +00:00
// case 'unconnected':
// $search_flags = ABOOK_FLAG_UNCONNECTED;
// $head = t('Unconnected');
// $unconnected = true;
// break;
2013-01-28 01:05:32 +00:00
2012-10-31 04:37:58 +00:00
case 'all' :
2013-01-28 01:05:32 +00:00
$head = t ( 'All' );
2012-10-31 04:37:58 +00:00
default :
$search_flags = 0 ;
$all = true ;
break ;
2012-11-30 02:15:13 +00:00
2012-10-31 04:37:58 +00:00
}
2012-11-30 02:15:13 +00:00
$sql_extra = (( $search_flags ) ? " and ( abook_flags & " . $search_flags . " ) " : " " );
2014-03-01 00:00:29 +00:00
if ( argv ( 1 ) === 'pending' )
$sql_extra .= " and not ( abook_flags & " . ABOOK_FLAG_IGNORED . " ) " ;
2012-11-30 02:15:13 +00:00
}
else {
$sql_extra = " and not ( abook_flags & " . ABOOK_FLAG_BLOCKED . " ) " ;
$unblocked = true ;
2012-08-17 04:41:07 +00:00
}
2012-10-31 04:37:58 +00:00
$search = (( x ( $_REQUEST , 'search' )) ? notags ( trim ( $_REQUEST [ 'search' ])) : '' );
2012-08-17 04:41:07 +00:00
$tabs = array (
array (
'label' => t ( 'Suggestions' ),
2014-03-04 22:35:42 +00:00
'url' => z_root () . '/suggest' ,
2012-08-17 04:41:07 +00:00
'sel' => '' ,
2012-11-03 09:00:48 +00:00
'title' => t ( 'Suggest new connections' ),
2012-08-17 04:41:07 +00:00
),
2013-01-28 01:05:32 +00:00
array (
'label' => t ( 'New Connections' ),
2014-03-04 22:35:42 +00:00
'url' => z_root () . '/connections/pending' ,
2013-01-28 01:05:32 +00:00
'sel' => ( $pending ) ? 'active' : '' ,
'title' => t ( 'Show pending (new) connections' ),
),
2012-08-17 04:41:07 +00:00
array (
2012-11-03 09:00:48 +00:00
'label' => t ( 'All Connections' ),
2014-03-04 22:35:42 +00:00
'url' => z_root () . '/connections/all' ,
2012-08-17 04:41:07 +00:00
'sel' => ( $all ) ? 'active' : '' ,
2012-11-03 09:00:48 +00:00
'title' => t ( 'Show all connections' ),
2012-08-17 04:41:07 +00:00
),
array (
'label' => t ( 'Unblocked' ),
2014-03-04 22:35:42 +00:00
'url' => z_root () . '/connections' ,
2012-11-30 02:15:13 +00:00
'sel' => (( $unblocked ) && ( ! $search ) && ( ! $nets )) ? 'active' : '' ,
2012-11-03 09:00:48 +00:00
'title' => t ( 'Only show unblocked connections' ),
2012-08-17 04:41:07 +00:00
),
array (
'label' => t ( 'Blocked' ),
2014-03-04 22:35:42 +00:00
'url' => z_root () . '/connections/blocked' ,
2012-08-17 04:41:07 +00:00
'sel' => ( $blocked ) ? 'active' : '' ,
2012-11-03 09:00:48 +00:00
'title' => t ( 'Only show blocked connections' ),
2012-08-17 04:41:07 +00:00
),
array (
'label' => t ( 'Ignored' ),
2014-03-04 22:35:42 +00:00
'url' => z_root () . '/connections/ignored' ,
2012-08-17 04:41:07 +00:00
'sel' => ( $ignored ) ? 'active' : '' ,
2012-11-03 09:00:48 +00:00
'title' => t ( 'Only show ignored connections' ),
2012-08-17 04:41:07 +00:00
),
array (
'label' => t ( 'Archived' ),
2014-03-04 22:35:42 +00:00
'url' => z_root () . '/connections/archived' ,
2012-08-17 04:41:07 +00:00
'sel' => ( $archived ) ? 'active' : '' ,
2012-11-03 09:00:48 +00:00
'title' => t ( 'Only show archived connections' ),
2012-08-17 04:41:07 +00:00
),
array (
'label' => t ( 'Hidden' ),
2014-03-04 22:35:42 +00:00
'url' => z_root () . '/connections/hidden' ,
2012-08-17 04:41:07 +00:00
'sel' => ( $hidden ) ? 'active' : '' ,
2012-11-03 09:00:48 +00:00
'title' => t ( 'Only show hidden connections' ),
2012-08-17 04:41:07 +00:00
),
2014-07-18 03:54:30 +00:00
// array(
// 'label' => t('Unconnected'),
// 'url' => z_root() . '/connections/unconnected',
// 'sel' => ($unconnected) ? 'active' : '',
// 'title' => t('Only show one-way connections'),
// ),
2014-03-04 22:35:42 +00:00
2012-08-17 04:41:07 +00:00
);
$tab_tpl = get_markup_template ( 'common_tabs.tpl' );
$t = replace_macros ( $tab_tpl , array ( '$tabs' => $tabs ));
$searching = false ;
if ( $search ) {
$search_hdr = $search ;
$search_txt = dbesc ( protect_sprintf ( preg_quote ( $search )));
$searching = true ;
}
2012-11-13 10:57:15 +00:00
$sql_extra .= (( $searching ) ? protect_sprintf ( " AND xchan_name like '% $search_txt %' " ) : " " );
2012-08-17 04:41:07 +00:00
2014-08-18 00:48:41 +00:00
if ( $_REQUEST [ 'gid' ]) {
$sql_extra .= " and xchan_hash in ( select xchan from group_member where gid = " . intval ( $_REQUEST [ 'gid' ]) . " and uid = " . intval ( local_user ()) . " ) " ;
}
2012-11-10 10:54:29 +00:00
2012-10-31 23:55:19 +00:00
$r = q ( " SELECT COUNT(abook.abook_id) AS total FROM abook left join xchan on abook.abook_xchan = xchan.xchan_hash
2013-12-20 03:16:46 +00:00
where abook_channel = % d and not ( abook_flags & % d ) and not ( xchan_flags & % d ) $sql_extra $sql_extra2 " ,
2012-10-31 23:55:19 +00:00
intval ( local_user ()),
2013-12-20 03:16:46 +00:00
intval ( ABOOK_FLAG_SELF ),
2014-04-15 00:29:19 +00:00
intval ( XCHAN_FLAGS_DELETED | XCHAN_FLAGS_ORPHAN )
2012-10-31 23:55:19 +00:00
);
2014-04-05 00:43:44 +00:00
if ( $r ) {
2012-08-17 04:41:07 +00:00
$a -> set_pager_total ( $r [ 0 ][ 'total' ]);
$total = $r [ 0 ][ 'total' ];
}
2012-12-08 22:18:02 +00:00
2012-11-02 05:23:13 +00:00
$r = q ( " SELECT abook.*, xchan.* FROM abook left join xchan on abook.abook_xchan = xchan.xchan_hash
2013-12-20 03:16:46 +00:00
WHERE abook_channel = % d and not ( abook_flags & % d ) and not ( xchan_flags & % d ) $sql_extra $sql_extra2 ORDER BY xchan_name LIMIT % d , % d " ,
2012-10-31 23:55:19 +00:00
intval ( local_user ()),
intval ( ABOOK_FLAG_SELF ),
2014-04-15 00:29:19 +00:00
intval ( XCHAN_FLAGS_DELETED | XCHAN_FLAGS_ORPHAN ),
2012-08-17 04:41:07 +00:00
intval ( $a -> pager [ 'start' ]),
intval ( $a -> pager [ 'itemspage' ])
);
2012-12-08 22:18:02 +00:00
2012-08-17 04:41:07 +00:00
$contacts = array ();
if ( count ( $r )) {
foreach ( $r as $rr ) {
2013-08-04 12:12:11 +00:00
if ( $rr [ 'xchan_url' ]) {
$contacts [] = array (
'img_hover' => sprintf ( t ( '%1$s [%2$s]' ), $rr [ 'xchan_name' ], $rr [ 'xchan_url' ]),
2014-09-11 06:08:24 +00:00
'edit_hover' => t ( 'Edit connection' ),
2013-08-04 12:12:11 +00:00
'id' => $rr [ 'abook_id' ],
'alt_text' => $alt_text ,
'dir_icon' => $dir_icon ,
'thumb' => $rr [ 'xchan_photo_m' ],
'name' => $rr [ 'xchan_name' ],
'username' => $rr [ 'xchan_name' ],
2014-04-07 05:21:51 +00:00
'classes' => (( $rr [ 'abook_flags' ] & ABOOK_FLAG_ARCHIVED ) ? 'archived' : '' ),
2013-12-18 09:00:08 +00:00
'link' => z_root () . '/connedit/' . $rr [ 'abook_id' ],
2014-09-11 06:08:24 +00:00
'edit' => t ( 'Edit' ),
2014-04-07 04:40:23 +00:00
'url' => chanlink_url ( $rr [ 'xchan_url' ]),
2013-08-04 12:12:11 +00:00
'network' => network_to_name ( $rr [ 'network' ]),
);
}
2012-08-17 04:41:07 +00:00
}
}
2014-09-11 06:08:24 +00:00
if ( $_REQUEST [ 'aj' ]) {
if ( $contacts ) {
$o = replace_macros ( get_markup_template ( 'contactsajax.tpl' ), array (
'$contacts' => $contacts ,
'$edit' => t ( 'Edit' ),
));
}
else {
$o = '<div id="content-complete"></div>' ;
}
echo $o ;
killme ();
}
else {
$o .= " <script> var page_query = ' " . $_GET [ 'q' ] . " '; var extra_args = ' " . extra_query_args () . " ' ; </script> " ;
$o .= replace_macros ( get_markup_template ( 'connections.tpl' ), array (
'$header' => t ( 'Connections' ) . (( $head ) ? ' - ' . $head : '' ),
'$tabs' => $t ,
'$total' => $total ,
'$search' => $search_hdr ,
'$desc' => t ( 'Search your connections' ),
'$finding' => (( $searching ) ? t ( 'Finding: ' ) . " ' " . $search . " ' " : " " ),
'$submit' => t ( 'Find' ),
'$edit' => t ( 'Edit' ),
'$cmd' => $a -> cmd ,
'$contacts' => $contacts ,
'$paginate' => paginate ( $a ),
));
}
if ( ! $contacts )
$o .= '<div id="content-complete"></div>' ;
2012-08-17 04:41:07 +00:00
return $o ;
}