]>
Commit | Line | Data |
---|---|---|
2c08214a | 1 | <?php |
e6c886bf AD |
2 | class RSSUtils { |
3 | static function calculate_article_hash($article, $pluginhost) { | |
af244f92 AD |
4 | $tmp = ""; |
5 | ||
6 | foreach ($article as $k => $v) { | |
7 | if ($k != "feed" && isset($v)) { | |
24e6ff5d AD |
8 | $x = strip_tags(is_array($v) ? implode(",", $v) : $v); |
9 | ||
10 | //_debug("$k:" . sha1($x) . ":" . htmlspecialchars($x), true); | |
11 | ||
12 | $tmp .= sha1("$k:" . sha1($x)); | |
af244f92 AD |
13 | } |
14 | } | |
15 | ||
eb16bd9f | 16 | return sha1(implode(",", $pluginhost->get_plugin_names()) . $tmp); |
b1840673 AD |
17 | } |
18 | ||
e6c886bf | 19 | static function update_feedbrowser_cache() { |
79178062 | 20 | |
a42c55f0 | 21 | $result = db_query("SELECT feed_url, site_url, title, COUNT(id) AS subscribers |
45378752 LD |
22 | FROM ttrss_feeds WHERE feed_url NOT IN (SELECT feed_url FROM ttrss_feeds |
23 | WHERE private IS true OR auth_login != '' OR auth_pass != '' OR feed_url LIKE '%:%@%/%') | |
79178062 AD |
24 | GROUP BY feed_url, site_url, title ORDER BY subscribers DESC LIMIT 1000"); |
25 | ||
a42c55f0 | 26 | db_query("BEGIN"); |
79178062 | 27 | |
a42c55f0 | 28 | db_query("DELETE FROM ttrss_feedbrowser_cache"); |
79178062 AD |
29 | |
30 | $count = 0; | |
31 | ||
32 | while ($line = db_fetch_assoc($result)) { | |
a42c55f0 AD |
33 | $subscribers = db_escape_string($line["subscribers"]); |
34 | $feed_url = db_escape_string($line["feed_url"]); | |
35 | $title = db_escape_string($line["title"]); | |
36 | $site_url = db_escape_string($line["site_url"]); | |
79178062 | 37 | |
a42c55f0 | 38 | $tmp_result = db_query("SELECT subscribers FROM |
79178062 AD |
39 | ttrss_feedbrowser_cache WHERE feed_url = '$feed_url'"); |
40 | ||
41 | if (db_num_rows($tmp_result) == 0) { | |
42 | ||
a42c55f0 | 43 | db_query("INSERT INTO ttrss_feedbrowser_cache |
79178062 AD |
44 | (feed_url, site_url, title, subscribers) VALUES ('$feed_url', |
45 | '$site_url', '$title', '$subscribers')"); | |
46 | ||
47 | ++$count; | |
48 | ||
49 | } | |
50 | ||
51 | } | |
52 | ||
a42c55f0 | 53 | db_query("COMMIT"); |
79178062 AD |
54 | |
55 | return $count; | |
56 | ||
57 | } | |
58 | ||
e6c886bf | 59 | static function update_daemon_common($limit = DAEMON_FEED_LIMIT, $debug = true) { |
6322ac79 | 60 | $schema_version = get_schema_version(); |
857efe49 AD |
61 | |
62 | if ($schema_version != SCHEMA_VERSION) { | |
63 | die("Schema version is wrong, please upgrade the database.\n"); | |
64 | } | |
65 | ||
09e8bdfd | 66 | if (!SINGLE_USER_MODE && DAEMON_UPDATE_LOGIN_LIMIT > 0) { |
2c08214a AD |
67 | if (DB_TYPE == "pgsql") { |
68 | $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'"; | |
69 | } else { | |
70 | $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)"; | |
71 | } | |
72 | } else { | |
73 | $login_thresh_qpart = ""; | |
74 | } | |
75 | ||
2c08214a AD |
76 | if (DB_TYPE == "pgsql") { |
77 | $update_limit_qpart = "AND (( | |
78 | ttrss_feeds.update_interval = 0 | |
ee0542ce | 79 | AND ttrss_user_prefs.value != '-1' |
2c08214a AD |
80 | AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL) |
81 | ) OR ( | |
82 | ttrss_feeds.update_interval > 0 | |
83 | AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL) | |
f08426e3 AD |
84 | ) OR (ttrss_feeds.last_updated IS NULL |
85 | AND ttrss_user_prefs.value != '-1') | |
86 | OR (last_updated = '1970-01-01 00:00:00' | |
87 | AND ttrss_user_prefs.value != '-1'))"; | |
2c08214a AD |
88 | } else { |
89 | $update_limit_qpart = "AND (( | |
90 | ttrss_feeds.update_interval = 0 | |
ee0542ce | 91 | AND ttrss_user_prefs.value != '-1' |
2c08214a AD |
92 | AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE) |
93 | ) OR ( | |
94 | ttrss_feeds.update_interval > 0 | |
95 | AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE) | |
f08426e3 AD |
96 | ) OR (ttrss_feeds.last_updated IS NULL |
97 | AND ttrss_user_prefs.value != '-1') | |
98 | OR (last_updated = '1970-01-01 00:00:00' | |
99 | AND ttrss_user_prefs.value != '-1'))"; | |
2c08214a AD |
100 | } |
101 | ||
102 | // Test if feed is currently being updated by another process. | |
103 | if (DB_TYPE == "pgsql") { | |
566417c4 | 104 | $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '10 minutes')"; |
2c08214a | 105 | } else { |
566417c4 | 106 | $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 10 MINUTE))"; |
2c08214a AD |
107 | } |
108 | ||
93af11cb | 109 | $query_limit = $limit ? sprintf("LIMIT %d", $limit) : ""; |
2c08214a | 110 | |
98070db0 TK |
111 | // Update the least recently updated feeds first |
112 | $query_order = "ORDER BY last_updated"; | |
113 | if (DB_TYPE == "pgsql") $query_order .= " NULLS FIRST"; | |
114 | ||
fce451a4 | 115 | $query = "SELECT DISTINCT ttrss_feeds.feed_url, ttrss_feeds.last_updated |
2c08214a AD |
116 | FROM |
117 | ttrss_feeds, ttrss_users, ttrss_user_prefs | |
f4ae0f05 | 118 | WHERE |
2c08214a | 119 | ttrss_feeds.owner_uid = ttrss_users.id |
f08426e3 | 120 | AND ttrss_user_prefs.profile IS NULL |
2c08214a AD |
121 | AND ttrss_users.id = ttrss_user_prefs.owner_uid |
122 | AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL' | |
123 | $login_thresh_qpart $update_limit_qpart | |
1c4421fc | 124 | $updstart_thresh_qpart |
98070db0 | 125 | $query_order $query_limit"; |
fce451a4 | 126 | |
fce451a4 | 127 | $result = db_query($query); |
2c08214a | 128 | |
93af11cb | 129 | if ($debug) _debug(sprintf("Scheduled %d feeds to update...", db_num_rows($result))); |
2c08214a | 130 | |
2c08214a AD |
131 | $feeds_to_update = array(); |
132 | while ($line = db_fetch_assoc($result)) { | |
93af11cb | 133 | array_push($feeds_to_update, $line['feed_url']); |
2c08214a AD |
134 | } |
135 | ||
93af11cb AD |
136 | // Update last_update_started before actually starting the batch |
137 | // in order to minimize collision risk for parallel daemon tasks | |
138 | if (count($feeds_to_update) > 0) { | |
139 | $feeds_quoted = array_map(function ($s) { return "'" . db_escape_string($s) . "'"; }, $feeds_to_update); | |
1c4421fc | 140 | |
a42c55f0 | 141 | db_query(sprintf("UPDATE ttrss_feeds SET last_update_started = NOW() |
1c4421fc | 142 | WHERE feed_url IN (%s)", implode(',', $feeds_quoted))); |
2c08214a AD |
143 | } |
144 | ||
8292d05b | 145 | $nf = 0; |
2d9c5684 | 146 | $bstarted = microtime(true); |
8292d05b | 147 | |
5cbd1fe8 AD |
148 | $batch_owners = array(); |
149 | ||
1c4421fc AD |
150 | foreach ($feeds_to_update as $feed) { |
151 | if($debug) _debug("Base feed: $feed"); | |
2c08214a | 152 | |
a42c55f0 | 153 | //update_rss_feed($line["id"], true); |
2c08214a | 154 | |
1c4421fc | 155 | // since we have the data cached, we can deal with other feeds with the same url |
15c762be | 156 | $tmp_result = db_query("SELECT DISTINCT ttrss_feeds.id,last_updated,ttrss_feeds.owner_uid |
ee0542ce AD |
157 | FROM ttrss_feeds, ttrss_users, ttrss_user_prefs WHERE |
158 | ttrss_user_prefs.owner_uid = ttrss_feeds.owner_uid AND | |
159 | ttrss_users.id = ttrss_user_prefs.owner_uid AND | |
160 | ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL' AND | |
f08426e3 | 161 | ttrss_user_prefs.profile IS NULL AND |
9e84bab4 AD |
162 | feed_url = '".db_escape_string($feed)."' |
163 | $update_limit_qpart | |
1c4421fc | 164 | $login_thresh_qpart |
5929a0c1 | 165 | ORDER BY ttrss_feeds.id $query_limit"); |
1c4421fc AD |
166 | |
167 | if (db_num_rows($tmp_result) > 0) { | |
168 | while ($tline = db_fetch_assoc($tmp_result)) { | |
93af11cb | 169 | if ($debug) _debug(" => " . $tline["last_updated"] . ", " . $tline["id"] . " " . $tline["owner_uid"]); |
f08426e3 | 170 | |
5cbd1fe8 AD |
171 | if (array_search($tline["owner_uid"], $batch_owners) === FALSE) |
172 | array_push($batch_owners, $tline["owner_uid"]); | |
173 | ||
2d9c5684 | 174 | $fstarted = microtime(true); |
e6c886bf | 175 | RSSUtils::update_rss_feed($tline["id"], true, false); |
4f71d743 | 176 | _debug_suppress(false); |
2d9c5684 AD |
177 | |
178 | _debug(sprintf(" %.4f (sec)", microtime(true) - $fstarted)); | |
179 | ||
8292d05b | 180 | ++$nf; |
1c4421fc AD |
181 | } |
182 | } | |
2c08214a AD |
183 | } |
184 | ||
2d9c5684 AD |
185 | if ($nf > 0) { |
186 | _debug(sprintf("Processed %d feeds in %.4f (sec), %.4f (sec/feed avg)", $nf, | |
187 | microtime(true) - $bstarted, (microtime(true) - $bstarted) / $nf)); | |
188 | } | |
189 | ||
5cbd1fe8 AD |
190 | foreach ($batch_owners as $owner_uid) { |
191 | _debug("Running housekeeping tasks for user $owner_uid..."); | |
192 | ||
e6c886bf | 193 | RSSUtils::housekeeping_user($owner_uid); |
5cbd1fe8 AD |
194 | } |
195 | ||
2c08214a | 196 | // Send feed digests by email if needed. |
c2f0f24e | 197 | Digest::send_headlines_digests($debug); |
2c08214a | 198 | |
8292d05b AD |
199 | return $nf; |
200 | ||
7b55001e | 201 | } |
2c08214a | 202 | |
6022776d | 203 | // this is used when subscribing |
e6c886bf | 204 | static function set_basic_feed_info($feed) { |
6022776d AD |
205 | |
206 | $feed = db_escape_string($feed); | |
207 | ||
9bd422c2 | 208 | $result = db_query("SELECT feed_url,auth_pass,auth_login,auth_pass_encrypted |
6022776d AD |
209 | FROM ttrss_feeds WHERE id = '$feed'"); |
210 | ||
211 | $auth_pass_encrypted = sql_bool_to_bool(db_fetch_result($result, | |
212 | 0, "auth_pass_encrypted")); | |
213 | ||
214 | $auth_login = db_fetch_result($result, 0, "auth_login"); | |
215 | $auth_pass = db_fetch_result($result, 0, "auth_pass"); | |
216 | ||
17a8e61d | 217 | if ($auth_pass_encrypted && function_exists("mcrypt_decrypt")) { |
6022776d AD |
218 | require_once "crypt.php"; |
219 | $auth_pass = decrypt_string($auth_pass); | |
220 | } | |
221 | ||
222 | $fetch_url = db_fetch_result($result, 0, "feed_url"); | |
223 | ||
224 | $feed_data = fetch_file_contents($fetch_url, false, | |
225 | $auth_login, $auth_pass, false, | |
3d5d2890 | 226 | FEED_FETCH_TIMEOUT, |
6022776d AD |
227 | 0); |
228 | ||
229 | global $fetch_curl_used; | |
230 | ||
231 | if (!$fetch_curl_used) { | |
232 | $tmp = @gzdecode($feed_data); | |
233 | ||
234 | if ($tmp) $feed_data = $tmp; | |
235 | } | |
236 | ||
237 | $feed_data = trim($feed_data); | |
238 | ||
239 | $rss = new FeedParser($feed_data); | |
240 | $rss->init(); | |
241 | ||
242 | if (!$rss->error()) { | |
243 | ||
244 | $result = db_query("SELECT title, site_url FROM ttrss_feeds WHERE id = '$feed'"); | |
245 | ||
246 | $registered_title = db_fetch_result($result, 0, "title"); | |
247 | $orig_site_url = db_fetch_result($result, 0, "site_url"); | |
248 | ||
249 | $site_url = db_escape_string(mb_substr(rewrite_relative_url($fetch_url, $rss->get_link()), 0, 245)); | |
250 | $feed_title = db_escape_string(mb_substr($rss->get_title(), 0, 199)); | |
251 | ||
252 | if ($feed_title && (!$registered_title || $registered_title == "[Unknown]")) { | |
253 | db_query("UPDATE ttrss_feeds SET | |
254 | title = '$feed_title' WHERE id = '$feed'"); | |
255 | } | |
256 | ||
257 | if ($site_url && $orig_site_url != $site_url) { | |
258 | db_query("UPDATE ttrss_feeds SET | |
259 | site_url = '$site_url' WHERE id = '$feed'"); | |
260 | } | |
261 | } | |
262 | } | |
263 | ||
7b55001e | 264 | /** |
e6c886bf AD |
265 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
266 | */ | |
267 | static function update_rss_feed($feed, $no_cache = false) { | |
2c08214a | 268 | |
2c08214a AD |
269 | $debug_enabled = defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']; |
270 | ||
4f71d743 | 271 | _debug_suppress(!$debug_enabled); |
68cccafc | 272 | _debug("start", $debug_enabled); |
2c08214a | 273 | |
6bb96beb AD |
274 | $result = db_query("SELECT title FROM ttrss_feeds |
275 | WHERE id = '$feed'"); | |
bfe1eb4e AD |
276 | |
277 | if (db_num_rows($result) == 0) { | |
278 | _debug("feed $feed NOT FOUND/SKIPPED", $debug_enabled); | |
279 | user_error("Attempt to update unknown/invalid feed $feed", E_USER_WARNING); | |
280 | return false; | |
281 | } | |
282 | ||
6bb96beb AD |
283 | $title = db_fetch_result($result, 0, "title"); |
284 | ||
285 | // feed was batch-subscribed or something, we need to get basic info | |
286 | // this is not optimal currently as it fetches stuff separately TODO: optimize | |
287 | if ($title == "[Unknown]") { | |
288 | _debug("setting basic feed info for $feed..."); | |
e6c886bf | 289 | RSSUtils::set_basic_feed_info($feed); |
6bb96beb AD |
290 | } |
291 | ||
a42c55f0 | 292 | $result = db_query("SELECT id,update_interval,auth_login, |
5ba1ddd4 | 293 | feed_url,auth_pass,cache_images, |
5321e775 | 294 | mark_unread_on_update, owner_uid, |
5b6ea1ef | 295 | auth_pass_encrypted, feed_language |
87764a50 | 296 | FROM ttrss_feeds WHERE id = '$feed'"); |
2c08214a | 297 | |
2c08214a AD |
298 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); |
299 | $mark_unread_on_update = sql_bool_to_bool(db_fetch_result($result, | |
300 | 0, "mark_unread_on_update")); | |
044cff2d AD |
301 | $auth_pass_encrypted = sql_bool_to_bool(db_fetch_result($result, |
302 | 0, "auth_pass_encrypted")); | |
2c08214a | 303 | |
a42c55f0 | 304 | db_query("UPDATE ttrss_feeds SET last_update_started = NOW() |
2c08214a AD |
305 | WHERE id = '$feed'"); |
306 | ||
307 | $auth_login = db_fetch_result($result, 0, "auth_login"); | |
308 | $auth_pass = db_fetch_result($result, 0, "auth_pass"); | |
309 | ||
17a8e61d | 310 | if ($auth_pass_encrypted && function_exists("mcrypt_decrypt")) { |
044cff2d AD |
311 | require_once "crypt.php"; |
312 | $auth_pass = decrypt_string($auth_pass); | |
313 | } | |
314 | ||
2c08214a AD |
315 | $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images")); |
316 | $fetch_url = db_fetch_result($result, 0, "feed_url"); | |
df659891 AD |
317 | $feed_language = db_escape_string(mb_strtolower(db_fetch_result($result, 0, "feed_language"))); |
318 | if (!$feed_language) $feed_language = 'english'; | |
2c08214a | 319 | |
a42c55f0 | 320 | $feed = db_escape_string($feed); |
2c08214a | 321 | |
f074ffe9 | 322 | $date_feed_processed = date('Y-m-d H:i'); |
2c08214a | 323 | |
865a3ed6 | 324 | $cache_filename = CACHE_DIR . "/simplepie/" . sha1($fetch_url) . ".xml"; |
f074ffe9 | 325 | |
ee65bef4 AD |
326 | $pluginhost = new PluginHost(); |
327 | $pluginhost->set_debug($debug_enabled); | |
328 | $user_plugins = get_pref("_ENABLED_PLUGINS", $owner_uid); | |
329 | ||
330 | $pluginhost->load(PLUGINS, PluginHost::KIND_ALL); | |
331 | $pluginhost->load($user_plugins, PluginHost::KIND_USER, $owner_uid); | |
332 | $pluginhost->load_data(); | |
333 | ||
7b55001e | 334 | $rss_hash = false; |
4f9cbdff | 335 | |
7b55001e AD |
336 | $force_refetch = isset($_REQUEST["force_refetch"]); |
337 | $feed_data = ""; | |
687a4f59 | 338 | |
7b55001e AD |
339 | foreach ($pluginhost->get_hooks(PluginHost::HOOK_FETCH_FEED) as $plugin) { |
340 | $feed_data = $plugin->hook_fetch_feed($feed_data, $fetch_url, $owner_uid, $feed, 0, $auth_login, $auth_pass); | |
341 | } | |
2c08214a | 342 | |
7b55001e AD |
343 | // try cache |
344 | if (!$feed_data && | |
345 | file_exists($cache_filename) && | |
346 | is_readable($cache_filename) && | |
347 | !$auth_login && !$auth_pass && | |
348 | filemtime($cache_filename) > time() - 30) { | |
be574731 | 349 | |
7b55001e | 350 | _debug("using local cache [$cache_filename].", $debug_enabled); |
52637d3b | 351 | |
7b55001e | 352 | @$feed_data = file_get_contents($cache_filename); |
f074ffe9 | 353 | |
7b55001e AD |
354 | if ($feed_data) { |
355 | $rss_hash = sha1($feed_data); | |
88edaa93 | 356 | } |
ee65bef4 | 357 | |
7b55001e AD |
358 | } else { |
359 | _debug("local cache will not be used for this feed", $debug_enabled); | |
360 | } | |
312742db | 361 | |
7b55001e AD |
362 | // fetch feed from source |
363 | if (!$feed_data) { | |
364 | _debug("fetching [$fetch_url]...", $debug_enabled); | |
312742db | 365 | |
7b55001e AD |
366 | if (ini_get("open_basedir") && function_exists("curl_init")) { |
367 | _debug("not using CURL due to open_basedir restrictions"); | |
368 | } | |
3f6f0857 | 369 | |
7b55001e AD |
370 | $feed_data = fetch_file_contents($fetch_url, false, |
371 | $auth_login, $auth_pass, false, | |
372 | $no_cache ? FEED_FETCH_NO_CACHE_TIMEOUT : FEED_FETCH_TIMEOUT, | |
373 | 0); | |
3f6f0857 | 374 | |
7b55001e | 375 | global $fetch_curl_used; |
3f6f0857 | 376 | |
7b55001e AD |
377 | if (!$fetch_curl_used) { |
378 | $tmp = @gzdecode($feed_data); | |
1367bc3f | 379 | |
7b55001e AD |
380 | if ($tmp) $feed_data = $tmp; |
381 | } | |
017401dd | 382 | |
7b55001e | 383 | $feed_data = trim($feed_data); |
fd687300 | 384 | |
7b55001e | 385 | _debug("fetch done.", $debug_enabled); |
95beaa14 | 386 | |
7b55001e AD |
387 | // cache vanilla feed data for re-use |
388 | if ($feed_data && !$auth_pass && !$auth_login && is_writable(CACHE_DIR . "/simplepie")) { | |
389 | $new_rss_hash = sha1($feed_data); | |
390 | ||
391 | if ($new_rss_hash != $rss_hash) { | |
392 | _debug("saving $cache_filename", $debug_enabled); | |
393 | @file_put_contents($cache_filename, $feed_data); | |
95beaa14 | 394 | } |
4f9cbdff | 395 | } |
7b55001e | 396 | } |
017401dd | 397 | |
7b55001e AD |
398 | if (!$feed_data) { |
399 | global $fetch_last_error; | |
400 | global $fetch_last_error_code; | |
f074ffe9 | 401 | |
7b55001e | 402 | _debug("unable to fetch: $fetch_last_error [$fetch_last_error_code]", $debug_enabled); |
f074ffe9 | 403 | |
7b55001e | 404 | $error_escaped = ''; |
7a01dc77 | 405 | |
7b55001e AD |
406 | // If-Modified-Since |
407 | if ($fetch_last_error_code != 304) { | |
408 | $error_escaped = db_escape_string($fetch_last_error); | |
409 | } else { | |
410 | _debug("source claims data not modified, nothing to do.", $debug_enabled); | |
411 | } | |
4f9cbdff | 412 | |
7b55001e AD |
413 | db_query( |
414 | "UPDATE ttrss_feeds SET last_error = '$error_escaped', | |
415 | last_updated = NOW() WHERE id = '$feed'"); | |
4f9cbdff | 416 | |
7b55001e | 417 | return; |
f074ffe9 AD |
418 | } |
419 | ||
1ffe3391 | 420 | foreach ($pluginhost->get_hooks(PluginHost::HOOK_FEED_FETCHED) as $plugin) { |
6791af0c | 421 | $feed_data = $plugin->hook_feed_fetched($feed_data, $fetch_url, $owner_uid, $feed); |
017401dd AD |
422 | } |
423 | ||
07d3431e AD |
424 | $rss = new FeedParser($feed_data); |
425 | $rss->init(); | |
2c08214a | 426 | |
a42c55f0 | 427 | $feed = db_escape_string($feed); |
2c08214a | 428 | |
19b3992b | 429 | if (!$rss->error()) { |
2c08214a | 430 | |
d2a421e3 | 431 | // We use local pluginhost here because we need to load different per-user feed plugins |
1ffe3391 | 432 | $pluginhost->run_hooks(PluginHost::HOOK_FEED_PARSED, "hook_feed_parsed", $rss); |
4412b877 | 433 | |
df659891 | 434 | _debug("language: $feed_language", $debug_enabled); |
68cccafc | 435 | _debug("processing feed data...", $debug_enabled); |
2c08214a | 436 | |
a42c55f0 | 437 | // db_query("BEGIN"); |
2c08214a | 438 | |
382268c6 AD |
439 | if (DB_TYPE == "pgsql") { |
440 | $favicon_interval_qpart = "favicon_last_checked < NOW() - INTERVAL '12 hour'"; | |
441 | } else { | |
442 | $favicon_interval_qpart = "favicon_last_checked < DATE_SUB(NOW(), INTERVAL 12 HOUR)"; | |
443 | } | |
444 | ||
fe4535e6 | 445 | $result = db_query("SELECT owner_uid,favicon_avg_color, |
382268c6 AD |
446 | (favicon_last_checked IS NULL OR $favicon_interval_qpart) AS |
447 | favicon_needs_check | |
2c08214a AD |
448 | FROM ttrss_feeds WHERE id = '$feed'"); |
449 | ||
382268c6 AD |
450 | $favicon_needs_check = sql_bool_to_bool(db_fetch_result($result, 0, |
451 | "favicon_needs_check")); | |
36490f11 | 452 | $favicon_avg_color = db_fetch_result($result, 0, "favicon_avg_color"); |
2c08214a AD |
453 | |
454 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
455 | ||
a42c55f0 | 456 | $site_url = db_escape_string(mb_substr(rewrite_relative_url($fetch_url, $rss->get_link()), 0, 245)); |
2c08214a | 457 | |
cd07592c AD |
458 | _debug("site_url: $site_url", $debug_enabled); |
459 | _debug("feed_title: " . $rss->get_title(), $debug_enabled); | |
460 | ||
687a4f59 | 461 | if ($favicon_needs_check || $force_refetch) { |
36490f11 AD |
462 | |
463 | /* terrible hack: if we crash on floicon shit here, we won't check | |
560cbd8c | 464 | * the icon avgcolor again (unless the icon got updated) */ |
36490f11 | 465 | |
560cbd8c AD |
466 | $favicon_file = ICONS_DIR . "/$feed.ico"; |
467 | $favicon_modified = @filemtime($favicon_file); | |
468 | ||
68cccafc | 469 | _debug("checking favicon...", $debug_enabled); |
687a4f59 | 470 | |
e6c886bf | 471 | RSSUtils::check_feed_favicon($site_url, $feed); |
560cbd8c AD |
472 | $favicon_modified_new = @filemtime($favicon_file); |
473 | ||
474 | if ($favicon_modified_new > $favicon_modified) | |
475 | $favicon_avg_color = ''; | |
687a4f59 | 476 | |
6ee0d4b0 | 477 | if (file_exists($favicon_file) && function_exists("imagecreatefromstring") && $favicon_avg_color == '') { |
e6c886bf | 478 | require_once "colors.php"; |
687a4f59 | 479 | |
e6c886bf | 480 | db_query("UPDATE ttrss_feeds SET favicon_avg_color = 'fail' WHERE |
aafd55ba AD |
481 | id = '$feed'"); |
482 | ||
e6c886bf AD |
483 | $favicon_color = db_escape_string( |
484 | calculate_avg_color($favicon_file)); | |
63c323f7 | 485 | |
e6c886bf | 486 | $favicon_colorstring = ",favicon_avg_color = '".$favicon_color."'"; |
36490f11 | 487 | } else if ($favicon_avg_color == 'fail') { |
84ceb6bd | 488 | _debug("floicon failed on this file, not trying to recalculate avg color", $debug_enabled); |
6ac722d5 | 489 | } |
687a4f59 | 490 | |
a42c55f0 | 491 | db_query("UPDATE ttrss_feeds SET favicon_last_checked = NOW() |
bc7a144d | 492 | $favicon_colorstring |
f2798eb6 AD |
493 | WHERE id = '$feed'"); |
494 | } | |
2c08214a | 495 | |
68cccafc | 496 | _debug("loading filters & labels...", $debug_enabled); |
2c08214a | 497 | |
a42c55f0 | 498 | $filters = load_filters($feed, $owner_uid); |
2c08214a | 499 | |
02f3992a AD |
500 | if ($debug_enabled) { |
501 | print_r($filters); | |
502 | } | |
503 | ||
68cccafc | 504 | _debug("" . count($filters) . " filters loaded.", $debug_enabled); |
2c08214a | 505 | |
19b3992b | 506 | $items = $rss->get_items(); |
2c08214a | 507 | |
19b3992b | 508 | if (!is_array($items)) { |
68cccafc | 509 | _debug("no articles found.", $debug_enabled); |
2c08214a | 510 | |
a42c55f0 | 511 | db_query("UPDATE ttrss_feeds |
2c08214a AD |
512 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); |
513 | ||
514 | return; // no articles | |
515 | } | |
516 | ||
68cccafc | 517 | _debug("processing articles...", $debug_enabled); |
2c08214a | 518 | |
6c9f3d4a AD |
519 | $tstart = time(); |
520 | ||
19b3992b | 521 | foreach ($items as $item) { |
5d56d100 | 522 | if ($_REQUEST['xdebug'] == 3) { |
2c08214a AD |
523 | print_r($item); |
524 | } | |
525 | ||
6c9f3d4a AD |
526 | if (ini_get("max_execution_time") > 0 && time() - $tstart >= ini_get("max_execution_time") * 0.7) { |
527 | _debug("looks like there's too many articles to process at once, breaking out", $debug_enabled); | |
528 | break; | |
529 | } | |
530 | ||
19b3992b AD |
531 | $entry_guid = $item->get_id(); |
532 | if (!$entry_guid) $entry_guid = $item->get_link(); | |
e6c886bf | 533 | if (!$entry_guid) $entry_guid = RSSUtils::make_guid_from_title($item->get_title()); |
2c08214a AD |
534 | if (!$entry_guid) continue; |
535 | ||
3a4c8973 AD |
536 | $entry_guid = "$owner_uid,$entry_guid"; |
537 | ||
a42c55f0 | 538 | $entry_guid_hashed = db_escape_string('SHA1:' . sha1($entry_guid)); |
5e3d5480 | 539 | |
68cccafc | 540 | _debug("guid $entry_guid / $entry_guid_hashed", $debug_enabled); |
5e3d5480 | 541 | |
2c08214a AD |
542 | $entry_timestamp = ""; |
543 | ||
04d2f9c8 AD |
544 | $entry_timestamp = $item->get_date(); |
545 | ||
546 | _debug("orig date: " . $item->get_date(), $debug_enabled); | |
2c08214a | 547 | |
30123fe6 | 548 | if ($entry_timestamp == -1 || !$entry_timestamp || $entry_timestamp > time()) { |
2c08214a | 549 | $entry_timestamp = time(); |
2c08214a AD |
550 | } |
551 | ||
552 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
553 | ||
68cccafc | 554 | _debug("date $entry_timestamp [$entry_timestamp_fmt]", $debug_enabled); |
2c08214a | 555 | |
99429e57 AD |
556 | // $entry_title = html_entity_decode($item->get_title(), ENT_COMPAT, 'UTF-8'); |
557 | // $entry_title = decode_numeric_entities($entry_title); | |
558 | $entry_title = $item->get_title(); | |
1b35d30c | 559 | |
5d56d100 | 560 | $entry_link = rewrite_relative_url($site_url, $item->get_link()); |
2c08214a | 561 | |
68cccafc AD |
562 | _debug("title $entry_title", $debug_enabled); |
563 | _debug("link $entry_link", $debug_enabled); | |
2c08214a AD |
564 | |
565 | if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);; | |
566 | ||
19b3992b AD |
567 | $entry_content = $item->get_content(); |
568 | if (!$entry_content) $entry_content = $item->get_description(); | |
2c08214a AD |
569 | |
570 | if ($_REQUEST["xdebug"] == 2) { | |
9ec10352 | 571 | print "content: "; |
0bc503ff | 572 | print htmlspecialchars($entry_content); |
3c696512 | 573 | print "\n"; |
2c08214a AD |
574 | } |
575 | ||
12ff230b AD |
576 | $entry_comments = db_escape_string(mb_substr($item->get_comments_url(), 0, 245)); |
577 | $num_comments = (int) $item->get_comments_count(); | |
2c08214a | 578 | |
12ff230b | 579 | $entry_author = $item->get_author(); // escaped later |
a42c55f0 | 580 | $entry_guid = db_escape_string(mb_substr($entry_guid, 0, 245)); |
2c08214a | 581 | |
68cccafc AD |
582 | _debug("author $entry_author", $debug_enabled); |
583 | _debug("num_comments: $num_comments", $debug_enabled); | |
ee78f81c | 584 | _debug("looking for tags...", $debug_enabled); |
2c08214a AD |
585 | |
586 | // parse <category> entries into tags | |
587 | ||
588 | $additional_tags = array(); | |
589 | ||
19b3992b | 590 | $additional_tags_src = $item->get_categories(); |
2c08214a | 591 | |
19b3992b AD |
592 | if (is_array($additional_tags_src)) { |
593 | foreach ($additional_tags_src as $tobj) { | |
cd07592c | 594 | array_push($additional_tags, $tobj); |
2c08214a | 595 | } |
19b3992b | 596 | } |
2c08214a | 597 | |
fa6fbd36 | 598 | $entry_tags = array_unique($additional_tags); |
2c08214a AD |
599 | |
600 | for ($i = 0; $i < count($entry_tags); $i++) | |
601 | $entry_tags[$i] = mb_strtolower($entry_tags[$i], 'utf-8'); | |
602 | ||
ee78f81c AD |
603 | _debug("tags found: " . join(",", $entry_tags), $debug_enabled); |
604 | ||
68cccafc | 605 | _debug("done collecting data.", $debug_enabled); |
2c08214a | 606 | |
3318d324 | 607 | $result = db_query("SELECT id, content_hash, lang FROM ttrss_entries |
b1840673 | 608 | WHERE guid = '".db_escape_string($entry_guid)."' OR guid = '$entry_guid_hashed'"); |
b30abdad AD |
609 | |
610 | if (db_num_rows($result) != 0) { | |
b1840673 AD |
611 | $base_entry_id = db_fetch_result($result, 0, "id"); |
612 | $entry_stored_hash = db_fetch_result($result, 0, "content_hash"); | |
4a0da0e5 | 613 | $article_labels = Article::get_article_labels($base_entry_id, $owner_uid); |
3318d324 | 614 | $entry_language = db_fetch_result($result, 0, "lang"); |
a8ac7661 | 615 | |
2ed0d6c4 | 616 | $existing_tags = Article::get_article_tags($base_entry_id, $owner_uid); |
a8ac7661 AD |
617 | $entry_tags = array_unique(array_merge($entry_tags, $existing_tags)); |
618 | ||
b30abdad | 619 | } else { |
b1840673 AD |
620 | $base_entry_id = false; |
621 | $entry_stored_hash = ""; | |
a29fe121 | 622 | $article_labels = array(); |
3318d324 | 623 | $entry_language = ""; |
b30abdad AD |
624 | } |
625 | ||
455b1401 | 626 | $article = array("owner_uid" => $owner_uid, // read only |
b30abdad | 627 | "guid" => $entry_guid, // read only |
59e83455 | 628 | "guid_hashed" => $entry_guid_hashed, // read only |
19b3992b AD |
629 | "title" => $entry_title, |
630 | "content" => $entry_content, | |
631 | "link" => $entry_link, | |
a29fe121 | 632 | "labels" => $article_labels, // current limitation: can add labels to article, can't remove them |
19b3992b | 633 | "tags" => $entry_tags, |
e02555c1 | 634 | "author" => $entry_author, |
c9299c28 | 635 | "force_catchup" => false, // ugly hack for the time being |
6de3a1be | 636 | "score_modifier" => 0, // no previous value, plugin should recalculate score modifier based on content if needed |
3318d324 | 637 | "language" => $entry_language, |
f73e03e0 AD |
638 | "feed" => array("id" => $feed, |
639 | "fetch_url" => $fetch_url, | |
babfadbf J |
640 | "site_url" => $site_url, |
641 | "cache_images" => $cache_images) | |
e6c886bf | 642 | ); |
cc85704f | 643 | |
b1840673 | 644 | $entry_plugin_data = ""; |
e6c886bf | 645 | $entry_current_hash = RSSUtils::calculate_article_hash($article, $pluginhost); |
b1840673 AD |
646 | |
647 | _debug("article hash: $entry_current_hash [stored=$entry_stored_hash]", $debug_enabled); | |
648 | ||
522e8b35 | 649 | if ($entry_current_hash == $entry_stored_hash && !isset($_REQUEST["force_rehash"])) { |
b1840673 AD |
650 | _debug("stored article seems up to date [IID: $base_entry_id], updating timestamp only", $debug_enabled); |
651 | ||
652 | // we keep encountering the entry in feeds, so we need to | |
653 | // update date_updated column so that we don't get horrible | |
654 | // dupes when the entry gets purged and reinserted again e.g. | |
655 | // in the case of SLOW SLOW OMG SLOW updating feeds | |
656 | ||
657 | $base_entry_id = db_fetch_result($result, 0, "id"); | |
658 | ||
659 | db_query("UPDATE ttrss_entries SET date_updated = NOW() | |
660 | WHERE id = '$base_entry_id'"); | |
661 | ||
5bdcb8fd | 662 | continue; |
b1840673 AD |
663 | } |
664 | ||
665 | _debug("hash differs, applying plugin filters:", $debug_enabled); | |
666 | ||
1ffe3391 | 667 | foreach ($pluginhost->get_hooks(PluginHost::HOOK_ARTICLE_FILTER) as $plugin) { |
b1840673 AD |
668 | _debug("... " . get_class($plugin), $debug_enabled); |
669 | ||
670 | $start = microtime(true); | |
19b3992b | 671 | $article = $plugin->hook_article_filter($article); |
0084f0d1 | 672 | |
b1840673 AD |
673 | _debug("=== " . sprintf("%.4f (sec)", microtime(true) - $start), $debug_enabled); |
674 | ||
675 | $entry_plugin_data .= mb_strtolower(get_class($plugin)) . ","; | |
cc85704f AD |
676 | } |
677 | ||
0bc503ff AD |
678 | if ($_REQUEST["xdebug"] == 2) { |
679 | print "processed content: "; | |
680 | print htmlspecialchars($article["content"]); | |
681 | print "\n"; | |
682 | } | |
683 | ||
b1840673 AD |
684 | $entry_plugin_data = db_escape_string($entry_plugin_data); |
685 | ||
686 | _debug("plugin data: $entry_plugin_data", $debug_enabled); | |
687 | ||
35c12dc4 AD |
688 | // Workaround: 4-byte unicode requires utf8mb4 in MySQL. See https://tt-rss.org/forum/viewtopic.php?f=1&t=3377&p=20077#p20077 |
689 | if (DB_TYPE == "mysql") { | |
690 | foreach ($article as $k => $v) { | |
35c37354 AD |
691 | |
692 | // i guess we'll have to take the risk of 4byte unicode labels & tags here | |
dae16f72 | 693 | if (is_string($article[$k])) { |
35c37354 AD |
694 | $article[$k] = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $v); |
695 | } | |
35c12dc4 AD |
696 | } |
697 | } | |
698 | ||
b8774453 AD |
699 | /* Collect article tags here so we could filter by them: */ |
700 | ||
557d86fe AD |
701 | $matched_rules = array(); |
702 | ||
e6c886bf | 703 | $article_filters = RSSUtils::get_article_filters($filters, $article["title"], |
7b55001e | 704 | $article["content"], $article["link"], $article["author"], |
557d86fe | 705 | $article["tags"], $matched_rules); |
b8774453 AD |
706 | |
707 | if ($debug_enabled) { | |
557d86fe AD |
708 | _debug("matched filter rules: ", $debug_enabled); |
709 | ||
710 | if (count($matched_rules) != 0) { | |
711 | print_r($matched_rules); | |
712 | } | |
713 | ||
714 | _debug("filter actions: ", $debug_enabled); | |
715 | ||
b8774453 AD |
716 | if (count($article_filters) != 0) { |
717 | print_r($article_filters); | |
718 | } | |
719 | } | |
720 | ||
e6c886bf | 721 | $plugin_filter_names = RSSUtils::find_article_filters($article_filters, "plugin"); |
b8774453 AD |
722 | $plugin_filter_actions = $pluginhost->get_filter_actions(); |
723 | ||
724 | if (count($plugin_filter_names) > 0) { | |
725 | _debug("applying plugin filter actions...", $debug_enabled); | |
726 | ||
727 | foreach ($plugin_filter_names as $pfn) { | |
728 | list($pfclass,$pfaction) = explode(":", $pfn["param"]); | |
729 | ||
730 | if (isset($plugin_filter_actions[$pfclass])) { | |
731 | $plugin = $pluginhost->get_plugin($pfclass); | |
732 | ||
733 | _debug("... $pfclass: $pfaction", $debug_enabled); | |
734 | ||
735 | if ($plugin) { | |
736 | $start = microtime(true); | |
737 | $article = $plugin->hook_article_filter_action($article, $pfaction); | |
738 | ||
739 | _debug("=== " . sprintf("%.4f (sec)", microtime(true) - $start), $debug_enabled); | |
740 | } else { | |
741 | _debug("??? $pfclass: plugin object not found."); | |
742 | } | |
743 | } else { | |
744 | _debug("??? $pfclass: filter plugin not registered."); | |
745 | } | |
746 | } | |
747 | } | |
748 | ||
19b3992b | 749 | $entry_tags = $article["tags"]; |
a42c55f0 AD |
750 | $entry_guid = db_escape_string($entry_guid); |
751 | $entry_title = db_escape_string($article["title"]); | |
12ff230b | 752 | $entry_author = db_escape_string(mb_substr($article["author"], 0, 245)); |
a42c55f0 | 753 | $entry_link = db_escape_string($article["link"]); |
f935d98e | 754 | $entry_content = $article["content"]; // escaped below |
c9299c28 | 755 | $entry_force_catchup = $article["force_catchup"]; |
a29fe121 | 756 | $article_labels = $article["labels"]; |
6de3a1be | 757 | $entry_score_modifier = (int) $article["score_modifier"]; |
3318d324 | 758 | $entry_language = db_escape_string($article["language"]); |
a29fe121 AD |
759 | |
760 | if ($debug_enabled) { | |
761 | _debug("article labels:", $debug_enabled); | |
557d86fe AD |
762 | |
763 | if (count($article_labels) != 0) { | |
764 | print_r($article_labels); | |
765 | } | |
a29fe121 | 766 | } |
c9299c28 AD |
767 | |
768 | _debug("force catchup: $entry_force_catchup"); | |
f935d98e | 769 | |
0a3fd79b | 770 | if ($cache_images && is_writable(CACHE_DIR . '/images')) |
e6c886bf | 771 | RSSUtils::cache_media($entry_content, $site_url, $debug_enabled); |
0a3fd79b | 772 | |
a42c55f0 | 773 | $entry_content = db_escape_string($entry_content, false); |
1f45c857 | 774 | |
07877caf | 775 | //db_query("BEGIN"); |
2c08214a | 776 | |
a42c55f0 | 777 | $result = db_query("SELECT id FROM ttrss_entries |
5e3d5480 | 778 | WHERE (guid = '$entry_guid' OR guid = '$entry_guid_hashed')"); |
9e222305 | 779 | |
2c08214a AD |
780 | if (db_num_rows($result) == 0) { |
781 | ||
07d3431e | 782 | _debug("base guid [$entry_guid or $entry_guid_hashed] not found, creating...", $debug_enabled); |
2c08214a AD |
783 | |
784 | // base post entry does not exist, create it | |
785 | ||
07d3431e | 786 | db_query( |
2c08214a AD |
787 | "INSERT INTO ttrss_entries |
788 | (title, | |
789 | guid, | |
790 | link, | |
791 | updated, | |
792 | content, | |
793 | content_hash, | |
794 | no_orig_date, | |
795 | date_updated, | |
796 | date_entered, | |
797 | comments, | |
798 | num_comments, | |
b30abdad | 799 | plugin_data, |
6b461797 | 800 | lang, |
2c08214a AD |
801 | author) |
802 | VALUES | |
803 | ('$entry_title', | |
5e3d5480 | 804 | '$entry_guid_hashed', |
2c08214a AD |
805 | '$entry_link', |
806 | '$entry_timestamp_fmt', | |
807 | '$entry_content', | |
b1840673 | 808 | '$entry_current_hash', |
5ba1ddd4 | 809 | false, |
2c08214a | 810 | NOW(), |
be574731 | 811 | '$date_feed_processed', |
2c08214a AD |
812 | '$entry_comments', |
813 | '$num_comments', | |
b30abdad | 814 | '$entry_plugin_data', |
6b461797 | 815 | '$entry_language', |
2c08214a | 816 | '$entry_author')"); |
e8291805 | 817 | |
2c08214a AD |
818 | } |
819 | ||
820 | // now it should exist, if not - bad luck then | |
821 | ||
b1840673 | 822 | $result = db_query("SELECT id FROM ttrss_entries |
5e3d5480 | 823 | WHERE guid = '$entry_guid' OR guid = '$entry_guid_hashed'"); |
2c08214a AD |
824 | |
825 | $entry_ref_id = 0; | |
826 | $entry_int_id = 0; | |
827 | ||
828 | if (db_num_rows($result) == 1) { | |
829 | ||
68cccafc | 830 | _debug("base guid found, checking for user record", $debug_enabled); |
2c08214a | 831 | |
2c08214a AD |
832 | $ref_id = db_fetch_result($result, 0, "id"); |
833 | $entry_ref_id = $ref_id; | |
834 | ||
5e3d5480 AD |
835 | /* $stored_guid = db_fetch_result($result, 0, "guid"); |
836 | if ($stored_guid != $entry_guid_hashed) { | |
68cccafc | 837 | if ($debug_enabled) _debug("upgrading compat guid to hashed one", $debug_enabled); |
5e3d5480 | 838 | |
a42c55f0 | 839 | db_query("UPDATE ttrss_entries SET guid = '$entry_guid_hashed' WHERE |
5e3d5480 AD |
840 | id = '$ref_id'"); |
841 | } */ | |
842 | ||
e6c886bf | 843 | if (RSSUtils::find_article_filter($article_filters, "filter")) { |
07877caf | 844 | //db_query("COMMIT"); // close transaction in progress |
2c08214a AD |
845 | continue; |
846 | } | |
847 | ||
e6c886bf | 848 | $score = RSSUtils::calculate_article_score($article_filters) + $entry_score_modifier; |
2c08214a | 849 | |
6de3a1be | 850 | _debug("initial score: $score [including plugin modifier: $entry_score_modifier]", $debug_enabled); |
2c08214a | 851 | |
4f186b1f AD |
852 | // check for user post link to main table |
853 | ||
2c08214a | 854 | $query = "SELECT ref_id, int_id FROM ttrss_user_entries WHERE |
5bdcb8fd | 855 | ref_id = '$ref_id' AND owner_uid = '$owner_uid'"; |
2c08214a AD |
856 | |
857 | // if ($_REQUEST["xdebug"]) print "$query\n"; | |
858 | ||
a42c55f0 | 859 | $result = db_query($query); |
2c08214a AD |
860 | |
861 | // okay it doesn't exist - create user entry | |
862 | if (db_num_rows($result) == 0) { | |
863 | ||
68cccafc | 864 | _debug("user record not found, creating...", $debug_enabled); |
2c08214a | 865 | |
e6c886bf | 866 | if ($score >= -500 && !RSSUtils::find_article_filter($article_filters, 'catchup') && !$entry_force_catchup) { |
2c08214a AD |
867 | $unread = 'true'; |
868 | $last_read_qpart = 'NULL'; | |
869 | } else { | |
870 | $unread = 'false'; | |
871 | $last_read_qpart = 'NOW()'; | |
872 | } | |
873 | ||
e6c886bf | 874 | if (RSSUtils::find_article_filter($article_filters, 'mark') || $score > 1000) { |
2c08214a AD |
875 | $marked = 'true'; |
876 | } else { | |
877 | $marked = 'false'; | |
878 | } | |
879 | ||
e6c886bf | 880 | if (RSSUtils::find_article_filter($article_filters, 'publish')) { |
2c08214a AD |
881 | $published = 'true'; |
882 | } else { | |
883 | $published = 'false'; | |
884 | } | |
885 | ||
7873d588 AD |
886 | $last_marked = ($marked == 'true') ? 'NOW()' : 'NULL'; |
887 | $last_published = ($published == 'true') ? 'NOW()' : 'NULL'; | |
888 | ||
6322ac79 | 889 | $result = db_query( |
2c08214a AD |
890 | "INSERT INTO ttrss_user_entries |
891 | (ref_id, owner_uid, feed_id, unread, last_read, marked, | |
7873d588 AD |
892 | published, score, tag_cache, label_cache, uuid, |
893 | last_marked, last_published) | |
2c08214a | 894 | VALUES ('$ref_id', '$owner_uid', '$feed', $unread, |
7873d588 AD |
895 | $last_read_qpart, $marked, $published, '$score', '', '', |
896 | '', $last_marked, $last_published)"); | |
2c08214a | 897 | |
6322ac79 | 898 | $result = db_query( |
2c08214a AD |
899 | "SELECT int_id FROM ttrss_user_entries WHERE |
900 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND | |
901 | feed_id = '$feed' LIMIT 1"); | |
902 | ||
903 | if (db_num_rows($result) == 1) { | |
904 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
905 | } | |
906 | } else { | |
68cccafc | 907 | _debug("user record FOUND", $debug_enabled); |
2c08214a AD |
908 | |
909 | $entry_ref_id = db_fetch_result($result, 0, "ref_id"); | |
910 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
911 | } | |
912 | ||
68cccafc | 913 | _debug("RID: $entry_ref_id, IID: $entry_int_id", $debug_enabled); |
2c08214a | 914 | |
e854442e | 915 | if (DB_TYPE == "pgsql") { |
e6c886bf | 916 | $tsvector_combined = db_escape_string(mb_substr($entry_title . ' ' . strip_tags(str_replace('<', ' <', $entry_content)), |
e854442e AD |
917 | 0, 1000000)); |
918 | ||
df659891 | 919 | $tsvector_qpart = "tsvector_combined = to_tsvector('$feed_language', '$tsvector_combined'),"; |
e854442e AD |
920 | |
921 | } else { | |
922 | $tsvector_qpart = ""; | |
923 | } | |
924 | ||
b1840673 AD |
925 | db_query("UPDATE ttrss_entries |
926 | SET title = '$entry_title', | |
927 | content = '$entry_content', | |
928 | content_hash = '$entry_current_hash', | |
929 | updated = '$entry_timestamp_fmt', | |
e854442e | 930 | $tsvector_qpart |
b1840673 AD |
931 | num_comments = '$num_comments', |
932 | plugin_data = '$entry_plugin_data', | |
933 | author = '$entry_author', | |
934 | lang = '$entry_language' | |
935 | WHERE id = '$ref_id'"); | |
936 | ||
59e83455 AD |
937 | // update aux data |
938 | db_query("UPDATE ttrss_user_entries | |
939 | SET score = '$score' WHERE ref_id = '$ref_id'"); | |
940 | ||
b1840673 | 941 | if ($mark_unread_on_update) { |
24e6ff5d AD |
942 | _debug("article updated, marking unread as requested.", $debug_enabled); |
943 | ||
b1840673 AD |
944 | db_query("UPDATE ttrss_user_entries |
945 | SET last_read = null, unread = true WHERE ref_id = '$ref_id'"); | |
2c08214a AD |
946 | } |
947 | } | |
948 | ||
07877caf | 949 | //db_query("COMMIT"); |
2c08214a | 950 | |
a29fe121 AD |
951 | _debug("assigning labels [other]...", $debug_enabled); |
952 | ||
953 | foreach ($article_labels as $label) { | |
7c9b5a3f | 954 | Labels::add_article($entry_ref_id, $label[1], $owner_uid); |
a29fe121 AD |
955 | } |
956 | ||
957 | _debug("assigning labels [filters]...", $debug_enabled); | |
2c08214a | 958 | |
e6c886bf | 959 | RSSUtils::assign_article_to_label_filters($entry_ref_id, $article_filters, |
b24504b1 | 960 | $owner_uid, $article_labels); |
2c08214a | 961 | |
68cccafc | 962 | _debug("looking for enclosures...", $debug_enabled); |
2c08214a AD |
963 | |
964 | // enclosures | |
965 | ||
966 | $enclosures = array(); | |
967 | ||
19b3992b | 968 | $encs = $item->get_enclosures(); |
2c08214a | 969 | |
19b3992b AD |
970 | if (is_array($encs)) { |
971 | foreach ($encs as $e) { | |
972 | $e_item = array( | |
86e53429 AD |
973 | rewrite_relative_url($site_url, $e->link), |
974 | $e->type, $e->length, $e->title, $e->width, $e->height); | |
2c08214a | 975 | array_push($enclosures, $e_item); |
2c08214a AD |
976 | } |
977 | } | |
978 | ||
388d4dfa | 979 | if ($cache_images && is_writable(CACHE_DIR . '/images')) |
e6c886bf | 980 | RSSUtils::cache_enclosures($enclosures, $site_url, $debug_enabled); |
388d4dfa | 981 | |
2c08214a | 982 | if ($debug_enabled) { |
68cccafc | 983 | _debug("article enclosures:", $debug_enabled); |
2c08214a AD |
984 | print_r($enclosures); |
985 | } | |
986 | ||
dab229cd | 987 | //db_query("BEGIN"); |
2c08214a | 988 | |
5c54e683 AD |
989 | // debugging |
990 | // db_query("DELETE FROM ttrss_enclosures WHERE post_id = '$entry_ref_id'"); | |
991 | ||
2c08214a | 992 | foreach ($enclosures as $enc) { |
a42c55f0 AD |
993 | $enc_url = db_escape_string($enc[0]); |
994 | $enc_type = db_escape_string($enc[1]); | |
995 | $enc_dur = db_escape_string($enc[2]); | |
5c54e683 | 996 | $enc_title = db_escape_string($enc[3]); |
523bd90b FE |
997 | $enc_width = intval($enc[4]); |
998 | $enc_height = intval($enc[5]); | |
2c08214a | 999 | |
a42c55f0 | 1000 | $result = db_query("SELECT id FROM ttrss_enclosures |
2c08214a AD |
1001 | WHERE content_url = '$enc_url' AND post_id = '$entry_ref_id'"); |
1002 | ||
1003 | if (db_num_rows($result) == 0) { | |
a42c55f0 | 1004 | db_query("INSERT INTO ttrss_enclosures |
523bd90b FE |
1005 | (content_url, content_type, title, duration, post_id, width, height) VALUES |
1006 | ('$enc_url', '$enc_type', '$enc_title', '$enc_dur', '$entry_ref_id', $enc_width, $enc_height)"); | |
2c08214a AD |
1007 | } |
1008 | } | |
1009 | ||
dab229cd | 1010 | //db_query("COMMIT"); |
2c08214a AD |
1011 | |
1012 | // check for manual tags (we have to do it here since they're loaded from filters) | |
1013 | ||
1014 | foreach ($article_filters as $f) { | |
6aff7845 | 1015 | if ($f["type"] == "tag") { |
2c08214a | 1016 | |
6aff7845 | 1017 | $manual_tags = trim_array(explode(",", $f["param"])); |
2c08214a AD |
1018 | |
1019 | foreach ($manual_tags as $tag) { | |
1020 | if (tag_is_valid($tag)) { | |
1021 | array_push($entry_tags, $tag); | |
1022 | } | |
1023 | } | |
1024 | } | |
1025 | } | |
1026 | ||
1027 | // Skip boring tags | |
1028 | ||
6322ac79 | 1029 | $boring_tags = trim_array(explode(",", mb_strtolower(get_pref( |
2c08214a AD |
1030 | 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8'))); |
1031 | ||
1032 | $filtered_tags = array(); | |
1033 | $tags_to_cache = array(); | |
1034 | ||
1035 | if ($entry_tags && is_array($entry_tags)) { | |
1036 | foreach ($entry_tags as $tag) { | |
1037 | if (array_search($tag, $boring_tags) === false) { | |
1038 | array_push($filtered_tags, $tag); | |
1039 | } | |
1040 | } | |
1041 | } | |
1042 | ||
1043 | $filtered_tags = array_unique($filtered_tags); | |
1044 | ||
1045 | if ($debug_enabled) { | |
68cccafc | 1046 | _debug("filtered article tags:", $debug_enabled); |
2c08214a AD |
1047 | print_r($filtered_tags); |
1048 | } | |
1049 | ||
1050 | // Save article tags in the database | |
1051 | ||
1052 | if (count($filtered_tags) > 0) { | |
1053 | ||
dab229cd | 1054 | //db_query("BEGIN"); |
2c08214a AD |
1055 | |
1056 | foreach ($filtered_tags as $tag) { | |
1057 | ||
1058 | $tag = sanitize_tag($tag); | |
a42c55f0 | 1059 | $tag = db_escape_string($tag); |
2c08214a AD |
1060 | |
1061 | if (!tag_is_valid($tag)) continue; | |
1062 | ||
a42c55f0 | 1063 | $result = db_query("SELECT id FROM ttrss_tags |
2c08214a AD |
1064 | WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND |
1065 | owner_uid = '$owner_uid' LIMIT 1"); | |
1066 | ||
e6c886bf | 1067 | if ($result && db_num_rows($result) == 0) { |
2c08214a | 1068 | |
e6c886bf | 1069 | db_query("INSERT INTO ttrss_tags |
2c08214a AD |
1070 | (owner_uid,tag_name,post_int_id) |
1071 | VALUES ('$owner_uid','$tag', '$entry_int_id')"); | |
e6c886bf | 1072 | } |
2c08214a AD |
1073 | |
1074 | array_push($tags_to_cache, $tag); | |
1075 | } | |
1076 | ||
1077 | /* update the cache */ | |
1078 | ||
1079 | $tags_to_cache = array_unique($tags_to_cache); | |
1080 | ||
a42c55f0 | 1081 | $tags_str = db_escape_string(join(",", $tags_to_cache)); |
2c08214a | 1082 | |
a42c55f0 | 1083 | db_query("UPDATE ttrss_user_entries |
2c08214a AD |
1084 | SET tag_cache = '$tags_str' WHERE ref_id = '$entry_ref_id' |
1085 | AND owner_uid = $owner_uid"); | |
1086 | ||
dab229cd | 1087 | //db_query("COMMIT"); |
2c08214a AD |
1088 | } |
1089 | ||
68cccafc | 1090 | _debug("article processed", $debug_enabled); |
2c08214a AD |
1091 | } |
1092 | ||
68cccafc | 1093 | _debug("purging feed...", $debug_enabled); |
2c08214a | 1094 | |
a42c55f0 | 1095 | purge_feed($feed, 0, $debug_enabled); |
2c08214a | 1096 | |
a42c55f0 | 1097 | db_query("UPDATE ttrss_feeds |
f2798eb6 | 1098 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); |
2c08214a | 1099 | |
a42c55f0 | 1100 | // db_query("COMMIT"); |
2c08214a AD |
1101 | |
1102 | } else { | |
1103 | ||
a42c55f0 | 1104 | $error_msg = db_escape_string(mb_substr($rss->error(), 0, 245)); |
2c08214a | 1105 | |
4ad04ee2 AD |
1106 | _debug("fetch error: $error_msg", $debug_enabled); |
1107 | ||
1108 | if (count($rss->errors()) > 1) { | |
1109 | foreach ($rss->errors() as $error) { | |
1110 | _debug("+ $error"); | |
1111 | } | |
1112 | } | |
2c08214a | 1113 | |
6322ac79 | 1114 | db_query( |
2c08214a | 1115 | "UPDATE ttrss_feeds SET last_error = '$error_msg', |
88edaa93 | 1116 | last_updated = NOW() WHERE id = '$feed'"); |
2c08214a | 1117 | |
88edaa93 | 1118 | unset($rss); |
7b55001e | 1119 | return; |
88edaa93 | 1120 | } |
2c08214a | 1121 | |
68cccafc | 1122 | _debug("done", $debug_enabled); |
88edaa93 | 1123 | |
7b55001e | 1124 | return true; |
2c08214a AD |
1125 | } |
1126 | ||
e6c886bf | 1127 | static function cache_enclosures($enclosures, $site_url, $debug) { |
388d4dfa AD |
1128 | foreach ($enclosures as $enc) { |
1129 | ||
1130 | if (preg_match("/(image|audio|video)/", $enc[1])) { | |
1131 | ||
1132 | $src = rewrite_relative_url($site_url, $enc[0]); | |
1133 | ||
1134 | $local_filename = CACHE_DIR . "/images/" . sha1($src); | |
1135 | ||
1136 | if ($debug) _debug("cache_enclosures: downloading: $src to $local_filename"); | |
1137 | ||
1138 | if (!file_exists($local_filename)) { | |
1139 | $file_content = fetch_file_contents($src); | |
1140 | ||
6fd03996 | 1141 | if ($file_content && strlen($file_content) > MIN_CACHE_FILE_SIZE) { |
388d4dfa AD |
1142 | file_put_contents($local_filename, $file_content); |
1143 | } | |
1144 | } else { | |
1145 | touch($local_filename); | |
1146 | } | |
1147 | } | |
1148 | } | |
1149 | } | |
1150 | ||
e6c886bf | 1151 | static function cache_media($html, $site_url, $debug) { |
3c696512 AD |
1152 | libxml_use_internal_errors(true); |
1153 | ||
1154 | $charset_hack = '<head> | |
1155 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
1156 | </head>'; | |
1157 | ||
1158 | $doc = new DOMDocument(); | |
1159 | $doc->loadHTML($charset_hack . $html); | |
1160 | $xpath = new DOMXPath($doc); | |
1161 | ||
388d4dfa | 1162 | $entries = $xpath->query('(//img[@src])|(//video/source[@src])|(//audio/source[@src])'); |
3c696512 AD |
1163 | |
1164 | foreach ($entries as $entry) { | |
5edd605a | 1165 | if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) { |
3c696512 AD |
1166 | $src = rewrite_relative_url($site_url, $entry->getAttribute('src')); |
1167 | ||
41bead9b | 1168 | $local_filename = CACHE_DIR . "/images/" . sha1($src); |
3c696512 | 1169 | |
41bead9b | 1170 | if ($debug) _debug("cache_media: downloading: $src to $local_filename"); |
3c696512 AD |
1171 | |
1172 | if (!file_exists($local_filename)) { | |
1173 | $file_content = fetch_file_contents($src); | |
1174 | ||
6fd03996 | 1175 | if ($file_content && strlen($file_content) > MIN_CACHE_FILE_SIZE) { |
3c696512 AD |
1176 | file_put_contents($local_filename, $file_content); |
1177 | } | |
4a27966e J |
1178 | } else { |
1179 | touch($local_filename); | |
3c696512 | 1180 | } |
3c696512 AD |
1181 | } |
1182 | } | |
3c696512 AD |
1183 | } |
1184 | ||
e6c886bf | 1185 | static function expire_error_log($debug) { |
e2261e17 AD |
1186 | if ($debug) _debug("Removing old error log entries..."); |
1187 | ||
1188 | if (DB_TYPE == "pgsql") { | |
a42c55f0 | 1189 | db_query("DELETE FROM ttrss_error_log |
e2261e17 AD |
1190 | WHERE created_at < NOW() - INTERVAL '7 days'"); |
1191 | } else { | |
a42c55f0 | 1192 | db_query("DELETE FROM ttrss_error_log |
e2261e17 AD |
1193 | WHERE created_at < DATE_SUB(NOW(), INTERVAL 7 DAY)"); |
1194 | } | |
1195 | ||
1196 | } | |
1197 | ||
e6c886bf | 1198 | static function expire_lock_files($debug) { |
65465085 | 1199 | //if ($debug) _debug("Removing old lock files..."); |
2a91b6ff AD |
1200 | |
1201 | $num_deleted = 0; | |
1202 | ||
1203 | if (is_writable(LOCK_DIRECTORY)) { | |
1204 | $files = glob(LOCK_DIRECTORY . "/*.lock"); | |
1205 | ||
1206 | if ($files) { | |
1207 | foreach ($files as $file) { | |
11344971 | 1208 | if (!file_is_locked(basename($file)) && time() - filemtime($file) > 86400*2) { |
2a91b6ff AD |
1209 | unlink($file); |
1210 | ++$num_deleted; | |
1211 | } | |
1212 | } | |
1213 | } | |
1214 | } | |
1215 | ||
65465085 | 1216 | if ($debug) _debug("Removed $num_deleted old lock files."); |
2a91b6ff AD |
1217 | } |
1218 | ||
e6c886bf | 1219 | static function expire_cached_files($debug) { |
3306daec | 1220 | foreach (array("simplepie", "images", "export", "upload") as $dir) { |
3c696512 | 1221 | $cache_dir = CACHE_DIR . "/$dir"; |
2c08214a | 1222 | |
65465085 | 1223 | // if ($debug) _debug("Expiring $cache_dir"); |
2c08214a | 1224 | |
3c696512 AD |
1225 | $num_deleted = 0; |
1226 | ||
1227 | if (is_writable($cache_dir)) { | |
1228 | $files = glob("$cache_dir/*"); | |
1229 | ||
2a91b6ff | 1230 | if ($files) { |
2ab20c31 | 1231 | foreach ($files as $file) { |
6fd03996 | 1232 | if (time() - filemtime($file) > 86400*CACHE_MAX_DAYS) { |
2ab20c31 | 1233 | unlink($file); |
3c696512 | 1234 | |
2ab20c31 AD |
1235 | ++$num_deleted; |
1236 | } | |
3c696512 AD |
1237 | } |
1238 | } | |
2a91b6ff | 1239 | } |
3c696512 | 1240 | |
65465085 | 1241 | if ($debug) _debug("$cache_dir: removed $num_deleted files."); |
3c696512 AD |
1242 | } |
1243 | } | |
2c08214a | 1244 | |
a3e0bdcf | 1245 | /** |
e6c886bf AD |
1246 | * Source: http://www.php.net/manual/en/function.parse-url.php#104527 |
1247 | * Returns the url query as associative array | |
1248 | * | |
1249 | * @param string query | |
1250 | * @return array params | |
1251 | */ | |
1252 | static function convertUrlQuery($query) { | |
a3e0bdcf AD |
1253 | $queryParts = explode('&', $query); |
1254 | ||
1255 | $params = array(); | |
1256 | ||
1257 | foreach ($queryParts as $param) { | |
1258 | $item = explode('=', $param); | |
1259 | $params[$item[0]] = $item[1]; | |
1260 | } | |
1261 | ||
1262 | return $params; | |
1263 | } | |
92c14e9d | 1264 | |
e6c886bf | 1265 | static function get_article_filters($filters, $title, $content, $link, $author, $tags, &$matched_rules = false) { |
92c14e9d AD |
1266 | $matches = array(); |
1267 | ||
1268 | foreach ($filters as $filter) { | |
1269 | $match_any_rule = $filter["match_any_rule"]; | |
a3a896a1 | 1270 | $inverse = $filter["inverse"]; |
92c14e9d AD |
1271 | $filter_match = false; |
1272 | ||
1273 | foreach ($filter["rules"] as $rule) { | |
1274 | $match = false; | |
ffa1bd7b | 1275 | $reg_exp = str_replace('/', '\/', $rule["reg_exp"]); |
a3a896a1 | 1276 | $rule_inverse = $rule["inverse"]; |
92c14e9d AD |
1277 | |
1278 | if (!$reg_exp) | |
1279 | continue; | |
1280 | ||
1281 | switch ($rule["type"]) { | |
e6c886bf AD |
1282 | case "title": |
1283 | $match = @preg_match("/$reg_exp/iu", $title); | |
1284 | break; | |
1285 | case "content": | |
1286 | // we don't need to deal with multiline regexps | |
1287 | $content = preg_replace("/[\r\n\t]/", "", $content); | |
d03ae73e | 1288 | |
e6c886bf AD |
1289 | $match = @preg_match("/$reg_exp/iu", $content); |
1290 | break; | |
1291 | case "both": | |
1292 | // we don't need to deal with multiline regexps | |
1293 | $content = preg_replace("/[\r\n\t]/", "", $content); | |
d03ae73e | 1294 | |
e6c886bf AD |
1295 | $match = (@preg_match("/$reg_exp/iu", $title) || @preg_match("/$reg_exp/iu", $content)); |
1296 | break; | |
1297 | case "link": | |
1298 | $match = @preg_match("/$reg_exp/iu", $link); | |
1299 | break; | |
1300 | case "author": | |
1301 | $match = @preg_match("/$reg_exp/iu", $author); | |
1302 | break; | |
1303 | case "tag": | |
1304 | foreach ($tags as $tag) { | |
1305 | if (@preg_match("/$reg_exp/iu", $tag)) { | |
1306 | $match = true; | |
1307 | break; | |
1308 | } | |
7b80b5e1 | 1309 | } |
e6c886bf | 1310 | break; |
92c14e9d AD |
1311 | } |
1312 | ||
a3a896a1 AD |
1313 | if ($rule_inverse) $match = !$match; |
1314 | ||
92c14e9d AD |
1315 | if ($match_any_rule) { |
1316 | if ($match) { | |
1317 | $filter_match = true; | |
1318 | break; | |
1319 | } | |
1320 | } else { | |
1321 | $filter_match = $match; | |
1322 | if (!$match) { | |
1323 | break; | |
1324 | } | |
1325 | } | |
1326 | } | |
1327 | ||
a3a896a1 AD |
1328 | if ($inverse) $filter_match = !$filter_match; |
1329 | ||
92c14e9d | 1330 | if ($filter_match) { |
557d86fe AD |
1331 | if (is_array($matched_rules)) array_push($matched_rules, $rule); |
1332 | ||
92c14e9d AD |
1333 | foreach ($filter["actions"] AS $action) { |
1334 | array_push($matches, $action); | |
5e736e45 AD |
1335 | |
1336 | // if Stop action encountered, perform no further processing | |
fd3e5e8d | 1337 | if (isset($action["type"]) && $action["type"] == "stop") return $matches; |
92c14e9d AD |
1338 | } |
1339 | } | |
1340 | } | |
1341 | ||
1342 | return $matches; | |
1343 | } | |
1344 | ||
e6c886bf | 1345 | static function find_article_filter($filters, $filter_name) { |
92c14e9d AD |
1346 | foreach ($filters as $f) { |
1347 | if ($f["type"] == $filter_name) { | |
1348 | return $f; | |
1349 | }; | |
1350 | } | |
1351 | return false; | |
1352 | } | |
1353 | ||
e6c886bf | 1354 | static function find_article_filters($filters, $filter_name) { |
92c14e9d AD |
1355 | $results = array(); |
1356 | ||
1357 | foreach ($filters as $f) { | |
1358 | if ($f["type"] == $filter_name) { | |
1359 | array_push($results, $f); | |
1360 | }; | |
1361 | } | |
1362 | return $results; | |
1363 | } | |
1364 | ||
e6c886bf | 1365 | static function calculate_article_score($filters) { |
92c14e9d AD |
1366 | $score = 0; |
1367 | ||
1368 | foreach ($filters as $f) { | |
1369 | if ($f["type"] == "score") { | |
1370 | $score += $f["param"]; | |
1371 | }; | |
1372 | } | |
1373 | return $score; | |
1374 | } | |
1375 | ||
e6c886bf | 1376 | static function labels_contains_caption($labels, $caption) { |
b24504b1 AD |
1377 | foreach ($labels as $label) { |
1378 | if ($label[1] == $caption) { | |
1379 | return true; | |
1380 | } | |
1381 | } | |
1382 | ||
1383 | return false; | |
1384 | } | |
1385 | ||
e6c886bf | 1386 | static function assign_article_to_label_filters($id, $filters, $owner_uid, $article_labels) { |
92c14e9d AD |
1387 | foreach ($filters as $f) { |
1388 | if ($f["type"] == "label") { | |
e6c886bf | 1389 | if (!RSSUtils::labels_contains_caption($article_labels, $f["param"])) { |
7c9b5a3f | 1390 | Labels::add_article($id, $f["param"], $owner_uid); |
b24504b1 AD |
1391 | } |
1392 | } | |
92c14e9d AD |
1393 | } |
1394 | } | |
87764a50 | 1395 | |
e6c886bf | 1396 | static function make_guid_from_title($title) { |
87d7e850 AD |
1397 | return preg_replace("/[ \"\',.:;]/", "-", |
1398 | mb_strtolower(strip_tags($title), 'utf-8')); | |
1399 | } | |
1400 | ||
e6c886bf | 1401 | static function cleanup_counters_cache($debug) { |
168cf351 AD |
1402 | $result = db_query("DELETE FROM ttrss_counters_cache |
1403 | WHERE feed_id > 0 AND | |
1404 | (SELECT COUNT(id) FROM ttrss_feeds WHERE | |
1405 | id = feed_id AND | |
1406 | ttrss_counters_cache.owner_uid = ttrss_feeds.owner_uid) = 0"); | |
1407 | $frows = db_affected_rows($result); | |
1408 | ||
1409 | $result = db_query("DELETE FROM ttrss_cat_counters_cache | |
1410 | WHERE feed_id > 0 AND | |
1411 | (SELECT COUNT(id) FROM ttrss_feed_categories WHERE | |
1412 | id = feed_id AND | |
1413 | ttrss_cat_counters_cache.owner_uid = ttrss_feed_categories.owner_uid) = 0"); | |
1414 | $crows = db_affected_rows($result); | |
1415 | ||
7b55001e | 1416 | if ($debug) _debug("Removed $frows (feeds) $crows (cats) orphaned counter cache entries."); |
168cf351 AD |
1417 | } |
1418 | ||
e6c886bf | 1419 | static function housekeeping_user($owner_uid) { |
5cbd1fe8 AD |
1420 | $tmph = new PluginHost(); |
1421 | ||
1422 | load_user_plugins($owner_uid, $tmph); | |
1423 | ||
1424 | $tmph->run_hooks(PluginHost::HOOK_HOUSE_KEEPING, "hook_house_keeping", ""); | |
1425 | } | |
1426 | ||
e6c886bf AD |
1427 | static function housekeeping_common($debug) { |
1428 | RSSUtils::expire_cached_files($debug); | |
1429 | RSSUtils::expire_lock_files($debug); | |
1430 | RSSUtils::expire_error_log($debug); | |
e2cf81e2 | 1431 | |
e6c886bf | 1432 | $count = RSSUtils::update_feedbrowser_cache(); |
e2cf81e2 AD |
1433 | _debug("Feedbrowser updated, $count feeds processed."); |
1434 | ||
a230bf88 | 1435 | Article::purge_orphans( true); |
e6c886bf | 1436 | RSSUtils::cleanup_counters_cache($debug); |
e2cf81e2 | 1437 | |
9b736a20 AD |
1438 | //$rc = cleanup_tags( 14, 50000); |
1439 | //_debug("Cleaned $rc cached tags."); | |
8e470220 | 1440 | |
00f22824 | 1441 | PluginHost::getInstance()->run_hooks(PluginHost::HOOK_HOUSE_KEEPING, "hook_house_keeping", ""); |
e2cf81e2 | 1442 | } |
ea79a0e0 | 1443 | |
e6c886bf AD |
1444 | static function check_feed_favicon($site_url, $feed) { |
1445 | # print "FAVICON [$site_url]: $favicon_url\n"; | |
a230bf88 AD |
1446 | |
1447 | $icon_file = ICONS_DIR . "/$feed.ico"; | |
1448 | ||
1449 | if (!file_exists($icon_file)) { | |
1450 | $favicon_url = get_favicon_url($site_url); | |
1451 | ||
1452 | if ($favicon_url) { | |
1453 | // Limiting to "image" type misses those served with text/plain | |
1454 | $contents = fetch_file_contents($favicon_url); // , "image"); | |
1455 | ||
1456 | if ($contents) { | |
1457 | // Crude image type matching. | |
1458 | // Patterns gleaned from the file(1) source code. | |
1459 | if (preg_match('/^\x00\x00\x01\x00/', $contents)) { | |
1460 | // 0 string \000\000\001\000 MS Windows icon resource | |
1461 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa MS Windows icon resource"); | |
1462 | } | |
1463 | elseif (preg_match('/^GIF8/', $contents)) { | |
1464 | // 0 string GIF8 GIF image data | |
1465 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa GIF image"); | |
1466 | } | |
1467 | elseif (preg_match('/^\x89PNG\x0d\x0a\x1a\x0a/', $contents)) { | |
1468 | // 0 string \x89PNG\x0d\x0a\x1a\x0a PNG image data | |
1469 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa PNG image"); | |
1470 | } | |
1471 | elseif (preg_match('/^\xff\xd8/', $contents)) { | |
1472 | // 0 beshort 0xffd8 JPEG image data | |
1473 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa JPG image"); | |
1474 | } | |
1475 | else { | |
1476 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa UNKNOWN type"); | |
1477 | $contents = ""; | |
1478 | } | |
1479 | } | |
1480 | ||
1481 | if ($contents) { | |
1482 | $fp = @fopen($icon_file, "w"); | |
1483 | ||
1484 | if ($fp) { | |
1485 | fwrite($fp, $contents); | |
1486 | fclose($fp); | |
1487 | chmod($icon_file, 0644); | |
1488 | } | |
1489 | } | |
1490 | } | |
1491 | return $icon_file; | |
1492 | } | |
1493 | } | |
e6c886bf AD |
1494 | |
1495 | ||
1496 | ||
1497 | } |