Upgrade 1-11.38

This commit is contained in:
xesmyd
2026-03-30 14:10:30 +02:00
parent f2a7e6d1fc
commit ac648ef29d
24665 changed files with 69682 additions and 2205004 deletions
+91 -34
View File
@@ -16,6 +16,7 @@ use Chamilo\CourseBundle\Entity\CItemProperty;
use Chamilo\UserBundle\Entity\User;
use Doctrine\ORM\Query\Expr\Join;
use Mpdf\MpdfException;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
@@ -1417,7 +1418,7 @@ class PortfolioController
);
$urlUserString = "";
if (isset($urlUser)) {
if (!empty($urlUser)) {
$urlUserString = "user=".$urlUser;
}
@@ -2350,12 +2351,24 @@ class PortfolioController
$itemDirectory = $item->getCreationDate()->format('Y-m-d-H-i-s');
$itemFilename = sprintf('%s/items/%s/item.html', $tempPortfolioDirectory, $itemDirectory);
$itemFileContent = $this->fixImagesSourcesToHtml($itemsHtml[$i]);
$imagePaths = [];
$itemFileContent = $this->fixMediaSourcesToHtml($itemsHtml[$i], $imagePaths);
$fs->dumpFile($itemFilename, $itemFileContent);
$filenames[] = $itemFilename;
foreach ($imagePaths as $imagePath) {
$inlineFile = dirname($itemFilename).'/'.basename($imagePath);
try {
$filenames[] = $inlineFile;
$fs->copy($imagePath, $inlineFile);
} catch (FileNotFoundException $notFoundException) {
continue;
}
}
$attachments = $attachmentsRepo->findFromItem($item);
/** @var PortfolioAttachment $attachment */
@@ -2367,12 +2380,15 @@ class PortfolioController
$attachment->getFilename()
);
$fs->copy(
$attachmentsDirectory.$attachment->getPath(),
$attachmentFilename
);
$filenames[] = $attachmentFilename;
try {
$fs->copy(
$attachmentsDirectory.$attachment->getPath(),
$attachmentFilename
);
$filenames[] = $attachmentFilename;
} catch (FileNotFoundException $notFoundException) {
continue;
}
}
$tblItemsData[] = [
@@ -2397,13 +2413,25 @@ class PortfolioController
foreach ($comments as $i => $comment) {
$commentDirectory = $comment->getDate()->format('Y-m-d-H-i-s');
$commentFileContent = $this->fixImagesSourcesToHtml($commentsHtml[$i]);
$imagePaths = [];
$commentFileContent = $this->fixMediaSourcesToHtml($commentsHtml[$i], $imagePaths);
$commentFilename = sprintf('%s/comments/%s/comment.html', $tempPortfolioDirectory, $commentDirectory);
$fs->dumpFile($commentFilename, $commentFileContent);
$filenames[] = $commentFilename;
foreach ($imagePaths as $imagePath) {
$inlineFile = dirname($commentFilename).'/'.basename($imagePath);
try {
$filenames[] = $inlineFile;
$fs->copy($imagePath, $inlineFile);
} catch (FileNotFoundException $notFoundException) {
continue;
}
}
$attachments = $attachmentsRepo->findFromComment($comment);
/** @var PortfolioAttachment $attachment */
@@ -2415,12 +2443,15 @@ class PortfolioController
$attachment->getFilename()
);
$fs->copy(
$attachmentsDirectory.$attachment->getPath(),
$attachmentFilename
);
$filenames[] = $attachmentFilename;
try {
$fs->copy(
$attachmentsDirectory.$attachment->getPath(),
$attachmentFilename
);
$filenames[] = $attachmentFilename;
} catch (FileNotFoundException $notFoundException) {
continue;
}
}
$tblCommentsData[] = [
@@ -4277,44 +4308,70 @@ class PortfolioController
return $commentsHtml;
}
private function fixImagesSourcesToHtml(string $htmlContent): string
/**
* @param array $imagePaths Relative paths found in $htmlContent
*/
private function fixMediaSourcesToHtml(string $htmlContent, array &$imagePaths): string
{
$doc = new DOMDocument();
@$doc->loadHTML($htmlContent);
$elements = $doc->getElementsByTagName('img');
$tagsWithSrc = ['img', 'video', 'audio', 'source'];
/** @var array<int, \DOMElement> $elements */
$elements = [];
if (empty($elements->length)) {
foreach ($tagsWithSrc as $tag) {
foreach ($doc->getElementsByTagName($tag) as $element) {
if ($element->hasAttribute('src')) {
$elements[] = $element;
}
}
}
if (empty($elements)) {
return $htmlContent;
}
$webCoursePath = api_get_path(WEB_COURSE_PATH);
$webUploadPath = api_get_path(WEB_UPLOAD_PATH);
/** @var array<int, \DOMElement> $anchorElements */
$anchorElements = $doc->getElementsByTagName('a');
$webPath = api_get_path(WEB_PATH);
$sysPath = rtrim(api_get_path(SYS_PATH), '/');
$paths = [
'/app/upload/' => $sysPath,
'/courses/' => $sysPath.'/app',
];
/** @var \DOMElement $element */
foreach ($elements as $element) {
$src = trim($element->getAttribute('src'));
if (strpos($src, 'http') === 0) {
if (!str_starts_with($src, '/')
&& !str_starts_with($src, $webPath)
) {
continue;
}
if (strpos($src, '/app/upload/') === 0) {
$element->setAttribute(
'src',
preg_replace('/\/app/upload\//', $webUploadPath, $src, 1)
);
// to search anchors linking to files
if ($anchorElements->length > 0) {
foreach ($anchorElements as $anchorElement) {
if (!$anchorElement->hasAttribute('href')) {
continue;
}
continue;
if ($src === $anchorElement->getAttribute('href')) {
$anchorElement->setAttribute('href', basename($src));
}
}
}
if (strpos($src, '/courses/') === 0) {
$element->setAttribute(
'src',
preg_replace('/\/courses\//', $webCoursePath, $src, 1)
);
$src = str_replace($webPath, '/', $src);
continue;
foreach ($paths as $prefix => $basePath) {
if (str_starts_with($src, $prefix)) {
$imagePaths[] = $basePath.urldecode($src);
$element->setAttribute('src', basename($src));
}
}
}