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