@@ -2351,7 +2351,7 @@ public function exportZip(HttpRequest $httpRequest)
2351
2351
2352
2352
$ itemFilename = sprintf ('%s/items/%s/item.html ' , $ tempPortfolioDirectory , $ itemDirectory );
2353
2353
$ imagePaths = [];
2354
- $ itemFileContent = $ this ->fixImagesSourcesToHtml ($ itemsHtml [$ i ], $ imagePaths );
2354
+ $ itemFileContent = $ this ->fixMediaSourcesToHtml ($ itemsHtml [$ i ], $ imagePaths );
2355
2355
2356
2356
$ fs ->dumpFile ($ itemFilename , $ itemFileContent );
2357
2357
@@ -2406,7 +2406,7 @@ public function exportZip(HttpRequest $httpRequest)
2406
2406
$ commentDirectory = $ comment ->getDate ()->format ('Y-m-d-H-i-s ' );
2407
2407
2408
2408
$ imagePaths = [];
2409
- $ commentFileContent = $ this ->fixImagesSourcesToHtml ($ commentsHtml [$ i ], $ imagePaths );
2409
+ $ commentFileContent = $ this ->fixMediaSourcesToHtml ($ commentsHtml [$ i ], $ imagePaths );
2410
2410
$ commentFilename = sprintf ('%s/comments/%s/comment.html ' , $ tempPortfolioDirectory , $ commentDirectory );
2411
2411
2412
2412
$ fs ->dumpFile ($ commentFilename , $ commentFileContent );
@@ -4292,21 +4292,44 @@ private function getCommentsInHtmlFormatted(array $comments): array
4292
4292
return $ commentsHtml ;
4293
4293
}
4294
4294
4295
- private function fixImagesSourcesToHtml (string $ htmlContent , array &$ imagePaths ): string
4295
+ /**
4296
+ * @param string $htmlContent
4297
+ * @param array $imagePaths Relative paths found in $htmlContent
4298
+ *
4299
+ * @return string
4300
+ */
4301
+ private function fixMediaSourcesToHtml (string $ htmlContent , array &$ imagePaths ): string
4296
4302
{
4297
4303
$ doc = new DOMDocument ();
4298
4304
@$ doc ->loadHTML ($ htmlContent );
4299
4305
4300
- $ elements = $ doc ->getElementsByTagName ('img ' );
4306
+ $ tagsWithSrc = ['img ' , 'video ' , 'audio ' , 'source ' ];
4307
+ /** @var array<int, \DOMElement> $elements */
4308
+ $ elements = [];
4309
+
4310
+ foreach ($ tagsWithSrc as $ tag ) {
4311
+ foreach ($ doc ->getElementsByTagName ($ tag ) as $ element ) {
4312
+ if ($ element ->hasAttribute ('src ' )) {
4313
+ $ elements [] = $ element ;
4314
+ }
4315
+ }
4316
+ }
4301
4317
4302
- if (empty ($ elements-> length )) {
4318
+ if (empty ($ elements )) {
4303
4319
return $ htmlContent ;
4304
4320
}
4305
4321
4322
+ /** @var array<int, \DOMElement> $anchorElements */
4323
+ $ anchorElements = $ doc ->getElementsByTagName ('a ' );
4324
+
4306
4325
$ webPath = api_get_path (WEB_PATH );
4307
4326
$ sysPath = rtrim (api_get_path (SYS_PATH ), '/ ' );
4308
4327
4309
- /** @var \DOMElement $element */
4328
+ $ paths = [
4329
+ '/app/upload/ ' => $ sysPath ,
4330
+ '/courses/ ' => $ sysPath .'/app '
4331
+ ];
4332
+
4310
4333
foreach ($ elements as $ element ) {
4311
4334
$ src = trim ($ element ->getAttribute ('src ' ));
4312
4335
@@ -4316,28 +4339,25 @@ private function fixImagesSourcesToHtml(string $htmlContent, array &$imagePaths)
4316
4339
continue ;
4317
4340
}
4318
4341
4319
- $ src = str_replace ($ webPath , '/ ' , $ src );
4320
-
4321
- if (strpos ($ src , '/app/upload/ ' ) === 0 ) {
4322
- $ imagePaths [] = $ sysPath .$ src ;
4323
-
4324
- $ element ->setAttribute (
4325
- 'src ' ,
4326
- basename ($ src )
4327
- );
4342
+ if ($ anchorElements ->length > 0 ) {
4343
+ foreach ($ anchorElements as $ anchorElement ) {
4344
+ if (!$ anchorElement ->hasAttribute ('href ' )) {
4345
+ continue ;
4346
+ }
4328
4347
4329
- continue ;
4348
+ if ($ src === $ anchorElement ->getAttribute ('href ' )) {
4349
+ $ anchorElement ->setAttribute ('href ' , basename ($ src ));
4350
+ }
4351
+ }
4330
4352
}
4331
4353
4332
- if (strpos ($ src , '/courses/ ' ) === 0 ) {
4333
- $ imagePaths [] = $ sysPath .'/app ' .$ src ;
4334
-
4335
- $ element ->setAttribute (
4336
- 'src ' ,
4337
- basename ($ src )
4338
- );
4354
+ $ src = str_replace ($ webPath , '/ ' , $ src );
4339
4355
4340
- continue ;
4356
+ foreach ($ paths as $ prefix => $ basePath ) {
4357
+ if (str_starts_with ($ src , $ prefix )) {
4358
+ $ imagePaths [] = $ basePath .urldecode ($ src );
4359
+ $ element ->setAttribute ('src ' , basename ($ src ));
4360
+ }
4341
4361
}
4342
4362
}
4343
4363
0 commit comments