]> git.wh0rd.org - tt-rss.git/blame - functions.php
schema: fix typo
[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
793185a9 1195 function login_sequence($link, $mobile = false) {
b8aa49bc 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)) {
793185a9 1230 render_login_form($link, $mobile);
01a87dff 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
ac7bcd71
AD
2112 print "<param key=\"cdm_auto_catchup\" value=\"" .
2113 sprintf("%d", get_pref($link, "CDM_AUTO_CATCHUP")) . "\"/>";
2114
3ac2b520
AD
2115 print "</init-params>";
2116 }
f54f515f
AD
2117
2118 function print_runtime_info($link) {
2119 print "<runtime-info>";
71ad883b
AD
2120 if (ENABLE_UPDATE_DAEMON) {
2121 print "<param key=\"daemon_is_running\" value=\"".
2122 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
2123 }
d9fa39f1
AD
2124 if (CHECK_FOR_NEW_VERSION && $_SESSION["access_level"] >= 10) {
2125
2126 if ($_SESSION["last_version_check"] + 600 < time()) {
2127 $new_version_details = check_for_update($link);
2128
2129 print "<param key=\"new_version_available\" value=\"".
2130 sprintf("%d", $new_version_details != ""). "\"/>";
2131
2132 $_SESSION["last_version_check"] = time();
2133 }
2134 }
2135
f54f515f
AD
2136 print "</runtime-info>";
2137 }
ef393de7 2138
88040f57 2139 function getSearchSql($search, $match_on) {
ef393de7 2140
88040f57 2141 $search_query_part = "";
e20c9d88 2142
88040f57
AD
2143 $keywords = split(" ", $search);
2144 $query_keywords = array();
e20c9d88 2145
88040f57 2146 if ($match_on == "both") {
e20c9d88 2147
88040f57
AD
2148 foreach ($keywords as $k) {
2149 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2150 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2151 }
e20c9d88 2152
88040f57 2153 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 2154
88040f57 2155 } else if ($match_on == "title") {
e20c9d88 2156
88040f57
AD
2157 foreach ($keywords as $k) {
2158 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2159 }
e20c9d88 2160
88040f57 2161 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 2162
88040f57
AD
2163 } else if ($match_on == "content") {
2164
2165 foreach ($keywords as $k) {
2166 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2167 }
2168 }
2169
2170 $search_query_part = implode("AND", $query_keywords);
2171
2172 return $search_query_part;
2173 }
2174
c1a0b534
AD
2175 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0) {
2176
88040f57
AD
2177 if ($search) {
2178
2179 $search_query_part = getSearchSql($search, $match_on);
2180 $search_query_part .= " AND ";
e20c9d88 2181
ef393de7
AD
2182 } else {
2183 $search_query_part = "";
2184 }
2185
2186 $view_query_part = "";
2187
2188 if ($view_mode == "adaptive") {
2189 if ($search) {
2190 $view_query_part = " ";
2191 } else if ($feed != -1) {
f295c368 2192 $unread = getFeedUnread($link, $feed, $cat_view);
ef393de7
AD
2193 if ($unread > 0) {
2194 $view_query_part = " unread = true AND ";
2195 }
2196 }
2197 }
2198
2199 if ($view_mode == "marked") {
2200 $view_query_part = " marked = true AND ";
2201 }
2202
2203 if ($view_mode == "unread") {
2204 $view_query_part = " unread = true AND ";
2205 }
2206
2207 if ($limit > 0) {
2208 $limit_query_part = "LIMIT " . $limit;
2209 }
2210
2211 $vfeed_query_part = "";
2212
2213 // override query strategy and enable feed display when searching globally
2214 if ($search && $search_mode == "all_feeds") {
2215 $query_strategy_part = "ttrss_entries.id > 0";
2216 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2217 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2218 $query_strategy_part = "ttrss_entries.id > 0";
2219 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2220 id = feed_id) as feed_title,";
2221 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2222
2223 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
0a6c4846
AD
2224
2225 $tmp_result = false;
2226
2227 if ($cat_view) {
2228 $tmp_result = db_query($link, "SELECT id
2229 FROM ttrss_feeds WHERE cat_id = '$feed'");
2230 } else {
2231 $tmp_result = db_query($link, "SELECT id
2232 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2233 WHERE id = '$feed') AND id != '$feed'");
2234 }
ef393de7
AD
2235
2236 $cat_siblings = array();
2237
2238 if (db_num_rows($tmp_result) > 0) {
2239 while ($p = db_fetch_assoc($tmp_result)) {
2240 array_push($cat_siblings, "feed_id = " . $p["id"]);
2241 }
2242
2243 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2244 $feed, implode(" OR ", $cat_siblings));
2245
2246 } else {
2247 $query_strategy_part = "ttrss_entries.id > 0";
2248 }
2249
2250 } else if ($feed >= 0) {
2251
2252 if ($cat_view) {
5c365f60 2253
ef393de7
AD
2254 if ($feed > 0) {
2255 $query_strategy_part = "cat_id = '$feed'";
2256 } else {
2257 $query_strategy_part = "cat_id IS NULL";
2258 }
2259
2260 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
5c365f60 2261
ef393de7
AD
2262 } else {
2263 $tmp_result = db_query($link, "SELECT id
2264 FROM ttrss_feeds WHERE parent_feed = '$feed'
2265 ORDER BY cat_id,title");
2266
2267 $parent_ids = array();
2268
2269 if (db_num_rows($tmp_result) > 0) {
2270 while ($p = db_fetch_assoc($tmp_result)) {
2271 array_push($parent_ids, "feed_id = " . $p["id"]);
2272 }
2273
2274 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2275 $feed, implode(" OR ", $parent_ids));
2276
2277 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2278 } else {
2279 $query_strategy_part = "feed_id = '$feed'";
2280 }
2281 }
2282 } else if ($feed == -1) { // starred virtual feed
2283 $query_strategy_part = "marked = true";
2284 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2285 } else if ($feed <= -10) { // labels
2286 $label_id = -$feed - 11;
2287
2288 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2289 WHERE id = '$label_id'");
2290
2291 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2292
2293 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2294 } else {
2295 $query_strategy_part = "id > 0"; // dumb
2296 }
d6e5706d
AD
2297
2298 if (get_pref($link, 'REVERSE_HEADLINES')) {
2299 $order_by = "updated";
2300 } else {
2301 $order_by = "updated DESC";
2302 }
e939722a
AD
2303
2304 if ($override_order) {
2305 $order_by = $override_order;
2306 }
ef393de7
AD
2307
2308 $feed_title = "";
2309
2310 if ($search && $search_mode == "all_feeds") {
d1db26aa 2311 $feed_title = __("Global search results")." ($search)";
ef393de7 2312 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
d1db26aa 2313 $feed_title = __("Tag search results")." ($search, $feed)";
ef393de7
AD
2314 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2315 $feed_title = $feed;
2316 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2317
2318 if ($cat_view) {
5c365f60 2319
ef393de7
AD
2320 if ($feed != 0) {
2321 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2322 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2323 $feed_title = db_fetch_result($result, 0, "title");
2324 } else {
d1db26aa 2325 $feed_title = __("Uncategorized");
ef393de7 2326 }
e1eb2147
AD
2327
2328 if ($search) {
d1db26aa 2329 $feed_title = __("Category search results")." ($search, $feed_title)";
e1eb2147
AD
2330 }
2331
ef393de7
AD
2332 } else {
2333
2334 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2335 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2336
2337 $feed_title = db_fetch_result($result, 0, "title");
2338 $feed_site_url = db_fetch_result($result, 0, "site_url");
2339 $last_error = db_fetch_result($result, 0, "last_error");
e1eb2147
AD
2340
2341 if ($search) {
d1db26aa 2342 $feed_title = __("Feed search results") . " ($search, $feed_title)";
e1eb2147 2343 }
ef393de7
AD
2344 }
2345
2346 } else if ($feed == -1) {
d1db26aa 2347 $feed_title = __("Starred articles");
ef393de7
AD
2348 } else if ($feed < -10) {
2349 $label_id = -$feed - 11;
2350 $result = db_query($link, "SELECT description FROM ttrss_labels
2351 WHERE id = '$label_id'");
2352 $feed_title = db_fetch_result($result, 0, "description");
88040f57
AD
2353
2354 if ($search) {
d1db26aa 2355 $feed_title = __("Label search results") . " ($search, $feed_title)";
88040f57 2356 }
ef393de7
AD
2357 } else {
2358 $feed_title = "?";
2359 }
2360
2361 $feed_title = db_unescape_string($feed_title);
2362
2363 if ($feed < -10) error_reporting (0);
2364
2365 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2366
2367 if ($feed >= 0) {
2368 $feed_kind = "Feeds";
2369 } else {
2370 $feed_kind = "Labels";
2371 }
2372
2373 $content_query_part = "content as content_preview,";
203de776 2374
95a82c08
AD
2375 if ($limit_query_part) {
2376 $offset_query_part = "OFFSET $offset";
2377 }
2378
ef393de7 2379 $query = "SELECT
1f64b1be 2380 guid,
ef393de7
AD
2381 ttrss_entries.id,ttrss_entries.title,
2382 SUBSTRING(updated,1,16) as updated,
2383 unread,feed_id,marked,link,last_read,
2384 SUBSTRING(last_read,1,19) as last_read_noms,
2385 $vfeed_query_part
2386 $content_query_part
d4b4b9de
AD
2387 SUBSTRING(updated,1,19) as updated_noms,
2388 author
ef393de7
AD
2389 FROM
2390 ttrss_entries,ttrss_user_entries,ttrss_feeds
2391 WHERE
cfb02131 2392 ttrss_feeds.hidden = false AND
ef393de7
AD
2393 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2394 ttrss_user_entries.ref_id = ttrss_entries.id AND
2395 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2396 $search_query_part
2397 $view_query_part
2398 $query_strategy_part ORDER BY $order_by
95a82c08 2399 $limit_query_part $offset_query_part";
ef393de7
AD
2400
2401 $result = db_query($link, $query);
2402
2403 if ($_GET["debug"]) print $query;
2404
2405 } else {
2406 // browsing by tag
2407
2408 $feed_kind = "Tags";
2409
2410 $result = db_query($link, "SELECT
1f64b1be 2411 guid,
ef393de7
AD
2412 ttrss_entries.id as id,title,
2413 SUBSTRING(updated,1,16) as updated,
2414 unread,feed_id,
2415 marked,link,last_read,
2416 SUBSTRING(last_read,1,19) as last_read_noms,
2417 $vfeed_query_part
2418 $content_query_part
2419 SUBSTRING(updated,1,19) as updated_noms
2420 FROM
2421 ttrss_entries,ttrss_user_entries,ttrss_tags
2422 WHERE
2423 ref_id = ttrss_entries.id AND
2424 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2425 post_int_id = int_id AND tag_name = '$feed' AND
2426 $view_query_part
2427 $search_query_part
2428 $query_strategy_part ORDER BY $order_by
2429 $limit_query_part");
2430 }
2431
c7188969 2432 return array($result, $feed_title, $feed_site_url, $last_error);
ef393de7
AD
2433
2434 }
2435
e1eb2147 2436 function generate_syndicated_feed($link, $feed, $is_cat,
3baeeeca 2437 $search, $search_mode, $match_on) {
18664970
AD
2438
2439 $qfh_ret = queryFeedHeadlines($link, $feed,
e939722a 2440 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC");
18664970
AD
2441
2442 $result = $qfh_ret[0];
59e2aab4 2443 $feed_title = htmlspecialchars($qfh_ret[1]);
18664970
AD
2444 $feed_site_url = $qfh_ret[2];
2445 $last_error = $qfh_ret[3];
2446
3baeeeca
AD
2447 print "<rss version=\"2.0\">
2448 <channel>
2449 <title>$feed_title</title>
2450 <link>$feed_site_url</link>
2451 <generator>Tiny Tiny RSS v".VERSION."</generator>";
2452
2453 while ($line = db_fetch_assoc($result)) {
2454 print "<item>";
2455 print "<id>" . htmlspecialchars($line["guid"]) . "</id>";
2456 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2457
2458 $rfc822_date = date('r', strtotime($line["updated"]));
2459
2460 print "<pubDate>$rfc822_date</pubDate>";
2461
2462 print "<title>" .
2463 htmlspecialchars($line["title"]) . "</title>";
2464
2465 print "<description>" .
2466 htmlspecialchars($line["content_preview"]) . "</description>";
2467
2468 print "</item>";
2469 }
2470
2471 print "</channel></rss>";
18664970
AD
2472
2473 }
2474
0a6c4846
AD
2475 function getCategoryTitle($link, $cat_id) {
2476
2477 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2478 id = '$cat_id'");
2479
2480 if (db_num_rows($result) == 1) {
2481 return db_fetch_result($result, 0, "title");
2482 } else {
2483 return "Uncategorized";
2484 }
2485 }
2486
183ad07b 2487 function sanitize_rss($str) {
60452879 2488 $res = $str;
183ad07b
AD
2489
2490 $res = preg_replace('/<script.*?>/i',
60452879 2491 "<p class=\"scriptWarn\">Disabled script: ", $res);
183ad07b 2492
60452879
AD
2493 $res = preg_replace('/<\/script.*?>/i', "</p>", $res);
2494
8511f62f 2495/* $res = preg_replace('/<embed.*?>/i', "", $res);
183ad07b 2496
a262b161
AD
2497 $res = preg_replace('/<object.*?>.*?<\/object>/i',
2498 "<p class=\"objectWarn\">(Disabled html object
8511f62f 2499 - flash or other embedded content)</p>", $res); */
a262b161 2500
183ad07b
AD
2501 return $res;
2502 }
b72c3ef8 2503
9cd7c995
AD
2504 function send_headlines_digests($link, $limit = 100) {
2505
1ddba275
AD
2506 if (!DIGEST_ENABLE) return false;
2507
9cd7c995 2508 $user_limit = DIGEST_EMAIL_LIMIT;
5430c959 2509 $days = 1;
9cd7c995
AD
2510
2511 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
2512
2513 if (DB_TYPE == "pgsql") {
2514 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
2515 } else if (DB_TYPE == "mysql") {
2516 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
2517 }
2518
2519 $result = db_query($link, "SELECT id,email FROM ttrss_users
2520 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
2521
2522 while ($line = db_fetch_assoc($result)) {
2523 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
2524 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
2525
2526 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
2527 $digest = $tuple[0];
2528 $headlines_count = $tuple[1];
2529
2530 if ($headlines_count > 0) {
2531 $rc = mail($line["login"] . " <" . $line["email"] . ">",
2532 "[tt-rss] New headlines for last 24 hours", $digest,
3ab3c1f0
AD
2533 "From: " . MAIL_FROM . "\n".
2534 "Content-Type: text/plain; charset=\"utf-8\"\n".
2535 "Content-Transfer-Encoding: 8bit\n");
9cd7c995
AD
2536 print "RC=$rc\n";
2537 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
2538 WHERE id = " . $line["id"]);
2539 } else {
2540 print "No headlines\n";
2541 }
2542 }
2543 }
2544
2545// $digest = prepare_headlines_digest($link, $user_id, $days, $limit);
2546
2547 }
2548
7e3634d9 2549 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
d1db26aa 2550 $tmp = __("New headlines for last 24 hours, as of ") . date("Y/m/d H:m") . "\n";
7e3634d9
AD
2551 $tmp .= "=======================================================\n\n";
2552
2553 if (DB_TYPE == "pgsql") {
9cd7c995 2554 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
7e3634d9
AD
2555 } else if (DB_TYPE == "mysql") {
2556 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
2557 }
2558
2559 $result = db_query($link, "SELECT ttrss_entries.title,
2560 ttrss_feeds.title AS feed_title,
2561 date_entered,
2562 link,
2563 SUBSTRING(last_updated,1,19) AS last_updated
2564 FROM
2565 ttrss_user_entries,ttrss_entries,ttrss_feeds
2566 WHERE
2567 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
3dd9183c 2568 AND include_in_digest = true
7e3634d9 2569 AND $interval_query
448b0abd 2570 AND ttrss_user_entries.owner_uid = $user_id
7e3634d9
AD
2571 AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC
2572 LIMIT $limit");
2573
2574 $cur_feed_title = "";
2575
9cd7c995
AD
2576 $headlines_count = db_num_rows($result);
2577
7e3634d9
AD
2578 while ($line = db_fetch_assoc($result)) {
2579 $updated = smart_date_time(strtotime($line["last_updated"]));
2580 $feed_title = $line["feed_title"];
2581
2582 if ($cur_feed_title != $feed_title) {
2583 $cur_feed_title = $feed_title;
2584
2585 $tmp .= "$feed_title\n\n";
2586 }
2587
2588 $tmp .= " * " . trim($line["title"]) . " - $updated\n";
2589 $tmp .= " " . trim($line["link"]) . "\n";
2590 $tmp .= "\n";
2591 }
2592
2593 $tmp .= "--- \n";
d1db26aa 2594 $tmp .= __("You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at ") .
dfe6f833 2595 DIGEST_HOSTNAME . "\n".
d1db26aa 2596 __("To unsubscribe, visit your configuration options or contact instance owner.\n");
7e3634d9
AD
2597
2598
9cd7c995 2599 return array($tmp, $headlines_count);
7e3634d9
AD
2600 }
2601
d9fa39f1 2602 function check_for_update($link, $brief_fmt = true) {
b72c3ef8
AD
2603 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
2604
2605 if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) {
2606 return;
2607 }
2608
2609 error_reporting(0);
2610 $rss = fetch_rss($releases_feed);
2611 error_reporting (DEFAULT_ERROR_LEVEL);
2612
2613 if ($rss) {
2614
2615 $items = $rss->items;
2616
2617 if (!$items || !is_array($items)) $items = $rss->entries;
2618 if (!$items || !is_array($items)) $items = $rss;
2619
da412ad3 2620 if (!is_array($items) || count($items) == 0) {
b72c3ef8 2621 return;
da412ad3 2622 }
b72c3ef8 2623
a41d2c65 2624 $latest_item = $items[0];
b72c3ef8 2625
a41d2c65 2626 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $latest_item["title"]));
b72c3ef8 2627
48e1a342
AD
2628 $release_url = sanitize_rss($latest_item["link"]);
2629 $content = sanitize_rss($latest_item["description"]);
2630
a41d2c65 2631 if (version_compare(VERSION, $latest_version) == -1) {
d9fa39f1 2632 if ($brief_fmt) {
0d32b41e 2633 return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\">
d9fa39f1 2634 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
0d32b41e 2635 <div id=\"milestoneDetails\">$content</div>");
d9fa39f1 2636 } else {
92625568
AD
2637 return "New version of Tiny-Tiny RSS ($latest_version) is available:
2638 <div class='milestoneDetails'>$content</div>
2639 Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for
2640 download and update information.";
d9fa39f1
AD
2641 }
2642
da412ad3 2643 }
b72c3ef8
AD
2644 }
2645 }
472782e8 2646
18eddb2c
AD
2647 function markArticlesById($link, $ids, $cmode) {
2648
2649 $tmp_ids = array();
2650
2651 foreach ($ids as $id) {
2652 array_push($tmp_ids, "ref_id = '$id'");
2653 }
2654
2655 $ids_qpart = join(" OR ", $tmp_ids);
2656
2657 if ($cmode == 0) {
2658 db_query($link, "UPDATE ttrss_user_entries SET
2659 marked = false,last_read = NOW()
2660 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2661 } else if ($cmode == 1) {
2662 db_query($link, "UPDATE ttrss_user_entries SET
2663 marked = true
2664 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2665 } else {
2666 db_query($link, "UPDATE ttrss_user_entries SET
2667 marked = NOT marked,last_read = NOW()
2668 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2669 }
2670 }
2671
472782e8
AD
2672 function catchupArticlesById($link, $ids, $cmode) {
2673
2674 $tmp_ids = array();
2675
2676 foreach ($ids as $id) {
2677 array_push($tmp_ids, "ref_id = '$id'");
2678 }
2679
2680 $ids_qpart = join(" OR ", $tmp_ids);
2681
2682 if ($cmode == 0) {
2683 db_query($link, "UPDATE ttrss_user_entries SET
2684 unread = false,last_read = NOW()
2685 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2686 } else if ($cmode == 1) {
2687 db_query($link, "UPDATE ttrss_user_entries SET
2688 unread = true
2689 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2690 } else {
2691 db_query($link, "UPDATE ttrss_user_entries SET
2692 unread = NOT unread,last_read = NOW()
2693 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2694 }
2695 }
2696
a262b161
AD
2697 function escape_for_form($s) {
2698 return htmlspecialchars(db_unescape_string($s));
2699 }
2700
1f64b1be
AD
2701 function make_guid_from_title($title) {
2702 return preg_replace("/[ \"\',.:;]/", "-",
2703 mb_strtolower(strip_tags($title)));
2704 }
2705
11befbb2
AD
2706 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
2707 $bottom = false, $rtl_content = false, $feed_id = 0,
2708 $is_cat = false, $search = false, $match_on = false,
95a82c08 2709 $search_mode = false, $offset = 0, $limit = 0) {
11befbb2 2710
e6c115b2
AD
2711 $user_page_offset = $offset + 1;
2712
11befbb2
AD
2713 if (!$bottom) {
2714 $class = "headlinesSubToolbar";
2715 $tid = "headlineActionsTop";
2716 } else {
2717 $class = "headlinesSubToolbar";
2718 $tid = "headlineActionsBottom";
2719 }
2720
2721 print "<table class=\"$class\" id=\"$tid\"
2722 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
2723
2724 if ($rtl_content) {
2725 $rtl_cpart = "RTL";
2726 } else {
2727 $rtl_cpart = "";
2728 }
2729
e6c115b2
AD
2730 $page_prev_link = "javascript:viewFeedGoPage(-1)";
2731 $page_next_link = "javascript:viewFeedGoPage(1)";
2732 $page_first_link = "javascript:viewFeedGoPage(0)";
203de776 2733
eb28b131
AD
2734 $catchup_page_link = "javascript:catchupPage()";
2735 $catchup_feed_link = "javascript:catchupCurrentFeed()";
c6008b62 2736
11befbb2
AD
2737 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
2738
c6008b62
AD
2739 $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
2740 $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
2741 $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
11befbb2 2742
c6008b62
AD
2743 $tog_unread_link = "javascript:selectionToggleUnread()";
2744 $tog_marked_link = "javascript:selectionToggleMarked()";
11befbb2 2745
c6008b62 2746 } else {
11befbb2 2747
c6008b62
AD
2748 $sel_all_link = "javascript:cdmSelectArticles('all')";
2749 $sel_unread_link = "javascript:cdmSelectArticles('unread')";
2750 $sel_none_link = "javascript:cdmSelectArticles('none')";
2751
2752 $tog_unread_link = "javascript:selectionToggleUnread(true)";
2753 $tog_marked_link = "javascript:selectionToggleMarked(true)";
2754
2755 }
2756
2dd2c13b 2757 if (!strstr($_SESSION["client.userAgent"], "MSIE")) {
c6008b62 2758
2dd2c13b
AD
2759 print "<td class=\"headlineActions$rtl_cpart\">
2760 <ul class=\"headlineDropdownMenu\">
2761 <li class=\"top2\">
2762 Select:
1025ad87
AD
2763 <a href=\"$sel_all_link\">".__('All')."</a>,
2764 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
2765 <a href=\"$sel_none_link\">".__('None')."</a></li>
2dd2c13b
AD
2766 <li class=\"vsep\">&nbsp;</li>
2767 <li class=\"top\">Selection<ul>
1025ad87
AD
2768 <li onclick=\"$tog_unread_link\">".__('Toggle unread')."</li>
2769 <li onclick=\"$tog_marked_link\">".__('Toggle starred')."</li></ul></li>
2dd2c13b 2770 <li class=\"vsep\">&nbsp;</li>
1025ad87
AD
2771 <li class=\"top\"><a href=\"$catchup_page_link\">".__('Mark as read')."</a><ul>
2772 <li onclick=\"$catchup_page_link\">".__('This page')."</li>
2773 <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li>
2774 <li class=\"vsep\">&nbsp;</li>";
95a82c08
AD
2775
2776 if ($limit != 0) {
2777 print "
1025ad87
AD
2778 <li class=\"top\"><a href=\"$page_next_link\">".__('Next page')."</a><ul>
2779 <li onclick=\"$page_prev_link\">".__('Previous page')."</li>
2780 <li onclick=\"$page_first_link\">".__('First page')."</li></ul></li>
95a82c08
AD
2781 </ul>";
2782 }
2783
2784 print "
2dd2c13b
AD
2785 </td>";
2786
2787 } else {
e6c115b2
AD
2788 // old style subtoolbar:
2789
2dd2c13b 2790 print "<td class=\"headlineActions$rtl_cpart\">".
d1db26aa 2791 __('Select:')."
1025ad87
AD
2792 <a href=\"$sel_all_link\">".__('All')."</a>,
2793 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
2794 <a href=\"$sel_none_link\">".__('None')."</a>
2dd2c13b 2795 &nbsp;&nbsp;".
1025ad87
AD
2796 __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>,
2797 <a href=\"$tog_marked_link\">".__('Starred')."</a>
2dd2c13b 2798 &nbsp;&nbsp;".
d1db26aa 2799 __('Mark as read:')."
1025ad87
AD
2800 <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
2801 <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
2dd2c13b
AD
2802 print "</td>";
2803
2804 }
c6008b62
AD
2805
2806 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
2807 print "<td class=\"headlineActions$rtl_cpart\">
2808 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
2809 '$match_on', '$feed_id', '$is_cat');\">
d1db26aa 2810 ".__('Convert to Label')."</a></td>";
11befbb2
AD
2811 }
2812
2813 print "<td class=\"headlineTitle$rtl_cpart\">";
2814
2815 if ($feed_site_url) {
2816 if (!$bottom) {
2817 $target = "target=\"_blank\"";
2818 }
2819 print "<a $target href=\"$feed_site_url\">$feed_title</a>";
2820 } else {
2821 print $feed_title;
2822 }
2823
2824 if ($search) {
2825 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
2826 }
2827
e6c115b2
AD
2828 if ($user_page_offset > 1) {
2829 print " [$user_page_offset] ";
2830 }
2831
11befbb2 2832 if (!$bottom) {
e6c115b2 2833 print "
11befbb2
AD
2834 <a target=\"_new\"
2835 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
2836 <img class=\"noborder\"
1025ad87 2837 alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
11befbb2
AD
2838 </a>";
2839 }
2840
2841 print "</td>";
2842 print "</tr></table>";
2843
2844 }
2845
f407c086
AD
2846 function outputFeedList($link, $tags = false) {
2847
2848 print "<ul class=\"feedList\" id=\"feedList\">\n";
2849
2850 $owner_uid = $_SESSION["uid"];
2851
cf4d339c 2852 /* virtual feeds */
f407c086 2853
cf4d339c 2854 if (get_pref($link, 'ENABLE_FEED_CATS')) {
d1db26aa 2855 print "<li class=\"feedCat\">".__('Special')."</li>";
cf4d339c
AD
2856 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2857 }
f407c086 2858
cf4d339c 2859 $num_starred = getFeedUnread($link, -1);
f407c086 2860
cf4d339c 2861 $class = "virt";
f407c086 2862
cf4d339c 2863 if ($num_starred > 0) $class .= "Unread";
f407c086 2864
d1db26aa 2865 printFeedEntry(-1, $class, __("Starred articles"), $num_starred,
cf4d339c 2866 "images/mark_set.png", $link);
f407c086 2867
cf4d339c
AD
2868 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2869 print "</ul>\n";
2870 }
f407c086 2871
cf4d339c 2872 if (!$tags) {
f407c086
AD
2873
2874 if (GLOBAL_ENABLE_LABELS && get_pref($link, 'ENABLE_LABELS')) {
2875
2876 $result = db_query($link, "SELECT id,sql_exp,description FROM
2877 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
2878
2879 if (db_num_rows($result) > 0) {
2880 if (get_pref($link, 'ENABLE_FEED_CATS')) {
d1db26aa 2881 print "<li class=\"feedCat\">".__('Labels')."</li>";
f407c086
AD
2882 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2883 } else {
2884 print "<li><hr></li>";
2885 }
2886 }
2887
2888 while ($line = db_fetch_assoc($result)) {
2889
2890 error_reporting (0);
2891
2892 $label_id = -$line['id'] - 11;
2893 $count = getFeedUnread($link, $label_id);
2894
2895 $class = "label";
2896
2897 if ($count > 0) {
2898 $class .= "Unread";
2899 }
2900
2901 error_reporting (DEFAULT_ERROR_LEVEL);
2902
2903 printFeedEntry($label_id,
2904 $class, db_unescape_string($line["description"]),
2905 $count, "images/label.png", $link);
2906
2907 }
2908
2909 if (db_num_rows($result) > 0) {
2910 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2911 print "</ul>";
2912 }
2913 }
2914
2915 }
2916
2917 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
2918 print "<li><hr></li>";
2919 }
2920
2921 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2922 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
2923 $order_by_qpart = "category,unread DESC,title";
2924 } else {
2925 $order_by_qpart = "category,title";
2926 }
2927 } else {
2928 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
2929 $order_by_qpart = "unread DESC,title";
2930 } else {
2931 $order_by_qpart = "title";
2932 }
2933 }
2934
2935 $result = db_query($link, "SELECT ttrss_feeds.*,
2936 SUBSTRING(last_updated,1,19) AS last_updated_noms,
2937 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
2938 WHERE feed_id = ttrss_feeds.id AND unread = true
2939 AND ttrss_user_entries.ref_id = ttrss_entries.id
2940 AND owner_uid = '$owner_uid') as unread,
2941 cat_id,last_error,
2942 ttrss_feed_categories.title AS category,
2943 ttrss_feed_categories.collapsed
2944 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
2945 ON (ttrss_feed_categories.id = cat_id)
2946 WHERE
2947 ttrss_feeds.hidden = false AND
2948 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
2949 ORDER BY $order_by_qpart");
2950
2951 $actid = $_GET["actid"];
2952
2953 /* real feeds */
2954
2955 $lnum = 0;
2956
2957 $total_unread = 0;
2958
2959 $category = "";
2960
2961 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2962
2963 while ($line = db_fetch_assoc($result)) {
2964
2965 $feed = db_unescape_string($line["title"]);
2966 $feed_id = $line["id"];
2967
2968 $subop = $_GET["subop"];
2969
2970 $unread = $line["unread"];
2971
2972 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2973 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
2974 } else {
2975 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
2976 }
2977
2978 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
2979
2980 if ($rtl_content) {
2981 $rtl_tag = "dir=\"RTL\"";
2982 } else {
2983 $rtl_tag = "";
2984 }
2985
2986 $tmp_result = db_query($link,
2987 "SELECT id,COUNT(unread) AS unread
2988 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
2989 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
2990 WHERE parent_feed = '$feed_id' AND unread = true
2991 GROUP BY ttrss_feeds.id");
2992
2993 if (db_num_rows($tmp_result) > 0) {
2994 while ($l = db_fetch_assoc($tmp_result)) {
2995 $unread += $l["unread"];
2996 }
2997 }
2998
2999 $cat_id = $line["cat_id"];
3000
3001 $tmp_category = $line["category"];
3002
3003 if (!$tmp_category) {
d1db26aa 3004 $tmp_category = __("Uncategorized");
f407c086
AD
3005 }
3006
3007 // $class = ($lnum % 2) ? "even" : "odd";
3008
3009 if ($line["last_error"]) {
3010 $class = "error";
3011 } else {
3012 $class = "feed";
3013 }
3014
3015 if ($unread > 0) $class .= "Unread";
3016
3017 if ($actid == $feed_id) {
3018 $class .= "Selected";
3019 }
3020
3021 $total_unread += $unread;
3022
3023 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
3024
3025 if ($category) {
3026 print "</ul></li>";
3027 }
3028
3029 $category = $tmp_category;
3030
3031 $collapsed = $line["collapsed"];
3032
3033 // workaround for NULL category
d1db26aa 3034 if ($category == __("Uncategorized")) {
f407c086
AD
3035 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
3036 $collapsed = "t";
3037 }
3038 }
3039
3040 if ($collapsed == "t" || $collapsed == "1") {
3041 $holder_class = "invisible";
3042 $ellipsis = "...";
3043 } else {
3044 $holder_class = "";
3045 $ellipsis = "";
3046 }
3047
3048 $cat_id = sprintf("%d", $cat_id);
3049
3050 $cat_unread = getCategoryUnread($link, $cat_id);
67dabe1a
AD
3051
3052 $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
3053
f407c086 3054 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
439bbe63 3055 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
7210613a 3056 <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
67dabe1a
AD
3057 <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\"
3058 class=\"$catctr_class\">($cat_unread)</span> $ellipsis
f407c086
AD
3059 </a></li>";
3060
3061 // !!! NO SPACE before <ul...feedCatList - breaks firstChild DOM function
3062 // -> keyboard navigation, etc.
3063 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\">";
3064 }
3065
3066 printFeedEntry($feed_id, $class, $feed, $unread,
3067 "icons/$feed_id.ico", $link, $rtl_content,
3068 $last_updated, $line["last_error"]);
3069
3070 ++$lnum;
3071 }
3072
3073 if (db_num_rows($result) == 0) {
d1db26aa 3074 print "<li>".__('No feeds to display.')."</li>";
f407c086
AD
3075 }
3076
3077 } else {
3078
3079 // tags
3080
3081/* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
3082 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
3083 post_int_id = ttrss_user_entries.int_id AND
3084 unread = true AND ref_id = ttrss_entries.id
3085 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
3086 UNION
3087 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
3088 ORDER BY tag_name"); */
3089
3090 if (get_pref($link, 'ENABLE_FEED_CATS')) {
d1db26aa 3091 print "<li class=\"feedCat\">".__('Tags')."</li>";
f407c086
AD
3092 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
3093 }
3094
3095 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
3096 FROM ttrss_user_entries WHERE int_id = post_int_id
3097 AND unread = true)) AS count FROM ttrss_tags
22e00732 3098 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name ORDER BY tag_name");
f407c086
AD
3099
3100 $tags = array();
3101
3102 while ($line = db_fetch_assoc($result)) {
3103 $tags[$line["tag_name"]] += $line["count"];
3104 }
3105
3106 foreach (array_keys($tags) as $tag) {
3107
3108 $unread = $tags[$tag];
3109
3110 $class = "tag";
3111
3112 if ($unread > 0) {
3113 $class .= "Unread";
3114 }
3115
3116 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
3117
3118 }
3119
3120 if (db_num_rows($result) == 0) {
3121 print "<li>No tags to display.</li>";
3122 }
3123
3124 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3125 print "</ul>\n";
3126 }
3127
3128 }
3129
3130 print "</ul>";
3131
3132 }
3133
0b126ac2
AD
3134 function get_article_tags($link, $id) {
3135
3136 $a_id = db_escape_string($id);
3137
3138 $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
3139 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
93135102 3140 ref_id = '$a_id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1) ORDER BY tag_name");
0b126ac2
AD
3141
3142 $tags = array();
3143
3144 while ($tmp_line = db_fetch_assoc($tmp_result)) {
3145 array_push($tags, $tmp_line["tag_name"]);
3146 }
3147
3148 return $tags;
3149 }
3150
d62a3b63
AD
3151 function trim_value(&$value) {
3152 $value = trim($value);
3153 }
3154
3155 function trim_array($array) {
3156 $tmp = $array;
3157 array_walk($tmp, 'trim_value');
3158 return $tmp;
3159 }
3160
be832a1a 3161 function tag_is_valid($tag) {
ef063748
AD
3162 if ($tag == '') return false;
3163 if (preg_match("/^[0-9]*$/", $tag)) return false;
3164
3165 $tag = iconv("utf-8", "utf-8", $tag);
3166 if (!$tag) return false;
3167
3168 return true;
be832a1a
AD
3169 }
3170
793185a9
AD
3171 function render_login_form($link, $mobile = false) {
3172 if (!$mobile) {
3173 require_once "login_form.php";
3174 } else {
3175 require_once "mobile/login_form.php";
3176 }
01a87dff
AD
3177 }
3178
dc56b3b7
AD
3179 // from http://developer.apple.com/internet/safari/faq.html
3180 function no_cache_incantation() {
3181 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
3182 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
3183 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
3184 header("Cache-Control: post-check=0, pre-check=0", false);
3185 header("Pragma: no-cache"); // HTTP/1.0
3186 }
3187
42395d28
AD
3188 function format_warning($msg, $id = "") {
3189 return "<div class=\"warning\" id=\"$id\">
0d32b41e
AD
3190 <img src=\"images/sign_excl.png\">$msg</div>";
3191 }
3192
3193 function format_notice($msg) {
3194 return "<div class=\"notice\">
3195 <img src=\"images/sign_info.png\">$msg</div>";
3196 }
3197
4dccf1ed
AD
3198 function print_notice($msg) {
3199 return print format_notice($msg);
3200 }
3201
3202 function print_warning($msg) {
3203 return print format_warning($msg);
3204 }
3205
659468eb
AD
3206 function startup_gettext() {
3207
3208 # Get locale from Accept-Language header
3209 $lang = al2gt(array("en_US", "ru_RU"), "text/html");
3210
3211 if ($lang) {
3212 _setlocale(LC_MESSAGES, $lang);
3213 _bindtextdomain("messages", "locale");
3214 _textdomain("messages");
3215 _bind_textdomain_codeset("messages", "UTF-8");
3216 }
3217 }
3218
4dccf1ed
AD
3219 function T_sprintf() {
3220 $args = func_get_args();
3221 return vsprintf(__(array_shift($args)), $args);
3222 }
3223
40d13c28 3224?>