-
Notifications
You must be signed in to change notification settings - Fork 1
/
boot.php
96 lines (70 loc) · 3.13 KB
/
boot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
if (rex_addon::get('cronjob')->isAvailable()) {
rex_cronjob_manager::registerType(\FactFinder\FfGptTools\lib\FfGptToolsCronjob::class);
}
if (rex::isFrontend()) {
rex_extension::register('OUTPUT_FILTER', static function (rex_extension_point $ep) {
// Log to confirm the extension is being executed
if (rex::isDebugMode()) {
rex_logger::logError(1, 'OUTPUT_FILTER extension point reached.', __FILE__, __LINE__);
}
$content = $ep->getSubject();
// Log the content length
if (rex::isDebugMode()) {
rex_logger::logError(1, 'Content length: ' . strlen($content), __FILE__, __LINE__);
}
$altCache = [];
// Match all img tags
$content = preg_replace_callback(
'/<img\b[^>]*>/i',
static function ($matches) use (&$altCache) {
$imgTag = $matches[0];
// Check if alt attribute exists
if (preg_match('/\balt\s*=\s*("|\')(.*?)\1/i', $imgTag, $altMatches)) {
$altValue = trim($altMatches[2]);
if ($altValue !== '') {
// Alt attribute exists and is not empty, do not modify
return $imgTag;
}
// Alt attribute exists but is empty, we need to update it
}
// Extract image source from src attribute
$imageSrc = null;
if (preg_match('/\bsrc\s*=\s*["\']([^"\']+)["\']/i', $imgTag, $srcMatches)) {
$imageSrc = $srcMatches[1];
}
$altText = 'No description available';
if ($imageSrc) {
$filename = basename($imageSrc);
if (isset($altCache[$filename])) {
$altText = $altCache[$filename];
} else {
// Query database for image description
$sql = rex_sql::factory();
$sql->setQuery(
'SELECT med_description FROM ' . rex::getTablePrefix() . 'media WHERE filename = ?',
[$filename]
);
if ($sql->hasError()) {
rex_logger::logError(1, 'SQL Error: ' . $sql->getError(), __FILE__, __LINE__);
} elseif ($sql->getRows() > 0) {
$altText = $sql->getValue('med_description');
}
$altCache[$filename] = $altText;
}
}
// Remove existing alt attribute if it's empty
$imgTag = preg_replace('/\balt\s*=\s*("|\')(.*?)\1/i', '', $imgTag);
// Add or update the alt attribute
$imgTag = preg_replace(
'/<img\b/i',
'<img alt="' . htmlspecialchars($altText, ENT_QUOTES, 'UTF-8') . '"',
$imgTag
);
return $imgTag;
},
$content
);
return $content;
});
}