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