]> git.wh0rd.org - tt-rss.git/blame - functions.php
change description for option ENABLE_SIMPLEPIE
[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';
cc17c205 10
f8c612d4 11 function get_translations() {
6a214f92 12 $tr = array(
019bd5a9 13 "auto" => "Detect automatically",
6a214f92
AD
14 "en_US" => "English",
15 "fr_FR" => "Français",
16 "ru_RU" => "Русский",
17 "zh_CN" => "Simplified Chinese");
f8c612d4
AD
18
19 return $tr;
20 }
21
8d039718 22 if (ENABLE_TRANSLATIONS == true) {
865220a4
AD
23 require_once "accept-to-gettext.php";
24 require_once "gettext/gettext.inc";
aba609e0 25
8d039718
AD
26 function startup_gettext() {
27
28 # Get locale from Accept-Language header
6a214f92 29 $lang = al2gt(array_keys(get_translations()), "text/html");
89cb787e
AD
30
31 if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) {
32 $lang = _TRANSLATION_OVERRIDE_DEFAULT;
33 }
34
672f3f3c 35 if ($_COOKIE["ttrss_lang"] && $_COOKIE["ttrss_lang"] != "auto") {
68659d98
AD
36 $lang = $_COOKIE["ttrss_lang"];
37 }
38
8d039718
AD
39 if ($lang) {
40 _setlocale(LC_MESSAGES, $lang);
41 _bindtextdomain("messages", "locale");
42 _textdomain("messages");
43 _bind_textdomain_codeset("messages", "UTF-8");
44 }
aba609e0 45 }
aba609e0 46
cc17c205 47 startup_gettext();
865220a4
AD
48
49 } else {
50 function __($msg) {
51 return $msg;
52 }
53 function startup_gettext() {
54 // no-op
55 return true;
56 }
cc17c205
AD
57 }
58
b619ff15 59 require_once 'db-prefs.php';
5bc0bd27 60 require_once 'compat.php';
af106b0e 61 require_once 'errors.php';
8911ac8b 62 require_once 'version.php';
40d13c28 63
49f9c923 64 define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')');
a3ee2a38
AD
65 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
66
9fdf7824
AD
67 if (ENABLE_SIMPLEPIE) {
68 require_once "simplepie/simplepie.inc";
69 } else {
70 require_once "magpierss/rss_fetch.inc";
71 require_once 'magpierss/rss_utils.inc';
72 }
49f9c923 73
f738aef1
AD
74 include_once "tw/tw-config.php";
75 include_once "tw/tw.php";
76 include_once TW_SETUP . "paranoya.php";
77
78 $tw_parser = new twParser();
79
6f9e33e4
AD
80 function _debug($msg) {
81 $ts = strftime("%H:%M:%S", time());
82 print "[$ts] $msg\n";
83 }
84
ad507f85
AD
85 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
86
87 $rows = -1;
4c193675 88
fefa6ca3 89 if (DB_TYPE == "pgsql") {
44e241cb 90/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 91 marked = false AND feed_id = '$feed_id' AND
35d8cf43 92 (SELECT date_entered FROM ttrss_entries WHERE
44e241cb
AD
93 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
94
6e7f8d26
AD
95 $pg_version = get_pgsql_version($link);
96
97 if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
1e59ae35
AD
98
99 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
100 ttrss_entries.id = ref_id AND
101 marked = false AND
102 feed_id = '$feed_id' AND
103 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
104
105 } else {
106
107 $result = db_query($link, "DELETE FROM ttrss_user_entries
108 USING ttrss_entries
109 WHERE ttrss_entries.id = ref_id AND
110 marked = false AND
111 feed_id = '$feed_id' AND
fc774155 112 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
1e59ae35 113 }
ad507f85
AD
114
115 $rows = pg_affected_rows($result);
116
fefa6ca3 117 } else {
1e59ae35 118
30f1746f 119/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 120 marked = false AND feed_id = '$feed_id' AND
35d8cf43 121 (SELECT date_entered FROM ttrss_entries WHERE
30f1746f
AD
122 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
123
124 $result = db_query($link, "DELETE FROM ttrss_user_entries
125 USING ttrss_user_entries, ttrss_entries
126 WHERE ttrss_entries.id = ref_id AND
127 marked = false AND
128 feed_id = '$feed_id' AND
129 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
130
ad507f85
AD
131 $rows = mysql_affected_rows($link);
132
133 }
134
135 if ($debug) {
6f9e33e4 136 _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
fefa6ca3
AD
137 }
138 }
139
44e241cb
AD
140 function global_purge_old_posts($link, $do_output = false, $limit = false) {
141
894ebcf5 142 $random_qpart = sql_random_function();
fefa6ca3 143
44e241cb
AD
144 if ($limit) {
145 $limit_qpart = "LIMIT $limit";
146 } else {
147 $limit_qpart = "";
148 }
149
fefa6ca3 150 $result = db_query($link,
44e241cb
AD
151 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
152 ORDER BY $random_qpart $limit_qpart");
fefa6ca3
AD
153
154 while ($line = db_fetch_assoc($result)) {
155
156 $feed_id = $line["id"];
157 $purge_interval = $line["purge_interval"];
158 $owner_uid = $line["owner_uid"];
159
160 if ($purge_interval == 0) {
161
162 $tmp_result = db_query($link,
163 "SELECT value FROM ttrss_user_prefs WHERE
164 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
165
166 if (db_num_rows($tmp_result) != 0) {
167 $purge_interval = db_fetch_result($tmp_result, 0, "value");
168 }
169 }
170
171 if ($do_output) {
ad507f85 172// print "Feed $feed_id: purge interval = $purge_interval\n";
fefa6ca3
AD
173 }
174
175 if ($purge_interval > 0) {
ad507f85 176 purge_feed($link, $feed_id, $purge_interval, $do_output);
fefa6ca3
AD
177 }
178 }
179
71604ca4
AD
180 // purge orphaned posts in main content table
181 db_query($link, "DELETE FROM ttrss_entries WHERE
182 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
183
fefa6ca3
AD
184 }
185
b6eefba5 186 function purge_old_posts($link) {
5d73494a 187
f1a80dae
AD
188 $user_id = $_SESSION["uid"];
189
190 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
191 WHERE owner_uid = '$user_id'");
5d73494a
AD
192
193 while ($line = db_fetch_assoc($result)) {
194
195 $feed_id = $line["id"];
196 $purge_interval = $line["purge_interval"];
197
b619ff15 198 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
5d73494a 199
140aae81 200 if ($purge_interval > 0) {
fefa6ca3 201 purge_feed($link, $feed_id, $purge_interval);
5d73494a
AD
202 }
203 }
71604ca4
AD
204
205 // purge orphaned posts in main content table
206 db_query($link, "DELETE FROM ttrss_entries WHERE
207 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
c3a8d71a
AD
208 }
209
1f2b01ed 210 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
40d13c28 211
4769ddaf 212 if (WEB_DEMO_MODE) return;
b0b4abcf 213
a2770077
AD
214 if (!$user_id) {
215 $user_id = $_SESSION["uid"];
216 purge_old_posts($link);
217 }
218
25af8dad 219// db_query($link, "BEGIN");
b82af8c3 220
cbd8650d
AD
221 if (MAX_UPDATE_TIME > 0) {
222 if (DB_TYPE == "mysql") {
223 $q_order = "RAND()";
224 } else {
225 $q_order = "RANDOM()";
226 }
227 } else {
228 $q_order = "last_updated DESC";
229 }
230
d148926e 231 $result = db_query($link, "SELECT feed_url,id,
798f722b 232 SUBSTRING(last_updated,1,19) AS last_updated,
5c563acd 233 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
cbd8650d
AD
234 ORDER BY $q_order");
235
236 $upd_start = time();
40d13c28 237
b6eefba5 238 while ($line = db_fetch_assoc($result)) {
d148926e
AD
239 $upd_intl = $line["update_interval"];
240
b619ff15 241 if (!$upd_intl || $upd_intl == 0) {
e289ca71 242 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
b619ff15 243 }
d148926e 244
c1e202b7
AD
245 if ($upd_intl < 0) {
246 // Updates for this feed are disabled
247 continue;
248 }
249
93d40f50
AD
250 if ($fetch || (!$line["last_updated"] ||
251 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
c5142cca 252
cbd8650d
AD
253// print "<!-- feed: ".$line["feed_url"]." -->";
254
1f2b01ed 255 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
cbd8650d
AD
256
257 $upd_elapsed = time() - $upd_start;
258
259 if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) {
260 return;
261 }
d148926e 262 }
40d13c28
AD
263 }
264
25af8dad 265// db_query($link, "COMMIT");
b82af8c3 266
40d13c28
AD
267 }
268
4065b60b
AD
269 function fetch_file_contents($url) {
270 if (USE_CURL_FOR_ICONS) {
271 $tmpfile = tempnam(TMP_DIRECTORY, "ttrss-tmp");
272
273 $ch = curl_init($url);
274 $fp = fopen($tmpfile, "w");
275
276 if ($fp) {
277 curl_setopt($ch, CURLOPT_FILE, $fp);
dd966fed
AD
278 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
279 curl_setopt($ch, CURLOPT_TIMEOUT, 45);
4065b60b
AD
280 curl_exec($ch);
281 curl_close($ch);
282 fclose($fp);
283 }
284
285 $contents = file_get_contents($tmpfile);
286 unlink($tmpfile);
287
288 return $contents;
289
290 } else {
291 return file_get_contents($url);
292 }
293
294 }
78800912 295
4065b60b
AD
296 // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
297 // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
78800912 298
4065b60b 299 function get_favicon_url($url) {
99331724 300
4065b60b 301 if ($html = @fetch_file_contents($url)) {
78800912 302
4065b60b
AD
303 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
304 // Attempt to grab a favicon link from their webpage url
305 $linkUrl = html_entity_decode($matches[1]);
c798704b 306
4065b60b
AD
307 if (substr($linkUrl, 0, 1) == '/') {
308 $urlParts = parse_url($url);
309 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
310 } else if (substr($linkUrl, 0, 7) == 'http://') {
311 $faviconURL = $linkUrl;
312 } else if (substr($url, -1, 1) == '/') {
313 $faviconURL = $url.$linkUrl;
314 } else {
315 $faviconURL = $url.'/'.$linkUrl;
e695fdc8 316 }
717f5e64 317
c798704b 318 } else {
4065b60b
AD
319 // If unsuccessful, attempt to "guess" the favicon location
320 $urlParts = parse_url($url);
321 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
322 }
323 }
c798704b 324
4065b60b
AD
325 // Run a test to see if what we have attempted to get actually exists.
326 if(USE_CURL_FOR_ICONS || url_validate($faviconURL)) {
327 return $faviconURL;
328 } else {
329 return false;
330 }
331 }
332
333 function url_validate($link) {
334
335 $url_parts = @parse_url($link);
336
337 if ( empty( $url_parts["host"] ) )
338 return false;
339
340 if ( !empty( $url_parts["path"] ) ) {
341 $documentpath = $url_parts["path"];
342 } else {
343 $documentpath = "/";
344 }
345
346 if ( !empty( $url_parts["query"] ) )
347 $documentpath .= "?" . $url_parts["query"];
348
349 $host = $url_parts["host"];
350 $port = $url_parts["port"];
351
352 if ( empty($port) )
353 $port = "80";
354
355 $socket = @fsockopen( $host, $port, $errno, $errstr, 30 );
356
357 if ( !$socket )
358 return false;
c798704b 359
4065b60b
AD
360 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
361
362 $http_response = fgets( $socket, 22 );
363
364 $responses = "/(200 OK)|(30[0-9] Moved)/";
365 if ( preg_match($responses, $http_response) ) {
366 fclose($socket);
367 return true;
368 } else {
369 return false;
370 }
371
372 }
373
374 function check_feed_favicon($site_url, $feed, $link) {
375 $favicon_url = get_favicon_url($site_url);
376
377# print "FAVICON [$site_url]: $favicon_url\n";
378
379 error_reporting(0);
380
381 $icon_file = ICONS_DIR . "/$feed.ico";
382
383 if ($favicon_url && !file_exists($icon_file)) {
384 $contents = fetch_file_contents($favicon_url);
385
386 $fp = fopen($icon_file, "w");
78800912 387
4065b60b
AD
388 if ($fp) {
389 fwrite($fp, $contents);
390 fclose($fp);
391 chmod($icon_file, 0644);
392 }
78800912 393 }
4065b60b
AD
394
395 error_reporting(DEFAULT_ERROR_LEVEL);
396
78800912
AD
397 }
398
ddb68b81 399 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
40d13c28 400
ddb68b81 401 if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) {
21cfcdf2
AD
402 return;
403 }
404
4bc64807 405 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb 406 _debug("update_rss_feed: start");
219bd8fc
AD
407 }
408
47c6c988 409 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
a88c1f36
AD
410 FROM ttrss_feeds WHERE id = '$feed'");
411
47439031
AD
412 $auth_login = db_fetch_result($result, 0, "auth_login");
413 $auth_pass = db_fetch_result($result, 0, "auth_pass");
47c6c988 414
a88c1f36
AD
415 $update_interval = db_fetch_result($result, 0, "update_interval");
416
417 if ($update_interval < 0) { return; }
418
ab3d0b99
AD
419 $feed = db_escape_string($feed);
420
47c6c988
AD
421 $fetch_url = $feed_url;
422
423 if ($auth_login && $auth_pass) {
424 $url_parts = array();
425 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
426
427 if ($url_parts[1] && $url_parts[2]) {
428 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
429 }
430
431 }
ab3d0b99 432
4bc64807 433 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
ca872b9d 434 _debug("update_rss_feed: fetching [$fetch_url]...");
219bd8fc
AD
435 }
436
9fdf7824 437 if (!defined('DAEMON_EXTENDED_DEBUG') && !$_GET['xdebug']) {
219bd8fc
AD
438 error_reporting(0);
439 }
440
9fdf7824
AD
441 if (!ENABLE_SIMPLEPIE) {
442 $rss = fetch_rss($fetch_url);
443 } else {
ca872b9d
AD
444 $rss = new SimplePie();
445 $rss->set_useragent(SIMPLEPIE_USERAGENT . MAGPIE_USER_AGENT_EXT);
446 $rss->set_feed_url($fetch_url);
447 $rss->set_output_encoding('UTF-8');
9fdf7824 448 $rss->init();
9fdf7824
AD
449 }
450
451// print_r($rss);
219bd8fc 452
4bc64807 453 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb 454 _debug("update_rss_feed: fetch done, parsing...");
219bd8fc
AD
455 } else {
456 error_reporting (DEFAULT_ERROR_LEVEL);
457 }
458
b6eefba5 459 $feed = db_escape_string($feed);
dcee8f61 460
ca872b9d
AD
461 if (ENABLE_SIMPLEPIE) {
462 $fetch_ok = !$rss->error();
463 } else {
464 $fetch_ok = !!$rss;
465 }
466
467 if ($fetch_ok) {
50b62214 468
4bc64807 469 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
50b62214
AD
470 _debug("update_rss_feed: processing feed data...");
471 }
472
44e241cb 473// db_query($link, "BEGIN");
dd8c76a9 474
a88c1f36 475 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
f324892e 476 FROM ttrss_feeds WHERE id = '$feed'");
331900c6 477
b6eefba5
AD
478 $registered_title = db_fetch_result($result, 0, "title");
479 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
f324892e 480 $orig_site_url = db_fetch_result($result, 0, "site_url");
331900c6 481
7fed1940
AD
482 $owner_uid = db_fetch_result($result, 0, "owner_uid");
483
9fdf7824
AD
484 if (ENABLE_SIMPLEPIE) {
485 $site_url = $rss->get_link();
486 } else {
487 $site_url = $rss->channel["link"];
488 }
489
8d0ec6fd 490 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
4bc64807 491 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
50b62214
AD
492 _debug("update_rss_feed: checking favicon...");
493 }
9fdf7824
AD
494
495 check_feed_favicon($site_url, $feed, $link);
a2770077
AD
496 }
497
746b249f 498 if (!$registered_title || $registered_title == "[Unknown]") {
4bc64807 499
fb486a33
AD
500 if (ENABLE_SIMPLEPIE) {
501 $feed_title = $rss->get_title();
502 } else {
503 $feed_title = db_escape_string($rss->channel["title"]);
504 }
4bc64807
AD
505
506 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
507 _debug("update_rss_feed: registering title: $feed_title");
508 }
7c5a308d 509
f324892e
AD
510 db_query($link, "UPDATE ttrss_feeds SET
511 title = '$feed_title' WHERE id = '$feed'");
512 }
513
49f9c923 514 // weird, weird Magpie
9fdf7824
AD
515 if (!ENABLE_SIMPLEPIE) {
516 if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
517 }
147f7691
AD
518
519 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
f324892e
AD
520 db_query($link, "UPDATE ttrss_feeds SET
521 site_url = '$site_url' WHERE id = '$feed'");
331900c6 522 }
40d13c28 523
b7f4bda2
AD
524// print "I: " . $rss->channel["image"]["url"];
525
9fdf7824
AD
526 if (!ENABLE_SIMPLEPIE) {
527 $icon_url = $rss->image["url"];
528 } else {
529 $icon_url = $rss->get_image_url();
530 }
b7f4bda2 531
147f7691 532 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
b6eefba5
AD
533 $icon_url = db_escape_string($icon_url);
534 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
b7f4bda2
AD
535 }
536
51e456d6 537 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
50b62214
AD
538 _debug("update_rss_feed: loading filters...");
539 }
e6155a06
AD
540
541 $filters = array();
542
4b3dff6e 543 $result = db_query($link, "SELECT reg_exp,
db42b934 544 ttrss_filter_types.name AS name,
073ca0e6 545 ttrss_filter_actions.name AS action,
c2d9322b 546 inverse,
073ca0e6 547 action_param
db42b934 548 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
e8b79d16 549 enabled = true AND
db42b934
AD
550 owner_uid = $owner_uid AND
551 ttrss_filter_types.id = filter_type AND
552 ttrss_filter_actions.id = action_id AND
f8382011 553 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
e6155a06 554
b6eefba5 555 while ($line = db_fetch_assoc($result)) {
e6155a06 556 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
19c9cb11
AD
557
558 $filter["reg_exp"] = $line["reg_exp"];
559 $filter["action"] = $line["action"];
073ca0e6 560 $filter["action_param"] = $line["action_param"];
c2d9322b 561 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
073ca0e6 562
19c9cb11 563 array_push($filters[$line["name"]], $filter);
e6155a06
AD
564 }
565
9fdf7824
AD
566 if (ENABLE_SIMPLEPIE) {
567 $iterator = $rss->get_items();
568 } else {
569 $iterator = $rss->items;
570 if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
571 if (!$iterator || !is_array($iterator)) $iterator = $rss;
572 }
c22789da
AD
573
574 if (!is_array($iterator)) {
39541e74 575 /* db_query($link, "UPDATE ttrss_feeds
75bd0669 576 SET last_error = 'Parse error: can\'t find any articles.'
77f0a2a7
AD
577 WHERE id = '$feed'"); */
578
579 // clear any errors and mark feed as updated if fetched okay
580 // even if it's blank
581
51e456d6 582 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
844012bc
AD
583 _debug("update_rss_feed: entry iterator is not an array, no articles?");
584 }
585
77f0a2a7
AD
586 db_query($link, "UPDATE ttrss_feeds
587 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
588
589 return; // no articles
c22789da 590 }
ddb68b81 591
51e456d6 592 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
50b62214
AD
593 _debug("update_rss_feed: processing articles...");
594 }
595
ddb68b81 596 foreach ($iterator as $item) {
7c5a308d 597
9fdf7824
AD
598 if (ENABLE_SIMPLEPIE) {
599 $entry_guid = $item->get_id();
600 if (!$entry_guid) $entry_guid = $item->get_link();
601 if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title());
602
603 } else {
604
605 $entry_guid = $item["id"];
34e420fb 606
9fdf7824
AD
607 if (!$entry_guid) $entry_guid = $item["guid"];
608 if (!$entry_guid) $entry_guid = $item["link"];
609 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
610 }
be832a1a 611
9fdf7824 612 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb
AD
613 _debug("update_rss_feed: guid $entry_guid");
614 }
615
be832a1a
AD
616 if (!$entry_guid) continue;
617
618 $entry_timestamp = "";
619
9fdf7824
AD
620 if (ENABLE_SIMPLEPIE) {
621 $entry_timestamp = strtotime($item->get_date());
622 } else {
623 $rss_2_date = $item['pubdate'];
624 $rss_1_date = $item['dc']['date'];
625 $atom_date = $item['issued'];
626 if (!$atom_date) $atom_date = $item['updated'];
be832a1a 627
9fdf7824
AD
628 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
629 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
630 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
631 }
b82af8c3 632
2e930846 633 if ($entry_timestamp == "" || $entry_timestamp == -1 || !$entry_timestamp) {
be832a1a
AD
634 $entry_timestamp = time();
635 $no_orig_date = 'true';
636 } else {
637 $no_orig_date = 'false';
638 }
639
640 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
641
9fdf7824
AD
642 if (ENABLE_SIMPLEPIE) {
643 $entry_title = $item->get_title();
644 } else {
645 $entry_title = trim(strip_tags($item["title"]));
646 }
be832a1a 647
9fdf7824
AD
648 if (ENABLE_SIMPLEPIE) {
649 $entry_link = $item->get_link();
650 } else {
651 // strange Magpie workaround
652 $entry_link = $item["link_"];
653 if (!$entry_link) $entry_link = $item["link"];
654 }
be832a1a
AD
655
656 if (!$entry_title) continue;
7d3ab0dd 657
be832a1a 658 $entry_link = strip_tags($entry_link);
7d3ab0dd 659
9fdf7824
AD
660 if (ENABLE_SIMPLEPIE) {
661 $entry_content = $item->get_description();
662 } else {
663 $entry_content = $item["content:escaped"];
664
665 if (!$entry_content) $entry_content = $item["content:encoded"];
666 if (!$entry_content) $entry_content = $item["content"];
667 if (!$entry_content) $entry_content = $item["atom_content"];
668 if (!$entry_content) $entry_content = $item["summary"];
669 if (!$entry_content) $entry_content = $item["description"];
670
671 // WTF
672 if (is_array($entry_content)) {
673 $entry_content = $entry_content["encoded"];
674 if (!$entry_content) $entry_content = $entry_content["escaped"];
675 }
be832a1a
AD
676 }
677
678// print_r($item);
679// print_r(htmlspecialchars($entry_content));
680// print "<br>";
681
682 $entry_content_unescaped = $entry_content;
be832a1a 683
9fdf7824
AD
684 if (ENABLE_SIMPLEPIE) {
685 $entry_comments = strip_tags($item->data["comments"]);
686 $entry_author = $item->get_author();
687 } else {
688 $entry_comments = strip_tags($item["comments"]);
689
690 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
4d6e9157 691
9fdf7824
AD
692 if ($item['author']) {
693
694 if (is_array($item['author'])) {
695
696 if (!$entry_author) {
697 $entry_author = db_escape_string(strip_tags($item['author']['name']));
698 }
699
700 if (!$entry_author) {
701 $entry_author = db_escape_string(strip_tags($item['author']['email']));
702 }
4d6e9157 703 }
9fdf7824 704
4d6e9157 705 if (!$entry_author) {
9fdf7824 706 $entry_author = db_escape_string(strip_tags($item['author']));
4d6e9157 707 }
83f114c8 708 }
be832a1a
AD
709 }
710
83f114c8
AD
711 if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = '';
712
be832a1a
AD
713 $entry_guid = db_escape_string(strip_tags($entry_guid));
714
715 $result = db_query($link, "SELECT id FROM ttrss_entries
716 WHERE guid = '$entry_guid'");
717
718 $entry_content = db_escape_string($entry_content);
7e43ad58
AD
719
720 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
721
be832a1a
AD
722 $entry_title = db_escape_string($entry_title);
723 $entry_link = db_escape_string($entry_link);
2544f36b
AD
724 $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250);
725 $entry_author = mb_substr($entry_author, 0, 250);
be832a1a 726
9fdf7824
AD
727 if (ENABLE_SIMPLEPIE) {
728 $num_comments = 0; #FIXME#
729 } else {
730 $num_comments = db_escape_string($item["slash"]["comments"]);
731 }
be832a1a
AD
732
733 if (!$num_comments) $num_comments = 0;
734
fefef828 735 // parse <category> entries into tags
be832a1a 736
9fdf7824 737 if (ENABLE_SIMPLEPIE) {
be832a1a 738
fefef828 739 $additional_tags = array();
9fdf7824
AD
740 $additional_tags_src = $item->get_categories();
741
742 foreach ($additional_tags_src as $tobj) {
743 array_push($additional_tags, $tobj->get_term());
fefef828 744 }
fefef828 745
9fdf7824 746 } else {
fefef828 747
9fdf7824 748 $t_ctr = $item['category#'];
fefef828 749
9fdf7824
AD
750 $additional_tags = false;
751
752 if ($t_ctr == 0) {
753 $additional_tags = false;
754 } else if ($t_ctr == 1) {
755 $additional_tags = array($item['category']);
756 } else {
757 $additional_tags = array();
758 for ($i = 0; $i <= $t_ctr; $i++ ) {
759 if ($item["category#$i"]) {
760 array_push($additional_tags, $item["category#$i"]);
761 }
762 }
763 }
764
765 // parse <dc:subject> elements
766
767 $t_ctr = $item['dc']['subject#'];
768
769 if ($t_ctr == 1) {
770 $additional_tags = array($item['dc']['subject']);
771 } else if ($t_ctr > 1) {
772 $additional_tags = array();
773 for ($i = 0; $i <= $t_ctr; $i++ ) {
774 if ($item['dc']["subject#$i"]) {
775 array_push($additional_tags, $item['dc']["subject#$i"]);
776 }
fefef828
AD
777 }
778 }
779 }
8add756a 780
d48d160c 781 # sanitize content
183ad07b 782
007a38d4 783// $entry_content = sanitize_rss($entry_content);
d48d160c 784
9fdf7824 785 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb
AD
786 _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
787 }
788
44e241cb
AD
789 db_query($link, "BEGIN");
790
4c193675
AD
791 if (db_num_rows($result) == 0) {
792
9fdf7824 793 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb
AD
794 _debug("update_rss_feed: base guid not found");
795 }
796
4c193675
AD
797 // base post entry does not exist, create it
798
4c193675
AD
799 $result = db_query($link,
800 "INSERT INTO ttrss_entries
801 (title,
802 guid,
803 link,
804 updated,
805 content,
806 content_hash,
807 no_orig_date,
808 date_entered,
11b0dce2 809 comments,
b6104dee
AD
810 num_comments,
811 author)
4c193675
AD
812 VALUES
813 ('$entry_title',
814 '$entry_guid',
815 '$entry_link',
816 '$entry_timestamp_fmt',
817 '$entry_content',
818 '$content_hash',
819 $no_orig_date,
820 NOW(),
11b0dce2 821 '$entry_comments',
b6104dee
AD
822 '$num_comments',
823 '$entry_author')");
8926aab8
AD
824 } else {
825 // we keep encountering the entry in feeds, so we need to
826 // update date_entered column so that we don't get horrible
827 // dupes when the entry gets purged and reinserted again e.g.
828 // in the case of SLOW SLOW OMG SLOW updating feeds
829
830 $base_entry_id = db_fetch_result($result, 0, "id");
831
832 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
833 WHERE id = '$base_entry_id'");
4c193675
AD
834 }
835
836 // now it should exist, if not - bad luck then
837
6385315d
AD
838 $result = db_query($link, "SELECT
839 id,content_hash,no_orig_date,title,
8926aab8 840 substring(date_entered,1,19) as date_entered,
11b0dce2
AD
841 substring(updated,1,19) as updated,
842 num_comments
6385315d
AD
843 FROM
844 ttrss_entries
845 WHERE guid = '$entry_guid'");
4c193675
AD
846
847 if (db_num_rows($result) == 1) {
848
9fdf7824 849 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
7ca91eb3 850 _debug("update_rss_feed: base guid found, checking for user record");
34e420fb
AD
851 }
852
11b0dce2
AD
853 // this will be used below in update handler
854 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
855 $orig_title = db_fetch_result($result, 0, "title");
856 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
8926aab8
AD
857 $orig_date_entered = strtotime(db_fetch_result($result,
858 0, "date_entered"));
6385315d 859
11b0dce2 860 $ref_id = db_fetch_result($result, 0, "id");
4c193675 861
11b0dce2 862 // check for user post link to main table
4c193675 863
11b0dce2 864 // do we allow duplicate posts with same GUID in different feeds?
8d0ec6fd 865 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
11b0dce2
AD
866 $dupcheck_qpart = "AND feed_id = '$feed'";
867 } else {
868 $dupcheck_qpart = "";
869 }
71604ca4 870
11b0dce2 871// error_reporting(0);
19c9cb11 872
f8382011
AD
873 $article_filters = get_article_filters($filters, $entry_title,
874 $entry_content, $entry_link);
19c9cb11 875
9fdf7824 876 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
7ca91eb3
AD
877 _debug("update_rss_feed: article filters: ");
878 if (count($article_filters) != 0) {
879 print_r($article_filters);
880 }
881 }
882
f8382011 883 if (find_article_filter($article_filters, "filter")) {
11b0dce2
AD
884 continue;
885 }
19c9cb11 886
11b0dce2 887// error_reporting (DEFAULT_ERROR_LEVEL);
3a933f22 888
11b0dce2
AD
889 $result = db_query($link,
890 "SELECT ref_id FROM ttrss_user_entries WHERE
891 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
892 $dupcheck_qpart");
7ca91eb3 893
11b0dce2
AD
894 // okay it doesn't exist - create user entry
895 if (db_num_rows($result) == 0) {
896
9fdf7824 897 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
7ca91eb3
AD
898 _debug("update_rss_feed: user record not found, creating...");
899 }
900
f8382011 901 if (!find_article_filter($article_filters, 'catchup')) {
11b0dce2
AD
902 $unread = 'true';
903 $last_read_qpart = 'NULL';
904 } else {
905 $unread = 'false';
906 $last_read_qpart = 'NOW()';
907 }
dd7d3187 908
f8382011 909 if (find_article_filter($article_filters, 'mark')) {
dd7d3187
AD
910 $marked = 'true';
911 } else {
912 $marked = 'false';
913 }
a36c0dfe
AD
914
915 if (find_article_filter($article_filters, 'publish')) {
916 $published = 'true';
917 } else {
918 $published = 'false';
919 }
920
11b0dce2
AD
921 $result = db_query($link,
922 "INSERT INTO ttrss_user_entries
a36c0dfe 923 (ref_id, owner_uid, feed_id, unread, last_read, marked, published)
11b0dce2 924 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
a36c0dfe 925 $last_read_qpart, $marked, $published)");
11b0dce2
AD
926 }
927
6385315d
AD
928 $post_needs_update = false;
929
8d0ec6fd 930 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
6385315d 931 ($content_hash != $orig_content_hash)) {
7e43ad58 932// print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->";
6385315d
AD
933 $post_needs_update = true;
934 }
935
7e43ad58 936 if (db_escape_string($orig_title) != $entry_title) {
6385315d
AD
937 $post_needs_update = true;
938 }
939
11b0dce2
AD
940 if ($orig_num_comments != $num_comments) {
941 $post_needs_update = true;
942 }
943
6385315d
AD
944// this doesn't seem to be very reliable
945//
946// if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
947// $post_needs_update = true;
948// }
949
950 // if post needs update, update it and mark all user entries
1c73bc0c 951 // linking to this post as updated
6385315d
AD
952 if ($post_needs_update) {
953
7ca91eb3
AD
954 if (defined('DAEMON_EXTENDED_DEBUG')) {
955 _debug("update_rss_feed: post $entry_guid needs update...");
956 }
957
6385315d
AD
958// print "<!-- post $orig_title needs update : $post_needs_update -->";
959
6385315d 960 db_query($link, "UPDATE ttrss_entries
11b0dce2 961 SET title = '$entry_title', content = '$entry_content',
7e43ad58 962 content_hash = '$content_hash',
11b0dce2 963 num_comments = '$num_comments'
6385315d
AD
964 WHERE id = '$ref_id'");
965
8d0ec6fd 966 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
4919fb42
AD
967 db_query($link, "UPDATE ttrss_user_entries
968 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
969 } else {
970 db_query($link, "UPDATE ttrss_user_entries
971 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
972 }
6385315d
AD
973
974 }
4c193675
AD
975 }
976
44e241cb
AD
977 db_query($link, "COMMIT");
978
9fdf7824 979 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb
AD
980 _debug("update_rss_feed: looking for tags...");
981 }
982
eb36b4eb 983 /* taaaags */
40e1a95b 984 // <a href="..." rel="tag">Xorg</a>, //
eb36b4eb 985
05732aa0 986 $entry_tags = null;
eb36b4eb 987
40e1a95b 988 preg_match_all("/<a.*?rel=['\"]tag['\"].*?>([^<]+)<\/a>/i",
ee2c3050
AD
989 $entry_content_unescaped, $entry_tags);
990
fefef828
AD
991/* print "<p><br/>$entry_title : $entry_content_unescaped<br>";
992 print_r($entry_tags);
993 print "<br/></p>"; */
eb36b4eb
AD
994
995 $entry_tags = $entry_tags[1];
996
073ca0e6
AD
997 # check for manual tags
998
f8382011
AD
999 $tag_filter = find_article_filter($article_filters, "tag");
1000
1001 if ($tag_filter) {
073ca0e6 1002
f8382011 1003 $manual_tags = trim_array(split(",", $tag_filter[1]));
073ca0e6 1004
f8382011 1005 foreach ($manual_tags as $tag) {
be832a1a
AD
1006 if (tag_is_valid($tag)) {
1007 array_push($entry_tags, $tag);
1008 }
1009 }
1010 }
1011
11c9ea1f
AD
1012 $boring_tags = trim_array(split(",", mb_strtolower(get_pref($link,
1013 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8')));
8fc70781 1014
fefef828
AD
1015 if ($additional_tags && is_array($additional_tags)) {
1016 foreach ($additional_tags as $tag) {
156a785d
AD
1017 if (tag_is_valid($tag) &&
1018 array_search($tag, $boring_tags) === FALSE) {
073ca0e6
AD
1019 array_push($entry_tags, $tag);
1020 }
1021 }
fefef828
AD
1022 }
1023
fcc95e24 1024// print "<p>TAGS: "; print_r($entry_tags); print "</p>";
073ca0e6 1025
9fdf7824
AD
1026 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1027 print_r($entry_tags);
1028 }
1029
eb36b4eb
AD
1030 if (count($entry_tags) > 0) {
1031
44e241cb
AD
1032 db_query($link, "BEGIN");
1033
05732aa0
AD
1034 $result = db_query($link, "SELECT id,int_id
1035 FROM ttrss_entries,ttrss_user_entries
25da6909 1036 WHERE guid = '$entry_guid'
05732aa0 1037 AND feed_id = '$feed' AND ref_id = id
7fed1940 1038 AND owner_uid = '$owner_uid'");
eb36b4eb 1039
fe99ab12 1040 if (db_num_rows($result) == 1) {
eb36b4eb 1041
fe99ab12
AD
1042 $entry_id = db_fetch_result($result, 0, "id");
1043 $entry_int_id = db_fetch_result($result, 0, "int_id");
1044
1045 foreach ($entry_tags as $tag) {
fefef828 1046
14b6c54b 1047 $tag = sanitize_tag($tag);
fefef828 1048 $tag = db_escape_string($tag);
31483fc1 1049
ef063748
AD
1050 if (!tag_is_valid($tag)) continue;
1051
fe99ab12
AD
1052 $result = db_query($link, "SELECT id FROM ttrss_tags
1053 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
1054 owner_uid = '$owner_uid' LIMIT 1");
1055
1056 // print db_fetch_result($result, 0, "id");
1057
1058 if ($result && db_num_rows($result) == 0) {
1059
fe99ab12
AD
1060 db_query($link, "INSERT INTO ttrss_tags
1061 (owner_uid,tag_name,post_int_id)
1062 VALUES ('$owner_uid','$tag', '$entry_int_id')");
1063 }
1064 }
eb36b4eb 1065 }
44e241cb 1066 db_query($link, "COMMIT");
05732aa0 1067 }
9fdf7824
AD
1068
1069 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1070 _debug("update_rss_feed: article processed");
1071 }
4c193675 1072 }
40d13c28 1073
ab3d0b99
AD
1074 db_query($link, "UPDATE ttrss_feeds
1075 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
eb36b4eb 1076
44e241cb 1077// db_query($link, "COMMIT");
dd8c76a9 1078
ab3d0b99 1079 } else {
ca872b9d 1080
9fdf7824 1081 if (ENABLE_SIMPLEPIE) {
ca872b9d 1082 $error_msg = mb_substr($rss->error(), 0, 250);
9fdf7824 1083 } else {
ca872b9d 1084 $error_msg = mb_substr(magpie_error(), 0, 250);
9fdf7824 1085 }
ca872b9d
AD
1086
1087 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1088 _debug("update_rss_feed: error fetching feed: $error_msg");
1089 }
1090
1091 $error_msg = db_escape_string($error_msg);
1092
ab3d0b99 1093 db_query($link,
aa5f9f5f
AD
1094 "UPDATE ttrss_feeds SET last_error = '$error_msg',
1095 last_updated = NOW() WHERE id = '$feed'");
40d13c28
AD
1096 }
1097
9fdf7824 1098 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb 1099 _debug("update_rss_feed: done");
219bd8fc
AD
1100 }
1101
40d13c28
AD
1102 }
1103
f175937c 1104 function print_select($id, $default, $values, $attributes = "") {
79f3553b 1105 print "<select name=\"$id\" id=\"$id\" $attributes>";
a0d53889
AD
1106 foreach ($values as $v) {
1107 if ($v == $default)
1108 $sel = " selected";
1109 else
1110 $sel = "";
1111
1112 print "<option$sel>$v</option>";
1113 }
1114 print "</select>";
1115 }
40d13c28 1116
79f3553b
AD
1117 function print_select_hash($id, $default, $values, $attributes = "") {
1118 print "<select name=\"$id\" id='$id' $attributes>";
673d54ca
AD
1119 foreach (array_keys($values) as $v) {
1120 if ($v == $default)
1121 $sel = "selected";
1122 else
1123 $sel = "";
1124
1125 print "<option $sel value=\"$v\">".$values[$v]."</option>";
1126 }
1127
1128 print "</select>";
1129 }
1130
f8382011 1131 function get_article_filters($filters, $title, $content, $link) {
240054f1 1132 $matches = array();
c2d9322b 1133
240054f1
AD
1134 if ($filters["title"]) {
1135 foreach ($filters["title"] as $filter) {
c2d9322b
AD
1136 $reg_exp = $filter["reg_exp"];
1137 $inverse = $filter["inverse"];
1138 if ((!$inverse && preg_match("/$reg_exp/i", $title)) ||
1139 ($inverse && !preg_match("/$reg_exp/i", $title))) {
1140
240054f1
AD
1141 array_push($matches, array($filter["action"], $filter["action_param"]));
1142 }
1143 }
1144 }
1145
1146 if ($filters["content"]) {
1147 foreach ($filters["content"] as $filter) {
c2d9322b
AD
1148 $reg_exp = $filter["reg_exp"];
1149 $inverse = $filter["inverse"];
1150
1151 if ((!$inverse && preg_match("/$reg_exp/i", $content)) ||
1152 ($inverse && !preg_match("/$reg_exp/i", $content))) {
1153
240054f1
AD
1154 array_push($matches, array($filter["action"], $filter["action_param"]));
1155 }
1156 }
1157 }
1158
1159 if ($filters["both"]) {
1160 foreach ($filters["both"] as $filter) {
1161 $reg_exp = $filter["reg_exp"];
c2d9322b
AD
1162 $inverse = $filter["inverse"];
1163
1164 if ($inverse) {
1165 if (!preg_match("/$reg_exp/i", $title) || !preg_match("/$reg_exp/i", $content)) {
1166 array_push($matches, array($filter["action"], $filter["action_param"]));
1167 }
1168 } else {
1169 if (preg_match("/$reg_exp/i", $title) || preg_match("/$reg_exp/i", $content)) {
1170 array_push($matches, array($filter["action"], $filter["action_param"]));
1171 }
240054f1
AD
1172 }
1173 }
1174 }
1175
1176 if ($filters["link"]) {
1177 $reg_exp = $filter["reg_exp"];
1178 foreach ($filters["link"] as $filter) {
1179 $reg_exp = $filter["reg_exp"];
c2d9322b
AD
1180 $inverse = $filter["inverse"];
1181
1182 if ((!$inverse && preg_match("/$reg_exp/i", $link)) ||
1183 ($inverse && !preg_match("/$reg_exp/i", $link))) {
1184
240054f1
AD
1185 array_push($matches, array($filter["action"], $filter["action_param"]));
1186 }
1187 }
1188 }
1189
1190 return $matches;
1191 }
1192
f8382011
AD
1193 function find_article_filter($filters, $filter_name) {
1194 foreach ($filters as $f) {
1195 if ($f[0] == $filter_name) {
1196 return $f;
1197 };
1198 }
1199 return false;
1200 }
1201
9323147e 1202 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
fb1fb4ab 1203 $rtl_content = false, $last_updated = false, $last_error = false) {
254e0e4b
AD
1204
1205 if (file_exists($icon_file) && filesize($icon_file) > 0) {
023fe037 1206 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
254e0e4b 1207 } else {
023fe037 1208 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
254e0e4b
AD
1209 }
1210
9323147e
AD
1211 if ($rtl_content) {
1212 $rtl_tag = "dir=\"rtl\"";
1213 } else {
1214 $rtl_tag = "dir=\"ltr\"";
1215 }
1216
78d5212c
AD
1217 $error_notify_msg = "";
1218
fb1fb4ab
AD
1219 if ($last_error) {
1220 $link_title = "Error: $last_error ($last_updated)";
78d5212c 1221 $error_notify_msg = "(Error)";
ad780e9c 1222 } else if ($last_updated) {
fb1fb4ab
AD
1223 $link_title = "Updated: $last_updated";
1224 }
1225
7210613a 1226 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
c50e2b30 1227 href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
254e0e4b
AD
1228
1229 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
b619ff15 1230 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
254e0e4b
AD
1231 print "$feed_icon";
1232 }
1233
9323147e 1234 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
254e0e4b
AD
1235
1236 if ($unread != 0) {
1237 $fctr_class = "";
1238 } else {
1239 $fctr_class = "class=\"invisible\"";
1240 }
1241
9323147e 1242 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
254e0e4b 1243 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
78d5212c
AD
1244
1245 if (get_pref($link, "EXTENDED_FEEDLIST")) {
1246 print "<div class=\"feedExtInfo\">
1247 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
1248 }
1249
254e0e4b
AD
1250 print "</li>";
1251
1252 }
1253
406d9489
AD
1254 function getmicrotime() {
1255 list($usec, $sec) = explode(" ",microtime());
1256 return ((float)$usec + (float)$sec);
1257 }
1258
f541eb78 1259 function print_radio($id, $default, $true_is, $values, $attributes = "") {
77e96719
AD
1260 foreach ($values as $v) {
1261
1262 if ($v == $default)
5da169d9 1263 $sel = "checked";
77e96719 1264 else
5da169d9
AD
1265 $sel = "";
1266
f541eb78 1267 if ($v == $true_is) {
5da169d9
AD
1268 $sel .= " value=\"1\"";
1269 } else {
1270 $sel .= " value=\"0\"";
1271 }
77e96719 1272
69654950
AD
1273 print "<input class=\"noborder\"
1274 type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
77e96719
AD
1275
1276 }
1277 }
1278
ff485f1d
AD
1279 function initialize_user_prefs($link, $uid) {
1280
1281 $uid = db_escape_string($uid);
1282
1283 db_query($link, "BEGIN");
1284
1285 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
1286
1287 $u_result = db_query($link, "SELECT pref_name
1288 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
1289
1290 $active_prefs = array();
1291
1292 while ($line = db_fetch_assoc($u_result)) {
1293 array_push($active_prefs, $line["pref_name"]);
1294 }
1295
1296 while ($line = db_fetch_assoc($result)) {
1297 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
1298// print "adding " . $line["pref_name"] . "<br>";
1299
1300 db_query($link, "INSERT INTO ttrss_user_prefs
1301 (owner_uid,pref_name,value) VALUES
1302 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
1303
1304 }
1305 }
1306
1307 db_query($link, "COMMIT");
1308
1309 }
956c7629
AD
1310
1311 function lookup_user_id($link, $user) {
1312
1313 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1314 login = '$login'");
1315
1316 if (db_num_rows($result) == 1) {
1317 return db_fetch_result($result, 0, "id");
1318 } else {
1319 return false;
1320 }
1321 }
1322
18664970
AD
1323 function http_authenticate_user($link) {
1324
4bc64807
AD
1325 error_log("http_authenticate_user: ".$_SERVER["PHP_AUTH_USER"]."\n", 3, '/tmp/tt-rss.log');
1326
18664970
AD
1327 if (!$_SERVER["PHP_AUTH_USER"]) {
1328
1329 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1330 header('HTTP/1.0 401 Unauthorized');
1331 exit;
1332
1333 } else {
1334 $auth_result = authenticate_user($link,
1335 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1336
1337 if (!$auth_result) {
1338 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1339 header('HTTP/1.0 401 Unauthorized');
1340 exit;
1341 }
1342 }
1343
1344 return true;
1345 }
1346
461766f3 1347 function authenticate_user($link, $login, $password, $force_auth = false) {
c8437f35 1348
131b01b3 1349 if (!SINGLE_USER_MODE) {
c8437f35 1350
131b01b3 1351 $pwd_hash = 'SHA1:' . sha1($password);
461766f3
AD
1352
1353 if ($force_auth && defined('_DEBUG_USER_SWITCH')) {
1354 $query = "SELECT id,login,access_level
1355 FROM ttrss_users WHERE
1356 login = '$login'";
1357 } else {
1358 $query = "SELECT id,login,access_level
1359 FROM ttrss_users WHERE
1360 login = '$login' AND pwd_hash = '$pwd_hash'";
1361 }
1362
1363 $result = db_query($link, $query);
131b01b3
AD
1364
1365 if (db_num_rows($result) == 1) {
1366 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1367 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1368 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1369
1370 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1371 $_SESSION["uid"]);
1372
1373 $user_theme = get_user_theme_path($link);
1374
1375 $_SESSION["theme"] = $user_theme;
1376 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1377
1378 initialize_user_prefs($link, $_SESSION["uid"]);
1379
1380 return true;
1381 }
1382
1383 return false;
503eb349 1384
131b01b3 1385 } else {
503eb349 1386
131b01b3
AD
1387 $_SESSION["uid"] = 1;
1388 $_SESSION["name"] = "admin";
f557cd78 1389
0bbba72d
AD
1390 $user_theme = get_user_theme_path($link);
1391
1392 $_SESSION["theme"] = $user_theme;
1393 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1394
1395 initialize_user_prefs($link, $_SESSION["uid"]);
1396
c8437f35
AD
1397 return true;
1398 }
c8437f35
AD
1399 }
1400
e6cb77a0
AD
1401 function make_password($length = 8) {
1402
1403 $password = "";
798f722b
AD
1404 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1405
1406 $i = 0;
e6cb77a0
AD
1407
1408 while ($i < $length) {
1409 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1410
1411 if (!strstr($password, $char)) {
1412 $password .= $char;
1413 $i++;
1414 }
1415 }
1416 return $password;
1417 }
1418
1419 // this is called after user is created to initialize default feeds, labels
1420 // or whatever else
1421
1422 // user preferences are checked on every login, not here
1423
1424 function initialize_user($link, $uid) {
1425
1426 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1427 values ('$uid','unread = true', 'Unread articles')");
1428
1429 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1430 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1431
1432 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
74bff337 1433 values ('$uid', 'Tiny Tiny RSS: New Releases',
628fcd2c 1434 'http://tt-rss.spb.ru/releases.rss')");
3b0feb9b 1435
cd2cd415
AD
1436 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1437 values ('$uid', 'Tiny Tiny RSS: Forum',
1438 'http://tt-rss.spb.ru/forum/rss.php')");
3b0feb9b 1439 }
e6cb77a0 1440
b8aa49bc 1441 function logout_user() {
5ccc1cf5
AD
1442 session_destroy();
1443 if (isset($_COOKIE[session_name()])) {
1444 setcookie(session_name(), '', time()-42000, '/');
1445 }
b8aa49bc
AD
1446 }
1447
75836f33 1448 function get_script_urlpath() {
87a79fa4 1449 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
75836f33
AD
1450 }
1451
916f788a 1452 function validate_session($link) {
a2e9b457 1453 if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) {
916f788a
AD
1454 if ($_SESSION["ip_address"]) {
1455 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
7f0acba7 1456 $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)";
916f788a
AD
1457 return false;
1458 }
1459 }
1460 }
d620cfe7 1461
a885f0ec 1462/* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
d620cfe7 1463
8e849206 1464 //print_r($_SESSION);
d620cfe7
AD
1465
1466 if (time() > $_SESSION["cookie_lifetime"]) {
1467 return false;
1468 }
a885f0ec
AD
1469 } */
1470
916f788a
AD
1471 return true;
1472 }
1473
793185a9 1474 function login_sequence($link, $mobile = false) {
b8aa49bc 1475 if (!SINGLE_USER_MODE) {
75836f33 1476
461766f3
AD
1477 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1478 $swu = db_escape_string($_REQUEST["swu"]);
1479 if ($swu) {
1480 $_SESSION["prefs_cache"] = false;
1481 return authenticate_user($link, $swu, null, true);
1482 }
1483 }
1484
7f0acba7 1485 $login_action = $_POST["login_action"];
a885f0ec 1486
01a87dff 1487 # try to authenticate user if called from login form
7f0acba7 1488 if ($login_action == "do_login") {
01a87dff
AD
1489 $login = $_POST["login"];
1490 $password = $_POST["password"];
d620cfe7 1491 $remember_me = $_POST["remember_me"];
f557cd78 1492
01a87dff
AD
1493 if (authenticate_user($link, $login, $password)) {
1494 $_POST["password"] = "";
d620cfe7 1495
f8c612d4
AD
1496 $_SESSION["language"] = $_POST["language"];
1497
d620cfe7
AD
1498 header("Location: " . $_SERVER["REQUEST_URI"]);
1499 exit;
1500
01a87dff 1501 return;
7f0acba7
AD
1502 } else {
1503 $_SESSION["login_error_msg"] = "Incorrect username or password";
01a87dff
AD
1504 }
1505 }
1506
1df0f48b
AD
1507// print session_id();
1508// print_r($_SESSION);
7f0acba7
AD
1509
1510 if (!$_SESSION["uid"] || !validate_session($link)) {
793185a9 1511 render_login_form($link, $mobile);
01a87dff 1512 exit;
d3687e7a
AD
1513 } else {
1514 /* bump login timestamp */
1515 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1516 $_SESSION["uid"]);
019bd5a9 1517
d54780bc 1518 if ($_SESSION["language"] && SESSION_COOKIE_LIFETIME > 0) {
019bd5a9
AD
1519 setcookie("ttrss_lang", $_SESSION["language"],
1520 time() + SESSION_COOKIE_LIFETIME);
1521 }
b8aa49bc 1522 }
d620cfe7 1523
b8aa49bc 1524 } else {
0bbba72d 1525 return authenticate_user($link, "admin", null);
b8aa49bc
AD
1526 }
1527 }
3547842a
AD
1528
1529 function truncate_string($str, $max_len) {
12db369c
AD
1530 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1531 return mb_substr($str, 0, $max_len, "utf-8") . "...";
3547842a
AD
1532 } else {
1533 return $str;
1534 }
1535 }
54a60e1a
AD
1536
1537 function get_user_theme_path($link) {
798f722b
AD
1538 $result = db_query($link, "SELECT theme_path
1539 FROM
1540 ttrss_themes,ttrss_users
1541 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
54a60e1a
AD
1542 if (db_num_rows($result) != 0) {
1543 return db_fetch_result($result, 0, "theme_path");
1544 } else {
1545 return null;
1546 }
1547 }
be773442
AD
1548
1549 function smart_date_time($timestamp) {
1550 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1551 return date("G:i", $timestamp);
f26450f1 1552 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
1553 return date("M d, G:i", $timestamp);
1554 } else {
7d7e0509 1555 return date("Y/m/d, G:i", $timestamp);
be773442
AD
1556 }
1557 }
1558
1559 function smart_date($timestamp) {
1560 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1561 return "Today";
f26450f1 1562 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
1563 return date("D m", $timestamp);
1564 } else {
b02111c2 1565 return date("Y/m/d", $timestamp);
be773442
AD
1566 }
1567 }
a654a595
AD
1568
1569 function sql_bool_to_string($s) {
1570 if ($s == "t" || $s == "1") {
1571 return "true";
1572 } else {
1573 return "false";
1574 }
1575 }
e3c99f3b
AD
1576
1577 function sql_bool_to_bool($s) {
1578 if ($s == "t" || $s == "1") {
1579 return true;
1580 } else {
1581 return false;
1582 }
1583 }
0ea4fb50 1584
e3c99f3b 1585
0ea4fb50
AD
1586 function toggleEvenOdd($a) {
1587 if ($a == "even")
1588 return "odd";
1589 else
1590 return "even";
1591 }
6043fb7e
AD
1592
1593 function sanity_check($link) {
9cbca41f 1594
aec3ce39
AD
1595 error_reporting(0);
1596
6043fb7e
AD
1597 $error_code = 0;
1598 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1599 $schema_version = db_fetch_result($result, 0, "schema_version");
1600
1601 if ($schema_version != SCHEMA_VERSION) {
1602 $error_code = 5;
1603 }
1604
aec3ce39
AD
1605 if (DB_TYPE == "mysql") {
1606 $result = db_query($link, "SELECT true", false);
1607 if (db_num_rows($result) != 1) {
1608 $error_code = 10;
1609 }
1610 }
1611
1612 error_reporting (DEFAULT_ERROR_LEVEL);
1613
6043fb7e 1614 if ($error_code != 0) {
aec3ce39 1615 print_error_xml($error_code);
6043fb7e
AD
1616 return false;
1617 } else {
1618 return true;
4220d6b0 1619 }
6043fb7e
AD
1620 }
1621
27981ca3
AD
1622 function file_is_locked($filename) {
1623 error_reporting(0);
1624 $fp = fopen($filename, "r");
1625 error_reporting(DEFAULT_ERROR_LEVEL);
1626 if ($fp) {
1627 if (flock($fp, LOCK_EX | LOCK_NB)) {
1628 flock($fp, LOCK_UN);
1629 fclose($fp);
1630 return false;
1631 }
1632 fclose($fp);
1633 return true;
1634 }
1635 return false;
1636 }
1637
fcb4c0c9
AD
1638 function make_lockfile($filename) {
1639 $fp = fopen($filename, "w");
1640
1641 if (flock($fp, LOCK_EX | LOCK_NB)) {
1642 return $fp;
1643 } else {
1644 return false;
1645 }
1646 }
1647
bf7fcde8
AD
1648 function make_stampfile($filename) {
1649 $fp = fopen($filename, "w");
1650
8e00ae9b 1651 if (flock($fp, LOCK_EX | LOCK_NB)) {
bf7fcde8 1652 fwrite($fp, time() . "\n");
8e00ae9b 1653 flock($fp, LOCK_UN);
bf7fcde8
AD
1654 fclose($fp);
1655 return true;
1656 } else {
1657 return false;
1658 }
1659 }
1660
8e00ae9b
AD
1661 function read_stampfile($filename) {
1662
1663 error_reporting(0);
1664 $fp = fopen($filename, "r");
1665 error_reporting (DEFAULT_ERROR_LEVEL);
1666
1667 if (flock($fp, LOCK_EX)) {
1668 $stamp = fgets($fp);
1669 flock($fp, LOCK_UN);
1670 fclose($fp);
1671 return $stamp;
1672 } else {
1673 return false;
1674 }
1675 }
bf7fcde8 1676
894ebcf5
AD
1677 function sql_random_function() {
1678 if (DB_TYPE == "mysql") {
1679 return "RAND()";
1680 } else {
1681 return "RANDOM()";
1682 }
1683 }
1684
23aa0d16 1685 function catchup_feed($link, $feed, $cat_view) {
88040f57
AD
1686
1687 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
23aa0d16
AD
1688
1689 if ($cat_view) {
1690
1691 if ($feed > 0) {
1692 $cat_qpart = "cat_id = '$feed'";
1693 } else {
1694 $cat_qpart = "cat_id IS NULL";
1695 }
1696
1697 $tmp_result = db_query($link, "SELECT id
1698 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1699 $_SESSION["uid"]);
1700
1701 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1702
1703 $tmp_feed = $tmp_line["id"];
1704
1705 db_query($link, "UPDATE ttrss_user_entries
1706 SET unread = false,last_read = NOW()
1707 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1708 }
1709
1710 } else if ($feed > 0) {
1711
1712 $tmp_result = db_query($link, "SELECT id
1713 FROM ttrss_feeds WHERE parent_feed = '$feed'
1714 ORDER BY cat_id,title");
1715
1716 $parent_ids = array();
1717
1718 if (db_num_rows($tmp_result) > 0) {
1719 while ($p = db_fetch_assoc($tmp_result)) {
1720 array_push($parent_ids, "feed_id = " . $p["id"]);
1721 }
1722
1723 $children_qpart = implode(" OR ", $parent_ids);
1724
1725 db_query($link, "UPDATE ttrss_user_entries
1726 SET unread = false,last_read = NOW()
1727 WHERE (feed_id = '$feed' OR $children_qpart)
1728 AND owner_uid = " . $_SESSION["uid"]);
1729
1730 } else {
1731 db_query($link, "UPDATE ttrss_user_entries
1732 SET unread = false,last_read = NOW()
1733 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1734 }
1735
1736 } else if ($feed < 0 && $feed > -10) { // special, like starred
1737
1738 if ($feed == -1) {
1739 db_query($link, "UPDATE ttrss_user_entries
1740 SET unread = false,last_read = NOW()
1741 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1742 }
e4f4b46f
AD
1743
1744 if ($feed == -2) {
1745 db_query($link, "UPDATE ttrss_user_entries
1746 SET unread = false,last_read = NOW()
1747 WHERE published = true AND owner_uid = ".$_SESSION["uid"]);
1748 }
1749
2d24f032
AD
1750 if ($feed == -3) {
1751
c1d7e6c3
AD
1752 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
1753
2d24f032 1754 if (DB_TYPE == "pgsql") {
c1d7e6c3 1755 $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
2d24f032
AD
1756 } else {
1757 $match_part .= " AND date_entered > DATE_SUB(NOW(),
c1d7e6c3 1758 INTERVAL $intl HOUR) ";
2d24f032
AD
1759 }
1760
1761 db_query($link, "UPDATE ttrss_user_entries
1762 SET unread = false,last_read = NOW()
1763 WHERE $match_part AND owner_uid = ".$_SESSION["uid"]);
1764 }
1765
23aa0d16
AD
1766 } else if ($feed < -10) { // label
1767
1768 // TODO make this more efficient
1769
1770 $label_id = -$feed - 11;
1771
1772 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1773 WHERE id = '$label_id'");
1774
1775 if ($tmp_result) {
1776 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1777
1778 db_query($link, "BEGIN");
1779
1780 $tmp2_result = db_query($link,
1781 "SELECT
1782 int_id
1783 FROM
88040f57 1784 ttrss_user_entries,ttrss_entries,ttrss_feeds
23aa0d16 1785 WHERE
88040f57
AD
1786 ref_id = ttrss_entries.id AND
1787 ttrss_user_entries.feed_id = ttrss_feeds.id AND
23aa0d16 1788 $sql_exp AND
88040f57 1789 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
23aa0d16
AD
1790
1791 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1792 db_query($link, "UPDATE
1793 ttrss_user_entries
1794 SET
1795 unread = false, last_read = NOW()
1796 WHERE
1797 int_id = " . $tmp_line["int_id"]);
1798 }
1799
1800 db_query($link, "COMMIT");
1801
1802/* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1803 SET unread = false,last_read = NOW()
1804 WHERE $sql_exp
1805 AND ref_id = id
1806 AND owner_uid = ".$_SESSION["uid"]); */
1807 }
1808 }
1809 } else { // tag
1810 db_query($link, "BEGIN");
1811
1812 $tag_name = db_escape_string($feed);
1813
1814 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1815 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1816
1817 while ($line = db_fetch_assoc($result)) {
1818 db_query($link, "UPDATE ttrss_user_entries SET
1819 unread = false, last_read = NOW()
1820 WHERE int_id = " . $line["post_int_id"]);
1821 }
1822 db_query($link, "COMMIT");
1823 }
1824 }
1825
1826 function update_generic_feed($link, $feed, $cat_view) {
1827 if ($cat_view) {
1828
1829 if ($feed > 0) {
1830 $cat_qpart = "cat_id = '$feed'";
1831 } else {
1832 $cat_qpart = "cat_id IS NULL";
1833 }
1834
1835 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1836 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1837
1838 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1839 $feed_url = $tmp_line["feed_url"];
1840 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1841 }
1842
1843 } else {
1844 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1845 WHERE id = '$feed'");
1846 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1847 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1848 }
1849 }
a9cb1f83 1850
2bc2147f 1851 function getAllCounters($link, $omode = "flc") {
cf4d339c 1852/* getLabelCounters($link);
a9cb1f83
AD
1853 getFeedCounters($link);
1854 getTagCounters($link);
1855 getGlobalCounters($link);
1856 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1857 getCategoryCounters($link);
cf4d339c
AD
1858 } */
1859
2bc2147f 1860 if (!$omode) $omode = "flc";
cf4d339c
AD
1861
1862 getGlobalCounters($link);
1863
1864 if (strchr($omode, "l")) getLabelCounters($link);
1865 if (strchr($omode, "f")) getFeedCounters($link);
1866 if (strchr($omode, "t")) getTagCounters($link);
1867 if (strchr($omode, "c")) {
1868 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1869 getCategoryCounters($link);
1870 }
a9cb1f83
AD
1871 }
1872 }
1873
1874 function getCategoryCounters($link) {
bba7c4bf
AD
1875 # two special categories are -1 and -2 (all virtuals; all labels)
1876
1877 $ctr = getCategoryUnread($link, -1);
1878
1879 print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>";
1880
1881 $ctr = getCategoryUnread($link, -2);
1882
1883 print "<counter type=\"category\" id=\"-2\" counter=\"$ctr\"/>";
1884
a9cb1f83
AD
1885 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1886 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1887 AND unread = true)) AS unread FROM ttrss_feeds
1888 WHERE
cfb02131 1889 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
a9cb1f83
AD
1890
1891 while ($line = db_fetch_assoc($result)) {
1892 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1893 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1894 $line["unread"]."\"/>";
1895 }
1896 }
1897
f295c368
AD
1898 function getCategoryUnread($link, $cat) {
1899
bba7c4bf 1900 if ($cat >= 0) {
18664970 1901
bba7c4bf
AD
1902 if ($cat != 0) {
1903 $cat_query = "cat_id = '$cat'";
1904 } else {
1905 $cat_query = "cat_id IS NULL";
1906 }
1907
1908 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
1909 AND hidden = false
1910 AND owner_uid = " . $_SESSION["uid"]);
1911
1912 $cat_feeds = array();
1913 while ($line = db_fetch_assoc($result)) {
1914 array_push($cat_feeds, "feed_id = " . $line["id"]);
1915 }
1916
1917 if (count($cat_feeds) == 0) return 0;
1918
1919 $match_part = implode(" OR ", $cat_feeds);
1920
1921 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1922 FROM ttrss_user_entries
1923 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1924
1925 $unread = 0;
1926
1927 # this needs to be rewritten
1928 while ($line = db_fetch_assoc($result)) {
1929 $unread += $line["unread"];
1930 }
1931
1932 return $unread;
1933 } else if ($cat == -1) {
2d24f032 1934 return getFeedUnread($link, -1) + getFeedUnread($link, -2) + getFeedUnread($link, -3);
bba7c4bf 1935 } else if ($cat == -2) {
f295c368 1936
bba7c4bf
AD
1937 $rv = getLabelCounters($link, false, true);
1938 $ctr = 0;
f295c368 1939
bba7c4bf
AD
1940 foreach (array_keys($rv) as $k) {
1941 if ($k < -10) {
1942 $ctr += $rv[$k]["counter"];
1943 }
1944 }
f295c368 1945
bba7c4bf 1946 return $ctr;
f295c368 1947 }
f295c368
AD
1948 }
1949
1950 function getFeedUnread($link, $feed, $is_cat = false) {
a9cb1f83 1951 $n_feed = sprintf("%d", $feed);
f295c368
AD
1952
1953 if ($is_cat) {
831ff047 1954 return getCategoryUnread($link, $n_feed);
f295c368 1955 } else if ($n_feed == -1) {
a9cb1f83 1956 $match_part = "marked = true";
e4f4b46f
AD
1957 } else if ($n_feed == -2) {
1958 $match_part = "published = true";
2d24f032
AD
1959 } else if ($n_feed == -3) {
1960 $match_part = "unread = true";
1961
c1d7e6c3
AD
1962 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
1963
2d24f032 1964 if (DB_TYPE == "pgsql") {
c1d7e6c3 1965 $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
2d24f032 1966 } else {
c1d7e6c3 1967 $match_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2d24f032
AD
1968 }
1969
4919fb42 1970 } else if ($n_feed > 0) {
831ff047 1971
e8b8485f
AD
1972 $result = db_query($link, "SELECT id FROM ttrss_feeds
1973 WHERE parent_feed = '$n_feed'
318260cc 1974 AND hidden = false
4919fb42 1975 AND owner_uid = " . $_SESSION["uid"]);
831ff047
AD
1976
1977 if (db_num_rows($result) > 0) {
4919fb42 1978
831ff047
AD
1979 $linked_feeds = array();
1980 while ($line = db_fetch_assoc($result)) {
1981 array_push($linked_feeds, "feed_id = " . $line["id"]);
1982 }
e8b8485f
AD
1983
1984 array_push($linked_feeds, "feed_id = $n_feed");
831ff047
AD
1985
1986 $match_part = implode(" OR ", $linked_feeds);
1987
4919fb42 1988 $result = db_query($link, "SELECT COUNT(int_id) AS unread
318260cc 1989 FROM ttrss_user_entries
e8b8485f
AD
1990 WHERE unread = true AND ($match_part)
1991 AND owner_uid = " . $_SESSION["uid"]);
4919fb42
AD
1992
1993 $unread = 0;
1994
1995 # this needs to be rewritten
1996 while ($line = db_fetch_assoc($result)) {
1997 $unread += $line["unread"];
1998 }
1999
2000 return $unread;
2001
831ff047
AD
2002 } else {
2003 $match_part = "feed_id = '$n_feed'";
2004 }
a9cb1f83 2005 } else if ($feed < -10) {
318260cc 2006
a9cb1f83
AD
2007 $label_id = -$feed - 11;
2008
2009 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
2010 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
2011
2012 $match_part = db_fetch_result($result, 0, "sql_exp");
2013 }
2014
2015 if ($match_part) {
2016
2017 $result = db_query($link, "SELECT count(int_id) AS unread
88040f57
AD
2018 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
2019 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2020 ttrss_user_entries.ref_id = ttrss_entries.id AND
cfb02131 2021 ttrss_feeds.hidden = false AND
88040f57 2022 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
a9cb1f83
AD
2023
2024 } else {
2025
2026 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
2027 FROM ttrss_tags,ttrss_user_entries
2028 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
2029 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
2030 }
2031
2032 $unread = db_fetch_result($result, 0, "unread");
cfb02131 2033
a9cb1f83
AD
2034 return $unread;
2035 }
2036
2037 /* FIXME this needs reworking */
2038
f3acc32e
AD
2039 function getGlobalUnread($link, $user_id = false) {
2040
2041 if (!$user_id) {
2042 $user_id = $_SESSION["uid"];
2043 }
2044
3831db41 2045 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
a9cb1f83 2046 WHERE unread = true AND
3831db41 2047 ttrss_user_entries.feed_id = ttrss_feeds.id AND
a9cb1f83 2048 ttrss_user_entries.ref_id = ttrss_entries.id AND
3831db41 2049 hidden = false AND
f3acc32e 2050 ttrss_user_entries.owner_uid = '$user_id'");
a9cb1f83
AD
2051 $c_id = db_fetch_result($result, 0, "c_id");
2052 return $c_id;
2053 }
2054
2055 function getGlobalCounters($link, $global_unread = -1) {
2056 if ($global_unread == -1) {
2057 $global_unread = getGlobalUnread($link);
2058 }
7bf7e4d3
AD
2059 print "<counter type=\"global\" id='global-unread'
2060 counter='$global_unread'/>";
2061
2062 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
2063 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
2064
2065 $subscribed_feeds = db_fetch_result($result, 0, "fn");
2066
2067 print "<counter type=\"global\" id='subscribed-feeds'
2068 counter='$subscribed_feeds'/>";
2069
a9cb1f83
AD
2070 }
2071
2072 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
2073
2074 if ($smart_mode) {
2075 if (!$_SESSION["tctr_last_value"]) {
2076 $_SESSION["tctr_last_value"] = array();
2077 }
2078 }
2079
2080 $old_counters = $_SESSION["tctr_last_value"];
2081
2082 $tctrs_modified = false;
2083
2084/* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
2085 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
2086 ttrss_user_entries.ref_id = ttrss_entries.id AND
2087 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
2088 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
2089 UNION
2090 select tag_name,0 as count FROM ttrss_tags
2091 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
2092
2093 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
2094 FROM ttrss_user_entries WHERE int_id = post_int_id
2095 AND unread = true)) AS count FROM ttrss_tags
ef1ac7c7
AD
2096 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
2097 ORDER BY count DESC LIMIT 55");
a9cb1f83
AD
2098
2099 $tags = array();
2100
2101 while ($line = db_fetch_assoc($result)) {
2102 $tags[$line["tag_name"]] += $line["count"];
2103 }
2104
2105 foreach (array_keys($tags) as $tag) {
2106 $unread = $tags[$tag];
2107
2108 $tag = htmlspecialchars($tag);
2109
2110 if (!$smart_mode || $old_counters[$tag] != $unread) {
2111 $old_counters[$tag] = $unread;
2112 $tctrs_modified = true;
2113 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
2114 }
2115
2116 }
2117
2118 if ($smart_mode && $tctrs_modified) {
2119 $_SESSION["tctr_last_value"] = $old_counters;
2120 }
2121
2122 }
2123
ef393de7 2124 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) {
a9cb1f83
AD
2125
2126 if ($smart_mode) {
2127 if (!$_SESSION["lctr_last_value"]) {
2128 $_SESSION["lctr_last_value"] = array();
2129 }
2130 }
2131
ef393de7
AD
2132 $ret_arr = array();
2133
a9cb1f83
AD
2134 $old_counters = $_SESSION["lctr_last_value"];
2135 $lctrs_modified = false;
2136
2d24f032 2137 $count = getFeedUnread($link, -1);
a9cb1f83 2138
ef393de7
AD
2139 if (!$ret_mode) {
2140 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
2141 } else {
2142 $ret_arr["-1"]["counter"] = $count;
bba7c4bf 2143 $ret_arr["-1"]["description"] = __("Starred articles");
ef393de7 2144 }
a9cb1f83 2145
2d24f032 2146 $count = getFeedUnread($link, -2);
e4f4b46f
AD
2147
2148 if (!$ret_mode) {
2149 print "<counter type=\"label\" id=\"-2\" counter=\"$count\"/>";
2150 } else {
2151 $ret_arr["-2"]["counter"] = $count;
bba7c4bf 2152 $ret_arr["-2"]["description"] = __("Published articles");
e4f4b46f
AD
2153 }
2154
2d24f032
AD
2155 $count = getFeedUnread($link, -3);
2156
2157 if (!$ret_mode) {
2158 print "<counter type=\"label\" id=\"-3\" counter=\"$count\"/>";
2159 } else {
2160 $ret_arr["-3"]["counter"] = $count;
2161 $ret_arr["-3"]["description"] = __("Fresh articles");
2162 }
2163
e4f4b46f 2164
a9cb1f83
AD
2165 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
2166 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
2167
2168 while ($line = db_fetch_assoc($result)) {
2169
2170 $id = -$line["id"] - 11;
2171
ef393de7
AD
2172 $label_name = $line["description"];
2173
a9cb1f83
AD
2174 error_reporting (0);
2175
88040f57 2176 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
a9cb1f83 2177 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
cfb02131 2178 ttrss_feeds.hidden = false AND
88040f57 2179 ttrss_user_entries.feed_id = ttrss_feeds.id AND
a9cb1f83 2180 ttrss_user_entries.ref_id = ttrss_entries.id AND
88040f57 2181 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
a9cb1f83
AD
2182
2183 $count = db_fetch_result($tmp_result, 0, "count");
2184
2185 if (!$smart_mode || $old_counters[$id] != $count) {
2186 $old_counters[$id] = $count;
2187 $lctrs_modified = true;
ef393de7
AD
2188 if (!$ret_mode) {
2189 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
2190 } else {
2191 $ret_arr[$id]["counter"] = $count;
2192 $ret_arr[$id]["description"] = $label_name;
2193 }
a9cb1f83
AD
2194 }
2195
2196 error_reporting (DEFAULT_ERROR_LEVEL);
2197 }
2198
2199 if ($smart_mode && $lctrs_modified) {
2200 $_SESSION["lctr_last_value"] = $old_counters;
2201 }
ef393de7
AD
2202
2203 return $ret_arr;
a9cb1f83
AD
2204 }
2205
2206/* function getFeedCounter($link, $id) {
2207
2208 $result = db_query($link, "SELECT
2209 count(id) as count,last_error
2210 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
2211 WHERE feed_id = '$id' AND unread = true
2212 AND ttrss_user_entries.feed_id = ttrss_feeds.id
2213 AND ttrss_user_entries.ref_id = ttrss_entries.id");
2214
2215 $count = db_fetch_result($result, 0, "count");
2216 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
2217
2218 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
2219 } */
2220
2221 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
2222
2223 if ($smart_mode) {
2224 if (!$_SESSION["fctr_last_value"]) {
2225 $_SESSION["fctr_last_value"] = array();
2226 }
2227 }
2228
2229 $old_counters = $_SESSION["fctr_last_value"];
2230
1b1b8a7b 2231/* $result = db_query($link, "SELECT id,last_error,parent_feed,
fb1fb4ab 2232 SUBSTRING(last_updated,1,19) AS last_updated,
a9cb1f83
AD
2233 (SELECT count(id)
2234 FROM ttrss_entries,ttrss_user_entries
2235 WHERE feed_id = ttrss_feeds.id AND
2236 ttrss_user_entries.ref_id = ttrss_entries.id
2237 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
2238 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1b1b8a7b
AD
2239 AND parent_feed IS NULL"); */
2240
2241 $result = db_query($link, "SELECT ttrss_feeds.id,
2242 SUBSTRING(ttrss_feeds.last_updated,1,19) AS last_updated,
2243 last_error,
bc60fda3 2244 COUNT(ttrss_entries.id) AS count
1b1b8a7b
AD
2245 FROM ttrss_feeds
2246 LEFT JOIN ttrss_user_entries ON (ttrss_user_entries.feed_id = ttrss_feeds.id
2247 AND ttrss_user_entries.owner_uid = ttrss_feeds.owner_uid
2248 AND ttrss_user_entries.unread = true)
2249 LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id)
2250 WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
2251 AND parent_feed IS NULL
2252 GROUP BY ttrss_feeds.id, ttrss_feeds.title, ttrss_feeds.last_updated, last_error");
a9cb1f83
AD
2253
2254 $fctrs_modified = false;
2255
fb1fb4ab
AD
2256 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2257
a9cb1f83
AD
2258 while ($line = db_fetch_assoc($result)) {
2259
2260 $id = $line["id"];
2261 $count = $line["count"];
2262 $last_error = htmlspecialchars($line["last_error"]);
fb1fb4ab
AD
2263
2264 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2265 $last_updated = smart_date_time(strtotime($line["last_updated"]));
2266 } else {
2267 $last_updated = date($short_date, strtotime($line["last_updated"]));
2268 }
2269
a9cb1f83
AD
2270 $has_img = is_file(ICONS_DIR . "/$id.ico");
2271
2272 $tmp_result = db_query($link,
2273 "SELECT id,COUNT(unread) AS unread
2274 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
2275 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
2276 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
2277
2278 if (db_num_rows($tmp_result) > 0) {
2279 while ($l = db_fetch_assoc($tmp_result)) {
2280 $count += $l["unread"];
2281 }
2282 }
2283
2284 if (!$smart_mode || $old_counters[$id] != $count) {
2285 $old_counters[$id] = $count;
2286 $fctrs_modified = true;
2287
2288 if ($last_error) {
2289 $error_part = "error=\"$last_error\"";
2290 } else {
2291 $error_part = "";
2292 }
2293
2294 if ($has_img) {
2295 $has_img_part = "hi=\"$has_img\"";
2296 } else {
2297 $has_img_part = "";
2298 }
2299
fb1fb4ab 2300 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
a9cb1f83
AD
2301 }
2302 }
2303
2304 if ($smart_mode && $fctrs_modified) {
2305 $_SESSION["fctr_last_value"] = $old_counters;
2306 }
2307 }
2308
1b758780 2309 function get_script_dt_add() {
34e420fb 2310 if (strpos(VERSION, ".99") === false) {
1b758780
AD
2311 return VERSION;
2312 } else {
2313 return time();
2314 }
2315 }
2316
6e7f8d26
AD
2317 function get_pgsql_version($link) {
2318 $result = db_query($link, "SELECT version() AS version");
2319 $version = split(" ", db_fetch_result($result, 0, "version"));
2320 return $version[1];
2321 }
2322
af106b0e
AD
2323 function print_error_xml($code, $add_msg = "") {
2324 global $ERRORS;
2325
2326 $error_msg = $ERRORS[$code];
2327
2328 if ($add_msg) {
2329 $error_msg = "$error_msg; $add_msg";
2330 }
2331
4c2abbc1 2332 print "<rpc-reply>";
af106b0e 2333 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
4c2abbc1 2334 print "</rpc-reply>";
af106b0e 2335 }
956c7629 2336
f27de515
AD
2337 function subscribe_to_feed($link, $feed_link, $cat_id = 0,
2338 $auth_login = '', $auth_pass = '') {
bb0f29a4 2339
b3dfe8ba 2340 # check for feed:http://url
c91c2249
AD
2341 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
2342
b3dfe8ba 2343 # check for feed://URL
e2d84cdb 2344 if (strpos($feed_link, "//") === 0) {
b3dfe8ba
AD
2345 $feed_link = "http:$feed_link";
2346 }
2347
c91c2249 2348 if ($feed_link == "") return;
bb0f29a4 2349
956c7629
AD
2350 if ($cat_id == "0" || !$cat_id) {
2351 $cat_qpart = "NULL";
2352 } else {
2353 $cat_qpart = "'$cat_id'";
2354 }
2355
2356 $result = db_query($link,
2357 "SELECT id FROM ttrss_feeds
2358 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
2359
2360 if (db_num_rows($result) == 0) {
2361
2362 $result = db_query($link,
f27de515
AD
2363 "INSERT INTO ttrss_feeds
2364 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass)
956c7629 2365 VALUES ('".$_SESSION["uid"]."', '$feed_link',
f27de515 2366 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')");
956c7629
AD
2367
2368 $result = db_query($link,
2369 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
f27de515 2370 AND owner_uid = " . $_SESSION["uid"]);
956c7629
AD
2371
2372 $feed_id = db_fetch_result($result, 0, "id");
2373
2374 if ($feed_id) {
2375 update_rss_feed($link, $feed_link, $feed_id, true);
2376 }
2377
2378 return true;
2379 } else {
2380 return false;
2381 }
2382 }
2383
673d54ca
AD
2384 function print_feed_select($link, $id, $default_id = "",
2385 $attributes = "", $include_all_feeds = true) {
2386
79f3553b 2387 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca 2388 if ($include_all_feeds) {
89cb787e 2389 print "<option value=\"0\">".__('All feeds')."</option>";
673d54ca
AD
2390 }
2391
2392 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
2393 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2394
2395 if (db_num_rows($result) > 0 && $include_all_feeds) {
2396 print "<option disabled>--------</option>";
2397 }
2398
2399 while ($line = db_fetch_assoc($result)) {
2400 if ($line["id"] == $default_id) {
2401 $is_selected = "selected";
2402 } else {
2403 $is_selected = "";
2404 }
79f3553b 2405 printf("<option $is_selected value='%d'>%s</option>",
47439031 2406 $line["id"], htmlspecialchars($line["title"]));
673d54ca
AD
2407 }
2408
2409 print "</select>";
2410 }
2411
2412 function print_feed_cat_select($link, $id, $default_id = "",
2413 $attributes = "", $include_all_cats = true) {
2414
79f3553b 2415 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca
AD
2416
2417 if ($include_all_cats) {
d1db26aa 2418 print "<option value=\"0\">".__('Uncategorized')."</option>";
673d54ca
AD
2419 }
2420
2421 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
2422 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2423
2424 if (db_num_rows($result) > 0 && $include_all_cats) {
2425 print "<option disabled>--------</option>";
2426 }
2427
2428 while ($line = db_fetch_assoc($result)) {
2429 if ($line["id"] == $default_id) {
2430 $is_selected = "selected";
2431 } else {
2432 $is_selected = "";
2433 }
14f69488 2434 printf("<option $is_selected value='%d'>%s</option>",
47439031 2435 $line["id"], htmlspecialchars($line["title"]));
673d54ca
AD
2436 }
2437
2438 print "</select>";
2439 }
2440
14f69488
AD
2441 function checkbox_to_sql_bool($val) {
2442 return ($val == "on") ? "true" : "false";
2443 }
86b682ce
AD
2444
2445 function getFeedCatTitle($link, $id) {
2446 if ($id == -1) {
d1db26aa 2447 return __("Special");
86b682ce 2448 } else if ($id < -10) {
d1db26aa 2449 return __("Labels");
86b682ce
AD
2450 } else if ($id > 0) {
2451 $result = db_query($link, "SELECT ttrss_feed_categories.title
2452 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
2453 cat_id = ttrss_feed_categories.id");
2454 if (db_num_rows($result) == 1) {
2455 return db_fetch_result($result, 0, "title");
2456 } else {
d1db26aa 2457 return __("Uncategorized");
86b682ce
AD
2458 }
2459 } else {
2460 return "getFeedCatTitle($id) failed";
2461 }
2462
2463 }
2464
2465 function getFeedTitle($link, $id) {
2466 if ($id == -1) {
d1db26aa 2467 return __("Starred articles");
945c243e
AD
2468 } else if ($id == -2) {
2469 return __("Published articles");
2d24f032
AD
2470 } else if ($id == -3) {
2471 return __("Fresh articles");
86b682ce
AD
2472 } else if ($id < -10) {
2473 $label_id = -10 - $id;
2474 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2475 if (db_num_rows($result) == 1) {
2476 return db_fetch_result($result, 0, "description");
2477 } else {
2478 return "Unknown label ($label_id)";
2479 }
2480
2481 } else if ($id > 0) {
2482 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2483 if (db_num_rows($result) == 1) {
2484 return db_fetch_result($result, 0, "title");
2485 } else {
2486 return "Unknown feed ($id)";
2487 }
2488 } else {
2489 return "getFeedTitle($id) failed";
2490 }
2491
2492 }
3dd46f19
AD
2493
2494 function get_session_cookie_name() {
2495 return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME);
2496 }
3ac2b520
AD
2497
2498 function print_init_params($link) {
2499 print "<init-params>";
2500 if ($_SESSION["stored-params"]) {
2501 foreach (array_keys($_SESSION["stored-params"]) as $key) {
5f57b06d
AD
2502 if ($key) {
2503 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2504 print "<param key=\"$key\" value=\"$value\"/>";
2505 }
3ac2b520
AD
2506 }
2507 }
2508
2509 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>";
2510 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>";
0d51e25d 2511 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY . "\"/>";
3ac2b520
AD
2512
2513 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2514 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2515
e8bd0da9 2516 print "<param key=\"hide_read_feeds\" value=\"" .
465ff90b 2517 (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>";
e8bd0da9 2518
c9268ed5 2519 print "<param key=\"feeds_sort_by_unread\" value=\"" .
465ff90b 2520 (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>";
c9268ed5 2521
f6d6e22f 2522 print "<param key=\"confirm_feed_catchup\" value=\"" .
465ff90b 2523 (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>";
f6d6e22f 2524
ac7bcd71 2525 print "<param key=\"cdm_auto_catchup\" value=\"" .
465ff90b 2526 (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>";
ac7bcd71 2527
8e9c121b
AD
2528 print "<param key=\"icons_url\" value=\"" . ICONS_URL . "\"/>";
2529
be0801a1
AD
2530 print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME . "\"/>";
2531
40496720
AD
2532 print "<param key=\"default_view_mode\" value=\"" .
2533 get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>";
2534
2535 print "<param key=\"default_view_limit\" value=\"" .
465ff90b 2536 (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>";
40496720 2537
fe8d2059
AD
2538 print "<param key=\"prefs_active_tab\" value=\"" .
2539 get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>";
2540
465ff90b
AD
2541 print "<param key=\"infobox_disable_overlay\" value=\"" .
2542 get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>";
2543
527c3bf0
AD
2544 print "<param key=\"icons_location\" value=\"" .
2545 ICONS_URL . "\"/>";
2546
3ac2b520
AD
2547 print "</init-params>";
2548 }
f54f515f
AD
2549
2550 function print_runtime_info($link) {
2551 print "<runtime-info>";
71ad883b
AD
2552 if (ENABLE_UPDATE_DAEMON) {
2553 print "<param key=\"daemon_is_running\" value=\"".
2554 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
8e00ae9b 2555
784b47a3 2556 if ($_SESSION["daemon_stamp_check"] + 600 < time()) {
8e00ae9b
AD
2557
2558 $stamp = (int)read_stampfile("update_daemon.stamp");
2559
2560 if ($stamp) {
2561 if ($stamp + 86400*3 < time()) {
2562 print "<param key=\"daemon_stamp_ok\" value=\"0\"/>";
2563 } else {
2564 print "<param key=\"daemon_stamp_ok\" value=\"1\"/>";
2565 }
2566
2567 $stamp_fmt = date("Y.m.d, G:i", $stamp);
2568
2569 print "<param key=\"daemon_stamp\" value=\"$stamp_fmt\"/>";
2570 }
2571
2572 $_SESSION["daemon_stamp_check"] = time();
2573 }
71ad883b 2574 }
8e00ae9b 2575
d9fa39f1
AD
2576 if (CHECK_FOR_NEW_VERSION && $_SESSION["access_level"] >= 10) {
2577
c04206e5 2578 if ($_SESSION["last_version_check"] + 7200 < time()) {
d9fa39f1
AD
2579 $new_version_details = check_for_update($link);
2580
2581 print "<param key=\"new_version_available\" value=\"".
2582 sprintf("%d", $new_version_details != ""). "\"/>";
2583
2584 $_SESSION["last_version_check"] = time();
2585 }
2586 }
2587
f54f515f
AD
2588 print "</runtime-info>";
2589 }
ef393de7 2590
88040f57 2591 function getSearchSql($search, $match_on) {
ef393de7 2592
88040f57 2593 $search_query_part = "";
e20c9d88 2594
88040f57
AD
2595 $keywords = split(" ", $search);
2596 $query_keywords = array();
e20c9d88 2597
88040f57 2598 if ($match_on == "both") {
e20c9d88 2599
88040f57
AD
2600 foreach ($keywords as $k) {
2601 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2602 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2603 }
e20c9d88 2604
88040f57 2605 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 2606
88040f57 2607 } else if ($match_on == "title") {
e20c9d88 2608
88040f57
AD
2609 foreach ($keywords as $k) {
2610 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2611 }
e20c9d88 2612
88040f57 2613 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 2614
88040f57
AD
2615 } else if ($match_on == "content") {
2616
2617 foreach ($keywords as $k) {
2618 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2619 }
2620 }
2621
2622 $search_query_part = implode("AND", $query_keywords);
2623
2624 return $search_query_part;
2625 }
2626
c36bf4d5
AD
2627 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) {
2628
2629 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
c1a0b534 2630
88040f57
AD
2631 if ($search) {
2632
2633 $search_query_part = getSearchSql($search, $match_on);
2634 $search_query_part .= " AND ";
e20c9d88 2635
ef393de7
AD
2636 } else {
2637 $search_query_part = "";
2638 }
2639
2640 $view_query_part = "";
2641
2642 if ($view_mode == "adaptive") {
2643 if ($search) {
2644 $view_query_part = " ";
2645 } else if ($feed != -1) {
f295c368 2646 $unread = getFeedUnread($link, $feed, $cat_view);
ef393de7
AD
2647 if ($unread > 0) {
2648 $view_query_part = " unread = true AND ";
2649 }
2650 }
2651 }
2652
2653 if ($view_mode == "marked") {
2654 $view_query_part = " marked = true AND ";
2655 }
2656
2657 if ($view_mode == "unread") {
2658 $view_query_part = " unread = true AND ";
2659 }
2660
2661 if ($limit > 0) {
2662 $limit_query_part = "LIMIT " . $limit;
2663 }
2664
2665 $vfeed_query_part = "";
2666
2667 // override query strategy and enable feed display when searching globally
2668 if ($search && $search_mode == "all_feeds") {
2669 $query_strategy_part = "ttrss_entries.id > 0";
2670 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2671 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2672 $query_strategy_part = "ttrss_entries.id > 0";
2673 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2674 id = feed_id) as feed_title,";
2675 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2676
2677 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
0a6c4846
AD
2678
2679 $tmp_result = false;
2680
2681 if ($cat_view) {
2682 $tmp_result = db_query($link, "SELECT id
2683 FROM ttrss_feeds WHERE cat_id = '$feed'");
2684 } else {
2685 $tmp_result = db_query($link, "SELECT id
2686 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2687 WHERE id = '$feed') AND id != '$feed'");
2688 }
ef393de7
AD
2689
2690 $cat_siblings = array();
2691
2692 if (db_num_rows($tmp_result) > 0) {
2693 while ($p = db_fetch_assoc($tmp_result)) {
2694 array_push($cat_siblings, "feed_id = " . $p["id"]);
2695 }
2696
2697 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2698 $feed, implode(" OR ", $cat_siblings));
2699
2700 } else {
2701 $query_strategy_part = "ttrss_entries.id > 0";
2702 }
2703
2704 } else if ($feed >= 0) {
2705
2706 if ($cat_view) {
5c365f60 2707
ef393de7
AD
2708 if ($feed > 0) {
2709 $query_strategy_part = "cat_id = '$feed'";
2710 } else {
2711 $query_strategy_part = "cat_id IS NULL";
2712 }
2713
2714 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
5c365f60 2715
ef393de7
AD
2716 } else {
2717 $tmp_result = db_query($link, "SELECT id
2718 FROM ttrss_feeds WHERE parent_feed = '$feed'
2719 ORDER BY cat_id,title");
2720
2721 $parent_ids = array();
2722
2723 if (db_num_rows($tmp_result) > 0) {
2724 while ($p = db_fetch_assoc($tmp_result)) {
2725 array_push($parent_ids, "feed_id = " . $p["id"]);
2726 }
2727
2728 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2729 $feed, implode(" OR ", $parent_ids));
2730
2731 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2732 } else {
2733 $query_strategy_part = "feed_id = '$feed'";
2734 }
2735 }
2736 } else if ($feed == -1) { // starred virtual feed
2737 $query_strategy_part = "marked = true";
2738 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
e4f4b46f
AD
2739 } else if ($feed == -2) { // published virtual feed
2740 $query_strategy_part = "published = true";
2d24f032
AD
2741 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2742 } else if ($feed == -3) { // fresh virtual feed
2743 $query_strategy_part = "unread = true";
2744
c1d7e6c3
AD
2745 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
2746
2d24f032 2747 if (DB_TYPE == "pgsql") {
c1d7e6c3 2748 $query_strategy_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
2d24f032 2749 } else {
c1d7e6c3 2750 $query_strategy_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2d24f032
AD
2751 }
2752
e4f4b46f 2753 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
ef393de7
AD
2754 } else if ($feed <= -10) { // labels
2755 $label_id = -$feed - 11;
2756
2757 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2758 WHERE id = '$label_id'");
2759
2760 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
3de0261a
AD
2761
2762 if (!$query_strategy_part) {
2763 return false;
2764 }
2765
ef393de7
AD
2766 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2767 } else {
2768 $query_strategy_part = "id > 0"; // dumb
2769 }
d6e5706d
AD
2770
2771 if (get_pref($link, 'REVERSE_HEADLINES')) {
2772 $order_by = "updated";
2773 } else {
2774 $order_by = "updated DESC";
2775 }
e939722a
AD
2776
2777 if ($override_order) {
2778 $order_by = $override_order;
2779 }
ef393de7
AD
2780
2781 $feed_title = "";
2782
2783 if ($search && $search_mode == "all_feeds") {
b36e002f 2784 $feed_title = __("Search results")." ($search)";
ef393de7 2785 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
b36e002f 2786 $feed_title = __("Search results")." ($search, $feed)";
ef393de7
AD
2787 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2788 $feed_title = $feed;
2789 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2790
2791 if ($cat_view) {
5c365f60 2792
ef393de7
AD
2793 if ($feed != 0) {
2794 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
c36bf4d5 2795 WHERE id = '$feed' AND owner_uid = $owner_uid");
ef393de7
AD
2796 $feed_title = db_fetch_result($result, 0, "title");
2797 } else {
d1db26aa 2798 $feed_title = __("Uncategorized");
ef393de7 2799 }
e1eb2147
AD
2800
2801 if ($search) {
b36e002f 2802 $feed_title = __("Searched for")." $search ($feed_title)";
e1eb2147
AD
2803 }
2804
ef393de7
AD
2805 } else {
2806
2807 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
c36bf4d5 2808 WHERE id = '$feed' AND owner_uid = $owner_uid");
ef393de7
AD
2809
2810 $feed_title = db_fetch_result($result, 0, "title");
2811 $feed_site_url = db_fetch_result($result, 0, "site_url");
2812 $last_error = db_fetch_result($result, 0, "last_error");
e1eb2147
AD
2813
2814 if ($search) {
b36e002f 2815 $feed_title = __("Searched for") . " $search ($feed_title)";
e1eb2147 2816 }
ef393de7
AD
2817 }
2818
2819 } else if ($feed == -1) {
d1db26aa 2820 $feed_title = __("Starred articles");
e4f4b46f
AD
2821 } else if ($feed == -2) {
2822 $feed_title = __("Published articles");
2d24f032
AD
2823 } else if ($feed == -3) {
2824 $feed_title = __("Fresh articles");
ef393de7
AD
2825 } else if ($feed < -10) {
2826 $label_id = -$feed - 11;
2827 $result = db_query($link, "SELECT description FROM ttrss_labels
2828 WHERE id = '$label_id'");
2829 $feed_title = db_fetch_result($result, 0, "description");
88040f57
AD
2830
2831 if ($search) {
b36e002f 2832 $feed_title = __("Searched for") . " $search ($feed_title)";
88040f57 2833 }
ef393de7
AD
2834 } else {
2835 $feed_title = "?";
2836 }
2837
ef393de7
AD
2838 if ($feed < -10) error_reporting (0);
2839
2840 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2841
2842 if ($feed >= 0) {
2843 $feed_kind = "Feeds";
2844 } else {
2845 $feed_kind = "Labels";
2846 }
2847
2848 $content_query_part = "content as content_preview,";
203de776 2849
95a82c08
AD
2850 if ($limit_query_part) {
2851 $offset_query_part = "OFFSET $offset";
2852 }
2853
ef393de7 2854 $query = "SELECT
1f64b1be 2855 guid,
ef393de7 2856 ttrss_entries.id,ttrss_entries.title,
46921916 2857 updated,
e4f4b46f 2858 unread,feed_id,marked,published,link,last_read,
ef393de7
AD
2859 SUBSTRING(last_read,1,19) as last_read_noms,
2860 $vfeed_query_part
2861 $content_query_part
d4b4b9de
AD
2862 SUBSTRING(updated,1,19) as updated_noms,
2863 author
ef393de7
AD
2864 FROM
2865 ttrss_entries,ttrss_user_entries,ttrss_feeds
2866 WHERE
cfb02131 2867 ttrss_feeds.hidden = false AND
ef393de7
AD
2868 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2869 ttrss_user_entries.ref_id = ttrss_entries.id AND
c36bf4d5 2870 ttrss_user_entries.owner_uid = '$owner_uid' AND
ef393de7
AD
2871 $search_query_part
2872 $view_query_part
2873 $query_strategy_part ORDER BY $order_by
95a82c08 2874 $limit_query_part $offset_query_part";
ef393de7
AD
2875
2876 $result = db_query($link, $query);
2877
2878 if ($_GET["debug"]) print $query;
2879
2880 } else {
2881 // browsing by tag
2882
2883 $feed_kind = "Tags";
2884
2885 $result = db_query($link, "SELECT
1f64b1be 2886 guid,
ef393de7 2887 ttrss_entries.id as id,title,
46921916 2888 updated,
ef393de7
AD
2889 unread,feed_id,
2890 marked,link,last_read,
2891 SUBSTRING(last_read,1,19) as last_read_noms,
2892 $vfeed_query_part
2893 $content_query_part
2894 SUBSTRING(updated,1,19) as updated_noms
2895 FROM
2896 ttrss_entries,ttrss_user_entries,ttrss_tags
2897 WHERE
2898 ref_id = ttrss_entries.id AND
c36bf4d5 2899 ttrss_user_entries.owner_uid = '$owner_uid' AND
ef393de7
AD
2900 post_int_id = int_id AND tag_name = '$feed' AND
2901 $view_query_part
2902 $search_query_part
2903 $query_strategy_part ORDER BY $order_by
2904 $limit_query_part");
2905 }
2906
c7188969 2907 return array($result, $feed_title, $feed_site_url, $last_error);
ef393de7
AD
2908
2909 }
2910
c36bf4d5 2911 function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat,
3baeeeca 2912 $search, $search_mode, $match_on) {
18664970
AD
2913
2914 $qfh_ret = queryFeedHeadlines($link, $feed,
c36bf4d5
AD
2915 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC", 0,
2916 $owner_uid);
18664970
AD
2917
2918 $result = $qfh_ret[0];
59e2aab4 2919 $feed_title = htmlspecialchars($qfh_ret[1]);
18664970
AD
2920 $feed_site_url = $qfh_ret[2];
2921 $last_error = $qfh_ret[3];
2922
a5472764 2923// if (!$feed_site_url) $feed_site_url = "http://localhost/";
4bc64807 2924
a5472764
AD
2925 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>
2926 <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?>
2927 <rss version=\"2.0\">
3baeeeca
AD
2928 <channel>
2929 <title>$feed_title</title>
4bc64807
AD
2930 <link>$feed_site_url</link>
2931 <description>Feed generated by Tiny Tiny RSS</description>";
3baeeeca
AD
2932
2933 while ($line = db_fetch_assoc($result)) {
2934 print "<item>";
4bc64807 2935 print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>";
3baeeeca 2936 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
0c3d1c68 2937
bc976a8c 2938 $tags = get_article_tags($link, $line["id"], $owner_uid);
0c3d1c68
AD
2939
2940 foreach ($tags as $tag) {
2941 print "<category>" . htmlspecialchars($tag) . "</category>";
2942 }
2943
3baeeeca
AD
2944 $rfc822_date = date('r', strtotime($line["updated"]));
2945
2946 print "<pubDate>$rfc822_date</pubDate>";
2947
2948 print "<title>" .
2949 htmlspecialchars($line["title"]) . "</title>";
2950
0c3d1c68
AD
2951 print "<description><![CDATA[" .
2952 $line["content_preview"] . "]]></description>";
3baeeeca
AD
2953
2954 print "</item>";
2955 }
2956
2957 print "</channel></rss>";
18664970
AD
2958
2959 }
2960
0a6c4846
AD
2961 function getCategoryTitle($link, $cat_id) {
2962
bba7c4bf
AD
2963 if ($cat_id == -1) {
2964 return __("Special");
2965 } else if ($cat_id == -2) {
2966 return __("Labels");
0a6c4846 2967 } else {
bba7c4bf
AD
2968
2969 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2970 id = '$cat_id'");
2971
2972 if (db_num_rows($result) == 1) {
2973 return db_fetch_result($result, 0, "title");
2974 } else {
2975 return "Uncategorized";
2976 }
0a6c4846
AD
2977 }
2978 }
2979
f738aef1
AD
2980 // http://ru2.php.net/strip-tags
2981
2982 function strip_tags_long($textstring, $allowed){
2983 while($textstring != strip_tags($textstring, $allowed))
2984 {
2985 while (strlen($textstring) != 0)
2986 {
2987 if (strlen($textstring) > 1024) {
2988 $otherlen = 1024;
2989 } else {
2990 $otherlen = strlen($textstring);
2991 }
2992 $temptext = strip_tags(substr($textstring,0,$otherlen), $allowed);
2993 $safetext .= $temptext;
2994 $textstring = substr_replace($textstring,'',0,$otherlen);
2995 }
2996 $textstring = $safetext;
2997 }
2998 return $textstring;
2999 }
3000
3001
1ac0baf4 3002 function sanitize_rss($link, $str, $force_strip_tags = false) {
60452879 3003 $res = $str;
183ad07b 3004
1ac0baf4 3005 if (get_pref($link, "STRIP_UNSAFE_TAGS") || $force_strip_tags) {
f738aef1
AD
3006 global $tw_parser;
3007 global $tw_paranoya_setup;
3008
3009 $res = $tw_parser->strip_tags($res, $tw_paranoya_setup);
3010
3011// $res = preg_replace("/\r\n|\n|\r/", "", $res);
3012// $res = strip_tags_long($res, "<p><a><i><em><b><strong><blockquote><br><img><div><span>");
f826eee1
AD
3013 }
3014
183ad07b
AD
3015 return $res;
3016 }
b72c3ef8 3017
9cd7c995
AD
3018 function send_headlines_digests($link, $limit = 100) {
3019
1ddba275
AD
3020 if (!DIGEST_ENABLE) return false;
3021
9cd7c995 3022 $user_limit = DIGEST_EMAIL_LIMIT;
5430c959 3023 $days = 1;
9cd7c995
AD
3024
3025 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
3026
3027 if (DB_TYPE == "pgsql") {
3028 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
3029 } else if (DB_TYPE == "mysql") {
3030 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
3031 }
3032
3033 $result = db_query($link, "SELECT id,email FROM ttrss_users
3034 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
3035
3036 while ($line = db_fetch_assoc($result)) {
3037 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
3038 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
3039
3040 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
3041 $digest = $tuple[0];
3042 $headlines_count = $tuple[1];
3043
3044 if ($headlines_count > 0) {
3045 $rc = mail($line["login"] . " <" . $line["email"] . ">",
3046 "[tt-rss] New headlines for last 24 hours", $digest,
3ab3c1f0
AD
3047 "From: " . MAIL_FROM . "\n".
3048 "Content-Type: text/plain; charset=\"utf-8\"\n".
3049 "Content-Transfer-Encoding: 8bit\n");
9cd7c995
AD
3050 print "RC=$rc\n";
3051 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
3052 WHERE id = " . $line["id"]);
3053 } else {
3054 print "No headlines\n";
3055 }
3056 }
3057 }
3058
3059// $digest = prepare_headlines_digest($link, $user_id, $days, $limit);
3060
3061 }
3062
7e3634d9 3063 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
d1db26aa 3064 $tmp = __("New headlines for last 24 hours, as of ") . date("Y/m/d H:m") . "\n";
7e3634d9
AD
3065 $tmp .= "=======================================================\n\n";
3066
3067 if (DB_TYPE == "pgsql") {
9cd7c995 3068 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
7e3634d9
AD
3069 } else if (DB_TYPE == "mysql") {
3070 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
3071 }
3072
3073 $result = db_query($link, "SELECT ttrss_entries.title,
3074 ttrss_feeds.title AS feed_title,
3075 date_entered,
3076 link,
3077 SUBSTRING(last_updated,1,19) AS last_updated
3078 FROM
3079 ttrss_user_entries,ttrss_entries,ttrss_feeds
3080 WHERE
3081 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
3dd9183c 3082 AND include_in_digest = true
7e3634d9 3083 AND $interval_query
448b0abd 3084 AND ttrss_user_entries.owner_uid = $user_id
7e3634d9
AD
3085 AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC
3086 LIMIT $limit");
3087
3088 $cur_feed_title = "";
3089
9cd7c995
AD
3090 $headlines_count = db_num_rows($result);
3091
7e3634d9
AD
3092 while ($line = db_fetch_assoc($result)) {
3093 $updated = smart_date_time(strtotime($line["last_updated"]));
3094 $feed_title = $line["feed_title"];
3095
3096 if ($cur_feed_title != $feed_title) {
3097 $cur_feed_title = $feed_title;
3098
3099 $tmp .= "$feed_title\n\n";
3100 }
3101
3102 $tmp .= " * " . trim($line["title"]) . " - $updated\n";
3103 $tmp .= " " . trim($line["link"]) . "\n";
3104 $tmp .= "\n";
3105 }
3106
3107 $tmp .= "--- \n";
d1db26aa 3108 $tmp .= __("You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at ") .
dfe6f833 3109 DIGEST_HOSTNAME . "\n".
d1db26aa 3110 __("To unsubscribe, visit your configuration options or contact instance owner.\n");
7e3634d9
AD
3111
3112
9cd7c995 3113 return array($tmp, $headlines_count);
7e3634d9
AD
3114 }
3115
d9fa39f1 3116 function check_for_update($link, $brief_fmt = true) {
b72c3ef8
AD
3117 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
3118
3119 if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) {
3120 return;
3121 }
3122
3123 error_reporting(0);
3124 $rss = fetch_rss($releases_feed);
3125 error_reporting (DEFAULT_ERROR_LEVEL);
3126
3127 if ($rss) {
3128
3129 $items = $rss->items;
3130
3131 if (!$items || !is_array($items)) $items = $rss->entries;
3132 if (!$items || !is_array($items)) $items = $rss;
3133
da412ad3 3134 if (!is_array($items) || count($items) == 0) {
b72c3ef8 3135 return;
da412ad3 3136 }
b72c3ef8 3137
a41d2c65 3138 $latest_item = $items[0];
b72c3ef8 3139
a41d2c65 3140 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $latest_item["title"]));
b72c3ef8 3141
007a38d4
AD
3142 $release_url = sanitize_rss($link, $latest_item["link"]);
3143 $content = sanitize_rss($link, $latest_item["description"]);
48e1a342 3144
a41d2c65 3145 if (version_compare(VERSION, $latest_version) == -1) {
d9fa39f1 3146 if ($brief_fmt) {
0d32b41e 3147 return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\">
d9fa39f1 3148 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
0d32b41e 3149 <div id=\"milestoneDetails\">$content</div>");
d9fa39f1 3150 } else {
92625568
AD
3151 return "New version of Tiny-Tiny RSS ($latest_version) is available:
3152 <div class='milestoneDetails'>$content</div>
3153 Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for
3154 download and update information.";
d9fa39f1
AD
3155 }
3156
da412ad3 3157 }
b72c3ef8
AD
3158 }
3159 }
472782e8 3160
18eddb2c
AD
3161 function markArticlesById($link, $ids, $cmode) {
3162
3163 $tmp_ids = array();
3164
3165 foreach ($ids as $id) {
3166 array_push($tmp_ids, "ref_id = '$id'");
3167 }
3168
3169 $ids_qpart = join(" OR ", $tmp_ids);
3170
3171 if ($cmode == 0) {
3172 db_query($link, "UPDATE ttrss_user_entries SET
3173 marked = false,last_read = NOW()
3174 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3175 } else if ($cmode == 1) {
3176 db_query($link, "UPDATE ttrss_user_entries SET
3177 marked = true
3178 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3179 } else {
3180 db_query($link, "UPDATE ttrss_user_entries SET
3181 marked = NOT marked,last_read = NOW()
3182 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3183 }
3184 }
3185
e4f4b46f
AD
3186 function publishArticlesById($link, $ids, $cmode) {
3187
3188 $tmp_ids = array();
3189
3190 foreach ($ids as $id) {
3191 array_push($tmp_ids, "ref_id = '$id'");
3192 }
3193
3194 $ids_qpart = join(" OR ", $tmp_ids);
3195
3196 if ($cmode == 0) {
3197 db_query($link, "UPDATE ttrss_user_entries SET
3198 published = false,last_read = NOW()
3199 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3200 } else if ($cmode == 1) {
3201 db_query($link, "UPDATE ttrss_user_entries SET
3202 published = true
3203 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3204 } else {
3205 db_query($link, "UPDATE ttrss_user_entries SET
3206 published = NOT published,last_read = NOW()
3207 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3208 }
3209 }
3210
472782e8
AD
3211 function catchupArticlesById($link, $ids, $cmode) {
3212
3213 $tmp_ids = array();
3214
3215 foreach ($ids as $id) {
3216 array_push($tmp_ids, "ref_id = '$id'");
3217 }
3218
3219 $ids_qpart = join(" OR ", $tmp_ids);
3220
3221 if ($cmode == 0) {
3222 db_query($link, "UPDATE ttrss_user_entries SET
3223 unread = false,last_read = NOW()
3224 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3225 } else if ($cmode == 1) {
3226 db_query($link, "UPDATE ttrss_user_entries SET
3227 unread = true
3228 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3229 } else {
3230 db_query($link, "UPDATE ttrss_user_entries SET
3231 unread = NOT unread,last_read = NOW()
3232 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3233 }
3234 }
3235
e097e8be
AD
3236 function catchupArticleById($link, $id, $cmode) {
3237
3238 if ($cmode == 0) {
3239 db_query($link, "UPDATE ttrss_user_entries SET
3240 unread = false,last_read = NOW()
3241 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3242 } else if ($cmode == 1) {
3243 db_query($link, "UPDATE ttrss_user_entries SET
3244 unread = true
3245 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3246 } else {
3247 db_query($link, "UPDATE ttrss_user_entries SET
3248 unread = NOT unread,last_read = NOW()
3249 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3250 }
3251 }
3252
1f64b1be
AD
3253 function make_guid_from_title($title) {
3254 return preg_replace("/[ \"\',.:;]/", "-",
fefef828 3255 mb_strtolower(strip_tags($title), 'utf-8'));
1f64b1be
AD
3256 }
3257
11befbb2
AD
3258 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
3259 $bottom = false, $rtl_content = false, $feed_id = 0,
3260 $is_cat = false, $search = false, $match_on = false,
95a82c08 3261 $search_mode = false, $offset = 0, $limit = 0) {
11befbb2 3262
e6c115b2
AD
3263 $user_page_offset = $offset + 1;
3264
11befbb2
AD
3265 if (!$bottom) {
3266 $class = "headlinesSubToolbar";
3267 $tid = "headlineActionsTop";
3268 } else {
3269 $class = "headlinesSubToolbar";
3270 $tid = "headlineActionsBottom";
3271 }
3272
3273 print "<table class=\"$class\" id=\"$tid\"
3274 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
3275
3276 if ($rtl_content) {
3277 $rtl_cpart = "RTL";
3278 } else {
3279 $rtl_cpart = "";
3280 }
3281
e6c115b2
AD
3282 $page_prev_link = "javascript:viewFeedGoPage(-1)";
3283 $page_next_link = "javascript:viewFeedGoPage(1)";
3284 $page_first_link = "javascript:viewFeedGoPage(0)";
203de776 3285
eb28b131
AD
3286 $catchup_page_link = "javascript:catchupPage()";
3287 $catchup_feed_link = "javascript:catchupCurrentFeed()";
a5ae125a 3288 $catchup_sel_link = "javascript:catchupSelection()";
c6008b62 3289
11befbb2
AD
3290 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3291
c6008b62
AD
3292 $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
3293 $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
3294 $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
11befbb2 3295
c6008b62
AD
3296 $tog_unread_link = "javascript:selectionToggleUnread()";
3297 $tog_marked_link = "javascript:selectionToggleMarked()";
e4f4b46f 3298 $tog_published_link = "javascript:selectionTogglePublished()";
11befbb2 3299
c6008b62 3300 } else {
11befbb2 3301
c6008b62
AD
3302 $sel_all_link = "javascript:cdmSelectArticles('all')";
3303 $sel_unread_link = "javascript:cdmSelectArticles('unread')";
3304 $sel_none_link = "javascript:cdmSelectArticles('none')";
3305
3306 $tog_unread_link = "javascript:selectionToggleUnread(true)";
3307 $tog_marked_link = "javascript:selectionToggleMarked(true)";
e4f4b46f 3308 $tog_published_link = "javascript:selectionTogglePublished(true)";
c6008b62
AD
3309
3310 }
3311
d420f2ee 3312 if (strpos($_SESSION["client.userAgent"], "MSIE") === false) {
c6008b62 3313
2dd2c13b
AD
3314 print "<td class=\"headlineActions$rtl_cpart\">
3315 <ul class=\"headlineDropdownMenu\">
3316 <li class=\"top2\">
3692e98f 3317 ".__('Select:')."
1025ad87
AD
3318 <a href=\"$sel_all_link\">".__('All')."</a>,
3319 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3320 <a href=\"$sel_none_link\">".__('None')."</a></li>
2dd2c13b 3321 <li class=\"vsep\">&nbsp;</li>
89cb787e 3322 <li class=\"top\">".__('Toggle')."<ul>
9cc600d1 3323 <li onclick=\"$tog_unread_link\">".__('Unread')."</li>
e4f4b46f
AD
3324 <li onclick=\"$tog_marked_link\">".__('Starred')."</li>
3325 <li onclick=\"$tog_published_link\">".__('Published')."</li>
3326 </ul></li>
2dd2c13b 3327 <li class=\"vsep\">&nbsp;</li>
1025ad87 3328 <li class=\"top\"><a href=\"$catchup_page_link\">".__('Mark as read')."</a><ul>
a5ae125a 3329 <li onclick=\"$catchup_sel_link\">".__('Selection')."</li>
8be83f42
AD
3330 <!-- <li onclick=\"$catchup_page_link\">".__('This page')."</li> -->
3331 <li><span class=\"insensitive\">--------</span></li>
3332 <li onclick=\"catchupRelativeToArticle(0)\">".__("Above active article")."</li>
3333 <li onclick=\"catchupRelativeToArticle(1)\">".__("Below active article")."</li>
3334 <li><span class=\"insensitive\">--------</span></li>
1025ad87 3335 <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li>
237ec2ad 3336 ";
95a82c08 3337
237ec2ad
AD
3338 $enable_pagination = get_pref($link, "_PREFS_ENABLE_PAGINATION");
3339
3340 if ($limit != 0 && !$search && $enable_pagination) {
95a82c08 3341 print "
237ec2ad 3342 <li class=\"vsep\">&nbsp;</li>
1025ad87
AD
3343 <li class=\"top\"><a href=\"$page_next_link\">".__('Next page')."</a><ul>
3344 <li onclick=\"$page_prev_link\">".__('Previous page')."</li>
3345 <li onclick=\"$page_first_link\">".__('First page')."</li></ul></li>
95a82c08 3346 </ul>";
d420f2ee 3347 }
95a82c08 3348
d420f2ee 3349 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
237ec2ad
AD
3350 print "
3351 <li class=\"vsep\">&nbsp;</li>
3352 <li class=\"top3\">
d420f2ee
AD
3353 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3354 '$match_on', '$feed_id', '$is_cat');\">
b36e002f 3355 ".__('Convert to label')."</a></td>";
d420f2ee 3356 }
95a82c08 3357 print "
2dd2c13b
AD
3358 </td>";
3359
3360 } else {
e6c115b2
AD
3361 // old style subtoolbar:
3362
2dd2c13b 3363 print "<td class=\"headlineActions$rtl_cpart\">".
d1db26aa 3364 __('Select:')."
1025ad87
AD
3365 <a href=\"$sel_all_link\">".__('All')."</a>,
3366 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3367 <a href=\"$sel_none_link\">".__('None')."</a>
2dd2c13b 3368 &nbsp;&nbsp;".
1025ad87
AD
3369 __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>,
3370 <a href=\"$tog_marked_link\">".__('Starred')."</a>
2dd2c13b 3371 &nbsp;&nbsp;".
d1db26aa 3372 __('Mark as read:')."
1025ad87
AD
3373 <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
3374 <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
d420f2ee
AD
3375
3376 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
3377
3378 print "&nbsp;&nbsp;
3379 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3380 '$match_on', '$feed_id', '$is_cat');\">
b36e002f 3381 ".__('Convert to label')."</a>";
d420f2ee
AD
3382 }
3383
2dd2c13b
AD
3384 print "</td>";
3385
3386 }
c6008b62 3387
d420f2ee 3388/* if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
c6008b62
AD
3389 print "<td class=\"headlineActions$rtl_cpart\">
3390 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3391 '$match_on', '$feed_id', '$is_cat');\">
d1db26aa 3392 ".__('Convert to Label')."</a></td>";
d420f2ee 3393} */
11befbb2
AD
3394
3395 print "<td class=\"headlineTitle$rtl_cpart\">";
3396
3397 if ($feed_site_url) {
3398 if (!$bottom) {
3fc2eed5 3399 $target = "target=\"_new\"";
11befbb2 3400 }
02815555
AD
3401 print "<a $target href=\"$feed_site_url\">".
3402 truncate_string($feed_title,30)."</a>";
11befbb2
AD
3403 } else {
3404 print $feed_title;
3405 }
3406
3407 if ($search) {
3408 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
3409 }
3410
e6c115b2
AD
3411 if ($user_page_offset > 1) {
3412 print " [$user_page_offset] ";
3413 }
3414
11befbb2 3415 if (!$bottom) {
e6c115b2 3416 print "
11befbb2
AD
3417 <a target=\"_new\"
3418 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
3419 <img class=\"noborder\"
1025ad87 3420 alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
11befbb2
AD
3421 </a>";
3422 }
3423
3424 print "</td>";
3425 print "</tr></table>";
3426
3427 }
3428
bba7c4bf
AD
3429 function printCategoryHeader($link, $cat_id, $hidden = false, $can_browse = true) {
3430
3431 $tmp_category = getCategoryTitle($link, $cat_id);
3432 $cat_unread = getCategoryUnread($link, $cat_id);
3433
3434 if ($hidden) {
3435 $holder_style = "display:none;";
3436 $ellipsis = "...";
3437 } else {
3438 $holder_style = "";
3439 $ellipsis = "";
3440 }
3441
3442 $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
3443
3444 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
3445 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>";
3446
3447 if ($can_browse) {
3448 print "<a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">";
3449 } else {
3450 print "<span id=\"FCAP-$cat_id\">";
3451 }
3452
3453 print " <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\"
3454 class=\"$catctr_class\">($cat_unread)</span> $ellipsis";
3455
3456 if ($can_browse) {
3457 print "</a>";
3458 } else {
3459 print "</span>";
3460 }
3461
3462 print "</li>";
3463
3464 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
3465 }
3466
f407c086
AD
3467 function outputFeedList($link, $tags = false) {
3468
3bd9a780 3469 print "<ul class=\"feedList\" id=\"feedList\">";
f407c086
AD
3470
3471 $owner_uid = $_SESSION["uid"];
3472
cf4d339c 3473 /* virtual feeds */
f407c086 3474
cf4d339c 3475 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3b086176
AD
3476
3477 if ($_COOKIE["ttrss_vf_vclps"] == 1) {
bba7c4bf 3478 $cat_hidden = true;
3b086176 3479 } else {
bba7c4bf 3480 $cat_hidden = false;
3b086176
AD
3481 }
3482
3483# print "<li class=\"feedCat\">".__('Special')."</li>";
3484# print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";
bba7c4bf
AD
3485# print "<li class=\"feedCat\">".
3486# "<a id=\"FCATN--1\" href=\"javascript:toggleCollapseCat(-1)\">".
3487# __('Special')."</a> <span id='FCAP--1'>$ellipsis</span></li>";
3488#
3489# print "<li id=\"feedCatHolder\" class=\"feedCatHolder\">
3490# <ul class=\"feedCatList\" id='FCATLIST--1' style='$holder_style'>";
3b086176 3491
bba7c4bf
AD
3492# $cat_unread = getCategoryUnread($link, -1);
3493# $tmp_category = __("Special");
3494# $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
3b086176 3495
bba7c4bf 3496 printCategoryHeader($link, -1, $cat_hidden, false);
cf4d339c 3497 }
f407c086 3498
cf4d339c 3499 $num_starred = getFeedUnread($link, -1);
e4f4b46f 3500 $num_published = getFeedUnread($link, -2);
2d24f032
AD
3501 $num_fresh = getFeedUnread($link, -3);
3502
3503 $class = "virt";
3504
3505 if ($num_fresh > 0) $class .= "Unread";
3506
3507 printFeedEntry(-3, $class, __("Fresh articles"), $num_fresh,
3508 "images/fresh.png", $link);
f407c086 3509
cf4d339c 3510 $class = "virt";
f407c086 3511
cf4d339c 3512 if ($num_starred > 0) $class .= "Unread";
f407c086 3513
d1db26aa 3514 printFeedEntry(-1, $class, __("Starred articles"), $num_starred,
cf4d339c 3515 "images/mark_set.png", $link);
f407c086 3516
e4f4b46f
AD
3517 $class = "virt";
3518
3519 if ($num_published > 0) $class .= "Unread";
3520
3521 printFeedEntry(-2, $class, __("Published articles"), $num_published,
3522 "images/pub_set.png", $link);
3523
cf4d339c 3524 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3bd9a780 3525 print "</ul>";
cf4d339c 3526 }
f407c086 3527
cf4d339c 3528 if (!$tags) {
f407c086
AD
3529
3530 if (GLOBAL_ENABLE_LABELS && get_pref($link, 'ENABLE_LABELS')) {
3531
3532 $result = db_query($link, "SELECT id,sql_exp,description FROM
3533 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
3534
3535 if (db_num_rows($result) > 0) {
3536 if (get_pref($link, 'ENABLE_FEED_CATS')) {
bd64489f
AD
3537
3538 if ($_COOKIE["ttrss_vf_lclps"] == 1) {
bba7c4bf 3539 $cat_hidden = true;
bd64489f 3540 } else {
bba7c4bf 3541 $cat_hidden = false;
bd64489f
AD
3542 }
3543
bba7c4bf 3544 printCategoryHeader($link, -2, $cat_hidden, false);
bd64489f 3545
bba7c4bf
AD
3546# print "<li class=\"feedCat\">".
3547# "<a id=\"FCATN--2\" href=\"javascript:toggleCollapseCat(-2)\">".
3548# __('Labels')."</a> <span id='FCAP--2'>$ellipsis</span></li>";
3549#
3550# print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\" id='FCATLIST--2' style='$holder_style'>";
f407c086 3551 } else {
3bd9a780 3552 print "<li><hr></li>";
f407c086
AD
3553 }
3554 }
3555
3556 while ($line = db_fetch_assoc($result)) {
3557
3558 error_reporting (0);
3559
3560 $label_id = -$line['id'] - 11;
3561 $count = getFeedUnread($link, $label_id);
3562
3563 $class = "label";
3564
3565 if ($count > 0) {
3566 $class .= "Unread";
3567 }
3568
3569 error_reporting (DEFAULT_ERROR_LEVEL);
3570
3571 printFeedEntry($label_id,
47439031 3572 $class, $line["description"],
f407c086
AD
3573 $count, "images/label.png", $link);
3574
3575 }
3576
3577 if (db_num_rows($result) > 0) {
3578 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3579 print "</ul>";
3580 }
3581 }
3582
3583 }
3584
3585 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
3bd9a780 3586 print "<li><hr></li>";
f407c086
AD
3587 }
3588
3589 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3590 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
3591 $order_by_qpart = "category,unread DESC,title";
3592 } else {
3593 $order_by_qpart = "category,title";
3594 }
3595 } else {
3596 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
3597 $order_by_qpart = "unread DESC,title";
3598 } else {
3599 $order_by_qpart = "title";
3600 }
3601 }
3602
3603 $result = db_query($link, "SELECT ttrss_feeds.*,
3604 SUBSTRING(last_updated,1,19) AS last_updated_noms,
3605 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
3606 WHERE feed_id = ttrss_feeds.id AND unread = true
3607 AND ttrss_user_entries.ref_id = ttrss_entries.id
3608 AND owner_uid = '$owner_uid') as unread,
3609 cat_id,last_error,
3610 ttrss_feed_categories.title AS category,
3611 ttrss_feed_categories.collapsed
3612 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
3613 ON (ttrss_feed_categories.id = cat_id)
3614 WHERE
3615 ttrss_feeds.hidden = false AND
3616 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
3617 ORDER BY $order_by_qpart");
3618
3619 $actid = $_GET["actid"];
3620
3621 /* real feeds */
3622
3623 $lnum = 0;
3624
3625 $total_unread = 0;
3626
3627 $category = "";
3628
3629 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
3630
3631 while ($line = db_fetch_assoc($result)) {
3632
47439031 3633 $feed = trim($line["title"]);
0f39ae20
AD
3634
3635 if (!$feed) $feed = "[Untitled]";
3636
f407c086
AD
3637 $feed_id = $line["id"];
3638
3639 $subop = $_GET["subop"];
3640
3641 $unread = $line["unread"];
3642
3643 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
3644 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
3645 } else {
3646 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
3647 }
3648
3649 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
3650
3651 if ($rtl_content) {
3652 $rtl_tag = "dir=\"RTL\"";
3653 } else {
3654 $rtl_tag = "";
3655 }
3656
3657 $tmp_result = db_query($link,
3658 "SELECT id,COUNT(unread) AS unread
3659 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
3660 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
3661 WHERE parent_feed = '$feed_id' AND unread = true
3662 GROUP BY ttrss_feeds.id");
3663
3664 if (db_num_rows($tmp_result) > 0) {
3665 while ($l = db_fetch_assoc($tmp_result)) {
3666 $unread += $l["unread"];
3667 }
3668 }
3669
3670 $cat_id = $line["cat_id"];
3671
3672 $tmp_category = $line["category"];
3673
3674 if (!$tmp_category) {
d1db26aa 3675 $tmp_category = __("Uncategorized");
f407c086
AD
3676 }
3677
3678 // $class = ($lnum % 2) ? "even" : "odd";
3679
3680 if ($line["last_error"]) {
3681 $class = "error";
3682 } else {
3683 $class = "feed";
3684 }
3685
3686 if ($unread > 0) $class .= "Unread";
3687
3688 if ($actid == $feed_id) {
3689 $class .= "Selected";
3690 }
3691
3692 $total_unread += $unread;
3693
3694 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
3695
3696 if ($category) {
3697 print "</ul></li>";
3698 }
3699
3700 $category = $tmp_category;
3701
3702 $collapsed = $line["collapsed"];
3703
3704 // workaround for NULL category
d1db26aa 3705 if ($category == __("Uncategorized")) {
f407c086
AD
3706 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
3707 $collapsed = "t";
3708 }
3709 }
3710
3711 if ($collapsed == "t" || $collapsed == "1") {
225ec0d4
AD
3712 $holder_class = "feedCatHolder";
3713 $holder_style = "display:none;";
f407c086
AD
3714 $ellipsis = "...";
3715 } else {
be5b75da 3716 $holder_class = "feedCatHolder";
225ec0d4 3717 $holder_style = "";
f407c086
AD
3718 $ellipsis = "";
3719 }
3720
3721 $cat_id = sprintf("%d", $cat_id);
3722
3723 $cat_unread = getCategoryUnread($link, $cat_id);
67dabe1a
AD
3724
3725 $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
3726
3bd9a780 3727 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
439bbe63 3728 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
7210613a 3729 <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
67dabe1a
AD
3730 <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\"
3731 class=\"$catctr_class\">($cat_unread)</span> $ellipsis
3bd9a780 3732 </a></li>";
f407c086 3733
225ec0d4 3734 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
f407c086
AD
3735 }
3736
3737 printFeedEntry($feed_id, $class, $feed, $unread,
99e37e09 3738 ICONS_DIR."/$feed_id.ico", $link, $rtl_content,
f407c086
AD
3739 $last_updated, $line["last_error"]);
3740
3741 ++$lnum;
3742 }
3743
3744 if (db_num_rows($result) == 0) {
3bd9a780 3745 print "<li>".__('No feeds to display.')."</li>";
f407c086
AD
3746 }
3747
3748 } else {
3749
3750 // tags
3751
3752/* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
3753 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
3754 post_int_id = ttrss_user_entries.int_id AND
3755 unread = true AND ref_id = ttrss_entries.id
3756 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
3757 UNION
3758 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
3759 ORDER BY tag_name"); */
3760
3761 if (get_pref($link, 'ENABLE_FEED_CATS')) {
d1db26aa 3762 print "<li class=\"feedCat\">".__('Tags')."</li>";
f407c086
AD
3763 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
3764 }
3765
3766 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
3767 FROM ttrss_user_entries WHERE int_id = post_int_id
3768 AND unread = true)) AS count FROM ttrss_tags
ef1ac7c7
AD
3769 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
3770 ORDER BY count DESC LIMIT 50");
f407c086
AD
3771
3772 $tags = array();
3773
3774 while ($line = db_fetch_assoc($result)) {
3775 $tags[$line["tag_name"]] += $line["count"];
3776 }
3777
3778 foreach (array_keys($tags) as $tag) {
3779
3780 $unread = $tags[$tag];
3781
3782 $class = "tag";
3783
3784 if ($unread > 0) {
3785 $class .= "Unread";
3786 }
3787
3788 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
3789
3790 }
3791
3792 if (db_num_rows($result) == 0) {
3793 print "<li>No tags to display.</li>";
3794 }
3795
3796 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3bd9a780 3797 print "</ul>";
f407c086
AD
3798 }
3799
3800 }
3801
3802 print "</ul>";
3803
3804 }
3805
bc976a8c 3806 function get_article_tags($link, $id, $owner_uid = 0) {
0b126ac2
AD
3807
3808 $a_id = db_escape_string($id);
3809
bc976a8c
AD
3810 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
3811
0c3d1c68
AD
3812 $tmp_result = db_query($link, "SELECT DISTINCT tag_name,
3813 owner_uid as owner FROM
0b126ac2 3814 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
bc976a8c 3815 ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name");
0b126ac2
AD
3816
3817 $tags = array();
3818
3819 while ($tmp_line = db_fetch_assoc($tmp_result)) {
3820 array_push($tags, $tmp_line["tag_name"]);
3821 }
3822
3823 return $tags;
3824 }
3825
d62a3b63
AD
3826 function trim_value(&$value) {
3827 $value = trim($value);
3828 }
3829
3830 function trim_array($array) {
3831 $tmp = $array;
3832 array_walk($tmp, 'trim_value');
3833 return $tmp;
3834 }
3835
be832a1a 3836 function tag_is_valid($tag) {
ef063748
AD
3837 if ($tag == '') return false;
3838 if (preg_match("/^[0-9]*$/", $tag)) return false;
3839
3840 $tag = iconv("utf-8", "utf-8", $tag);
3841 if (!$tag) return false;
3842
3843 return true;
be832a1a
AD
3844 }
3845
793185a9
AD
3846 function render_login_form($link, $mobile = false) {
3847 if (!$mobile) {
3848 require_once "login_form.php";
3849 } else {
3850 require_once "mobile/login_form.php";
3851 }
01a87dff
AD
3852 }
3853
dc56b3b7
AD
3854 // from http://developer.apple.com/internet/safari/faq.html
3855 function no_cache_incantation() {
3856 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
3857 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
3858 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
3859 header("Cache-Control: post-check=0, pre-check=0", false);
3860 header("Pragma: no-cache"); // HTTP/1.0
3861 }
3862
42395d28
AD
3863 function format_warning($msg, $id = "") {
3864 return "<div class=\"warning\" id=\"$id\">
0d32b41e
AD
3865 <img src=\"images/sign_excl.png\">$msg</div>";
3866 }
3867
3868 function format_notice($msg) {
3869 return "<div class=\"notice\">
3870 <img src=\"images/sign_info.png\">$msg</div>";
3871 }
3872
68d2f95e
AD
3873 function format_error($msg) {
3874 return "<div class=\"error\">
3875 <img src=\"images/sign_excl.png\">$msg</div>";
3876 }
3877
4dccf1ed
AD
3878 function print_notice($msg) {
3879 return print format_notice($msg);
3880 }
3881
3882 function print_warning($msg) {
3883 return print format_warning($msg);
3884 }
3885
68d2f95e
AD
3886 function print_error($msg) {
3887 return print format_error($msg);
3888 }
3889
3890
4dccf1ed
AD
3891 function T_sprintf() {
3892 $args = func_get_args();
3893 return vsprintf(__(array_shift($args)), $args);
3894 }
3895
3de0261a
AD
3896 function outputArticleXML($link, $id, $feed_id, $mark_as_read = true) {
3897
10eb9da8
AD
3898 /* we can figure out feed_id from article id anyway, why do we
3899 * pass feed_id here? */
3900
3901 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
3902 WHERE ref_id = '$id'");
3903
3904 $feed_id = db_fetch_result($result, 0, "feed_id");
3905
3de0261a
AD
3906 print "<article id='$id'><![CDATA[";
3907
3908 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
3909 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
3910
3911 if (db_num_rows($result) == 1) {
3912 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
3913 } else {
3914 $rtl_content = false;
3915 }
3916
3917 if ($rtl_content) {
3918 $rtl_tag = "dir=\"RTL\"";
3919 $rtl_class = "RTL";
3920 } else {
3921 $rtl_tag = "";
3922 $rtl_class = "";
3923 }
3924
3925 if ($mark_as_read) {
3926 $result = db_query($link, "UPDATE ttrss_user_entries
3927 SET unread = false,last_read = NOW()
3928 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3929 }
3930
3931 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
3932 SUBSTRING(updated,1,16) as updated,
3933 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
3934 num_comments,
3935 author
3936 FROM ttrss_entries,ttrss_user_entries
3937 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
3938
3939 if ($result) {
3940
3941 $link_target = "";
3942
3943 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
3944 $link_target = "target=\"_new\"";
3945 }
3946
3947 $line = db_fetch_assoc($result);
3948
3949 if ($line["icon_url"]) {
3950 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
3951 } else {
3952 $feed_icon = "&nbsp;";
3953 }
3954
3955/* if ($line["comments"] && $line["link"] != $line["comments"]) {
3956 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
3957 } else {
3958 $entry_comments = "";
3959 } */
3960
3961 $num_comments = $line["num_comments"];
3962 $entry_comments = "";
3963
3964 if ($num_comments > 0) {
3965 if ($line["comments"]) {
3966 $comments_url = $line["comments"];
3967 } else {
3968 $comments_url = $line["link"];
3969 }
3970 $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>";
3971 } else {
3972 if ($line["comments"] && $line["link"] != $line["comments"]) {
3973 $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>";
3974 }
3975 }
3976
3977 print "<div class=\"postReply\">";
3978
3979 print "<div class=\"postHeader\">";
3980
3981 $entry_author = $line["author"];
3982
3983 if ($entry_author) {
3984 $entry_author = __(" - by ") . $entry_author;
3985 }
3986
3987 $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'),
3988 strtotime($line["updated"]));
3989
3990 print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
3991
3992 if ($line["link"]) {
3993 print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" .
06202d88 3994 $line["title"] . "</a><span class='author'>$entry_author</span></div>";
3de0261a
AD
3995 } else {
3996 print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
3997 }
3998
9cfd409d 3999/* $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
3de0261a 4000 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
9cfd409d
AD
4001 ORDER BY tag_name"); */
4002
4003 $tags = get_article_tags($link, $id);
3de0261a
AD
4004
4005 $tags_str = "";
4006 $f_tags_str = "";
4007
4008 $num_tags = 0;
4009
9cfd409d 4010 foreach ($tags as $tag) {
3de0261a 4011 $num_tags++;
14b6c54b
AD
4012 $tag_escaped = str_replace("'", "\\'", $tag);
4013
4014 $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>, ";
3de0261a
AD
4015
4016 if ($num_tags == 6) {
e7544143
AD
4017 $tags_str .= "...";
4018
3de0261a
AD
4019 } else if ($num_tags < 6) {
4020 $tags_str .= $tag_str;
4021 }
4022 $f_tags_str .= $tag_str;
4023 }
4024
4025 $tags_str = preg_replace("/, $/", "", $tags_str);
4026 $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
4027
e7544143
AD
4028 $all_tags_div = "<span class='cdmAllTagsCtr'>...<div class='cdmAllTags'>All Tags: $f_tags_str</div></span>";
4029 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
4030
3de0261a
AD
4031 if (!$entry_comments) $entry_comments = "&nbsp;"; # placeholder
4032
4033 if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>';
4034
5f014cf1
AD
4035 print "<div style='float : right'>
4036 <img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'>
4037 $tags_str
3de0261a
AD
4038 <a title=\"Edit tags for this article\"
4039 href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div>
4040 <div clear='both'>$entry_comments</div>";
4041
4042 print "</div>";
4043
4044 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
4045 print "<div class=\"postContent\">";
4046
e7544143 4047 #print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>";
3de0261a 4048
c41890b0
AD
4049 $line["content"] = sanitize_rss($link, $line["content"]);
4050
3de0261a
AD
4051 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4052 $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]);
4053 }
4054
3de0261a
AD
4055 print $line["content"] . "</div>";
4056
4057 print "</div>";
4058
4059 }
4060
4061 print "]]></article>";
4062
4063 }
4064
4065 function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view,
4066 $next_unread_feed, $offset) {
4067
46921916
AD
4068 $timing_info = getmicrotime();
4069
961f4c73
AD
4070 $topmost_article_ids = array();
4071
ac541432
AD
4072 if (!$offset) {
4073 $offset = 0;
4074 }
3de0261a
AD
4075
4076 if ($subop == "undefined") $subop = "";
4077
4078 if ($subop == "CatchupSelected") {
4079 $ids = split(",", db_escape_string($_GET["ids"]));
4080 $cmode = sprintf("%d", $_GET["cmode"]);
4081
4082 catchupArticlesById($link, $ids, $cmode);
4083 }
4084
4085 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
4086 update_generic_feed($link, $feed, $cat_view);
4087 }
4088
4089 if ($subop == "MarkAllRead") {
4090 catchup_feed($link, $feed, $cat_view);
4091
4092 if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
4093 if ($next_unread_feed) {
4094 $feed = $next_unread_feed;
4095 }
4096 }
4097 }
4098
4099 if ($feed_id > 0) {
4100 $result = db_query($link,
4101 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
4102
4103 if (db_num_rows($result) == 0) {
4104 print "<div align='center'>".__('Feed not found.')."</div>";
4105 return;
4106 }
4107 }
4108
4109 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
10eb9da8 4110
3de0261a
AD
4111 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
4112 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
4113
4114 if (db_num_rows($result) == 1) {
4115 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
4116 } else {
4117 $rtl_content = false;
4118 }
4119
4120 if ($rtl_content) {
4121 $rtl_tag = "dir=\"RTL\"";
4122 } else {
4123 $rtl_tag = "";
4124 }
4125 } else {
4126 $rtl_tag = "";
4127 $rtl_content = false;
4128 }
4129
4130 $script_dt_add = get_script_dt_add();
4131
4132 /// START /////////////////////////////////////////////////////////////////////////////////
4133
4134 $search = db_escape_string($_GET["query"]);
4135 $search_mode = db_escape_string($_GET["search_mode"]);
4136 $match_on = db_escape_string($_GET["match_on"]);
4137
4138 if (!$match_on) {
4139 $match_on = "both";
4140 }
4141
4142 $real_offset = $offset * $limit;
4143
46921916
AD
4144 if ($_GET["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
4145
3de0261a
AD
4146 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view,
4147 $search, $search_mode, $match_on, false, $real_offset);
4148
46921916
AD
4149 if ($_GET["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
4150
3de0261a
AD
4151 $result = $qfh_ret[0];
4152 $feed_title = $qfh_ret[1];
4153 $feed_site_url = $qfh_ret[2];
4154 $last_error = $qfh_ret[3];
f56e3080
AD
4155
4156 if ($feed == -2) {
4157 $feed_site_url = article_publish_url($link);
4158 }
4159
3de0261a
AD
4160 /// STOP //////////////////////////////////////////////////////////////////////////////////
4161
ac541432
AD
4162 if (!$offset) {
4163 print "<div id=\"headlinesContainer\" $rtl_tag>";
3de0261a 4164
ac541432
AD
4165 if (!$result) {
4166 print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
4167 return;
4168 }
3de0261a 4169
ac541432
AD
4170 print_headline_subtoolbar($link, $feed_site_url, $feed_title, false,
4171 $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode,
4172 $offset, $limit);
3de0261a 4173
ac541432
AD
4174 print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
4175 }
3de0261a
AD
4176
4177 if (db_num_rows($result) > 0) {
4178
4179# print "\{$offset}";
4180
ac541432 4181 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
3de0261a
AD
4182 print "<table class=\"headlinesList\" id=\"headlinesList\"
4183 cellspacing=\"0\">";
4184 }
4185
4186 $lnum = 0;
4187
4188 error_reporting (DEFAULT_ERROR_LEVEL);
4189
4190 $num_unread = 0;
4191
4192 while ($line = db_fetch_assoc($result)) {
4193
4194 $class = ($lnum % 2) ? "even" : "odd";
4195
4196 $id = $line["id"];
4197 $feed_id = $line["feed_id"];
961f4c73
AD
4198
4199 if (count($topmost_article_ids) < 5) {
4200 array_push($topmost_article_ids, $id);
4201 }
4202
3de0261a
AD
4203 if ($line["last_read"] == "" &&
4204 ($line["unread"] != "t" && $line["unread"] != "1")) {
4205
4206 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
4207 alt=\"Updated\">";
4208 } else {
4209 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
4210 alt=\"Updated\">";
4211 }
4212
4213 if ($line["unread"] == "t" || $line["unread"] == "1") {
4214 $class .= "Unread";
4215 ++$num_unread;
4216 $is_unread = true;
4217 } else {
4218 $is_unread = false;
4219 }
4220
4221 if ($line["marked"] == "t" || $line["marked"] == "1") {
67343d9f 4222 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.png\"
3de0261a 4223 class=\"markedPic\"
67343d9f 4224 alt=\"Reset mark\" onclick='javascript:tMark($id)'>";
3de0261a 4225 } else {
67343d9f 4226 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.png\"
3de0261a 4227 class=\"markedPic\"
67343d9f 4228 alt=\"Set mark\" onclick='javascript:tMark($id)'>";
3de0261a
AD
4229 }
4230
e4f4b46f
AD
4231 if ($line["published"] == "t" || $line["published"] == "1") {
4232 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_set.png\"
4233 class=\"markedPic\"
4234 alt=\"Unpublish\" onclick='javascript:tPub($id)'>";
4235 } else {
4236 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_unset.png\"
4237 class=\"markedPic\"
4238 alt=\"Publish\" onclick='javascript:tPub($id)'>";
4239 }
4240
3de0261a
AD
4241# $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" .
4242# $line["title"] . "</a>";
4243
4244 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
4245 $line["title"] . "</a>";
4246
4247# $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
4248# $line["title"] . "</a>";
4249
4250 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
46921916 4251 $updated_fmt = smart_date_time(strtotime($line["updated_noms"]));
3de0261a
AD
4252 } else {
4253 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
46921916 4254 $updated_fmt = date($short_date, strtotime($line["updated_noms"]));
3de0261a
AD
4255 }
4256
4257 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
4258 $content_preview = truncate_string(strip_tags($line["content_preview"]),
4259 100);
4260 }
4261
4262 $entry_author = $line["author"];
4263
4264 if ($entry_author) {
4265 $entry_author = " - by $entry_author";
4266 }
4267
4268 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
4269
4270 print "<tr class='$class' id='RROW-$id'>";
4271
67343d9f 4272 print "<td class='hlUpdPic'>$update_pic</td>";
3de0261a
AD
4273
4274 print "<td class='hlSelectRow'>
67343d9f
AD
4275 <input type=\"checkbox\" onclick=\"tSR(this)\"
4276 id=\"RCHK-$id\">
3de0261a
AD
4277 </td>";
4278
4279 print "<td class='hlMarkedPic'>$marked_pic</td>";
e4f4b46f 4280 print "<td class='hlMarkedPic'>$published_pic</td>";
3de0261a
AD
4281
4282 if ($line["feed_title"]) {
4283 print "<td class='hlContent'>$content_link</td>";
4284 print "<td class='hlFeed'>
4285 <a href=\"javascript:viewfeed($feed_id, '', false)\">".
02815555 4286 truncate_string($line["feed_title"],30)."</a>&nbsp;</td>";
3de0261a
AD
4287 } else {
4288 print "<td class='hlContent' valign='middle'>";
4289
4290 print "<a href=\"javascript:view($id,$feed_id);\">" .
4291 $line["title"];
4292
4293 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
4294 if ($content_preview) {
4295 print "<span class=\"contentPreview\"> - $content_preview</span>";
4296 }
4297 }
4298
4299 print "</a>";
4300 print "</td>";
4301 }
4302
4303 print "<td class=\"hlUpdated\"><nobr>$updated_fmt&nbsp;</nobr></td>";
4304
4305 print "</tr>";
4306
4307 } else {
4308
4309 if ($is_unread) {
4310 $add_class = "Unread";
4311 } else {
4312 $add_class = "";
4313 }
4314
e4914b62
AD
4315 print "<div class=\"cdmArticle$add_class\"
4316 id=\"RROW-$id\" onmouseover='cdmMouseIn(this)'
4317 onmouseout='cdmMouseOut(this)'>";
3de0261a
AD
4318
4319 print "<div class=\"cdmHeader\">";
4320
4321 print "<div class=\"articleUpdated\">$updated_fmt</div>";
4322
4323 print "<a class=\"title\"
4324 onclick=\"javascript:toggleUnread($id, 0)\"
3fc2eed5 4325 target=\"_new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
3de0261a
AD
4326
4327 print $entry_author;
4328
4329 if ($line["feed_title"]) {
4330 print "&nbsp;(<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
4331 }
4332
4333 print "</div>";
4334
3fc2eed5
AD
4335 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4336 $line["content_preview"] = preg_replace("/href=/i",
4337 "target=\"_new\" href=", $line["content_preview"]);
4338 }
4339
3de0261a
AD
4340 print "<div class=\"cdmContent\">" . $line["content_preview"] . "</div><br clear=\"all\">";
4341
e2ccbfab 4342 print "<div class=\"cdmFooter\"><span class='s0'>";
3de0261a 4343
e2ccbfab 4344 /* print "<div class=\"markedPic\">Star it: $marked_pic</div>"; */
3de0261a 4345
e2ccbfab
AD
4346 print __("Select:").
4347 " <input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
3de0261a
AD
4348 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">";
4349
e2ccbfab 4350 print "</span><span class='s1'>$marked_pic</span> ";
e4f4b46f 4351 print "<span class='s1'>$published_pic</span> ";
e2ccbfab 4352
3de0261a
AD
4353 $tags = get_article_tags($link, $id);
4354
4355 $tags_str = "";
22d1f3db 4356 $full_tags_str = "";
d735ebd2 4357 $num_tags = 0;
3de0261a
AD
4358
4359 foreach ($tags as $tag) {
4360 $num_tags++;
22d1f3db
AD
4361 $full_tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
4362 if ($num_tags < 5) {
4363 $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
4364 } else if ($num_tags == 5) {
4365 $tags_str .= "...";
4366 }
3de0261a
AD
4367 }
4368
4369 $tags_str = preg_replace("/, $/", "", $tags_str);
22d1f3db
AD
4370 $full_tags_str = preg_replace("/, $/", "", $full_tags_str);
4371
4372 $all_tags_div = "<span class='cdmAllTagsCtr'>...<div class='cdmAllTags'>All Tags: $full_tags_str</div></span>";
4373
4374 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
4375
3de0261a
AD
4376
4377 if ($tags_str == "") $tags_str = "no tags";
e2ccbfab
AD
4378
4379// print "<img src='images/tag.png' class='markedPic'>";
4380
5f014cf1
AD
4381 print "<span class='s1'>
4382 <img class='tagsPic' src='images/tag.png' alt='Tags'
4383 title='Tags'> $tags_str <a title=\"Edit tags for this article\"
3de0261a
AD
4384 href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>";
4385
e2ccbfab 4386 print "</span>";
3de0261a 4387
e2ccbfab
AD
4388 print "<span class='s2'>Toggle: <a class=\"cdmToggleLink\"
4389 href=\"javascript:toggleUnread($id)\">
4390 Unread</a></span>";
3de0261a 4391
e2ccbfab 4392 print "</div>";
3de0261a
AD
4393 print "</div>";
4394
4395 }
4396
4397 ++$lnum;
4398 }
4399
ac541432 4400 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
3de0261a
AD
4401 print "</table>";
4402 }
4403
4404// print_headline_subtoolbar($link,
4405// "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
4406
4407
4408 } else {
ac541432 4409 if (!$offset) print "<div class='whiteBox'>".__('No articles found.')."</div>";
3de0261a
AD
4410 }
4411
ac541432
AD
4412 if (!$offset) {
4413 print "</div>";
4414 print "</div>";
4415 }
3de0261a 4416
961f4c73 4417 return $topmost_article_ids;
3de0261a 4418 }
0979b696
AD
4419
4420// from here: http://www.roscripts.com/Create_tag_cloud-71.html
4421
4422 function printTagCloud($link) {
35a03bdd
AD
4423
4424 /* get first ref_id to count from */
4425
dcac082b
AD
4426 /*
4427
35a03bdd
AD
4428 $query = "";
4429
4430 if (DB_TYPE == "pgsql") {
4431 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
4432 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
4433 AND date_entered > NOW() - INTERVAL '30 days'";
4434 } else {
4435 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
4436 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
4437 AND date_entered > DATE_SUB(NOW(), INTERVAL 30 DAY)";
4438 }
4439
4440 $result = db_query($link, $query);
dcac082b 4441 $first_id = db_fetch_result($result, 0, "id"); */
35a03bdd 4442
dcac082b 4443 //AND post_int_id >= '$first_id'
0979b696
AD
4444 $query = "SELECT tag_name, COUNT(post_int_id) AS count
4445 FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]."
b31af972 4446 GROUP BY tag_name ORDER BY count DESC LIMIT 50";
0979b696
AD
4447
4448 $result = db_query($link, $query);
4449
4450 $tags = array();
4451
4452 while ($line = db_fetch_assoc($result)) {
4453 $tags[$line["tag_name"]] = $line["count"];
4454 }
4455
4456 ksort($tags);
4457
4458 $max_size = 32; // max font size in pixels
4548a580 4459 $min_size = 11; // min font size in pixels
0979b696
AD
4460
4461 // largest and smallest array values
4462 $max_qty = max(array_values($tags));
4463 $min_qty = min(array_values($tags));
4464
4465 // find the range of values
4466 $spread = $max_qty - $min_qty;
4467 if ($spread == 0) { // we don't want to divide by zero
4468 $spread = 1;
4469 }
4470
4471 // set the font-size increment
4472 $step = ($max_size - $min_size) / ($spread);
4473
4474 // loop through the tag array
4475 foreach ($tags as $key => $value) {
4476 // calculate font-size
4477 // find the $value in excess of $min_qty
4478 // multiply by the font-size increment ($size)
4479 // and add the $min_size set above
4480 $size = round($min_size + (($value - $min_qty) * $step));
546ffab4
AD
4481
4482 $key_escaped = str_replace("'", "\\'", $key);
4483
4484 echo "<a href=\"javascript:viewfeed('$key_escaped') \" style=\"font-size: " .
0979b696
AD
4485 $size . "px\" title=\"$value articles tagged with " .
4486 $key . '">' . $key . '</a> ';
4487 }
4488 }
46921916
AD
4489
4490 function print_checkpoint($n, $s) {
4491 $ts = getmicrotime();
4492 echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s);
4493 return $ts;
4494 }
14b6c54b
AD
4495
4496 function sanitize_tag($tag) {
4497 $tag = trim($tag);
4498
4499 $tag = mb_strtolower($tag, 'utf-8');
4500
0c3d1c68
AD
4501 $tag = preg_replace('/[\"\+\>\<]/', "", $tag);
4502
4503// $tag = str_replace('"', "", $tag);
4504// $tag = str_replace("+", " ", $tag);
14b6c54b
AD
4505 $tag = str_replace("technorati tag: ", "", $tag);
4506
4507 return $tag;
4508 }
e4f4b46f
AD
4509
4510 function generate_publish_key() {
4511 return sha1(uniqid(rand(), true));
4512 }
4513
f56e3080
AD
4514 function article_publish_url($link) {
4515
17a756d1 4516 $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
f56e3080
AD
4517
4518 $url_path .= "?op=publish&key=" . get_pref($link, "_PREFS_PUBLISH_KEY");
4519
4520 return $url_path;
4521 }
4522
40d13c28 4523?>