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