federated alt text support for image attachments

This commit is contained in:
nobody 2021-04-06 13:31:44 -07:00
parent cbbe280fe8
commit 136066b971
2 changed files with 38 additions and 6 deletions

View file

@ -623,6 +623,9 @@ class Activity {
$entry['type'] = $att['mediaType'];
elseif (array_key_exists('type',$att) && $att['type'] === 'Image')
$entry['type'] = 'image/jpeg';
if (array_key_exists('name',$att) && $att['name']) {
$entry['name'] = html2plain(purify_html($att['name']),256);
}
if ($entry)
$ret[] = $entry;
}
@ -1208,11 +1211,17 @@ class Activity {
}
if ($activitypub && $has_images && $ret['type'] === 'Note') {
$img = [];
foreach ($images as $match) {
// handle Friendica-style img links with [img=$url]$alttext[/img]
$img = [];
// handle Friendica/Hubzilla style img links with [img=$url]$alttext[/img]
if (strpos($match[1],'=http') === 0) {
$img[] = [ 'type' => 'Image', 'url' => substr($match[1],1) ];
$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 ];
}
else {
$img[] = [ 'type' => 'Image', 'url' => $match[2] ];
@ -1221,7 +1230,17 @@ class Activity {
if (! $ret['attachment']) {
$ret['attachment'] = [];
}
$ret['attachment'] = array_merge($img,$ret['attachment']);
$already_added = false;
if ($img) {
foreach ($ret['attachment'] as $a) {
if (isset($a['url']) && $a['url'] === $img[0]['url']) {
$already_added = true;
}
}
if (! $already_added) {
$ret['attachment'] = array_merge($img,$ret['attachment']);
}
}
}
}
@ -2638,7 +2657,7 @@ class Activity {
$s['location'] = escape_tags($location['name']);
}
if (array_key_exists('content',$location)) {
$s['location'] = html2plain(html2plain(purify_html($location['content']),256));
$s['location'] = html2plain(purify_html($location['content']),256);
}
if (array_key_exists('latitude',$location) && array_key_exists('longitude',$location)) {
@ -3517,7 +3536,13 @@ class Activity {
foreach ($attach as $a) {
if (array_key_exists('type',$a) && stripos($a['type'],'image') !== false) {
if (self::media_not_in_body($a['href'],$body)) {
$ret .= "\n\n" . '[img]' . $a['href'] . '[/img]';
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]';
}
}
}
if (array_key_exists('type',$a) && stripos($a['type'], 'video') !== false) {
@ -3619,6 +3644,9 @@ class Activity {
if (($x = self::get_textfield($act,$a,$binary)) !== false) {
$content[$a] = $x;
}
if (isset($content['name'])) {
$content['name'] = html2plain(purify_html($content['name']),256);
}
}
if ($event && ! $binary) {

View file

@ -3510,6 +3510,8 @@ function cleanup_bbcode($body) {
$body = preg_replace_callback('/\[url(.*?)\[\/(url)\]/ism','\red_escape_codeblock',$body);
$body = preg_replace_callback('/\[zrl(.*?)\[\/(zrl)\]/ism','\red_escape_codeblock',$body);
$body = preg_replace_callback('/\[svg(.*?)\[\/(svg)\]/ism','\red_escape_codeblock',$body);
$body = preg_replace_callback('/\[img(.*?)\[\/(img)\]/ism','\red_escape_codeblock',$body);
$body = preg_replace_callback('/\[zmg(.*?)\[\/(zmg)\]/ism','\red_escape_codeblock',$body);
$body = preg_replace_callback("/([^\]\='".'"'."\;\/\{\(]|^|\#\^)(https?\:\/\/[a-zA-Z0-9\pL\:\/\-\?\&\;\.\=\@\_\~\#\%\$\!\\+\,\(\)]+)/ismu", '\nakedoembed', $body);
@ -3520,6 +3522,8 @@ function cleanup_bbcode($body) {
$body = preg_replace_callback('/\[\$b64url(.*?)\[\/(url)\]/ism','\red_unescape_codeblock',$body);
$body = preg_replace_callback('/\[\$b64zrl(.*?)\[\/(zrl)\]/ism','\red_unescape_codeblock',$body);
$body = preg_replace_callback('/\[\$b64svg(.*?)\[\/(svg)\]/ism','\red_unescape_codeblock',$body);
$body = preg_replace_callback('/\[\$b64img(.*?)\[\/(img)\]/ism','\red_unescape_codeblock',$body);
$body = preg_replace_callback('/\[\$b64zmg(.*?)\[\/(zmg)\]/ism','\red_unescape_codeblock',$body);
// fix any img tags that should be zmg