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