Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions core/src/Revolution/Processors/Element/Template/GetList.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of the MODX Revolution package.
*
Expand All @@ -10,7 +11,6 @@

namespace MODX\Revolution\Processors\Element\Template;


use MODX\Revolution\modTemplate;
use xPDO\Om\xPDOObject;
use xPDO\Om\xPDOQuery;
Expand Down Expand Up @@ -73,18 +73,20 @@ public function beforeIteration(array $list)

public function prepareRow(xPDOObject $object)
{
$preview = $object->getPreviewUrl();

if (!empty($preview)) {
$preview = '';
$previewPath = $object->getPreviewPath();
if (!empty($previewPath)) {
$imageQuery = http_build_query([
'src' => $preview,
'src' => $previewPath,
'source' => $object->getPreviewSourceId(),
'w' => 335,
'h' => 236,
'HTTP_MODAUTH' => $this->modx->user->getUserToken($this->modx->context->get('key')),
'zc' => 1
]);

$preview = $this->modx->getOption('connectors_url', MODX_CONNECTORS_URL) . 'system/phpthumb.php?' . urldecode($imageQuery);
$connectorsUrl = $this->modx->getOption('connectors_url', MODX_CONNECTORS_URL);
$preview = $connectorsUrl . 'system/phpthumb.php?' . urldecode($imageQuery);
}

return [
Expand Down
4 changes: 2 additions & 2 deletions core/src/Revolution/Processors/System/PhpThumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public function initialize()
]);
$this->unsetProperty('wctx');
$this->unsetProperty('version');
error_reporting(E_ALL);
return true;
}

Expand All @@ -72,7 +71,8 @@ public function process()
return '';
}

if (strtolower(pathinfo($src, PATHINFO_EXTENSION)) === 'svg') {
$ext = pathinfo($src, PATHINFO_EXTENSION);
if (is_string($ext) && strtolower($ext) === 'svg') {
/* Skip thumbnail generation for svg and output the file directly */
header('Content-Type: image/svg+xml');
echo file_get_contents($src);
Expand Down
29 changes: 29 additions & 0 deletions core/src/Revolution/Sources/modMediaSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -1576,12 +1576,41 @@ public function setProperties($properties, $merge = false)
/**
* Prepare a src parameter to be rendered with phpThumb
*
* Converts local HTTP URLs to filesystem paths for Flysystem compatibility.
* Note: file_exists() is called when resolving URLs; for large template lists
* prefer passing path + source (getPreviewPath/getPreviewSourceId) to avoid this.
*
* @param string $src
*
* @return string
*/
public function prepareSrcForThumb($src)
{
if (substr($src, 0, 4) === 'http') {
$baseUrl = rtrim($this->ctx->getOption('base_url', null, MODX_BASE_URL), '/');
$basePath = $this->ctx->getOption('base_path', null, MODX_BASE_PATH);
$baseUrlPath = parse_url($baseUrl, PHP_URL_PATH);
$baseUrlPath = $baseUrlPath !== null ? rtrim($baseUrlPath, '/') : '';
$parsed = parse_url($src);
if (isset($parsed['path'])) {
$path = $parsed['path'];
if ($baseUrlPath !== '' && str_starts_with($path, $baseUrlPath)) {
$path = substr($path, strlen($baseUrlPath));
}
$path = ltrim($path, '/');
$resolved = $basePath . $path;
if (file_exists($resolved)) {
$sourceBase = rtrim($this->getBasePath(), DIRECTORY_SEPARATOR);
$resolvedNorm = rtrim($resolved, DIRECTORY_SEPARATOR);
if ($sourceBase !== '' && str_starts_with($resolvedNorm, $sourceBase)) {
$src = ltrim(substr($resolvedNorm, strlen($sourceBase)), DIRECTORY_SEPARATOR);
} else {
$src = $resolved;
}
}
}
}

try {
if (!$this->filesystem->fileExists($src)) {
return '';
Expand Down
72 changes: 72 additions & 0 deletions core/src/Revolution/modElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -1202,4 +1202,76 @@ public function getPreviewUrl()

return '';
}

/**
* Resolve preview file source: media source with file, or default.
*
* @return array|null ['sourceId' => int, 'path' => string] when resolved, path non-empty if in media source; null when preview_file empty
*/
private function resolvePreviewSource(): ?array
{
$previewfile = $this->get('preview_file');
if (empty($previewfile)) {
return null;
}

if ($this->get('source') > 0) {
$source = $this->getOne('Source');
if ($source && $source->get('is_stream')) {
$source->initialize();
$basePath = $source->getBasePath();
if ($basePath !== '' && file_exists($basePath . $previewfile)) {
return ['sourceId' => (int) $this->get('source'), 'path' => $previewfile];
}
}
}

return ['sourceId' => 1, 'path' => ''];
}

/**
* Get the filesystem path for the preview file for use with phpThumb.
*
* @return string Path relative to media source basePath, or absolute path.
*/
public function getPreviewPath()
{
$resolved = $this->resolvePreviewSource();
if ($resolved === null) {
return '';
}
if ($resolved['path'] !== '') {
return $resolved['path'];
}

$previewfile = $this->get('preview_file');
$basePath = $this->xpdo->getOption('base_path', null, MODX_BASE_PATH);
if (file_exists($basePath . $previewfile)) {
$assetsPrefix = 'assets' . DIRECTORY_SEPARATOR;
if (strpos($previewfile, $assetsPrefix) === 0) {
return substr($previewfile, strlen($assetsPrefix));
}
return $basePath . $previewfile;
}

if (file_exists($previewfile)) {
return $previewfile;
}

return '';
}

/**
* Get the media source ID to use for the preview file with phpThumb.
*
* @return int
*/
public function getPreviewSourceId()
{
$resolved = $this->resolvePreviewSource();
if ($resolved === null) {
return 1;
}
return $resolved['sourceId'];
}
}