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