]> git.wh0rd.org - tt-rss.git/blame - functions.php
make prefs tabs smaller
[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
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 688 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
fb1fb4ab 689 $rtl_content = false, $last_updated = false, $last_error = 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
fb1fb4ab
AD
703 if ($last_error) {
704 $link_title = "Error: $last_error ($last_updated)";
ad780e9c 705 } else if ($last_updated) {
fb1fb4ab
AD
706 $link_title = "Updated: $last_updated";
707 }
708
709 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
254e0e4b
AD
710
711 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
b619ff15 712 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
254e0e4b
AD
713 print "$feed_icon";
714 }
715
9323147e 716 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
254e0e4b
AD
717
718 if ($unread != 0) {
719 $fctr_class = "";
720 } else {
721 $fctr_class = "class=\"invisible\"";
722 }
723
9323147e 724 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
254e0e4b
AD
725 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
726
727 print "</li>";
728
729 }
730
406d9489
AD
731 function getmicrotime() {
732 list($usec, $sec) = explode(" ",microtime());
733 return ((float)$usec + (float)$sec);
734 }
735
77e96719
AD
736 function print_radio($id, $default, $values, $attributes = "") {
737 foreach ($values as $v) {
738
739 if ($v == $default)
5da169d9 740 $sel = "checked";
77e96719 741 else
5da169d9
AD
742 $sel = "";
743
744 if ($v == "Yes") {
745 $sel .= " value=\"1\"";
746 } else {
747 $sel .= " value=\"0\"";
748 }
77e96719 749
69654950
AD
750 print "<input class=\"noborder\"
751 type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
77e96719
AD
752
753 }
754 }
755
ff485f1d
AD
756 function initialize_user_prefs($link, $uid) {
757
758 $uid = db_escape_string($uid);
759
760 db_query($link, "BEGIN");
761
762 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
763
764 $u_result = db_query($link, "SELECT pref_name
765 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
766
767 $active_prefs = array();
768
769 while ($line = db_fetch_assoc($u_result)) {
770 array_push($active_prefs, $line["pref_name"]);
771 }
772
773 while ($line = db_fetch_assoc($result)) {
774 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
775// print "adding " . $line["pref_name"] . "<br>";
776
777 db_query($link, "INSERT INTO ttrss_user_prefs
778 (owner_uid,pref_name,value) VALUES
779 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
780
781 }
782 }
783
784 db_query($link, "COMMIT");
785
786 }
c8437f35
AD
787
788 function authenticate_user($link, $login, $password) {
789
790 $pwd_hash = 'SHA1:' . sha1($password);
791
203b6d25 792 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
7f16656e 793 login = '$login' AND pwd_hash = '$pwd_hash'");
c8437f35
AD
794
795 if (db_num_rows($result) == 1) {
796 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
797 $_SESSION["name"] = db_fetch_result($result, 0, "login");
203b6d25 798 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
c8437f35 799
f6f32198
AD
800 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
801 $_SESSION["uid"]);
802
503eb349
AD
803 $user_theme = get_user_theme_path($link);
804
805 $_SESSION["theme"] = $user_theme;
916f788a 806 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
503eb349 807
f557cd78
AD
808 initialize_user_prefs($link, $_SESSION["uid"]);
809
c8437f35
AD
810 return true;
811 }
ff485f1d 812
c8437f35
AD
813 return false;
814
815 }
816
e6cb77a0
AD
817 function make_password($length = 8) {
818
819 $password = "";
798f722b
AD
820 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
821
822 $i = 0;
e6cb77a0
AD
823
824 while ($i < $length) {
825 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
826
827 if (!strstr($password, $char)) {
828 $password .= $char;
829 $i++;
830 }
831 }
832 return $password;
833 }
834
835 // this is called after user is created to initialize default feeds, labels
836 // or whatever else
837
838 // user preferences are checked on every login, not here
839
840 function initialize_user($link, $uid) {
841
842 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
843 values ('$uid','unread = true', 'Unread articles')");
844
845 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
846 values ('$uid','last_read is null and unread = false', 'Updated articles')");
847
848 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
74bff337 849 values ('$uid', 'Tiny Tiny RSS: New Releases',
628fcd2c 850 'http://tt-rss.spb.ru/releases.rss')");
3b0feb9b
AD
851
852 }
e6cb77a0 853
b8aa49bc 854 function logout_user() {
5ccc1cf5
AD
855 session_destroy();
856 if (isset($_COOKIE[session_name()])) {
857 setcookie(session_name(), '', time()-42000, '/');
858 }
b8aa49bc
AD
859 }
860
75836f33 861 function get_script_urlpath() {
87a79fa4 862 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
75836f33
AD
863 }
864
865 function get_login_redirect() {
866 $server = $_SERVER["SERVER_NAME"];
867
868 if (ENABLE_LOGIN_SSL) {
869 $protocol = "https";
870 } else {
871 $protocol = "http";
872 }
873
874 $url_path = get_script_urlpath();
875
876 $redirect_uri = "$protocol://$server$url_path/login.php";
877
878 return $redirect_uri;
879 }
880
916f788a 881 function validate_session($link) {
a2e9b457 882 if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) {
916f788a
AD
883 if ($_SESSION["ip_address"]) {
884 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
885 return false;
886 }
887 }
888 }
889 return true;
890 }
891
7ae65adf
AD
892 function basic_nosid_redirect_check() {
893 if (!SINGLE_USER_MODE) {
894 if (!$_COOKIE["ttrss_sid"]) {
895 $redirect_uri = get_login_redirect();
896 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
897 header("Location: $redirect_uri?rt=$return_to");
898 exit;
899 }
900 }
901 }
902
b8aa49bc
AD
903 function login_sequence($link) {
904 if (!SINGLE_USER_MODE) {
75836f33 905
916f788a
AD
906 if (!validate_session($link)) {
907 logout_user();
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
b8aa49bc
AD
914 if (!USE_HTTP_AUTH) {
915 if (!$_SESSION["uid"]) {
75836f33 916 $redirect_uri = get_login_redirect();
e31dca14
AD
917 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
918 header("Location: $redirect_uri?rt=$return_to");
b8aa49bc
AD
919 exit;
920 }
921 } else {
f557cd78
AD
922 if (!$_SESSION["uid"]) {
923 if (!$_SERVER["PHP_AUTH_USER"]) {
924
925 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
926 header('HTTP/1.0 401 Unauthorized');
927 exit;
928
929 } else {
930 $auth_result = authenticate_user($link,
931 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
932
933 if (!$auth_result) {
934 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
935 header('HTTP/1.0 401 Unauthorized');
936 exit;
937 }
938 }
939 }
b8aa49bc
AD
940 }
941 } else {
942 $_SESSION["uid"] = 1;
943 $_SESSION["name"] = "admin";
c7a03b7a 944 initialize_user_prefs($link, 1);
b8aa49bc
AD
945 }
946 }
3547842a
AD
947
948 function truncate_string($str, $max_len) {
12db369c
AD
949 if (mb_strlen($str, "utf-8") > $max_len - 3) {
950 return mb_substr($str, 0, $max_len, "utf-8") . "...";
3547842a
AD
951 } else {
952 return $str;
953 }
954 }
54a60e1a
AD
955
956 function get_user_theme_path($link) {
798f722b
AD
957 $result = db_query($link, "SELECT theme_path
958 FROM
959 ttrss_themes,ttrss_users
960 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
54a60e1a
AD
961 if (db_num_rows($result) != 0) {
962 return db_fetch_result($result, 0, "theme_path");
963 } else {
964 return null;
965 }
966 }
be773442
AD
967
968 function smart_date_time($timestamp) {
969 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
970 return date("G:i", $timestamp);
f26450f1 971 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
972 return date("M d, G:i", $timestamp);
973 } else {
b02111c2 974 return date("Y/m/d G:i", $timestamp);
be773442
AD
975 }
976 }
977
978 function smart_date($timestamp) {
979 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
980 return "Today";
f26450f1 981 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
982 return date("D m", $timestamp);
983 } else {
b02111c2 984 return date("Y/m/d", $timestamp);
be773442
AD
985 }
986 }
a654a595
AD
987
988 function sql_bool_to_string($s) {
989 if ($s == "t" || $s == "1") {
990 return "true";
991 } else {
992 return "false";
993 }
994 }
e3c99f3b
AD
995
996 function sql_bool_to_bool($s) {
997 if ($s == "t" || $s == "1") {
998 return true;
999 } else {
1000 return false;
1001 }
1002 }
0ea4fb50 1003
e3c99f3b 1004
0ea4fb50
AD
1005 function toggleEvenOdd($a) {
1006 if ($a == "even")
1007 return "odd";
1008 else
1009 return "even";
1010 }
6043fb7e
AD
1011
1012 function sanity_check($link) {
9cbca41f 1013
6043fb7e
AD
1014 $error_code = 0;
1015 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1016 $schema_version = db_fetch_result($result, 0, "schema_version");
1017
1018 if ($schema_version != SCHEMA_VERSION) {
1019 $error_code = 5;
1020 }
1021
6043fb7e 1022 if ($error_code != 0) {
9cbca41f 1023 print "<error error-code='$error_code'/>";
6043fb7e
AD
1024 return false;
1025 } else {
1026 return true;
9cbca41f 1027 }
6043fb7e
AD
1028 }
1029
27981ca3
AD
1030 function file_is_locked($filename) {
1031 error_reporting(0);
1032 $fp = fopen($filename, "r");
1033 error_reporting(DEFAULT_ERROR_LEVEL);
1034 if ($fp) {
1035 if (flock($fp, LOCK_EX | LOCK_NB)) {
1036 flock($fp, LOCK_UN);
1037 fclose($fp);
1038 return false;
1039 }
1040 fclose($fp);
1041 return true;
1042 }
1043 return false;
1044 }
1045
fcb4c0c9
AD
1046 function make_lockfile($filename) {
1047 $fp = fopen($filename, "w");
1048
1049 if (flock($fp, LOCK_EX | LOCK_NB)) {
1050 return $fp;
1051 } else {
1052 return false;
1053 }
1054 }
1055
894ebcf5
AD
1056 function sql_random_function() {
1057 if (DB_TYPE == "mysql") {
1058 return "RAND()";
1059 } else {
1060 return "RANDOM()";
1061 }
1062 }
1063
23aa0d16
AD
1064 function catchup_feed($link, $feed, $cat_view) {
1065 if (preg_match("/^[0-9][0-9]*$/", $feed) != false && $feed >= 0) {
1066
1067 if ($cat_view) {
1068
1069 if ($feed > 0) {
1070 $cat_qpart = "cat_id = '$feed'";
1071 } else {
1072 $cat_qpart = "cat_id IS NULL";
1073 }
1074
1075 $tmp_result = db_query($link, "SELECT id
1076 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1077 $_SESSION["uid"]);
1078
1079 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1080
1081 $tmp_feed = $tmp_line["id"];
1082
1083 db_query($link, "UPDATE ttrss_user_entries
1084 SET unread = false,last_read = NOW()
1085 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1086 }
1087
1088 } else if ($feed > 0) {
1089
1090 $tmp_result = db_query($link, "SELECT id
1091 FROM ttrss_feeds WHERE parent_feed = '$feed'
1092 ORDER BY cat_id,title");
1093
1094 $parent_ids = array();
1095
1096 if (db_num_rows($tmp_result) > 0) {
1097 while ($p = db_fetch_assoc($tmp_result)) {
1098 array_push($parent_ids, "feed_id = " . $p["id"]);
1099 }
1100
1101 $children_qpart = implode(" OR ", $parent_ids);
1102
1103 db_query($link, "UPDATE ttrss_user_entries
1104 SET unread = false,last_read = NOW()
1105 WHERE (feed_id = '$feed' OR $children_qpart)
1106 AND owner_uid = " . $_SESSION["uid"]);
1107
1108 } else {
1109 db_query($link, "UPDATE ttrss_user_entries
1110 SET unread = false,last_read = NOW()
1111 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1112 }
1113
1114 } else if ($feed < 0 && $feed > -10) { // special, like starred
1115
1116 if ($feed == -1) {
1117 db_query($link, "UPDATE ttrss_user_entries
1118 SET unread = false,last_read = NOW()
1119 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1120 }
1121
1122 } else if ($feed < -10) { // label
1123
1124 // TODO make this more efficient
1125
1126 $label_id = -$feed - 11;
1127
1128 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1129 WHERE id = '$label_id'");
1130
1131 if ($tmp_result) {
1132 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1133
1134 db_query($link, "BEGIN");
1135
1136 $tmp2_result = db_query($link,
1137 "SELECT
1138 int_id
1139 FROM
1140 ttrss_user_entries,ttrss_entries
1141 WHERE
1142 ref_id = id AND
1143 $sql_exp AND
1144 owner_uid = " . $_SESSION["uid"]);
1145
1146 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1147 db_query($link, "UPDATE
1148 ttrss_user_entries
1149 SET
1150 unread = false, last_read = NOW()
1151 WHERE
1152 int_id = " . $tmp_line["int_id"]);
1153 }
1154
1155 db_query($link, "COMMIT");
1156
1157/* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1158 SET unread = false,last_read = NOW()
1159 WHERE $sql_exp
1160 AND ref_id = id
1161 AND owner_uid = ".$_SESSION["uid"]); */
1162 }
1163 }
1164 } else { // tag
1165 db_query($link, "BEGIN");
1166
1167 $tag_name = db_escape_string($feed);
1168
1169 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1170 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1171
1172 while ($line = db_fetch_assoc($result)) {
1173 db_query($link, "UPDATE ttrss_user_entries SET
1174 unread = false, last_read = NOW()
1175 WHERE int_id = " . $line["post_int_id"]);
1176 }
1177 db_query($link, "COMMIT");
1178 }
1179 }
1180
1181 function update_generic_feed($link, $feed, $cat_view) {
1182 if ($cat_view) {
1183
1184 if ($feed > 0) {
1185 $cat_qpart = "cat_id = '$feed'";
1186 } else {
1187 $cat_qpart = "cat_id IS NULL";
1188 }
1189
1190 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1191 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1192
1193 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1194 $feed_url = $tmp_line["feed_url"];
1195 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1196 }
1197
1198 } else {
1199 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1200 WHERE id = '$feed'");
1201 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1202 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1203 }
1204 }
a9cb1f83
AD
1205
1206 function getAllCounters($link) {
1207 getLabelCounters($link);
1208 getFeedCounters($link);
1209 getTagCounters($link);
1210 getGlobalCounters($link);
1211 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1212 getCategoryCounters($link);
1213 }
1214 }
1215
1216 function getCategoryCounters($link) {
1217 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1218 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1219 AND unread = true)) AS unread FROM ttrss_feeds
1220 WHERE
1221 owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1222
1223 while ($line = db_fetch_assoc($result)) {
1224 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1225 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1226 $line["unread"]."\"/>";
1227 }
1228 }
1229
1230 function getFeedUnread($link, $feed) {
1231 $n_feed = sprintf("%d", $feed);
1232
1233 if ($n_feed == -1) {
1234 $match_part = "marked = true";
1235 } else if ($feed > 0) {
1236 $match_part = "feed_id = '$n_feed'";
1237 } else if ($feed < -10) {
1238 $label_id = -$feed - 11;
1239
1240 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1241 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1242
1243 $match_part = db_fetch_result($result, 0, "sql_exp");
1244 }
1245
1246 if ($match_part) {
1247
1248 $result = db_query($link, "SELECT count(int_id) AS unread
1249 FROM ttrss_user_entries
1250 WHERE unread = true AND $match_part AND owner_uid = " . $_SESSION["uid"]);
1251
1252 } else {
1253
1254 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1255 FROM ttrss_tags,ttrss_user_entries
1256 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1257 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1258 }
1259
1260 $unread = db_fetch_result($result, 0, "unread");
1261 return $unread;
1262 }
1263
1264 /* FIXME this needs reworking */
1265
1266 function getGlobalUnread($link) {
1267 $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries
1268 WHERE unread = true AND
1269 ttrss_user_entries.ref_id = ttrss_entries.id AND
1270 owner_uid = " . $_SESSION["uid"]);
1271 $c_id = db_fetch_result($result, 0, "c_id");
1272 return $c_id;
1273 }
1274
1275 function getGlobalCounters($link, $global_unread = -1) {
1276 if ($global_unread == -1) {
1277 $global_unread = getGlobalUnread($link);
1278 }
1279 print "<counter type=\"global\" id='global-unread' counter='$global_unread'/>";
1280 }
1281
1282 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1283
1284 if ($smart_mode) {
1285 if (!$_SESSION["tctr_last_value"]) {
1286 $_SESSION["tctr_last_value"] = array();
1287 }
1288 }
1289
1290 $old_counters = $_SESSION["tctr_last_value"];
1291
1292 $tctrs_modified = false;
1293
1294/* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1295 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1296 ttrss_user_entries.ref_id = ttrss_entries.id AND
1297 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1298 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1299 UNION
1300 select tag_name,0 as count FROM ttrss_tags
1301 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1302
1303 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1304 FROM ttrss_user_entries WHERE int_id = post_int_id
1305 AND unread = true)) AS count FROM ttrss_tags
1306 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
1307
1308 $tags = array();
1309
1310 while ($line = db_fetch_assoc($result)) {
1311 $tags[$line["tag_name"]] += $line["count"];
1312 }
1313
1314 foreach (array_keys($tags) as $tag) {
1315 $unread = $tags[$tag];
1316
1317 $tag = htmlspecialchars($tag);
1318
1319 if (!$smart_mode || $old_counters[$tag] != $unread) {
1320 $old_counters[$tag] = $unread;
1321 $tctrs_modified = true;
1322 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1323 }
1324
1325 }
1326
1327 if ($smart_mode && $tctrs_modified) {
1328 $_SESSION["tctr_last_value"] = $old_counters;
1329 }
1330
1331 }
1332
1333 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1334
1335 if ($smart_mode) {
1336 if (!$_SESSION["lctr_last_value"]) {
1337 $_SESSION["lctr_last_value"] = array();
1338 }
1339 }
1340
1341 $old_counters = $_SESSION["lctr_last_value"];
1342 $lctrs_modified = false;
1343
1344 $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries
1345 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1346 unread = true AND owner_uid = ".$_SESSION["uid"]);
1347
1348 $count = db_fetch_result($result, 0, "count");
1349
1350 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1351
1352 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1353 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1354
1355 while ($line = db_fetch_assoc($result)) {
1356
1357 $id = -$line["id"] - 11;
1358
1359 error_reporting (0);
1360
1361 $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries
1362 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1363 ttrss_user_entries.ref_id = ttrss_entries.id AND
1364 owner_uid = ".$_SESSION["uid"]);
1365
1366 $count = db_fetch_result($tmp_result, 0, "count");
1367
1368 if (!$smart_mode || $old_counters[$id] != $count) {
1369 $old_counters[$id] = $count;
1370 $lctrs_modified = true;
1371 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1372 }
1373
1374 error_reporting (DEFAULT_ERROR_LEVEL);
1375 }
1376
1377 if ($smart_mode && $lctrs_modified) {
1378 $_SESSION["lctr_last_value"] = $old_counters;
1379 }
1380 }
1381
1382/* function getFeedCounter($link, $id) {
1383
1384 $result = db_query($link, "SELECT
1385 count(id) as count,last_error
1386 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1387 WHERE feed_id = '$id' AND unread = true
1388 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1389 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1390
1391 $count = db_fetch_result($result, 0, "count");
1392 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1393
1394 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1395 } */
1396
1397 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1398
1399 if ($smart_mode) {
1400 if (!$_SESSION["fctr_last_value"]) {
1401 $_SESSION["fctr_last_value"] = array();
1402 }
1403 }
1404
1405 $old_counters = $_SESSION["fctr_last_value"];
1406
1407 $result = db_query($link, "SELECT id,last_error,parent_feed,
fb1fb4ab 1408 SUBSTRING(last_updated,1,19) AS last_updated,
a9cb1f83
AD
1409 (SELECT count(id)
1410 FROM ttrss_entries,ttrss_user_entries
1411 WHERE feed_id = ttrss_feeds.id AND
1412 ttrss_user_entries.ref_id = ttrss_entries.id
1413 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1414 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1415 AND parent_feed IS NULL");
1416
1417 $fctrs_modified = false;
1418
fb1fb4ab
AD
1419 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1420
a9cb1f83
AD
1421 while ($line = db_fetch_assoc($result)) {
1422
1423 $id = $line["id"];
1424 $count = $line["count"];
1425 $last_error = htmlspecialchars($line["last_error"]);
fb1fb4ab
AD
1426
1427 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1428 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1429 } else {
1430 $last_updated = date($short_date, strtotime($line["last_updated"]));
1431 }
1432
a9cb1f83
AD
1433 $has_img = is_file(ICONS_DIR . "/$id.ico");
1434
1435 $tmp_result = db_query($link,
1436 "SELECT id,COUNT(unread) AS unread
1437 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1438 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1439 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1440
1441 if (db_num_rows($tmp_result) > 0) {
1442 while ($l = db_fetch_assoc($tmp_result)) {
1443 $count += $l["unread"];
1444 }
1445 }
1446
1447 if (!$smart_mode || $old_counters[$id] != $count) {
1448 $old_counters[$id] = $count;
1449 $fctrs_modified = true;
1450
1451 if ($last_error) {
1452 $error_part = "error=\"$last_error\"";
1453 } else {
1454 $error_part = "";
1455 }
1456
1457 if ($has_img) {
1458 $has_img_part = "hi=\"$has_img\"";
1459 } else {
1460 $has_img_part = "";
1461 }
1462
fb1fb4ab 1463 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
a9cb1f83
AD
1464 }
1465 }
1466
1467 if ($smart_mode && $fctrs_modified) {
1468 $_SESSION["fctr_last_value"] = $old_counters;
1469 }
1470 }
1471
40d13c28 1472?>