Commit 348d8da2 authored by Nigel McNie's avatar Nigel McNie

Fix broken temporary download image URLs in blogposts caused by a combination of bugs.

Firstly, an over-enthusiastic optimisation I was responsible for broke the replacement of downloadtemp images, especially on sites which are not installed at the root of a domain. Then, some greedy regexes meant that if there was more than one image in the post, only one of them would have the URL fixed properly. The same bug was happening again when posts were being displayed in views.

This patch fixes these problems, and contains a blog database upgrade to fix existing broken paths.
parent 6ea1fb49
......@@ -50,6 +50,49 @@ function xmldb_artefact_blog_upgrade($oldversion=0) {
$status = $status && create_table($table);
}
if ($oldversion < 2008012200) {
// From 0.9, some files were not having their temporary download paths
// translated to proper artefact/file/download.php paths. This upgrade
// attempts to fix them. It should work in the vast majority of cases,
// the largest assumption made is that artefacts were inserted in
// ascending ID order when the post was created, which is a pretty safe
// bet.
if ($blogfiles = get_records_array('artefact_blog_blogpost_file', '', '', 'blogpost ASC, file ASC')) {
$blogpostids = join(', ', array_map(create_function('$a', 'return $a->blogpost;'), $blogfiles));
// Find all blogposts that have attached files
if ($blogposts = get_records_select_array('artefact', 'id IN(' . $blogpostids . ')', null, 'id ASC')) {
foreach ($blogposts as $post) {
log_debug("Checking post {$post->id}");
// Only doublecheck posts that are likely to have a broken URL in them
if (false !== strpos($post->description, 'createid')) {
log_debug(" * Looks like post " . $post->id . " has a createid in it");
$i = 0;
$body = $post->description;
foreach ($blogfiles as $file) {
if ($file->blogpost == $post->id) {
// This file is connected to this post, so likely it is to be displayed
$i++;
log_debug('* Replace uploadnumber = ' . $i . ' with artefact id ' . $file->file);
$regexps = array('/<img([^>]+)src="([^>]+)downloadtemp.php\?uploadnumber=' . $i .'&amp;createid=\d+/',
'/alt="uploaded:' . $i . '"/');
$subs = array('<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $file->file,
'alt="artefact:' . $file->file . '"');
$body = preg_replace($regexps, $subs, $body);
}
}
// Update the post if necessary
if ($body != $post->description) {
$postobj = new ArtefactTypeBlogPost($post->id, null);
$postobj->set('description', $body);
$postobj->commit();
}
}
}
}
}
}
return $status;
}
......
......@@ -484,7 +484,7 @@ class ArtefactTypeBlogPost extends ArtefactType {
// We need to make sure that the images in the post have the right viewid associated with them
$postcontent = $this->get('description');
if (isset($options['viewid'])) {
$postcontent = preg_replace('#(<img src=".*artefact/file/download\.php\?file=\d+)#', '\1&amp;view=' . $options['viewid'], $postcontent);
$postcontent = preg_replace('#(<img[^>]+src="[^>]+artefact/file/download\.php\?file=\d+)#', '\1&amp;view=' . $options['viewid'], $postcontent);
}
$smarty->assign('artefactdescription', $postcontent);
$smarty->assign('artefact', $this);
......
......@@ -132,9 +132,9 @@ if (!empty($uploads)) {
if (!empty($uploadartefact)) {
$originalbody = $body;
foreach ($uploadartefact as $k => $v) {
$regexps = array('/<img([^>]+)src="(artefact\/blog\/)?downloadtemp.php\?uploadnumber=' . $k .'&amp;createid=\d+/',
$regexps = array('/<img([^>]+)src="([^>]+)downloadtemp.php\?uploadnumber=' . $k .'&amp;createid=\d+/',
'/alt="uploaded:' . $k . '"/');
$subs = array('<img$1src="/artefact/file/download.php?file=' . $v,
$subs = array('<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $v,
'alt="artefact:' . $v . '"');
$body = preg_replace($regexps, $subs, $body);
}
......
......@@ -27,7 +27,7 @@
defined('INTERNAL') || die();
$config = new StdClass;
$config->version = 2006121501;
$config->release = '0.1';
$config->version = 2008012200;
$config->release = '0.1.1';
?>
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