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