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