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