Allow hotlinking images

This commit is contained in:
2026-01-26 09:38:52 +01:00
parent 16e80f81c0
commit 7a4ce8609c
4 changed files with 68 additions and 33 deletions

View File

@@ -45,34 +45,26 @@ class syntax_plugin_luxtools_image extends SyntaxPlugin
// Remove the leading {{image> and trailing }}
$match = substr($match, strlen('{{image>'), -2);
// Split path and caption by |
$parts = explode('|', $match, 2);
// Split by | into: path, caption, options
// Format: {{image>path|caption|options}}
$parts = explode('|', $match, 3);
$pathPart = trim($parts[0]);
$caption = isset($parts[1]) ? trim($parts[1]) : '';
$optionStr = isset($parts[2]) ? trim($parts[2]) : '';
// Parse optional parameters from path (e.g., /path/image.jpg?200x150&right)
// Supported formats:
// ?200 - width only
// ?200x150 - width and height
// ?left - alignment only (left, right, center)
// ?200&right - width and alignment
// ?200x150&center - full options
// Parse options from third part (e.g., "200x150&right")
$width = null;
$height = null;
$align = null; // Will use default if not specified
$align = null;
if (strpos($pathPart, '?') !== false) {
[$pathPart, $paramStr] = explode('?', $pathPart, 2);
$paramParts = explode('&', $paramStr);
foreach ($paramParts as $param) {
if ($optionStr !== '') {
$optionParts = explode('&', $optionStr);
foreach ($optionParts as $param) {
$param = trim($param);
if ($param === '') continue;
// Check if it's an alignment keyword
if (in_array($param, ['left', 'right', 'center'], true)) {
$align = $param;
// Check if it's a size specification (digits, optionally with 'x' and more digits)
} elseif (preg_match('/^(\d+)(?:x(\d+))?$/', $param, $m)) {
$width = (int)$m[1];
if (isset($m[2]) && $m[2] !== '') {
@@ -82,10 +74,12 @@ class syntax_plugin_luxtools_image extends SyntaxPlugin
}
}
$path = Path::cleanPath($pathPart, false);
$isRemote = ThumbnailHelper::isRemoteUrl($pathPart);
$path = $isRemote ? $pathPart : Path::cleanPath($pathPart, false);
return [
'path' => $path,
'is_remote' => $isRemote,
'caption' => $caption,
'align' => $align,
'width' => $width,
@@ -120,6 +114,23 @@ class syntax_plugin_luxtools_image extends SyntaxPlugin
}
}
if (!empty($data['is_remote'])) {
if (empty($data['path']) || !ThumbnailHelper::isRemoteUrl($data['path'])) {
$renderer->cdata('[n/a: Invalid URL]');
return true;
}
// Remote images: link directly, no proxying or thumbnailing
$thumb = [
'url' => $data['path'],
'isFinal' => true,
'thumbUrl' => $data['path'],
];
$this->renderImageBox($renderer, $thumb, $data['path'], $data);
return true;
}
try {
$pathHelper = new Path($this->getConf('paths'));
// Use addTrailingSlash=false since this is a file path, not a directory
@@ -162,7 +173,7 @@ class syntax_plugin_luxtools_image extends SyntaxPlugin
);
// Build full-size URL for linking
$fullUrl = $this->buildFileUrl($pathInfo, null, null, false);
$fullUrl = $this->buildImageUrl($pathInfo, null, null, false);
$this->renderImageBox($renderer, $thumb, $fullUrl, $data);
@@ -170,15 +181,15 @@ class syntax_plugin_luxtools_image extends SyntaxPlugin
}
/**
* Build the file.php URL for the image.
* Build the file.php URL for a local image.
*
* @param array $pathInfo Path info from Path helper
* @param array $pathInfo Path info array from Path helper
* @param int|null $width Optional width
* @param int|null $height Optional height
* @param bool $thumbnail Whether to generate a thumbnail
* @return string
*/
protected function buildFileUrl(array $pathInfo, ?int $width, ?int $height, bool $thumbnail = false): string
protected function buildImageUrl(array $pathInfo, ?int $width, ?int $height, bool $thumbnail): string
{
global $ID;
@@ -189,9 +200,8 @@ class syntax_plugin_luxtools_image extends SyntaxPlugin
];
if ($thumbnail && ($width !== null || $height !== null)) {
// Enable thumbnail mode (same as gallery logic)
$params['thumb'] = 1;
$params['q'] = 80; // JPEG quality
$params['q'] = 80;
}
if ($width !== null) {