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