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