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