From 48cd5855fe0bb530d3a26ea3a095bd3ab9c2493e Mon Sep 17 00:00:00 2001 From: Max Kostikov Date: Thu, 29 Nov 2018 11:06:14 +0100 Subject: Add photo cache support using sslify() --- include/text.php | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) (limited to 'include/text.php') diff --git a/include/text.php b/include/text.php index 646bfe749..113448a92 100644 --- a/include/text.php +++ b/include/text.php @@ -1105,12 +1105,33 @@ function linkify($s, $me = false) { * to a local redirector which uses https and which redirects to the selected content * * @param string $s + * @param int $uid * @returns string */ -function sslify($s) { +function sslify($s, $uid) { + + // Local photo cache + if(get_config('system','photo_cache_enable', 0)) { + $matches = null; + $cnt = preg_match_all("/\/",$s,$matches,PREG_SET_ORDER); + if ($cnt) { + foreach ($matches as $match) { + logger('uid: ' . $uid . '; url: ' . $match[2], LOGGER_DEBUG); + $cache = array( + 'url' => $match[2], + 'uid' => $uid + ); + call_hooks('cache_url_hook', $cache); + logger('cache status: ' . intval($cache['status']) .'; cached as: ' . ($cache['cached'] ? $cache['hash'] : '-'), LOGGER_DEBUG); + if($cache['cached']) + $s = str_replace($match[2], z_root() . '/photo/' . $cache['hash'] . '-' . $cache['res'], $s); + } + } + } + if (strpos(z_root(),'https:') === false) return $s; - + // By default we'll only sslify img tags because media files will probably choke. // You can set sslify_everything if you want - but it will likely white-screen if it hits your php memory limit. // The downside is that http: media files will likely be blocked by your browser @@ -1118,7 +1139,7 @@ function sslify($s) { $allow = get_config('system','sslify_everything'); - $pattern = (($allow) ? "/\<(.*?)src=\"(http\:.*?)\"(.*?)\>/" : "/\/" ); + $pattern = (($allow) ? "/\<(.*?)src=[\"|'](http\:.*?)[\"|'](.*?)\>/" : "/\/" ); $matches = null; $cnt = preg_match_all($pattern,$s,$matches,PREG_SET_ORDER); @@ -1681,7 +1702,7 @@ function prepare_body(&$item,$attach = false,$opts = false) { if(local_channel() == $item['uid']) $filer = format_filer($item); - $s = sslify($s); + $s = sslify($s, $item['uid']); $prep_arr = array( 'item' => $item, @@ -3259,7 +3280,7 @@ function share_unshield($m) { } -function cleanup_bbcode($body) { +function cleanup_bbcode($body, $uid = 0) { /** * fix naked links by passing through a callback to see if this is a hubzilla site @@ -3295,7 +3316,7 @@ function cleanup_bbcode($body) { $body = preg_replace('/\[\/code\]\s*\[code\]/ism',"\n",$body); - $body = scale_external_images($body,false); + $body = scale_external_images($body, false, false, $uid); return $body; } -- cgit v1.2.3 From d9faf6c4bb5d26d6eff5e3176b46761ea26dcf63 Mon Sep 17 00:00:00 2001 From: Max Kostikov Date: Thu, 29 Nov 2018 11:48:12 +0100 Subject: Update text.php --- include/text.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include/text.php') diff --git a/include/text.php b/include/text.php index 113448a92..5574b9054 100644 --- a/include/text.php +++ b/include/text.php @@ -3280,7 +3280,7 @@ function share_unshield($m) { } -function cleanup_bbcode($body, $uid = 0) { +function cleanup_bbcode($body) { /** * fix naked links by passing through a callback to see if this is a hubzilla site @@ -3316,7 +3316,7 @@ function cleanup_bbcode($body, $uid = 0) { $body = preg_replace('/\[\/code\]\s*\[code\]/ism',"\n",$body); - $body = scale_external_images($body, false, false, $uid); + $body = scale_external_images($body, false); return $body; } -- cgit v1.2.3 From d8b5f383f52274e30e02a8cf8ce77aac0a6bd910 Mon Sep 17 00:00:00 2001 From: Max Kostikov Date: Thu, 29 Nov 2018 16:21:50 +0100 Subject: Disable links to cached photos for non logged viewers --- include/text.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/text.php') diff --git a/include/text.php b/include/text.php index 5574b9054..00e12dc06 100644 --- a/include/text.php +++ b/include/text.php @@ -1111,7 +1111,7 @@ function linkify($s, $me = false) { function sslify($s, $uid) { // Local photo cache - if(get_config('system','photo_cache_enable', 0)) { + if(get_config('system','photo_cache_enable', 0) && local_channel()) { $matches = null; $cnt = preg_match_all("/\/",$s,$matches,PREG_SET_ORDER); if ($cnt) { -- cgit v1.2.3 From 9de650e17521542d84d9d07e2c3fabfbf1519acc Mon Sep 17 00:00:00 2001 From: Max Kostikov Date: Thu, 29 Nov 2018 19:13:43 +0100 Subject: Cache for viewer channel --- include/text.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'include/text.php') diff --git a/include/text.php b/include/text.php index 00e12dc06..a20d9564b 100644 --- a/include/text.php +++ b/include/text.php @@ -1108,18 +1108,17 @@ function linkify($s, $me = false) { * @param int $uid * @returns string */ -function sslify($s, $uid) { +function sslify($s) { // Local photo cache if(get_config('system','photo_cache_enable', 0) && local_channel()) { $matches = null; - $cnt = preg_match_all("/\/",$s,$matches,PREG_SET_ORDER); + $cnt = preg_match_all("/\/",$s,$matches,PREG_SET_ORDER); if ($cnt) { foreach ($matches as $match) { logger('uid: ' . $uid . '; url: ' . $match[2], LOGGER_DEBUG); $cache = array( - 'url' => $match[2], - 'uid' => $uid + 'url' => $match[2] ); call_hooks('cache_url_hook', $cache); logger('cache status: ' . intval($cache['status']) .'; cached as: ' . ($cache['cached'] ? $cache['hash'] : '-'), LOGGER_DEBUG); @@ -1702,7 +1701,7 @@ function prepare_body(&$item,$attach = false,$opts = false) { if(local_channel() == $item['uid']) $filer = format_filer($item); - $s = sslify($s, $item['uid']); + $s = sslify($s); $prep_arr = array( 'item' => $item, -- cgit v1.2.3 From 1a9ea6452305c44df285e16f17475a5b16b54a37 Mon Sep 17 00:00:00 2001 From: Max Kostikov Date: Thu, 29 Nov 2018 19:28:54 +0100 Subject: Update text.php --- include/text.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'include/text.php') diff --git a/include/text.php b/include/text.php index a20d9564b..bb9c6075b 100644 --- a/include/text.php +++ b/include/text.php @@ -1118,7 +1118,8 @@ function sslify($s) { foreach ($matches as $match) { logger('uid: ' . $uid . '; url: ' . $match[2], LOGGER_DEBUG); $cache = array( - 'url' => $match[2] + 'url' => $match[2], + 'uid' => local_channel() ); call_hooks('cache_url_hook', $cache); logger('cache status: ' . intval($cache['status']) .'; cached as: ' . ($cache['cached'] ? $cache['hash'] : '-'), LOGGER_DEBUG); -- cgit v1.2.3 From 3265c2e54305fd46fdb237d716d34b4e77e98e08 Mon Sep 17 00:00:00 2001 From: Max Kostikov Date: Thu, 29 Nov 2018 20:36:21 +0100 Subject: Skip sslify for images if it already were cached with https enabled --- include/text.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'include/text.php') diff --git a/include/text.php b/include/text.php index bb9c6075b..426229d36 100644 --- a/include/text.php +++ b/include/text.php @@ -1111,7 +1111,8 @@ function linkify($s, $me = false) { function sslify($s) { // Local photo cache - if(get_config('system','photo_cache_enable', 0) && local_channel()) { + $cacheon = get_config('system','photo_cache_enable', 0); + if($cache && local_channel()) { $matches = null; $cnt = preg_match_all("/\/",$s,$matches,PREG_SET_ORDER); if ($cnt) { @@ -1138,6 +1139,10 @@ function sslify($s) { // Complain to your browser maker $allow = get_config('system','sslify_everything'); + + //We can skip next part if this only images and cache is on + if((! $allow) && $cacheon) + return $s; $pattern = (($allow) ? "/\<(.*?)src=[\"|'](http\:.*?)[\"|'](.*?)\>/" : "/\/" ); -- cgit v1.2.3 From d6442ec99160c1f556d9e5b7bc3b2fec76200aa1 Mon Sep 17 00:00:00 2001 From: Max Kostikov Date: Thu, 29 Nov 2018 23:07:39 +0100 Subject: Update text.php --- include/text.php | 5 ----- 1 file changed, 5 deletions(-) (limited to 'include/text.php') diff --git a/include/text.php b/include/text.php index 426229d36..234747342 100644 --- a/include/text.php +++ b/include/text.php @@ -1139,11 +1139,6 @@ function sslify($s) { // Complain to your browser maker $allow = get_config('system','sslify_everything'); - - //We can skip next part if this only images and cache is on - if((! $allow) && $cacheon) - return $s; - $pattern = (($allow) ? "/\<(.*?)src=[\"|'](http\:.*?)[\"|'](.*?)\>/" : "/\/" ); $matches = null; -- cgit v1.2.3 From 6f2d09eb205f09cdc83225b6fadff316b934e1eb Mon Sep 17 00:00:00 2001 From: Max Kostikov Date: Thu, 29 Nov 2018 23:28:45 +0100 Subject: Fix wrong parameter --- include/text.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'include/text.php') diff --git a/include/text.php b/include/text.php index 234747342..7c0610ed9 100644 --- a/include/text.php +++ b/include/text.php @@ -1111,10 +1111,9 @@ function linkify($s, $me = false) { function sslify($s) { // Local photo cache - $cacheon = get_config('system','photo_cache_enable', 0); - if($cache && local_channel()) { + if(get_config('system','photo_cache_enable', 0) && local_channel()) { $matches = null; - $cnt = preg_match_all("/\/",$s,$matches,PREG_SET_ORDER); + $cnt = preg_match_all("/\/", $s, $matches, PREG_SET_ORDER); if ($cnt) { foreach ($matches as $match) { logger('uid: ' . $uid . '; url: ' . $match[2], LOGGER_DEBUG); @@ -1142,7 +1141,7 @@ function sslify($s) { $pattern = (($allow) ? "/\<(.*?)src=[\"|'](http\:.*?)[\"|'](.*?)\>/" : "/\/" ); $matches = null; - $cnt = preg_match_all($pattern,$s,$matches,PREG_SET_ORDER); + $cnt = preg_match_all($pattern, $s, $matches, PREG_SET_ORDER); if ($cnt) { foreach ($matches as $match) { $filename = basename( parse_url($match[2], PHP_URL_PATH) ); -- cgit v1.2.3 From 0ee403eab3a29a98f85dc9fe304c25c6f037b24a Mon Sep 17 00:00:00 2001 From: Max Kostikov Date: Fri, 30 Nov 2018 10:50:00 +0100 Subject: Move cache code to addon --- include/text.php | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) (limited to 'include/text.php') diff --git a/include/text.php b/include/text.php index 7c0610ed9..866b3fa2c 100644 --- a/include/text.php +++ b/include/text.php @@ -1111,23 +1111,13 @@ function linkify($s, $me = false) { function sslify($s) { // Local photo cache - if(get_config('system','photo_cache_enable', 0) && local_channel()) { - $matches = null; - $cnt = preg_match_all("/\/", $s, $matches, PREG_SET_ORDER); - if ($cnt) { - foreach ($matches as $match) { - logger('uid: ' . $uid . '; url: ' . $match[2], LOGGER_DEBUG); - $cache = array( - 'url' => $match[2], - 'uid' => local_channel() - ); - call_hooks('cache_url_hook', $cache); - logger('cache status: ' . intval($cache['status']) .'; cached as: ' . ($cache['cached'] ? $cache['hash'] : '-'), LOGGER_DEBUG); - if($cache['cached']) - $s = str_replace($match[2], z_root() . '/photo/' . $cache['hash'] . '-' . $cache['res'], $s); - } - } - } + $str = array( + 'body' => $s, + 'uid' => local_channel() + ); + call_hooks('cache_body_hook', $str); + + $s = $str['body']; if (strpos(z_root(),'https:') === false) return $s; -- cgit v1.2.3 From 53d9cbc69cecc0825c1e8fdc670cfe8ea09b03bf Mon Sep 17 00:00:00 2001 From: Max Kostikov Date: Fri, 30 Nov 2018 11:38:47 +0100 Subject: Check local_channel() before cache call --- include/text.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'include/text.php') diff --git a/include/text.php b/include/text.php index 866b3fa2c..6e4402a9f 100644 --- a/include/text.php +++ b/include/text.php @@ -1111,13 +1111,15 @@ function linkify($s, $me = false) { function sslify($s) { // Local photo cache - $str = array( - 'body' => $s, - 'uid' => local_channel() - ); - call_hooks('cache_body_hook', $str); + if(local_channel()) { + $str = array( + 'body' => $s, + 'uid' => local_channel() + ); + call_hooks('cache_body_hook', $str); - $s = $str['body']; + $s = $str['body']; + } if (strpos(z_root(),'https:') === false) return $s; -- cgit v1.2.3 From 2a57e00cb4a7dada0993e91e0e2176ad335cf12f Mon Sep 17 00:00:00 2001 From: Max Kostikov Date: Sun, 2 Dec 2018 10:07:12 +0100 Subject: Remove local_channel() check on cache_body_hook call --- include/text.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'include/text.php') diff --git a/include/text.php b/include/text.php index 6e4402a9f..7f6e87d3b 100644 --- a/include/text.php +++ b/include/text.php @@ -1111,16 +1111,14 @@ function linkify($s, $me = false) { function sslify($s) { // Local photo cache - if(local_channel()) { - $str = array( - 'body' => $s, - 'uid' => local_channel() - ); - call_hooks('cache_body_hook', $str); - - $s = $str['body']; - } + $str = array( + 'body' => $s, + 'uid' => local_channel() + ); + call_hooks('cache_body_hook', $str); + $s = $str['body']; + if (strpos(z_root(),'https:') === false) return $s; -- cgit v1.2.3