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