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