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