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