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