-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathextension.php
190 lines (172 loc) · 7.16 KB
/
extension.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
declare(strict_types=1);
final class ImageCacheExtension extends Minz_Extension
{
// Defaults
private const CACHE_URL = 'https://wsrv.nl/?url=';
private const CACHE_POST_URL = 'https://example.com/prepare';
private const CACHE_ACCESS_TOKEN = '';
private const URL_ENCODE = '1';
private const CACHE_POST_ENABLED = '';
#[\Override]
public function init(): void
{
if (!FreshRSS_Context::hasSystemConf()) {
throw new FreshRSS_Context_Exception('System configuration not initialised!');
}
$this->registerHook('entry_before_display', [self::class, 'content_modification_hook']);
$this->registerHook('entry_before_insert', [self::class, 'image_upload_hook']);
// Defaults
$save = false;
if (is_null(FreshRSS_Context::userConf()->image_cache_url)) {
FreshRSS_Context::userConf()->image_cache_url = self::CACHE_URL;
$save = true;
}
if (is_null(FreshRSS_Context::userConf()->image_cache_post_url)) {
FreshRSS_Context::userConf()->image_cache_post_url = self::CACHE_POST_URL;
$save = true;
}
if (is_null(FreshRSS_Context::userConf()->image_cache_post_url)) {
FreshRSS_Context::userConf()->image_cache_access_token = self::CACHE_ACCESS_TOKEN;
$save = true;
}
if (is_null(FreshRSS_Context::userConf()->image_cache_post_enabled)) {
FreshRSS_Context::userConf()->image_cache_post_enabled = self::CACHE_POST_ENABLED;
$save = true;
}
if ($save) {
FreshRSS_Context::userConf()->save();
}
}
#[\Override]
public function handleConfigureAction(): void
{
$this->registerTranslates();
if (Minz_Request::isPost()) {
FreshRSS_Context::userConf()->image_cache_url = Minz_Request::paramString('image_cache_url');
FreshRSS_Context::userConf()->image_cache_post_url = Minz_Request::paramString('image_cache_post_url');
FreshRSS_Context::userConf()->image_cache_access_token = Minz_Request::paramString('image_cache_access_token');
FreshRSS_Context::userConf()->image_cache_post_enabled = Minz_Request::paramString('image_cache_post_enabled');
FreshRSS_Context::userConf()->save();
}
}
public static function curlPostRequest(string $url, array $data): bool
{
$data = json_encode($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json;charset='utf-8'",
'Content-Length: ' . strlen($data),
"Accept: application/json")
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$output = curl_exec($curl);
curl_close($curl);
if ($output === false) {
return false;
}
return true;
}
public static function is_remote_url(string $url): bool
{
return strcmp(substr(strtolower($url), 0, 5), "data:") != 0;
}
public static function send_proactive_cache_request(string $url): bool
{
if (FreshRSS_Context::userConf()->image_cache_post_enabled && self::is_remote_url($url)) {
$post_url = FreshRSS_Context::userConf()->image_cache_post_url;
return self::curlPostRequest($post_url, array("access_token" => FreshRSS_Context::userConf()->image_cache_access_token, "url" => $url));
}
return false;
}
public static function getCacheImageUri(string $url): string
{
if (!self::is_remote_url($url)) {
return $url;
}
$url = rawurlencode($url);
return FreshRSS_Context::userConf()->image_cache_url . $url;
}
public static function small($string)
{
return substr($string, 0, 20);
}
public static function cache_images(string $content): string
{
if (empty($content)) {
return $content;
}
$doc = new DOMDocument();
libxml_use_internal_errors(true); // prevent tag soup errors from showing
$encoding = mb_detect_encoding($content);
$doc->loadHTML('<!DOCTYPE html><meta charset="'.$encoding.'">'.$content);
$imgs = $doc->getElementsByTagName('img');
foreach ($imgs as $img) {
if ($img->hasAttribute('src')) {
self::send_proactive_cache_request($img->getAttribute('src'));
}
if ($img->hasAttribute('srcset')) {
preg_replace_callback('/(?:([^\s,]+)(\s*(?:\s+\d+[wx])(?:,\s*)?))/',
function (array $matches): string {
self::send_proactive_cache_request($matches[1]);
return '';
},
$img->getAttribute('srcset'));
}
}
return $content;
}
public static function getSrcSetUris(array $matches): string {
return str_replace($matches[1], self::getCacheImageUri($matches[1]), $matches[0]);
}
public static function swapUris(string $content): string
{
if (empty($content)) {
return $content;
}
$doc = new DOMDocument();
libxml_use_internal_errors(true); // prevent tag soup errors from showing
$encoding = mb_detect_encoding($content);
$doc->loadHTML('<!DOCTYPE html><meta charset="'.$encoding.'">'.$content);
$imgs = $doc->getElementsByTagName('img');
foreach ($imgs as $img) {
if ($img->hasAttribute('src')) {
$src = $img->getAttribute('src');
$newSrc = self::getCacheImageUri($src);
/*
Due to the URL change, FreshRSS is not aware of already rendered enclosures.
Adding data-xextension-imagecache-original-src / srcset ensures that original URLs are present in the content for the renderer check FreshRSS_Entry->containsLink.
*/
$img->setAttribute('data-xextension-imagecache-original-src', $src);
$img->setAttribute('src', $newSrc);
}
if ($img->hasAttribute('srcset')) {
$srcSet = $img->getAttribute('srcset');
$newSrcSet = preg_replace_callback('/(?:([^\s,]+)(\s*(?:\s+\d+[wx])(?:,\s*)?))/', fn (array $matches) => self::getSrcSetUris($matches), $srcSet);
if ($newSrcSet != null) {
$img->setAttribute('data-xextension-imagecache-original-srcset', $srcSet);
$img->setAttribute('srcset', $newSrcSet);
}
}
}
return $doc->saveHTML();
}
public static function content_modification_hook($entry)
{
$entry->_content(
self::swapUris($entry->content())
);
return $entry;
}
public static function image_upload_hook($entry)
{
self::cache_images($entry->content());
return $entry;
}
}