]>
Commit | Line | Data |
---|---|---|
40d13c28 | 1 | <? |
f1a80dae | 2 | |
894ebcf5 | 3 | /* if ($_GET["debug"]) { |
cce28758 AD |
4 | define('DEFAULT_ERROR_LEVEL', E_ALL); |
5 | } else { | |
6 | define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE); | |
894ebcf5 | 7 | } */ |
cce28758 | 8 | |
40d13c28 | 9 | require_once 'config.php'; |
b619ff15 | 10 | require_once 'db-prefs.php'; |
5bc0bd27 | 11 | require_once 'compat.php'; |
40d13c28 | 12 | |
387234f3 AD |
13 | require_once 'magpierss/rss_utils.inc'; |
14 | ||
a3ee2a38 AD |
15 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); |
16 | ||
ad507f85 AD |
17 | function purge_feed($link, $feed_id, $purge_interval, $debug = false) { |
18 | ||
19 | $rows = -1; | |
4c193675 | 20 | |
fefa6ca3 | 21 | if (DB_TYPE == "pgsql") { |
44e241cb | 22 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 23 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 24 | (SELECT date_entered FROM ttrss_entries WHERE |
44e241cb AD |
25 | id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */ |
26 | ||
27 | $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE | |
28 | ttrss_entries.id = ref_id AND | |
29 | marked = false AND | |
30 | feed_id = '$feed_id' AND | |
31 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); | |
ad507f85 AD |
32 | |
33 | $rows = pg_affected_rows($result); | |
34 | ||
fefa6ca3 | 35 | } else { |
30f1746f | 36 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 37 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 38 | (SELECT date_entered FROM ttrss_entries WHERE |
30f1746f AD |
39 | id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */ |
40 | ||
41 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
42 | USING ttrss_user_entries, ttrss_entries | |
43 | WHERE ttrss_entries.id = ref_id AND | |
44 | marked = false AND | |
45 | feed_id = '$feed_id' AND | |
46 | ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); | |
47 | ||
ad507f85 AD |
48 | $rows = mysql_affected_rows($link); |
49 | ||
50 | } | |
51 | ||
52 | if ($debug) { | |
53 | print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n"; | |
fefa6ca3 AD |
54 | } |
55 | } | |
56 | ||
44e241cb AD |
57 | function global_purge_old_posts($link, $do_output = false, $limit = false) { |
58 | ||
894ebcf5 | 59 | $random_qpart = sql_random_function(); |
fefa6ca3 | 60 | |
44e241cb AD |
61 | if ($limit) { |
62 | $limit_qpart = "LIMIT $limit"; | |
63 | } else { | |
64 | $limit_qpart = ""; | |
65 | } | |
66 | ||
fefa6ca3 | 67 | $result = db_query($link, |
44e241cb AD |
68 | "SELECT id,purge_interval,owner_uid FROM ttrss_feeds |
69 | ORDER BY $random_qpart $limit_qpart"); | |
fefa6ca3 AD |
70 | |
71 | while ($line = db_fetch_assoc($result)) { | |
72 | ||
73 | $feed_id = $line["id"]; | |
74 | $purge_interval = $line["purge_interval"]; | |
75 | $owner_uid = $line["owner_uid"]; | |
76 | ||
77 | if ($purge_interval == 0) { | |
78 | ||
79 | $tmp_result = db_query($link, | |
80 | "SELECT value FROM ttrss_user_prefs WHERE | |
81 | pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'"); | |
82 | ||
83 | if (db_num_rows($tmp_result) != 0) { | |
84 | $purge_interval = db_fetch_result($tmp_result, 0, "value"); | |
85 | } | |
86 | } | |
87 | ||
88 | if ($do_output) { | |
ad507f85 | 89 | // print "Feed $feed_id: purge interval = $purge_interval\n"; |
fefa6ca3 AD |
90 | } |
91 | ||
92 | if ($purge_interval > 0) { | |
ad507f85 | 93 | purge_feed($link, $feed_id, $purge_interval, $do_output); |
fefa6ca3 AD |
94 | } |
95 | } | |
96 | ||
71604ca4 AD |
97 | // purge orphaned posts in main content table |
98 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
99 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
100 | ||
fefa6ca3 AD |
101 | } |
102 | ||
b6eefba5 | 103 | function purge_old_posts($link) { |
5d73494a | 104 | |
f1a80dae AD |
105 | $user_id = $_SESSION["uid"]; |
106 | ||
107 | $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds | |
108 | WHERE owner_uid = '$user_id'"); | |
5d73494a AD |
109 | |
110 | while ($line = db_fetch_assoc($result)) { | |
111 | ||
112 | $feed_id = $line["id"]; | |
113 | $purge_interval = $line["purge_interval"]; | |
114 | ||
b619ff15 | 115 | if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS'); |
5d73494a | 116 | |
140aae81 | 117 | if ($purge_interval > 0) { |
fefa6ca3 | 118 | purge_feed($link, $feed_id, $purge_interval); |
5d73494a AD |
119 | } |
120 | } | |
71604ca4 AD |
121 | |
122 | // purge orphaned posts in main content table | |
123 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
124 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
c3a8d71a AD |
125 | } |
126 | ||
1f2b01ed | 127 | function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) { |
40d13c28 | 128 | |
4769ddaf | 129 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 130 | |
a2770077 AD |
131 | if (!$user_id) { |
132 | $user_id = $_SESSION["uid"]; | |
133 | purge_old_posts($link); | |
134 | } | |
135 | ||
25af8dad | 136 | // db_query($link, "BEGIN"); |
b82af8c3 | 137 | |
cbd8650d AD |
138 | if (MAX_UPDATE_TIME > 0) { |
139 | if (DB_TYPE == "mysql") { | |
140 | $q_order = "RAND()"; | |
141 | } else { | |
142 | $q_order = "RANDOM()"; | |
143 | } | |
144 | } else { | |
145 | $q_order = "last_updated DESC"; | |
146 | } | |
147 | ||
d148926e | 148 | $result = db_query($link, "SELECT feed_url,id, |
798f722b | 149 | SUBSTRING(last_updated,1,19) AS last_updated, |
5c563acd | 150 | update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id' |
cbd8650d AD |
151 | ORDER BY $q_order"); |
152 | ||
153 | $upd_start = time(); | |
40d13c28 | 154 | |
b6eefba5 | 155 | while ($line = db_fetch_assoc($result)) { |
d148926e AD |
156 | $upd_intl = $line["update_interval"]; |
157 | ||
b619ff15 | 158 | if (!$upd_intl || $upd_intl == 0) { |
a2770077 | 159 | $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id); |
b619ff15 | 160 | } |
d148926e | 161 | |
c1e202b7 AD |
162 | if ($upd_intl < 0) { |
163 | // Updates for this feed are disabled | |
164 | continue; | |
165 | } | |
166 | ||
93d40f50 AD |
167 | if ($fetch || (!$line["last_updated"] || |
168 | time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) { | |
c5142cca | 169 | |
cbd8650d AD |
170 | // print "<!-- feed: ".$line["feed_url"]." -->"; |
171 | ||
1f2b01ed | 172 | update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon); |
cbd8650d AD |
173 | |
174 | $upd_elapsed = time() - $upd_start; | |
175 | ||
176 | if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) { | |
177 | return; | |
178 | } | |
d148926e | 179 | } |
40d13c28 AD |
180 | } |
181 | ||
25af8dad | 182 | // db_query($link, "COMMIT"); |
b82af8c3 | 183 | |
40d13c28 AD |
184 | } |
185 | ||
9e997874 | 186 | function check_feed_favicon($feed_url, $feed, $link) { |
78800912 AD |
187 | $feed_url = str_replace("http://", "", $feed_url); |
188 | $feed_url = preg_replace("/\/.*$/", "", $feed_url); | |
189 | ||
190 | $icon_url = "http://$feed_url/favicon.ico"; | |
273a2f6b | 191 | $icon_file = ICONS_DIR . "/$feed.ico"; |
78800912 AD |
192 | |
193 | if (!file_exists($icon_file)) { | |
e695fdc8 | 194 | |
78800912 AD |
195 | error_reporting(0); |
196 | $r = fopen($icon_url, "r"); | |
cce28758 | 197 | error_reporting (DEFAULT_ERROR_LEVEL); |
78800912 AD |
198 | |
199 | if ($r) { | |
7d7cbaf5 | 200 | $tmpfname = tempnam(TMP_DIRECTORY, "ttrssicon"); |
78800912 AD |
201 | |
202 | $t = fopen($tmpfname, "w"); | |
203 | ||
204 | while (!feof($r)) { | |
205 | $buf = fread($r, 16384); | |
206 | fwrite($t, $buf); | |
207 | } | |
208 | ||
209 | fclose($r); | |
210 | fclose($t); | |
211 | ||
e695fdc8 AD |
212 | error_reporting(0); |
213 | if (!rename($tmpfname, $icon_file)) { | |
214 | unlink($tmpfname); | |
215 | } | |
717f5e64 AD |
216 | |
217 | chmod($icon_file, 0644); | |
218 | ||
cce28758 | 219 | error_reporting (DEFAULT_ERROR_LEVEL); |
78800912 AD |
220 | |
221 | } | |
222 | } | |
223 | } | |
224 | ||
ddb68b81 | 225 | function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) { |
40d13c28 | 226 | |
4769ddaf | 227 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 228 | |
ddb68b81 | 229 | if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) { |
21cfcdf2 AD |
230 | return; |
231 | } | |
232 | ||
47c6c988 | 233 | $result = db_query($link, "SELECT update_interval,auth_login,auth_pass |
a88c1f36 AD |
234 | FROM ttrss_feeds WHERE id = '$feed'"); |
235 | ||
47c6c988 AD |
236 | $auth_login = db_fetch_result($result, 0, "auth_login"); |
237 | $auth_pass = db_fetch_result($result, 0, "auth_pass"); | |
238 | ||
a88c1f36 AD |
239 | $update_interval = db_fetch_result($result, 0, "update_interval"); |
240 | ||
241 | if ($update_interval < 0) { return; } | |
242 | ||
ab3d0b99 AD |
243 | $feed = db_escape_string($feed); |
244 | ||
47c6c988 AD |
245 | $fetch_url = $feed_url; |
246 | ||
247 | if ($auth_login && $auth_pass) { | |
248 | $url_parts = array(); | |
249 | preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts); | |
250 | ||
251 | if ($url_parts[1] && $url_parts[2]) { | |
252 | $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2]; | |
253 | } | |
254 | ||
255 | } | |
3ad5aa85 | 256 | error_reporting(0); |
47c6c988 | 257 | $rss = fetch_rss($fetch_url); |
ab3d0b99 | 258 | |
cce28758 | 259 | error_reporting (DEFAULT_ERROR_LEVEL); |
76798ff3 | 260 | |
b6eefba5 | 261 | $feed = db_escape_string($feed); |
dcee8f61 | 262 | |
40d13c28 | 263 | if ($rss) { |
b82af8c3 | 264 | |
44e241cb | 265 | // db_query($link, "BEGIN"); |
dd8c76a9 | 266 | |
a88c1f36 | 267 | $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid |
f324892e | 268 | FROM ttrss_feeds WHERE id = '$feed'"); |
331900c6 | 269 | |
b6eefba5 AD |
270 | $registered_title = db_fetch_result($result, 0, "title"); |
271 | $orig_icon_url = db_fetch_result($result, 0, "icon_url"); | |
f324892e | 272 | $orig_site_url = db_fetch_result($result, 0, "site_url"); |
331900c6 | 273 | |
7fed1940 AD |
274 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); |
275 | ||
a2770077 AD |
276 | if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) { |
277 | check_feed_favicon($feed_url, $feed, $link); | |
278 | } | |
279 | ||
746b249f | 280 | if (!$registered_title || $registered_title == "[Unknown]") { |
e1305a97 | 281 | $feed_title = db_escape_string($rss->channel["title"]); |
f324892e AD |
282 | db_query($link, "UPDATE ttrss_feeds SET |
283 | title = '$feed_title' WHERE id = '$feed'"); | |
284 | } | |
285 | ||
147f7691 | 286 | $site_url = $rss->channel["link"]; |
832b7bfc AD |
287 | // weird, weird Magpie |
288 | if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]); | |
147f7691 AD |
289 | |
290 | if ($site_url && $orig_site_url != db_escape_string($site_url)) { | |
f324892e AD |
291 | db_query($link, "UPDATE ttrss_feeds SET |
292 | site_url = '$site_url' WHERE id = '$feed'"); | |
331900c6 | 293 | } |
40d13c28 | 294 | |
b7f4bda2 AD |
295 | // print "I: " . $rss->channel["image"]["url"]; |
296 | ||
297 | $icon_url = $rss->image["url"]; | |
298 | ||
147f7691 | 299 | if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) { |
b6eefba5 AD |
300 | $icon_url = db_escape_string($icon_url); |
301 | db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'"); | |
b7f4bda2 AD |
302 | } |
303 | ||
e6155a06 AD |
304 | |
305 | $filters = array(); | |
306 | ||
4b3dff6e | 307 | $result = db_query($link, "SELECT reg_exp, |
db42b934 AD |
308 | ttrss_filter_types.name AS name, |
309 | ttrss_filter_actions.name AS action | |
310 | FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE | |
311 | owner_uid = $owner_uid AND | |
312 | ttrss_filter_types.id = filter_type AND | |
313 | ttrss_filter_actions.id = action_id AND | |
ead60402 | 314 | (feed_id IS NULL OR feed_id = '$feed')"); |
e6155a06 | 315 | |
b6eefba5 | 316 | while ($line = db_fetch_assoc($result)) { |
e6155a06 | 317 | if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); |
19c9cb11 AD |
318 | |
319 | $filter["reg_exp"] = $line["reg_exp"]; | |
320 | $filter["action"] = $line["action"]; | |
321 | ||
322 | array_push($filters[$line["name"]], $filter); | |
e6155a06 AD |
323 | } |
324 | ||
ddb68b81 AD |
325 | $iterator = $rss->items; |
326 | ||
c22789da AD |
327 | if (!$iterator || !is_array($iterator)) $iterator = $rss->entries; |
328 | if (!$iterator || !is_array($iterator)) $iterator = $rss; | |
329 | ||
330 | if (!is_array($iterator)) { | |
9a7a7b41 | 331 | db_query($link, "UPDATE ttrss_feeds |
75bd0669 | 332 | SET last_error = 'Parse error: can\'t find any articles.' |
9a7a7b41 | 333 | WHERE id = '$feed'"); |
c22789da AD |
334 | return; // WTF? |
335 | } | |
ddb68b81 AD |
336 | |
337 | foreach ($iterator as $item) { | |
40d13c28 AD |
338 | |
339 | $entry_guid = $item["id"]; | |
340 | ||
341 | if (!$entry_guid) $entry_guid = $item["guid"]; | |
342 | if (!$entry_guid) $entry_guid = $item["link"]; | |
466001c4 AD |
343 | |
344 | if (!$entry_guid) continue; | |
a116f569 | 345 | |
9c9c7e6b | 346 | $entry_timestamp = ""; |
b82af8c3 | 347 | |
9c9c7e6b AD |
348 | $rss_2_date = $item['pubdate']; |
349 | $rss_1_date = $item['dc']['date']; | |
350 | $atom_date = $item['issued']; | |
59ba2c75 | 351 | if (!$atom_date) $atom_date = $item['updated']; |
b197f117 | 352 | |
9c9c7e6b AD |
353 | if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date); |
354 | if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date); | |
355 | if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date); | |
b82af8c3 AD |
356 | |
357 | if ($entry_timestamp == "") { | |
358 | $entry_timestamp = time(); | |
359 | $no_orig_date = 'true'; | |
466001c4 AD |
360 | } else { |
361 | $no_orig_date = 'false'; | |
b82af8c3 | 362 | } |
b197f117 | 363 | |
466001c4 | 364 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); |
71ad3959 | 365 | |
40d13c28 | 366 | $entry_title = $item["title"]; |
ddb68b81 AD |
367 | |
368 | // strange Magpie workaround | |
369 | $entry_link = $item["link_"]; | |
370 | if (!$entry_link) $entry_link = $item["link"]; | |
71ad3959 AD |
371 | |
372 | if (!$entry_title) continue; | |
373 | if (!$entry_link) continue; | |
374 | ||
1696229f AD |
375 | $entry_content = $item["content:escaped"]; |
376 | ||
377 | if (!$entry_content) $entry_content = $item["content:encoded"]; | |
40d13c28 | 378 | if (!$entry_content) $entry_content = $item["content"]; |
372ced8b | 379 | if (!$entry_content) $entry_content = $item["summary"]; |
1696229f | 380 | if (!$entry_content) $entry_content = $item["description"]; |
a2015351 | 381 | |
a116f569 | 382 | // if (!$entry_content) continue; |
a2015351 | 383 | |
8add756a AD |
384 | // WTF |
385 | if (is_array($entry_content)) { | |
386 | $entry_content = $entry_content["encoded"]; | |
1696229f | 387 | if (!$entry_content) $entry_content = $entry_content["escaped"]; |
8add756a AD |
388 | } |
389 | ||
1696229f | 390 | // print_r($item); |
372ced8b AD |
391 | // print_r(htmlspecialchars($entry_content)); |
392 | // print "<br>"; | |
1696229f | 393 | |
372ced8b | 394 | $entry_content_unescaped = $entry_content; |
466001c4 | 395 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); |
cb0bd8bd | 396 | |
a1ea1e12 AD |
397 | $entry_comments = $item["comments"]; |
398 | ||
b6104dee AD |
399 | $entry_author = db_escape_string($item['dc']['creator']); |
400 | ||
b6eefba5 | 401 | $entry_guid = db_escape_string($entry_guid); |
2651fc4f | 402 | |
05732aa0 AD |
403 | $result = db_query($link, "SELECT id FROM ttrss_entries |
404 | WHERE guid = '$entry_guid'"); | |
4c193675 | 405 | |
b17fcb1a AD |
406 | $entry_content = db_escape_string($entry_content); |
407 | $entry_title = db_escape_string($entry_title); | |
408 | $entry_link = db_escape_string($entry_link); | |
409 | $entry_comments = db_escape_string($entry_comments); | |
410 | ||
27f089dc | 411 | $num_comments = db_escape_string($item["slash"]["comments"]); |
11b0dce2 | 412 | |
e31073bd AD |
413 | if (!$num_comments) $num_comments = 0; |
414 | ||
44e241cb AD |
415 | db_query($link, "BEGIN"); |
416 | ||
4c193675 AD |
417 | if (db_num_rows($result) == 0) { |
418 | ||
419 | // base post entry does not exist, create it | |
420 | ||
4c193675 AD |
421 | $result = db_query($link, |
422 | "INSERT INTO ttrss_entries | |
423 | (title, | |
424 | guid, | |
425 | link, | |
426 | updated, | |
427 | content, | |
428 | content_hash, | |
429 | no_orig_date, | |
430 | date_entered, | |
11b0dce2 | 431 | comments, |
b6104dee AD |
432 | num_comments, |
433 | author) | |
4c193675 AD |
434 | VALUES |
435 | ('$entry_title', | |
436 | '$entry_guid', | |
437 | '$entry_link', | |
438 | '$entry_timestamp_fmt', | |
439 | '$entry_content', | |
440 | '$content_hash', | |
441 | $no_orig_date, | |
442 | NOW(), | |
11b0dce2 | 443 | '$entry_comments', |
b6104dee AD |
444 | '$num_comments', |
445 | '$entry_author')"); | |
8926aab8 AD |
446 | } else { |
447 | // we keep encountering the entry in feeds, so we need to | |
448 | // update date_entered column so that we don't get horrible | |
449 | // dupes when the entry gets purged and reinserted again e.g. | |
450 | // in the case of SLOW SLOW OMG SLOW updating feeds | |
451 | ||
452 | $base_entry_id = db_fetch_result($result, 0, "id"); | |
453 | ||
454 | db_query($link, "UPDATE ttrss_entries SET date_entered = NOW() | |
455 | WHERE id = '$base_entry_id'"); | |
4c193675 AD |
456 | } |
457 | ||
458 | // now it should exist, if not - bad luck then | |
459 | ||
6385315d AD |
460 | $result = db_query($link, "SELECT |
461 | id,content_hash,no_orig_date,title, | |
8926aab8 | 462 | substring(date_entered,1,19) as date_entered, |
11b0dce2 AD |
463 | substring(updated,1,19) as updated, |
464 | num_comments | |
6385315d AD |
465 | FROM |
466 | ttrss_entries | |
467 | WHERE guid = '$entry_guid'"); | |
4c193675 AD |
468 | |
469 | if (db_num_rows($result) == 1) { | |
470 | ||
11b0dce2 AD |
471 | // this will be used below in update handler |
472 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); | |
473 | $orig_title = db_fetch_result($result, 0, "title"); | |
474 | $orig_num_comments = db_fetch_result($result, 0, "num_comments"); | |
8926aab8 AD |
475 | $orig_date_entered = strtotime(db_fetch_result($result, |
476 | 0, "date_entered")); | |
6385315d | 477 | |
11b0dce2 | 478 | $ref_id = db_fetch_result($result, 0, "id"); |
4c193675 | 479 | |
11b0dce2 | 480 | // check for user post link to main table |
4c193675 | 481 | |
11b0dce2 AD |
482 | // do we allow duplicate posts with same GUID in different feeds? |
483 | if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) { | |
484 | $dupcheck_qpart = "AND feed_id = '$feed'"; | |
485 | } else { | |
486 | $dupcheck_qpart = ""; | |
487 | } | |
71604ca4 | 488 | |
11b0dce2 | 489 | // error_reporting(0); |
19c9cb11 | 490 | |
11b0dce2 AD |
491 | $filter_name = get_filter_name($entry_title, $entry_content, |
492 | $entry_link, $filters); | |
19c9cb11 | 493 | |
11b0dce2 AD |
494 | if ($filter_name == "filter") { |
495 | continue; | |
496 | } | |
19c9cb11 | 497 | |
11b0dce2 | 498 | // error_reporting (DEFAULT_ERROR_LEVEL); |
3a933f22 | 499 | |
11b0dce2 AD |
500 | $result = db_query($link, |
501 | "SELECT ref_id FROM ttrss_user_entries WHERE | |
502 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' | |
503 | $dupcheck_qpart"); | |
504 | ||
505 | // okay it doesn't exist - create user entry | |
506 | if (db_num_rows($result) == 0) { | |
507 | ||
508 | if ($filter_name != 'catchup') { | |
509 | $unread = 'true'; | |
510 | $last_read_qpart = 'NULL'; | |
511 | } else { | |
512 | $unread = 'false'; | |
513 | $last_read_qpart = 'NOW()'; | |
514 | } | |
19c9cb11 | 515 | |
11b0dce2 AD |
516 | $result = db_query($link, |
517 | "INSERT INTO ttrss_user_entries | |
518 | (ref_id, owner_uid, feed_id, unread, last_read) | |
519 | VALUES ('$ref_id', '$owner_uid', '$feed', $unread, | |
520 | $last_read_qpart)"); | |
521 | } | |
522 | ||
6385315d AD |
523 | $post_needs_update = false; |
524 | ||
a2770077 | 525 | if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) && |
6385315d AD |
526 | ($content_hash != $orig_content_hash)) { |
527 | $post_needs_update = true; | |
528 | } | |
529 | ||
530 | if ($orig_title != $entry_title) { | |
531 | $post_needs_update = true; | |
532 | } | |
533 | ||
11b0dce2 AD |
534 | if ($orig_num_comments != $num_comments) { |
535 | $post_needs_update = true; | |
536 | } | |
537 | ||
6385315d AD |
538 | // this doesn't seem to be very reliable |
539 | // | |
540 | // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) { | |
541 | // $post_needs_update = true; | |
542 | // } | |
543 | ||
544 | // if post needs update, update it and mark all user entries | |
1c73bc0c | 545 | // linking to this post as updated |
6385315d AD |
546 | if ($post_needs_update) { |
547 | ||
548 | // print "<!-- post $orig_title needs update : $post_needs_update -->"; | |
549 | ||
6385315d | 550 | db_query($link, "UPDATE ttrss_entries |
11b0dce2 AD |
551 | SET title = '$entry_title', content = '$entry_content', |
552 | num_comments = '$num_comments' | |
6385315d AD |
553 | WHERE id = '$ref_id'"); |
554 | ||
555 | db_query($link, "UPDATE ttrss_user_entries | |
556 | SET last_read = null WHERE ref_id = '$ref_id' AND unread = false"); | |
557 | ||
558 | } | |
4c193675 AD |
559 | } |
560 | ||
44e241cb AD |
561 | db_query($link, "COMMIT"); |
562 | ||
eb36b4eb AD |
563 | /* taaaags */ |
564 | // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, // | |
565 | ||
05732aa0 | 566 | $entry_tags = null; |
eb36b4eb | 567 | |
372ced8b | 568 | preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i", |
ee2c3050 AD |
569 | $entry_content_unescaped, $entry_tags); |
570 | ||
571 | // print "<br>$entry_title : $entry_content_unescaped<br>"; | |
572 | // print_r($entry_tags); | |
372ced8b | 573 | // print "<br>"; |
eb36b4eb AD |
574 | |
575 | $entry_tags = $entry_tags[1]; | |
576 | ||
577 | if (count($entry_tags) > 0) { | |
578 | ||
44e241cb AD |
579 | db_query($link, "BEGIN"); |
580 | ||
05732aa0 AD |
581 | $result = db_query($link, "SELECT id,int_id |
582 | FROM ttrss_entries,ttrss_user_entries | |
25da6909 | 583 | WHERE guid = '$entry_guid' |
05732aa0 | 584 | AND feed_id = '$feed' AND ref_id = id |
7fed1940 | 585 | AND owner_uid = '$owner_uid'"); |
eb36b4eb | 586 | |
fe99ab12 | 587 | if (db_num_rows($result) == 1) { |
eb36b4eb | 588 | |
fe99ab12 AD |
589 | $entry_id = db_fetch_result($result, 0, "id"); |
590 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
591 | ||
592 | foreach ($entry_tags as $tag) { | |
593 | $tag = db_escape_string(strtolower($tag)); | |
31483fc1 AD |
594 | |
595 | $tag = str_replace("+", " ", $tag); | |
fe99ab12 AD |
596 | $tag = str_replace("technorati tag: ", "", $tag); |
597 | ||
598 | $result = db_query($link, "SELECT id FROM ttrss_tags | |
599 | WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND | |
600 | owner_uid = '$owner_uid' LIMIT 1"); | |
601 | ||
602 | // print db_fetch_result($result, 0, "id"); | |
603 | ||
604 | if ($result && db_num_rows($result) == 0) { | |
605 | ||
606 | // print "tagging $entry_id as $tag<br>"; | |
607 | ||
608 | db_query($link, "INSERT INTO ttrss_tags | |
609 | (owner_uid,tag_name,post_int_id) | |
610 | VALUES ('$owner_uid','$tag', '$entry_int_id')"); | |
611 | } | |
612 | } | |
eb36b4eb | 613 | } |
44e241cb | 614 | db_query($link, "COMMIT"); |
05732aa0 | 615 | } |
4c193675 | 616 | } |
40d13c28 | 617 | |
ab3d0b99 AD |
618 | db_query($link, "UPDATE ttrss_feeds |
619 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
eb36b4eb | 620 | |
44e241cb | 621 | // db_query($link, "COMMIT"); |
dd8c76a9 | 622 | |
ab3d0b99 AD |
623 | } else { |
624 | $error_msg = db_escape_string(magpie_error()); | |
625 | db_query($link, | |
aa5f9f5f AD |
626 | "UPDATE ttrss_feeds SET last_error = '$error_msg', |
627 | last_updated = NOW() WHERE id = '$feed'"); | |
40d13c28 AD |
628 | } |
629 | ||
630 | } | |
631 | ||
f175937c AD |
632 | function print_select($id, $default, $values, $attributes = "") { |
633 | print "<select id=\"$id\" $attributes>"; | |
a0d53889 AD |
634 | foreach ($values as $v) { |
635 | if ($v == $default) | |
636 | $sel = " selected"; | |
637 | else | |
638 | $sel = ""; | |
639 | ||
640 | print "<option$sel>$v</option>"; | |
641 | } | |
642 | print "</select>"; | |
643 | } | |
40d13c28 | 644 | |
19c9cb11 | 645 | function get_filter_name($title, $content, $link, $filters) { |
e6155a06 AD |
646 | |
647 | if ($filters["title"]) { | |
19c9cb11 AD |
648 | foreach ($filters["title"] as $filter) { |
649 | $reg_exp = $filter["reg_exp"]; | |
650 | if (preg_match("/$reg_exp/i", $title)) { | |
651 | return $filter["action"]; | |
652 | } | |
e6155a06 AD |
653 | } |
654 | } | |
655 | ||
656 | if ($filters["content"]) { | |
19c9cb11 AD |
657 | foreach ($filters["content"] as $filter) { |
658 | $reg_exp = $filter["reg_exp"]; | |
659 | if (preg_match("/$reg_exp/i", $content)) { | |
660 | return $filter["action"]; | |
661 | } | |
e6155a06 AD |
662 | } |
663 | } | |
664 | ||
665 | if ($filters["both"]) { | |
666 | foreach ($filters["both"] as $filter) { | |
19c9cb11 AD |
667 | $reg_exp = $filter["reg_exp"]; |
668 | if (preg_match("/$reg_exp/i", $title) || | |
669 | preg_match("/$reg_exp/i", $content)) { | |
670 | return $filter["action"]; | |
671 | } | |
e6155a06 AD |
672 | } |
673 | } | |
674 | ||
3a933f22 | 675 | if ($filters["link"]) { |
19c9cb11 AD |
676 | $reg_exp = $filter["reg_exp"]; |
677 | foreach ($filters["link"] as $filter) { | |
678 | $reg_exp = $filter["reg_exp"]; | |
679 | if (preg_match("/$reg_exp/i", $link)) { | |
680 | return $filter["action"]; | |
681 | } | |
3a933f22 AD |
682 | } |
683 | } | |
684 | ||
e6155a06 AD |
685 | return false; |
686 | } | |
687 | ||
9323147e AD |
688 | function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link, |
689 | $rtl_content = false) { | |
254e0e4b AD |
690 | |
691 | if (file_exists($icon_file) && filesize($icon_file) > 0) { | |
023fe037 | 692 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">"; |
254e0e4b | 693 | } else { |
023fe037 | 694 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">"; |
254e0e4b AD |
695 | } |
696 | ||
9323147e AD |
697 | if ($rtl_content) { |
698 | $rtl_tag = "dir=\"rtl\""; | |
699 | } else { | |
700 | $rtl_tag = "dir=\"ltr\""; | |
701 | } | |
702 | ||
8143ae1f | 703 | $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>"; |
254e0e4b AD |
704 | |
705 | print "<li id=\"FEEDR-$feed_id\" class=\"$class\">"; | |
b619ff15 | 706 | if (get_pref($link, 'ENABLE_FEED_ICONS')) { |
254e0e4b AD |
707 | print "$feed_icon"; |
708 | } | |
709 | ||
9323147e | 710 | print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>"; |
254e0e4b AD |
711 | |
712 | if ($unread != 0) { | |
713 | $fctr_class = ""; | |
714 | } else { | |
715 | $fctr_class = "class=\"invisible\""; | |
716 | } | |
717 | ||
9323147e | 718 | print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\"> |
254e0e4b AD |
719 | (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>"; |
720 | ||
721 | print "</li>"; | |
722 | ||
723 | } | |
724 | ||
406d9489 AD |
725 | function getmicrotime() { |
726 | list($usec, $sec) = explode(" ",microtime()); | |
727 | return ((float)$usec + (float)$sec); | |
728 | } | |
729 | ||
77e96719 AD |
730 | function print_radio($id, $default, $values, $attributes = "") { |
731 | foreach ($values as $v) { | |
732 | ||
733 | if ($v == $default) | |
5da169d9 | 734 | $sel = "checked"; |
77e96719 | 735 | else |
5da169d9 AD |
736 | $sel = ""; |
737 | ||
738 | if ($v == "Yes") { | |
739 | $sel .= " value=\"1\""; | |
740 | } else { | |
741 | $sel .= " value=\"0\""; | |
742 | } | |
77e96719 | 743 | |
69654950 AD |
744 | print "<input class=\"noborder\" |
745 | type=\"radio\" $sel $attributes name=\"$id\"> $v "; | |
77e96719 AD |
746 | |
747 | } | |
748 | } | |
749 | ||
ff485f1d AD |
750 | function initialize_user_prefs($link, $uid) { |
751 | ||
752 | $uid = db_escape_string($uid); | |
753 | ||
754 | db_query($link, "BEGIN"); | |
755 | ||
756 | $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs"); | |
757 | ||
758 | $u_result = db_query($link, "SELECT pref_name | |
759 | FROM ttrss_user_prefs WHERE owner_uid = '$uid'"); | |
760 | ||
761 | $active_prefs = array(); | |
762 | ||
763 | while ($line = db_fetch_assoc($u_result)) { | |
764 | array_push($active_prefs, $line["pref_name"]); | |
765 | } | |
766 | ||
767 | while ($line = db_fetch_assoc($result)) { | |
768 | if (array_search($line["pref_name"], $active_prefs) === FALSE) { | |
769 | // print "adding " . $line["pref_name"] . "<br>"; | |
770 | ||
771 | db_query($link, "INSERT INTO ttrss_user_prefs | |
772 | (owner_uid,pref_name,value) VALUES | |
773 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')"); | |
774 | ||
775 | } | |
776 | } | |
777 | ||
778 | db_query($link, "COMMIT"); | |
779 | ||
780 | } | |
c8437f35 AD |
781 | |
782 | function authenticate_user($link, $login, $password) { | |
783 | ||
784 | $pwd_hash = 'SHA1:' . sha1($password); | |
785 | ||
203b6d25 | 786 | $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE |
7f16656e | 787 | login = '$login' AND pwd_hash = '$pwd_hash'"); |
c8437f35 AD |
788 | |
789 | if (db_num_rows($result) == 1) { | |
790 | $_SESSION["uid"] = db_fetch_result($result, 0, "id"); | |
791 | $_SESSION["name"] = db_fetch_result($result, 0, "login"); | |
203b6d25 | 792 | $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level"); |
c8437f35 | 793 | |
f6f32198 AD |
794 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . |
795 | $_SESSION["uid"]); | |
796 | ||
503eb349 AD |
797 | $user_theme = get_user_theme_path($link); |
798 | ||
799 | $_SESSION["theme"] = $user_theme; | |
916f788a | 800 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; |
503eb349 | 801 | |
f557cd78 AD |
802 | initialize_user_prefs($link, $_SESSION["uid"]); |
803 | ||
c8437f35 AD |
804 | return true; |
805 | } | |
ff485f1d | 806 | |
c8437f35 AD |
807 | return false; |
808 | ||
809 | } | |
810 | ||
e6cb77a0 AD |
811 | function make_password($length = 8) { |
812 | ||
813 | $password = ""; | |
798f722b AD |
814 | $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ"; |
815 | ||
816 | $i = 0; | |
e6cb77a0 AD |
817 | |
818 | while ($i < $length) { | |
819 | $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); | |
820 | ||
821 | if (!strstr($password, $char)) { | |
822 | $password .= $char; | |
823 | $i++; | |
824 | } | |
825 | } | |
826 | return $password; | |
827 | } | |
828 | ||
829 | // this is called after user is created to initialize default feeds, labels | |
830 | // or whatever else | |
831 | ||
832 | // user preferences are checked on every login, not here | |
833 | ||
834 | function initialize_user($link, $uid) { | |
835 | ||
836 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
837 | values ('$uid','unread = true', 'Unread articles')"); | |
838 | ||
839 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
840 | values ('$uid','last_read is null and unread = false', 'Updated articles')"); | |
841 | ||
842 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) | |
74bff337 | 843 | values ('$uid', 'Tiny Tiny RSS: New Releases', |
628fcd2c | 844 | 'http://tt-rss.spb.ru/releases.rss')"); |
3b0feb9b AD |
845 | |
846 | } | |
e6cb77a0 | 847 | |
b8aa49bc | 848 | function logout_user() { |
5ccc1cf5 AD |
849 | session_destroy(); |
850 | if (isset($_COOKIE[session_name()])) { | |
851 | setcookie(session_name(), '', time()-42000, '/'); | |
852 | } | |
b8aa49bc AD |
853 | } |
854 | ||
75836f33 | 855 | function get_script_urlpath() { |
87a79fa4 | 856 | return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]); |
75836f33 AD |
857 | } |
858 | ||
859 | function get_login_redirect() { | |
860 | $server = $_SERVER["SERVER_NAME"]; | |
861 | ||
862 | if (ENABLE_LOGIN_SSL) { | |
863 | $protocol = "https"; | |
864 | } else { | |
865 | $protocol = "http"; | |
866 | } | |
867 | ||
868 | $url_path = get_script_urlpath(); | |
869 | ||
870 | $redirect_uri = "$protocol://$server$url_path/login.php"; | |
871 | ||
872 | return $redirect_uri; | |
873 | } | |
874 | ||
916f788a | 875 | function validate_session($link) { |
a2e9b457 | 876 | if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) { |
916f788a AD |
877 | if ($_SESSION["ip_address"]) { |
878 | if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) { | |
879 | return false; | |
880 | } | |
881 | } | |
882 | } | |
883 | return true; | |
884 | } | |
885 | ||
7ae65adf AD |
886 | function basic_nosid_redirect_check() { |
887 | if (!SINGLE_USER_MODE) { | |
888 | if (!$_COOKIE["ttrss_sid"]) { | |
889 | $redirect_uri = get_login_redirect(); | |
890 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); | |
891 | header("Location: $redirect_uri?rt=$return_to"); | |
892 | exit; | |
893 | } | |
894 | } | |
895 | } | |
896 | ||
b8aa49bc AD |
897 | function login_sequence($link) { |
898 | if (!SINGLE_USER_MODE) { | |
75836f33 | 899 | |
916f788a AD |
900 | if (!validate_session($link)) { |
901 | logout_user(); | |
902 | $redirect_uri = get_login_redirect(); | |
903 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); | |
904 | header("Location: $redirect_uri?rt=$return_to"); | |
905 | exit; | |
906 | } | |
907 | ||
b8aa49bc AD |
908 | if (!USE_HTTP_AUTH) { |
909 | if (!$_SESSION["uid"]) { | |
75836f33 | 910 | $redirect_uri = get_login_redirect(); |
e31dca14 AD |
911 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); |
912 | header("Location: $redirect_uri?rt=$return_to"); | |
b8aa49bc AD |
913 | exit; |
914 | } | |
915 | } else { | |
f557cd78 AD |
916 | if (!$_SESSION["uid"]) { |
917 | if (!$_SERVER["PHP_AUTH_USER"]) { | |
918 | ||
919 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
920 | header('HTTP/1.0 401 Unauthorized'); | |
921 | exit; | |
922 | ||
923 | } else { | |
924 | $auth_result = authenticate_user($link, | |
925 | $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]); | |
926 | ||
927 | if (!$auth_result) { | |
928 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
929 | header('HTTP/1.0 401 Unauthorized'); | |
930 | exit; | |
931 | } | |
932 | } | |
933 | } | |
b8aa49bc AD |
934 | } |
935 | } else { | |
936 | $_SESSION["uid"] = 1; | |
937 | $_SESSION["name"] = "admin"; | |
c7a03b7a | 938 | initialize_user_prefs($link, 1); |
b8aa49bc AD |
939 | } |
940 | } | |
3547842a AD |
941 | |
942 | function truncate_string($str, $max_len) { | |
12db369c AD |
943 | if (mb_strlen($str, "utf-8") > $max_len - 3) { |
944 | return mb_substr($str, 0, $max_len, "utf-8") . "..."; | |
3547842a AD |
945 | } else { |
946 | return $str; | |
947 | } | |
948 | } | |
54a60e1a AD |
949 | |
950 | function get_user_theme_path($link) { | |
798f722b AD |
951 | $result = db_query($link, "SELECT theme_path |
952 | FROM | |
953 | ttrss_themes,ttrss_users | |
954 | WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]); | |
54a60e1a AD |
955 | if (db_num_rows($result) != 0) { |
956 | return db_fetch_result($result, 0, "theme_path"); | |
957 | } else { | |
958 | return null; | |
959 | } | |
960 | } | |
be773442 AD |
961 | |
962 | function smart_date_time($timestamp) { | |
963 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
964 | return date("G:i", $timestamp); | |
f26450f1 | 965 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
966 | return date("M d, G:i", $timestamp); |
967 | } else { | |
b02111c2 | 968 | return date("Y/m/d G:i", $timestamp); |
be773442 AD |
969 | } |
970 | } | |
971 | ||
972 | function smart_date($timestamp) { | |
973 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
974 | return "Today"; | |
f26450f1 | 975 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
976 | return date("D m", $timestamp); |
977 | } else { | |
b02111c2 | 978 | return date("Y/m/d", $timestamp); |
be773442 AD |
979 | } |
980 | } | |
a654a595 AD |
981 | |
982 | function sql_bool_to_string($s) { | |
983 | if ($s == "t" || $s == "1") { | |
984 | return "true"; | |
985 | } else { | |
986 | return "false"; | |
987 | } | |
988 | } | |
e3c99f3b AD |
989 | |
990 | function sql_bool_to_bool($s) { | |
991 | if ($s == "t" || $s == "1") { | |
992 | return true; | |
993 | } else { | |
994 | return false; | |
995 | } | |
996 | } | |
0ea4fb50 | 997 | |
e3c99f3b | 998 | |
0ea4fb50 AD |
999 | function toggleEvenOdd($a) { |
1000 | if ($a == "even") | |
1001 | return "odd"; | |
1002 | else | |
1003 | return "even"; | |
1004 | } | |
6043fb7e AD |
1005 | |
1006 | function sanity_check($link) { | |
9cbca41f | 1007 | |
6043fb7e AD |
1008 | $error_code = 0; |
1009 | $result = db_query($link, "SELECT schema_version FROM ttrss_version"); | |
1010 | $schema_version = db_fetch_result($result, 0, "schema_version"); | |
1011 | ||
1012 | if ($schema_version != SCHEMA_VERSION) { | |
1013 | $error_code = 5; | |
1014 | } | |
1015 | ||
6043fb7e | 1016 | if ($error_code != 0) { |
9cbca41f | 1017 | print "<error error-code='$error_code'/>"; |
6043fb7e AD |
1018 | return false; |
1019 | } else { | |
1020 | return true; | |
9cbca41f | 1021 | } |
6043fb7e AD |
1022 | } |
1023 | ||
27981ca3 AD |
1024 | function file_is_locked($filename) { |
1025 | error_reporting(0); | |
1026 | $fp = fopen($filename, "r"); | |
1027 | error_reporting(DEFAULT_ERROR_LEVEL); | |
1028 | if ($fp) { | |
1029 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1030 | flock($fp, LOCK_UN); | |
1031 | fclose($fp); | |
1032 | return false; | |
1033 | } | |
1034 | fclose($fp); | |
1035 | return true; | |
1036 | } | |
1037 | return false; | |
1038 | } | |
1039 | ||
fcb4c0c9 AD |
1040 | function make_lockfile($filename) { |
1041 | $fp = fopen($filename, "w"); | |
1042 | ||
1043 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1044 | return $fp; | |
1045 | } else { | |
1046 | return false; | |
1047 | } | |
1048 | } | |
1049 | ||
894ebcf5 AD |
1050 | function sql_random_function() { |
1051 | if (DB_TYPE == "mysql") { | |
1052 | return "RAND()"; | |
1053 | } else { | |
1054 | return "RANDOM()"; | |
1055 | } | |
1056 | } | |
1057 | ||
23aa0d16 AD |
1058 | function catchup_feed($link, $feed, $cat_view) { |
1059 | if (preg_match("/^[0-9][0-9]*$/", $feed) != false && $feed >= 0) { | |
1060 | ||
1061 | if ($cat_view) { | |
1062 | ||
1063 | if ($feed > 0) { | |
1064 | $cat_qpart = "cat_id = '$feed'"; | |
1065 | } else { | |
1066 | $cat_qpart = "cat_id IS NULL"; | |
1067 | } | |
1068 | ||
1069 | $tmp_result = db_query($link, "SELECT id | |
1070 | FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " . | |
1071 | $_SESSION["uid"]); | |
1072 | ||
1073 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1074 | ||
1075 | $tmp_feed = $tmp_line["id"]; | |
1076 | ||
1077 | db_query($link, "UPDATE ttrss_user_entries | |
1078 | SET unread = false,last_read = NOW() | |
1079 | WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]); | |
1080 | } | |
1081 | ||
1082 | } else if ($feed > 0) { | |
1083 | ||
1084 | $tmp_result = db_query($link, "SELECT id | |
1085 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
1086 | ORDER BY cat_id,title"); | |
1087 | ||
1088 | $parent_ids = array(); | |
1089 | ||
1090 | if (db_num_rows($tmp_result) > 0) { | |
1091 | while ($p = db_fetch_assoc($tmp_result)) { | |
1092 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
1093 | } | |
1094 | ||
1095 | $children_qpart = implode(" OR ", $parent_ids); | |
1096 | ||
1097 | db_query($link, "UPDATE ttrss_user_entries | |
1098 | SET unread = false,last_read = NOW() | |
1099 | WHERE (feed_id = '$feed' OR $children_qpart) | |
1100 | AND owner_uid = " . $_SESSION["uid"]); | |
1101 | ||
1102 | } else { | |
1103 | db_query($link, "UPDATE ttrss_user_entries | |
1104 | SET unread = false,last_read = NOW() | |
1105 | WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
1106 | } | |
1107 | ||
1108 | } else if ($feed < 0 && $feed > -10) { // special, like starred | |
1109 | ||
1110 | if ($feed == -1) { | |
1111 | db_query($link, "UPDATE ttrss_user_entries | |
1112 | SET unread = false,last_read = NOW() | |
1113 | WHERE marked = true AND owner_uid = ".$_SESSION["uid"]); | |
1114 | } | |
1115 | ||
1116 | } else if ($feed < -10) { // label | |
1117 | ||
1118 | // TODO make this more efficient | |
1119 | ||
1120 | $label_id = -$feed - 11; | |
1121 | ||
1122 | $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels | |
1123 | WHERE id = '$label_id'"); | |
1124 | ||
1125 | if ($tmp_result) { | |
1126 | $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp"); | |
1127 | ||
1128 | db_query($link, "BEGIN"); | |
1129 | ||
1130 | $tmp2_result = db_query($link, | |
1131 | "SELECT | |
1132 | int_id | |
1133 | FROM | |
1134 | ttrss_user_entries,ttrss_entries | |
1135 | WHERE | |
1136 | ref_id = id AND | |
1137 | $sql_exp AND | |
1138 | owner_uid = " . $_SESSION["uid"]); | |
1139 | ||
1140 | while ($tmp_line = db_fetch_assoc($tmp2_result)) { | |
1141 | db_query($link, "UPDATE | |
1142 | ttrss_user_entries | |
1143 | SET | |
1144 | unread = false, last_read = NOW() | |
1145 | WHERE | |
1146 | int_id = " . $tmp_line["int_id"]); | |
1147 | } | |
1148 | ||
1149 | db_query($link, "COMMIT"); | |
1150 | ||
1151 | /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries | |
1152 | SET unread = false,last_read = NOW() | |
1153 | WHERE $sql_exp | |
1154 | AND ref_id = id | |
1155 | AND owner_uid = ".$_SESSION["uid"]); */ | |
1156 | } | |
1157 | } | |
1158 | } else { // tag | |
1159 | db_query($link, "BEGIN"); | |
1160 | ||
1161 | $tag_name = db_escape_string($feed); | |
1162 | ||
1163 | $result = db_query($link, "SELECT post_int_id FROM ttrss_tags | |
1164 | WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]); | |
1165 | ||
1166 | while ($line = db_fetch_assoc($result)) { | |
1167 | db_query($link, "UPDATE ttrss_user_entries SET | |
1168 | unread = false, last_read = NOW() | |
1169 | WHERE int_id = " . $line["post_int_id"]); | |
1170 | } | |
1171 | db_query($link, "COMMIT"); | |
1172 | } | |
1173 | } | |
1174 | ||
1175 | function update_generic_feed($link, $feed, $cat_view) { | |
1176 | if ($cat_view) { | |
1177 | ||
1178 | if ($feed > 0) { | |
1179 | $cat_qpart = "cat_id = '$feed'"; | |
1180 | } else { | |
1181 | $cat_qpart = "cat_id IS NULL"; | |
1182 | } | |
1183 | ||
1184 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1185 | WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]); | |
1186 | ||
1187 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1188 | $feed_url = $tmp_line["feed_url"]; | |
1189 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1190 | } | |
1191 | ||
1192 | } else { | |
1193 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1194 | WHERE id = '$feed'"); | |
1195 | $feed_url = db_fetch_result($tmp_result, 0, "feed_url"); | |
1196 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1197 | } | |
1198 | } | |
a9cb1f83 AD |
1199 | |
1200 | function getAllCounters($link) { | |
1201 | getLabelCounters($link); | |
1202 | getFeedCounters($link); | |
1203 | getTagCounters($link); | |
1204 | getGlobalCounters($link); | |
1205 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
1206 | getCategoryCounters($link); | |
1207 | } | |
1208 | } | |
1209 | ||
1210 | function getCategoryCounters($link) { | |
1211 | $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id) | |
1212 | FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id | |
1213 | AND unread = true)) AS unread FROM ttrss_feeds | |
1214 | WHERE | |
1215 | owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id"); | |
1216 | ||
1217 | while ($line = db_fetch_assoc($result)) { | |
1218 | $line["cat_id"] = sprintf("%d", $line["cat_id"]); | |
1219 | print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"". | |
1220 | $line["unread"]."\"/>"; | |
1221 | } | |
1222 | } | |
1223 | ||
1224 | function getFeedUnread($link, $feed) { | |
1225 | $n_feed = sprintf("%d", $feed); | |
1226 | ||
1227 | if ($n_feed == -1) { | |
1228 | $match_part = "marked = true"; | |
1229 | } else if ($feed > 0) { | |
1230 | $match_part = "feed_id = '$n_feed'"; | |
1231 | } else if ($feed < -10) { | |
1232 | $label_id = -$feed - 11; | |
1233 | ||
1234 | $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE | |
1235 | id = '$label_id' AND owner_uid = " . $_SESSION["uid"]); | |
1236 | ||
1237 | $match_part = db_fetch_result($result, 0, "sql_exp"); | |
1238 | } | |
1239 | ||
1240 | if ($match_part) { | |
1241 | ||
1242 | $result = db_query($link, "SELECT count(int_id) AS unread | |
1243 | FROM ttrss_user_entries | |
1244 | WHERE unread = true AND $match_part AND owner_uid = " . $_SESSION["uid"]); | |
1245 | ||
1246 | } else { | |
1247 | ||
1248 | $result = db_query($link, "SELECT COUNT(post_int_id) AS unread | |
1249 | FROM ttrss_tags,ttrss_user_entries | |
1250 | WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND | |
1251 | ttrss_tags.owner_uid = " . $_SESSION["uid"]); | |
1252 | } | |
1253 | ||
1254 | $unread = db_fetch_result($result, 0, "unread"); | |
1255 | return $unread; | |
1256 | } | |
1257 | ||
1258 | /* FIXME this needs reworking */ | |
1259 | ||
1260 | function getGlobalUnread($link) { | |
1261 | $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries | |
1262 | WHERE unread = true AND | |
1263 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1264 | owner_uid = " . $_SESSION["uid"]); | |
1265 | $c_id = db_fetch_result($result, 0, "c_id"); | |
1266 | return $c_id; | |
1267 | } | |
1268 | ||
1269 | function getGlobalCounters($link, $global_unread = -1) { | |
1270 | if ($global_unread == -1) { | |
1271 | $global_unread = getGlobalUnread($link); | |
1272 | } | |
1273 | print "<counter type=\"global\" id='global-unread' counter='$global_unread'/>"; | |
1274 | } | |
1275 | ||
1276 | function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1277 | ||
1278 | if ($smart_mode) { | |
1279 | if (!$_SESSION["tctr_last_value"]) { | |
1280 | $_SESSION["tctr_last_value"] = array(); | |
1281 | } | |
1282 | } | |
1283 | ||
1284 | $old_counters = $_SESSION["tctr_last_value"]; | |
1285 | ||
1286 | $tctrs_modified = false; | |
1287 | ||
1288 | /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count | |
1289 | FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE | |
1290 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1291 | ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND | |
1292 | post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name | |
1293 | UNION | |
1294 | select tag_name,0 as count FROM ttrss_tags | |
1295 | WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */ | |
1296 | ||
1297 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) | |
1298 | FROM ttrss_user_entries WHERE int_id = post_int_id | |
1299 | AND unread = true)) AS count FROM ttrss_tags | |
1300 | WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name"); | |
1301 | ||
1302 | $tags = array(); | |
1303 | ||
1304 | while ($line = db_fetch_assoc($result)) { | |
1305 | $tags[$line["tag_name"]] += $line["count"]; | |
1306 | } | |
1307 | ||
1308 | foreach (array_keys($tags) as $tag) { | |
1309 | $unread = $tags[$tag]; | |
1310 | ||
1311 | $tag = htmlspecialchars($tag); | |
1312 | ||
1313 | if (!$smart_mode || $old_counters[$tag] != $unread) { | |
1314 | $old_counters[$tag] = $unread; | |
1315 | $tctrs_modified = true; | |
1316 | print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>"; | |
1317 | } | |
1318 | ||
1319 | } | |
1320 | ||
1321 | if ($smart_mode && $tctrs_modified) { | |
1322 | $_SESSION["tctr_last_value"] = $old_counters; | |
1323 | } | |
1324 | ||
1325 | } | |
1326 | ||
1327 | function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1328 | ||
1329 | if ($smart_mode) { | |
1330 | if (!$_SESSION["lctr_last_value"]) { | |
1331 | $_SESSION["lctr_last_value"] = array(); | |
1332 | } | |
1333 | } | |
1334 | ||
1335 | $old_counters = $_SESSION["lctr_last_value"]; | |
1336 | $lctrs_modified = false; | |
1337 | ||
1338 | $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries | |
1339 | WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1340 | unread = true AND owner_uid = ".$_SESSION["uid"]); | |
1341 | ||
1342 | $count = db_fetch_result($result, 0, "count"); | |
1343 | ||
1344 | print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>"; | |
1345 | ||
1346 | $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM | |
1347 | ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description"); | |
1348 | ||
1349 | while ($line = db_fetch_assoc($result)) { | |
1350 | ||
1351 | $id = -$line["id"] - 11; | |
1352 | ||
1353 | error_reporting (0); | |
1354 | ||
1355 | $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries | |
1356 | WHERE (" . $line["sql_exp"] . ") AND unread = true AND | |
1357 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1358 | owner_uid = ".$_SESSION["uid"]); | |
1359 | ||
1360 | $count = db_fetch_result($tmp_result, 0, "count"); | |
1361 | ||
1362 | if (!$smart_mode || $old_counters[$id] != $count) { | |
1363 | $old_counters[$id] = $count; | |
1364 | $lctrs_modified = true; | |
1365 | print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>"; | |
1366 | } | |
1367 | ||
1368 | error_reporting (DEFAULT_ERROR_LEVEL); | |
1369 | } | |
1370 | ||
1371 | if ($smart_mode && $lctrs_modified) { | |
1372 | $_SESSION["lctr_last_value"] = $old_counters; | |
1373 | } | |
1374 | } | |
1375 | ||
1376 | /* function getFeedCounter($link, $id) { | |
1377 | ||
1378 | $result = db_query($link, "SELECT | |
1379 | count(id) as count,last_error | |
1380 | FROM ttrss_entries,ttrss_user_entries,ttrss_feeds | |
1381 | WHERE feed_id = '$id' AND unread = true | |
1382 | AND ttrss_user_entries.feed_id = ttrss_feeds.id | |
1383 | AND ttrss_user_entries.ref_id = ttrss_entries.id"); | |
1384 | ||
1385 | $count = db_fetch_result($result, 0, "count"); | |
1386 | $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error")); | |
1387 | ||
1388 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>"; | |
1389 | } */ | |
1390 | ||
1391 | function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1392 | ||
1393 | if ($smart_mode) { | |
1394 | if (!$_SESSION["fctr_last_value"]) { | |
1395 | $_SESSION["fctr_last_value"] = array(); | |
1396 | } | |
1397 | } | |
1398 | ||
1399 | $old_counters = $_SESSION["fctr_last_value"]; | |
1400 | ||
1401 | $result = db_query($link, "SELECT id,last_error,parent_feed, | |
1402 | (SELECT count(id) | |
1403 | FROM ttrss_entries,ttrss_user_entries | |
1404 | WHERE feed_id = ttrss_feeds.id AND | |
1405 | ttrss_user_entries.ref_id = ttrss_entries.id | |
1406 | AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count | |
1407 | FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . " | |
1408 | AND parent_feed IS NULL"); | |
1409 | ||
1410 | $fctrs_modified = false; | |
1411 | ||
1412 | while ($line = db_fetch_assoc($result)) { | |
1413 | ||
1414 | $id = $line["id"]; | |
1415 | $count = $line["count"]; | |
1416 | $last_error = htmlspecialchars($line["last_error"]); | |
23aa0d16 | 1417 | |
a9cb1f83 AD |
1418 | $has_img = is_file(ICONS_DIR . "/$id.ico"); |
1419 | ||
1420 | $tmp_result = db_query($link, | |
1421 | "SELECT id,COUNT(unread) AS unread | |
1422 | FROM ttrss_feeds LEFT JOIN ttrss_user_entries | |
1423 | ON (ttrss_feeds.id = ttrss_user_entries.feed_id) | |
1424 | WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id"); | |
1425 | ||
1426 | if (db_num_rows($tmp_result) > 0) { | |
1427 | while ($l = db_fetch_assoc($tmp_result)) { | |
1428 | $count += $l["unread"]; | |
1429 | } | |
1430 | } | |
1431 | ||
1432 | if (!$smart_mode || $old_counters[$id] != $count) { | |
1433 | $old_counters[$id] = $count; | |
1434 | $fctrs_modified = true; | |
1435 | ||
1436 | if ($last_error) { | |
1437 | $error_part = "error=\"$last_error\""; | |
1438 | } else { | |
1439 | $error_part = ""; | |
1440 | } | |
1441 | ||
1442 | if ($has_img) { | |
1443 | $has_img_part = "hi=\"$has_img\""; | |
1444 | } else { | |
1445 | $has_img_part = ""; | |
1446 | } | |
1447 | ||
1448 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part/>"; | |
1449 | } | |
1450 | } | |
1451 | ||
1452 | if ($smart_mode && $fctrs_modified) { | |
1453 | $_SESSION["fctr_last_value"] = $old_counters; | |
1454 | } | |
1455 | } | |
1456 | ||
40d13c28 | 1457 | ?> |