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