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