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