2018-05-30 04:08:52 +00:00
< ? php
namespace Zotlabs\Lib ;
2020-08-07 03:19:03 +00:00
use App ;
2019-01-14 05:22:45 +00:00
use Zotlabs\Web\HTTPSig ;
2018-09-07 00:36:35 +00:00
use Zotlabs\Access\Permissions ;
use Zotlabs\Access\PermissionRoles ;
2019-04-02 02:17:28 +00:00
use Zotlabs\Access\PermissionLimits ;
2020-06-15 07:29:56 +00:00
use Zotlabs\Daemon\Run ;
2020-02-12 05:52:52 +00:00
use Zotlabs\Lib\PConfig ;
2020-06-02 04:00:21 +00:00
use Zotlabs\Lib\Config ;
2020-04-17 11:08:04 +00:00
use Zotlabs\Lib\LibBlock ;
2020-05-28 01:56:51 +00:00
use Zotlabs\Lib\Markdown ;
2020-08-15 21:55:03 +00:00
use Zotlabs\Lib\Libzotdir ;
2020-10-22 04:29:07 +00:00
use Zotlabs\Lib\Nodeinfo ;
2020-02-21 02:17:16 +00:00
use Emoji ;
2018-05-30 04:08:52 +00:00
2019-09-18 04:25:26 +00:00
require_once ( 'include/html2bbcode.php' );
require_once ( 'include/html2plain.php' );
require_once ( 'include/event.php' );
2018-05-30 04:08:52 +00:00
class Activity {
2018-09-19 23:25:05 +00:00
static $ACTOR_CACHE_DAYS = 3 ;
2021-06-19 23:04:32 +00:00
// $x (string|array)
// if json string, decode it
// returns activitystreams object as an array except if it is a URL
// which returns the URL as string
2018-05-30 04:08:52 +00:00
static function encode_object ( $x ) {
2021-06-19 05:12:23 +00:00
if ( $x ) {
2021-06-19 23:04:32 +00:00
if ( is_string ( $x )) {
$tmp = json_decode ( $x , true );
if ( $tmp !== NULL ) {
$x = $tmp ;
}
2021-06-19 05:12:23 +00:00
}
}
if ( is_string ( $x )) {
return ( $x );
2018-05-30 04:08:52 +00:00
}
2020-03-16 22:58:41 +00:00
2019-05-13 06:57:57 +00:00
if ( $x [ 'type' ] === ACTIVITY_OBJ_PERSON ) {
2018-05-30 04:08:52 +00:00
return self :: fetch_person ( $x );
}
2019-05-13 06:57:57 +00:00
if ( $x [ 'type' ] === ACTIVITY_OBJ_PROFILE ) {
2018-05-30 04:08:52 +00:00
return self :: fetch_profile ( $x );
}
2019-05-13 06:57:57 +00:00
if ( in_array ( $x [ 'type' ], [ ACTIVITY_OBJ_NOTE , ACTIVITY_OBJ_ARTICLE ] )) {
2019-09-10 04:03:33 +00:00
// Use Mastodon-specific note and media hacks if nomadic. Else HTML.
// Eventually this needs to be passed in much further up the stack
// and base the decision on whether or not we are encoding for ActivityPub or Zot6
2020-11-27 00:17:07 +00:00
return self :: fetch_item ( $x ,(( get_config ( 'system' , 'activitypub' , ACTIVITYPUB_ENABLED )) ? true : false ));
2018-05-30 04:08:52 +00:00
}
2019-05-13 06:57:57 +00:00
if ( $x [ 'type' ] === ACTIVITY_OBJ_THING ) {
2018-05-30 04:08:52 +00:00
return self :: fetch_thing ( $x );
}
2019-09-22 16:16:07 +00:00
call_hooks ( 'encode_object' , $x );
2018-07-17 02:08:10 +00:00
return $x ;
2018-05-30 04:08:52 +00:00
}
2019-01-14 05:22:45 +00:00
2019-02-19 05:07:48 +00:00
static function fetch ( $url , $channel = null , $hub = null ) {
2019-01-14 05:22:45 +00:00
$redirects = 0 ;
2019-05-13 06:57:57 +00:00
if ( ! check_siteallowed ( $url )) {
2020-06-14 11:58:27 +00:00
logger ( 'denied: ' . $url );
2019-01-14 05:22:45 +00:00
return null ;
}
2019-05-13 06:57:57 +00:00
if ( ! $channel ) {
2019-01-14 05:22:45 +00:00
$channel = get_sys_channel ();
}
2019-02-18 22:40:46 +00:00
2021-04-29 03:29:46 +00:00
$idn = parse_url ( $url );
if ( $idn [ 'host' ] !== punify ( $idn [ 'host' ])) {
$url = str_replace ( $idn [ 'host' ], punify ( $idn [ 'host' ]), $url );
}
2019-02-18 22:40:46 +00:00
logger ( 'fetch: ' . $url , LOGGER_DEBUG );
2019-05-13 06:57:57 +00:00
if ( strpos ( $url , 'x-zot:' ) === 0 ) {
2019-02-19 05:07:48 +00:00
$x = ZotURL :: fetch ( $url , $channel , $hub );
2019-02-18 22:40:46 +00:00
}
else {
2019-01-14 05:22:45 +00:00
$m = parse_url ( $url );
2019-08-16 06:53:02 +00:00
// handle bearcaps
if ( $m [ 'scheme' ] === 'bear' && $m [ 'query' ]) {
$params = explode ( '&' , $m [ 'query' ]);
if ( $params ) {
foreach ( $params as $p ) {
if ( substr ( $p , 0 , 2 ) === 'u=' ) {
$url = substr ( $p , 2 );
}
if ( substr ( $p , 0 , 2 ) === 't=' ) {
$token = substr ( $p , 2 );
}
}
2019-09-27 01:52:51 +00:00
// re-parse the URL because it changed and we need the host in the next section
$m = parse_url ( $url );
2019-08-16 06:53:02 +00:00
}
2019-09-27 01:52:51 +00:00
2019-08-16 06:53:02 +00:00
}
2019-01-14 05:22:45 +00:00
$headers = [
2021-02-19 01:55:51 +00:00
'Accept' => 'application/activity+json, application/x-zot-activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams"' ,
2019-01-14 05:22:45 +00:00
'Host' => $m [ 'host' ],
2019-11-08 20:16:29 +00:00
'Date' => datetime_convert ( 'UTC' , 'UTC' , 'now' , 'D, d M Y H:i:s \\G\\M\\T' ),
2019-11-03 23:07:49 +00:00
'(request-target)' => 'get ' . get_request_string ( $url )
2019-01-14 05:22:45 +00:00
];
2019-08-16 06:53:02 +00:00
if ( isset ( $token )) {
$headers [ 'Authorization' ] = 'Bearer ' . $token ;
}
2019-01-14 05:22:45 +00:00
$h = HTTPSig :: create_sig ( $headers , $channel [ 'channel_prvkey' ], channel_url ( $channel ), false );
2019-02-18 22:40:46 +00:00
$x = z_fetch_url ( $url , true , $redirects , [ 'headers' => $h ] );
2019-01-14 05:22:45 +00:00
}
2019-05-13 06:57:57 +00:00
if ( $x [ 'success' ]) {
2020-10-31 23:11:35 +00:00
2019-01-14 05:22:45 +00:00
$y = json_decode ( $x [ 'body' ], true );
logger ( 'returned: ' . json_encode ( $y , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ));
2020-10-31 23:11:35 +00:00
2021-01-29 00:47:01 +00:00
$m = parse_url ( $url );
if ( $m ) {
2021-03-08 23:36:50 +00:00
$site_url = unparse_url ( [ 'scheme' => $m [ 'scheme' ], 'host' => $m [ 'host' ], 'port' => (( array_key_exists ( 'port' , $m ) && intval ( $m [ 'port' ])) ? $m [ 'port' ] : 0 ) ] );
2021-01-29 00:47:01 +00:00
q ( " update site set site_update = '%s' where site_url = '%s' and site_update < %s - INTERVAL %s " ,
dbesc ( datetime_convert ()),
dbesc ( $site_url ),
db_utcnow (), db_quoteinterval ( '1 DAY' )
);
}
2020-10-31 23:11:35 +00:00
// check for a valid signature, but only if this is not an actor object. If it is signed, it must be valid.
// Ignore actors because of the potential for infinite recursion if we perform this step while
// fetching an actor key to validate a signature elsewhere. This should validate relayed activities
// over litepub which arrived at our inbox that do not use LD signatures
if (( $y [ 'type' ]) && ( ! ActivityStreams :: is_an_actor ( $y [ 'type' ]))) {
$sigblock = HTTPSig :: verify ( $x );
if (( $sigblock [ 'header_signed' ]) && ( ! $sigblock [ 'header_valid' ])) {
return null ;
}
}
2019-01-14 05:22:45 +00:00
return json_decode ( $x [ 'body' ], true );
2020-10-31 23:11:35 +00:00
2019-01-14 05:22:45 +00:00
}
else {
logger ( 'fetch failed: ' . $url );
}
return null ;
}
2018-05-30 04:08:52 +00:00
static function fetch_person ( $x ) {
return self :: fetch_profile ( $x );
}
static function fetch_profile ( $x ) {
2019-06-17 03:35:47 +00:00
$r = q ( " select * from xchan left join hubloc on xchan_hash = hubloc_hash where hubloc_id_url = '%s' limit 1 " ,
dbesc ( $x [ 'id' ])
2018-05-30 04:08:52 +00:00
);
2019-05-13 06:57:57 +00:00
if ( ! $r ) {
2018-05-30 04:08:52 +00:00
$r = q ( " select * from xchan where xchan_hash = '%s' limit 1 " ,
dbesc ( $x [ 'id' ])
);
2019-06-17 03:35:47 +00:00
}
2019-05-13 06:57:57 +00:00
if ( ! $r ) {
2018-05-30 04:08:52 +00:00
return [];
2019-05-13 06:57:57 +00:00
}
2019-06-17 03:35:47 +00:00
2018-09-18 05:51:27 +00:00
return self :: encode_person ( $r [ 0 ], false );
2018-05-30 04:08:52 +00:00
}
static function fetch_thing ( $x ) {
$r = q ( " select * from obj where obj_type = %d and obj_obj = '%s' limit 1 " ,
intval ( TERM_OBJ_THING ),
dbesc ( $x [ 'id' ])
);
2019-05-13 06:57:57 +00:00
if ( ! $r ) {
2018-05-30 04:08:52 +00:00
return [];
2019-05-13 06:57:57 +00:00
}
2018-05-30 04:08:52 +00:00
$x = [
'type' => 'Object' ,
'id' => z_root () . '/thing/' . $r [ 0 ][ 'obj_obj' ],
'name' => $r [ 0 ][ 'obj_term' ]
];
2019-05-13 06:57:57 +00:00
if ( $r [ 0 ][ 'obj_image' ]) {
2018-05-30 04:08:52 +00:00
$x [ 'image' ] = $r [ 0 ][ 'obj_image' ];
2019-05-13 06:57:57 +00:00
}
2018-05-30 04:08:52 +00:00
return $x ;
}
2019-09-10 04:03:33 +00:00
static function fetch_item ( $x , $activitypub = false ) {
2018-05-30 04:08:52 +00:00
2018-07-17 02:08:10 +00:00
if ( array_key_exists ( 'source' , $x )) {
// This item is already processed and encoded
return $x ;
}
2018-05-30 04:08:52 +00:00
$r = q ( " select * from item where mid = '%s' limit 1 " ,
dbesc ( $x [ 'id' ])
);
2019-05-13 06:57:57 +00:00
if ( $r ) {
2018-05-30 04:08:52 +00:00
xchan_query ( $r , true );
$r = fetch_post_tags ( $r , true );
2020-01-11 22:53:39 +00:00
if ( $r [ 0 ][ 'verb' ] === 'Invite' ) {
return self :: encode_activity ( $r [ 0 ], $activitypub );
}
2019-09-10 04:03:33 +00:00
return self :: encode_item ( $r [ 0 ], $activitypub );
2018-05-30 04:08:52 +00:00
}
}
2020-08-07 03:19:03 +00:00
static function paged_collection_init ( $total , $id , $type = 'OrderedCollection' ) {
2018-05-30 04:08:52 +00:00
$ret = [
'id' => z_root () . '/' . $id ,
'type' => $type ,
2020-08-07 03:19:03 +00:00
'totalItems' => $total ,
2018-05-30 04:08:52 +00:00
];
2020-08-07 03:19:03 +00:00
$numpages = $total / App :: $pager [ 'itemspage' ];
$lastpage = (( $numpages > intval ( $numpages )) ? intval ( $numpages ) + 1 : $numpages );
$ret [ 'first' ] = z_root () . '/' . App :: $query_string . '?page=1' ;
$ret [ 'last' ] = z_root () . '/' . App :: $query_string . '?page=' . $lastpage ;
return $ret ;
}
static function encode_item_collection ( $items , $id , $type , $activitypub = false , $total = 0 ) {
if ( $total > 100 ) {
$ret = [
'id' => z_root () . '/' . $id ,
'type' => $type . 'Page' ,
];
$numpages = $total / App :: $pager [ 'itemspage' ];
$lastpage = (( $numpages > intval ( $numpages )) ? intval ( $numpages ) + 1 : $numpages );
2021-02-13 22:38:31 +00:00
$url_parts = parse_url ( $id );
2020-08-07 03:19:03 +00:00
2021-02-13 22:38:31 +00:00
$ret [ 'partOf' ] = z_root () . '/' . $url_parts [ 'path' ];
$extra_query_args = '' ;
$query_args = null ;
if ( isset ( $url_parts [ 'query' ])) {
parse_str ( $url_parts [ 'query' ], $query_args );
}
if ( is_array ( $query_args )) {
unset ( $query_args [ 'page' ]);
foreach ( $query_args as $k => $v ) {
$extra_query_args .= '&' . urlencode ( $k ) . '=' . urlencode ( $v );
}
}
2020-08-07 03:19:03 +00:00
if ( App :: $pager [ 'page' ] < $lastpage ) {
2021-02-13 22:38:31 +00:00
$ret [ 'next' ] = z_root () . '/' . $url_parts [ 'path' ] . '?page=' . ( intval ( App :: $pager [ 'page' ]) + 1 ) . $extra_query_args ;
2020-08-07 03:19:03 +00:00
}
if ( App :: $pager [ 'page' ] > 1 ) {
2021-02-13 22:38:31 +00:00
$ret [ 'prev' ] = z_root () . '/' . $url_parts [ 'path' ] . '?page=' . ( intval ( App :: $pager [ 'page' ]) - 1 ) . $extra_query_args ;
2020-08-07 03:19:03 +00:00
}
}
else {
$ret = [
'id' => z_root () . '/' . $id ,
'type' => $type ,
'totalItems' => $total ,
];
}
2019-05-13 06:57:57 +00:00
if ( $items ) {
2018-05-30 04:08:52 +00:00
$x = [];
2019-05-13 06:57:57 +00:00
foreach ( $items as $i ) {
2018-09-10 03:55:51 +00:00
$m = get_iconfig ( $i [ 'id' ], 'activitypub' , 'rawmsg' );
2019-05-13 06:57:57 +00:00
if ( $m ) {
2018-09-10 03:55:51 +00:00
$t = json_decode ( $m , true );
}
else {
$t = self :: encode_activity ( $i , $activitypub );
}
2019-05-13 06:57:57 +00:00
if ( $t ) {
2018-05-30 04:08:52 +00:00
$x [] = $t ;
2019-05-13 06:57:57 +00:00
}
2018-05-30 04:08:52 +00:00
}
2019-05-13 06:57:57 +00:00
if ( $type === 'OrderedCollection' ) {
2018-05-30 04:08:52 +00:00
$ret [ 'orderedItems' ] = $x ;
2019-05-13 06:57:57 +00:00
}
else {
2018-05-30 04:08:52 +00:00
$ret [ 'items' ] = $x ;
2019-05-13 06:57:57 +00:00
}
2018-05-30 04:08:52 +00:00
}
return $ret ;
}
2020-08-07 03:19:03 +00:00
static function encode_follow_collection ( $items , $id , $type , $total = 0 , $extra = null ) {
2018-05-30 04:08:52 +00:00
2020-08-07 03:19:03 +00:00
if ( $total > 100 ) {
$ret = [
'id' => z_root () . '/' . $id ,
'type' => $type . 'Page' ,
];
$numpages = $total / App :: $pager [ 'itemspage' ];
$lastpage = (( $numpages > intval ( $numpages )) ? intval ( $numpages ) + 1 : $numpages );
$stripped = preg_replace ( '/([&|\?]page=[0-9]*)/' , '' , $id );
$stripped = rtrim ( $stripped , '/' );
$ret [ 'partOf' ] = z_root () . '/' . $stripped ;
if ( App :: $pager [ 'page' ] < $lastpage ) {
$ret [ 'next' ] = z_root () . '/' . $stripped . '?page=' . ( intval ( App :: $pager [ 'page' ]) + 1 );
}
if ( App :: $pager [ 'page' ] > 1 ) {
$ret [ 'prev' ] = z_root () . '/' . $stripped . '?page=' . ( intval ( App :: $pager [ 'page' ]) - 1 );
}
}
else {
$ret = [
'id' => z_root () . '/' . $id ,
'type' => $type ,
'totalItems' => $total ,
];
}
2019-05-13 06:57:57 +00:00
if ( $extra ) {
2018-05-30 04:08:52 +00:00
$ret = array_merge ( $ret , $extra );
2019-05-13 06:57:57 +00:00
}
2018-05-30 04:08:52 +00:00
2019-05-13 06:57:57 +00:00
if ( $items ) {
2018-05-30 04:08:52 +00:00
$x = [];
2019-05-13 06:57:57 +00:00
foreach ( $items as $i ) {
2020-09-28 23:46:08 +00:00
if ( $i [ 'xchan_network' ] === 'activitypub' ) {
$x [] = $i [ 'xchan_hash' ];
}
else {
2018-05-30 04:08:52 +00:00
$x [] = $i [ 'xchan_url' ];
}
}
2019-05-13 06:57:57 +00:00
if ( $type === 'OrderedCollection' ) {
2018-05-30 04:08:52 +00:00
$ret [ 'orderedItems' ] = $x ;
2019-05-13 06:57:57 +00:00
}
else {
2018-05-30 04:08:52 +00:00
$ret [ 'items' ] = $x ;
2019-05-13 06:57:57 +00:00
}
2018-05-30 04:08:52 +00:00
}
return $ret ;
}
2021-04-01 00:22:47 +00:00
static function encode_simple_collection ( $items , $id , $type , $total = 0 , $extra = null ) {
$ret = [
'id' => z_root () . '/' . $id ,
'type' => $type ,
'totalItems' => $total ,
];
if ( $extra ) {
$ret = array_merge ( $ret , $extra );
}
if ( $items ) {
if ( $type === 'OrderedCollection' ) {
$ret [ 'orderedItems' ] = $items ;
}
else {
$ret [ 'items' ] = $items ;
}
}
return $ret ;
}
2018-05-30 04:08:52 +00:00
static function decode_taxonomy ( $item ) {
$ret = [];
2021-03-08 23:36:50 +00:00
if ( array_key_exists ( 'tag' , $item ) && is_array ( $item [ 'tag' ])) {
2019-11-03 22:21:47 +00:00
$ptr = $item [ 'tag' ];
if ( ! array_key_exists ( 0 , $ptr )) {
$ptr = [ $ptr ];
}
foreach ( $ptr as $t ) {
2020-11-11 01:11:19 +00:00
if ( ! array_key_exists ( 'type' , $t )) {
2018-05-30 04:08:52 +00:00
$t [ 'type' ] = 'Hashtag' ;
2020-11-11 01:11:19 +00:00
}
2021-03-08 23:36:50 +00:00
if ( ! ( array_key_exists ( 'name' , $t ))) {
continue ;
}
if ( ! ( array_path_exists ( 'icon/url' , $t ) || array_key_exists ( 'href' , $t ))) {
continue ;
}
2018-05-30 04:08:52 +00:00
switch ( $t [ 'type' ]) {
case 'Hashtag' :
2018-06-05 06:19:16 +00:00
$ret [] = [ 'ttype' => TERM_HASHTAG , 'url' => $t [ 'href' ], 'term' => escape_tags (( substr ( $t [ 'name' ], 0 , 1 ) === '#' ) ? substr ( $t [ 'name' ], 1 ) : $t [ 'name' ]) ];
2018-05-30 04:08:52 +00:00
break ;
2019-04-03 23:22:03 +00:00
case 'topicalCollection' :
$ret [] = [ 'ttype' => TERM_PCATEGORY , 'url' => $t [ 'href' ], 'term' => escape_tags ( $t [ 'name' ]) ];
break ;
2020-11-11 01:11:19 +00:00
case 'Category' :
$ret [] = [ 'ttype' => TERM_CATEGORY , 'url' => $t [ 'href' ], 'term' => escape_tags ( $t [ 'name' ]) ];
break ;
2018-05-30 04:08:52 +00:00
case 'Mention' :
2018-06-29 03:25:43 +00:00
$mention_type = substr ( $t [ 'name' ], 0 , 1 );
2019-05-13 06:57:57 +00:00
if ( $mention_type === '!' ) {
2018-06-29 03:25:43 +00:00
$ret [] = [ 'ttype' => TERM_FORUM , 'url' => $t [ 'href' ], 'term' => escape_tags ( substr ( $t [ 'name' ], 1 )) ];
}
else {
$ret [] = [ 'ttype' => TERM_MENTION , 'url' => $t [ 'href' ], 'term' => escape_tags (( substr ( $t [ 'name' ], 0 , 1 ) === '@' ) ? substr ( $t [ 'name' ], 1 ) : $t [ 'name' ]) ];
}
2018-05-30 04:08:52 +00:00
break ;
2018-09-06 23:40:13 +00:00
case 'Emoji' :
2018-09-07 05:52:48 +00:00
$ret [] = [ 'ttype' => TERM_EMOJI , 'url' => $t [ 'icon' ][ 'url' ], 'term' => escape_tags ( $t [ 'name' ]) ];
2018-09-06 23:40:13 +00:00
break ;
2018-05-30 04:08:52 +00:00
default :
break ;
}
}
}
return $ret ;
}
static function encode_taxonomy ( $item ) {
$ret = [];
2021-03-12 01:09:22 +00:00
if ( isset ( $item [ 'term' ]) && is_array ( $item [ 'term' ]) && $item [ 'term' ]) {
2019-05-13 06:57:57 +00:00
foreach ( $item [ 'term' ] as $t ) {
2018-05-30 04:08:52 +00:00
switch ( $t [ 'ttype' ]) {
case TERM_HASHTAG :
// An id is required so if we don't have a url in the taxonomy, ignore it and keep going.
2019-05-13 06:57:57 +00:00
if ( $t [ 'url' ]) {
2018-05-30 04:08:52 +00:00
$ret [] = [ 'id' => $t [ 'url' ], 'name' => '#' . $t [ 'term' ] ];
}
break ;
2019-04-03 23:22:03 +00:00
case TERM_PCATEGORY :
2019-05-13 06:57:57 +00:00
if ( $t [ 'url' ] && $t [ 'term' ]) {
2019-04-03 23:22:03 +00:00
$ret [] = [ 'type' => 'topicalCollection' , 'href' => $t [ 'url' ], 'name' => $t [ 'term' ] ];
}
break ;
2020-11-11 01:11:19 +00:00
case TERM_CATEGORY :
if ( $t [ 'url' ] && $t [ 'term' ]) {
$ret [] = [ 'type' => 'Category' , 'href' => $t [ 'url' ], 'name' => $t [ 'term' ] ];
}
break ;
2018-06-29 03:25:43 +00:00
case TERM_FORUM :
2018-11-08 02:24:22 +00:00
$term = self :: lookup_term_addr ( $t [ 'url' ], $t [ 'term' ]);
$ret [] = [ 'type' => 'Mention' , 'href' => $t [ 'url' ], 'name' => '!' . (( $term ) ? $term : $t [ 'term' ]) ];
2018-06-29 03:25:43 +00:00
break ;
2018-05-30 04:08:52 +00:00
case TERM_MENTION :
2018-11-08 02:24:22 +00:00
$term = self :: lookup_term_addr ( $t [ 'url' ], $t [ 'term' ]);
$ret [] = [ 'type' => 'Mention' , 'href' => $t [ 'url' ], 'name' => '@' . (( $term ) ? $term : $t [ 'term' ]) ];
2018-05-30 04:08:52 +00:00
break ;
default :
break ;
}
}
}
return $ret ;
}
2018-11-08 02:24:22 +00:00
static function lookup_term_addr ( $url , $name ) {
// The visible mention in our activities is always the full name.
// In the object taxonomy change this to the webfinger handle in case
// platforms expect the Mastodon form in order to generate notifications
// Try a couple of different things in case the url provided isn't the canonical id.
// If all else fails, try to match the name.
$r = false ;
2019-05-13 06:57:57 +00:00
if ( $url ) {
2018-11-08 02:24:22 +00:00
$r = q ( " select xchan_addr from xchan where ( xchan_url = '%s' OR xchan_hash = '%s' ) limit 1 " ,
dbesc ( $url ),
dbesc ( $url )
);
2019-05-13 06:57:57 +00:00
if ( $r ) {
2018-11-08 02:24:22 +00:00
return $r [ 0 ][ 'xchan_addr' ];
}
}
2019-05-13 06:57:57 +00:00
if ( $name ) {
2018-11-08 02:24:22 +00:00
$r = q ( " select xchan_addr from xchan where xchan_name = '%s' limit 1 " ,
dbesc ( $name )
);
2019-05-13 06:57:57 +00:00
if ( $r ) {
2018-11-08 02:24:22 +00:00
return $r [ 0 ][ 'xchan_addr' ];
}
}
return EMPTY_STR ;
}
static function lookup_term_url ( $url ) {
// The xchan_url for mastodon is a text/html rendering. This is called from map_mentions where we need
// to convert the mention url to an ActivityPub id. If this fails for any reason, return the url we have
2020-05-15 02:48:18 +00:00
$r = q ( " select * from hubloc where hubloc_id_url = '%s' or hubloc_hash = '%s' limit 1 " ,
dbesc ( $url ),
2018-11-08 02:24:22 +00:00
dbesc ( $url )
);
2019-05-13 06:57:57 +00:00
if ( $r ) {
2019-12-17 23:28:24 +00:00
if ( $r [ 0 ][ 'hubloc_network' ] === 'activitypub' ) {
return $r [ 0 ][ 'hubloc_hash' ];
}
2018-11-08 02:24:22 +00:00
return $r [ 0 ][ 'hubloc_id_url' ];
}
2019-12-17 23:28:24 +00:00
return $url ;
2018-11-08 02:24:22 +00:00
}
2018-05-30 04:08:52 +00:00
static function encode_attachment ( $item ) {
$ret = [];
2021-03-08 23:36:50 +00:00
if ( array_key_exists ( 'attach' , $item )) {
2019-04-11 11:10:59 +00:00
$atts = (( is_array ( $item [ 'attach' ])) ? $item [ 'attach' ] : json_decode ( $item [ 'attach' ], true ));
2019-05-13 06:57:57 +00:00
if ( $atts ) {
foreach ( $atts as $att ) {
if ( strpos ( $att [ 'type' ], 'image' )) {
2018-05-30 04:08:52 +00:00
$ret [] = [ 'type' => 'Image' , 'url' => $att [ 'href' ] ];
}
else {
$ret [] = [ 'type' => 'Link' , 'mediaType' => $att [ 'type' ], 'href' => $att [ 'href' ] ];
}
}
}
}
2021-03-08 23:36:50 +00:00
if ( array_key_exists ( 'iconfig' , $item ) && is_array ( $item [ 'iconfig' ])) {
2020-02-23 23:27:39 +00:00
foreach ( $item [ 'iconfig' ] as $att ) {
if ( $att [ 'sharing' ]) {
$ret [] = [ 'type' => 'PropertyValue' , 'name' => 'zot.' . $att [ 'cat' ] . '.' . $att [ 'k' ], 'value' => unserialise ( $att [ 'v' ]) ];
}
}
}
2018-05-30 04:08:52 +00:00
return $ret ;
}
2020-02-23 23:27:39 +00:00
static function decode_iconfig ( $item ) {
$ret = [];
if ( is_array ( $item [ 'attachment' ]) && $item [ 'attachment' ]) {
$ptr = $item [ 'attachment' ];
if ( ! array_key_exists ( 0 , $ptr )) {
$ptr = [ $ptr ];
}
foreach ( $ptr as $att ) {
$entry = [];
if ( $att [ 'type' ] === 'PropertyValue' ) {
if ( array_key_exists ( 'name' , $att ) && $att [ 'name' ]) {
$key = explode ( '.' , $att [ 'name' ]);
if ( count ( $key ) === 3 && $key [ 0 ] === 'zot' ) {
$entry [ 'cat' ] = $key [ 1 ];
$entry [ 'k' ] = $key [ 2 ];
$entry [ 'v' ] = $att [ 'value' ];
$entry [ 'sharing' ] = '1' ;
$ret [] = $entry ;
}
}
}
}
}
return $ret ;
}
2018-05-30 04:08:52 +00:00
static function decode_attachment ( $item ) {
$ret = [];
2021-03-08 23:36:50 +00:00
if ( array_key_exists ( 'attachment' , $item ) && is_array ( $item [ 'attachment' ])) {
2019-11-03 22:21:47 +00:00
$ptr = $item [ 'attachment' ];
if ( ! array_key_exists ( 0 , $ptr )) {
$ptr = [ $ptr ];
}
foreach ( $ptr as $att ) {
2018-05-30 04:08:52 +00:00
$entry = [];
2021-03-08 23:36:50 +00:00
if ( array_key_exists ( 'href' , $att ) && $att [ 'href' ])
2018-05-30 04:08:52 +00:00
$entry [ 'href' ] = $att [ 'href' ];
2021-03-08 23:36:50 +00:00
elseif ( array_key_exists ( 'url' , $att ) && $att [ 'url' ])
2018-05-30 04:08:52 +00:00
$entry [ 'href' ] = $att [ 'url' ];
2021-03-08 23:36:50 +00:00
if ( array_key_exists ( 'mediaType' , $att ) && $att [ 'mediaType' ])
2018-05-30 04:08:52 +00:00
$entry [ 'type' ] = $att [ 'mediaType' ];
2021-03-08 23:36:50 +00:00
elseif ( array_key_exists ( 'type' , $att ) && $att [ 'type' ] === 'Image' )
2018-05-30 04:08:52 +00:00
$entry [ 'type' ] = 'image/jpeg' ;
2021-04-06 20:31:44 +00:00
if ( array_key_exists ( 'name' , $att ) && $att [ 'name' ]) {
$entry [ 'name' ] = html2plain ( purify_html ( $att [ 'name' ]), 256 );
}
2019-05-13 06:57:57 +00:00
if ( $entry )
2018-05-30 04:08:52 +00:00
$ret [] = $entry ;
}
}
2019-11-09 20:33:47 +00:00
elseif ( is_string ( $item [ 'attachment' ])) {
2019-11-03 22:21:47 +00:00
btlogger ( 'not an array: ' . $item [ 'attachment' ]);
}
2018-05-30 04:08:52 +00:00
return $ret ;
}
2020-10-06 02:41:47 +00:00
// the $recurse flag encodes the original non-deleted object of a deleted activity
2018-05-30 04:08:52 +00:00
2020-10-06 02:41:47 +00:00
static function encode_activity ( $i , $activitypub = false , $recurse = false ) {
2018-05-30 04:08:52 +00:00
$ret = [];
$reply = false ;
2020-10-06 02:41:47 +00:00
if ( intval ( $i [ 'item_deleted' ]) && ( ! $recurse )) {
$is_response = ActivityStreams :: is_response_activity ( $i [ 'verb' ]);
if ( $is_response ) {
2020-10-01 06:52:04 +00:00
$ret [ 'type' ] = 'Undo' ;
2020-10-06 02:41:47 +00:00
$fragment = '#undo' ;
2020-10-01 06:52:04 +00:00
}
else {
$ret [ 'type' ] = 'Delete' ;
2020-10-06 02:41:47 +00:00
$fragment = '#delete' ;
2020-10-01 06:52:04 +00:00
}
2020-10-06 02:41:47 +00:00
$ret [ 'id' ] = str_replace ( '/item/' , '/activity/' , $i [ 'mid' ]) . $fragment ;
2018-10-11 01:53:26 +00:00
$actor = self :: encode_person ( $i [ 'author' ], false );
2019-05-13 06:57:57 +00:00
if ( $actor )
2018-10-11 01:53:26 +00:00
$ret [ 'actor' ] = $actor ;
else
return [];
2020-10-06 02:41:47 +00:00
$obj = (( $is_response ) ? self :: encode_activity ( $i , $activitypub , true ) : self :: encode_item ( $i , $activitypub ));
if ( $obj ) {
if ( array_path_exists ( 'object/id' , $obj )) {
$obj [ 'object' ] = $obj [ 'object' ][ 'id' ];
}
if ( $obj ) {
$ret [ 'object' ] = $obj ;
}
}
else {
return [];
}
2018-10-11 01:53:26 +00:00
$ret [ 'to' ] = [ ACTIVITY_PUBLIC_INBOX ];
2018-05-30 04:08:52 +00:00
return $ret ;
2018-10-11 01:53:26 +00:00
2018-05-30 04:08:52 +00:00
}
2018-05-30 06:30:18 +00:00
$ret [ 'type' ] = self :: activity_mapper ( $i [ 'verb' ]);
2018-09-03 06:12:53 +00:00
2019-05-13 06:57:57 +00:00
if ( strpos ( $i [ 'mid' ], z_root () . '/item/' ) !== false ) {
2018-09-04 01:22:31 +00:00
$ret [ 'id' ] = str_replace ( '/item/' , '/activity/' , $i [ 'mid' ]);
}
2019-05-13 06:57:57 +00:00
elseif ( strpos ( $i [ 'mid' ], z_root () . '/event/' ) !== false ) {
2018-10-16 20:56:46 +00:00
$ret [ 'id' ] = str_replace ( '/event/' , '/activity/' , $i [ 'mid' ]);
}
2018-09-04 01:22:31 +00:00
else {
$ret [ 'id' ] = $i [ 'mid' ];
2018-09-03 06:12:53 +00:00
}
2018-05-30 04:08:52 +00:00
2019-05-13 06:57:57 +00:00
if ( $i [ 'title' ]) {
2018-08-20 03:39:23 +00:00
$ret [ 'name' ] = $i [ 'title' ];
}
2018-07-17 02:08:10 +00:00
2019-05-13 06:57:57 +00:00
if ( $i [ 'summary' ]) {
2018-10-05 04:16:20 +00:00
$ret [ 'summary' ] = bbcode ( $i [ 'summary' ], [ 'export' => true ]);
2018-08-20 03:39:23 +00:00
}
2018-05-30 04:08:52 +00:00
2019-05-13 06:57:57 +00:00
if ( $ret [ 'type' ] === 'Announce' ) {
2018-09-03 03:16:33 +00:00
$tmp = $i [ 'body' ];
2018-10-05 04:16:20 +00:00
$ret [ 'content' ] = bbcode ( $tmp , [ 'export' => true ]);
2018-08-01 02:39:50 +00:00
$ret [ 'source' ] = [
'content' => $i [ 'body' ],
'mediaType' => 'text/bbcode'
];
2021-03-22 05:33:52 +00:00
if ( $i [ 'summary' ]) {
$ret [ 'source' ][ 'summary' ] = $i [ 'summary' ];
}
2018-08-01 02:39:50 +00:00
}
2018-05-30 04:08:52 +00:00
$ret [ 'published' ] = datetime_convert ( 'UTC' , 'UTC' , $i [ 'created' ], ATOM_TIME );
2020-02-07 02:50:18 +00:00
if ( $i [ 'created' ] !== $i [ 'edited' ]) {
2018-05-30 04:08:52 +00:00
$ret [ 'updated' ] = datetime_convert ( 'UTC' , 'UTC' , $i [ 'edited' ], ATOM_TIME );
2020-02-07 02:50:18 +00:00
if ( $ret [ 'type' ] === 'Create' ) {
$ret [ 'type' ] = 'Update' ;
}
}
2019-05-13 06:57:57 +00:00
if ( $i [ 'app' ]) {
2019-05-29 04:10:32 +00:00
$ret [ 'generator' ] = [ 'type' => 'Application' , 'name' => $i [ 'app' ] ];
2018-05-30 04:08:52 +00:00
}
2019-05-13 06:57:57 +00:00
if ( $i [ 'location' ] || $i [ 'coord' ]) {
2018-05-30 04:08:52 +00:00
$ret [ 'location' ] = [ 'type' => 'Place' ];
2019-05-13 06:57:57 +00:00
if ( $i [ 'location' ]) {
2018-05-30 04:08:52 +00:00
$ret [ 'location' ][ 'name' ] = $i [ 'location' ];
}
2019-05-13 06:57:57 +00:00
if ( $i [ 'coord' ]) {
2018-05-30 04:08:52 +00:00
$l = explode ( ' ' , $i [ 'coord' ]);
$ret [ 'location' ][ 'latitude' ] = $l [ 0 ];
$ret [ 'location' ][ 'longitude' ] = $l [ 1 ];
}
}
2019-10-01 22:52:42 +00:00
if ( $i [ 'mid' ] !== $i [ 'parent_mid' ]) {
$reply = true ;
2019-11-14 01:08:18 +00:00
2020-01-11 22:53:39 +00:00
// inReplyTo needs to be set in the activity for followup actions (Like, Dislike, Announce, etc.),
// but *not* for comments and RSVPs, where it should only be present in the object
2019-11-14 01:08:18 +00:00
2020-01-11 22:53:39 +00:00
if ( ! in_array ( $ret [ 'type' ],[ 'Create' , 'Update' , 'Accept' , 'Reject' , 'TentativeAccept' , 'TentativeReject' ])) {
2019-11-14 01:08:18 +00:00
$ret [ 'inReplyTo' ] = $i [ 'thr_parent' ];
2021-04-17 06:53:35 +00:00
$cnv = get_iconfig ( $i [ 'parent' ], 'activitypub' , 'context' );
if ( ! $cnv ) {
$cnv = get_iconfig ( $i [ 'parent' ], 'ostatus' , 'conversation' );
}
2019-11-14 01:08:18 +00:00
if ( ! $cnv ) {
$cnv = $ret [ 'parent_mid' ];
}
2018-12-02 22:39:58 +00:00
}
2018-05-30 04:08:52 +00:00
}
2021-03-12 01:09:22 +00:00
if ( ! ( isset ( $cnv ) && $cnv )) {
2021-04-17 06:53:35 +00:00
$cnv = get_iconfig ( $i , 'activitypub' , 'context' );
if ( ! $cnv ) {
2021-03-12 04:36:42 +00:00
$cnv = get_iconfig ( $i , 'ostatus' , 'conversation' );
}
2021-04-17 06:53:35 +00:00
if ( ! $cnv ) {
2021-03-12 04:36:42 +00:00
$cnv = $i [ 'parent_mid' ];
}
2018-12-02 22:39:58 +00:00
}
2021-03-12 01:09:22 +00:00
if ( isset ( $cnv ) && $cnv ) {
2021-04-17 06:53:35 +00:00
if ( strpos ( $cnv , z_root ()) === 0 ) {
$cnv = str_replace ([ '/item/' , '/activity/' ],[ '/conversation/' , '/conversation/' ], $cnv );
}
$ret [ 'context' ] = $cnv ;
2018-12-02 22:39:58 +00:00
$ret [ 'conversation' ] = $cnv ;
}
2021-06-19 05:12:23 +00:00
2020-01-17 01:22:32 +00:00
if ( intval ( $i [ 'item_private' ]) === 2 ) {
$ret [ 'directMessage' ] = true ;
}
2018-06-26 03:55:53 +00:00
$actor = self :: encode_person ( $i [ 'author' ], false );
2019-05-13 06:57:57 +00:00
if ( $actor )
2018-05-30 04:08:52 +00:00
$ret [ 'actor' ] = $actor ;
else
return [];
2021-04-17 23:34:36 +00:00
$replyto = unserialise ( $i [ 'replyto' ]);
if ( $replyto ) {
$ret [ 'replyTo' ] = $replyto ;
}
2021-06-19 05:12:23 +00:00
2021-03-19 01:53:29 +00:00
if ( ! isset ( $ret [ 'url' ])) {
$urls = [];
if ( intval ( $i [ 'item_wall' ])) {
$locs = self :: nomadic_locations ( $i );
if ( $locs ) {
foreach ( $locs as $l ) {
if ( strpos ( $ret [ 'id' ], $l [ 'hubloc_url' ]) !== false ) {
continue ;
}
$urls [] = [
'type' => 'Link' ,
'href' => str_replace ( z_root (), $l [ 'hubloc_url' ], $ret [ 'id' ]),
'rel' => 'alternate' ,
'mediaType' => 'text/html'
];
$urls [] = [
'type' => 'Link' ,
'href' => str_replace ( z_root (), $l [ 'hubloc_url' ], $ret [ 'id' ]),
'rel' => 'alternate' ,
'mediaType' => 'application/activity+json'
];
$urls [] = [
'type' => 'Link' ,
'href' => str_replace ( z_root (), $l [ 'hubloc_url' ], $ret [ 'id' ]),
'rel' => 'alternate' ,
'mediaType' => 'application/x-zot+json'
];
}
}
}
if ( $urls ) {
$curr [] = [
'type' => 'Link' ,
'href' => $ret [ 'id' ],
'rel' => 'alternate' ,
'mediaType' => 'text/html'
];
$ret [ 'url' ] = array_merge ( $curr , $urls );
}
else {
$ret [ 'url' ] = $ret [ 'id' ];
}
}
2019-05-13 06:57:57 +00:00
if ( $i [ 'obj' ]) {
2021-06-19 23:04:32 +00:00
if ( is_string ( $i [ 'obj' ])) {
$tmp = json_decode ( $i [ 'obj' ], true );
if ( $tmp !== NULL ) {
$i [ 'obj' ] = $tmp ;
}
2018-07-12 01:02:25 +00:00
}
2018-05-30 04:08:52 +00:00
$obj = self :: encode_object ( $i [ 'obj' ]);
2019-05-13 06:57:57 +00:00
if ( $obj )
2018-05-30 04:08:52 +00:00
$ret [ 'object' ] = $obj ;
else
return [];
}
else {
2018-10-04 02:10:52 +00:00
$obj = self :: encode_item ( $i , $activitypub );
2021-06-19 05:12:23 +00:00
if ( $obj ) {
2018-05-30 04:08:52 +00:00
$ret [ 'object' ] = $obj ;
2021-06-19 05:12:23 +00:00
}
else {
2018-05-30 04:08:52 +00:00
return [];
2021-06-19 05:12:23 +00:00
}
2018-05-30 04:08:52 +00:00
}
2019-05-13 06:57:57 +00:00
if ( $i [ 'target' ]) {
2021-06-19 23:04:32 +00:00
if ( is_string ( $i [ 'target' ])) {
$tmp = json_decode ( $i [ 'target' ], true );
if ( $tmp !== NULL ) {
$i [ 'target' ] = $tmp ;
}
2018-07-12 01:02:25 +00:00
}
2018-05-30 04:08:52 +00:00
$tgt = self :: encode_object ( $i [ 'target' ]);
2019-05-13 06:57:57 +00:00
if ( $tgt ) {
2018-05-30 04:08:52 +00:00
$ret [ 'target' ] = $tgt ;
2019-05-01 06:18:39 +00:00
}
2018-05-30 04:08:52 +00:00
}
2019-10-03 01:07:13 +00:00
$t = self :: encode_taxonomy ( $i );
if ( $t ) {
$ret [ 'tag' ] = $t ;
}
2019-10-01 22:52:42 +00:00
// addressing madness
2019-05-13 06:57:57 +00:00
if ( $activitypub ) {
2019-10-01 22:52:42 +00:00
2019-10-03 01:07:13 +00:00
$public = (( $i [ 'item_private' ]) ? false : true );
2019-10-01 22:52:42 +00:00
$top_level = (( $reply ) ? false : true );
2021-06-04 04:57:08 +00:00
if ( ! $top_level ) {
$recips = get_iconfig ( $i [ 'parent' ], 'activitypub' , 'recips' );
if ( $recips ) {
$parent_i [ 'to' ] = $recips [ 'to' ];
$parent_i [ 'cc' ] = $recips [ 'cc' ];
}
}
2019-10-01 22:52:42 +00:00
if ( $public ) {
2019-12-05 23:55:45 +00:00
$ret [ 'to' ] = [ ACTIVITY_PUBLIC_INBOX ];
2021-06-04 04:57:08 +00:00
if ( isset ( $parent_i [ 'to' ]) && is_array ( $parent_i [ 'to' ])) {
$ret [ 'to' ] = array_values ( array_unique ( array_merge ( $ret [ 'to' ], $parent_i [ 'to' ])));
}
2019-12-05 23:55:45 +00:00
$ret [ 'cc' ] = [ z_root () . '/followers/' . substr ( $i [ 'author' ][ 'xchan_addr' ], 0 , strpos ( $i [ 'author' ][ 'xchan_addr' ], '@' )) ];
2021-06-04 04:57:08 +00:00
if ( isset ( $parent_i [ 'cc' ]) && is_array ( $parent_i [ 'cc' ])) {
$ret [ 'cc' ] = array_values ( array_unique ( array_merge ( $ret [ 'cc' ], $parent_i [ 'cc' ])));
}
2019-10-01 22:52:42 +00:00
}
else {
2019-10-02 00:44:58 +00:00
// private activity
2019-10-01 22:52:42 +00:00
if ( $top_level ) {
2019-10-03 01:07:13 +00:00
$ret [ 'to' ] = self :: map_acl ( $i );
2021-06-04 04:57:08 +00:00
if ( isset ( $parent_i [ 'to' ]) && is_array ( $parent_i [ 'to' ])) {
$ret [ 'to' ] = array_values ( array_unique ( array_merge ( $ret [ 'to' ], $parent_i [ 'to' ])));
}
2019-10-03 01:07:13 +00:00
}
else {
2020-08-20 00:59:05 +00:00
$ret [ 'cc' ] = self :: map_acl ( $i );
2021-06-04 04:57:08 +00:00
if ( isset ( $parent_i [ 'cc' ]) && is_array ( $parent_i [ 'cc' ])) {
$ret [ 'cc' ] = array_values ( array_unique ( array_merge ( $ret [ 'cc' ], $parent_i [ 'cc' ])));
}
2019-10-03 01:07:13 +00:00
if ( $ret [ 'tag' ]) {
foreach ( $ret [ 'tag' ] as $mention ) {
2020-08-20 00:59:05 +00:00
if ( is_array ( $mention ) && array_key_exists ( 'ttype' , $mention ) && in_array ( $mention [ 'ttype' ],[ TERM_FORUM , TERM_MENTION ]) && array_key_exists ( 'href' , $mention ) && $mention [ 'href' ]) {
2019-12-17 23:28:24 +00:00
$h = q ( " select * from hubloc where hubloc_id_url = '%s' limit 1 " ,
dbesc ( $mention [ 'href' ])
);
if ( $h ) {
if ( $h [ 0 ][ 'hubloc_network' ] === 'activitypub' ) {
$addr = $h [ 0 ][ 'hubloc_hash' ];
}
else {
$addr = $h [ 0 ][ 'hubloc_id_url' ];
}
if ( ! in_array ( $addr , $ret [ 'to' ])) {
$ret [ 'to' ][] = $addr ;
}
}
2019-09-21 22:26:55 +00:00
}
}
}
2019-12-17 23:28:24 +00:00
2020-08-20 00:59:05 +00:00
$d = q ( " select hubloc.* from hubloc left join item on hubloc_hash = owner_xchan where item.parent_mid = '%s' and item.uid = %d limit 1 " ,
dbesc ( $i [ 'parent_mid' ]),
intval ( $i [ 'uid' ])
2019-10-01 22:52:42 +00:00
);
if ( $d ) {
2019-12-17 23:28:24 +00:00
if ( $d [ 0 ][ 'hubloc_network' ] === 'activitypub' ) {
$addr = $d [ 0 ][ 'hubloc_hash' ];
}
else {
$addr = $d [ 0 ][ 'hubloc_id_url' ];
}
2020-08-20 06:34:02 +00:00
$ret [ 'cc' ][] = $addr ;
2019-10-03 01:07:13 +00:00
}
}
}
$mentions = self :: map_mentions ( $i );
if ( count ( $mentions ) > 0 ) {
if ( ! $ret [ 'to' ]) {
$ret [ 'to' ] = $mentions ;
}
else {
$ret [ 'to' ] = array_values ( array_unique ( array_merge ( $ret [ 'to' ], $mentions )));
}
}
}
2020-08-20 00:59:05 +00:00
$cc = [];
if ( $ret [ 'cc' ] && is_array ( $ret [ 'cc' ])) {
foreach ( $ret [ 'cc' ] as $e ) {
if ( ! is_array ( $ret [ 'to' ])) {
$cc [] = $e ;
}
elseif ( ! in_array ( $e , $ret [ 'to' ])) {
$cc [] = $e ;
}
}
}
$ret [ 'cc' ] = $cc ;
2019-10-03 01:07:13 +00:00
return $ret ;
}
2021-03-18 01:30:37 +00:00
static function nomadic_locations ( $item ) {
$synchubs = [];
$h = q ( " select hubloc.*, site.site_crypto from hubloc left join site on site_url = hubloc_url
where hubloc_hash = '%s' and hubloc_network = 'zot6' and hubloc_deleted = 0 " ,
dbesc ( $item [ 'author_xchan' ])
);
if ( ! $h ) {
return [];
}
foreach ( $h as $x ) {
$y = q ( " select site_dead from site where site_url = '%s' limit 1 " ,
dbesc ( $x [ 'hubloc_url' ])
);
if (( ! $y ) || intval ( $y [ 0 ][ 'site_dead' ]) === 0 ) {
$synchubs [] = $x ;
}
}
return $synchubs ;
}
2019-10-03 01:07:13 +00:00
static function encode_item ( $i , $activitypub = false ) {
$ret = [];
$reply = false ;
$is_directmessage = false ;
2019-11-25 23:54:23 +00:00
$bbopts = (( $activitypub ) ? 'activitypub' : 'export' );
2019-10-03 01:07:13 +00:00
$objtype = self :: activity_obj_mapper ( $i [ 'obj_type' ]);
if ( intval ( $i [ 'item_deleted' ])) {
$ret [ 'type' ] = 'Tombstone' ;
$ret [ 'formerType' ] = $objtype ;
$ret [ 'id' ] = $i [ 'mid' ];
$ret [ 'to' ] = [ ACTIVITY_PUBLIC_INBOX ];
return $ret ;
}
2021-06-20 03:54:54 +00:00
if ( isset ( $i [ 'obj' ]) && $i [ 'obj' ]) {
2021-06-19 23:04:32 +00:00
if ( is_string ( $i [ 'obj' ])) {
$tmp = json_decode ( $i [ 'obj' ], true );
if ( $tmp !== NULL ) {
$i [ 'obj' ] = $tmp ;
}
2020-03-16 22:58:41 +00:00
}
2021-06-19 23:04:32 +00:00
$ret = $i [ 'obj' ];
2021-06-20 03:49:30 +00:00
if ( is_string ( $ret )) {
return $ret ;
}
2020-03-16 22:58:41 +00:00
}
2021-06-20 03:49:30 +00:00
2019-10-03 01:07:13 +00:00
$ret [ 'type' ] = $objtype ;
2020-02-06 01:45:48 +00:00
if ( $objtype === 'Question' ) {
if ( $i [ 'obj' ]) {
if ( is_array ( $i [ 'obj' ])) {
$ret = $i [ 'obj' ];
}
else {
$ret = json_decode ( $i [ 'obj' ], true );
}
2020-02-07 05:51:36 +00:00
if ( array_path_exists ( 'actor/id' , $ret )) {
$ret [ 'actor' ] = $ret [ 'actor' ][ 'id' ];
}
2020-02-06 05:58:50 +00:00
}
2020-02-06 01:45:48 +00:00
}
2019-10-03 01:07:13 +00:00
$images = false ;
2020-05-17 23:20:11 +00:00
$has_images = preg_match_all ( '/\[[zi]mg(.*?)\](.*?)\[/ism' , $i [ 'body' ], $images , PREG_SET_ORDER );
2019-10-03 01:07:13 +00:00
$ret [ 'id' ] = $i [ 'mid' ];
2021-07-02 07:58:53 +00:00
// $token = IConfig::get($i,'ocap','relay');
// if ($token) {
// if (defined('USE_BEARCAPS')) {
// $ret['id'] = 'bear:?u=' . $ret['id'] . '&t=' . $token;
// }
// else {
// $ret['id'] = $ret['id'] . '?token=' . $token;
// }
// }
2021-06-19 05:12:23 +00:00
2019-10-03 01:07:13 +00:00
$ret [ 'published' ] = datetime_convert ( 'UTC' , 'UTC' , $i [ 'created' ], ATOM_TIME );
if ( $i [ 'created' ] !== $i [ 'edited' ]) {
$ret [ 'updated' ] = datetime_convert ( 'UTC' , 'UTC' , $i [ 'edited' ], ATOM_TIME );
}
2019-11-09 22:00:50 +00:00
if ( $i [ 'expires' ] > NULL_DATE ) {
2019-10-03 01:07:13 +00:00
$ret [ 'expires' ] = datetime_convert ( 'UTC' , 'UTC' , $i [ 'expires' ], ATOM_TIME );
}
if ( $i [ 'app' ]) {
$ret [ 'generator' ] = [ 'type' => 'Application' , 'name' => $i [ 'app' ] ];
}
if ( $i [ 'location' ] || $i [ 'coord' ]) {
$ret [ 'location' ] = [ 'type' => 'Place' ];
if ( $i [ 'location' ]) {
$ret [ 'location' ][ 'name' ] = $i [ 'location' ];
}
if ( $i [ 'coord' ]) {
$l = explode ( ' ' , $i [ 'coord' ]);
$ret [ 'location' ][ 'latitude' ] = $l [ 0 ];
$ret [ 'location' ][ 'longitude' ] = $l [ 1 ];
}
}
if ( intval ( $i [ 'item_wall' ]) && $i [ 'mid' ] === $i [ 'parent_mid' ]) {
2021-04-14 01:12:15 +00:00
$ret [ 'commentPolicy' ] = $i [ 'comment_policy' ];
2019-10-03 01:07:13 +00:00
}
if ( intval ( $i [ 'item_private' ]) === 2 ) {
$ret [ 'directMessage' ] = true ;
}
2021-03-12 04:00:59 +00:00
if ( intval ( $i [ 'item_nocomment' ])) {
2020-06-18 04:47:04 +00:00
if ( $ret [ 'commentPolicy' ]) {
$ret [ 'commentPolicy' ] .= ' ' ;
}
$ret [ 'commentPolicy' ] .= 'until=' . datetime_convert ( 'UTC' , 'UTC' , $i [ 'created' ], ATOM_TIME );
}
elseif ( array_key_exists ( 'comments_closed' , $i ) && $i [ 'comments_closed' ] !== EMPTY_STR && $i [ 'comments_closed' ] > NULL_DATE ) {
2019-10-03 01:07:13 +00:00
if ( $ret [ 'commentPolicy' ]) {
$ret [ 'commentPolicy' ] .= ' ' ;
}
$ret [ 'commentPolicy' ] .= 'until=' . datetime_convert ( 'UTC' , 'UTC' , $i [ 'comments_closed' ], ATOM_TIME );
}
2020-06-18 04:47:04 +00:00
2021-04-06 05:36:32 +00:00
$ret [ 'attributedTo' ] = (( $i [ 'author' ][ 'xchan_network' ] === 'zot6' ) ? $i [ 'author' ][ 'xchan_url' ] : $i [ 'author' ][ 'xchan_hash' ]);
2019-10-03 01:07:13 +00:00
if ( $i [ 'mid' ] !== $i [ 'parent_mid' ]) {
$ret [ 'inReplyTo' ] = $i [ 'thr_parent' ];
2021-04-17 06:53:35 +00:00
$cnv = get_iconfig ( $i [ 'parent' ], 'activitypub' , 'context' );
if ( ! $cnv ) {
$cnv = get_iconfig ( $i [ 'parent' ], 'ostatus' , 'conversation' );
}
2019-10-03 01:07:13 +00:00
if ( ! $cnv ) {
$cnv = $ret [ 'parent_mid' ];
}
$reply = true ;
if ( $i [ 'item_private' ]) {
$d = q ( " select xchan_url, xchan_addr, xchan_name from item left join xchan on xchan_hash = author_xchan where id = %d limit 1 " ,
intval ( $i [ 'parent' ])
);
if ( $d ) {
$recips = get_iconfig ( $i [ 'parent' ], 'activitypub' , 'recips' );
2019-10-01 22:52:42 +00:00
2019-11-03 22:21:47 +00:00
if ( is_array ( $recips ) && in_array ( $i [ 'author' ][ 'xchan_url' ], $recips [ 'to' ])) {
2019-10-03 01:07:13 +00:00
$reply_url = $d [ 0 ][ 'xchan_url' ];
$is_directmessage = true ;
2019-10-01 22:52:42 +00:00
}
else {
2019-10-03 01:07:13 +00:00
$reply_url = z_root () . '/followers/' . substr ( $i [ 'author' ][ 'xchan_addr' ], 0 , strpos ( $i [ 'author' ][ 'xchan_addr' ], '@' ));
}
$reply_addr = (( $d [ 0 ][ 'xchan_addr' ]) ? $d [ 0 ][ 'xchan_addr' ] : $d [ 0 ][ 'xchan_name' ]);
}
}
}
2021-03-12 04:00:59 +00:00
if ( ! isset ( $cnv )) {
2021-04-17 06:53:35 +00:00
$cnv = get_iconfig ( $i , 'activitypub' , 'context' );
if ( ! $cnv ) {
2021-03-12 04:36:42 +00:00
$cnv = get_iconfig ( $i , 'ostatus' , 'conversation' );
}
2021-04-17 06:53:35 +00:00
if ( ! $cnv ) {
2021-03-12 04:36:42 +00:00
$cnv = $i [ 'parent_mid' ];
}
2019-10-03 01:07:13 +00:00
}
2021-04-17 06:53:35 +00:00
if ( isset ( $cnv ) && $cnv ) {
if ( strpos ( $cnv , z_root ()) === 0 ) {
$cnv = str_replace ([ '/item/' , '/activity/' ],[ '/conversation/' , '/conversation/' ], $cnv );
}
$ret [ 'context' ] = $cnv ;
2019-10-03 01:07:13 +00:00
$ret [ 'conversation' ] = $cnv ;
}
2019-10-22 05:46:53 +00:00
// provide ocap access token for private media.
// set this for descendants even if the current item is not private
// because it may have been relayed from a private item.
$token = get_iconfig ( $i , 'ocap' , 'relay' );
if ( $token && $has_images ) {
2019-10-23 02:52:00 +00:00
for ( $n = 0 ; $n < count ( $images ); $n ++ ) {
$match = $images [ $n ];
2021-02-23 23:06:59 +00:00
if ( strpos ( $match [ 1 ], '=http' ) === 0 && strpos ( $match [ 1 ], '/photo/' !== false )) {
$i [ 'body' ] = str_replace ( $match [ 1 ], $match [ 1 ] . '?token=' . $token , $i [ 'body' ]);
$images [ $n ][ 2 ] = substr ( $match [ 1 ], 1 ) . '?token=' . $token ;
}
elseif ( strpos ( $match [ 2 ], z_root () . '/photo/' ) !== false ) {
2019-10-22 05:46:53 +00:00
$i [ 'body' ] = str_replace ( $match [ 2 ], $match [ 2 ] . '?token=' . $token , $i [ 'body' ]);
2019-10-23 02:52:00 +00:00
$images [ $n ][ 2 ] = $match [ 2 ] . '?token=' . $token ;
2019-10-16 03:16:25 +00:00
}
}
}
2020-02-07 02:50:18 +00:00
if ( $i [ 'title' ]) {
$ret [ 'name' ] = $i [ 'title' ];
}
2019-10-03 01:07:13 +00:00
if ( $i [ 'mimetype' ] === 'text/bbcode' ) {
if ( $i [ 'summary' ]) {
2019-11-25 23:54:23 +00:00
$ret [ 'summary' ] = bbcode ( $i [ 'summary' ], [ $bbopts => true ]);
2019-10-03 01:07:13 +00:00
}
2020-01-07 23:47:27 +00:00
$opts = [ $bbopts => true ];
$ret [ 'content' ] = bbcode ( $i [ 'body' ], $opts );
2019-11-09 22:00:50 +00:00
$ret [ 'source' ] = [ 'content' => $i [ 'body' ], 'mediaType' => 'text/bbcode' ];
2021-03-12 04:00:59 +00:00
if ( isset ( $ret [ 'summary' ])) {
2019-11-09 22:00:50 +00:00
$ret [ 'source' ][ 'summary' ] = $i [ 'summary' ];
}
2019-10-03 01:07:13 +00:00
}
2020-02-12 23:21:39 +00:00
else {
$ret [ 'mediaType' ] = $i [ 'mimetype' ];
$ret [ 'content' ] = $i [ 'body' ];
}
2020-03-16 22:58:41 +00:00
2021-03-12 04:00:59 +00:00
if ( ! ( isset ( $ret [ 'actor' ]) || isset ( $ret [ 'attributedTo' ]))) {
2020-03-16 22:58:41 +00:00
$actor = self :: encode_person ( $i [ 'author' ], false );
if ( $actor ) {
$ret [ 'actor' ] = $actor ;
}
else {
return [];
}
2019-10-03 01:07:13 +00:00
}
2019-11-09 20:33:47 +00:00
2021-04-17 23:34:36 +00:00
$replyto = unserialise ( $i [ 'replyto' ]);
if ( $replyto ) {
$ret [ 'replyTo' ] = $replyto ;
}
2021-04-06 05:36:32 +00:00
2021-03-12 04:00:59 +00:00
if ( ! isset ( $ret [ 'url' ])) {
2021-03-19 01:53:29 +00:00
$urls = [];
if ( intval ( $i [ 'item_wall' ])) {
$locs = self :: nomadic_locations ( $i );
if ( $locs ) {
foreach ( $locs as $l ) {
if ( strpos ( $i [ 'mid' ], $l [ 'hubloc_url' ]) !== false ) {
continue ;
}
$urls [] = [
'type' => 'Link' ,
'href' => str_replace ( z_root (), $l [ 'hubloc_url' ], $ret [ 'id' ]),
'rel' => 'alternate' ,
'mediaType' => 'text/html'
];
$urls [] = [
'type' => 'Link' ,
'href' => str_replace ( z_root (), $l [ 'hubloc_url' ], $ret [ 'id' ]),
'rel' => 'alternate' ,
'mediaType' => 'application/activity+json'
];
$urls [] = [
'type' => 'Link' ,
'href' => str_replace ( z_root (), $l [ 'hubloc_url' ], $ret [ 'id' ]),
'rel' => 'alternate' ,
'mediaType' => 'application/x-zot+json'
];
}
}
}
if ( $urls ) {
$curr [] = [
'type' => 'Link' ,
'href' => $ret [ 'id' ],
'rel' => 'alternate' ,
'mediaType' => 'text/html'
];
$ret [ 'url' ] = array_merge ( $curr , $urls );
}
else {
$ret [ 'url' ] = $ret [ 'id' ];
}
2020-03-16 22:58:41 +00:00
}
2019-11-19 23:02:52 +00:00
2019-10-03 01:07:13 +00:00
$t = self :: encode_taxonomy ( $i );
if ( $t ) {
$ret [ 'tag' ] = $t ;
}
$a = self :: encode_attachment ( $i );
if ( $a ) {
$ret [ 'attachment' ] = $a ;
}
2021-05-07 23:58:34 +00:00
2019-10-03 01:07:13 +00:00
if ( $activitypub && $has_images && $ret [ 'type' ] === 'Note' ) {
foreach ( $images as $match ) {
2021-04-06 20:31:44 +00:00
$img = [];
// handle Friendica/Hubzilla style img links with [img=$url]$alttext[/img]
2021-02-25 08:35:36 +00:00
if ( strpos ( $match [ 1 ], '=http' ) === 0 ) {
2021-04-06 20:31:44 +00:00
$img [] = [ 'type' => 'Image' , 'url' => substr ( $match [ 1 ], 1 ), 'name' => $match [ 2 ] ];
}
// preferred mechanism for adding alt text
elseif ( strpos ( $match [ 1 ], 'alt=' ) !== false ) {
$txt = str_replace ( '"' , '"' , $match [ 1 ]);
$txt = substr ( $match [ 1 ], strpos ( $match [ 1 ], 'alt="' ) + 5 , - 1 );
$img [] = [ 'type' => 'Image' , 'url' => $match [ 2 ], 'name' => $txt ];
2021-02-23 23:22:30 +00:00
}
else {
$img [] = [ 'type' => 'Image' , 'url' => $match [ 2 ] ];
}
2021-02-23 23:06:59 +00:00
2021-02-23 23:22:30 +00:00
if ( ! $ret [ 'attachment' ]) {
$ret [ 'attachment' ] = [];
}
2021-04-06 20:31:44 +00:00
$already_added = false ;
if ( $img ) {
2021-05-07 23:58:34 +00:00
for ( $pc = 0 ; $pc < count ( $ret [ 'attachment' ]); $pc ++ ) {
// caution: image attachments use url and links use href, and our own links will be 'attach' links based on the image href
if (( isset ( $ret [ 'attachment' ][ $pc ][ 'href' ]) && strpos ( $img [ 0 ][ 'url' ], str_replace ( '/attach/' , '/photo/' , $ret [ 'attachment' ][ $pc ][ 'href' ])) !== false ) || ( isset ( $ret [ 'attachment' ][ $pc ][ 'url' ]) && $ret [ 'attachment' ][ $pc ][ 'url' ] === $img [ 0 ][ 'url' ])) {
// if it's already there, replace it with our alt-text aware version
$ret [ 'attachment' ][ $pc ] = $img [ 0 ];
2021-04-06 20:31:44 +00:00
$already_added = true ;
}
}
if ( ! $already_added ) {
2021-05-07 23:58:34 +00:00
// add it
2021-04-06 20:31:44 +00:00
$ret [ 'attachment' ] = array_merge ( $img , $ret [ 'attachment' ]);
}
}
2021-02-23 23:22:30 +00:00
}
}
2019-10-03 01:07:13 +00:00
// addressing madness
if ( $activitypub ) {
$public = (( $i [ 'item_private' ]) ? false : true );
$top_level = (( $i [ 'mid' ] === $i [ 'parent_mid' ]) ? true : false );
2021-06-04 04:57:08 +00:00
if ( ! $top_level ) {
if ( intval ( $i [ 'parent' ])) {
$recips = get_iconfig ( $i [ 'parent' ], 'activitypub' , 'recips' );
}
else {
// if we are encoding this item for storage there won't be a parent.
$p = q ( " select parent from item where parent_mid = '%s' and uid = %d " ,
dbesc ( $i [ 'parent_mid' ]),
intval ( $i [ 'uid' ])
);
if ( $p ) {
$recips = get_iconfig ( $p [ 0 ][ 'parent' ], 'activitypub' , 'recips' );
}
}
if ( $recips ) {
$parent_i [ 'to' ] = $recips [ 'to' ];
$parent_i [ 'cc' ] = $recips [ 'cc' ];
}
}
2019-10-03 01:07:13 +00:00
if ( $public ) {
2019-12-05 23:55:45 +00:00
$ret [ 'to' ] = [ ACTIVITY_PUBLIC_INBOX ];
2021-06-04 04:57:08 +00:00
if ( isset ( $parent_i [ 'to' ]) && is_array ( $parent_i [ 'to' ])) {
$ret [ 'to' ] = array_values ( array_unique ( array_merge ( $ret [ 'to' ], $parent_i [ 'to' ])));
}
2019-12-05 23:55:45 +00:00
$ret [ 'cc' ] = [ z_root () . '/followers/' . substr ( $i [ 'author' ][ 'xchan_addr' ], 0 , strpos ( $i [ 'author' ][ 'xchan_addr' ], '@' )) ];
2021-06-04 04:57:08 +00:00
if ( isset ( $parent_i [ 'cc' ]) && is_array ( $parent_i [ 'cc' ])) {
$ret [ 'cc' ] = array_values ( array_unique ( array_merge ( $ret [ 'cc' ], $parent_i [ 'cc' ])));
}
2019-10-03 01:07:13 +00:00
}
else {
// private activity
if ( $top_level ) {
$ret [ 'to' ] = self :: map_acl ( $i );
2021-06-04 04:57:08 +00:00
if ( isset ( $parent_i [ 'to' ]) && is_array ( $parent_i [ 'to' ])) {
$ret [ 'to' ] = array_values ( array_unique ( array_merge ( $ret [ 'to' ], $parent_i [ 'to' ])));
}
2019-10-03 01:07:13 +00:00
}
else {
2020-08-20 00:59:05 +00:00
$ret [ 'cc' ] = self :: map_acl ( $i );
2021-06-04 04:57:08 +00:00
if ( isset ( $parent_i [ 'cc' ]) && is_array ( $parent_i [ 'cc' ])) {
$ret [ 'cc' ] = array_values ( array_unique ( array_merge ( $ret [ 'cc' ], $parent_i [ 'cc' ])));
}
2019-10-03 01:07:13 +00:00
if ( $ret [ 'tag' ]) {
foreach ( $ret [ 'tag' ] as $mention ) {
2020-08-20 00:59:05 +00:00
if ( is_array ( $mention ) && array_key_exists ( 'ttype' , $mention ) && in_array ( $mention [ 'ttype' ],[ TERM_FORUM , TERM_MENTION ]) && array_key_exists ( 'href' , $mention ) && $mention [ 'href' ]) {
2020-05-15 02:48:18 +00:00
$h = q ( " select * from hubloc where hubloc_id_url = '%s' or hubloc_hash = '%s' limit 1 " ,
dbesc ( $mention [ 'href' ]),
2019-12-17 23:28:24 +00:00
dbesc ( $mention [ 'href' ])
);
if ( $h ) {
if ( $h [ 0 ][ 'hubloc_network' ] === 'activitypub' ) {
$addr = $h [ 0 ][ 'hubloc_hash' ];
}
else {
$addr = $h [ 0 ][ 'hubloc_id_url' ];
}
if ( ! in_array ( $addr , $ret [ 'to' ])) {
$ret [ 'to' ][] = $addr ;
}
}
2019-10-03 01:07:13 +00:00
}
2019-10-01 22:52:42 +00:00
}
2019-10-03 01:07:13 +00:00
}
2020-08-20 00:59:05 +00:00
$d = q ( " select hubloc.* from hubloc left join item on hubloc_hash = owner_xchan where item.parent_mid = '%s' and item.uid = %d limit 1 " ,
dbesc ( $i [ 'parent_mid' ]),
intval ( $i [ 'uid' ])
2019-10-03 01:07:13 +00:00
);
2020-08-20 00:59:05 +00:00
2019-10-03 01:07:13 +00:00
if ( $d ) {
2019-12-17 23:28:24 +00:00
if ( $d [ 0 ][ 'hubloc_network' ] === 'activitypub' ) {
$addr = $d [ 0 ][ 'hubloc_hash' ];
}
else {
$addr = $d [ 0 ][ 'hubloc_id_url' ];
}
2020-08-20 06:34:02 +00:00
$ret [ 'cc' ][] = $addr ;
2019-10-01 22:52:42 +00:00
}
2018-08-20 03:39:23 +00:00
}
}
2019-10-01 22:52:42 +00:00
2018-08-20 03:39:23 +00:00
$mentions = self :: map_mentions ( $i );
2019-05-13 06:57:57 +00:00
if ( count ( $mentions ) > 0 ) {
if ( ! $ret [ 'to' ]) {
2018-10-29 03:39:01 +00:00
$ret [ 'to' ] = $mentions ;
2018-08-20 03:39:23 +00:00
}
else {
2019-10-03 01:07:13 +00:00
$ret [ 'to' ] = array_values ( array_unique ( array_merge ( $ret [ 'to' ], $mentions )));
2018-08-20 03:39:23 +00:00
}
}
}
2020-08-20 00:59:05 +00:00
// remove any duplicates from 'cc' that are present in 'to'
// as this may indicate that mentions changed the audience from secondary to primary
$cc = [];
if ( $ret [ 'cc' ] && is_array ( $ret [ 'cc' ])) {
foreach ( $ret [ 'cc' ] as $e ) {
if ( ! is_array ( $ret [ 'to' ])) {
$cc [] = $e ;
}
elseif ( ! in_array ( $e , $ret [ 'to' ])) {
$cc [] = $e ;
}
}
}
$ret [ 'cc' ] = $cc ;
2018-05-30 04:08:52 +00:00
return $ret ;
}
2019-10-03 01:07:13 +00:00
2019-10-01 22:52:42 +00:00
// Returns an array of URLS for any mention tags found in the item array $i.
2018-05-30 04:08:52 +00:00
static function map_mentions ( $i ) {
2021-03-08 23:36:50 +00:00
if ( ! ( array_key_exists ( 'term' , $i ) && is_array ( $i [ 'term' ]))) {
2018-05-30 04:08:52 +00:00
return [];
}
$list = [];
foreach ( $i [ 'term' ] as $t ) {
2021-03-08 23:36:50 +00:00
if ( ! ( array_key_exists ( 'url' , $t ) && $t [ 'url' ])) {
2019-10-01 02:25:48 +00:00
continue ;
}
2021-03-08 23:36:50 +00:00
if ( array_key_exists ( 'ttype' , $t ) && $t [ 'ttype' ] == TERM_MENTION ) {
2018-11-08 02:24:22 +00:00
$url = self :: lookup_term_url ( $t [ 'url' ]);
$list [] = (( $url ) ? $url : $t [ 'url' ]);
2018-05-30 04:08:52 +00:00
}
}
return $list ;
}
2019-10-03 01:07:13 +00:00
// Returns an array of all recipients targeted by private item array $i.
2019-10-01 22:52:42 +00:00
2019-10-03 01:07:13 +00:00
static function map_acl ( $i ) {
$ret = [];
2018-08-20 03:39:23 +00:00
2019-10-03 01:07:13 +00:00
if ( ! $i [ 'item_private' ]) {
return $ret ;
}
2018-05-30 04:08:52 +00:00
2020-08-20 00:59:05 +00:00
if ( $i [ 'mid' ] !== $i [ 'parent_mid' ]) {
$i = q ( " select * from item where parent_mid = '%s' and uid = %d " ,
dbesc ( $i [ 'parent_mid' ]),
intval ( $i [ 'uid' ])
);
if ( $i ) {
$i = array_shift ( $i );
}
}
2019-10-03 01:07:13 +00:00
if ( $i [ 'allow_gid' ]) {
$tmp = expand_acl ( $i [ 'allow_gid' ]);
if ( $tmp ) {
foreach ( $tmp as $t ) {
$ret [] = z_root () . '/lists/' . $t ;
}
}
}
2018-05-30 04:08:52 +00:00
2019-10-03 01:07:13 +00:00
if ( $i [ 'allow_cid' ]) {
$tmp = expand_acl ( $i [ 'allow_cid' ]);
$list = stringify_array ( $tmp , true );
if ( $list ) {
2020-08-20 00:59:05 +00:00
$details = q ( " select hubloc_id_url, hubloc_hash, hubloc_network from hubloc where hubloc_hash in ( " . $list . " ) " );
2019-10-03 01:07:13 +00:00
if ( $details ) {
foreach ( $details as $d ) {
2020-08-20 00:59:05 +00:00
if ( $d [ 'hubloc_network' ] === 'activitypub' ) {
$ret [] = $d [ 'hubloc_hash' ];
}
else {
2020-08-20 06:34:02 +00:00
$ret [] = $d [ 'hubloc_id_url' ];
2020-08-20 00:59:05 +00:00
}
2018-05-30 04:08:52 +00:00
}
}
}
}
2020-08-20 00:59:05 +00:00
$x = get_iconfig ( $i [ 'id' ], 'activitypub' , 'recips' );
if ( $x ) {
foreach ([ 'to' , 'cc' ] as $k ) {
if ( isset ( $x [ $k ])) {
if ( is_string ( $x [ $k ])) {
$ret [] = $x [ $k ];
}
else {
$ret = array_merge ( $ret , $x [ $k ]);
}
}
}
}
return array_values ( array_unique ( $ret ));
2018-05-30 04:08:52 +00:00
}
2018-08-20 03:39:23 +00:00
static function encode_person ( $p , $extended = true , $activitypub = false ) {
2018-06-26 03:55:53 +00:00
2018-10-05 01:40:57 +00:00
$ret = [];
2019-05-13 06:57:57 +00:00
if ( ! $p [ 'xchan_url' ])
2018-10-05 01:40:57 +00:00
return $ret ;
2018-05-30 04:08:52 +00:00
2019-05-13 06:57:57 +00:00
if ( ! $extended ) {
2018-06-26 03:55:53 +00:00
return $p [ 'xchan_url' ];
}
2018-10-05 01:40:57 +00:00
$c = (( array_key_exists ( 'channel_id' , $p )) ? $p : channelx_by_hash ( $p [ 'xchan_hash' ]));
2018-05-30 04:08:52 +00:00
$ret [ 'type' ] = 'Person' ;
2018-10-05 01:40:57 +00:00
2019-05-13 06:57:57 +00:00
if ( $c ) {
2018-12-12 23:39:17 +00:00
$role = PConfig :: Get ( $c [ 'channel_id' ], 'system' , 'permissions_role' );
2019-05-13 06:57:57 +00:00
if ( strpos ( $role , 'forum' ) !== false ) {
2018-10-05 01:40:57 +00:00
$ret [ 'type' ] = 'Group' ;
}
}
2019-08-15 04:21:40 +00:00
if ( $c ) {
$ret [ 'id' ] = channel_url ( $c );
}
else {
$ret [ 'id' ] = (( strpos ( $p [ 'xchan_hash' ], 'http' ) === 0 ) ? $p [ 'xchan_hash' ] : $p [ 'xchan_url' ]);
}
2019-05-13 06:57:57 +00:00
if ( $p [ 'xchan_addr' ] && strpos ( $p [ 'xchan_addr' ], '@' ))
2018-05-30 04:08:52 +00:00
$ret [ 'preferredUsername' ] = substr ( $p [ 'xchan_addr' ], 0 , strpos ( $p [ 'xchan_addr' ], '@' ));
$ret [ 'name' ] = $p [ 'xchan_name' ];
2018-08-14 00:27:46 +00:00
$ret [ 'updated' ] = datetime_convert ( 'UTC' , 'UTC' , $p [ 'xchan_name_date' ], ATOM_TIME );
2018-05-30 04:08:52 +00:00
$ret [ 'icon' ] = [
'type' => 'Image' ,
'mediaType' => (( $p [ 'xchan_photo_mimetype' ]) ? $p [ 'xchan_photo_mimetype' ] : 'image/png' ),
2018-08-14 00:27:46 +00:00
'updated' => datetime_convert ( 'UTC' , 'UTC' , $p [ 'xchan_photo_date' ], ATOM_TIME ),
2018-05-30 04:08:52 +00:00
'url' => $p [ 'xchan_photo_l' ],
'height' => 300 ,
'width' => 300 ,
2018-06-25 01:54:29 +00:00
];
2019-08-21 02:25:36 +00:00
$ret [ 'url' ] = $p [ 'xchan_url' ];
2021-07-11 23:46:03 +00:00
if ( isset ( $p [ 'channel_location' ]) && $p [ 'channel_location' ]) {
2021-02-20 20:58:42 +00:00
$ret [ 'location' ] = [ 'type' => 'Place' , 'name' => $p [ 'channel_location' ] ];
}
2018-08-29 09:19:32 +00:00
2020-11-27 00:17:07 +00:00
if ( $activitypub && get_config ( 'system' , 'activitypub' , ACTIVITYPUB_ENABLED )) {
2018-08-29 09:19:32 +00:00
2019-05-13 06:57:57 +00:00
if ( $c ) {
2020-11-27 00:17:07 +00:00
if ( get_pconfig ( $c [ 'channel_id' ], 'system' , 'activitypub' , ACTIVITYPUB_ENABLED )) {
2019-09-23 05:48:14 +00:00
$ret [ 'inbox' ] = z_root () . '/inbox/' . $c [ 'channel_address' ];
}
else {
$ret [ 'inbox' ] = null ;
}
2018-08-20 03:39:23 +00:00
$ret [ 'outbox' ] = z_root () . '/outbox/' . $c [ 'channel_address' ];
$ret [ 'followers' ] = z_root () . '/followers/' . $c [ 'channel_address' ];
$ret [ 'following' ] = z_root () . '/following/' . $c [ 'channel_address' ];
2019-10-02 00:44:58 +00:00
2021-03-30 01:47:39 +00:00
$ret [ 'endpoints' ] = [
2021-04-16 22:44:20 +00:00
'sharedInbox' => z_root () . '/inbox' ,
'oauthRegistrationEndpoint' => z_root () . '/api/client/register' ,
2021-03-30 01:47:39 +00:00
'oauthAuthorizationEndpoint' => z_root () . '/authorize' ,
2021-04-16 22:44:20 +00:00
'oauthTokenEndpoint' => z_root () . '/token'
2021-03-30 01:47:39 +00:00
];
2020-08-12 03:45:09 +00:00
$ret [ 'discoverable' ] = (( 1 - intval ( $p [ 'xchan_hidden' ])) ? true : false );
2018-08-20 03:39:23 +00:00
$ret [ 'publicKey' ] = [
2019-07-19 03:38:36 +00:00
'id' => $p [ 'xchan_url' ],
2018-08-20 03:39:23 +00:00
'owner' => $p [ 'xchan_url' ],
'publicKeyPem' => $p [ 'xchan_pubkey' ]
];
2018-12-04 05:45:35 +00:00
2019-09-21 22:26:55 +00:00
// map other nomadic identities linked with this channel
$locations = [];
$locs = Libzot :: encode_locations ( $c );
if ( $locs ) {
foreach ( $locs as $loc ) {
if ( $loc [ 'url' ] !== z_root ()) {
$locations [] = $loc [ 'id_url' ];
}
}
}
if ( $locations ) {
2019-09-27 00:05:30 +00:00
if ( count ( $locations ) === 1 ) {
$locations = array_shift ( $locations );
}
$ret [ 'copiedTo' ] = $locations ;
$ret [ 'alsoKnownAs' ] = $locations ;
2019-09-21 22:26:55 +00:00
}
2018-12-04 05:45:35 +00:00
$cp = get_cover_photo ( $c [ 'channel_id' ], 'array' );
2019-05-13 06:57:57 +00:00
if ( $cp ) {
2018-12-04 05:45:35 +00:00
$ret [ 'image' ] = [
'type' => 'Image' ,
'mediaType' => $cp [ 'type' ],
'url' => $cp [ 'url' ]
];
}
$dp = q ( " select about from profile where uid = %d and is_default = 1 " ,
intval ( $c [ 'channel_id' ])
);
2019-05-13 06:57:57 +00:00
if ( $dp && $dp [ 0 ][ 'about' ]) {
2018-12-04 05:45:35 +00:00
$ret [ 'summary' ] = bbcode ( $dp [ 0 ][ 'about' ],[ 'export' => true ]);
}
2018-08-20 03:39:23 +00:00
}
else {
2018-11-22 03:04:42 +00:00
$collections = get_xconfig ( $p [ 'xchan_hash' ], 'activitypub' , 'collections' ,[]);
2019-05-13 06:57:57 +00:00
if ( $collections ) {
2018-08-20 03:39:23 +00:00
$ret = array_merge ( $ret , $collections );
}
else {
$ret [ 'inbox' ] = null ;
$ret [ 'outbox' ] = null ;
}
}
}
2018-08-29 09:19:32 +00:00
else {
$ret [ 'publicKey' ] = [
2019-08-26 00:35:14 +00:00
'id' => $p [ 'xchan_url' ],
2018-08-29 09:19:32 +00:00
'owner' => $p [ 'xchan_url' ],
'publicKeyPem' => $p [ 'xchan_pubkey' ]
];
}
2018-08-20 03:39:23 +00:00
$arr = [ 'xchan' => $p , 'encoded' => $ret , 'activitypub' => $activitypub ];
2018-08-06 01:41:33 +00:00
call_hooks ( 'encode_person' , $arr );
$ret = $arr [ 'encoded' ];
2018-05-30 04:08:52 +00:00
return $ret ;
}
static function activity_mapper ( $verb ) {
2019-05-13 06:57:57 +00:00
if ( strpos ( $verb , '/' ) === false ) {
2018-05-30 04:08:52 +00:00
return $verb ;
}
$acts = [
'http://activitystrea.ms/schema/1.0/post' => 'Create' ,
'http://activitystrea.ms/schema/1.0/share' => 'Announce' ,
'http://activitystrea.ms/schema/1.0/update' => 'Update' ,
'http://activitystrea.ms/schema/1.0/like' => 'Like' ,
'http://activitystrea.ms/schema/1.0/favorite' => 'Like' ,
'http://purl.org/zot/activity/dislike' => 'Dislike' ,
'http://activitystrea.ms/schema/1.0/tag' => 'Add' ,
'http://activitystrea.ms/schema/1.0/follow' => 'Follow' ,
2021-05-17 21:14:57 +00:00
'http://activitystrea.ms/schema/1.0/unfollow' => 'Ignore' ,
2018-05-30 04:08:52 +00:00
];
2019-09-22 16:16:07 +00:00
call_hooks ( 'activity_mapper' , $acts );
2018-05-30 04:08:52 +00:00
2019-05-13 06:57:57 +00:00
if ( array_key_exists ( $verb , $acts ) && $acts [ $verb ]) {
2018-05-30 04:08:52 +00:00
return $acts [ $verb ];
}
// Reactions will just map to normal activities
2019-05-13 06:57:57 +00:00
if ( strpos ( $verb , ACTIVITY_REACT ) !== false )
2018-05-30 04:08:52 +00:00
return 'Create' ;
2019-05-13 06:57:57 +00:00
if ( strpos ( $verb , ACTIVITY_MOOD ) !== false )
2018-05-30 04:08:52 +00:00
return 'Create' ;
2019-05-13 06:57:57 +00:00
if ( strpos ( $verb , ACTIVITY_POKE ) !== false )
2018-05-30 04:08:52 +00:00
return 'Activity' ;
2020-06-09 05:42:58 +00:00
// We should return false, however this will trigger an uncaught exception and crash
2018-05-30 04:08:52 +00:00
// the delivery system if encountered by the JSON-LDSignature library
logger ( 'Unmapped activity: ' . $verb );
return 'Create' ;
2019-05-15 05:16:49 +00:00
// return false;
}
2018-05-30 04:08:52 +00:00
static function activity_obj_mapper ( $obj ) {
$objs = [
'http://activitystrea.ms/schema/1.0/note' => 'Note' ,
'http://activitystrea.ms/schema/1.0/comment' => 'Note' ,
'http://activitystrea.ms/schema/1.0/person' => 'Person' ,
'http://purl.org/zot/activity/profile' => 'Profile' ,
'http://activitystrea.ms/schema/1.0/photo' => 'Image' ,
'http://activitystrea.ms/schema/1.0/profile-photo' => 'Icon' ,
'http://activitystrea.ms/schema/1.0/event' => 'Event' ,
'http://activitystrea.ms/schema/1.0/wiki' => 'Document' ,
'http://purl.org/zot/activity/location' => 'Place' ,
'http://purl.org/zot/activity/chessgame' => 'Game' ,
'http://purl.org/zot/activity/tagterm' => 'zot:Tag' ,
'http://purl.org/zot/activity/thing' => 'Object' ,
'http://purl.org/zot/activity/file' => 'zot:File' ,
'http://purl.org/zot/activity/mood' => 'zot:Mood' ,
];
2019-09-22 16:16:07 +00:00
call_hooks ( 'activity_obj_mapper' , $objs );
2020-01-21 21:07:41 +00:00
if ( $obj === 'Answer' ) {
return 'Note' ;
}
if ( strpos ( $obj , '/' ) === false ) {
return $obj ;
}
2019-05-13 06:57:57 +00:00
if ( array_key_exists ( $obj , $objs )) {
2018-05-30 04:08:52 +00:00
return $objs [ $obj ];
}
logger ( 'Unmapped activity object: ' . $obj );
return 'Note' ;
// return false;
}
static function follow ( $channel , $act ) {
$contact = null ;
$their_follow_id = null ;
2020-08-12 01:03:57 +00:00
if ( intval ( $channel [ 'channel_system' ])) {
// The system channel ignores all follow requests
return ;
}
2018-05-30 04:08:52 +00:00
/*
*
* if $act -> type === 'Follow' , actor is now following $channel
* if $act -> type === 'Accept' , actor has approved a follow request from $channel
*
*/
$person_obj = $act -> actor ;
2020-11-16 22:51:03 +00:00
if ( in_array ( $act -> type , [ 'Follow' , 'Invite' , 'Join' ])) {
2018-05-30 04:08:52 +00:00
$their_follow_id = $act -> id ;
}
2019-05-13 06:57:57 +00:00
elseif ( $act -> type === 'Accept' ) {
2018-05-30 04:08:52 +00:00
$my_follow_id = z_root () . '/follow/' . $contact [ 'id' ];
}
2019-05-13 06:57:57 +00:00
if ( is_array ( $person_obj )) {
2018-05-30 04:08:52 +00:00
// store their xchan and hubloc
self :: actor_store ( $person_obj [ 'id' ], $person_obj );
// Find any existing abook record
$r = q ( " select * from abook left join xchan on abook_xchan = xchan_hash where abook_xchan = '%s' and abook_channel = %d limit 1 " ,
dbesc ( $person_obj [ 'id' ]),
intval ( $channel [ 'channel_id' ])
);
2019-05-13 06:57:57 +00:00
if ( $r ) {
2018-05-30 04:08:52 +00:00
$contact = $r [ 0 ];
}
}
2018-09-07 00:36:35 +00:00
$x = PermissionRoles :: role_perms ( 'social' );
$p = Permissions :: FilledPerms ( $x [ 'perms_connect' ]);
2018-10-25 05:43:00 +00:00
// add tag_deliver permissions to remote groups
2019-05-13 06:57:57 +00:00
if ( is_array ( $person_obj ) && $person_obj [ 'type' ] === 'Group' ) {
2018-10-25 05:43:00 +00:00
$p [ 'tag_deliver' ] = 1 ;
}
2018-09-07 00:36:35 +00:00
$their_perms = Permissions :: serialise ( $p );
2018-05-30 04:08:52 +00:00
2018-10-25 05:43:00 +00:00
2019-05-13 06:57:57 +00:00
if ( $contact && $contact [ 'abook_id' ]) {
2018-05-30 04:08:52 +00:00
// A relationship of some form already exists on this site.
switch ( $act -> type ) {
case 'Follow' :
2020-11-16 22:51:03 +00:00
case 'Invite' :
case 'Join' :
2018-05-30 04:08:52 +00:00
// A second Follow request, but we haven't approved the first one
2019-05-13 06:57:57 +00:00
if ( $contact [ 'abook_pending' ]) {
2018-05-30 04:08:52 +00:00
return ;
}
// We've already approved them or followed them first
// Send an Accept back to them
2018-09-20 06:53:26 +00:00
set_abconfig ( $channel [ 'channel_id' ], $person_obj [ 'id' ], 'activitypub' , 'their_follow_id' , $their_follow_id );
2021-04-30 00:12:51 +00:00
set_abconfig ( $channel [ 'channel_id' ], $person_obj [ 'id' ], 'activitypub' , 'their_follow_type' , $act -> type );
2020-06-15 07:29:56 +00:00
Run :: Summon ([ 'Notifier' , 'permissions_accept' , $contact [ 'abook_id' ] ]);
2018-05-30 04:08:52 +00:00
return ;
case 'Accept' :
// They accepted our Follow request - set default permissions
2018-06-08 02:44:09 +00:00
set_abconfig ( $channel [ 'channel_id' ], $contact [ 'abook_xchan' ], 'system' , 'their_perms' , $their_perms );
2018-05-30 04:08:52 +00:00
$abook_instance = $contact [ 'abook_instance' ];
2019-05-13 06:57:57 +00:00
if ( strpos ( $abook_instance , z_root ()) === false ) {
if ( $abook_instance )
2018-05-30 04:08:52 +00:00
$abook_instance .= ',' ;
$abook_instance .= z_root ();
$r = q ( " update abook set abook_instance = '%s', abook_not_here = 0
where abook_id = % d and abook_channel = % d " ,
dbesc ( $abook_instance ),
intval ( $contact [ 'abook_id' ]),
intval ( $channel [ 'channel_id' ])
);
}
return ;
default :
return ;
}
}
// No previous relationship exists.
2019-05-13 06:57:57 +00:00
if ( $act -> type === 'Accept' ) {
2018-05-30 04:08:52 +00:00
// This should not happen unless we deleted the connection before it was accepted.
return ;
}
// From here on out we assume a Follow activity to somebody we have no existing relationship with
2018-09-20 06:53:26 +00:00
set_abconfig ( $channel [ 'channel_id' ], $person_obj [ 'id' ], 'activitypub' , 'their_follow_id' , $their_follow_id );
2021-04-30 00:12:51 +00:00
set_abconfig ( $channel [ 'channel_id' ], $person_obj [ 'id' ], 'activitypub' , 'their_follow_type' , $act -> type );
2018-05-30 04:08:52 +00:00
// The xchan should have been created by actor_store() above
$r = q ( " select * from xchan where xchan_hash = '%s' and xchan_network = 'activitypub' limit 1 " ,
dbesc ( $person_obj [ 'id' ])
);
2019-05-13 06:57:57 +00:00
if ( ! $r ) {
2018-05-30 04:08:52 +00:00
logger ( 'xchan not found for ' . $person_obj [ 'id' ]);
return ;
}
$ret = $r [ 0 ];
2020-04-17 11:08:04 +00:00
$blocked = LibBlock :: fetch ( $channel [ 'channel_id' ], BLOCKTYPE_SERVER );
if ( $blocked ) {
foreach ( $blocked as $b ) {
if ( strpos ( $ret [ 'xchan_url' ], $b [ 'block_entity' ]) !== false ) {
logger ( 'siteblock - follower denied' );
return ;
}
}
}
if ( LibBlock :: fetch_by_entity ( $channel [ 'channel_id' ], $ret [ 'xchan_hash' ])) {
logger ( 'actorblock - follower denied' );
return ;
}
2018-09-07 00:36:35 +00:00
$p = Permissions :: connect_perms ( $channel [ 'channel_id' ]);
$my_perms = Permissions :: serialise ( $p [ 'perms' ]);
2018-05-30 04:08:52 +00:00
$automatic = $p [ 'automatic' ];
2018-12-12 23:39:17 +00:00
$closeness = PConfig :: Get ( $channel [ 'channel_id' ], 'system' , 'new_abook_closeness' , 80 );
2018-05-30 04:08:52 +00:00
$r = abook_store_lowlevel (
[
'abook_account' => intval ( $channel [ 'channel_account_id' ]),
'abook_channel' => intval ( $channel [ 'channel_id' ]),
'abook_xchan' => $ret [ 'xchan_hash' ],
'abook_closeness' => intval ( $closeness ),
'abook_created' => datetime_convert (),
'abook_updated' => datetime_convert (),
'abook_connected' => datetime_convert (),
'abook_dob' => NULL_DATE ,
'abook_pending' => intval (( $automatic ) ? 0 : 1 ),
'abook_instance' => z_root ()
]
);
2019-05-13 06:57:57 +00:00
if ( $my_perms )
2018-12-13 13:57:00 +00:00
AbConfig :: Set ( $channel [ 'channel_id' ], $ret [ 'xchan_hash' ], 'system' , 'my_perms' , $my_perms );
2018-05-30 04:08:52 +00:00
2019-05-13 06:57:57 +00:00
if ( $their_perms )
2018-12-12 23:39:17 +00:00
AbConfig :: Set ( $channel [ 'channel_id' ], $ret [ 'xchan_hash' ], 'system' , 'their_perms' , $their_perms );
2018-05-30 04:08:52 +00:00
2019-05-13 06:57:57 +00:00
if ( $r ) {
2018-05-30 04:08:52 +00:00
logger ( " New ActivityPub follower for { $channel [ 'channel_name' ] } " );
$new_connection = q ( " select * from abook left join xchan on abook_xchan = xchan_hash left join hubloc on hubloc_hash = xchan_hash where abook_channel = %d and abook_xchan = '%s' order by abook_created desc limit 1 " ,
intval ( $channel [ 'channel_id' ]),
dbesc ( $ret [ 'xchan_hash' ])
);
2019-05-13 06:57:57 +00:00
if ( $new_connection ) {
2018-09-07 00:36:35 +00:00
Enotify :: submit (
2018-05-30 04:08:52 +00:00
[
'type' => NOTIFY_INTRO ,
'from_xchan' => $ret [ 'xchan_hash' ],
'to_xchan' => $channel [ 'channel_hash' ],
'link' => z_root () . '/connedit/' . $new_connection [ 0 ][ 'abook_id' ],
]
);
2019-05-13 06:57:57 +00:00
if ( $my_perms && $automatic ) {
2018-05-30 04:08:52 +00:00
// send an Accept for this Follow activity
2020-06-15 07:29:56 +00:00
Run :: Summon ([ 'Notifier' , 'permissions_accept' , $new_connection [ 0 ][ 'abook_id' ] ]);
2018-05-30 04:08:52 +00:00
// Send back a Follow notification to them
2020-06-15 07:29:56 +00:00
Run :: Summon ([ 'Notifier' , 'permissions_create' , $new_connection [ 0 ][ 'abook_id' ] ]);
2018-05-30 04:08:52 +00:00
}
2021-03-15 05:10:44 +00:00
$clone = [];
2019-05-13 06:57:57 +00:00
foreach ( $new_connection [ 0 ] as $k => $v ) {
if ( strpos ( $k , 'abook_' ) === 0 ) {
2018-05-30 04:08:52 +00:00
$clone [ $k ] = $v ;
}
}
unset ( $clone [ 'abook_id' ]);
unset ( $clone [ 'abook_account' ]);
unset ( $clone [ 'abook_channel' ]);
$abconfig = load_abconfig ( $channel [ 'channel_id' ], $clone [ 'abook_xchan' ]);
2021-03-15 05:10:44 +00:00
if ( $abconfig ) {
2018-05-30 04:08:52 +00:00
$clone [ 'abconfig' ] = $abconfig ;
2021-03-15 05:10:44 +00:00
}
2021-04-13 01:19:27 +00:00
Libsync :: build_sync_packet ( $channel [ 'channel_id' ], [ 'abook' => [ $clone ] ] );
2018-05-30 04:08:52 +00:00
}
}
/* If there is a default group for this channel and permissions are automatic, add this member to it */
2019-05-13 06:57:57 +00:00
if ( $channel [ 'channel_default_group' ] && $automatic ) {
2019-02-14 23:26:37 +00:00
$g = AccessList :: rec_byhash ( $channel [ 'channel_id' ], $channel [ 'channel_default_group' ]);
2021-03-15 05:10:44 +00:00
if ( $g ) {
2019-02-14 23:26:37 +00:00
AccessList :: member_add ( $channel [ 'channel_id' ], '' , $ret [ 'xchan_hash' ], $g [ 'id' ]);
2021-03-15 05:10:44 +00:00
}
2018-05-30 04:08:52 +00:00
}
return ;
}
static function unfollow ( $channel , $act ) {
$contact = null ;
/* actor is unfollowing $channel */
$person_obj = $act -> actor ;
2019-05-13 06:57:57 +00:00
if ( is_array ( $person_obj )) {
2018-05-30 04:08:52 +00:00
$r = q ( " select * from abook left join xchan on abook_xchan = xchan_hash where abook_xchan = '%s' and abook_channel = %d limit 1 " ,
dbesc ( $person_obj [ 'id' ]),
intval ( $channel [ 'channel_id' ])
);
2019-05-13 06:57:57 +00:00
if ( $r ) {
2018-05-30 04:08:52 +00:00
// remove all permissions they provided
2018-06-08 02:44:09 +00:00
del_abconfig ( $channel [ 'channel_id' ], $r [ 0 ][ 'xchan_hash' ], 'system' , 'their_perms' , EMPTY_STR );
2018-05-30 04:08:52 +00:00
}
}
return ;
}
2019-11-14 22:45:55 +00:00
static function actor_store ( $url , $person_obj , $force = false ) {
2018-05-30 04:08:52 +00:00
2019-10-14 22:55:58 +00:00
if ( ! is_array ( $person_obj )) {
2018-05-30 04:08:52 +00:00
return ;
2019-10-14 22:55:58 +00:00
}
2018-05-30 04:08:52 +00:00
2018-10-09 04:27:35 +00:00
// logger('person_obj: ' . print_r($person_obj,true));
2018-09-19 23:25:05 +00:00
// We may have been passed a cached entry. If it is, and the cache duration has expired
// fetch a fresh copy before continuing.
2019-05-13 06:57:57 +00:00
if ( array_key_exists ( 'cached' , $person_obj )) {
2019-11-14 22:45:55 +00:00
if ( array_key_exists ( 'updated' , $person_obj ) && ( datetime_convert ( 'UTC' , 'UTC' , $person_obj [ 'updated' ]) < datetime_convert ( 'UTC' , 'UTC' , 'now - ' . self :: $ACTOR_CACHE_DAYS . ' days' ) || $force )) {
2019-01-14 05:22:45 +00:00
$person_obj = self :: fetch ( $url );
2018-11-08 23:00:33 +00:00
}
else {
return ;
}
2018-09-19 23:25:05 +00:00
}
2019-10-18 05:01:53 +00:00
if ( is_array ( $person_obj ) && array_key_exists ( 'movedTo' , $person_obj ) && $person_obj [ 'movedTo' ] && ! is_array ( $person_obj [ 'movedTo' ])) {
2019-10-14 22:55:58 +00:00
$tgt = self :: fetch ( $person_obj [ 'movedTo' ]);
self :: actor_store ( $person_obj [ 'movedTo' ], $tgt );
ActivityPub :: move ( $person_obj [ 'id' ], $tgt );
return ;
}
2021-03-01 00:14:40 +00:00
2018-11-09 01:02:04 +00:00
$url = $person_obj [ 'id' ];
2018-09-19 23:25:05 +00:00
2019-05-13 06:57:57 +00:00
if ( ! $url ) {
2018-11-14 01:23:09 +00:00
return ;
}
2020-10-26 01:15:51 +00:00
$name = escape_tags ( $person_obj [ 'name' ]);
2019-05-13 06:57:57 +00:00
if ( ! $name )
2020-10-26 01:15:51 +00:00
$name = escape_tags ( $person_obj [ 'preferredUsername' ]);
2019-05-13 06:57:57 +00:00
if ( ! $name )
2020-10-26 01:15:51 +00:00
$name = escape_tags ( t ( 'Unknown' ));
2018-05-30 04:08:52 +00:00
2020-10-26 01:15:51 +00:00
$username = escape_tags ( $person_obj [ 'preferredUsername' ]);
2018-09-17 04:41:24 +00:00
$h = parse_url ( $url );
2019-05-13 06:57:57 +00:00
if ( $h && $h [ 'host' ]) {
2018-09-17 04:41:24 +00:00
$username .= '@' . $h [ 'host' ];
}
2019-05-13 06:57:57 +00:00
if ( $person_obj [ 'icon' ]) {
if ( is_array ( $person_obj [ 'icon' ])) {
if ( array_key_exists ( 'url' , $person_obj [ 'icon' ]))
2018-05-30 04:08:52 +00:00
$icon = $person_obj [ 'icon' ][ 'url' ];
2020-10-19 02:59:04 +00:00
else {
if ( is_string ( $person_obj [ 'icon' ][ 0 ])) {
$icon = $person_obj [ 'icon' ][ 0 ];
}
elseif ( array_key_exists ( 'url' , $person_obj [ 'icon' ][ 0 ])) {
$icon = $person_obj [ 'icon' ][ 0 ][ 'url' ];
}
}
2018-05-30 04:08:52 +00:00
}
2021-03-12 22:37:18 +00:00
else {
2018-05-30 04:08:52 +00:00
$icon = $person_obj [ 'icon' ];
2021-03-12 22:26:56 +00:00
}
2018-05-30 04:08:52 +00:00
}
2021-03-12 22:26:56 +00:00
if ( ! ( isset ( $icon ) && $icon )) {
2018-09-05 02:06:19 +00:00
$icon = z_root () . '/' . get_default_profile_photo ();
}
2018-05-30 04:08:52 +00:00
2021-07-11 21:41:51 +00:00
$cover_photo = false ;
if ( isset ( $person_obj [ 'image' ])) {
if ( is_string ( $person_obj [ 'image' ])) {
$cover_photo = $person_obj [ 'image' ];
}
if ( isset ( $person_obj [ 'image' ][ 'url' ])) {
$cover_photo = $person_obj [ 'image' ][ 'url' ];
}
}
2020-08-12 01:03:57 +00:00
$hidden = false ;
if ( array_key_exists ( 'discoverable' , $person_obj ) && ( ! intval ( $person_obj [ 'discoverable' ]))) {
$hidden = true ;
}
2018-05-30 04:08:52 +00:00
2018-09-24 00:03:10 +00:00
$links = false ;
$profile = false ;
2019-05-13 06:57:57 +00:00
if ( is_array ( $person_obj [ 'url' ])) {
if ( ! array_key_exists ( 0 , $person_obj [ 'url' ])) {
2018-09-24 00:03:10 +00:00
$links = [ $person_obj [ 'url' ] ];
}
else {
$links = $person_obj [ 'url' ];
}
}
2021-05-17 21:14:57 +00:00
if ( is_array ( $links ) && $links ) {
2019-05-13 06:57:57 +00:00
foreach ( $links as $link ) {
2021-05-17 21:14:57 +00:00
if ( is_array ( $link ) && array_key_exists ( 'mediaType' , $link ) && $link [ 'mediaType' ] === 'text/html' ) {
2018-09-24 00:03:10 +00:00
$profile = $link [ 'href' ];
}
}
2019-05-13 06:57:57 +00:00
if ( ! $profile ) {
2018-09-24 00:03:10 +00:00
$profile = $links [ 0 ][ 'href' ];
}
}
2019-05-13 06:57:57 +00:00
elseif ( isset ( $person_obj [ 'url' ]) && is_string ( $person_obj [ 'url' ])) {
2018-09-24 00:03:10 +00:00
$profile = $person_obj [ 'url' ];
}
2019-05-13 06:57:57 +00:00
if ( ! $profile ) {
2018-09-24 00:03:10 +00:00
$profile = $url ;
}
2018-05-30 04:08:52 +00:00
2021-03-08 23:36:50 +00:00
$inbox = (( array_key_exists ( 'inbox' , $person_obj )) ? $person_obj [ 'inbox' ] : null );
2018-05-30 04:08:52 +00:00
2018-11-08 23:00:33 +00:00
// either an invalid identity or a cached entry of some kind which didn't get caught above
2019-05-13 06:57:57 +00:00
if (( ! $inbox ) || strpos ( $inbox , z_root ()) !== false ) {
2018-11-08 23:00:33 +00:00
return ;
}
2018-05-30 04:08:52 +00:00
$collections = [];
2019-05-13 06:57:57 +00:00
if ( $inbox ) {
2018-05-30 04:08:52 +00:00
$collections [ 'inbox' ] = $inbox ;
2021-03-08 23:36:50 +00:00
if ( array_key_exists ( 'outbox' , $person_obj ) && is_string ( $person_obj [ 'outbox' ])) {
2018-05-30 04:08:52 +00:00
$collections [ 'outbox' ] = $person_obj [ 'outbox' ];
2021-03-08 23:36:50 +00:00
}
if ( array_key_exists ( 'followers' , $person_obj ) && is_string ( $person_obj [ 'followers' ])) {
2018-05-30 04:08:52 +00:00
$collections [ 'followers' ] = $person_obj [ 'followers' ];
2021-03-08 23:36:50 +00:00
}
if ( array_key_exists ( 'following' , $person_obj ) && is_string ( $person_obj [ 'following' ])) {
2018-05-30 04:08:52 +00:00
$collections [ 'following' ] = $person_obj [ 'following' ];
2021-03-08 23:36:50 +00:00
}
2021-04-30 04:24:30 +00:00
if ( array_key_exists ( 'wall' , $person_obj ) && is_string ( $person_obj [ 'wall' ])) {
$collections [ 'wall' ] = $person_obj [ 'wall' ];
}
2021-03-08 23:36:50 +00:00
if ( array_path_exists ( 'endpoints/sharedInbox' , $person_obj ) && is_string ( $person_obj [ 'endpoints' ][ 'sharedInbox' ])) {
2018-05-30 04:08:52 +00:00
$collections [ 'sharedInbox' ] = $person_obj [ 'endpoints' ][ 'sharedInbox' ];
2021-03-08 23:36:50 +00:00
}
2018-05-30 04:08:52 +00:00
}
2019-05-13 06:57:57 +00:00
if ( isset ( $person_obj [ 'publicKey' ][ 'publicKeyPem' ])) {
if ( $person_obj [ 'id' ] === $person_obj [ 'publicKey' ][ 'owner' ]) {
2018-05-30 04:08:52 +00:00
$pubkey = $person_obj [ 'publicKey' ][ 'publicKeyPem' ];
2019-05-13 06:57:57 +00:00
if ( strstr ( $pubkey , 'RSA ' )) {
2018-12-14 20:46:03 +00:00
$pubkey = Keyutils :: rsatopem ( $pubkey );
2018-05-30 04:08:52 +00:00
}
}
}
2020-08-13 02:06:13 +00:00
$keywords = [];
if ( is_array ( $person_obj [ 'tag' ])) {
foreach ( $person_obj [ 'tag' ] as $t ) {
if ( is_array ( $t ) && isset ( $t [ 'type' ]) && $t [ 'type' ] === 'Hashtag' ) {
if ( isset ( $t [ 'name' ])) {
$tag = escape_tags (( substr ( $t [ 'name' ], 0 , 1 ) === '#' ) ? substr ( $t [ 'name' ], 1 ) : $t [ 'name' ]);
if ( $tag ) {
$keywords [] = $tag ;
}
}
}
}
}
2020-11-16 22:51:03 +00:00
$xchan_type = (( $person_obj [ 'type' ] === 'Group' ) ? 1 : 0 );
2020-09-01 02:24:21 +00:00
$about = (( isset ( $person_obj [ 'summary' ])) ? html2bbcode ( purify_html ( $person_obj [ 'summary' ])) : EMPTY_STR );
$p = q ( " select * from xchan where xchan_url = '%s' and xchan_network = 'zot6' limit 1 " ,
dbesc ( $url )
);
if ( $p ) {
set_xconfig ( $url , 'system' , 'protocols' , 'zot6,activitypub' );
}
2018-05-30 04:08:52 +00:00
$r = q ( " select * from xchan where xchan_hash = '%s' limit 1 " ,
dbesc ( $url )
);
2019-05-13 06:57:57 +00:00
if ( ! $r ) {
2018-05-30 04:08:52 +00:00
// create a new record
$r = xchan_store_lowlevel (
[
2018-09-05 06:42:06 +00:00
'xchan_hash' => $url ,
'xchan_guid' => $url ,
'xchan_pubkey' => $pubkey ,
2018-09-17 04:41:24 +00:00
'xchan_addr' => (( strpos ( $username , '@' )) ? $username : '' ),
2018-09-05 06:42:06 +00:00
'xchan_url' => $profile ,
'xchan_name' => $name ,
2020-08-12 01:03:57 +00:00
'xchan_hidden' => intval ( $hidden ),
2020-09-02 05:47:32 +00:00
'xchan_updated' => datetime_convert (),
2018-09-05 06:42:06 +00:00
'xchan_name_date' => datetime_convert (),
'xchan_network' => 'activitypub' ,
2020-11-16 22:51:03 +00:00
'xchan_type' => $xchan_type ,
2018-09-05 06:42:06 +00:00
'xchan_photo_date' => datetime_convert ( 'UTC' , 'UTC' , '1968-01-01' ),
2018-09-06 23:59:19 +00:00
'xchan_photo_l' => z_root () . '/' . get_default_profile_photo (),
'xchan_photo_m' => z_root () . '/' . get_default_profile_photo ( 80 ),
'xchan_photo_s' => z_root () . '/' . get_default_profile_photo ( 48 ),
2018-09-05 06:42:06 +00:00
'xchan_photo_mimetype' => 'image/png' ,
2018-05-30 04:08:52 +00:00
]
);
}
else {
2018-09-19 23:25:05 +00:00
// Record exists. Cache existing records for a set number of days
2018-05-30 04:08:52 +00:00
// then refetch to catch updated profile photos, names, etc.
2021-03-15 05:10:44 +00:00
if ( $r [ 0 ][ 'xchan_name_date' ] >= datetime_convert ( 'UTC' , 'UTC' , 'now - ' . self :: $ACTOR_CACHE_DAYS . ' days' ) && ( ! $force )) {
2018-05-30 04:08:52 +00:00
return ;
2018-09-19 23:25:05 +00:00
}
2018-05-30 04:08:52 +00:00
// update existing record
2020-11-16 22:51:03 +00:00
$u = q ( " update xchan set xchan_updated = '%s', xchan_name = '%s', xchan_pubkey = '%s', xchan_network = '%s', xchan_name_date = '%s', xchan_hidden = %d, xchan_type = %d where xchan_hash = '%s' " ,
2020-09-02 05:47:32 +00:00
dbesc ( datetime_convert ()),
2018-05-30 04:08:52 +00:00
dbesc ( $name ),
dbesc ( $pubkey ),
dbesc ( 'activitypub' ),
dbesc ( datetime_convert ()),
2020-08-12 01:03:57 +00:00
intval ( $hidden ),
2020-11-16 22:51:03 +00:00
intval ( $xchan_type ),
2018-05-30 04:08:52 +00:00
dbesc ( $url )
);
2018-11-14 01:23:09 +00:00
2019-05-13 06:57:57 +00:00
if ( strpos ( $username , '@' ) && ( $r [ 0 ][ 'xchan_addr' ] !== $username )) {
2018-11-14 01:23:09 +00:00
$r = q ( " update xchan set xchan_addr = '%s' where xchan_hash = '%s' " ,
dbesc ( $username ),
dbesc ( $url )
);
}
2018-05-30 04:08:52 +00:00
}
2021-07-11 21:41:51 +00:00
if ( $cover_photo ) {
set_xconfig ( $url , 'system' , 'cover_photo' , $cover_photo );
}
2020-10-22 04:29:07 +00:00
$m = parse_url ( $url );
if ( $m [ 'scheme' ] && $m [ 'host' ]) {
$site_url = $m [ 'scheme' ] . '://' . $m [ 'host' ];
$ni = Nodeinfo :: fetch ( $site_url );
if ( $ni && is_array ( $ni )) {
$software = (( array_path_exists ( 'software/name' , $ni )) ? $ni [ 'software' ][ 'name' ] : '' );
$version = (( array_path_exists ( 'software/version' , $ni )) ? $ni [ 'software' ][ 'version' ] : '' );
2020-11-24 05:28:33 +00:00
$register = $ni [ 'openRegistrations' ];
2020-10-29 04:25:16 +00:00
$site = q ( " select * from site where site_url = '%s' " ,
2020-10-22 04:29:07 +00:00
dbesc ( $site_url )
);
2020-10-29 04:25:16 +00:00
if ( $site ) {
2021-01-29 00:47:01 +00:00
q ( " update site set site_project = '%s', site_update = '%s', site_version = '%s' where site_url = '%s' " ,
2020-10-22 04:29:07 +00:00
dbesc ( $software ),
2021-01-29 00:47:01 +00:00
dbesc ( datetime_convert ()),
2020-10-22 04:29:07 +00:00
dbesc ( $version ),
dbesc ( $site_url )
);
2020-10-29 04:25:16 +00:00
// it may have been saved originally as an unknown type, but we now know what it is
if ( intval ( $site [ 0 ][ 'site_type' ]) === SITE_TYPE_UNKNOWN ) {
q ( " update site set site_type = %d where site_url = '%s' " ,
intval ( SITE_TYPE_NOTZOT ),
dbesc ( $site_url )
);
}
2020-10-22 04:29:07 +00:00
}
else {
site_store_lowlevel (
[
'site_url' => $site_url ,
'site_update' => datetime_convert (),
'site_dead' => 0 ,
'site_type' => SITE_TYPE_NOTZOT ,
'site_project' => $software ,
2020-11-24 05:28:33 +00:00
'site_version' => $version ,
'site_access' => (( $register ) ? ACCESS_FREE : ACCESS_PRIVATE ),
'site_register' => (( $register ) ? REGISTER_OPEN : REGISTER_CLOSED )
2020-10-22 04:29:07 +00:00
]
);
}
}
}
2020-08-17 04:55:59 +00:00
Libzotdir :: import_directory_profile ( $url ,[ 'about' => $about , 'keywords' => $keywords , 'dob' => '0000-00-00' ], null , 0 , true );
2020-08-15 21:55:03 +00:00
2019-05-13 06:57:57 +00:00
if ( $collections ) {
2018-05-30 04:08:52 +00:00
set_xconfig ( $url , 'activitypub' , 'collections' , $collections );
}
2018-12-05 03:57:28 +00:00
$h = q ( " select * from hubloc where hubloc_hash = '%s' limit 1 " ,
2018-05-30 04:08:52 +00:00
dbesc ( $url )
);
$m = parse_url ( $url );
2019-05-13 06:57:57 +00:00
if ( $m ) {
2018-05-30 04:08:52 +00:00
$hostname = $m [ 'host' ];
$baseurl = $m [ 'scheme' ] . '://' . $m [ 'host' ] . (( $m [ 'port' ]) ? ':' . $m [ 'port' ] : '' );
}
2019-05-13 06:57:57 +00:00
if ( ! $h ) {
2018-05-30 04:08:52 +00:00
$r = hubloc_store_lowlevel (
[
'hubloc_guid' => $url ,
'hubloc_hash' => $url ,
2019-12-02 00:35:19 +00:00
'hubloc_id_url' => $profile ,
2018-09-17 04:41:24 +00:00
'hubloc_addr' => (( strpos ( $username , '@' )) ? $username : '' ),
2018-05-30 04:08:52 +00:00
'hubloc_network' => 'activitypub' ,
'hubloc_url' => $baseurl ,
'hubloc_host' => $hostname ,
'hubloc_callback' => $inbox ,
'hubloc_updated' => datetime_convert (),
'hubloc_primary' => 1
]
);
}
2018-11-14 01:23:09 +00:00
else {
2019-05-13 06:57:57 +00:00
if ( strpos ( $username , '@' ) && ( $h [ 0 ][ 'hubloc_addr' ] !== $username )) {
2018-11-14 01:23:09 +00:00
$r = q ( " update hubloc set hubloc_addr = '%s' where hubloc_hash = '%s' " ,
dbesc ( $username ),
dbesc ( $url )
);
}
2019-05-13 06:57:57 +00:00
if ( $inbox !== $h [ 0 ][ 'hubloc_callback' ]) {
2018-12-04 11:24:41 +00:00
$r = q ( " update hubloc set hubloc_callback = '%s' where hubloc_hash = '%s' " ,
dbesc ( $inbox ),
dbesc ( $url )
);
}
2019-12-02 00:35:19 +00:00
if ( $profile !== $h [ 0 ][ 'hubloc_id_url' ]) {
$r = q ( " update hubloc set hubloc_id_url = '%s' where hubloc_hash = '%s' " ,
dbesc ( $profile ),
dbesc ( $url )
);
}
2018-11-21 05:50:41 +00:00
$r = q ( " update hubloc set hubloc_updated = '%s' where hubloc_hash = '%s' " ,
dbesc ( datetime_convert ()),
dbesc ( $url )
);
2018-11-14 01:23:09 +00:00
}
2018-05-30 04:08:52 +00:00
2019-09-25 03:03:04 +00:00
if ( ! $icon ) {
2018-05-30 04:08:52 +00:00
$icon = z_root () . '/' . get_default_profile_photo ( 300 );
2019-09-25 03:03:04 +00:00
}
2021-03-01 00:14:40 +00:00
// We store all ActivityPub actors we can resolve. Some of them may be able to communicate over Zot6. Find them.
// Only probe if it looks like it looks something like a zot6 URL as there isn't anything in the actor record which we can reliably use for this purpose
// and adding zot discovery urls to the actor record will cause federation to fail with the 20-30 projects which don't accept arrays in the url field.
2019-09-25 03:03:04 +00:00
2021-03-01 00:14:40 +00:00
if ( strpos ( $url , '/channel/' ) !== false ) {
$zx = q ( " select * from hubloc where hubloc_id_url = '%s' and hubloc_network = 'zot6' " ,
dbesc ( $url )
);
if (( $username ) && strpos ( $username , '@' ) && ( ! $zx )) {
Run :: Summon ( [ 'Gprobe' , bin2hex ( $username ) ] );
}
}
2020-06-15 07:29:56 +00:00
Run :: Summon ( [ 'Xchan_photo' , bin2hex ( $icon ), bin2hex ( $url ) ] );
2018-05-30 04:08:52 +00:00
}
2018-10-11 01:53:26 +00:00
static function drop ( $channel , $observer , $act ) {
$r = q ( " select * from item where mid = '%s' and uid = %d limit 1 " ,
2019-09-25 03:03:04 +00:00
dbesc (( is_array ( $act -> obj )) ? $act -> obj [ 'id' ] : $act -> obj ),
intval ( $channel [ 'channel_id' ])
2018-10-11 01:53:26 +00:00
);
2019-05-13 06:57:57 +00:00
if ( ! $r ) {
2018-10-11 01:53:26 +00:00
return ;
}
2019-05-13 06:57:57 +00:00
if ( in_array ( $observer ,[ $r [ 0 ][ 'author_xchan' ], $r [ 0 ][ 'owner_xchan' ] ])) {
2018-10-11 01:53:26 +00:00
drop_item ( $r [ 0 ][ 'id' ], false );
}
2019-05-13 06:57:57 +00:00
elseif ( in_array ( $act -> actor [ 'id' ],[ $r [ 0 ][ 'author_xchan' ], $r [ 0 ][ 'owner_xchan' ] ])) {
2018-10-11 01:53:26 +00:00
drop_item ( $r [ 0 ][ 'id' ], false );
}
}
2018-05-30 04:08:52 +00:00
// sort function width decreasing
2018-09-17 05:04:11 +00:00
static function vid_sort ( $a , $b ) {
2019-05-13 06:57:57 +00:00
if ( $a [ 'width' ] === $b [ 'width' ])
2018-05-30 04:08:52 +00:00
return 0 ;
return (( $a [ 'width' ] > $b [ 'width' ]) ? - 1 : 1 );
}
2018-08-31 02:19:32 +00:00
static function share_bb ( $obj ) {
// @fixme - error check and set defaults
$name = urlencode ( $obj [ 'actor' ][ 'name' ]);
$profile = $obj [ 'actor' ][ 'id' ];
$photo = $obj [ 'icon' ][ 'url' ];
$s = " \r \n [share author=' " . $name .
" ' profile=' " . $profile .
" ' avatar=' " . $photo .
" ' link=' " . $act -> obj [ 'id' ] .
" ' auth=' " . (( is_matrix_url ( $act -> obj [ 'id' ])) ? 'true' : 'false' ) .
" ' posted=' " . $act -> obj [ 'published' ] .
" ' message_id=' " . $act -> obj [ 'id' ] .
" '] " ;
return $s ;
}
2018-10-14 22:47:57 +00:00
static function get_actor_bbmention ( $id ) {
$x = q ( " select * from hubloc left join xchan on hubloc_hash = xchan_hash where hubloc_hash = '%s' or hubloc_id_url = '%s' limit 1 " ,
dbesc ( $id ),
dbesc ( $id )
);
2019-05-13 06:57:57 +00:00
if ( $x ) {
2020-12-19 21:08:16 +00:00
// a name starting with a left paren can trick the markdown parser into creating a link so insert a zero-width space
if ( substr ( $x [ 0 ][ 'xchan_name' ], 0 , 1 ) === '(' ) {
$x [ 0 ][ 'xchan_name' ] = htmlspecialchars_decode ( '​' , ENT_QUOTES ) . $x [ 0 ][ 'xchan_name' ];
}
2018-10-14 22:47:57 +00:00
return sprintf ( '@[zrl=%s]%s[/zrl]' , $x [ 0 ][ 'xchan_url' ], $x [ 0 ][ 'xchan_name' ]);
}
return '@{' . $id . '}' ;
}
2020-02-26 02:32:16 +00:00
static function update_poll ( $item , $post ) {
2020-01-27 23:54:43 +00:00
$multi = false ;
2020-02-26 02:32:16 +00:00
$mid = $post [ 'mid' ];
$content = $post [ 'title' ];
2020-01-28 02:11:28 +00:00
if ( ! $item ) {
2020-01-27 23:54:43 +00:00
return false ;
}
2020-01-28 02:11:28 +00:00
2020-01-27 23:54:43 +00:00
$o = json_decode ( $item [ 'obj' ], true );
2020-01-29 02:17:47 +00:00
if ( $o && array_key_exists ( 'anyOf' , $o )) {
2020-01-27 23:54:43 +00:00
$multi = true ;
}
2020-02-26 02:32:16 +00:00
2020-09-25 07:22:20 +00:00
$r = q ( " select mid, title from item where parent_mid = '%s' and author_xchan = '%s' and mid != parent_mid " ,
2020-02-26 02:32:16 +00:00
dbesc ( $item [ 'mid' ]),
dbesc ( $post [ 'author_xchan' ])
);
// prevent any duplicate votes by same author for oneOf and duplicate votes with same author and same answer for anyOf
if ( $r ) {
if ( $multi ) {
foreach ( $r as $rv ) {
if ( $rv [ 'title' ] === $content && $rv [ 'mid' ] !== $mid ) {
return false ;
}
}
}
else {
foreach ( $r as $rv ) {
if ( $rv [ 'mid' ] !== $mid ) {
return false ;
}
}
}
}
2020-01-27 23:54:43 +00:00
$answer_found = false ;
$found = false ;
if ( $multi ) {
2020-01-28 04:46:14 +00:00
for ( $c = 0 ; $c < count ( $o [ 'anyOf' ]); $c ++ ) {
if ( $o [ 'anyOf' ][ $c ][ 'name' ] === $content ) {
2020-01-27 23:54:43 +00:00
$answer_found = true ;
2020-01-28 04:46:14 +00:00
if ( is_array ( $o [ 'anyOf' ][ $c ][ 'replies' ])) {
foreach ( $o [ 'anyOf' ][ $c ][ 'replies' ] as $reply ) {
2020-09-25 07:22:20 +00:00
if ( is_array ( $reply ) && array_key_exists ( 'id' , $reply ) && $reply [ 'id' ] === $mid ) {
2020-01-28 04:46:14 +00:00
$found = true ;
}
2020-01-27 23:54:43 +00:00
}
}
2020-01-28 04:46:14 +00:00
if ( ! $found ) {
$o [ 'anyOf' ][ $c ][ 'replies' ][ 'totalItems' ] ++ ;
$o [ 'anyOf' ][ $c ][ 'replies' ][ 'items' ][] = [ 'id' => $mid , 'type' => 'Note' ];
}
2020-01-27 23:54:43 +00:00
}
}
}
else {
2020-01-28 04:46:14 +00:00
for ( $c = 0 ; $c < count ( $o [ 'oneOf' ]); $c ++ ) {
if ( $o [ 'oneOf' ][ $c ][ 'name' ] === $content ) {
$answer_found = true ;
if ( is_array ( $o [ 'oneOf' ][ $c ][ 'replies' ])) {
foreach ( $o [ 'oneOf' ][ $c ][ 'replies' ] as $reply ) {
2020-09-25 07:22:20 +00:00
if ( is_array ( $reply ) && array_key_exists ( 'id' , $reply ) && $reply [ 'id' ] === $mid ) {
2020-01-28 04:46:14 +00:00
$found = true ;
}
2020-01-27 23:54:43 +00:00
}
}
2020-01-28 04:46:14 +00:00
if ( ! $found ) {
$o [ 'oneOf' ][ $c ][ 'replies' ][ 'totalItems' ] ++ ;
$o [ 'oneOf' ][ $c ][ 'replies' ][ 'items' ][] = [ 'id' => $mid , 'type' => 'Note' ];
}
2020-01-27 23:54:43 +00:00
}
}
}
2020-02-07 02:50:18 +00:00
if ( $item [ 'comments_closed' ] > NULL_DATE ) {
if ( $item [ 'comments_closed' ] > datetime_convert ()) {
$o [ 'closed' ] = datetime_convert ( 'UTC' , 'UTC' , $item [ 'comments_closed' ], ATOM_TIME );
// set this to force an update
$answer_found = true ;
}
}
2020-01-28 04:46:14 +00:00
logger ( 'updated_poll: ' . print_r ( $o , true ), LOGGER_DATA );
2020-01-27 23:54:43 +00:00
if ( $answer_found && ! $found ) {
2020-01-28 02:11:28 +00:00
$x = q ( " update item set obj = '%s', edited = '%s' where id = %d " ,
2020-01-27 23:54:43 +00:00
dbesc ( json_encode ( $o )),
dbesc ( datetime_convert ()),
intval ( $item [ 'id' ])
);
2020-06-15 07:29:56 +00:00
Run :: Summon ( [ 'Notifier' , 'wall-new' , $item [ 'id' ] ] );
2020-01-27 23:54:43 +00:00
return true ;
}
return false ;
}
2018-10-14 22:47:57 +00:00
2020-03-26 22:41:09 +00:00
static function decode_note ( $act , $cacheable = false ) {
2018-05-30 06:30:18 +00:00
2018-10-14 22:47:57 +00:00
$response_activity = false ;
2020-01-27 23:54:43 +00:00
$poll_handled = false ;
2018-05-30 06:30:18 +00:00
$s = [];
2019-05-13 06:57:57 +00:00
if ( is_array ( $act -> obj )) {
2020-02-12 23:21:39 +00:00
$binary = false ;
2020-05-28 01:56:51 +00:00
$markdown = false ;
2020-02-14 05:08:28 +00:00
if ( array_key_exists ( 'mediaType' , $act -> obj ) && $act -> obj [ 'mediaType' ] !== 'text/html' ) {
2020-05-28 01:56:51 +00:00
if ( $act -> obj [ 'mediaType' ] === 'text/markdown' ) {
$markdown = true ;
}
else {
$s [ 'mimetype' ] = escape_tags ( $act -> obj [ 'mediaType' ]);
$binary = true ;
}
2020-02-12 23:21:39 +00:00
}
$content = self :: get_content ( $act -> obj , $binary );
2020-05-28 01:56:51 +00:00
2020-06-11 01:20:46 +00:00
if ( $cacheable ) {
// Zot6 activities will all be rendered from bbcode source in order to generate dynamic content.
// If the activity came from ActivityPub (hence $cacheable is set), use the HTML rendering
// and discard the bbcode source since it is unlikely that it is compatible with our implementation.
// Friendica for example.
unset ( $content [ 'bbcode' ]);
}
2020-05-28 01:56:51 +00:00
// handle markdown conversion inline (peertube)
if ( $markdown ) {
foreach ( [ 'summary' , 'content' ] as $t ) {
$content [ $t ] = Markdown :: to_bbcode ( $content [ $t ], true , [ 'preserve_lf' => true ]);
}
}
2020-02-07 02:50:18 +00:00
}
2020-01-27 23:54:43 +00:00
2020-02-07 02:50:18 +00:00
// These activities should have been handled separately in the Inbox module and should not be turned into posts
2020-02-10 05:39:26 +00:00
if ( in_array ( $act -> type , [ 'Follow' , 'Accept' , 'Reject' , 'Create' , 'Update' ]) && is_array ( $act -> obj ) && array_key_exists ( 'type' , $act -> obj )
&& ( $act -> obj [ 'type' ] === 'Follow' || ActivityStreams :: is_an_actor ( $act -> obj [ 'type' ]))) {
2020-02-07 02:50:18 +00:00
return false ;
2018-09-01 22:52:28 +00:00
}
2020-02-07 02:50:18 +00:00
2021-03-13 22:42:19 +00:00
// Within our family of projects, Follow/Unfollow of a thread is an internal activity which should not be transmitted,
// hence if we receive it - ignore or reject it.
// This may have to be revisited if AP projects start using Follow for objects other than actors.
2021-03-13 20:35:22 +00:00
2021-05-17 21:14:57 +00:00
if ( in_array ( $act -> type , [ ACTIVITY_FOLLOW , ACTIVITY_IGNORE ])) {
2021-03-13 20:35:22 +00:00
return false ;
}
2018-06-28 01:43:39 +00:00
$s [ 'owner_xchan' ] = $act -> actor [ 'id' ];
2018-05-30 06:30:18 +00:00
$s [ 'author_xchan' ] = $act -> actor [ 'id' ];
2019-02-03 19:45:29 +00:00
// ensure we store the original actor
self :: actor_store ( $act -> actor [ 'id' ], $act -> actor );
2021-06-23 23:56:49 +00:00
$s [ 'mid' ] = (( is_array ( $act -> obj ) && isset ( $act -> obj [ 'id' ])) ? $act -> obj [ 'id' ] : $act -> obj );
if ( ! $s [ 'mid' ]) {
return false ;
}
2018-06-28 01:43:39 +00:00
$s [ 'parent_mid' ] = $act -> parent_id ;
2018-05-30 06:30:18 +00:00
2021-03-08 23:36:50 +00:00
if ( array_key_exists ( 'published' , $act -> data ) && $act -> data [ 'published' ]) {
2018-05-30 06:30:18 +00:00
$s [ 'created' ] = datetime_convert ( 'UTC' , 'UTC' , $act -> data [ 'published' ]);
}
2021-03-08 23:36:50 +00:00
elseif ( array_key_exists ( 'published' , $act -> obj ) && $act -> obj [ 'published' ]) {
2018-05-30 06:30:18 +00:00
$s [ 'created' ] = datetime_convert ( 'UTC' , 'UTC' , $act -> obj [ 'published' ]);
}
2021-03-08 23:36:50 +00:00
if ( array_key_exists ( 'updated' , $act -> data ) && $act -> data [ 'updated' ]) {
2018-05-30 06:30:18 +00:00
$s [ 'edited' ] = datetime_convert ( 'UTC' , 'UTC' , $act -> data [ 'updated' ]);
}
2021-03-08 23:36:50 +00:00
elseif ( array_key_exists ( 'updated' , $act -> obj ) && $act -> obj [ 'updated' ]) {
2018-05-30 06:30:18 +00:00
$s [ 'edited' ] = datetime_convert ( 'UTC' , 'UTC' , $act -> obj [ 'updated' ]);
}
2021-03-08 23:36:50 +00:00
if ( array_key_exists ( 'expires' , $act -> data ) && $act -> data [ 'expires' ]) {
2019-05-28 23:42:32 +00:00
$s [ 'expires' ] = datetime_convert ( 'UTC' , 'UTC' , $act -> data [ 'expires' ]);
}
2021-03-08 23:36:50 +00:00
elseif ( array_key_exists ( 'expires' , $act -> obj ) && $act -> obj [ 'expires' ]) {
2019-05-28 23:42:32 +00:00
$s [ 'expires' ] = datetime_convert ( 'UTC' , 'UTC' , $act -> obj [ 'expires' ]);
}
2018-05-30 06:30:18 +00:00
2021-06-23 23:56:49 +00:00
if ( $act -> type === 'Invite' && is_array ( $act -> obj ) && array_key_exists ( 'type' , $act -> obj ) && $act -> obj [ 'type' ] === 'Event' ) {
2020-01-14 20:11:23 +00:00
$s [ 'mid' ] = $s [ 'parent_mid' ] = $act -> id ;
}
2018-05-30 06:30:18 +00:00
2021-04-17 23:34:36 +00:00
if ( isset ( $act -> replyto ) && ! empty ( $act -> replyto )) {
if ( is_array ( $act -> replyto ) && isset ( $act -> replyto [ 'id' ])) {
$s [ 'replyto' ] = $act -> replyto [ 'id' ];
}
else {
$s [ 'replyto' ] = $act -> replyto ;
}
}
2020-10-06 02:41:47 +00:00
if ( ActivityStreams :: is_response_activity ( $act -> type )) {
2018-10-14 22:47:57 +00:00
$response_activity = true ;
2018-09-26 00:50:12 +00:00
$s [ 'mid' ] = $act -> id ;
2021-06-23 23:56:49 +00:00
$s [ 'parent_mid' ] = (( is_array ( $act -> obj ) && isset ( $act -> obj [ 'id' ])) ? $act -> obj [ 'id' ] : $act -> obj );
2021-04-06 05:36:32 +00:00
2019-09-10 04:03:33 +00:00
2018-09-26 00:50:12 +00:00
// over-ride the object timestamp with the activity
2019-05-13 06:57:57 +00:00
if ( $act -> data [ 'published' ]) {
2018-09-26 00:50:12 +00:00
$s [ 'created' ] = datetime_convert ( 'UTC' , 'UTC' , $act -> data [ 'published' ]);
}
2019-05-13 06:57:57 +00:00
if ( $act -> data [ 'updated' ]) {
2018-10-14 22:47:57 +00:00
$s [ 'edited' ] = datetime_convert ( 'UTC' , 'UTC' , $act -> data [ 'updated' ]);
}
2018-10-10 05:32:20 +00:00
$obj_actor = (( isset ( $act -> obj [ 'actor' ])) ? $act -> obj [ 'actor' ] : $act -> get_actor ( 'attributedTo' , $act -> obj ));
2019-06-17 03:35:47 +00:00
2020-07-01 04:30:03 +00:00
// We already check for admin blocks of third-party objects when fetching them explicitly.
// Repeat here just in case the entire object was supplied inline and did not require fetching
if ( $obj_actor && array_key_exists ( 'id' , $obj_actor )) {
$m = parse_url ( $obj_actor [ 'id' ]);
if ( $m && $m [ 'scheme' ] && $m [ 'host' ]) {
if ( ! check_siteallowed ( $m [ 'scheme' ] . '://' . $m [ 'host' ])) {
return ;
}
}
if ( ! check_channelallowed ( $obj_actor [ 'id' ])) {
return ;
}
}
// if the object is an actor, it is not really a response activity, so reset a couple of things
2019-06-17 03:35:47 +00:00
if ( ActivityStreams :: is_an_actor ( $act -> obj [ 'type' ])) {
$obj_actor = $act -> actor ;
$s [ 'parent_mid' ] = $s [ 'mid' ];
}
2020-07-01 04:30:03 +00:00
2018-10-14 22:47:57 +00:00
// ensure we store the original actor
self :: actor_store ( $obj_actor [ 'id' ], $obj_actor );
$mention = self :: get_actor_bbmention ( $obj_actor [ 'id' ]);
2018-10-10 05:32:20 +00:00
2020-11-19 04:09:05 +00:00
$quoted_content = '[quote]' . $content [ 'content' ] . '[/quote]' ;
2019-05-13 06:57:57 +00:00
if ( $act -> type === 'Like' ) {
2020-11-19 04:09:05 +00:00
$content [ 'content' ] = sprintf ( t ( 'Likes %1$s\'s %2$s' ), $mention , (( ActivityStreams :: is_an_actor ( $act -> obj [ 'type' ])) ? t ( 'Profile' ) : $act -> obj [ 'type' ])) . EOL . EOL . $quoted_content ;
2018-09-26 00:50:12 +00:00
}
2019-05-13 06:57:57 +00:00
if ( $act -> type === 'Dislike' ) {
2020-11-19 04:09:05 +00:00
$content [ 'content' ] = sprintf ( t ( 'Doesn\'t like %1$s\'s %2$s' ), $mention , (( ActivityStreams :: is_an_actor ( $act -> obj [ 'type' ])) ? t ( 'Profile' ) : $act -> obj [ 'type' ])) . EOL . EOL . $quoted_content ;
2018-09-26 00:50:12 +00:00
}
2020-01-11 22:53:39 +00:00
// handle event RSVPs
if (( $act -> obj [ 'type' ] === 'Event' ) || ( $act -> obj [ 'type' ] === 'Invite' && array_path_exists ( 'object/type' , $act -> obj ) && $act -> obj [ 'object' ][ 'type' ] === 'Event' )) {
if ( $act -> type === 'Accept' ) {
2020-11-19 04:09:05 +00:00
$content [ 'content' ] = sprintf ( t ( 'Will attend %s\'s event' ), $mention ) . EOL . EOL . $quoted_content ;
2020-01-11 22:53:39 +00:00
}
if ( $act -> type === 'Reject' ) {
2020-11-19 04:09:05 +00:00
$content [ 'content' ] = sprintf ( t ( 'Will not attend %s\'s event' ), $mention ) . EOL . EOL . $quoted_content ;
2020-01-11 22:53:39 +00:00
}
if ( $act -> type === 'TentativeAccept' ) {
2020-11-19 04:09:05 +00:00
$content [ 'content' ] = sprintf ( t ( 'May attend %s\'s event' ), $mention ) . EOL . EOL . $quoted_content ;
2020-01-11 22:53:39 +00:00
}
if ( $act -> type === 'TentativeReject' ) {
2020-11-19 04:09:05 +00:00
$content [ 'content' ] = sprintf ( t ( 'May not attend %s\'s event' ), $mention ) . EOL . EOL . $quoted_content ;
2020-01-11 22:53:39 +00:00
}
2019-05-29 04:10:32 +00:00
}
2020-01-11 22:53:39 +00:00
2019-05-13 06:57:57 +00:00
if ( $act -> type === 'Announce' ) {
2018-10-15 02:02:15 +00:00
$content [ 'content' ] = sprintf ( t ( '🔁 Repeated %1$s\'s %2$s' ), $mention , $act -> obj [ 'type' ]);
2018-08-01 03:15:00 +00:00
}
2020-02-05 01:01:11 +00:00
2019-05-03 04:21:10 +00:00
if ( $act -> type === 'emojiReaction' ) {
2020-02-04 01:51:42 +00:00
// Hubzilla reactions
2019-05-03 04:21:10 +00:00
$content [ 'content' ] = (( $act -> tgt && $act -> tgt [ 'type' ] === 'Image' ) ? '[img=32x32]' . $act -> tgt [ 'url' ] . '[/img]' : '&#x' . $act -> tgt [ 'name' ] . ';' );
}
2020-02-05 01:01:11 +00:00
2020-02-04 23:44:26 +00:00
if ( in_array ( $act -> type ,[ 'EmojiReaction' , 'EmojiReact' ])) {
2020-02-04 01:51:42 +00:00
// Pleroma reactions
2020-07-28 03:26:12 +00:00
$t = trim ( self :: get_textfield ( $act -> data , 'content' ));
$e = Emoji\is_single_emoji ( $t ) || mb_strlen ( $t ) === 1 ;
2020-02-04 01:51:42 +00:00
if ( $e ) {
2020-07-28 03:26:12 +00:00
$content [ 'content' ] = $t ;
2020-02-05 01:01:11 +00:00
}
2019-12-10 23:35:36 +00:00
}
2018-08-01 02:50:39 +00:00
}
2018-09-03 03:16:33 +00:00
2021-04-14 05:08:41 +00:00
$s [ 'comment_policy' ] = 'authenticated' ;
2021-04-14 01:12:15 +00:00
if ( $s [ 'mid' ] === $s [ 'parent_mid' ]) {
// it is a parent node - decode the comment policy info if present
if ( isset ( $act -> obj [ 'commentPolicy' ])) {
$until = strpos ( $act -> obj [ 'commentPolicy' ], 'until=' );
if ( $until !== false ) {
2021-04-14 05:08:41 +00:00
$s [ 'comments_closed' ] = datetime_convert ( 'UTC' , 'UTC' , substr ( $act -> obj [ 'commentPolicy' ], $until + 6 ));
if ( $s [ 'comments_closed' ] < datetime_convert ()) {
$s [ 'nocomment' ] = true ;
2021-04-14 01:12:15 +00:00
}
}
$remainder = substr ( $act -> obj [ 'commentPolicy' ], 0 ,(( $until ) ? $until : strlen ( $act -> obj [ 'commentPolicy' ])));
if ( $remainder ) {
2021-04-14 05:08:41 +00:00
$s [ 'comment_policy' ] = $remainder ;
}
if ( ! ( isset ( $item [ 'comment_policy' ]) && strlen ( $item [ 'comment_policy' ]))) {
$s [ 'comment_policy' ] = 'contacts' ;
2021-04-14 01:12:15 +00:00
}
}
}
2021-03-08 23:36:50 +00:00
if ( ! ( array_key_exists ( 'created' , $s ) && $s [ 'created' ])) {
2018-09-26 00:50:12 +00:00
$s [ 'created' ] = datetime_convert ();
2020-07-01 04:30:03 +00:00
}
2021-03-08 23:36:50 +00:00
if ( ! ( array_key_exists ( 'edited' , $s ) && $s [ 'edited' ])) {
2018-09-26 00:50:12 +00:00
$s [ 'edited' ] = $s [ 'created' ];
2020-07-01 04:30:03 +00:00
}
2018-12-12 05:03:50 +00:00
$s [ 'title' ] = (( $response_activity ) ? EMPTY_STR : self :: bb_content ( $content , 'name' ));
2018-09-03 03:16:33 +00:00
$s [ 'summary' ] = self :: bb_content ( $content , 'summary' );
2018-08-01 02:50:39 +00:00
2021-03-09 02:41:29 +00:00
if ( array_key_exists ( 'mimetype' , $s ) && ( ! in_array ( $s [ 'mimetype' ], [ 'text/bbcode' , 'text/x-multicode' ]))) {
$s [ 'body' ] = $content [ 'content' ];
2020-02-12 23:21:39 +00:00
}
else {
2021-03-09 02:41:29 +00:00
$s [ 'body' ] = (( self :: bb_content ( $content , 'bbcode' ) && ( ! $response_activity )) ? self :: bb_content ( $content , 'bbcode' ) : self :: bb_content ( $content , 'content' ));
2020-02-12 23:21:39 +00:00
}
2020-06-02 04:00:21 +00:00
2019-05-15 05:16:49 +00:00
// handle some of the more widely used of the numerous and varied ways of deleting something
2020-10-01 06:52:04 +00:00
if ( in_array ( $act -> type , [ 'Delete' , 'Undo' , 'Tombstone' ])) {
2019-05-15 05:16:49 +00:00
$s [ 'item_deleted' ] = 1 ;
}
if ( $act -> type === 'Create' && $act -> obj [ 'type' ] === 'Tombstone' ) {
$s [ 'item_deleted' ] = 1 ;
}
2018-08-04 02:26:16 +00:00
2019-12-10 05:39:48 +00:00
if ( $act -> obj && array_key_exists ( 'sensitive' , $act -> obj ) && boolval ( $act -> obj [ 'sensitive' ])) {
2019-10-18 01:09:40 +00:00
$s [ 'item_nsfw' ] = 1 ;
}
2019-06-11 04:19:19 +00:00
2019-02-01 20:47:34 +00:00
$s [ 'verb' ] = self :: activity_mapper ( $act -> type );
2020-01-29 00:42:38 +00:00
// Mastodon does not provide update timestamps when updating poll tallies which means race conditions may occur here.
if ( $act -> type === 'Update' && $act -> obj [ 'type' ] === 'Question' && $s [ 'edited' ] === $s [ 'created' ]) {
$s [ 'edited' ] = datetime_convert ();
}
2020-01-27 23:54:43 +00:00
2018-06-28 01:43:39 +00:00
$s [ 'obj_type' ] = self :: activity_obj_mapper ( $act -> obj [ 'type' ]);
$s [ 'obj' ] = $act -> obj ;
2020-02-06 05:58:50 +00:00
if ( is_array ( $s [ 'obj' ]) && array_path_exists ( 'actor/id' , $s [ 'obj' ])) {
2018-12-26 23:33:01 +00:00
$s [ 'obj' ][ 'actor' ] = $s [ 'obj' ][ 'actor' ][ 'id' ];
}
2021-04-30 00:50:13 +00:00
if ( is_array ( $act -> tgt ) && $act -> tgt ) {
if ( array_key_exists ( 'type' , $act -> tgt )) {
$s [ 'tgt_type' ] = self :: activity_obj_mapper ( $act -> tgt [ 'type' ]);
}
// We shouldn't need to store collection contents which could be large. We will often only require the meta-data
if ( isset ( $s [ 'tgt_type' ]) && strpos ( $s [ 'tgt_type' ], 'Collection' ) !== false ) {
2021-04-30 04:37:53 +00:00
$s [ 'target' ] = [ 'id' => $act -> tgt [ 'id' ], 'type' => $s [ 'tgt_type' ], 'attributedTo' => (( isset ( $act -> tgt [ 'attributedTo' ])) ? $act -> tgt [ 'attributedTo' ] : $act -> tgt [ 'actor' ]) ];
2021-04-30 00:50:13 +00:00
}
}
2018-05-30 06:30:18 +00:00
2019-05-29 04:10:32 +00:00
$generator = $act -> get_property_obj ( 'generator' );
if (( ! $generator ) && ( ! $response_activity )) {
$generator = $act -> get_property_obj ( 'generator' , $act -> obj );
2018-10-14 22:47:57 +00:00
}
2018-06-05 06:19:16 +00:00
2019-05-29 04:10:32 +00:00
if ( $generator && array_key_exists ( 'type' , $generator )
&& in_array ( $generator [ 'type' ], [ 'Application' , 'Service' ] ) && array_key_exists ( 'name' , $generator )) {
$s [ 'app' ] = escape_tags ( $generator [ 'name' ]);
2018-06-05 06:19:16 +00:00
}
2021-02-20 20:58:42 +00:00
$location = $act -> get_property_obj ( 'location' );
if ( is_array ( $location ) && array_key_exists ( 'type' , $location ) && $location [ 'type' ] === 'Place' ) {
if ( array_key_exists ( 'name' , $location )) {
$s [ 'location' ] = escape_tags ( $location [ 'name' ]);
}
if ( array_key_exists ( 'content' , $location )) {
2021-04-06 20:31:44 +00:00
$s [ 'location' ] = html2plain ( purify_html ( $location [ 'content' ]), 256 );
2021-02-20 20:58:42 +00:00
}
if ( array_key_exists ( 'latitude' , $location ) && array_key_exists ( 'longitude' , $location )) {
$s [ 'coord' ] = escape_tags ( $location [ 'latitude' ]) . ' ' . escape_tags ( $location [ 'longitude' ]);
}
}
2019-05-13 06:57:57 +00:00
if ( ! $response_activity ) {
2018-10-14 22:47:57 +00:00
$a = self :: decode_taxonomy ( $act -> obj );
2019-05-13 06:57:57 +00:00
if ( $a ) {
2018-10-14 22:47:57 +00:00
$s [ 'term' ] = $a ;
2019-05-13 06:57:57 +00:00
foreach ( $a as $b ) {
if ( $b [ 'ttype' ] === TERM_EMOJI ) {
2020-02-23 23:27:39 +00:00
// $s['title'] = str_replace($b['term'],'[img=16x16]' . $b['url'] . '[/img]',$s['title']);
2018-10-14 22:47:57 +00:00
$s [ 'summary' ] = str_replace ( $b [ 'term' ], '[img=16x16]' . $b [ 'url' ] . '[/img]' , $s [ 'summary' ]);
$s [ 'body' ] = str_replace ( $b [ 'term' ], '[img=16x16]' . $b [ 'url' ] . '[/img]' , $s [ 'body' ]);
}
2018-09-06 23:40:13 +00:00
}
}
2018-05-30 06:30:18 +00:00
2020-02-24 23:40:20 +00:00
$a = self :: decode_attachment ( $act -> obj );
if ( $a ) {
$s [ 'attach' ] = $a ;
}
2020-02-23 23:27:39 +00:00
2020-02-24 23:40:20 +00:00
$a = self :: decode_iconfig ( $act -> obj );
if ( $a ) {
$s [ 'iconfig' ] = $a ;
}
2018-05-30 06:30:18 +00:00
}
2020-02-24 23:40:20 +00:00
2020-06-02 04:00:21 +00:00
2021-06-21 02:49:33 +00:00
if ( $act -> obj [ 'type' ] === 'Note' && isset ( $s [ 'attach' ]) && $s [ 'attach' ]) {
2018-10-11 04:36:55 +00:00
$s [ 'body' ] .= self :: bb_attach ( $s [ 'attach' ], $s [ 'body' ]);
2018-09-01 22:51:44 +00:00
}
2020-01-22 04:54:05 +00:00
if ( $act -> obj [ 'type' ] === 'Question' && in_array ( $act -> type ,[ 'Create' , 'Update' ])) {
if ( $act -> obj [ 'endTime' ]) {
$s [ 'comments_closed' ] = datetime_convert ( 'UTC' , 'UTC' , $act -> obj [ 'endTime' ]);
}
}
2021-03-08 23:36:50 +00:00
if ( array_key_exists ( 'closed' , $act -> obj ) && $act -> obj [ 'closed' ]) {
2020-01-22 04:54:05 +00:00
$s [ 'comments_closed' ] = datetime_convert ( 'UTC' , 'UTC' , $act -> obj [ 'closed' ]);
}
2018-09-01 22:51:44 +00:00
2018-05-30 06:30:18 +00:00
// we will need a hook here to extract magnet links e.g. peertube
// right now just link to the largest mp4 we find that will fit in our
// standard content region
2019-05-13 06:57:57 +00:00
if ( ! $response_activity ) {
if ( $act -> obj [ 'type' ] === 'Video' ) {
2018-05-30 06:30:18 +00:00
2018-10-14 22:47:57 +00:00
$vtypes = [
'video/mp4' ,
'video/ogg' ,
'video/webm'
];
2018-05-30 06:30:18 +00:00
2020-05-28 01:56:51 +00:00
$mps = [];
$poster = null ;
$ptr = null ;
2020-07-31 23:25:24 +00:00
// try to find a poster to display on the video element
2020-05-28 01:56:51 +00:00
if ( array_key_exists ( 'icon' , $act -> obj )) {
2020-07-31 23:25:24 +00:00
if ( is_array ( $act -> obj [ 'icon' ])) {
if ( array_key_exists ( 0 , $act -> obj [ 'icon' ])) {
$ptr = $act -> obj [ 'icon' ];
}
else {
$ptr = [ $act -> obj [ 'icon' ] ];
}
}
if ( $ptr ) {
foreach ( $ptr as $foo ) {
if ( is_array ( $foo ) && array_key_exists ( 'type' , $foo ) && $foo [ 'type' ] === 'Image' && is_string ( $foo [ 'url' ])) {
$poster = $foo [ 'url' ];
}
}
2020-05-28 01:56:51 +00:00
}
}
$tag = (( $poster ) ? '[video poster="' . $poster . '"]' : '[video]' );
2020-07-31 23:25:24 +00:00
$ptr = null ;
2019-05-13 06:57:57 +00:00
if ( array_key_exists ( 'url' , $act -> obj )) {
if ( is_array ( $act -> obj [ 'url' ])) {
if ( array_key_exists ( 0 , $act -> obj [ 'url' ])) {
2018-10-14 22:47:57 +00:00
$ptr = $act -> obj [ 'url' ];
}
else {
$ptr = [ $act -> obj [ 'url' ] ];
}
2020-07-31 23:25:24 +00:00
// handle peertube's weird url link tree if we find it here
// 0 => html link, 1 => application/x-mpegURL with 'tag' set to an array of actual media links
foreach ( $ptr as $idex ) {
if ( is_array ( $idex ) && array_key_exists ( 'mediaType' , $idex )) {
if ( $idex [ 'mediaType' ] === 'application/x-mpegURL' && isset ( $idex [ 'tag' ]) && is_array ( $idex [ 'tag' ])) {
$ptr = $idex [ 'tag' ];
break ;
2018-10-16 01:27:03 +00:00
}
}
2020-07-31 23:25:24 +00:00
}
foreach ( $ptr as $vurl ) {
if ( array_key_exists ( 'mediaType' , $vurl )) {
2019-05-13 06:57:57 +00:00
if ( in_array ( $vurl [ 'mediaType' ], $vtypes )) {
if ( ! array_key_exists ( 'width' , $vurl )) {
2018-10-16 01:27:03 +00:00
$vurl [ 'width' ] = 0 ;
}
$mps [] = $vurl ;
2018-10-14 22:47:57 +00:00
}
2018-09-26 06:50:35 +00:00
}
2018-05-30 06:30:18 +00:00
}
}
2019-05-13 06:57:57 +00:00
if ( $mps ) {
2018-10-14 22:47:57 +00:00
usort ( $mps ,[ __CLASS__ , 'vid_sort' ]);
2019-05-13 06:57:57 +00:00
foreach ( $mps as $m ) {
if ( intval ( $m [ 'width' ]) < 500 && self :: media_not_in_body ( $m [ 'href' ], $s [ 'body' ])) {
2020-05-28 01:56:51 +00:00
$s [ 'body' ] .= " \n \n " . $tag . $m [ 'href' ] . '[/video]' ;
2018-10-14 22:47:57 +00:00
break ;
}
2018-09-26 06:50:35 +00:00
}
2018-05-30 06:30:18 +00:00
}
2019-05-13 06:57:57 +00:00
elseif ( is_string ( $act -> obj [ 'url' ]) && self :: media_not_in_body ( $act -> obj [ 'url' ], $s [ 'body' ])) {
2020-05-28 01:56:51 +00:00
$s [ 'body' ] .= " \n \n " . $tag . $act -> obj [ 'url' ] . '[/video]' ;
2018-10-14 22:47:57 +00:00
}
2018-09-26 06:50:35 +00:00
}
2018-05-30 06:30:18 +00:00
}
2019-05-13 06:57:57 +00:00
if ( $act -> obj [ 'type' ] === 'Audio' ) {
2018-09-20 05:44:22 +00:00
2018-10-14 22:47:57 +00:00
$atypes = [
'audio/mpeg' ,
'audio/ogg' ,
'audio/wav'
];
2018-09-20 05:44:22 +00:00
2018-10-14 22:47:57 +00:00
$ptr = null ;
2018-09-26 06:50:35 +00:00
2019-05-13 06:57:57 +00:00
if ( array_key_exists ( 'url' , $act -> obj )) {
if ( is_array ( $act -> obj [ 'url' ])) {
if ( array_key_exists ( 0 , $act -> obj [ 'url' ])) {
2018-10-14 22:47:57 +00:00
$ptr = $act -> obj [ 'url' ];
}
else {
$ptr = [ $act -> obj [ 'url' ] ];
}
2019-05-13 06:57:57 +00:00
foreach ( $ptr as $vurl ) {
2021-04-27 04:32:37 +00:00
if ( isset ( $vurl [ 'mediaType' ]) && in_array ( $vurl [ 'mediaType' ], $atypes ) && self :: media_not_in_body ( $vurl [ 'href' ], $s [ 'body' ])) {
2018-10-14 22:47:57 +00:00
$s [ 'body' ] .= " \n \n " . '[audio]' . $vurl [ 'href' ] . '[/audio]' ;
break ;
}
2018-09-26 06:50:35 +00:00
}
}
2019-05-13 06:57:57 +00:00
elseif ( is_string ( $act -> obj [ 'url' ]) && self :: media_not_in_body ( $act -> obj [ 'url' ], $s [ 'body' ])) {
2018-10-14 22:47:57 +00:00
$s [ 'body' ] .= " \n \n " . '[audio]' . $act -> obj [ 'url' ] . '[/audio]' ;
}
2018-09-26 06:50:35 +00:00
}
2020-07-27 23:49:21 +00:00
// Pleroma audio scrobbler
elseif ( $act -> type === 'Listen' && array_key_exists ( 'artist' , $act -> obj ) && array_key_exists ( 'title' , $act -> obj ) && $s [ 'body' ] === EMPTY_STR ) {
$s [ 'body' ] .= " \n \n " . sprintf ( 'Listening to \"%1$s\" by %2$s' , escape_tags ( $act -> obj [ 'title' ]), escape_tags ( $act -> obj [ 'artist' ]));
if ( isset ( $act -> obj [ 'album' ])) {
$s [ 'body' ] .= " \n " . sprintf ( '(%s)' , escape_tags ( $act -> obj [ 'album' ]));
}
}
2018-10-14 22:47:57 +00:00
}
2018-09-20 05:44:22 +00:00
2019-05-13 06:57:57 +00:00
if ( $act -> obj [ 'type' ] === 'Image' && strpos ( $s [ 'body' ], 'zrl=' ) === false ) {
2018-09-26 06:50:35 +00:00
2018-10-14 22:47:57 +00:00
$ptr = null ;
2018-09-26 06:50:35 +00:00
2019-05-13 06:57:57 +00:00
if ( array_key_exists ( 'url' , $act -> obj )) {
if ( is_array ( $act -> obj [ 'url' ])) {
if ( array_key_exists ( 0 , $act -> obj [ 'url' ])) {
2018-10-14 22:47:57 +00:00
$ptr = $act -> obj [ 'url' ];
}
else {
$ptr = [ $act -> obj [ 'url' ] ];
}
2019-05-13 06:57:57 +00:00
foreach ( $ptr as $vurl ) {
2021-04-27 04:32:37 +00:00
if ( is_array ( $vurl ) && isset ( $vurl [ 'href' ]) && strpos ( $s [ 'body' ], $vurl [ 'href' ]) === false ) {
2018-10-14 22:47:57 +00:00
$s [ 'body' ] .= " \n \n " . '[zmg]' . $vurl [ 'href' ] . '[/zmg]' ;
break ;
}
2018-09-26 06:50:35 +00:00
}
}
2019-05-13 06:57:57 +00:00
elseif ( is_string ( $act -> obj [ 'url' ])) {
if ( strpos ( $s [ 'body' ], $act -> obj [ 'url' ]) === false ) {
2018-10-14 22:47:57 +00:00
$s [ 'body' ] .= " \n \n " . '[zmg]' . $act -> obj [ 'url' ] . '[/zmg]' ;
}
2018-09-20 05:44:22 +00:00
}
}
}
2018-11-01 23:53:45 +00:00
2019-05-13 06:57:57 +00:00
if ( $act -> obj [ 'type' ] === 'Page' && ! $s [ 'body' ]) {
2018-11-01 23:53:45 +00:00
2018-11-14 01:23:09 +00:00
$ptr = null ;
$purl = EMPTY_STR ;
2018-11-01 23:53:45 +00:00
2019-05-13 06:57:57 +00:00
if ( array_key_exists ( 'url' , $act -> obj )) {
if ( is_array ( $act -> obj [ 'url' ])) {
if ( array_key_exists ( 0 , $act -> obj [ 'url' ])) {
2018-11-01 23:53:45 +00:00
$ptr = $act -> obj [ 'url' ];
}
else {
$ptr = [ $act -> obj [ 'url' ] ];
}
2019-05-13 06:57:57 +00:00
foreach ( $ptr as $vurl ) {
2021-04-27 04:32:37 +00:00
if ( is_array ( $vurl ) && array_key_exists ( 'mediaType' , $vurl ) && $vurl [ 'mediaType' ] === 'text/html' ) {
2018-11-14 01:23:09 +00:00
$purl = $vurl [ 'href' ];
2018-11-01 23:53:45 +00:00
break ;
}
2019-05-13 06:57:57 +00:00
elseif ( array_key_exists ( 'mimeType' , $vurl ) && $vurl [ 'mimeType' ] === 'text/html' ) {
2018-11-14 01:23:09 +00:00
$purl = $vurl [ 'href' ];
2018-11-13 03:38:21 +00:00
break ;
}
2018-11-01 23:53:45 +00:00
}
}
2019-05-13 06:57:57 +00:00
elseif ( is_string ( $act -> obj [ 'url' ])) {
2018-11-14 01:23:09 +00:00
$purl = $act -> obj [ 'url' ];
}
2019-05-13 06:57:57 +00:00
if ( $purl ) {
2018-11-14 01:23:09 +00:00
$li = z_fetch_url ( z_root () . '/linkinfo?binurl=' . bin2hex ( $purl ));
2019-05-13 06:57:57 +00:00
if ( $li [ 'success' ] && $li [ 'body' ]) {
2018-11-14 01:23:09 +00:00
$s [ 'body' ] .= " \n " . $li [ 'body' ];
}
else {
$s [ 'body' ] .= " \n \n " . $purl ;
}
2018-11-01 23:53:45 +00:00
}
}
}
2018-09-20 05:44:22 +00:00
}
2018-10-14 22:47:57 +00:00
2019-05-13 06:57:57 +00:00
if ( in_array ( $act -> obj [ 'type' ],[ 'Note' , 'Article' , 'Page' ])) {
2018-09-26 06:50:35 +00:00
$ptr = null ;
2019-05-13 06:57:57 +00:00
if ( array_key_exists ( 'url' , $act -> obj )) {
if ( is_array ( $act -> obj [ 'url' ])) {
if ( array_key_exists ( 0 , $act -> obj [ 'url' ])) {
2018-09-26 06:50:35 +00:00
$ptr = $act -> obj [ 'url' ];
}
else {
$ptr = [ $act -> obj [ 'url' ] ];
}
2019-05-13 06:57:57 +00:00
foreach ( $ptr as $vurl ) {
2021-04-27 04:32:37 +00:00
if ( is_array ( $vurl ) && array_key_exists ( 'mediaType' , $vurl ) && $vurl [ 'mediaType' ] === 'text/html' ) {
2018-09-26 06:50:35 +00:00
$s [ 'plink' ] = $vurl [ 'href' ];
break ;
}
}
}
2019-05-13 06:57:57 +00:00
elseif ( is_string ( $act -> obj [ 'url' ])) {
2018-09-26 06:50:35 +00:00
$s [ 'plink' ] = $act -> obj [ 'url' ];
}
}
}
2018-09-20 05:44:22 +00:00
2019-05-13 06:57:57 +00:00
if ( ! $s [ 'plink' ]) {
2018-09-26 06:50:35 +00:00
$s [ 'plink' ] = $s [ 'mid' ];
}
2018-09-20 05:44:22 +00:00
2020-04-17 22:37:50 +00:00
// assume this is private unless specifically told otherwise.
2018-05-30 06:30:18 +00:00
2020-04-17 22:37:50 +00:00
$s [ 'item_private' ] = 1 ;
2021-03-30 01:47:39 +00:00
if ( $act -> recips && ( in_array ( ACTIVITY_PUBLIC_INBOX , $act -> recips ) || in_array ( 'Public' , $act -> recips ) || in_array ( 'as:Public' , $act -> recips ))) {
2020-04-17 22:37:50 +00:00
$s [ 'item_private' ] = 0 ;
}
2019-06-19 02:21:31 +00:00
2019-10-11 23:45:04 +00:00
if ( is_array ( $act -> obj )) {
if ( array_key_exists ( 'directMessage' , $act -> obj ) && intval ( $act -> obj [ 'directMessage' ])) {
$s [ 'item_private' ] = 2 ;
}
2019-06-19 02:21:31 +00:00
}
2019-10-11 23:45:04 +00:00
2018-05-30 06:30:18 +00:00
set_iconfig ( $s , 'activitypub' , 'recips' , $act -> raw_recips );
2020-01-17 01:22:32 +00:00
if ( array_key_exists ( 'directMessage' , $act -> data ) && intval ( $act -> data [ 'directMessage' ])) {
$s [ 'item_private' ] = 2 ;
}
2019-05-13 06:57:57 +00:00
if ( $parent ) {
2018-05-30 06:30:18 +00:00
set_iconfig ( $s , 'activitypub' , 'rawmsg' , $act -> raw , 1 );
}
2020-06-02 04:00:21 +00:00
2020-03-26 22:41:09 +00:00
// Restrict html caching to ActivityPub senders.
// Zot has dynamic content and this library is used by both.
2020-03-26 08:17:22 +00:00
2020-03-26 22:41:09 +00:00
if ( $cacheable ) {
2021-03-08 05:32:28 +00:00
if (( ! array_key_exists ( 'mimetype' , $s )) || ( in_array ( $s [ 'mimetype' ], [ 'text/bbcode' , 'text/x-multicode' ]))) {
2021-01-22 02:36:44 +00:00
// preserve the original purified HTML content *unless* we've modified $s['body']
// within this function (to add attachments or reaction descriptions or mention rewrites).
// This avoids/bypasses some markdown rendering issues which can occur when
// converting to our markdown-enhanced bbcode and then back to HTML again.
// Also if we do need bbcode, use the 'bbonly' flag to ignore markdown and only
// interpret bbcode; which is much less susceptible to false positives in the
// conversion regexes.
if ( $s [ 'body' ] === self :: bb_content ( $content , 'content' )) {
$s [ 'html' ] = $content [ 'content' ];
}
else {
$s [ 'html' ] = bbcode ( $s [ 'body' ], [ 'bbonly' => true ]);
}
2020-03-26 08:17:22 +00:00
}
2020-03-25 20:05:56 +00:00
}
2020-03-26 08:17:22 +00:00
2019-09-22 16:16:07 +00:00
$hookinfo = [
'act' => $act ,
's' => $s
];
call_hooks ( 'decode_note' , $hookinfo );
$s = $hookinfo [ 's' ];
2018-05-30 06:30:18 +00:00
return $s ;
}
2021-02-05 04:35:51 +00:00
static function rewrite_mentions_sub ( & $s , $pref , & $obj = null ) {
2020-06-02 05:36:55 +00:00
2021-03-09 02:41:29 +00:00
if ( isset ( $s [ 'term' ]) && is_array ( $s [ 'term' ])) {
2020-06-02 04:00:21 +00:00
foreach ( $s [ 'term' ] as $tag ) {
$txt = EMPTY_STR ;
if ( intval ( $tag [ 'ttype' ]) === TERM_MENTION ) {
2020-06-02 05:36:55 +00:00
// some platforms put the identity url into href rather than the profile url. Accept either form.
$x = q ( " select * from xchan where xchan_url = '%s' or xchan_hash = '%s' limit 1 " ,
dbesc ( $tag [ 'url' ]),
2020-06-02 08:35:09 +00:00
dbesc ( $tag [ 'url' ])
2020-06-02 04:00:21 +00:00
);
if ( $x ) {
switch ( $pref ) {
case 0 :
$txt = $x [ 0 ][ 'xchan_name' ];
break ;
case 1 :
$txt = (( $x [ 0 ][ 'xchan_addr' ]) ? $x [ 0 ][ 'xchan_addr' ] : $x [ 0 ][ 'xchan_name' ]);
break ;
case 2 :
default ;
if ( $x [ 0 ][ 'xchan_addr' ]) {
$txt = sprintf ( t ( '%1$s (%2$s)' ), $x [ 0 ][ 'xchan_name' ], $x [ 0 ][ 'xchan_addr' ]);
}
else {
$txt = $x [ 0 ][ 'xchan_name' ];
}
break ;
}
}
}
if ( $txt ) {
2020-11-20 01:42:55 +00:00
// the Markdown filter will get tripped up and think this is a markdown link
// if $txt begins with parens so put it behind a zero-width space
if ( substr ( $txt , 0 , 1 ) === '(' ) {
$txt = htmlspecialchars_decode ( '​' , ENT_QUOTES ) . $txt ;
}
2020-06-02 05:43:33 +00:00
$s [ 'body' ] = preg_replace ( '/\@\[zrl\=' . preg_quote ( $x [ 0 ][ 'xchan_url' ], '/' ) . '\](.*?)\[\/zrl\]/ism' ,
'@[zrl=' . $x [ 0 ][ 'xchan_url' ] . ']' . $txt . '[/zrl]' , $s [ 'body' ]);
$s [ 'body' ] = preg_replace ( '/\@\[url\=' . preg_quote ( $x [ 0 ][ 'xchan_url' ], '/' ) . '\](.*?)\[\/url\]/ism' ,
'@[url=' . $x [ 0 ][ 'xchan_url' ] . ']' . $txt . '[/url]' , $s [ 'body' ]);
$s [ 'body' ] = preg_replace ( '/\[zrl\=' . preg_quote ( $x [ 0 ][ 'xchan_url' ], '/' ) . '\]@(.*?)\[\/zrl\]/ism' ,
2020-06-02 10:46:11 +00:00
'@[zrl=' . $x [ 0 ][ 'xchan_url' ] . ']' . $txt . '[/zrl]' , $s [ 'body' ]);
2020-06-02 05:43:33 +00:00
$s [ 'body' ] = preg_replace ( '/\[url\=' . preg_quote ( $x [ 0 ][ 'xchan_url' ], '/' ) . '\]@(.*?)\[\/url\]/ism' ,
2020-06-02 10:46:11 +00:00
'@[url=' . $x [ 0 ][ 'xchan_url' ] . ']' . $txt . '[/url]' , $s [ 'body' ]);
2020-11-15 00:13:23 +00:00
// replace these just in case the sender (in this case Friendica) got it wrong
$s [ 'body' ] = preg_replace ( '/\@\[zrl\=' . preg_quote ( $x [ 0 ][ 'xchan_hash' ], '/' ) . '\](.*?)\[\/zrl\]/ism' ,
'@[zrl=' . $x [ 0 ][ 'xchan_url' ] . ']' . $txt . '[/zrl]' , $s [ 'body' ]);
$s [ 'body' ] = preg_replace ( '/\@\[url\=' . preg_quote ( $x [ 0 ][ 'xchan_hash' ], '/' ) . '\](.*?)\[\/url\]/ism' ,
'@[url=' . $x [ 0 ][ 'xchan_url' ] . ']' . $txt . '[/url]' , $s [ 'body' ]);
$s [ 'body' ] = preg_replace ( '/\[zrl\=' . preg_quote ( $x [ 0 ][ 'xchan_hash' ], '/' ) . '\]@(.*?)\[\/zrl\]/ism' ,
'@[zrl=' . $x [ 0 ][ 'xchan_url' ] . ']' . $txt . '[/zrl]' , $s [ 'body' ]);
$s [ 'body' ] = preg_replace ( '/\[url\=' . preg_quote ( $x [ 0 ][ 'xchan_hash' ], '/' ) . '\]@(.*?)\[\/url\]/ism' ,
'@[url=' . $x [ 0 ][ 'xchan_url' ] . ']' . $txt . '[/url]' , $s [ 'body' ]);
2021-02-05 04:35:51 +00:00
2021-02-05 05:24:27 +00:00
if ( $obj && $txt ) {
2021-02-05 04:35:51 +00:00
if ( ! is_array ( $obj )) {
$obj = json_decode ( $obj , true );
}
if ( array_path_exists ( 'source/content' , $obj )) {
$obj [ 'source' ][ 'content' ] = preg_replace ( '/\@\[zrl\=' . preg_quote ( $x [ 0 ][ 'xchan_url' ], '/' ) . '\](.*?)\[\/zrl\]/ism' ,
'@[zrl=' . $x [ 0 ][ 'xchan_url' ] . ']' . $txt . '[/zrl]' , $obj [ 'source' ][ 'content' ]);
$obj [ 'source' ][ 'content' ] = preg_replace ( '/\@\[url\=' . preg_quote ( $x [ 0 ][ 'xchan_url' ], '/' ) . '\](.*?)\[\/url\]/ism' ,
'@[url=' . $x [ 0 ][ 'xchan_url' ] . ']' . $txt . '[/url]' , $obj [ 'source' ][ 'content' ]);
}
$obj [ 'content' ] = preg_replace ( '/\@(.*?)\<a (.*?)href\=\"' . preg_quote ( $x [ 0 ][ 'xchan_url' ], '/' ) . '\"(.*?)\>(.*?)\<\/a\>/ism' ,
'@$1<a $2 href="' . $x [ 0 ][ 'xchan_url' ] . '"$3>' . $txt . '</a>' , $obj [ 'content' ]);
}
2020-06-02 04:00:21 +00:00
}
}
}
2021-02-05 00:35:18 +00:00
2020-06-02 04:00:21 +00:00
// $s['html'] will be populated if caching was enabled.
// This is usually the case for ActivityPub sourced content, while Zot6 content is not cached.
if ( $s [ 'html' ]) {
2021-02-05 00:09:59 +00:00
$s [ 'html' ] = bbcode ( $s [ 'body' ], [ 'bbonly' => true ] );
2020-06-02 04:00:21 +00:00
}
return ;
}
2018-05-30 06:30:18 +00:00
2021-02-05 00:35:18 +00:00
static function rewrite_mentions ( & $s ) {
// rewrite incoming mentions in accordance with system.tag_username setting
// 0 - displayname
// 1 - username
// 2 - displayname (username)
// 127 - default
$pref = intval ( PConfig :: Get ( $s [ 'uid' ], 'system' , 'tag_username' , Config :: Get ( 'system' , 'tag_username' , false )));
if ( $pref === 127 ) {
return ;
}
self :: rewrite_mentions_sub ( $s , $pref );
return ;
}
2021-01-24 22:40:40 +00:00
// $force is used when manually fetching a remote item - it assumes you are granting one-time
// permission for the selected item/conversation regardless of your relationship with the author and
// assumes that you are in fact the sender. Please do not use it for anything else. The only permission
// checking that is performed is that the author isn't blocked by the site admin.
2020-11-28 10:18:14 +00:00
static function store ( $channel , $observer_hash , $act , $item , $fetch_parents = true , $force = false ) {
2018-08-20 03:39:23 +00:00
2020-11-28 10:18:14 +00:00
if ( $act && $act -> implied_create && ! $force ) {
2019-12-13 00:31:13 +00:00
// This is originally a S2S object with no associated activity
logger ( 'Not storing implied create activity!' );
return ;
}
2018-08-20 03:39:23 +00:00
$is_sys_channel = is_sys_channel ( $channel [ 'channel_id' ]);
2018-11-19 05:53:09 +00:00
$is_child_node = false ;
2018-08-20 03:39:23 +00:00
2020-09-21 02:49:41 +00:00
// Pleroma scrobbles can be really noisy and contain lots of duplicate activities. Disable them by default.
if (( $act -> type === 'Listen' ) && ( $is_sys_channel || get_pconfig ( $channel [ 'channel_id' ], 'system' , 'allow_scrobbles' , false ))) {
return ;
}
2018-08-20 03:39:23 +00:00
// Mastodon only allows visibility in public timelines if the public inbox is listed in the 'to' field.
// They are hidden in the public timeline if the public inbox is listed in the 'cc' field.
// This is not part of the activitypub protocol - we might change this to show all public posts in pubstream at some point.
2021-03-30 01:47:39 +00:00
$pubstream = (( is_array ( $act -> obj ) && array_key_exists ( 'to' , $act -> obj ) && is_array ( $act -> obj [ 'to' ]) && ( in_array ( ACTIVITY_PUBLIC_INBOX , $act -> obj [ 'to' ]) || in_array ( 'Public' , $act -> obj [ 'to' ]) || in_array ( 'as:Public' , $act -> obj [ 'to' ]))) ? true : false );
2018-08-20 03:39:23 +00:00
2019-10-11 23:45:04 +00:00
// very unpleasant and imperfect way of determining a Mastodon DM
2019-10-20 22:24:03 +00:00
if ( $act -> raw_recips && array_key_exists ( 'to' , $act -> raw_recips ) && is_array ( $act -> raw_recips [ 'to' ]) && count ( $act -> raw_recips [ 'to' ]) === 1 && $act -> raw_recips [ 'to' ][ 0 ] === channel_url ( $channel ) && ! $act -> raw_recips [ 'cc' ]) {
2019-10-11 23:45:04 +00:00
$item [ 'item_private' ] = 2 ;
}
2019-05-13 06:57:57 +00:00
if ( $item [ 'parent_mid' ] && $item [ 'parent_mid' ] !== $item [ 'mid' ]) {
2018-11-19 05:53:09 +00:00
$is_child_node = true ;
}
$allowed = false ;
2021-01-27 01:03:43 +00:00
$reason = [ 'init' ];
2020-07-04 02:36:46 +00:00
$permit_mentions = intval ( PConfig :: Get ( $channel [ 'channel_id' ], 'system' , 'permit_all_mentions' ) && i_am_mentioned ( $channel , $item ));
2021-04-08 06:22:10 +00:00
2019-09-22 06:18:20 +00:00
if ( $is_child_node ) {
2020-06-18 06:48:18 +00:00
$p = q ( " select * from item where mid = '%s' and uid = %d and item_wall = 1 " ,
2019-09-21 22:26:55 +00:00
dbesc ( $item [ 'parent_mid' ]),
intval ( $channel [ 'channel_id' ])
);
if ( $p ) {
2021-01-24 04:02:40 +00:00
// set the owner to the owner of the parent
$item [ 'owner_xchan' ] = $p [ 0 ][ 'owner_xchan' ];
2021-06-23 23:56:49 +00:00
// quietly reject group comment boosts by group owner
// (usually only sent via ActivityPub so groups will work on microblog platforms)
// This catches those activities if they slipped in via a conversation fetch
if ( $p [ 0 ][ 'parent_mid' ] !== $item [ 'parent_mid' ]) {
if ( $item [ 'verb' ] === 'Announce' && $item [ 'author_xchan' ] === $item [ 'owner_xchan' ]) {
logger ( 'group boost activity by group owner rejected' );
return ;
}
}
2019-09-30 02:20:09 +00:00
// check permissions against the author, not the sender
$allowed = perm_is_allowed ( $channel [ 'channel_id' ], $item [ 'author_xchan' ], 'post_comments' );
2021-01-27 01:03:43 +00:00
if ( ! $allowed ) {
$reason [] = 'post_comments perm' ;
}
2020-07-04 02:36:46 +00:00
if (( ! $allowed ) && $permit_mentions ) {
if ( $p [ 0 ][ 'owner_xchan' ] === $channel [ 'channel_hash' ]) {
2020-07-03 22:48:58 +00:00
$allowed = false ;
2021-01-27 01:03:43 +00:00
$reason [] = 'ownership' ;
2020-07-03 22:48:58 +00:00
}
else {
$allowed = true ;
}
2020-02-12 05:52:52 +00:00
}
2020-06-18 03:30:18 +00:00
if ( absolutely_no_comments ( $p [ 0 ])) {
$allowed = false ;
2021-01-27 01:03:43 +00:00
$reason [] = 'absolutely' ;
2020-06-18 03:30:18 +00:00
}
2021-06-23 23:56:49 +00:00
2019-09-21 22:26:55 +00:00
if ( ! $allowed ) {
2019-09-30 02:20:09 +00:00
logger ( 'rejected comment from ' . $item [ 'author_xchan' ] . ' for ' . $channel [ 'channel_address' ]);
2021-01-27 01:03:43 +00:00
logger ( 'rejected reason ' . print_r ( $reason , true ));
2019-09-26 01:39:37 +00:00
logger ( 'rejected: ' . print_r ( $item , true ), LOGGER_DATA );
2019-09-22 06:18:20 +00:00
// let the sender know we received their comment but we don't permit spam here.
2019-09-30 02:20:09 +00:00
self :: send_rejection_activity ( $channel , $item [ 'author_xchan' ], $item );
return ;
2019-09-21 22:26:55 +00:00
}
2019-11-03 23:07:49 +00:00
if ( perm_is_allowed ( $channel [ 'channel_id' ], $item [ 'author_xchan' ], 'moderated' )) {
$item [ 'item_blocked' ] = ITEM_MODERATED ;
}
2019-09-21 22:26:55 +00:00
}
else {
2021-06-17 23:17:31 +00:00
// By default if we allow you to send_stream and comments and this is a comment, it is allowed.
// A side effect of this action is that if you take away send_stream permission, comments to those
// posts you previously allowed will still be accepted. It is possible but might be difficult to fix this.
2019-09-21 22:26:55 +00:00
$allowed = true ;
2021-01-24 22:40:40 +00:00
2019-09-22 06:18:20 +00:00
// reject public stream comments that weren't sent by the conversation owner
2021-01-24 22:40:40 +00:00
// but only on remote message deliveries to our site ($fetch_parents === true)
2021-01-24 04:02:40 +00:00
if ( $is_sys_channel && $pubstream && $item [ 'owner_xchan' ] !== $observer_hash && ! $fetch_parents ) {
2019-09-22 06:18:20 +00:00
$allowed = false ;
2021-01-27 01:03:43 +00:00
$reason [] = 'sender ' . $observer_hash . ' not owner ' . $item [ 'owner_xchan' ];
2019-09-22 06:18:20 +00:00
}
2019-09-21 22:26:55 +00:00
}
2020-02-07 02:50:18 +00:00
if ( $p && $p [ 0 ][ 'obj_type' ] === 'Question' ) {
2020-02-10 01:29:04 +00:00
if ( $item [ 'obj_type' ] === 'Note' && $item [ 'title' ] && ( ! $item [ 'content' ])) {
2020-02-07 02:50:18 +00:00
$item [ 'obj_type' ] = 'Answer' ;
}
}
2018-11-19 05:53:09 +00:00
}
2020-07-04 02:36:46 +00:00
else {
if ( perm_is_allowed ( $channel [ 'channel_id' ], $observer_hash , 'send_stream' ) || ( $is_sys_channel && $pubstream )) {
$allowed = true ;
}
if ( $permit_mentions ) {
$allowed = true ;
}
2018-11-19 05:53:09 +00:00
}
2018-11-22 01:46:08 +00:00
if ( tgroup_check ( $channel [ 'channel_id' ], $item ) && ( ! $is_child_node )) {
// for forum deliveries, make sure we keep a copy of the signed original
set_iconfig ( $item , 'activitypub' , 'rawmsg' , $act -> raw , 1 );
$allowed = true ;
}
2021-04-08 06:22:10 +00:00
if ( get_abconfig ( $channel [ 'channel_id' ], $observer_hash , 'system' , 'block_announce' , false )) {
if ( $item [ 'verb' ] === 'Announce' || strpos ( $item [ 'body' ], '[/share]' )) {
$allowed = false ;
}
}
2021-01-23 00:39:55 +00:00
if ( $is_sys_channel ) {
2019-01-05 22:47:21 +00:00
2021-03-10 01:10:28 +00:00
if ( ! $pubstream ) {
$allowed = false ;
$reason [] = 'unlisted post delivered to sys channel' ;
}
2019-05-13 06:57:57 +00:00
if ( ! check_pubstream_channelallowed ( $observer_hash )) {
2019-01-05 22:47:21 +00:00
$allowed = false ;
2021-03-10 01:10:28 +00:00
$reason [] = 'pubstream channel blocked' ;
2019-01-05 22:47:21 +00:00
}
2020-07-01 00:47:31 +00:00
2020-06-14 11:58:27 +00:00
// don't allow pubstream posts if the sender even has a clone on a pubstream denied site
2019-01-05 22:47:21 +00:00
$h = q ( " select hubloc_url from hubloc where hubloc_hash = '%s' " ,
dbesc ( $observer_hash )
);
2019-05-13 06:57:57 +00:00
if ( $h ) {
foreach ( $h as $hub ) {
if ( ! check_pubstream_siteallowed ( $hub [ 'hubloc_url' ])) {
2019-01-05 22:47:21 +00:00
$allowed = false ;
2021-03-10 01:10:28 +00:00
$reason = 'pubstream site blocked' ;
2019-01-05 22:47:21 +00:00
break ;
}
}
}
2021-01-23 00:39:55 +00:00
if ( intval ( $item [ 'item_private' ])) {
$allowed = false ;
2021-01-27 01:03:43 +00:00
$reason [] = 'private item' ;
2021-01-23 00:39:55 +00:00
}
2019-01-05 22:47:21 +00:00
}
2018-11-22 01:46:08 +00:00
2020-04-17 11:08:04 +00:00
$blocked = LibBlock :: fetch ( $channel [ 'channel_id' ], BLOCKTYPE_SERVER );
if ( $blocked ) {
foreach ( $blocked as $b ) {
if ( strpos ( $observer_hash , $b [ 'block_entity' ]) !== false ) {
$allowed = false ;
2021-01-27 01:03:43 +00:00
$reason [] = 'blocked' ;
2020-04-17 11:08:04 +00:00
}
}
}
2020-11-28 10:18:14 +00:00
if ( ! $allowed && ! $force ) {
2021-01-25 19:45:43 +00:00
logger ( 'no permission: channel ' . $channel [ 'channel_address' ] . ', id = ' . $item [ 'mid' ]);
2021-01-27 01:03:43 +00:00
logger ( 'no permission: reason ' . print_r ( $reason , true ));
2018-08-20 03:39:23 +00:00
return ;
}
$item [ 'aid' ] = $channel [ 'channel_account_id' ];
$item [ 'uid' ] = $channel [ 'channel_id' ];
2018-12-12 04:30:47 +00:00
// Some authors may be zot6 authors in which case we want to store their nomadic identity
// instead of their ActivityPub identity
$item [ 'author_xchan' ] = self :: find_best_identity ( $item [ 'author_xchan' ]);
$item [ 'owner_xchan' ] = self :: find_best_identity ( $item [ 'owner_xchan' ]);
2019-05-13 06:57:57 +00:00
if ( ! ( $item [ 'author_xchan' ] && $item [ 'owner_xchan' ])) {
2018-09-04 04:30:39 +00:00
logger ( 'owner or author missing.' );
return ;
}
2019-05-13 06:57:57 +00:00
if ( $channel [ 'channel_system' ]) {
if ( ! MessageFilter :: evaluate ( $item , get_config ( 'system' , 'pubstream_incl' ), get_config ( 'system' , 'pubstream_excl' ))) {
2018-08-20 03:39:23 +00:00
logger ( 'post is filtered' );
return ;
}
}
$abook = q ( " select * from abook where abook_xchan = '%s' and abook_channel = %d limit 1 " ,
dbesc ( $observer_hash ),
intval ( $channel [ 'channel_id' ])
);
2019-05-13 06:57:57 +00:00
if ( ! post_is_importable ( $channel [ 'channel_id' ], $item , $abook [ 0 ])) {
2018-12-11 23:57:15 +00:00
logger ( 'post is filtered' );
return ;
}
2018-08-31 01:50:21 +00:00
2021-04-22 21:41:26 +00:00
$maxlen = get_max_import_size ();
if ( $maxlen && mb_strlen ( $item [ 'body' ]) > $maxlen ) {
$item [ 'body' ] = mb_substr ( $item [ 'body' ], 0 , $maxlen , 'UTF-8' );
logger ( 'message length exceeds max_import_size: truncated' );
}
if ( $maxlen && mb_strlen ( $item [ 'summary' ]) > $maxlen ) {
$item [ 'summary' ] = mb_substr ( $item [ 'summary' ], 0 , $maxlen , 'UTF-8' );
logger ( 'message summary length exceeds max_import_size: truncated' );
}
2021-04-17 06:53:35 +00:00
if ( $act -> obj [ 'context' ]) {
set_iconfig ( $item , 'activitypub' , 'context' , $act -> obj [ 'context' ], 1 );
}
2019-05-13 06:57:57 +00:00
if ( $act -> obj [ 'conversation' ]) {
2018-08-20 03:39:23 +00:00
set_iconfig ( $item , 'ostatus' , 'conversation' , $act -> obj [ 'conversation' ], 1 );
}
set_iconfig ( $item , 'activitypub' , 'recips' , $act -> raw_recips );
2019-05-13 06:57:57 +00:00
if ( ! ( isset ( $act -> data [ 'inheritPrivacy' ]) && $act -> data [ 'inheritPrivacy' ])) {
if ( $item [ 'item_private' ]) {
2018-11-21 02:55:33 +00:00
$item [ 'item_restrict' ] = $item [ 'item_restrict' ] & 1 ;
2019-05-13 06:57:57 +00:00
if ( $is_child_node ) {
2018-11-20 01:30:22 +00:00
$item [ 'allow_cid' ] = '<' . $channel [ 'channel_hash' ] . '>' ;
$item [ 'allow_gid' ] = $item [ 'deny_cid' ] = $item [ 'deny_gid' ] = '' ;
}
2018-11-19 05:53:09 +00:00
logger ( 'restricted' );
}
}
2019-05-13 06:57:57 +00:00
if ( intval ( $act -> sigok )) {
2018-11-27 00:16:07 +00:00
$item [ 'item_verified' ] = 1 ;
}
2020-09-23 05:27:41 +00:00
$parent = null ;
2019-05-13 06:57:57 +00:00
if ( $is_child_node ) {
2018-11-19 05:53:09 +00:00
2020-10-21 05:18:30 +00:00
$parent = q ( " select * from item where mid = '%s' and uid = %d limit 1 " ,
2018-08-31 01:50:21 +00:00
dbesc ( $item [ 'parent_mid' ]),
intval ( $item [ 'uid' ])
);
2020-09-23 05:27:41 +00:00
if ( ! $parent ) {
2020-11-27 00:17:07 +00:00
if ( ! get_config ( 'system' , 'activitypub' , ACTIVITYPUB_ENABLED )) {
2019-06-18 02:10:30 +00:00
return ;
}
else {
2020-09-23 05:27:41 +00:00
$fetch = false ;
2021-01-22 02:36:44 +00:00
if ( intval ( $channel [ 'channel_system' ]) || ( perm_is_allowed ( $channel [ 'channel_id' ], $observer_hash , 'send_stream' ) && ( PConfig :: Get ( $channel [ 'channel_id' ], 'system' , 'hyperdrive' , true ) || $act -> type === 'Announce' ))) {
2020-09-23 05:27:41 +00:00
$fetch = (( $fetch_parents ) ? self :: fetch_and_store_parents ( $channel , $observer_hash , $act , $item ) : false );
2019-06-18 02:10:30 +00:00
}
2020-09-23 05:27:41 +00:00
if ( $fetch ) {
2020-10-21 05:18:30 +00:00
$parent = q ( " select * from item where mid = '%s' and uid = %d limit 1 " ,
2019-06-18 02:10:30 +00:00
dbesc ( $item [ 'parent_mid' ]),
intval ( $item [ 'uid' ])
);
}
else {
2020-09-23 05:27:41 +00:00
logger ( 'no parent' );
return ;
2019-06-18 02:10:30 +00:00
}
}
2018-08-31 01:50:21 +00:00
}
2021-04-14 01:12:15 +00:00
$item [ 'comment_policy' ] = $parent [ 0 ][ 'comment_policy' ];
$item [ 'item_nocomment' ] = $parent [ 0 ][ 'item_nocomment' ];
$item [ 'comments_closed' ] = $parent [ 0 ][ 'comments_closed' ];
2019-06-18 02:04:06 +00:00
2020-09-23 05:27:41 +00:00
if ( $parent [ 0 ][ 'parent_mid' ] !== $item [ 'parent_mid' ]) {
2018-08-31 01:50:21 +00:00
$item [ 'thr_parent' ] = $item [ 'parent_mid' ];
}
else {
2020-09-23 05:27:41 +00:00
$item [ 'thr_parent' ] = $parent [ 0 ][ 'parent_mid' ];
2018-08-31 01:50:21 +00:00
}
2020-09-23 05:27:41 +00:00
$item [ 'parent_mid' ] = $parent [ 0 ][ 'parent_mid' ];
2018-08-31 01:50:21 +00:00
}
2020-06-02 04:00:21 +00:00
self :: rewrite_mentions ( $item );
2018-09-04 11:06:50 +00:00
$r = q ( " select id, created, edited from item where mid = '%s' and uid = %d limit 1 " ,
2018-08-20 03:39:23 +00:00
dbesc ( $item [ 'mid' ]),
intval ( $item [ 'uid' ])
);
2019-05-13 06:57:57 +00:00
if ( $r ) {
if ( $item [ 'edited' ] > $r [ 0 ][ 'edited' ]) {
2018-09-04 11:06:50 +00:00
$item [ 'id' ] = $r [ 0 ][ 'id' ];
2018-08-20 03:39:23 +00:00
$x = item_store_update ( $item );
}
else {
return ;
}
}
else {
$x = item_store ( $item );
}
2021-06-17 23:17:31 +00:00
// experimental code that needs more work. What this did was once we fetched a conversation to find the root node,
// start at that root node and fetch children so you get all the branches and not just the branch related to the current node.
2021-06-17 23:18:50 +00:00
// Unfortunately there is no standard method for achieving this. Mastodon provides a 'replies' collection and Nomad projects
2021-06-17 23:17:31 +00:00
// can fetch the 'context'. For other platforms it's a wild guess. Additionally when we tested this, it started an infinite
// recursion and has been disabled until the recursive behaviour is tracked down and fixed.
2021-01-26 19:25:21 +00:00
// if ($fetch_parents && $parent && ! intval($parent[0]['item_private'])) {
// logger('topfetch', LOGGER_DEBUG);
// // if the thread owner is a connnection, we will already receive any additional comments to their posts
// // but if they are not we can try to fetch others in the background
// $x = q("SELECT abook.*, xchan.* FROM abook left join xchan on abook_xchan = xchan_hash
// WHERE abook_channel = %d and abook_xchan = '%s' LIMIT 1",
// intval($channel['channel_id']),
// dbesc($parent[0]['owner_xchan'])
// );
// if (! $x) {
// // determine if the top-level post provides a replies collection
// if ($parent[0]['obj']) {
// $parent[0]['obj'] = json_decode($parent[0]['obj'],true);
// }
// logger('topfetch: ' . print_r($parent[0],true), LOGGER_ALL);
// $id = ((array_path_exists('obj/replies/id',$parent[0])) ? $parent[0]['obj']['replies']['id'] : false);
// if (! $id) {
// $id = ((array_path_exists('obj/replies',$parent[0]) && is_string($parent[0]['obj']['replies'])) ? $parent[0]['obj']['replies'] : false);
// }
// if ($id) {
// Run::Summon( [ 'Convo', $id, $channel['channel_id'], $observer_hash ] );
// }
// }
// }
2018-08-20 03:39:23 +00:00
2019-05-13 06:57:57 +00:00
if ( is_array ( $x ) && $x [ 'item_id' ]) {
if ( $is_child_node ) {
if ( $item [ 'owner_xchan' ] === $channel [ 'channel_hash' ]) {
2018-08-20 03:39:23 +00:00
// We are the owner of this conversation, so send all received comments back downstream
2021-04-13 03:49:58 +00:00
Run :: Summon ( [ 'Notifier' , 'comment-import' , $x [ 'item_id' ] ] );
2018-08-20 03:39:23 +00:00
}
$r = q ( " select * from item where id = %d limit 1 " ,
intval ( $x [ 'item_id' ])
);
2019-05-13 06:57:57 +00:00
if ( $r ) {
2018-08-20 03:39:23 +00:00
send_status_notifications ( $x [ 'item_id' ], $r [ 0 ]);
}
}
sync_an_item ( $channel [ 'channel_id' ], $x [ 'item_id' ]);
}
}
2018-12-12 04:30:47 +00:00
static public function find_best_identity ( $xchan ) {
$r = q ( " select hubloc_hash from hubloc where hubloc_id_url = '%s' limit 1 " ,
dbesc ( $xchan )
);
2019-05-13 06:57:57 +00:00
if ( $r ) {
2018-12-12 04:30:47 +00:00
return $r [ 0 ][ 'hubloc_hash' ];
}
return $xchan ;
}
2018-09-01 22:52:28 +00:00
static public function fetch_and_store_parents ( $channel , $observer_hash , $act , $item ) {
2018-09-04 01:22:31 +00:00
logger ( 'fetching parents' );
2018-09-01 22:52:28 +00:00
$p = [];
$current_act = $act ;
$current_item = $item ;
2018-08-20 03:39:23 +00:00
2021-01-23 00:39:55 +00:00
while ( $current_item [ 'parent_mid' ] !== $current_item [ 'mid' ]) {
2019-01-14 05:22:45 +00:00
$n = self :: fetch ( $current_item [ 'parent_mid' ]);
2019-05-13 06:57:57 +00:00
if ( ! $n ) {
2018-09-01 22:52:28 +00:00
break ;
}
2019-10-07 21:40:51 +00:00
// set client flag to convert objects to implied activities
$a = new ActivityStreams ( $n , null , true );
2019-09-30 02:20:09 +00:00
if ( $a -> type === 'Announce' && is_array ( $a -> obj )
&& array_key_exists ( 'object' , $a -> obj ) && array_key_exists ( 'actor' , $a -> obj )) {
// This is a relayed/forwarded Activity (as opposed to a shared/boosted object)
// Reparse the encapsulated Activity and use that instead
logger ( 'relayed activity' , LOGGER_DEBUG );
2019-10-07 21:40:51 +00:00
$a = new ActivityStreams ( $a -> obj , null , true );
2019-09-30 02:20:09 +00:00
}
2018-09-04 01:22:31 +00:00
2020-09-23 02:57:28 +00:00
logger ( $a -> debug (), LOGGER_DATA );
2018-09-04 01:22:31 +00:00
2019-05-13 06:57:57 +00:00
if ( ! $a -> is_valid ()) {
2019-10-07 21:40:51 +00:00
logger ( 'not a valid activity' );
2018-09-01 22:52:28 +00:00
break ;
}
2019-05-13 06:57:57 +00:00
if ( is_array ( $a -> actor ) && array_key_exists ( 'id' , $a -> actor )) {
2018-09-01 22:52:28 +00:00
Activity :: actor_store ( $a -> actor [ 'id' ], $a -> actor );
}
2020-03-26 22:41:09 +00:00
// ActivityPub sourced items are cacheable
$item = Activity :: decode_note ( $a , true );
2018-09-01 22:52:28 +00:00
2019-05-13 06:57:57 +00:00
if ( ! $item ) {
2018-09-01 22:52:28 +00:00
break ;
}
2019-09-22 16:16:07 +00:00
$hookinfo = [
2020-02-05 01:01:11 +00:00
'a' => $a ,
'item' => $item
2019-09-22 16:16:07 +00:00
];
call_hooks ( 'fetch_and_store' , $hookinfo );
$item = $hookinfo [ 'item' ];
2021-01-22 21:42:14 +00:00
if ( $item ) {
// don't leak any private conversations to the public stream
// even if they contain publicly addressed comments/reactions
2021-01-23 00:39:55 +00:00
if ( intval ( $channel [ 'channel_system' ]) && intval ( $item [ 'item_private' ])) {
2021-03-10 01:10:28 +00:00
logger ( 'private conversation ignored' );
2021-01-22 21:52:55 +00:00
$p = [];
2021-01-22 21:42:14 +00:00
break ;
}
2019-09-22 16:16:07 +00:00
2021-01-22 21:52:55 +00:00
if ( count ( $p ) > 100 ) {
2021-03-10 01:10:28 +00:00
logger ( 'Conversation overflow' );
2021-01-22 21:52:55 +00:00
$p = [];
break ;
}
2019-09-22 16:16:07 +00:00
array_unshift ( $p ,[ $a , $item ]);
2021-01-22 21:52:55 +00:00
if ( $item [ 'parent_mid' ] === $item [ 'mid' ]) {
2019-09-22 16:16:07 +00:00
break ;
}
2018-09-01 22:52:28 +00:00
}
$current_act = $a ;
$current_item = $item ;
}
2021-01-27 01:03:43 +00:00
2019-05-13 06:57:57 +00:00
if ( $p ) {
foreach ( $p as $pv ) {
if ( $pv [ 0 ] -> is_valid ()) {
2018-12-03 00:46:52 +00:00
Activity :: store ( $channel , $observer_hash , $pv [ 0 ], $pv [ 1 ], false );
}
2020-09-23 05:27:41 +00:00
}
2018-09-01 22:52:28 +00:00
return true ;
}
return false ;
}
2018-08-20 03:39:23 +00:00
2021-02-22 01:39:48 +00:00
2021-06-21 02:49:33 +00:00
// This function is designed to work with Zot attachments and item body
2021-02-22 01:39:48 +00:00
2018-10-11 04:36:55 +00:00
static function bb_attach ( $attach , $body ) {
2018-05-30 04:08:52 +00:00
$ret = false ;
2021-06-21 02:49:33 +00:00
if ( ! ( is_array ( $attach ) && $attach )) {
2021-02-22 00:40:14 +00:00
return EMPTY_STR ;
}
2019-05-13 06:57:57 +00:00
foreach ( $attach as $a ) {
2021-02-22 00:40:14 +00:00
if ( array_key_exists ( 'type' , $a ) && stripos ( $a [ 'type' ], 'image' ) !== false ) {
2019-05-13 06:57:57 +00:00
if ( self :: media_not_in_body ( $a [ 'href' ], $body )) {
2021-04-06 20:31:44 +00:00
if ( isset ( $a [ 'name' ]) && $a [ 'name' ]) {
$alt = htmlspecialchars ( $a [ 'name' ], ENT_QUOTES );
$ret .= " \n \n " . '[img alt="' . $alt . '"]' . $a [ 'href' ] . '[/img]' ;
}
else {
$ret .= " \n \n " . '[img]' . $a [ 'href' ] . '[/img]' ;
}
2018-10-11 04:36:55 +00:00
}
2018-05-30 04:08:52 +00:00
}
2021-02-22 01:39:48 +00:00
if ( array_key_exists ( 'type' , $a ) && stripos ( $a [ 'type' ], 'video' ) !== false ) {
2019-05-13 06:57:57 +00:00
if ( self :: media_not_in_body ( $a [ 'href' ], $body )) {
2018-10-11 04:36:55 +00:00
$ret .= " \n \n " . '[video]' . $a [ 'href' ] . '[/video]' ;
}
2018-05-30 04:08:52 +00:00
}
2021-02-22 01:39:48 +00:00
if ( array_key_exists ( 'type' , $a ) && stripos ( $a [ 'type' ], 'audio' ) !== false ) {
2019-05-13 06:57:57 +00:00
if ( self :: media_not_in_body ( $a [ 'href' ], $body )) {
2018-10-11 04:36:55 +00:00
$ret .= " \n \n " . '[audio]' . $a [ 'href' ] . '[/audio]' ;
}
2018-05-30 04:08:52 +00:00
}
}
return $ret ;
}
2018-10-11 23:54:31 +00:00
// check for the existence of existing media link in body
static function media_not_in_body ( $s , $body ) {
2021-06-21 02:47:30 +00:00
$s_alt = htmlspecialchars ( $s , ENT_QUOTES , 'UTF-8' );
2019-05-13 06:57:57 +00:00
if (( strpos ( $body , ']' . $s . '[/img]' ) === false ) &&
2018-10-20 09:03:31 +00:00
( strpos ( $body , ']' . $s . '[/zmg]' ) === false ) &&
2018-10-11 23:54:31 +00:00
( strpos ( $body , ']' . $s . '[/video]' ) === false ) &&
2020-05-26 06:43:40 +00:00
( strpos ( $body , ']' . $s . '[/zvideo]' ) === false ) &&
( strpos ( $body , ']' . $s . '[/audio]' ) === false ) &&
2021-06-21 02:47:30 +00:00
( strpos ( $body , ']' . $s . '[/zaudio]' ) === false ) &&
( strpos ( $body , ']' . $s_alt . '[/img]' ) === false ) &&
( strpos ( $body , ']' . $s_alt . '[/zmg]' ) === false ) &&
( strpos ( $body , ']' . $s_alt . '[/video]' ) === false ) &&
( strpos ( $body , ']' . $s_alt . '[/zvideo]' ) === false ) &&
( strpos ( $body , ']' . $s_alt . '[/audio]' ) === false ) &&
( strpos ( $body , ']' . $s_alt . '[/zaudio]' ) === false )) {
2018-10-11 23:54:31 +00:00
return true ;
}
return false ;
}
2018-05-30 04:08:52 +00:00
static function bb_content ( $content , $field ) {
$ret = false ;
2019-12-10 05:39:48 +00:00
if ( ! is_array ( $content )) {
btlogger ( 'content not initialised' );
return $ret ;
}
2021-03-08 23:36:50 +00:00
if ( array_key_exists ( $field , $content ) && is_array ( $content [ $field ])) {
2019-05-13 06:57:57 +00:00
foreach ( $content [ $field ] as $k => $v ) {
2018-10-23 22:36:48 +00:00
$ret .= html2bbcode ( $v );
// save this for auto-translate or dynamic filtering
// $ret .= '[language=' . $k . ']' . html2bbcode($v) . '[/language]';
2018-05-30 04:08:52 +00:00
}
}
else {
2019-05-13 06:57:57 +00:00
if ( $field === 'bbcode' && array_key_exists ( 'bbcode' , $content )) {
2018-07-09 05:28:15 +00:00
$ret = $content [ $field ];
}
else {
$ret = html2bbcode ( $content [ $field ]);
}
2018-05-30 04:08:52 +00:00
}
2019-05-13 06:57:57 +00:00
if ( $field === 'content' && $content [ 'event' ] && ( ! strpos ( $ret , '[event' ))) {
2018-08-23 01:45:53 +00:00
$ret .= format_event_bbcode ( $content [ 'event' ]);
}
2018-05-30 04:08:52 +00:00
return $ret ;
}
2020-02-12 23:21:39 +00:00
static function get_content ( $act , $binary = false ) {
2018-05-30 04:08:52 +00:00
$content = [];
2018-08-23 01:45:53 +00:00
$event = null ;
2018-09-01 22:52:28 +00:00
if (( ! $act ) || ( ! is_array ( $act ))) {
2018-07-18 04:56:51 +00:00
return $content ;
}
2018-05-30 04:08:52 +00:00
2020-02-12 23:21:39 +00:00
2019-05-13 06:57:57 +00:00
if ( $act [ 'type' ] === 'Event' ) {
2019-04-11 06:45:24 +00:00
$adjust = false ;
$event = [];
$event [ 'event_hash' ] = $act [ 'id' ];
2019-05-13 06:57:57 +00:00
if ( array_key_exists ( 'startTime' , $act ) && strpos ( $act [ 'startTime' ], - 1 , 1 ) === 'Z' ) {
2019-04-11 06:45:24 +00:00
$adjust = true ;
$event [ 'adjust' ] = 1 ;
$event [ 'dtstart' ] = datetime_convert ( 'UTC' , 'UTC' , $event [ 'startTime' ] . (( $adjust ) ? '' : 'Z' ));
}
2019-05-13 06:57:57 +00:00
if ( array_key_exists ( 'endTime' , $act )) {
2019-04-11 06:45:24 +00:00
$event [ 'dtend' ] = datetime_convert ( 'UTC' , 'UTC' , $event [ 'endTime' ] . (( $adjust ) ? '' : 'Z' ));
}
else {
$event [ 'nofinish' ] = true ;
}
2019-05-13 06:57:57 +00:00
if ( array_key_exists ( 'eventRepeat' , $act )) {
2019-04-11 06:45:24 +00:00
$event [ 'event_repeat' ] = $act [ 'eventRepeat' ];
}
2018-08-23 01:45:53 +00:00
}
2018-07-18 04:56:51 +00:00
foreach ([ 'name' , 'summary' , 'content' ] as $a ) {
2020-02-12 23:21:39 +00:00
if (( $x = self :: get_textfield ( $act , $a , $binary )) !== false ) {
2018-05-30 04:08:52 +00:00
$content [ $a ] = $x ;
}
2021-04-06 20:31:44 +00:00
if ( isset ( $content [ 'name' ])) {
$content [ 'name' ] = html2plain ( purify_html ( $content [ 'name' ]), 256 );
}
2018-05-30 04:08:52 +00:00
}
2018-08-23 01:45:53 +00:00
2020-02-12 23:21:39 +00:00
if ( $event && ! $binary ) {
2019-12-17 04:26:43 +00:00
$event [ 'summary' ] = html2plain ( purify_html ( $content [ 'summary' ]), 256 );
2019-09-12 03:41:16 +00:00
if ( ! $event [ 'summary' ]) {
if ( $content [ 'name' ]) {
2019-09-18 04:25:26 +00:00
$event [ 'summary' ] = html2plain ( purify_html ( $content [ 'name' ]), 256 );
2019-09-12 03:41:16 +00:00
}
}
2019-12-17 04:26:43 +00:00
if ( ! $event [ 'summary' ]) {
if ( $content [ 'content' ]) {
$event [ 'summary' ] = html2plain ( purify_html ( $content [ 'content' ]), 256 );
}
}
if ( $event [ 'summary' ]) {
$event [ 'summary' ] = substr ( $event [ 'summary' ], 0 , 256 );
}
2018-08-23 01:45:53 +00:00
$event [ 'description' ] = html2bbcode ( $content [ 'content' ]);
2019-05-13 06:57:57 +00:00
if ( $event [ 'summary' ] && $event [ 'dtstart' ]) {
2018-08-23 01:45:53 +00:00
$content [ 'event' ] = $event ;
}
}
2020-07-07 04:05:31 +00:00
if ( array_path_exists ( 'source/mediaType' , $act ) && array_path_exists ( 'source/content' , $act )) {
2021-03-08 05:32:28 +00:00
if ( in_array ( $act [ 'source' ][ 'mediaType' ], [ 'text/bbcode' , 'text/x-multicode' ])) {
2021-03-08 00:59:19 +00:00
if ( is_string ( $act [ 'source' ][ 'content' ]) && strpos ( $act [ 'source' ][ 'content' ], '<' ) !== false ) {
2021-02-24 01:40:25 +00:00
$content [ 'bbcode' ] = multicode_purify ( $act [ 'source' ][ 'content' ]);
}
else {
2021-03-09 02:41:29 +00:00
$content [ 'bbcode' ] = purify_html ( $act [ 'source' ][ 'content' ], [ 'escape' ] );
2021-02-24 01:40:25 +00:00
}
2018-07-09 05:28:15 +00:00
}
}
2018-05-30 04:08:52 +00:00
return $content ;
}
2020-02-12 23:21:39 +00:00
static function get_textfield ( $act , $field , $binary = false ) {
2018-05-30 04:08:52 +00:00
$content = false ;
2019-05-13 06:57:57 +00:00
if ( array_key_exists ( $field , $act ) && $act [ $field ])
2020-02-12 23:21:39 +00:00
$content = (( $binary ) ? $act [ $field ] : purify_html ( $act [ $field ]));
2019-05-13 06:57:57 +00:00
elseif ( array_key_exists ( $field . 'Map' , $act ) && $act [ $field . 'Map' ]) {
foreach ( $act [ $field . 'Map' ] as $k => $v ) {
2020-02-12 23:21:39 +00:00
$content [ escape_tags ( $k )] = (( $binary ) ? $v : purify_html ( $v ));
2018-05-30 04:08:52 +00:00
}
}
return $content ;
}
2018-08-23 06:04:37 +00:00
2019-09-22 06:18:20 +00:00
static function send_rejection_activity ( $channel , $observer_hash , $item ) {
2019-09-21 22:26:55 +00:00
2019-09-22 06:18:20 +00:00
$recip = q ( " select * from hubloc where hubloc_hash = '%s' limit 1 " ,
dbesc ( $observer_hash )
);
if ( ! $recip ) {
return ;
}
2019-09-21 22:26:55 +00:00
2019-09-22 06:18:20 +00:00
$arr = [
'id' => z_root () . '/bounces/' . new_uuid (),
'to' => [ $observer_hash ],
'type' => 'Reject' ,
'actor' => channel_url ( $channel ),
'name' => 'Permission denied' ,
'object' => $item [ 'message_id' ]
];
$msg = array_merge ([ '@context' => [
ACTIVITYSTREAMS_JSONLD_REV ,
'https://w3id.org/security/v1' ,
2021-01-23 00:59:56 +00:00
self :: ap_schema ()
2019-09-22 06:18:20 +00:00
]], $arr );
2019-10-18 05:01:53 +00:00
$queue_id = ActivityPub :: queue_message ( json_encode ( $msg , JSON_UNESCAPED_SLASHES ), $channel , $recip [ 0 ]);
2019-09-22 06:18:20 +00:00
do_delivery ( [ $queue_id ] );
2019-09-21 22:26:55 +00:00
}
2019-09-30 02:20:09 +00:00
// Find either an Authorization: Bearer token or 'token' request variable
// in the current web request and return it
static function token_from_request () {
2019-09-27 01:52:51 +00:00
foreach ( [ 'REDIRECT_REMOTE_USER' , 'HTTP_AUTHORIZATION' ] as $s ) {
$auth = (( array_key_exists ( $s , $_SERVER ) && strpos ( $_SERVER [ $s ], 'Bearer ' ) === 0 )
? str_replace ( 'Bearer ' , EMPTY_STR , $_SERVER [ $s ])
: EMPTY_STR
);
2019-09-30 02:20:09 +00:00
if ( $auth ) {
break ;
}
}
if ( ! $auth ) {
if ( array_key_exists ( 'token' , $_REQUEST ) && $_REQUEST [ 'token' ]) {
$auth = $_REQUEST [ 'token' ];
}
2019-09-27 01:52:51 +00:00
}
2019-09-30 02:20:09 +00:00
return $auth ;
2019-09-27 01:52:51 +00:00
}
2019-09-21 22:26:55 +00:00
2021-01-23 00:59:56 +00:00
static function ap_schema () {
return [
2021-02-22 01:39:48 +00:00
'zot' => z_root () . '/apschema#' ,
// 'as' => 'https://www.w3.org/ns/activitystreams#',
'toot' => 'http://joinmastodon.org/ns#' ,
'ostatus' => 'http://ostatus.org#' ,
'schema' => 'http://schema.org#' ,
2021-04-16 22:44:20 +00:00
'litepub' => 'http://litepub.social/ns#' ,
2021-04-30 04:24:30 +00:00
'sm' => 'http://smithereen.software/ns#' ,
2021-02-22 01:39:48 +00:00
'conversation' => 'ostatus:conversation' ,
'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers' ,
2021-04-16 22:44:20 +00:00
'oauthRegistrationEndpoint' => 'litepub:oauthRegistrationEndpoint' ,
2021-02-22 01:39:48 +00:00
'sensitive' => 'as:sensitive' ,
'movedTo' => 'as:movedTo' ,
'copiedTo' => 'as:copiedTo' ,
'alsoKnownAs' => 'as:alsoKnownAs' ,
'inheritPrivacy' => 'as:inheritPrivacy' ,
'EmojiReact' => 'as:EmojiReact' ,
'commentPolicy' => 'zot:commentPolicy' ,
'topicalCollection' => 'zot:topicalCollection' ,
'eventRepeat' => 'zot:eventRepeat' ,
'emojiReaction' => 'zot:emojiReaction' ,
'expires' => 'zot:expires' ,
'directMessage' => 'zot:directMessage' ,
'Category' => 'zot:Category' ,
'replyTo' => 'zot:replyTo' ,
'PropertyValue' => 'schema:PropertyValue' ,
'value' => 'schema:value' ,
'discoverable' => 'toot:discoverable' ,
2021-04-30 04:24:30 +00:00
'wall' => 'sm:wall' ,
2021-01-23 00:59:56 +00:00
];
}
2019-09-21 22:26:55 +00:00
2018-11-03 14:20:18 +00:00
}