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