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