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