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