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