From eb1e882d7c4887cff346c26562f68bcf8280758c Mon Sep 17 00:00:00 2001 From: rabuzarus <> Date: Thu, 14 Apr 2016 14:07:20 +0200 Subject: [PATCH] open friendica photos from the stream with colorbox --- js/theme.js | 28 ++++++++++++++++++++++++++++ theme.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/js/theme.js b/js/theme.js index fb1a2236c6..7cd98c862d 100644 --- a/js/theme.js +++ b/js/theme.js @@ -73,6 +73,34 @@ $(document).ready(function(){ }); }); + // Add Colorbox for viewing Network page images + //var cBoxClasses = new Array(); + $(".wall-item-body a img").each(function(){ + var aElem = $(this).parent(); + var imgHref = aElem.attr("href"); + + // We need to make sure we only put a Colorbox on links to Friendica images + // We'll try to do this by looking for links of the form + // .../photo/ab803d8eg08daf85023adfec08 (with nothing more following), in hopes + // that that will be unique enough + if(imgHref.match(/\/photo\/[a-fA-F0-9]+(-[0-9]\.[\w]+?)?$/)) { + + // Add a unique class to all the images of a certain post, to allow scrolling through + var cBoxClass = $(this).closest(".wall-item-body").attr("id") + "-lightbox"; + $(this).addClass(cBoxClass); + +// if( $.inArray(cBoxClass, cBoxClasses) < 0 ) { +// cBoxClasses.push(cBoxClass); +// } + + aElem.colorbox({ + maxHeight: '90%', + photo: true, // Colorbox doesn't recognize a URL that don't end in .jpg, etc. as a photo + rel: cBoxClass //$(this).attr("class").match(/wall-item-body-[\d]+-lightbox/)[0] + }); + } + }); + // overwrite Dialog.show from main js to load the filebrowser into a bs modal Dialog.show = function(url) { var modal = $('#modal').modal(); diff --git a/theme.php b/theme.php index 8dae2c3aa1..0d74116d98 100644 --- a/theme.php +++ b/theme.php @@ -83,11 +83,53 @@ EOT; } function frio_install() { + register_hook('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links'); logger("installed theme frio"); } function frio_uninstall() { + unregister_hook('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links'); logger("uninstalled theme frio"); } +/** + * @brief Replace friendica photo links + * + * This function does replace the links to photos + * of other friendica users. Original the photos are + * linked to the photo page. Now they will linked directly + * to the photo file. This function is nessesary to use colorbox + * in the network stream + * + * @param App $a + * @param array $body_info The item and its html output + */ +function frio_item_photo_links(&$a, &$body_info) { + require_once('include/Photo.php'); + + $phototypes = Photo::supportedTypes(); + $occurence = 1; + $p = bb_find_open_close($body_info['html'], ""); + + while($p !== false && ($occurence++ < 500)) { + $link = substr($body_info['html'], $p['start'], $p['end'] - $p['start']); + $matches = array(); + + preg_match("/\/photos\/[\w]+\/image\/([\w]+)/", $link, $matches); + if($matches) { + // Replace the link for the photo's page with a direct link to the photo itself + $newlink = str_replace($matches[0], "/photo/{$matches[1]}", $link); + + // Add a "quiet" parameter to any redir links to prevent the "XX welcomes YY" info boxes + $newlink = preg_replace("/href=\"([^\"]+)\/redir\/([^\"]+)&url=([^\"]+)\"/", 'href="$1/redir/$2&quiet=1&url=$3"', $newlink); + + // Having any arguments to the link for Colorbox causes it to fetch base64 code instead of the image + $newlink = preg_replace("/\/[?&]zrl=([^&\"]+)/", '', $newlink); + + $body_info['html'] = str_replace($link, $newlink, $body_info['html']); + } + + $p = bb_find_open_close($body_info['html'], "", $occurence); + } +}