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