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