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