Commit ffac44d1 authored by Aaron Wells's avatar Aaron Wells Committed by Robert Lyon

Fixing issues in embedded images in copied pages

Bug 1489274
- DB upgrade to fill in artefact_file_embedded records for
existing blog entries
- Improving the regex that rewrites links in copied journal
entries, notes, and text blocks

behatnotneeded: Requires upgrading the site; not testable via Behat

Change-Id: Ie5d5dc12065ed039df663910e5ee738d57377a91
parent 7cff066c
......@@ -59,5 +59,26 @@ function xmldb_artefact_blog_upgrade($oldversion=0) {
delete_records('institution_config', 'field', 'progressbaritem_blog_blog');
}
if ($oldversion < 2015011501) {
$records = get_records_select_array('artefact', "artefacttype = ? AND description LIKE '%artefact/file/download.php%'", array('blogpost'), 'id', 'id, description, author');
if ($records) {
require_once('embeddedimage.php');
foreach ($records as $rec) {
set_field(
'artefact',
'description',
EmbeddedImage::prepare_embedded_images(
$rec->description,
'blogpost',
$rec->id,
null,
$rec->author
),
'id',
$rec->id
);
}
}
}
return true;
}
......@@ -470,8 +470,8 @@ class ArtefactTypeBlog extends ArtefactType {
foreach ($artefactcopies[$oldid]->oldembeds as $a) {
if (isset($artefactcopies[$a])) {
// Change the old image id to the new one
$regexp[] = '#<img([^>]+)src="' . get_config('wwwroot') . 'artefact/file/download.php\?file=' . $a . '(&|&amp;)embedded=1(.*?"[^>]+)#';
$replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactcopies[$a]->newid . '$2embedded=1$3';
$regexp[] = '#<img([^>]+)src="' . get_config('wwwroot') . 'artefact/file/download.php\?file=' . $a . '([^0-9])#';
$replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactcopies[$a]->newid . '$2';
}
}
require_once('embeddedimage.php');
......@@ -1069,8 +1069,8 @@ class ArtefactTypeBlogPost extends ArtefactType {
foreach ($artefactcopies[$oldid]->oldembeds as $a) {
if (isset($artefactcopies[$a])) {
// Change the old image id to the new one
$regexp[] = '#<img([^>]+)src="' . get_config('wwwroot') . 'artefact/file/download.php\?file=' . $a . '(&|&amp;)embedded=1(.*?"[^>]+)#';
$replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactcopies[$a]->newid . '$2embedded=1$3';
$regexp[] = '#<img([^>]+)src="' . get_config('wwwroot') . 'artefact/file/download.php\?file=' . $a . '([^0-9])#';
$replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactcopies[$a]->newid . '$2';
}
}
require_once('embeddedimage.php');
......
......@@ -12,5 +12,5 @@
defined('INTERNAL') || die();
$config = new StdClass;
$config->version = 2015011500;
$config->release = '1.1.1';
$config->version = 2015011501;
$config->release = '1.2.0';
......@@ -918,8 +918,8 @@ class ArtefactTypeHtml extends ArtefactType {
foreach ($artefactcopies[$oldid]->oldembeds as $a) {
if (isset($artefactcopies[$a])) {
// Change the old image id to the new one
$regexp[] = '#<img([^>]+)src="' . get_config('wwwroot') . 'artefact/file/download.php\?file=' . $a . '(&|&amp;)embedded=1(.*?"[^>]+)#';
$replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactcopies[$a]->newid . '$2embedded=1$3';
$regexp[] = '#<img([^>]+)src="' . get_config('wwwroot') . 'artefact/file/download.php\?file=' . $a . '([^0-9])#';
$replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactcopies[$a]->newid . '$2';
}
}
require_once('embeddedimage.php');
......
......@@ -303,16 +303,8 @@ class PluginBlocktypeText extends SystemBlocktype {
$replacetext = array();
foreach ($artefactcopies as $copyobj) {
// Change the old image id to the new one
$regexp[] = '#<img([^>]+)src=("|\\")'
. preg_quote(
get_config('wwwroot')
. 'artefact/file/download.php?file=' . $copyobj->oldid
)
. '(&|&amp;)embedded=1([^"]*)"#';
$replacetext[] = '<img$1src="'
. get_config('wwwroot')
. 'artefact/file/download.php?file=' . $copyobj->newid
. '&embedded=1"';
$regexp[] = '#<img([^>]+)src="' . get_config('wwwroot') . 'artefact/file/download.php\?file=' . $copyobj->oldid . '([^0-9])#';
$replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $copyobj->newid . '$2';
}
$configdata['text'] = preg_replace($regexp, $replacetext, $configdata['text']);
return $configdata;
......
......@@ -25,15 +25,30 @@ class EmbeddedImage {
* @param string $resourcetype The type of resource which the TinyMCE editor is used in, e.g. 'forum', 'topic', 'post' for forum text boxes
* @param int $resourceid The id of the resourcetype
* @param int $groupid The id of the group the resource is in if applicable
* @param int $userid The user trying to embed the image (current user if null)
* @return string The updated $fieldvalue
*/
public function prepare_embedded_images($fieldvalue, $resourcetype, $resourceid, $groupid = NULL) {
public function prepare_embedded_images($fieldvalue, $resourcetype, $resourceid, $groupid = NULL, $userid = NULL) {
if (empty($fieldvalue) || empty($resourcetype) || empty($resourceid)) {
return $fieldvalue;
}
global $USER;
if ($userid == null) {
$user = $USER;
}
else {
$user = new User();
try {
$user->find_by_id($userid);
}
catch (AuthUnknownUserException $e) {
log_warn('No user found with ID ' . $userid);
return $fieldvalue;
}
}
$dom = new DOMDocument();
$dom->encoding = 'utf-8';
$oldval = libxml_use_internal_errors(true);
......@@ -58,7 +73,7 @@ class EmbeddedImage {
foreach ($matches[1] as $imgid) {
$file = artefact_instance_from_id($imgid);
if (!($file instanceof ArtefactTypeImage)
|| !$USER->can_publish_artefact($file)
|| !$user->can_publish_artefact($file)
) {
return $fieldvalue;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment