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