@@ -2351,7 +2351,7 @@ public function exportZip(HttpRequest $httpRequest)
23512351
23522352 $ itemFilename = sprintf ('%s/items/%s/item.html ' , $ tempPortfolioDirectory , $ itemDirectory );
23532353 $ imagePaths = [];
2354- $ itemFileContent = $ this ->fixImagesSourcesToHtml ($ itemsHtml [$ i ], $ imagePaths );
2354+ $ itemFileContent = $ this ->fixMediaSourcesToHtml ($ itemsHtml [$ i ], $ imagePaths );
23552355
23562356 $ fs ->dumpFile ($ itemFilename , $ itemFileContent );
23572357
@@ -2406,7 +2406,7 @@ public function exportZip(HttpRequest $httpRequest)
24062406 $ commentDirectory = $ comment ->getDate ()->format ('Y-m-d-H-i-s ' );
24072407
24082408 $ imagePaths = [];
2409- $ commentFileContent = $ this ->fixImagesSourcesToHtml ($ commentsHtml [$ i ], $ imagePaths );
2409+ $ commentFileContent = $ this ->fixMediaSourcesToHtml ($ commentsHtml [$ i ], $ imagePaths );
24102410 $ commentFilename = sprintf ('%s/comments/%s/comment.html ' , $ tempPortfolioDirectory , $ commentDirectory );
24112411
24122412 $ fs ->dumpFile ($ commentFilename , $ commentFileContent );
@@ -4292,21 +4292,44 @@ private function getCommentsInHtmlFormatted(array $comments): array
42924292 return $ commentsHtml ;
42934293 }
42944294
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
42964302 {
42974303 $ doc = new DOMDocument ();
42984304 @$ doc ->loadHTML ($ htmlContent );
42994305
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+ }
43014317
4302- if (empty ($ elements-> length )) {
4318+ if (empty ($ elements )) {
43034319 return $ htmlContent ;
43044320 }
43054321
4322+ /** @var array<int, \DOMElement> $anchorElements */
4323+ $ anchorElements = $ doc ->getElementsByTagName ('a ' );
4324+
43064325 $ webPath = api_get_path (WEB_PATH );
43074326 $ sysPath = rtrim (api_get_path (SYS_PATH ), '/ ' );
43084327
4309- /** @var \DOMElement $element */
4328+ $ paths = [
4329+ '/app/upload/ ' => $ sysPath ,
4330+ '/courses/ ' => $ sysPath .'/app '
4331+ ];
4332+
43104333 foreach ($ elements as $ element ) {
43114334 $ src = trim ($ element ->getAttribute ('src ' ));
43124335
@@ -4316,28 +4339,25 @@ private function fixImagesSourcesToHtml(string $htmlContent, array &$imagePaths)
43164339 continue ;
43174340 }
43184341
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+ }
43284347
4329- continue ;
4348+ if ($ src === $ anchorElement ->getAttribute ('href ' )) {
4349+ $ anchorElement ->setAttribute ('href ' , basename ($ src ));
4350+ }
4351+ }
43304352 }
43314353
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 );
43394355
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+ }
43414361 }
43424362 }
43434363
0 commit comments