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