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