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