Skip to content

Commit 6d99fd9

Browse files
committed
Portfolio: Include files linked in post and comments when exporting ZIP file - refs BT#22709
1 parent b0c75f4 commit 6d99fd9

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

main/inc/lib/PortfolioController.php

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,12 +2350,20 @@ public function exportZip(HttpRequest $httpRequest)
23502350
$itemDirectory = $item->getCreationDate()->format('Y-m-d-H-i-s');
23512351

23522352
$itemFilename = sprintf('%s/items/%s/item.html', $tempPortfolioDirectory, $itemDirectory);
2353-
$itemFileContent = $this->fixImagesSourcesToHtml($itemsHtml[$i]);
2353+
$imagePaths = [];
2354+
$itemFileContent = $this->fixImagesSourcesToHtml($itemsHtml[$i], $imagePaths);
23542355

23552356
$fs->dumpFile($itemFilename, $itemFileContent);
23562357

23572358
$filenames[] = $itemFilename;
23582359

2360+
foreach ($imagePaths as $imagePath) {
2361+
$inlineFile = dirname($itemFilename).'/'.basename($imagePath);
2362+
$filenames[] = $inlineFile;
2363+
$fs->copy($imagePath, $inlineFile);
2364+
}
2365+
2366+
23592367
$attachments = $attachmentsRepo->findFromItem($item);
23602368

23612369
/** @var PortfolioAttachment $attachment */
@@ -2397,13 +2405,20 @@ public function exportZip(HttpRequest $httpRequest)
23972405
foreach ($comments as $i => $comment) {
23982406
$commentDirectory = $comment->getDate()->format('Y-m-d-H-i-s');
23992407

2400-
$commentFileContent = $this->fixImagesSourcesToHtml($commentsHtml[$i]);
2408+
$imagePaths = [];
2409+
$commentFileContent = $this->fixImagesSourcesToHtml($commentsHtml[$i], $imagePaths);
24012410
$commentFilename = sprintf('%s/comments/%s/comment.html', $tempPortfolioDirectory, $commentDirectory);
24022411

24032412
$fs->dumpFile($commentFilename, $commentFileContent);
24042413

24052414
$filenames[] = $commentFilename;
24062415

2416+
foreach ($imagePaths as $imagePath) {
2417+
$inlineFile = dirname($commentFilename).'/'.basename($imagePath);
2418+
$filenames[] = $inlineFile;
2419+
$fs->copy($imagePath, $inlineFile);
2420+
}
2421+
24072422
$attachments = $attachmentsRepo->findFromComment($comment);
24082423

24092424
/** @var PortfolioAttachment $attachment */
@@ -4277,7 +4292,7 @@ private function getCommentsInHtmlFormatted(array $comments): array
42774292
return $commentsHtml;
42784293
}
42794294

4280-
private function fixImagesSourcesToHtml(string $htmlContent): string
4295+
private function fixImagesSourcesToHtml(string $htmlContent, array &$imagePaths): string
42814296
{
42824297
$doc = new DOMDocument();
42834298
@$doc->loadHTML($htmlContent);
@@ -4288,30 +4303,38 @@ private function fixImagesSourcesToHtml(string $htmlContent): string
42884303
return $htmlContent;
42894304
}
42904305

4291-
$webCoursePath = api_get_path(WEB_COURSE_PATH);
4292-
$webUploadPath = api_get_path(WEB_UPLOAD_PATH);
4306+
$webPath = api_get_path(WEB_PATH);
4307+
$sysPath = rtrim(api_get_path(SYS_PATH), '/');
42934308

42944309
/** @var \DOMElement $element */
42954310
foreach ($elements as $element) {
42964311
$src = trim($element->getAttribute('src'));
42974312

4298-
if (strpos($src, 'http') === 0) {
4313+
if (!str_starts_with($src, '/')
4314+
&& !str_starts_with($src, $webPath)
4315+
) {
42994316
continue;
43004317
}
43014318

4319+
$src = str_replace($webPath, '/', $src);
4320+
43024321
if (strpos($src, '/app/upload/') === 0) {
4322+
$imagePaths[] = $sysPath.$src;
4323+
43034324
$element->setAttribute(
43044325
'src',
4305-
preg_replace('/\/app/upload\//', $webUploadPath, $src, 1)
4326+
basename($src)
43064327
);
43074328

43084329
continue;
43094330
}
43104331

43114332
if (strpos($src, '/courses/') === 0) {
4333+
$imagePaths[] = $sysPath.'/app'.$src;
4334+
43124335
$element->setAttribute(
43134336
'src',
4314-
preg_replace('/\/courses\//', $webCoursePath, $src, 1)
4337+
basename($src)
43154338
);
43164339

43174340
continue;

0 commit comments

Comments
 (0)