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