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