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