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