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