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