aboutsummaryrefslogtreecommitdiffstats
path: root/include/items.php
diff options
context:
space:
mode:
Diffstat (limited to 'include/items.php')
-rwxr-xr-xinclude/items.php164
1 files changed, 146 insertions, 18 deletions
diff --git a/include/items.php b/include/items.php
index cf903ac13..36a027eec 100755
--- a/include/items.php
+++ b/include/items.php
@@ -4,6 +4,8 @@ require_once('include/bbcode.php');
require_once('include/oembed.php');
require_once('include/salmon.php');
require_once('include/crypto.php');
+require_once('include/Photo.php');
+
function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
@@ -278,7 +280,99 @@ function construct_activity_target($item) {
}
return '';
-}
+}
+
+/* limit_body_size()
+ *
+ * The purpose of this function is to apply system message length limits to
+ * imported messages without including any embedded photos in the length
+ */
+if(! function_exists('limit_body_size')) {
+function limit_body_size($body) {
+
+ logger('limit_body_size: start', LOGGER_DEBUG);
+
+ $maxlen = get_max_import_size();
+
+ // If the length of the body, including the embedded images, is smaller
+ // than the maximum, then don't waste time looking for the images
+ if($maxlen && (strlen($body) > $maxlen)) {
+
+ logger('limit_body_size: the total body length exceeds the limit', LOGGER_DEBUG);
+
+ $orig_body = $body;
+ $new_body = '';
+ $textlen = 0;
+ $max_found = false;
+
+ $img_start = strpos($orig_body, '[img');
+ $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
+ $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
+ while(($img_st_close !== false) && ($img_end !== false)) {
+
+ $img_st_close++; // make it point to AFTER the closing bracket
+ $img_end += $img_start;
+ $img_end += strlen('[/img]');
+
+ if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
+ // This is an embedded image
+
+ if( ($textlen + $img_start) > $maxlen ) {
+ if($textlen < $maxlen) {
+ logger('limit_body_size: the limit happens before an embedded image', LOGGER_DEBUG);
+ $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
+ $textlen = $maxlen;
+ }
+ }
+ else {
+ $new_body = $new_body . substr($orig_body, 0, $img_start);
+ $textlen += $img_start;
+ }
+
+ $new_body = $new_body . substr($orig_body, $img_start, $img_end - $img_start);
+ }
+ else {
+
+ if( ($textlen + $img_end) > $maxlen ) {
+ if($textlen < $maxlen) {
+ logger('limit_body_size: the limit happens before the end of a non-embedded image', LOGGER_DEBUG);
+ $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
+ $textlen = $maxlen;
+ }
+ }
+ else {
+ $new_body = $new_body . substr($orig_body, 0, $img_end);
+ $textlen += $img_end;
+ }
+ }
+ $orig_body = substr($orig_body, $img_end);
+
+ if($orig_body === false) // in case the body ends on a closing image tag
+ $orig_body = '';
+
+ $img_start = strpos($orig_body, '[img');
+ $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
+ $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
+ }
+
+ if( ($textlen + strlen($orig_body)) > $maxlen) {
+ if($textlen < $maxlen) {
+ logger('limit_body_size: the limit happens after the end of the last image', LOGGER_DEBUG);
+ $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
+ $textlen = $maxlen;
+ }
+ }
+ else {
+ logger('limit_body_size: the text size with embedded images extracted did not violate the limit', LOGGER_DEBUG);
+ $new_body = $new_body . $orig_body;
+ $textlen += strlen($orig_body);
+ }
+
+ return $new_body;
+ }
+ else
+ return $body;
+}}
@@ -414,9 +508,8 @@ function get_atom_elements($feed,$item) {
$res['body'] = notags(base64url_decode($res['body']));
}
- $maxlen = get_max_import_size();
- if($maxlen && (strlen($res['body']) > $maxlen))
- $res['body'] = substr($res['body'],0, $maxlen);
+
+ $res['body'] = limit_body_size($res['body']);
// It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
// the content type. Our own network only emits text normally, though it might have been converted to
@@ -3088,20 +3181,33 @@ function atom_entry($item,$type,$author,$owner,$comment = false,$cid = 0) {
return $o;
}
-function fix_private_photos($s,$uid, $item = null, $cid = 0) {
+function fix_private_photos($s, $uid, $item = null, $cid = 0) {
$a = get_app();
logger('fix_private_photos', LOGGER_DEBUG);
$site = substr($a->get_baseurl(),strpos($a->get_baseurl(),'://'));
- if(preg_match("/\[img(.*?)\](.*?)\[\/img\]/is",$s,$matches)) {
- $image = $matches[2];
+ $orig_body = $s;
+ $new_body = '';
+
+ $img_start = strpos($orig_body, '[img');
+ $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
+ $img_len = ($img_start !== false ? strpos(substr($orig_body, $img_start + $img_st_close + 1), '[/img]') : false);
+ while( ($img_st_close !== false) && ($img_len !== false) ) {
+
+ $img_st_close++; // make it point to AFTER the closing bracket
+ $image = substr($orig_body, $img_start + $img_st_close, $img_len);
+
logger('fix_private_photos: found photo ' . $image, LOGGER_DEBUG);
+
+
if(stristr($image , $site . '/photo/')) {
+ // Only embed locally hosted photos
$replace = false;
$i = basename($image);
$i = str_replace(array('.jpg','.png'),array('',''),$i);
$x = strpos($i,'-');
+
if($x) {
$res = substr($i,$x+1);
$i = substr($i,0,$x);
@@ -3119,14 +3225,6 @@ function fix_private_photos($s,$uid, $item = null, $cid = 0) {
// 3. Otherwise, if we have an item, see if the item permissions match the photo
// permissions, regardless of order but first check to see if they're an exact
// match to save some processing overhead.
-
- // Currently we only embed one private photo per message so as not to hit import
- // size limits at the receiving end.
-
- // To embed multiples, we would need to parse out the embedded photos on message
- // receipt and limit size based only on the text component. Would also need to
- // ignore all photos during bbcode translation and item localisation, as these
- // will hit internal regex backtrace limits.
if(has_permissions($r[0])) {
if($cid) {
@@ -3141,15 +3239,45 @@ function fix_private_photos($s,$uid, $item = null, $cid = 0) {
}
}
if($replace) {
+ $data = $r[0]['data'];
+ $type = $r[0]['type'];
+
+ // If a custom width and height were specified, apply before embedding
+ if(preg_match("/\[img\=([0-9]*)x([0-9]*)\]/is", substr($orig_body, $img_start, $img_st_close), $match)) {
+ logger('fix_private_photos: scaling photo', LOGGER_DEBUG);
+
+ $width = intval($match[1]);
+ $height = intval($match[2]);
+
+ $ph = new Photo($data, $type);
+ if($ph->is_valid()) {
+ $ph->scaleImage(max($width, $height));
+ $data = $ph->imageString();
+ $type = $ph->getType();
+ }
+ }
+
logger('fix_private_photos: replacing photo', LOGGER_DEBUG);
- $s = str_replace($image, 'data:' . $r[0]['type'] . ';base64,' . base64_encode($r[0]['data']), $s);
- logger('fix_private_photos: replaced: ' . $s, LOGGER_DATA);
+ $image = 'data:' . $type . ';base64,' . base64_encode($data);
+ logger('fix_private_photos: replaced: ' . $image, LOGGER_DATA);
}
}
}
}
+
+ $new_body = $new_body . substr($orig_body, 0, $img_start + $img_st_close) . $image . '[/img]';
+ $orig_body = substr($orig_body, $img_start + $img_st_close + $img_len + strlen('[/img]'));
+ if($orig_body === false)
+ $orig_body = '';
+
+ $img_start = strpos($orig_body, '[img');
+ $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
+ $img_len = ($img_start !== false ? strpos(substr($orig_body, $img_start + $img_st_close + 1), '[/img]') : false);
}
- return($s);
+
+ $new_body = $new_body . $orig_body;
+
+ return($new_body);
}