16
16
use Chamilo \UserBundle \Entity \User ;
17
17
use Doctrine \ORM \Query \Expr \Join ;
18
18
use Mpdf \MpdfException ;
19
+ use Symfony \Component \Filesystem \Exception \FileNotFoundException ;
19
20
use Symfony \Component \Filesystem \Filesystem ;
20
21
use Symfony \Component \HttpFoundation \Request as HttpRequest ;
21
22
@@ -1417,7 +1418,7 @@ public function view(Portfolio $item, $urlUser)
1417
1418
);
1418
1419
1419
1420
$ urlUserString = "" ;
1420
- if (isset ($ urlUser )) {
1421
+ if (! empty ($ urlUser )) {
1421
1422
$ urlUserString = "user= " .$ urlUser ;
1422
1423
}
1423
1424
@@ -2350,12 +2351,24 @@ public function exportZip(HttpRequest $httpRequest)
2350
2351
$ itemDirectory = $ item ->getCreationDate ()->format ('Y-m-d-H-i-s ' );
2351
2352
2352
2353
$ itemFilename = sprintf ('%s/items/%s/item.html ' , $ tempPortfolioDirectory , $ itemDirectory );
2353
- $ itemFileContent = $ this ->fixImagesSourcesToHtml ($ itemsHtml [$ i ]);
2354
+ $ imagePaths = [];
2355
+ $ itemFileContent = $ this ->fixMediaSourcesToHtml ($ itemsHtml [$ i ], $ imagePaths );
2354
2356
2355
2357
$ fs ->dumpFile ($ itemFilename , $ itemFileContent );
2356
2358
2357
2359
$ filenames [] = $ itemFilename ;
2358
2360
2361
+ foreach ($ imagePaths as $ imagePath ) {
2362
+ $ inlineFile = dirname ($ itemFilename ).'/ ' .basename ($ imagePath );
2363
+
2364
+ try {
2365
+ $ filenames [] = $ inlineFile ;
2366
+ $ fs ->copy ($ imagePath , $ inlineFile );
2367
+ } catch (FileNotFoundException $ notFoundException ) {
2368
+ continue ;
2369
+ }
2370
+ }
2371
+
2359
2372
$ attachments = $ attachmentsRepo ->findFromItem ($ item );
2360
2373
2361
2374
/** @var PortfolioAttachment $attachment */
@@ -2367,12 +2380,15 @@ public function exportZip(HttpRequest $httpRequest)
2367
2380
$ attachment ->getFilename ()
2368
2381
);
2369
2382
2370
- $ fs ->copy (
2371
- $ attachmentsDirectory .$ attachment ->getPath (),
2372
- $ attachmentFilename
2373
- );
2374
-
2375
- $ filenames [] = $ attachmentFilename ;
2383
+ try {
2384
+ $ fs ->copy (
2385
+ $ attachmentsDirectory .$ attachment ->getPath (),
2386
+ $ attachmentFilename
2387
+ );
2388
+ $ filenames [] = $ attachmentFilename ;
2389
+ } catch (FileNotFoundException $ notFoundException ) {
2390
+ continue ;
2391
+ }
2376
2392
}
2377
2393
2378
2394
$ tblItemsData [] = [
@@ -2397,13 +2413,25 @@ public function exportZip(HttpRequest $httpRequest)
2397
2413
foreach ($ comments as $ i => $ comment ) {
2398
2414
$ commentDirectory = $ comment ->getDate ()->format ('Y-m-d-H-i-s ' );
2399
2415
2400
- $ commentFileContent = $ this ->fixImagesSourcesToHtml ($ commentsHtml [$ i ]);
2416
+ $ imagePaths = [];
2417
+ $ commentFileContent = $ this ->fixMediaSourcesToHtml ($ commentsHtml [$ i ], $ imagePaths );
2401
2418
$ commentFilename = sprintf ('%s/comments/%s/comment.html ' , $ tempPortfolioDirectory , $ commentDirectory );
2402
2419
2403
2420
$ fs ->dumpFile ($ commentFilename , $ commentFileContent );
2404
2421
2405
2422
$ filenames [] = $ commentFilename ;
2406
2423
2424
+ foreach ($ imagePaths as $ imagePath ) {
2425
+ $ inlineFile = dirname ($ commentFilename ).'/ ' .basename ($ imagePath );
2426
+
2427
+ try {
2428
+ $ filenames [] = $ inlineFile ;
2429
+ $ fs ->copy ($ imagePath , $ inlineFile );
2430
+ } catch (FileNotFoundException $ notFoundException ) {
2431
+ continue ;
2432
+ }
2433
+ }
2434
+
2407
2435
$ attachments = $ attachmentsRepo ->findFromComment ($ comment );
2408
2436
2409
2437
/** @var PortfolioAttachment $attachment */
@@ -2415,12 +2443,15 @@ public function exportZip(HttpRequest $httpRequest)
2415
2443
$ attachment ->getFilename ()
2416
2444
);
2417
2445
2418
- $ fs ->copy (
2419
- $ attachmentsDirectory .$ attachment ->getPath (),
2420
- $ attachmentFilename
2421
- );
2422
-
2423
- $ filenames [] = $ attachmentFilename ;
2446
+ try {
2447
+ $ fs ->copy (
2448
+ $ attachmentsDirectory .$ attachment ->getPath (),
2449
+ $ attachmentFilename
2450
+ );
2451
+ $ filenames [] = $ attachmentFilename ;
2452
+ } catch (FileNotFoundException $ notFoundException ) {
2453
+ continue ;
2454
+ }
2424
2455
}
2425
2456
2426
2457
$ tblCommentsData [] = [
@@ -4277,44 +4308,73 @@ private function getCommentsInHtmlFormatted(array $comments): array
4277
4308
return $ commentsHtml ;
4278
4309
}
4279
4310
4280
- private function fixImagesSourcesToHtml (string $ htmlContent ): string
4311
+ /**
4312
+ * @param string $htmlContent
4313
+ * @param array $imagePaths Relative paths found in $htmlContent
4314
+ *
4315
+ * @return string
4316
+ */
4317
+ private function fixMediaSourcesToHtml (string $ htmlContent , array &$ imagePaths ): string
4281
4318
{
4282
4319
$ doc = new DOMDocument ();
4283
4320
@$ doc ->loadHTML ($ htmlContent );
4284
4321
4285
- $ elements = $ doc ->getElementsByTagName ('img ' );
4322
+ $ tagsWithSrc = ['img ' , 'video ' , 'audio ' , 'source ' ];
4323
+ /** @var array<int, \DOMElement> $elements */
4324
+ $ elements = [];
4325
+
4326
+ foreach ($ tagsWithSrc as $ tag ) {
4327
+ foreach ($ doc ->getElementsByTagName ($ tag ) as $ element ) {
4328
+ if ($ element ->hasAttribute ('src ' )) {
4329
+ $ elements [] = $ element ;
4330
+ }
4331
+ }
4332
+ }
4286
4333
4287
- if (empty ($ elements-> length )) {
4334
+ if (empty ($ elements )) {
4288
4335
return $ htmlContent ;
4289
4336
}
4290
4337
4291
- $ webCoursePath = api_get_path (WEB_COURSE_PATH );
4292
- $ webUploadPath = api_get_path (WEB_UPLOAD_PATH );
4338
+ /** @var array<int, \DOMElement> $anchorElements */
4339
+ $ anchorElements = $ doc ->getElementsByTagName ('a ' );
4340
+
4341
+ $ webPath = api_get_path (WEB_PATH );
4342
+ $ sysPath = rtrim (api_get_path (SYS_PATH ), '/ ' );
4343
+
4344
+ $ paths = [
4345
+ '/app/upload/ ' => $ sysPath ,
4346
+ '/courses/ ' => $ sysPath .'/app '
4347
+ ];
4293
4348
4294
- /** @var \DOMElement $element */
4295
4349
foreach ($ elements as $ element ) {
4296
4350
$ src = trim ($ element ->getAttribute ('src ' ));
4297
4351
4298
- if (strpos ($ src , 'http ' ) === 0 ) {
4352
+ if (!str_starts_with ($ src , '/ ' )
4353
+ && !str_starts_with ($ src , $ webPath )
4354
+ ) {
4299
4355
continue ;
4300
4356
}
4301
4357
4302
- if (strpos ($ src , '/app/upload/ ' ) === 0 ) {
4303
- $ element ->setAttribute (
4304
- 'src ' ,
4305
- preg_replace ('/\/app/upload\// ' , $ webUploadPath , $ src , 1 )
4306
- );
4358
+ // to search anchors linking to files
4359
+ if ($ anchorElements ->length > 0 ) {
4360
+ foreach ($ anchorElements as $ anchorElement ) {
4361
+ if (!$ anchorElement ->hasAttribute ('href ' )) {
4362
+ continue ;
4363
+ }
4307
4364
4308
- continue ;
4365
+ if ($ src === $ anchorElement ->getAttribute ('href ' )) {
4366
+ $ anchorElement ->setAttribute ('href ' , basename ($ src ));
4367
+ }
4368
+ }
4309
4369
}
4310
4370
4311
- if (strpos ($ src , '/courses/ ' ) === 0 ) {
4312
- $ element ->setAttribute (
4313
- 'src ' ,
4314
- preg_replace ('/\/courses\// ' , $ webCoursePath , $ src , 1 )
4315
- );
4371
+ $ src = str_replace ($ webPath , '/ ' , $ src );
4316
4372
4317
- continue ;
4373
+ foreach ($ paths as $ prefix => $ basePath ) {
4374
+ if (str_starts_with ($ src , $ prefix )) {
4375
+ $ imagePaths [] = $ basePath .urldecode ($ src );
4376
+ $ element ->setAttribute ('src ' , basename ($ src ));
4377
+ }
4318
4378
}
4319
4379
}
4320
4380
0 commit comments