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