]> git.wh0rd.org - tt-rss.git/blame - functions.php
fix hideOrShowFeeds issue in tag mode
[tt-rss.git] / functions.php
CommitLineData
1d3a17c7 1<?php
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';
af106b0e 12 require_once 'errors.php';
8911ac8b 13 require_once 'version.php';
40d13c28 14
7c5a308d 15 if (RSS_BACKEND_TYPE == "magpie") {
ed891a51 16 require_once "magpierss/rss_fetch.inc";
7c5a308d
AD
17 require_once 'magpierss/rss_utils.inc';
18 } else if (RSS_BACKEND_TYPE == "simplepie") {
19 require_once 'simplepie/simplepie.inc';
20 }
387234f3 21
a3ee2a38
AD
22 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
23
ad507f85
AD
24 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
25
26 $rows = -1;
4c193675 27
fefa6ca3 28 if (DB_TYPE == "pgsql") {
44e241cb 29/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 30 marked = false AND feed_id = '$feed_id' AND
35d8cf43 31 (SELECT date_entered FROM ttrss_entries WHERE
44e241cb
AD
32 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
33
6e7f8d26
AD
34 $pg_version = get_pgsql_version($link);
35
36 if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
1e59ae35
AD
37
38 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
39 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
44 } else {
45
46 $result = db_query($link, "DELETE FROM ttrss_user_entries
47 USING ttrss_entries
48 WHERE ttrss_entries.id = ref_id AND
49 marked = false AND
50 feed_id = '$feed_id' AND
fc774155 51 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
1e59ae35 52 }
ad507f85
AD
53
54 $rows = pg_affected_rows($result);
55
fefa6ca3 56 } else {
1e59ae35 57
30f1746f 58/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 59 marked = false AND feed_id = '$feed_id' AND
35d8cf43 60 (SELECT date_entered FROM ttrss_entries WHERE
30f1746f
AD
61 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
62
63 $result = db_query($link, "DELETE FROM ttrss_user_entries
64 USING ttrss_user_entries, ttrss_entries
65 WHERE ttrss_entries.id = ref_id AND
66 marked = false AND
67 feed_id = '$feed_id' AND
68 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
69
ad507f85
AD
70 $rows = mysql_affected_rows($link);
71
72 }
73
74 if ($debug) {
75 print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n";
fefa6ca3
AD
76 }
77 }
78
44e241cb
AD
79 function global_purge_old_posts($link, $do_output = false, $limit = false) {
80
894ebcf5 81 $random_qpart = sql_random_function();
fefa6ca3 82
44e241cb
AD
83 if ($limit) {
84 $limit_qpart = "LIMIT $limit";
85 } else {
86 $limit_qpart = "";
87 }
88
fefa6ca3 89 $result = db_query($link,
44e241cb
AD
90 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
91 ORDER BY $random_qpart $limit_qpart");
fefa6ca3
AD
92
93 while ($line = db_fetch_assoc($result)) {
94
95 $feed_id = $line["id"];
96 $purge_interval = $line["purge_interval"];
97 $owner_uid = $line["owner_uid"];
98
99 if ($purge_interval == 0) {
100
101 $tmp_result = db_query($link,
102 "SELECT value FROM ttrss_user_prefs WHERE
103 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
104
105 if (db_num_rows($tmp_result) != 0) {
106 $purge_interval = db_fetch_result($tmp_result, 0, "value");
107 }
108 }
109
110 if ($do_output) {
ad507f85 111// print "Feed $feed_id: purge interval = $purge_interval\n";
fefa6ca3
AD
112 }
113
114 if ($purge_interval > 0) {
ad507f85 115 purge_feed($link, $feed_id, $purge_interval, $do_output);
fefa6ca3
AD
116 }
117 }
118
71604ca4
AD
119 // purge orphaned posts in main content table
120 db_query($link, "DELETE FROM ttrss_entries WHERE
121 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
122
fefa6ca3
AD
123 }
124
b6eefba5 125 function purge_old_posts($link) {
5d73494a 126
f1a80dae
AD
127 $user_id = $_SESSION["uid"];
128
129 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
130 WHERE owner_uid = '$user_id'");
5d73494a
AD
131
132 while ($line = db_fetch_assoc($result)) {
133
134 $feed_id = $line["id"];
135 $purge_interval = $line["purge_interval"];
136
b619ff15 137 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
5d73494a 138
140aae81 139 if ($purge_interval > 0) {
fefa6ca3 140 purge_feed($link, $feed_id, $purge_interval);
5d73494a
AD
141 }
142 }
71604ca4
AD
143
144 // purge orphaned posts in main content table
145 db_query($link, "DELETE FROM ttrss_entries WHERE
146 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
c3a8d71a
AD
147 }
148
1f2b01ed 149 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
40d13c28 150
4769ddaf 151 if (WEB_DEMO_MODE) return;
b0b4abcf 152
a2770077
AD
153 if (!$user_id) {
154 $user_id = $_SESSION["uid"];
155 purge_old_posts($link);
156 }
157
25af8dad 158// db_query($link, "BEGIN");
b82af8c3 159
cbd8650d
AD
160 if (MAX_UPDATE_TIME > 0) {
161 if (DB_TYPE == "mysql") {
162 $q_order = "RAND()";
163 } else {
164 $q_order = "RANDOM()";
165 }
166 } else {
167 $q_order = "last_updated DESC";
168 }
169
d148926e 170 $result = db_query($link, "SELECT feed_url,id,
798f722b 171 SUBSTRING(last_updated,1,19) AS last_updated,
5c563acd 172 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
cbd8650d
AD
173 ORDER BY $q_order");
174
175 $upd_start = time();
40d13c28 176
b6eefba5 177 while ($line = db_fetch_assoc($result)) {
d148926e
AD
178 $upd_intl = $line["update_interval"];
179
b619ff15 180 if (!$upd_intl || $upd_intl == 0) {
a2770077 181 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
b619ff15 182 }
d148926e 183
c1e202b7
AD
184 if ($upd_intl < 0) {
185 // Updates for this feed are disabled
186 continue;
187 }
188
93d40f50
AD
189 if ($fetch || (!$line["last_updated"] ||
190 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
c5142cca 191
cbd8650d
AD
192// print "<!-- feed: ".$line["feed_url"]." -->";
193
1f2b01ed 194 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
cbd8650d
AD
195
196 $upd_elapsed = time() - $upd_start;
197
198 if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) {
199 return;
200 }
d148926e 201 }
40d13c28
AD
202 }
203
25af8dad 204// db_query($link, "COMMIT");
b82af8c3 205
40d13c28
AD
206 }
207
9e997874 208 function check_feed_favicon($feed_url, $feed, $link) {
78800912
AD
209 $feed_url = str_replace("http://", "", $feed_url);
210 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
211
212 $icon_url = "http://$feed_url/favicon.ico";
273a2f6b 213 $icon_file = ICONS_DIR . "/$feed.ico";
78800912
AD
214
215 if (!file_exists($icon_file)) {
78800912 216
c798704b 217 if (USE_CURL_FOR_ICONS) {
99331724
AD
218 error_reporting(0);
219
c798704b
AD
220 $ch = curl_init($icon_url);
221 $fp = fopen($icon_file, "w");
78800912 222
c798704b
AD
223 if ($fp) {
224 curl_setopt($ch, CURLOPT_FILE, $fp);
225 curl_setopt($ch, CURLOPT_FILE, $fp);
226
227 curl_exec($ch);
228 curl_close($ch);
229 fclose($fp);
e695fdc8 230 }
717f5e64 231
99331724 232 error_reporting (DEFAULT_ERROR_LEVEL);
c798704b
AD
233
234 } else {
235
236 error_reporting(0);
237 $r = fopen($icon_url, "r");
cce28758 238 error_reporting (DEFAULT_ERROR_LEVEL);
c798704b
AD
239
240 if ($r) {
241 $tmpfname = tempnam(TMP_DIRECTORY, "ttrssicon");
242
243 $t = fopen($tmpfname, "w");
244
245 while (!feof($r)) {
246 $buf = fread($r, 16384);
247 fwrite($t, $buf);
248 }
249
250 fclose($r);
251 fclose($t);
252
253 error_reporting(0);
254 if (!rename($tmpfname, $icon_file)) {
255 unlink($tmpfname);
256 }
257
258 chmod($icon_file, 0644);
259
260 error_reporting (DEFAULT_ERROR_LEVEL);
261 }
78800912
AD
262
263 }
264 }
265 }
266
ddb68b81 267 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
40d13c28 268
4769ddaf 269 if (WEB_DEMO_MODE) return;
b0b4abcf 270
ddb68b81 271 if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) {
21cfcdf2
AD
272 return;
273 }
274
47c6c988 275 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
a88c1f36
AD
276 FROM ttrss_feeds WHERE id = '$feed'");
277
ff25e639
AD
278 $auth_login = db_unescape_string(db_fetch_result($result, 0, "auth_login"));
279 $auth_pass = db_unescape_string(db_fetch_result($result, 0, "auth_pass"));
47c6c988 280
a88c1f36
AD
281 $update_interval = db_fetch_result($result, 0, "update_interval");
282
283 if ($update_interval < 0) { return; }
284
ab3d0b99
AD
285 $feed = db_escape_string($feed);
286
47c6c988
AD
287 $fetch_url = $feed_url;
288
289 if ($auth_login && $auth_pass) {
290 $url_parts = array();
291 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
292
293 if ($url_parts[1] && $url_parts[2]) {
294 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
295 }
296
297 }
ab3d0b99 298
7c5a308d
AD
299 if (RSS_BACKEND_TYPE == "magpie") {
300 error_reporting(0);
301 $rss = fetch_rss($fetch_url);
302 error_reporting (DEFAULT_ERROR_LEVEL);
303 } else if (RSS_BACKEND_TYPE == "simplepie") {
304
305 if (!file_exists(SIMPLEPIE_CACHE_DIR)) {
306 mkdir(SIMPLEPIE_CACHE_DIR);
307 }
76798ff3 308
7c5a308d
AD
309 $rss = new SimplePie();
310 $rss->feed_url($fetch_url);
311 $rss->cache_location(SIMPLEPIE_CACHE_DIR);
312 $rss->init();
313 }
314
b6eefba5 315 $feed = db_escape_string($feed);
dcee8f61 316
7c5a308d 317 $rss_check = $rss;
b82af8c3 318
7c5a308d
AD
319 if (RSS_BACKEND_TYPE == "simplepie") {
320 $rss_check = $rss->data;
321 }
322
323 if ($rss_check) {
324
44e241cb 325// db_query($link, "BEGIN");
dd8c76a9 326
a88c1f36 327 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
f324892e 328 FROM ttrss_feeds WHERE id = '$feed'");
331900c6 329
b6eefba5
AD
330 $registered_title = db_fetch_result($result, 0, "title");
331 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
f324892e 332 $orig_site_url = db_fetch_result($result, 0, "site_url");
331900c6 333
7fed1940
AD
334 $owner_uid = db_fetch_result($result, 0, "owner_uid");
335
8d0ec6fd 336 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
a2770077
AD
337 check_feed_favicon($feed_url, $feed, $link);
338 }
339
746b249f 340 if (!$registered_title || $registered_title == "[Unknown]") {
7c5a308d
AD
341
342 if (RSS_BACKEND_TYPE == "magpie") {
343 $feed_title = db_escape_string($rss->channel["title"]);
344 } else {
345 $feed_title = $rss->get_feed_title();
346 }
347
f324892e
AD
348 db_query($link, "UPDATE ttrss_feeds SET
349 title = '$feed_title' WHERE id = '$feed'");
350 }
351
7c5a308d
AD
352 if (RSS_BACKEND_TYPE == "magpie") {
353 $site_url = $rss->channel["link"];
354 // weird, weird Magpie
355 if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
356 } else {
357 $site_url = $rss->get_feed_link();
358 }
147f7691
AD
359
360 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
f324892e
AD
361 db_query($link, "UPDATE ttrss_feeds SET
362 site_url = '$site_url' WHERE id = '$feed'");
331900c6 363 }
40d13c28 364
b7f4bda2
AD
365// print "I: " . $rss->channel["image"]["url"];
366
7c5a308d
AD
367 if (RSS_BACKEND_TYPE == "magpie") {
368 $icon_url = $rss->image["url"];
369 } else {
370 $icon_url = $rss->get_image_url(); # FIXME
371 }
b7f4bda2 372
147f7691 373 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
b6eefba5
AD
374 $icon_url = db_escape_string($icon_url);
375 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
b7f4bda2
AD
376 }
377
e6155a06
AD
378
379 $filters = array();
380
4b3dff6e 381 $result = db_query($link, "SELECT reg_exp,
db42b934 382 ttrss_filter_types.name AS name,
52db9978 383 ttrss_filter_actions.name AS action
db42b934 384 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
e8b79d16 385 enabled = true AND
db42b934
AD
386 owner_uid = $owner_uid AND
387 ttrss_filter_types.id = filter_type AND
388 ttrss_filter_actions.id = action_id AND
ead60402 389 (feed_id IS NULL OR feed_id = '$feed')");
e6155a06 390
b6eefba5 391 while ($line = db_fetch_assoc($result)) {
e6155a06 392 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
19c9cb11
AD
393
394 $filter["reg_exp"] = $line["reg_exp"];
395 $filter["action"] = $line["action"];
396
397 array_push($filters[$line["name"]], $filter);
e6155a06
AD
398 }
399
7c5a308d
AD
400 if (RSS_BACKEND_TYPE == "magpie") {
401 $iterator = $rss->items;
ddb68b81 402
7c5a308d
AD
403 if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
404 if (!$iterator || !is_array($iterator)) $iterator = $rss;
405
406 } else {
407 $iterator = $rss->get_items();
408 }
c22789da
AD
409
410 if (!is_array($iterator)) {
39541e74 411 /* db_query($link, "UPDATE ttrss_feeds
75bd0669 412 SET last_error = 'Parse error: can\'t find any articles.'
39541e74 413 WHERE id = '$feed'"); */
c22789da
AD
414 return; // WTF?
415 }
ddb68b81
AD
416
417 foreach ($iterator as $item) {
7c5a308d
AD
418
419 if (RSS_BACKEND_TYPE == "magpie") {
420
421 $entry_guid = $item["id"];
422
423 if (!$entry_guid) $entry_guid = $item["guid"];
424 if (!$entry_guid) $entry_guid = $item["link"];
40d13c28 425
7c5a308d 426 if (!$entry_guid) continue;
40d13c28 427
7c5a308d
AD
428 $entry_timestamp = "";
429
430 $rss_2_date = $item['pubdate'];
431 $rss_1_date = $item['dc']['date'];
432 $atom_date = $item['issued'];
433 if (!$atom_date) $atom_date = $item['updated'];
b82af8c3 434
7c5a308d
AD
435 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
436 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
437 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
438
439 if ($entry_timestamp == "") {
440 $entry_timestamp = time();
441 $no_orig_date = 'true';
442 } else {
443 $no_orig_date = 'false';
444 }
445
446 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
447
448 $entry_title = $item["title"];
449
450 // strange Magpie workaround
451 $entry_link = $item["link_"];
452 if (!$entry_link) $entry_link = $item["link"];
453
454 if (!$entry_title) continue;
455 if (!$entry_link) continue;
456
457 $entry_content = $item["content:escaped"];
458
459 if (!$entry_content) $entry_content = $item["content:encoded"];
460 if (!$entry_content) $entry_content = $item["content"];
79b5d2d2 461 if (!$entry_content) $entry_content = $item["atom_content"];
7c5a308d
AD
462 if (!$entry_content) $entry_content = $item["summary"];
463 if (!$entry_content) $entry_content = $item["description"];
464
465 // if (!$entry_content) continue;
466
467 // WTF
468 if (is_array($entry_content)) {
469 $entry_content = $entry_content["encoded"];
470 if (!$entry_content) $entry_content = $entry_content["escaped"];
471 }
472
473 // print_r($item);
474 // print_r(htmlspecialchars($entry_content));
475 // print "<br>";
476
477 $entry_content_unescaped = $entry_content;
478 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
479
480 $entry_comments = $item["comments"];
481
482 $entry_author = db_escape_string($item['dc']['creator']);
483
484 $entry_guid = db_escape_string($entry_guid);
485
486 $result = db_query($link, "SELECT id FROM ttrss_entries
487 WHERE guid = '$entry_guid'");
488
489 $entry_content = db_escape_string($entry_content);
490 $entry_title = db_escape_string($entry_title);
491 $entry_link = db_escape_string($entry_link);
492 $entry_comments = db_escape_string($entry_comments);
493
494 $num_comments = db_escape_string($item["slash"]["comments"]);
495
496 if (!$num_comments) $num_comments = 0;
71ad3959 497
7c5a308d 498 } else if (RSS_BACKEND_TYPE == "simplepie") {
ddb68b81 499
7c5a308d 500 $entry_guid = $item->get_id();
71ad3959 501
7c5a308d
AD
502 if (!$entry_guid) {
503 $entry_guid = $item->get_permalink();
504 }
505
506 if (!$entry_guid) continue;
507
508 $entry_timestamp = $item->get_date("U");
509
510 if ($entry_timestamp == "") {
511 $entry_timestamp = time();
512 $no_orig_date = 'true';
513 } else {
514 $no_orig_date = 'false';
515 }
516
517 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
518
519 $entry_title = $item->get_title();
520 $entry_link = $item->get_permalink();
521
522 if (!$entry_title) continue;
523 if (!$entry_link) continue;
71ad3959 524
7c5a308d
AD
525 $entry_content = $item->get_description();
526
527// print_r(htmlspecialchars($entry_content));
528// print "<br>";
529
530 $entry_content_unescaped = $entry_content;
531 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
532
533 $entry_comments = ""; # FIXME
534
535 $entry_author = $item->get_author(0);
1696229f 536
7c5a308d
AD
537 $entry_author = db_escape_string($entry_author->name);
538
539 $entry_guid = db_escape_string($entry_guid);
a2015351 540
7c5a308d
AD
541 $result = db_query($link, "SELECT id FROM ttrss_entries
542 WHERE guid = '$entry_guid'");
543
544 $entry_content = db_escape_string($entry_content);
545 $entry_title = db_escape_string($entry_title);
546 $entry_link = db_escape_string($entry_link);
547 $entry_comments = db_escape_string($entry_comments);
548
549 $num_comments = 0; # FIXME
550
551 if (!$num_comments) $num_comments = 0;
a2015351 552
8add756a
AD
553 }
554
d48d160c 555 # sanitize content
183ad07b
AD
556
557 $entry_content = sanitize_rss($entry_content);
558 $entry_title = sanitize_rss($entry_title);
559 $entry_link = sanitize_rss($entry_link);
560 $entry_comments = sanitize_rss($entry_comments);
d48d160c 561
44e241cb
AD
562 db_query($link, "BEGIN");
563
4c193675
AD
564 if (db_num_rows($result) == 0) {
565
566 // base post entry does not exist, create it
567
4c193675
AD
568 $result = db_query($link,
569 "INSERT INTO ttrss_entries
570 (title,
571 guid,
572 link,
573 updated,
574 content,
575 content_hash,
576 no_orig_date,
577 date_entered,
11b0dce2 578 comments,
b6104dee
AD
579 num_comments,
580 author)
4c193675
AD
581 VALUES
582 ('$entry_title',
583 '$entry_guid',
584 '$entry_link',
585 '$entry_timestamp_fmt',
586 '$entry_content',
587 '$content_hash',
588 $no_orig_date,
589 NOW(),
11b0dce2 590 '$entry_comments',
b6104dee
AD
591 '$num_comments',
592 '$entry_author')");
8926aab8
AD
593 } else {
594 // we keep encountering the entry in feeds, so we need to
595 // update date_entered column so that we don't get horrible
596 // dupes when the entry gets purged and reinserted again e.g.
597 // in the case of SLOW SLOW OMG SLOW updating feeds
598
599 $base_entry_id = db_fetch_result($result, 0, "id");
600
601 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
602 WHERE id = '$base_entry_id'");
4c193675
AD
603 }
604
605 // now it should exist, if not - bad luck then
606
6385315d
AD
607 $result = db_query($link, "SELECT
608 id,content_hash,no_orig_date,title,
8926aab8 609 substring(date_entered,1,19) as date_entered,
11b0dce2
AD
610 substring(updated,1,19) as updated,
611 num_comments
6385315d
AD
612 FROM
613 ttrss_entries
614 WHERE guid = '$entry_guid'");
4c193675
AD
615
616 if (db_num_rows($result) == 1) {
617
11b0dce2
AD
618 // this will be used below in update handler
619 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
620 $orig_title = db_fetch_result($result, 0, "title");
621 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
8926aab8
AD
622 $orig_date_entered = strtotime(db_fetch_result($result,
623 0, "date_entered"));
6385315d 624
11b0dce2 625 $ref_id = db_fetch_result($result, 0, "id");
4c193675 626
11b0dce2 627 // check for user post link to main table
4c193675 628
11b0dce2 629 // do we allow duplicate posts with same GUID in different feeds?
8d0ec6fd 630 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
11b0dce2
AD
631 $dupcheck_qpart = "AND feed_id = '$feed'";
632 } else {
633 $dupcheck_qpart = "";
634 }
71604ca4 635
11b0dce2 636// error_reporting(0);
19c9cb11 637
11b0dce2
AD
638 $filter_name = get_filter_name($entry_title, $entry_content,
639 $entry_link, $filters);
19c9cb11 640
11b0dce2
AD
641 if ($filter_name == "filter") {
642 continue;
643 }
19c9cb11 644
11b0dce2 645// error_reporting (DEFAULT_ERROR_LEVEL);
3a933f22 646
11b0dce2
AD
647 $result = db_query($link,
648 "SELECT ref_id FROM ttrss_user_entries WHERE
649 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
650 $dupcheck_qpart");
651
652 // okay it doesn't exist - create user entry
653 if (db_num_rows($result) == 0) {
654
655 if ($filter_name != 'catchup') {
656 $unread = 'true';
657 $last_read_qpart = 'NULL';
658 } else {
659 $unread = 'false';
660 $last_read_qpart = 'NOW()';
661 }
dd7d3187
AD
662
663 if ($filter_name == 'mark') {
664 $marked = 'true';
665 } else {
666 $marked = 'false';
667 }
19c9cb11 668
11b0dce2
AD
669 $result = db_query($link,
670 "INSERT INTO ttrss_user_entries
dd7d3187 671 (ref_id, owner_uid, feed_id, unread, last_read, marked)
11b0dce2 672 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
dd7d3187 673 $last_read_qpart, $marked)");
11b0dce2
AD
674 }
675
6385315d
AD
676 $post_needs_update = false;
677
8d0ec6fd 678 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
6385315d
AD
679 ($content_hash != $orig_content_hash)) {
680 $post_needs_update = true;
681 }
682
683 if ($orig_title != $entry_title) {
684 $post_needs_update = true;
685 }
686
11b0dce2
AD
687 if ($orig_num_comments != $num_comments) {
688 $post_needs_update = true;
689 }
690
6385315d
AD
691// this doesn't seem to be very reliable
692//
693// if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
694// $post_needs_update = true;
695// }
696
697 // if post needs update, update it and mark all user entries
1c73bc0c 698 // linking to this post as updated
6385315d
AD
699 if ($post_needs_update) {
700
701// print "<!-- post $orig_title needs update : $post_needs_update -->";
702
6385315d 703 db_query($link, "UPDATE ttrss_entries
11b0dce2
AD
704 SET title = '$entry_title', content = '$entry_content',
705 num_comments = '$num_comments'
6385315d
AD
706 WHERE id = '$ref_id'");
707
8d0ec6fd 708 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
4919fb42
AD
709 db_query($link, "UPDATE ttrss_user_entries
710 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
711 } else {
712 db_query($link, "UPDATE ttrss_user_entries
713 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
714 }
6385315d
AD
715
716 }
4c193675
AD
717 }
718
44e241cb
AD
719 db_query($link, "COMMIT");
720
eb36b4eb
AD
721 /* taaaags */
722 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
723
05732aa0 724 $entry_tags = null;
eb36b4eb 725
372ced8b 726 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
ee2c3050
AD
727 $entry_content_unescaped, $entry_tags);
728
729// print "<br>$entry_title : $entry_content_unescaped<br>";
730// print_r($entry_tags);
372ced8b 731// print "<br>";
eb36b4eb
AD
732
733 $entry_tags = $entry_tags[1];
734
735 if (count($entry_tags) > 0) {
736
44e241cb
AD
737 db_query($link, "BEGIN");
738
05732aa0
AD
739 $result = db_query($link, "SELECT id,int_id
740 FROM ttrss_entries,ttrss_user_entries
25da6909 741 WHERE guid = '$entry_guid'
05732aa0 742 AND feed_id = '$feed' AND ref_id = id
7fed1940 743 AND owner_uid = '$owner_uid'");
eb36b4eb 744
fe99ab12 745 if (db_num_rows($result) == 1) {
eb36b4eb 746
fe99ab12
AD
747 $entry_id = db_fetch_result($result, 0, "id");
748 $entry_int_id = db_fetch_result($result, 0, "int_id");
749
750 foreach ($entry_tags as $tag) {
751 $tag = db_escape_string(strtolower($tag));
31483fc1
AD
752
753 $tag = str_replace("+", " ", $tag);
fe99ab12
AD
754 $tag = str_replace("technorati tag: ", "", $tag);
755
756 $result = db_query($link, "SELECT id FROM ttrss_tags
757 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
758 owner_uid = '$owner_uid' LIMIT 1");
759
760 // print db_fetch_result($result, 0, "id");
761
762 if ($result && db_num_rows($result) == 0) {
763
764 // print "tagging $entry_id as $tag<br>";
765
766 db_query($link, "INSERT INTO ttrss_tags
767 (owner_uid,tag_name,post_int_id)
768 VALUES ('$owner_uid','$tag', '$entry_int_id')");
769 }
770 }
eb36b4eb 771 }
44e241cb 772 db_query($link, "COMMIT");
05732aa0 773 }
4c193675 774 }
40d13c28 775
ab3d0b99
AD
776 db_query($link, "UPDATE ttrss_feeds
777 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
eb36b4eb 778
44e241cb 779// db_query($link, "COMMIT");
dd8c76a9 780
ab3d0b99
AD
781 } else {
782 $error_msg = db_escape_string(magpie_error());
783 db_query($link,
aa5f9f5f
AD
784 "UPDATE ttrss_feeds SET last_error = '$error_msg',
785 last_updated = NOW() WHERE id = '$feed'");
40d13c28
AD
786 }
787
788 }
789
f175937c 790 function print_select($id, $default, $values, $attributes = "") {
79f3553b 791 print "<select name=\"$id\" id=\"$id\" $attributes>";
a0d53889
AD
792 foreach ($values as $v) {
793 if ($v == $default)
794 $sel = " selected";
795 else
796 $sel = "";
797
798 print "<option$sel>$v</option>";
799 }
800 print "</select>";
801 }
40d13c28 802
79f3553b
AD
803 function print_select_hash($id, $default, $values, $attributes = "") {
804 print "<select name=\"$id\" id='$id' $attributes>";
673d54ca
AD
805 foreach (array_keys($values) as $v) {
806 if ($v == $default)
807 $sel = "selected";
808 else
809 $sel = "";
810
811 print "<option $sel value=\"$v\">".$values[$v]."</option>";
812 }
813
814 print "</select>";
815 }
816
19c9cb11 817 function get_filter_name($title, $content, $link, $filters) {
e6155a06
AD
818
819 if ($filters["title"]) {
19c9cb11
AD
820 foreach ($filters["title"] as $filter) {
821 $reg_exp = $filter["reg_exp"];
822 if (preg_match("/$reg_exp/i", $title)) {
823 return $filter["action"];
824 }
e6155a06
AD
825 }
826 }
827
828 if ($filters["content"]) {
19c9cb11
AD
829 foreach ($filters["content"] as $filter) {
830 $reg_exp = $filter["reg_exp"];
831 if (preg_match("/$reg_exp/i", $content)) {
832 return $filter["action"];
833 }
e6155a06
AD
834 }
835 }
836
837 if ($filters["both"]) {
838 foreach ($filters["both"] as $filter) {
19c9cb11
AD
839 $reg_exp = $filter["reg_exp"];
840 if (preg_match("/$reg_exp/i", $title) ||
841 preg_match("/$reg_exp/i", $content)) {
842 return $filter["action"];
843 }
e6155a06
AD
844 }
845 }
846
3a933f22 847 if ($filters["link"]) {
19c9cb11
AD
848 $reg_exp = $filter["reg_exp"];
849 foreach ($filters["link"] as $filter) {
850 $reg_exp = $filter["reg_exp"];
851 if (preg_match("/$reg_exp/i", $link)) {
852 return $filter["action"];
853 }
3a933f22
AD
854 }
855 }
856
e6155a06
AD
857 return false;
858 }
859
9323147e 860 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
fb1fb4ab 861 $rtl_content = false, $last_updated = false, $last_error = false) {
254e0e4b
AD
862
863 if (file_exists($icon_file) && filesize($icon_file) > 0) {
023fe037 864 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
254e0e4b 865 } else {
023fe037 866 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
254e0e4b
AD
867 }
868
9323147e
AD
869 if ($rtl_content) {
870 $rtl_tag = "dir=\"rtl\"";
871 } else {
872 $rtl_tag = "dir=\"ltr\"";
873 }
874
78d5212c
AD
875 $error_notify_msg = "";
876
fb1fb4ab
AD
877 if ($last_error) {
878 $link_title = "Error: $last_error ($last_updated)";
78d5212c 879 $error_notify_msg = "(Error)";
ad780e9c 880 } else if ($last_updated) {
fb1fb4ab
AD
881 $link_title = "Updated: $last_updated";
882 }
883
767e2486 884 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', '', false);\">$feed_title</a>";
254e0e4b
AD
885
886 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
b619ff15 887 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
254e0e4b
AD
888 print "$feed_icon";
889 }
890
9323147e 891 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
254e0e4b
AD
892
893 if ($unread != 0) {
894 $fctr_class = "";
895 } else {
896 $fctr_class = "class=\"invisible\"";
897 }
898
9323147e 899 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
254e0e4b 900 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
78d5212c
AD
901
902 if (get_pref($link, "EXTENDED_FEEDLIST")) {
903 print "<div class=\"feedExtInfo\">
904 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
905 }
906
254e0e4b
AD
907 print "</li>";
908
909 }
910
406d9489
AD
911 function getmicrotime() {
912 list($usec, $sec) = explode(" ",microtime());
913 return ((float)$usec + (float)$sec);
914 }
915
77e96719
AD
916 function print_radio($id, $default, $values, $attributes = "") {
917 foreach ($values as $v) {
918
919 if ($v == $default)
5da169d9 920 $sel = "checked";
77e96719 921 else
5da169d9
AD
922 $sel = "";
923
924 if ($v == "Yes") {
925 $sel .= " value=\"1\"";
926 } else {
927 $sel .= " value=\"0\"";
928 }
77e96719 929
69654950
AD
930 print "<input class=\"noborder\"
931 type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
77e96719
AD
932
933 }
934 }
935
ff485f1d
AD
936 function initialize_user_prefs($link, $uid) {
937
938 $uid = db_escape_string($uid);
939
940 db_query($link, "BEGIN");
941
942 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
943
944 $u_result = db_query($link, "SELECT pref_name
945 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
946
947 $active_prefs = array();
948
949 while ($line = db_fetch_assoc($u_result)) {
950 array_push($active_prefs, $line["pref_name"]);
951 }
952
953 while ($line = db_fetch_assoc($result)) {
954 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
955// print "adding " . $line["pref_name"] . "<br>";
956
957 db_query($link, "INSERT INTO ttrss_user_prefs
958 (owner_uid,pref_name,value) VALUES
959 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
960
961 }
962 }
963
964 db_query($link, "COMMIT");
965
966 }
956c7629
AD
967
968 function lookup_user_id($link, $user) {
969
970 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
971 login = '$login'");
972
973 if (db_num_rows($result) == 1) {
974 return db_fetch_result($result, 0, "id");
975 } else {
976 return false;
977 }
978 }
979
18664970
AD
980 function http_authenticate_user($link) {
981
982 if (!$_SERVER["PHP_AUTH_USER"]) {
983
984 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
985 header('HTTP/1.0 401 Unauthorized');
986 exit;
987
988 } else {
989 $auth_result = authenticate_user($link,
990 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
991
992 if (!$auth_result) {
993 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
994 header('HTTP/1.0 401 Unauthorized');
995 exit;
996 }
997 }
998
999 return true;
1000 }
1001
c8437f35
AD
1002 function authenticate_user($link, $login, $password) {
1003
131b01b3 1004 if (!SINGLE_USER_MODE) {
c8437f35 1005
131b01b3
AD
1006 $pwd_hash = 'SHA1:' . sha1($password);
1007
1008 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
1009 login = '$login' AND pwd_hash = '$pwd_hash'");
1010
1011 if (db_num_rows($result) == 1) {
1012 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1013 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1014 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1015
1016 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1017 $_SESSION["uid"]);
1018
1019 $user_theme = get_user_theme_path($link);
1020
1021 $_SESSION["theme"] = $user_theme;
1022 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1023
1024 initialize_user_prefs($link, $_SESSION["uid"]);
1025
1026 return true;
1027 }
1028
1029 return false;
503eb349 1030
131b01b3 1031 } else {
503eb349 1032
131b01b3
AD
1033 $_SESSION["uid"] = 1;
1034 $_SESSION["name"] = "admin";
f557cd78 1035
0bbba72d
AD
1036 $user_theme = get_user_theme_path($link);
1037
1038 $_SESSION["theme"] = $user_theme;
1039 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1040
1041 initialize_user_prefs($link, $_SESSION["uid"]);
1042
c8437f35
AD
1043 return true;
1044 }
c8437f35
AD
1045 }
1046
e6cb77a0
AD
1047 function make_password($length = 8) {
1048
1049 $password = "";
798f722b
AD
1050 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1051
1052 $i = 0;
e6cb77a0
AD
1053
1054 while ($i < $length) {
1055 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1056
1057 if (!strstr($password, $char)) {
1058 $password .= $char;
1059 $i++;
1060 }
1061 }
1062 return $password;
1063 }
1064
1065 // this is called after user is created to initialize default feeds, labels
1066 // or whatever else
1067
1068 // user preferences are checked on every login, not here
1069
1070 function initialize_user($link, $uid) {
1071
1072 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1073 values ('$uid','unread = true', 'Unread articles')");
1074
1075 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1076 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1077
1078 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
74bff337 1079 values ('$uid', 'Tiny Tiny RSS: New Releases',
628fcd2c 1080 'http://tt-rss.spb.ru/releases.rss')");
3b0feb9b
AD
1081
1082 }
e6cb77a0 1083
b8aa49bc 1084 function logout_user() {
5ccc1cf5
AD
1085 session_destroy();
1086 if (isset($_COOKIE[session_name()])) {
1087 setcookie(session_name(), '', time()-42000, '/');
1088 }
b8aa49bc
AD
1089 }
1090
75836f33 1091 function get_script_urlpath() {
87a79fa4 1092 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
75836f33
AD
1093 }
1094
1095 function get_login_redirect() {
1096 $server = $_SERVER["SERVER_NAME"];
1097
1098 if (ENABLE_LOGIN_SSL) {
1099 $protocol = "https";
1100 } else {
1101 $protocol = "http";
1102 }
1103
1104 $url_path = get_script_urlpath();
1105
1106 $redirect_uri = "$protocol://$server$url_path/login.php";
1107
1108 return $redirect_uri;
1109 }
1110
916f788a 1111 function validate_session($link) {
a2e9b457 1112 if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) {
916f788a
AD
1113 if ($_SESSION["ip_address"]) {
1114 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1115 return false;
1116 }
1117 }
1118 }
1119 return true;
1120 }
1121
7ae65adf
AD
1122 function basic_nosid_redirect_check() {
1123 if (!SINGLE_USER_MODE) {
3dd46f19 1124 if (!$_COOKIE[get_session_cookie_name()]) {
7ae65adf
AD
1125 $redirect_uri = get_login_redirect();
1126 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1127 header("Location: $redirect_uri?rt=$return_to");
1128 exit;
1129 }
1130 }
1131 }
1132
b8aa49bc
AD
1133 function login_sequence($link) {
1134 if (!SINGLE_USER_MODE) {
75836f33 1135
916f788a
AD
1136 if (!validate_session($link)) {
1137 logout_user();
1138 $redirect_uri = get_login_redirect();
1139 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1140 header("Location: $redirect_uri?rt=$return_to");
1141 exit;
1142 }
1143
b8aa49bc
AD
1144 if (!USE_HTTP_AUTH) {
1145 if (!$_SESSION["uid"]) {
75836f33 1146 $redirect_uri = get_login_redirect();
e31dca14
AD
1147 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1148 header("Location: $redirect_uri?rt=$return_to");
b8aa49bc
AD
1149 exit;
1150 }
1151 } else {
f557cd78
AD
1152 if (!$_SESSION["uid"]) {
1153 if (!$_SERVER["PHP_AUTH_USER"]) {
1154
1155 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1156 header('HTTP/1.0 401 Unauthorized');
1157 exit;
1158
1159 } else {
1160 $auth_result = authenticate_user($link,
1161 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1162
1163 if (!$auth_result) {
1164 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1165 header('HTTP/1.0 401 Unauthorized');
1166 exit;
1167 }
1168 }
1169 }
b8aa49bc
AD
1170 }
1171 } else {
0bbba72d 1172 return authenticate_user($link, "admin", null);
b8aa49bc
AD
1173 }
1174 }
3547842a
AD
1175
1176 function truncate_string($str, $max_len) {
12db369c
AD
1177 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1178 return mb_substr($str, 0, $max_len, "utf-8") . "...";
3547842a
AD
1179 } else {
1180 return $str;
1181 }
1182 }
54a60e1a
AD
1183
1184 function get_user_theme_path($link) {
798f722b
AD
1185 $result = db_query($link, "SELECT theme_path
1186 FROM
1187 ttrss_themes,ttrss_users
1188 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
54a60e1a
AD
1189 if (db_num_rows($result) != 0) {
1190 return db_fetch_result($result, 0, "theme_path");
1191 } else {
1192 return null;
1193 }
1194 }
be773442
AD
1195
1196 function smart_date_time($timestamp) {
1197 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1198 return date("G:i", $timestamp);
f26450f1 1199 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
1200 return date("M d, G:i", $timestamp);
1201 } else {
b02111c2 1202 return date("Y/m/d G:i", $timestamp);
be773442
AD
1203 }
1204 }
1205
1206 function smart_date($timestamp) {
1207 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1208 return "Today";
f26450f1 1209 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
1210 return date("D m", $timestamp);
1211 } else {
b02111c2 1212 return date("Y/m/d", $timestamp);
be773442
AD
1213 }
1214 }
a654a595
AD
1215
1216 function sql_bool_to_string($s) {
1217 if ($s == "t" || $s == "1") {
1218 return "true";
1219 } else {
1220 return "false";
1221 }
1222 }
e3c99f3b
AD
1223
1224 function sql_bool_to_bool($s) {
1225 if ($s == "t" || $s == "1") {
1226 return true;
1227 } else {
1228 return false;
1229 }
1230 }
0ea4fb50 1231
e3c99f3b 1232
0ea4fb50
AD
1233 function toggleEvenOdd($a) {
1234 if ($a == "even")
1235 return "odd";
1236 else
1237 return "even";
1238 }
6043fb7e
AD
1239
1240 function sanity_check($link) {
9cbca41f 1241
aec3ce39
AD
1242 error_reporting(0);
1243
6043fb7e
AD
1244 $error_code = 0;
1245 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1246 $schema_version = db_fetch_result($result, 0, "schema_version");
1247
1248 if ($schema_version != SCHEMA_VERSION) {
1249 $error_code = 5;
1250 }
1251
aec3ce39
AD
1252 if (DB_TYPE == "mysql") {
1253 $result = db_query($link, "SELECT true", false);
1254 if (db_num_rows($result) != 1) {
1255 $error_code = 10;
1256 }
1257 }
1258
1259 error_reporting (DEFAULT_ERROR_LEVEL);
1260
6043fb7e 1261 if ($error_code != 0) {
aec3ce39 1262 print_error_xml($error_code);
6043fb7e
AD
1263 return false;
1264 } else {
1265 return true;
9cbca41f 1266 }
6043fb7e
AD
1267 }
1268
27981ca3
AD
1269 function file_is_locked($filename) {
1270 error_reporting(0);
1271 $fp = fopen($filename, "r");
1272 error_reporting(DEFAULT_ERROR_LEVEL);
1273 if ($fp) {
1274 if (flock($fp, LOCK_EX | LOCK_NB)) {
1275 flock($fp, LOCK_UN);
1276 fclose($fp);
1277 return false;
1278 }
1279 fclose($fp);
1280 return true;
1281 }
1282 return false;
1283 }
1284
fcb4c0c9
AD
1285 function make_lockfile($filename) {
1286 $fp = fopen($filename, "w");
1287
1288 if (flock($fp, LOCK_EX | LOCK_NB)) {
1289 return $fp;
1290 } else {
1291 return false;
1292 }
1293 }
1294
894ebcf5
AD
1295 function sql_random_function() {
1296 if (DB_TYPE == "mysql") {
1297 return "RAND()";
1298 } else {
1299 return "RANDOM()";
1300 }
1301 }
1302
23aa0d16 1303 function catchup_feed($link, $feed, $cat_view) {
88040f57
AD
1304
1305 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
23aa0d16
AD
1306
1307 if ($cat_view) {
1308
1309 if ($feed > 0) {
1310 $cat_qpart = "cat_id = '$feed'";
1311 } else {
1312 $cat_qpart = "cat_id IS NULL";
1313 }
1314
1315 $tmp_result = db_query($link, "SELECT id
1316 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1317 $_SESSION["uid"]);
1318
1319 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1320
1321 $tmp_feed = $tmp_line["id"];
1322
1323 db_query($link, "UPDATE ttrss_user_entries
1324 SET unread = false,last_read = NOW()
1325 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1326 }
1327
1328 } else if ($feed > 0) {
1329
1330 $tmp_result = db_query($link, "SELECT id
1331 FROM ttrss_feeds WHERE parent_feed = '$feed'
1332 ORDER BY cat_id,title");
1333
1334 $parent_ids = array();
1335
1336 if (db_num_rows($tmp_result) > 0) {
1337 while ($p = db_fetch_assoc($tmp_result)) {
1338 array_push($parent_ids, "feed_id = " . $p["id"]);
1339 }
1340
1341 $children_qpart = implode(" OR ", $parent_ids);
1342
1343 db_query($link, "UPDATE ttrss_user_entries
1344 SET unread = false,last_read = NOW()
1345 WHERE (feed_id = '$feed' OR $children_qpart)
1346 AND owner_uid = " . $_SESSION["uid"]);
1347
1348 } else {
1349 db_query($link, "UPDATE ttrss_user_entries
1350 SET unread = false,last_read = NOW()
1351 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1352 }
1353
1354 } else if ($feed < 0 && $feed > -10) { // special, like starred
1355
1356 if ($feed == -1) {
1357 db_query($link, "UPDATE ttrss_user_entries
1358 SET unread = false,last_read = NOW()
1359 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1360 }
1361
1362 } else if ($feed < -10) { // label
1363
1364 // TODO make this more efficient
1365
1366 $label_id = -$feed - 11;
1367
1368 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1369 WHERE id = '$label_id'");
1370
1371 if ($tmp_result) {
1372 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1373
1374 db_query($link, "BEGIN");
1375
1376 $tmp2_result = db_query($link,
1377 "SELECT
1378 int_id
1379 FROM
88040f57 1380 ttrss_user_entries,ttrss_entries,ttrss_feeds
23aa0d16 1381 WHERE
88040f57
AD
1382 ref_id = ttrss_entries.id AND
1383 ttrss_user_entries.feed_id = ttrss_feeds.id AND
23aa0d16 1384 $sql_exp AND
88040f57 1385 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
23aa0d16
AD
1386
1387 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1388 db_query($link, "UPDATE
1389 ttrss_user_entries
1390 SET
1391 unread = false, last_read = NOW()
1392 WHERE
1393 int_id = " . $tmp_line["int_id"]);
1394 }
1395
1396 db_query($link, "COMMIT");
1397
1398/* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1399 SET unread = false,last_read = NOW()
1400 WHERE $sql_exp
1401 AND ref_id = id
1402 AND owner_uid = ".$_SESSION["uid"]); */
1403 }
1404 }
1405 } else { // tag
1406 db_query($link, "BEGIN");
1407
1408 $tag_name = db_escape_string($feed);
1409
1410 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1411 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1412
1413 while ($line = db_fetch_assoc($result)) {
1414 db_query($link, "UPDATE ttrss_user_entries SET
1415 unread = false, last_read = NOW()
1416 WHERE int_id = " . $line["post_int_id"]);
1417 }
1418 db_query($link, "COMMIT");
1419 }
1420 }
1421
1422 function update_generic_feed($link, $feed, $cat_view) {
1423 if ($cat_view) {
1424
1425 if ($feed > 0) {
1426 $cat_qpart = "cat_id = '$feed'";
1427 } else {
1428 $cat_qpart = "cat_id IS NULL";
1429 }
1430
1431 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1432 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1433
1434 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1435 $feed_url = $tmp_line["feed_url"];
1436 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1437 }
1438
1439 } else {
1440 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1441 WHERE id = '$feed'");
1442 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1443 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1444 }
1445 }
a9cb1f83
AD
1446
1447 function getAllCounters($link) {
1448 getLabelCounters($link);
1449 getFeedCounters($link);
1450 getTagCounters($link);
1451 getGlobalCounters($link);
1452 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1453 getCategoryCounters($link);
1454 }
1455 }
1456
1457 function getCategoryCounters($link) {
1458 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1459 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1460 AND unread = true)) AS unread FROM ttrss_feeds
1461 WHERE
cfb02131 1462 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
a9cb1f83
AD
1463
1464 while ($line = db_fetch_assoc($result)) {
1465 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1466 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1467 $line["unread"]."\"/>";
1468 }
1469 }
1470
f295c368
AD
1471 function getCategoryUnread($link, $cat) {
1472
18664970
AD
1473 if ($cat != 0) {
1474 $cat_query = "cat_id = '$cat'";
1475 } else {
1476 $cat_query = "cat_id IS NULL";
1477 }
1478
1479 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
cfb02131 1480 AND hidden = false
f295c368
AD
1481 AND owner_uid = " . $_SESSION["uid"]);
1482
1483 $cat_feeds = array();
1484 while ($line = db_fetch_assoc($result)) {
1485 array_push($cat_feeds, "feed_id = " . $line["id"]);
1486 }
1487
18664970
AD
1488 if (count($cat_feeds) == 0) return 0;
1489
f295c368
AD
1490 $match_part = implode(" OR ", $cat_feeds);
1491
1492 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1493 FROM ttrss_user_entries
4919fb42 1494 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
f295c368
AD
1495
1496 $unread = 0;
1497
1498 # this needs to be rewritten
1499 while ($line = db_fetch_assoc($result)) {
1500 $unread += $line["unread"];
1501 }
1502
1503 return $unread;
1504
1505 }
1506
1507 function getFeedUnread($link, $feed, $is_cat = false) {
a9cb1f83 1508 $n_feed = sprintf("%d", $feed);
f295c368
AD
1509
1510 if ($is_cat) {
831ff047 1511 return getCategoryUnread($link, $n_feed);
f295c368 1512 } else if ($n_feed == -1) {
a9cb1f83 1513 $match_part = "marked = true";
4919fb42 1514 } else if ($n_feed > 0) {
831ff047 1515
4919fb42 1516 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE parent_feed = '$n_feed'
318260cc 1517 AND hidden = false
4919fb42 1518 AND owner_uid = " . $_SESSION["uid"]);
831ff047
AD
1519
1520 if (db_num_rows($result) > 0) {
4919fb42 1521
831ff047
AD
1522 $linked_feeds = array();
1523 while ($line = db_fetch_assoc($result)) {
1524 array_push($linked_feeds, "feed_id = " . $line["id"]);
1525 }
1526
1527 $match_part = implode(" OR ", $linked_feeds);
1528
4919fb42 1529 $result = db_query($link, "SELECT COUNT(int_id) AS unread
318260cc 1530 FROM ttrss_user_entries
4919fb42
AD
1531 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1532
1533 $unread = 0;
1534
1535 # this needs to be rewritten
1536 while ($line = db_fetch_assoc($result)) {
1537 $unread += $line["unread"];
1538 }
1539
1540 return $unread;
1541
831ff047
AD
1542 } else {
1543 $match_part = "feed_id = '$n_feed'";
1544 }
a9cb1f83 1545 } else if ($feed < -10) {
318260cc 1546
a9cb1f83
AD
1547 $label_id = -$feed - 11;
1548
1549 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1550 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1551
1552 $match_part = db_fetch_result($result, 0, "sql_exp");
1553 }
1554
1555 if ($match_part) {
1556
1557 $result = db_query($link, "SELECT count(int_id) AS unread
88040f57
AD
1558 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
1559 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1560 ttrss_user_entries.ref_id = ttrss_entries.id AND
cfb02131 1561 ttrss_feeds.hidden = false AND
88040f57 1562 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
a9cb1f83
AD
1563
1564 } else {
1565
1566 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1567 FROM ttrss_tags,ttrss_user_entries
1568 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1569 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1570 }
1571
1572 $unread = db_fetch_result($result, 0, "unread");
cfb02131 1573
a9cb1f83
AD
1574 return $unread;
1575 }
1576
1577 /* FIXME this needs reworking */
1578
f3acc32e
AD
1579 function getGlobalUnread($link, $user_id = false) {
1580
1581 if (!$user_id) {
1582 $user_id = $_SESSION["uid"];
1583 }
1584
3831db41 1585 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
a9cb1f83 1586 WHERE unread = true AND
3831db41 1587 ttrss_user_entries.feed_id = ttrss_feeds.id AND
a9cb1f83 1588 ttrss_user_entries.ref_id = ttrss_entries.id AND
3831db41 1589 hidden = false AND
f3acc32e 1590 ttrss_user_entries.owner_uid = '$user_id'");
a9cb1f83
AD
1591 $c_id = db_fetch_result($result, 0, "c_id");
1592 return $c_id;
1593 }
1594
1595 function getGlobalCounters($link, $global_unread = -1) {
1596 if ($global_unread == -1) {
1597 $global_unread = getGlobalUnread($link);
1598 }
7bf7e4d3
AD
1599 print "<counter type=\"global\" id='global-unread'
1600 counter='$global_unread'/>";
1601
1602 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
1603 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
1604
1605 $subscribed_feeds = db_fetch_result($result, 0, "fn");
1606
1607 print "<counter type=\"global\" id='subscribed-feeds'
1608 counter='$subscribed_feeds'/>";
1609
a9cb1f83
AD
1610 }
1611
1612 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1613
1614 if ($smart_mode) {
1615 if (!$_SESSION["tctr_last_value"]) {
1616 $_SESSION["tctr_last_value"] = array();
1617 }
1618 }
1619
1620 $old_counters = $_SESSION["tctr_last_value"];
1621
1622 $tctrs_modified = false;
1623
1624/* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1625 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1626 ttrss_user_entries.ref_id = ttrss_entries.id AND
1627 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1628 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1629 UNION
1630 select tag_name,0 as count FROM ttrss_tags
1631 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1632
1633 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1634 FROM ttrss_user_entries WHERE int_id = post_int_id
1635 AND unread = true)) AS count FROM ttrss_tags
1636 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
1637
1638 $tags = array();
1639
1640 while ($line = db_fetch_assoc($result)) {
1641 $tags[$line["tag_name"]] += $line["count"];
1642 }
1643
1644 foreach (array_keys($tags) as $tag) {
1645 $unread = $tags[$tag];
1646
1647 $tag = htmlspecialchars($tag);
1648
1649 if (!$smart_mode || $old_counters[$tag] != $unread) {
1650 $old_counters[$tag] = $unread;
1651 $tctrs_modified = true;
1652 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1653 }
1654
1655 }
1656
1657 if ($smart_mode && $tctrs_modified) {
1658 $_SESSION["tctr_last_value"] = $old_counters;
1659 }
1660
1661 }
1662
ef393de7 1663 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) {
a9cb1f83
AD
1664
1665 if ($smart_mode) {
1666 if (!$_SESSION["lctr_last_value"]) {
1667 $_SESSION["lctr_last_value"] = array();
1668 }
1669 }
1670
ef393de7
AD
1671 $ret_arr = array();
1672
a9cb1f83
AD
1673 $old_counters = $_SESSION["lctr_last_value"];
1674 $lctrs_modified = false;
1675
88040f57 1676 $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
a9cb1f83 1677 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
88040f57
AD
1678 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1679 unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
a9cb1f83
AD
1680
1681 $count = db_fetch_result($result, 0, "count");
1682
ef393de7
AD
1683 if (!$ret_mode) {
1684 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1685 } else {
1686 $ret_arr["-1"]["counter"] = $count;
1687 $ret_arr["-1"]["description"] = "Starred";
1688 }
a9cb1f83
AD
1689
1690 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1691 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1692
1693 while ($line = db_fetch_assoc($result)) {
1694
1695 $id = -$line["id"] - 11;
1696
ef393de7
AD
1697 $label_name = $line["description"];
1698
a9cb1f83
AD
1699 error_reporting (0);
1700
88040f57 1701 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
a9cb1f83 1702 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
cfb02131 1703 ttrss_feeds.hidden = false AND
88040f57 1704 ttrss_user_entries.feed_id = ttrss_feeds.id AND
a9cb1f83 1705 ttrss_user_entries.ref_id = ttrss_entries.id AND
88040f57 1706 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
a9cb1f83
AD
1707
1708 $count = db_fetch_result($tmp_result, 0, "count");
1709
1710 if (!$smart_mode || $old_counters[$id] != $count) {
1711 $old_counters[$id] = $count;
1712 $lctrs_modified = true;
ef393de7
AD
1713 if (!$ret_mode) {
1714 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1715 } else {
1716 $ret_arr[$id]["counter"] = $count;
1717 $ret_arr[$id]["description"] = $label_name;
1718 }
a9cb1f83
AD
1719 }
1720
1721 error_reporting (DEFAULT_ERROR_LEVEL);
1722 }
1723
1724 if ($smart_mode && $lctrs_modified) {
1725 $_SESSION["lctr_last_value"] = $old_counters;
1726 }
ef393de7
AD
1727
1728 return $ret_arr;
a9cb1f83
AD
1729 }
1730
1731/* function getFeedCounter($link, $id) {
1732
1733 $result = db_query($link, "SELECT
1734 count(id) as count,last_error
1735 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1736 WHERE feed_id = '$id' AND unread = true
1737 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1738 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1739
1740 $count = db_fetch_result($result, 0, "count");
1741 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1742
1743 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1744 } */
1745
1746 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1747
1748 if ($smart_mode) {
1749 if (!$_SESSION["fctr_last_value"]) {
1750 $_SESSION["fctr_last_value"] = array();
1751 }
1752 }
1753
1754 $old_counters = $_SESSION["fctr_last_value"];
1755
1756 $result = db_query($link, "SELECT id,last_error,parent_feed,
fb1fb4ab 1757 SUBSTRING(last_updated,1,19) AS last_updated,
a9cb1f83
AD
1758 (SELECT count(id)
1759 FROM ttrss_entries,ttrss_user_entries
1760 WHERE feed_id = ttrss_feeds.id AND
1761 ttrss_user_entries.ref_id = ttrss_entries.id
1762 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1763 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1764 AND parent_feed IS NULL");
1765
1766 $fctrs_modified = false;
1767
fb1fb4ab
AD
1768 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1769
a9cb1f83
AD
1770 while ($line = db_fetch_assoc($result)) {
1771
1772 $id = $line["id"];
1773 $count = $line["count"];
1774 $last_error = htmlspecialchars($line["last_error"]);
fb1fb4ab
AD
1775
1776 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1777 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1778 } else {
1779 $last_updated = date($short_date, strtotime($line["last_updated"]));
1780 }
1781
a9cb1f83
AD
1782 $has_img = is_file(ICONS_DIR . "/$id.ico");
1783
1784 $tmp_result = db_query($link,
1785 "SELECT id,COUNT(unread) AS unread
1786 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1787 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1788 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1789
1790 if (db_num_rows($tmp_result) > 0) {
1791 while ($l = db_fetch_assoc($tmp_result)) {
1792 $count += $l["unread"];
1793 }
1794 }
1795
1796 if (!$smart_mode || $old_counters[$id] != $count) {
1797 $old_counters[$id] = $count;
1798 $fctrs_modified = true;
1799
1800 if ($last_error) {
1801 $error_part = "error=\"$last_error\"";
1802 } else {
1803 $error_part = "";
1804 }
1805
1806 if ($has_img) {
1807 $has_img_part = "hi=\"$has_img\"";
1808 } else {
1809 $has_img_part = "";
1810 }
1811
fb1fb4ab 1812 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
a9cb1f83
AD
1813 }
1814 }
1815
1816 if ($smart_mode && $fctrs_modified) {
1817 $_SESSION["fctr_last_value"] = $old_counters;
1818 }
1819 }
1820
1b758780
AD
1821 function get_script_dt_add() {
1822 if (strpos(VERSION, "99") === false) {
1823 return VERSION;
1824 } else {
1825 return time();
1826 }
1827 }
1828
6e7f8d26
AD
1829 function get_pgsql_version($link) {
1830 $result = db_query($link, "SELECT version() AS version");
1831 $version = split(" ", db_fetch_result($result, 0, "version"));
1832 return $version[1];
1833 }
1834
af106b0e
AD
1835 function print_error_xml($code, $add_msg = "") {
1836 global $ERRORS;
1837
1838 $error_msg = $ERRORS[$code];
1839
1840 if ($add_msg) {
1841 $error_msg = "$error_msg; $add_msg";
1842 }
1843
4c2abbc1 1844 print "<rpc-reply>";
af106b0e 1845 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
4c2abbc1 1846 print "</rpc-reply>";
af106b0e 1847 }
956c7629
AD
1848
1849 function subscribe_to_feed($link, $feed_link, $cat_id = 0) {
1850
1851 if ($cat_id == "0" || !$cat_id) {
1852 $cat_qpart = "NULL";
1853 } else {
1854 $cat_qpart = "'$cat_id'";
1855 }
1856
1857 $result = db_query($link,
1858 "SELECT id FROM ttrss_feeds
1859 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
1860
1861 if (db_num_rows($result) == 0) {
1862
1863 $result = db_query($link,
1864 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
1865 VALUES ('".$_SESSION["uid"]."', '$feed_link',
1866 '[Unknown]', $cat_qpart)");
1867
1868 $result = db_query($link,
1869 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
1870 AND owner_uid = " . $_SESSION["uid"]);
1871
1872 $feed_id = db_fetch_result($result, 0, "id");
1873
1874 if ($feed_id) {
1875 update_rss_feed($link, $feed_link, $feed_id, true);
1876 }
1877
1878 return true;
1879 } else {
1880 return false;
1881 }
1882 }
1883
673d54ca
AD
1884 function print_feed_select($link, $id, $default_id = "",
1885 $attributes = "", $include_all_feeds = true) {
1886
79f3553b 1887 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca 1888 if ($include_all_feeds) {
79f3553b 1889 print "<option value=\"0\">All feeds</option>";
673d54ca
AD
1890 }
1891
1892 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
1893 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1894
1895 if (db_num_rows($result) > 0 && $include_all_feeds) {
1896 print "<option disabled>--------</option>";
1897 }
1898
1899 while ($line = db_fetch_assoc($result)) {
1900 if ($line["id"] == $default_id) {
1901 $is_selected = "selected";
1902 } else {
1903 $is_selected = "";
1904 }
79f3553b 1905 printf("<option $is_selected value='%d'>%s</option>",
07164479 1906 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
673d54ca
AD
1907 }
1908
1909 print "</select>";
1910 }
1911
1912 function print_feed_cat_select($link, $id, $default_id = "",
1913 $attributes = "", $include_all_cats = true) {
1914
79f3553b 1915 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca
AD
1916
1917 if ($include_all_cats) {
14f69488 1918 print "<option value=\"0\">Uncategorized</option>";
673d54ca
AD
1919 }
1920
1921 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
1922 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1923
1924 if (db_num_rows($result) > 0 && $include_all_cats) {
1925 print "<option disabled>--------</option>";
1926 }
1927
1928 while ($line = db_fetch_assoc($result)) {
1929 if ($line["id"] == $default_id) {
1930 $is_selected = "selected";
1931 } else {
1932 $is_selected = "";
1933 }
14f69488 1934 printf("<option $is_selected value='%d'>%s</option>",
07164479 1935 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
673d54ca
AD
1936 }
1937
1938 print "</select>";
1939 }
1940
14f69488
AD
1941 function checkbox_to_sql_bool($val) {
1942 return ($val == "on") ? "true" : "false";
1943 }
86b682ce
AD
1944
1945 function getFeedCatTitle($link, $id) {
1946 if ($id == -1) {
1947 return "Special";
1948 } else if ($id < -10) {
1949 return "Labels";
1950 } else if ($id > 0) {
1951 $result = db_query($link, "SELECT ttrss_feed_categories.title
1952 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
1953 cat_id = ttrss_feed_categories.id");
1954 if (db_num_rows($result) == 1) {
1955 return db_fetch_result($result, 0, "title");
1956 } else {
eb4311d0 1957 return "Uncategorized";
86b682ce
AD
1958 }
1959 } else {
1960 return "getFeedCatTitle($id) failed";
1961 }
1962
1963 }
1964
1965 function getFeedTitle($link, $id) {
1966 if ($id == -1) {
1967 return "Starred articles";
1968 } else if ($id < -10) {
1969 $label_id = -10 - $id;
1970 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
1971 if (db_num_rows($result) == 1) {
1972 return db_fetch_result($result, 0, "description");
1973 } else {
1974 return "Unknown label ($label_id)";
1975 }
1976
1977 } else if ($id > 0) {
1978 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
1979 if (db_num_rows($result) == 1) {
1980 return db_fetch_result($result, 0, "title");
1981 } else {
1982 return "Unknown feed ($id)";
1983 }
1984 } else {
1985 return "getFeedTitle($id) failed";
1986 }
1987
1988 }
3dd46f19
AD
1989
1990 function get_session_cookie_name() {
1991 return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME);
1992 }
3ac2b520
AD
1993
1994 function print_init_params($link) {
1995 print "<init-params>";
1996 if ($_SESSION["stored-params"]) {
1997 foreach (array_keys($_SESSION["stored-params"]) as $key) {
5f57b06d
AD
1998 if ($key) {
1999 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2000 print "<param key=\"$key\" value=\"$value\"/>";
2001 }
3ac2b520
AD
2002 }
2003 }
2004
2005 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>";
2006 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>";
0d51e25d 2007 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY . "\"/>";
3ac2b520
AD
2008
2009 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2010 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2011
e8bd0da9
AD
2012 print "<param key=\"hide_read_feeds\" value=\"" .
2013 sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>";
2014
c9268ed5
AD
2015 print "<param key=\"feeds_sort_by_unread\" value=\"" .
2016 sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>";
2017
3ac2b520
AD
2018 print "</init-params>";
2019 }
f54f515f
AD
2020
2021 function print_runtime_info($link) {
2022 print "<runtime-info>";
71ad883b
AD
2023 if (ENABLE_UPDATE_DAEMON) {
2024 print "<param key=\"daemon_is_running\" value=\"".
2025 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
2026 }
f54f515f
AD
2027 print "</runtime-info>";
2028 }
ef393de7 2029
88040f57 2030 function getSearchSql($search, $match_on) {
ef393de7 2031
88040f57 2032 $search_query_part = "";
e20c9d88 2033
88040f57
AD
2034 $keywords = split(" ", $search);
2035 $query_keywords = array();
e20c9d88 2036
88040f57 2037 if ($match_on == "both") {
e20c9d88 2038
88040f57
AD
2039 foreach ($keywords as $k) {
2040 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2041 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2042 }
e20c9d88 2043
88040f57 2044 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 2045
88040f57 2046 } else if ($match_on == "title") {
e20c9d88 2047
88040f57
AD
2048 foreach ($keywords as $k) {
2049 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2050 }
e20c9d88 2051
88040f57 2052 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 2053
88040f57
AD
2054 } else if ($match_on == "content") {
2055
2056 foreach ($keywords as $k) {
2057 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2058 }
2059 }
2060
2061 $search_query_part = implode("AND", $query_keywords);
2062
2063 return $search_query_part;
2064 }
2065
2066 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false) {
88040f57
AD
2067 if ($search) {
2068
2069 $search_query_part = getSearchSql($search, $match_on);
2070 $search_query_part .= " AND ";
e20c9d88 2071
ef393de7
AD
2072 } else {
2073 $search_query_part = "";
2074 }
2075
2076 $view_query_part = "";
2077
2078 if ($view_mode == "adaptive") {
2079 if ($search) {
2080 $view_query_part = " ";
2081 } else if ($feed != -1) {
f295c368 2082 $unread = getFeedUnread($link, $feed, $cat_view);
ef393de7
AD
2083 if ($unread > 0) {
2084 $view_query_part = " unread = true AND ";
2085 }
2086 }
2087 }
2088
2089 if ($view_mode == "marked") {
2090 $view_query_part = " marked = true AND ";
2091 }
2092
2093 if ($view_mode == "unread") {
2094 $view_query_part = " unread = true AND ";
2095 }
2096
2097 if ($limit > 0) {
2098 $limit_query_part = "LIMIT " . $limit;
2099 }
2100
2101 $vfeed_query_part = "";
2102
2103 // override query strategy and enable feed display when searching globally
2104 if ($search && $search_mode == "all_feeds") {
2105 $query_strategy_part = "ttrss_entries.id > 0";
2106 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2107 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2108 $query_strategy_part = "ttrss_entries.id > 0";
2109 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2110 id = feed_id) as feed_title,";
2111 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2112
2113 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
0a6c4846
AD
2114
2115 $tmp_result = false;
2116
2117 if ($cat_view) {
2118 $tmp_result = db_query($link, "SELECT id
2119 FROM ttrss_feeds WHERE cat_id = '$feed'");
2120 } else {
2121 $tmp_result = db_query($link, "SELECT id
2122 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2123 WHERE id = '$feed') AND id != '$feed'");
2124 }
ef393de7
AD
2125
2126 $cat_siblings = array();
2127
2128 if (db_num_rows($tmp_result) > 0) {
2129 while ($p = db_fetch_assoc($tmp_result)) {
2130 array_push($cat_siblings, "feed_id = " . $p["id"]);
2131 }
2132
2133 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2134 $feed, implode(" OR ", $cat_siblings));
2135
2136 } else {
2137 $query_strategy_part = "ttrss_entries.id > 0";
2138 }
2139
2140 } else if ($feed >= 0) {
2141
2142 if ($cat_view) {
5c365f60 2143
ef393de7
AD
2144 if ($feed > 0) {
2145 $query_strategy_part = "cat_id = '$feed'";
2146 } else {
2147 $query_strategy_part = "cat_id IS NULL";
2148 }
2149
2150 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
5c365f60 2151
ef393de7
AD
2152 } else {
2153 $tmp_result = db_query($link, "SELECT id
2154 FROM ttrss_feeds WHERE parent_feed = '$feed'
2155 ORDER BY cat_id,title");
2156
2157 $parent_ids = array();
2158
2159 if (db_num_rows($tmp_result) > 0) {
2160 while ($p = db_fetch_assoc($tmp_result)) {
2161 array_push($parent_ids, "feed_id = " . $p["id"]);
2162 }
2163
2164 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2165 $feed, implode(" OR ", $parent_ids));
2166
2167 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2168 } else {
2169 $query_strategy_part = "feed_id = '$feed'";
2170 }
2171 }
2172 } else if ($feed == -1) { // starred virtual feed
2173 $query_strategy_part = "marked = true";
2174 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2175 } else if ($feed <= -10) { // labels
2176 $label_id = -$feed - 11;
2177
2178 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2179 WHERE id = '$label_id'");
2180
2181 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2182
2183 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2184 } else {
2185 $query_strategy_part = "id > 0"; // dumb
2186 }
d6e5706d
AD
2187
2188 if (get_pref($link, 'REVERSE_HEADLINES')) {
2189 $order_by = "updated";
2190 } else {
2191 $order_by = "updated DESC";
2192 }
e939722a
AD
2193
2194 if ($override_order) {
2195 $order_by = $override_order;
2196 }
ef393de7
AD
2197
2198 $feed_title = "";
2199
2200 if ($search && $search_mode == "all_feeds") {
2201 $feed_title = "Global search results ($search)";
2202 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
e1eb2147 2203 $feed_title = "Tag search results ($search, $feed)";
ef393de7
AD
2204 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2205 $feed_title = $feed;
2206 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2207
2208 if ($cat_view) {
5c365f60 2209
ef393de7
AD
2210 if ($feed != 0) {
2211 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2212 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2213 $feed_title = db_fetch_result($result, 0, "title");
2214 } else {
2215 $feed_title = "Uncategorized";
2216 }
e1eb2147
AD
2217
2218 if ($search) {
2219 $feed_title = "Category search results ($search, $feed_title)";
2220 }
2221
ef393de7
AD
2222 } else {
2223
2224 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2225 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2226
2227 $feed_title = db_fetch_result($result, 0, "title");
2228 $feed_site_url = db_fetch_result($result, 0, "site_url");
2229 $last_error = db_fetch_result($result, 0, "last_error");
e1eb2147
AD
2230
2231 if ($search) {
2232 $feed_title = "Feed search results ($search, $feed_title)";
2233 }
ef393de7
AD
2234 }
2235
2236 } else if ($feed == -1) {
2237 $feed_title = "Starred articles";
2238 } else if ($feed < -10) {
2239 $label_id = -$feed - 11;
2240 $result = db_query($link, "SELECT description FROM ttrss_labels
2241 WHERE id = '$label_id'");
2242 $feed_title = db_fetch_result($result, 0, "description");
88040f57
AD
2243
2244 if ($search) {
2245 $feed_title = "Label search results ($search, $feed_title)";
2246 }
ef393de7
AD
2247 } else {
2248 $feed_title = "?";
2249 }
2250
2251 $feed_title = db_unescape_string($feed_title);
2252
2253 if ($feed < -10) error_reporting (0);
2254
2255 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2256
2257 if ($feed >= 0) {
2258 $feed_kind = "Feeds";
2259 } else {
2260 $feed_kind = "Labels";
2261 }
2262
2263 $content_query_part = "content as content_preview,";
2264
2265 $query = "SELECT
2266 ttrss_entries.id,ttrss_entries.title,
2267 SUBSTRING(updated,1,16) as updated,
2268 unread,feed_id,marked,link,last_read,
2269 SUBSTRING(last_read,1,19) as last_read_noms,
2270 $vfeed_query_part
2271 $content_query_part
2272 SUBSTRING(updated,1,19) as updated_noms
2273 FROM
2274 ttrss_entries,ttrss_user_entries,ttrss_feeds
2275 WHERE
cfb02131 2276 ttrss_feeds.hidden = false AND
ef393de7
AD
2277 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2278 ttrss_user_entries.ref_id = ttrss_entries.id AND
2279 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2280 $search_query_part
2281 $view_query_part
2282 $query_strategy_part ORDER BY $order_by
2283 $limit_query_part";
2284
2285 $result = db_query($link, $query);
2286
2287 if ($_GET["debug"]) print $query;
2288
2289 } else {
2290 // browsing by tag
2291
2292 $feed_kind = "Tags";
2293
2294 $result = db_query($link, "SELECT
2295 ttrss_entries.id as id,title,
2296 SUBSTRING(updated,1,16) as updated,
2297 unread,feed_id,
2298 marked,link,last_read,
2299 SUBSTRING(last_read,1,19) as last_read_noms,
2300 $vfeed_query_part
2301 $content_query_part
2302 SUBSTRING(updated,1,19) as updated_noms
2303 FROM
2304 ttrss_entries,ttrss_user_entries,ttrss_tags
2305 WHERE
2306 ref_id = ttrss_entries.id AND
2307 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2308 post_int_id = int_id AND tag_name = '$feed' AND
2309 $view_query_part
2310 $search_query_part
2311 $query_strategy_part ORDER BY $order_by
2312 $limit_query_part");
2313 }
2314
c7188969 2315 return array($result, $feed_title, $feed_site_url, $last_error);
ef393de7
AD
2316
2317 }
2318
e1eb2147
AD
2319 function generate_syndicated_feed($link, $feed, $is_cat,
2320 $search, $search_mode, $match_on) {
18664970
AD
2321
2322 $qfh_ret = queryFeedHeadlines($link, $feed,
e939722a 2323 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC");
18664970
AD
2324
2325 $result = $qfh_ret[0];
59e2aab4 2326 $feed_title = htmlspecialchars($qfh_ret[1]);
18664970
AD
2327 $feed_site_url = $qfh_ret[2];
2328 $last_error = $qfh_ret[3];
2329
2330 print "<rss version=\"2.0\">
2331 <channel>
2332 <title>$feed_title</title>
2333 <link>$feed_site_url</link>
2334 <generator>Tiny Tiny RSS v".VERSION."</generator>";
2335
2336 while ($line = db_fetch_assoc($result)) {
2337 print "<item>";
2338 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2339
2340 $rfc822_date = date('r', strtotime($line["updated"]));
2341
2342 print "<pubDate>$rfc822_date</pubDate>";
2343
2344 print "<title>" .
2345 htmlspecialchars($line["title"]) . "</title>";
2346
2347 print "<description>" .
2348 htmlspecialchars($line["content_preview"]) . "</description>";
2349
2350 print "</item>";
2351 }
2352
2353 print "</channel></rss>";
2354
2355 }
2356
0a6c4846
AD
2357 function getCategoryTitle($link, $cat_id) {
2358
2359 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2360 id = '$cat_id'");
2361
2362 if (db_num_rows($result) == 1) {
2363 return db_fetch_result($result, 0, "title");
2364 } else {
2365 return "Uncategorized";
2366 }
2367 }
2368
183ad07b
AD
2369 function sanitize_rss($str) {
2370 $res = "";
2371
2372 $res = preg_replace('/<script.*?>/i',
2373 "<p class=\"scriptWarn\">", $str);
2374
2375 $res = preg_replace('/<\/script>/i',
2376 "</p>", $res);
2377
2378 return $res;
2379 }
b72c3ef8 2380
9cd7c995
AD
2381 function send_headlines_digests($link, $limit = 100) {
2382
1ddba275
AD
2383 if (!DIGEST_ENABLE) return false;
2384
9cd7c995
AD
2385 $user_limit = DIGEST_EMAIL_LIMIT;
2386 $days = DIGEST_DAYS_BACK;
2387
2388 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
2389
2390 if (DB_TYPE == "pgsql") {
2391 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
2392 } else if (DB_TYPE == "mysql") {
2393 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
2394 }
2395
2396 $result = db_query($link, "SELECT id,email FROM ttrss_users
2397 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
2398
2399 while ($line = db_fetch_assoc($result)) {
2400 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
2401 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
2402
2403 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
2404 $digest = $tuple[0];
2405 $headlines_count = $tuple[1];
2406
2407 if ($headlines_count > 0) {
2408 $rc = mail($line["login"] . " <" . $line["email"] . ">",
2409 "[tt-rss] New headlines for last 24 hours", $digest,
3ab3c1f0
AD
2410 "From: " . MAIL_FROM . "\n".
2411 "Content-Type: text/plain; charset=\"utf-8\"\n".
2412 "Content-Transfer-Encoding: 8bit\n");
9cd7c995
AD
2413 print "RC=$rc\n";
2414 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
2415 WHERE id = " . $line["id"]);
2416 } else {
2417 print "No headlines\n";
2418 }
2419 }
2420 }
2421
2422// $digest = prepare_headlines_digest($link, $user_id, $days, $limit);
2423
2424 }
2425
7e3634d9
AD
2426 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
2427 $tmp = "New headlines for last 24 hours, as of " . date("Y/m/d H:m") . "\n";
2428 $tmp .= "=======================================================\n\n";
2429
2430 if (DB_TYPE == "pgsql") {
9cd7c995 2431 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
7e3634d9
AD
2432 } else if (DB_TYPE == "mysql") {
2433 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
2434 }
2435
2436 $result = db_query($link, "SELECT ttrss_entries.title,
2437 ttrss_feeds.title AS feed_title,
2438 date_entered,
2439 link,
2440 SUBSTRING(last_updated,1,19) AS last_updated
2441 FROM
2442 ttrss_user_entries,ttrss_entries,ttrss_feeds
2443 WHERE
2444 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
3dd9183c 2445 AND include_in_digest = true
7e3634d9 2446 AND $interval_query
448b0abd 2447 AND ttrss_user_entries.owner_uid = $user_id
7e3634d9
AD
2448 AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC
2449 LIMIT $limit");
2450
2451 $cur_feed_title = "";
2452
9cd7c995
AD
2453 $headlines_count = db_num_rows($result);
2454
7e3634d9
AD
2455 while ($line = db_fetch_assoc($result)) {
2456 $updated = smart_date_time(strtotime($line["last_updated"]));
2457 $feed_title = $line["feed_title"];
2458
2459 if ($cur_feed_title != $feed_title) {
2460 $cur_feed_title = $feed_title;
2461
2462 $tmp .= "$feed_title\n\n";
2463 }
2464
2465 $tmp .= " * " . trim($line["title"]) . " - $updated\n";
2466 $tmp .= " " . trim($line["link"]) . "\n";
2467 $tmp .= "\n";
2468 }
2469
2470 $tmp .= "--- \n";
dfe6f833
AD
2471 $tmp .= "You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at " .
2472 DIGEST_HOSTNAME . "\n".
2473 "To unsubscribe, visit your configuration options or contact instance owner.\n";
7e3634d9
AD
2474
2475
9cd7c995 2476 return array($tmp, $headlines_count);
7e3634d9
AD
2477 }
2478
b72c3ef8
AD
2479 function check_for_update($link) {
2480 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
2481
2482 if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) {
2483 return;
2484 }
2485
2486 error_reporting(0);
2487 $rss = fetch_rss($releases_feed);
2488 error_reporting (DEFAULT_ERROR_LEVEL);
2489
2490 if ($rss) {
2491
2492 $items = $rss->items;
2493
2494 if (!$items || !is_array($items)) $items = $rss->entries;
2495 if (!$items || !is_array($items)) $items = $rss;
2496
da412ad3 2497 if (!is_array($items) || count($items) == 0) {
b72c3ef8 2498 return;
da412ad3 2499 }
b72c3ef8
AD
2500
2501 $last_item = $items[0];
2502
2503 $last_version = trim(preg_replace("/(Milestone)|(completed)/", "", $last_item["title"]));
2504 $cur_version = preg_replace("/\.99/", "", VERSION);
2505
8a797376
AD
2506 $release_url = sanitize_rss($last_item["link"]);
2507 $content = sanitize_rss($last_item["description"]);
b72c3ef8 2508
9157e015 2509 if ($cur_version != $last_version) {
8a797376
AD
2510 return "<div class=\"notice\"><a href=\"javascript:showBlockElement('milestoneDetails')\">
2511 New version of Tiny-Tiny RSS ($last_version) is available (click for details)</a>
2512 <div id=\"milestoneDetails\">$content</div></div>";
da412ad3 2513 }
b72c3ef8
AD
2514 }
2515 }
40d13c28 2516?>