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