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