Fix Friendica private image URL replacing

- Add baseurl to redir_private_images to prevent empty src attribute in
generated HTML
- URL encode the URL parameter url and conurl
- Formatting
- Documentation
This commit is contained in:
Hypolite Petovan 2016-11-18 22:05:56 -05:00
parent 90f100578b
commit c2eb2f460b

View File

@ -1170,33 +1170,29 @@ function link_compare($a,$b) {
return false; return false;
}} }}
if(! function_exists('redir_private_images')) {
/** /**
* Find any non-embedded images in private items and add redir links to them * @brief Find any non-embedded images in private items and add redir links to them
* *
* @param App $a * @param App $a
* @param array $item * @param array &$item The field array of an item row
*/ */
function redir_private_images($a, &$item) { function redir_private_images($a, &$item)
{
$matches = false; $matches = false;
$cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER); $cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER);
if ($cnt) { if ($cnt) {
//logger("redir_private_images: matches = " . print_r($matches, true));
foreach ($matches as $mtch) { foreach ($matches as $mtch) {
if(strpos($mtch[1], '/redir') !== false) if(strpos($mtch[1], '/redir') !== false) {
continue; continue;
}
if ((local_user() == $item['uid']) && ($item['private'] != 0) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == NETWORK_DFRN)) { if ((local_user() == $item['uid']) && ($item['private'] != 0) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == NETWORK_DFRN)) {
//logger("redir_private_images: redir"); $img_url = $a->get_baseurl() . '/redir?f=1&quiet=1&url=' . urlencode($mtch[1]) . '&conurl=' . urlencode($item['author-link']);
$img_url = 'redir?f=1&quiet=1&url=' . $mtch[1] . '&conurl=' . $item['author-link']; $item['body'] = str_replace($mtch[0], '[img]' . $img_url . '[/img]', $item['body']);
$item['body'] = str_replace($mtch[0], "[img]".$img_url."[/img]", $item['body']); }
} }
} }
} }
}}
function put_item_in_cache(&$item, $update = false) { function put_item_in_cache(&$item, $update = false) {