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