1616use Chamilo \UserBundle \Entity \User ;
1717use Doctrine \ORM \Query \Expr \Join ;
1818use Mpdf \MpdfException ;
19+ use Symfony \Component \Filesystem \Exception \FileNotFoundException ;
1920use Symfony \Component \Filesystem \Filesystem ;
2021use Symfony \Component \HttpFoundation \Request as HttpRequest ;
2122
@@ -1417,7 +1418,7 @@ public function view(Portfolio $item, $urlUser)
14171418 );
14181419
14191420 $ urlUserString = "" ;
1420- if (isset ($ urlUser )) {
1421+ if (! empty ($ urlUser )) {
14211422 $ urlUserString = "user= " .$ urlUser ;
14221423 }
14231424
@@ -2350,12 +2351,24 @@ public function exportZip(HttpRequest $httpRequest)
23502351 $ itemDirectory = $ item ->getCreationDate ()->format ('Y-m-d-H-i-s ' );
23512352
23522353 $ itemFilename = sprintf ('%s/items/%s/item.html ' , $ tempPortfolioDirectory , $ itemDirectory );
2353- $ itemFileContent = $ this ->fixImagesSourcesToHtml ($ itemsHtml [$ i ]);
2354+ $ imagePaths = [];
2355+ $ itemFileContent = $ this ->fixMediaSourcesToHtml ($ itemsHtml [$ i ], $ imagePaths );
23542356
23552357 $ fs ->dumpFile ($ itemFilename , $ itemFileContent );
23562358
23572359 $ filenames [] = $ itemFilename ;
23582360
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+
23592372 $ attachments = $ attachmentsRepo ->findFromItem ($ item );
23602373
23612374 /** @var PortfolioAttachment $attachment */
@@ -2367,12 +2380,15 @@ public function exportZip(HttpRequest $httpRequest)
23672380 $ attachment ->getFilename ()
23682381 );
23692382
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+ }
23762392 }
23772393
23782394 $ tblItemsData [] = [
@@ -2397,13 +2413,25 @@ public function exportZip(HttpRequest $httpRequest)
23972413 foreach ($ comments as $ i => $ comment ) {
23982414 $ commentDirectory = $ comment ->getDate ()->format ('Y-m-d-H-i-s ' );
23992415
2400- $ commentFileContent = $ this ->fixImagesSourcesToHtml ($ commentsHtml [$ i ]);
2416+ $ imagePaths = [];
2417+ $ commentFileContent = $ this ->fixMediaSourcesToHtml ($ commentsHtml [$ i ], $ imagePaths );
24012418 $ commentFilename = sprintf ('%s/comments/%s/comment.html ' , $ tempPortfolioDirectory , $ commentDirectory );
24022419
24032420 $ fs ->dumpFile ($ commentFilename , $ commentFileContent );
24042421
24052422 $ filenames [] = $ commentFilename ;
24062423
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+
24072435 $ attachments = $ attachmentsRepo ->findFromComment ($ comment );
24082436
24092437 /** @var PortfolioAttachment $attachment */
@@ -2415,12 +2443,15 @@ public function exportZip(HttpRequest $httpRequest)
24152443 $ attachment ->getFilename ()
24162444 );
24172445
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+ }
24242455 }
24252456
24262457 $ tblCommentsData [] = [
@@ -4277,44 +4308,73 @@ private function getCommentsInHtmlFormatted(array $comments): array
42774308 return $ commentsHtml ;
42784309 }
42794310
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
42814318 {
42824319 $ doc = new DOMDocument ();
42834320 @$ doc ->loadHTML ($ htmlContent );
42844321
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+ }
42864333
4287- if (empty ($ elements-> length )) {
4334+ if (empty ($ elements )) {
42884335 return $ htmlContent ;
42894336 }
42904337
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+ ];
42934348
4294- /** @var \DOMElement $element */
42954349 foreach ($ elements as $ element ) {
42964350 $ src = trim ($ element ->getAttribute ('src ' ));
42974351
4298- if (strpos ($ src , 'http ' ) === 0 ) {
4352+ if (!str_starts_with ($ src , '/ ' )
4353+ && !str_starts_with ($ src , $ webPath )
4354+ ) {
42994355 continue ;
43004356 }
43014357
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+ }
43074364
4308- continue ;
4365+ if ($ src === $ anchorElement ->getAttribute ('href ' )) {
4366+ $ anchorElement ->setAttribute ('href ' , basename ($ src ));
4367+ }
4368+ }
43094369 }
43104370
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 );
43164372
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+ }
43184378 }
43194379 }
43204380
0 commit comments